xref: /linux/tools/net/ynl/tests/ovs.c (revision 91a4855d6c03e770e42f17c798a36a3c46e63de2)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include <ynl.h>
6 
7 #include <kselftest_harness.h>
8 
9 #include "ovs_datapath-user.h"
10 
11 static void ovs_print_datapath(struct __test_metadata *_metadata,
12 			       struct ovs_datapath_get_rsp *dp)
13 {
14 	EXPECT_TRUE((bool)dp->_len.name);
15 	if (!dp->_len.name)
16 		return;
17 
18 	EXPECT_TRUE((bool)dp->_hdr.dp_ifindex);
19 	ksft_print_msg("%s(%d): pid:%u cache:%u\n",
20 		       dp->name, dp->_hdr.dp_ifindex,
21 		       dp->upcall_pid, dp->masks_cache_size);
22 }
23 
24 FIXTURE(ovs)
25 {
26 	struct ynl_sock *ys;
27 	char *dp_name;
28 };
29 
30 FIXTURE_SETUP(ovs)
31 {
32 	self->ys = ynl_sock_create(&ynl_ovs_datapath_family, NULL);
33 	ASSERT_NE(NULL, self->ys)
34 		TH_LOG("failed to create OVS datapath socket");
35 }
36 
37 FIXTURE_TEARDOWN(ovs)
38 {
39 	if (self->dp_name) {
40 		struct ovs_datapath_del_req *req;
41 
42 		req = ovs_datapath_del_req_alloc();
43 		if (req) {
44 			ovs_datapath_del_req_set_name(req, self->dp_name);
45 			ovs_datapath_del(self->ys, req);
46 			ovs_datapath_del_req_free(req);
47 		}
48 	}
49 	ynl_sock_destroy(self->ys);
50 }
51 
52 TEST_F(ovs, crud)
53 {
54 	struct ovs_datapath_get_req_dump *dreq;
55 	struct ovs_datapath_new_req *new_req;
56 	struct ovs_datapath_get_list *dps;
57 	struct ovs_datapath_get_rsp *dp;
58 	struct ovs_datapath_get_req *req;
59 	bool found = false;
60 	int err;
61 
62 	new_req = ovs_datapath_new_req_alloc();
63 	ASSERT_NE(NULL, new_req);
64 	ovs_datapath_new_req_set_upcall_pid(new_req, 1);
65 	ovs_datapath_new_req_set_name(new_req, "ynl-test");
66 
67 	err = ovs_datapath_new(self->ys, new_req);
68 	ovs_datapath_new_req_free(new_req);
69 	ASSERT_EQ(0, err) {
70 		TH_LOG("new failed: %s", self->ys->err.msg);
71 	}
72 	self->dp_name = "ynl-test";
73 
74 	ksft_print_msg("get:\n");
75 	req = ovs_datapath_get_req_alloc();
76 	ASSERT_NE(NULL, req);
77 	ovs_datapath_get_req_set_name(req, "ynl-test");
78 
79 	dp = ovs_datapath_get(self->ys, req);
80 	ovs_datapath_get_req_free(req);
81 	ASSERT_NE(NULL, dp) {
82 		TH_LOG("get failed: %s", self->ys->err.msg);
83 	}
84 
85 	ovs_print_datapath(_metadata, dp);
86 	EXPECT_STREQ("ynl-test", dp->name);
87 	ovs_datapath_get_rsp_free(dp);
88 
89 	ksft_print_msg("dump:\n");
90 	dreq = ovs_datapath_get_req_dump_alloc();
91 	ASSERT_NE(NULL, dreq);
92 
93 	dps = ovs_datapath_get_dump(self->ys, dreq);
94 	ovs_datapath_get_req_dump_free(dreq);
95 	ASSERT_NE(NULL, dps) {
96 		TH_LOG("dump failed: %s", self->ys->err.msg);
97 	}
98 
99 	ynl_dump_foreach(dps, d) {
100 		ovs_print_datapath(_metadata, d);
101 		if (d->name && !strcmp(d->name, "ynl-test"))
102 			found = true;
103 	}
104 	ovs_datapath_get_list_free(dps);
105 	EXPECT_TRUE(found);
106 }
107 
108 TEST_HARNESS_MAIN
109