~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/net/ynl/samples/netdev.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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(" xsk-features (%llx):", d->xsk_features);
 48         for (int i = 0; d->xsk_features >= 1U << i; i++) {
 49                 if (d->xsk_features & (1U << i))
 50                         printf(" %s", netdev_xsk_flags_str(1 << i));
 51         }
 52 
 53         printf(" xdp-zc-max-segs=%u", d->xdp_zc_max_segs);
 54 
 55         name = netdev_op_str(op);
 56         if (name)
 57                 printf(" (ntf: %s)", name);
 58         printf("\n");
 59 }
 60 
 61 int main(int argc, char **argv)
 62 {
 63         struct netdev_dev_get_list *devs;
 64         struct ynl_ntf_base_type *ntf;
 65         struct ynl_error yerr;
 66         struct ynl_sock *ys;
 67         int ifindex = 0;
 68 
 69         if (argc > 1)
 70                 ifindex = strtol(argv[1], NULL, 0);
 71 
 72         ys = ynl_sock_create(&ynl_netdev_family, &yerr);
 73         if (!ys) {
 74                 fprintf(stderr, "YNL: %s\n", yerr.msg);
 75                 return 1;
 76         }
 77 
 78         if (ynl_subscribe(ys, "mgmt"))
 79                 goto err_close;
 80 
 81         printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): ");
 82         scanf("%d", &ifindex);
 83 
 84         if (ifindex > 0) {
 85                 struct netdev_dev_get_req *req;
 86                 struct netdev_dev_get_rsp *d;
 87 
 88                 req = netdev_dev_get_req_alloc();
 89                 netdev_dev_get_req_set_ifindex(req, ifindex);
 90 
 91                 d = netdev_dev_get(ys, req);
 92                 netdev_dev_get_req_free(req);
 93                 if (!d)
 94                         goto err_close;
 95 
 96                 netdev_print_device(d, 0);
 97                 netdev_dev_get_rsp_free(d);
 98         } else if (!ifindex) {
 99                 devs = netdev_dev_get_dump(ys);
100                 if (!devs)
101                         goto err_close;
102 
103                 if (ynl_dump_empty(devs))
104                         fprintf(stderr, "Error: no devices reported\n");
105                 ynl_dump_foreach(devs, d)
106                         netdev_print_device(d, 0);
107                 netdev_dev_get_list_free(devs);
108         } else if (ifindex == -2) {
109                 ynl_ntf_check(ys);
110         }
111         while ((ntf = ynl_ntf_dequeue(ys))) {
112                 netdev_print_device((struct netdev_dev_get_rsp *)&ntf->data,
113                                     ntf->cmd);
114                 ynl_ntf_free(ntf);
115         }
116 
117         ynl_sock_destroy(ys);
118         return 0;
119 
120 err_close:
121         fprintf(stderr, "YNL: %s\n", ys->err.msg);
122         ynl_sock_destroy(ys);
123         return 2;
124 }
125 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php