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