1 // SPDX-License-Identifier: GPL-2.0
9 #include "netdev-user.h"
11 /* netdev genetlink family code sample
12 * This sample shows off basics of the netdev family but also notification
13 * handling, hence the somewhat odd UI. We subscribe to notifications first
14 * then wait for ifc selection, so the socket may already accumulate
15 * notifications as we wait. This allows us to test that YNL can handle
16 * requests and notifications getting interleaved.
19 static void netdev_print_device(struct netdev_dev_get_rsp *d, unsigned int op)
21 char ifname[IF_NAMESIZE];
24 if (!d->_present.ifindex)
27 name = if_indextoname(d->ifindex, ifname);
30 printf("[%d]\t", d->ifindex);
32 if (!d->_present.xdp_features)
35 printf("%llx:", d->xdp_features);
36 for (int i = 0; d->xdp_features > 1U << i; i++) {
37 if (d->xdp_features & (1U << i))
38 printf(" %s", netdev_xdp_act_str(1 << i));
41 printf(" xdp-zc-max-segs=%u", d->xdp_zc_max_segs);
43 name = netdev_op_str(op);
45 printf(" (ntf: %s)", name);
49 int main(int argc, char **argv)
51 struct netdev_dev_get_list *devs;
52 struct ynl_ntf_base_type *ntf;
53 struct ynl_error yerr;
58 ifindex = strtol(argv[1], NULL, 0);
60 ys = ynl_sock_create(&ynl_netdev_family, &yerr);
62 fprintf(stderr, "YNL: %s\n", yerr.msg);
66 if (ynl_subscribe(ys, "mgmt"))
69 printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): ");
70 scanf("%d", &ifindex);
73 struct netdev_dev_get_req *req;
74 struct netdev_dev_get_rsp *d;
76 req = netdev_dev_get_req_alloc();
77 netdev_dev_get_req_set_ifindex(req, ifindex);
79 d = netdev_dev_get(ys, req);
80 netdev_dev_get_req_free(req);
84 netdev_print_device(d, 0);
85 netdev_dev_get_rsp_free(d);
86 } else if (!ifindex) {
87 devs = netdev_dev_get_dump(ys);
91 ynl_dump_foreach(devs, d)
92 netdev_print_device(d, 0);
93 netdev_dev_get_list_free(devs);
94 } else if (ifindex == -2) {
97 while ((ntf = ynl_ntf_dequeue(ys))) {
98 netdev_print_device((struct netdev_dev_get_rsp *)&ntf->data,
103 ynl_sock_destroy(ys);
107 fprintf(stderr, "YNL: %s\n", ys->err.msg);
108 ynl_sock_destroy(ys);