1 // SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause)
2 /* Do not edit directly, auto-generated from: */
3 /*	Documentation/netlink/specs/netdev.yaml */
4 /* YNL-GEN user source */
5 
6 #include <stdlib.h>
7 #include <string.h>
8 #include "netdev-user.h"
9 #include "ynl.h"
10 #include <linux/netdev.h>
11 
12 #include <libmnl/libmnl.h>
13 #include <linux/genetlink.h>
14 
15 /* Enums */
16 static const char * const netdev_op_strmap[] = {
17 	[NETDEV_CMD_DEV_GET] = "dev-get",
18 	[NETDEV_CMD_DEV_ADD_NTF] = "dev-add-ntf",
19 	[NETDEV_CMD_DEV_DEL_NTF] = "dev-del-ntf",
20 	[NETDEV_CMD_DEV_CHANGE_NTF] = "dev-change-ntf",
21 };
22 
23 const char *netdev_op_str(int op)
24 {
25 	if (op < 0 || op >= (int)MNL_ARRAY_SIZE(netdev_op_strmap))
26 		return NULL;
27 	return netdev_op_strmap[op];
28 }
29 
30 static const char * const netdev_xdp_act_strmap[] = {
31 	[0] = "basic",
32 	[1] = "redirect",
33 	[2] = "ndo-xmit",
34 	[3] = "xsk-zerocopy",
35 	[4] = "hw-offload",
36 	[5] = "rx-sg",
37 	[6] = "ndo-xmit-sg",
38 };
39 
40 const char *netdev_xdp_act_str(enum netdev_xdp_act value)
41 {
42 	value = ffs(value) - 1;
43 	if (value < 0 || value >= (int)MNL_ARRAY_SIZE(netdev_xdp_act_strmap))
44 		return NULL;
45 	return netdev_xdp_act_strmap[value];
46 }
47 
48 /* Policies */
49 struct ynl_policy_attr netdev_dev_policy[NETDEV_A_DEV_MAX + 1] = {
50 	[NETDEV_A_DEV_IFINDEX] = { .name = "ifindex", .type = YNL_PT_U32, },
51 	[NETDEV_A_DEV_PAD] = { .name = "pad", .type = YNL_PT_IGNORE, },
52 	[NETDEV_A_DEV_XDP_FEATURES] = { .name = "xdp-features", .type = YNL_PT_U64, },
53 	[NETDEV_A_DEV_XDP_ZC_MAX_SEGS] = { .name = "xdp-zc-max-segs", .type = YNL_PT_U32, },
54 };
55 
56 struct ynl_policy_nest netdev_dev_nest = {
57 	.max_attr = NETDEV_A_DEV_MAX,
58 	.table = netdev_dev_policy,
59 };
60 
61 /* Common nested types */
62 /* ============== NETDEV_CMD_DEV_GET ============== */
63 /* NETDEV_CMD_DEV_GET - do */
64 void netdev_dev_get_req_free(struct netdev_dev_get_req *req)
65 {
66 	free(req);
67 }
68 
69 void netdev_dev_get_rsp_free(struct netdev_dev_get_rsp *rsp)
70 {
71 	free(rsp);
72 }
73 
74 int netdev_dev_get_rsp_parse(const struct nlmsghdr *nlh, void *data)
75 {
76 	struct ynl_parse_arg *yarg = data;
77 	struct netdev_dev_get_rsp *dst;
78 	const struct nlattr *attr;
79 
80 	dst = yarg->data;
81 
82 	mnl_attr_for_each(attr, nlh, sizeof(struct genlmsghdr)) {
83 		unsigned int type = mnl_attr_get_type(attr);
84 
85 		if (type == NETDEV_A_DEV_IFINDEX) {
86 			if (ynl_attr_validate(yarg, attr))
87 				return MNL_CB_ERROR;
88 			dst->_present.ifindex = 1;
89 			dst->ifindex = mnl_attr_get_u32(attr);
90 		} else if (type == NETDEV_A_DEV_XDP_FEATURES) {
91 			if (ynl_attr_validate(yarg, attr))
92 				return MNL_CB_ERROR;
93 			dst->_present.xdp_features = 1;
94 			dst->xdp_features = mnl_attr_get_u64(attr);
95 		} else if (type == NETDEV_A_DEV_XDP_ZC_MAX_SEGS) {
96 			if (ynl_attr_validate(yarg, attr))
97 				return MNL_CB_ERROR;
98 			dst->_present.xdp_zc_max_segs = 1;
99 			dst->xdp_zc_max_segs = mnl_attr_get_u32(attr);
100 		}
101 	}
102 
103 	return MNL_CB_OK;
104 }
105 
106 struct netdev_dev_get_rsp *
107 netdev_dev_get(struct ynl_sock *ys, struct netdev_dev_get_req *req)
108 {
109 	struct ynl_req_state yrs = { .yarg = { .ys = ys, }, };
110 	struct netdev_dev_get_rsp *rsp;
111 	struct nlmsghdr *nlh;
112 	int err;
113 
114 	nlh = ynl_gemsg_start_req(ys, ys->family_id, NETDEV_CMD_DEV_GET, 1);
115 	ys->req_policy = &netdev_dev_nest;
116 	yrs.yarg.rsp_policy = &netdev_dev_nest;
117 
118 	if (req->_present.ifindex)
119 		mnl_attr_put_u32(nlh, NETDEV_A_DEV_IFINDEX, req->ifindex);
120 
121 	rsp = calloc(1, sizeof(*rsp));
122 	yrs.yarg.data = rsp;
123 	yrs.cb = netdev_dev_get_rsp_parse;
124 	yrs.rsp_cmd = NETDEV_CMD_DEV_GET;
125 
126 	err = ynl_exec(ys, nlh, &yrs);
127 	if (err < 0)
128 		goto err_free;
129 
130 	return rsp;
131 
132 err_free:
133 	netdev_dev_get_rsp_free(rsp);
134 	return NULL;
135 }
136 
137 /* NETDEV_CMD_DEV_GET - dump */
138 void netdev_dev_get_list_free(struct netdev_dev_get_list *rsp)
139 {
140 	struct netdev_dev_get_list *next = rsp;
141 
142 	while ((void *)next != YNL_LIST_END) {
143 		rsp = next;
144 		next = rsp->next;
145 
146 		free(rsp);
147 	}
148 }
149 
150 struct netdev_dev_get_list *netdev_dev_get_dump(struct ynl_sock *ys)
151 {
152 	struct ynl_dump_state yds = {};
153 	struct nlmsghdr *nlh;
154 	int err;
155 
156 	yds.ys = ys;
157 	yds.alloc_sz = sizeof(struct netdev_dev_get_list);
158 	yds.cb = netdev_dev_get_rsp_parse;
159 	yds.rsp_cmd = NETDEV_CMD_DEV_GET;
160 	yds.rsp_policy = &netdev_dev_nest;
161 
162 	nlh = ynl_gemsg_start_dump(ys, ys->family_id, NETDEV_CMD_DEV_GET, 1);
163 
164 	err = ynl_exec_dump(ys, nlh, &yds);
165 	if (err < 0)
166 		goto free_list;
167 
168 	return yds.first;
169 
170 free_list:
171 	netdev_dev_get_list_free(yds.first);
172 	return NULL;
173 }
174 
175 /* NETDEV_CMD_DEV_GET - notify */
176 void netdev_dev_get_ntf_free(struct netdev_dev_get_ntf *rsp)
177 {
178 	free(rsp);
179 }
180 
181 static const struct ynl_ntf_info netdev_ntf_info[] =  {
182 	[NETDEV_CMD_DEV_ADD_NTF] =  {
183 		.alloc_sz	= sizeof(struct netdev_dev_get_ntf),
184 		.cb		= netdev_dev_get_rsp_parse,
185 		.policy		= &netdev_dev_nest,
186 		.free		= (void *)netdev_dev_get_ntf_free,
187 	},
188 	[NETDEV_CMD_DEV_DEL_NTF] =  {
189 		.alloc_sz	= sizeof(struct netdev_dev_get_ntf),
190 		.cb		= netdev_dev_get_rsp_parse,
191 		.policy		= &netdev_dev_nest,
192 		.free		= (void *)netdev_dev_get_ntf_free,
193 	},
194 	[NETDEV_CMD_DEV_CHANGE_NTF] =  {
195 		.alloc_sz	= sizeof(struct netdev_dev_get_ntf),
196 		.cb		= netdev_dev_get_rsp_parse,
197 		.policy		= &netdev_dev_nest,
198 		.free		= (void *)netdev_dev_get_ntf_free,
199 	},
200 };
201 
202 const struct ynl_family ynl_netdev_family =  {
203 	.name		= "netdev",
204 	.ntf_info	= netdev_ntf_info,
205 	.ntf_info_size	= MNL_ARRAY_SIZE(netdev_ntf_info),
206 };
207