]>
Commit | Line | Data |
---|---|---|
a3c4b484 JP |
1 | /* |
2 | * devlink.c Devlink tool | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Jiri Pirko <jiri@mellanox.com> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <stdbool.h> | |
16 | #include <unistd.h> | |
17 | #include <getopt.h> | |
18 | #include <limits.h> | |
19 | #include <errno.h> | |
20 | #include <linux/genetlink.h> | |
21 | #include <linux/devlink.h> | |
22 | #include <libmnl/libmnl.h> | |
23 | ||
24 | #include "SNAPSHOT.h" | |
25 | #include "list.h" | |
26 | #include "mnlg.h" | |
e3d0f0c0 | 27 | #include "json_writer.h" |
a3c4b484 | 28 | |
f57856fa OG |
29 | #define ESWITCH_MODE_LEGACY "legacy" |
30 | #define ESWITCH_MODE_SWITCHDEV "switchdev" | |
6566ca8c RD |
31 | #define ESWITCH_INLINE_MODE_NONE "none" |
32 | #define ESWITCH_INLINE_MODE_LINK "link" | |
33 | #define ESWITCH_INLINE_MODE_NETWORK "network" | |
34 | #define ESWITCH_INLINE_MODE_TRANSPORT "transport" | |
f57856fa | 35 | |
a3c4b484 JP |
36 | #define pr_err(args...) fprintf(stderr, ##args) |
37 | #define pr_out(args...) fprintf(stdout, ##args) | |
25ec49be JP |
38 | #define pr_out_sp(num, args...) \ |
39 | do { \ | |
40 | int ret = fprintf(stdout, ##args); \ | |
41 | if (ret < num) \ | |
42 | fprintf(stdout, "%*s", num - ret, ""); \ | |
43 | } while (0) | |
a3c4b484 JP |
44 | |
45 | static int _mnlg_socket_recv_run(struct mnlg_socket *nlg, | |
46 | mnl_cb_t data_cb, void *data) | |
47 | { | |
48 | int err; | |
49 | ||
50 | err = mnlg_socket_recv_run(nlg, data_cb, data); | |
51 | if (err < 0) { | |
52 | pr_err("devlink answers: %s\n", strerror(errno)); | |
53 | return -errno; | |
54 | } | |
55 | return 0; | |
56 | } | |
57 | ||
58 | static int _mnlg_socket_sndrcv(struct mnlg_socket *nlg, | |
59 | const struct nlmsghdr *nlh, | |
60 | mnl_cb_t data_cb, void *data) | |
61 | { | |
62 | int err; | |
63 | ||
64 | err = mnlg_socket_send(nlg, nlh); | |
65 | if (err < 0) { | |
66 | pr_err("Failed to call mnlg_socket_send\n"); | |
67 | return -errno; | |
68 | } | |
69 | return _mnlg_socket_recv_run(nlg, data_cb, data); | |
70 | } | |
71 | ||
72 | static int _mnlg_socket_group_add(struct mnlg_socket *nlg, | |
73 | const char *group_name) | |
74 | { | |
75 | int err; | |
76 | ||
77 | err = mnlg_socket_group_add(nlg, group_name); | |
78 | if (err < 0) { | |
79 | pr_err("Failed to call mnlg_socket_group_add\n"); | |
80 | return -errno; | |
81 | } | |
82 | return 0; | |
83 | } | |
84 | ||
85 | struct ifname_map { | |
86 | struct list_head list; | |
87 | char *bus_name; | |
88 | char *dev_name; | |
89 | uint32_t port_index; | |
90 | char *ifname; | |
91 | }; | |
92 | ||
93 | static struct ifname_map *ifname_map_alloc(const char *bus_name, | |
94 | const char *dev_name, | |
95 | uint32_t port_index, | |
96 | const char *ifname) | |
97 | { | |
98 | struct ifname_map *ifname_map; | |
99 | ||
100 | ifname_map = calloc(1, sizeof(*ifname_map)); | |
101 | if (!ifname_map) | |
102 | return NULL; | |
103 | ifname_map->bus_name = strdup(bus_name); | |
104 | ifname_map->dev_name = strdup(dev_name); | |
105 | ifname_map->port_index = port_index; | |
106 | ifname_map->ifname = strdup(ifname); | |
107 | if (!ifname_map->bus_name || !ifname_map->dev_name || | |
108 | !ifname_map->ifname) { | |
109 | free(ifname_map->ifname); | |
110 | free(ifname_map->dev_name); | |
111 | free(ifname_map->bus_name); | |
112 | free(ifname_map); | |
113 | return NULL; | |
114 | } | |
115 | return ifname_map; | |
116 | } | |
117 | ||
118 | static void ifname_map_free(struct ifname_map *ifname_map) | |
119 | { | |
120 | free(ifname_map->ifname); | |
121 | free(ifname_map->dev_name); | |
122 | free(ifname_map->bus_name); | |
123 | free(ifname_map); | |
124 | } | |
125 | ||
6563a6eb JP |
126 | #define BIT(nr) (1UL << (nr)) |
127 | #define DL_OPT_HANDLE BIT(0) | |
128 | #define DL_OPT_HANDLEP BIT(1) | |
129 | #define DL_OPT_PORT_TYPE BIT(2) | |
130 | #define DL_OPT_PORT_COUNT BIT(3) | |
e6d7367d JP |
131 | #define DL_OPT_SB BIT(4) |
132 | #define DL_OPT_SB_POOL BIT(5) | |
133 | #define DL_OPT_SB_SIZE BIT(6) | |
134 | #define DL_OPT_SB_TYPE BIT(7) | |
135 | #define DL_OPT_SB_THTYPE BIT(8) | |
136 | #define DL_OPT_SB_TH BIT(9) | |
137 | #define DL_OPT_SB_TC BIT(10) | |
f57856fa | 138 | #define DL_OPT_ESWITCH_MODE BIT(11) |
6566ca8c | 139 | #define DL_OPT_ESWITCH_INLINE_MODE BIT(12) |
6563a6eb JP |
140 | |
141 | struct dl_opts { | |
142 | uint32_t present; /* flags of present items */ | |
143 | char *bus_name; | |
144 | char *dev_name; | |
145 | uint32_t port_index; | |
146 | enum devlink_port_type port_type; | |
147 | uint32_t port_count; | |
e6d7367d JP |
148 | uint32_t sb_index; |
149 | uint16_t sb_pool_index; | |
150 | uint32_t sb_pool_size; | |
151 | enum devlink_sb_pool_type sb_pool_type; | |
152 | enum devlink_sb_threshold_type sb_pool_thtype; | |
153 | uint32_t sb_threshold; | |
154 | uint16_t sb_tc_index; | |
f57856fa | 155 | enum devlink_eswitch_mode eswitch_mode; |
6566ca8c | 156 | enum devlink_eswitch_inline_mode eswitch_inline_mode; |
6563a6eb JP |
157 | }; |
158 | ||
a3c4b484 JP |
159 | struct dl { |
160 | struct mnlg_socket *nlg; | |
161 | struct list_head ifname_map_list; | |
162 | int argc; | |
163 | char **argv; | |
43f35be4 | 164 | bool no_nice_names; |
6563a6eb | 165 | struct dl_opts opts; |
e3d0f0c0 JP |
166 | json_writer_t *jw; |
167 | bool json_output; | |
168 | bool pretty_output; | |
169 | struct { | |
170 | bool present; | |
171 | char *bus_name; | |
172 | char *dev_name; | |
173 | uint32_t port_index; | |
174 | } arr_last; | |
a3c4b484 JP |
175 | }; |
176 | ||
177 | static int dl_argc(struct dl *dl) | |
178 | { | |
179 | return dl->argc; | |
180 | } | |
181 | ||
182 | static char *dl_argv(struct dl *dl) | |
183 | { | |
184 | if (dl_argc(dl) == 0) | |
185 | return NULL; | |
186 | return *dl->argv; | |
187 | } | |
188 | ||
189 | static void dl_arg_inc(struct dl *dl) | |
190 | { | |
191 | if (dl_argc(dl) == 0) | |
192 | return; | |
193 | dl->argc--; | |
194 | dl->argv++; | |
195 | } | |
196 | ||
197 | static char *dl_argv_next(struct dl *dl) | |
198 | { | |
199 | char *ret; | |
200 | ||
201 | if (dl_argc(dl) == 0) | |
202 | return NULL; | |
203 | ||
204 | ret = *dl->argv; | |
205 | dl_arg_inc(dl); | |
206 | return ret; | |
207 | } | |
208 | ||
209 | static char *dl_argv_index(struct dl *dl, unsigned int index) | |
210 | { | |
211 | if (index >= dl_argc(dl)) | |
212 | return NULL; | |
213 | return dl->argv[index]; | |
214 | } | |
215 | ||
216 | static int strcmpx(const char *str1, const char *str2) | |
217 | { | |
218 | if (strlen(str1) > strlen(str2)) | |
219 | return -1; | |
220 | return strncmp(str1, str2, strlen(str1)); | |
221 | } | |
222 | ||
223 | static bool dl_argv_match(struct dl *dl, const char *pattern) | |
224 | { | |
225 | if (dl_argc(dl) == 0) | |
226 | return false; | |
227 | return strcmpx(dl_argv(dl), pattern) == 0; | |
228 | } | |
229 | ||
230 | static bool dl_no_arg(struct dl *dl) | |
231 | { | |
232 | return dl_argc(dl) == 0; | |
233 | } | |
234 | ||
235 | static int attr_cb(const struct nlattr *attr, void *data) | |
236 | { | |
237 | const struct nlattr **tb = data; | |
238 | int type; | |
239 | ||
240 | type = mnl_attr_get_type(attr); | |
241 | ||
242 | if (mnl_attr_type_valid(attr, DEVLINK_ATTR_MAX) < 0) | |
243 | return MNL_CB_ERROR; | |
244 | ||
245 | if (type == DEVLINK_ATTR_BUS_NAME && | |
246 | mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) | |
247 | return MNL_CB_ERROR; | |
248 | if (type == DEVLINK_ATTR_DEV_NAME && | |
249 | mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) | |
250 | return MNL_CB_ERROR; | |
251 | if (type == DEVLINK_ATTR_PORT_INDEX && | |
252 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
253 | return MNL_CB_ERROR; | |
254 | if (type == DEVLINK_ATTR_PORT_TYPE && | |
255 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
256 | return MNL_CB_ERROR; | |
257 | if (type == DEVLINK_ATTR_PORT_DESIRED_TYPE && | |
258 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
259 | return MNL_CB_ERROR; | |
260 | if (type == DEVLINK_ATTR_PORT_NETDEV_IFINDEX && | |
261 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
262 | return MNL_CB_ERROR; | |
263 | if (type == DEVLINK_ATTR_PORT_NETDEV_NAME && | |
264 | mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) | |
265 | return MNL_CB_ERROR; | |
266 | if (type == DEVLINK_ATTR_PORT_IBDEV_NAME && | |
267 | mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0) | |
268 | return MNL_CB_ERROR; | |
e6d7367d JP |
269 | if (type == DEVLINK_ATTR_SB_INDEX && |
270 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
271 | return MNL_CB_ERROR; | |
272 | if (type == DEVLINK_ATTR_SB_SIZE && | |
273 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
274 | return MNL_CB_ERROR; | |
275 | if (type == DEVLINK_ATTR_SB_INGRESS_POOL_COUNT && | |
276 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
277 | return MNL_CB_ERROR; | |
278 | if (type == DEVLINK_ATTR_SB_EGRESS_POOL_COUNT && | |
279 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
280 | return MNL_CB_ERROR; | |
281 | if (type == DEVLINK_ATTR_SB_INGRESS_TC_COUNT && | |
282 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
283 | return MNL_CB_ERROR; | |
284 | if (type == DEVLINK_ATTR_SB_EGRESS_TC_COUNT && | |
285 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
286 | return MNL_CB_ERROR; | |
287 | if (type == DEVLINK_ATTR_SB_POOL_INDEX && | |
288 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
289 | return MNL_CB_ERROR; | |
290 | if (type == DEVLINK_ATTR_SB_POOL_TYPE && | |
291 | mnl_attr_validate(attr, MNL_TYPE_U8) < 0) | |
292 | return MNL_CB_ERROR; | |
293 | if (type == DEVLINK_ATTR_SB_POOL_SIZE && | |
294 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
295 | return MNL_CB_ERROR; | |
296 | if (type == DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE && | |
297 | mnl_attr_validate(attr, MNL_TYPE_U8) < 0) | |
298 | return MNL_CB_ERROR; | |
299 | if (type == DEVLINK_ATTR_SB_THRESHOLD && | |
300 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
301 | return MNL_CB_ERROR; | |
302 | if (type == DEVLINK_ATTR_SB_TC_INDEX && | |
303 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
304 | return MNL_CB_ERROR; | |
25ec49be JP |
305 | if (type == DEVLINK_ATTR_SB_OCC_CUR && |
306 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
307 | return MNL_CB_ERROR; | |
308 | if (type == DEVLINK_ATTR_SB_OCC_MAX && | |
309 | mnl_attr_validate(attr, MNL_TYPE_U32) < 0) | |
310 | return MNL_CB_ERROR; | |
f57856fa OG |
311 | if (type == DEVLINK_ATTR_ESWITCH_MODE && |
312 | mnl_attr_validate(attr, MNL_TYPE_U16) < 0) | |
313 | return MNL_CB_ERROR; | |
6566ca8c RD |
314 | if (type == DEVLINK_ATTR_ESWITCH_INLINE_MODE && |
315 | mnl_attr_validate(attr, MNL_TYPE_U8) < 0) | |
316 | return MNL_CB_ERROR; | |
a3c4b484 JP |
317 | tb[type] = attr; |
318 | return MNL_CB_OK; | |
319 | } | |
320 | ||
321 | static int ifname_map_cb(const struct nlmsghdr *nlh, void *data) | |
322 | { | |
323 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
324 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
325 | struct dl *dl = data; | |
326 | struct ifname_map *ifname_map; | |
327 | const char *bus_name; | |
328 | const char *dev_name; | |
329 | uint32_t port_ifindex; | |
330 | const char *port_ifname; | |
331 | ||
332 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
333 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
334 | !tb[DEVLINK_ATTR_PORT_INDEX]) | |
335 | return MNL_CB_ERROR; | |
336 | ||
337 | if (!tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) | |
338 | return MNL_CB_OK; | |
339 | ||
340 | bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]); | |
341 | dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); | |
342 | port_ifindex = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]); | |
343 | port_ifname = mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME]); | |
344 | ifname_map = ifname_map_alloc(bus_name, dev_name, | |
345 | port_ifindex, port_ifname); | |
346 | if (!ifname_map) | |
347 | return MNL_CB_ERROR; | |
348 | list_add(&ifname_map->list, &dl->ifname_map_list); | |
349 | ||
350 | return MNL_CB_OK; | |
351 | } | |
352 | ||
353 | static void ifname_map_fini(struct dl *dl) | |
354 | { | |
355 | struct ifname_map *ifname_map, *tmp; | |
356 | ||
357 | list_for_each_entry_safe(ifname_map, tmp, | |
358 | &dl->ifname_map_list, list) { | |
359 | list_del(&ifname_map->list); | |
360 | ifname_map_free(ifname_map); | |
361 | } | |
362 | } | |
363 | ||
364 | static int ifname_map_init(struct dl *dl) | |
365 | { | |
366 | struct nlmsghdr *nlh; | |
367 | int err; | |
368 | ||
369 | INIT_LIST_HEAD(&dl->ifname_map_list); | |
370 | ||
371 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET, | |
372 | NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP); | |
373 | ||
374 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, ifname_map_cb, dl); | |
375 | if (err) { | |
376 | ifname_map_fini(dl); | |
377 | return err; | |
378 | } | |
379 | return 0; | |
380 | } | |
381 | ||
382 | static int ifname_map_lookup(struct dl *dl, const char *ifname, | |
383 | char **p_bus_name, char **p_dev_name, | |
384 | uint32_t *p_port_index) | |
385 | { | |
386 | struct ifname_map *ifname_map; | |
387 | ||
388 | list_for_each_entry(ifname_map, &dl->ifname_map_list, list) { | |
389 | if (strcmp(ifname, ifname_map->ifname) == 0) { | |
390 | *p_bus_name = ifname_map->bus_name; | |
391 | *p_dev_name = ifname_map->dev_name; | |
392 | *p_port_index = ifname_map->port_index; | |
393 | return 0; | |
394 | } | |
395 | } | |
396 | return -ENOENT; | |
397 | } | |
398 | ||
e6d7367d JP |
399 | static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name, |
400 | const char *dev_name, uint32_t port_index, | |
401 | char **p_ifname) | |
402 | { | |
403 | struct ifname_map *ifname_map; | |
404 | ||
405 | list_for_each_entry(ifname_map, &dl->ifname_map_list, list) { | |
406 | if (strcmp(bus_name, ifname_map->bus_name) == 0 && | |
407 | strcmp(dev_name, ifname_map->dev_name) == 0 && | |
408 | port_index == ifname_map->port_index) { | |
409 | *p_ifname = ifname_map->ifname; | |
410 | return 0; | |
411 | } | |
412 | } | |
413 | return -ENOENT; | |
414 | } | |
415 | ||
a3c4b484 JP |
416 | static unsigned int strslashcount(char *str) |
417 | { | |
418 | unsigned int count = 0; | |
419 | char *pos = str; | |
420 | ||
421 | while ((pos = strchr(pos, '/'))) { | |
422 | count++; | |
423 | pos++; | |
424 | } | |
425 | return count; | |
426 | } | |
427 | ||
428 | static int strslashrsplit(char *str, char **before, char **after) | |
429 | { | |
430 | char *slash; | |
431 | ||
432 | slash = strrchr(str, '/'); | |
433 | if (!slash) | |
434 | return -EINVAL; | |
435 | *slash = '\0'; | |
436 | *before = str; | |
437 | *after = slash + 1; | |
438 | return 0; | |
439 | } | |
440 | ||
441 | static int strtouint32_t(const char *str, uint32_t *p_val) | |
442 | { | |
443 | char *endptr; | |
444 | unsigned long int val; | |
445 | ||
446 | val = strtoul(str, &endptr, 10); | |
447 | if (endptr == str || *endptr != '\0') | |
448 | return -EINVAL; | |
449 | if (val > UINT_MAX) | |
450 | return -ERANGE; | |
451 | *p_val = val; | |
452 | return 0; | |
453 | } | |
454 | ||
e6d7367d JP |
455 | static int strtouint16_t(const char *str, uint16_t *p_val) |
456 | { | |
457 | char *endptr; | |
458 | unsigned long int val; | |
459 | ||
460 | val = strtoul(str, &endptr, 10); | |
461 | if (endptr == str || *endptr != '\0') | |
462 | return -EINVAL; | |
463 | if (val > USHRT_MAX) | |
464 | return -ERANGE; | |
465 | *p_val = val; | |
466 | return 0; | |
467 | } | |
468 | ||
2f85a9c5 JP |
469 | static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name) |
470 | { | |
471 | strslashrsplit(str, p_bus_name, p_dev_name); | |
472 | return 0; | |
473 | } | |
474 | ||
6563a6eb | 475 | static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name) |
a3c4b484 JP |
476 | { |
477 | char *str = dl_argv_next(dl); | |
a3c4b484 JP |
478 | |
479 | if (!str) { | |
480 | pr_err("Devlink identification (\"bus_name/dev_name\") expected\n"); | |
481 | return -EINVAL; | |
482 | } | |
483 | if (strslashcount(str) != 1) { | |
484 | pr_err("Wrong devlink identification string format.\n"); | |
485 | pr_err("Expected \"bus_name/dev_name\".\n"); | |
486 | return -EINVAL; | |
487 | } | |
2f85a9c5 JP |
488 | return __dl_argv_handle(str, p_bus_name, p_dev_name); |
489 | } | |
a3c4b484 | 490 | |
2f85a9c5 JP |
491 | static int __dl_argv_handle_port(char *str, |
492 | char **p_bus_name, char **p_dev_name, | |
493 | uint32_t *p_port_index) | |
494 | { | |
495 | char *handlestr = handlestr; | |
496 | char *portstr = portstr; | |
497 | int err; | |
498 | ||
499 | strslashrsplit(str, &handlestr, &portstr); | |
500 | err = strtouint32_t(portstr, p_port_index); | |
501 | if (err) { | |
502 | pr_err("Port index \"%s\" is not a number or not within range\n", | |
503 | portstr); | |
504 | return err; | |
505 | } | |
506 | strslashrsplit(handlestr, p_bus_name, p_dev_name); | |
507 | return 0; | |
508 | } | |
509 | ||
510 | static int __dl_argv_handle_port_ifname(struct dl *dl, char *str, | |
511 | char **p_bus_name, char **p_dev_name, | |
512 | uint32_t *p_port_index) | |
513 | { | |
514 | int err; | |
515 | ||
516 | err = ifname_map_lookup(dl, str, p_bus_name, p_dev_name, | |
517 | p_port_index); | |
518 | if (err) { | |
519 | pr_err("Netdevice \"%s\" not found\n", str); | |
520 | return err; | |
521 | } | |
a3c4b484 JP |
522 | return 0; |
523 | } | |
524 | ||
6563a6eb JP |
525 | static int dl_argv_handle_port(struct dl *dl, char **p_bus_name, |
526 | char **p_dev_name, uint32_t *p_port_index) | |
a3c4b484 JP |
527 | { |
528 | char *str = dl_argv_next(dl); | |
529 | unsigned int slash_count; | |
a3c4b484 JP |
530 | |
531 | if (!str) { | |
532 | pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n"); | |
533 | return -EINVAL; | |
534 | } | |
535 | slash_count = strslashcount(str); | |
7a34b9d0 HL |
536 | switch (slash_count) { |
537 | case 0: | |
538 | return __dl_argv_handle_port_ifname(dl, str, p_bus_name, | |
539 | p_dev_name, p_port_index); | |
540 | case 2: | |
541 | return __dl_argv_handle_port(str, p_bus_name, | |
542 | p_dev_name, p_port_index); | |
543 | default: | |
a3c4b484 JP |
544 | pr_err("Wrong port identification string format.\n"); |
545 | pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n"); | |
546 | return -EINVAL; | |
547 | } | |
2f85a9c5 JP |
548 | } |
549 | ||
550 | static int dl_argv_handle_both(struct dl *dl, char **p_bus_name, | |
551 | char **p_dev_name, uint32_t *p_port_index, | |
552 | uint32_t *p_handle_bit) | |
553 | { | |
554 | char *str = dl_argv_next(dl); | |
555 | unsigned int slash_count; | |
556 | int err; | |
557 | ||
558 | if (!str) { | |
559 | pr_err("One of following identifications expected:\n" | |
560 | "Devlink identification (\"bus_name/dev_name\")\n" | |
561 | "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n"); | |
562 | return -EINVAL; | |
563 | } | |
564 | slash_count = strslashcount(str); | |
565 | if (slash_count == 1) { | |
566 | err = __dl_argv_handle(str, p_bus_name, p_dev_name); | |
567 | if (err) | |
a3c4b484 | 568 | return err; |
2f85a9c5 JP |
569 | *p_handle_bit = DL_OPT_HANDLE; |
570 | } else if (slash_count == 2) { | |
571 | err = __dl_argv_handle_port(str, p_bus_name, | |
572 | p_dev_name, p_port_index); | |
573 | if (err) | |
574 | return err; | |
575 | *p_handle_bit = DL_OPT_HANDLEP; | |
a3c4b484 | 576 | } else if (slash_count == 0) { |
2f85a9c5 JP |
577 | err = __dl_argv_handle_port_ifname(dl, str, p_bus_name, |
578 | p_dev_name, p_port_index); | |
579 | if (err) | |
a3c4b484 | 580 | return err; |
2f85a9c5 JP |
581 | *p_handle_bit = DL_OPT_HANDLEP; |
582 | } else { | |
583 | pr_err("Wrong port identification string format.\n"); | |
584 | pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n"); | |
585 | return -EINVAL; | |
a3c4b484 | 586 | } |
a3c4b484 JP |
587 | return 0; |
588 | } | |
589 | ||
590 | static int dl_argv_uint32_t(struct dl *dl, uint32_t *p_val) | |
591 | { | |
592 | char *str = dl_argv_next(dl); | |
593 | int err; | |
594 | ||
595 | if (!str) { | |
596 | pr_err("Unsigned number argument expected\n"); | |
597 | return -EINVAL; | |
598 | } | |
599 | ||
600 | err = strtouint32_t(str, p_val); | |
601 | if (err) { | |
602 | pr_err("\"%s\" is not a number or not within range\n", str); | |
603 | return err; | |
604 | } | |
605 | return 0; | |
606 | } | |
607 | ||
e6d7367d JP |
608 | static int dl_argv_uint16_t(struct dl *dl, uint16_t *p_val) |
609 | { | |
610 | char *str = dl_argv_next(dl); | |
611 | int err; | |
612 | ||
613 | if (!str) { | |
614 | pr_err("Unsigned number argument expected\n"); | |
615 | return -EINVAL; | |
616 | } | |
617 | ||
618 | err = strtouint16_t(str, p_val); | |
619 | if (err) { | |
620 | pr_err("\"%s\" is not a number or not within range\n", str); | |
621 | return err; | |
622 | } | |
623 | return 0; | |
624 | } | |
625 | ||
a3c4b484 JP |
626 | static int dl_argv_str(struct dl *dl, const char **p_str) |
627 | { | |
628 | const char *str = dl_argv_next(dl); | |
629 | ||
630 | if (!str) { | |
631 | pr_err("String parameter expected\n"); | |
632 | return -EINVAL; | |
633 | } | |
634 | *p_str = str; | |
635 | return 0; | |
636 | } | |
637 | ||
638 | static int port_type_get(const char *typestr, enum devlink_port_type *p_type) | |
639 | { | |
640 | if (strcmp(typestr, "auto") == 0) { | |
641 | *p_type = DEVLINK_PORT_TYPE_AUTO; | |
642 | } else if (strcmp(typestr, "eth") == 0) { | |
643 | *p_type = DEVLINK_PORT_TYPE_ETH; | |
644 | } else if (strcmp(typestr, "ib") == 0) { | |
645 | *p_type = DEVLINK_PORT_TYPE_IB; | |
646 | } else { | |
647 | pr_err("Unknown port type \"%s\"\n", typestr); | |
648 | return -EINVAL; | |
649 | } | |
650 | return 0; | |
651 | } | |
652 | ||
e6d7367d JP |
653 | static int pool_type_get(const char *typestr, enum devlink_sb_pool_type *p_type) |
654 | { | |
655 | if (strcmp(typestr, "ingress") == 0) { | |
656 | *p_type = DEVLINK_SB_POOL_TYPE_INGRESS; | |
657 | } else if (strcmp(typestr, "egress") == 0) { | |
658 | *p_type = DEVLINK_SB_POOL_TYPE_EGRESS; | |
659 | } else { | |
660 | pr_err("Unknown pool type \"%s\"\n", typestr); | |
661 | return -EINVAL; | |
662 | } | |
663 | return 0; | |
664 | } | |
665 | ||
666 | static int threshold_type_get(const char *typestr, | |
667 | enum devlink_sb_threshold_type *p_type) | |
668 | { | |
669 | if (strcmp(typestr, "static") == 0) { | |
670 | *p_type = DEVLINK_SB_THRESHOLD_TYPE_STATIC; | |
671 | } else if (strcmp(typestr, "dynamic") == 0) { | |
672 | *p_type = DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC; | |
673 | } else { | |
674 | pr_err("Unknown threshold type \"%s\"\n", typestr); | |
675 | return -EINVAL; | |
676 | } | |
677 | return 0; | |
678 | } | |
679 | ||
7c55d770 SH |
680 | static int eswitch_mode_get(const char *typestr, |
681 | enum devlink_eswitch_mode *p_mode) | |
f57856fa OG |
682 | { |
683 | if (strcmp(typestr, ESWITCH_MODE_LEGACY) == 0) { | |
684 | *p_mode = DEVLINK_ESWITCH_MODE_LEGACY; | |
685 | } else if (strcmp(typestr, ESWITCH_MODE_SWITCHDEV) == 0) { | |
686 | *p_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV; | |
687 | } else { | |
688 | pr_err("Unknown eswitch mode \"%s\"\n", typestr); | |
689 | return -EINVAL; | |
690 | } | |
691 | return 0; | |
692 | } | |
693 | ||
6566ca8c RD |
694 | static int eswitch_inline_mode_get(const char *typestr, |
695 | enum devlink_eswitch_inline_mode *p_mode) | |
696 | { | |
697 | if (strcmp(typestr, ESWITCH_INLINE_MODE_NONE) == 0) { | |
698 | *p_mode = DEVLINK_ESWITCH_INLINE_MODE_NONE; | |
699 | } else if (strcmp(typestr, ESWITCH_INLINE_MODE_LINK) == 0) { | |
700 | *p_mode = DEVLINK_ESWITCH_INLINE_MODE_LINK; | |
701 | } else if (strcmp(typestr, ESWITCH_INLINE_MODE_NETWORK) == 0) { | |
702 | *p_mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK; | |
703 | } else if (strcmp(typestr, ESWITCH_INLINE_MODE_TRANSPORT) == 0) { | |
704 | *p_mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT; | |
705 | } else { | |
706 | pr_err("Unknown eswitch inline mode \"%s\"\n", typestr); | |
707 | return -EINVAL; | |
708 | } | |
709 | return 0; | |
710 | } | |
711 | ||
6563a6eb JP |
712 | static int dl_argv_parse(struct dl *dl, uint32_t o_required, |
713 | uint32_t o_optional) | |
a3c4b484 | 714 | { |
6563a6eb | 715 | struct dl_opts *opts = &dl->opts; |
a3c4b484 JP |
716 | uint32_t o_all = o_required | o_optional; |
717 | uint32_t o_found = 0; | |
718 | int err; | |
719 | ||
2f85a9c5 JP |
720 | if (o_required & DL_OPT_HANDLE && o_required & DL_OPT_HANDLEP) { |
721 | uint32_t handle_bit = handle_bit; | |
722 | ||
723 | err = dl_argv_handle_both(dl, &opts->bus_name, &opts->dev_name, | |
724 | &opts->port_index, &handle_bit); | |
725 | if (err) | |
726 | return err; | |
727 | o_found |= handle_bit; | |
728 | } else if (o_required & DL_OPT_HANDLE) { | |
6563a6eb | 729 | err = dl_argv_handle(dl, &opts->bus_name, &opts->dev_name); |
a3c4b484 JP |
730 | if (err) |
731 | return err; | |
6563a6eb | 732 | o_found |= DL_OPT_HANDLE; |
a3c4b484 | 733 | } else if (o_required & DL_OPT_HANDLEP) { |
6563a6eb JP |
734 | err = dl_argv_handle_port(dl, &opts->bus_name, &opts->dev_name, |
735 | &opts->port_index); | |
a3c4b484 JP |
736 | if (err) |
737 | return err; | |
6563a6eb | 738 | o_found |= DL_OPT_HANDLEP; |
a3c4b484 JP |
739 | } |
740 | ||
741 | while (dl_argc(dl)) { | |
742 | if (dl_argv_match(dl, "type") && | |
743 | (o_all & DL_OPT_PORT_TYPE)) { | |
a3c4b484 JP |
744 | const char *typestr; |
745 | ||
746 | dl_arg_inc(dl); | |
747 | err = dl_argv_str(dl, &typestr); | |
748 | if (err) | |
749 | return err; | |
6563a6eb | 750 | err = port_type_get(typestr, &opts->port_type); |
a3c4b484 JP |
751 | if (err) |
752 | return err; | |
a3c4b484 JP |
753 | o_found |= DL_OPT_PORT_TYPE; |
754 | } else if (dl_argv_match(dl, "count") && | |
755 | (o_all & DL_OPT_PORT_COUNT)) { | |
a3c4b484 | 756 | dl_arg_inc(dl); |
6563a6eb | 757 | err = dl_argv_uint32_t(dl, &opts->port_count); |
a3c4b484 JP |
758 | if (err) |
759 | return err; | |
a3c4b484 | 760 | o_found |= DL_OPT_PORT_COUNT; |
e6d7367d JP |
761 | } else if (dl_argv_match(dl, "sb") && |
762 | (o_all & DL_OPT_SB)) { | |
763 | dl_arg_inc(dl); | |
764 | err = dl_argv_uint32_t(dl, &opts->sb_index); | |
765 | if (err) | |
766 | return err; | |
767 | o_found |= DL_OPT_SB; | |
768 | } else if (dl_argv_match(dl, "pool") && | |
769 | (o_all & DL_OPT_SB_POOL)) { | |
770 | dl_arg_inc(dl); | |
771 | err = dl_argv_uint16_t(dl, &opts->sb_pool_index); | |
772 | if (err) | |
773 | return err; | |
774 | o_found |= DL_OPT_SB_POOL; | |
775 | } else if (dl_argv_match(dl, "size") && | |
776 | (o_all & DL_OPT_SB_SIZE)) { | |
777 | dl_arg_inc(dl); | |
778 | err = dl_argv_uint32_t(dl, &opts->sb_pool_size); | |
779 | if (err) | |
780 | return err; | |
781 | o_found |= DL_OPT_SB_SIZE; | |
782 | } else if (dl_argv_match(dl, "type") && | |
783 | (o_all & DL_OPT_SB_TYPE)) { | |
784 | const char *typestr; | |
785 | ||
786 | dl_arg_inc(dl); | |
787 | err = dl_argv_str(dl, &typestr); | |
788 | if (err) | |
789 | return err; | |
790 | err = pool_type_get(typestr, &opts->sb_pool_type); | |
791 | if (err) | |
792 | return err; | |
793 | o_found |= DL_OPT_SB_TYPE; | |
794 | } else if (dl_argv_match(dl, "thtype") && | |
795 | (o_all & DL_OPT_SB_THTYPE)) { | |
796 | const char *typestr; | |
797 | ||
798 | dl_arg_inc(dl); | |
799 | err = dl_argv_str(dl, &typestr); | |
800 | if (err) | |
801 | return err; | |
802 | err = threshold_type_get(typestr, | |
803 | &opts->sb_pool_thtype); | |
804 | if (err) | |
805 | return err; | |
806 | o_found |= DL_OPT_SB_THTYPE; | |
807 | } else if (dl_argv_match(dl, "th") && | |
808 | (o_all & DL_OPT_SB_TH)) { | |
809 | dl_arg_inc(dl); | |
810 | err = dl_argv_uint32_t(dl, &opts->sb_threshold); | |
811 | if (err) | |
812 | return err; | |
813 | o_found |= DL_OPT_SB_TH; | |
814 | } else if (dl_argv_match(dl, "tc") && | |
815 | (o_all & DL_OPT_SB_TC)) { | |
816 | dl_arg_inc(dl); | |
817 | err = dl_argv_uint16_t(dl, &opts->sb_tc_index); | |
818 | if (err) | |
819 | return err; | |
820 | o_found |= DL_OPT_SB_TC; | |
f57856fa OG |
821 | } else if (dl_argv_match(dl, "mode") && |
822 | (o_all & DL_OPT_ESWITCH_MODE)) { | |
823 | const char *typestr; | |
7c55d770 | 824 | |
f57856fa OG |
825 | dl_arg_inc(dl); |
826 | err = dl_argv_str(dl, &typestr); | |
827 | if (err) | |
828 | return err; | |
829 | err = eswitch_mode_get(typestr, &opts->eswitch_mode); | |
830 | if (err) | |
831 | return err; | |
832 | o_found |= DL_OPT_ESWITCH_MODE; | |
6566ca8c RD |
833 | } else if (dl_argv_match(dl, "inline-mode") && |
834 | (o_all & DL_OPT_ESWITCH_INLINE_MODE)) { | |
835 | const char *typestr; | |
836 | ||
837 | dl_arg_inc(dl); | |
838 | err = dl_argv_str(dl, &typestr); | |
839 | if (err) | |
840 | return err; | |
841 | err = eswitch_inline_mode_get( | |
842 | typestr, &opts->eswitch_inline_mode); | |
843 | if (err) | |
844 | return err; | |
845 | o_found |= DL_OPT_ESWITCH_INLINE_MODE; | |
a3c4b484 JP |
846 | } else { |
847 | pr_err("Unknown option \"%s\"\n", dl_argv(dl)); | |
848 | return -EINVAL; | |
849 | } | |
850 | } | |
851 | ||
6563a6eb JP |
852 | opts->present = o_found; |
853 | ||
e6d7367d JP |
854 | if ((o_optional & DL_OPT_SB) && !(o_found & DL_OPT_SB)) { |
855 | opts->sb_index = 0; | |
856 | opts->present |= DL_OPT_SB; | |
857 | } | |
858 | ||
a3c4b484 JP |
859 | if ((o_required & DL_OPT_PORT_TYPE) && !(o_found & DL_OPT_PORT_TYPE)) { |
860 | pr_err("Port type option expected.\n"); | |
861 | return -EINVAL; | |
862 | } | |
863 | ||
864 | if ((o_required & DL_OPT_PORT_COUNT) && | |
865 | !(o_found & DL_OPT_PORT_COUNT)) { | |
866 | pr_err("Port split count option expected.\n"); | |
867 | return -EINVAL; | |
868 | } | |
869 | ||
e6d7367d JP |
870 | if ((o_required & DL_OPT_SB_POOL) && !(o_found & DL_OPT_SB_POOL)) { |
871 | pr_err("Pool index option expected.\n"); | |
872 | return -EINVAL; | |
873 | } | |
874 | ||
875 | if ((o_required & DL_OPT_SB_SIZE) && !(o_found & DL_OPT_SB_SIZE)) { | |
876 | pr_err("Pool size option expected.\n"); | |
877 | return -EINVAL; | |
878 | } | |
879 | ||
880 | if ((o_required & DL_OPT_SB_TYPE) && !(o_found & DL_OPT_SB_TYPE)) { | |
881 | pr_err("Pool type option expected.\n"); | |
882 | return -EINVAL; | |
883 | } | |
884 | ||
885 | if ((o_required & DL_OPT_SB_THTYPE) && !(o_found & DL_OPT_SB_THTYPE)) { | |
886 | pr_err("Pool threshold type option expected.\n"); | |
887 | return -EINVAL; | |
888 | } | |
889 | ||
890 | if ((o_required & DL_OPT_SB_TH) && !(o_found & DL_OPT_SB_TH)) { | |
891 | pr_err("Threshold option expected.\n"); | |
892 | return -EINVAL; | |
893 | } | |
894 | ||
895 | if ((o_required & DL_OPT_SB_TC) && !(o_found & DL_OPT_SB_TC)) { | |
896 | pr_err("TC index option expected.\n"); | |
897 | return -EINVAL; | |
898 | } | |
f57856fa | 899 | |
7c55d770 SH |
900 | if ((o_required & DL_OPT_ESWITCH_MODE) && |
901 | !(o_found & DL_OPT_ESWITCH_MODE)) { | |
f57856fa OG |
902 | pr_err("E-Switch mode option expected.\n"); |
903 | return -EINVAL; | |
904 | } | |
905 | ||
6566ca8c RD |
906 | if ((o_required & DL_OPT_ESWITCH_INLINE_MODE) && |
907 | !(o_found & DL_OPT_ESWITCH_INLINE_MODE)) { | |
908 | pr_err("E-Switch inline-mode option expected.\n"); | |
909 | return -EINVAL; | |
910 | } | |
911 | ||
a3c4b484 JP |
912 | return 0; |
913 | } | |
914 | ||
6563a6eb JP |
915 | static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl) |
916 | { | |
917 | struct dl_opts *opts = &dl->opts; | |
918 | ||
919 | if (opts->present & DL_OPT_HANDLE) { | |
920 | mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name); | |
921 | mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name); | |
922 | } else if (opts->present & DL_OPT_HANDLEP) { | |
923 | mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name); | |
924 | mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name); | |
925 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_INDEX, | |
926 | opts->port_index); | |
927 | } | |
928 | if (opts->present & DL_OPT_PORT_TYPE) | |
929 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_TYPE, | |
930 | opts->port_type); | |
931 | if (opts->present & DL_OPT_PORT_COUNT) | |
932 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_SPLIT_COUNT, | |
933 | opts->port_count); | |
e6d7367d JP |
934 | if (opts->present & DL_OPT_SB) |
935 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_INDEX, | |
936 | opts->sb_index); | |
937 | if (opts->present & DL_OPT_SB_POOL) | |
938 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_POOL_INDEX, | |
939 | opts->sb_pool_index); | |
940 | if (opts->present & DL_OPT_SB_SIZE) | |
941 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_POOL_SIZE, | |
942 | opts->sb_pool_size); | |
943 | if (opts->present & DL_OPT_SB_TYPE) | |
944 | mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_TYPE, | |
945 | opts->sb_pool_type); | |
946 | if (opts->present & DL_OPT_SB_THTYPE) | |
947 | mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE, | |
948 | opts->sb_pool_thtype); | |
949 | if (opts->present & DL_OPT_SB_TH) | |
950 | mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_THRESHOLD, | |
951 | opts->sb_threshold); | |
952 | if (opts->present & DL_OPT_SB_TC) | |
953 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_TC_INDEX, | |
954 | opts->sb_tc_index); | |
f57856fa OG |
955 | if (opts->present & DL_OPT_ESWITCH_MODE) |
956 | mnl_attr_put_u16(nlh, DEVLINK_ATTR_ESWITCH_MODE, | |
957 | opts->eswitch_mode); | |
6566ca8c RD |
958 | if (opts->present & DL_OPT_ESWITCH_INLINE_MODE) |
959 | mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_INLINE_MODE, | |
960 | opts->eswitch_inline_mode); | |
6563a6eb JP |
961 | } |
962 | ||
963 | static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl, | |
964 | uint32_t o_required, uint32_t o_optional) | |
965 | { | |
966 | int err; | |
967 | ||
968 | err = dl_argv_parse(dl, o_required, o_optional); | |
969 | if (err) | |
970 | return err; | |
971 | dl_opts_put(nlh, dl); | |
972 | return 0; | |
973 | } | |
974 | ||
25ec49be JP |
975 | static bool dl_dump_filter(struct dl *dl, struct nlattr **tb) |
976 | { | |
977 | struct dl_opts *opts = &dl->opts; | |
978 | struct nlattr *attr_bus_name = tb[DEVLINK_ATTR_BUS_NAME]; | |
979 | struct nlattr *attr_dev_name = tb[DEVLINK_ATTR_DEV_NAME]; | |
980 | struct nlattr *attr_port_index = tb[DEVLINK_ATTR_PORT_INDEX]; | |
981 | struct nlattr *attr_sb_index = tb[DEVLINK_ATTR_SB_INDEX]; | |
982 | ||
983 | if (opts->present & DL_OPT_HANDLE && | |
984 | attr_bus_name && attr_dev_name) { | |
985 | const char *bus_name = mnl_attr_get_str(attr_bus_name); | |
986 | const char *dev_name = mnl_attr_get_str(attr_dev_name); | |
987 | ||
988 | if (strcmp(bus_name, opts->bus_name) != 0 || | |
989 | strcmp(dev_name, opts->dev_name) != 0) | |
990 | return false; | |
991 | } | |
992 | if (opts->present & DL_OPT_HANDLEP && | |
993 | attr_bus_name && attr_dev_name && attr_port_index) { | |
994 | const char *bus_name = mnl_attr_get_str(attr_bus_name); | |
995 | const char *dev_name = mnl_attr_get_str(attr_dev_name); | |
996 | uint32_t port_index = mnl_attr_get_u32(attr_port_index); | |
997 | ||
998 | if (strcmp(bus_name, opts->bus_name) != 0 || | |
999 | strcmp(dev_name, opts->dev_name) != 0 || | |
1000 | port_index != opts->port_index) | |
1001 | return false; | |
1002 | } | |
1003 | if (opts->present & DL_OPT_SB && attr_sb_index) { | |
1004 | uint32_t sb_index = mnl_attr_get_u32(attr_sb_index); | |
1005 | ||
1006 | if (sb_index != opts->sb_index) | |
1007 | return false; | |
1008 | } | |
1009 | return true; | |
1010 | } | |
707a91c5 | 1011 | |
a3c4b484 JP |
1012 | static void cmd_dev_help(void) |
1013 | { | |
7a9466db | 1014 | pr_err("Usage: devlink dev show [ DEV ]\n"); |
a93b6bb3 | 1015 | pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n"); |
6566ca8c | 1016 | pr_err(" [ inline-mode { none | link | network | transport } ]\n"); |
a93b6bb3 | 1017 | pr_err(" devlink dev eswitch show DEV\n"); |
a3c4b484 JP |
1018 | } |
1019 | ||
e3d0f0c0 JP |
1020 | static bool cmp_arr_last_handle(struct dl *dl, const char *bus_name, |
1021 | const char *dev_name) | |
43f35be4 | 1022 | { |
e3d0f0c0 JP |
1023 | if (!dl->arr_last.present) |
1024 | return false; | |
1025 | return strcmp(dl->arr_last.bus_name, bus_name) == 0 && | |
1026 | strcmp(dl->arr_last.dev_name, dev_name) == 0; | |
43f35be4 JP |
1027 | } |
1028 | ||
e3d0f0c0 JP |
1029 | static void arr_last_handle_set(struct dl *dl, const char *bus_name, |
1030 | const char *dev_name) | |
a3c4b484 | 1031 | { |
e3d0f0c0 JP |
1032 | dl->arr_last.present = true; |
1033 | free(dl->arr_last.dev_name); | |
1034 | free(dl->arr_last.bus_name); | |
1035 | dl->arr_last.bus_name = strdup(bus_name); | |
1036 | dl->arr_last.dev_name = strdup(dev_name); | |
a3c4b484 JP |
1037 | } |
1038 | ||
e3d0f0c0 JP |
1039 | static bool should_arr_last_handle_start(struct dl *dl, const char *bus_name, |
1040 | const char *dev_name) | |
43f35be4 | 1041 | { |
e3d0f0c0 | 1042 | return !cmp_arr_last_handle(dl, bus_name, dev_name); |
43f35be4 JP |
1043 | } |
1044 | ||
e3d0f0c0 JP |
1045 | static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name, |
1046 | const char *dev_name) | |
68cab0ba | 1047 | { |
e3d0f0c0 JP |
1048 | return dl->arr_last.present && |
1049 | !cmp_arr_last_handle(dl, bus_name, dev_name); | |
43f35be4 JP |
1050 | } |
1051 | ||
e3d0f0c0 JP |
1052 | static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb, |
1053 | bool content, bool array) | |
e6d7367d | 1054 | { |
e3d0f0c0 JP |
1055 | const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]); |
1056 | const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); | |
1057 | char buf[32]; | |
e6d7367d | 1058 | |
e3d0f0c0 | 1059 | sprintf(buf, "%s/%s", bus_name, dev_name); |
e6d7367d | 1060 | |
e3d0f0c0 JP |
1061 | if (dl->json_output) { |
1062 | if (array) { | |
1063 | if (should_arr_last_handle_end(dl, bus_name, dev_name)) | |
1064 | jsonw_end_array(dl->jw); | |
1065 | if (should_arr_last_handle_start(dl, bus_name, | |
1066 | dev_name)) { | |
1067 | jsonw_name(dl->jw, buf); | |
1068 | jsonw_start_array(dl->jw); | |
1069 | jsonw_start_object(dl->jw); | |
1070 | arr_last_handle_set(dl, bus_name, dev_name); | |
1071 | } else { | |
1072 | jsonw_start_object(dl->jw); | |
1073 | } | |
1074 | } else { | |
1075 | jsonw_name(dl->jw, buf); | |
1076 | jsonw_start_object(dl->jw); | |
1077 | } | |
1078 | } else { | |
1079 | pr_out("%s%s", buf, content ? ":" : ""); | |
1080 | } | |
1081 | } | |
e6d7367d | 1082 | |
e3d0f0c0 JP |
1083 | static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb) |
1084 | { | |
1085 | __pr_out_handle_start(dl, tb, true, true); | |
e6d7367d JP |
1086 | } |
1087 | ||
e3d0f0c0 JP |
1088 | static void pr_out_handle_end(struct dl *dl) |
1089 | { | |
1090 | if (dl->json_output) | |
1091 | jsonw_end_object(dl->jw); | |
1092 | else | |
1093 | pr_out("\n"); | |
1094 | } | |
1095 | ||
1096 | static void pr_out_handle(struct dl *dl, struct nlattr **tb) | |
1097 | { | |
1098 | __pr_out_handle_start(dl, tb, false, false); | |
1099 | pr_out_handle_end(dl); | |
1100 | } | |
1101 | ||
1102 | static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name, | |
1103 | const char *dev_name, uint32_t port_index) | |
1104 | { | |
1105 | return cmp_arr_last_handle(dl, bus_name, dev_name) && | |
1106 | dl->arr_last.port_index == port_index; | |
1107 | } | |
1108 | ||
1109 | static void arr_last_port_handle_set(struct dl *dl, const char *bus_name, | |
1110 | const char *dev_name, uint32_t port_index) | |
1111 | { | |
1112 | arr_last_handle_set(dl, bus_name, dev_name); | |
1113 | dl->arr_last.port_index = port_index; | |
1114 | } | |
1115 | ||
1116 | static bool should_arr_last_port_handle_start(struct dl *dl, | |
1117 | const char *bus_name, | |
1118 | const char *dev_name, | |
1119 | uint32_t port_index) | |
1120 | { | |
1121 | return !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index); | |
1122 | } | |
1123 | ||
1124 | static bool should_arr_last_port_handle_end(struct dl *dl, | |
1125 | const char *bus_name, | |
1126 | const char *dev_name, | |
1127 | uint32_t port_index) | |
1128 | { | |
1129 | return dl->arr_last.present && | |
1130 | !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index); | |
1131 | } | |
1132 | ||
1133 | static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name, | |
1134 | const char *dev_name, | |
1135 | uint32_t port_index, bool try_nice, | |
1136 | bool array) | |
1137 | { | |
1138 | static char buf[32]; | |
1139 | char *ifname = NULL; | |
1140 | ||
1141 | if (dl->no_nice_names || !try_nice || | |
1142 | ifname_map_rev_lookup(dl, bus_name, dev_name, | |
1143 | port_index, &ifname) != 0) | |
1144 | sprintf(buf, "%s/%s/%d", bus_name, dev_name, port_index); | |
1145 | else | |
1146 | sprintf(buf, "%s", ifname); | |
1147 | ||
1148 | if (dl->json_output) { | |
1149 | if (array) { | |
1150 | if (should_arr_last_port_handle_end(dl, bus_name, | |
1151 | dev_name, | |
1152 | port_index)) | |
1153 | jsonw_end_array(dl->jw); | |
1154 | if (should_arr_last_port_handle_start(dl, bus_name, | |
1155 | dev_name, | |
1156 | port_index)) { | |
1157 | jsonw_name(dl->jw, buf); | |
1158 | jsonw_start_array(dl->jw); | |
1159 | jsonw_start_object(dl->jw); | |
1160 | arr_last_port_handle_set(dl, bus_name, dev_name, | |
1161 | port_index); | |
1162 | } else { | |
1163 | jsonw_start_object(dl->jw); | |
1164 | } | |
1165 | } else { | |
1166 | jsonw_name(dl->jw, buf); | |
1167 | jsonw_start_object(dl->jw); | |
1168 | } | |
1169 | } else { | |
1170 | pr_out("%s:", buf); | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | static void pr_out_port_handle_start(struct dl *dl, struct nlattr **tb, bool try_nice) | |
e6d7367d JP |
1175 | { |
1176 | const char *bus_name; | |
1177 | const char *dev_name; | |
1178 | uint32_t port_index; | |
1179 | ||
1180 | bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]); | |
1181 | dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); | |
1182 | port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]); | |
e3d0f0c0 JP |
1183 | __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, false); |
1184 | } | |
1185 | ||
1186 | static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool try_nice) | |
1187 | { | |
1188 | const char *bus_name; | |
1189 | const char *dev_name; | |
1190 | uint32_t port_index; | |
e6d7367d | 1191 | |
e3d0f0c0 JP |
1192 | bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]); |
1193 | dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]); | |
1194 | port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]); | |
1195 | __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, true); | |
e6d7367d JP |
1196 | } |
1197 | ||
e3d0f0c0 | 1198 | static void pr_out_port_handle_end(struct dl *dl) |
a3c4b484 | 1199 | { |
e3d0f0c0 JP |
1200 | if (dl->json_output) |
1201 | jsonw_end_object(dl->jw); | |
1202 | else | |
1203 | pr_out("\n"); | |
1204 | } | |
1205 | ||
1206 | ||
1207 | static void pr_out_str(struct dl *dl, const char *name, const char *val) | |
1208 | { | |
1209 | if (dl->json_output) | |
1210 | jsonw_string_field(dl->jw, name, val); | |
1211 | else | |
1212 | pr_out(" %s %s", name, val); | |
1213 | } | |
1214 | ||
1215 | static void pr_out_uint(struct dl *dl, const char *name, unsigned int val) | |
1216 | { | |
1217 | if (dl->json_output) | |
1218 | jsonw_uint_field(dl->jw, name, val); | |
1219 | else | |
1220 | pr_out(" %s %u", name, val); | |
1221 | } | |
1222 | ||
1223 | static void pr_out_dev(struct dl *dl, struct nlattr **tb) | |
1224 | { | |
1225 | pr_out_handle(dl, tb); | |
1226 | } | |
1227 | ||
1228 | static void pr_out_section_start(struct dl *dl, const char *name) | |
1229 | { | |
1230 | if (dl->json_output) { | |
1231 | jsonw_start_object(dl->jw); | |
1232 | jsonw_name(dl->jw, name); | |
1233 | jsonw_start_object(dl->jw); | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | static void pr_out_section_end(struct dl *dl) | |
1238 | { | |
1239 | if (dl->json_output) { | |
1240 | if (dl->arr_last.present) | |
1241 | jsonw_end_array(dl->jw); | |
1242 | jsonw_end_object(dl->jw); | |
1243 | jsonw_end_object(dl->jw); | |
1244 | } | |
a3c4b484 JP |
1245 | } |
1246 | ||
f57856fa OG |
1247 | static const char *eswitch_mode_name(uint32_t mode) |
1248 | { | |
1249 | switch (mode) { | |
1250 | case DEVLINK_ESWITCH_MODE_LEGACY: return ESWITCH_MODE_LEGACY; | |
1251 | case DEVLINK_ESWITCH_MODE_SWITCHDEV: return ESWITCH_MODE_SWITCHDEV; | |
1252 | default: return "<unknown mode>"; | |
1253 | } | |
1254 | } | |
1255 | ||
6566ca8c RD |
1256 | static const char *eswitch_inline_mode_name(uint32_t mode) |
1257 | { | |
1258 | switch (mode) { | |
1259 | case DEVLINK_ESWITCH_INLINE_MODE_NONE: | |
1260 | return ESWITCH_INLINE_MODE_NONE; | |
1261 | case DEVLINK_ESWITCH_INLINE_MODE_LINK: | |
1262 | return ESWITCH_INLINE_MODE_LINK; | |
1263 | case DEVLINK_ESWITCH_INLINE_MODE_NETWORK: | |
1264 | return ESWITCH_INLINE_MODE_NETWORK; | |
1265 | case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT: | |
1266 | return ESWITCH_INLINE_MODE_TRANSPORT; | |
1267 | default: | |
1268 | return "<unknown mode>"; | |
1269 | } | |
1270 | } | |
1271 | ||
f57856fa OG |
1272 | static void pr_out_eswitch(struct dl *dl, struct nlattr **tb) |
1273 | { | |
1274 | __pr_out_handle_start(dl, tb, true, false); | |
1275 | ||
1276 | if (tb[DEVLINK_ATTR_ESWITCH_MODE]) | |
1277 | pr_out_str(dl, "mode", | |
1278 | eswitch_mode_name(mnl_attr_get_u16(tb[DEVLINK_ATTR_ESWITCH_MODE]))); | |
6566ca8c RD |
1279 | |
1280 | if (tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) | |
1281 | pr_out_str(dl, "inline-mode", | |
1282 | eswitch_inline_mode_name(mnl_attr_get_u8( | |
1283 | tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]))); | |
1284 | ||
f57856fa OG |
1285 | pr_out_handle_end(dl); |
1286 | } | |
1287 | ||
1288 | static int cmd_dev_eswitch_show_cb(const struct nlmsghdr *nlh, void *data) | |
1289 | { | |
1290 | struct dl *dl = data; | |
1291 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
1292 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1293 | ||
1294 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1295 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME]) | |
1296 | return MNL_CB_ERROR; | |
1297 | pr_out_eswitch(dl, tb); | |
1298 | return MNL_CB_OK; | |
1299 | } | |
1300 | ||
1301 | static int cmd_dev_eswitch_show(struct dl *dl) | |
1302 | { | |
1303 | struct nlmsghdr *nlh; | |
1304 | int err; | |
1305 | ||
1306 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_MODE_GET, | |
1307 | NLM_F_REQUEST | NLM_F_ACK); | |
1308 | ||
1309 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0); | |
1310 | if (err) | |
1311 | return err; | |
1312 | ||
1313 | pr_out_section_start(dl, "dev"); | |
1314 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_eswitch_show_cb, dl); | |
1315 | pr_out_section_end(dl); | |
1316 | return err; | |
1317 | } | |
1318 | ||
1319 | static int cmd_dev_eswitch_set(struct dl *dl) | |
1320 | { | |
1321 | struct nlmsghdr *nlh; | |
1322 | int err; | |
1323 | ||
1324 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_MODE_SET, | |
1325 | NLM_F_REQUEST | NLM_F_ACK); | |
1326 | ||
6566ca8c RD |
1327 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, |
1328 | DL_OPT_ESWITCH_MODE | | |
1329 | DL_OPT_ESWITCH_INLINE_MODE); | |
1330 | ||
f57856fa OG |
1331 | if (err) |
1332 | return err; | |
1333 | ||
6566ca8c RD |
1334 | if (dl->opts.present == 1) { |
1335 | pr_err("Need to set at least one option\n"); | |
1336 | return -ENOENT; | |
1337 | } | |
1338 | ||
f57856fa OG |
1339 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); |
1340 | } | |
1341 | ||
1342 | static int cmd_dev_eswitch(struct dl *dl) | |
1343 | { | |
a93b6bb3 RD |
1344 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { |
1345 | cmd_dev_help(); | |
1346 | return 0; | |
1347 | } else if (dl_argv_match(dl, "set")) { | |
f57856fa OG |
1348 | dl_arg_inc(dl); |
1349 | return cmd_dev_eswitch_set(dl); | |
1350 | } else if (dl_argv_match(dl, "show")) { | |
1351 | dl_arg_inc(dl); | |
1352 | return cmd_dev_eswitch_show(dl); | |
1353 | } | |
1354 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1355 | return -ENOENT; | |
1356 | } | |
1357 | ||
a3c4b484 JP |
1358 | static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data) |
1359 | { | |
e3d0f0c0 | 1360 | struct dl *dl = data; |
a3c4b484 JP |
1361 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
1362 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1363 | ||
1364 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1365 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME]) | |
1366 | return MNL_CB_ERROR; | |
e3d0f0c0 | 1367 | pr_out_dev(dl, tb); |
a3c4b484 JP |
1368 | return MNL_CB_OK; |
1369 | } | |
1370 | ||
1371 | static int cmd_dev_show(struct dl *dl) | |
1372 | { | |
1373 | struct nlmsghdr *nlh; | |
1374 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
1375 | int err; | |
1376 | ||
1377 | if (dl_argc(dl) == 0) | |
1378 | flags |= NLM_F_DUMP; | |
1379 | ||
1380 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_GET, flags); | |
1381 | ||
1382 | if (dl_argc(dl) > 0) { | |
1383 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0); | |
1384 | if (err) | |
1385 | return err; | |
1386 | } | |
1387 | ||
e3d0f0c0 JP |
1388 | pr_out_section_start(dl, "dev"); |
1389 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_show_cb, dl); | |
1390 | pr_out_section_end(dl); | |
1391 | return err; | |
a3c4b484 JP |
1392 | } |
1393 | ||
1394 | static int cmd_dev(struct dl *dl) | |
1395 | { | |
1396 | if (dl_argv_match(dl, "help")) { | |
1397 | cmd_dev_help(); | |
1398 | return 0; | |
1399 | } else if (dl_argv_match(dl, "show") || | |
1400 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
1401 | dl_arg_inc(dl); | |
1402 | return cmd_dev_show(dl); | |
f57856fa OG |
1403 | } else if (dl_argv_match(dl, "eswitch")) { |
1404 | dl_arg_inc(dl); | |
1405 | return cmd_dev_eswitch(dl); | |
a3c4b484 JP |
1406 | } |
1407 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1408 | return -ENOENT; | |
1409 | } | |
1410 | ||
1411 | static void cmd_port_help(void) | |
1412 | { | |
7a9466db JP |
1413 | pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n"); |
1414 | pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n"); | |
1415 | pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n"); | |
1416 | pr_err(" devlink port unsplit DEV/PORT_INDEX\n"); | |
a3c4b484 JP |
1417 | } |
1418 | ||
1419 | static const char *port_type_name(uint32_t type) | |
1420 | { | |
1421 | switch (type) { | |
1422 | case DEVLINK_PORT_TYPE_NOTSET: return "notset"; | |
1423 | case DEVLINK_PORT_TYPE_AUTO: return "auto"; | |
1424 | case DEVLINK_PORT_TYPE_ETH: return "eth"; | |
1425 | case DEVLINK_PORT_TYPE_IB: return "ib"; | |
1426 | default: return "<unknown type>"; | |
1427 | } | |
1428 | } | |
1429 | ||
e3d0f0c0 | 1430 | static void pr_out_port(struct dl *dl, struct nlattr **tb) |
a3c4b484 JP |
1431 | { |
1432 | struct nlattr *pt_attr = tb[DEVLINK_ATTR_PORT_TYPE]; | |
1433 | struct nlattr *dpt_attr = tb[DEVLINK_ATTR_PORT_DESIRED_TYPE]; | |
1434 | ||
e3d0f0c0 | 1435 | pr_out_port_handle_start(dl, tb, false); |
a3c4b484 JP |
1436 | if (pt_attr) { |
1437 | uint16_t port_type = mnl_attr_get_u16(pt_attr); | |
1438 | ||
e3d0f0c0 | 1439 | pr_out_str(dl, "type", port_type_name(port_type)); |
a3c4b484 JP |
1440 | if (dpt_attr) { |
1441 | uint16_t des_port_type = mnl_attr_get_u16(dpt_attr); | |
1442 | ||
1443 | if (port_type != des_port_type) | |
e3d0f0c0 JP |
1444 | pr_out_str(dl, "des_type", |
1445 | port_type_name(des_port_type)); | |
a3c4b484 JP |
1446 | } |
1447 | } | |
1448 | if (tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) | |
e3d0f0c0 JP |
1449 | pr_out_str(dl, "netdev", |
1450 | mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME])); | |
a3c4b484 | 1451 | if (tb[DEVLINK_ATTR_PORT_IBDEV_NAME]) |
e3d0f0c0 JP |
1452 | pr_out_str(dl, "ibdev", |
1453 | mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_IBDEV_NAME])); | |
a3c4b484 | 1454 | if (tb[DEVLINK_ATTR_PORT_SPLIT_GROUP]) |
e3d0f0c0 JP |
1455 | pr_out_uint(dl, "split_group", |
1456 | mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])); | |
1457 | pr_out_port_handle_end(dl); | |
a3c4b484 JP |
1458 | } |
1459 | ||
1460 | static int cmd_port_show_cb(const struct nlmsghdr *nlh, void *data) | |
1461 | { | |
e3d0f0c0 | 1462 | struct dl *dl = data; |
a3c4b484 JP |
1463 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
1464 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1465 | ||
1466 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1467 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
1468 | !tb[DEVLINK_ATTR_PORT_INDEX]) | |
1469 | return MNL_CB_ERROR; | |
e3d0f0c0 | 1470 | pr_out_port(dl, tb); |
a3c4b484 JP |
1471 | return MNL_CB_OK; |
1472 | } | |
1473 | ||
1474 | static int cmd_port_show(struct dl *dl) | |
1475 | { | |
1476 | struct nlmsghdr *nlh; | |
1477 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
1478 | int err; | |
1479 | ||
1480 | if (dl_argc(dl) == 0) | |
1481 | flags |= NLM_F_DUMP; | |
1482 | ||
1483 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET, flags); | |
1484 | ||
1485 | if (dl_argc(dl) > 0) { | |
1486 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0); | |
1487 | if (err) | |
1488 | return err; | |
1489 | } | |
1490 | ||
e3d0f0c0 JP |
1491 | pr_out_section_start(dl, "port"); |
1492 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_port_show_cb, dl); | |
1493 | pr_out_section_end(dl); | |
1494 | return err; | |
a3c4b484 JP |
1495 | } |
1496 | ||
1497 | static int cmd_port_set(struct dl *dl) | |
1498 | { | |
1499 | struct nlmsghdr *nlh; | |
1500 | int err; | |
1501 | ||
1502 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SET, | |
1503 | NLM_F_REQUEST | NLM_F_ACK); | |
1504 | ||
1505 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_TYPE, 0); | |
1506 | if (err) | |
1507 | return err; | |
1508 | ||
1509 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
1510 | } | |
1511 | ||
1512 | static int cmd_port_split(struct dl *dl) | |
1513 | { | |
1514 | struct nlmsghdr *nlh; | |
1515 | int err; | |
1516 | ||
1517 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SPLIT, | |
1518 | NLM_F_REQUEST | NLM_F_ACK); | |
1519 | ||
1520 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_COUNT, 0); | |
1521 | if (err) | |
1522 | return err; | |
1523 | ||
1524 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
1525 | } | |
1526 | ||
1527 | static int cmd_port_unsplit(struct dl *dl) | |
1528 | { | |
1529 | struct nlmsghdr *nlh; | |
1530 | int err; | |
1531 | ||
1532 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_UNSPLIT, | |
1533 | NLM_F_REQUEST | NLM_F_ACK); | |
1534 | ||
1535 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0); | |
1536 | if (err) | |
1537 | return err; | |
1538 | ||
1539 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
1540 | } | |
1541 | ||
1542 | static int cmd_port(struct dl *dl) | |
1543 | { | |
1544 | if (dl_argv_match(dl, "help")) { | |
1545 | cmd_port_help(); | |
1546 | return 0; | |
1547 | } else if (dl_argv_match(dl, "show") || | |
1548 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
1549 | dl_arg_inc(dl); | |
1550 | return cmd_port_show(dl); | |
1551 | } else if (dl_argv_match(dl, "set")) { | |
1552 | dl_arg_inc(dl); | |
1553 | return cmd_port_set(dl); | |
1554 | } else if (dl_argv_match(dl, "split")) { | |
1555 | dl_arg_inc(dl); | |
1556 | return cmd_port_split(dl); | |
1557 | } else if (dl_argv_match(dl, "unsplit")) { | |
1558 | dl_arg_inc(dl); | |
1559 | return cmd_port_unsplit(dl); | |
1560 | } | |
1561 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1562 | return -ENOENT; | |
1563 | } | |
1564 | ||
e6d7367d JP |
1565 | static void cmd_sb_help(void) |
1566 | { | |
7a9466db JP |
1567 | pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n"); |
1568 | pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n"); | |
1569 | pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n"); | |
1570 | pr_err(" size POOL_SIZE thtype { static | dynamic }\n"); | |
1571 | pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n"); | |
1572 | pr_err(" pool POOL_INDEX ]\n"); | |
1573 | pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n"); | |
1574 | pr_err(" pool POOL_INDEX th THRESHOLD\n"); | |
1575 | pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n"); | |
1576 | pr_err(" type { ingress | egress } ]\n"); | |
1577 | pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n"); | |
1578 | pr_err(" type { ingress | egress } pool POOL_INDEX\n"); | |
1579 | pr_err(" th THRESHOLD\n"); | |
1580 | pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n"); | |
1581 | pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n"); | |
1582 | pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n"); | |
e6d7367d JP |
1583 | } |
1584 | ||
e3d0f0c0 | 1585 | static void pr_out_sb(struct dl *dl, struct nlattr **tb) |
e6d7367d | 1586 | { |
e3d0f0c0 JP |
1587 | pr_out_handle_start_arr(dl, tb); |
1588 | pr_out_uint(dl, "sb", | |
1589 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
1590 | pr_out_uint(dl, "size", | |
1591 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_SIZE])); | |
1592 | pr_out_uint(dl, "ing_pools", | |
1593 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT])); | |
1594 | pr_out_uint(dl, "eg_pools", | |
1595 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT])); | |
1596 | pr_out_uint(dl, "ing_tcs", | |
1597 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT])); | |
1598 | pr_out_uint(dl, "eg_tcs", | |
1599 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT])); | |
1600 | pr_out_handle_end(dl); | |
e6d7367d JP |
1601 | } |
1602 | ||
1603 | static int cmd_sb_show_cb(const struct nlmsghdr *nlh, void *data) | |
1604 | { | |
e3d0f0c0 | 1605 | struct dl *dl = data; |
e6d7367d JP |
1606 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
1607 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1608 | ||
1609 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1610 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
1611 | !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_SIZE] || | |
1612 | !tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] || | |
1613 | !tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] || | |
1614 | !tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT] || | |
1615 | !tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT]) | |
1616 | return MNL_CB_ERROR; | |
e3d0f0c0 | 1617 | pr_out_sb(dl, tb); |
e6d7367d JP |
1618 | return MNL_CB_OK; |
1619 | } | |
1620 | ||
1621 | static int cmd_sb_show(struct dl *dl) | |
1622 | { | |
1623 | struct nlmsghdr *nlh; | |
1624 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
1625 | int err; | |
1626 | ||
1627 | if (dl_argc(dl) == 0) | |
1628 | flags |= NLM_F_DUMP; | |
1629 | ||
1630 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_GET, flags); | |
1631 | ||
1632 | if (dl_argc(dl) > 0) { | |
1633 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB); | |
1634 | if (err) | |
1635 | return err; | |
1636 | } | |
1637 | ||
e3d0f0c0 JP |
1638 | pr_out_section_start(dl, "sb"); |
1639 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_show_cb, dl); | |
1640 | pr_out_section_end(dl); | |
1641 | return err; | |
e6d7367d JP |
1642 | } |
1643 | ||
1644 | static const char *pool_type_name(uint8_t type) | |
1645 | { | |
1646 | switch (type) { | |
1647 | case DEVLINK_SB_POOL_TYPE_INGRESS: return "ingress"; | |
1648 | case DEVLINK_SB_POOL_TYPE_EGRESS: return "egress"; | |
1649 | default: return "<unknown type>"; | |
1650 | } | |
1651 | } | |
1652 | ||
1653 | static const char *threshold_type_name(uint8_t type) | |
1654 | { | |
1655 | switch (type) { | |
1656 | case DEVLINK_SB_THRESHOLD_TYPE_STATIC: return "static"; | |
1657 | case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC: return "dynamic"; | |
1658 | default: return "<unknown type>"; | |
1659 | } | |
1660 | } | |
1661 | ||
e3d0f0c0 | 1662 | static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb) |
e6d7367d | 1663 | { |
e3d0f0c0 JP |
1664 | pr_out_handle_start_arr(dl, tb); |
1665 | pr_out_uint(dl, "sb", | |
1666 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
1667 | pr_out_uint(dl, "pool", | |
1668 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); | |
1669 | pr_out_str(dl, "type", | |
1670 | pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); | |
1671 | pr_out_uint(dl, "size", | |
1672 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE])); | |
1673 | pr_out_str(dl, "thtype", | |
1674 | threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]))); | |
1675 | pr_out_handle_end(dl); | |
e6d7367d JP |
1676 | } |
1677 | ||
1678 | static int cmd_sb_pool_show_cb(const struct nlmsghdr *nlh, void *data) | |
1679 | { | |
e3d0f0c0 | 1680 | struct dl *dl = data; |
e6d7367d JP |
1681 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; |
1682 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1683 | ||
1684 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1685 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
1686 | !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_INDEX] || | |
1687 | !tb[DEVLINK_ATTR_SB_POOL_TYPE] || !tb[DEVLINK_ATTR_SB_POOL_SIZE] || | |
1688 | !tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]) | |
1689 | return MNL_CB_ERROR; | |
e3d0f0c0 | 1690 | pr_out_sb_pool(dl, tb); |
e6d7367d JP |
1691 | return MNL_CB_OK; |
1692 | } | |
1693 | ||
1694 | static int cmd_sb_pool_show(struct dl *dl) | |
1695 | { | |
1696 | struct nlmsghdr *nlh; | |
1697 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
1698 | int err; | |
1699 | ||
1700 | if (dl_argc(dl) == 0) | |
1701 | flags |= NLM_F_DUMP; | |
1702 | ||
1703 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_GET, flags); | |
1704 | ||
1705 | if (dl_argc(dl) > 0) { | |
1706 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL, | |
1707 | DL_OPT_SB); | |
1708 | if (err) | |
1709 | return err; | |
1710 | } | |
1711 | ||
e3d0f0c0 JP |
1712 | pr_out_section_start(dl, "pool"); |
1713 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_pool_show_cb, dl); | |
1714 | pr_out_section_end(dl); | |
1715 | return err; | |
e6d7367d JP |
1716 | } |
1717 | ||
1718 | static int cmd_sb_pool_set(struct dl *dl) | |
1719 | { | |
1720 | struct nlmsghdr *nlh; | |
1721 | int err; | |
1722 | ||
1723 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_SET, | |
1724 | NLM_F_REQUEST | NLM_F_ACK); | |
1725 | ||
1726 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL | | |
1727 | DL_OPT_SB_SIZE | DL_OPT_SB_THTYPE, DL_OPT_SB); | |
1728 | if (err) | |
1729 | return err; | |
1730 | ||
1731 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
1732 | } | |
1733 | ||
1734 | static int cmd_sb_pool(struct dl *dl) | |
1735 | { | |
1736 | if (dl_argv_match(dl, "help")) { | |
1737 | cmd_sb_help(); | |
1738 | return 0; | |
1739 | } else if (dl_argv_match(dl, "show") || | |
1740 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
1741 | dl_arg_inc(dl); | |
1742 | return cmd_sb_pool_show(dl); | |
1743 | } else if (dl_argv_match(dl, "set")) { | |
1744 | dl_arg_inc(dl); | |
1745 | return cmd_sb_pool_set(dl); | |
1746 | } | |
1747 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1748 | return -ENOENT; | |
1749 | } | |
1750 | ||
1751 | static void pr_out_sb_port_pool(struct dl *dl, struct nlattr **tb) | |
1752 | { | |
e3d0f0c0 JP |
1753 | pr_out_port_handle_start_arr(dl, tb, true); |
1754 | pr_out_uint(dl, "sb", | |
1755 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
1756 | pr_out_uint(dl, "pool", | |
1757 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); | |
1758 | pr_out_uint(dl, "threshold", | |
1759 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); | |
1760 | pr_out_port_handle_end(dl); | |
e6d7367d JP |
1761 | } |
1762 | ||
1763 | static int cmd_sb_port_pool_show_cb(const struct nlmsghdr *nlh, void *data) | |
1764 | { | |
1765 | struct dl *dl = data; | |
1766 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
1767 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1768 | ||
1769 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1770 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
1771 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
1772 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD]) | |
1773 | return MNL_CB_ERROR; | |
1774 | pr_out_sb_port_pool(dl, tb); | |
1775 | return MNL_CB_OK; | |
1776 | } | |
1777 | ||
1778 | static int cmd_sb_port_pool_show(struct dl *dl) | |
1779 | { | |
1780 | struct nlmsghdr *nlh; | |
1781 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
1782 | int err; | |
1783 | ||
1784 | if (dl_argc(dl) == 0) | |
1785 | flags |= NLM_F_DUMP; | |
1786 | ||
1787 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags); | |
1788 | ||
1789 | if (dl_argc(dl) > 0) { | |
1790 | err = dl_argv_parse_put(nlh, dl, | |
1791 | DL_OPT_HANDLEP | DL_OPT_SB_POOL, | |
1792 | DL_OPT_SB); | |
1793 | if (err) | |
1794 | return err; | |
1795 | } | |
1796 | ||
e3d0f0c0 JP |
1797 | pr_out_section_start(dl, "port_pool"); |
1798 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_port_pool_show_cb, dl); | |
1799 | pr_out_section_end(dl); | |
1800 | return 0; | |
e6d7367d JP |
1801 | } |
1802 | ||
1803 | static int cmd_sb_port_pool_set(struct dl *dl) | |
1804 | { | |
1805 | struct nlmsghdr *nlh; | |
1806 | int err; | |
1807 | ||
1808 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_SET, | |
1809 | NLM_F_REQUEST | NLM_F_ACK); | |
1810 | ||
1811 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_POOL | | |
1812 | DL_OPT_SB_TH, DL_OPT_SB); | |
1813 | if (err) | |
1814 | return err; | |
1815 | ||
1816 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
1817 | } | |
1818 | ||
1819 | static int cmd_sb_port_pool(struct dl *dl) | |
1820 | { | |
1821 | if (dl_argv_match(dl, "help")) { | |
1822 | cmd_sb_help(); | |
1823 | return 0; | |
1824 | } else if (dl_argv_match(dl, "show") || | |
1825 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
1826 | dl_arg_inc(dl); | |
1827 | return cmd_sb_port_pool_show(dl); | |
1828 | } else if (dl_argv_match(dl, "set")) { | |
1829 | dl_arg_inc(dl); | |
1830 | return cmd_sb_port_pool_set(dl); | |
1831 | } | |
1832 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1833 | return -ENOENT; | |
1834 | } | |
1835 | ||
1836 | static int cmd_sb_port(struct dl *dl) | |
1837 | { | |
1838 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
1839 | cmd_sb_help(); | |
1840 | return 0; | |
1841 | } else if (dl_argv_match(dl, "pool")) { | |
1842 | dl_arg_inc(dl); | |
1843 | return cmd_sb_port_pool(dl); | |
1844 | } | |
1845 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1846 | return -ENOENT; | |
1847 | } | |
1848 | ||
1849 | static void pr_out_sb_tc_bind(struct dl *dl, struct nlattr **tb) | |
1850 | { | |
e3d0f0c0 JP |
1851 | pr_out_port_handle_start_arr(dl, tb, true); |
1852 | pr_out_uint(dl, "sb", | |
1853 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX])); | |
1854 | pr_out_uint(dl, "tc", | |
1855 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX])); | |
1856 | pr_out_str(dl, "type", | |
1857 | pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]))); | |
1858 | pr_out_uint(dl, "pool", | |
1859 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX])); | |
1860 | pr_out_uint(dl, "threshold", | |
e6d7367d | 1861 | mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD])); |
e3d0f0c0 | 1862 | pr_out_port_handle_end(dl); |
e6d7367d JP |
1863 | } |
1864 | ||
1865 | static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr *nlh, void *data) | |
1866 | { | |
1867 | struct dl *dl = data; | |
1868 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
1869 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
1870 | ||
1871 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
1872 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
1873 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
1874 | !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] || | |
1875 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD]) | |
1876 | return MNL_CB_ERROR; | |
1877 | pr_out_sb_tc_bind(dl, tb); | |
1878 | return MNL_CB_OK; | |
1879 | } | |
1880 | ||
1881 | static int cmd_sb_tc_bind_show(struct dl *dl) | |
1882 | { | |
1883 | struct nlmsghdr *nlh; | |
1884 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK; | |
1885 | int err; | |
1886 | ||
1887 | if (dl_argc(dl) == 0) | |
1888 | flags |= NLM_F_DUMP; | |
1889 | ||
1890 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags); | |
1891 | ||
1892 | if (dl_argc(dl) > 0) { | |
1893 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC | | |
1894 | DL_OPT_SB_TYPE, DL_OPT_SB); | |
1895 | if (err) | |
1896 | return err; | |
1897 | } | |
1898 | ||
e3d0f0c0 JP |
1899 | pr_out_section_start(dl, "tc_bind"); |
1900 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_tc_bind_show_cb, dl); | |
1901 | pr_out_section_end(dl); | |
1902 | return err; | |
e6d7367d JP |
1903 | } |
1904 | ||
1905 | static int cmd_sb_tc_bind_set(struct dl *dl) | |
1906 | { | |
1907 | struct nlmsghdr *nlh; | |
1908 | int err; | |
1909 | ||
1910 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_SET, | |
1911 | NLM_F_REQUEST | NLM_F_ACK); | |
1912 | ||
1913 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC | | |
1914 | DL_OPT_SB_TYPE | DL_OPT_SB_POOL | DL_OPT_SB_TH, | |
1915 | DL_OPT_SB); | |
1916 | if (err) | |
1917 | return err; | |
1918 | ||
1919 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
1920 | } | |
1921 | ||
1922 | static int cmd_sb_tc_bind(struct dl *dl) | |
1923 | { | |
1924 | if (dl_argv_match(dl, "help")) { | |
1925 | cmd_sb_help(); | |
1926 | return 0; | |
1927 | } else if (dl_argv_match(dl, "show") || | |
1928 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
1929 | dl_arg_inc(dl); | |
1930 | return cmd_sb_tc_bind_show(dl); | |
1931 | } else if (dl_argv_match(dl, "set")) { | |
1932 | dl_arg_inc(dl); | |
1933 | return cmd_sb_tc_bind_set(dl); | |
1934 | } | |
1935 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1936 | return -ENOENT; | |
1937 | } | |
1938 | ||
1939 | static int cmd_sb_tc(struct dl *dl) | |
1940 | { | |
1941 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
1942 | cmd_sb_help(); | |
1943 | return 0; | |
1944 | } else if (dl_argv_match(dl, "bind")) { | |
1945 | dl_arg_inc(dl); | |
1946 | return cmd_sb_tc_bind(dl); | |
1947 | } | |
1948 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
1949 | return -ENOENT; | |
1950 | } | |
1951 | ||
25ec49be JP |
1952 | struct occ_item { |
1953 | struct list_head list; | |
1954 | uint32_t index; | |
1955 | uint32_t cur; | |
1956 | uint32_t max; | |
1957 | uint32_t bound_pool_index; | |
1958 | }; | |
1959 | ||
1960 | struct occ_port { | |
1961 | struct list_head list; | |
1962 | char *bus_name; | |
1963 | char *dev_name; | |
1964 | uint32_t port_index; | |
1965 | uint32_t sb_index; | |
1966 | struct list_head pool_list; | |
1967 | struct list_head ing_tc_list; | |
1968 | struct list_head eg_tc_list; | |
1969 | }; | |
1970 | ||
1971 | struct occ_show { | |
1972 | struct dl *dl; | |
1973 | int err; | |
1974 | struct list_head port_list; | |
1975 | }; | |
1976 | ||
1977 | static struct occ_item *occ_item_alloc(void) | |
1978 | { | |
1979 | return calloc(1, sizeof(struct occ_item)); | |
1980 | } | |
1981 | ||
1982 | static void occ_item_free(struct occ_item *occ_item) | |
1983 | { | |
1984 | free(occ_item); | |
1985 | } | |
1986 | ||
1987 | static struct occ_port *occ_port_alloc(uint32_t port_index) | |
1988 | { | |
1989 | struct occ_port *occ_port; | |
1990 | ||
1991 | occ_port = calloc(1, sizeof(*occ_port)); | |
1992 | if (!occ_port) | |
1993 | return NULL; | |
1994 | occ_port->port_index = port_index; | |
1995 | INIT_LIST_HEAD(&occ_port->pool_list); | |
1996 | INIT_LIST_HEAD(&occ_port->ing_tc_list); | |
1997 | INIT_LIST_HEAD(&occ_port->eg_tc_list); | |
1998 | return occ_port; | |
1999 | } | |
2000 | ||
2001 | static void occ_port_free(struct occ_port *occ_port) | |
2002 | { | |
2003 | struct occ_item *occ_item, *tmp; | |
2004 | ||
2005 | list_for_each_entry_safe(occ_item, tmp, &occ_port->pool_list, list) | |
2006 | occ_item_free(occ_item); | |
2007 | list_for_each_entry_safe(occ_item, tmp, &occ_port->ing_tc_list, list) | |
2008 | occ_item_free(occ_item); | |
2009 | list_for_each_entry_safe(occ_item, tmp, &occ_port->eg_tc_list, list) | |
2010 | occ_item_free(occ_item); | |
2011 | } | |
2012 | ||
2013 | static struct occ_show *occ_show_alloc(struct dl *dl) | |
2014 | { | |
2015 | struct occ_show *occ_show; | |
2016 | ||
2017 | occ_show = calloc(1, sizeof(*occ_show)); | |
2018 | if (!occ_show) | |
2019 | return NULL; | |
2020 | occ_show->dl = dl; | |
2021 | INIT_LIST_HEAD(&occ_show->port_list); | |
2022 | return occ_show; | |
2023 | } | |
2024 | ||
2025 | static void occ_show_free(struct occ_show *occ_show) | |
2026 | { | |
2027 | struct occ_port *occ_port, *tmp; | |
2028 | ||
2029 | list_for_each_entry_safe(occ_port, tmp, &occ_show->port_list, list) | |
2030 | occ_port_free(occ_port); | |
2031 | } | |
2032 | ||
2033 | static struct occ_port *occ_port_get(struct occ_show *occ_show, | |
2034 | struct nlattr **tb) | |
2035 | { | |
2036 | struct occ_port *occ_port; | |
2037 | uint32_t port_index; | |
2038 | ||
2039 | port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]); | |
2040 | ||
2041 | list_for_each_entry_reverse(occ_port, &occ_show->port_list, list) { | |
2042 | if (occ_port->port_index == port_index) | |
2043 | return occ_port; | |
2044 | } | |
2045 | occ_port = occ_port_alloc(port_index); | |
2046 | if (!occ_port) | |
2047 | return NULL; | |
2048 | list_add_tail(&occ_port->list, &occ_show->port_list); | |
2049 | return occ_port; | |
2050 | } | |
2051 | ||
2052 | static void pr_out_occ_show_item_list(const char *label, struct list_head *list, | |
2053 | bool bound_pool) | |
2054 | { | |
2055 | struct occ_item *occ_item; | |
2056 | int i = 1; | |
2057 | ||
2058 | pr_out_sp(7, " %s:", label); | |
2059 | list_for_each_entry(occ_item, list, list) { | |
2060 | if ((i - 1) % 4 == 0 && i != 1) | |
2061 | pr_out_sp(7, " "); | |
2062 | if (bound_pool) | |
2063 | pr_out_sp(7, "%2u(%u):", occ_item->index, | |
2064 | occ_item->bound_pool_index); | |
2065 | else | |
2066 | pr_out_sp(7, "%2u:", occ_item->index); | |
2067 | pr_out_sp(15, "%7u/%u", occ_item->cur, occ_item->max); | |
2068 | if (i++ % 4 == 0) | |
2069 | pr_out("\n"); | |
2070 | } | |
2071 | if ((i - 1) % 4 != 0) | |
2072 | pr_out("\n"); | |
2073 | } | |
2074 | ||
e3d0f0c0 JP |
2075 | static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label, |
2076 | struct list_head *list, | |
2077 | bool bound_pool) | |
2078 | { | |
2079 | struct occ_item *occ_item; | |
2080 | char buf[32]; | |
2081 | ||
2082 | jsonw_name(dl->jw, label); | |
2083 | jsonw_start_object(dl->jw); | |
2084 | list_for_each_entry(occ_item, list, list) { | |
2085 | sprintf(buf, "%u", occ_item->index); | |
2086 | jsonw_name(dl->jw, buf); | |
2087 | jsonw_start_object(dl->jw); | |
2088 | if (bound_pool) | |
2089 | jsonw_uint_field(dl->jw, "bound_pool", | |
2090 | occ_item->bound_pool_index); | |
2091 | jsonw_uint_field(dl->jw, "current", occ_item->cur); | |
2092 | jsonw_uint_field(dl->jw, "max", occ_item->max); | |
2093 | jsonw_end_object(dl->jw); | |
2094 | } | |
2095 | jsonw_end_object(dl->jw); | |
2096 | } | |
2097 | ||
2098 | static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port) | |
25ec49be | 2099 | { |
e3d0f0c0 JP |
2100 | if (dl->json_output) { |
2101 | pr_out_json_occ_show_item_list(dl, "pool", | |
2102 | &occ_port->pool_list, false); | |
2103 | pr_out_json_occ_show_item_list(dl, "itc", | |
2104 | &occ_port->ing_tc_list, true); | |
2105 | pr_out_json_occ_show_item_list(dl, "etc", | |
2106 | &occ_port->eg_tc_list, true); | |
2107 | } else { | |
2108 | pr_out("\n"); | |
2109 | pr_out_occ_show_item_list("pool", &occ_port->pool_list, false); | |
2110 | pr_out_occ_show_item_list("itc", &occ_port->ing_tc_list, true); | |
2111 | pr_out_occ_show_item_list("etc", &occ_port->eg_tc_list, true); | |
2112 | } | |
25ec49be JP |
2113 | } |
2114 | ||
2115 | static void pr_out_occ_show(struct occ_show *occ_show) | |
2116 | { | |
2117 | struct dl *dl = occ_show->dl; | |
2118 | struct dl_opts *opts = &dl->opts; | |
2119 | struct occ_port *occ_port; | |
2120 | ||
2121 | list_for_each_entry(occ_port, &occ_show->port_list, list) { | |
e3d0f0c0 JP |
2122 | __pr_out_port_handle_start(dl, opts->bus_name, opts->dev_name, |
2123 | occ_port->port_index, true, false); | |
2124 | pr_out_occ_show_port(dl, occ_port); | |
2125 | pr_out_port_handle_end(dl); | |
25ec49be JP |
2126 | } |
2127 | } | |
2128 | ||
2129 | static void cmd_sb_occ_port_pool_process(struct occ_show *occ_show, | |
2130 | struct nlattr **tb) | |
2131 | { | |
2132 | struct occ_port *occ_port; | |
2133 | struct occ_item *occ_item; | |
2134 | ||
2135 | if (occ_show->err || !dl_dump_filter(occ_show->dl, tb)) | |
2136 | return; | |
2137 | ||
2138 | occ_port = occ_port_get(occ_show, tb); | |
2139 | if (!occ_port) { | |
2140 | occ_show->err = -ENOMEM; | |
2141 | return; | |
2142 | } | |
2143 | ||
2144 | occ_item = occ_item_alloc(); | |
2145 | if (!occ_item) { | |
2146 | occ_show->err = -ENOMEM; | |
2147 | return; | |
2148 | } | |
2149 | occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]); | |
2150 | occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]); | |
2151 | occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]); | |
2152 | list_add_tail(&occ_item->list, &occ_port->pool_list); | |
2153 | } | |
2154 | ||
2155 | static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr *nlh, void *data) | |
2156 | { | |
2157 | struct occ_show *occ_show = data; | |
2158 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2159 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2160 | ||
2161 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2162 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2163 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
2164 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || | |
2165 | !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX]) | |
2166 | return MNL_CB_ERROR; | |
2167 | cmd_sb_occ_port_pool_process(occ_show, tb); | |
2168 | return MNL_CB_OK; | |
2169 | } | |
2170 | ||
2171 | static void cmd_sb_occ_tc_pool_process(struct occ_show *occ_show, | |
2172 | struct nlattr **tb) | |
2173 | { | |
2174 | struct occ_port *occ_port; | |
2175 | struct occ_item *occ_item; | |
2176 | uint8_t pool_type; | |
2177 | ||
2178 | if (occ_show->err || !dl_dump_filter(occ_show->dl, tb)) | |
2179 | return; | |
2180 | ||
2181 | occ_port = occ_port_get(occ_show, tb); | |
2182 | if (!occ_port) { | |
2183 | occ_show->err = -ENOMEM; | |
2184 | return; | |
2185 | } | |
2186 | ||
2187 | occ_item = occ_item_alloc(); | |
2188 | if (!occ_item) { | |
2189 | occ_show->err = -ENOMEM; | |
2190 | return; | |
2191 | } | |
2192 | occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX]); | |
2193 | occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]); | |
2194 | occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]); | |
2195 | occ_item->bound_pool_index = | |
2196 | mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]); | |
2197 | pool_type = mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]); | |
2198 | if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS) | |
2199 | list_add_tail(&occ_item->list, &occ_port->ing_tc_list); | |
2200 | else if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS) | |
2201 | list_add_tail(&occ_item->list, &occ_port->eg_tc_list); | |
2202 | else | |
2203 | occ_item_free(occ_item); | |
2204 | } | |
2205 | ||
2206 | static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr *nlh, void *data) | |
2207 | { | |
2208 | struct occ_show *occ_show = data; | |
2209 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2210 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2211 | ||
2212 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2213 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2214 | !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] || | |
2215 | !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] || | |
2216 | !tb[DEVLINK_ATTR_SB_POOL_INDEX] || | |
2217 | !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX]) | |
2218 | return MNL_CB_ERROR; | |
2219 | cmd_sb_occ_tc_pool_process(occ_show, tb); | |
2220 | return MNL_CB_OK; | |
2221 | } | |
2222 | ||
2223 | static int cmd_sb_occ_show(struct dl *dl) | |
2224 | { | |
2225 | struct nlmsghdr *nlh; | |
2226 | struct occ_show *occ_show; | |
2227 | uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP; | |
2228 | int err; | |
2229 | ||
2230 | err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP, DL_OPT_SB); | |
2231 | if (err) | |
2232 | return err; | |
2233 | ||
2234 | occ_show = occ_show_alloc(dl); | |
2235 | if (!occ_show) | |
2236 | return -ENOMEM; | |
2237 | ||
2238 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags); | |
2239 | ||
2240 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, | |
2241 | cmd_sb_occ_port_pool_process_cb, occ_show); | |
2242 | if (err) | |
2243 | goto out; | |
2244 | ||
2245 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags); | |
2246 | ||
2247 | err = _mnlg_socket_sndrcv(dl->nlg, nlh, | |
2248 | cmd_sb_occ_tc_pool_process_cb, occ_show); | |
2249 | if (err) | |
2250 | goto out; | |
2251 | ||
e3d0f0c0 | 2252 | pr_out_section_start(dl, "occupancy"); |
25ec49be | 2253 | pr_out_occ_show(occ_show); |
e3d0f0c0 | 2254 | pr_out_section_end(dl); |
25ec49be JP |
2255 | |
2256 | out: | |
2257 | occ_show_free(occ_show); | |
2258 | return err; | |
2259 | } | |
2260 | ||
2261 | static int cmd_sb_occ_snapshot(struct dl *dl) | |
2262 | { | |
2263 | struct nlmsghdr *nlh; | |
2264 | int err; | |
2265 | ||
2266 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_SNAPSHOT, | |
2267 | NLM_F_REQUEST | NLM_F_ACK); | |
2268 | ||
2269 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB); | |
2270 | if (err) | |
2271 | return err; | |
2272 | ||
2273 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2274 | } | |
2275 | ||
2276 | static int cmd_sb_occ_clearmax(struct dl *dl) | |
2277 | { | |
2278 | struct nlmsghdr *nlh; | |
2279 | int err; | |
2280 | ||
2281 | nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_MAX_CLEAR, | |
2282 | NLM_F_REQUEST | NLM_F_ACK); | |
2283 | ||
2284 | err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB); | |
2285 | if (err) | |
2286 | return err; | |
2287 | ||
2288 | return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL); | |
2289 | } | |
2290 | ||
2291 | static int cmd_sb_occ(struct dl *dl) | |
2292 | { | |
2293 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
2294 | cmd_sb_help(); | |
2295 | return 0; | |
2296 | } else if (dl_argv_match(dl, "show") || | |
2297 | dl_argv_match(dl, "list")) { | |
2298 | dl_arg_inc(dl); | |
2299 | return cmd_sb_occ_show(dl); | |
2300 | } else if (dl_argv_match(dl, "snapshot")) { | |
2301 | dl_arg_inc(dl); | |
2302 | return cmd_sb_occ_snapshot(dl); | |
2303 | } else if (dl_argv_match(dl, "clearmax")) { | |
2304 | dl_arg_inc(dl); | |
2305 | return cmd_sb_occ_clearmax(dl); | |
2306 | } | |
2307 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2308 | return -ENOENT; | |
2309 | } | |
2310 | ||
e6d7367d JP |
2311 | static int cmd_sb(struct dl *dl) |
2312 | { | |
2313 | if (dl_argv_match(dl, "help")) { | |
2314 | cmd_sb_help(); | |
2315 | return 0; | |
2316 | } else if (dl_argv_match(dl, "show") || | |
2317 | dl_argv_match(dl, "list") || dl_no_arg(dl)) { | |
2318 | dl_arg_inc(dl); | |
2319 | return cmd_sb_show(dl); | |
2320 | } else if (dl_argv_match(dl, "pool")) { | |
2321 | dl_arg_inc(dl); | |
2322 | return cmd_sb_pool(dl); | |
2323 | } else if (dl_argv_match(dl, "port")) { | |
2324 | dl_arg_inc(dl); | |
2325 | return cmd_sb_port(dl); | |
2326 | } else if (dl_argv_match(dl, "tc")) { | |
2327 | dl_arg_inc(dl); | |
2328 | return cmd_sb_tc(dl); | |
25ec49be JP |
2329 | } else if (dl_argv_match(dl, "occupancy")) { |
2330 | dl_arg_inc(dl); | |
2331 | return cmd_sb_occ(dl); | |
e6d7367d JP |
2332 | } |
2333 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2334 | return -ENOENT; | |
2335 | } | |
2336 | ||
a3c4b484 JP |
2337 | static const char *cmd_name(uint8_t cmd) |
2338 | { | |
2339 | switch (cmd) { | |
2340 | case DEVLINK_CMD_UNSPEC: return "unspec"; | |
2341 | case DEVLINK_CMD_GET: return "get"; | |
2342 | case DEVLINK_CMD_SET: return "set"; | |
2343 | case DEVLINK_CMD_NEW: return "new"; | |
2344 | case DEVLINK_CMD_DEL: return "del"; | |
2345 | case DEVLINK_CMD_PORT_GET: return "get"; | |
2346 | case DEVLINK_CMD_PORT_SET: return "set"; | |
2347 | case DEVLINK_CMD_PORT_NEW: return "net"; | |
2348 | case DEVLINK_CMD_PORT_DEL: return "del"; | |
2349 | default: return "<unknown cmd>"; | |
2350 | } | |
2351 | } | |
2352 | ||
2353 | static const char *cmd_obj(uint8_t cmd) | |
2354 | { | |
2355 | switch (cmd) { | |
2356 | case DEVLINK_CMD_UNSPEC: return "unspec"; | |
2357 | case DEVLINK_CMD_GET: | |
2358 | case DEVLINK_CMD_SET: | |
2359 | case DEVLINK_CMD_NEW: | |
2360 | case DEVLINK_CMD_DEL: | |
2361 | return "dev"; | |
2362 | case DEVLINK_CMD_PORT_GET: | |
2363 | case DEVLINK_CMD_PORT_SET: | |
2364 | case DEVLINK_CMD_PORT_NEW: | |
2365 | case DEVLINK_CMD_PORT_DEL: | |
2366 | return "port"; | |
2367 | default: return "<unknown obj>"; | |
2368 | } | |
2369 | } | |
2370 | ||
2371 | static void pr_out_mon_header(uint8_t cmd) | |
2372 | { | |
2373 | pr_out("[%s,%s] ", cmd_obj(cmd), cmd_name(cmd)); | |
2374 | } | |
2375 | ||
2376 | static bool cmd_filter_check(struct dl *dl, uint8_t cmd) | |
2377 | { | |
2378 | const char *obj = cmd_obj(cmd); | |
2379 | unsigned int index = 0; | |
2380 | const char *cur_obj; | |
2381 | ||
2382 | if (dl_no_arg(dl)) | |
2383 | return true; | |
2384 | while ((cur_obj = dl_argv_index(dl, index++))) { | |
2385 | if (strcmp(cur_obj, obj) == 0 || strcmp(cur_obj, "all") == 0) | |
2386 | return true; | |
2387 | } | |
2388 | return false; | |
2389 | } | |
2390 | ||
2391 | static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data) | |
2392 | { | |
2393 | struct dl *dl = data; | |
2394 | struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {}; | |
2395 | struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh); | |
2396 | uint8_t cmd = genl->cmd; | |
2397 | ||
2398 | if (!cmd_filter_check(dl, cmd)) | |
2399 | return MNL_CB_OK; | |
2400 | ||
2401 | switch (cmd) { | |
2402 | case DEVLINK_CMD_GET: /* fall through */ | |
2403 | case DEVLINK_CMD_SET: /* fall through */ | |
2404 | case DEVLINK_CMD_NEW: /* fall through */ | |
2405 | case DEVLINK_CMD_DEL: | |
2406 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2407 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME]) | |
2408 | return MNL_CB_ERROR; | |
2409 | pr_out_mon_header(genl->cmd); | |
e3d0f0c0 | 2410 | pr_out_dev(dl, tb); |
a3c4b484 JP |
2411 | break; |
2412 | case DEVLINK_CMD_PORT_GET: /* fall through */ | |
2413 | case DEVLINK_CMD_PORT_SET: /* fall through */ | |
2414 | case DEVLINK_CMD_PORT_NEW: /* fall through */ | |
2415 | case DEVLINK_CMD_PORT_DEL: | |
2416 | mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb); | |
2417 | if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] || | |
2418 | !tb[DEVLINK_ATTR_PORT_INDEX]) | |
2419 | return MNL_CB_ERROR; | |
2420 | pr_out_mon_header(genl->cmd); | |
e3d0f0c0 | 2421 | pr_out_port(dl, tb); |
a3c4b484 JP |
2422 | break; |
2423 | } | |
2424 | return MNL_CB_OK; | |
2425 | } | |
2426 | ||
2427 | static int cmd_mon_show(struct dl *dl) | |
2428 | { | |
2429 | int err; | |
2430 | unsigned int index = 0; | |
2431 | const char *cur_obj; | |
2432 | ||
2433 | while ((cur_obj = dl_argv_index(dl, index++))) { | |
2434 | if (strcmp(cur_obj, "all") != 0 && | |
2435 | strcmp(cur_obj, "dev") != 0 && | |
2436 | strcmp(cur_obj, "port") != 0) { | |
2437 | pr_err("Unknown object \"%s\"\n", cur_obj); | |
2438 | return -EINVAL; | |
2439 | } | |
2440 | } | |
2441 | err = _mnlg_socket_group_add(dl->nlg, DEVLINK_GENL_MCGRP_CONFIG_NAME); | |
2442 | if (err) | |
2443 | return err; | |
2444 | err = _mnlg_socket_recv_run(dl->nlg, cmd_mon_show_cb, dl); | |
2445 | if (err) | |
2446 | return err; | |
2447 | return 0; | |
2448 | } | |
2449 | ||
2450 | static void cmd_mon_help(void) | |
2451 | { | |
7a9466db | 2452 | pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n" |
a3c4b484 JP |
2453 | "where OBJECT-LIST := { dev | port }\n"); |
2454 | } | |
2455 | ||
2456 | static int cmd_mon(struct dl *dl) | |
2457 | { | |
2458 | if (dl_argv_match(dl, "help")) { | |
2459 | cmd_mon_help(); | |
2460 | return 0; | |
2461 | } else if (dl_no_arg(dl)) { | |
2462 | dl_arg_inc(dl); | |
2463 | return cmd_mon_show(dl); | |
2464 | } | |
2465 | pr_err("Command \"%s\" not found\n", dl_argv(dl)); | |
2466 | return -ENOENT; | |
2467 | } | |
2468 | ||
2469 | static void help(void) | |
2470 | { | |
7a9466db | 2471 | pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n" |
e6d7367d | 2472 | "where OBJECT := { dev | port | sb | monitor }\n" |
43f35be4 | 2473 | " OPTIONS := { -V[ersion] | -n[no-nice-names] }\n"); |
a3c4b484 JP |
2474 | } |
2475 | ||
2476 | static int dl_cmd(struct dl *dl) | |
2477 | { | |
2478 | if (dl_argv_match(dl, "help") || dl_no_arg(dl)) { | |
2479 | help(); | |
2480 | return 0; | |
2481 | } else if (dl_argv_match(dl, "dev")) { | |
2482 | dl_arg_inc(dl); | |
2483 | return cmd_dev(dl); | |
2484 | } else if (dl_argv_match(dl, "port")) { | |
2485 | dl_arg_inc(dl); | |
2486 | return cmd_port(dl); | |
e6d7367d JP |
2487 | } else if (dl_argv_match(dl, "sb")) { |
2488 | dl_arg_inc(dl); | |
2489 | return cmd_sb(dl); | |
a3c4b484 JP |
2490 | } else if (dl_argv_match(dl, "monitor")) { |
2491 | dl_arg_inc(dl); | |
2492 | return cmd_mon(dl); | |
2493 | } | |
2494 | pr_err("Object \"%s\" not found\n", dl_argv(dl)); | |
2495 | return -ENOENT; | |
2496 | } | |
2497 | ||
2498 | static int dl_init(struct dl *dl, int argc, char **argv) | |
2499 | { | |
2500 | int err; | |
2501 | ||
2502 | dl->argc = argc; | |
2503 | dl->argv = argv; | |
2504 | ||
2505 | dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION); | |
2506 | if (!dl->nlg) { | |
2507 | pr_err("Failed to connect to devlink Netlink\n"); | |
2508 | return -errno; | |
2509 | } | |
2510 | ||
2511 | err = ifname_map_init(dl); | |
2512 | if (err) { | |
2513 | pr_err("Failed to create index map\n"); | |
2514 | goto err_ifname_map_create; | |
2515 | } | |
e3d0f0c0 JP |
2516 | if (dl->json_output) { |
2517 | dl->jw = jsonw_new(stdout); | |
2518 | if (!dl->jw) { | |
2519 | pr_err("Failed to create JSON writer\n"); | |
2520 | goto err_json_new; | |
2521 | } | |
2522 | jsonw_pretty(dl->jw, dl->pretty_output); | |
2523 | } | |
a3c4b484 JP |
2524 | return 0; |
2525 | ||
e3d0f0c0 JP |
2526 | err_json_new: |
2527 | ifname_map_fini(dl); | |
a3c4b484 JP |
2528 | err_ifname_map_create: |
2529 | mnlg_socket_close(dl->nlg); | |
2530 | return err; | |
2531 | } | |
2532 | ||
2533 | static void dl_fini(struct dl *dl) | |
2534 | { | |
e3d0f0c0 JP |
2535 | if (dl->json_output) |
2536 | jsonw_destroy(&dl->jw); | |
a3c4b484 JP |
2537 | ifname_map_fini(dl); |
2538 | mnlg_socket_close(dl->nlg); | |
2539 | } | |
2540 | ||
2541 | static struct dl *dl_alloc(void) | |
2542 | { | |
2543 | struct dl *dl; | |
2544 | ||
2545 | dl = calloc(1, sizeof(*dl)); | |
2546 | if (!dl) | |
2547 | return NULL; | |
2548 | return dl; | |
2549 | } | |
2550 | ||
2551 | static void dl_free(struct dl *dl) | |
2552 | { | |
2553 | free(dl); | |
2554 | } | |
2555 | ||
2556 | int main(int argc, char **argv) | |
2557 | { | |
2558 | static const struct option long_options[] = { | |
2559 | { "Version", no_argument, NULL, 'V' }, | |
43f35be4 | 2560 | { "no-nice-names", no_argument, NULL, 'n' }, |
e3d0f0c0 JP |
2561 | { "json", no_argument, NULL, 'j' }, |
2562 | { "pretty", no_argument, NULL, 'p' }, | |
a3c4b484 JP |
2563 | { NULL, 0, NULL, 0 } |
2564 | }; | |
2565 | struct dl *dl; | |
2566 | int opt; | |
2567 | int err; | |
2568 | int ret; | |
2569 | ||
43f35be4 JP |
2570 | dl = dl_alloc(); |
2571 | if (!dl) { | |
2572 | pr_err("Failed to allocate memory for devlink\n"); | |
2573 | return EXIT_FAILURE; | |
2574 | } | |
2575 | ||
e3d0f0c0 | 2576 | while ((opt = getopt_long(argc, argv, "Vnjp", |
a3c4b484 JP |
2577 | long_options, NULL)) >= 0) { |
2578 | ||
2579 | switch (opt) { | |
2580 | case 'V': | |
2581 | printf("devlink utility, iproute2-ss%s\n", SNAPSHOT); | |
2582 | return EXIT_SUCCESS; | |
43f35be4 JP |
2583 | case 'n': |
2584 | dl->no_nice_names = true; | |
2585 | break; | |
e3d0f0c0 JP |
2586 | case 'j': |
2587 | dl->json_output = true; | |
2588 | break; | |
2589 | case 'p': | |
2590 | dl->pretty_output = true; | |
2591 | break; | |
a3c4b484 JP |
2592 | default: |
2593 | pr_err("Unknown option.\n"); | |
2594 | help(); | |
2595 | return EXIT_FAILURE; | |
2596 | } | |
2597 | } | |
2598 | ||
2599 | argc -= optind; | |
2600 | argv += optind; | |
2601 | ||
a3c4b484 JP |
2602 | err = dl_init(dl, argc, argv); |
2603 | if (err) { | |
2604 | ret = EXIT_FAILURE; | |
2605 | goto dl_free; | |
2606 | } | |
2607 | ||
2608 | err = dl_cmd(dl); | |
2609 | if (err) { | |
2610 | ret = EXIT_FAILURE; | |
2611 | goto dl_fini; | |
2612 | } | |
2613 | ||
2614 | ret = EXIT_SUCCESS; | |
2615 | ||
2616 | dl_fini: | |
2617 | dl_fini(dl); | |
2618 | dl_free: | |
2619 | dl_free(dl); | |
2620 | ||
2621 | return ret; | |
2622 | } |