1 // SPDX-License-Identifier: GPL-2.0 2 #include <stdio.h> 3 #include <string.h> 4 5 #include <ynl.h> 6 7 #include <net/if.h> 8 9 #include "netdev-user.h" 10 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. 17 */ 18 19 static void netdev_print_device(struct netdev_dev_get_rsp *d, unsigned int op) 20 { 21 char ifname[IF_NAMESIZE]; 22 const char *name; 23 24 if (!d->_present.ifindex) 25 return; 26 27 name = if_indextoname(d->ifindex, ifname); 28 if (name) 29 printf("%8s", name); 30 printf("[%d]\t", d->ifindex); 31 32 if (!d->_present.xdp_features) 33 return; 34 35 printf("xdp-features (%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)); 39 } 40 41 printf(" xdp-rx-metadata-features (%llx):", d->xdp_rx_metadata_features); 42 for (int i = 0; d->xdp_rx_metadata_features > 1U << i; i++) { 43 if (d->xdp_rx_metadata_features & (1U << i)) 44 printf(" %s", netdev_xdp_rx_metadata_str(1 << i)); 45 } 46 47 printf(" xdp-zc-max-segs=%u", d->xdp_zc_max_segs); 48 49 name = netdev_op_str(op); 50 if (name) 51 printf(" (ntf: %s)", name); 52 printf("\n"); 53 } 54 55 int main(int argc, char **argv) 56 { 57 struct netdev_dev_get_list *devs; 58 struct ynl_ntf_base_type *ntf; 59 struct ynl_error yerr; 60 struct ynl_sock *ys; 61 int ifindex = 0; 62 63 if (argc > 1) 64 ifindex = strtol(argv[1], NULL, 0); 65 66 ys = ynl_sock_create(&ynl_netdev_family, &yerr); 67 if (!ys) { 68 fprintf(stderr, "YNL: %s\n", yerr.msg); 69 return 1; 70 } 71 72 if (ynl_subscribe(ys, "mgmt")) 73 goto err_close; 74 75 printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): "); 76 scanf("%d", &ifindex); 77 78 if (ifindex > 0) { 79 struct netdev_dev_get_req *req; 80 struct netdev_dev_get_rsp *d; 81 82 req = netdev_dev_get_req_alloc(); 83 netdev_dev_get_req_set_ifindex(req, ifindex); 84 85 d = netdev_dev_get(ys, req); 86 netdev_dev_get_req_free(req); 87 if (!d) 88 goto err_close; 89 90 netdev_print_device(d, 0); 91 netdev_dev_get_rsp_free(d); 92 } else if (!ifindex) { 93 devs = netdev_dev_get_dump(ys); 94 if (!devs) 95 goto err_close; 96 97 ynl_dump_foreach(devs, d) 98 netdev_print_device(d, 0); 99 netdev_dev_get_list_free(devs); 100 } else if (ifindex == -2) { 101 ynl_ntf_check(ys); 102 } 103 while ((ntf = ynl_ntf_dequeue(ys))) { 104 netdev_print_device((struct netdev_dev_get_rsp *)&ntf->data, 105 ntf->cmd); 106 ynl_ntf_free(ntf); 107 } 108 109 ynl_sock_destroy(ys); 110 return 0; 111 112 err_close: 113 fprintf(stderr, "YNL: %s\n", ys->err.msg); 114 ynl_sock_destroy(ys); 115 return 2; 116 } 117