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