]> git.proxmox.com Git - mirror_iproute2.git/blob - devlink/devlink.c
Merge branch 'devlink-reload' into next
[mirror_iproute2.git] / devlink / devlink.c
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 <stdarg.h>
15 #include <string.h>
16 #include <stdbool.h>
17 #include <unistd.h>
18 #include <getopt.h>
19 #include <limits.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <signal.h>
23 #include <time.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <sys/sysinfo.h>
27 #define _LINUX_SYSINFO_H /* avoid collision with musl header */
28 #include <linux/genetlink.h>
29 #include <linux/devlink.h>
30 #include <linux/netlink.h>
31 #include <libmnl/libmnl.h>
32 #include <netinet/ether.h>
33 #include <sys/select.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <rt_names.h>
38
39 #include "version.h"
40 #include "list.h"
41 #include "mnlg.h"
42 #include "json_print.h"
43 #include "utils.h"
44 #include "namespace.h"
45
46 #define ESWITCH_MODE_LEGACY "legacy"
47 #define ESWITCH_MODE_SWITCHDEV "switchdev"
48 #define ESWITCH_INLINE_MODE_NONE "none"
49 #define ESWITCH_INLINE_MODE_LINK "link"
50 #define ESWITCH_INLINE_MODE_NETWORK "network"
51 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
52
53 #define ESWITCH_ENCAP_MODE_NONE "none"
54 #define ESWITCH_ENCAP_MODE_BASIC "basic"
55
56 #define PARAM_CMODE_RUNTIME_STR "runtime"
57 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
58 #define PARAM_CMODE_PERMANENT_STR "permanent"
59 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
60
61 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
62 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
63 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
64
65 static int g_new_line_count;
66 static int g_indent_level;
67 static bool g_indent_newline;
68
69 #define INDENT_STR_STEP 2
70 #define INDENT_STR_MAXLEN 32
71 static char g_indent_str[INDENT_STR_MAXLEN + 1] = "";
72
73 static void __attribute__((format(printf, 1, 2)))
74 pr_err(const char *fmt, ...)
75 {
76 va_list ap;
77
78 va_start(ap, fmt);
79 vfprintf(stderr, fmt, ap);
80 va_end(ap);
81 }
82
83 static void __attribute__((format(printf, 1, 2)))
84 pr_out(const char *fmt, ...)
85 {
86 va_list ap;
87
88 if (g_indent_newline) {
89 printf("%s", g_indent_str);
90 g_indent_newline = false;
91 }
92 va_start(ap, fmt);
93 vprintf(fmt, ap);
94 va_end(ap);
95 g_new_line_count = 0;
96 }
97
98 static void __attribute__((format(printf, 2, 3)))
99 pr_out_sp(unsigned int num, const char *fmt, ...)
100 {
101 va_list ap;
102 int ret;
103
104 va_start(ap, fmt);
105 ret = vprintf(fmt, ap);
106 va_end(ap);
107
108 if (ret < num)
109 printf("%*s", num - ret, "");
110 g_new_line_count = 0; \
111 }
112
113 static void __attribute__((format(printf, 1, 2)))
114 pr_out_tty(const char *fmt, ...)
115 {
116 va_list ap;
117
118 if (!isatty(STDOUT_FILENO))
119 return;
120 va_start(ap, fmt);
121 vprintf(fmt, ap);
122 va_end(ap);
123 }
124
125 static void __pr_out_indent_inc(void)
126 {
127 if (g_indent_level + INDENT_STR_STEP > INDENT_STR_MAXLEN)
128 return;
129 g_indent_level += INDENT_STR_STEP;
130 memset(g_indent_str, ' ', sizeof(g_indent_str));
131 g_indent_str[g_indent_level] = '\0';
132 }
133
134 static void __pr_out_indent_dec(void)
135 {
136 if (g_indent_level - INDENT_STR_STEP < 0)
137 return;
138 g_indent_level -= INDENT_STR_STEP;
139 g_indent_str[g_indent_level] = '\0';
140 }
141
142 static void __pr_out_newline(void)
143 {
144 if (g_new_line_count < 1) {
145 pr_out("\n");
146 g_indent_newline = true;
147 }
148 g_new_line_count++;
149 }
150
151 static int _mnlg_socket_recv_run(struct mnlg_socket *nlg,
152 mnl_cb_t data_cb, void *data)
153 {
154 int err;
155
156 err = mnlg_socket_recv_run(nlg, data_cb, data);
157 if (err < 0) {
158 pr_err("devlink answers: %s\n", strerror(errno));
159 return -errno;
160 }
161 return 0;
162 }
163
164 static void dummy_signal_handler(int signum)
165 {
166 }
167
168 static int _mnlg_socket_recv_run_intr(struct mnlg_socket *nlg,
169 mnl_cb_t data_cb, void *data)
170 {
171 struct sigaction act, oact;
172 int err;
173
174 act.sa_handler = dummy_signal_handler;
175 sigemptyset(&act.sa_mask);
176 act.sa_flags = SA_NODEFER;
177
178 sigaction(SIGINT, &act, &oact);
179 err = mnlg_socket_recv_run(nlg, data_cb, data);
180 sigaction(SIGINT, &oact, NULL);
181 if (err < 0 && errno != EINTR) {
182 pr_err("devlink answers: %s\n", strerror(errno));
183 return -errno;
184 }
185 return 0;
186 }
187
188 static int _mnlg_socket_send(struct mnlg_socket *nlg,
189 const struct nlmsghdr *nlh)
190 {
191 int err;
192
193 err = mnlg_socket_send(nlg, nlh);
194 if (err < 0) {
195 pr_err("Failed to call mnlg_socket_send\n");
196 return -errno;
197 }
198 return 0;
199 }
200
201 static int _mnlg_socket_sndrcv(struct mnlg_socket *nlg,
202 const struct nlmsghdr *nlh,
203 mnl_cb_t data_cb, void *data)
204 {
205 int err;
206
207 err = _mnlg_socket_send(nlg, nlh);
208 if (err)
209 return err;
210 return _mnlg_socket_recv_run(nlg, data_cb, data);
211 }
212
213 static int _mnlg_socket_group_add(struct mnlg_socket *nlg,
214 const char *group_name)
215 {
216 int err;
217
218 err = mnlg_socket_group_add(nlg, group_name);
219 if (err < 0) {
220 pr_err("Failed to call mnlg_socket_group_add\n");
221 return -errno;
222 }
223 return 0;
224 }
225
226 struct ifname_map {
227 struct list_head list;
228 char *bus_name;
229 char *dev_name;
230 uint32_t port_index;
231 char *ifname;
232 };
233
234 static struct ifname_map *ifname_map_alloc(const char *bus_name,
235 const char *dev_name,
236 uint32_t port_index,
237 const char *ifname)
238 {
239 struct ifname_map *ifname_map;
240
241 ifname_map = calloc(1, sizeof(*ifname_map));
242 if (!ifname_map)
243 return NULL;
244 ifname_map->bus_name = strdup(bus_name);
245 ifname_map->dev_name = strdup(dev_name);
246 ifname_map->port_index = port_index;
247 ifname_map->ifname = strdup(ifname);
248 if (!ifname_map->bus_name || !ifname_map->dev_name ||
249 !ifname_map->ifname) {
250 free(ifname_map->ifname);
251 free(ifname_map->dev_name);
252 free(ifname_map->bus_name);
253 free(ifname_map);
254 return NULL;
255 }
256 return ifname_map;
257 }
258
259 static void ifname_map_free(struct ifname_map *ifname_map)
260 {
261 free(ifname_map->ifname);
262 free(ifname_map->dev_name);
263 free(ifname_map->bus_name);
264 free(ifname_map);
265 }
266
267 #define DL_OPT_HANDLE BIT(0)
268 #define DL_OPT_HANDLEP BIT(1)
269 #define DL_OPT_PORT_TYPE BIT(2)
270 #define DL_OPT_PORT_COUNT BIT(3)
271 #define DL_OPT_SB BIT(4)
272 #define DL_OPT_SB_POOL BIT(5)
273 #define DL_OPT_SB_SIZE BIT(6)
274 #define DL_OPT_SB_TYPE BIT(7)
275 #define DL_OPT_SB_THTYPE BIT(8)
276 #define DL_OPT_SB_TH BIT(9)
277 #define DL_OPT_SB_TC BIT(10)
278 #define DL_OPT_ESWITCH_MODE BIT(11)
279 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
280 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
281 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
282 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
283 #define DL_OPT_RESOURCE_PATH BIT(16)
284 #define DL_OPT_RESOURCE_SIZE BIT(17)
285 #define DL_OPT_PARAM_NAME BIT(18)
286 #define DL_OPT_PARAM_VALUE BIT(19)
287 #define DL_OPT_PARAM_CMODE BIT(20)
288 #define DL_OPT_HANDLE_REGION BIT(21)
289 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
290 #define DL_OPT_REGION_ADDRESS BIT(23)
291 #define DL_OPT_REGION_LENGTH BIT(24)
292 #define DL_OPT_FLASH_FILE_NAME BIT(25)
293 #define DL_OPT_FLASH_COMPONENT BIT(26)
294 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
295 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
296 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
297 #define DL_OPT_TRAP_NAME BIT(30)
298 #define DL_OPT_TRAP_ACTION BIT(31)
299 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
300 #define DL_OPT_NETNS BIT(33)
301 #define DL_OPT_TRAP_POLICER_ID BIT(34)
302 #define DL_OPT_TRAP_POLICER_RATE BIT(35)
303 #define DL_OPT_TRAP_POLICER_BURST BIT(36)
304 #define DL_OPT_HEALTH_REPORTER_AUTO_DUMP BIT(37)
305 #define DL_OPT_PORT_FUNCTION_HW_ADDR BIT(38)
306 #define DL_OPT_FLASH_OVERWRITE BIT(39)
307 #define DL_OPT_RELOAD_ACTION BIT(40)
308 #define DL_OPT_RELOAD_LIMIT BIT(41)
309
310 struct dl_opts {
311 uint64_t present; /* flags of present items */
312 char *bus_name;
313 char *dev_name;
314 uint32_t port_index;
315 enum devlink_port_type port_type;
316 uint32_t port_count;
317 uint32_t sb_index;
318 uint16_t sb_pool_index;
319 uint32_t sb_pool_size;
320 enum devlink_sb_pool_type sb_pool_type;
321 enum devlink_sb_threshold_type sb_pool_thtype;
322 uint32_t sb_threshold;
323 uint16_t sb_tc_index;
324 enum devlink_eswitch_mode eswitch_mode;
325 enum devlink_eswitch_inline_mode eswitch_inline_mode;
326 const char *dpipe_table_name;
327 bool dpipe_counters_enabled;
328 enum devlink_eswitch_encap_mode eswitch_encap_mode;
329 const char *resource_path;
330 uint64_t resource_size;
331 uint32_t resource_id;
332 bool resource_id_valid;
333 const char *param_name;
334 const char *param_value;
335 enum devlink_param_cmode cmode;
336 char *region_name;
337 uint32_t region_snapshot_id;
338 uint64_t region_address;
339 uint64_t region_length;
340 const char *flash_file_name;
341 const char *flash_component;
342 const char *reporter_name;
343 uint64_t reporter_graceful_period;
344 bool reporter_auto_recover;
345 bool reporter_auto_dump;
346 const char *trap_name;
347 const char *trap_group_name;
348 enum devlink_trap_action trap_action;
349 bool netns_is_pid;
350 uint32_t netns;
351 uint32_t trap_policer_id;
352 uint64_t trap_policer_rate;
353 uint64_t trap_policer_burst;
354 char port_function_hw_addr[MAX_ADDR_LEN];
355 uint32_t port_function_hw_addr_len;
356 uint32_t overwrite_mask;
357 enum devlink_reload_action reload_action;
358 enum devlink_reload_limit reload_limit;
359 };
360
361 struct dl {
362 struct mnlg_socket *nlg;
363 struct list_head ifname_map_list;
364 int argc;
365 char **argv;
366 bool no_nice_names;
367 struct dl_opts opts;
368 bool json_output;
369 bool pretty_output;
370 bool verbose;
371 bool stats;
372 struct {
373 bool present;
374 char *bus_name;
375 char *dev_name;
376 uint32_t port_index;
377 } arr_last;
378 };
379
380 static int dl_argc(struct dl *dl)
381 {
382 return dl->argc;
383 }
384
385 static char *dl_argv(struct dl *dl)
386 {
387 if (dl_argc(dl) == 0)
388 return NULL;
389 return *dl->argv;
390 }
391
392 static void dl_arg_inc(struct dl *dl)
393 {
394 if (dl_argc(dl) == 0)
395 return;
396 dl->argc--;
397 dl->argv++;
398 }
399
400 static void dl_arg_dec(struct dl *dl)
401 {
402 dl->argc++;
403 dl->argv--;
404 }
405
406 static char *dl_argv_next(struct dl *dl)
407 {
408 char *ret;
409
410 if (dl_argc(dl) == 0)
411 return NULL;
412
413 ret = *dl->argv;
414 dl_arg_inc(dl);
415 return ret;
416 }
417
418 static char *dl_argv_index(struct dl *dl, unsigned int index)
419 {
420 if (index >= dl_argc(dl))
421 return NULL;
422 return dl->argv[index];
423 }
424
425 static int strcmpx(const char *str1, const char *str2)
426 {
427 if (strlen(str1) > strlen(str2))
428 return -1;
429 return strncmp(str1, str2, strlen(str1));
430 }
431
432 static bool dl_argv_match(struct dl *dl, const char *pattern)
433 {
434 if (dl_argc(dl) == 0)
435 return false;
436 return strcmpx(dl_argv(dl), pattern) == 0;
437 }
438
439 static bool dl_no_arg(struct dl *dl)
440 {
441 return dl_argc(dl) == 0;
442 }
443
444 static void __pr_out_indent_newline(struct dl *dl)
445 {
446 if (!g_indent_newline && !dl->json_output)
447 pr_out(" ");
448 }
449
450 static bool is_binary_eol(int i)
451 {
452 return !(i%16);
453 }
454
455 static void pr_out_binary_value(struct dl *dl, uint8_t *data, uint32_t len)
456 {
457 int i = 0;
458
459 while (i < len) {
460 if (dl->json_output)
461 print_int(PRINT_JSON, NULL, NULL, data[i]);
462 else
463 pr_out("%02x ", data[i]);
464 i++;
465 if (!dl->json_output && is_binary_eol(i))
466 __pr_out_newline();
467 }
468 if (!dl->json_output && !is_binary_eol(i))
469 __pr_out_newline();
470 }
471
472 static void pr_out_name(struct dl *dl, const char *name)
473 {
474 __pr_out_indent_newline(dl);
475 if (dl->json_output)
476 print_string(PRINT_JSON, name, NULL, NULL);
477 else
478 pr_out("%s:", name);
479 }
480
481 static void pr_out_u64(struct dl *dl, const char *name, uint64_t val)
482 {
483 __pr_out_indent_newline(dl);
484 if (val == (uint64_t) -1)
485 return print_string_name_value(name, "unlimited");
486
487 if (dl->json_output)
488 print_u64(PRINT_JSON, name, NULL, val);
489 else
490 pr_out("%s %"PRIu64, name, val);
491 }
492
493 static void pr_out_section_start(struct dl *dl, const char *name)
494 {
495 if (dl->json_output) {
496 open_json_object(NULL);
497 open_json_object(name);
498 }
499 }
500
501 static void pr_out_section_end(struct dl *dl)
502 {
503 if (dl->json_output) {
504 if (dl->arr_last.present)
505 close_json_array(PRINT_JSON, NULL);
506 close_json_object();
507 close_json_object();
508 }
509 }
510
511 static void pr_out_array_start(struct dl *dl, const char *name)
512 {
513 if (dl->json_output) {
514 open_json_array(PRINT_JSON, name);
515 } else {
516 __pr_out_indent_inc();
517 __pr_out_newline();
518 pr_out("%s:", name);
519 __pr_out_indent_inc();
520 __pr_out_newline();
521 }
522 }
523
524 static void pr_out_array_end(struct dl *dl)
525 {
526 if (dl->json_output) {
527 close_json_array(PRINT_JSON, NULL);
528 } else {
529 __pr_out_indent_dec();
530 __pr_out_indent_dec();
531 }
532 }
533
534 static void pr_out_object_start(struct dl *dl, const char *name)
535 {
536 if (dl->json_output) {
537 open_json_object(name);
538 } else {
539 __pr_out_indent_inc();
540 __pr_out_newline();
541 pr_out("%s:", name);
542 __pr_out_indent_inc();
543 __pr_out_newline();
544 }
545 }
546
547 static void pr_out_object_end(struct dl *dl)
548 {
549 if (dl->json_output) {
550 close_json_object();
551 } else {
552 __pr_out_indent_dec();
553 __pr_out_indent_dec();
554 }
555 }
556
557 static void pr_out_entry_start(struct dl *dl)
558 {
559 if (dl->json_output)
560 open_json_object(NULL);
561 }
562
563 static void pr_out_entry_end(struct dl *dl)
564 {
565 if (dl->json_output)
566 close_json_object();
567 else
568 __pr_out_newline();
569 }
570
571 static void check_indent_newline(struct dl *dl)
572 {
573 __pr_out_indent_newline(dl);
574
575 if (g_indent_newline && !is_json_context()) {
576 printf("%s", g_indent_str);
577 g_indent_newline = false;
578 }
579 g_new_line_count = 0;
580 }
581
582 static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = {
583 [DEVLINK_ATTR_BUS_NAME] = MNL_TYPE_NUL_STRING,
584 [DEVLINK_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING,
585 [DEVLINK_ATTR_PORT_INDEX] = MNL_TYPE_U32,
586 [DEVLINK_ATTR_PORT_TYPE] = MNL_TYPE_U16,
587 [DEVLINK_ATTR_PORT_DESIRED_TYPE] = MNL_TYPE_U16,
588 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX] = MNL_TYPE_U32,
589 [DEVLINK_ATTR_PORT_NETDEV_NAME] = MNL_TYPE_NUL_STRING,
590 [DEVLINK_ATTR_PORT_IBDEV_NAME] = MNL_TYPE_NUL_STRING,
591 [DEVLINK_ATTR_PORT_LANES] = MNL_TYPE_U32,
592 [DEVLINK_ATTR_PORT_SPLITTABLE] = MNL_TYPE_U8,
593 [DEVLINK_ATTR_SB_INDEX] = MNL_TYPE_U32,
594 [DEVLINK_ATTR_SB_SIZE] = MNL_TYPE_U32,
595 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] = MNL_TYPE_U16,
596 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] = MNL_TYPE_U16,
597 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT] = MNL_TYPE_U16,
598 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT] = MNL_TYPE_U16,
599 [DEVLINK_ATTR_SB_POOL_INDEX] = MNL_TYPE_U16,
600 [DEVLINK_ATTR_SB_POOL_TYPE] = MNL_TYPE_U8,
601 [DEVLINK_ATTR_SB_POOL_SIZE] = MNL_TYPE_U32,
602 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = MNL_TYPE_U8,
603 [DEVLINK_ATTR_SB_THRESHOLD] = MNL_TYPE_U32,
604 [DEVLINK_ATTR_SB_TC_INDEX] = MNL_TYPE_U16,
605 [DEVLINK_ATTR_SB_OCC_CUR] = MNL_TYPE_U32,
606 [DEVLINK_ATTR_SB_OCC_MAX] = MNL_TYPE_U32,
607 [DEVLINK_ATTR_ESWITCH_MODE] = MNL_TYPE_U16,
608 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = MNL_TYPE_U8,
609 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = MNL_TYPE_U8,
610 [DEVLINK_ATTR_DPIPE_TABLES] = MNL_TYPE_NESTED,
611 [DEVLINK_ATTR_DPIPE_TABLE] = MNL_TYPE_NESTED,
612 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = MNL_TYPE_STRING,
613 [DEVLINK_ATTR_DPIPE_TABLE_SIZE] = MNL_TYPE_U64,
614 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES] = MNL_TYPE_NESTED,
615 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] = MNL_TYPE_NESTED,
616 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = MNL_TYPE_U8,
617 [DEVLINK_ATTR_DPIPE_ENTRIES] = MNL_TYPE_NESTED,
618 [DEVLINK_ATTR_DPIPE_ENTRY] = MNL_TYPE_NESTED,
619 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX] = MNL_TYPE_U64,
620 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] = MNL_TYPE_NESTED,
621 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES] = MNL_TYPE_NESTED,
622 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER] = MNL_TYPE_U64,
623 [DEVLINK_ATTR_DPIPE_MATCH] = MNL_TYPE_NESTED,
624 [DEVLINK_ATTR_DPIPE_MATCH_VALUE] = MNL_TYPE_NESTED,
625 [DEVLINK_ATTR_DPIPE_MATCH_TYPE] = MNL_TYPE_U32,
626 [DEVLINK_ATTR_DPIPE_ACTION] = MNL_TYPE_NESTED,
627 [DEVLINK_ATTR_DPIPE_ACTION_VALUE] = MNL_TYPE_NESTED,
628 [DEVLINK_ATTR_DPIPE_ACTION_TYPE] = MNL_TYPE_U32,
629 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING] = MNL_TYPE_U32,
630 [DEVLINK_ATTR_DPIPE_HEADERS] = MNL_TYPE_NESTED,
631 [DEVLINK_ATTR_DPIPE_HEADER] = MNL_TYPE_NESTED,
632 [DEVLINK_ATTR_DPIPE_HEADER_NAME] = MNL_TYPE_STRING,
633 [DEVLINK_ATTR_DPIPE_HEADER_ID] = MNL_TYPE_U32,
634 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS] = MNL_TYPE_NESTED,
635 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL] = MNL_TYPE_U8,
636 [DEVLINK_ATTR_DPIPE_HEADER_INDEX] = MNL_TYPE_U32,
637 [DEVLINK_ATTR_DPIPE_FIELD] = MNL_TYPE_NESTED,
638 [DEVLINK_ATTR_DPIPE_FIELD_NAME] = MNL_TYPE_STRING,
639 [DEVLINK_ATTR_DPIPE_FIELD_ID] = MNL_TYPE_U32,
640 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] = MNL_TYPE_U32,
641 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE] = MNL_TYPE_U32,
642 [DEVLINK_ATTR_PARAM] = MNL_TYPE_NESTED,
643 [DEVLINK_ATTR_PARAM_NAME] = MNL_TYPE_STRING,
644 [DEVLINK_ATTR_PARAM_TYPE] = MNL_TYPE_U8,
645 [DEVLINK_ATTR_PARAM_VALUES_LIST] = MNL_TYPE_NESTED,
646 [DEVLINK_ATTR_PARAM_VALUE] = MNL_TYPE_NESTED,
647 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = MNL_TYPE_U8,
648 [DEVLINK_ATTR_REGION_NAME] = MNL_TYPE_STRING,
649 [DEVLINK_ATTR_REGION_SIZE] = MNL_TYPE_U64,
650 [DEVLINK_ATTR_REGION_SNAPSHOTS] = MNL_TYPE_NESTED,
651 [DEVLINK_ATTR_REGION_SNAPSHOT] = MNL_TYPE_NESTED,
652 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = MNL_TYPE_U32,
653 [DEVLINK_ATTR_REGION_CHUNKS] = MNL_TYPE_NESTED,
654 [DEVLINK_ATTR_REGION_CHUNK] = MNL_TYPE_NESTED,
655 [DEVLINK_ATTR_REGION_CHUNK_DATA] = MNL_TYPE_BINARY,
656 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = MNL_TYPE_U64,
657 [DEVLINK_ATTR_REGION_CHUNK_LEN] = MNL_TYPE_U64,
658 [DEVLINK_ATTR_INFO_DRIVER_NAME] = MNL_TYPE_STRING,
659 [DEVLINK_ATTR_INFO_SERIAL_NUMBER] = MNL_TYPE_STRING,
660 [DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER] = MNL_TYPE_STRING,
661 [DEVLINK_ATTR_INFO_VERSION_FIXED] = MNL_TYPE_NESTED,
662 [DEVLINK_ATTR_INFO_VERSION_RUNNING] = MNL_TYPE_NESTED,
663 [DEVLINK_ATTR_INFO_VERSION_STORED] = MNL_TYPE_NESTED,
664 [DEVLINK_ATTR_INFO_VERSION_NAME] = MNL_TYPE_STRING,
665 [DEVLINK_ATTR_INFO_VERSION_VALUE] = MNL_TYPE_STRING,
666 [DEVLINK_ATTR_HEALTH_REPORTER] = MNL_TYPE_NESTED,
667 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = MNL_TYPE_STRING,
668 [DEVLINK_ATTR_HEALTH_REPORTER_STATE] = MNL_TYPE_U8,
669 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT] = MNL_TYPE_U64,
670 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT] = MNL_TYPE_U64,
671 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS] = MNL_TYPE_U64,
672 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = MNL_TYPE_U64,
673 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = MNL_TYPE_STRING,
674 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG] = MNL_TYPE_STRING,
675 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE] = MNL_TYPE_U64,
676 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL] = MNL_TYPE_U64,
677 [DEVLINK_ATTR_STATS] = MNL_TYPE_NESTED,
678 [DEVLINK_ATTR_TRAP_NAME] = MNL_TYPE_STRING,
679 [DEVLINK_ATTR_TRAP_ACTION] = MNL_TYPE_U8,
680 [DEVLINK_ATTR_TRAP_TYPE] = MNL_TYPE_U8,
681 [DEVLINK_ATTR_TRAP_GENERIC] = MNL_TYPE_FLAG,
682 [DEVLINK_ATTR_TRAP_METADATA] = MNL_TYPE_NESTED,
683 [DEVLINK_ATTR_TRAP_GROUP_NAME] = MNL_TYPE_STRING,
684 [DEVLINK_ATTR_RELOAD_FAILED] = MNL_TYPE_U8,
685 [DEVLINK_ATTR_DEV_STATS] = MNL_TYPE_NESTED,
686 [DEVLINK_ATTR_RELOAD_STATS] = MNL_TYPE_NESTED,
687 [DEVLINK_ATTR_RELOAD_STATS_ENTRY] = MNL_TYPE_NESTED,
688 [DEVLINK_ATTR_RELOAD_ACTION] = MNL_TYPE_U8,
689 [DEVLINK_ATTR_RELOAD_STATS_LIMIT] = MNL_TYPE_U8,
690 [DEVLINK_ATTR_RELOAD_STATS_VALUE] = MNL_TYPE_U32,
691 [DEVLINK_ATTR_REMOTE_RELOAD_STATS] = MNL_TYPE_NESTED,
692 [DEVLINK_ATTR_RELOAD_ACTION_INFO] = MNL_TYPE_NESTED,
693 [DEVLINK_ATTR_RELOAD_ACTION_STATS] = MNL_TYPE_NESTED,
694 [DEVLINK_ATTR_TRAP_POLICER_ID] = MNL_TYPE_U32,
695 [DEVLINK_ATTR_TRAP_POLICER_RATE] = MNL_TYPE_U64,
696 [DEVLINK_ATTR_TRAP_POLICER_BURST] = MNL_TYPE_U64,
697 };
698
699 static const enum mnl_attr_data_type
700 devlink_stats_policy[DEVLINK_ATTR_STATS_MAX + 1] = {
701 [DEVLINK_ATTR_STATS_RX_PACKETS] = MNL_TYPE_U64,
702 [DEVLINK_ATTR_STATS_RX_BYTES] = MNL_TYPE_U64,
703 [DEVLINK_ATTR_STATS_RX_DROPPED] = MNL_TYPE_U64,
704 };
705
706 static int attr_cb(const struct nlattr *attr, void *data)
707 {
708 const struct nlattr **tb = data;
709 int type;
710
711 if (mnl_attr_type_valid(attr, DEVLINK_ATTR_MAX) < 0)
712 return MNL_CB_OK;
713
714 type = mnl_attr_get_type(attr);
715 if (mnl_attr_validate(attr, devlink_policy[type]) < 0)
716 return MNL_CB_ERROR;
717
718 tb[type] = attr;
719 return MNL_CB_OK;
720 }
721
722 static int attr_stats_cb(const struct nlattr *attr, void *data)
723 {
724 const struct nlattr **tb = data;
725 int type;
726
727 /* Allow the tool to work on top of newer kernels that might contain
728 * more attributes.
729 */
730 if (mnl_attr_type_valid(attr, DEVLINK_ATTR_STATS_MAX) < 0)
731 return MNL_CB_OK;
732
733 type = mnl_attr_get_type(attr);
734 if (mnl_attr_validate(attr, devlink_stats_policy[type]) < 0)
735 return MNL_CB_ERROR;
736
737 tb[type] = attr;
738 return MNL_CB_OK;
739 }
740
741 static const enum mnl_attr_data_type
742 devlink_function_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
743 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR ] = MNL_TYPE_BINARY,
744 };
745
746 static int function_attr_cb(const struct nlattr *attr, void *data)
747 {
748 const struct nlattr **tb = data;
749 int type;
750
751 /* Allow the tool to work on top of newer kernels that might contain
752 * more attributes.
753 */
754 if (mnl_attr_type_valid(attr, DEVLINK_PORT_FUNCTION_ATTR_MAX) < 0)
755 return MNL_CB_OK;
756
757 type = mnl_attr_get_type(attr);
758 if (mnl_attr_validate(attr, devlink_function_policy[type]) < 0)
759 return MNL_CB_ERROR;
760
761 tb[type] = attr;
762 return MNL_CB_OK;
763 }
764
765 static int ifname_map_cb(const struct nlmsghdr *nlh, void *data)
766 {
767 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
768 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
769 struct dl *dl = data;
770 struct ifname_map *ifname_map;
771 const char *bus_name;
772 const char *dev_name;
773 uint32_t port_ifindex;
774 const char *port_ifname;
775
776 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
777 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
778 !tb[DEVLINK_ATTR_PORT_INDEX])
779 return MNL_CB_ERROR;
780
781 if (!tb[DEVLINK_ATTR_PORT_NETDEV_NAME])
782 return MNL_CB_OK;
783
784 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
785 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
786 port_ifindex = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
787 port_ifname = mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME]);
788 ifname_map = ifname_map_alloc(bus_name, dev_name,
789 port_ifindex, port_ifname);
790 if (!ifname_map)
791 return MNL_CB_ERROR;
792 list_add(&ifname_map->list, &dl->ifname_map_list);
793
794 return MNL_CB_OK;
795 }
796
797 static void ifname_map_fini(struct dl *dl)
798 {
799 struct ifname_map *ifname_map, *tmp;
800
801 list_for_each_entry_safe(ifname_map, tmp,
802 &dl->ifname_map_list, list) {
803 list_del(&ifname_map->list);
804 ifname_map_free(ifname_map);
805 }
806 }
807
808 static int ifname_map_init(struct dl *dl)
809 {
810 struct nlmsghdr *nlh;
811 int err;
812
813 INIT_LIST_HEAD(&dl->ifname_map_list);
814
815 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET,
816 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
817
818 err = _mnlg_socket_sndrcv(dl->nlg, nlh, ifname_map_cb, dl);
819 if (err) {
820 ifname_map_fini(dl);
821 return err;
822 }
823 return 0;
824 }
825
826 static int ifname_map_lookup(struct dl *dl, const char *ifname,
827 char **p_bus_name, char **p_dev_name,
828 uint32_t *p_port_index)
829 {
830 struct ifname_map *ifname_map;
831
832 list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
833 if (strcmp(ifname, ifname_map->ifname) == 0) {
834 *p_bus_name = ifname_map->bus_name;
835 *p_dev_name = ifname_map->dev_name;
836 *p_port_index = ifname_map->port_index;
837 return 0;
838 }
839 }
840 return -ENOENT;
841 }
842
843 static int ifname_map_rev_lookup(struct dl *dl, const char *bus_name,
844 const char *dev_name, uint32_t port_index,
845 char **p_ifname)
846 {
847 struct ifname_map *ifname_map;
848
849 list_for_each_entry(ifname_map, &dl->ifname_map_list, list) {
850 if (strcmp(bus_name, ifname_map->bus_name) == 0 &&
851 strcmp(dev_name, ifname_map->dev_name) == 0 &&
852 port_index == ifname_map->port_index) {
853 *p_ifname = ifname_map->ifname;
854 return 0;
855 }
856 }
857 return -ENOENT;
858 }
859
860 static unsigned int strslashcount(char *str)
861 {
862 unsigned int count = 0;
863 char *pos = str;
864
865 while ((pos = strchr(pos, '/'))) {
866 count++;
867 pos++;
868 }
869 return count;
870 }
871
872 static int strslashrsplit(char *str, char **before, char **after)
873 {
874 char *slash;
875
876 slash = strrchr(str, '/');
877 if (!slash)
878 return -EINVAL;
879 *slash = '\0';
880 *before = str;
881 *after = slash + 1;
882 return 0;
883 }
884
885 static int strtouint64_t(const char *str, uint64_t *p_val)
886 {
887 char *endptr;
888 unsigned long long int val;
889
890 val = strtoull(str, &endptr, 10);
891 if (endptr == str || *endptr != '\0')
892 return -EINVAL;
893 if (val > ULONG_MAX)
894 return -ERANGE;
895 *p_val = val;
896 return 0;
897 }
898
899 static int strtouint32_t(const char *str, uint32_t *p_val)
900 {
901 char *endptr;
902 unsigned long int val;
903
904 val = strtoul(str, &endptr, 10);
905 if (endptr == str || *endptr != '\0')
906 return -EINVAL;
907 if (val > UINT_MAX)
908 return -ERANGE;
909 *p_val = val;
910 return 0;
911 }
912
913 static int strtouint16_t(const char *str, uint16_t *p_val)
914 {
915 char *endptr;
916 unsigned long int val;
917
918 val = strtoul(str, &endptr, 10);
919 if (endptr == str || *endptr != '\0')
920 return -EINVAL;
921 if (val > USHRT_MAX)
922 return -ERANGE;
923 *p_val = val;
924 return 0;
925 }
926
927 static int strtouint8_t(const char *str, uint8_t *p_val)
928 {
929 char *endptr;
930 unsigned long int val;
931
932 val = strtoul(str, &endptr, 10);
933 if (endptr == str || *endptr != '\0')
934 return -EINVAL;
935 if (val > UCHAR_MAX)
936 return -ERANGE;
937 *p_val = val;
938 return 0;
939 }
940
941 static int strtobool(const char *str, bool *p_val)
942 {
943 bool val;
944
945 if (!strcmp(str, "true") || !strcmp(str, "1") ||
946 !strcmp(str, "enable"))
947 val = true;
948 else if (!strcmp(str, "false") || !strcmp(str, "0") ||
949 !strcmp(str, "disable"))
950 val = false;
951 else
952 return -EINVAL;
953 *p_val = val;
954 return 0;
955 }
956
957 static int __dl_argv_handle(char *str, char **p_bus_name, char **p_dev_name)
958 {
959 strslashrsplit(str, p_bus_name, p_dev_name);
960 return 0;
961 }
962
963 static int dl_argv_handle(struct dl *dl, char **p_bus_name, char **p_dev_name)
964 {
965 char *str = dl_argv_next(dl);
966
967 if (!str) {
968 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
969 return -EINVAL;
970 }
971 if (strslashcount(str) != 1) {
972 pr_err("Wrong devlink identification string format.\n");
973 pr_err("Expected \"bus_name/dev_name\".\n");
974 return -EINVAL;
975 }
976 return __dl_argv_handle(str, p_bus_name, p_dev_name);
977 }
978
979 static int __dl_argv_handle_port(char *str,
980 char **p_bus_name, char **p_dev_name,
981 uint32_t *p_port_index)
982 {
983 char *handlestr;
984 char *portstr;
985 int err;
986
987 err = strslashrsplit(str, &handlestr, &portstr);
988 if (err) {
989 pr_err("Port identification \"%s\" is invalid\n", str);
990 return err;
991 }
992 err = strtouint32_t(portstr, p_port_index);
993 if (err) {
994 pr_err("Port index \"%s\" is not a number or not within range\n",
995 portstr);
996 return err;
997 }
998 err = strslashrsplit(handlestr, p_bus_name, p_dev_name);
999 if (err) {
1000 pr_err("Port identification \"%s\" is invalid\n", str);
1001 return err;
1002 }
1003 return 0;
1004 }
1005
1006 static int __dl_argv_handle_port_ifname(struct dl *dl, char *str,
1007 char **p_bus_name, char **p_dev_name,
1008 uint32_t *p_port_index)
1009 {
1010 int err;
1011
1012 err = ifname_map_lookup(dl, str, p_bus_name, p_dev_name,
1013 p_port_index);
1014 if (err) {
1015 pr_err("Netdevice \"%s\" not found\n", str);
1016 return err;
1017 }
1018 return 0;
1019 }
1020
1021 static int dl_argv_handle_port(struct dl *dl, char **p_bus_name,
1022 char **p_dev_name, uint32_t *p_port_index)
1023 {
1024 char *str = dl_argv_next(dl);
1025 unsigned int slash_count;
1026
1027 if (!str) {
1028 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
1029 return -EINVAL;
1030 }
1031 slash_count = strslashcount(str);
1032 switch (slash_count) {
1033 case 0:
1034 return __dl_argv_handle_port_ifname(dl, str, p_bus_name,
1035 p_dev_name, p_port_index);
1036 case 2:
1037 return __dl_argv_handle_port(str, p_bus_name,
1038 p_dev_name, p_port_index);
1039 default:
1040 pr_err("Wrong port identification string format.\n");
1041 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1042 return -EINVAL;
1043 }
1044 }
1045
1046 static int dl_argv_handle_both(struct dl *dl, char **p_bus_name,
1047 char **p_dev_name, uint32_t *p_port_index,
1048 uint64_t *p_handle_bit)
1049 {
1050 char *str = dl_argv_next(dl);
1051 unsigned int slash_count;
1052 int err;
1053
1054 if (!str) {
1055 pr_err("One of following identifications expected:\n"
1056 "Devlink identification (\"bus_name/dev_name\")\n"
1057 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
1058 return -EINVAL;
1059 }
1060 slash_count = strslashcount(str);
1061 if (slash_count == 1) {
1062 err = __dl_argv_handle(str, p_bus_name, p_dev_name);
1063 if (err)
1064 return err;
1065 *p_handle_bit = DL_OPT_HANDLE;
1066 } else if (slash_count == 2) {
1067 err = __dl_argv_handle_port(str, p_bus_name,
1068 p_dev_name, p_port_index);
1069 if (err)
1070 return err;
1071 *p_handle_bit = DL_OPT_HANDLEP;
1072 } else if (slash_count == 0) {
1073 err = __dl_argv_handle_port_ifname(dl, str, p_bus_name,
1074 p_dev_name, p_port_index);
1075 if (err)
1076 return err;
1077 *p_handle_bit = DL_OPT_HANDLEP;
1078 } else {
1079 pr_err("Wrong port identification string format.\n");
1080 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1081 return -EINVAL;
1082 }
1083 return 0;
1084 }
1085
1086 static int __dl_argv_handle_region(char *str, char **p_bus_name,
1087 char **p_dev_name, char **p_region)
1088 {
1089 char *handlestr;
1090 int err;
1091
1092 err = strslashrsplit(str, &handlestr, p_region);
1093 if (err) {
1094 pr_err("Region identification \"%s\" is invalid\n", str);
1095 return err;
1096 }
1097 err = strslashrsplit(handlestr, p_bus_name, p_dev_name);
1098 if (err) {
1099 pr_err("Region identification \"%s\" is invalid\n", str);
1100 return err;
1101 }
1102 return 0;
1103 }
1104
1105 static int dl_argv_handle_region(struct dl *dl, char **p_bus_name,
1106 char **p_dev_name, char **p_region)
1107 {
1108 char *str = dl_argv_next(dl);
1109 unsigned int slash_count;
1110
1111 if (!str) {
1112 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
1113 return -EINVAL;
1114 }
1115
1116 slash_count = strslashcount(str);
1117 if (slash_count != 2) {
1118 pr_err("Wrong region identification string format.\n");
1119 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
1120 return -EINVAL;
1121 }
1122
1123 return __dl_argv_handle_region(str, p_bus_name, p_dev_name, p_region);
1124 }
1125
1126 static int dl_argv_uint64_t(struct dl *dl, uint64_t *p_val)
1127 {
1128 char *str = dl_argv_next(dl);
1129 int err;
1130
1131 if (!str) {
1132 pr_err("Unsigned number argument expected\n");
1133 return -EINVAL;
1134 }
1135
1136 err = strtouint64_t(str, p_val);
1137 if (err) {
1138 pr_err("\"%s\" is not a number or not within range\n", str);
1139 return err;
1140 }
1141 return 0;
1142 }
1143
1144 static int dl_argv_uint32_t(struct dl *dl, uint32_t *p_val)
1145 {
1146 char *str = dl_argv_next(dl);
1147 int err;
1148
1149 if (!str) {
1150 pr_err("Unsigned number argument expected\n");
1151 return -EINVAL;
1152 }
1153
1154 err = strtouint32_t(str, p_val);
1155 if (err) {
1156 pr_err("\"%s\" is not a number or not within range\n", str);
1157 return err;
1158 }
1159 return 0;
1160 }
1161
1162 static int dl_argv_uint16_t(struct dl *dl, uint16_t *p_val)
1163 {
1164 char *str = dl_argv_next(dl);
1165 int err;
1166
1167 if (!str) {
1168 pr_err("Unsigned number argument expected\n");
1169 return -EINVAL;
1170 }
1171
1172 err = strtouint16_t(str, p_val);
1173 if (err) {
1174 pr_err("\"%s\" is not a number or not within range\n", str);
1175 return err;
1176 }
1177 return 0;
1178 }
1179
1180 static int dl_argv_bool(struct dl *dl, bool *p_val)
1181 {
1182 char *str = dl_argv_next(dl);
1183 int err;
1184
1185 if (!str) {
1186 pr_err("Boolean argument expected\n");
1187 return -EINVAL;
1188 }
1189
1190 err = strtobool(str, p_val);
1191 if (err) {
1192 pr_err("\"%s\" is not a valid boolean value\n", str);
1193 return err;
1194 }
1195 return 0;
1196 }
1197
1198 static int dl_argv_str(struct dl *dl, const char **p_str)
1199 {
1200 const char *str = dl_argv_next(dl);
1201
1202 if (!str) {
1203 pr_err("String parameter expected\n");
1204 return -EINVAL;
1205 }
1206 *p_str = str;
1207 return 0;
1208 }
1209
1210 static int port_type_get(const char *typestr, enum devlink_port_type *p_type)
1211 {
1212 if (strcmp(typestr, "auto") == 0) {
1213 *p_type = DEVLINK_PORT_TYPE_AUTO;
1214 } else if (strcmp(typestr, "eth") == 0) {
1215 *p_type = DEVLINK_PORT_TYPE_ETH;
1216 } else if (strcmp(typestr, "ib") == 0) {
1217 *p_type = DEVLINK_PORT_TYPE_IB;
1218 } else {
1219 pr_err("Unknown port type \"%s\"\n", typestr);
1220 return -EINVAL;
1221 }
1222 return 0;
1223 }
1224
1225 static int pool_type_get(const char *typestr, enum devlink_sb_pool_type *p_type)
1226 {
1227 if (strcmp(typestr, "ingress") == 0) {
1228 *p_type = DEVLINK_SB_POOL_TYPE_INGRESS;
1229 } else if (strcmp(typestr, "egress") == 0) {
1230 *p_type = DEVLINK_SB_POOL_TYPE_EGRESS;
1231 } else {
1232 pr_err("Unknown pool type \"%s\"\n", typestr);
1233 return -EINVAL;
1234 }
1235 return 0;
1236 }
1237
1238 static int threshold_type_get(const char *typestr,
1239 enum devlink_sb_threshold_type *p_type)
1240 {
1241 if (strcmp(typestr, "static") == 0) {
1242 *p_type = DEVLINK_SB_THRESHOLD_TYPE_STATIC;
1243 } else if (strcmp(typestr, "dynamic") == 0) {
1244 *p_type = DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC;
1245 } else {
1246 pr_err("Unknown threshold type \"%s\"\n", typestr);
1247 return -EINVAL;
1248 }
1249 return 0;
1250 }
1251
1252 static int eswitch_mode_get(const char *typestr,
1253 enum devlink_eswitch_mode *p_mode)
1254 {
1255 if (strcmp(typestr, ESWITCH_MODE_LEGACY) == 0) {
1256 *p_mode = DEVLINK_ESWITCH_MODE_LEGACY;
1257 } else if (strcmp(typestr, ESWITCH_MODE_SWITCHDEV) == 0) {
1258 *p_mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
1259 } else {
1260 pr_err("Unknown eswitch mode \"%s\"\n", typestr);
1261 return -EINVAL;
1262 }
1263 return 0;
1264 }
1265
1266 static int eswitch_inline_mode_get(const char *typestr,
1267 enum devlink_eswitch_inline_mode *p_mode)
1268 {
1269 if (strcmp(typestr, ESWITCH_INLINE_MODE_NONE) == 0) {
1270 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_NONE;
1271 } else if (strcmp(typestr, ESWITCH_INLINE_MODE_LINK) == 0) {
1272 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_LINK;
1273 } else if (strcmp(typestr, ESWITCH_INLINE_MODE_NETWORK) == 0) {
1274 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_NETWORK;
1275 } else if (strcmp(typestr, ESWITCH_INLINE_MODE_TRANSPORT) == 0) {
1276 *p_mode = DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT;
1277 } else {
1278 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr);
1279 return -EINVAL;
1280 }
1281 return 0;
1282 }
1283
1284 static int
1285 eswitch_encap_mode_get(const char *typestr,
1286 enum devlink_eswitch_encap_mode *p_encap_mode)
1287 {
1288 /* The initial implementation incorrectly accepted "enable"/"disable".
1289 * Carry it to maintain backward compatibility.
1290 */
1291 if (strcmp(typestr, "disable") == 0 ||
1292 strcmp(typestr, ESWITCH_ENCAP_MODE_NONE) == 0) {
1293 *p_encap_mode = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1294 } else if (strcmp(typestr, "enable") == 0 ||
1295 strcmp(typestr, ESWITCH_ENCAP_MODE_BASIC) == 0) {
1296 *p_encap_mode = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1297 } else {
1298 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr);
1299 return -EINVAL;
1300 }
1301 return 0;
1302 }
1303
1304 static int flash_overwrite_section_get(const char *sectionstr, uint32_t *mask)
1305 {
1306 if (strcmp(sectionstr, "settings") == 0) {
1307 *mask |= DEVLINK_FLASH_OVERWRITE_SETTINGS;
1308 } else if (strcmp(sectionstr, "identifiers") == 0) {
1309 *mask |= DEVLINK_FLASH_OVERWRITE_IDENTIFIERS;
1310 } else {
1311 pr_err("Unknown overwrite section \"%s\"\n", sectionstr);
1312 return -EINVAL;
1313 }
1314 return 0;
1315 }
1316
1317 static int param_cmode_get(const char *cmodestr,
1318 enum devlink_param_cmode *cmode)
1319 {
1320 if (strcmp(cmodestr, PARAM_CMODE_RUNTIME_STR) == 0) {
1321 *cmode = DEVLINK_PARAM_CMODE_RUNTIME;
1322 } else if (strcmp(cmodestr, PARAM_CMODE_DRIVERINIT_STR) == 0) {
1323 *cmode = DEVLINK_PARAM_CMODE_DRIVERINIT;
1324 } else if (strcmp(cmodestr, PARAM_CMODE_PERMANENT_STR) == 0) {
1325 *cmode = DEVLINK_PARAM_CMODE_PERMANENT;
1326 } else {
1327 pr_err("Unknown configuration mode \"%s\"\n", cmodestr);
1328 return -EINVAL;
1329 }
1330 return 0;
1331 }
1332
1333 static int trap_action_get(const char *actionstr,
1334 enum devlink_trap_action *p_action)
1335 {
1336 if (strcmp(actionstr, "drop") == 0) {
1337 *p_action = DEVLINK_TRAP_ACTION_DROP;
1338 } else if (strcmp(actionstr, "trap") == 0) {
1339 *p_action = DEVLINK_TRAP_ACTION_TRAP;
1340 } else if (strcmp(actionstr, "mirror") == 0) {
1341 *p_action = DEVLINK_TRAP_ACTION_MIRROR;
1342 } else {
1343 pr_err("Unknown trap action \"%s\"\n", actionstr);
1344 return -EINVAL;
1345 }
1346 return 0;
1347 }
1348
1349 static int hw_addr_parse(const char *addrstr, char *hw_addr, uint32_t *len)
1350 {
1351 int alen;
1352
1353 alen = ll_addr_a2n(hw_addr, MAX_ADDR_LEN, addrstr);
1354 if (alen < 0)
1355 return -EINVAL;
1356 *len = alen;
1357 return 0;
1358 }
1359
1360 static int reload_action_get(struct dl *dl, const char *actionstr,
1361 enum devlink_reload_action *action)
1362 {
1363 if (strcmp(actionstr, "driver_reinit") == 0) {
1364 *action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
1365 } else if (strcmp(actionstr, "fw_activate") == 0) {
1366 *action = DEVLINK_RELOAD_ACTION_FW_ACTIVATE;
1367 } else {
1368 pr_err("Unknown reload action \"%s\"\n", actionstr);
1369 return -EINVAL;
1370 }
1371 return 0;
1372 }
1373
1374 static int reload_limit_get(struct dl *dl, const char *limitstr,
1375 enum devlink_reload_limit *limit)
1376 {
1377 if (strcmp(limitstr, "no_reset") == 0) {
1378 *limit = DEVLINK_RELOAD_LIMIT_NO_RESET;
1379 } else {
1380 pr_err("Unknown reload limit \"%s\"\n", limitstr);
1381 return -EINVAL;
1382 }
1383 return 0;
1384 }
1385
1386 struct dl_args_metadata {
1387 uint64_t o_flag;
1388 char err_msg[DL_ARGS_REQUIRED_MAX_ERR_LEN];
1389 };
1390
1391 static const struct dl_args_metadata dl_args_required[] = {
1392 {DL_OPT_PORT_TYPE, "Port type not set."},
1393 {DL_OPT_PORT_COUNT, "Port split count option expected."},
1394 {DL_OPT_SB_POOL, "Pool index option expected."},
1395 {DL_OPT_SB_SIZE, "Pool size option expected."},
1396 {DL_OPT_SB_TYPE, "Pool type option expected."},
1397 {DL_OPT_SB_THTYPE, "Pool threshold type option expected."},
1398 {DL_OPT_SB_TH, "Threshold option expected."},
1399 {DL_OPT_SB_TC, "TC index option expected."},
1400 {DL_OPT_ESWITCH_MODE, "E-Switch mode option expected."},
1401 {DL_OPT_ESWITCH_INLINE_MODE, "E-Switch inline-mode option expected."},
1402 {DL_OPT_DPIPE_TABLE_NAME, "Dpipe table name expected."},
1403 {DL_OPT_DPIPE_TABLE_COUNTERS, "Dpipe table counter state expected."},
1404 {DL_OPT_ESWITCH_ENCAP_MODE, "E-Switch encapsulation option expected."},
1405 {DL_OPT_RESOURCE_PATH, "Resource path expected."},
1406 {DL_OPT_RESOURCE_SIZE, "Resource size expected."},
1407 {DL_OPT_PARAM_NAME, "Parameter name expected."},
1408 {DL_OPT_PARAM_VALUE, "Value to set expected."},
1409 {DL_OPT_PARAM_CMODE, "Configuration mode expected."},
1410 {DL_OPT_REGION_SNAPSHOT_ID, "Region snapshot id expected."},
1411 {DL_OPT_REGION_ADDRESS, "Region address value expected."},
1412 {DL_OPT_REGION_LENGTH, "Region length value expected."},
1413 {DL_OPT_HEALTH_REPORTER_NAME, "Reporter's name is expected."},
1414 {DL_OPT_TRAP_NAME, "Trap's name is expected."},
1415 {DL_OPT_TRAP_GROUP_NAME, "Trap group's name is expected."},
1416 {DL_OPT_PORT_FUNCTION_HW_ADDR, "Port function's hardware address is expected."},
1417 };
1418
1419 static int dl_args_finding_required_validate(uint64_t o_required,
1420 uint64_t o_found)
1421 {
1422 uint64_t o_flag;
1423 int i;
1424
1425 for (i = 0; i < ARRAY_SIZE(dl_args_required); i++) {
1426 o_flag = dl_args_required[i].o_flag;
1427 if ((o_required & o_flag) && !(o_found & o_flag)) {
1428 pr_err("%s\n", dl_args_required[i].err_msg);
1429 return -EINVAL;
1430 }
1431 }
1432 if (o_required & ~o_found) {
1433 pr_err("BUG: unknown argument required but not found\n");
1434 return -EINVAL;
1435 }
1436 return 0;
1437 }
1438
1439 static int dl_argv_parse(struct dl *dl, uint64_t o_required,
1440 uint64_t o_optional)
1441 {
1442 struct dl_opts *opts = &dl->opts;
1443 uint64_t o_all = o_required | o_optional;
1444 uint64_t o_found = 0;
1445 int err;
1446
1447 if (o_required & DL_OPT_HANDLE && o_required & DL_OPT_HANDLEP) {
1448 uint64_t handle_bit;
1449
1450 err = dl_argv_handle_both(dl, &opts->bus_name, &opts->dev_name,
1451 &opts->port_index, &handle_bit);
1452 if (err)
1453 return err;
1454 o_required &= ~(DL_OPT_HANDLE | DL_OPT_HANDLEP) | handle_bit;
1455 o_found |= handle_bit;
1456 } else if (o_required & DL_OPT_HANDLE) {
1457 err = dl_argv_handle(dl, &opts->bus_name, &opts->dev_name);
1458 if (err)
1459 return err;
1460 o_found |= DL_OPT_HANDLE;
1461 } else if (o_required & DL_OPT_HANDLEP) {
1462 err = dl_argv_handle_port(dl, &opts->bus_name, &opts->dev_name,
1463 &opts->port_index);
1464 if (err)
1465 return err;
1466 o_found |= DL_OPT_HANDLEP;
1467 } else if (o_required & DL_OPT_HANDLE_REGION) {
1468 err = dl_argv_handle_region(dl, &opts->bus_name,
1469 &opts->dev_name,
1470 &opts->region_name);
1471 if (err)
1472 return err;
1473 o_found |= DL_OPT_HANDLE_REGION;
1474 }
1475
1476 while (dl_argc(dl)) {
1477 if (dl_argv_match(dl, "type") &&
1478 (o_all & DL_OPT_PORT_TYPE)) {
1479 const char *typestr;
1480
1481 dl_arg_inc(dl);
1482 err = dl_argv_str(dl, &typestr);
1483 if (err)
1484 return err;
1485 err = port_type_get(typestr, &opts->port_type);
1486 if (err)
1487 return err;
1488 o_found |= DL_OPT_PORT_TYPE;
1489 } else if (dl_argv_match(dl, "count") &&
1490 (o_all & DL_OPT_PORT_COUNT)) {
1491 dl_arg_inc(dl);
1492 err = dl_argv_uint32_t(dl, &opts->port_count);
1493 if (err)
1494 return err;
1495 o_found |= DL_OPT_PORT_COUNT;
1496 } else if (dl_argv_match(dl, "sb") &&
1497 (o_all & DL_OPT_SB)) {
1498 dl_arg_inc(dl);
1499 err = dl_argv_uint32_t(dl, &opts->sb_index);
1500 if (err)
1501 return err;
1502 o_found |= DL_OPT_SB;
1503 } else if (dl_argv_match(dl, "pool") &&
1504 (o_all & DL_OPT_SB_POOL)) {
1505 dl_arg_inc(dl);
1506 err = dl_argv_uint16_t(dl, &opts->sb_pool_index);
1507 if (err)
1508 return err;
1509 o_found |= DL_OPT_SB_POOL;
1510 } else if (dl_argv_match(dl, "size") &&
1511 (o_all & DL_OPT_SB_SIZE)) {
1512 dl_arg_inc(dl);
1513 err = dl_argv_uint32_t(dl, &opts->sb_pool_size);
1514 if (err)
1515 return err;
1516 o_found |= DL_OPT_SB_SIZE;
1517 } else if (dl_argv_match(dl, "type") &&
1518 (o_all & DL_OPT_SB_TYPE)) {
1519 const char *typestr;
1520
1521 dl_arg_inc(dl);
1522 err = dl_argv_str(dl, &typestr);
1523 if (err)
1524 return err;
1525 err = pool_type_get(typestr, &opts->sb_pool_type);
1526 if (err)
1527 return err;
1528 o_found |= DL_OPT_SB_TYPE;
1529 } else if (dl_argv_match(dl, "thtype") &&
1530 (o_all & DL_OPT_SB_THTYPE)) {
1531 const char *typestr;
1532
1533 dl_arg_inc(dl);
1534 err = dl_argv_str(dl, &typestr);
1535 if (err)
1536 return err;
1537 err = threshold_type_get(typestr,
1538 &opts->sb_pool_thtype);
1539 if (err)
1540 return err;
1541 o_found |= DL_OPT_SB_THTYPE;
1542 } else if (dl_argv_match(dl, "th") &&
1543 (o_all & DL_OPT_SB_TH)) {
1544 dl_arg_inc(dl);
1545 err = dl_argv_uint32_t(dl, &opts->sb_threshold);
1546 if (err)
1547 return err;
1548 o_found |= DL_OPT_SB_TH;
1549 } else if (dl_argv_match(dl, "tc") &&
1550 (o_all & DL_OPT_SB_TC)) {
1551 dl_arg_inc(dl);
1552 err = dl_argv_uint16_t(dl, &opts->sb_tc_index);
1553 if (err)
1554 return err;
1555 o_found |= DL_OPT_SB_TC;
1556 } else if (dl_argv_match(dl, "mode") &&
1557 (o_all & DL_OPT_ESWITCH_MODE)) {
1558 const char *typestr;
1559
1560 dl_arg_inc(dl);
1561 err = dl_argv_str(dl, &typestr);
1562 if (err)
1563 return err;
1564 err = eswitch_mode_get(typestr, &opts->eswitch_mode);
1565 if (err)
1566 return err;
1567 o_found |= DL_OPT_ESWITCH_MODE;
1568 } else if (dl_argv_match(dl, "inline-mode") &&
1569 (o_all & DL_OPT_ESWITCH_INLINE_MODE)) {
1570 const char *typestr;
1571
1572 dl_arg_inc(dl);
1573 err = dl_argv_str(dl, &typestr);
1574 if (err)
1575 return err;
1576 err = eswitch_inline_mode_get(
1577 typestr, &opts->eswitch_inline_mode);
1578 if (err)
1579 return err;
1580 o_found |= DL_OPT_ESWITCH_INLINE_MODE;
1581 } else if (dl_argv_match(dl, "name") &&
1582 (o_all & DL_OPT_DPIPE_TABLE_NAME)) {
1583 dl_arg_inc(dl);
1584 err = dl_argv_str(dl, &opts->dpipe_table_name);
1585 if (err)
1586 return err;
1587 o_found |= DL_OPT_DPIPE_TABLE_NAME;
1588 } else if ((dl_argv_match(dl, "counters") ||
1589 dl_argv_match(dl, "counters_enabled")) &&
1590 (o_all & DL_OPT_DPIPE_TABLE_COUNTERS)) {
1591 dl_arg_inc(dl);
1592 err = dl_argv_bool(dl, &opts->dpipe_counters_enabled);
1593 if (err)
1594 return err;
1595 o_found |= DL_OPT_DPIPE_TABLE_COUNTERS;
1596 } else if ((dl_argv_match(dl, "encap") || /* Original incorrect implementation */
1597 dl_argv_match(dl, "encap-mode")) &&
1598 (o_all & DL_OPT_ESWITCH_ENCAP_MODE)) {
1599 const char *typestr;
1600
1601 dl_arg_inc(dl);
1602 err = dl_argv_str(dl, &typestr);
1603 if (err)
1604 return err;
1605 err = eswitch_encap_mode_get(typestr,
1606 &opts->eswitch_encap_mode);
1607 if (err)
1608 return err;
1609 o_found |= DL_OPT_ESWITCH_ENCAP_MODE;
1610 } else if (dl_argv_match(dl, "path") &&
1611 (o_all & DL_OPT_RESOURCE_PATH)) {
1612 dl_arg_inc(dl);
1613 err = dl_argv_str(dl, &opts->resource_path);
1614 if (err)
1615 return err;
1616 o_found |= DL_OPT_RESOURCE_PATH;
1617 } else if (dl_argv_match(dl, "size") &&
1618 (o_all & DL_OPT_RESOURCE_SIZE)) {
1619 dl_arg_inc(dl);
1620 err = dl_argv_uint64_t(dl, &opts->resource_size);
1621 if (err)
1622 return err;
1623 o_found |= DL_OPT_RESOURCE_SIZE;
1624 } else if (dl_argv_match(dl, "name") &&
1625 (o_all & DL_OPT_PARAM_NAME)) {
1626 dl_arg_inc(dl);
1627 err = dl_argv_str(dl, &opts->param_name);
1628 if (err)
1629 return err;
1630 o_found |= DL_OPT_PARAM_NAME;
1631 } else if (dl_argv_match(dl, "value") &&
1632 (o_all & DL_OPT_PARAM_VALUE)) {
1633 dl_arg_inc(dl);
1634 err = dl_argv_str(dl, &opts->param_value);
1635 if (err)
1636 return err;
1637 o_found |= DL_OPT_PARAM_VALUE;
1638 } else if (dl_argv_match(dl, "cmode") &&
1639 (o_all & DL_OPT_PARAM_CMODE)) {
1640 const char *cmodestr;
1641
1642 dl_arg_inc(dl);
1643 err = dl_argv_str(dl, &cmodestr);
1644 if (err)
1645 return err;
1646 err = param_cmode_get(cmodestr, &opts->cmode);
1647 if (err)
1648 return err;
1649 o_found |= DL_OPT_PARAM_CMODE;
1650 } else if (dl_argv_match(dl, "snapshot") &&
1651 (o_all & DL_OPT_REGION_SNAPSHOT_ID)) {
1652 dl_arg_inc(dl);
1653 err = dl_argv_uint32_t(dl, &opts->region_snapshot_id);
1654 if (err)
1655 return err;
1656 o_found |= DL_OPT_REGION_SNAPSHOT_ID;
1657 } else if (dl_argv_match(dl, "address") &&
1658 (o_all & DL_OPT_REGION_ADDRESS)) {
1659 dl_arg_inc(dl);
1660 err = dl_argv_uint64_t(dl, &opts->region_address);
1661 if (err)
1662 return err;
1663 o_found |= DL_OPT_REGION_ADDRESS;
1664 } else if (dl_argv_match(dl, "length") &&
1665 (o_all & DL_OPT_REGION_LENGTH)) {
1666 dl_arg_inc(dl);
1667 err = dl_argv_uint64_t(dl, &opts->region_length);
1668 if (err)
1669 return err;
1670 o_found |= DL_OPT_REGION_LENGTH;
1671 } else if (dl_argv_match(dl, "file") &&
1672 (o_all & DL_OPT_FLASH_FILE_NAME)) {
1673 dl_arg_inc(dl);
1674 err = dl_argv_str(dl, &opts->flash_file_name);
1675 if (err)
1676 return err;
1677 o_found |= DL_OPT_FLASH_FILE_NAME;
1678 } else if (dl_argv_match(dl, "component") &&
1679 (o_all & DL_OPT_FLASH_COMPONENT)) {
1680 dl_arg_inc(dl);
1681 err = dl_argv_str(dl, &opts->flash_component);
1682 if (err)
1683 return err;
1684 o_found |= DL_OPT_FLASH_COMPONENT;
1685
1686 } else if (dl_argv_match(dl, "overwrite") &&
1687 (o_all & DL_OPT_FLASH_OVERWRITE)) {
1688 const char *sectionstr;
1689
1690 dl_arg_inc(dl);
1691 err = dl_argv_str(dl, &sectionstr);
1692 if(err)
1693 return err;
1694 err = flash_overwrite_section_get(sectionstr,
1695 &opts->overwrite_mask);
1696 if (err)
1697 return err;
1698 o_found |= DL_OPT_FLASH_OVERWRITE;
1699
1700 } else if (dl_argv_match(dl, "reporter") &&
1701 (o_all & DL_OPT_HEALTH_REPORTER_NAME)) {
1702 dl_arg_inc(dl);
1703 err = dl_argv_str(dl, &opts->reporter_name);
1704 if (err)
1705 return err;
1706 o_found |= DL_OPT_HEALTH_REPORTER_NAME;
1707 } else if (dl_argv_match(dl, "grace_period") &&
1708 (o_all & DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD)) {
1709 dl_arg_inc(dl);
1710 err = dl_argv_uint64_t(dl,
1711 &opts->reporter_graceful_period);
1712 if (err)
1713 return err;
1714 o_found |= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD;
1715 } else if (dl_argv_match(dl, "auto_recover") &&
1716 (o_all & DL_OPT_HEALTH_REPORTER_AUTO_RECOVER)) {
1717 dl_arg_inc(dl);
1718 err = dl_argv_bool(dl, &opts->reporter_auto_recover);
1719 if (err)
1720 return err;
1721 o_found |= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER;
1722 } else if (dl_argv_match(dl, "auto_dump") &&
1723 (o_all & DL_OPT_HEALTH_REPORTER_AUTO_DUMP)) {
1724 dl_arg_inc(dl);
1725 err = dl_argv_bool(dl, &opts->reporter_auto_dump);
1726 if (err)
1727 return err;
1728 o_found |= DL_OPT_HEALTH_REPORTER_AUTO_DUMP;
1729 } else if (dl_argv_match(dl, "trap") &&
1730 (o_all & DL_OPT_TRAP_NAME)) {
1731 dl_arg_inc(dl);
1732 err = dl_argv_str(dl, &opts->trap_name);
1733 if (err)
1734 return err;
1735 o_found |= DL_OPT_TRAP_NAME;
1736 } else if (dl_argv_match(dl, "group") &&
1737 (o_all & DL_OPT_TRAP_GROUP_NAME)) {
1738 dl_arg_inc(dl);
1739 err = dl_argv_str(dl, &opts->trap_group_name);
1740 if (err)
1741 return err;
1742 o_found |= DL_OPT_TRAP_GROUP_NAME;
1743 } else if (dl_argv_match(dl, "action") &&
1744 (o_all & DL_OPT_TRAP_ACTION)) {
1745 const char *actionstr;
1746
1747 dl_arg_inc(dl);
1748 err = dl_argv_str(dl, &actionstr);
1749 if (err)
1750 return err;
1751 err = trap_action_get(actionstr, &opts->trap_action);
1752 if (err)
1753 return err;
1754 o_found |= DL_OPT_TRAP_ACTION;
1755 } else if (dl_argv_match(dl, "netns") &&
1756 (o_all & DL_OPT_NETNS)) {
1757 const char *netns_str;
1758
1759 dl_arg_inc(dl);
1760 err = dl_argv_str(dl, &netns_str);
1761 if (err)
1762 return err;
1763 opts->netns = netns_get_fd(netns_str);
1764 if ((int)opts->netns < 0) {
1765 dl_arg_dec(dl);
1766 err = dl_argv_uint32_t(dl, &opts->netns);
1767 if (err)
1768 return err;
1769 opts->netns_is_pid = true;
1770 }
1771 o_found |= DL_OPT_NETNS;
1772 } else if (dl_argv_match(dl, "action") &&
1773 (o_all & DL_OPT_RELOAD_ACTION)) {
1774 const char *actionstr;
1775
1776 dl_arg_inc(dl);
1777 err = dl_argv_str(dl, &actionstr);
1778 if (err)
1779 return err;
1780 err = reload_action_get(dl, actionstr, &opts->reload_action);
1781 if (err)
1782 return err;
1783 o_found |= DL_OPT_RELOAD_ACTION;
1784 } else if (dl_argv_match(dl, "limit") &&
1785 (o_all & DL_OPT_RELOAD_LIMIT)) {
1786 const char *limitstr;
1787
1788 dl_arg_inc(dl);
1789 err = dl_argv_str(dl, &limitstr);
1790 if (err)
1791 return err;
1792 err = reload_limit_get(dl, limitstr, &opts->reload_limit);
1793 if (err)
1794 return err;
1795 o_found |= DL_OPT_RELOAD_LIMIT;
1796 } else if (dl_argv_match(dl, "policer") &&
1797 (o_all & DL_OPT_TRAP_POLICER_ID)) {
1798 dl_arg_inc(dl);
1799 err = dl_argv_uint32_t(dl, &opts->trap_policer_id);
1800 if (err)
1801 return err;
1802 o_found |= DL_OPT_TRAP_POLICER_ID;
1803 } else if (dl_argv_match(dl, "nopolicer") &&
1804 (o_all & DL_OPT_TRAP_POLICER_ID)) {
1805 dl_arg_inc(dl);
1806 opts->trap_policer_id = 0;
1807 o_found |= DL_OPT_TRAP_POLICER_ID;
1808 } else if (dl_argv_match(dl, "rate") &&
1809 (o_all & DL_OPT_TRAP_POLICER_RATE)) {
1810 dl_arg_inc(dl);
1811 err = dl_argv_uint64_t(dl, &opts->trap_policer_rate);
1812 if (err)
1813 return err;
1814 o_found |= DL_OPT_TRAP_POLICER_RATE;
1815 } else if (dl_argv_match(dl, "burst") &&
1816 (o_all & DL_OPT_TRAP_POLICER_BURST)) {
1817 dl_arg_inc(dl);
1818 err = dl_argv_uint64_t(dl, &opts->trap_policer_burst);
1819 if (err)
1820 return err;
1821 o_found |= DL_OPT_TRAP_POLICER_BURST;
1822 } else if (dl_argv_match(dl, "hw_addr") &&
1823 (o_all & DL_OPT_PORT_FUNCTION_HW_ADDR)) {
1824 const char *addrstr;
1825
1826 dl_arg_inc(dl);
1827 err = dl_argv_str(dl, &addrstr);
1828 if (err)
1829 return err;
1830 err = hw_addr_parse(addrstr, opts->port_function_hw_addr,
1831 &opts->port_function_hw_addr_len);
1832 if (err)
1833 return err;
1834 o_found |= DL_OPT_PORT_FUNCTION_HW_ADDR;
1835
1836 } else {
1837 pr_err("Unknown option \"%s\"\n", dl_argv(dl));
1838 return -EINVAL;
1839 }
1840 }
1841
1842 opts->present = o_found;
1843
1844 if ((o_optional & DL_OPT_SB) && !(o_found & DL_OPT_SB)) {
1845 opts->sb_index = 0;
1846 opts->present |= DL_OPT_SB;
1847 }
1848
1849 return dl_args_finding_required_validate(o_required, o_found);
1850 }
1851
1852 static void
1853 dl_function_attr_put(struct nlmsghdr *nlh, const struct dl_opts *opts)
1854 {
1855 struct nlattr *nest;
1856
1857 nest = mnl_attr_nest_start(nlh, DEVLINK_ATTR_PORT_FUNCTION);
1858 mnl_attr_put(nlh, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR,
1859 opts->port_function_hw_addr_len,
1860 opts->port_function_hw_addr);
1861 mnl_attr_nest_end(nlh, nest);
1862 }
1863
1864 static void
1865 dl_flash_update_overwrite_put(struct nlmsghdr *nlh, const struct dl_opts *opts)
1866 {
1867 struct nla_bitfield32 overwrite_mask;
1868
1869 overwrite_mask.selector = DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS;
1870 overwrite_mask.value = opts->overwrite_mask;
1871
1872 mnl_attr_put(nlh, DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK,
1873 sizeof(overwrite_mask), &overwrite_mask);
1874 }
1875
1876 static void
1877 dl_reload_limits_put(struct nlmsghdr *nlh, const struct dl_opts *opts)
1878 {
1879 struct nla_bitfield32 limits;
1880
1881 limits.selector = DEVLINK_RELOAD_LIMITS_VALID_MASK;
1882 limits.value = BIT(opts->reload_limit);
1883 mnl_attr_put(nlh, DEVLINK_ATTR_RELOAD_LIMITS, sizeof(limits), &limits);
1884 }
1885
1886 static void dl_opts_put(struct nlmsghdr *nlh, struct dl *dl)
1887 {
1888 struct dl_opts *opts = &dl->opts;
1889
1890 if (opts->present & DL_OPT_HANDLE) {
1891 mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
1892 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
1893 } else if (opts->present & DL_OPT_HANDLEP) {
1894 mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
1895 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
1896 mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_INDEX,
1897 opts->port_index);
1898 } else if (opts->present & DL_OPT_HANDLE_REGION) {
1899 mnl_attr_put_strz(nlh, DEVLINK_ATTR_BUS_NAME, opts->bus_name);
1900 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DEV_NAME, opts->dev_name);
1901 mnl_attr_put_strz(nlh, DEVLINK_ATTR_REGION_NAME,
1902 opts->region_name);
1903 }
1904 if (opts->present & DL_OPT_PORT_TYPE)
1905 mnl_attr_put_u16(nlh, DEVLINK_ATTR_PORT_TYPE,
1906 opts->port_type);
1907 if (opts->present & DL_OPT_PORT_COUNT)
1908 mnl_attr_put_u32(nlh, DEVLINK_ATTR_PORT_SPLIT_COUNT,
1909 opts->port_count);
1910 if (opts->present & DL_OPT_SB)
1911 mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_INDEX,
1912 opts->sb_index);
1913 if (opts->present & DL_OPT_SB_POOL)
1914 mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_POOL_INDEX,
1915 opts->sb_pool_index);
1916 if (opts->present & DL_OPT_SB_SIZE)
1917 mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_POOL_SIZE,
1918 opts->sb_pool_size);
1919 if (opts->present & DL_OPT_SB_TYPE)
1920 mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_TYPE,
1921 opts->sb_pool_type);
1922 if (opts->present & DL_OPT_SB_THTYPE)
1923 mnl_attr_put_u8(nlh, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
1924 opts->sb_pool_thtype);
1925 if (opts->present & DL_OPT_SB_TH)
1926 mnl_attr_put_u32(nlh, DEVLINK_ATTR_SB_THRESHOLD,
1927 opts->sb_threshold);
1928 if (opts->present & DL_OPT_SB_TC)
1929 mnl_attr_put_u16(nlh, DEVLINK_ATTR_SB_TC_INDEX,
1930 opts->sb_tc_index);
1931 if (opts->present & DL_OPT_ESWITCH_MODE)
1932 mnl_attr_put_u16(nlh, DEVLINK_ATTR_ESWITCH_MODE,
1933 opts->eswitch_mode);
1934 if (opts->present & DL_OPT_ESWITCH_INLINE_MODE)
1935 mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1936 opts->eswitch_inline_mode);
1937 if (opts->present & DL_OPT_DPIPE_TABLE_NAME)
1938 mnl_attr_put_strz(nlh, DEVLINK_ATTR_DPIPE_TABLE_NAME,
1939 opts->dpipe_table_name);
1940 if (opts->present & DL_OPT_DPIPE_TABLE_COUNTERS)
1941 mnl_attr_put_u8(nlh, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1942 opts->dpipe_counters_enabled);
1943 if (opts->present & DL_OPT_ESWITCH_ENCAP_MODE)
1944 mnl_attr_put_u8(nlh, DEVLINK_ATTR_ESWITCH_ENCAP_MODE,
1945 opts->eswitch_encap_mode);
1946 if ((opts->present & DL_OPT_RESOURCE_PATH) && opts->resource_id_valid)
1947 mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_ID,
1948 opts->resource_id);
1949 if (opts->present & DL_OPT_RESOURCE_SIZE)
1950 mnl_attr_put_u64(nlh, DEVLINK_ATTR_RESOURCE_SIZE,
1951 opts->resource_size);
1952 if (opts->present & DL_OPT_PARAM_NAME)
1953 mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_NAME,
1954 opts->param_name);
1955 if (opts->present & DL_OPT_PARAM_CMODE)
1956 mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_CMODE,
1957 opts->cmode);
1958 if (opts->present & DL_OPT_REGION_SNAPSHOT_ID)
1959 mnl_attr_put_u32(nlh, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
1960 opts->region_snapshot_id);
1961 if (opts->present & DL_OPT_REGION_ADDRESS)
1962 mnl_attr_put_u64(nlh, DEVLINK_ATTR_REGION_CHUNK_ADDR,
1963 opts->region_address);
1964 if (opts->present & DL_OPT_REGION_LENGTH)
1965 mnl_attr_put_u64(nlh, DEVLINK_ATTR_REGION_CHUNK_LEN,
1966 opts->region_length);
1967 if (opts->present & DL_OPT_FLASH_FILE_NAME)
1968 mnl_attr_put_strz(nlh, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME,
1969 opts->flash_file_name);
1970 if (opts->present & DL_OPT_FLASH_COMPONENT)
1971 mnl_attr_put_strz(nlh, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
1972 opts->flash_component);
1973 if (opts->present & DL_OPT_FLASH_OVERWRITE)
1974 dl_flash_update_overwrite_put(nlh, opts);
1975 if (opts->present & DL_OPT_HEALTH_REPORTER_NAME)
1976 mnl_attr_put_strz(nlh, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
1977 opts->reporter_name);
1978 if (opts->present & DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD)
1979 mnl_attr_put_u64(nlh,
1980 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
1981 opts->reporter_graceful_period);
1982 if (opts->present & DL_OPT_HEALTH_REPORTER_AUTO_RECOVER)
1983 mnl_attr_put_u8(nlh, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
1984 opts->reporter_auto_recover);
1985 if (opts->present & DL_OPT_HEALTH_REPORTER_AUTO_DUMP)
1986 mnl_attr_put_u8(nlh, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
1987 opts->reporter_auto_dump);
1988 if (opts->present & DL_OPT_TRAP_NAME)
1989 mnl_attr_put_strz(nlh, DEVLINK_ATTR_TRAP_NAME,
1990 opts->trap_name);
1991 if (opts->present & DL_OPT_TRAP_GROUP_NAME)
1992 mnl_attr_put_strz(nlh, DEVLINK_ATTR_TRAP_GROUP_NAME,
1993 opts->trap_group_name);
1994 if (opts->present & DL_OPT_TRAP_ACTION)
1995 mnl_attr_put_u8(nlh, DEVLINK_ATTR_TRAP_ACTION,
1996 opts->trap_action);
1997 if (opts->present & DL_OPT_NETNS)
1998 mnl_attr_put_u32(nlh,
1999 opts->netns_is_pid ? DEVLINK_ATTR_NETNS_PID :
2000 DEVLINK_ATTR_NETNS_FD,
2001 opts->netns);
2002 if (opts->present & DL_OPT_RELOAD_ACTION)
2003 mnl_attr_put_u8(nlh, DEVLINK_ATTR_RELOAD_ACTION,
2004 opts->reload_action);
2005 if (opts->present & DL_OPT_RELOAD_LIMIT)
2006 dl_reload_limits_put(nlh, opts);
2007 if (opts->present & DL_OPT_TRAP_POLICER_ID)
2008 mnl_attr_put_u32(nlh, DEVLINK_ATTR_TRAP_POLICER_ID,
2009 opts->trap_policer_id);
2010 if (opts->present & DL_OPT_TRAP_POLICER_RATE)
2011 mnl_attr_put_u64(nlh, DEVLINK_ATTR_TRAP_POLICER_RATE,
2012 opts->trap_policer_rate);
2013 if (opts->present & DL_OPT_TRAP_POLICER_BURST)
2014 mnl_attr_put_u64(nlh, DEVLINK_ATTR_TRAP_POLICER_BURST,
2015 opts->trap_policer_burst);
2016 if (opts->present & DL_OPT_PORT_FUNCTION_HW_ADDR)
2017 dl_function_attr_put(nlh, opts);
2018 }
2019
2020 static int dl_argv_parse_put(struct nlmsghdr *nlh, struct dl *dl,
2021 uint64_t o_required, uint64_t o_optional)
2022 {
2023 int err;
2024
2025 err = dl_argv_parse(dl, o_required, o_optional);
2026 if (err)
2027 return err;
2028 dl_opts_put(nlh, dl);
2029 return 0;
2030 }
2031
2032 static bool dl_dump_filter(struct dl *dl, struct nlattr **tb)
2033 {
2034 struct dl_opts *opts = &dl->opts;
2035 struct nlattr *attr_bus_name = tb[DEVLINK_ATTR_BUS_NAME];
2036 struct nlattr *attr_dev_name = tb[DEVLINK_ATTR_DEV_NAME];
2037 struct nlattr *attr_port_index = tb[DEVLINK_ATTR_PORT_INDEX];
2038 struct nlattr *attr_sb_index = tb[DEVLINK_ATTR_SB_INDEX];
2039
2040 if (opts->present & DL_OPT_HANDLE &&
2041 attr_bus_name && attr_dev_name) {
2042 const char *bus_name = mnl_attr_get_str(attr_bus_name);
2043 const char *dev_name = mnl_attr_get_str(attr_dev_name);
2044
2045 if (strcmp(bus_name, opts->bus_name) != 0 ||
2046 strcmp(dev_name, opts->dev_name) != 0)
2047 return false;
2048 }
2049 if (opts->present & DL_OPT_HANDLEP &&
2050 attr_bus_name && attr_dev_name && attr_port_index) {
2051 const char *bus_name = mnl_attr_get_str(attr_bus_name);
2052 const char *dev_name = mnl_attr_get_str(attr_dev_name);
2053 uint32_t port_index = mnl_attr_get_u32(attr_port_index);
2054
2055 if (strcmp(bus_name, opts->bus_name) != 0 ||
2056 strcmp(dev_name, opts->dev_name) != 0 ||
2057 port_index != opts->port_index)
2058 return false;
2059 }
2060 if (opts->present & DL_OPT_SB && attr_sb_index) {
2061 uint32_t sb_index = mnl_attr_get_u32(attr_sb_index);
2062
2063 if (sb_index != opts->sb_index)
2064 return false;
2065 }
2066 return true;
2067 }
2068
2069 static void cmd_dev_help(void)
2070 {
2071 pr_err("Usage: devlink dev show [ DEV ]\n");
2072 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
2073 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
2074 pr_err(" [ encap-mode { none | basic } ]\n");
2075 pr_err(" devlink dev eswitch show DEV\n");
2076 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
2077 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
2078 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2079 pr_err(" [ action { driver_reinit | fw_activate } ] [ limit no_reset ]\n");
2080 pr_err(" devlink dev info [ DEV ]\n");
2081 pr_err(" devlink dev flash DEV file PATH [ component NAME ] [ overwrite SECTION ]\n");
2082 }
2083
2084 static bool cmp_arr_last_handle(struct dl *dl, const char *bus_name,
2085 const char *dev_name)
2086 {
2087 if (!dl->arr_last.present)
2088 return false;
2089 return strcmp(dl->arr_last.bus_name, bus_name) == 0 &&
2090 strcmp(dl->arr_last.dev_name, dev_name) == 0;
2091 }
2092
2093 static void arr_last_handle_set(struct dl *dl, const char *bus_name,
2094 const char *dev_name)
2095 {
2096 dl->arr_last.present = true;
2097 free(dl->arr_last.dev_name);
2098 free(dl->arr_last.bus_name);
2099 dl->arr_last.bus_name = strdup(bus_name);
2100 dl->arr_last.dev_name = strdup(dev_name);
2101 }
2102
2103 static bool should_arr_last_handle_start(struct dl *dl, const char *bus_name,
2104 const char *dev_name)
2105 {
2106 return !cmp_arr_last_handle(dl, bus_name, dev_name);
2107 }
2108
2109 static bool should_arr_last_handle_end(struct dl *dl, const char *bus_name,
2110 const char *dev_name)
2111 {
2112 return dl->arr_last.present &&
2113 !cmp_arr_last_handle(dl, bus_name, dev_name);
2114 }
2115
2116 static void __pr_out_handle_start(struct dl *dl, struct nlattr **tb,
2117 bool content, bool array)
2118 {
2119 const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
2120 const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
2121 char buf[64];
2122
2123 sprintf(buf, "%s/%s", bus_name, dev_name);
2124
2125 if (dl->json_output) {
2126 if (array) {
2127 if (should_arr_last_handle_end(dl, bus_name, dev_name))
2128 close_json_array(PRINT_JSON, NULL);
2129 if (should_arr_last_handle_start(dl, bus_name,
2130 dev_name)) {
2131 open_json_array(PRINT_JSON, buf);
2132 open_json_object(NULL);
2133 arr_last_handle_set(dl, bus_name, dev_name);
2134 } else {
2135 open_json_object(NULL);
2136 }
2137 } else {
2138 open_json_object(buf);
2139 }
2140 } else {
2141 if (array) {
2142 if (should_arr_last_handle_end(dl, bus_name, dev_name))
2143 __pr_out_indent_dec();
2144 if (should_arr_last_handle_start(dl, bus_name,
2145 dev_name)) {
2146 pr_out("%s%s", buf, content ? ":" : "");
2147 __pr_out_newline();
2148 __pr_out_indent_inc();
2149 arr_last_handle_set(dl, bus_name, dev_name);
2150 }
2151 } else {
2152 pr_out("%s%s", buf, content ? ":" : "");
2153 }
2154 }
2155 }
2156
2157 static void pr_out_handle_start_arr(struct dl *dl, struct nlattr **tb)
2158 {
2159 __pr_out_handle_start(dl, tb, true, true);
2160 }
2161
2162 static void pr_out_handle_end(struct dl *dl)
2163 {
2164 if (dl->json_output)
2165 close_json_object();
2166 else
2167 __pr_out_newline();
2168 }
2169
2170 static void pr_out_handle(struct dl *dl, struct nlattr **tb)
2171 {
2172 __pr_out_handle_start(dl, tb, false, false);
2173 pr_out_handle_end(dl);
2174 }
2175
2176 static bool cmp_arr_last_port_handle(struct dl *dl, const char *bus_name,
2177 const char *dev_name, uint32_t port_index)
2178 {
2179 return cmp_arr_last_handle(dl, bus_name, dev_name) &&
2180 dl->arr_last.port_index == port_index;
2181 }
2182
2183 static void arr_last_port_handle_set(struct dl *dl, const char *bus_name,
2184 const char *dev_name, uint32_t port_index)
2185 {
2186 arr_last_handle_set(dl, bus_name, dev_name);
2187 dl->arr_last.port_index = port_index;
2188 }
2189
2190 static bool should_arr_last_port_handle_start(struct dl *dl,
2191 const char *bus_name,
2192 const char *dev_name,
2193 uint32_t port_index)
2194 {
2195 return !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index);
2196 }
2197
2198 static bool should_arr_last_port_handle_end(struct dl *dl,
2199 const char *bus_name,
2200 const char *dev_name,
2201 uint32_t port_index)
2202 {
2203 return dl->arr_last.present &&
2204 !cmp_arr_last_port_handle(dl, bus_name, dev_name, port_index);
2205 }
2206
2207 static void __pr_out_port_handle_start(struct dl *dl, const char *bus_name,
2208 const char *dev_name,
2209 uint32_t port_index, bool try_nice,
2210 bool array)
2211 {
2212 static char buf[64];
2213 char *ifname = NULL;
2214
2215 if (dl->no_nice_names || !try_nice ||
2216 ifname_map_rev_lookup(dl, bus_name, dev_name,
2217 port_index, &ifname) != 0)
2218 sprintf(buf, "%s/%s/%d", bus_name, dev_name, port_index);
2219 else
2220 sprintf(buf, "%s", ifname);
2221
2222 if (dl->json_output) {
2223 if (array) {
2224 if (should_arr_last_port_handle_end(dl, bus_name,
2225 dev_name,
2226 port_index))
2227 close_json_array(PRINT_JSON, NULL);
2228 if (should_arr_last_port_handle_start(dl, bus_name,
2229 dev_name,
2230 port_index)) {
2231 open_json_array(PRINT_JSON, buf);
2232 open_json_object(NULL);
2233 arr_last_port_handle_set(dl, bus_name, dev_name,
2234 port_index);
2235 } else {
2236 open_json_object(NULL);
2237 }
2238 } else {
2239 open_json_object(buf);
2240 }
2241 } else {
2242 if (array) {
2243 if (should_arr_last_port_handle_end(dl, bus_name, dev_name, port_index))
2244 __pr_out_indent_dec();
2245 if (should_arr_last_port_handle_start(dl, bus_name,
2246 dev_name, port_index)) {
2247 pr_out("%s:", buf);
2248 __pr_out_newline();
2249 __pr_out_indent_inc();
2250 arr_last_port_handle_set(dl, bus_name, dev_name, port_index);
2251 }
2252 } else {
2253 pr_out("%s:", buf);
2254 }
2255 }
2256 }
2257
2258 static void pr_out_port_handle_start(struct dl *dl, struct nlattr **tb, bool try_nice)
2259 {
2260 const char *bus_name;
2261 const char *dev_name;
2262 uint32_t port_index;
2263
2264 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
2265 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
2266 port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
2267 __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, false);
2268 }
2269
2270 static void pr_out_port_handle_start_arr(struct dl *dl, struct nlattr **tb, bool try_nice)
2271 {
2272 const char *bus_name;
2273 const char *dev_name;
2274 uint32_t port_index;
2275
2276 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
2277 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
2278 port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
2279 __pr_out_port_handle_start(dl, bus_name, dev_name, port_index, try_nice, true);
2280 }
2281
2282 static void pr_out_port_handle_end(struct dl *dl)
2283 {
2284 if (dl->json_output)
2285 close_json_object();
2286 else
2287 pr_out("\n");
2288 }
2289
2290 static void pr_out_region_chunk_start(struct dl *dl, uint64_t addr)
2291 {
2292 if (dl->json_output) {
2293 print_uint(PRINT_JSON, "address", NULL, addr);
2294 open_json_array(PRINT_JSON, "data");
2295 }
2296 }
2297
2298 static void pr_out_region_chunk_end(struct dl *dl)
2299 {
2300 if (dl->json_output)
2301 close_json_array(PRINT_JSON, NULL);
2302 }
2303
2304 static void pr_out_region_chunk(struct dl *dl, uint8_t *data, uint32_t len,
2305 uint64_t addr)
2306 {
2307 static uint64_t align_val;
2308 uint32_t i = 0;
2309
2310 pr_out_region_chunk_start(dl, addr);
2311 while (i < len) {
2312 if (!dl->json_output)
2313 if (!(align_val % 16))
2314 pr_out("%s%016"PRIx64" ",
2315 align_val ? "\n" : "",
2316 addr);
2317
2318 align_val++;
2319
2320 if (dl->json_output)
2321 print_int(PRINT_JSON, NULL, NULL, data[i]);
2322 else
2323 pr_out("%02x ", data[i]);
2324
2325 addr++;
2326 i++;
2327 }
2328 pr_out_region_chunk_end(dl);
2329 }
2330
2331 static void pr_out_stats(struct dl *dl, struct nlattr *nla_stats)
2332 {
2333 struct nlattr *tb[DEVLINK_ATTR_STATS_MAX + 1] = {};
2334 int err;
2335
2336 if (!dl->stats)
2337 return;
2338
2339 err = mnl_attr_parse_nested(nla_stats, attr_stats_cb, tb);
2340 if (err != MNL_CB_OK)
2341 return;
2342
2343 pr_out_object_start(dl, "stats");
2344 pr_out_object_start(dl, "rx");
2345 if (tb[DEVLINK_ATTR_STATS_RX_BYTES])
2346 pr_out_u64(dl, "bytes",
2347 mnl_attr_get_u64(tb[DEVLINK_ATTR_STATS_RX_BYTES]));
2348 if (tb[DEVLINK_ATTR_STATS_RX_PACKETS])
2349 pr_out_u64(dl, "packets",
2350 mnl_attr_get_u64(tb[DEVLINK_ATTR_STATS_RX_PACKETS]));
2351 if (tb[DEVLINK_ATTR_STATS_RX_DROPPED])
2352 pr_out_u64(dl, "dropped",
2353 mnl_attr_get_u64(tb[DEVLINK_ATTR_STATS_RX_DROPPED]));
2354 pr_out_object_end(dl);
2355 pr_out_object_end(dl);
2356 }
2357
2358 static const char *param_cmode_name(uint8_t cmode)
2359 {
2360 switch (cmode) {
2361 case DEVLINK_PARAM_CMODE_RUNTIME:
2362 return PARAM_CMODE_RUNTIME_STR;
2363 case DEVLINK_PARAM_CMODE_DRIVERINIT:
2364 return PARAM_CMODE_DRIVERINIT_STR;
2365 case DEVLINK_PARAM_CMODE_PERMANENT:
2366 return PARAM_CMODE_PERMANENT_STR;
2367 default: return "<unknown type>";
2368 }
2369 }
2370
2371 static const char *reload_action_name(uint8_t reload_action)
2372 {
2373 switch (reload_action) {
2374 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
2375 return "driver_reinit";
2376 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
2377 return "fw_activate";
2378 default:
2379 return "<unknown reload action>";
2380 }
2381 }
2382
2383 static const char *reload_limit_name(uint8_t reload_limit)
2384 {
2385 switch (reload_limit) {
2386 case DEVLINK_RELOAD_LIMIT_UNSPEC:
2387 return "unspecified";
2388 case DEVLINK_RELOAD_LIMIT_NO_RESET:
2389 return "no_reset";
2390 default:
2391 return "<unknown reload action>";
2392 }
2393 }
2394
2395 static const char *eswitch_mode_name(uint32_t mode)
2396 {
2397 switch (mode) {
2398 case DEVLINK_ESWITCH_MODE_LEGACY: return ESWITCH_MODE_LEGACY;
2399 case DEVLINK_ESWITCH_MODE_SWITCHDEV: return ESWITCH_MODE_SWITCHDEV;
2400 default: return "<unknown mode>";
2401 }
2402 }
2403
2404 static const char *eswitch_inline_mode_name(uint32_t mode)
2405 {
2406 switch (mode) {
2407 case DEVLINK_ESWITCH_INLINE_MODE_NONE:
2408 return ESWITCH_INLINE_MODE_NONE;
2409 case DEVLINK_ESWITCH_INLINE_MODE_LINK:
2410 return ESWITCH_INLINE_MODE_LINK;
2411 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK:
2412 return ESWITCH_INLINE_MODE_NETWORK;
2413 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT:
2414 return ESWITCH_INLINE_MODE_TRANSPORT;
2415 default:
2416 return "<unknown mode>";
2417 }
2418 }
2419
2420 static const char *eswitch_encap_mode_name(uint32_t mode)
2421 {
2422 switch (mode) {
2423 case DEVLINK_ESWITCH_ENCAP_MODE_NONE:
2424 return ESWITCH_ENCAP_MODE_NONE;
2425 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC:
2426 return ESWITCH_ENCAP_MODE_BASIC;
2427 default:
2428 return "<unknown mode>";
2429 }
2430 }
2431
2432 static void pr_out_eswitch(struct dl *dl, struct nlattr **tb)
2433 {
2434 __pr_out_handle_start(dl, tb, true, false);
2435
2436 if (tb[DEVLINK_ATTR_ESWITCH_MODE]) {
2437 check_indent_newline(dl);
2438 print_string(PRINT_ANY, "mode", "mode %s",
2439 eswitch_mode_name(mnl_attr_get_u16(
2440 tb[DEVLINK_ATTR_ESWITCH_MODE])));
2441 }
2442 if (tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2443 check_indent_newline(dl);
2444 print_string(PRINT_ANY, "inline-mode", "inline-mode %s",
2445 eswitch_inline_mode_name(mnl_attr_get_u8(
2446 tb[DEVLINK_ATTR_ESWITCH_INLINE_MODE])));
2447 }
2448 if (tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2449 check_indent_newline(dl);
2450 print_string(PRINT_ANY, "encap-mode", "encap-mode %s",
2451 eswitch_encap_mode_name(mnl_attr_get_u8(
2452 tb[DEVLINK_ATTR_ESWITCH_ENCAP_MODE])));
2453 }
2454
2455 pr_out_handle_end(dl);
2456 }
2457
2458 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr *nlh, void *data)
2459 {
2460 struct dl *dl = data;
2461 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2462 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2463
2464 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2465 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
2466 return MNL_CB_ERROR;
2467 pr_out_eswitch(dl, tb);
2468 return MNL_CB_OK;
2469 }
2470
2471 static int cmd_dev_eswitch_show(struct dl *dl)
2472 {
2473 struct nlmsghdr *nlh;
2474 int err;
2475
2476 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_GET,
2477 NLM_F_REQUEST | NLM_F_ACK);
2478
2479 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
2480 if (err)
2481 return err;
2482
2483 pr_out_section_start(dl, "dev");
2484 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_eswitch_show_cb, dl);
2485 pr_out_section_end(dl);
2486 return err;
2487 }
2488
2489 static int cmd_dev_eswitch_set(struct dl *dl)
2490 {
2491 struct nlmsghdr *nlh;
2492 int err;
2493
2494 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_ESWITCH_SET,
2495 NLM_F_REQUEST | NLM_F_ACK);
2496
2497 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE,
2498 DL_OPT_ESWITCH_MODE |
2499 DL_OPT_ESWITCH_INLINE_MODE |
2500 DL_OPT_ESWITCH_ENCAP_MODE);
2501
2502 if (err)
2503 return err;
2504
2505 if (dl->opts.present == 1) {
2506 pr_err("Need to set at least one option\n");
2507 return -ENOENT;
2508 }
2509
2510 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
2511 }
2512
2513 static int cmd_dev_eswitch(struct dl *dl)
2514 {
2515 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
2516 cmd_dev_help();
2517 return 0;
2518 } else if (dl_argv_match(dl, "set")) {
2519 dl_arg_inc(dl);
2520 return cmd_dev_eswitch_set(dl);
2521 } else if (dl_argv_match(dl, "show")) {
2522 dl_arg_inc(dl);
2523 return cmd_dev_eswitch_show(dl);
2524 }
2525 pr_err("Command \"%s\" not found\n", dl_argv(dl));
2526 return -ENOENT;
2527 }
2528
2529 struct param_val_conv {
2530 const char *name;
2531 const char *vstr;
2532 uint32_t vuint;
2533 };
2534
2535 static bool param_val_conv_exists(const struct param_val_conv *param_val_conv,
2536 uint32_t len, const char *name)
2537 {
2538 uint32_t i;
2539
2540 for (i = 0; i < len; i++)
2541 if (!strcmp(param_val_conv[i].name, name))
2542 return true;
2543
2544 return false;
2545 }
2546
2547 static int
2548 param_val_conv_uint_get(const struct param_val_conv *param_val_conv,
2549 uint32_t len, const char *name, const char *vstr,
2550 uint32_t *vuint)
2551 {
2552 uint32_t i;
2553
2554 for (i = 0; i < len; i++)
2555 if (!strcmp(param_val_conv[i].name, name) &&
2556 !strcmp(param_val_conv[i].vstr, vstr)) {
2557 *vuint = param_val_conv[i].vuint;
2558 return 0;
2559 }
2560
2561 return -ENOENT;
2562 }
2563
2564 static int
2565 param_val_conv_str_get(const struct param_val_conv *param_val_conv,
2566 uint32_t len, const char *name, uint32_t vuint,
2567 const char **vstr)
2568 {
2569 uint32_t i;
2570
2571 for (i = 0; i < len; i++)
2572 if (!strcmp(param_val_conv[i].name, name) &&
2573 param_val_conv[i].vuint == vuint) {
2574 *vstr = param_val_conv[i].vstr;
2575 return 0;
2576 }
2577
2578 return -ENOENT;
2579 }
2580
2581 static const struct param_val_conv param_val_conv[] = {
2582 {
2583 .name = "fw_load_policy",
2584 .vstr = "driver",
2585 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER,
2586 },
2587 {
2588 .name = "fw_load_policy",
2589 .vstr = "flash",
2590 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH,
2591 },
2592 {
2593 .name = "fw_load_policy",
2594 .vstr = "disk",
2595 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK,
2596 },
2597 {
2598 .name = "reset_dev_on_drv_probe",
2599 .vstr = "unknown",
2600 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN,
2601 },
2602 {
2603 .name = "fw_load_policy",
2604 .vstr = "unknown",
2605 .vuint = DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN,
2606 },
2607 {
2608 .name = "reset_dev_on_drv_probe",
2609 .vstr = "always",
2610 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS,
2611 },
2612 {
2613 .name = "reset_dev_on_drv_probe",
2614 .vstr = "never",
2615 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER,
2616 },
2617 {
2618 .name = "reset_dev_on_drv_probe",
2619 .vstr = "disk",
2620 .vuint = DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK,
2621 },
2622 };
2623
2624 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2625
2626 static void pr_out_param_value(struct dl *dl, const char *nla_name,
2627 int nla_type, struct nlattr *nl)
2628 {
2629 struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {};
2630 struct nlattr *val_attr;
2631 const char *vstr;
2632 bool conv_exists;
2633 int err;
2634
2635 err = mnl_attr_parse_nested(nl, attr_cb, nla_value);
2636 if (err != MNL_CB_OK)
2637 return;
2638
2639 if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] ||
2640 (nla_type != MNL_TYPE_FLAG &&
2641 !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]))
2642 return;
2643
2644 check_indent_newline(dl);
2645 print_string(PRINT_ANY, "cmode", "cmode %s",
2646 param_cmode_name(mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE])));
2647
2648 val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA];
2649
2650 conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN,
2651 nla_name);
2652
2653 switch (nla_type) {
2654 case MNL_TYPE_U8:
2655 if (conv_exists) {
2656 err = param_val_conv_str_get(param_val_conv,
2657 PARAM_VAL_CONV_LEN,
2658 nla_name,
2659 mnl_attr_get_u8(val_attr),
2660 &vstr);
2661 if (err)
2662 return;
2663 print_string(PRINT_ANY, "value", " value %s", vstr);
2664 } else {
2665 print_uint(PRINT_ANY, "value", " value %u",
2666 mnl_attr_get_u8(val_attr));
2667 }
2668 break;
2669 case MNL_TYPE_U16:
2670 if (conv_exists) {
2671 err = param_val_conv_str_get(param_val_conv,
2672 PARAM_VAL_CONV_LEN,
2673 nla_name,
2674 mnl_attr_get_u16(val_attr),
2675 &vstr);
2676 if (err)
2677 return;
2678 print_string(PRINT_ANY, "value", " value %s", vstr);
2679 } else {
2680 print_uint(PRINT_ANY, "value", " value %u",
2681 mnl_attr_get_u16(val_attr));
2682 }
2683 break;
2684 case MNL_TYPE_U32:
2685 if (conv_exists) {
2686 err = param_val_conv_str_get(param_val_conv,
2687 PARAM_VAL_CONV_LEN,
2688 nla_name,
2689 mnl_attr_get_u32(val_attr),
2690 &vstr);
2691 if (err)
2692 return;
2693 print_string(PRINT_ANY, "value", " value %s", vstr);
2694 } else {
2695 print_uint(PRINT_ANY, "value", " value %u",
2696 mnl_attr_get_u32(val_attr));
2697 }
2698 break;
2699 case MNL_TYPE_STRING:
2700 print_string(PRINT_ANY, "value", " value %s",
2701 mnl_attr_get_str(val_attr));
2702 break;
2703 case MNL_TYPE_FLAG:
2704 print_bool(PRINT_ANY, "value", " value %s", val_attr);
2705 break;
2706 }
2707 }
2708
2709 static void pr_out_param(struct dl *dl, struct nlattr **tb, bool array)
2710 {
2711 struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {};
2712 struct nlattr *param_value_attr;
2713 const char *nla_name;
2714 int nla_type;
2715 int err;
2716
2717 err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param);
2718 if (err != MNL_CB_OK)
2719 return;
2720 if (!nla_param[DEVLINK_ATTR_PARAM_NAME] ||
2721 !nla_param[DEVLINK_ATTR_PARAM_TYPE] ||
2722 !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST])
2723 return;
2724
2725 if (array)
2726 pr_out_handle_start_arr(dl, tb);
2727 else
2728 __pr_out_handle_start(dl, tb, true, false);
2729
2730 nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]);
2731
2732 nla_name = mnl_attr_get_str(nla_param[DEVLINK_ATTR_PARAM_NAME]);
2733 check_indent_newline(dl);
2734 print_string(PRINT_ANY, "name", "name %s ", nla_name);
2735 if (!nla_param[DEVLINK_ATTR_PARAM_GENERIC])
2736 print_string(PRINT_ANY, "type", "type %s", "driver-specific");
2737 else
2738 print_string(PRINT_ANY, "type", "type %s", "generic");
2739
2740 pr_out_array_start(dl, "values");
2741 mnl_attr_for_each_nested(param_value_attr,
2742 nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) {
2743 pr_out_entry_start(dl);
2744 pr_out_param_value(dl, nla_name, nla_type, param_value_attr);
2745 pr_out_entry_end(dl);
2746 }
2747 pr_out_array_end(dl);
2748 pr_out_handle_end(dl);
2749 }
2750
2751 static int cmd_dev_param_show_cb(const struct nlmsghdr *nlh, void *data)
2752 {
2753 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2754 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2755 struct dl *dl = data;
2756
2757 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2758 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
2759 !tb[DEVLINK_ATTR_PARAM])
2760 return MNL_CB_ERROR;
2761 pr_out_param(dl, tb, true);
2762 return MNL_CB_OK;
2763 }
2764
2765 struct param_ctx {
2766 struct dl *dl;
2767 int nla_type;
2768 union {
2769 uint8_t vu8;
2770 uint16_t vu16;
2771 uint32_t vu32;
2772 const char *vstr;
2773 bool vbool;
2774 } value;
2775 };
2776
2777 static int cmd_dev_param_set_cb(const struct nlmsghdr *nlh, void *data)
2778 {
2779 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
2780 struct nlattr *nla_param[DEVLINK_ATTR_MAX + 1] = {};
2781 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
2782 struct nlattr *param_value_attr;
2783 enum devlink_param_cmode cmode;
2784 struct param_ctx *ctx = data;
2785 struct dl *dl = ctx->dl;
2786 int nla_type;
2787 int err;
2788
2789 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
2790 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
2791 !tb[DEVLINK_ATTR_PARAM])
2792 return MNL_CB_ERROR;
2793
2794 err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_PARAM], attr_cb, nla_param);
2795 if (err != MNL_CB_OK)
2796 return MNL_CB_ERROR;
2797
2798 if (!nla_param[DEVLINK_ATTR_PARAM_TYPE] ||
2799 !nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST])
2800 return MNL_CB_ERROR;
2801
2802 nla_type = mnl_attr_get_u8(nla_param[DEVLINK_ATTR_PARAM_TYPE]);
2803 mnl_attr_for_each_nested(param_value_attr,
2804 nla_param[DEVLINK_ATTR_PARAM_VALUES_LIST]) {
2805 struct nlattr *nla_value[DEVLINK_ATTR_MAX + 1] = {};
2806 struct nlattr *val_attr;
2807
2808 err = mnl_attr_parse_nested(param_value_attr,
2809 attr_cb, nla_value);
2810 if (err != MNL_CB_OK)
2811 return MNL_CB_ERROR;
2812
2813 if (!nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE] ||
2814 (nla_type != MNL_TYPE_FLAG &&
2815 !nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA]))
2816 return MNL_CB_ERROR;
2817
2818 cmode = mnl_attr_get_u8(nla_value[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
2819 if (cmode == dl->opts.cmode) {
2820 val_attr = nla_value[DEVLINK_ATTR_PARAM_VALUE_DATA];
2821 switch (nla_type) {
2822 case MNL_TYPE_U8:
2823 ctx->value.vu8 = mnl_attr_get_u8(val_attr);
2824 break;
2825 case MNL_TYPE_U16:
2826 ctx->value.vu16 = mnl_attr_get_u16(val_attr);
2827 break;
2828 case MNL_TYPE_U32:
2829 ctx->value.vu32 = mnl_attr_get_u32(val_attr);
2830 break;
2831 case MNL_TYPE_STRING:
2832 ctx->value.vstr = mnl_attr_get_str(val_attr);
2833 break;
2834 case MNL_TYPE_FLAG:
2835 ctx->value.vbool = val_attr ? true : false;
2836 break;
2837 }
2838 break;
2839 }
2840 }
2841 ctx->nla_type = nla_type;
2842 return MNL_CB_OK;
2843 }
2844
2845 static int cmd_dev_param_set(struct dl *dl)
2846 {
2847 struct param_ctx ctx = {};
2848 struct nlmsghdr *nlh;
2849 bool conv_exists;
2850 uint32_t val_u32 = 0;
2851 uint16_t val_u16;
2852 uint8_t val_u8;
2853 bool val_bool;
2854 int err;
2855
2856 err = dl_argv_parse(dl, DL_OPT_HANDLE |
2857 DL_OPT_PARAM_NAME |
2858 DL_OPT_PARAM_VALUE |
2859 DL_OPT_PARAM_CMODE, 0);
2860 if (err)
2861 return err;
2862
2863 /* Get value type */
2864 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_GET,
2865 NLM_F_REQUEST | NLM_F_ACK);
2866 dl_opts_put(nlh, dl);
2867
2868 ctx.dl = dl;
2869 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_param_set_cb, &ctx);
2870 if (err)
2871 return err;
2872
2873 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_SET,
2874 NLM_F_REQUEST | NLM_F_ACK);
2875 dl_opts_put(nlh, dl);
2876
2877 conv_exists = param_val_conv_exists(param_val_conv, PARAM_VAL_CONV_LEN,
2878 dl->opts.param_name);
2879
2880 mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_TYPE, ctx.nla_type);
2881 switch (ctx.nla_type) {
2882 case MNL_TYPE_U8:
2883 if (conv_exists) {
2884 err = param_val_conv_uint_get(param_val_conv,
2885 PARAM_VAL_CONV_LEN,
2886 dl->opts.param_name,
2887 dl->opts.param_value,
2888 &val_u32);
2889 val_u8 = val_u32;
2890 } else {
2891 err = strtouint8_t(dl->opts.param_value, &val_u8);
2892 }
2893 if (err)
2894 goto err_param_value_parse;
2895 if (val_u8 == ctx.value.vu8)
2896 return 0;
2897 mnl_attr_put_u8(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u8);
2898 break;
2899 case MNL_TYPE_U16:
2900 if (conv_exists) {
2901 err = param_val_conv_uint_get(param_val_conv,
2902 PARAM_VAL_CONV_LEN,
2903 dl->opts.param_name,
2904 dl->opts.param_value,
2905 &val_u32);
2906 val_u16 = val_u32;
2907 } else {
2908 err = strtouint16_t(dl->opts.param_value, &val_u16);
2909 }
2910 if (err)
2911 goto err_param_value_parse;
2912 if (val_u16 == ctx.value.vu16)
2913 return 0;
2914 mnl_attr_put_u16(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u16);
2915 break;
2916 case MNL_TYPE_U32:
2917 if (conv_exists)
2918 err = param_val_conv_uint_get(param_val_conv,
2919 PARAM_VAL_CONV_LEN,
2920 dl->opts.param_name,
2921 dl->opts.param_value,
2922 &val_u32);
2923 else
2924 err = strtouint32_t(dl->opts.param_value, &val_u32);
2925 if (err)
2926 goto err_param_value_parse;
2927 if (val_u32 == ctx.value.vu32)
2928 return 0;
2929 mnl_attr_put_u32(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA, val_u32);
2930 break;
2931 case MNL_TYPE_FLAG:
2932 err = strtobool(dl->opts.param_value, &val_bool);
2933 if (err)
2934 goto err_param_value_parse;
2935 if (val_bool == ctx.value.vbool)
2936 return 0;
2937 if (val_bool)
2938 mnl_attr_put(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
2939 0, NULL);
2940 break;
2941 case MNL_TYPE_STRING:
2942 mnl_attr_put_strz(nlh, DEVLINK_ATTR_PARAM_VALUE_DATA,
2943 dl->opts.param_value);
2944 if (!strcmp(dl->opts.param_value, ctx.value.vstr))
2945 return 0;
2946 break;
2947 default:
2948 printf("Value type not supported\n");
2949 return -ENOTSUP;
2950 }
2951 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
2952
2953 err_param_value_parse:
2954 pr_err("Value \"%s\" is not a number or not within range\n",
2955 dl->opts.param_value);
2956 return err;
2957 }
2958
2959 static int cmd_dev_param_show(struct dl *dl)
2960 {
2961 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
2962 struct nlmsghdr *nlh;
2963 int err;
2964
2965 if (dl_argc(dl) == 0)
2966 flags |= NLM_F_DUMP;
2967
2968 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PARAM_GET, flags);
2969
2970 if (dl_argc(dl) > 0) {
2971 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE |
2972 DL_OPT_PARAM_NAME, 0);
2973 if (err)
2974 return err;
2975 }
2976
2977 pr_out_section_start(dl, "param");
2978 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_param_show_cb, dl);
2979 pr_out_section_end(dl);
2980 return err;
2981 }
2982
2983 static int cmd_dev_param(struct dl *dl)
2984 {
2985 if (dl_argv_match(dl, "help")) {
2986 cmd_dev_help();
2987 return 0;
2988 } else if (dl_argv_match(dl, "show") ||
2989 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
2990 dl_arg_inc(dl);
2991 return cmd_dev_param_show(dl);
2992 } else if (dl_argv_match(dl, "set")) {
2993 dl_arg_inc(dl);
2994 return cmd_dev_param_set(dl);
2995 }
2996 pr_err("Command \"%s\" not found\n", dl_argv(dl));
2997 return -ENOENT;
2998 }
2999
3000 static void pr_out_action_stats(struct dl *dl, struct nlattr *action_stats)
3001 {
3002 struct nlattr *tb_stats_entry[DEVLINK_ATTR_MAX + 1] = {};
3003 struct nlattr *nla_reload_stats_entry, *nla_limit, *nla_value;
3004 enum devlink_reload_limit limit;
3005 uint32_t value;
3006 int err;
3007
3008 mnl_attr_for_each_nested(nla_reload_stats_entry, action_stats) {
3009 err = mnl_attr_parse_nested(nla_reload_stats_entry, attr_cb,
3010 tb_stats_entry);
3011 if (err != MNL_CB_OK)
3012 return;
3013
3014 nla_limit = tb_stats_entry[DEVLINK_ATTR_RELOAD_STATS_LIMIT];
3015 nla_value = tb_stats_entry[DEVLINK_ATTR_RELOAD_STATS_VALUE];
3016 if (!nla_limit || !nla_value)
3017 return;
3018
3019 check_indent_newline(dl);
3020 limit = mnl_attr_get_u8(nla_limit);
3021 value = mnl_attr_get_u32(nla_value);
3022 print_uint_name_value(reload_limit_name(limit), value);
3023 }
3024 }
3025
3026 static void pr_out_reload_stats(struct dl *dl, struct nlattr *reload_stats)
3027 {
3028 struct nlattr *nla_action_info, *nla_action, *nla_action_stats;
3029 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3030 enum devlink_reload_action action;
3031 int err;
3032
3033 mnl_attr_for_each_nested(nla_action_info, reload_stats) {
3034 err = mnl_attr_parse_nested(nla_action_info, attr_cb, tb);
3035 if (err != MNL_CB_OK)
3036 return;
3037 nla_action = tb[DEVLINK_ATTR_RELOAD_ACTION];
3038 nla_action_stats = tb[DEVLINK_ATTR_RELOAD_ACTION_STATS];
3039 if (!nla_action || !nla_action_stats)
3040 return;
3041
3042 action = mnl_attr_get_u8(nla_action);
3043 pr_out_object_start(dl, reload_action_name(action));
3044 pr_out_action_stats(dl, nla_action_stats);
3045 pr_out_object_end(dl);
3046 }
3047 }
3048
3049 static void pr_out_reload_data(struct dl *dl, struct nlattr **tb)
3050 {
3051 struct nlattr *nla_reload_stats, *nla_remote_reload_stats;
3052 struct nlattr *tb_stats[DEVLINK_ATTR_MAX + 1] = {};
3053 uint8_t reload_failed = 0;
3054 int err;
3055
3056 if (tb[DEVLINK_ATTR_RELOAD_FAILED])
3057 reload_failed = mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED]);
3058
3059 if (reload_failed) {
3060 check_indent_newline(dl);
3061 print_bool(PRINT_ANY, "reload_failed", "reload_failed %s", true);
3062 }
3063 if (!tb[DEVLINK_ATTR_DEV_STATS] || !dl->stats)
3064 return;
3065 err = mnl_attr_parse_nested(tb[DEVLINK_ATTR_DEV_STATS], attr_cb,
3066 tb_stats);
3067 if (err != MNL_CB_OK)
3068 return;
3069
3070 pr_out_object_start(dl, "stats");
3071
3072 nla_reload_stats = tb_stats[DEVLINK_ATTR_RELOAD_STATS];
3073 if (nla_reload_stats) {
3074 pr_out_object_start(dl, "reload");
3075 pr_out_reload_stats(dl, nla_reload_stats);
3076 pr_out_object_end(dl);
3077 }
3078 nla_remote_reload_stats = tb_stats[DEVLINK_ATTR_REMOTE_RELOAD_STATS];
3079 if (nla_remote_reload_stats) {
3080 pr_out_object_start(dl, "remote_reload");
3081 pr_out_reload_stats(dl, nla_remote_reload_stats);
3082 pr_out_object_end(dl);
3083 }
3084
3085 pr_out_object_end(dl);
3086 }
3087
3088
3089 static void pr_out_dev(struct dl *dl, struct nlattr **tb)
3090 {
3091 if ((tb[DEVLINK_ATTR_RELOAD_FAILED] && mnl_attr_get_u8(tb[DEVLINK_ATTR_RELOAD_FAILED])) ||
3092 (tb[DEVLINK_ATTR_DEV_STATS] && dl->stats)) {
3093 __pr_out_handle_start(dl, tb, true, false);
3094 pr_out_reload_data(dl, tb);
3095 pr_out_handle_end(dl);
3096 } else {
3097 pr_out_handle(dl, tb);
3098 }
3099 }
3100
3101 static int cmd_dev_show_cb(const struct nlmsghdr *nlh, void *data)
3102 {
3103 struct dl *dl = data;
3104 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3105 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3106
3107 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3108 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
3109 return MNL_CB_ERROR;
3110
3111 pr_out_dev(dl, tb);
3112 return MNL_CB_OK;
3113 }
3114
3115 static int cmd_dev_show(struct dl *dl)
3116 {
3117 struct nlmsghdr *nlh;
3118 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3119 int err;
3120
3121 if (dl_argc(dl) == 0)
3122 flags |= NLM_F_DUMP;
3123
3124 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_GET, flags);
3125
3126 if (dl_argc(dl) > 0) {
3127 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
3128 if (err)
3129 return err;
3130 }
3131
3132 pr_out_section_start(dl, "dev");
3133 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_show_cb, dl);
3134 pr_out_section_end(dl);
3135 return err;
3136 }
3137
3138 static void pr_out_reload_actions_performed(struct dl *dl, struct nlattr **tb)
3139 {
3140 struct nlattr *nla_actions_performed;
3141 struct nla_bitfield32 *actions;
3142 uint32_t actions_performed;
3143 uint16_t len;
3144 int action;
3145
3146 if (!tb[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED])
3147 return;
3148
3149 nla_actions_performed = tb[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED];
3150 len = mnl_attr_get_payload_len(nla_actions_performed);
3151 if (len != sizeof(*actions))
3152 return;
3153 actions = mnl_attr_get_payload(nla_actions_performed);
3154 if (!actions)
3155 return;
3156 g_new_line_count = 1; /* Avoid extra new line in non-json print */
3157 pr_out_array_start(dl, "reload_actions_performed");
3158 actions_performed = actions->value & actions->selector;
3159 for (action = 0; action <= DEVLINK_RELOAD_ACTION_MAX; action++) {
3160 if (BIT(action) & actions_performed) {
3161 check_indent_newline(dl);
3162 print_string(PRINT_ANY, NULL, "%s",
3163 reload_action_name(action));
3164 }
3165 }
3166 pr_out_array_end(dl);
3167 if (!dl->json_output)
3168 __pr_out_newline();
3169 }
3170
3171 static int cmd_dev_reload_cb(const struct nlmsghdr *nlh, void *data)
3172 {
3173 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3174 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3175 struct dl *dl = data;
3176
3177 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3178 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3179 !tb[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED])
3180 return MNL_CB_ERROR;
3181
3182 pr_out_section_start(dl, "reload");
3183 pr_out_reload_actions_performed(dl, tb);
3184 pr_out_section_end(dl);
3185
3186 return MNL_CB_OK;
3187 }
3188
3189 static int cmd_dev_reload(struct dl *dl)
3190 {
3191 struct nlmsghdr *nlh;
3192 int err;
3193
3194 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
3195 cmd_dev_help();
3196 return 0;
3197 }
3198
3199 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RELOAD,
3200 NLM_F_REQUEST | NLM_F_ACK);
3201
3202 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE,
3203 DL_OPT_NETNS | DL_OPT_RELOAD_ACTION |
3204 DL_OPT_RELOAD_LIMIT);
3205 if (err)
3206 return err;
3207
3208 return _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dev_reload_cb, dl);
3209 }
3210
3211 static void pr_out_versions_single(struct dl *dl, const struct nlmsghdr *nlh,
3212 const char *name, int type)
3213 {
3214 struct nlattr *version;
3215
3216 mnl_attr_for_each(version, nlh, sizeof(struct genlmsghdr)) {
3217 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3218 const char *ver_value;
3219 const char *ver_name;
3220 int err;
3221
3222 if (mnl_attr_get_type(version) != type)
3223 continue;
3224
3225 err = mnl_attr_parse_nested(version, attr_cb, tb);
3226 if (err != MNL_CB_OK)
3227 continue;
3228
3229 if (!tb[DEVLINK_ATTR_INFO_VERSION_NAME] ||
3230 !tb[DEVLINK_ATTR_INFO_VERSION_VALUE])
3231 continue;
3232
3233 if (name) {
3234 pr_out_object_start(dl, name);
3235 name = NULL;
3236 }
3237
3238 ver_name = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_NAME]);
3239 ver_value = mnl_attr_get_str(tb[DEVLINK_ATTR_INFO_VERSION_VALUE]);
3240
3241 check_indent_newline(dl);
3242 print_string_name_value(ver_name, ver_value);
3243 if (!dl->json_output)
3244 __pr_out_newline();
3245 }
3246
3247 if (!name)
3248 pr_out_object_end(dl);
3249 }
3250
3251 static void pr_out_info(struct dl *dl, const struct nlmsghdr *nlh,
3252 struct nlattr **tb, bool has_versions)
3253 {
3254 __pr_out_handle_start(dl, tb, true, false);
3255
3256 __pr_out_indent_inc();
3257 if (tb[DEVLINK_ATTR_INFO_DRIVER_NAME]) {
3258 struct nlattr *nla_drv = tb[DEVLINK_ATTR_INFO_DRIVER_NAME];
3259
3260 if (!dl->json_output)
3261 __pr_out_newline();
3262 check_indent_newline(dl);
3263 print_string(PRINT_ANY, "driver", "driver %s",
3264 mnl_attr_get_str(nla_drv));
3265 }
3266
3267 if (tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER]) {
3268 struct nlattr *nla_sn = tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER];
3269
3270 if (!dl->json_output)
3271 __pr_out_newline();
3272 check_indent_newline(dl);
3273 print_string(PRINT_ANY, "serial_number", "serial_number %s",
3274 mnl_attr_get_str(nla_sn));
3275 }
3276
3277 if (tb[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER]) {
3278 struct nlattr *nla_bsn = tb[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER];
3279
3280 if (!dl->json_output)
3281 __pr_out_newline();
3282 check_indent_newline(dl);
3283 print_string(PRINT_ANY, "board.serial_number", "board.serial_number %s",
3284 mnl_attr_get_str(nla_bsn));
3285 }
3286 __pr_out_indent_dec();
3287
3288 if (has_versions) {
3289 pr_out_object_start(dl, "versions");
3290
3291 pr_out_versions_single(dl, nlh, "fixed",
3292 DEVLINK_ATTR_INFO_VERSION_FIXED);
3293 pr_out_versions_single(dl, nlh, "running",
3294 DEVLINK_ATTR_INFO_VERSION_RUNNING);
3295 pr_out_versions_single(dl, nlh, "stored",
3296 DEVLINK_ATTR_INFO_VERSION_STORED);
3297
3298 pr_out_object_end(dl);
3299 }
3300
3301 pr_out_handle_end(dl);
3302 }
3303
3304 static int cmd_versions_show_cb(const struct nlmsghdr *nlh, void *data)
3305 {
3306 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3307 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3308 bool has_versions, has_info;
3309 struct dl *dl = data;
3310
3311 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3312
3313 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
3314 return MNL_CB_ERROR;
3315
3316 has_versions = tb[DEVLINK_ATTR_INFO_VERSION_FIXED] ||
3317 tb[DEVLINK_ATTR_INFO_VERSION_RUNNING] ||
3318 tb[DEVLINK_ATTR_INFO_VERSION_STORED];
3319 has_info = tb[DEVLINK_ATTR_INFO_DRIVER_NAME] ||
3320 tb[DEVLINK_ATTR_INFO_SERIAL_NUMBER] ||
3321 tb[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER] ||
3322 has_versions;
3323
3324 if (has_info)
3325 pr_out_info(dl, nlh, tb, has_versions);
3326
3327 return MNL_CB_OK;
3328 }
3329
3330 static int cmd_dev_info(struct dl *dl)
3331 {
3332 struct nlmsghdr *nlh;
3333 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3334 int err;
3335
3336 if (dl_argv_match(dl, "help")) {
3337 cmd_dev_help();
3338 return 0;
3339 }
3340
3341 if (dl_argc(dl) == 0)
3342 flags |= NLM_F_DUMP;
3343
3344 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_INFO_GET, flags);
3345
3346 if (dl_argc(dl) > 0) {
3347 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
3348 if (err)
3349 return err;
3350 }
3351
3352 pr_out_section_start(dl, "info");
3353 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_versions_show_cb, dl);
3354 pr_out_section_end(dl);
3355 return err;
3356 }
3357
3358 struct cmd_dev_flash_status_ctx {
3359 struct dl *dl;
3360 struct timespec time_of_last_status;
3361 uint64_t status_msg_timeout;
3362 size_t elapsed_time_msg_len;
3363 char *last_msg;
3364 char *last_component;
3365 uint8_t not_first:1,
3366 last_pc:1,
3367 received_end:1,
3368 flash_done:1;
3369 };
3370
3371 static int nullstrcmp(const char *str1, const char *str2)
3372 {
3373 if (str1 && str2)
3374 return strcmp(str1, str2);
3375 if (!str1 && !str2)
3376 return 0;
3377 return str1 ? 1 : -1;
3378 }
3379
3380 static void cmd_dev_flash_clear_elapsed_time(struct cmd_dev_flash_status_ctx *ctx)
3381 {
3382 int i;
3383
3384 for (i = 0; i < ctx->elapsed_time_msg_len; i++)
3385 pr_out_tty("\b \b");
3386
3387 ctx->elapsed_time_msg_len = 0;
3388 }
3389
3390 static int cmd_dev_flash_status_cb(const struct nlmsghdr *nlh, void *data)
3391 {
3392 struct cmd_dev_flash_status_ctx *ctx = data;
3393 struct dl_opts *opts = &ctx->dl->opts;
3394 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3395 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3396 const char *component = NULL;
3397 uint64_t done = 0, total = 0;
3398 const char *msg = NULL;
3399 const char *bus_name;
3400 const char *dev_name;
3401
3402 cmd_dev_flash_clear_elapsed_time(ctx);
3403
3404 if (genl->cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS &&
3405 genl->cmd != DEVLINK_CMD_FLASH_UPDATE_END)
3406 return MNL_CB_STOP;
3407
3408 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3409 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
3410 return MNL_CB_ERROR;
3411 bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
3412 dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
3413 if (strcmp(bus_name, opts->bus_name) ||
3414 strcmp(dev_name, opts->dev_name))
3415 return MNL_CB_ERROR;
3416
3417 if (genl->cmd == DEVLINK_CMD_FLASH_UPDATE_END && ctx->not_first) {
3418 pr_out("\n");
3419 free(ctx->last_msg);
3420 free(ctx->last_component);
3421 ctx->received_end = 1;
3422 return MNL_CB_STOP;
3423 }
3424
3425 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG])
3426 msg = mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]);
3427 if (tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT])
3428 component = mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]);
3429 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE])
3430 done = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE]);
3431 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL])
3432 total = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL]);
3433 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT])
3434 ctx->status_msg_timeout = mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT]);
3435 else
3436 ctx->status_msg_timeout = 0;
3437
3438 if (!nullstrcmp(msg, ctx->last_msg) &&
3439 !nullstrcmp(component, ctx->last_component) &&
3440 ctx->last_pc && ctx->not_first) {
3441 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3442 } else {
3443 /* only update the last status timestamp if the message changed */
3444 clock_gettime(CLOCK_MONOTONIC, &ctx->time_of_last_status);
3445
3446 if (ctx->not_first)
3447 pr_out("\n");
3448 if (component) {
3449 pr_out("[%s] ", component);
3450 free(ctx->last_component);
3451 ctx->last_component = strdup(component);
3452 }
3453 if (msg) {
3454 pr_out("%s", msg);
3455 free(ctx->last_msg);
3456 ctx->last_msg = strdup(msg);
3457 }
3458 }
3459 if (total) {
3460 pr_out_tty(" %3lu%%", (done * 100) / total);
3461 ctx->last_pc = 1;
3462 } else {
3463 ctx->last_pc = 0;
3464 }
3465 fflush(stdout);
3466 ctx->not_first = 1;
3467
3468 return MNL_CB_STOP;
3469 }
3470
3471 static void cmd_dev_flash_time_elapsed(struct cmd_dev_flash_status_ctx *ctx)
3472 {
3473 struct timespec now, res;
3474
3475 clock_gettime(CLOCK_MONOTONIC, &now);
3476
3477 res.tv_sec = now.tv_sec - ctx->time_of_last_status.tv_sec;
3478 res.tv_nsec = now.tv_nsec - ctx->time_of_last_status.tv_nsec;
3479 if (res.tv_nsec < 0) {
3480 res.tv_sec--;
3481 res.tv_nsec += 1000000000L;
3482 }
3483
3484 /* Only begin displaying an elapsed time message if we've waited a few
3485 * seconds with no response, or the status message included a timeout
3486 * value.
3487 */
3488 if (res.tv_sec > 2 || ctx->status_msg_timeout) {
3489 uint64_t elapsed_m, elapsed_s;
3490 char msg[128];
3491 size_t len;
3492
3493 /* clear the last elapsed time message, if we have one */
3494 cmd_dev_flash_clear_elapsed_time(ctx);
3495
3496 elapsed_m = res.tv_sec / 60;
3497 elapsed_s = res.tv_sec % 60;
3498
3499 /**
3500 * If we've elapsed a few seconds without receiving any status
3501 * notification from the device, we display a time elapsed
3502 * message. This has a few possible formats:
3503 *
3504 * 1) just time elapsed, when no timeout was provided
3505 * " ( Xm Ys )"
3506 * 2) time elapsed out of a timeout that came from the device
3507 * driver via DEVLINK_CMD_FLASH_UPDATE_STATUS_TIMEOUT
3508 * " ( Xm Ys : Am Ys)"
3509 * 3) time elapsed if we still receive no status after
3510 * reaching the provided timeout.
3511 * " ( Xm Ys : timeout reached )"
3512 */
3513 if (!ctx->status_msg_timeout) {
3514 len = snprintf(msg, sizeof(msg),
3515 " ( %lum %lus )", elapsed_m, elapsed_s);
3516 } else if (res.tv_sec <= ctx->status_msg_timeout) {
3517 uint64_t timeout_m, timeout_s;
3518
3519 timeout_m = ctx->status_msg_timeout / 60;
3520 timeout_s = ctx->status_msg_timeout % 60;
3521
3522 len = snprintf(msg, sizeof(msg),
3523 " ( %lum %lus : %lum %lus )",
3524 elapsed_m, elapsed_s, timeout_m, timeout_s);
3525 } else {
3526 len = snprintf(msg, sizeof(msg),
3527 " ( %lum %lus : timeout reached )", elapsed_m, elapsed_s);
3528 }
3529
3530 ctx->elapsed_time_msg_len = len;
3531
3532 pr_out_tty("%s", msg);
3533 fflush(stdout);
3534 }
3535 }
3536
3537 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx *ctx,
3538 struct mnlg_socket *nlg_ntf,
3539 int pipe_r)
3540 {
3541 int nlfd = mnlg_socket_get_fd(nlg_ntf);
3542 struct timeval timeout;
3543 fd_set fds[3];
3544 int fdmax;
3545 int i;
3546 int err;
3547 int err2;
3548
3549 for (i = 0; i < 3; i++)
3550 FD_ZERO(&fds[i]);
3551 FD_SET(pipe_r, &fds[0]);
3552 fdmax = pipe_r + 1;
3553 FD_SET(nlfd, &fds[0]);
3554 if (nlfd >= fdmax)
3555 fdmax = nlfd + 1;
3556
3557 /* select only for a short while (1/10th of a second) in order to
3558 * allow periodically updating the screen with an elapsed time
3559 * indicator.
3560 */
3561 timeout.tv_sec = 0;
3562 timeout.tv_usec = 100000;
3563
3564 while (select(fdmax, &fds[0], &fds[1], &fds[2], &timeout) < 0) {
3565 if (errno == EINTR)
3566 continue;
3567 pr_err("select() failed\n");
3568 return -errno;
3569 }
3570 if (FD_ISSET(nlfd, &fds[0])) {
3571 err = _mnlg_socket_recv_run(nlg_ntf,
3572 cmd_dev_flash_status_cb, ctx);
3573 if (err)
3574 return err;
3575 }
3576 if (FD_ISSET(pipe_r, &fds[0])) {
3577 err = read(pipe_r, &err2, sizeof(err2));
3578 if (err == -1) {
3579 pr_err("Failed to read pipe\n");
3580 return -errno;
3581 }
3582 if (err2)
3583 return err2;
3584 ctx->flash_done = 1;
3585 }
3586 cmd_dev_flash_time_elapsed(ctx);
3587 return 0;
3588 }
3589
3590
3591 static int cmd_dev_flash(struct dl *dl)
3592 {
3593 struct cmd_dev_flash_status_ctx ctx = {.dl = dl,};
3594 struct mnlg_socket *nlg_ntf;
3595 struct nlmsghdr *nlh;
3596 int pipe_r, pipe_w;
3597 int pipe_fds[2];
3598 pid_t pid;
3599 int err;
3600
3601 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
3602 cmd_dev_help();
3603 return 0;
3604 }
3605
3606 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_FLASH_UPDATE,
3607 NLM_F_REQUEST | NLM_F_ACK);
3608
3609 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_FLASH_FILE_NAME,
3610 DL_OPT_FLASH_COMPONENT | DL_OPT_FLASH_OVERWRITE);
3611 if (err)
3612 return err;
3613
3614 nlg_ntf = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
3615 if (!nlg_ntf)
3616 return err;
3617
3618 err = _mnlg_socket_group_add(nlg_ntf, DEVLINK_GENL_MCGRP_CONFIG_NAME);
3619 if (err)
3620 return err;
3621
3622 err = pipe(pipe_fds);
3623 if (err == -1)
3624 return -errno;
3625 pipe_r = pipe_fds[0];
3626 pipe_w = pipe_fds[1];
3627
3628 pid = fork();
3629 if (pid == -1) {
3630 close(pipe_r);
3631 close(pipe_w);
3632 return -errno;
3633 } else if (!pid) {
3634 /* In child, just execute the flash and pass returned
3635 * value through pipe once it is done.
3636 */
3637 int cc;
3638
3639 close(pipe_r);
3640 err = _mnlg_socket_send(dl->nlg, nlh);
3641 cc = write(pipe_w, &err, sizeof(err));
3642 close(pipe_w);
3643 exit(cc != sizeof(err));
3644 }
3645 close(pipe_w);
3646
3647 /* initialize starting time to allow comparison for when to begin
3648 * displaying a time elapsed message.
3649 */
3650 clock_gettime(CLOCK_MONOTONIC, &ctx.time_of_last_status);
3651
3652 do {
3653 err = cmd_dev_flash_fds_process(&ctx, nlg_ntf, pipe_r);
3654 if (err)
3655 goto out;
3656 } while (!ctx.flash_done || (ctx.not_first && !ctx.received_end));
3657
3658 err = _mnlg_socket_recv_run(dl->nlg, NULL, NULL);
3659 out:
3660 close(pipe_r);
3661 mnlg_socket_close(nlg_ntf);
3662 return err;
3663 }
3664
3665 static int cmd_dev(struct dl *dl)
3666 {
3667 if (dl_argv_match(dl, "help")) {
3668 cmd_dev_help();
3669 return 0;
3670 } else if (dl_argv_match(dl, "show") ||
3671 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3672 dl_arg_inc(dl);
3673 return cmd_dev_show(dl);
3674 } else if (dl_argv_match(dl, "eswitch")) {
3675 dl_arg_inc(dl);
3676 return cmd_dev_eswitch(dl);
3677 } else if (dl_argv_match(dl, "reload")) {
3678 dl_arg_inc(dl);
3679 return cmd_dev_reload(dl);
3680 } else if (dl_argv_match(dl, "param")) {
3681 dl_arg_inc(dl);
3682 return cmd_dev_param(dl);
3683 } else if (dl_argv_match(dl, "info")) {
3684 dl_arg_inc(dl);
3685 return cmd_dev_info(dl);
3686 } else if (dl_argv_match(dl, "flash")) {
3687 dl_arg_inc(dl);
3688 return cmd_dev_flash(dl);
3689 }
3690 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3691 return -ENOENT;
3692 }
3693
3694 static void cmd_port_help(void)
3695 {
3696 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3697 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3698 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3699 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3700 pr_err(" devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3701 pr_err(" devlink port health show [ DEV/PORT_INDEX reporter REPORTER_NAME ]\n");
3702 }
3703
3704 static const char *port_type_name(uint32_t type)
3705 {
3706 switch (type) {
3707 case DEVLINK_PORT_TYPE_NOTSET: return "notset";
3708 case DEVLINK_PORT_TYPE_AUTO: return "auto";
3709 case DEVLINK_PORT_TYPE_ETH: return "eth";
3710 case DEVLINK_PORT_TYPE_IB: return "ib";
3711 default: return "<unknown type>";
3712 }
3713 }
3714
3715 static const char *port_flavour_name(uint16_t flavour)
3716 {
3717 switch (flavour) {
3718 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
3719 return "physical";
3720 case DEVLINK_PORT_FLAVOUR_CPU:
3721 return "cpu";
3722 case DEVLINK_PORT_FLAVOUR_DSA:
3723 return "dsa";
3724 case DEVLINK_PORT_FLAVOUR_PCI_PF:
3725 return "pcipf";
3726 case DEVLINK_PORT_FLAVOUR_PCI_VF:
3727 return "pcivf";
3728 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
3729 return "virtual";
3730 default:
3731 return "<unknown flavour>";
3732 }
3733 }
3734
3735 static void pr_out_port_pfvf_num(struct dl *dl, struct nlattr **tb)
3736 {
3737 uint16_t fn_num;
3738
3739 if (tb[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER])
3740 print_uint(PRINT_ANY, "controller", " controller %u",
3741 mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]));
3742 if (tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
3743 fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
3744 print_uint(PRINT_ANY, "pfnum", " pfnum %u", fn_num);
3745 }
3746 if (tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]) {
3747 fn_num = mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_PCI_VF_NUMBER]);
3748 print_uint(PRINT_ANY, "vfnum", " vfnum %u", fn_num);
3749 }
3750 if (tb[DEVLINK_ATTR_PORT_EXTERNAL]) {
3751 uint8_t external;
3752
3753 external = mnl_attr_get_u8(tb[DEVLINK_ATTR_PORT_EXTERNAL]);
3754 print_bool(PRINT_ANY, "external", " external %s", external);
3755 }
3756 }
3757
3758 static void pr_out_port_function(struct dl *dl, struct nlattr **tb_port)
3759 {
3760 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {};
3761 unsigned char *data;
3762 SPRINT_BUF(hw_addr);
3763 uint32_t len;
3764 int err;
3765
3766 if (!tb_port[DEVLINK_ATTR_PORT_FUNCTION])
3767 return;
3768
3769 err = mnl_attr_parse_nested(tb_port[DEVLINK_ATTR_PORT_FUNCTION],
3770 function_attr_cb, tb);
3771 if (err != MNL_CB_OK)
3772 return;
3773
3774 if (!tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR])
3775 return;
3776
3777 len = mnl_attr_get_payload_len(tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]);
3778 data = mnl_attr_get_payload(tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]);
3779
3780 pr_out_object_start(dl, "function");
3781 check_indent_newline(dl);
3782 print_string(PRINT_ANY, "hw_addr", "hw_addr %s",
3783 ll_addr_n2a(data, len, 0, hw_addr, sizeof(hw_addr)));
3784 if (!dl->json_output)
3785 __pr_out_indent_dec();
3786 pr_out_object_end(dl);
3787 }
3788
3789 static void pr_out_port(struct dl *dl, struct nlattr **tb)
3790 {
3791 struct nlattr *pt_attr = tb[DEVLINK_ATTR_PORT_TYPE];
3792 struct nlattr *dpt_attr = tb[DEVLINK_ATTR_PORT_DESIRED_TYPE];
3793
3794 pr_out_port_handle_start(dl, tb, false);
3795 check_indent_newline(dl);
3796 if (pt_attr) {
3797 uint16_t port_type = mnl_attr_get_u16(pt_attr);
3798
3799 print_string(PRINT_ANY, "type", "type %s",
3800 port_type_name(port_type));
3801 if (dpt_attr) {
3802 uint16_t des_port_type = mnl_attr_get_u16(dpt_attr);
3803
3804 if (port_type != des_port_type)
3805 print_string(PRINT_ANY, "des_type", " des_type %s",
3806 port_type_name(des_port_type));
3807 }
3808 }
3809 if (tb[DEVLINK_ATTR_PORT_NETDEV_NAME]) {
3810 print_string(PRINT_ANY, "netdev", " netdev %s",
3811 mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_NETDEV_NAME]));
3812 }
3813 if (tb[DEVLINK_ATTR_PORT_IBDEV_NAME]) {
3814 print_string(PRINT_ANY, "ibdev", " ibdev %s",
3815 mnl_attr_get_str(tb[DEVLINK_ATTR_PORT_IBDEV_NAME]));
3816 }
3817 if (tb[DEVLINK_ATTR_PORT_FLAVOUR]) {
3818 uint16_t port_flavour =
3819 mnl_attr_get_u16(tb[DEVLINK_ATTR_PORT_FLAVOUR]);
3820
3821 print_string(PRINT_ANY, "flavour", " flavour %s",
3822 port_flavour_name(port_flavour));
3823
3824 switch (port_flavour) {
3825 case DEVLINK_PORT_FLAVOUR_PCI_PF:
3826 case DEVLINK_PORT_FLAVOUR_PCI_VF:
3827 pr_out_port_pfvf_num(dl, tb);
3828 break;
3829 default:
3830 break;
3831 }
3832 }
3833 if (tb[DEVLINK_ATTR_PORT_NUMBER]) {
3834 uint32_t port_number;
3835
3836 port_number = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_NUMBER]);
3837 print_uint(PRINT_ANY, "port", " port %u", port_number);
3838 }
3839 if (tb[DEVLINK_ATTR_PORT_SPLIT_GROUP])
3840 print_uint(PRINT_ANY, "split_group", " split_group %u",
3841 mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_SPLIT_GROUP]));
3842 if (tb[DEVLINK_ATTR_PORT_SPLITTABLE])
3843 print_bool(PRINT_ANY, "splittable", " splittable %s",
3844 mnl_attr_get_u8(tb[DEVLINK_ATTR_PORT_SPLITTABLE]));
3845 if (tb[DEVLINK_ATTR_PORT_LANES])
3846 print_uint(PRINT_ANY, "lanes", " lanes %u",
3847 mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_LANES]));
3848
3849 pr_out_port_function(dl, tb);
3850 pr_out_port_handle_end(dl);
3851 }
3852
3853 static int cmd_port_show_cb(const struct nlmsghdr *nlh, void *data)
3854 {
3855 struct dl *dl = data;
3856 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
3857 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
3858
3859 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
3860 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
3861 !tb[DEVLINK_ATTR_PORT_INDEX])
3862 return MNL_CB_ERROR;
3863 pr_out_port(dl, tb);
3864 return MNL_CB_OK;
3865 }
3866
3867 static int cmd_port_show(struct dl *dl)
3868 {
3869 struct nlmsghdr *nlh;
3870 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
3871 int err;
3872
3873 if (dl_argc(dl) == 0)
3874 flags |= NLM_F_DUMP;
3875
3876 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_GET, flags);
3877
3878 if (dl_argc(dl) > 0) {
3879 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0);
3880 if (err)
3881 return err;
3882 }
3883
3884 pr_out_section_start(dl, "port");
3885 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_port_show_cb, dl);
3886 pr_out_section_end(dl);
3887 return err;
3888 }
3889
3890 static int cmd_port_set(struct dl *dl)
3891 {
3892 struct nlmsghdr *nlh;
3893 int err;
3894
3895 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SET,
3896 NLM_F_REQUEST | NLM_F_ACK);
3897
3898 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_TYPE, 0);
3899 if (err)
3900 return err;
3901
3902 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3903 }
3904
3905 static int cmd_port_split(struct dl *dl)
3906 {
3907 struct nlmsghdr *nlh;
3908 int err;
3909
3910 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SPLIT,
3911 NLM_F_REQUEST | NLM_F_ACK);
3912
3913 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_COUNT, 0);
3914 if (err)
3915 return err;
3916
3917 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3918 }
3919
3920 static int cmd_port_unsplit(struct dl *dl)
3921 {
3922 struct nlmsghdr *nlh;
3923 int err;
3924
3925 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_UNSPLIT,
3926 NLM_F_REQUEST | NLM_F_ACK);
3927
3928 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP, 0);
3929 if (err)
3930 return err;
3931
3932 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3933 }
3934
3935 static void cmd_port_function_help(void)
3936 {
3937 pr_err("Usage: devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3938 }
3939
3940 static int cmd_port_function_set(struct dl *dl)
3941 {
3942 struct nlmsghdr *nlh;
3943 int err;
3944
3945 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_PORT_SET, NLM_F_REQUEST | NLM_F_ACK);
3946
3947 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_PORT_FUNCTION_HW_ADDR, 0);
3948 if (err)
3949 return err;
3950
3951 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
3952 }
3953
3954 static int cmd_port_function(struct dl *dl)
3955 {
3956 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
3957 cmd_port_function_help();
3958 return 0;
3959 } else if (dl_argv_match(dl, "set")) {
3960 dl_arg_inc(dl);
3961 return cmd_port_function_set(dl);
3962 }
3963 pr_err("Command \"%s\" not found\n", dl_argv(dl));
3964 return -ENOENT;
3965 }
3966
3967 static int cmd_health(struct dl *dl);
3968 static int __cmd_health_show(struct dl *dl, bool show_device, bool show_port);
3969
3970 static int cmd_port(struct dl *dl)
3971 {
3972 if (dl_argv_match(dl, "help")) {
3973 cmd_port_help();
3974 return 0;
3975 } else if (dl_argv_match(dl, "show") ||
3976 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
3977 dl_arg_inc(dl);
3978 return cmd_port_show(dl);
3979 } else if (dl_argv_match(dl, "set")) {
3980 dl_arg_inc(dl);
3981 return cmd_port_set(dl);
3982 } else if (dl_argv_match(dl, "split")) {
3983 dl_arg_inc(dl);
3984 return cmd_port_split(dl);
3985 } else if (dl_argv_match(dl, "unsplit")) {
3986 dl_arg_inc(dl);
3987 return cmd_port_unsplit(dl);
3988 } else if (dl_argv_match(dl, "function")) {
3989 dl_arg_inc(dl);
3990 return cmd_port_function(dl);
3991 } else if (dl_argv_match(dl, "health")) {
3992 dl_arg_inc(dl);
3993 if (dl_argv_match(dl, "list") || dl_no_arg(dl)
3994 || (dl_argv_match(dl, "show") && dl_argc(dl) == 1)) {
3995 dl_arg_inc(dl);
3996 return __cmd_health_show(dl, false, true);
3997 } else {
3998 return cmd_health(dl);
3999 }
4000 }
4001 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4002 return -ENOENT;
4003 }
4004
4005 static void cmd_sb_help(void)
4006 {
4007 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
4008 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
4009 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
4010 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
4011 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
4012 pr_err(" pool POOL_INDEX ]\n");
4013 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
4014 pr_err(" pool POOL_INDEX th THRESHOLD\n");
4015 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
4016 pr_err(" type { ingress | egress } ]\n");
4017 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
4018 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
4019 pr_err(" th THRESHOLD\n");
4020 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
4021 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
4022 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
4023 }
4024
4025 static void pr_out_sb(struct dl *dl, struct nlattr **tb)
4026 {
4027 pr_out_handle_start_arr(dl, tb);
4028 check_indent_newline(dl);
4029 print_uint(PRINT_ANY, "sb", "sb %u",
4030 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
4031 print_uint(PRINT_ANY, "size", " size %u",
4032 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_SIZE]));
4033 print_uint(PRINT_ANY, "ing_pools", " ing_pools %u",
4034 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT]));
4035 print_uint(PRINT_ANY, "eg_pools", " eg_pools %u",
4036 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT]));
4037 print_uint(PRINT_ANY, "ing_tcs", " ing_tcs %u",
4038 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT]));
4039 print_uint(PRINT_ANY, "eg_tcs", " eg_tcs %u",
4040 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT]));
4041 pr_out_handle_end(dl);
4042 }
4043
4044 static int cmd_sb_show_cb(const struct nlmsghdr *nlh, void *data)
4045 {
4046 struct dl *dl = data;
4047 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4048 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4049
4050 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4051 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4052 !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_SIZE] ||
4053 !tb[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT] ||
4054 !tb[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT] ||
4055 !tb[DEVLINK_ATTR_SB_INGRESS_TC_COUNT] ||
4056 !tb[DEVLINK_ATTR_SB_EGRESS_TC_COUNT])
4057 return MNL_CB_ERROR;
4058 pr_out_sb(dl, tb);
4059 return MNL_CB_OK;
4060 }
4061
4062 static int cmd_sb_show(struct dl *dl)
4063 {
4064 struct nlmsghdr *nlh;
4065 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
4066 int err;
4067
4068 if (dl_argc(dl) == 0)
4069 flags |= NLM_F_DUMP;
4070
4071 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_GET, flags);
4072
4073 if (dl_argc(dl) > 0) {
4074 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB);
4075 if (err)
4076 return err;
4077 }
4078
4079 pr_out_section_start(dl, "sb");
4080 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_show_cb, dl);
4081 pr_out_section_end(dl);
4082 return err;
4083 }
4084
4085 static const char *pool_type_name(uint8_t type)
4086 {
4087 switch (type) {
4088 case DEVLINK_SB_POOL_TYPE_INGRESS: return "ingress";
4089 case DEVLINK_SB_POOL_TYPE_EGRESS: return "egress";
4090 default: return "<unknown type>";
4091 }
4092 }
4093
4094 static const char *threshold_type_name(uint8_t type)
4095 {
4096 switch (type) {
4097 case DEVLINK_SB_THRESHOLD_TYPE_STATIC: return "static";
4098 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC: return "dynamic";
4099 default: return "<unknown type>";
4100 }
4101 }
4102
4103 static void pr_out_sb_pool(struct dl *dl, struct nlattr **tb)
4104 {
4105 pr_out_handle_start_arr(dl, tb);
4106 check_indent_newline(dl);
4107 print_uint(PRINT_ANY, "sb", "sb %u",
4108 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
4109 print_uint(PRINT_ANY, "pool", " pool %u",
4110 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]));
4111 print_string(PRINT_ANY, "type", " type %s",
4112 pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE])));
4113 print_uint(PRINT_ANY, "size", " size %u",
4114 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_SIZE]));
4115 print_string(PRINT_ANY, "thtype", " thtype %s",
4116 threshold_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])));
4117 if (tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE])
4118 print_uint(PRINT_ANY, "cell_size", " cell size %u",
4119 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_POOL_CELL_SIZE]));
4120 pr_out_handle_end(dl);
4121 }
4122
4123 static int cmd_sb_pool_show_cb(const struct nlmsghdr *nlh, void *data)
4124 {
4125 struct dl *dl = data;
4126 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4127 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4128
4129 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4130 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4131 !tb[DEVLINK_ATTR_SB_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_INDEX] ||
4132 !tb[DEVLINK_ATTR_SB_POOL_TYPE] || !tb[DEVLINK_ATTR_SB_POOL_SIZE] ||
4133 !tb[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
4134 return MNL_CB_ERROR;
4135 pr_out_sb_pool(dl, tb);
4136 return MNL_CB_OK;
4137 }
4138
4139 static int cmd_sb_pool_show(struct dl *dl)
4140 {
4141 struct nlmsghdr *nlh;
4142 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
4143 int err;
4144
4145 if (dl_argc(dl) == 0)
4146 flags |= NLM_F_DUMP;
4147
4148 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_GET, flags);
4149
4150 if (dl_argc(dl) > 0) {
4151 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL,
4152 DL_OPT_SB);
4153 if (err)
4154 return err;
4155 }
4156
4157 pr_out_section_start(dl, "pool");
4158 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_pool_show_cb, dl);
4159 pr_out_section_end(dl);
4160 return err;
4161 }
4162
4163 static int cmd_sb_pool_set(struct dl *dl)
4164 {
4165 struct nlmsghdr *nlh;
4166 int err;
4167
4168 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_POOL_SET,
4169 NLM_F_REQUEST | NLM_F_ACK);
4170
4171 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_SB_POOL |
4172 DL_OPT_SB_SIZE | DL_OPT_SB_THTYPE, DL_OPT_SB);
4173 if (err)
4174 return err;
4175
4176 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4177 }
4178
4179 static int cmd_sb_pool(struct dl *dl)
4180 {
4181 if (dl_argv_match(dl, "help")) {
4182 cmd_sb_help();
4183 return 0;
4184 } else if (dl_argv_match(dl, "show") ||
4185 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
4186 dl_arg_inc(dl);
4187 return cmd_sb_pool_show(dl);
4188 } else if (dl_argv_match(dl, "set")) {
4189 dl_arg_inc(dl);
4190 return cmd_sb_pool_set(dl);
4191 }
4192 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4193 return -ENOENT;
4194 }
4195
4196 static void pr_out_sb_port_pool(struct dl *dl, struct nlattr **tb)
4197 {
4198 pr_out_port_handle_start_arr(dl, tb, true);
4199 check_indent_newline(dl);
4200 print_uint(PRINT_ANY, "sb", "sb %u",
4201 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
4202 print_uint(PRINT_ANY, "pool", " pool %u",
4203 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]));
4204 print_uint(PRINT_ANY, "threshold", " threshold %u",
4205 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD]));
4206 pr_out_port_handle_end(dl);
4207 }
4208
4209 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr *nlh, void *data)
4210 {
4211 struct dl *dl = data;
4212 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4213 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4214
4215 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4216 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4217 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
4218 !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD])
4219 return MNL_CB_ERROR;
4220 pr_out_sb_port_pool(dl, tb);
4221 return MNL_CB_OK;
4222 }
4223
4224 static int cmd_sb_port_pool_show(struct dl *dl)
4225 {
4226 struct nlmsghdr *nlh;
4227 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
4228 int err;
4229
4230 if (dl_argc(dl) == 0)
4231 flags |= NLM_F_DUMP;
4232
4233 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags);
4234
4235 if (dl_argc(dl) > 0) {
4236 err = dl_argv_parse_put(nlh, dl,
4237 DL_OPT_HANDLEP | DL_OPT_SB_POOL,
4238 DL_OPT_SB);
4239 if (err)
4240 return err;
4241 }
4242
4243 pr_out_section_start(dl, "port_pool");
4244 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_port_pool_show_cb, dl);
4245 pr_out_section_end(dl);
4246 return 0;
4247 }
4248
4249 static int cmd_sb_port_pool_set(struct dl *dl)
4250 {
4251 struct nlmsghdr *nlh;
4252 int err;
4253
4254 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_SET,
4255 NLM_F_REQUEST | NLM_F_ACK);
4256
4257 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_POOL |
4258 DL_OPT_SB_TH, DL_OPT_SB);
4259 if (err)
4260 return err;
4261
4262 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4263 }
4264
4265 static int cmd_sb_port_pool(struct dl *dl)
4266 {
4267 if (dl_argv_match(dl, "help")) {
4268 cmd_sb_help();
4269 return 0;
4270 } else if (dl_argv_match(dl, "show") ||
4271 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
4272 dl_arg_inc(dl);
4273 return cmd_sb_port_pool_show(dl);
4274 } else if (dl_argv_match(dl, "set")) {
4275 dl_arg_inc(dl);
4276 return cmd_sb_port_pool_set(dl);
4277 }
4278 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4279 return -ENOENT;
4280 }
4281
4282 static int cmd_sb_port(struct dl *dl)
4283 {
4284 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
4285 cmd_sb_help();
4286 return 0;
4287 } else if (dl_argv_match(dl, "pool")) {
4288 dl_arg_inc(dl);
4289 return cmd_sb_port_pool(dl);
4290 }
4291 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4292 return -ENOENT;
4293 }
4294
4295 static void pr_out_sb_tc_bind(struct dl *dl, struct nlattr **tb)
4296 {
4297 pr_out_port_handle_start_arr(dl, tb, true);
4298 check_indent_newline(dl);
4299 print_uint(PRINT_ANY, "sb", "sb %u",
4300 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_INDEX]));
4301 print_uint(PRINT_ANY, "tc", " tc %u",
4302 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX]));
4303 print_string(PRINT_ANY, "type", " type %s",
4304 pool_type_name(mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE])));
4305 print_uint(PRINT_ANY, "pool", " pool %u",
4306 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]));
4307 print_uint(PRINT_ANY, "threshold", " threshold %u",
4308 mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_THRESHOLD]));
4309 pr_out_port_handle_end(dl);
4310 }
4311
4312 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr *nlh, void *data)
4313 {
4314 struct dl *dl = data;
4315 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4316 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4317
4318 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4319 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4320 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
4321 !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] ||
4322 !tb[DEVLINK_ATTR_SB_POOL_INDEX] || !tb[DEVLINK_ATTR_SB_THRESHOLD])
4323 return MNL_CB_ERROR;
4324 pr_out_sb_tc_bind(dl, tb);
4325 return MNL_CB_OK;
4326 }
4327
4328 static int cmd_sb_tc_bind_show(struct dl *dl)
4329 {
4330 struct nlmsghdr *nlh;
4331 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
4332 int err;
4333
4334 if (dl_argc(dl) == 0)
4335 flags |= NLM_F_DUMP;
4336
4337 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags);
4338
4339 if (dl_argc(dl) > 0) {
4340 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC |
4341 DL_OPT_SB_TYPE, DL_OPT_SB);
4342 if (err)
4343 return err;
4344 }
4345
4346 pr_out_section_start(dl, "tc_bind");
4347 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_sb_tc_bind_show_cb, dl);
4348 pr_out_section_end(dl);
4349 return err;
4350 }
4351
4352 static int cmd_sb_tc_bind_set(struct dl *dl)
4353 {
4354 struct nlmsghdr *nlh;
4355 int err;
4356
4357 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_SET,
4358 NLM_F_REQUEST | NLM_F_ACK);
4359
4360 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLEP | DL_OPT_SB_TC |
4361 DL_OPT_SB_TYPE | DL_OPT_SB_POOL | DL_OPT_SB_TH,
4362 DL_OPT_SB);
4363 if (err)
4364 return err;
4365
4366 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4367 }
4368
4369 static int cmd_sb_tc_bind(struct dl *dl)
4370 {
4371 if (dl_argv_match(dl, "help")) {
4372 cmd_sb_help();
4373 return 0;
4374 } else if (dl_argv_match(dl, "show") ||
4375 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
4376 dl_arg_inc(dl);
4377 return cmd_sb_tc_bind_show(dl);
4378 } else if (dl_argv_match(dl, "set")) {
4379 dl_arg_inc(dl);
4380 return cmd_sb_tc_bind_set(dl);
4381 }
4382 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4383 return -ENOENT;
4384 }
4385
4386 static int cmd_sb_tc(struct dl *dl)
4387 {
4388 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
4389 cmd_sb_help();
4390 return 0;
4391 } else if (dl_argv_match(dl, "bind")) {
4392 dl_arg_inc(dl);
4393 return cmd_sb_tc_bind(dl);
4394 }
4395 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4396 return -ENOENT;
4397 }
4398
4399 struct occ_item {
4400 struct list_head list;
4401 uint32_t index;
4402 uint32_t cur;
4403 uint32_t max;
4404 uint32_t bound_pool_index;
4405 };
4406
4407 struct occ_port {
4408 struct list_head list;
4409 char *bus_name;
4410 char *dev_name;
4411 uint32_t port_index;
4412 uint32_t sb_index;
4413 struct list_head pool_list;
4414 struct list_head ing_tc_list;
4415 struct list_head eg_tc_list;
4416 };
4417
4418 struct occ_show {
4419 struct dl *dl;
4420 int err;
4421 struct list_head port_list;
4422 };
4423
4424 static struct occ_item *occ_item_alloc(void)
4425 {
4426 return calloc(1, sizeof(struct occ_item));
4427 }
4428
4429 static void occ_item_free(struct occ_item *occ_item)
4430 {
4431 free(occ_item);
4432 }
4433
4434 static struct occ_port *occ_port_alloc(uint32_t port_index)
4435 {
4436 struct occ_port *occ_port;
4437
4438 occ_port = calloc(1, sizeof(*occ_port));
4439 if (!occ_port)
4440 return NULL;
4441 occ_port->port_index = port_index;
4442 INIT_LIST_HEAD(&occ_port->pool_list);
4443 INIT_LIST_HEAD(&occ_port->ing_tc_list);
4444 INIT_LIST_HEAD(&occ_port->eg_tc_list);
4445 return occ_port;
4446 }
4447
4448 static void occ_port_free(struct occ_port *occ_port)
4449 {
4450 struct occ_item *occ_item, *tmp;
4451
4452 list_for_each_entry_safe(occ_item, tmp, &occ_port->pool_list, list)
4453 occ_item_free(occ_item);
4454 list_for_each_entry_safe(occ_item, tmp, &occ_port->ing_tc_list, list)
4455 occ_item_free(occ_item);
4456 list_for_each_entry_safe(occ_item, tmp, &occ_port->eg_tc_list, list)
4457 occ_item_free(occ_item);
4458 }
4459
4460 static struct occ_show *occ_show_alloc(struct dl *dl)
4461 {
4462 struct occ_show *occ_show;
4463
4464 occ_show = calloc(1, sizeof(*occ_show));
4465 if (!occ_show)
4466 return NULL;
4467 occ_show->dl = dl;
4468 INIT_LIST_HEAD(&occ_show->port_list);
4469 return occ_show;
4470 }
4471
4472 static void occ_show_free(struct occ_show *occ_show)
4473 {
4474 struct occ_port *occ_port, *tmp;
4475
4476 list_for_each_entry_safe(occ_port, tmp, &occ_show->port_list, list)
4477 occ_port_free(occ_port);
4478 }
4479
4480 static struct occ_port *occ_port_get(struct occ_show *occ_show,
4481 struct nlattr **tb)
4482 {
4483 struct occ_port *occ_port;
4484 uint32_t port_index;
4485
4486 port_index = mnl_attr_get_u32(tb[DEVLINK_ATTR_PORT_INDEX]);
4487
4488 list_for_each_entry_reverse(occ_port, &occ_show->port_list, list) {
4489 if (occ_port->port_index == port_index)
4490 return occ_port;
4491 }
4492 occ_port = occ_port_alloc(port_index);
4493 if (!occ_port)
4494 return NULL;
4495 list_add_tail(&occ_port->list, &occ_show->port_list);
4496 return occ_port;
4497 }
4498
4499 static void pr_out_occ_show_item_list(const char *label, struct list_head *list,
4500 bool bound_pool)
4501 {
4502 struct occ_item *occ_item;
4503 int i = 1;
4504
4505 pr_out_sp(7, " %s:", label);
4506 list_for_each_entry(occ_item, list, list) {
4507 if ((i - 1) % 4 == 0 && i != 1)
4508 pr_out_sp(7, " ");
4509 if (bound_pool)
4510 pr_out_sp(7, "%2u(%u):", occ_item->index,
4511 occ_item->bound_pool_index);
4512 else
4513 pr_out_sp(7, "%2u:", occ_item->index);
4514 pr_out_sp(21, "%10u/%u", occ_item->cur, occ_item->max);
4515 if (i++ % 4 == 0)
4516 pr_out("\n");
4517 }
4518 if ((i - 1) % 4 != 0)
4519 pr_out("\n");
4520 }
4521
4522 static void pr_out_json_occ_show_item_list(struct dl *dl, const char *label,
4523 struct list_head *list,
4524 bool bound_pool)
4525 {
4526 struct occ_item *occ_item;
4527 char buf[32];
4528
4529 open_json_object(label);
4530 list_for_each_entry(occ_item, list, list) {
4531 sprintf(buf, "%u", occ_item->index);
4532 open_json_object(buf);
4533 if (bound_pool)
4534 print_uint(PRINT_JSON, "bound_pool", NULL,
4535 occ_item->bound_pool_index);
4536 print_uint(PRINT_JSON, "current", NULL, occ_item->cur);
4537 print_uint(PRINT_JSON, "max", NULL, occ_item->max);
4538 close_json_object();
4539 }
4540 close_json_object();
4541 }
4542
4543 static void pr_out_occ_show_port(struct dl *dl, struct occ_port *occ_port)
4544 {
4545 if (dl->json_output) {
4546 pr_out_json_occ_show_item_list(dl, "pool",
4547 &occ_port->pool_list, false);
4548 pr_out_json_occ_show_item_list(dl, "itc",
4549 &occ_port->ing_tc_list, true);
4550 pr_out_json_occ_show_item_list(dl, "etc",
4551 &occ_port->eg_tc_list, true);
4552 } else {
4553 pr_out("\n");
4554 pr_out_occ_show_item_list("pool", &occ_port->pool_list, false);
4555 pr_out_occ_show_item_list("itc", &occ_port->ing_tc_list, true);
4556 pr_out_occ_show_item_list("etc", &occ_port->eg_tc_list, true);
4557 }
4558 }
4559
4560 static void pr_out_occ_show(struct occ_show *occ_show)
4561 {
4562 struct dl *dl = occ_show->dl;
4563 struct dl_opts *opts = &dl->opts;
4564 struct occ_port *occ_port;
4565
4566 list_for_each_entry(occ_port, &occ_show->port_list, list) {
4567 __pr_out_port_handle_start(dl, opts->bus_name, opts->dev_name,
4568 occ_port->port_index, true, false);
4569 pr_out_occ_show_port(dl, occ_port);
4570 pr_out_port_handle_end(dl);
4571 }
4572 }
4573
4574 static void cmd_sb_occ_port_pool_process(struct occ_show *occ_show,
4575 struct nlattr **tb)
4576 {
4577 struct occ_port *occ_port;
4578 struct occ_item *occ_item;
4579
4580 if (occ_show->err || !dl_dump_filter(occ_show->dl, tb))
4581 return;
4582
4583 occ_port = occ_port_get(occ_show, tb);
4584 if (!occ_port) {
4585 occ_show->err = -ENOMEM;
4586 return;
4587 }
4588
4589 occ_item = occ_item_alloc();
4590 if (!occ_item) {
4591 occ_show->err = -ENOMEM;
4592 return;
4593 }
4594 occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]);
4595 occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]);
4596 occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]);
4597 list_add_tail(&occ_item->list, &occ_port->pool_list);
4598 }
4599
4600 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr *nlh, void *data)
4601 {
4602 struct occ_show *occ_show = data;
4603 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4604 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4605
4606 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4607 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4608 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
4609 !tb[DEVLINK_ATTR_SB_POOL_INDEX] ||
4610 !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX])
4611 return MNL_CB_ERROR;
4612 cmd_sb_occ_port_pool_process(occ_show, tb);
4613 return MNL_CB_OK;
4614 }
4615
4616 static void cmd_sb_occ_tc_pool_process(struct occ_show *occ_show,
4617 struct nlattr **tb)
4618 {
4619 struct occ_port *occ_port;
4620 struct occ_item *occ_item;
4621 uint8_t pool_type;
4622
4623 if (occ_show->err || !dl_dump_filter(occ_show->dl, tb))
4624 return;
4625
4626 occ_port = occ_port_get(occ_show, tb);
4627 if (!occ_port) {
4628 occ_show->err = -ENOMEM;
4629 return;
4630 }
4631
4632 occ_item = occ_item_alloc();
4633 if (!occ_item) {
4634 occ_show->err = -ENOMEM;
4635 return;
4636 }
4637 occ_item->index = mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_TC_INDEX]);
4638 occ_item->cur = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_CUR]);
4639 occ_item->max = mnl_attr_get_u32(tb[DEVLINK_ATTR_SB_OCC_MAX]);
4640 occ_item->bound_pool_index =
4641 mnl_attr_get_u16(tb[DEVLINK_ATTR_SB_POOL_INDEX]);
4642 pool_type = mnl_attr_get_u8(tb[DEVLINK_ATTR_SB_POOL_TYPE]);
4643 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS)
4644 list_add_tail(&occ_item->list, &occ_port->ing_tc_list);
4645 else if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS)
4646 list_add_tail(&occ_item->list, &occ_port->eg_tc_list);
4647 else
4648 occ_item_free(occ_item);
4649 }
4650
4651 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr *nlh, void *data)
4652 {
4653 struct occ_show *occ_show = data;
4654 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4655 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4656
4657 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4658 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4659 !tb[DEVLINK_ATTR_PORT_INDEX] || !tb[DEVLINK_ATTR_SB_INDEX] ||
4660 !tb[DEVLINK_ATTR_SB_TC_INDEX] || !tb[DEVLINK_ATTR_SB_POOL_TYPE] ||
4661 !tb[DEVLINK_ATTR_SB_POOL_INDEX] ||
4662 !tb[DEVLINK_ATTR_SB_OCC_CUR] || !tb[DEVLINK_ATTR_SB_OCC_MAX])
4663 return MNL_CB_ERROR;
4664 cmd_sb_occ_tc_pool_process(occ_show, tb);
4665 return MNL_CB_OK;
4666 }
4667
4668 static int cmd_sb_occ_show(struct dl *dl)
4669 {
4670 struct nlmsghdr *nlh;
4671 struct occ_show *occ_show;
4672 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP;
4673 int err;
4674
4675 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP, DL_OPT_SB);
4676 if (err)
4677 return err;
4678
4679 occ_show = occ_show_alloc(dl);
4680 if (!occ_show)
4681 return -ENOMEM;
4682
4683 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_PORT_POOL_GET, flags);
4684
4685 err = _mnlg_socket_sndrcv(dl->nlg, nlh,
4686 cmd_sb_occ_port_pool_process_cb, occ_show);
4687 if (err)
4688 goto out;
4689
4690 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_TC_POOL_BIND_GET, flags);
4691
4692 err = _mnlg_socket_sndrcv(dl->nlg, nlh,
4693 cmd_sb_occ_tc_pool_process_cb, occ_show);
4694 if (err)
4695 goto out;
4696
4697 pr_out_section_start(dl, "occupancy");
4698 pr_out_occ_show(occ_show);
4699 pr_out_section_end(dl);
4700
4701 out:
4702 occ_show_free(occ_show);
4703 return err;
4704 }
4705
4706 static int cmd_sb_occ_snapshot(struct dl *dl)
4707 {
4708 struct nlmsghdr *nlh;
4709 int err;
4710
4711 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_SNAPSHOT,
4712 NLM_F_REQUEST | NLM_F_ACK);
4713
4714 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB);
4715 if (err)
4716 return err;
4717
4718 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4719 }
4720
4721 static int cmd_sb_occ_clearmax(struct dl *dl)
4722 {
4723 struct nlmsghdr *nlh;
4724 int err;
4725
4726 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_SB_OCC_MAX_CLEAR,
4727 NLM_F_REQUEST | NLM_F_ACK);
4728
4729 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, DL_OPT_SB);
4730 if (err)
4731 return err;
4732
4733 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
4734 }
4735
4736 static int cmd_sb_occ(struct dl *dl)
4737 {
4738 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
4739 cmd_sb_help();
4740 return 0;
4741 } else if (dl_argv_match(dl, "show") ||
4742 dl_argv_match(dl, "list")) {
4743 dl_arg_inc(dl);
4744 return cmd_sb_occ_show(dl);
4745 } else if (dl_argv_match(dl, "snapshot")) {
4746 dl_arg_inc(dl);
4747 return cmd_sb_occ_snapshot(dl);
4748 } else if (dl_argv_match(dl, "clearmax")) {
4749 dl_arg_inc(dl);
4750 return cmd_sb_occ_clearmax(dl);
4751 }
4752 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4753 return -ENOENT;
4754 }
4755
4756 static int cmd_sb(struct dl *dl)
4757 {
4758 if (dl_argv_match(dl, "help")) {
4759 cmd_sb_help();
4760 return 0;
4761 } else if (dl_argv_match(dl, "show") ||
4762 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
4763 dl_arg_inc(dl);
4764 return cmd_sb_show(dl);
4765 } else if (dl_argv_match(dl, "pool")) {
4766 dl_arg_inc(dl);
4767 return cmd_sb_pool(dl);
4768 } else if (dl_argv_match(dl, "port")) {
4769 dl_arg_inc(dl);
4770 return cmd_sb_port(dl);
4771 } else if (dl_argv_match(dl, "tc")) {
4772 dl_arg_inc(dl);
4773 return cmd_sb_tc(dl);
4774 } else if (dl_argv_match(dl, "occupancy")) {
4775 dl_arg_inc(dl);
4776 return cmd_sb_occ(dl);
4777 }
4778 pr_err("Command \"%s\" not found\n", dl_argv(dl));
4779 return -ENOENT;
4780 }
4781
4782 static const char *cmd_name(uint8_t cmd)
4783 {
4784 switch (cmd) {
4785 case DEVLINK_CMD_UNSPEC: return "unspec";
4786 case DEVLINK_CMD_GET: return "get";
4787 case DEVLINK_CMD_SET: return "set";
4788 case DEVLINK_CMD_NEW: return "new";
4789 case DEVLINK_CMD_DEL: return "del";
4790 case DEVLINK_CMD_PORT_GET: return "get";
4791 case DEVLINK_CMD_PORT_SET: return "set";
4792 case DEVLINK_CMD_PORT_NEW: return "new";
4793 case DEVLINK_CMD_PORT_DEL: return "del";
4794 case DEVLINK_CMD_PARAM_GET: return "get";
4795 case DEVLINK_CMD_PARAM_SET: return "set";
4796 case DEVLINK_CMD_PARAM_NEW: return "new";
4797 case DEVLINK_CMD_PARAM_DEL: return "del";
4798 case DEVLINK_CMD_REGION_GET: return "get";
4799 case DEVLINK_CMD_REGION_SET: return "set";
4800 case DEVLINK_CMD_REGION_NEW: return "new";
4801 case DEVLINK_CMD_REGION_DEL: return "del";
4802 case DEVLINK_CMD_FLASH_UPDATE: return "begin";
4803 case DEVLINK_CMD_FLASH_UPDATE_END: return "end";
4804 case DEVLINK_CMD_FLASH_UPDATE_STATUS: return "status";
4805 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER: return "status";
4806 case DEVLINK_CMD_TRAP_GET: return "get";
4807 case DEVLINK_CMD_TRAP_SET: return "set";
4808 case DEVLINK_CMD_TRAP_NEW: return "new";
4809 case DEVLINK_CMD_TRAP_DEL: return "del";
4810 case DEVLINK_CMD_TRAP_GROUP_GET: return "get";
4811 case DEVLINK_CMD_TRAP_GROUP_SET: return "set";
4812 case DEVLINK_CMD_TRAP_GROUP_NEW: return "new";
4813 case DEVLINK_CMD_TRAP_GROUP_DEL: return "del";
4814 case DEVLINK_CMD_TRAP_POLICER_GET: return "get";
4815 case DEVLINK_CMD_TRAP_POLICER_SET: return "set";
4816 case DEVLINK_CMD_TRAP_POLICER_NEW: return "new";
4817 case DEVLINK_CMD_TRAP_POLICER_DEL: return "del";
4818 default: return "<unknown cmd>";
4819 }
4820 }
4821
4822 static const char *cmd_obj(uint8_t cmd)
4823 {
4824 switch (cmd) {
4825 case DEVLINK_CMD_UNSPEC: return "unspec";
4826 case DEVLINK_CMD_GET:
4827 case DEVLINK_CMD_SET:
4828 case DEVLINK_CMD_NEW:
4829 case DEVLINK_CMD_DEL:
4830 return "dev";
4831 case DEVLINK_CMD_PORT_GET:
4832 case DEVLINK_CMD_PORT_SET:
4833 case DEVLINK_CMD_PORT_NEW:
4834 case DEVLINK_CMD_PORT_DEL:
4835 return "port";
4836 case DEVLINK_CMD_PARAM_GET:
4837 case DEVLINK_CMD_PARAM_SET:
4838 case DEVLINK_CMD_PARAM_NEW:
4839 case DEVLINK_CMD_PARAM_DEL:
4840 return "param";
4841 case DEVLINK_CMD_REGION_GET:
4842 case DEVLINK_CMD_REGION_SET:
4843 case DEVLINK_CMD_REGION_NEW:
4844 case DEVLINK_CMD_REGION_DEL:
4845 return "region";
4846 case DEVLINK_CMD_FLASH_UPDATE:
4847 case DEVLINK_CMD_FLASH_UPDATE_END:
4848 case DEVLINK_CMD_FLASH_UPDATE_STATUS:
4849 return "flash";
4850 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER:
4851 return "health";
4852 case DEVLINK_CMD_TRAP_GET:
4853 case DEVLINK_CMD_TRAP_SET:
4854 case DEVLINK_CMD_TRAP_NEW:
4855 case DEVLINK_CMD_TRAP_DEL:
4856 return "trap";
4857 case DEVLINK_CMD_TRAP_GROUP_GET:
4858 case DEVLINK_CMD_TRAP_GROUP_SET:
4859 case DEVLINK_CMD_TRAP_GROUP_NEW:
4860 case DEVLINK_CMD_TRAP_GROUP_DEL:
4861 return "trap-group";
4862 case DEVLINK_CMD_TRAP_POLICER_GET:
4863 case DEVLINK_CMD_TRAP_POLICER_SET:
4864 case DEVLINK_CMD_TRAP_POLICER_NEW:
4865 case DEVLINK_CMD_TRAP_POLICER_DEL:
4866 return "trap-policer";
4867 default: return "<unknown obj>";
4868 }
4869 }
4870
4871 static void pr_out_mon_header(uint8_t cmd)
4872 {
4873 if (!is_json_context()) {
4874 pr_out("[%s,%s] ", cmd_obj(cmd), cmd_name(cmd));
4875 } else {
4876 open_json_object(NULL);
4877 print_string(PRINT_JSON, "command", NULL, cmd_name(cmd));
4878 open_json_object(cmd_obj(cmd));
4879 }
4880 }
4881
4882 static void pr_out_mon_footer(void)
4883 {
4884 if (is_json_context()) {
4885 close_json_object();
4886 close_json_object();
4887 }
4888 }
4889
4890 static bool cmd_filter_check(struct dl *dl, uint8_t cmd)
4891 {
4892 const char *obj = cmd_obj(cmd);
4893 unsigned int index = 0;
4894 const char *cur_obj;
4895
4896 if (dl_no_arg(dl))
4897 return true;
4898 while ((cur_obj = dl_argv_index(dl, index++))) {
4899 if (strcmp(cur_obj, obj) == 0 || strcmp(cur_obj, "all") == 0)
4900 return true;
4901 }
4902 return false;
4903 }
4904
4905 static void pr_out_flash_update(struct dl *dl, struct nlattr **tb)
4906 {
4907 __pr_out_handle_start(dl, tb, true, false);
4908
4909 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]) {
4910 check_indent_newline(dl);
4911 print_string(PRINT_ANY, "msg", "msg %s",
4912 mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG]));
4913 }
4914 if (tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]) {
4915 check_indent_newline(dl);
4916 print_string(PRINT_ANY, "component", "component %s",
4917 mnl_attr_get_str(tb[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT]));
4918 }
4919
4920 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE])
4921 pr_out_u64(dl, "done",
4922 mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE]));
4923
4924 if (tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL])
4925 pr_out_u64(dl, "total",
4926 mnl_attr_get_u64(tb[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL]));
4927
4928 pr_out_handle_end(dl);
4929 }
4930
4931 static void pr_out_region(struct dl *dl, struct nlattr **tb);
4932 static void pr_out_health(struct dl *dl, struct nlattr **tb_health,
4933 bool show_device, bool show_port);
4934 static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array);
4935 static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array);
4936 static void pr_out_trap_policer(struct dl *dl, struct nlattr **tb, bool array);
4937
4938 static int cmd_mon_show_cb(const struct nlmsghdr *nlh, void *data)
4939 {
4940 struct dl *dl = data;
4941 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
4942 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
4943 uint8_t cmd = genl->cmd;
4944
4945 if (!cmd_filter_check(dl, cmd))
4946 return MNL_CB_OK;
4947
4948 switch (cmd) {
4949 case DEVLINK_CMD_GET: /* fall through */
4950 case DEVLINK_CMD_SET: /* fall through */
4951 case DEVLINK_CMD_NEW: /* fall through */
4952 case DEVLINK_CMD_DEL:
4953 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4954 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
4955 return MNL_CB_ERROR;
4956 pr_out_mon_header(genl->cmd);
4957 dl->stats = true;
4958 pr_out_dev(dl, tb);
4959 pr_out_mon_footer();
4960 break;
4961 case DEVLINK_CMD_PORT_GET: /* fall through */
4962 case DEVLINK_CMD_PORT_SET: /* fall through */
4963 case DEVLINK_CMD_PORT_NEW: /* fall through */
4964 case DEVLINK_CMD_PORT_DEL:
4965 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4966 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4967 !tb[DEVLINK_ATTR_PORT_INDEX])
4968 return MNL_CB_ERROR;
4969 pr_out_mon_header(genl->cmd);
4970 pr_out_port(dl, tb);
4971 pr_out_mon_footer();
4972 break;
4973 case DEVLINK_CMD_PARAM_GET: /* fall through */
4974 case DEVLINK_CMD_PARAM_SET: /* fall through */
4975 case DEVLINK_CMD_PARAM_NEW: /* fall through */
4976 case DEVLINK_CMD_PARAM_DEL:
4977 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4978 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4979 !tb[DEVLINK_ATTR_PARAM])
4980 return MNL_CB_ERROR;
4981 pr_out_mon_header(genl->cmd);
4982 pr_out_param(dl, tb, false);
4983 pr_out_mon_footer();
4984 break;
4985 case DEVLINK_CMD_REGION_GET: /* fall through */
4986 case DEVLINK_CMD_REGION_SET: /* fall through */
4987 case DEVLINK_CMD_REGION_NEW: /* fall through */
4988 case DEVLINK_CMD_REGION_DEL:
4989 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
4990 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
4991 !tb[DEVLINK_ATTR_REGION_NAME])
4992 return MNL_CB_ERROR;
4993 pr_out_mon_header(genl->cmd);
4994 pr_out_region(dl, tb);
4995 pr_out_mon_footer();
4996 break;
4997 case DEVLINK_CMD_FLASH_UPDATE: /* fall through */
4998 case DEVLINK_CMD_FLASH_UPDATE_END: /* fall through */
4999 case DEVLINK_CMD_FLASH_UPDATE_STATUS:
5000 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5001 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME])
5002 return MNL_CB_ERROR;
5003 pr_out_mon_header(genl->cmd);
5004 pr_out_flash_update(dl, tb);
5005 pr_out_mon_footer();
5006 break;
5007 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER:
5008 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5009 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5010 !tb[DEVLINK_ATTR_HEALTH_REPORTER])
5011 return MNL_CB_ERROR;
5012 pr_out_mon_header(genl->cmd);
5013 pr_out_health(dl, tb, true, true);
5014 pr_out_mon_footer();
5015 break;
5016 case DEVLINK_CMD_TRAP_GET: /* fall through */
5017 case DEVLINK_CMD_TRAP_SET: /* fall through */
5018 case DEVLINK_CMD_TRAP_NEW: /* fall through */
5019 case DEVLINK_CMD_TRAP_DEL:
5020 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5021 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5022 !tb[DEVLINK_ATTR_TRAP_NAME] ||
5023 !tb[DEVLINK_ATTR_TRAP_TYPE] ||
5024 !tb[DEVLINK_ATTR_TRAP_ACTION] ||
5025 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] ||
5026 !tb[DEVLINK_ATTR_TRAP_METADATA] ||
5027 !tb[DEVLINK_ATTR_STATS])
5028 return MNL_CB_ERROR;
5029 pr_out_mon_header(genl->cmd);
5030 pr_out_trap(dl, tb, false);
5031 pr_out_mon_footer();
5032 break;
5033 case DEVLINK_CMD_TRAP_GROUP_GET: /* fall through */
5034 case DEVLINK_CMD_TRAP_GROUP_SET: /* fall through */
5035 case DEVLINK_CMD_TRAP_GROUP_NEW: /* fall through */
5036 case DEVLINK_CMD_TRAP_GROUP_DEL:
5037 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5038 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5039 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] ||
5040 !tb[DEVLINK_ATTR_STATS])
5041 return MNL_CB_ERROR;
5042 pr_out_mon_header(genl->cmd);
5043 pr_out_trap_group(dl, tb, false);
5044 pr_out_mon_footer();
5045 break;
5046 case DEVLINK_CMD_TRAP_POLICER_GET: /* fall through */
5047 case DEVLINK_CMD_TRAP_POLICER_SET: /* fall through */
5048 case DEVLINK_CMD_TRAP_POLICER_NEW: /* fall through */
5049 case DEVLINK_CMD_TRAP_POLICER_DEL: /* fall through */
5050 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5051 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5052 !tb[DEVLINK_ATTR_TRAP_POLICER_ID] ||
5053 !tb[DEVLINK_ATTR_TRAP_POLICER_RATE] ||
5054 !tb[DEVLINK_ATTR_TRAP_POLICER_BURST])
5055 return MNL_CB_ERROR;
5056 pr_out_mon_header(genl->cmd);
5057 pr_out_trap_policer(dl, tb, false);
5058 break;
5059 }
5060 fflush(stdout);
5061 return MNL_CB_OK;
5062 }
5063
5064 static int cmd_mon_show(struct dl *dl)
5065 {
5066 int err;
5067 unsigned int index = 0;
5068 const char *cur_obj;
5069
5070 while ((cur_obj = dl_argv_index(dl, index++))) {
5071 if (strcmp(cur_obj, "all") != 0 &&
5072 strcmp(cur_obj, "dev") != 0 &&
5073 strcmp(cur_obj, "port") != 0 &&
5074 strcmp(cur_obj, "health") != 0 &&
5075 strcmp(cur_obj, "trap") != 0 &&
5076 strcmp(cur_obj, "trap-group") != 0 &&
5077 strcmp(cur_obj, "trap-policer") != 0) {
5078 pr_err("Unknown object \"%s\"\n", cur_obj);
5079 return -EINVAL;
5080 }
5081 }
5082 err = _mnlg_socket_group_add(dl->nlg, DEVLINK_GENL_MCGRP_CONFIG_NAME);
5083 if (err)
5084 return err;
5085 open_json_object(NULL);
5086 open_json_array(PRINT_JSON, "mon");
5087 err = _mnlg_socket_recv_run_intr(dl->nlg, cmd_mon_show_cb, dl);
5088 close_json_array(PRINT_JSON, NULL);
5089 close_json_object();
5090 if (err)
5091 return err;
5092 return 0;
5093 }
5094
5095 static void cmd_mon_help(void)
5096 {
5097 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
5098 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
5099 }
5100
5101 static int cmd_mon(struct dl *dl)
5102 {
5103 if (dl_argv_match(dl, "help")) {
5104 cmd_mon_help();
5105 return 0;
5106 }
5107 return cmd_mon_show(dl);
5108 }
5109
5110 struct dpipe_field {
5111 char *name;
5112 unsigned int id;
5113 unsigned int bitwidth;
5114 enum devlink_dpipe_field_mapping_type mapping_type;
5115 };
5116
5117 struct dpipe_header {
5118 struct list_head list;
5119 char *name;
5120 unsigned int id;
5121 struct dpipe_field *fields;
5122 unsigned int fields_count;
5123 };
5124
5125 struct dpipe_table {
5126 struct list_head list;
5127 char *name;
5128 unsigned int resource_id;
5129 bool resource_valid;
5130 };
5131
5132 struct dpipe_tables {
5133 struct list_head table_list;
5134 };
5135
5136 struct resource {
5137 char *name;
5138 uint64_t size;
5139 uint64_t size_new;
5140 uint64_t size_min;
5141 uint64_t size_max;
5142 uint64_t size_gran;
5143 enum devlink_resource_unit unit;
5144 bool size_valid;
5145 uint64_t size_occ;
5146 bool occ_valid;
5147 uint64_t id;
5148 struct list_head list;
5149 struct list_head resource_list;
5150 struct resource *parent;
5151 };
5152
5153 struct resources {
5154 struct list_head resource_list;
5155 };
5156
5157 struct resource_ctx {
5158 struct dl *dl;
5159 int err;
5160 struct resources *resources;
5161 struct dpipe_tables *tables;
5162 bool print_resources;
5163 bool pending_change;
5164 };
5165
5166 static struct resource *resource_alloc(void)
5167 {
5168 struct resource *resource;
5169
5170 resource = calloc(1, sizeof(struct resource));
5171 if (!resource)
5172 return NULL;
5173 INIT_LIST_HEAD(&resource->resource_list);
5174 return resource;
5175 }
5176
5177 static void resource_free(struct resource *resource)
5178 {
5179 struct resource *child_resource, *tmp;
5180
5181 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
5182 list) {
5183 free(child_resource->name);
5184 resource_free(child_resource);
5185 }
5186 free(resource);
5187 }
5188
5189 static struct resources *resources_alloc(void)
5190 {
5191 struct resources *resources;
5192
5193 resources = calloc(1, sizeof(struct resources));
5194 if (!resources)
5195 return NULL;
5196 INIT_LIST_HEAD(&resources->resource_list);
5197 return resources;
5198 }
5199
5200 static void resources_free(struct resources *resources)
5201 {
5202 struct resource *resource, *tmp;
5203
5204 list_for_each_entry_safe(resource, tmp, &resources->resource_list, list)
5205 resource_free(resource);
5206 }
5207
5208 static int resource_ctx_init(struct resource_ctx *ctx, struct dl *dl)
5209 {
5210 ctx->resources = resources_alloc();
5211 if (!ctx->resources)
5212 return -ENOMEM;
5213 ctx->dl = dl;
5214 return 0;
5215 }
5216
5217 static void resource_ctx_fini(struct resource_ctx *ctx)
5218 {
5219 resources_free(ctx->resources);
5220 }
5221
5222 struct dpipe_ctx {
5223 struct dl *dl;
5224 int err;
5225 struct list_head global_headers;
5226 struct list_head local_headers;
5227 struct dpipe_tables *tables;
5228 struct resources *resources;
5229 bool print_headers;
5230 bool print_tables;
5231 };
5232
5233 static struct dpipe_header *dpipe_header_alloc(unsigned int fields_count)
5234 {
5235 struct dpipe_header *header;
5236
5237 header = calloc(1, sizeof(struct dpipe_header));
5238 if (!header)
5239 return NULL;
5240 header->fields = calloc(fields_count, sizeof(struct dpipe_field));
5241 if (!header->fields)
5242 goto err_fields_alloc;
5243 header->fields_count = fields_count;
5244 return header;
5245
5246 err_fields_alloc:
5247 free(header);
5248 return NULL;
5249 }
5250
5251 static void dpipe_header_free(struct dpipe_header *header)
5252 {
5253 free(header->fields);
5254 free(header);
5255 }
5256
5257 static void dpipe_header_clear(struct dpipe_header *header)
5258 {
5259 struct dpipe_field *field;
5260 int i;
5261
5262 for (i = 0; i < header->fields_count; i++) {
5263 field = &header->fields[i];
5264 free(field->name);
5265 }
5266 free(header->name);
5267 }
5268
5269 static void dpipe_header_add(struct dpipe_ctx *ctx,
5270 struct dpipe_header *header, bool global)
5271 {
5272 if (global)
5273 list_add(&header->list, &ctx->global_headers);
5274 else
5275 list_add(&header->list, &ctx->local_headers);
5276 }
5277
5278 static void dpipe_header_del(struct dpipe_header *header)
5279 {
5280 list_del(&header->list);
5281 }
5282
5283 static struct dpipe_table *dpipe_table_alloc(void)
5284 {
5285 return calloc(1, sizeof(struct dpipe_table));
5286 }
5287
5288 static void dpipe_table_free(struct dpipe_table *table)
5289 {
5290 free(table);
5291 }
5292
5293 static struct dpipe_tables *dpipe_tables_alloc(void)
5294 {
5295 struct dpipe_tables *tables;
5296
5297 tables = calloc(1, sizeof(struct dpipe_tables));
5298 if (!tables)
5299 return NULL;
5300 INIT_LIST_HEAD(&tables->table_list);
5301 return tables;
5302 }
5303
5304 static void dpipe_tables_free(struct dpipe_tables *tables)
5305 {
5306 struct dpipe_table *table, *tmp;
5307
5308 list_for_each_entry_safe(table, tmp, &tables->table_list, list)
5309 dpipe_table_free(table);
5310 free(tables);
5311 }
5312
5313 static int dpipe_ctx_init(struct dpipe_ctx *ctx, struct dl *dl)
5314 {
5315 ctx->tables = dpipe_tables_alloc();
5316 if (!ctx->tables)
5317 return -ENOMEM;
5318
5319 ctx->dl = dl;
5320 INIT_LIST_HEAD(&ctx->global_headers);
5321 INIT_LIST_HEAD(&ctx->local_headers);
5322 return 0;
5323 }
5324
5325 static void dpipe_ctx_fini(struct dpipe_ctx *ctx)
5326 {
5327 struct dpipe_header *header, *tmp;
5328
5329 list_for_each_entry_safe(header, tmp, &ctx->global_headers,
5330 list) {
5331 dpipe_header_del(header);
5332 dpipe_header_clear(header);
5333 dpipe_header_free(header);
5334 }
5335 list_for_each_entry_safe(header, tmp, &ctx->local_headers,
5336 list) {
5337 dpipe_header_del(header);
5338 dpipe_header_clear(header);
5339 dpipe_header_free(header);
5340 }
5341 dpipe_tables_free(ctx->tables);
5342 }
5343
5344 static const char *dpipe_header_id2s(struct dpipe_ctx *ctx,
5345 uint32_t header_id, bool global)
5346 {
5347 struct list_head *header_list;
5348 struct dpipe_header *header;
5349
5350 if (global)
5351 header_list = &ctx->global_headers;
5352 else
5353 header_list = &ctx->local_headers;
5354 list_for_each_entry(header, header_list, list) {
5355 if (header->id != header_id)
5356 continue;
5357 return header->name;
5358 }
5359 return NULL;
5360 }
5361
5362 static const char *dpipe_field_id2s(struct dpipe_ctx *ctx,
5363 uint32_t header_id,
5364 uint32_t field_id, bool global)
5365 {
5366 struct list_head *header_list;
5367 struct dpipe_header *header;
5368
5369 if (global)
5370 header_list = &ctx->global_headers;
5371 else
5372 header_list = &ctx->local_headers;
5373 list_for_each_entry(header, header_list, list) {
5374 if (header->id != header_id)
5375 continue;
5376 return header->fields[field_id].name;
5377 }
5378 return NULL;
5379 }
5380
5381 static const char *
5382 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type)
5383 {
5384 switch (mapping_type) {
5385 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE:
5386 return NULL;
5387 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX:
5388 return "ifindex";
5389 default:
5390 return "<unknown>";
5391 }
5392 }
5393
5394 static const char *
5395 dpipe_mapping_get(struct dpipe_ctx *ctx, uint32_t header_id,
5396 uint32_t field_id, bool global)
5397 {
5398 enum devlink_dpipe_field_mapping_type mapping_type;
5399 struct list_head *header_list;
5400 struct dpipe_header *header;
5401
5402 if (global)
5403 header_list = &ctx->global_headers;
5404 else
5405 header_list = &ctx->local_headers;
5406 list_for_each_entry(header, header_list, list) {
5407 if (header->id != header_id)
5408 continue;
5409 mapping_type = header->fields[field_id].mapping_type;
5410 return dpipe_field_mapping_e2s(mapping_type);
5411 }
5412 return NULL;
5413 }
5414
5415 static void pr_out_dpipe_fields(struct dpipe_ctx *ctx,
5416 struct dpipe_field *fields,
5417 unsigned int field_count)
5418 {
5419 struct dpipe_field *field;
5420 int i;
5421
5422 for (i = 0; i < field_count; i++) {
5423 field = &fields[i];
5424 pr_out_entry_start(ctx->dl);
5425 check_indent_newline(ctx->dl);
5426 print_string(PRINT_ANY, "name", "name %s", field->name);
5427 if (ctx->dl->verbose)
5428 print_uint(PRINT_ANY, "id", " id %u", field->id);
5429 print_uint(PRINT_ANY, "bitwidth", " bitwidth %u", field->bitwidth);
5430 if (field->mapping_type) {
5431 print_string(PRINT_ANY, "mapping_type", " mapping_type %s",
5432 dpipe_field_mapping_e2s(field->mapping_type));
5433 }
5434 pr_out_entry_end(ctx->dl);
5435 }
5436 }
5437
5438 static void
5439 pr_out_dpipe_header(struct dpipe_ctx *ctx, struct nlattr **tb,
5440 struct dpipe_header *header, bool global)
5441 {
5442 pr_out_handle_start_arr(ctx->dl, tb);
5443 check_indent_newline(ctx->dl);
5444 print_string(PRINT_ANY, "name", "name %s", header->name);
5445 if (ctx->dl->verbose) {
5446 print_uint(PRINT_ANY, "id", " id %u", header->id);
5447 print_bool(PRINT_ANY, "global", " global %s", global);
5448 }
5449 pr_out_array_start(ctx->dl, "field");
5450 pr_out_dpipe_fields(ctx, header->fields,
5451 header->fields_count);
5452 pr_out_array_end(ctx->dl);
5453 pr_out_handle_end(ctx->dl);
5454 }
5455
5456 static void pr_out_dpipe_headers(struct dpipe_ctx *ctx,
5457 struct nlattr **tb)
5458 {
5459 struct dpipe_header *header;
5460
5461 list_for_each_entry(header, &ctx->local_headers, list)
5462 pr_out_dpipe_header(ctx, tb, header, false);
5463
5464 list_for_each_entry(header, &ctx->global_headers, list)
5465 pr_out_dpipe_header(ctx, tb, header, true);
5466 }
5467
5468 static int dpipe_header_field_get(struct nlattr *nl, struct dpipe_field *field)
5469 {
5470 struct nlattr *nla_field[DEVLINK_ATTR_MAX + 1] = {};
5471 const char *name;
5472 int err;
5473
5474 err = mnl_attr_parse_nested(nl, attr_cb, nla_field);
5475 if (err != MNL_CB_OK)
5476 return -EINVAL;
5477 if (!nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID] ||
5478 !nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME] ||
5479 !nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH] ||
5480 !nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE])
5481 return -EINVAL;
5482
5483 name = mnl_attr_get_str(nla_field[DEVLINK_ATTR_DPIPE_FIELD_NAME]);
5484 field->id = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_ID]);
5485 field->bitwidth = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH]);
5486 field->name = strdup(name);
5487 if (!field->name)
5488 return -ENOMEM;
5489 field->mapping_type = mnl_attr_get_u32(nla_field[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE]);
5490 return 0;
5491 }
5492
5493 static int dpipe_header_fields_get(struct nlattr *nla_fields,
5494 struct dpipe_field *fields)
5495 {
5496 struct nlattr *nla_field;
5497 int count = 0;
5498 int err;
5499
5500 mnl_attr_for_each_nested(nla_field, nla_fields) {
5501 err = dpipe_header_field_get(nla_field, &fields[count]);
5502 if (err)
5503 return err;
5504 count++;
5505 }
5506 return 0;
5507 }
5508
5509 static unsigned int dpipe_header_field_count_get(struct nlattr *nla_fields)
5510 {
5511 struct nlattr *nla_field;
5512 unsigned int count = 0;
5513
5514 mnl_attr_for_each_nested(nla_field, nla_fields)
5515 count++;
5516 return count;
5517 }
5518
5519 static int dpipe_header_get(struct dpipe_ctx *ctx, struct nlattr *nl)
5520 {
5521 struct nlattr *nla_header[DEVLINK_ATTR_MAX + 1] = {};
5522 struct dpipe_header *header;
5523 unsigned int fields_count;
5524 const char *header_name;
5525 bool global;
5526 int err;
5527
5528 err = mnl_attr_parse_nested(nl, attr_cb, nla_header);
5529 if (err != MNL_CB_OK)
5530 return -EINVAL;
5531
5532 if (!nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME] ||
5533 !nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
5534 !nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS])
5535 return -EINVAL;
5536
5537 fields_count = dpipe_header_field_count_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS]);
5538 header = dpipe_header_alloc(fields_count);
5539 if (!header)
5540 return -ENOMEM;
5541
5542 header_name = mnl_attr_get_str(nla_header[DEVLINK_ATTR_DPIPE_HEADER_NAME]);
5543 header->name = strdup(header_name);
5544 header->id = mnl_attr_get_u32(nla_header[DEVLINK_ATTR_DPIPE_HEADER_ID]);
5545 header->fields_count = fields_count;
5546 global = !!mnl_attr_get_u8(nla_header[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
5547
5548 err = dpipe_header_fields_get(nla_header[DEVLINK_ATTR_DPIPE_HEADER_FIELDS],
5549 header->fields);
5550 if (err)
5551 goto err_field_get;
5552 dpipe_header_add(ctx, header, global);
5553 return 0;
5554
5555 err_field_get:
5556 dpipe_header_free(header);
5557 return err;
5558 }
5559
5560 static int dpipe_headers_get(struct dpipe_ctx *ctx, struct nlattr **tb)
5561 {
5562 struct nlattr *nla_headers = tb[DEVLINK_ATTR_DPIPE_HEADERS];
5563 struct nlattr *nla_header;
5564 int err;
5565
5566 mnl_attr_for_each_nested(nla_header, nla_headers) {
5567 err = dpipe_header_get(ctx, nla_header);
5568 if (err)
5569 return err;
5570 }
5571 return 0;
5572 }
5573
5574 static int cmd_dpipe_header_cb(const struct nlmsghdr *nlh, void *data)
5575 {
5576 struct dpipe_ctx *ctx = data;
5577 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
5578 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
5579 int err;
5580
5581 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5582 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5583 !tb[DEVLINK_ATTR_DPIPE_HEADERS])
5584 return MNL_CB_ERROR;
5585 err = dpipe_headers_get(ctx, tb);
5586 if (err) {
5587 ctx->err = err;
5588 return MNL_CB_ERROR;
5589 }
5590
5591 if (ctx->print_headers)
5592 pr_out_dpipe_headers(ctx, tb);
5593 return MNL_CB_OK;
5594 }
5595
5596 static int cmd_dpipe_headers_show(struct dl *dl)
5597 {
5598 struct nlmsghdr *nlh;
5599 struct dpipe_ctx ctx = {};
5600 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
5601 int err;
5602
5603 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
5604
5605 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE, 0);
5606 if (err)
5607 return err;
5608
5609 err = dpipe_ctx_init(&ctx, dl);
5610 if (err)
5611 return err;
5612
5613 ctx.print_headers = true;
5614
5615 pr_out_section_start(dl, "header");
5616 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, &ctx);
5617 if (err)
5618 pr_err("error get headers %s\n", strerror(ctx.err));
5619 pr_out_section_end(dl);
5620
5621 dpipe_ctx_fini(&ctx);
5622 return err;
5623 }
5624
5625 static void cmd_dpipe_help(void)
5626 {
5627 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5628 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5629 pr_err(" [ counters_enabled { true | false } ]\n");
5630 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5631 pr_err(" devlink dpipe header show DEV\n");
5632 }
5633
5634 static int cmd_dpipe_header(struct dl *dl)
5635 {
5636 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
5637 cmd_dpipe_help();
5638 return 0;
5639 } else if (dl_argv_match(dl, "show")) {
5640 dl_arg_inc(dl);
5641 return cmd_dpipe_headers_show(dl);
5642 }
5643 pr_err("Command \"%s\" not found\n", dl_argv(dl));
5644 return -ENOENT;
5645 }
5646
5647 static const char
5648 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type)
5649 {
5650 switch (action_type) {
5651 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY:
5652 return "field_modify";
5653 default:
5654 return "<unknown>";
5655 }
5656 }
5657
5658 struct dpipe_op_info {
5659 uint32_t header_id;
5660 uint32_t field_id;
5661 bool header_global;
5662 };
5663
5664 struct dpipe_action {
5665 struct dpipe_op_info info;
5666 uint32_t type;
5667 };
5668
5669 static void pr_out_dpipe_action(struct dpipe_action *action,
5670 struct dpipe_ctx *ctx)
5671 {
5672 struct dpipe_op_info *op_info = &action->info;
5673 const char *mapping;
5674
5675 check_indent_newline(ctx->dl);
5676 print_string(PRINT_ANY, "type", "type %s",
5677 dpipe_action_type_e2s(action->type));
5678 print_string(PRINT_ANY, "header", " header %s",
5679 dpipe_header_id2s(ctx, op_info->header_id,
5680 op_info->header_global));
5681 print_string(PRINT_ANY, "field", " field %s",
5682 dpipe_field_id2s(ctx, op_info->header_id,
5683 op_info->field_id,
5684 op_info->header_global));
5685 mapping = dpipe_mapping_get(ctx, op_info->header_id,
5686 op_info->field_id,
5687 op_info->header_global);
5688 if (mapping)
5689 print_string(PRINT_ANY, "mapping", " mapping %s", mapping);
5690 }
5691
5692 static int dpipe_action_parse(struct dpipe_action *action, struct nlattr *nl)
5693 {
5694 struct nlattr *nla_action[DEVLINK_ATTR_MAX + 1] = {};
5695 int err;
5696
5697 err = mnl_attr_parse_nested(nl, attr_cb, nla_action);
5698 if (err != MNL_CB_OK)
5699 return -EINVAL;
5700
5701 if (!nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE] ||
5702 !nla_action[DEVLINK_ATTR_DPIPE_HEADER_INDEX] ||
5703 !nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
5704 !nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]) {
5705 return -EINVAL;
5706 }
5707
5708 action->type = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_ACTION_TYPE]);
5709 action->info.header_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_HEADER_ID]);
5710 action->info.field_id = mnl_attr_get_u32(nla_action[DEVLINK_ATTR_DPIPE_FIELD_ID]);
5711 action->info.header_global = !!mnl_attr_get_u8(nla_action[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
5712
5713 return 0;
5714 }
5715
5716 static int dpipe_table_actions_show(struct dpipe_ctx *ctx,
5717 struct nlattr *nla_actions)
5718 {
5719 struct nlattr *nla_action;
5720 struct dpipe_action action;
5721
5722 mnl_attr_for_each_nested(nla_action, nla_actions) {
5723 pr_out_entry_start(ctx->dl);
5724 if (dpipe_action_parse(&action, nla_action))
5725 goto err_action_parse;
5726 pr_out_dpipe_action(&action, ctx);
5727 pr_out_entry_end(ctx->dl);
5728 }
5729 return 0;
5730
5731 err_action_parse:
5732 pr_out_entry_end(ctx->dl);
5733 return -EINVAL;
5734 }
5735
5736 static const char *
5737 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type)
5738 {
5739 switch (match_type) {
5740 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT:
5741 return "field_exact";
5742 default:
5743 return "<unknown>";
5744 }
5745 }
5746
5747 struct dpipe_match {
5748 struct dpipe_op_info info;
5749 uint32_t type;
5750 };
5751
5752 static void pr_out_dpipe_match(struct dpipe_match *match,
5753 struct dpipe_ctx *ctx)
5754 {
5755 struct dpipe_op_info *op_info = &match->info;
5756 const char *mapping;
5757
5758 check_indent_newline(ctx->dl);
5759 print_string(PRINT_ANY, "type", "type %s",
5760 dpipe_match_type_e2s(match->type));
5761 print_string(PRINT_ANY, "header", " header %s",
5762 dpipe_header_id2s(ctx, op_info->header_id,
5763 op_info->header_global));
5764 print_string(PRINT_ANY, "field", " field %s",
5765 dpipe_field_id2s(ctx, op_info->header_id,
5766 op_info->field_id,
5767 op_info->header_global));
5768 mapping = dpipe_mapping_get(ctx, op_info->header_id,
5769 op_info->field_id,
5770 op_info->header_global);
5771 if (mapping)
5772 print_string(PRINT_ANY, "mapping", " mapping %s", mapping);
5773 }
5774
5775 static int dpipe_match_parse(struct dpipe_match *match,
5776 struct nlattr *nl)
5777
5778 {
5779 struct nlattr *nla_match[DEVLINK_ATTR_MAX + 1] = {};
5780 int err;
5781
5782 err = mnl_attr_parse_nested(nl, attr_cb, nla_match);
5783 if (err != MNL_CB_OK)
5784 return -EINVAL;
5785
5786 if (!nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE] ||
5787 !nla_match[DEVLINK_ATTR_DPIPE_HEADER_INDEX] ||
5788 !nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID] ||
5789 !nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]) {
5790 return -EINVAL;
5791 }
5792
5793 match->type = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_MATCH_TYPE]);
5794 match->info.header_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_HEADER_ID]);
5795 match->info.field_id = mnl_attr_get_u32(nla_match[DEVLINK_ATTR_DPIPE_FIELD_ID]);
5796 match->info.header_global = !!mnl_attr_get_u8(nla_match[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL]);
5797
5798 return 0;
5799 }
5800
5801 static int dpipe_table_matches_show(struct dpipe_ctx *ctx,
5802 struct nlattr *nla_matches)
5803 {
5804 struct nlattr *nla_match;
5805 struct dpipe_match match;
5806
5807 mnl_attr_for_each_nested(nla_match, nla_matches) {
5808 pr_out_entry_start(ctx->dl);
5809 if (dpipe_match_parse(&match, nla_match))
5810 goto err_match_parse;
5811 pr_out_dpipe_match(&match, ctx);
5812 pr_out_entry_end(ctx->dl);
5813 }
5814 return 0;
5815
5816 err_match_parse:
5817 pr_out_entry_end(ctx->dl);
5818 return -EINVAL;
5819 }
5820
5821 static struct resource *
5822 resource_find(struct resources *resources, struct resource *resource,
5823 uint64_t resource_id)
5824 {
5825 struct list_head *list_head;
5826
5827 if (!resource)
5828 list_head = &resources->resource_list;
5829 else
5830 list_head = &resource->resource_list;
5831
5832 list_for_each_entry(resource, list_head, list) {
5833 struct resource *child_resource;
5834
5835 if (resource->id == resource_id)
5836 return resource;
5837
5838 child_resource = resource_find(resources, resource,
5839 resource_id);
5840 if (child_resource)
5841 return child_resource;
5842 }
5843 return NULL;
5844 }
5845
5846 static void
5847 resource_path_print(struct dl *dl, struct resources *resources,
5848 uint64_t resource_id)
5849 {
5850 struct resource *resource, *parent_resource;
5851 const char del[] = "/";
5852 int path_len = 0;
5853 char *path;
5854
5855 resource = resource_find(resources, NULL, resource_id);
5856 if (!resource)
5857 return;
5858
5859 for (parent_resource = resource; parent_resource;
5860 parent_resource = parent_resource->parent)
5861 path_len += strlen(parent_resource->name) + 1;
5862
5863 path_len++;
5864 path = calloc(1, path_len);
5865 if (!path)
5866 return;
5867
5868 path += path_len - 1;
5869 for (parent_resource = resource; parent_resource;
5870 parent_resource = parent_resource->parent) {
5871 path -= strlen(parent_resource->name);
5872 memcpy(path, parent_resource->name,
5873 strlen(parent_resource->name));
5874 path -= strlen(del);
5875 memcpy(path, del, strlen(del));
5876 }
5877 check_indent_newline(dl);
5878 print_string(PRINT_ANY, "resource_path", "resource_path %s", path);
5879 free(path);
5880 }
5881
5882 static int dpipe_table_show(struct dpipe_ctx *ctx, struct nlattr *nl)
5883 {
5884 struct nlattr *nla_table[DEVLINK_ATTR_MAX + 1] = {};
5885 struct dpipe_table *table;
5886 uint32_t resource_units;
5887 bool counters_enabled;
5888 bool resource_valid;
5889 uint32_t size;
5890 int err;
5891
5892 err = mnl_attr_parse_nested(nl, attr_cb, nla_table);
5893 if (err != MNL_CB_OK)
5894 return -EINVAL;
5895
5896 if (!nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
5897 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE] ||
5898 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS] ||
5899 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES] ||
5900 !nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]) {
5901 return -EINVAL;
5902 }
5903
5904 table = dpipe_table_alloc();
5905 if (!table)
5906 return -ENOMEM;
5907
5908 table->name = strdup(mnl_attr_get_str(nla_table[DEVLINK_ATTR_DPIPE_TABLE_NAME]));
5909 size = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_SIZE]);
5910 counters_enabled = !!mnl_attr_get_u8(nla_table[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
5911
5912 resource_valid = nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID] &&
5913 ctx->resources;
5914 if (resource_valid) {
5915 table->resource_id = mnl_attr_get_u64(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID]);
5916 table->resource_valid = true;
5917 }
5918
5919 list_add_tail(&table->list, &ctx->tables->table_list);
5920 if (!ctx->print_tables)
5921 return 0;
5922
5923 check_indent_newline(ctx->dl);
5924 print_string(PRINT_ANY, "name", "name %s", table->name);
5925 print_uint(PRINT_ANY, "size", " size %u", size);
5926 print_bool(PRINT_ANY, "counters_enabled", " counters_enabled %s", counters_enabled);
5927
5928 if (resource_valid) {
5929 resource_units = mnl_attr_get_u32(nla_table[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS]);
5930 resource_path_print(ctx->dl, ctx->resources,
5931 table->resource_id);
5932 print_uint(PRINT_ANY, "resource_units", " resource_units %u",
5933 resource_units);
5934 }
5935
5936 pr_out_array_start(ctx->dl, "match");
5937 if (dpipe_table_matches_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_MATCHES]))
5938 goto err_matches_show;
5939 pr_out_array_end(ctx->dl);
5940
5941 pr_out_array_start(ctx->dl, "action");
5942 if (dpipe_table_actions_show(ctx, nla_table[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS]))
5943 goto err_actions_show;
5944 pr_out_array_end(ctx->dl);
5945
5946 return 0;
5947
5948 err_actions_show:
5949 err_matches_show:
5950 pr_out_array_end(ctx->dl);
5951 return -EINVAL;
5952 }
5953
5954 static int dpipe_tables_show(struct dpipe_ctx *ctx, struct nlattr **tb)
5955 {
5956 struct nlattr *nla_tables = tb[DEVLINK_ATTR_DPIPE_TABLES];
5957 struct nlattr *nla_table;
5958
5959 mnl_attr_for_each_nested(nla_table, nla_tables) {
5960 if (ctx->print_tables)
5961 pr_out_handle_start_arr(ctx->dl, tb);
5962 if (dpipe_table_show(ctx, nla_table))
5963 goto err_table_show;
5964 if (ctx->print_tables)
5965 pr_out_handle_end(ctx->dl);
5966 }
5967 return 0;
5968
5969 err_table_show:
5970 if (ctx->print_tables)
5971 pr_out_handle_end(ctx->dl);
5972 return -EINVAL;
5973 }
5974
5975 static int cmd_dpipe_table_show_cb(const struct nlmsghdr *nlh, void *data)
5976 {
5977 struct dpipe_ctx *ctx = data;
5978 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
5979 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
5980
5981 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
5982 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
5983 !tb[DEVLINK_ATTR_DPIPE_TABLES])
5984 return MNL_CB_ERROR;
5985
5986 if (dpipe_tables_show(ctx, tb))
5987 return MNL_CB_ERROR;
5988 return MNL_CB_OK;
5989 }
5990
5991 static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data);
5992
5993 static int cmd_dpipe_table_show(struct dl *dl)
5994 {
5995 struct nlmsghdr *nlh;
5996 struct dpipe_ctx dpipe_ctx = {};
5997 struct resource_ctx resource_ctx = {};
5998 uint16_t flags = NLM_F_REQUEST;
5999 int err;
6000
6001 err = dl_argv_parse(dl, DL_OPT_HANDLE, DL_OPT_DPIPE_TABLE_NAME);
6002 if (err)
6003 return err;
6004
6005 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
6006
6007 err = dpipe_ctx_init(&dpipe_ctx, dl);
6008 if (err)
6009 return err;
6010
6011 dpipe_ctx.print_tables = true;
6012
6013 dl_opts_put(nlh, dl);
6014 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb,
6015 &dpipe_ctx);
6016 if (err) {
6017 pr_err("error get headers %s\n", strerror(dpipe_ctx.err));
6018 goto err_headers_get;
6019 }
6020
6021 err = resource_ctx_init(&resource_ctx, dl);
6022 if (err)
6023 goto err_resource_ctx_init;
6024
6025 resource_ctx.print_resources = false;
6026 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP, flags);
6027 dl_opts_put(nlh, dl);
6028 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb,
6029 &resource_ctx);
6030 if (!err)
6031 dpipe_ctx.resources = resource_ctx.resources;
6032
6033 flags = NLM_F_REQUEST | NLM_F_ACK;
6034 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET, flags);
6035 dl_opts_put(nlh, dl);
6036
6037 pr_out_section_start(dl, "table");
6038 _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb, &dpipe_ctx);
6039 pr_out_section_end(dl);
6040
6041 resource_ctx_fini(&resource_ctx);
6042 dpipe_ctx_fini(&dpipe_ctx);
6043 return 0;
6044
6045 err_resource_ctx_init:
6046 err_headers_get:
6047 dpipe_ctx_fini(&dpipe_ctx);
6048 return err;
6049 }
6050
6051 static int cmd_dpipe_table_set(struct dl *dl)
6052 {
6053 struct nlmsghdr *nlh;
6054 int err;
6055
6056 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
6057 NLM_F_REQUEST | NLM_F_ACK);
6058
6059 err = dl_argv_parse_put(nlh, dl,
6060 DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME |
6061 DL_OPT_DPIPE_TABLE_COUNTERS, 0);
6062 if (err)
6063 return err;
6064
6065 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6066 }
6067
6068 enum dpipe_value_type {
6069 DPIPE_VALUE_TYPE_VALUE,
6070 DPIPE_VALUE_TYPE_MASK,
6071 };
6072
6073 static const char *
6074 dpipe_value_type_e2s(enum dpipe_value_type type)
6075 {
6076 switch (type) {
6077 case DPIPE_VALUE_TYPE_VALUE:
6078 return "value";
6079 case DPIPE_VALUE_TYPE_MASK:
6080 return "value_mask";
6081 default:
6082 return "<unknown>";
6083 }
6084 }
6085
6086 struct dpipe_field_printer {
6087 unsigned int field_id;
6088 void (*printer)(struct dpipe_ctx *, enum dpipe_value_type, void *);
6089 };
6090
6091 struct dpipe_header_printer {
6092 struct dpipe_field_printer *printers;
6093 unsigned int printers_count;
6094 unsigned int header_id;
6095 };
6096
6097 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx *ctx,
6098 enum dpipe_value_type type,
6099 void *value)
6100 {
6101 struct in_addr ip_addr;
6102
6103 ip_addr.s_addr = htonl(*(uint32_t *)value);
6104 check_indent_newline(ctx->dl);
6105 print_string_name_value(dpipe_value_type_e2s(type), inet_ntoa(ip_addr));
6106 }
6107
6108 static void
6109 dpipe_field_printer_ethernet_addr(struct dpipe_ctx *ctx,
6110 enum dpipe_value_type type,
6111 void *value)
6112 {
6113 check_indent_newline(ctx->dl);
6114 print_string_name_value(dpipe_value_type_e2s(type),
6115 ether_ntoa((struct ether_addr *)value));
6116 }
6117
6118 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx *ctx,
6119 enum dpipe_value_type type,
6120 void *value)
6121 {
6122 char str[INET6_ADDRSTRLEN];
6123
6124 inet_ntop(AF_INET6, value, str, INET6_ADDRSTRLEN);
6125 check_indent_newline(ctx->dl);
6126 print_string_name_value(dpipe_value_type_e2s(type), str);
6127 }
6128
6129 static struct dpipe_field_printer dpipe_field_printers_ipv4[] = {
6130 {
6131 .printer = dpipe_field_printer_ipv4_addr,
6132 .field_id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
6133 }
6134 };
6135
6136 static struct dpipe_header_printer dpipe_header_printer_ipv4 = {
6137 .printers = dpipe_field_printers_ipv4,
6138 .printers_count = ARRAY_SIZE(dpipe_field_printers_ipv4),
6139 .header_id = DEVLINK_DPIPE_HEADER_IPV4,
6140 };
6141
6142 static struct dpipe_field_printer dpipe_field_printers_ethernet[] = {
6143 {
6144 .printer = dpipe_field_printer_ethernet_addr,
6145 .field_id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
6146 },
6147 };
6148
6149 static struct dpipe_header_printer dpipe_header_printer_ethernet = {
6150 .printers = dpipe_field_printers_ethernet,
6151 .printers_count = ARRAY_SIZE(dpipe_field_printers_ethernet),
6152 .header_id = DEVLINK_DPIPE_HEADER_ETHERNET,
6153 };
6154
6155 static struct dpipe_field_printer dpipe_field_printers_ipv6[] = {
6156 {
6157 .printer = dpipe_field_printer_ipv6_addr,
6158 .field_id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
6159 }
6160 };
6161
6162 static struct dpipe_header_printer dpipe_header_printer_ipv6 = {
6163 .printers = dpipe_field_printers_ipv6,
6164 .printers_count = ARRAY_SIZE(dpipe_field_printers_ipv6),
6165 .header_id = DEVLINK_DPIPE_HEADER_IPV6,
6166 };
6167
6168 static struct dpipe_header_printer *dpipe_header_printers[] = {
6169 &dpipe_header_printer_ipv4,
6170 &dpipe_header_printer_ethernet,
6171 &dpipe_header_printer_ipv6,
6172 };
6173
6174 static int dpipe_print_prot_header(struct dpipe_ctx *ctx,
6175 struct dpipe_op_info *info,
6176 enum dpipe_value_type type,
6177 void *value)
6178 {
6179 unsigned int header_printers_count = ARRAY_SIZE(dpipe_header_printers);
6180 struct dpipe_header_printer *header_printer;
6181 struct dpipe_field_printer *field_printer;
6182 unsigned int field_printers_count;
6183 int j;
6184 int i;
6185
6186 for (i = 0; i < header_printers_count; i++) {
6187 header_printer = dpipe_header_printers[i];
6188 if (header_printer->header_id != info->header_id)
6189 continue;
6190 field_printers_count = header_printer->printers_count;
6191 for (j = 0; j < field_printers_count; j++) {
6192 field_printer = &header_printer->printers[j];
6193 if (field_printer->field_id != info->field_id)
6194 continue;
6195 field_printer->printer(ctx, type, value);
6196 return 0;
6197 }
6198 }
6199
6200 return -EINVAL;
6201 }
6202
6203 static void __pr_out_entry_value(struct dpipe_ctx *ctx,
6204 void *value,
6205 unsigned int value_len,
6206 struct dpipe_op_info *info,
6207 enum dpipe_value_type type)
6208 {
6209 if (info->header_global &&
6210 !dpipe_print_prot_header(ctx, info, type, value))
6211 return;
6212
6213 if (value_len == sizeof(uint32_t)) {
6214 uint32_t *value_32 = value;
6215
6216 check_indent_newline(ctx->dl);
6217 print_uint_name_value(dpipe_value_type_e2s(type), *value_32);
6218 }
6219 }
6220
6221 static void pr_out_dpipe_entry_value(struct dpipe_ctx *ctx,
6222 struct nlattr **nla_match_value,
6223 struct dpipe_op_info *info)
6224 {
6225 void *value, *value_mask;
6226 uint32_t value_mapping;
6227 uint16_t value_len;
6228 bool mask, mapping;
6229
6230 mask = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MASK];
6231 mapping = !!nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING];
6232
6233 value_len = mnl_attr_get_payload_len(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
6234 value = mnl_attr_get_payload(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
6235
6236 if (mapping) {
6237 value_mapping = mnl_attr_get_u32(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE_MAPPING]);
6238 check_indent_newline(ctx->dl);
6239 print_uint(PRINT_ANY, "mapping_value", "mapping_value %u", value_mapping);
6240 }
6241
6242 if (mask) {
6243 value_mask = mnl_attr_get_payload(nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]);
6244 __pr_out_entry_value(ctx, value_mask, value_len, info,
6245 DPIPE_VALUE_TYPE_MASK);
6246 }
6247
6248 __pr_out_entry_value(ctx, value, value_len, info, DPIPE_VALUE_TYPE_VALUE);
6249 }
6250
6251 static int dpipe_entry_match_value_show(struct dpipe_ctx *ctx,
6252 struct nlattr *nl)
6253 {
6254 struct nlattr *nla_match_value[DEVLINK_ATTR_MAX + 1] = {};
6255 struct dpipe_match match;
6256 int err;
6257
6258 err = mnl_attr_parse_nested(nl, attr_cb, nla_match_value);
6259 if (err != MNL_CB_OK)
6260 return -EINVAL;
6261
6262 if (!nla_match_value[DEVLINK_ATTR_DPIPE_MATCH] ||
6263 !nla_match_value[DEVLINK_ATTR_DPIPE_VALUE]) {
6264 return -EINVAL;
6265 }
6266
6267 pr_out_entry_start(ctx->dl);
6268 if (dpipe_match_parse(&match,
6269 nla_match_value[DEVLINK_ATTR_DPIPE_MATCH]))
6270 goto err_match_parse;
6271 pr_out_dpipe_match(&match, ctx);
6272 pr_out_dpipe_entry_value(ctx, nla_match_value, &match.info);
6273 pr_out_entry_end(ctx->dl);
6274
6275 return 0;
6276
6277 err_match_parse:
6278 pr_out_entry_end(ctx->dl);
6279 return -EINVAL;
6280 }
6281
6282 static int dpipe_entry_action_value_show(struct dpipe_ctx *ctx,
6283 struct nlattr *nl)
6284 {
6285 struct nlattr *nla_action_value[DEVLINK_ATTR_MAX + 1] = {};
6286 struct dpipe_action action;
6287 int err;
6288
6289 err = mnl_attr_parse_nested(nl, attr_cb, nla_action_value);
6290 if (err != MNL_CB_OK)
6291 return -EINVAL;
6292
6293 if (!nla_action_value[DEVLINK_ATTR_DPIPE_ACTION] ||
6294 !nla_action_value[DEVLINK_ATTR_DPIPE_VALUE]) {
6295 return -EINVAL;
6296 }
6297
6298 pr_out_entry_start(ctx->dl);
6299 if (dpipe_action_parse(&action,
6300 nla_action_value[DEVLINK_ATTR_DPIPE_ACTION]))
6301 goto err_action_parse;
6302 pr_out_dpipe_action(&action, ctx);
6303 pr_out_dpipe_entry_value(ctx, nla_action_value, &action.info);
6304 pr_out_entry_end(ctx->dl);
6305
6306 return 0;
6307
6308 err_action_parse:
6309 pr_out_entry_end(ctx->dl);
6310 return -EINVAL;
6311 }
6312
6313 static int
6314 dpipe_tables_action_values_show(struct dpipe_ctx *ctx,
6315 struct nlattr *nla_action_values)
6316 {
6317 struct nlattr *nla_action_value;
6318
6319 mnl_attr_for_each_nested(nla_action_value, nla_action_values) {
6320 if (dpipe_entry_action_value_show(ctx, nla_action_value))
6321 return -EINVAL;
6322 }
6323 return 0;
6324 }
6325
6326 static int
6327 dpipe_tables_match_values_show(struct dpipe_ctx *ctx,
6328 struct nlattr *nla_match_values)
6329 {
6330 struct nlattr *nla_match_value;
6331
6332 mnl_attr_for_each_nested(nla_match_value, nla_match_values) {
6333 if (dpipe_entry_match_value_show(ctx, nla_match_value))
6334 return -EINVAL;
6335 }
6336 return 0;
6337 }
6338
6339 static int dpipe_entry_show(struct dpipe_ctx *ctx, struct nlattr *nl)
6340 {
6341 struct nlattr *nla_entry[DEVLINK_ATTR_MAX + 1] = {};
6342 uint32_t entry_index;
6343 uint64_t counter;
6344 int err;
6345
6346 err = mnl_attr_parse_nested(nl, attr_cb, nla_entry);
6347 if (err != MNL_CB_OK)
6348 return -EINVAL;
6349
6350 if (!nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX] ||
6351 !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES] ||
6352 !nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]) {
6353 return -EINVAL;
6354 }
6355
6356 check_indent_newline(ctx->dl);
6357 entry_index = mnl_attr_get_u32(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_INDEX]);
6358 print_uint(PRINT_ANY, "index", "index %u", entry_index);
6359
6360 if (nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]) {
6361 counter = mnl_attr_get_u64(nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER]);
6362 print_uint(PRINT_ANY, "counter", " counter %u", counter);
6363 }
6364
6365 pr_out_array_start(ctx->dl, "match_value");
6366 if (dpipe_tables_match_values_show(ctx,
6367 nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES]))
6368 goto err_match_values_show;
6369 pr_out_array_end(ctx->dl);
6370
6371 pr_out_array_start(ctx->dl, "action_value");
6372 if (dpipe_tables_action_values_show(ctx,
6373 nla_entry[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES]))
6374 goto err_action_values_show;
6375 pr_out_array_end(ctx->dl);
6376 return 0;
6377
6378 err_action_values_show:
6379 err_match_values_show:
6380 pr_out_array_end(ctx->dl);
6381 return -EINVAL;
6382 }
6383
6384 static int dpipe_table_entries_show(struct dpipe_ctx *ctx, struct nlattr **tb)
6385 {
6386 struct nlattr *nla_entries = tb[DEVLINK_ATTR_DPIPE_ENTRIES];
6387 struct nlattr *nla_entry;
6388
6389 mnl_attr_for_each_nested(nla_entry, nla_entries) {
6390 pr_out_handle_start_arr(ctx->dl, tb);
6391 if (dpipe_entry_show(ctx, nla_entry))
6392 goto err_entry_show;
6393 pr_out_handle_end(ctx->dl);
6394 }
6395 return 0;
6396
6397 err_entry_show:
6398 pr_out_handle_end(ctx->dl);
6399 return -EINVAL;
6400 }
6401
6402 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr *nlh, void *data)
6403 {
6404 struct dpipe_ctx *ctx = data;
6405 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6406 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6407
6408 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6409 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6410 !tb[DEVLINK_ATTR_DPIPE_ENTRIES])
6411 return MNL_CB_ERROR;
6412
6413 if (dpipe_table_entries_show(ctx, tb))
6414 return MNL_CB_ERROR;
6415 return MNL_CB_OK;
6416 }
6417
6418 static int cmd_dpipe_table_dump(struct dl *dl)
6419 {
6420 struct nlmsghdr *nlh;
6421 struct dpipe_ctx ctx = {};
6422 uint16_t flags = NLM_F_REQUEST;
6423 int err;
6424
6425 err = dpipe_ctx_init(&ctx, dl);
6426 if (err)
6427 return err;
6428
6429 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_DPIPE_TABLE_NAME, 0);
6430 if (err)
6431 goto out;
6432
6433 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_HEADERS_GET, flags);
6434 dl_opts_put(nlh, dl);
6435 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_header_cb, &ctx);
6436 if (err) {
6437 pr_err("error get headers %s\n", strerror(ctx.err));
6438 goto out;
6439 }
6440
6441 flags = NLM_F_REQUEST | NLM_F_ACK;
6442 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_ENTRIES_GET, flags);
6443 dl_opts_put(nlh, dl);
6444
6445 pr_out_section_start(dl, "table_entry");
6446 _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_entry_dump_cb, &ctx);
6447 pr_out_section_end(dl);
6448 out:
6449 dpipe_ctx_fini(&ctx);
6450 return err;
6451 }
6452
6453 static int cmd_dpipe_table(struct dl *dl)
6454 {
6455 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
6456 cmd_dpipe_help();
6457 return 0;
6458 } else if (dl_argv_match(dl, "show")) {
6459 dl_arg_inc(dl);
6460 return cmd_dpipe_table_show(dl);
6461 } else if (dl_argv_match(dl, "set")) {
6462 dl_arg_inc(dl);
6463 return cmd_dpipe_table_set(dl);
6464 } else if (dl_argv_match(dl, "dump")) {
6465 dl_arg_inc(dl);
6466 return cmd_dpipe_table_dump(dl);
6467 }
6468 pr_err("Command \"%s\" not found\n", dl_argv(dl));
6469 return -ENOENT;
6470 }
6471
6472 static int cmd_dpipe(struct dl *dl)
6473 {
6474 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
6475 cmd_dpipe_help();
6476 return 0;
6477 } else if (dl_argv_match(dl, "header")) {
6478 dl_arg_inc(dl);
6479 return cmd_dpipe_header(dl);
6480 } else if (dl_argv_match(dl, "table")) {
6481 dl_arg_inc(dl);
6482 return cmd_dpipe_table(dl);
6483 }
6484 pr_err("Command \"%s\" not found\n", dl_argv(dl));
6485 return -ENOENT;
6486 }
6487
6488 static int
6489 resource_parse(struct resource_ctx *ctx, struct resource *resource,
6490 struct nlattr **nla_resource)
6491 {
6492 if (!nla_resource[DEVLINK_ATTR_RESOURCE_NAME] ||
6493 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE] ||
6494 !nla_resource[DEVLINK_ATTR_RESOURCE_ID] ||
6495 !nla_resource[DEVLINK_ATTR_RESOURCE_UNIT] ||
6496 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN] ||
6497 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX] ||
6498 !nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]) {
6499 return -EINVAL;
6500 }
6501
6502 resource->name = strdup(mnl_attr_get_str(nla_resource[DEVLINK_ATTR_RESOURCE_NAME]));
6503 resource->size = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE]);
6504 resource->id = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_ID]);
6505 resource->unit = mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_UNIT]);
6506 resource->size_min = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MIN]);
6507 resource->size_max = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_MAX]);
6508 resource->size_gran = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_GRAN]);
6509
6510 if (nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW])
6511 resource->size_new = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_NEW]);
6512 else
6513 resource->size_new = resource->size;
6514
6515 if (nla_resource[DEVLINK_ATTR_RESOURCE_OCC]) {
6516 resource->size_occ = mnl_attr_get_u64(nla_resource[DEVLINK_ATTR_RESOURCE_OCC]);
6517 resource->occ_valid = true;
6518 }
6519
6520 if (resource->size_new != resource->size)
6521 ctx->pending_change = true;
6522
6523 return 0;
6524 }
6525
6526 static int
6527 resource_get(struct resource_ctx *ctx, struct resource *resource,
6528 struct resource *parent_resource, struct nlattr *nl)
6529 {
6530 struct nlattr *nla_resource[DEVLINK_ATTR_MAX + 1] = {};
6531 struct nlattr *nla_child_resource;
6532 struct nlattr *nla_resources;
6533 bool top = false;
6534 int err;
6535
6536 if (!resource) {
6537 nla_resources = nl;
6538 top = true;
6539 goto out;
6540 }
6541
6542 err = mnl_attr_parse_nested(nl, attr_cb, nla_resource);
6543 if (err != MNL_CB_OK)
6544 return -EINVAL;
6545
6546 err = resource_parse(ctx, resource, nla_resource);
6547 if (err)
6548 return err;
6549
6550 resource->parent = parent_resource;
6551 if (!nla_resource[DEVLINK_ATTR_RESOURCE_LIST])
6552 return 0;
6553
6554 resource->size_valid = !!mnl_attr_get_u8(nla_resource[DEVLINK_ATTR_RESOURCE_SIZE_VALID]);
6555 nla_resources = nla_resource[DEVLINK_ATTR_RESOURCE_LIST];
6556 out:
6557 mnl_attr_for_each_nested(nla_child_resource, nla_resources) {
6558 struct resource *child_resource;
6559 struct list_head *list;
6560
6561 child_resource = resource_alloc();
6562 if (!child_resource)
6563 return -ENOMEM;
6564
6565 if (top)
6566 list = &ctx->resources->resource_list;
6567 else
6568 list = &resource->resource_list;
6569
6570 list_add_tail(&child_resource->list, list);
6571 err = resource_get(ctx, child_resource, resource,
6572 nla_child_resource);
6573 if (err)
6574 return err;
6575 }
6576
6577 return 0;
6578 }
6579
6580 static const char *resource_unit_str_get(enum devlink_resource_unit unit)
6581 {
6582 switch (unit) {
6583 case DEVLINK_RESOURCE_UNIT_ENTRY: return "entry";
6584 default: return "<unknown unit>";
6585 }
6586 }
6587
6588 static void resource_show(struct resource *resource,
6589 struct resource_ctx *ctx)
6590 {
6591 struct resource *child_resource;
6592 struct dpipe_table *table;
6593 struct dl *dl = ctx->dl;
6594 bool array = false;
6595
6596 check_indent_newline(dl);
6597 print_string(PRINT_ANY, "name", "name %s", resource->name);
6598 if (dl->verbose)
6599 resource_path_print(dl, ctx->resources, resource->id);
6600 pr_out_u64(dl, "size", resource->size);
6601 if (resource->size != resource->size_new)
6602 pr_out_u64(dl, "size_new", resource->size_new);
6603 if (resource->occ_valid)
6604 print_uint(PRINT_ANY, "occ", " occ %u", resource->size_occ);
6605 print_string(PRINT_ANY, "unit", " unit %s",
6606 resource_unit_str_get(resource->unit));
6607
6608 if (resource->size_min != resource->size_max) {
6609 print_uint(PRINT_ANY, "size_min", " size_min %u",
6610 resource->size_min);
6611 pr_out_u64(dl, "size_max", resource->size_max);
6612 print_uint(PRINT_ANY, "size_gran", " size_gran %u",
6613 resource->size_gran);
6614 }
6615
6616 list_for_each_entry(table, &ctx->tables->table_list, list)
6617 if (table->resource_id == resource->id &&
6618 table->resource_valid)
6619 array = true;
6620
6621 if (array)
6622 pr_out_array_start(dl, "dpipe_tables");
6623 else
6624 print_string(PRINT_ANY, "dpipe_tables", " dpipe_tables none",
6625 "none");
6626
6627 list_for_each_entry(table, &ctx->tables->table_list, list) {
6628 if (table->resource_id != resource->id ||
6629 !table->resource_valid)
6630 continue;
6631 pr_out_entry_start(dl);
6632 check_indent_newline(dl);
6633 print_string(PRINT_ANY, "table_name", "table_name %s",
6634 table->name);
6635 pr_out_entry_end(dl);
6636 }
6637 if (array)
6638 pr_out_array_end(dl);
6639
6640 if (list_empty(&resource->resource_list))
6641 return;
6642
6643 if (ctx->pending_change) {
6644 check_indent_newline(dl);
6645 print_string(PRINT_ANY, "size_valid", "size_valid %s",
6646 resource->size_valid ? "true" : "false");
6647 }
6648 pr_out_array_start(dl, "resources");
6649 list_for_each_entry(child_resource, &resource->resource_list, list) {
6650 pr_out_entry_start(dl);
6651 resource_show(child_resource, ctx);
6652 pr_out_entry_end(dl);
6653 }
6654 pr_out_array_end(dl);
6655 }
6656
6657 static void
6658 resources_show(struct resource_ctx *ctx, struct nlattr **tb)
6659 {
6660 struct resources *resources = ctx->resources;
6661 struct resource *resource;
6662
6663 list_for_each_entry(resource, &resources->resource_list, list) {
6664 pr_out_handle_start_arr(ctx->dl, tb);
6665 resource_show(resource, ctx);
6666 pr_out_handle_end(ctx->dl);
6667 }
6668 }
6669
6670 static int resources_get(struct resource_ctx *ctx, struct nlattr **tb)
6671 {
6672 return resource_get(ctx, NULL, NULL, tb[DEVLINK_ATTR_RESOURCE_LIST]);
6673 }
6674
6675 static int cmd_resource_dump_cb(const struct nlmsghdr *nlh, void *data)
6676 {
6677 struct resource_ctx *ctx = data;
6678 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6679 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6680 int err;
6681
6682 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6683 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6684 !tb[DEVLINK_ATTR_RESOURCE_LIST])
6685 return MNL_CB_ERROR;
6686
6687 err = resources_get(ctx, tb);
6688 if (err) {
6689 ctx->err = err;
6690 return MNL_CB_ERROR;
6691 }
6692
6693 if (ctx->print_resources)
6694 resources_show(ctx, tb);
6695
6696 return MNL_CB_OK;
6697 }
6698
6699 static int cmd_resource_show(struct dl *dl)
6700 {
6701 struct nlmsghdr *nlh;
6702 struct dpipe_ctx dpipe_ctx = {};
6703 struct resource_ctx resource_ctx = {};
6704 int err;
6705
6706 err = dl_argv_parse(dl, DL_OPT_HANDLE, 0);
6707 if (err)
6708 return err;
6709
6710 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_DPIPE_TABLE_GET,
6711 NLM_F_REQUEST);
6712 dl_opts_put(nlh, dl);
6713
6714 err = dpipe_ctx_init(&dpipe_ctx, dl);
6715 if (err)
6716 return err;
6717
6718 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_dpipe_table_show_cb,
6719 &dpipe_ctx);
6720 if (err) {
6721 pr_err("error get tables %s\n", strerror(dpipe_ctx.err));
6722 goto out;
6723 }
6724
6725 err = resource_ctx_init(&resource_ctx, dl);
6726 if (err)
6727 goto out;
6728
6729 resource_ctx.print_resources = true;
6730 resource_ctx.tables = dpipe_ctx.tables;
6731 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP,
6732 NLM_F_REQUEST | NLM_F_ACK);
6733 dl_opts_put(nlh, dl);
6734 pr_out_section_start(dl, "resources");
6735 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb,
6736 &resource_ctx);
6737 pr_out_section_end(dl);
6738 resource_ctx_fini(&resource_ctx);
6739 out:
6740 dpipe_ctx_fini(&dpipe_ctx);
6741 return err;
6742 }
6743
6744 static void cmd_resource_help(void)
6745 {
6746 pr_err("Usage: devlink resource show DEV\n"
6747 " devlink resource set DEV path PATH size SIZE\n");
6748 }
6749
6750 static struct resource *
6751 resource_find_by_name(struct list_head *list, char *name)
6752 {
6753 struct resource *resource;
6754
6755 list_for_each_entry(resource, list, list) {
6756 if (!strcmp(resource->name, name))
6757 return resource;
6758 }
6759 return NULL;
6760 }
6761
6762 static int
6763 resource_path_parse(struct resource_ctx *ctx, const char *resource_path,
6764 uint32_t *p_resource_id, bool *p_resource_valid)
6765 {
6766 struct resource *resource;
6767 uint32_t resource_id = 0;
6768 char *resource_path_dup;
6769 struct list_head *list;
6770 const char del[] = "/";
6771 char *resource_name;
6772
6773 resource_path_dup = strdup(resource_path);
6774 list = &ctx->resources->resource_list;
6775 resource_name = strtok(resource_path_dup, del);
6776 while (resource_name != NULL) {
6777 resource = resource_find_by_name(list, resource_name);
6778 if (!resource)
6779 goto err_resource_lookup;
6780
6781 list = &resource->resource_list;
6782 resource_name = strtok(NULL, del);
6783 resource_id = resource->id;
6784 }
6785 free(resource_path_dup);
6786 *p_resource_valid = true;
6787 *p_resource_id = resource_id;
6788 return 0;
6789
6790 err_resource_lookup:
6791 free(resource_path_dup);
6792 return -EINVAL;
6793 }
6794
6795 static int cmd_resource_set(struct dl *dl)
6796 {
6797 struct nlmsghdr *nlh;
6798 struct resource_ctx ctx = {};
6799 int err;
6800
6801 err = resource_ctx_init(&ctx, dl);
6802 if (err)
6803 return err;
6804
6805 ctx.print_resources = false;
6806 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_RESOURCE_PATH |
6807 DL_OPT_RESOURCE_SIZE, 0);
6808 if (err)
6809 goto out;
6810
6811 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_DUMP,
6812 NLM_F_REQUEST);
6813 dl_opts_put(nlh, dl);
6814 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_resource_dump_cb, &ctx);
6815 if (err) {
6816 pr_err("error getting resources %s\n", strerror(ctx.err));
6817 goto out;
6818 }
6819
6820 err = resource_path_parse(&ctx, dl->opts.resource_path,
6821 &dl->opts.resource_id,
6822 &dl->opts.resource_id_valid);
6823 if (err) {
6824 pr_err("error parsing resource path %s\n", strerror(-err));
6825 goto out;
6826 }
6827
6828 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_RESOURCE_SET,
6829 NLM_F_REQUEST | NLM_F_ACK);
6830
6831 dl_opts_put(nlh, dl);
6832 err = _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
6833 out:
6834 resource_ctx_fini(&ctx);
6835 return err;
6836 }
6837
6838 static int cmd_resource(struct dl *dl)
6839 {
6840 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
6841 cmd_resource_help();
6842 return 0;
6843 } else if (dl_argv_match(dl, "show")) {
6844 dl_arg_inc(dl);
6845 return cmd_resource_show(dl);
6846 } else if (dl_argv_match(dl, "set")) {
6847 dl_arg_inc(dl);
6848 return cmd_resource_set(dl);
6849 }
6850 pr_err("Command \"%s\" not found\n", dl_argv(dl));
6851 return -ENOENT;
6852 }
6853
6854 static void pr_out_region_handle_start(struct dl *dl, struct nlattr **tb)
6855 {
6856 const char *bus_name = mnl_attr_get_str(tb[DEVLINK_ATTR_BUS_NAME]);
6857 const char *dev_name = mnl_attr_get_str(tb[DEVLINK_ATTR_DEV_NAME]);
6858 const char *region_name = mnl_attr_get_str(tb[DEVLINK_ATTR_REGION_NAME]);
6859 char buf[256];
6860
6861 sprintf(buf, "%s/%s/%s", bus_name, dev_name, region_name);
6862 if (dl->json_output)
6863 open_json_object(buf);
6864 else
6865 pr_out("%s:", buf);
6866 }
6867
6868 static void pr_out_region_handle_end(struct dl *dl)
6869 {
6870 if (dl->json_output)
6871 close_json_object();
6872 else
6873 pr_out("\n");
6874 }
6875
6876 static void pr_out_region_snapshots_start(struct dl *dl, bool array)
6877 {
6878 __pr_out_indent_newline(dl);
6879 if (dl->json_output)
6880 open_json_array(PRINT_JSON, "snapshot");
6881 else
6882 pr_out("snapshot %s", array ? "[" : "");
6883 }
6884
6885 static void pr_out_region_snapshots_end(struct dl *dl, bool array)
6886 {
6887 if (dl->json_output)
6888 close_json_array(PRINT_JSON, NULL);
6889 else if (array)
6890 pr_out("]");
6891 }
6892
6893 static void pr_out_region_snapshots_id(struct dl *dl, struct nlattr **tb, int index)
6894 {
6895 uint32_t snapshot_id;
6896
6897 if (!tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
6898 return;
6899
6900 snapshot_id = mnl_attr_get_u32(tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6901
6902 if (dl->json_output)
6903 print_uint(PRINT_JSON, NULL, NULL, snapshot_id);
6904 else
6905 pr_out("%s%u", index ? " " : "", snapshot_id);
6906 }
6907
6908 static void pr_out_snapshots(struct dl *dl, struct nlattr **tb)
6909 {
6910 struct nlattr *tb_snapshot[DEVLINK_ATTR_MAX + 1] = {};
6911 struct nlattr *nla_sanpshot;
6912 int err, index = 0;
6913
6914 pr_out_region_snapshots_start(dl, true);
6915 mnl_attr_for_each_nested(nla_sanpshot, tb[DEVLINK_ATTR_REGION_SNAPSHOTS]) {
6916 err = mnl_attr_parse_nested(nla_sanpshot, attr_cb, tb_snapshot);
6917 if (err != MNL_CB_OK)
6918 return;
6919 pr_out_region_snapshots_id(dl, tb_snapshot, index++);
6920 }
6921 pr_out_region_snapshots_end(dl, true);
6922 }
6923
6924 static void pr_out_snapshot(struct dl *dl, struct nlattr **tb)
6925 {
6926 pr_out_region_snapshots_start(dl, false);
6927 pr_out_region_snapshots_id(dl, tb, 0);
6928 pr_out_region_snapshots_end(dl, false);
6929 }
6930
6931 static void pr_out_region(struct dl *dl, struct nlattr **tb)
6932 {
6933 pr_out_region_handle_start(dl, tb);
6934
6935 if (tb[DEVLINK_ATTR_REGION_SIZE])
6936 pr_out_u64(dl, "size",
6937 mnl_attr_get_u64(tb[DEVLINK_ATTR_REGION_SIZE]));
6938
6939 if (tb[DEVLINK_ATTR_REGION_SNAPSHOTS])
6940 pr_out_snapshots(dl, tb);
6941
6942 if (tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
6943 pr_out_snapshot(dl, tb);
6944
6945 pr_out_region_handle_end(dl);
6946 }
6947
6948 static int cmd_region_show_cb(const struct nlmsghdr *nlh, void *data)
6949 {
6950 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
6951 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
6952 struct dl *dl = data;
6953
6954 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
6955 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
6956 !tb[DEVLINK_ATTR_REGION_NAME] || !tb[DEVLINK_ATTR_REGION_SIZE])
6957 return MNL_CB_ERROR;
6958
6959 pr_out_region(dl, tb);
6960
6961 return MNL_CB_OK;
6962 }
6963
6964 static int cmd_region_show(struct dl *dl)
6965 {
6966 struct nlmsghdr *nlh;
6967 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
6968 int err;
6969
6970 if (dl_argc(dl) == 0)
6971 flags |= NLM_F_DUMP;
6972
6973 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_GET, flags);
6974
6975 if (dl_argc(dl) > 0) {
6976 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION, 0);
6977 if (err)
6978 return err;
6979 }
6980
6981 pr_out_section_start(dl, "regions");
6982 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_show_cb, dl);
6983 pr_out_section_end(dl);
6984 return err;
6985 }
6986
6987 static int cmd_region_snapshot_del(struct dl *dl)
6988 {
6989 struct nlmsghdr *nlh;
6990 int err;
6991
6992 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_DEL,
6993 NLM_F_REQUEST | NLM_F_ACK);
6994
6995 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
6996 DL_OPT_REGION_SNAPSHOT_ID, 0);
6997 if (err)
6998 return err;
6999
7000 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7001 }
7002
7003 static int cmd_region_read_cb(const struct nlmsghdr *nlh, void *data)
7004 {
7005 struct nlattr *nla_entry, *nla_chunk_data, *nla_chunk_addr;
7006 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7007 struct nlattr *tb_field[DEVLINK_ATTR_MAX + 1] = {};
7008 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7009 struct dl *dl = data;
7010 int err;
7011
7012 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7013 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7014 !tb[DEVLINK_ATTR_REGION_CHUNKS])
7015 return MNL_CB_ERROR;
7016
7017 mnl_attr_for_each_nested(nla_entry, tb[DEVLINK_ATTR_REGION_CHUNKS]) {
7018 err = mnl_attr_parse_nested(nla_entry, attr_cb, tb_field);
7019 if (err != MNL_CB_OK)
7020 return MNL_CB_ERROR;
7021
7022 nla_chunk_data = tb_field[DEVLINK_ATTR_REGION_CHUNK_DATA];
7023 if (!nla_chunk_data)
7024 continue;
7025
7026 nla_chunk_addr = tb_field[DEVLINK_ATTR_REGION_CHUNK_ADDR];
7027 if (!nla_chunk_addr)
7028 continue;
7029
7030 pr_out_region_chunk(dl, mnl_attr_get_payload(nla_chunk_data),
7031 mnl_attr_get_payload_len(nla_chunk_data),
7032 mnl_attr_get_u64(nla_chunk_addr));
7033 }
7034 return MNL_CB_OK;
7035 }
7036
7037 static int cmd_region_dump(struct dl *dl)
7038 {
7039 struct nlmsghdr *nlh;
7040 int err;
7041
7042 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_READ,
7043 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
7044
7045 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
7046 DL_OPT_REGION_SNAPSHOT_ID, 0);
7047 if (err)
7048 return err;
7049
7050 pr_out_section_start(dl, "dump");
7051 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_read_cb, dl);
7052 pr_out_section_end(dl);
7053 if (!dl->json_output)
7054 pr_out("\n");
7055 return err;
7056 }
7057
7058 static int cmd_region_read(struct dl *dl)
7059 {
7060 struct nlmsghdr *nlh;
7061 int err;
7062
7063 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_READ,
7064 NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP);
7065
7066 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION |
7067 DL_OPT_REGION_ADDRESS | DL_OPT_REGION_LENGTH |
7068 DL_OPT_REGION_SNAPSHOT_ID, 0);
7069 if (err)
7070 return err;
7071
7072 pr_out_section_start(dl, "read");
7073 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_read_cb, dl);
7074 pr_out_section_end(dl);
7075 if (!dl->json_output)
7076 pr_out("\n");
7077 return err;
7078 }
7079
7080 static int cmd_region_snapshot_new_cb(const struct nlmsghdr *nlh, void *data)
7081 {
7082 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7083 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7084 struct dl *dl = data;
7085
7086 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7087 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7088 !tb[DEVLINK_ATTR_REGION_NAME] ||
7089 !tb[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
7090 return MNL_CB_ERROR;
7091
7092 pr_out_region(dl, tb);
7093
7094 return MNL_CB_OK;
7095 }
7096
7097 static int cmd_region_snapshot_new(struct dl *dl)
7098 {
7099 struct nlmsghdr *nlh;
7100 int err;
7101
7102 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_REGION_NEW,
7103 NLM_F_REQUEST | NLM_F_ACK);
7104
7105 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE_REGION,
7106 DL_OPT_REGION_SNAPSHOT_ID);
7107 if (err)
7108 return err;
7109
7110 pr_out_section_start(dl, "regions");
7111 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_region_snapshot_new_cb, dl);
7112 pr_out_section_end(dl);
7113 return err;
7114 }
7115
7116 static void cmd_region_help(void)
7117 {
7118 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
7119 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
7120 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
7121 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
7122 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
7123 }
7124
7125 static int cmd_region(struct dl *dl)
7126 {
7127 if (dl_no_arg(dl)) {
7128 return cmd_region_show(dl);
7129 } else if (dl_argv_match(dl, "help")) {
7130 cmd_region_help();
7131 return 0;
7132 } else if (dl_argv_match(dl, "show")) {
7133 dl_arg_inc(dl);
7134 return cmd_region_show(dl);
7135 } else if (dl_argv_match(dl, "del")) {
7136 dl_arg_inc(dl);
7137 return cmd_region_snapshot_del(dl);
7138 } else if (dl_argv_match(dl, "dump")) {
7139 dl_arg_inc(dl);
7140 return cmd_region_dump(dl);
7141 } else if (dl_argv_match(dl, "read")) {
7142 dl_arg_inc(dl);
7143 return cmd_region_read(dl);
7144 } else if (dl_argv_match(dl, "new")) {
7145 dl_arg_inc(dl);
7146 return cmd_region_snapshot_new(dl);
7147 }
7148 pr_err("Command \"%s\" not found\n", dl_argv(dl));
7149 return -ENOENT;
7150 }
7151
7152 static int cmd_health_set_params(struct dl *dl)
7153 {
7154 struct nlmsghdr *nlh;
7155 int err;
7156
7157 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_SET,
7158 NLM_F_REQUEST | NLM_F_ACK);
7159 err = dl_argv_parse(dl, DL_OPT_HANDLE | DL_OPT_HANDLEP | DL_OPT_HEALTH_REPORTER_NAME,
7160 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD |
7161 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER |
7162 DL_OPT_HEALTH_REPORTER_AUTO_DUMP);
7163 if (err)
7164 return err;
7165
7166 dl_opts_put(nlh, dl);
7167 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7168 }
7169
7170 static int cmd_health_dump_clear(struct dl *dl)
7171 {
7172 struct nlmsghdr *nlh;
7173 int err;
7174
7175 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
7176 NLM_F_REQUEST | NLM_F_ACK);
7177
7178 err = dl_argv_parse_put(nlh, dl,
7179 DL_OPT_HANDLE | DL_OPT_HANDLEP |
7180 DL_OPT_HEALTH_REPORTER_NAME, 0);
7181 if (err)
7182 return err;
7183
7184 dl_opts_put(nlh, dl);
7185 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7186 }
7187
7188 static int fmsg_value_show(struct dl *dl, int type, struct nlattr *nl_data)
7189 {
7190 uint8_t *data;
7191 uint32_t len;
7192
7193 check_indent_newline(dl);
7194 switch (type) {
7195 case MNL_TYPE_FLAG:
7196 print_bool(PRINT_ANY, NULL, "%s", mnl_attr_get_u8(nl_data));
7197 break;
7198 case MNL_TYPE_U8:
7199 print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u8(nl_data));
7200 break;
7201 case MNL_TYPE_U16:
7202 print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u16(nl_data));
7203 break;
7204 case MNL_TYPE_U32:
7205 print_uint(PRINT_ANY, NULL, "%u", mnl_attr_get_u32(nl_data));
7206 break;
7207 case MNL_TYPE_U64:
7208 print_u64(PRINT_ANY, NULL, "%"PRIu64, mnl_attr_get_u64(nl_data));
7209 break;
7210 case MNL_TYPE_NUL_STRING:
7211 print_string(PRINT_ANY, NULL, "%s", mnl_attr_get_str(nl_data));
7212 break;
7213 case MNL_TYPE_BINARY:
7214 len = mnl_attr_get_payload_len(nl_data);
7215 data = mnl_attr_get_payload(nl_data);
7216 pr_out_binary_value(dl, data, len);
7217 break;
7218 default:
7219 return -EINVAL;
7220 }
7221 return MNL_CB_OK;
7222 }
7223
7224 static void pr_out_fmsg_name(struct dl *dl, char **name)
7225 {
7226 if (!*name)
7227 return;
7228
7229 pr_out_name(dl, *name);
7230 free(*name);
7231 *name = NULL;
7232 }
7233
7234 struct nest_entry {
7235 int attr_type;
7236 struct list_head list;
7237 };
7238
7239 struct fmsg_cb_data {
7240 char *name;
7241 struct dl *dl;
7242 uint8_t value_type;
7243 struct list_head entry_list;
7244 };
7245
7246 static int cmd_fmsg_nest_queue(struct fmsg_cb_data *fmsg_data,
7247 uint8_t *attr_value, bool insert)
7248 {
7249 struct nest_entry *entry;
7250
7251 if (insert) {
7252 entry = malloc(sizeof(struct nest_entry));
7253 if (!entry)
7254 return -ENOMEM;
7255
7256 entry->attr_type = *attr_value;
7257 list_add(&entry->list, &fmsg_data->entry_list);
7258 } else {
7259 if (list_empty(&fmsg_data->entry_list))
7260 return MNL_CB_ERROR;
7261 entry = list_first_entry(&fmsg_data->entry_list,
7262 struct nest_entry, list);
7263 *attr_value = entry->attr_type;
7264 list_del(&entry->list);
7265 free(entry);
7266 }
7267 return MNL_CB_OK;
7268 }
7269
7270 static void pr_out_fmsg_group_start(struct dl *dl, char **name)
7271 {
7272 __pr_out_newline();
7273 pr_out_fmsg_name(dl, name);
7274 __pr_out_newline();
7275 __pr_out_indent_inc();
7276 }
7277
7278 static void pr_out_fmsg_group_end(struct dl *dl)
7279 {
7280 __pr_out_newline();
7281 __pr_out_indent_dec();
7282 }
7283
7284 static void pr_out_fmsg_start_object(struct dl *dl, char **name)
7285 {
7286 if (dl->json_output) {
7287 pr_out_fmsg_name(dl, name);
7288 open_json_object(NULL);
7289 } else {
7290 pr_out_fmsg_group_start(dl, name);
7291 }
7292 }
7293
7294 static void pr_out_fmsg_end_object(struct dl *dl)
7295 {
7296 if (dl->json_output)
7297 close_json_object();
7298 else
7299 pr_out_fmsg_group_end(dl);
7300 }
7301
7302 static void pr_out_fmsg_start_array(struct dl *dl, char **name)
7303 {
7304 if (dl->json_output) {
7305 pr_out_fmsg_name(dl, name);
7306 open_json_array(PRINT_JSON, NULL);
7307 } else {
7308 pr_out_fmsg_group_start(dl, name);
7309 }
7310 }
7311
7312 static void pr_out_fmsg_end_array(struct dl *dl)
7313 {
7314 if (dl->json_output)
7315 close_json_array(PRINT_JSON, NULL);
7316 else
7317 pr_out_fmsg_group_end(dl);
7318 }
7319
7320 static int cmd_fmsg_nest(struct fmsg_cb_data *fmsg_data, uint8_t nest_value,
7321 bool start)
7322 {
7323 struct dl *dl = fmsg_data->dl;
7324 uint8_t value = nest_value;
7325 int err;
7326
7327 err = cmd_fmsg_nest_queue(fmsg_data, &value, start);
7328 if (err != MNL_CB_OK)
7329 return err;
7330
7331 switch (value) {
7332 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7333 if (start)
7334 pr_out_fmsg_start_object(dl, &fmsg_data->name);
7335 else
7336 pr_out_fmsg_end_object(dl);
7337 break;
7338 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7339 break;
7340 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7341 if (start)
7342 pr_out_fmsg_start_array(dl, &fmsg_data->name);
7343 else
7344 pr_out_fmsg_end_array(dl);
7345 break;
7346 default:
7347 return -EINVAL;
7348 }
7349 return MNL_CB_OK;
7350 }
7351
7352 static int cmd_fmsg_object_cb(const struct nlmsghdr *nlh, void *data)
7353 {
7354 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7355 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7356 struct fmsg_cb_data *fmsg_data = data;
7357 struct dl *dl = fmsg_data->dl;
7358 struct nlattr *nla_object;
7359 int attr_type;
7360 int err;
7361
7362 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7363 if (!tb[DEVLINK_ATTR_FMSG])
7364 return MNL_CB_ERROR;
7365
7366 mnl_attr_for_each_nested(nla_object, tb[DEVLINK_ATTR_FMSG]) {
7367 attr_type = mnl_attr_get_type(nla_object);
7368 switch (attr_type) {
7369 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7370 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7371 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7372 err = cmd_fmsg_nest(fmsg_data, attr_type, true);
7373 if (err != MNL_CB_OK)
7374 return err;
7375 break;
7376 case DEVLINK_ATTR_FMSG_NEST_END:
7377 err = cmd_fmsg_nest(fmsg_data, attr_type, false);
7378 if (err != MNL_CB_OK)
7379 return err;
7380 break;
7381 case DEVLINK_ATTR_FMSG_OBJ_NAME:
7382 free(fmsg_data->name);
7383 fmsg_data->name = strdup(mnl_attr_get_str(nla_object));
7384 if (!fmsg_data->name)
7385 return -ENOMEM;
7386 break;
7387 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE:
7388 fmsg_data->value_type = mnl_attr_get_u8(nla_object);
7389 break;
7390 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
7391 pr_out_fmsg_name(dl, &fmsg_data->name);
7392 err = fmsg_value_show(dl, fmsg_data->value_type,
7393 nla_object);
7394 if (err != MNL_CB_OK)
7395 return err;
7396 break;
7397 default:
7398 return -EINVAL;
7399 }
7400 }
7401 return MNL_CB_OK;
7402 }
7403
7404 static void cmd_fmsg_init(struct dl *dl, struct fmsg_cb_data *data)
7405 {
7406 /* FMSG is dynamic: opening of an object or array causes a
7407 * newline. JSON starts with an { or [, but plain text should
7408 * not start with a new line. Ensure this by setting
7409 * g_new_line_count to 1: avoiding newline before the first
7410 * print.
7411 */
7412 g_new_line_count = 1;
7413 data->name = NULL;
7414 data->dl = dl;
7415 INIT_LIST_HEAD(&data->entry_list);
7416 }
7417
7418 static int cmd_health_object_common(struct dl *dl, uint8_t cmd, uint16_t flags)
7419 {
7420 struct fmsg_cb_data data;
7421 struct nlmsghdr *nlh;
7422 int err;
7423
7424 nlh = mnlg_msg_prepare(dl->nlg, cmd, flags | NLM_F_REQUEST | NLM_F_ACK);
7425
7426 err = dl_argv_parse_put(nlh, dl,
7427 DL_OPT_HANDLE | DL_OPT_HANDLEP |
7428 DL_OPT_HEALTH_REPORTER_NAME, 0);
7429 if (err)
7430 return err;
7431
7432 cmd_fmsg_init(dl, &data);
7433 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_fmsg_object_cb, &data);
7434 free(data.name);
7435 return err;
7436 }
7437
7438 static int cmd_health_dump_show(struct dl *dl)
7439 {
7440 return cmd_health_object_common(dl,
7441 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
7442 NLM_F_DUMP);
7443 }
7444
7445 static int cmd_health_diagnose(struct dl *dl)
7446 {
7447 return cmd_health_object_common(dl,
7448 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
7449 0);
7450 }
7451
7452 static int cmd_health_test(struct dl *dl)
7453 {
7454 return cmd_health_object_common(dl,
7455 DEVLINK_CMD_HEALTH_REPORTER_TEST,
7456 0);
7457 }
7458
7459 static int cmd_health_recover(struct dl *dl)
7460 {
7461 struct nlmsghdr *nlh;
7462 int err;
7463
7464 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
7465 NLM_F_REQUEST | NLM_F_ACK);
7466
7467 err = dl_argv_parse_put(nlh, dl,
7468 DL_OPT_HANDLE | DL_OPT_HANDLEP |
7469 DL_OPT_HEALTH_REPORTER_NAME, 0);
7470 if (err)
7471 return err;
7472
7473 dl_opts_put(nlh, dl);
7474 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7475 }
7476
7477 enum devlink_health_reporter_state {
7478 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY,
7479 DEVLINK_HEALTH_REPORTER_STATE_ERROR,
7480 };
7481
7482 static const char *health_state_name(uint8_t state)
7483 {
7484 switch (state) {
7485 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY:
7486 return HEALTH_REPORTER_STATE_HEALTHY_STR;
7487 case DEVLINK_HEALTH_REPORTER_STATE_ERROR:
7488 return HEALTH_REPORTER_STATE_ERROR_STR;
7489 default:
7490 return "<unknown state>";
7491 }
7492 }
7493
7494 static void pr_out_dump_reporter_format_logtime(struct dl *dl, const struct nlattr *attr)
7495 {
7496 char dump_date[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
7497 char dump_time[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
7498 uint64_t time_ms = mnl_attr_get_u64(attr);
7499 struct sysinfo s_info;
7500 struct tm *info;
7501 time_t now, sec;
7502 int err;
7503
7504 time(&now);
7505 info = localtime(&now);
7506 err = sysinfo(&s_info);
7507 if (err)
7508 goto out;
7509 /* Subtract uptime in sec from now yields the time of system
7510 * uptime. To this, add time_ms which is the amount of
7511 * milliseconds elapsed between uptime and the dump taken.
7512 */
7513 sec = now - s_info.uptime + time_ms / 1000;
7514 info = localtime(&sec);
7515 out:
7516 strftime(dump_date, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%Y-%m-%d", info);
7517 strftime(dump_time, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%H:%M:%S", info);
7518 check_indent_newline(dl);
7519 print_string(PRINT_ANY, "last_dump_date", "last_dump_date %s", dump_date);
7520 print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time);
7521 }
7522
7523 static void pr_out_dump_report_timestamp(struct dl *dl, const struct nlattr *attr)
7524 {
7525 char dump_date[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
7526 char dump_time[HEALTH_REPORTER_TIMESTAMP_FMT_LEN];
7527 time_t tv_sec;
7528 struct tm *tm;
7529 uint64_t ts;
7530
7531 ts = mnl_attr_get_u64(attr);
7532 tv_sec = ts / 1000000000;
7533 tm = localtime(&tv_sec);
7534
7535 strftime(dump_date, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%Y-%m-%d", tm);
7536 strftime(dump_time, HEALTH_REPORTER_TIMESTAMP_FMT_LEN, "%H:%M:%S", tm);
7537
7538 check_indent_newline(dl);
7539 print_string(PRINT_ANY, "last_dump_date", "last_dump_date %s", dump_date);
7540 print_string(PRINT_ANY, "last_dump_time", " last_dump_time %s", dump_time);
7541 }
7542
7543 static void pr_out_health(struct dl *dl, struct nlattr **tb_health,
7544 bool print_device, bool print_port)
7545 {
7546 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7547 enum devlink_health_reporter_state state;
7548 int err;
7549
7550 err = mnl_attr_parse_nested(tb_health[DEVLINK_ATTR_HEALTH_REPORTER],
7551 attr_cb, tb);
7552 if (err != MNL_CB_OK)
7553 return;
7554
7555 if (!tb[DEVLINK_ATTR_HEALTH_REPORTER_NAME] ||
7556 !tb[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT] ||
7557 !tb[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT] ||
7558 !tb[DEVLINK_ATTR_HEALTH_REPORTER_STATE])
7559 return;
7560
7561 if (!print_device && !print_port)
7562 return;
7563 if (print_port) {
7564 if (!print_device && !tb_health[DEVLINK_ATTR_PORT_INDEX])
7565 return;
7566 else if (tb_health[DEVLINK_ATTR_PORT_INDEX])
7567 pr_out_port_handle_start_arr(dl, tb_health, false);
7568 }
7569 if (print_device) {
7570 if (!print_port && tb_health[DEVLINK_ATTR_PORT_INDEX])
7571 return;
7572 else if (!tb_health[DEVLINK_ATTR_PORT_INDEX])
7573 pr_out_handle_start_arr(dl, tb_health);
7574 }
7575
7576 check_indent_newline(dl);
7577 print_string(PRINT_ANY, "reporter", "reporter %s",
7578 mnl_attr_get_str(tb[DEVLINK_ATTR_HEALTH_REPORTER_NAME]));
7579 if (!dl->json_output) {
7580 __pr_out_newline();
7581 __pr_out_indent_inc();
7582 }
7583 state = mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_STATE]);
7584 check_indent_newline(dl);
7585 print_string(PRINT_ANY, "state", "state %s", health_state_name(state));
7586 pr_out_u64(dl, "error",
7587 mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT]));
7588 pr_out_u64(dl, "recover",
7589 mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT]));
7590 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS])
7591 pr_out_dump_report_timestamp(dl, tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS]);
7592 else if (tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS])
7593 pr_out_dump_reporter_format_logtime(dl, tb[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS]);
7594 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7595 pr_out_u64(dl, "grace_period",
7596 mnl_attr_get_u64(tb[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]));
7597 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7598 print_bool(PRINT_ANY, "auto_recover", " auto_recover %s",
7599 mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]));
7600 if (tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7601 print_bool(PRINT_ANY, "auto_dump", " auto_dump %s",
7602 mnl_attr_get_u8(tb[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]));
7603
7604 __pr_out_indent_dec();
7605 pr_out_handle_end(dl);
7606 }
7607
7608 struct health_ctx {
7609 struct dl *dl;
7610 bool show_device;
7611 bool show_port;
7612 };
7613
7614 static int cmd_health_show_cb(const struct nlmsghdr *nlh, void *data)
7615 {
7616 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7617 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7618 struct health_ctx *ctx = data;
7619 struct dl *dl = ctx->dl;
7620
7621 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7622 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7623 !tb[DEVLINK_ATTR_HEALTH_REPORTER])
7624 return MNL_CB_ERROR;
7625
7626 pr_out_health(dl, tb, ctx->show_device, ctx->show_port);
7627
7628 return MNL_CB_OK;
7629 }
7630
7631 static int __cmd_health_show(struct dl *dl, bool show_device, bool show_port)
7632 {
7633 struct nlmsghdr *nlh;
7634 struct health_ctx ctx = { dl, show_device, show_port };
7635 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7636 int err;
7637
7638 if (dl_argc(dl) == 0)
7639 flags |= NLM_F_DUMP;
7640 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_HEALTH_REPORTER_GET,
7641 flags);
7642
7643 if (dl_argc(dl) > 0) {
7644 ctx.show_port = true;
7645 err = dl_argv_parse_put(nlh, dl,
7646 DL_OPT_HANDLE | DL_OPT_HANDLEP |
7647 DL_OPT_HEALTH_REPORTER_NAME, 0);
7648 if (err)
7649 return err;
7650 }
7651 pr_out_section_start(dl, "health");
7652
7653 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_health_show_cb, &ctx);
7654 pr_out_section_end(dl);
7655 return err;
7656 }
7657
7658 static void cmd_health_help(void)
7659 {
7660 pr_err("Usage: devlink health show [ { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME ]\n");
7661 pr_err(" devlink health recover { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7662 pr_err(" devlink health diagnose { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7663 pr_err(" devlink health test { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7664 pr_err(" devlink health dump show { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7665 pr_err(" devlink health dump clear { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7666 pr_err(" devlink health set { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7667 pr_err(" [ grace_period MSEC ]\n");
7668 pr_err(" [ auto_recover { true | false } ]\n");
7669 pr_err(" [ auto_dump { true | false } ]\n");
7670 }
7671
7672 static int cmd_health(struct dl *dl)
7673 {
7674 if (dl_argv_match(dl, "help")) {
7675 cmd_health_help();
7676 return 0;
7677 } else if (dl_argv_match(dl, "show") ||
7678 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
7679 dl_arg_inc(dl);
7680 return __cmd_health_show(dl, true, true);
7681 } else if (dl_argv_match(dl, "recover")) {
7682 dl_arg_inc(dl);
7683 return cmd_health_recover(dl);
7684 } else if (dl_argv_match(dl, "diagnose")) {
7685 dl_arg_inc(dl);
7686 return cmd_health_diagnose(dl);
7687 } else if (dl_argv_match(dl, "test")) {
7688 dl_arg_inc(dl);
7689 return cmd_health_test(dl);
7690 } else if (dl_argv_match(dl, "dump")) {
7691 dl_arg_inc(dl);
7692 if (dl_argv_match(dl, "show")) {
7693 dl_arg_inc(dl);
7694 return cmd_health_dump_show(dl);
7695 } else if (dl_argv_match(dl, "clear")) {
7696 dl_arg_inc(dl);
7697 return cmd_health_dump_clear(dl);
7698 }
7699 } else if (dl_argv_match(dl, "set")) {
7700 dl_arg_inc(dl);
7701 return cmd_health_set_params(dl);
7702 }
7703 pr_err("Command \"%s\" not found\n", dl_argv(dl));
7704 return -ENOENT;
7705 }
7706
7707 static const char *trap_type_name(uint8_t type)
7708 {
7709 switch (type) {
7710 case DEVLINK_TRAP_TYPE_DROP:
7711 return "drop";
7712 case DEVLINK_TRAP_TYPE_EXCEPTION:
7713 return "exception";
7714 case DEVLINK_TRAP_TYPE_CONTROL:
7715 return "control";
7716 default:
7717 return "<unknown type>";
7718 }
7719 }
7720
7721 static const char *trap_action_name(uint8_t action)
7722 {
7723 switch (action) {
7724 case DEVLINK_TRAP_ACTION_DROP:
7725 return "drop";
7726 case DEVLINK_TRAP_ACTION_TRAP:
7727 return "trap";
7728 case DEVLINK_TRAP_ACTION_MIRROR:
7729 return "mirror";
7730 default:
7731 return "<unknown action>";
7732 }
7733 }
7734
7735 static const char *trap_metadata_name(const struct nlattr *attr)
7736 {
7737 switch (attr->nla_type) {
7738 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT:
7739 return "input_port";
7740 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE:
7741 return "flow_action_cookie";
7742 default:
7743 return "<unknown metadata type>";
7744 }
7745 }
7746 static void pr_out_trap_metadata(struct dl *dl, struct nlattr *attr)
7747 {
7748 struct nlattr *attr_metadata;
7749
7750 pr_out_array_start(dl, "metadata");
7751 mnl_attr_for_each_nested(attr_metadata, attr) {
7752 check_indent_newline(dl);
7753 print_string(PRINT_ANY, NULL, "%s",
7754 trap_metadata_name(attr_metadata));
7755 }
7756 pr_out_array_end(dl);
7757 }
7758
7759 static void pr_out_trap(struct dl *dl, struct nlattr **tb, bool array)
7760 {
7761 uint8_t action = mnl_attr_get_u8(tb[DEVLINK_ATTR_TRAP_ACTION]);
7762 uint8_t type = mnl_attr_get_u8(tb[DEVLINK_ATTR_TRAP_TYPE]);
7763
7764 if (array)
7765 pr_out_handle_start_arr(dl, tb);
7766 else
7767 __pr_out_handle_start(dl, tb, true, false);
7768
7769 check_indent_newline(dl);
7770 print_string(PRINT_ANY, "name", "name %s",
7771 mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_NAME]));
7772 print_string(PRINT_ANY, "type", " type %s", trap_type_name(type));
7773 print_bool(PRINT_ANY, "generic", " generic %s", !!tb[DEVLINK_ATTR_TRAP_GENERIC]);
7774 print_string(PRINT_ANY, "action", " action %s", trap_action_name(action));
7775 print_string(PRINT_ANY, "group", " group %s",
7776 mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME]));
7777 if (dl->verbose)
7778 pr_out_trap_metadata(dl, tb[DEVLINK_ATTR_TRAP_METADATA]);
7779 pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]);
7780 pr_out_handle_end(dl);
7781 }
7782
7783 static int cmd_trap_show_cb(const struct nlmsghdr *nlh, void *data)
7784 {
7785 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7786 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7787 struct dl *dl = data;
7788
7789 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7790 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7791 !tb[DEVLINK_ATTR_TRAP_NAME] || !tb[DEVLINK_ATTR_TRAP_TYPE] ||
7792 !tb[DEVLINK_ATTR_TRAP_ACTION] ||
7793 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] ||
7794 !tb[DEVLINK_ATTR_TRAP_METADATA] || !tb[DEVLINK_ATTR_STATS])
7795 return MNL_CB_ERROR;
7796
7797 pr_out_trap(dl, tb, true);
7798
7799 return MNL_CB_OK;
7800 }
7801
7802 static void cmd_trap_help(void)
7803 {
7804 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7805 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7806 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7807 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7808 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7809 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7810 pr_err(" devlink trap policer show DEV policer POLICER\n");
7811 }
7812
7813 static int cmd_trap_show(struct dl *dl)
7814 {
7815 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7816 struct nlmsghdr *nlh;
7817 int err;
7818
7819 if (dl_argc(dl) == 0)
7820 flags |= NLM_F_DUMP;
7821
7822 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_GET, flags);
7823
7824 if (dl_argc(dl) > 0) {
7825 err = dl_argv_parse_put(nlh, dl,
7826 DL_OPT_HANDLE | DL_OPT_TRAP_NAME, 0);
7827 if (err)
7828 return err;
7829 }
7830
7831 pr_out_section_start(dl, "trap");
7832 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_trap_show_cb, dl);
7833 pr_out_section_end(dl);
7834
7835 return err;
7836 }
7837
7838 static int cmd_trap_set(struct dl *dl)
7839 {
7840 struct nlmsghdr *nlh;
7841 int err;
7842
7843 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_SET,
7844 NLM_F_REQUEST | NLM_F_ACK);
7845
7846 err = dl_argv_parse_put(nlh, dl, DL_OPT_HANDLE | DL_OPT_TRAP_NAME,
7847 DL_OPT_TRAP_ACTION);
7848 if (err)
7849 return err;
7850
7851 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7852 }
7853
7854 static void pr_out_trap_group(struct dl *dl, struct nlattr **tb, bool array)
7855 {
7856 if (array)
7857 pr_out_handle_start_arr(dl, tb);
7858 else
7859 __pr_out_handle_start(dl, tb, true, false);
7860
7861 check_indent_newline(dl);
7862 print_string(PRINT_ANY, "name", "name %s",
7863 mnl_attr_get_str(tb[DEVLINK_ATTR_TRAP_GROUP_NAME]));
7864 print_bool(PRINT_ANY, "generic", " generic %s", !!tb[DEVLINK_ATTR_TRAP_GENERIC]);
7865 if (tb[DEVLINK_ATTR_TRAP_POLICER_ID])
7866 print_uint(PRINT_ANY, "policer", " policer %u",
7867 mnl_attr_get_u32(tb[DEVLINK_ATTR_TRAP_POLICER_ID]));
7868 pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]);
7869 pr_out_handle_end(dl);
7870 }
7871
7872 static int cmd_trap_group_show_cb(const struct nlmsghdr *nlh, void *data)
7873 {
7874 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7875 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7876 struct dl *dl = data;
7877
7878 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7879 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7880 !tb[DEVLINK_ATTR_TRAP_GROUP_NAME] || !tb[DEVLINK_ATTR_STATS])
7881 return MNL_CB_ERROR;
7882
7883 pr_out_trap_group(dl, tb, true);
7884
7885 return MNL_CB_OK;
7886 }
7887
7888 static int cmd_trap_group_show(struct dl *dl)
7889 {
7890 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7891 struct nlmsghdr *nlh;
7892 int err;
7893
7894 if (dl_argc(dl) == 0)
7895 flags |= NLM_F_DUMP;
7896
7897 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_GROUP_GET, flags);
7898
7899 if (dl_argc(dl) > 0) {
7900 err = dl_argv_parse_put(nlh, dl,
7901 DL_OPT_HANDLE | DL_OPT_TRAP_GROUP_NAME,
7902 0);
7903 if (err)
7904 return err;
7905 }
7906
7907 pr_out_section_start(dl, "trap_group");
7908 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_trap_group_show_cb, dl);
7909 pr_out_section_end(dl);
7910
7911 return err;
7912 }
7913
7914 static int cmd_trap_group_set(struct dl *dl)
7915 {
7916 struct nlmsghdr *nlh;
7917 int err;
7918
7919 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_GROUP_SET,
7920 NLM_F_REQUEST | NLM_F_ACK);
7921
7922 err = dl_argv_parse_put(nlh, dl,
7923 DL_OPT_HANDLE | DL_OPT_TRAP_GROUP_NAME,
7924 DL_OPT_TRAP_ACTION | DL_OPT_TRAP_POLICER_ID);
7925 if (err)
7926 return err;
7927
7928 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
7929 }
7930
7931 static int cmd_trap_group(struct dl *dl)
7932 {
7933 if (dl_argv_match(dl, "help")) {
7934 cmd_trap_help();
7935 return 0;
7936 } else if (dl_argv_match(dl, "show") ||
7937 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
7938 dl_arg_inc(dl);
7939 return cmd_trap_group_show(dl);
7940 } else if (dl_argv_match(dl, "set")) {
7941 dl_arg_inc(dl);
7942 return cmd_trap_group_set(dl);
7943 }
7944 pr_err("Command \"%s\" not found\n", dl_argv(dl));
7945 return -ENOENT;
7946 }
7947
7948 static void pr_out_trap_policer(struct dl *dl, struct nlattr **tb, bool array)
7949 {
7950 if (array)
7951 pr_out_handle_start_arr(dl, tb);
7952 else
7953 __pr_out_handle_start(dl, tb, true, false);
7954
7955 check_indent_newline(dl);
7956 print_uint(PRINT_ANY, "policer", "policer %u",
7957 mnl_attr_get_u32(tb[DEVLINK_ATTR_TRAP_POLICER_ID]));
7958 print_u64(PRINT_ANY, "rate", " rate %llu",
7959 mnl_attr_get_u64(tb[DEVLINK_ATTR_TRAP_POLICER_RATE]));
7960 print_u64(PRINT_ANY, "burst", " burst %llu",
7961 mnl_attr_get_u64(tb[DEVLINK_ATTR_TRAP_POLICER_BURST]));
7962 if (tb[DEVLINK_ATTR_STATS])
7963 pr_out_stats(dl, tb[DEVLINK_ATTR_STATS]);
7964 pr_out_handle_end(dl);
7965 }
7966
7967 static int cmd_trap_policer_show_cb(const struct nlmsghdr *nlh, void *data)
7968 {
7969 struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
7970 struct nlattr *tb[DEVLINK_ATTR_MAX + 1] = {};
7971 struct dl *dl = data;
7972
7973 mnl_attr_parse(nlh, sizeof(*genl), attr_cb, tb);
7974 if (!tb[DEVLINK_ATTR_BUS_NAME] || !tb[DEVLINK_ATTR_DEV_NAME] ||
7975 !tb[DEVLINK_ATTR_TRAP_POLICER_ID] ||
7976 !tb[DEVLINK_ATTR_TRAP_POLICER_RATE] ||
7977 !tb[DEVLINK_ATTR_TRAP_POLICER_BURST])
7978 return MNL_CB_ERROR;
7979
7980 pr_out_trap_policer(dl, tb, true);
7981
7982 return MNL_CB_OK;
7983 }
7984
7985 static int cmd_trap_policer_show(struct dl *dl)
7986 {
7987 uint16_t flags = NLM_F_REQUEST | NLM_F_ACK;
7988 struct nlmsghdr *nlh;
7989 int err;
7990
7991 if (dl_argc(dl) == 0)
7992 flags |= NLM_F_DUMP;
7993
7994 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_POLICER_GET, flags);
7995
7996 if (dl_argc(dl) > 0) {
7997 err = dl_argv_parse_put(nlh, dl,
7998 DL_OPT_HANDLE | DL_OPT_TRAP_POLICER_ID,
7999 0);
8000 if (err)
8001 return err;
8002 }
8003
8004 pr_out_section_start(dl, "trap_policer");
8005 err = _mnlg_socket_sndrcv(dl->nlg, nlh, cmd_trap_policer_show_cb, dl);
8006 pr_out_section_end(dl);
8007
8008 return err;
8009 }
8010
8011 static int cmd_trap_policer_set(struct dl *dl)
8012 {
8013 struct nlmsghdr *nlh;
8014 int err;
8015
8016 nlh = mnlg_msg_prepare(dl->nlg, DEVLINK_CMD_TRAP_POLICER_SET,
8017 NLM_F_REQUEST | NLM_F_ACK);
8018
8019 err = dl_argv_parse_put(nlh, dl,
8020 DL_OPT_HANDLE | DL_OPT_TRAP_POLICER_ID,
8021 DL_OPT_TRAP_POLICER_RATE |
8022 DL_OPT_TRAP_POLICER_BURST);
8023 if (err)
8024 return err;
8025
8026 return _mnlg_socket_sndrcv(dl->nlg, nlh, NULL, NULL);
8027 }
8028
8029 static int cmd_trap_policer(struct dl *dl)
8030 {
8031 if (dl_argv_match(dl, "help")) {
8032 cmd_trap_help();
8033 return 0;
8034 } else if (dl_argv_match(dl, "show") ||
8035 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
8036 dl_arg_inc(dl);
8037 return cmd_trap_policer_show(dl);
8038 } else if (dl_argv_match(dl, "set")) {
8039 dl_arg_inc(dl);
8040 return cmd_trap_policer_set(dl);
8041 }
8042 pr_err("Command \"%s\" not found\n", dl_argv(dl));
8043 return -ENOENT;
8044 }
8045
8046 static int cmd_trap(struct dl *dl)
8047 {
8048 if (dl_argv_match(dl, "help")) {
8049 cmd_trap_help();
8050 return 0;
8051 } else if (dl_argv_match(dl, "show") ||
8052 dl_argv_match(dl, "list") || dl_no_arg(dl)) {
8053 dl_arg_inc(dl);
8054 return cmd_trap_show(dl);
8055 } else if (dl_argv_match(dl, "set")) {
8056 dl_arg_inc(dl);
8057 return cmd_trap_set(dl);
8058 } else if (dl_argv_match(dl, "group")) {
8059 dl_arg_inc(dl);
8060 return cmd_trap_group(dl);
8061 } else if (dl_argv_match(dl, "policer")) {
8062 dl_arg_inc(dl);
8063 return cmd_trap_policer(dl);
8064 }
8065 pr_err("Command \"%s\" not found\n", dl_argv(dl));
8066 return -ENOENT;
8067 }
8068
8069 static void help(void)
8070 {
8071 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
8072 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
8073 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
8074 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
8075 }
8076
8077 static int dl_cmd(struct dl *dl, int argc, char **argv)
8078 {
8079 dl->argc = argc;
8080 dl->argv = argv;
8081
8082 if (dl_argv_match(dl, "help") || dl_no_arg(dl)) {
8083 help();
8084 return 0;
8085 } else if (dl_argv_match(dl, "dev")) {
8086 dl_arg_inc(dl);
8087 return cmd_dev(dl);
8088 } else if (dl_argv_match(dl, "port")) {
8089 dl_arg_inc(dl);
8090 return cmd_port(dl);
8091 } else if (dl_argv_match(dl, "sb")) {
8092 dl_arg_inc(dl);
8093 return cmd_sb(dl);
8094 } else if (dl_argv_match(dl, "monitor")) {
8095 dl_arg_inc(dl);
8096 return cmd_mon(dl);
8097 } else if (dl_argv_match(dl, "dpipe")) {
8098 dl_arg_inc(dl);
8099 return cmd_dpipe(dl);
8100 } else if (dl_argv_match(dl, "resource")) {
8101 dl_arg_inc(dl);
8102 return cmd_resource(dl);
8103 } else if (dl_argv_match(dl, "region")) {
8104 dl_arg_inc(dl);
8105 return cmd_region(dl);
8106 } else if (dl_argv_match(dl, "health")) {
8107 dl_arg_inc(dl);
8108 return cmd_health(dl);
8109 } else if (dl_argv_match(dl, "trap")) {
8110 dl_arg_inc(dl);
8111 return cmd_trap(dl);
8112 }
8113 pr_err("Object \"%s\" not found\n", dl_argv(dl));
8114 return -ENOENT;
8115 }
8116
8117 static int dl_init(struct dl *dl)
8118 {
8119 int err;
8120
8121 dl->nlg = mnlg_socket_open(DEVLINK_GENL_NAME, DEVLINK_GENL_VERSION);
8122 if (!dl->nlg) {
8123 pr_err("Failed to connect to devlink Netlink\n");
8124 return -errno;
8125 }
8126
8127 err = ifname_map_init(dl);
8128 if (err) {
8129 pr_err("Failed to create index map\n");
8130 goto err_ifname_map_create;
8131 }
8132 new_json_obj_plain(dl->json_output);
8133 return 0;
8134
8135 err_ifname_map_create:
8136 mnlg_socket_close(dl->nlg);
8137 return err;
8138 }
8139
8140 static void dl_fini(struct dl *dl)
8141 {
8142 delete_json_obj_plain();
8143 ifname_map_fini(dl);
8144 mnlg_socket_close(dl->nlg);
8145 }
8146
8147 static struct dl *dl_alloc(void)
8148 {
8149 struct dl *dl;
8150
8151 dl = calloc(1, sizeof(*dl));
8152 if (!dl)
8153 return NULL;
8154 return dl;
8155 }
8156
8157 static void dl_free(struct dl *dl)
8158 {
8159 free(dl);
8160 }
8161
8162 static int dl_batch_cmd(int argc, char *argv[], void *data)
8163 {
8164 struct dl *dl = data;
8165
8166 return dl_cmd(dl, argc, argv);
8167 }
8168
8169 static int dl_batch(struct dl *dl, const char *name, bool force)
8170 {
8171 return do_batch(name, force, dl_batch_cmd, dl);
8172 }
8173
8174 int main(int argc, char **argv)
8175 {
8176 static const struct option long_options[] = {
8177 { "Version", no_argument, NULL, 'V' },
8178 { "force", no_argument, NULL, 'f' },
8179 { "batch", required_argument, NULL, 'b' },
8180 { "no-nice-names", no_argument, NULL, 'n' },
8181 { "json", no_argument, NULL, 'j' },
8182 { "pretty", no_argument, NULL, 'p' },
8183 { "verbose", no_argument, NULL, 'v' },
8184 { "statistics", no_argument, NULL, 's' },
8185 { "Netns", required_argument, NULL, 'N' },
8186 { NULL, 0, NULL, 0 }
8187 };
8188 const char *batch_file = NULL;
8189 bool force = false;
8190 struct dl *dl;
8191 int opt;
8192 int err;
8193 int ret;
8194
8195 dl = dl_alloc();
8196 if (!dl) {
8197 pr_err("Failed to allocate memory for devlink\n");
8198 return EXIT_FAILURE;
8199 }
8200
8201 while ((opt = getopt_long(argc, argv, "Vfb:njpvsN:",
8202 long_options, NULL)) >= 0) {
8203
8204 switch (opt) {
8205 case 'V':
8206 printf("devlink utility, iproute2-%s\n", version);
8207 ret = EXIT_SUCCESS;
8208 goto dl_free;
8209 case 'f':
8210 force = true;
8211 break;
8212 case 'b':
8213 batch_file = optarg;
8214 break;
8215 case 'n':
8216 dl->no_nice_names = true;
8217 break;
8218 case 'j':
8219 dl->json_output = true;
8220 break;
8221 case 'p':
8222 pretty = true;
8223 break;
8224 case 'v':
8225 dl->verbose = true;
8226 break;
8227 case 's':
8228 dl->stats = true;
8229 break;
8230 case 'N':
8231 if (netns_switch(optarg)) {
8232 ret = EXIT_FAILURE;
8233 goto dl_free;
8234 }
8235 break;
8236 default:
8237 pr_err("Unknown option.\n");
8238 help();
8239 ret = EXIT_FAILURE;
8240 goto dl_free;
8241 }
8242 }
8243
8244 argc -= optind;
8245 argv += optind;
8246
8247 err = dl_init(dl);
8248 if (err) {
8249 ret = EXIT_FAILURE;
8250 goto dl_free;
8251 }
8252
8253 if (batch_file)
8254 err = dl_batch(dl, batch_file, force);
8255 else
8256 err = dl_cmd(dl, argc, argv);
8257
8258 if (err) {
8259 ret = EXIT_FAILURE;
8260 goto dl_fini;
8261 }
8262
8263 ret = EXIT_SUCCESS;
8264
8265 dl_fini:
8266 dl_fini(dl);
8267 dl_free:
8268 dl_free(dl);
8269
8270 return ret;
8271 }