summaryrefslogtreecommitdiffstats
path: root/tools/net/ynl/samples/netdev.c
blob: 591b90e21890c32310c46fd900cd13c8c13f66f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: GPL-2.0
#include <stdio.h>
#include <string.h>

#include <ynl.h>

#include <net/if.h>

#include "netdev-user.h"

/* netdev genetlink family code sample
 * This sample shows off basics of the netdev family but also notification
 * handling, hence the somewhat odd UI. We subscribe to notifications first
 * then wait for ifc selection, so the socket may already accumulate
 * notifications as we wait. This allows us to test that YNL can handle
 * requests and notifications getting interleaved.
 */

static void netdev_print_device(struct netdev_dev_get_rsp *d, unsigned int op)
{
	char ifname[IF_NAMESIZE];
	const char *name;

	if (!d->_present.ifindex)
		return;

	name = if_indextoname(d->ifindex, ifname);
	if (name)
		printf("%8s", name);
	printf("[%d]\t", d->ifindex);

	if (!d->_present.xdp_features)
		return;

	printf("xdp-features (%llx):", d->xdp_features);
	for (int i = 0; d->xdp_features >= 1U << i; i++) {
		if (d->xdp_features & (1U << i))
			printf(" %s", netdev_xdp_act_str(1 << i));
	}

	printf(" xdp-rx-metadata-features (%llx):", d->xdp_rx_metadata_features);
	for (int i = 0; d->xdp_rx_metadata_features >= 1U << i; i++) {
		if (d->xdp_rx_metadata_features & (1U << i))
			printf(" %s", netdev_xdp_rx_metadata_str(1 << i));
	}

	printf(" xsk-features (%llx):", d->xsk_features);
	for (int i = 0; d->xsk_features >= 1U << i; i++) {
		if (d->xsk_features & (1U << i))
			printf(" %s", netdev_xsk_flags_str(1 << i));
	}

	printf(" xdp-zc-max-segs=%u", d->xdp_zc_max_segs);

	name = netdev_op_str(op);
	if (name)
		printf(" (ntf: %s)", name);
	printf("\n");
}

int main(int argc, char **argv)
{
	struct netdev_dev_get_list *devs;
	struct ynl_ntf_base_type *ntf;
	struct ynl_error yerr;
	struct ynl_sock *ys;
	int ifindex = 0;

	if (argc > 1)
		ifindex = strtol(argv[1], NULL, 0);

	ys = ynl_sock_create(&ynl_netdev_family, &yerr);
	if (!ys) {
		fprintf(stderr, "YNL: %s\n", yerr.msg);
		return 1;
	}

	if (ynl_subscribe(ys, "mgmt"))
		goto err_close;

	printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): ");
	scanf("%d", &ifindex);

	if (ifindex > 0) {
		struct netdev_dev_get_req *req;
		struct netdev_dev_get_rsp *d;

		req = netdev_dev_get_req_alloc();
		netdev_dev_get_req_set_ifindex(req, ifindex);

		d = netdev_dev_get(ys, req);
		netdev_dev_get_req_free(req);
		if (!d)
			goto err_close;

		netdev_print_device(d, 0);
		netdev_dev_get_rsp_free(d);
	} else if (!ifindex) {
		devs = netdev_dev_get_dump(ys);
		if (!devs)
			goto err_close;

		ynl_dump_foreach(devs, d)
			netdev_print_device(d, 0);
		netdev_dev_get_list_free(devs);
	} else if (ifindex == -2) {
		ynl_ntf_check(ys);
	}
	while ((ntf = ynl_ntf_dequeue(ys))) {
		netdev_print_device((struct netdev_dev_get_rsp *)&ntf->data,
				    ntf->cmd);
		ynl_ntf_free(ntf);
	}

	ynl_sock_destroy(ys);
	return 0;

err_close:
	fprintf(stderr, "YNL: %s\n", ys->err.msg);
	ynl_sock_destroy(ys);
	return 2;
}