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