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