xref: /linux/tools/net/ynl/samples/ovs.c (revision 78c3925c048c752334873f56c3a3d1c9d53e0416)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include <ynl.h>
6 
7 #include "ovs_datapath-user.h"
8 
9 int main(int argc, char **argv)
10 {
11 	struct ynl_sock *ys;
12 	int err;
13 
14 	ys = ynl_sock_create(&ynl_ovs_datapath_family, NULL);
15 	if (!ys)
16 		return 1;
17 
18 	if (argc > 1) {
19 		struct ovs_datapath_new_req *req;
20 
21 		req = ovs_datapath_new_req_alloc();
22 		if (!req)
23 			goto err_close;
24 
25 		ovs_datapath_new_req_set_upcall_pid(req, 1);
26 		ovs_datapath_new_req_set_name(req, argv[1]);
27 
28 		err = ovs_datapath_new(ys, req);
29 		ovs_datapath_new_req_free(req);
30 		if (err)
31 			goto err_close;
32 	} else {
33 		struct ovs_datapath_get_req_dump *req;
34 		struct ovs_datapath_get_list *dps;
35 
36 		printf("Dump:\n");
37 		req = ovs_datapath_get_req_dump_alloc();
38 
39 		dps = ovs_datapath_get_dump(ys, req);
40 		ovs_datapath_get_req_dump_free(req);
41 		if (!dps)
42 			goto err_close;
43 
44 		ynl_dump_foreach(dps, dp) {
45 			printf("  %s(%d): pid:%u cache:%u\n",
46 			       dp->name, dp->_hdr.dp_ifindex,
47 			       dp->upcall_pid, dp->masks_cache_size);
48 		}
49 		ovs_datapath_get_list_free(dps);
50 	}
51 
52 	ynl_sock_destroy(ys);
53 
54 	return 0;
55 
56 err_close:
57 	fprintf(stderr, "YNL (%d): %s\n", ys->err.code, ys->err.msg);
58 	ynl_sock_destroy(ys);
59 	return 2;
60 }
61