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