2 * devlink.c Devlink tool
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.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <sys/sysinfo.h>
27 #define _LINUX_SYSINFO_H /* avoid collision with musl header */
28 #include <linux/genetlink.h>
29 #include <linux/devlink.h>
30 #include <linux/netlink.h>
31 #include <libmnl/libmnl.h>
32 #include <netinet/ether.h>
33 #include <sys/select.h>
34 #include <sys/socket.h>
35 #include <sys/types.h>
41 #include "json_print.h"
43 #include "namespace.h"
45 #define ESWITCH_MODE_LEGACY "legacy"
46 #define ESWITCH_MODE_SWITCHDEV "switchdev"
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"
52 #define ESWITCH_ENCAP_MODE_NONE "none"
53 #define ESWITCH_ENCAP_MODE_BASIC "basic"
55 #define PARAM_CMODE_RUNTIME_STR "runtime"
56 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
57 #define PARAM_CMODE_PERMANENT_STR "permanent"
58 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
60 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
61 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
62 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
64 static int g_new_line_count
;
65 static int g_indent_level
;
66 static bool g_indent_newline
;
68 #define INDENT_STR_STEP 2
69 #define INDENT_STR_MAXLEN 32
70 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
72 static void __attribute__((format(printf
, 1, 2)))
73 pr_err(const char *fmt
, ...)
78 vfprintf(stderr
, fmt
, ap
);
82 static void __attribute__((format(printf
, 1, 2)))
83 pr_out(const char *fmt
, ...)
87 if (g_indent_newline
) {
88 printf("%s", g_indent_str
);
89 g_indent_newline
= false;
97 static void __attribute__((format(printf
, 2, 3)))
98 pr_out_sp(unsigned int num
, const char *fmt
, ...)
104 ret
= vprintf(fmt
, ap
);
108 printf("%*s", num
- ret
, "");
109 g_new_line_count
= 0; \
112 static void __attribute__((format(printf
, 1, 2)))
113 pr_out_tty(const char *fmt
, ...)
117 if (!isatty(STDOUT_FILENO
))
124 static void __pr_out_indent_inc(void)
126 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
128 g_indent_level
+= INDENT_STR_STEP
;
129 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
130 g_indent_str
[g_indent_level
] = '\0';
133 static void __pr_out_indent_dec(void)
135 if (g_indent_level
- INDENT_STR_STEP
< 0)
137 g_indent_level
-= INDENT_STR_STEP
;
138 g_indent_str
[g_indent_level
] = '\0';
141 static void __pr_out_newline(void)
143 if (g_new_line_count
< 1) {
145 g_indent_newline
= true;
150 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
151 mnl_cb_t data_cb
, void *data
)
155 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
157 pr_err("devlink answers: %s\n", strerror(errno
));
163 static void dummy_signal_handler(int signum
)
167 static int _mnlg_socket_recv_run_intr(struct mnlg_socket
*nlg
,
168 mnl_cb_t data_cb
, void *data
)
170 struct sigaction act
, oact
;
173 act
.sa_handler
= dummy_signal_handler
;
174 sigemptyset(&act
.sa_mask
);
175 act
.sa_flags
= SA_NODEFER
;
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
));
187 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
188 const struct nlmsghdr
*nlh
)
192 err
= mnlg_socket_send(nlg
, nlh
);
194 pr_err("Failed to call mnlg_socket_send\n");
200 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
201 const struct nlmsghdr
*nlh
,
202 mnl_cb_t data_cb
, void *data
)
206 err
= _mnlg_socket_send(nlg
, nlh
);
209 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
212 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
213 const char *group_name
)
217 err
= mnlg_socket_group_add(nlg
, group_name
);
219 pr_err("Failed to call mnlg_socket_group_add\n");
226 struct list_head list
;
233 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
234 const char *dev_name
,
238 struct ifname_map
*ifname_map
;
240 ifname_map
= calloc(1, sizeof(*ifname_map
));
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
);
258 static void ifname_map_free(struct ifname_map
*ifname_map
)
260 free(ifname_map
->ifname
);
261 free(ifname_map
->dev_name
);
262 free(ifname_map
->bus_name
);
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)
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)
277 #define DL_OPT_ESWITCH_MODE BIT(11)
278 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
279 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
280 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
281 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
282 #define DL_OPT_RESOURCE_PATH BIT(16)
283 #define DL_OPT_RESOURCE_SIZE BIT(17)
284 #define DL_OPT_PARAM_NAME BIT(18)
285 #define DL_OPT_PARAM_VALUE BIT(19)
286 #define DL_OPT_PARAM_CMODE BIT(20)
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)
291 #define DL_OPT_FLASH_FILE_NAME BIT(25)
292 #define DL_OPT_FLASH_COMPONENT BIT(26)
293 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
294 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
295 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
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)
299 #define DL_OPT_NETNS BIT(33)
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)
303 #define DL_OPT_HEALTH_REPORTER_AUTO_DUMP BIT(37)
304 #define DL_OPT_PORT_FUNCTION_HW_ADDR BIT(38)
307 uint64_t present
; /* flags of present items */
311 enum devlink_port_type port_type
;
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
;
320 enum devlink_eswitch_mode eswitch_mode
;
321 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
322 const char *dpipe_table_name
;
323 bool dpipe_counters_enabled
;
324 enum devlink_eswitch_encap_mode eswitch_encap_mode
;
325 const char *resource_path
;
326 uint64_t resource_size
;
327 uint32_t resource_id
;
328 bool resource_id_valid
;
329 const char *param_name
;
330 const char *param_value
;
331 enum devlink_param_cmode cmode
;
333 uint32_t region_snapshot_id
;
334 uint64_t region_address
;
335 uint64_t region_length
;
336 const char *flash_file_name
;
337 const char *flash_component
;
338 const char *reporter_name
;
339 uint64_t reporter_graceful_period
;
340 bool reporter_auto_recover
;
341 bool reporter_auto_dump
;
342 const char *trap_name
;
343 const char *trap_group_name
;
344 enum devlink_trap_action trap_action
;
347 uint32_t trap_policer_id
;
348 uint64_t trap_policer_rate
;
349 uint64_t trap_policer_burst
;
350 char port_function_hw_addr
[MAX_ADDR_LEN
];
351 uint32_t port_function_hw_addr_len
;
355 struct mnlg_socket
*nlg
;
356 struct list_head ifname_map_list
;
373 static int dl_argc(struct dl
*dl
)
378 static char *dl_argv(struct dl
*dl
)
380 if (dl_argc(dl
) == 0)
385 static void dl_arg_inc(struct dl
*dl
)
387 if (dl_argc(dl
) == 0)
393 static void dl_arg_dec(struct dl
*dl
)
399 static char *dl_argv_next(struct dl
*dl
)
403 if (dl_argc(dl
) == 0)
411 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
413 if (index
>= dl_argc(dl
))
415 return dl
->argv
[index
];
418 static int strcmpx(const char *str1
, const char *str2
)
420 if (strlen(str1
) > strlen(str2
))
422 return strncmp(str1
, str2
, strlen(str1
));
425 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
427 if (dl_argc(dl
) == 0)
429 return strcmpx(dl_argv(dl
), pattern
) == 0;
432 static bool dl_no_arg(struct dl
*dl
)
434 return dl_argc(dl
) == 0;
437 static void __pr_out_indent_newline(struct dl
*dl
)
439 if (!g_indent_newline
&& !dl
->json_output
)
443 static bool is_binary_eol(int i
)
448 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
454 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
456 pr_out("%02x ", data
[i
]);
458 if (!dl
->json_output
&& is_binary_eol(i
))
461 if (!dl
->json_output
&& !is_binary_eol(i
))
465 static void pr_out_name(struct dl
*dl
, const char *name
)
467 __pr_out_indent_newline(dl
);
469 print_string(PRINT_JSON
, name
, NULL
, NULL
);
474 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
476 __pr_out_indent_newline(dl
);
477 if (val
== (uint64_t) -1)
478 return print_string_name_value(name
, "unlimited");
481 print_u64(PRINT_JSON
, name
, NULL
, val
);
483 pr_out("%s %"PRIu64
, name
, val
);
486 static void pr_out_section_start(struct dl
*dl
, const char *name
)
488 if (dl
->json_output
) {
489 open_json_object(NULL
);
490 open_json_object(name
);
494 static void pr_out_section_end(struct dl
*dl
)
496 if (dl
->json_output
) {
497 if (dl
->arr_last
.present
)
498 close_json_array(PRINT_JSON
, NULL
);
504 static void pr_out_array_start(struct dl
*dl
, const char *name
)
506 if (dl
->json_output
) {
507 open_json_array(PRINT_JSON
, name
);
509 __pr_out_indent_inc();
512 __pr_out_indent_inc();
517 static void pr_out_array_end(struct dl
*dl
)
519 if (dl
->json_output
) {
520 close_json_array(PRINT_JSON
, NULL
);
522 __pr_out_indent_dec();
523 __pr_out_indent_dec();
527 static void pr_out_object_start(struct dl
*dl
, const char *name
)
529 if (dl
->json_output
) {
530 open_json_object(name
);
532 __pr_out_indent_inc();
535 __pr_out_indent_inc();
540 static void pr_out_object_end(struct dl
*dl
)
542 if (dl
->json_output
) {
545 __pr_out_indent_dec();
546 __pr_out_indent_dec();
550 static void pr_out_entry_start(struct dl
*dl
)
553 open_json_object(NULL
);
556 static void pr_out_entry_end(struct dl
*dl
)
564 static void check_indent_newline(struct dl
*dl
)
566 __pr_out_indent_newline(dl
);
568 if (g_indent_newline
&& !is_json_context()) {
569 printf("%s", g_indent_str
);
570 g_indent_newline
= false;
572 g_new_line_count
= 0;
575 static 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
,
584 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
585 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
586 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
587 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
588 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
589 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
590 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
591 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
592 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
593 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
594 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
595 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
596 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
597 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
598 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
599 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
600 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
601 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
602 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
603 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
604 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
605 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
606 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
607 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
608 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
609 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
610 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
611 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
612 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
613 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
614 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
615 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
616 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
617 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
618 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
619 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
620 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
621 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
622 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
623 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
624 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
625 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
626 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
627 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
628 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
629 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
630 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
631 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
632 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
633 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
634 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
635 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
636 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
637 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
638 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
639 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
640 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
641 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
642 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
643 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
644 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
645 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
646 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
647 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
648 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
649 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
650 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
651 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
652 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
653 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
654 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
655 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
656 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
657 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
658 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
659 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
660 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
661 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
662 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
663 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
664 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
665 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
666 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
667 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
668 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
669 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
670 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
671 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
672 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
673 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
674 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
675 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
676 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
677 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
680 static const enum mnl_attr_data_type
681 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
682 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
683 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
684 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
687 static int attr_cb(const struct nlattr
*attr
, void *data
)
689 const struct nlattr
**tb
= data
;
692 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
695 type
= mnl_attr_get_type(attr
);
696 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
703 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
705 const struct nlattr
**tb
= data
;
708 /* Allow the tool to work on top of newer kernels that might contain
711 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
714 type
= mnl_attr_get_type(attr
);
715 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
722 static const enum mnl_attr_data_type
723 devlink_function_policy
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {
724 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
] = MNL_TYPE_BINARY
,
727 static int function_attr_cb(const struct nlattr
*attr
, void *data
)
729 const struct nlattr
**tb
= data
;
732 /* Allow the tool to work on top of newer kernels that might contain
735 if (mnl_attr_type_valid(attr
, DEVLINK_PORT_FUNCTION_ATTR_MAX
) < 0)
738 type
= mnl_attr_get_type(attr
);
739 if (mnl_attr_validate(attr
, devlink_function_policy
[type
]) < 0)
746 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
748 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
749 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
750 struct dl
*dl
= data
;
751 struct ifname_map
*ifname_map
;
752 const char *bus_name
;
753 const char *dev_name
;
754 uint32_t port_ifindex
;
755 const char *port_ifname
;
757 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
758 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
759 !tb
[DEVLINK_ATTR_PORT_INDEX
])
762 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
765 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
766 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
767 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
768 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
769 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
770 port_ifindex
, port_ifname
);
773 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
778 static void ifname_map_fini(struct dl
*dl
)
780 struct ifname_map
*ifname_map
, *tmp
;
782 list_for_each_entry_safe(ifname_map
, tmp
,
783 &dl
->ifname_map_list
, list
) {
784 list_del(&ifname_map
->list
);
785 ifname_map_free(ifname_map
);
789 static int ifname_map_init(struct dl
*dl
)
791 struct nlmsghdr
*nlh
;
794 INIT_LIST_HEAD(&dl
->ifname_map_list
);
796 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
797 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
799 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
807 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
808 char **p_bus_name
, char **p_dev_name
,
809 uint32_t *p_port_index
)
811 struct ifname_map
*ifname_map
;
813 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
814 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
815 *p_bus_name
= ifname_map
->bus_name
;
816 *p_dev_name
= ifname_map
->dev_name
;
817 *p_port_index
= ifname_map
->port_index
;
824 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
825 const char *dev_name
, uint32_t port_index
,
828 struct ifname_map
*ifname_map
;
830 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
831 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
832 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
833 port_index
== ifname_map
->port_index
) {
834 *p_ifname
= ifname_map
->ifname
;
841 static unsigned int strslashcount(char *str
)
843 unsigned int count
= 0;
846 while ((pos
= strchr(pos
, '/'))) {
853 static int strslashrsplit(char *str
, char **before
, char **after
)
857 slash
= strrchr(str
, '/');
866 static int strtouint64_t(const char *str
, uint64_t *p_val
)
869 unsigned long long int val
;
871 val
= strtoull(str
, &endptr
, 10);
872 if (endptr
== str
|| *endptr
!= '\0')
880 static int strtouint32_t(const char *str
, uint32_t *p_val
)
883 unsigned long int val
;
885 val
= strtoul(str
, &endptr
, 10);
886 if (endptr
== str
|| *endptr
!= '\0')
894 static int strtouint16_t(const char *str
, uint16_t *p_val
)
897 unsigned long int val
;
899 val
= strtoul(str
, &endptr
, 10);
900 if (endptr
== str
|| *endptr
!= '\0')
908 static int strtouint8_t(const char *str
, uint8_t *p_val
)
911 unsigned long int val
;
913 val
= strtoul(str
, &endptr
, 10);
914 if (endptr
== str
|| *endptr
!= '\0')
922 static int strtobool(const char *str
, bool *p_val
)
926 if (!strcmp(str
, "true") || !strcmp(str
, "1") ||
927 !strcmp(str
, "enable"))
929 else if (!strcmp(str
, "false") || !strcmp(str
, "0") ||
930 !strcmp(str
, "disable"))
938 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
940 strslashrsplit(str
, p_bus_name
, p_dev_name
);
944 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
946 char *str
= dl_argv_next(dl
);
949 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
952 if (strslashcount(str
) != 1) {
953 pr_err("Wrong devlink identification string format.\n");
954 pr_err("Expected \"bus_name/dev_name\".\n");
957 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
960 static int __dl_argv_handle_port(char *str
,
961 char **p_bus_name
, char **p_dev_name
,
962 uint32_t *p_port_index
)
968 err
= strslashrsplit(str
, &handlestr
, &portstr
);
970 pr_err("Port identification \"%s\" is invalid\n", str
);
973 err
= strtouint32_t(portstr
, p_port_index
);
975 pr_err("Port index \"%s\" is not a number or not within range\n",
979 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
981 pr_err("Port identification \"%s\" is invalid\n", str
);
987 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
988 char **p_bus_name
, char **p_dev_name
,
989 uint32_t *p_port_index
)
993 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
996 pr_err("Netdevice \"%s\" not found\n", str
);
1002 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
1003 char **p_dev_name
, uint32_t *p_port_index
)
1005 char *str
= dl_argv_next(dl
);
1006 unsigned int slash_count
;
1009 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
1012 slash_count
= strslashcount(str
);
1013 switch (slash_count
) {
1015 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
1016 p_dev_name
, p_port_index
);
1018 return __dl_argv_handle_port(str
, p_bus_name
,
1019 p_dev_name
, p_port_index
);
1021 pr_err("Wrong port identification string format.\n");
1022 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1027 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
1028 char **p_dev_name
, uint32_t *p_port_index
,
1029 uint64_t *p_handle_bit
)
1031 char *str
= dl_argv_next(dl
);
1032 unsigned int slash_count
;
1036 pr_err("One of following identifications expected:\n"
1037 "Devlink identification (\"bus_name/dev_name\")\n"
1038 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
1041 slash_count
= strslashcount(str
);
1042 if (slash_count
== 1) {
1043 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
1046 *p_handle_bit
= DL_OPT_HANDLE
;
1047 } else if (slash_count
== 2) {
1048 err
= __dl_argv_handle_port(str
, p_bus_name
,
1049 p_dev_name
, p_port_index
);
1052 *p_handle_bit
= DL_OPT_HANDLEP
;
1053 } else if (slash_count
== 0) {
1054 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
1055 p_dev_name
, p_port_index
);
1058 *p_handle_bit
= DL_OPT_HANDLEP
;
1060 pr_err("Wrong port identification string format.\n");
1061 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1067 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
1068 char **p_dev_name
, char **p_region
)
1073 err
= strslashrsplit(str
, &handlestr
, p_region
);
1075 pr_err("Region identification \"%s\" is invalid\n", str
);
1078 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
1080 pr_err("Region identification \"%s\" is invalid\n", str
);
1086 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
1087 char **p_dev_name
, char **p_region
)
1089 char *str
= dl_argv_next(dl
);
1090 unsigned int slash_count
;
1093 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
1097 slash_count
= strslashcount(str
);
1098 if (slash_count
!= 2) {
1099 pr_err("Wrong region identification string format.\n");
1100 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
1104 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
1107 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
1109 char *str
= dl_argv_next(dl
);
1113 pr_err("Unsigned number argument expected\n");
1117 err
= strtouint64_t(str
, p_val
);
1119 pr_err("\"%s\" is not a number or not within range\n", str
);
1125 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
1127 char *str
= dl_argv_next(dl
);
1131 pr_err("Unsigned number argument expected\n");
1135 err
= strtouint32_t(str
, p_val
);
1137 pr_err("\"%s\" is not a number or not within range\n", str
);
1143 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
1145 char *str
= dl_argv_next(dl
);
1149 pr_err("Unsigned number argument expected\n");
1153 err
= strtouint16_t(str
, p_val
);
1155 pr_err("\"%s\" is not a number or not within range\n", str
);
1161 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
1163 char *str
= dl_argv_next(dl
);
1167 pr_err("Boolean argument expected\n");
1171 err
= strtobool(str
, p_val
);
1173 pr_err("\"%s\" is not a valid boolean value\n", str
);
1179 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
1181 const char *str
= dl_argv_next(dl
);
1184 pr_err("String parameter expected\n");
1191 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1193 if (strcmp(typestr
, "auto") == 0) {
1194 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1195 } else if (strcmp(typestr
, "eth") == 0) {
1196 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1197 } else if (strcmp(typestr
, "ib") == 0) {
1198 *p_type
= DEVLINK_PORT_TYPE_IB
;
1200 pr_err("Unknown port type \"%s\"\n", typestr
);
1206 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1208 if (strcmp(typestr
, "ingress") == 0) {
1209 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1210 } else if (strcmp(typestr
, "egress") == 0) {
1211 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1213 pr_err("Unknown pool type \"%s\"\n", typestr
);
1219 static int threshold_type_get(const char *typestr
,
1220 enum devlink_sb_threshold_type
*p_type
)
1222 if (strcmp(typestr
, "static") == 0) {
1223 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1224 } else if (strcmp(typestr
, "dynamic") == 0) {
1225 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1227 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1233 static int eswitch_mode_get(const char *typestr
,
1234 enum devlink_eswitch_mode
*p_mode
)
1236 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1237 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1238 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1239 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1241 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1247 static int eswitch_inline_mode_get(const char *typestr
,
1248 enum devlink_eswitch_inline_mode
*p_mode
)
1250 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1251 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1252 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1253 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1254 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1255 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1256 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1257 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1259 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1266 eswitch_encap_mode_get(const char *typestr
,
1267 enum devlink_eswitch_encap_mode
*p_encap_mode
)
1269 /* The initial implementation incorrectly accepted "enable"/"disable".
1270 * Carry it to maintain backward compatibility.
1272 if (strcmp(typestr
, "disable") == 0 ||
1273 strcmp(typestr
, ESWITCH_ENCAP_MODE_NONE
) == 0) {
1274 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1275 } else if (strcmp(typestr
, "enable") == 0 ||
1276 strcmp(typestr
, ESWITCH_ENCAP_MODE_BASIC
) == 0) {
1277 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1279 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1285 static int param_cmode_get(const char *cmodestr
,
1286 enum devlink_param_cmode
*cmode
)
1288 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1289 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1290 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1291 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1292 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1293 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1295 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1301 static int trap_action_get(const char *actionstr
,
1302 enum devlink_trap_action
*p_action
)
1304 if (strcmp(actionstr
, "drop") == 0) {
1305 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1306 } else if (strcmp(actionstr
, "trap") == 0) {
1307 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1308 } else if (strcmp(actionstr
, "mirror") == 0) {
1309 *p_action
= DEVLINK_TRAP_ACTION_MIRROR
;
1311 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1317 static int hw_addr_parse(const char *addrstr
, char *hw_addr
, uint32_t *len
)
1321 alen
= ll_addr_a2n(hw_addr
, MAX_ADDR_LEN
, addrstr
);
1328 struct dl_args_metadata
{
1330 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1333 static const struct dl_args_metadata dl_args_required
[] = {
1334 {DL_OPT_PORT_TYPE
, "Port type not set."},
1335 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1336 {DL_OPT_SB_POOL
, "Pool index option expected."},
1337 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1338 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1339 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1340 {DL_OPT_SB_TH
, "Threshold option expected."},
1341 {DL_OPT_SB_TC
, "TC index option expected."},
1342 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1343 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1344 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1345 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1346 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1347 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1348 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1349 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1350 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1351 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1352 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1353 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1354 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1355 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1356 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1357 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1358 {DL_OPT_PORT_FUNCTION_HW_ADDR
, "Port function's hardware address is expected."},
1361 static int dl_args_finding_required_validate(uint64_t o_required
,
1367 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1368 o_flag
= dl_args_required
[i
].o_flag
;
1369 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1370 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1374 if (o_required
& ~o_found
) {
1375 pr_err("BUG: unknown argument required but not found\n");
1381 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1382 uint64_t o_optional
)
1384 struct dl_opts
*opts
= &dl
->opts
;
1385 uint64_t o_all
= o_required
| o_optional
;
1386 uint64_t o_found
= 0;
1389 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1390 uint64_t handle_bit
;
1392 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1393 &opts
->port_index
, &handle_bit
);
1396 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1397 o_found
|= handle_bit
;
1398 } else if (o_required
& DL_OPT_HANDLE
) {
1399 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1402 o_found
|= DL_OPT_HANDLE
;
1403 } else if (o_required
& DL_OPT_HANDLEP
) {
1404 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1408 o_found
|= DL_OPT_HANDLEP
;
1409 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1410 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1412 &opts
->region_name
);
1415 o_found
|= DL_OPT_HANDLE_REGION
;
1418 while (dl_argc(dl
)) {
1419 if (dl_argv_match(dl
, "type") &&
1420 (o_all
& DL_OPT_PORT_TYPE
)) {
1421 const char *typestr
;
1424 err
= dl_argv_str(dl
, &typestr
);
1427 err
= port_type_get(typestr
, &opts
->port_type
);
1430 o_found
|= DL_OPT_PORT_TYPE
;
1431 } else if (dl_argv_match(dl
, "count") &&
1432 (o_all
& DL_OPT_PORT_COUNT
)) {
1434 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1437 o_found
|= DL_OPT_PORT_COUNT
;
1438 } else if (dl_argv_match(dl
, "sb") &&
1439 (o_all
& DL_OPT_SB
)) {
1441 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1444 o_found
|= DL_OPT_SB
;
1445 } else if (dl_argv_match(dl
, "pool") &&
1446 (o_all
& DL_OPT_SB_POOL
)) {
1448 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1451 o_found
|= DL_OPT_SB_POOL
;
1452 } else if (dl_argv_match(dl
, "size") &&
1453 (o_all
& DL_OPT_SB_SIZE
)) {
1455 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1458 o_found
|= DL_OPT_SB_SIZE
;
1459 } else if (dl_argv_match(dl
, "type") &&
1460 (o_all
& DL_OPT_SB_TYPE
)) {
1461 const char *typestr
;
1464 err
= dl_argv_str(dl
, &typestr
);
1467 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1470 o_found
|= DL_OPT_SB_TYPE
;
1471 } else if (dl_argv_match(dl
, "thtype") &&
1472 (o_all
& DL_OPT_SB_THTYPE
)) {
1473 const char *typestr
;
1476 err
= dl_argv_str(dl
, &typestr
);
1479 err
= threshold_type_get(typestr
,
1480 &opts
->sb_pool_thtype
);
1483 o_found
|= DL_OPT_SB_THTYPE
;
1484 } else if (dl_argv_match(dl
, "th") &&
1485 (o_all
& DL_OPT_SB_TH
)) {
1487 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1490 o_found
|= DL_OPT_SB_TH
;
1491 } else if (dl_argv_match(dl
, "tc") &&
1492 (o_all
& DL_OPT_SB_TC
)) {
1494 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1497 o_found
|= DL_OPT_SB_TC
;
1498 } else if (dl_argv_match(dl
, "mode") &&
1499 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1500 const char *typestr
;
1503 err
= dl_argv_str(dl
, &typestr
);
1506 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1509 o_found
|= DL_OPT_ESWITCH_MODE
;
1510 } else if (dl_argv_match(dl
, "inline-mode") &&
1511 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1512 const char *typestr
;
1515 err
= dl_argv_str(dl
, &typestr
);
1518 err
= eswitch_inline_mode_get(
1519 typestr
, &opts
->eswitch_inline_mode
);
1522 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1523 } else if (dl_argv_match(dl
, "name") &&
1524 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1526 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1529 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1530 } else if ((dl_argv_match(dl
, "counters") ||
1531 dl_argv_match(dl
, "counters_enabled")) &&
1532 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1534 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1537 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1538 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1539 dl_argv_match(dl
, "encap-mode")) &&
1540 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1541 const char *typestr
;
1544 err
= dl_argv_str(dl
, &typestr
);
1547 err
= eswitch_encap_mode_get(typestr
,
1548 &opts
->eswitch_encap_mode
);
1551 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1552 } else if (dl_argv_match(dl
, "path") &&
1553 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1555 err
= dl_argv_str(dl
, &opts
->resource_path
);
1558 o_found
|= DL_OPT_RESOURCE_PATH
;
1559 } else if (dl_argv_match(dl
, "size") &&
1560 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1562 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1565 o_found
|= DL_OPT_RESOURCE_SIZE
;
1566 } else if (dl_argv_match(dl
, "name") &&
1567 (o_all
& DL_OPT_PARAM_NAME
)) {
1569 err
= dl_argv_str(dl
, &opts
->param_name
);
1572 o_found
|= DL_OPT_PARAM_NAME
;
1573 } else if (dl_argv_match(dl
, "value") &&
1574 (o_all
& DL_OPT_PARAM_VALUE
)) {
1576 err
= dl_argv_str(dl
, &opts
->param_value
);
1579 o_found
|= DL_OPT_PARAM_VALUE
;
1580 } else if (dl_argv_match(dl
, "cmode") &&
1581 (o_all
& DL_OPT_PARAM_CMODE
)) {
1582 const char *cmodestr
;
1585 err
= dl_argv_str(dl
, &cmodestr
);
1588 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1591 o_found
|= DL_OPT_PARAM_CMODE
;
1592 } else if (dl_argv_match(dl
, "snapshot") &&
1593 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1595 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1598 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1599 } else if (dl_argv_match(dl
, "address") &&
1600 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1602 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1605 o_found
|= DL_OPT_REGION_ADDRESS
;
1606 } else if (dl_argv_match(dl
, "length") &&
1607 (o_all
& DL_OPT_REGION_LENGTH
)) {
1609 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1612 o_found
|= DL_OPT_REGION_LENGTH
;
1613 } else if (dl_argv_match(dl
, "file") &&
1614 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1616 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1619 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1620 } else if (dl_argv_match(dl
, "component") &&
1621 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1623 err
= dl_argv_str(dl
, &opts
->flash_component
);
1626 o_found
|= DL_OPT_FLASH_COMPONENT
;
1627 } else if (dl_argv_match(dl
, "reporter") &&
1628 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1630 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1633 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1634 } else if (dl_argv_match(dl
, "grace_period") &&
1635 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1637 err
= dl_argv_uint64_t(dl
,
1638 &opts
->reporter_graceful_period
);
1641 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1642 } else if (dl_argv_match(dl
, "auto_recover") &&
1643 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1645 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1648 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1649 } else if (dl_argv_match(dl
, "auto_dump") &&
1650 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)) {
1652 err
= dl_argv_bool(dl
, &opts
->reporter_auto_dump
);
1655 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_DUMP
;
1656 } else if (dl_argv_match(dl
, "trap") &&
1657 (o_all
& DL_OPT_TRAP_NAME
)) {
1659 err
= dl_argv_str(dl
, &opts
->trap_name
);
1662 o_found
|= DL_OPT_TRAP_NAME
;
1663 } else if (dl_argv_match(dl
, "group") &&
1664 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1666 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1669 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1670 } else if (dl_argv_match(dl
, "action") &&
1671 (o_all
& DL_OPT_TRAP_ACTION
)) {
1672 const char *actionstr
;
1675 err
= dl_argv_str(dl
, &actionstr
);
1678 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1681 o_found
|= DL_OPT_TRAP_ACTION
;
1682 } else if (dl_argv_match(dl
, "netns") &&
1683 (o_all
& DL_OPT_NETNS
)) {
1684 const char *netns_str
;
1687 err
= dl_argv_str(dl
, &netns_str
);
1690 opts
->netns
= netns_get_fd(netns_str
);
1691 if ((int)opts
->netns
< 0) {
1693 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1696 opts
->netns_is_pid
= true;
1698 o_found
|= DL_OPT_NETNS
;
1699 } else if (dl_argv_match(dl
, "policer") &&
1700 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1702 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1705 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1706 } else if (dl_argv_match(dl
, "nopolicer") &&
1707 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1709 opts
->trap_policer_id
= 0;
1710 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1711 } else if (dl_argv_match(dl
, "rate") &&
1712 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1714 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1717 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1718 } else if (dl_argv_match(dl
, "burst") &&
1719 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1721 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1724 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1725 } else if (dl_argv_match(dl
, "hw_addr") &&
1726 (o_all
& DL_OPT_PORT_FUNCTION_HW_ADDR
)) {
1727 const char *addrstr
;
1730 err
= dl_argv_str(dl
, &addrstr
);
1733 err
= hw_addr_parse(addrstr
, opts
->port_function_hw_addr
,
1734 &opts
->port_function_hw_addr_len
);
1737 o_found
|= DL_OPT_PORT_FUNCTION_HW_ADDR
;
1740 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1745 opts
->present
= o_found
;
1747 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1749 opts
->present
|= DL_OPT_SB
;
1752 return dl_args_finding_required_validate(o_required
, o_found
);
1756 dl_function_attr_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1758 struct nlattr
*nest
;
1760 nest
= mnl_attr_nest_start(nlh
, DEVLINK_ATTR_PORT_FUNCTION
);
1761 mnl_attr_put(nlh
, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
,
1762 opts
->port_function_hw_addr_len
,
1763 opts
->port_function_hw_addr
);
1764 mnl_attr_nest_end(nlh
, nest
);
1767 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1769 struct dl_opts
*opts
= &dl
->opts
;
1771 if (opts
->present
& DL_OPT_HANDLE
) {
1772 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1773 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1774 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1775 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1776 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1777 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1779 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1780 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1781 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1782 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1785 if (opts
->present
& DL_OPT_PORT_TYPE
)
1786 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1788 if (opts
->present
& DL_OPT_PORT_COUNT
)
1789 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1791 if (opts
->present
& DL_OPT_SB
)
1792 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1794 if (opts
->present
& DL_OPT_SB_POOL
)
1795 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1796 opts
->sb_pool_index
);
1797 if (opts
->present
& DL_OPT_SB_SIZE
)
1798 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1799 opts
->sb_pool_size
);
1800 if (opts
->present
& DL_OPT_SB_TYPE
)
1801 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1802 opts
->sb_pool_type
);
1803 if (opts
->present
& DL_OPT_SB_THTYPE
)
1804 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1805 opts
->sb_pool_thtype
);
1806 if (opts
->present
& DL_OPT_SB_TH
)
1807 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1808 opts
->sb_threshold
);
1809 if (opts
->present
& DL_OPT_SB_TC
)
1810 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1812 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1813 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1814 opts
->eswitch_mode
);
1815 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1816 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1817 opts
->eswitch_inline_mode
);
1818 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1819 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1820 opts
->dpipe_table_name
);
1821 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1822 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1823 opts
->dpipe_counters_enabled
);
1824 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1825 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1826 opts
->eswitch_encap_mode
);
1827 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1828 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1830 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1831 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1832 opts
->resource_size
);
1833 if (opts
->present
& DL_OPT_PARAM_NAME
)
1834 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1836 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1837 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1839 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1840 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1841 opts
->region_snapshot_id
);
1842 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1843 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1844 opts
->region_address
);
1845 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1846 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1847 opts
->region_length
);
1848 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1849 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1850 opts
->flash_file_name
);
1851 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1852 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1853 opts
->flash_component
);
1854 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1855 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1856 opts
->reporter_name
);
1857 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1858 mnl_attr_put_u64(nlh
,
1859 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1860 opts
->reporter_graceful_period
);
1861 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1862 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1863 opts
->reporter_auto_recover
);
1864 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)
1865 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
1866 opts
->reporter_auto_dump
);
1867 if (opts
->present
& DL_OPT_TRAP_NAME
)
1868 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1870 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1871 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1872 opts
->trap_group_name
);
1873 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1874 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1876 if (opts
->present
& DL_OPT_NETNS
)
1877 mnl_attr_put_u32(nlh
,
1878 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1879 DEVLINK_ATTR_NETNS_FD
,
1881 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1882 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1883 opts
->trap_policer_id
);
1884 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1885 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1886 opts
->trap_policer_rate
);
1887 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1888 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1889 opts
->trap_policer_burst
);
1890 if (opts
->present
& DL_OPT_PORT_FUNCTION_HW_ADDR
)
1891 dl_function_attr_put(nlh
, opts
);
1894 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1895 uint64_t o_required
, uint64_t o_optional
)
1899 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1902 dl_opts_put(nlh
, dl
);
1906 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1908 struct dl_opts
*opts
= &dl
->opts
;
1909 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1910 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1911 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1912 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1914 if (opts
->present
& DL_OPT_HANDLE
&&
1915 attr_bus_name
&& attr_dev_name
) {
1916 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1917 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1919 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1920 strcmp(dev_name
, opts
->dev_name
) != 0)
1923 if (opts
->present
& DL_OPT_HANDLEP
&&
1924 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1925 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1926 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1927 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1929 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1930 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1931 port_index
!= opts
->port_index
)
1934 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1935 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1937 if (sb_index
!= opts
->sb_index
)
1943 static void cmd_dev_help(void)
1945 pr_err("Usage: devlink dev show [ DEV ]\n");
1946 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1947 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1948 pr_err(" [ encap-mode { none | basic } ]\n");
1949 pr_err(" devlink dev eswitch show DEV\n");
1950 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1951 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1952 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1953 pr_err(" devlink dev info [ DEV ]\n");
1954 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1957 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1958 const char *dev_name
)
1960 if (!dl
->arr_last
.present
)
1962 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1963 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1966 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1967 const char *dev_name
)
1969 dl
->arr_last
.present
= true;
1970 free(dl
->arr_last
.dev_name
);
1971 free(dl
->arr_last
.bus_name
);
1972 dl
->arr_last
.bus_name
= strdup(bus_name
);
1973 dl
->arr_last
.dev_name
= strdup(dev_name
);
1976 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1977 const char *dev_name
)
1979 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1982 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1983 const char *dev_name
)
1985 return dl
->arr_last
.present
&&
1986 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1989 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1990 bool content
, bool array
)
1992 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1993 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1996 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1998 if (dl
->json_output
) {
2000 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2001 close_json_array(PRINT_JSON
, NULL
);
2002 if (should_arr_last_handle_start(dl
, bus_name
,
2004 open_json_array(PRINT_JSON
, buf
);
2005 open_json_object(NULL
);
2006 arr_last_handle_set(dl
, bus_name
, dev_name
);
2008 open_json_object(NULL
);
2011 open_json_object(buf
);
2015 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2016 __pr_out_indent_dec();
2017 if (should_arr_last_handle_start(dl
, bus_name
,
2019 pr_out("%s%s", buf
, content
? ":" : "");
2021 __pr_out_indent_inc();
2022 arr_last_handle_set(dl
, bus_name
, dev_name
);
2025 pr_out("%s%s", buf
, content
? ":" : "");
2030 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
2032 __pr_out_handle_start(dl
, tb
, true, true);
2035 static void pr_out_handle_end(struct dl
*dl
)
2037 if (dl
->json_output
)
2038 close_json_object();
2043 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
2045 __pr_out_handle_start(dl
, tb
, false, false);
2046 pr_out_handle_end(dl
);
2049 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
2050 const char *dev_name
, uint32_t port_index
)
2052 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
2053 dl
->arr_last
.port_index
== port_index
;
2056 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
2057 const char *dev_name
, uint32_t port_index
)
2059 arr_last_handle_set(dl
, bus_name
, dev_name
);
2060 dl
->arr_last
.port_index
= port_index
;
2063 static bool should_arr_last_port_handle_start(struct dl
*dl
,
2064 const char *bus_name
,
2065 const char *dev_name
,
2066 uint32_t port_index
)
2068 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2071 static bool should_arr_last_port_handle_end(struct dl
*dl
,
2072 const char *bus_name
,
2073 const char *dev_name
,
2074 uint32_t port_index
)
2076 return dl
->arr_last
.present
&&
2077 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2080 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
2081 const char *dev_name
,
2082 uint32_t port_index
, bool try_nice
,
2085 static char buf
[64];
2086 char *ifname
= NULL
;
2088 if (dl
->no_nice_names
|| !try_nice
||
2089 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
2090 port_index
, &ifname
) != 0)
2091 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
2093 sprintf(buf
, "%s", ifname
);
2095 if (dl
->json_output
) {
2097 if (should_arr_last_port_handle_end(dl
, bus_name
,
2100 close_json_array(PRINT_JSON
, NULL
);
2101 if (should_arr_last_port_handle_start(dl
, bus_name
,
2104 open_json_array(PRINT_JSON
, buf
);
2105 open_json_object(NULL
);
2106 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
2109 open_json_object(NULL
);
2112 open_json_object(buf
);
2119 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2121 const char *bus_name
;
2122 const char *dev_name
;
2123 uint32_t port_index
;
2125 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2126 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2127 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2128 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
2131 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2133 const char *bus_name
;
2134 const char *dev_name
;
2135 uint32_t port_index
;
2137 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2138 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2139 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2140 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
2143 static void pr_out_port_handle_end(struct dl
*dl
)
2145 if (dl
->json_output
)
2146 close_json_object();
2151 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
2153 if (dl
->json_output
) {
2154 print_uint(PRINT_JSON
, "address", NULL
, addr
);
2155 open_json_array(PRINT_JSON
, "data");
2159 static void pr_out_region_chunk_end(struct dl
*dl
)
2161 if (dl
->json_output
)
2162 close_json_array(PRINT_JSON
, NULL
);
2165 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2168 static uint64_t align_val
;
2171 pr_out_region_chunk_start(dl
, addr
);
2173 if (!dl
->json_output
)
2174 if (!(align_val
% 16))
2175 pr_out("%s%016"PRIx64
" ",
2176 align_val
? "\n" : "",
2181 if (dl
->json_output
)
2182 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2184 pr_out("%02x ", data
[i
]);
2189 pr_out_region_chunk_end(dl
);
2192 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2194 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2200 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2201 if (err
!= MNL_CB_OK
)
2204 pr_out_object_start(dl
, "stats");
2205 pr_out_object_start(dl
, "rx");
2206 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2207 pr_out_u64(dl
, "bytes",
2208 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2209 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2210 pr_out_u64(dl
, "packets",
2211 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2212 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2213 pr_out_u64(dl
, "dropped",
2214 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2215 pr_out_object_end(dl
);
2216 pr_out_object_end(dl
);
2219 static const char *param_cmode_name(uint8_t cmode
)
2222 case DEVLINK_PARAM_CMODE_RUNTIME
:
2223 return PARAM_CMODE_RUNTIME_STR
;
2224 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2225 return PARAM_CMODE_DRIVERINIT_STR
;
2226 case DEVLINK_PARAM_CMODE_PERMANENT
:
2227 return PARAM_CMODE_PERMANENT_STR
;
2228 default: return "<unknown type>";
2232 static const char *eswitch_mode_name(uint32_t mode
)
2235 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2236 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2237 default: return "<unknown mode>";
2241 static const char *eswitch_inline_mode_name(uint32_t mode
)
2244 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2245 return ESWITCH_INLINE_MODE_NONE
;
2246 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2247 return ESWITCH_INLINE_MODE_LINK
;
2248 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2249 return ESWITCH_INLINE_MODE_NETWORK
;
2250 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2251 return ESWITCH_INLINE_MODE_TRANSPORT
;
2253 return "<unknown mode>";
2257 static const char *eswitch_encap_mode_name(uint32_t mode
)
2260 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2261 return ESWITCH_ENCAP_MODE_NONE
;
2262 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2263 return ESWITCH_ENCAP_MODE_BASIC
;
2265 return "<unknown mode>";
2269 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2271 __pr_out_handle_start(dl
, tb
, true, false);
2273 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2274 check_indent_newline(dl
);
2275 print_string(PRINT_ANY
, "mode", "mode %s",
2276 eswitch_mode_name(mnl_attr_get_u16(
2277 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2279 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2280 check_indent_newline(dl
);
2281 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2282 eswitch_inline_mode_name(mnl_attr_get_u8(
2283 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2285 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2286 check_indent_newline(dl
);
2287 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2288 eswitch_encap_mode_name(mnl_attr_get_u8(
2289 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2292 pr_out_handle_end(dl
);
2295 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2297 struct dl
*dl
= data
;
2298 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2299 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2301 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2302 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2303 return MNL_CB_ERROR
;
2304 pr_out_eswitch(dl
, tb
);
2308 static int cmd_dev_eswitch_show(struct dl
*dl
)
2310 struct nlmsghdr
*nlh
;
2313 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2314 NLM_F_REQUEST
| NLM_F_ACK
);
2316 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2320 pr_out_section_start(dl
, "dev");
2321 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2322 pr_out_section_end(dl
);
2326 static int cmd_dev_eswitch_set(struct dl
*dl
)
2328 struct nlmsghdr
*nlh
;
2331 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2332 NLM_F_REQUEST
| NLM_F_ACK
);
2334 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2335 DL_OPT_ESWITCH_MODE
|
2336 DL_OPT_ESWITCH_INLINE_MODE
|
2337 DL_OPT_ESWITCH_ENCAP_MODE
);
2342 if (dl
->opts
.present
== 1) {
2343 pr_err("Need to set at least one option\n");
2347 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2350 static int cmd_dev_eswitch(struct dl
*dl
)
2352 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2355 } else if (dl_argv_match(dl
, "set")) {
2357 return cmd_dev_eswitch_set(dl
);
2358 } else if (dl_argv_match(dl
, "show")) {
2360 return cmd_dev_eswitch_show(dl
);
2362 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2366 struct param_val_conv
{
2372 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2373 uint32_t len
, const char *name
)
2377 for (i
= 0; i
< len
; i
++)
2378 if (!strcmp(param_val_conv
[i
].name
, name
))
2385 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2386 uint32_t len
, const char *name
, const char *vstr
,
2391 for (i
= 0; i
< len
; i
++)
2392 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2393 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2394 *vuint
= param_val_conv
[i
].vuint
;
2402 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2403 uint32_t len
, const char *name
, uint32_t vuint
,
2408 for (i
= 0; i
< len
; i
++)
2409 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2410 param_val_conv
[i
].vuint
== vuint
) {
2411 *vstr
= param_val_conv
[i
].vstr
;
2418 static const struct param_val_conv param_val_conv
[] = {
2420 .name
= "fw_load_policy",
2422 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2425 .name
= "fw_load_policy",
2427 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2430 .name
= "fw_load_policy",
2432 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK
,
2435 .name
= "reset_dev_on_drv_probe",
2437 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2440 .name
= "fw_load_policy",
2442 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2445 .name
= "reset_dev_on_drv_probe",
2447 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2450 .name
= "reset_dev_on_drv_probe",
2452 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2455 .name
= "reset_dev_on_drv_probe",
2457 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2461 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2463 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2464 int nla_type
, struct nlattr
*nl
)
2466 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2467 struct nlattr
*val_attr
;
2472 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2473 if (err
!= MNL_CB_OK
)
2476 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2477 (nla_type
!= MNL_TYPE_FLAG
&&
2478 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2481 check_indent_newline(dl
);
2482 print_string(PRINT_ANY
, "cmode", "cmode %s",
2483 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2485 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2487 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2493 err
= param_val_conv_str_get(param_val_conv
,
2496 mnl_attr_get_u8(val_attr
),
2500 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2502 print_uint(PRINT_ANY
, "value", " value %u",
2503 mnl_attr_get_u8(val_attr
));
2508 err
= param_val_conv_str_get(param_val_conv
,
2511 mnl_attr_get_u16(val_attr
),
2515 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2517 print_uint(PRINT_ANY
, "value", " value %u",
2518 mnl_attr_get_u16(val_attr
));
2523 err
= param_val_conv_str_get(param_val_conv
,
2526 mnl_attr_get_u32(val_attr
),
2530 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2532 print_uint(PRINT_ANY
, "value", " value %u",
2533 mnl_attr_get_u32(val_attr
));
2536 case MNL_TYPE_STRING
:
2537 print_string(PRINT_ANY
, "value", " value %s",
2538 mnl_attr_get_str(val_attr
));
2541 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2546 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2548 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2549 struct nlattr
*param_value_attr
;
2550 const char *nla_name
;
2554 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2555 if (err
!= MNL_CB_OK
)
2557 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2558 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2559 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2563 pr_out_handle_start_arr(dl
, tb
);
2565 __pr_out_handle_start(dl
, tb
, true, false);
2567 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2569 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2570 check_indent_newline(dl
);
2571 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2572 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2573 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2575 print_string(PRINT_ANY
, "type", "type %s", "generic");
2577 pr_out_array_start(dl
, "values");
2578 mnl_attr_for_each_nested(param_value_attr
,
2579 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2580 pr_out_entry_start(dl
);
2581 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2582 pr_out_entry_end(dl
);
2584 pr_out_array_end(dl
);
2585 pr_out_handle_end(dl
);
2588 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2590 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2591 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2592 struct dl
*dl
= data
;
2594 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2595 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2596 !tb
[DEVLINK_ATTR_PARAM
])
2597 return MNL_CB_ERROR
;
2598 pr_out_param(dl
, tb
, true);
2614 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2616 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2617 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2618 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2619 struct nlattr
*param_value_attr
;
2620 enum devlink_param_cmode cmode
;
2621 struct param_ctx
*ctx
= data
;
2622 struct dl
*dl
= ctx
->dl
;
2626 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2627 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2628 !tb
[DEVLINK_ATTR_PARAM
])
2629 return MNL_CB_ERROR
;
2631 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2632 if (err
!= MNL_CB_OK
)
2633 return MNL_CB_ERROR
;
2635 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2636 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2637 return MNL_CB_ERROR
;
2639 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2640 mnl_attr_for_each_nested(param_value_attr
,
2641 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2642 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2643 struct nlattr
*val_attr
;
2645 err
= mnl_attr_parse_nested(param_value_attr
,
2646 attr_cb
, nla_value
);
2647 if (err
!= MNL_CB_OK
)
2648 return MNL_CB_ERROR
;
2650 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2651 (nla_type
!= MNL_TYPE_FLAG
&&
2652 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2653 return MNL_CB_ERROR
;
2655 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2656 if (cmode
== dl
->opts
.cmode
) {
2657 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2660 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2663 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2666 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2668 case MNL_TYPE_STRING
:
2669 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2672 ctx
->value
.vbool
= val_attr
? true : false;
2678 ctx
->nla_type
= nla_type
;
2682 static int cmd_dev_param_set(struct dl
*dl
)
2684 struct param_ctx ctx
= {};
2685 struct nlmsghdr
*nlh
;
2693 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2695 DL_OPT_PARAM_VALUE
|
2696 DL_OPT_PARAM_CMODE
, 0);
2700 /* Get value type */
2701 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2702 NLM_F_REQUEST
| NLM_F_ACK
);
2703 dl_opts_put(nlh
, dl
);
2706 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2710 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2711 NLM_F_REQUEST
| NLM_F_ACK
);
2712 dl_opts_put(nlh
, dl
);
2714 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2715 dl
->opts
.param_name
);
2717 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2718 switch (ctx
.nla_type
) {
2721 err
= param_val_conv_uint_get(param_val_conv
,
2723 dl
->opts
.param_name
,
2724 dl
->opts
.param_value
,
2728 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2731 goto err_param_value_parse
;
2732 if (val_u8
== ctx
.value
.vu8
)
2734 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2738 err
= param_val_conv_uint_get(param_val_conv
,
2740 dl
->opts
.param_name
,
2741 dl
->opts
.param_value
,
2745 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2748 goto err_param_value_parse
;
2749 if (val_u16
== ctx
.value
.vu16
)
2751 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2755 err
= param_val_conv_uint_get(param_val_conv
,
2757 dl
->opts
.param_name
,
2758 dl
->opts
.param_value
,
2761 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2763 goto err_param_value_parse
;
2764 if (val_u32
== ctx
.value
.vu32
)
2766 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2769 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2771 goto err_param_value_parse
;
2772 if (val_bool
== ctx
.value
.vbool
)
2775 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2778 case MNL_TYPE_STRING
:
2779 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2780 dl
->opts
.param_value
);
2781 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2785 printf("Value type not supported\n");
2788 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2790 err_param_value_parse
:
2791 pr_err("Value \"%s\" is not a number or not within range\n",
2792 dl
->opts
.param_value
);
2796 static int cmd_dev_param_show(struct dl
*dl
)
2798 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2799 struct nlmsghdr
*nlh
;
2802 if (dl_argc(dl
) == 0)
2803 flags
|= NLM_F_DUMP
;
2805 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2807 if (dl_argc(dl
) > 0) {
2808 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2809 DL_OPT_PARAM_NAME
, 0);
2814 pr_out_section_start(dl
, "param");
2815 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2816 pr_out_section_end(dl
);
2820 static int cmd_dev_param(struct dl
*dl
)
2822 if (dl_argv_match(dl
, "help")) {
2825 } else if (dl_argv_match(dl
, "show") ||
2826 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2828 return cmd_dev_param_show(dl
);
2829 } else if (dl_argv_match(dl
, "set")) {
2831 return cmd_dev_param_set(dl
);
2833 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2836 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2838 struct dl
*dl
= data
;
2839 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2840 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2841 uint8_t reload_failed
= 0;
2843 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2844 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2845 return MNL_CB_ERROR
;
2847 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2848 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2850 if (reload_failed
) {
2851 __pr_out_handle_start(dl
, tb
, true, false);
2852 check_indent_newline(dl
);
2853 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2854 pr_out_handle_end(dl
);
2856 pr_out_handle(dl
, tb
);
2862 static int cmd_dev_show(struct dl
*dl
)
2864 struct nlmsghdr
*nlh
;
2865 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2868 if (dl_argc(dl
) == 0)
2869 flags
|= NLM_F_DUMP
;
2871 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2873 if (dl_argc(dl
) > 0) {
2874 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2879 pr_out_section_start(dl
, "dev");
2880 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2881 pr_out_section_end(dl
);
2885 static int cmd_dev_reload(struct dl
*dl
)
2887 struct nlmsghdr
*nlh
;
2890 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2895 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2896 NLM_F_REQUEST
| NLM_F_ACK
);
2898 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2902 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2905 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2906 const char *name
, int type
)
2908 struct nlattr
*version
;
2910 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2911 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2912 const char *ver_value
;
2913 const char *ver_name
;
2916 if (mnl_attr_get_type(version
) != type
)
2919 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2920 if (err
!= MNL_CB_OK
)
2923 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2924 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2928 pr_out_object_start(dl
, name
);
2932 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2933 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2935 check_indent_newline(dl
);
2936 print_string_name_value(ver_name
, ver_value
);
2937 if (!dl
->json_output
)
2942 pr_out_object_end(dl
);
2945 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2946 struct nlattr
**tb
, bool has_versions
)
2948 __pr_out_handle_start(dl
, tb
, true, false);
2950 __pr_out_indent_inc();
2951 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2952 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2954 if (!dl
->json_output
)
2956 check_indent_newline(dl
);
2957 print_string(PRINT_ANY
, "driver", "driver %s",
2958 mnl_attr_get_str(nla_drv
));
2961 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2962 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2964 if (!dl
->json_output
)
2966 check_indent_newline(dl
);
2967 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2968 mnl_attr_get_str(nla_sn
));
2970 __pr_out_indent_dec();
2973 pr_out_object_start(dl
, "versions");
2975 pr_out_versions_single(dl
, nlh
, "fixed",
2976 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2977 pr_out_versions_single(dl
, nlh
, "running",
2978 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2979 pr_out_versions_single(dl
, nlh
, "stored",
2980 DEVLINK_ATTR_INFO_VERSION_STORED
);
2982 pr_out_object_end(dl
);
2985 pr_out_handle_end(dl
);
2988 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2990 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2991 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2992 bool has_versions
, has_info
;
2993 struct dl
*dl
= data
;
2995 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2997 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2998 return MNL_CB_ERROR
;
3000 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
3001 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
3002 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
3003 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
3004 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
3008 pr_out_info(dl
, nlh
, tb
, has_versions
);
3013 static int cmd_dev_info(struct dl
*dl
)
3015 struct nlmsghdr
*nlh
;
3016 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3019 if (dl_argv_match(dl
, "help")) {
3024 if (dl_argc(dl
) == 0)
3025 flags
|= NLM_F_DUMP
;
3027 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
3029 if (dl_argc(dl
) > 0) {
3030 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3035 pr_out_section_start(dl
, "info");
3036 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
3037 pr_out_section_end(dl
);
3041 struct cmd_dev_flash_status_ctx
{
3044 char *last_component
;
3045 uint8_t not_first
:1,
3051 static int nullstrcmp(const char *str1
, const char *str2
)
3054 return strcmp(str1
, str2
);
3057 return str1
? 1 : -1;
3060 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
3062 struct cmd_dev_flash_status_ctx
*ctx
= data
;
3063 struct dl_opts
*opts
= &ctx
->dl
->opts
;
3064 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3065 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3066 const char *component
= NULL
;
3067 uint64_t done
= 0, total
= 0;
3068 const char *msg
= NULL
;
3069 const char *bus_name
;
3070 const char *dev_name
;
3072 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
3073 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3076 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3077 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3078 return MNL_CB_ERROR
;
3079 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3080 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3081 if (strcmp(bus_name
, opts
->bus_name
) ||
3082 strcmp(dev_name
, opts
->dev_name
))
3083 return MNL_CB_ERROR
;
3085 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3087 free(ctx
->last_msg
);
3088 free(ctx
->last_component
);
3089 ctx
->received_end
= 1;
3093 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3094 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3095 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3096 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3097 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3098 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3099 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3100 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3102 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3103 !nullstrcmp(component
, ctx
->last_component
) &&
3104 ctx
->last_pc
&& ctx
->not_first
) {
3105 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3110 pr_out("[%s] ", component
);
3111 free(ctx
->last_component
);
3112 ctx
->last_component
= strdup(component
);
3116 free(ctx
->last_msg
);
3117 ctx
->last_msg
= strdup(msg
);
3121 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3132 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3133 struct mnlg_socket
*nlg_ntf
,
3136 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3143 for (i
= 0; i
< 3; i
++)
3145 FD_SET(pipe_r
, &fds
[0]);
3147 FD_SET(nlfd
, &fds
[0]);
3151 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3154 pr_err("select() failed\n");
3157 if (FD_ISSET(nlfd
, &fds
[0])) {
3158 err
= _mnlg_socket_recv_run(nlg_ntf
,
3159 cmd_dev_flash_status_cb
, ctx
);
3163 if (FD_ISSET(pipe_r
, &fds
[0])) {
3164 err
= read(pipe_r
, &err2
, sizeof(err2
));
3166 pr_err("Failed to read pipe\n");
3171 ctx
->flash_done
= 1;
3177 static int cmd_dev_flash(struct dl
*dl
)
3179 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3180 struct mnlg_socket
*nlg_ntf
;
3181 struct nlmsghdr
*nlh
;
3187 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3192 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3193 NLM_F_REQUEST
| NLM_F_ACK
);
3195 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3196 DL_OPT_FLASH_COMPONENT
);
3200 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3204 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3208 err
= pipe(pipe_fds
);
3211 pipe_r
= pipe_fds
[0];
3212 pipe_w
= pipe_fds
[1];
3220 /* In child, just execute the flash and pass returned
3221 * value through pipe once it is done.
3226 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3227 cc
= write(pipe_w
, &err
, sizeof(err
));
3229 exit(cc
!= sizeof(err
));
3234 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3237 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3239 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3242 mnlg_socket_close(nlg_ntf
);
3246 static int cmd_dev(struct dl
*dl
)
3248 if (dl_argv_match(dl
, "help")) {
3251 } else if (dl_argv_match(dl
, "show") ||
3252 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3254 return cmd_dev_show(dl
);
3255 } else if (dl_argv_match(dl
, "eswitch")) {
3257 return cmd_dev_eswitch(dl
);
3258 } else if (dl_argv_match(dl
, "reload")) {
3260 return cmd_dev_reload(dl
);
3261 } else if (dl_argv_match(dl
, "param")) {
3263 return cmd_dev_param(dl
);
3264 } else if (dl_argv_match(dl
, "info")) {
3266 return cmd_dev_info(dl
);
3267 } else if (dl_argv_match(dl
, "flash")) {
3269 return cmd_dev_flash(dl
);
3271 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3275 static void cmd_port_help(void)
3277 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3278 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3279 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3280 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3281 pr_err(" devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3284 static const char *port_type_name(uint32_t type
)
3287 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3288 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3289 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3290 case DEVLINK_PORT_TYPE_IB
: return "ib";
3291 default: return "<unknown type>";
3295 static const char *port_flavour_name(uint16_t flavour
)
3298 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3300 case DEVLINK_PORT_FLAVOUR_CPU
:
3302 case DEVLINK_PORT_FLAVOUR_DSA
:
3304 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3306 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3308 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3311 return "<unknown flavour>";
3315 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3319 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3320 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3321 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3323 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3324 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3325 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3329 static void pr_out_port_function(struct dl
*dl
, struct nlattr
**tb_port
)
3331 struct nlattr
*tb
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {};
3332 unsigned char *data
;
3333 SPRINT_BUF(hw_addr
);
3337 if (!tb_port
[DEVLINK_ATTR_PORT_FUNCTION
])
3340 err
= mnl_attr_parse_nested(tb_port
[DEVLINK_ATTR_PORT_FUNCTION
],
3341 function_attr_cb
, tb
);
3342 if (err
!= MNL_CB_OK
)
3345 if (!tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
])
3348 len
= mnl_attr_get_payload_len(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3349 data
= mnl_attr_get_payload(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3351 pr_out_object_start(dl
, "function");
3352 check_indent_newline(dl
);
3353 print_string(PRINT_ANY
, "hw_addr", "hw_addr %s",
3354 ll_addr_n2a(data
, len
, 0, hw_addr
, sizeof(hw_addr
)));
3355 if (!dl
->json_output
)
3356 __pr_out_indent_dec();
3357 pr_out_object_end(dl
);
3360 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3362 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3363 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3365 pr_out_port_handle_start(dl
, tb
, false);
3366 check_indent_newline(dl
);
3368 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3370 print_string(PRINT_ANY
, "type", "type %s",
3371 port_type_name(port_type
));
3373 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3375 if (port_type
!= des_port_type
)
3376 print_string(PRINT_ANY
, "des_type", " des_type %s",
3377 port_type_name(des_port_type
));
3380 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3381 print_string(PRINT_ANY
, "netdev", " netdev %s",
3382 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3384 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3385 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3386 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3388 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3389 uint16_t port_flavour
=
3390 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3392 print_string(PRINT_ANY
, "flavour", " flavour %s",
3393 port_flavour_name(port_flavour
));
3395 switch (port_flavour
) {
3396 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3397 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3398 pr_out_port_pfvf_num(dl
, tb
);
3404 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3405 uint32_t port_number
;
3407 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3408 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3410 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3411 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3412 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3413 pr_out_port_function(dl
, tb
);
3414 pr_out_port_handle_end(dl
);
3417 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3419 struct dl
*dl
= data
;
3420 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3421 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3423 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3424 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3425 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3426 return MNL_CB_ERROR
;
3427 pr_out_port(dl
, tb
);
3431 static int cmd_port_show(struct dl
*dl
)
3433 struct nlmsghdr
*nlh
;
3434 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3437 if (dl_argc(dl
) == 0)
3438 flags
|= NLM_F_DUMP
;
3440 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3442 if (dl_argc(dl
) > 0) {
3443 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3448 pr_out_section_start(dl
, "port");
3449 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3450 pr_out_section_end(dl
);
3454 static int cmd_port_set(struct dl
*dl
)
3456 struct nlmsghdr
*nlh
;
3459 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3460 NLM_F_REQUEST
| NLM_F_ACK
);
3462 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3466 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3469 static int cmd_port_split(struct dl
*dl
)
3471 struct nlmsghdr
*nlh
;
3474 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3475 NLM_F_REQUEST
| NLM_F_ACK
);
3477 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3481 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3484 static int cmd_port_unsplit(struct dl
*dl
)
3486 struct nlmsghdr
*nlh
;
3489 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3490 NLM_F_REQUEST
| NLM_F_ACK
);
3492 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3496 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3499 static void cmd_port_function_help(void)
3501 pr_err("Usage: devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3504 static int cmd_port_function_set(struct dl
*dl
)
3506 struct nlmsghdr
*nlh
;
3509 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
, NLM_F_REQUEST
| NLM_F_ACK
);
3511 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_FUNCTION_HW_ADDR
, 0);
3515 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3518 static int cmd_port_function(struct dl
*dl
)
3520 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3521 cmd_port_function_help();
3523 } else if (dl_argv_match(dl
, "set")) {
3525 return cmd_port_function_set(dl
);
3527 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3531 static int cmd_port(struct dl
*dl
)
3533 if (dl_argv_match(dl
, "help")) {
3536 } else if (dl_argv_match(dl
, "show") ||
3537 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3539 return cmd_port_show(dl
);
3540 } else if (dl_argv_match(dl
, "set")) {
3542 return cmd_port_set(dl
);
3543 } else if (dl_argv_match(dl
, "split")) {
3545 return cmd_port_split(dl
);
3546 } else if (dl_argv_match(dl
, "unsplit")) {
3548 return cmd_port_unsplit(dl
);
3549 } else if (dl_argv_match(dl
, "function")) {
3551 return cmd_port_function(dl
);
3553 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3557 static void cmd_sb_help(void)
3559 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3560 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3561 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3562 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3563 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3564 pr_err(" pool POOL_INDEX ]\n");
3565 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3566 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3567 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3568 pr_err(" type { ingress | egress } ]\n");
3569 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3570 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3571 pr_err(" th THRESHOLD\n");
3572 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3573 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3574 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3577 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3579 pr_out_handle_start_arr(dl
, tb
);
3580 check_indent_newline(dl
);
3581 print_uint(PRINT_ANY
, "sb", "sb %u",
3582 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3583 print_uint(PRINT_ANY
, "size", " size %u",
3584 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3585 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3586 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3587 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3588 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3589 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3590 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3591 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3592 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3593 pr_out_handle_end(dl
);
3596 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3598 struct dl
*dl
= data
;
3599 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3600 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3602 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3603 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3604 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3605 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3606 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3607 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3608 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3609 return MNL_CB_ERROR
;
3614 static int cmd_sb_show(struct dl
*dl
)
3616 struct nlmsghdr
*nlh
;
3617 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3620 if (dl_argc(dl
) == 0)
3621 flags
|= NLM_F_DUMP
;
3623 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3625 if (dl_argc(dl
) > 0) {
3626 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3631 pr_out_section_start(dl
, "sb");
3632 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3633 pr_out_section_end(dl
);
3637 static const char *pool_type_name(uint8_t type
)
3640 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3641 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3642 default: return "<unknown type>";
3646 static const char *threshold_type_name(uint8_t type
)
3649 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3650 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3651 default: return "<unknown type>";
3655 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3657 pr_out_handle_start_arr(dl
, tb
);
3658 check_indent_newline(dl
);
3659 print_uint(PRINT_ANY
, "sb", "sb %u",
3660 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3661 print_uint(PRINT_ANY
, "pool", " pool %u",
3662 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3663 print_string(PRINT_ANY
, "type", " type %s",
3664 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3665 print_uint(PRINT_ANY
, "size", " size %u",
3666 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3667 print_string(PRINT_ANY
, "thtype", " thtype %s",
3668 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3669 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3670 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3671 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3672 pr_out_handle_end(dl
);
3675 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3677 struct dl
*dl
= data
;
3678 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3679 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3681 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3682 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3683 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3684 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3685 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3686 return MNL_CB_ERROR
;
3687 pr_out_sb_pool(dl
, tb
);
3691 static int cmd_sb_pool_show(struct dl
*dl
)
3693 struct nlmsghdr
*nlh
;
3694 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3697 if (dl_argc(dl
) == 0)
3698 flags
|= NLM_F_DUMP
;
3700 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3702 if (dl_argc(dl
) > 0) {
3703 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3709 pr_out_section_start(dl
, "pool");
3710 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3711 pr_out_section_end(dl
);
3715 static int cmd_sb_pool_set(struct dl
*dl
)
3717 struct nlmsghdr
*nlh
;
3720 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3721 NLM_F_REQUEST
| NLM_F_ACK
);
3723 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3724 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3728 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3731 static int cmd_sb_pool(struct dl
*dl
)
3733 if (dl_argv_match(dl
, "help")) {
3736 } else if (dl_argv_match(dl
, "show") ||
3737 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3739 return cmd_sb_pool_show(dl
);
3740 } else if (dl_argv_match(dl
, "set")) {
3742 return cmd_sb_pool_set(dl
);
3744 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3748 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3750 pr_out_port_handle_start_arr(dl
, tb
, true);
3751 check_indent_newline(dl
);
3752 print_uint(PRINT_ANY
, "sb", "sb %u",
3753 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3754 print_uint(PRINT_ANY
, "pool", " pool %u",
3755 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3756 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3757 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3758 pr_out_port_handle_end(dl
);
3761 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3763 struct dl
*dl
= data
;
3764 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3765 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3767 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3768 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3769 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3770 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3771 return MNL_CB_ERROR
;
3772 pr_out_sb_port_pool(dl
, tb
);
3776 static int cmd_sb_port_pool_show(struct dl
*dl
)
3778 struct nlmsghdr
*nlh
;
3779 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3782 if (dl_argc(dl
) == 0)
3783 flags
|= NLM_F_DUMP
;
3785 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3787 if (dl_argc(dl
) > 0) {
3788 err
= dl_argv_parse_put(nlh
, dl
,
3789 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3795 pr_out_section_start(dl
, "port_pool");
3796 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3797 pr_out_section_end(dl
);
3801 static int cmd_sb_port_pool_set(struct dl
*dl
)
3803 struct nlmsghdr
*nlh
;
3806 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3807 NLM_F_REQUEST
| NLM_F_ACK
);
3809 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3810 DL_OPT_SB_TH
, DL_OPT_SB
);
3814 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3817 static int cmd_sb_port_pool(struct dl
*dl
)
3819 if (dl_argv_match(dl
, "help")) {
3822 } else if (dl_argv_match(dl
, "show") ||
3823 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3825 return cmd_sb_port_pool_show(dl
);
3826 } else if (dl_argv_match(dl
, "set")) {
3828 return cmd_sb_port_pool_set(dl
);
3830 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3834 static int cmd_sb_port(struct dl
*dl
)
3836 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3839 } else if (dl_argv_match(dl
, "pool")) {
3841 return cmd_sb_port_pool(dl
);
3843 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3847 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3849 pr_out_port_handle_start_arr(dl
, tb
, true);
3850 check_indent_newline(dl
);
3851 print_uint(PRINT_ANY
, "sb", "sb %u",
3852 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3853 print_uint(PRINT_ANY
, "tc", " tc %u",
3854 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3855 print_string(PRINT_ANY
, "type", " type %s",
3856 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3857 print_uint(PRINT_ANY
, "pool", " pool %u",
3858 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3859 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3860 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3861 pr_out_port_handle_end(dl
);
3864 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3866 struct dl
*dl
= data
;
3867 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3868 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3870 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3871 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3872 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3873 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3874 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3875 return MNL_CB_ERROR
;
3876 pr_out_sb_tc_bind(dl
, tb
);
3880 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3882 struct nlmsghdr
*nlh
;
3883 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3886 if (dl_argc(dl
) == 0)
3887 flags
|= NLM_F_DUMP
;
3889 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3891 if (dl_argc(dl
) > 0) {
3892 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3893 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3898 pr_out_section_start(dl
, "tc_bind");
3899 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3900 pr_out_section_end(dl
);
3904 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3906 struct nlmsghdr
*nlh
;
3909 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3910 NLM_F_REQUEST
| NLM_F_ACK
);
3912 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3913 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3918 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3921 static int cmd_sb_tc_bind(struct dl
*dl
)
3923 if (dl_argv_match(dl
, "help")) {
3926 } else if (dl_argv_match(dl
, "show") ||
3927 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3929 return cmd_sb_tc_bind_show(dl
);
3930 } else if (dl_argv_match(dl
, "set")) {
3932 return cmd_sb_tc_bind_set(dl
);
3934 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3938 static int cmd_sb_tc(struct dl
*dl
)
3940 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3943 } else if (dl_argv_match(dl
, "bind")) {
3945 return cmd_sb_tc_bind(dl
);
3947 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3952 struct list_head list
;
3956 uint32_t bound_pool_index
;
3960 struct list_head list
;
3963 uint32_t port_index
;
3965 struct list_head pool_list
;
3966 struct list_head ing_tc_list
;
3967 struct list_head eg_tc_list
;
3973 struct list_head port_list
;
3976 static struct occ_item
*occ_item_alloc(void)
3978 return calloc(1, sizeof(struct occ_item
));
3981 static void occ_item_free(struct occ_item
*occ_item
)
3986 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3988 struct occ_port
*occ_port
;
3990 occ_port
= calloc(1, sizeof(*occ_port
));
3993 occ_port
->port_index
= port_index
;
3994 INIT_LIST_HEAD(&occ_port
->pool_list
);
3995 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3996 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
4000 static void occ_port_free(struct occ_port
*occ_port
)
4002 struct occ_item
*occ_item
, *tmp
;
4004 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
4005 occ_item_free(occ_item
);
4006 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
4007 occ_item_free(occ_item
);
4008 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
4009 occ_item_free(occ_item
);
4012 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
4014 struct occ_show
*occ_show
;
4016 occ_show
= calloc(1, sizeof(*occ_show
));
4020 INIT_LIST_HEAD(&occ_show
->port_list
);
4024 static void occ_show_free(struct occ_show
*occ_show
)
4026 struct occ_port
*occ_port
, *tmp
;
4028 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
4029 occ_port_free(occ_port
);
4032 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
4035 struct occ_port
*occ_port
;
4036 uint32_t port_index
;
4038 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
4040 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
4041 if (occ_port
->port_index
== port_index
)
4044 occ_port
= occ_port_alloc(port_index
);
4047 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
4051 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
4054 struct occ_item
*occ_item
;
4057 pr_out_sp(7, " %s:", label
);
4058 list_for_each_entry(occ_item
, list
, list
) {
4059 if ((i
- 1) % 4 == 0 && i
!= 1)
4062 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
4063 occ_item
->bound_pool_index
);
4065 pr_out_sp(7, "%2u:", occ_item
->index
);
4066 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
4070 if ((i
- 1) % 4 != 0)
4074 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
4075 struct list_head
*list
,
4078 struct occ_item
*occ_item
;
4081 open_json_object(label
);
4082 list_for_each_entry(occ_item
, list
, list
) {
4083 sprintf(buf
, "%u", occ_item
->index
);
4084 open_json_object(buf
);
4086 print_uint(PRINT_JSON
, "bound_pool", NULL
,
4087 occ_item
->bound_pool_index
);
4088 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
4089 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
4090 close_json_object();
4092 close_json_object();
4095 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
4097 if (dl
->json_output
) {
4098 pr_out_json_occ_show_item_list(dl
, "pool",
4099 &occ_port
->pool_list
, false);
4100 pr_out_json_occ_show_item_list(dl
, "itc",
4101 &occ_port
->ing_tc_list
, true);
4102 pr_out_json_occ_show_item_list(dl
, "etc",
4103 &occ_port
->eg_tc_list
, true);
4106 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
4107 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
4108 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
4112 static void pr_out_occ_show(struct occ_show
*occ_show
)
4114 struct dl
*dl
= occ_show
->dl
;
4115 struct dl_opts
*opts
= &dl
->opts
;
4116 struct occ_port
*occ_port
;
4118 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
4119 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
4120 occ_port
->port_index
, true, false);
4121 pr_out_occ_show_port(dl
, occ_port
);
4122 pr_out_port_handle_end(dl
);
4126 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
4129 struct occ_port
*occ_port
;
4130 struct occ_item
*occ_item
;
4132 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4135 occ_port
= occ_port_get(occ_show
, tb
);
4137 occ_show
->err
= -ENOMEM
;
4141 occ_item
= occ_item_alloc();
4143 occ_show
->err
= -ENOMEM
;
4146 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4147 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4148 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4149 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4152 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4154 struct occ_show
*occ_show
= data
;
4155 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4156 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4158 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4159 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4160 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4161 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4162 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4163 return MNL_CB_ERROR
;
4164 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4168 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4171 struct occ_port
*occ_port
;
4172 struct occ_item
*occ_item
;
4175 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4178 occ_port
= occ_port_get(occ_show
, tb
);
4180 occ_show
->err
= -ENOMEM
;
4184 occ_item
= occ_item_alloc();
4186 occ_show
->err
= -ENOMEM
;
4189 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4190 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4191 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4192 occ_item
->bound_pool_index
=
4193 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4194 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4195 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4196 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4197 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4198 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4200 occ_item_free(occ_item
);
4203 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4205 struct occ_show
*occ_show
= data
;
4206 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4207 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4209 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4210 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4211 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4212 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4213 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4214 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4215 return MNL_CB_ERROR
;
4216 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4220 static int cmd_sb_occ_show(struct dl
*dl
)
4222 struct nlmsghdr
*nlh
;
4223 struct occ_show
*occ_show
;
4224 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4227 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4231 occ_show
= occ_show_alloc(dl
);
4235 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4237 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4238 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4242 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4244 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4245 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4249 pr_out_section_start(dl
, "occupancy");
4250 pr_out_occ_show(occ_show
);
4251 pr_out_section_end(dl
);
4254 occ_show_free(occ_show
);
4258 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4260 struct nlmsghdr
*nlh
;
4263 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4264 NLM_F_REQUEST
| NLM_F_ACK
);
4266 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4270 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4273 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4275 struct nlmsghdr
*nlh
;
4278 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4279 NLM_F_REQUEST
| NLM_F_ACK
);
4281 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4285 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4288 static int cmd_sb_occ(struct dl
*dl
)
4290 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4293 } else if (dl_argv_match(dl
, "show") ||
4294 dl_argv_match(dl
, "list")) {
4296 return cmd_sb_occ_show(dl
);
4297 } else if (dl_argv_match(dl
, "snapshot")) {
4299 return cmd_sb_occ_snapshot(dl
);
4300 } else if (dl_argv_match(dl
, "clearmax")) {
4302 return cmd_sb_occ_clearmax(dl
);
4304 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4308 static int cmd_sb(struct dl
*dl
)
4310 if (dl_argv_match(dl
, "help")) {
4313 } else if (dl_argv_match(dl
, "show") ||
4314 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4316 return cmd_sb_show(dl
);
4317 } else if (dl_argv_match(dl
, "pool")) {
4319 return cmd_sb_pool(dl
);
4320 } else if (dl_argv_match(dl
, "port")) {
4322 return cmd_sb_port(dl
);
4323 } else if (dl_argv_match(dl
, "tc")) {
4325 return cmd_sb_tc(dl
);
4326 } else if (dl_argv_match(dl
, "occupancy")) {
4328 return cmd_sb_occ(dl
);
4330 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4334 static const char *cmd_name(uint8_t cmd
)
4337 case DEVLINK_CMD_UNSPEC
: return "unspec";
4338 case DEVLINK_CMD_GET
: return "get";
4339 case DEVLINK_CMD_SET
: return "set";
4340 case DEVLINK_CMD_NEW
: return "new";
4341 case DEVLINK_CMD_DEL
: return "del";
4342 case DEVLINK_CMD_PORT_GET
: return "get";
4343 case DEVLINK_CMD_PORT_SET
: return "set";
4344 case DEVLINK_CMD_PORT_NEW
: return "new";
4345 case DEVLINK_CMD_PORT_DEL
: return "del";
4346 case DEVLINK_CMD_PARAM_GET
: return "get";
4347 case DEVLINK_CMD_PARAM_SET
: return "set";
4348 case DEVLINK_CMD_PARAM_NEW
: return "new";
4349 case DEVLINK_CMD_PARAM_DEL
: return "del";
4350 case DEVLINK_CMD_REGION_GET
: return "get";
4351 case DEVLINK_CMD_REGION_SET
: return "set";
4352 case DEVLINK_CMD_REGION_NEW
: return "new";
4353 case DEVLINK_CMD_REGION_DEL
: return "del";
4354 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4355 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4356 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4357 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4358 case DEVLINK_CMD_TRAP_GET
: return "get";
4359 case DEVLINK_CMD_TRAP_SET
: return "set";
4360 case DEVLINK_CMD_TRAP_NEW
: return "new";
4361 case DEVLINK_CMD_TRAP_DEL
: return "del";
4362 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4363 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4364 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4365 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4366 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4367 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4368 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4369 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4370 default: return "<unknown cmd>";
4374 static const char *cmd_obj(uint8_t cmd
)
4377 case DEVLINK_CMD_UNSPEC
: return "unspec";
4378 case DEVLINK_CMD_GET
:
4379 case DEVLINK_CMD_SET
:
4380 case DEVLINK_CMD_NEW
:
4381 case DEVLINK_CMD_DEL
:
4383 case DEVLINK_CMD_PORT_GET
:
4384 case DEVLINK_CMD_PORT_SET
:
4385 case DEVLINK_CMD_PORT_NEW
:
4386 case DEVLINK_CMD_PORT_DEL
:
4388 case DEVLINK_CMD_PARAM_GET
:
4389 case DEVLINK_CMD_PARAM_SET
:
4390 case DEVLINK_CMD_PARAM_NEW
:
4391 case DEVLINK_CMD_PARAM_DEL
:
4393 case DEVLINK_CMD_REGION_GET
:
4394 case DEVLINK_CMD_REGION_SET
:
4395 case DEVLINK_CMD_REGION_NEW
:
4396 case DEVLINK_CMD_REGION_DEL
:
4398 case DEVLINK_CMD_FLASH_UPDATE
:
4399 case DEVLINK_CMD_FLASH_UPDATE_END
:
4400 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4402 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4404 case DEVLINK_CMD_TRAP_GET
:
4405 case DEVLINK_CMD_TRAP_SET
:
4406 case DEVLINK_CMD_TRAP_NEW
:
4407 case DEVLINK_CMD_TRAP_DEL
:
4409 case DEVLINK_CMD_TRAP_GROUP_GET
:
4410 case DEVLINK_CMD_TRAP_GROUP_SET
:
4411 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4412 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4413 return "trap-group";
4414 case DEVLINK_CMD_TRAP_POLICER_GET
:
4415 case DEVLINK_CMD_TRAP_POLICER_SET
:
4416 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4417 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4418 return "trap-policer";
4419 default: return "<unknown obj>";
4423 static void pr_out_mon_header(uint8_t cmd
)
4425 if (!is_json_context()) {
4426 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4428 open_json_object(NULL
);
4429 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4430 open_json_object(cmd_obj(cmd
));
4434 static void pr_out_mon_footer(void)
4436 if (is_json_context()) {
4437 close_json_object();
4438 close_json_object();
4442 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4444 const char *obj
= cmd_obj(cmd
);
4445 unsigned int index
= 0;
4446 const char *cur_obj
;
4450 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4451 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4457 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4459 __pr_out_handle_start(dl
, tb
, true, false);
4461 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4462 check_indent_newline(dl
);
4463 print_string(PRINT_ANY
, "msg", "msg %s",
4464 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4466 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4467 check_indent_newline(dl
);
4468 print_string(PRINT_ANY
, "component", "component %s",
4469 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4472 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4473 pr_out_u64(dl
, "done",
4474 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4476 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4477 pr_out_u64(dl
, "total",
4478 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4480 pr_out_handle_end(dl
);
4483 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4484 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4485 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4486 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4487 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4489 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4491 struct dl
*dl
= data
;
4492 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4493 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4494 uint8_t cmd
= genl
->cmd
;
4496 if (!cmd_filter_check(dl
, cmd
))
4500 case DEVLINK_CMD_GET
: /* fall through */
4501 case DEVLINK_CMD_SET
: /* fall through */
4502 case DEVLINK_CMD_NEW
: /* fall through */
4503 case DEVLINK_CMD_DEL
:
4504 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4505 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4506 return MNL_CB_ERROR
;
4507 pr_out_mon_header(genl
->cmd
);
4508 pr_out_handle(dl
, tb
);
4509 pr_out_mon_footer();
4511 case DEVLINK_CMD_PORT_GET
: /* fall through */
4512 case DEVLINK_CMD_PORT_SET
: /* fall through */
4513 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4514 case DEVLINK_CMD_PORT_DEL
:
4515 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4516 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4517 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4518 return MNL_CB_ERROR
;
4519 pr_out_mon_header(genl
->cmd
);
4520 pr_out_port(dl
, tb
);
4521 pr_out_mon_footer();
4523 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4524 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4525 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4526 case DEVLINK_CMD_PARAM_DEL
:
4527 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4528 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4529 !tb
[DEVLINK_ATTR_PARAM
])
4530 return MNL_CB_ERROR
;
4531 pr_out_mon_header(genl
->cmd
);
4532 pr_out_param(dl
, tb
, false);
4533 pr_out_mon_footer();
4535 case DEVLINK_CMD_REGION_GET
: /* fall through */
4536 case DEVLINK_CMD_REGION_SET
: /* fall through */
4537 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4538 case DEVLINK_CMD_REGION_DEL
:
4539 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4540 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4541 !tb
[DEVLINK_ATTR_REGION_NAME
])
4542 return MNL_CB_ERROR
;
4543 pr_out_mon_header(genl
->cmd
);
4544 pr_out_region(dl
, tb
);
4545 pr_out_mon_footer();
4547 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4548 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4549 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4550 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4551 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4552 return MNL_CB_ERROR
;
4553 pr_out_mon_header(genl
->cmd
);
4554 pr_out_flash_update(dl
, tb
);
4555 pr_out_mon_footer();
4557 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
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_HEALTH_REPORTER
])
4561 return MNL_CB_ERROR
;
4562 pr_out_mon_header(genl
->cmd
);
4563 pr_out_health(dl
, tb
);
4564 pr_out_mon_footer();
4566 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4567 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4568 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4569 case DEVLINK_CMD_TRAP_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_TRAP_NAME
] ||
4573 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4574 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4575 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4576 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4577 !tb
[DEVLINK_ATTR_STATS
])
4578 return MNL_CB_ERROR
;
4579 pr_out_mon_header(genl
->cmd
);
4580 pr_out_trap(dl
, tb
, false);
4581 pr_out_mon_footer();
4583 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4584 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4585 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4586 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4587 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4588 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4589 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4590 !tb
[DEVLINK_ATTR_STATS
])
4591 return MNL_CB_ERROR
;
4592 pr_out_mon_header(genl
->cmd
);
4593 pr_out_trap_group(dl
, tb
, false);
4594 pr_out_mon_footer();
4596 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4597 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4598 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4599 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4600 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4601 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4602 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4603 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4604 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4605 return MNL_CB_ERROR
;
4606 pr_out_mon_header(genl
->cmd
);
4607 pr_out_trap_policer(dl
, tb
, false);
4613 static int cmd_mon_show(struct dl
*dl
)
4616 unsigned int index
= 0;
4617 const char *cur_obj
;
4619 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4620 if (strcmp(cur_obj
, "all") != 0 &&
4621 strcmp(cur_obj
, "dev") != 0 &&
4622 strcmp(cur_obj
, "port") != 0 &&
4623 strcmp(cur_obj
, "health") != 0 &&
4624 strcmp(cur_obj
, "trap") != 0 &&
4625 strcmp(cur_obj
, "trap-group") != 0 &&
4626 strcmp(cur_obj
, "trap-policer") != 0) {
4627 pr_err("Unknown object \"%s\"\n", cur_obj
);
4631 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4634 open_json_object(NULL
);
4635 open_json_array(PRINT_JSON
, "mon");
4636 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
4637 close_json_array(PRINT_JSON
, NULL
);
4638 close_json_object();
4644 static void cmd_mon_help(void)
4646 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4647 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4650 static int cmd_mon(struct dl
*dl
)
4652 if (dl_argv_match(dl
, "help")) {
4656 return cmd_mon_show(dl
);
4659 struct dpipe_field
{
4662 unsigned int bitwidth
;
4663 enum devlink_dpipe_field_mapping_type mapping_type
;
4666 struct dpipe_header
{
4667 struct list_head list
;
4670 struct dpipe_field
*fields
;
4671 unsigned int fields_count
;
4674 struct dpipe_table
{
4675 struct list_head list
;
4677 unsigned int resource_id
;
4678 bool resource_valid
;
4681 struct dpipe_tables
{
4682 struct list_head table_list
;
4692 enum devlink_resource_unit unit
;
4697 struct list_head list
;
4698 struct list_head resource_list
;
4699 struct resource
*parent
;
4703 struct list_head resource_list
;
4706 struct resource_ctx
{
4709 struct resources
*resources
;
4710 struct dpipe_tables
*tables
;
4711 bool print_resources
;
4712 bool pending_change
;
4715 static struct resource
*resource_alloc(void)
4717 struct resource
*resource
;
4719 resource
= calloc(1, sizeof(struct resource
));
4722 INIT_LIST_HEAD(&resource
->resource_list
);
4726 static void resource_free(struct resource
*resource
)
4728 struct resource
*child_resource
, *tmp
;
4730 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4732 free(child_resource
->name
);
4733 resource_free(child_resource
);
4738 static struct resources
*resources_alloc(void)
4740 struct resources
*resources
;
4742 resources
= calloc(1, sizeof(struct resources
));
4745 INIT_LIST_HEAD(&resources
->resource_list
);
4749 static void resources_free(struct resources
*resources
)
4751 struct resource
*resource
, *tmp
;
4753 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4754 resource_free(resource
);
4757 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4759 ctx
->resources
= resources_alloc();
4760 if (!ctx
->resources
)
4766 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4768 resources_free(ctx
->resources
);
4774 struct list_head global_headers
;
4775 struct list_head local_headers
;
4776 struct dpipe_tables
*tables
;
4777 struct resources
*resources
;
4782 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4784 struct dpipe_header
*header
;
4786 header
= calloc(1, sizeof(struct dpipe_header
));
4789 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4790 if (!header
->fields
)
4791 goto err_fields_alloc
;
4792 header
->fields_count
= fields_count
;
4800 static void dpipe_header_free(struct dpipe_header
*header
)
4802 free(header
->fields
);
4806 static void dpipe_header_clear(struct dpipe_header
*header
)
4808 struct dpipe_field
*field
;
4811 for (i
= 0; i
< header
->fields_count
; i
++) {
4812 field
= &header
->fields
[i
];
4818 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4819 struct dpipe_header
*header
, bool global
)
4822 list_add(&header
->list
, &ctx
->global_headers
);
4824 list_add(&header
->list
, &ctx
->local_headers
);
4827 static void dpipe_header_del(struct dpipe_header
*header
)
4829 list_del(&header
->list
);
4832 static struct dpipe_table
*dpipe_table_alloc(void)
4834 return calloc(1, sizeof(struct dpipe_table
));
4837 static void dpipe_table_free(struct dpipe_table
*table
)
4842 static struct dpipe_tables
*dpipe_tables_alloc(void)
4844 struct dpipe_tables
*tables
;
4846 tables
= calloc(1, sizeof(struct dpipe_tables
));
4849 INIT_LIST_HEAD(&tables
->table_list
);
4853 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4855 struct dpipe_table
*table
, *tmp
;
4857 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4858 dpipe_table_free(table
);
4862 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4864 ctx
->tables
= dpipe_tables_alloc();
4869 INIT_LIST_HEAD(&ctx
->global_headers
);
4870 INIT_LIST_HEAD(&ctx
->local_headers
);
4874 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4876 struct dpipe_header
*header
, *tmp
;
4878 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4880 dpipe_header_del(header
);
4881 dpipe_header_clear(header
);
4882 dpipe_header_free(header
);
4884 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4886 dpipe_header_del(header
);
4887 dpipe_header_clear(header
);
4888 dpipe_header_free(header
);
4890 dpipe_tables_free(ctx
->tables
);
4893 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4894 uint32_t header_id
, bool global
)
4896 struct list_head
*header_list
;
4897 struct dpipe_header
*header
;
4900 header_list
= &ctx
->global_headers
;
4902 header_list
= &ctx
->local_headers
;
4903 list_for_each_entry(header
, header_list
, list
) {
4904 if (header
->id
!= header_id
)
4906 return header
->name
;
4911 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4913 uint32_t field_id
, bool global
)
4915 struct list_head
*header_list
;
4916 struct dpipe_header
*header
;
4919 header_list
= &ctx
->global_headers
;
4921 header_list
= &ctx
->local_headers
;
4922 list_for_each_entry(header
, header_list
, list
) {
4923 if (header
->id
!= header_id
)
4925 return header
->fields
[field_id
].name
;
4931 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4933 switch (mapping_type
) {
4934 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4936 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4944 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4945 uint32_t field_id
, bool global
)
4947 enum devlink_dpipe_field_mapping_type mapping_type
;
4948 struct list_head
*header_list
;
4949 struct dpipe_header
*header
;
4952 header_list
= &ctx
->global_headers
;
4954 header_list
= &ctx
->local_headers
;
4955 list_for_each_entry(header
, header_list
, list
) {
4956 if (header
->id
!= header_id
)
4958 mapping_type
= header
->fields
[field_id
].mapping_type
;
4959 return dpipe_field_mapping_e2s(mapping_type
);
4964 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4965 struct dpipe_field
*fields
,
4966 unsigned int field_count
)
4968 struct dpipe_field
*field
;
4971 for (i
= 0; i
< field_count
; i
++) {
4973 pr_out_entry_start(ctx
->dl
);
4974 check_indent_newline(ctx
->dl
);
4975 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4976 if (ctx
->dl
->verbose
)
4977 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4978 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4979 if (field
->mapping_type
) {
4980 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4981 dpipe_field_mapping_e2s(field
->mapping_type
));
4983 pr_out_entry_end(ctx
->dl
);
4988 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4989 struct dpipe_header
*header
, bool global
)
4991 pr_out_handle_start_arr(ctx
->dl
, tb
);
4992 check_indent_newline(ctx
->dl
);
4993 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4994 if (ctx
->dl
->verbose
) {
4995 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4996 print_bool(PRINT_ANY
, "global", " global %s", global
);
4998 pr_out_array_start(ctx
->dl
, "field");
4999 pr_out_dpipe_fields(ctx
, header
->fields
,
5000 header
->fields_count
);
5001 pr_out_array_end(ctx
->dl
);
5002 pr_out_handle_end(ctx
->dl
);
5005 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
5008 struct dpipe_header
*header
;
5010 list_for_each_entry(header
, &ctx
->local_headers
, list
)
5011 pr_out_dpipe_header(ctx
, tb
, header
, false);
5013 list_for_each_entry(header
, &ctx
->global_headers
, list
)
5014 pr_out_dpipe_header(ctx
, tb
, header
, true);
5017 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
5019 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5023 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
5024 if (err
!= MNL_CB_OK
)
5026 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
5027 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
5028 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
5029 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
5032 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
5033 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5034 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
5035 field
->name
= strdup(name
);
5038 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
5042 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
5043 struct dpipe_field
*fields
)
5045 struct nlattr
*nla_field
;
5049 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
5050 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
5058 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
5060 struct nlattr
*nla_field
;
5061 unsigned int count
= 0;
5063 mnl_attr_for_each_nested(nla_field
, nla_fields
)
5068 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5070 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
5071 struct dpipe_header
*header
;
5072 unsigned int fields_count
;
5073 const char *header_name
;
5077 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
5078 if (err
!= MNL_CB_OK
)
5081 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
5082 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5083 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
5086 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
5087 header
= dpipe_header_alloc(fields_count
);
5091 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
5092 header
->name
= strdup(header_name
);
5093 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5094 header
->fields_count
= fields_count
;
5095 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5097 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
5101 dpipe_header_add(ctx
, header
, global
);
5105 dpipe_header_free(header
);
5109 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5111 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
5112 struct nlattr
*nla_header
;
5115 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
5116 err
= dpipe_header_get(ctx
, nla_header
);
5123 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
5125 struct dpipe_ctx
*ctx
= data
;
5126 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5127 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5130 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5131 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5132 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
5133 return MNL_CB_ERROR
;
5134 err
= dpipe_headers_get(ctx
, tb
);
5137 return MNL_CB_ERROR
;
5140 if (ctx
->print_headers
)
5141 pr_out_dpipe_headers(ctx
, tb
);
5145 static int cmd_dpipe_headers_show(struct dl
*dl
)
5147 struct nlmsghdr
*nlh
;
5148 struct dpipe_ctx ctx
= {};
5149 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5152 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5154 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5158 err
= dpipe_ctx_init(&ctx
, dl
);
5162 ctx
.print_headers
= true;
5164 pr_out_section_start(dl
, "header");
5165 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5167 pr_err("error get headers %s\n", strerror(ctx
.err
));
5168 pr_out_section_end(dl
);
5170 dpipe_ctx_fini(&ctx
);
5174 static void cmd_dpipe_help(void)
5176 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5177 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5178 pr_err(" [ counters_enabled { true | false } ]\n");
5179 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5180 pr_err(" devlink dpipe header show DEV\n");
5183 static int cmd_dpipe_header(struct dl
*dl
)
5185 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5188 } else if (dl_argv_match(dl
, "show")) {
5190 return cmd_dpipe_headers_show(dl
);
5192 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5197 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5199 switch (action_type
) {
5200 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5201 return "field_modify";
5207 struct dpipe_op_info
{
5213 struct dpipe_action
{
5214 struct dpipe_op_info info
;
5218 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5219 struct dpipe_ctx
*ctx
)
5221 struct dpipe_op_info
*op_info
= &action
->info
;
5222 const char *mapping
;
5224 check_indent_newline(ctx
->dl
);
5225 print_string(PRINT_ANY
, "type", "type %s",
5226 dpipe_action_type_e2s(action
->type
));
5227 print_string(PRINT_ANY
, "header", " header %s",
5228 dpipe_header_id2s(ctx
, op_info
->header_id
,
5229 op_info
->header_global
));
5230 print_string(PRINT_ANY
, "field", " field %s",
5231 dpipe_field_id2s(ctx
, op_info
->header_id
,
5233 op_info
->header_global
));
5234 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5236 op_info
->header_global
);
5238 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5241 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5243 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5246 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5247 if (err
!= MNL_CB_OK
)
5250 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5251 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5252 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5253 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5257 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5258 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5259 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5260 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5265 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5266 struct nlattr
*nla_actions
)
5268 struct nlattr
*nla_action
;
5269 struct dpipe_action action
;
5271 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5272 pr_out_entry_start(ctx
->dl
);
5273 if (dpipe_action_parse(&action
, nla_action
))
5274 goto err_action_parse
;
5275 pr_out_dpipe_action(&action
, ctx
);
5276 pr_out_entry_end(ctx
->dl
);
5281 pr_out_entry_end(ctx
->dl
);
5286 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5288 switch (match_type
) {
5289 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5290 return "field_exact";
5296 struct dpipe_match
{
5297 struct dpipe_op_info info
;
5301 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5302 struct dpipe_ctx
*ctx
)
5304 struct dpipe_op_info
*op_info
= &match
->info
;
5305 const char *mapping
;
5307 check_indent_newline(ctx
->dl
);
5308 print_string(PRINT_ANY
, "type", "type %s",
5309 dpipe_match_type_e2s(match
->type
));
5310 print_string(PRINT_ANY
, "header", " header %s",
5311 dpipe_header_id2s(ctx
, op_info
->header_id
,
5312 op_info
->header_global
));
5313 print_string(PRINT_ANY
, "field", " field %s",
5314 dpipe_field_id2s(ctx
, op_info
->header_id
,
5316 op_info
->header_global
));
5317 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5319 op_info
->header_global
);
5321 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5324 static int dpipe_match_parse(struct dpipe_match
*match
,
5328 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5331 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5332 if (err
!= MNL_CB_OK
)
5335 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5336 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5337 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5338 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5342 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5343 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5344 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5345 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5350 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5351 struct nlattr
*nla_matches
)
5353 struct nlattr
*nla_match
;
5354 struct dpipe_match match
;
5356 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5357 pr_out_entry_start(ctx
->dl
);
5358 if (dpipe_match_parse(&match
, nla_match
))
5359 goto err_match_parse
;
5360 pr_out_dpipe_match(&match
, ctx
);
5361 pr_out_entry_end(ctx
->dl
);
5366 pr_out_entry_end(ctx
->dl
);
5370 static struct resource
*
5371 resource_find(struct resources
*resources
, struct resource
*resource
,
5372 uint64_t resource_id
)
5374 struct list_head
*list_head
;
5377 list_head
= &resources
->resource_list
;
5379 list_head
= &resource
->resource_list
;
5381 list_for_each_entry(resource
, list_head
, list
) {
5382 struct resource
*child_resource
;
5384 if (resource
->id
== resource_id
)
5387 child_resource
= resource_find(resources
, resource
,
5390 return child_resource
;
5396 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5397 uint64_t resource_id
)
5399 struct resource
*resource
, *parent_resource
;
5400 const char del
[] = "/";
5404 resource
= resource_find(resources
, NULL
, resource_id
);
5408 for (parent_resource
= resource
; parent_resource
;
5409 parent_resource
= parent_resource
->parent
)
5410 path_len
+= strlen(parent_resource
->name
) + 1;
5413 path
= calloc(1, path_len
);
5417 path
+= path_len
- 1;
5418 for (parent_resource
= resource
; parent_resource
;
5419 parent_resource
= parent_resource
->parent
) {
5420 path
-= strlen(parent_resource
->name
);
5421 memcpy(path
, parent_resource
->name
,
5422 strlen(parent_resource
->name
));
5423 path
-= strlen(del
);
5424 memcpy(path
, del
, strlen(del
));
5426 check_indent_newline(dl
);
5427 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5431 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5433 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5434 struct dpipe_table
*table
;
5435 uint32_t resource_units
;
5436 bool counters_enabled
;
5437 bool resource_valid
;
5441 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5442 if (err
!= MNL_CB_OK
)
5445 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5446 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5447 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5448 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5449 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5453 table
= dpipe_table_alloc();
5457 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5458 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5459 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5461 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5463 if (resource_valid
) {
5464 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5465 table
->resource_valid
= true;
5468 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5469 if (!ctx
->print_tables
)
5472 check_indent_newline(ctx
->dl
);
5473 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5474 print_uint(PRINT_ANY
, "size", " size %u", size
);
5475 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5477 if (resource_valid
) {
5478 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5479 resource_path_print(ctx
->dl
, ctx
->resources
,
5480 table
->resource_id
);
5481 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5485 pr_out_array_start(ctx
->dl
, "match");
5486 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5487 goto err_matches_show
;
5488 pr_out_array_end(ctx
->dl
);
5490 pr_out_array_start(ctx
->dl
, "action");
5491 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5492 goto err_actions_show
;
5493 pr_out_array_end(ctx
->dl
);
5499 pr_out_array_end(ctx
->dl
);
5503 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5505 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5506 struct nlattr
*nla_table
;
5508 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5509 if (ctx
->print_tables
)
5510 pr_out_handle_start_arr(ctx
->dl
, tb
);
5511 if (dpipe_table_show(ctx
, nla_table
))
5512 goto err_table_show
;
5513 if (ctx
->print_tables
)
5514 pr_out_handle_end(ctx
->dl
);
5519 if (ctx
->print_tables
)
5520 pr_out_handle_end(ctx
->dl
);
5524 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5526 struct dpipe_ctx
*ctx
= data
;
5527 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5528 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5530 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5531 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5532 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5533 return MNL_CB_ERROR
;
5535 if (dpipe_tables_show(ctx
, tb
))
5536 return MNL_CB_ERROR
;
5540 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5542 static int cmd_dpipe_table_show(struct dl
*dl
)
5544 struct nlmsghdr
*nlh
;
5545 struct dpipe_ctx dpipe_ctx
= {};
5546 struct resource_ctx resource_ctx
= {};
5547 uint16_t flags
= NLM_F_REQUEST
;
5550 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5554 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5556 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5560 dpipe_ctx
.print_tables
= true;
5562 dl_opts_put(nlh
, dl
);
5563 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5566 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5567 goto err_headers_get
;
5570 err
= resource_ctx_init(&resource_ctx
, dl
);
5572 goto err_resource_ctx_init
;
5574 resource_ctx
.print_resources
= false;
5575 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5576 dl_opts_put(nlh
, dl
);
5577 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5580 dpipe_ctx
.resources
= resource_ctx
.resources
;
5582 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5583 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5584 dl_opts_put(nlh
, dl
);
5586 pr_out_section_start(dl
, "table");
5587 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5588 pr_out_section_end(dl
);
5590 resource_ctx_fini(&resource_ctx
);
5591 dpipe_ctx_fini(&dpipe_ctx
);
5594 err_resource_ctx_init
:
5596 dpipe_ctx_fini(&dpipe_ctx
);
5600 static int cmd_dpipe_table_set(struct dl
*dl
)
5602 struct nlmsghdr
*nlh
;
5605 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5606 NLM_F_REQUEST
| NLM_F_ACK
);
5608 err
= dl_argv_parse_put(nlh
, dl
,
5609 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5610 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5614 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5617 enum dpipe_value_type
{
5618 DPIPE_VALUE_TYPE_VALUE
,
5619 DPIPE_VALUE_TYPE_MASK
,
5623 dpipe_value_type_e2s(enum dpipe_value_type type
)
5626 case DPIPE_VALUE_TYPE_VALUE
:
5628 case DPIPE_VALUE_TYPE_MASK
:
5629 return "value_mask";
5635 struct dpipe_field_printer
{
5636 unsigned int field_id
;
5637 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5640 struct dpipe_header_printer
{
5641 struct dpipe_field_printer
*printers
;
5642 unsigned int printers_count
;
5643 unsigned int header_id
;
5646 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5647 enum dpipe_value_type type
,
5650 struct in_addr ip_addr
;
5652 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5653 check_indent_newline(ctx
->dl
);
5654 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5658 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5659 enum dpipe_value_type type
,
5662 check_indent_newline(ctx
->dl
);
5663 print_string_name_value(dpipe_value_type_e2s(type
),
5664 ether_ntoa((struct ether_addr
*)value
));
5667 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5668 enum dpipe_value_type type
,
5671 char str
[INET6_ADDRSTRLEN
];
5673 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5674 check_indent_newline(ctx
->dl
);
5675 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5678 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5680 .printer
= dpipe_field_printer_ipv4_addr
,
5681 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5685 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5686 .printers
= dpipe_field_printers_ipv4
,
5687 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5688 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5691 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5693 .printer
= dpipe_field_printer_ethernet_addr
,
5694 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5698 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5699 .printers
= dpipe_field_printers_ethernet
,
5700 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5701 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5704 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5706 .printer
= dpipe_field_printer_ipv6_addr
,
5707 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5711 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5712 .printers
= dpipe_field_printers_ipv6
,
5713 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5714 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5717 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5718 &dpipe_header_printer_ipv4
,
5719 &dpipe_header_printer_ethernet
,
5720 &dpipe_header_printer_ipv6
,
5723 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5724 struct dpipe_op_info
*info
,
5725 enum dpipe_value_type type
,
5728 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5729 struct dpipe_header_printer
*header_printer
;
5730 struct dpipe_field_printer
*field_printer
;
5731 unsigned int field_printers_count
;
5735 for (i
= 0; i
< header_printers_count
; i
++) {
5736 header_printer
= dpipe_header_printers
[i
];
5737 if (header_printer
->header_id
!= info
->header_id
)
5739 field_printers_count
= header_printer
->printers_count
;
5740 for (j
= 0; j
< field_printers_count
; j
++) {
5741 field_printer
= &header_printer
->printers
[j
];
5742 if (field_printer
->field_id
!= info
->field_id
)
5744 field_printer
->printer(ctx
, type
, value
);
5752 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5754 unsigned int value_len
,
5755 struct dpipe_op_info
*info
,
5756 enum dpipe_value_type type
)
5758 if (info
->header_global
&&
5759 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5762 if (value_len
== sizeof(uint32_t)) {
5763 uint32_t *value_32
= value
;
5765 check_indent_newline(ctx
->dl
);
5766 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5770 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5771 struct nlattr
**nla_match_value
,
5772 struct dpipe_op_info
*info
)
5774 void *value
, *value_mask
;
5775 uint32_t value_mapping
;
5779 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5780 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5782 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5783 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5786 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5787 check_indent_newline(ctx
->dl
);
5788 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5792 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5793 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5794 DPIPE_VALUE_TYPE_MASK
);
5797 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5800 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5803 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5804 struct dpipe_match match
;
5807 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5808 if (err
!= MNL_CB_OK
)
5811 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5812 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5816 pr_out_entry_start(ctx
->dl
);
5817 if (dpipe_match_parse(&match
,
5818 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5819 goto err_match_parse
;
5820 pr_out_dpipe_match(&match
, ctx
);
5821 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5822 pr_out_entry_end(ctx
->dl
);
5827 pr_out_entry_end(ctx
->dl
);
5831 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5834 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5835 struct dpipe_action action
;
5838 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5839 if (err
!= MNL_CB_OK
)
5842 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5843 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5847 pr_out_entry_start(ctx
->dl
);
5848 if (dpipe_action_parse(&action
,
5849 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5850 goto err_action_parse
;
5851 pr_out_dpipe_action(&action
, ctx
);
5852 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5853 pr_out_entry_end(ctx
->dl
);
5858 pr_out_entry_end(ctx
->dl
);
5863 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5864 struct nlattr
*nla_action_values
)
5866 struct nlattr
*nla_action_value
;
5868 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5869 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5876 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5877 struct nlattr
*nla_match_values
)
5879 struct nlattr
*nla_match_value
;
5881 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5882 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5888 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5890 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5891 uint32_t entry_index
;
5895 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5896 if (err
!= MNL_CB_OK
)
5899 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5900 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5901 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5905 check_indent_newline(ctx
->dl
);
5906 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5907 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5909 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5910 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5911 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5914 pr_out_array_start(ctx
->dl
, "match_value");
5915 if (dpipe_tables_match_values_show(ctx
,
5916 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5917 goto err_match_values_show
;
5918 pr_out_array_end(ctx
->dl
);
5920 pr_out_array_start(ctx
->dl
, "action_value");
5921 if (dpipe_tables_action_values_show(ctx
,
5922 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5923 goto err_action_values_show
;
5924 pr_out_array_end(ctx
->dl
);
5927 err_action_values_show
:
5928 err_match_values_show
:
5929 pr_out_array_end(ctx
->dl
);
5933 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5935 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5936 struct nlattr
*nla_entry
;
5938 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5939 pr_out_handle_start_arr(ctx
->dl
, tb
);
5940 if (dpipe_entry_show(ctx
, nla_entry
))
5941 goto err_entry_show
;
5942 pr_out_handle_end(ctx
->dl
);
5947 pr_out_handle_end(ctx
->dl
);
5951 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5953 struct dpipe_ctx
*ctx
= data
;
5954 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5955 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5957 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5958 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5959 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5960 return MNL_CB_ERROR
;
5962 if (dpipe_table_entries_show(ctx
, tb
))
5963 return MNL_CB_ERROR
;
5967 static int cmd_dpipe_table_dump(struct dl
*dl
)
5969 struct nlmsghdr
*nlh
;
5970 struct dpipe_ctx ctx
= {};
5971 uint16_t flags
= NLM_F_REQUEST
;
5974 err
= dpipe_ctx_init(&ctx
, dl
);
5978 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5982 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5983 dl_opts_put(nlh
, dl
);
5984 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5986 pr_err("error get headers %s\n", strerror(ctx
.err
));
5990 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5991 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5992 dl_opts_put(nlh
, dl
);
5994 pr_out_section_start(dl
, "table_entry");
5995 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5996 pr_out_section_end(dl
);
5998 dpipe_ctx_fini(&ctx
);
6002 static int cmd_dpipe_table(struct dl
*dl
)
6004 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6007 } else if (dl_argv_match(dl
, "show")) {
6009 return cmd_dpipe_table_show(dl
);
6010 } else if (dl_argv_match(dl
, "set")) {
6012 return cmd_dpipe_table_set(dl
);
6013 } else if (dl_argv_match(dl
, "dump")) {
6015 return cmd_dpipe_table_dump(dl
);
6017 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6021 static int cmd_dpipe(struct dl
*dl
)
6023 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6026 } else if (dl_argv_match(dl
, "header")) {
6028 return cmd_dpipe_header(dl
);
6029 } else if (dl_argv_match(dl
, "table")) {
6031 return cmd_dpipe_table(dl
);
6033 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6038 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
6039 struct nlattr
**nla_resource
)
6041 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
6042 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
6043 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
6044 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
6045 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
6046 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
6047 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
6051 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
6052 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
6053 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
6054 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
6055 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
6056 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
6057 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
6059 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
6060 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
6062 resource
->size_new
= resource
->size
;
6064 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
6065 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
6066 resource
->occ_valid
= true;
6069 if (resource
->size_new
!= resource
->size
)
6070 ctx
->pending_change
= true;
6076 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
6077 struct resource
*parent_resource
, struct nlattr
*nl
)
6079 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
6080 struct nlattr
*nla_child_resource
;
6081 struct nlattr
*nla_resources
;
6091 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
6092 if (err
!= MNL_CB_OK
)
6095 err
= resource_parse(ctx
, resource
, nla_resource
);
6099 resource
->parent
= parent_resource
;
6100 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
6103 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
6104 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
6106 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
6107 struct resource
*child_resource
;
6108 struct list_head
*list
;
6110 child_resource
= resource_alloc();
6111 if (!child_resource
)
6115 list
= &ctx
->resources
->resource_list
;
6117 list
= &resource
->resource_list
;
6119 list_add_tail(&child_resource
->list
, list
);
6120 err
= resource_get(ctx
, child_resource
, resource
,
6121 nla_child_resource
);
6129 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
6132 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
6133 default: return "<unknown unit>";
6137 static void resource_show(struct resource
*resource
,
6138 struct resource_ctx
*ctx
)
6140 struct resource
*child_resource
;
6141 struct dpipe_table
*table
;
6142 struct dl
*dl
= ctx
->dl
;
6145 check_indent_newline(dl
);
6146 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6148 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6149 pr_out_u64(dl
, "size", resource
->size
);
6150 if (resource
->size
!= resource
->size_new
)
6151 pr_out_u64(dl
, "size_new", resource
->size_new
);
6152 if (resource
->occ_valid
)
6153 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6154 print_string(PRINT_ANY
, "unit", " unit %s",
6155 resource_unit_str_get(resource
->unit
));
6157 if (resource
->size_min
!= resource
->size_max
) {
6158 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6159 resource
->size_min
);
6160 pr_out_u64(dl
, "size_max", resource
->size_max
);
6161 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6162 resource
->size_gran
);
6165 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6166 if (table
->resource_id
== resource
->id
&&
6167 table
->resource_valid
)
6171 pr_out_array_start(dl
, "dpipe_tables");
6173 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6176 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6177 if (table
->resource_id
!= resource
->id
||
6178 !table
->resource_valid
)
6180 pr_out_entry_start(dl
);
6181 check_indent_newline(dl
);
6182 print_string(PRINT_ANY
, "table_name", "table_name %s",
6184 pr_out_entry_end(dl
);
6187 pr_out_array_end(dl
);
6189 if (list_empty(&resource
->resource_list
))
6192 if (ctx
->pending_change
) {
6193 check_indent_newline(dl
);
6194 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6195 resource
->size_valid
? "true" : "false");
6197 pr_out_array_start(dl
, "resources");
6198 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6199 pr_out_entry_start(dl
);
6200 resource_show(child_resource
, ctx
);
6201 pr_out_entry_end(dl
);
6203 pr_out_array_end(dl
);
6207 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6209 struct resources
*resources
= ctx
->resources
;
6210 struct resource
*resource
;
6212 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6213 pr_out_handle_start_arr(ctx
->dl
, tb
);
6214 resource_show(resource
, ctx
);
6215 pr_out_handle_end(ctx
->dl
);
6219 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6221 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6224 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6226 struct resource_ctx
*ctx
= data
;
6227 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6228 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6231 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6232 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6233 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6234 return MNL_CB_ERROR
;
6236 err
= resources_get(ctx
, tb
);
6239 return MNL_CB_ERROR
;
6242 if (ctx
->print_resources
)
6243 resources_show(ctx
, tb
);
6248 static int cmd_resource_show(struct dl
*dl
)
6250 struct nlmsghdr
*nlh
;
6251 struct dpipe_ctx dpipe_ctx
= {};
6252 struct resource_ctx resource_ctx
= {};
6255 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6259 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6261 dl_opts_put(nlh
, dl
);
6263 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6267 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6270 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6274 err
= resource_ctx_init(&resource_ctx
, dl
);
6278 resource_ctx
.print_resources
= true;
6279 resource_ctx
.tables
= dpipe_ctx
.tables
;
6280 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6281 NLM_F_REQUEST
| NLM_F_ACK
);
6282 dl_opts_put(nlh
, dl
);
6283 pr_out_section_start(dl
, "resources");
6284 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6286 pr_out_section_end(dl
);
6287 resource_ctx_fini(&resource_ctx
);
6289 dpipe_ctx_fini(&dpipe_ctx
);
6293 static void cmd_resource_help(void)
6295 pr_err("Usage: devlink resource show DEV\n"
6296 " devlink resource set DEV path PATH size SIZE\n");
6299 static struct resource
*
6300 resource_find_by_name(struct list_head
*list
, char *name
)
6302 struct resource
*resource
;
6304 list_for_each_entry(resource
, list
, list
) {
6305 if (!strcmp(resource
->name
, name
))
6312 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6313 uint32_t *p_resource_id
, bool *p_resource_valid
)
6315 struct resource
*resource
;
6316 uint32_t resource_id
= 0;
6317 char *resource_path_dup
;
6318 struct list_head
*list
;
6319 const char del
[] = "/";
6320 char *resource_name
;
6322 resource_path_dup
= strdup(resource_path
);
6323 list
= &ctx
->resources
->resource_list
;
6324 resource_name
= strtok(resource_path_dup
, del
);
6325 while (resource_name
!= NULL
) {
6326 resource
= resource_find_by_name(list
, resource_name
);
6328 goto err_resource_lookup
;
6330 list
= &resource
->resource_list
;
6331 resource_name
= strtok(NULL
, del
);
6332 resource_id
= resource
->id
;
6334 free(resource_path_dup
);
6335 *p_resource_valid
= true;
6336 *p_resource_id
= resource_id
;
6339 err_resource_lookup
:
6340 free(resource_path_dup
);
6344 static int cmd_resource_set(struct dl
*dl
)
6346 struct nlmsghdr
*nlh
;
6347 struct resource_ctx ctx
= {};
6350 err
= resource_ctx_init(&ctx
, dl
);
6354 ctx
.print_resources
= false;
6355 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6356 DL_OPT_RESOURCE_SIZE
, 0);
6360 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6362 dl_opts_put(nlh
, dl
);
6363 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6365 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6369 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6370 &dl
->opts
.resource_id
,
6371 &dl
->opts
.resource_id_valid
);
6373 pr_err("error parsing resource path %s\n", strerror(-err
));
6377 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6378 NLM_F_REQUEST
| NLM_F_ACK
);
6380 dl_opts_put(nlh
, dl
);
6381 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6383 resource_ctx_fini(&ctx
);
6387 static int cmd_resource(struct dl
*dl
)
6389 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6390 cmd_resource_help();
6392 } else if (dl_argv_match(dl
, "show")) {
6394 return cmd_resource_show(dl
);
6395 } else if (dl_argv_match(dl
, "set")) {
6397 return cmd_resource_set(dl
);
6399 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6403 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6405 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6406 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6407 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6410 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6411 if (dl
->json_output
)
6412 open_json_object(buf
);
6417 static void pr_out_region_handle_end(struct dl
*dl
)
6419 if (dl
->json_output
)
6420 close_json_object();
6425 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6427 __pr_out_indent_newline(dl
);
6428 if (dl
->json_output
)
6429 open_json_array(PRINT_JSON
, "snapshot");
6431 pr_out("snapshot %s", array
? "[" : "");
6434 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6436 if (dl
->json_output
)
6437 close_json_array(PRINT_JSON
, NULL
);
6442 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6444 uint32_t snapshot_id
;
6446 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6449 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6451 if (dl
->json_output
)
6452 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6454 pr_out("%s%u", index
? " " : "", snapshot_id
);
6457 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6459 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6460 struct nlattr
*nla_sanpshot
;
6463 pr_out_region_snapshots_start(dl
, true);
6464 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6465 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6466 if (err
!= MNL_CB_OK
)
6468 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6470 pr_out_region_snapshots_end(dl
, true);
6473 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6475 pr_out_region_snapshots_start(dl
, false);
6476 pr_out_region_snapshots_id(dl
, tb
, 0);
6477 pr_out_region_snapshots_end(dl
, false);
6480 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6482 pr_out_region_handle_start(dl
, tb
);
6484 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6485 pr_out_u64(dl
, "size",
6486 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6488 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6489 pr_out_snapshots(dl
, tb
);
6491 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6492 pr_out_snapshot(dl
, tb
);
6494 pr_out_region_handle_end(dl
);
6497 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6499 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6500 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6501 struct dl
*dl
= data
;
6503 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6504 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6505 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6506 return MNL_CB_ERROR
;
6508 pr_out_region(dl
, tb
);
6513 static int cmd_region_show(struct dl
*dl
)
6515 struct nlmsghdr
*nlh
;
6516 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6519 if (dl_argc(dl
) == 0)
6520 flags
|= NLM_F_DUMP
;
6522 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6524 if (dl_argc(dl
) > 0) {
6525 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6530 pr_out_section_start(dl
, "regions");
6531 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6532 pr_out_section_end(dl
);
6536 static int cmd_region_snapshot_del(struct dl
*dl
)
6538 struct nlmsghdr
*nlh
;
6541 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6542 NLM_F_REQUEST
| NLM_F_ACK
);
6544 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6545 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6549 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6552 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6554 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6555 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6556 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6557 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6558 struct dl
*dl
= data
;
6561 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6562 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6563 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6564 return MNL_CB_ERROR
;
6566 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6567 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6568 if (err
!= MNL_CB_OK
)
6569 return MNL_CB_ERROR
;
6571 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6572 if (!nla_chunk_data
)
6575 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6576 if (!nla_chunk_addr
)
6579 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6580 mnl_attr_get_payload_len(nla_chunk_data
),
6581 mnl_attr_get_u64(nla_chunk_addr
));
6586 static int cmd_region_dump(struct dl
*dl
)
6588 struct nlmsghdr
*nlh
;
6591 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6592 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6594 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6595 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6599 pr_out_section_start(dl
, "dump");
6600 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6601 pr_out_section_end(dl
);
6602 if (!dl
->json_output
)
6607 static int cmd_region_read(struct dl
*dl
)
6609 struct nlmsghdr
*nlh
;
6612 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6613 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6615 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6616 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6617 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6621 pr_out_section_start(dl
, "read");
6622 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6623 pr_out_section_end(dl
);
6624 if (!dl
->json_output
)
6629 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
6631 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6632 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6633 struct dl
*dl
= data
;
6635 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6636 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6637 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
6638 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6639 return MNL_CB_ERROR
;
6641 pr_out_region(dl
, tb
);
6646 static int cmd_region_snapshot_new(struct dl
*dl
)
6648 struct nlmsghdr
*nlh
;
6651 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
6652 NLM_F_REQUEST
| NLM_F_ACK
);
6654 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
6655 DL_OPT_REGION_SNAPSHOT_ID
);
6659 pr_out_section_start(dl
, "regions");
6660 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
6661 pr_out_section_end(dl
);
6665 static void cmd_region_help(void)
6667 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6668 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6669 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
6670 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6671 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6674 static int cmd_region(struct dl
*dl
)
6676 if (dl_no_arg(dl
)) {
6677 return cmd_region_show(dl
);
6678 } else if (dl_argv_match(dl
, "help")) {
6681 } else if (dl_argv_match(dl
, "show")) {
6683 return cmd_region_show(dl
);
6684 } else if (dl_argv_match(dl
, "del")) {
6686 return cmd_region_snapshot_del(dl
);
6687 } else if (dl_argv_match(dl
, "dump")) {
6689 return cmd_region_dump(dl
);
6690 } else if (dl_argv_match(dl
, "read")) {
6692 return cmd_region_read(dl
);
6693 } else if (dl_argv_match(dl
, "new")) {
6695 return cmd_region_snapshot_new(dl
);
6697 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6701 static int cmd_health_set_params(struct dl
*dl
)
6703 struct nlmsghdr
*nlh
;
6706 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6707 NLM_F_REQUEST
| NLM_F_ACK
);
6708 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6709 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6710 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
6711 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
6715 dl_opts_put(nlh
, dl
);
6716 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6719 static int cmd_health_dump_clear(struct dl
*dl
)
6721 struct nlmsghdr
*nlh
;
6724 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6725 NLM_F_REQUEST
| NLM_F_ACK
);
6727 err
= dl_argv_parse_put(nlh
, dl
,
6728 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6732 dl_opts_put(nlh
, dl
);
6733 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6736 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6741 check_indent_newline(dl
);
6744 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6747 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6750 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6753 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6756 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6758 case MNL_TYPE_NUL_STRING
:
6759 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6761 case MNL_TYPE_BINARY
:
6762 len
= mnl_attr_get_payload_len(nl_data
);
6763 data
= mnl_attr_get_payload(nl_data
);
6764 pr_out_binary_value(dl
, data
, len
);
6772 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6777 pr_out_name(dl
, *name
);
6784 struct list_head list
;
6787 struct fmsg_cb_data
{
6791 struct list_head entry_list
;
6794 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6795 uint8_t *attr_value
, bool insert
)
6797 struct nest_entry
*entry
;
6800 entry
= malloc(sizeof(struct nest_entry
));
6804 entry
->attr_type
= *attr_value
;
6805 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6807 if (list_empty(&fmsg_data
->entry_list
))
6808 return MNL_CB_ERROR
;
6809 entry
= list_first_entry(&fmsg_data
->entry_list
,
6810 struct nest_entry
, list
);
6811 *attr_value
= entry
->attr_type
;
6812 list_del(&entry
->list
);
6818 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6821 pr_out_fmsg_name(dl
, name
);
6823 __pr_out_indent_inc();
6826 static void pr_out_fmsg_group_end(struct dl
*dl
)
6829 __pr_out_indent_dec();
6832 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6834 if (dl
->json_output
) {
6835 pr_out_fmsg_name(dl
, name
);
6836 open_json_object(NULL
);
6838 pr_out_fmsg_group_start(dl
, name
);
6842 static void pr_out_fmsg_end_object(struct dl
*dl
)
6844 if (dl
->json_output
)
6845 close_json_object();
6847 pr_out_fmsg_group_end(dl
);
6850 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6852 if (dl
->json_output
) {
6853 pr_out_fmsg_name(dl
, name
);
6854 open_json_array(PRINT_JSON
, NULL
);
6856 pr_out_fmsg_group_start(dl
, name
);
6860 static void pr_out_fmsg_end_array(struct dl
*dl
)
6862 if (dl
->json_output
)
6863 close_json_array(PRINT_JSON
, NULL
);
6865 pr_out_fmsg_group_end(dl
);
6868 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6871 struct dl
*dl
= fmsg_data
->dl
;
6872 uint8_t value
= nest_value
;
6875 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6876 if (err
!= MNL_CB_OK
)
6880 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6882 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6884 pr_out_fmsg_end_object(dl
);
6886 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6888 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6890 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6892 pr_out_fmsg_end_array(dl
);
6900 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6902 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6903 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6904 struct fmsg_cb_data
*fmsg_data
= data
;
6905 struct dl
*dl
= fmsg_data
->dl
;
6906 struct nlattr
*nla_object
;
6910 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6911 if (!tb
[DEVLINK_ATTR_FMSG
])
6912 return MNL_CB_ERROR
;
6914 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6915 attr_type
= mnl_attr_get_type(nla_object
);
6916 switch (attr_type
) {
6917 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6918 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6919 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6920 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6921 if (err
!= MNL_CB_OK
)
6924 case DEVLINK_ATTR_FMSG_NEST_END
:
6925 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6926 if (err
!= MNL_CB_OK
)
6929 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6930 free(fmsg_data
->name
);
6931 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6932 if (!fmsg_data
->name
)
6935 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6936 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6938 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6939 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6940 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6942 if (err
!= MNL_CB_OK
)
6952 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6954 /* FMSG is dynamic: opening of an object or array causes a
6955 * newline. JSON starts with an { or [, but plain text should
6956 * not start with a new line. Ensure this by setting
6957 * g_new_line_count to 1: avoiding newline before the first
6960 g_new_line_count
= 1;
6963 INIT_LIST_HEAD(&data
->entry_list
);
6966 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6968 struct fmsg_cb_data data
;
6969 struct nlmsghdr
*nlh
;
6972 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6974 err
= dl_argv_parse_put(nlh
, dl
,
6975 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6979 cmd_fmsg_init(dl
, &data
);
6980 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6985 static int cmd_health_dump_show(struct dl
*dl
)
6987 return cmd_health_object_common(dl
,
6988 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6992 static int cmd_health_diagnose(struct dl
*dl
)
6994 return cmd_health_object_common(dl
,
6995 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6999 static int cmd_health_recover(struct dl
*dl
)
7001 struct nlmsghdr
*nlh
;
7004 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
7005 NLM_F_REQUEST
| NLM_F_ACK
);
7007 err
= dl_argv_parse_put(nlh
, dl
,
7008 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
7012 dl_opts_put(nlh
, dl
);
7013 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7016 enum devlink_health_reporter_state
{
7017 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
7018 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
7021 static const char *health_state_name(uint8_t state
)
7024 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
7025 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
7026 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
7027 return HEALTH_REPORTER_STATE_ERROR_STR
;
7029 return "<unknown state>";
7033 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
7035 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7036 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7037 uint64_t time_ms
= mnl_attr_get_u64(attr
);
7038 struct sysinfo s_info
;
7044 info
= localtime(&now
);
7045 err
= sysinfo(&s_info
);
7048 /* Subtract uptime in sec from now yields the time of system
7049 * uptime. To this, add time_ms which is the amount of
7050 * milliseconds elapsed between uptime and the dump taken.
7052 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
7053 info
= localtime(&sec
);
7055 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
7056 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
7057 check_indent_newline(dl
);
7058 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7059 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7062 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
7064 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7065 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7070 ts
= mnl_attr_get_u64(attr
);
7071 tv_sec
= ts
/ 1000000000;
7072 tm
= localtime(&tv_sec
);
7074 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
7075 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
7077 check_indent_newline(dl
);
7078 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7079 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7082 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
7084 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7085 enum devlink_health_reporter_state state
;
7088 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
7090 if (err
!= MNL_CB_OK
)
7093 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
7094 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
7095 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
7096 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
7099 pr_out_handle_start_arr(dl
, tb_health
);
7101 check_indent_newline(dl
);
7102 print_string(PRINT_ANY
, "reporter", "reporter %s",
7103 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
7104 if (!dl
->json_output
) {
7106 __pr_out_indent_inc();
7108 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
7109 check_indent_newline(dl
);
7110 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
7111 pr_out_u64(dl
, "error",
7112 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
7113 pr_out_u64(dl
, "recover",
7114 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
7115 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
7116 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
7117 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
7118 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
7119 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
7120 pr_out_u64(dl
, "grace_period",
7121 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
7122 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
7123 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
7124 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
7125 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
7126 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
7127 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
7129 __pr_out_indent_dec();
7130 pr_out_handle_end(dl
);
7133 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7135 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7136 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7137 struct dl
*dl
= data
;
7139 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7140 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7141 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
7142 return MNL_CB_ERROR
;
7144 pr_out_health(dl
, tb
);
7149 static int cmd_health_show(struct dl
*dl
)
7151 struct nlmsghdr
*nlh
;
7152 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7155 if (dl_argc(dl
) == 0)
7156 flags
|= NLM_F_DUMP
;
7157 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7160 if (dl_argc(dl
) > 0) {
7161 err
= dl_argv_parse_put(nlh
, dl
,
7163 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7167 pr_out_section_start(dl
, "health");
7169 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
7170 pr_out_section_end(dl
);
7174 static void cmd_health_help(void)
7176 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
7177 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
7178 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
7179 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
7180 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
7181 pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
7182 pr_err(" [ grace_period MSEC ]\n");
7183 pr_err(" [ auto_recover { true | false } ]\n");
7184 pr_err(" [ auto_dump { true | false } ]\n");
7187 static int cmd_health(struct dl
*dl
)
7189 if (dl_argv_match(dl
, "help")) {
7192 } else if (dl_argv_match(dl
, "show") ||
7193 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7195 return cmd_health_show(dl
);
7196 } else if (dl_argv_match(dl
, "recover")) {
7198 return cmd_health_recover(dl
);
7199 } else if (dl_argv_match(dl
, "diagnose")) {
7201 return cmd_health_diagnose(dl
);
7202 } else if (dl_argv_match(dl
, "dump")) {
7204 if (dl_argv_match(dl
, "show")) {
7206 return cmd_health_dump_show(dl
);
7207 } else if (dl_argv_match(dl
, "clear")) {
7209 return cmd_health_dump_clear(dl
);
7211 } else if (dl_argv_match(dl
, "set")) {
7213 return cmd_health_set_params(dl
);
7215 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7219 static const char *trap_type_name(uint8_t type
)
7222 case DEVLINK_TRAP_TYPE_DROP
:
7224 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7226 case DEVLINK_TRAP_TYPE_CONTROL
:
7229 return "<unknown type>";
7233 static const char *trap_action_name(uint8_t action
)
7236 case DEVLINK_TRAP_ACTION_DROP
:
7238 case DEVLINK_TRAP_ACTION_TRAP
:
7240 case DEVLINK_TRAP_ACTION_MIRROR
:
7243 return "<unknown action>";
7247 static const char *trap_metadata_name(const struct nlattr
*attr
)
7249 switch (attr
->nla_type
) {
7250 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7251 return "input_port";
7252 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7253 return "flow_action_cookie";
7255 return "<unknown metadata type>";
7258 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7260 struct nlattr
*attr_metadata
;
7262 pr_out_array_start(dl
, "metadata");
7263 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7264 check_indent_newline(dl
);
7265 print_string(PRINT_ANY
, NULL
, "%s",
7266 trap_metadata_name(attr_metadata
));
7268 pr_out_array_end(dl
);
7271 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7273 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7274 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7277 pr_out_handle_start_arr(dl
, tb
);
7279 __pr_out_handle_start(dl
, tb
, true, false);
7281 check_indent_newline(dl
);
7282 print_string(PRINT_ANY
, "name", "name %s",
7283 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7284 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7285 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7286 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7287 print_string(PRINT_ANY
, "group", " group %s",
7288 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7290 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7291 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7292 pr_out_handle_end(dl
);
7295 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7297 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7298 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7299 struct dl
*dl
= data
;
7301 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7302 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7303 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7304 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7305 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7306 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7307 return MNL_CB_ERROR
;
7309 pr_out_trap(dl
, tb
, true);
7314 static void cmd_trap_help(void)
7316 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7317 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7318 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7319 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7320 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7321 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7322 pr_err(" devlink trap policer show DEV policer POLICER\n");
7325 static int cmd_trap_show(struct dl
*dl
)
7327 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7328 struct nlmsghdr
*nlh
;
7331 if (dl_argc(dl
) == 0)
7332 flags
|= NLM_F_DUMP
;
7334 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7336 if (dl_argc(dl
) > 0) {
7337 err
= dl_argv_parse_put(nlh
, dl
,
7338 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7343 pr_out_section_start(dl
, "trap");
7344 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7345 pr_out_section_end(dl
);
7350 static int cmd_trap_set(struct dl
*dl
)
7352 struct nlmsghdr
*nlh
;
7355 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7356 NLM_F_REQUEST
| NLM_F_ACK
);
7358 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7359 DL_OPT_TRAP_ACTION
);
7363 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7366 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7369 pr_out_handle_start_arr(dl
, tb
);
7371 __pr_out_handle_start(dl
, tb
, true, false);
7373 check_indent_newline(dl
);
7374 print_string(PRINT_ANY
, "name", "name %s",
7375 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7376 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7377 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7378 print_uint(PRINT_ANY
, "policer", " policer %u",
7379 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7380 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7381 pr_out_handle_end(dl
);
7384 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7386 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7387 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7388 struct dl
*dl
= data
;
7390 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7391 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7392 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7393 return MNL_CB_ERROR
;
7395 pr_out_trap_group(dl
, tb
, true);
7400 static int cmd_trap_group_show(struct dl
*dl
)
7402 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7403 struct nlmsghdr
*nlh
;
7406 if (dl_argc(dl
) == 0)
7407 flags
|= NLM_F_DUMP
;
7409 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7411 if (dl_argc(dl
) > 0) {
7412 err
= dl_argv_parse_put(nlh
, dl
,
7413 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7419 pr_out_section_start(dl
, "trap_group");
7420 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7421 pr_out_section_end(dl
);
7426 static int cmd_trap_group_set(struct dl
*dl
)
7428 struct nlmsghdr
*nlh
;
7431 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7432 NLM_F_REQUEST
| NLM_F_ACK
);
7434 err
= dl_argv_parse_put(nlh
, dl
,
7435 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7436 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7440 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7443 static int cmd_trap_group(struct dl
*dl
)
7445 if (dl_argv_match(dl
, "help")) {
7448 } else if (dl_argv_match(dl
, "show") ||
7449 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7451 return cmd_trap_group_show(dl
);
7452 } else if (dl_argv_match(dl
, "set")) {
7454 return cmd_trap_group_set(dl
);
7456 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7460 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7463 pr_out_handle_start_arr(dl
, tb
);
7465 __pr_out_handle_start(dl
, tb
, true, false);
7467 check_indent_newline(dl
);
7468 print_uint(PRINT_ANY
, "policer", "policer %u",
7469 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7470 print_u64(PRINT_ANY
, "rate", " rate %llu",
7471 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7472 print_u64(PRINT_ANY
, "burst", " burst %llu",
7473 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7474 if (tb
[DEVLINK_ATTR_STATS
])
7475 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7476 pr_out_handle_end(dl
);
7479 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7481 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7482 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7483 struct dl
*dl
= data
;
7485 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7486 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7487 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7488 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7489 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7490 return MNL_CB_ERROR
;
7492 pr_out_trap_policer(dl
, tb
, true);
7497 static int cmd_trap_policer_show(struct dl
*dl
)
7499 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7500 struct nlmsghdr
*nlh
;
7503 if (dl_argc(dl
) == 0)
7504 flags
|= NLM_F_DUMP
;
7506 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7508 if (dl_argc(dl
) > 0) {
7509 err
= dl_argv_parse_put(nlh
, dl
,
7510 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7516 pr_out_section_start(dl
, "trap_policer");
7517 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7518 pr_out_section_end(dl
);
7523 static int cmd_trap_policer_set(struct dl
*dl
)
7525 struct nlmsghdr
*nlh
;
7528 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7529 NLM_F_REQUEST
| NLM_F_ACK
);
7531 err
= dl_argv_parse_put(nlh
, dl
,
7532 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7533 DL_OPT_TRAP_POLICER_RATE
|
7534 DL_OPT_TRAP_POLICER_BURST
);
7538 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7541 static int cmd_trap_policer(struct dl
*dl
)
7543 if (dl_argv_match(dl
, "help")) {
7546 } else if (dl_argv_match(dl
, "show") ||
7547 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7549 return cmd_trap_policer_show(dl
);
7550 } else if (dl_argv_match(dl
, "set")) {
7552 return cmd_trap_policer_set(dl
);
7554 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7558 static int cmd_trap(struct dl
*dl
)
7560 if (dl_argv_match(dl
, "help")) {
7563 } else if (dl_argv_match(dl
, "show") ||
7564 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7566 return cmd_trap_show(dl
);
7567 } else if (dl_argv_match(dl
, "set")) {
7569 return cmd_trap_set(dl
);
7570 } else if (dl_argv_match(dl
, "group")) {
7572 return cmd_trap_group(dl
);
7573 } else if (dl_argv_match(dl
, "policer")) {
7575 return cmd_trap_policer(dl
);
7577 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7581 static void help(void)
7583 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7584 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7585 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7586 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7589 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7594 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7597 } else if (dl_argv_match(dl
, "dev")) {
7600 } else if (dl_argv_match(dl
, "port")) {
7602 return cmd_port(dl
);
7603 } else if (dl_argv_match(dl
, "sb")) {
7606 } else if (dl_argv_match(dl
, "monitor")) {
7609 } else if (dl_argv_match(dl
, "dpipe")) {
7611 return cmd_dpipe(dl
);
7612 } else if (dl_argv_match(dl
, "resource")) {
7614 return cmd_resource(dl
);
7615 } else if (dl_argv_match(dl
, "region")) {
7617 return cmd_region(dl
);
7618 } else if (dl_argv_match(dl
, "health")) {
7620 return cmd_health(dl
);
7621 } else if (dl_argv_match(dl
, "trap")) {
7623 return cmd_trap(dl
);
7625 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7629 static int dl_init(struct dl
*dl
)
7633 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7635 pr_err("Failed to connect to devlink Netlink\n");
7639 err
= ifname_map_init(dl
);
7641 pr_err("Failed to create index map\n");
7642 goto err_ifname_map_create
;
7644 new_json_obj_plain(dl
->json_output
);
7647 err_ifname_map_create
:
7648 mnlg_socket_close(dl
->nlg
);
7652 static void dl_fini(struct dl
*dl
)
7654 delete_json_obj_plain();
7655 ifname_map_fini(dl
);
7656 mnlg_socket_close(dl
->nlg
);
7659 static struct dl
*dl_alloc(void)
7663 dl
= calloc(1, sizeof(*dl
));
7669 static void dl_free(struct dl
*dl
)
7674 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7678 int ret
= EXIT_SUCCESS
;
7680 if (name
&& strcmp(name
, "-") != 0) {
7681 if (freopen(name
, "r", stdin
) == NULL
) {
7683 "Cannot open file \"%s\" for reading: %s\n",
7684 name
, strerror(errno
));
7685 return EXIT_FAILURE
;
7690 while (getcmdline(&line
, &len
, stdin
) != -1) {
7694 largc
= makeargs(line
, largv
, 100);
7696 continue; /* blank line */
7698 if (dl_cmd(dl
, largc
, largv
)) {
7699 fprintf(stderr
, "Command failed %s:%d\n",
7713 int main(int argc
, char **argv
)
7715 static const struct option long_options
[] = {
7716 { "Version", no_argument
, NULL
, 'V' },
7717 { "force", no_argument
, NULL
, 'f' },
7718 { "batch", required_argument
, NULL
, 'b' },
7719 { "no-nice-names", no_argument
, NULL
, 'n' },
7720 { "json", no_argument
, NULL
, 'j' },
7721 { "pretty", no_argument
, NULL
, 'p' },
7722 { "verbose", no_argument
, NULL
, 'v' },
7723 { "statistics", no_argument
, NULL
, 's' },
7724 { "Netns", required_argument
, NULL
, 'N' },
7725 { NULL
, 0, NULL
, 0 }
7727 const char *batch_file
= NULL
;
7736 pr_err("Failed to allocate memory for devlink\n");
7737 return EXIT_FAILURE
;
7740 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7741 long_options
, NULL
)) >= 0) {
7745 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7752 batch_file
= optarg
;
7755 dl
->no_nice_names
= true;
7758 dl
->json_output
= true;
7770 if (netns_switch(optarg
)) {
7776 pr_err("Unknown option.\n");
7793 err
= dl_batch(dl
, batch_file
, force
);
7795 err
= dl_cmd(dl
, argc
, argv
);