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("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
));
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
));
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
));
53 printf(" xdp-zc-max-segs=%u", d
->xdp_zc_max_segs
);
55 name
= netdev_op_str(op
);
57 printf(" (ntf: %s)", name
);
61 int main(int argc
, char **argv
)
63 struct netdev_dev_get_list
*devs
;
64 struct ynl_ntf_base_type
*ntf
;
65 struct ynl_error yerr
;
70 ifindex
= strtol(argv
[1], NULL
, 0);
72 ys
= ynl_sock_create(&ynl_netdev_family
, &yerr
);
74 fprintf(stderr
, "YNL: %s\n", yerr
.msg
);
78 if (ynl_subscribe(ys
, "mgmt"))
81 printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): ");
82 if (scanf("%d", &ifindex
) != 1) {
83 fprintf(stderr
, "Error: unable to parse input\n");
88 struct netdev_dev_get_req
*req
;
89 struct netdev_dev_get_rsp
*d
;
91 req
= netdev_dev_get_req_alloc();
92 netdev_dev_get_req_set_ifindex(req
, ifindex
);
94 d
= netdev_dev_get(ys
, req
);
95 netdev_dev_get_req_free(req
);
99 netdev_print_device(d
, 0);
100 netdev_dev_get_rsp_free(d
);
101 } else if (!ifindex
) {
102 devs
= netdev_dev_get_dump(ys
);
106 if (ynl_dump_empty(devs
))
107 fprintf(stderr
, "Error: no devices reported\n");
108 ynl_dump_foreach(devs
, d
)
109 netdev_print_device(d
, 0);
110 netdev_dev_get_list_free(devs
);
111 } else if (ifindex
== -2) {
114 while ((ntf
= ynl_ntf_dequeue(ys
))) {
115 netdev_print_device((struct netdev_dev_get_rsp
*)&ntf
->data
,
120 ynl_sock_destroy(ys
);
124 fprintf(stderr
, "YNL: %s\n", ys
->err
.msg
);
126 ynl_sock_destroy(ys
);