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>
42 #include "json_print.h"
44 #include "namespace.h"
46 #define ESWITCH_MODE_LEGACY "legacy"
47 #define ESWITCH_MODE_SWITCHDEV "switchdev"
48 #define ESWITCH_INLINE_MODE_NONE "none"
49 #define ESWITCH_INLINE_MODE_LINK "link"
50 #define ESWITCH_INLINE_MODE_NETWORK "network"
51 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
53 #define ESWITCH_ENCAP_MODE_NONE "none"
54 #define ESWITCH_ENCAP_MODE_BASIC "basic"
56 #define PARAM_CMODE_RUNTIME_STR "runtime"
57 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
58 #define PARAM_CMODE_PERMANENT_STR "permanent"
59 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
61 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
62 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
63 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
65 static int g_new_line_count
;
66 static int g_indent_level
;
67 static bool g_indent_newline
;
69 #define INDENT_STR_STEP 2
70 #define INDENT_STR_MAXLEN 32
71 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
73 static void __attribute__((format(printf
, 1, 2)))
74 pr_err(const char *fmt
, ...)
79 vfprintf(stderr
, fmt
, ap
);
83 static void __attribute__((format(printf
, 1, 2)))
84 pr_out(const char *fmt
, ...)
88 if (g_indent_newline
) {
89 printf("%s", g_indent_str
);
90 g_indent_newline
= false;
98 static void __attribute__((format(printf
, 2, 3)))
99 pr_out_sp(unsigned int num
, const char *fmt
, ...)
105 ret
= vprintf(fmt
, ap
);
109 printf("%*s", num
- ret
, "");
110 g_new_line_count
= 0; \
113 static void __attribute__((format(printf
, 1, 2)))
114 pr_out_tty(const char *fmt
, ...)
118 if (!isatty(STDOUT_FILENO
))
125 static void __pr_out_indent_inc(void)
127 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
129 g_indent_level
+= INDENT_STR_STEP
;
130 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
131 g_indent_str
[g_indent_level
] = '\0';
134 static void __pr_out_indent_dec(void)
136 if (g_indent_level
- INDENT_STR_STEP
< 0)
138 g_indent_level
-= INDENT_STR_STEP
;
139 g_indent_str
[g_indent_level
] = '\0';
142 static void __pr_out_newline(void)
144 if (g_new_line_count
< 1) {
146 g_indent_newline
= true;
151 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
152 mnl_cb_t data_cb
, void *data
)
156 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
158 pr_err("devlink answers: %s\n", strerror(errno
));
164 static void dummy_signal_handler(int signum
)
168 static int _mnlg_socket_recv_run_intr(struct mnlg_socket
*nlg
,
169 mnl_cb_t data_cb
, void *data
)
171 struct sigaction act
, oact
;
174 act
.sa_handler
= dummy_signal_handler
;
175 sigemptyset(&act
.sa_mask
);
176 act
.sa_flags
= SA_NODEFER
;
178 sigaction(SIGINT
, &act
, &oact
);
179 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
180 sigaction(SIGINT
, &oact
, NULL
);
181 if (err
< 0 && errno
!= EINTR
) {
182 pr_err("devlink answers: %s\n", strerror(errno
));
188 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
189 const struct nlmsghdr
*nlh
)
193 err
= mnlg_socket_send(nlg
, nlh
);
195 pr_err("Failed to call mnlg_socket_send\n");
201 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
202 const struct nlmsghdr
*nlh
,
203 mnl_cb_t data_cb
, void *data
)
207 err
= _mnlg_socket_send(nlg
, nlh
);
210 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
213 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
214 const char *group_name
)
218 err
= mnlg_socket_group_add(nlg
, group_name
);
220 pr_err("Failed to call mnlg_socket_group_add\n");
227 struct list_head list
;
234 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
235 const char *dev_name
,
239 struct ifname_map
*ifname_map
;
241 ifname_map
= calloc(1, sizeof(*ifname_map
));
244 ifname_map
->bus_name
= strdup(bus_name
);
245 ifname_map
->dev_name
= strdup(dev_name
);
246 ifname_map
->port_index
= port_index
;
247 ifname_map
->ifname
= strdup(ifname
);
248 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
249 !ifname_map
->ifname
) {
250 free(ifname_map
->ifname
);
251 free(ifname_map
->dev_name
);
252 free(ifname_map
->bus_name
);
259 static void ifname_map_free(struct ifname_map
*ifname_map
)
261 free(ifname_map
->ifname
);
262 free(ifname_map
->dev_name
);
263 free(ifname_map
->bus_name
);
267 #define DL_OPT_HANDLE BIT(0)
268 #define DL_OPT_HANDLEP BIT(1)
269 #define DL_OPT_PORT_TYPE BIT(2)
270 #define DL_OPT_PORT_COUNT BIT(3)
271 #define DL_OPT_SB BIT(4)
272 #define DL_OPT_SB_POOL BIT(5)
273 #define DL_OPT_SB_SIZE BIT(6)
274 #define DL_OPT_SB_TYPE BIT(7)
275 #define DL_OPT_SB_THTYPE BIT(8)
276 #define DL_OPT_SB_TH BIT(9)
277 #define DL_OPT_SB_TC BIT(10)
278 #define DL_OPT_ESWITCH_MODE BIT(11)
279 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
280 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
281 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
282 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
283 #define DL_OPT_RESOURCE_PATH BIT(16)
284 #define DL_OPT_RESOURCE_SIZE BIT(17)
285 #define DL_OPT_PARAM_NAME BIT(18)
286 #define DL_OPT_PARAM_VALUE BIT(19)
287 #define DL_OPT_PARAM_CMODE BIT(20)
288 #define DL_OPT_HANDLE_REGION BIT(21)
289 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
290 #define DL_OPT_REGION_ADDRESS BIT(23)
291 #define DL_OPT_REGION_LENGTH BIT(24)
292 #define DL_OPT_FLASH_FILE_NAME BIT(25)
293 #define DL_OPT_FLASH_COMPONENT BIT(26)
294 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
295 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
296 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
297 #define DL_OPT_TRAP_NAME BIT(30)
298 #define DL_OPT_TRAP_ACTION BIT(31)
299 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
300 #define DL_OPT_NETNS BIT(33)
301 #define DL_OPT_TRAP_POLICER_ID BIT(34)
302 #define DL_OPT_TRAP_POLICER_RATE BIT(35)
303 #define DL_OPT_TRAP_POLICER_BURST BIT(36)
304 #define DL_OPT_HEALTH_REPORTER_AUTO_DUMP BIT(37)
305 #define DL_OPT_PORT_FUNCTION_HW_ADDR BIT(38)
306 #define DL_OPT_FLASH_OVERWRITE BIT(39)
307 #define DL_OPT_RELOAD_ACTION BIT(40)
308 #define DL_OPT_RELOAD_LIMIT BIT(41)
311 uint64_t present
; /* flags of present items */
315 enum devlink_port_type port_type
;
318 uint16_t sb_pool_index
;
319 uint32_t sb_pool_size
;
320 enum devlink_sb_pool_type sb_pool_type
;
321 enum devlink_sb_threshold_type sb_pool_thtype
;
322 uint32_t sb_threshold
;
323 uint16_t sb_tc_index
;
324 enum devlink_eswitch_mode eswitch_mode
;
325 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
326 const char *dpipe_table_name
;
327 bool dpipe_counters_enabled
;
328 enum devlink_eswitch_encap_mode eswitch_encap_mode
;
329 const char *resource_path
;
330 uint64_t resource_size
;
331 uint32_t resource_id
;
332 bool resource_id_valid
;
333 const char *param_name
;
334 const char *param_value
;
335 enum devlink_param_cmode cmode
;
337 uint32_t region_snapshot_id
;
338 uint64_t region_address
;
339 uint64_t region_length
;
340 const char *flash_file_name
;
341 const char *flash_component
;
342 const char *reporter_name
;
343 uint64_t reporter_graceful_period
;
344 bool reporter_auto_recover
;
345 bool reporter_auto_dump
;
346 const char *trap_name
;
347 const char *trap_group_name
;
348 enum devlink_trap_action trap_action
;
351 uint32_t trap_policer_id
;
352 uint64_t trap_policer_rate
;
353 uint64_t trap_policer_burst
;
354 char port_function_hw_addr
[MAX_ADDR_LEN
];
355 uint32_t port_function_hw_addr_len
;
356 uint32_t overwrite_mask
;
357 enum devlink_reload_action reload_action
;
358 enum devlink_reload_limit reload_limit
;
362 struct mnlg_socket
*nlg
;
363 struct list_head ifname_map_list
;
380 static int dl_argc(struct dl
*dl
)
385 static char *dl_argv(struct dl
*dl
)
387 if (dl_argc(dl
) == 0)
392 static void dl_arg_inc(struct dl
*dl
)
394 if (dl_argc(dl
) == 0)
400 static void dl_arg_dec(struct dl
*dl
)
406 static char *dl_argv_next(struct dl
*dl
)
410 if (dl_argc(dl
) == 0)
418 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
420 if (index
>= dl_argc(dl
))
422 return dl
->argv
[index
];
425 static int strcmpx(const char *str1
, const char *str2
)
427 if (strlen(str1
) > strlen(str2
))
429 return strncmp(str1
, str2
, strlen(str1
));
432 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
434 if (dl_argc(dl
) == 0)
436 return strcmpx(dl_argv(dl
), pattern
) == 0;
439 static bool dl_no_arg(struct dl
*dl
)
441 return dl_argc(dl
) == 0;
444 static void __pr_out_indent_newline(struct dl
*dl
)
446 if (!g_indent_newline
&& !dl
->json_output
)
450 static bool is_binary_eol(int i
)
455 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
461 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
463 pr_out("%02x ", data
[i
]);
465 if (!dl
->json_output
&& is_binary_eol(i
))
468 if (!dl
->json_output
&& !is_binary_eol(i
))
472 static void pr_out_name(struct dl
*dl
, const char *name
)
474 __pr_out_indent_newline(dl
);
476 print_string(PRINT_JSON
, name
, NULL
, NULL
);
481 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
483 __pr_out_indent_newline(dl
);
484 if (val
== (uint64_t) -1)
485 return print_string_name_value(name
, "unlimited");
488 print_u64(PRINT_JSON
, name
, NULL
, val
);
490 pr_out("%s %"PRIu64
, name
, val
);
493 static void pr_out_section_start(struct dl
*dl
, const char *name
)
495 if (dl
->json_output
) {
496 open_json_object(NULL
);
497 open_json_object(name
);
501 static void pr_out_section_end(struct dl
*dl
)
503 if (dl
->json_output
) {
504 if (dl
->arr_last
.present
)
505 close_json_array(PRINT_JSON
, NULL
);
511 static void pr_out_array_start(struct dl
*dl
, const char *name
)
513 if (dl
->json_output
) {
514 open_json_array(PRINT_JSON
, name
);
516 __pr_out_indent_inc();
519 __pr_out_indent_inc();
524 static void pr_out_array_end(struct dl
*dl
)
526 if (dl
->json_output
) {
527 close_json_array(PRINT_JSON
, NULL
);
529 __pr_out_indent_dec();
530 __pr_out_indent_dec();
534 static void pr_out_object_start(struct dl
*dl
, const char *name
)
536 if (dl
->json_output
) {
537 open_json_object(name
);
539 __pr_out_indent_inc();
542 __pr_out_indent_inc();
547 static void pr_out_object_end(struct dl
*dl
)
549 if (dl
->json_output
) {
552 __pr_out_indent_dec();
553 __pr_out_indent_dec();
557 static void pr_out_entry_start(struct dl
*dl
)
560 open_json_object(NULL
);
563 static void pr_out_entry_end(struct dl
*dl
)
571 static void check_indent_newline(struct dl
*dl
)
573 __pr_out_indent_newline(dl
);
575 if (g_indent_newline
&& !is_json_context()) {
576 printf("%s", g_indent_str
);
577 g_indent_newline
= false;
579 g_new_line_count
= 0;
582 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
583 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
584 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
585 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
586 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
587 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
588 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
589 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
590 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
591 [DEVLINK_ATTR_PORT_LANES
] = MNL_TYPE_U32
,
592 [DEVLINK_ATTR_PORT_SPLITTABLE
] = MNL_TYPE_U8
,
593 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
594 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
595 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
596 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
597 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
598 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
599 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
600 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
601 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
602 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
603 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
604 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
605 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
606 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
607 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
608 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
609 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
610 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
611 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
612 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
613 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
614 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
615 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
616 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
617 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
618 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
619 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
620 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
621 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
622 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
623 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
624 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
625 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
626 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
627 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
628 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
629 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
630 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
631 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
632 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
633 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
634 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
635 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
636 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
637 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
638 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
639 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
640 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
641 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
642 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
643 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
644 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
645 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
646 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
647 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
648 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
649 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
650 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
651 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
652 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
653 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
654 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
655 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
656 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
657 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
658 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
659 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
660 [DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
661 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
662 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
663 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
664 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
665 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
666 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
667 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
668 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
669 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
670 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
671 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
672 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
673 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
674 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
675 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
676 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
677 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
678 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
679 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
680 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
681 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
682 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
683 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
684 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
685 [DEVLINK_ATTR_DEV_STATS
] = MNL_TYPE_NESTED
,
686 [DEVLINK_ATTR_RELOAD_STATS
] = MNL_TYPE_NESTED
,
687 [DEVLINK_ATTR_RELOAD_STATS_ENTRY
] = MNL_TYPE_NESTED
,
688 [DEVLINK_ATTR_RELOAD_ACTION
] = MNL_TYPE_U8
,
689 [DEVLINK_ATTR_RELOAD_STATS_LIMIT
] = MNL_TYPE_U8
,
690 [DEVLINK_ATTR_RELOAD_STATS_VALUE
] = MNL_TYPE_U32
,
691 [DEVLINK_ATTR_REMOTE_RELOAD_STATS
] = MNL_TYPE_NESTED
,
692 [DEVLINK_ATTR_RELOAD_ACTION_INFO
] = MNL_TYPE_NESTED
,
693 [DEVLINK_ATTR_RELOAD_ACTION_STATS
] = MNL_TYPE_NESTED
,
694 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
695 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
696 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
699 static const enum mnl_attr_data_type
700 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
701 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
702 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
703 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
706 static int attr_cb(const struct nlattr
*attr
, void *data
)
708 const struct nlattr
**tb
= data
;
711 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
714 type
= mnl_attr_get_type(attr
);
715 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
722 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
724 const struct nlattr
**tb
= data
;
727 /* Allow the tool to work on top of newer kernels that might contain
730 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
733 type
= mnl_attr_get_type(attr
);
734 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
741 static const enum mnl_attr_data_type
742 devlink_function_policy
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {
743 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
] = MNL_TYPE_BINARY
,
746 static int function_attr_cb(const struct nlattr
*attr
, void *data
)
748 const struct nlattr
**tb
= data
;
751 /* Allow the tool to work on top of newer kernels that might contain
754 if (mnl_attr_type_valid(attr
, DEVLINK_PORT_FUNCTION_ATTR_MAX
) < 0)
757 type
= mnl_attr_get_type(attr
);
758 if (mnl_attr_validate(attr
, devlink_function_policy
[type
]) < 0)
765 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
767 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
768 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
769 struct dl
*dl
= data
;
770 struct ifname_map
*ifname_map
;
771 const char *bus_name
;
772 const char *dev_name
;
773 uint32_t port_ifindex
;
774 const char *port_ifname
;
776 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
777 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
778 !tb
[DEVLINK_ATTR_PORT_INDEX
])
781 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
784 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
785 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
786 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
787 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
788 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
789 port_ifindex
, port_ifname
);
792 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
797 static void ifname_map_fini(struct dl
*dl
)
799 struct ifname_map
*ifname_map
, *tmp
;
801 list_for_each_entry_safe(ifname_map
, tmp
,
802 &dl
->ifname_map_list
, list
) {
803 list_del(&ifname_map
->list
);
804 ifname_map_free(ifname_map
);
808 static int ifname_map_init(struct dl
*dl
)
810 struct nlmsghdr
*nlh
;
813 INIT_LIST_HEAD(&dl
->ifname_map_list
);
815 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
816 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
818 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
826 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
827 char **p_bus_name
, char **p_dev_name
,
828 uint32_t *p_port_index
)
830 struct ifname_map
*ifname_map
;
832 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
833 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
834 *p_bus_name
= ifname_map
->bus_name
;
835 *p_dev_name
= ifname_map
->dev_name
;
836 *p_port_index
= ifname_map
->port_index
;
843 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
844 const char *dev_name
, uint32_t port_index
,
847 struct ifname_map
*ifname_map
;
849 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
850 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
851 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
852 port_index
== ifname_map
->port_index
) {
853 *p_ifname
= ifname_map
->ifname
;
860 static unsigned int strslashcount(char *str
)
862 unsigned int count
= 0;
865 while ((pos
= strchr(pos
, '/'))) {
872 static int strslashrsplit(char *str
, char **before
, char **after
)
876 slash
= strrchr(str
, '/');
885 static int strtouint64_t(const char *str
, uint64_t *p_val
)
888 unsigned long long int val
;
890 val
= strtoull(str
, &endptr
, 10);
891 if (endptr
== str
|| *endptr
!= '\0')
899 static int strtouint32_t(const char *str
, uint32_t *p_val
)
902 unsigned long int val
;
904 val
= strtoul(str
, &endptr
, 10);
905 if (endptr
== str
|| *endptr
!= '\0')
913 static int strtouint16_t(const char *str
, uint16_t *p_val
)
916 unsigned long int val
;
918 val
= strtoul(str
, &endptr
, 10);
919 if (endptr
== str
|| *endptr
!= '\0')
927 static int strtouint8_t(const char *str
, uint8_t *p_val
)
930 unsigned long int val
;
932 val
= strtoul(str
, &endptr
, 10);
933 if (endptr
== str
|| *endptr
!= '\0')
941 static int strtobool(const char *str
, bool *p_val
)
945 if (!strcmp(str
, "true") || !strcmp(str
, "1") ||
946 !strcmp(str
, "enable"))
948 else if (!strcmp(str
, "false") || !strcmp(str
, "0") ||
949 !strcmp(str
, "disable"))
957 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
959 strslashrsplit(str
, p_bus_name
, p_dev_name
);
963 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
965 char *str
= dl_argv_next(dl
);
968 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
971 if (strslashcount(str
) != 1) {
972 pr_err("Wrong devlink identification string format.\n");
973 pr_err("Expected \"bus_name/dev_name\".\n");
976 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
979 static int __dl_argv_handle_port(char *str
,
980 char **p_bus_name
, char **p_dev_name
,
981 uint32_t *p_port_index
)
987 err
= strslashrsplit(str
, &handlestr
, &portstr
);
989 pr_err("Port identification \"%s\" is invalid\n", str
);
992 err
= strtouint32_t(portstr
, p_port_index
);
994 pr_err("Port index \"%s\" is not a number or not within range\n",
998 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
1000 pr_err("Port identification \"%s\" is invalid\n", str
);
1006 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
1007 char **p_bus_name
, char **p_dev_name
,
1008 uint32_t *p_port_index
)
1012 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
1015 pr_err("Netdevice \"%s\" not found\n", str
);
1021 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
1022 char **p_dev_name
, uint32_t *p_port_index
)
1024 char *str
= dl_argv_next(dl
);
1025 unsigned int slash_count
;
1028 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
1031 slash_count
= strslashcount(str
);
1032 switch (slash_count
) {
1034 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
1035 p_dev_name
, p_port_index
);
1037 return __dl_argv_handle_port(str
, p_bus_name
,
1038 p_dev_name
, p_port_index
);
1040 pr_err("Wrong port identification string format.\n");
1041 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1046 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
1047 char **p_dev_name
, uint32_t *p_port_index
,
1048 uint64_t *p_handle_bit
)
1050 char *str
= dl_argv_next(dl
);
1051 unsigned int slash_count
;
1055 pr_err("One of following identifications expected:\n"
1056 "Devlink identification (\"bus_name/dev_name\")\n"
1057 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
1060 slash_count
= strslashcount(str
);
1061 if (slash_count
== 1) {
1062 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
1065 *p_handle_bit
= DL_OPT_HANDLE
;
1066 } else if (slash_count
== 2) {
1067 err
= __dl_argv_handle_port(str
, p_bus_name
,
1068 p_dev_name
, p_port_index
);
1071 *p_handle_bit
= DL_OPT_HANDLEP
;
1072 } else if (slash_count
== 0) {
1073 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
1074 p_dev_name
, p_port_index
);
1077 *p_handle_bit
= DL_OPT_HANDLEP
;
1079 pr_err("Wrong port identification string format.\n");
1080 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1086 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
1087 char **p_dev_name
, char **p_region
)
1092 err
= strslashrsplit(str
, &handlestr
, p_region
);
1094 pr_err("Region identification \"%s\" is invalid\n", str
);
1097 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
1099 pr_err("Region identification \"%s\" is invalid\n", str
);
1105 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
1106 char **p_dev_name
, char **p_region
)
1108 char *str
= dl_argv_next(dl
);
1109 unsigned int slash_count
;
1112 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
1116 slash_count
= strslashcount(str
);
1117 if (slash_count
!= 2) {
1118 pr_err("Wrong region identification string format.\n");
1119 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
1123 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
1126 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
1128 char *str
= dl_argv_next(dl
);
1132 pr_err("Unsigned number argument expected\n");
1136 err
= strtouint64_t(str
, p_val
);
1138 pr_err("\"%s\" is not a number or not within range\n", str
);
1144 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
1146 char *str
= dl_argv_next(dl
);
1150 pr_err("Unsigned number argument expected\n");
1154 err
= strtouint32_t(str
, p_val
);
1156 pr_err("\"%s\" is not a number or not within range\n", str
);
1162 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
1164 char *str
= dl_argv_next(dl
);
1168 pr_err("Unsigned number argument expected\n");
1172 err
= strtouint16_t(str
, p_val
);
1174 pr_err("\"%s\" is not a number or not within range\n", str
);
1180 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
1182 char *str
= dl_argv_next(dl
);
1186 pr_err("Boolean argument expected\n");
1190 err
= strtobool(str
, p_val
);
1192 pr_err("\"%s\" is not a valid boolean value\n", str
);
1198 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
1200 const char *str
= dl_argv_next(dl
);
1203 pr_err("String parameter expected\n");
1210 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1212 if (strcmp(typestr
, "auto") == 0) {
1213 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1214 } else if (strcmp(typestr
, "eth") == 0) {
1215 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1216 } else if (strcmp(typestr
, "ib") == 0) {
1217 *p_type
= DEVLINK_PORT_TYPE_IB
;
1219 pr_err("Unknown port type \"%s\"\n", typestr
);
1225 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1227 if (strcmp(typestr
, "ingress") == 0) {
1228 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1229 } else if (strcmp(typestr
, "egress") == 0) {
1230 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1232 pr_err("Unknown pool type \"%s\"\n", typestr
);
1238 static int threshold_type_get(const char *typestr
,
1239 enum devlink_sb_threshold_type
*p_type
)
1241 if (strcmp(typestr
, "static") == 0) {
1242 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1243 } else if (strcmp(typestr
, "dynamic") == 0) {
1244 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1246 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1252 static int eswitch_mode_get(const char *typestr
,
1253 enum devlink_eswitch_mode
*p_mode
)
1255 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1256 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1257 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1258 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1260 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1266 static int eswitch_inline_mode_get(const char *typestr
,
1267 enum devlink_eswitch_inline_mode
*p_mode
)
1269 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1270 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1271 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1272 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1273 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1274 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1275 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1276 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1278 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1285 eswitch_encap_mode_get(const char *typestr
,
1286 enum devlink_eswitch_encap_mode
*p_encap_mode
)
1288 /* The initial implementation incorrectly accepted "enable"/"disable".
1289 * Carry it to maintain backward compatibility.
1291 if (strcmp(typestr
, "disable") == 0 ||
1292 strcmp(typestr
, ESWITCH_ENCAP_MODE_NONE
) == 0) {
1293 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1294 } else if (strcmp(typestr
, "enable") == 0 ||
1295 strcmp(typestr
, ESWITCH_ENCAP_MODE_BASIC
) == 0) {
1296 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1298 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1304 static int flash_overwrite_section_get(const char *sectionstr
, uint32_t *mask
)
1306 if (strcmp(sectionstr
, "settings") == 0) {
1307 *mask
|= DEVLINK_FLASH_OVERWRITE_SETTINGS
;
1308 } else if (strcmp(sectionstr
, "identifiers") == 0) {
1309 *mask
|= DEVLINK_FLASH_OVERWRITE_IDENTIFIERS
;
1311 pr_err("Unknown overwrite section \"%s\"\n", sectionstr
);
1317 static int param_cmode_get(const char *cmodestr
,
1318 enum devlink_param_cmode
*cmode
)
1320 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1321 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1322 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1323 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1324 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1325 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1327 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1333 static int trap_action_get(const char *actionstr
,
1334 enum devlink_trap_action
*p_action
)
1336 if (strcmp(actionstr
, "drop") == 0) {
1337 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1338 } else if (strcmp(actionstr
, "trap") == 0) {
1339 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1340 } else if (strcmp(actionstr
, "mirror") == 0) {
1341 *p_action
= DEVLINK_TRAP_ACTION_MIRROR
;
1343 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1349 static int hw_addr_parse(const char *addrstr
, char *hw_addr
, uint32_t *len
)
1353 alen
= ll_addr_a2n(hw_addr
, MAX_ADDR_LEN
, addrstr
);
1360 static int reload_action_get(struct dl
*dl
, const char *actionstr
,
1361 enum devlink_reload_action
*action
)
1363 if (strcmp(actionstr
, "driver_reinit") == 0) {
1364 *action
= DEVLINK_RELOAD_ACTION_DRIVER_REINIT
;
1365 } else if (strcmp(actionstr
, "fw_activate") == 0) {
1366 *action
= DEVLINK_RELOAD_ACTION_FW_ACTIVATE
;
1368 pr_err("Unknown reload action \"%s\"\n", actionstr
);
1374 static int reload_limit_get(struct dl
*dl
, const char *limitstr
,
1375 enum devlink_reload_limit
*limit
)
1377 if (strcmp(limitstr
, "no_reset") == 0) {
1378 *limit
= DEVLINK_RELOAD_LIMIT_NO_RESET
;
1380 pr_err("Unknown reload limit \"%s\"\n", limitstr
);
1386 struct dl_args_metadata
{
1388 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1391 static const struct dl_args_metadata dl_args_required
[] = {
1392 {DL_OPT_PORT_TYPE
, "Port type not set."},
1393 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1394 {DL_OPT_SB_POOL
, "Pool index option expected."},
1395 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1396 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1397 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1398 {DL_OPT_SB_TH
, "Threshold option expected."},
1399 {DL_OPT_SB_TC
, "TC index option expected."},
1400 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1401 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1402 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1403 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1404 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1405 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1406 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1407 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1408 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1409 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1410 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1411 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1412 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1413 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1414 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1415 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1416 {DL_OPT_PORT_FUNCTION_HW_ADDR
, "Port function's hardware address is expected."},
1419 static int dl_args_finding_required_validate(uint64_t o_required
,
1425 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1426 o_flag
= dl_args_required
[i
].o_flag
;
1427 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1428 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1432 if (o_required
& ~o_found
) {
1433 pr_err("BUG: unknown argument required but not found\n");
1439 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1440 uint64_t o_optional
)
1442 struct dl_opts
*opts
= &dl
->opts
;
1443 uint64_t o_all
= o_required
| o_optional
;
1444 uint64_t o_found
= 0;
1447 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1448 uint64_t handle_bit
;
1450 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1451 &opts
->port_index
, &handle_bit
);
1454 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1455 o_found
|= handle_bit
;
1456 } else if (o_required
& DL_OPT_HANDLE
) {
1457 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1460 o_found
|= DL_OPT_HANDLE
;
1461 } else if (o_required
& DL_OPT_HANDLEP
) {
1462 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1466 o_found
|= DL_OPT_HANDLEP
;
1467 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1468 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1470 &opts
->region_name
);
1473 o_found
|= DL_OPT_HANDLE_REGION
;
1476 while (dl_argc(dl
)) {
1477 if (dl_argv_match(dl
, "type") &&
1478 (o_all
& DL_OPT_PORT_TYPE
)) {
1479 const char *typestr
;
1482 err
= dl_argv_str(dl
, &typestr
);
1485 err
= port_type_get(typestr
, &opts
->port_type
);
1488 o_found
|= DL_OPT_PORT_TYPE
;
1489 } else if (dl_argv_match(dl
, "count") &&
1490 (o_all
& DL_OPT_PORT_COUNT
)) {
1492 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1495 o_found
|= DL_OPT_PORT_COUNT
;
1496 } else if (dl_argv_match(dl
, "sb") &&
1497 (o_all
& DL_OPT_SB
)) {
1499 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1502 o_found
|= DL_OPT_SB
;
1503 } else if (dl_argv_match(dl
, "pool") &&
1504 (o_all
& DL_OPT_SB_POOL
)) {
1506 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1509 o_found
|= DL_OPT_SB_POOL
;
1510 } else if (dl_argv_match(dl
, "size") &&
1511 (o_all
& DL_OPT_SB_SIZE
)) {
1513 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1516 o_found
|= DL_OPT_SB_SIZE
;
1517 } else if (dl_argv_match(dl
, "type") &&
1518 (o_all
& DL_OPT_SB_TYPE
)) {
1519 const char *typestr
;
1522 err
= dl_argv_str(dl
, &typestr
);
1525 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1528 o_found
|= DL_OPT_SB_TYPE
;
1529 } else if (dl_argv_match(dl
, "thtype") &&
1530 (o_all
& DL_OPT_SB_THTYPE
)) {
1531 const char *typestr
;
1534 err
= dl_argv_str(dl
, &typestr
);
1537 err
= threshold_type_get(typestr
,
1538 &opts
->sb_pool_thtype
);
1541 o_found
|= DL_OPT_SB_THTYPE
;
1542 } else if (dl_argv_match(dl
, "th") &&
1543 (o_all
& DL_OPT_SB_TH
)) {
1545 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1548 o_found
|= DL_OPT_SB_TH
;
1549 } else if (dl_argv_match(dl
, "tc") &&
1550 (o_all
& DL_OPT_SB_TC
)) {
1552 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1555 o_found
|= DL_OPT_SB_TC
;
1556 } else if (dl_argv_match(dl
, "mode") &&
1557 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1558 const char *typestr
;
1561 err
= dl_argv_str(dl
, &typestr
);
1564 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1567 o_found
|= DL_OPT_ESWITCH_MODE
;
1568 } else if (dl_argv_match(dl
, "inline-mode") &&
1569 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1570 const char *typestr
;
1573 err
= dl_argv_str(dl
, &typestr
);
1576 err
= eswitch_inline_mode_get(
1577 typestr
, &opts
->eswitch_inline_mode
);
1580 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1581 } else if (dl_argv_match(dl
, "name") &&
1582 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1584 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1587 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1588 } else if ((dl_argv_match(dl
, "counters") ||
1589 dl_argv_match(dl
, "counters_enabled")) &&
1590 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1592 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1595 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1596 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1597 dl_argv_match(dl
, "encap-mode")) &&
1598 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1599 const char *typestr
;
1602 err
= dl_argv_str(dl
, &typestr
);
1605 err
= eswitch_encap_mode_get(typestr
,
1606 &opts
->eswitch_encap_mode
);
1609 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1610 } else if (dl_argv_match(dl
, "path") &&
1611 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1613 err
= dl_argv_str(dl
, &opts
->resource_path
);
1616 o_found
|= DL_OPT_RESOURCE_PATH
;
1617 } else if (dl_argv_match(dl
, "size") &&
1618 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1620 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1623 o_found
|= DL_OPT_RESOURCE_SIZE
;
1624 } else if (dl_argv_match(dl
, "name") &&
1625 (o_all
& DL_OPT_PARAM_NAME
)) {
1627 err
= dl_argv_str(dl
, &opts
->param_name
);
1630 o_found
|= DL_OPT_PARAM_NAME
;
1631 } else if (dl_argv_match(dl
, "value") &&
1632 (o_all
& DL_OPT_PARAM_VALUE
)) {
1634 err
= dl_argv_str(dl
, &opts
->param_value
);
1637 o_found
|= DL_OPT_PARAM_VALUE
;
1638 } else if (dl_argv_match(dl
, "cmode") &&
1639 (o_all
& DL_OPT_PARAM_CMODE
)) {
1640 const char *cmodestr
;
1643 err
= dl_argv_str(dl
, &cmodestr
);
1646 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1649 o_found
|= DL_OPT_PARAM_CMODE
;
1650 } else if (dl_argv_match(dl
, "snapshot") &&
1651 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1653 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1656 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1657 } else if (dl_argv_match(dl
, "address") &&
1658 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1660 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1663 o_found
|= DL_OPT_REGION_ADDRESS
;
1664 } else if (dl_argv_match(dl
, "length") &&
1665 (o_all
& DL_OPT_REGION_LENGTH
)) {
1667 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1670 o_found
|= DL_OPT_REGION_LENGTH
;
1671 } else if (dl_argv_match(dl
, "file") &&
1672 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1674 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1677 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1678 } else if (dl_argv_match(dl
, "component") &&
1679 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1681 err
= dl_argv_str(dl
, &opts
->flash_component
);
1684 o_found
|= DL_OPT_FLASH_COMPONENT
;
1686 } else if (dl_argv_match(dl
, "overwrite") &&
1687 (o_all
& DL_OPT_FLASH_OVERWRITE
)) {
1688 const char *sectionstr
;
1691 err
= dl_argv_str(dl
, §ionstr
);
1694 err
= flash_overwrite_section_get(sectionstr
,
1695 &opts
->overwrite_mask
);
1698 o_found
|= DL_OPT_FLASH_OVERWRITE
;
1700 } else if (dl_argv_match(dl
, "reporter") &&
1701 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1703 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1706 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1707 } else if (dl_argv_match(dl
, "grace_period") &&
1708 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1710 err
= dl_argv_uint64_t(dl
,
1711 &opts
->reporter_graceful_period
);
1714 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1715 } else if (dl_argv_match(dl
, "auto_recover") &&
1716 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1718 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1721 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1722 } else if (dl_argv_match(dl
, "auto_dump") &&
1723 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)) {
1725 err
= dl_argv_bool(dl
, &opts
->reporter_auto_dump
);
1728 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_DUMP
;
1729 } else if (dl_argv_match(dl
, "trap") &&
1730 (o_all
& DL_OPT_TRAP_NAME
)) {
1732 err
= dl_argv_str(dl
, &opts
->trap_name
);
1735 o_found
|= DL_OPT_TRAP_NAME
;
1736 } else if (dl_argv_match(dl
, "group") &&
1737 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1739 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1742 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1743 } else if (dl_argv_match(dl
, "action") &&
1744 (o_all
& DL_OPT_TRAP_ACTION
)) {
1745 const char *actionstr
;
1748 err
= dl_argv_str(dl
, &actionstr
);
1751 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1754 o_found
|= DL_OPT_TRAP_ACTION
;
1755 } else if (dl_argv_match(dl
, "netns") &&
1756 (o_all
& DL_OPT_NETNS
)) {
1757 const char *netns_str
;
1760 err
= dl_argv_str(dl
, &netns_str
);
1763 opts
->netns
= netns_get_fd(netns_str
);
1764 if ((int)opts
->netns
< 0) {
1766 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1769 opts
->netns_is_pid
= true;
1771 o_found
|= DL_OPT_NETNS
;
1772 } else if (dl_argv_match(dl
, "action") &&
1773 (o_all
& DL_OPT_RELOAD_ACTION
)) {
1774 const char *actionstr
;
1777 err
= dl_argv_str(dl
, &actionstr
);
1780 err
= reload_action_get(dl
, actionstr
, &opts
->reload_action
);
1783 o_found
|= DL_OPT_RELOAD_ACTION
;
1784 } else if (dl_argv_match(dl
, "limit") &&
1785 (o_all
& DL_OPT_RELOAD_LIMIT
)) {
1786 const char *limitstr
;
1789 err
= dl_argv_str(dl
, &limitstr
);
1792 err
= reload_limit_get(dl
, limitstr
, &opts
->reload_limit
);
1795 o_found
|= DL_OPT_RELOAD_LIMIT
;
1796 } else if (dl_argv_match(dl
, "policer") &&
1797 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1799 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1802 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1803 } else if (dl_argv_match(dl
, "nopolicer") &&
1804 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1806 opts
->trap_policer_id
= 0;
1807 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1808 } else if (dl_argv_match(dl
, "rate") &&
1809 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1811 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1814 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1815 } else if (dl_argv_match(dl
, "burst") &&
1816 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1818 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1821 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1822 } else if (dl_argv_match(dl
, "hw_addr") &&
1823 (o_all
& DL_OPT_PORT_FUNCTION_HW_ADDR
)) {
1824 const char *addrstr
;
1827 err
= dl_argv_str(dl
, &addrstr
);
1830 err
= hw_addr_parse(addrstr
, opts
->port_function_hw_addr
,
1831 &opts
->port_function_hw_addr_len
);
1834 o_found
|= DL_OPT_PORT_FUNCTION_HW_ADDR
;
1837 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1842 opts
->present
= o_found
;
1844 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1846 opts
->present
|= DL_OPT_SB
;
1849 return dl_args_finding_required_validate(o_required
, o_found
);
1853 dl_function_attr_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1855 struct nlattr
*nest
;
1857 nest
= mnl_attr_nest_start(nlh
, DEVLINK_ATTR_PORT_FUNCTION
);
1858 mnl_attr_put(nlh
, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
,
1859 opts
->port_function_hw_addr_len
,
1860 opts
->port_function_hw_addr
);
1861 mnl_attr_nest_end(nlh
, nest
);
1865 dl_flash_update_overwrite_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1867 struct nla_bitfield32 overwrite_mask
;
1869 overwrite_mask
.selector
= DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS
;
1870 overwrite_mask
.value
= opts
->overwrite_mask
;
1872 mnl_attr_put(nlh
, DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK
,
1873 sizeof(overwrite_mask
), &overwrite_mask
);
1877 dl_reload_limits_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1879 struct nla_bitfield32 limits
;
1881 limits
.selector
= DEVLINK_RELOAD_LIMITS_VALID_MASK
;
1882 limits
.value
= BIT(opts
->reload_limit
);
1883 mnl_attr_put(nlh
, DEVLINK_ATTR_RELOAD_LIMITS
, sizeof(limits
), &limits
);
1886 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1888 struct dl_opts
*opts
= &dl
->opts
;
1890 if (opts
->present
& DL_OPT_HANDLE
) {
1891 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1892 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1893 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1894 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1895 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1896 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1898 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1899 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1900 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1901 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1904 if (opts
->present
& DL_OPT_PORT_TYPE
)
1905 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1907 if (opts
->present
& DL_OPT_PORT_COUNT
)
1908 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1910 if (opts
->present
& DL_OPT_SB
)
1911 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1913 if (opts
->present
& DL_OPT_SB_POOL
)
1914 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1915 opts
->sb_pool_index
);
1916 if (opts
->present
& DL_OPT_SB_SIZE
)
1917 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1918 opts
->sb_pool_size
);
1919 if (opts
->present
& DL_OPT_SB_TYPE
)
1920 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1921 opts
->sb_pool_type
);
1922 if (opts
->present
& DL_OPT_SB_THTYPE
)
1923 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1924 opts
->sb_pool_thtype
);
1925 if (opts
->present
& DL_OPT_SB_TH
)
1926 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1927 opts
->sb_threshold
);
1928 if (opts
->present
& DL_OPT_SB_TC
)
1929 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1931 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1932 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1933 opts
->eswitch_mode
);
1934 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1935 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1936 opts
->eswitch_inline_mode
);
1937 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1938 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1939 opts
->dpipe_table_name
);
1940 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1941 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1942 opts
->dpipe_counters_enabled
);
1943 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1944 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1945 opts
->eswitch_encap_mode
);
1946 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1947 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1949 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1950 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1951 opts
->resource_size
);
1952 if (opts
->present
& DL_OPT_PARAM_NAME
)
1953 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1955 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1956 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1958 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1959 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1960 opts
->region_snapshot_id
);
1961 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1962 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1963 opts
->region_address
);
1964 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1965 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1966 opts
->region_length
);
1967 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1968 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1969 opts
->flash_file_name
);
1970 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1971 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1972 opts
->flash_component
);
1973 if (opts
->present
& DL_OPT_FLASH_OVERWRITE
)
1974 dl_flash_update_overwrite_put(nlh
, opts
);
1975 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1976 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1977 opts
->reporter_name
);
1978 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1979 mnl_attr_put_u64(nlh
,
1980 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1981 opts
->reporter_graceful_period
);
1982 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1983 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1984 opts
->reporter_auto_recover
);
1985 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)
1986 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
1987 opts
->reporter_auto_dump
);
1988 if (opts
->present
& DL_OPT_TRAP_NAME
)
1989 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1991 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1992 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1993 opts
->trap_group_name
);
1994 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1995 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1997 if (opts
->present
& DL_OPT_NETNS
)
1998 mnl_attr_put_u32(nlh
,
1999 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
2000 DEVLINK_ATTR_NETNS_FD
,
2002 if (opts
->present
& DL_OPT_RELOAD_ACTION
)
2003 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_RELOAD_ACTION
,
2004 opts
->reload_action
);
2005 if (opts
->present
& DL_OPT_RELOAD_LIMIT
)
2006 dl_reload_limits_put(nlh
, opts
);
2007 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
2008 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
2009 opts
->trap_policer_id
);
2010 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
2011 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
2012 opts
->trap_policer_rate
);
2013 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
2014 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
2015 opts
->trap_policer_burst
);
2016 if (opts
->present
& DL_OPT_PORT_FUNCTION_HW_ADDR
)
2017 dl_function_attr_put(nlh
, opts
);
2020 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
2021 uint64_t o_required
, uint64_t o_optional
)
2025 err
= dl_argv_parse(dl
, o_required
, o_optional
);
2028 dl_opts_put(nlh
, dl
);
2032 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
2034 struct dl_opts
*opts
= &dl
->opts
;
2035 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
2036 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
2037 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
2038 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
2040 if (opts
->present
& DL_OPT_HANDLE
&&
2041 attr_bus_name
&& attr_dev_name
) {
2042 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
2043 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
2045 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
2046 strcmp(dev_name
, opts
->dev_name
) != 0)
2049 if (opts
->present
& DL_OPT_HANDLEP
&&
2050 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
2051 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
2052 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
2053 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
2055 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
2056 strcmp(dev_name
, opts
->dev_name
) != 0 ||
2057 port_index
!= opts
->port_index
)
2060 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
2061 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
2063 if (sb_index
!= opts
->sb_index
)
2069 static void cmd_dev_help(void)
2071 pr_err("Usage: devlink dev show [ DEV ]\n");
2072 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
2073 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
2074 pr_err(" [ encap-mode { none | basic } ]\n");
2075 pr_err(" devlink dev eswitch show DEV\n");
2076 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
2077 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
2078 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2079 pr_err(" [ action { driver_reinit | fw_activate } ] [ limit no_reset ]\n");
2080 pr_err(" devlink dev info [ DEV ]\n");
2081 pr_err(" devlink dev flash DEV file PATH [ component NAME ] [ overwrite SECTION ]\n");
2084 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
2085 const char *dev_name
)
2087 if (!dl
->arr_last
.present
)
2089 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
2090 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
2093 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
2094 const char *dev_name
)
2096 dl
->arr_last
.present
= true;
2097 free(dl
->arr_last
.dev_name
);
2098 free(dl
->arr_last
.bus_name
);
2099 dl
->arr_last
.bus_name
= strdup(bus_name
);
2100 dl
->arr_last
.dev_name
= strdup(dev_name
);
2103 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
2104 const char *dev_name
)
2106 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
2109 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
2110 const char *dev_name
)
2112 return dl
->arr_last
.present
&&
2113 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
2116 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
2117 bool content
, bool array
)
2119 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2120 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2123 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
2125 if (dl
->json_output
) {
2127 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2128 close_json_array(PRINT_JSON
, NULL
);
2129 if (should_arr_last_handle_start(dl
, bus_name
,
2131 open_json_array(PRINT_JSON
, buf
);
2132 open_json_object(NULL
);
2133 arr_last_handle_set(dl
, bus_name
, dev_name
);
2135 open_json_object(NULL
);
2138 open_json_object(buf
);
2142 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2143 __pr_out_indent_dec();
2144 if (should_arr_last_handle_start(dl
, bus_name
,
2146 pr_out("%s%s", buf
, content
? ":" : "");
2148 __pr_out_indent_inc();
2149 arr_last_handle_set(dl
, bus_name
, dev_name
);
2152 pr_out("%s%s", buf
, content
? ":" : "");
2157 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
2159 __pr_out_handle_start(dl
, tb
, true, true);
2162 static void pr_out_handle_end(struct dl
*dl
)
2164 if (dl
->json_output
)
2165 close_json_object();
2170 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
2172 __pr_out_handle_start(dl
, tb
, false, false);
2173 pr_out_handle_end(dl
);
2176 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
2177 const char *dev_name
, uint32_t port_index
)
2179 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
2180 dl
->arr_last
.port_index
== port_index
;
2183 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
2184 const char *dev_name
, uint32_t port_index
)
2186 arr_last_handle_set(dl
, bus_name
, dev_name
);
2187 dl
->arr_last
.port_index
= port_index
;
2190 static bool should_arr_last_port_handle_start(struct dl
*dl
,
2191 const char *bus_name
,
2192 const char *dev_name
,
2193 uint32_t port_index
)
2195 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2198 static bool should_arr_last_port_handle_end(struct dl
*dl
,
2199 const char *bus_name
,
2200 const char *dev_name
,
2201 uint32_t port_index
)
2203 return dl
->arr_last
.present
&&
2204 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2207 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
2208 const char *dev_name
,
2209 uint32_t port_index
, bool try_nice
,
2212 static char buf
[64];
2213 char *ifname
= NULL
;
2215 if (dl
->no_nice_names
|| !try_nice
||
2216 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
2217 port_index
, &ifname
) != 0)
2218 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
2220 sprintf(buf
, "%s", ifname
);
2222 if (dl
->json_output
) {
2224 if (should_arr_last_port_handle_end(dl
, bus_name
,
2227 close_json_array(PRINT_JSON
, NULL
);
2228 if (should_arr_last_port_handle_start(dl
, bus_name
,
2231 open_json_array(PRINT_JSON
, buf
);
2232 open_json_object(NULL
);
2233 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
2236 open_json_object(NULL
);
2239 open_json_object(buf
);
2243 if (should_arr_last_port_handle_end(dl
, bus_name
, dev_name
, port_index
))
2244 __pr_out_indent_dec();
2245 if (should_arr_last_port_handle_start(dl
, bus_name
,
2246 dev_name
, port_index
)) {
2249 __pr_out_indent_inc();
2250 arr_last_port_handle_set(dl
, bus_name
, dev_name
, port_index
);
2258 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2260 const char *bus_name
;
2261 const char *dev_name
;
2262 uint32_t port_index
;
2264 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2265 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2266 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2267 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
2270 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2272 const char *bus_name
;
2273 const char *dev_name
;
2274 uint32_t port_index
;
2276 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2277 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2278 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2279 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
2282 static void pr_out_port_handle_end(struct dl
*dl
)
2284 if (dl
->json_output
)
2285 close_json_object();
2290 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
2292 if (dl
->json_output
) {
2293 print_uint(PRINT_JSON
, "address", NULL
, addr
);
2294 open_json_array(PRINT_JSON
, "data");
2298 static void pr_out_region_chunk_end(struct dl
*dl
)
2300 if (dl
->json_output
)
2301 close_json_array(PRINT_JSON
, NULL
);
2304 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2307 static uint64_t align_val
;
2310 pr_out_region_chunk_start(dl
, addr
);
2312 if (!dl
->json_output
)
2313 if (!(align_val
% 16))
2314 pr_out("%s%016"PRIx64
" ",
2315 align_val
? "\n" : "",
2320 if (dl
->json_output
)
2321 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2323 pr_out("%02x ", data
[i
]);
2328 pr_out_region_chunk_end(dl
);
2331 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2333 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2339 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2340 if (err
!= MNL_CB_OK
)
2343 pr_out_object_start(dl
, "stats");
2344 pr_out_object_start(dl
, "rx");
2345 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2346 pr_out_u64(dl
, "bytes",
2347 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2348 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2349 pr_out_u64(dl
, "packets",
2350 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2351 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2352 pr_out_u64(dl
, "dropped",
2353 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2354 pr_out_object_end(dl
);
2355 pr_out_object_end(dl
);
2358 static const char *param_cmode_name(uint8_t cmode
)
2361 case DEVLINK_PARAM_CMODE_RUNTIME
:
2362 return PARAM_CMODE_RUNTIME_STR
;
2363 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2364 return PARAM_CMODE_DRIVERINIT_STR
;
2365 case DEVLINK_PARAM_CMODE_PERMANENT
:
2366 return PARAM_CMODE_PERMANENT_STR
;
2367 default: return "<unknown type>";
2371 static const char *reload_action_name(uint8_t reload_action
)
2373 switch (reload_action
) {
2374 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT
:
2375 return "driver_reinit";
2376 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE
:
2377 return "fw_activate";
2379 return "<unknown reload action>";
2383 static const char *reload_limit_name(uint8_t reload_limit
)
2385 switch (reload_limit
) {
2386 case DEVLINK_RELOAD_LIMIT_UNSPEC
:
2387 return "unspecified";
2388 case DEVLINK_RELOAD_LIMIT_NO_RESET
:
2391 return "<unknown reload action>";
2395 static const char *eswitch_mode_name(uint32_t mode
)
2398 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2399 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2400 default: return "<unknown mode>";
2404 static const char *eswitch_inline_mode_name(uint32_t mode
)
2407 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2408 return ESWITCH_INLINE_MODE_NONE
;
2409 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2410 return ESWITCH_INLINE_MODE_LINK
;
2411 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2412 return ESWITCH_INLINE_MODE_NETWORK
;
2413 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2414 return ESWITCH_INLINE_MODE_TRANSPORT
;
2416 return "<unknown mode>";
2420 static const char *eswitch_encap_mode_name(uint32_t mode
)
2423 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2424 return ESWITCH_ENCAP_MODE_NONE
;
2425 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2426 return ESWITCH_ENCAP_MODE_BASIC
;
2428 return "<unknown mode>";
2432 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2434 __pr_out_handle_start(dl
, tb
, true, false);
2436 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2437 check_indent_newline(dl
);
2438 print_string(PRINT_ANY
, "mode", "mode %s",
2439 eswitch_mode_name(mnl_attr_get_u16(
2440 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2442 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2443 check_indent_newline(dl
);
2444 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2445 eswitch_inline_mode_name(mnl_attr_get_u8(
2446 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2448 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2449 check_indent_newline(dl
);
2450 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2451 eswitch_encap_mode_name(mnl_attr_get_u8(
2452 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2455 pr_out_handle_end(dl
);
2458 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2460 struct dl
*dl
= data
;
2461 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2462 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2464 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2465 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2466 return MNL_CB_ERROR
;
2467 pr_out_eswitch(dl
, tb
);
2471 static int cmd_dev_eswitch_show(struct dl
*dl
)
2473 struct nlmsghdr
*nlh
;
2476 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2477 NLM_F_REQUEST
| NLM_F_ACK
);
2479 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2483 pr_out_section_start(dl
, "dev");
2484 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2485 pr_out_section_end(dl
);
2489 static int cmd_dev_eswitch_set(struct dl
*dl
)
2491 struct nlmsghdr
*nlh
;
2494 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2495 NLM_F_REQUEST
| NLM_F_ACK
);
2497 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2498 DL_OPT_ESWITCH_MODE
|
2499 DL_OPT_ESWITCH_INLINE_MODE
|
2500 DL_OPT_ESWITCH_ENCAP_MODE
);
2505 if (dl
->opts
.present
== 1) {
2506 pr_err("Need to set at least one option\n");
2510 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2513 static int cmd_dev_eswitch(struct dl
*dl
)
2515 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2518 } else if (dl_argv_match(dl
, "set")) {
2520 return cmd_dev_eswitch_set(dl
);
2521 } else if (dl_argv_match(dl
, "show")) {
2523 return cmd_dev_eswitch_show(dl
);
2525 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2529 struct param_val_conv
{
2535 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2536 uint32_t len
, const char *name
)
2540 for (i
= 0; i
< len
; i
++)
2541 if (!strcmp(param_val_conv
[i
].name
, name
))
2548 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2549 uint32_t len
, const char *name
, const char *vstr
,
2554 for (i
= 0; i
< len
; i
++)
2555 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2556 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2557 *vuint
= param_val_conv
[i
].vuint
;
2565 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2566 uint32_t len
, const char *name
, uint32_t vuint
,
2571 for (i
= 0; i
< len
; i
++)
2572 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2573 param_val_conv
[i
].vuint
== vuint
) {
2574 *vstr
= param_val_conv
[i
].vstr
;
2581 static const struct param_val_conv param_val_conv
[] = {
2583 .name
= "fw_load_policy",
2585 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2588 .name
= "fw_load_policy",
2590 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2593 .name
= "fw_load_policy",
2595 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK
,
2598 .name
= "reset_dev_on_drv_probe",
2600 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2603 .name
= "fw_load_policy",
2605 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2608 .name
= "reset_dev_on_drv_probe",
2610 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2613 .name
= "reset_dev_on_drv_probe",
2615 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2618 .name
= "reset_dev_on_drv_probe",
2620 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2624 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2626 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2627 int nla_type
, struct nlattr
*nl
)
2629 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2630 struct nlattr
*val_attr
;
2635 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2636 if (err
!= MNL_CB_OK
)
2639 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2640 (nla_type
!= MNL_TYPE_FLAG
&&
2641 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2644 check_indent_newline(dl
);
2645 print_string(PRINT_ANY
, "cmode", "cmode %s",
2646 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2648 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2650 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2656 err
= param_val_conv_str_get(param_val_conv
,
2659 mnl_attr_get_u8(val_attr
),
2663 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2665 print_uint(PRINT_ANY
, "value", " value %u",
2666 mnl_attr_get_u8(val_attr
));
2671 err
= param_val_conv_str_get(param_val_conv
,
2674 mnl_attr_get_u16(val_attr
),
2678 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2680 print_uint(PRINT_ANY
, "value", " value %u",
2681 mnl_attr_get_u16(val_attr
));
2686 err
= param_val_conv_str_get(param_val_conv
,
2689 mnl_attr_get_u32(val_attr
),
2693 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2695 print_uint(PRINT_ANY
, "value", " value %u",
2696 mnl_attr_get_u32(val_attr
));
2699 case MNL_TYPE_STRING
:
2700 print_string(PRINT_ANY
, "value", " value %s",
2701 mnl_attr_get_str(val_attr
));
2704 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2709 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2711 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2712 struct nlattr
*param_value_attr
;
2713 const char *nla_name
;
2717 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2718 if (err
!= MNL_CB_OK
)
2720 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2721 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2722 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2726 pr_out_handle_start_arr(dl
, tb
);
2728 __pr_out_handle_start(dl
, tb
, true, false);
2730 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2732 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2733 check_indent_newline(dl
);
2734 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2735 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2736 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2738 print_string(PRINT_ANY
, "type", "type %s", "generic");
2740 pr_out_array_start(dl
, "values");
2741 mnl_attr_for_each_nested(param_value_attr
,
2742 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2743 pr_out_entry_start(dl
);
2744 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2745 pr_out_entry_end(dl
);
2747 pr_out_array_end(dl
);
2748 pr_out_handle_end(dl
);
2751 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2753 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2754 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2755 struct dl
*dl
= data
;
2757 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2758 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2759 !tb
[DEVLINK_ATTR_PARAM
])
2760 return MNL_CB_ERROR
;
2761 pr_out_param(dl
, tb
, true);
2777 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2779 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2780 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2781 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2782 struct nlattr
*param_value_attr
;
2783 enum devlink_param_cmode cmode
;
2784 struct param_ctx
*ctx
= data
;
2785 struct dl
*dl
= ctx
->dl
;
2789 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2790 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2791 !tb
[DEVLINK_ATTR_PARAM
])
2792 return MNL_CB_ERROR
;
2794 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2795 if (err
!= MNL_CB_OK
)
2796 return MNL_CB_ERROR
;
2798 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2799 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2800 return MNL_CB_ERROR
;
2802 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2803 mnl_attr_for_each_nested(param_value_attr
,
2804 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2805 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2806 struct nlattr
*val_attr
;
2808 err
= mnl_attr_parse_nested(param_value_attr
,
2809 attr_cb
, nla_value
);
2810 if (err
!= MNL_CB_OK
)
2811 return MNL_CB_ERROR
;
2813 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2814 (nla_type
!= MNL_TYPE_FLAG
&&
2815 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2816 return MNL_CB_ERROR
;
2818 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2819 if (cmode
== dl
->opts
.cmode
) {
2820 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2823 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2826 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2829 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2831 case MNL_TYPE_STRING
:
2832 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2835 ctx
->value
.vbool
= val_attr
? true : false;
2841 ctx
->nla_type
= nla_type
;
2845 static int cmd_dev_param_set(struct dl
*dl
)
2847 struct param_ctx ctx
= {};
2848 struct nlmsghdr
*nlh
;
2850 uint32_t val_u32
= 0;
2856 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2858 DL_OPT_PARAM_VALUE
|
2859 DL_OPT_PARAM_CMODE
, 0);
2863 /* Get value type */
2864 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2865 NLM_F_REQUEST
| NLM_F_ACK
);
2866 dl_opts_put(nlh
, dl
);
2869 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2873 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2874 NLM_F_REQUEST
| NLM_F_ACK
);
2875 dl_opts_put(nlh
, dl
);
2877 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2878 dl
->opts
.param_name
);
2880 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2881 switch (ctx
.nla_type
) {
2884 err
= param_val_conv_uint_get(param_val_conv
,
2886 dl
->opts
.param_name
,
2887 dl
->opts
.param_value
,
2891 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2894 goto err_param_value_parse
;
2895 if (val_u8
== ctx
.value
.vu8
)
2897 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2901 err
= param_val_conv_uint_get(param_val_conv
,
2903 dl
->opts
.param_name
,
2904 dl
->opts
.param_value
,
2908 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2911 goto err_param_value_parse
;
2912 if (val_u16
== ctx
.value
.vu16
)
2914 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2918 err
= param_val_conv_uint_get(param_val_conv
,
2920 dl
->opts
.param_name
,
2921 dl
->opts
.param_value
,
2924 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2926 goto err_param_value_parse
;
2927 if (val_u32
== ctx
.value
.vu32
)
2929 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2932 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2934 goto err_param_value_parse
;
2935 if (val_bool
== ctx
.value
.vbool
)
2938 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2941 case MNL_TYPE_STRING
:
2942 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2943 dl
->opts
.param_value
);
2944 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2948 printf("Value type not supported\n");
2951 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2953 err_param_value_parse
:
2954 pr_err("Value \"%s\" is not a number or not within range\n",
2955 dl
->opts
.param_value
);
2959 static int cmd_dev_param_show(struct dl
*dl
)
2961 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2962 struct nlmsghdr
*nlh
;
2965 if (dl_argc(dl
) == 0)
2966 flags
|= NLM_F_DUMP
;
2968 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2970 if (dl_argc(dl
) > 0) {
2971 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2972 DL_OPT_PARAM_NAME
, 0);
2977 pr_out_section_start(dl
, "param");
2978 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2979 pr_out_section_end(dl
);
2983 static int cmd_dev_param(struct dl
*dl
)
2985 if (dl_argv_match(dl
, "help")) {
2988 } else if (dl_argv_match(dl
, "show") ||
2989 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2991 return cmd_dev_param_show(dl
);
2992 } else if (dl_argv_match(dl
, "set")) {
2994 return cmd_dev_param_set(dl
);
2996 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3000 static void pr_out_action_stats(struct dl
*dl
, struct nlattr
*action_stats
)
3002 struct nlattr
*tb_stats_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
3003 struct nlattr
*nla_reload_stats_entry
, *nla_limit
, *nla_value
;
3004 enum devlink_reload_limit limit
;
3008 mnl_attr_for_each_nested(nla_reload_stats_entry
, action_stats
) {
3009 err
= mnl_attr_parse_nested(nla_reload_stats_entry
, attr_cb
,
3011 if (err
!= MNL_CB_OK
)
3014 nla_limit
= tb_stats_entry
[DEVLINK_ATTR_RELOAD_STATS_LIMIT
];
3015 nla_value
= tb_stats_entry
[DEVLINK_ATTR_RELOAD_STATS_VALUE
];
3016 if (!nla_limit
|| !nla_value
)
3019 check_indent_newline(dl
);
3020 limit
= mnl_attr_get_u8(nla_limit
);
3021 value
= mnl_attr_get_u32(nla_value
);
3022 print_uint_name_value(reload_limit_name(limit
), value
);
3026 static void pr_out_reload_stats(struct dl
*dl
, struct nlattr
*reload_stats
)
3028 struct nlattr
*nla_action_info
, *nla_action
, *nla_action_stats
;
3029 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3030 enum devlink_reload_action action
;
3033 mnl_attr_for_each_nested(nla_action_info
, reload_stats
) {
3034 err
= mnl_attr_parse_nested(nla_action_info
, attr_cb
, tb
);
3035 if (err
!= MNL_CB_OK
)
3037 nla_action
= tb
[DEVLINK_ATTR_RELOAD_ACTION
];
3038 nla_action_stats
= tb
[DEVLINK_ATTR_RELOAD_ACTION_STATS
];
3039 if (!nla_action
|| !nla_action_stats
)
3042 action
= mnl_attr_get_u8(nla_action
);
3043 pr_out_object_start(dl
, reload_action_name(action
));
3044 pr_out_action_stats(dl
, nla_action_stats
);
3045 pr_out_object_end(dl
);
3049 static void pr_out_reload_data(struct dl
*dl
, struct nlattr
**tb
)
3051 struct nlattr
*nla_reload_stats
, *nla_remote_reload_stats
;
3052 struct nlattr
*tb_stats
[DEVLINK_ATTR_MAX
+ 1] = {};
3053 uint8_t reload_failed
= 0;
3056 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
3057 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
3059 if (reload_failed
) {
3060 check_indent_newline(dl
);
3061 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
3063 if (!tb
[DEVLINK_ATTR_DEV_STATS
] || !dl
->stats
)
3065 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_DEV_STATS
], attr_cb
,
3067 if (err
!= MNL_CB_OK
)
3070 pr_out_object_start(dl
, "stats");
3072 nla_reload_stats
= tb_stats
[DEVLINK_ATTR_RELOAD_STATS
];
3073 if (nla_reload_stats
) {
3074 pr_out_object_start(dl
, "reload");
3075 pr_out_reload_stats(dl
, nla_reload_stats
);
3076 pr_out_object_end(dl
);
3078 nla_remote_reload_stats
= tb_stats
[DEVLINK_ATTR_REMOTE_RELOAD_STATS
];
3079 if (nla_remote_reload_stats
) {
3080 pr_out_object_start(dl
, "remote_reload");
3081 pr_out_reload_stats(dl
, nla_remote_reload_stats
);
3082 pr_out_object_end(dl
);
3085 pr_out_object_end(dl
);
3089 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
3091 if ((tb
[DEVLINK_ATTR_RELOAD_FAILED
] && mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
])) ||
3092 (tb
[DEVLINK_ATTR_DEV_STATS
] && dl
->stats
)) {
3093 __pr_out_handle_start(dl
, tb
, true, false);
3094 pr_out_reload_data(dl
, tb
);
3095 pr_out_handle_end(dl
);
3097 pr_out_handle(dl
, tb
);
3101 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3103 struct dl
*dl
= data
;
3104 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3105 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3107 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3108 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3109 return MNL_CB_ERROR
;
3115 static int cmd_dev_show(struct dl
*dl
)
3117 struct nlmsghdr
*nlh
;
3118 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3121 if (dl_argc(dl
) == 0)
3122 flags
|= NLM_F_DUMP
;
3124 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
3126 if (dl_argc(dl
) > 0) {
3127 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3132 pr_out_section_start(dl
, "dev");
3133 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
3134 pr_out_section_end(dl
);
3138 static void pr_out_reload_actions_performed(struct dl
*dl
, struct nlattr
**tb
)
3140 struct nlattr
*nla_actions_performed
;
3141 struct nla_bitfield32
*actions
;
3142 uint32_t actions_performed
;
3146 if (!tb
[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
])
3149 nla_actions_performed
= tb
[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
];
3150 len
= mnl_attr_get_payload_len(nla_actions_performed
);
3151 if (len
!= sizeof(*actions
))
3153 actions
= mnl_attr_get_payload(nla_actions_performed
);
3156 g_new_line_count
= 1; /* Avoid extra new line in non-json print */
3157 pr_out_array_start(dl
, "reload_actions_performed");
3158 actions_performed
= actions
->value
& actions
->selector
;
3159 for (action
= 0; action
<= DEVLINK_RELOAD_ACTION_MAX
; action
++) {
3160 if (BIT(action
) & actions_performed
) {
3161 check_indent_newline(dl
);
3162 print_string(PRINT_ANY
, NULL
, "%s",
3163 reload_action_name(action
));
3166 pr_out_array_end(dl
);
3167 if (!dl
->json_output
)
3171 static int cmd_dev_reload_cb(const struct nlmsghdr
*nlh
, void *data
)
3173 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3174 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3175 struct dl
*dl
= data
;
3177 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3178 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3179 !tb
[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
])
3180 return MNL_CB_ERROR
;
3182 pr_out_section_start(dl
, "reload");
3183 pr_out_reload_actions_performed(dl
, tb
);
3184 pr_out_section_end(dl
);
3189 static int cmd_dev_reload(struct dl
*dl
)
3191 struct nlmsghdr
*nlh
;
3194 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3199 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
3200 NLM_F_REQUEST
| NLM_F_ACK
);
3202 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
3203 DL_OPT_NETNS
| DL_OPT_RELOAD_ACTION
|
3204 DL_OPT_RELOAD_LIMIT
);
3208 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_reload_cb
, dl
);
3211 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
3212 const char *name
, int type
)
3214 struct nlattr
*version
;
3216 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
3217 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3218 const char *ver_value
;
3219 const char *ver_name
;
3222 if (mnl_attr_get_type(version
) != type
)
3225 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
3226 if (err
!= MNL_CB_OK
)
3229 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
3230 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
3234 pr_out_object_start(dl
, name
);
3238 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
3239 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
3241 check_indent_newline(dl
);
3242 print_string_name_value(ver_name
, ver_value
);
3243 if (!dl
->json_output
)
3248 pr_out_object_end(dl
);
3251 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
3252 struct nlattr
**tb
, bool has_versions
)
3254 __pr_out_handle_start(dl
, tb
, true, false);
3256 __pr_out_indent_inc();
3257 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
3258 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
3260 if (!dl
->json_output
)
3262 check_indent_newline(dl
);
3263 print_string(PRINT_ANY
, "driver", "driver %s",
3264 mnl_attr_get_str(nla_drv
));
3267 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
3268 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
3270 if (!dl
->json_output
)
3272 check_indent_newline(dl
);
3273 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
3274 mnl_attr_get_str(nla_sn
));
3277 if (tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
]) {
3278 struct nlattr
*nla_bsn
= tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
];
3280 if (!dl
->json_output
)
3282 check_indent_newline(dl
);
3283 print_string(PRINT_ANY
, "board.serial_number", "board.serial_number %s",
3284 mnl_attr_get_str(nla_bsn
));
3286 __pr_out_indent_dec();
3289 pr_out_object_start(dl
, "versions");
3291 pr_out_versions_single(dl
, nlh
, "fixed",
3292 DEVLINK_ATTR_INFO_VERSION_FIXED
);
3293 pr_out_versions_single(dl
, nlh
, "running",
3294 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
3295 pr_out_versions_single(dl
, nlh
, "stored",
3296 DEVLINK_ATTR_INFO_VERSION_STORED
);
3298 pr_out_object_end(dl
);
3301 pr_out_handle_end(dl
);
3304 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3306 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3307 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3308 bool has_versions
, has_info
;
3309 struct dl
*dl
= data
;
3311 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3313 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3314 return MNL_CB_ERROR
;
3316 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
3317 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
3318 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
3319 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
3320 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
3321 tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
] ||
3325 pr_out_info(dl
, nlh
, tb
, has_versions
);
3330 static int cmd_dev_info(struct dl
*dl
)
3332 struct nlmsghdr
*nlh
;
3333 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3336 if (dl_argv_match(dl
, "help")) {
3341 if (dl_argc(dl
) == 0)
3342 flags
|= NLM_F_DUMP
;
3344 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
3346 if (dl_argc(dl
) > 0) {
3347 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3352 pr_out_section_start(dl
, "info");
3353 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
3354 pr_out_section_end(dl
);
3358 struct cmd_dev_flash_status_ctx
{
3360 struct timespec time_of_last_status
;
3361 uint64_t status_msg_timeout
;
3362 size_t elapsed_time_msg_len
;
3364 char *last_component
;
3365 uint8_t not_first
:1,
3371 static int nullstrcmp(const char *str1
, const char *str2
)
3374 return strcmp(str1
, str2
);
3377 return str1
? 1 : -1;
3380 static void cmd_dev_flash_clear_elapsed_time(struct cmd_dev_flash_status_ctx
*ctx
)
3384 for (i
= 0; i
< ctx
->elapsed_time_msg_len
; i
++)
3385 pr_out_tty("\b \b");
3387 ctx
->elapsed_time_msg_len
= 0;
3390 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
3392 struct cmd_dev_flash_status_ctx
*ctx
= data
;
3393 struct dl_opts
*opts
= &ctx
->dl
->opts
;
3394 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3395 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3396 const char *component
= NULL
;
3397 uint64_t done
= 0, total
= 0;
3398 const char *msg
= NULL
;
3399 const char *bus_name
;
3400 const char *dev_name
;
3402 cmd_dev_flash_clear_elapsed_time(ctx
);
3404 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
3405 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3408 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3409 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3410 return MNL_CB_ERROR
;
3411 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3412 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3413 if (strcmp(bus_name
, opts
->bus_name
) ||
3414 strcmp(dev_name
, opts
->dev_name
))
3415 return MNL_CB_ERROR
;
3417 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3419 free(ctx
->last_msg
);
3420 free(ctx
->last_component
);
3421 ctx
->received_end
= 1;
3425 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3426 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3427 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3428 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3429 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3430 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3431 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3432 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3433 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
])
3434 ctx
->status_msg_timeout
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
]);
3436 ctx
->status_msg_timeout
= 0;
3438 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3439 !nullstrcmp(component
, ctx
->last_component
) &&
3440 ctx
->last_pc
&& ctx
->not_first
) {
3441 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3443 /* only update the last status timestamp if the message changed */
3444 clock_gettime(CLOCK_MONOTONIC
, &ctx
->time_of_last_status
);
3449 pr_out("[%s] ", component
);
3450 free(ctx
->last_component
);
3451 ctx
->last_component
= strdup(component
);
3455 free(ctx
->last_msg
);
3456 ctx
->last_msg
= strdup(msg
);
3460 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3471 static void cmd_dev_flash_time_elapsed(struct cmd_dev_flash_status_ctx
*ctx
)
3473 struct timespec now
, res
;
3475 clock_gettime(CLOCK_MONOTONIC
, &now
);
3477 res
.tv_sec
= now
.tv_sec
- ctx
->time_of_last_status
.tv_sec
;
3478 res
.tv_nsec
= now
.tv_nsec
- ctx
->time_of_last_status
.tv_nsec
;
3479 if (res
.tv_nsec
< 0) {
3481 res
.tv_nsec
+= 1000000000L;
3484 /* Only begin displaying an elapsed time message if we've waited a few
3485 * seconds with no response, or the status message included a timeout
3488 if (res
.tv_sec
> 2 || ctx
->status_msg_timeout
) {
3489 uint64_t elapsed_m
, elapsed_s
;
3493 /* clear the last elapsed time message, if we have one */
3494 cmd_dev_flash_clear_elapsed_time(ctx
);
3496 elapsed_m
= res
.tv_sec
/ 60;
3497 elapsed_s
= res
.tv_sec
% 60;
3500 * If we've elapsed a few seconds without receiving any status
3501 * notification from the device, we display a time elapsed
3502 * message. This has a few possible formats:
3504 * 1) just time elapsed, when no timeout was provided
3506 * 2) time elapsed out of a timeout that came from the device
3507 * driver via DEVLINK_CMD_FLASH_UPDATE_STATUS_TIMEOUT
3508 * " ( Xm Ys : Am Ys)"
3509 * 3) time elapsed if we still receive no status after
3510 * reaching the provided timeout.
3511 * " ( Xm Ys : timeout reached )"
3513 if (!ctx
->status_msg_timeout
) {
3514 len
= snprintf(msg
, sizeof(msg
),
3515 " ( %lum %lus )", elapsed_m
, elapsed_s
);
3516 } else if (res
.tv_sec
<= ctx
->status_msg_timeout
) {
3517 uint64_t timeout_m
, timeout_s
;
3519 timeout_m
= ctx
->status_msg_timeout
/ 60;
3520 timeout_s
= ctx
->status_msg_timeout
% 60;
3522 len
= snprintf(msg
, sizeof(msg
),
3523 " ( %lum %lus : %lum %lus )",
3524 elapsed_m
, elapsed_s
, timeout_m
, timeout_s
);
3526 len
= snprintf(msg
, sizeof(msg
),
3527 " ( %lum %lus : timeout reached )", elapsed_m
, elapsed_s
);
3530 ctx
->elapsed_time_msg_len
= len
;
3532 pr_out_tty("%s", msg
);
3537 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3538 struct mnlg_socket
*nlg_ntf
,
3541 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3542 struct timeval timeout
;
3549 for (i
= 0; i
< 3; i
++)
3551 FD_SET(pipe_r
, &fds
[0]);
3553 FD_SET(nlfd
, &fds
[0]);
3557 /* select only for a short while (1/10th of a second) in order to
3558 * allow periodically updating the screen with an elapsed time
3562 timeout
.tv_usec
= 100000;
3564 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], &timeout
) < 0) {
3567 pr_err("select() failed\n");
3570 if (FD_ISSET(nlfd
, &fds
[0])) {
3571 err
= _mnlg_socket_recv_run(nlg_ntf
,
3572 cmd_dev_flash_status_cb
, ctx
);
3576 if (FD_ISSET(pipe_r
, &fds
[0])) {
3577 err
= read(pipe_r
, &err2
, sizeof(err2
));
3579 pr_err("Failed to read pipe\n");
3584 ctx
->flash_done
= 1;
3586 cmd_dev_flash_time_elapsed(ctx
);
3591 static int cmd_dev_flash(struct dl
*dl
)
3593 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3594 struct mnlg_socket
*nlg_ntf
;
3595 struct nlmsghdr
*nlh
;
3601 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3606 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3607 NLM_F_REQUEST
| NLM_F_ACK
);
3609 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3610 DL_OPT_FLASH_COMPONENT
| DL_OPT_FLASH_OVERWRITE
);
3614 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3618 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3622 err
= pipe(pipe_fds
);
3625 pipe_r
= pipe_fds
[0];
3626 pipe_w
= pipe_fds
[1];
3634 /* In child, just execute the flash and pass returned
3635 * value through pipe once it is done.
3640 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3641 cc
= write(pipe_w
, &err
, sizeof(err
));
3643 exit(cc
!= sizeof(err
));
3647 /* initialize starting time to allow comparison for when to begin
3648 * displaying a time elapsed message.
3650 clock_gettime(CLOCK_MONOTONIC
, &ctx
.time_of_last_status
);
3653 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3656 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3658 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3661 mnlg_socket_close(nlg_ntf
);
3665 static int cmd_dev(struct dl
*dl
)
3667 if (dl_argv_match(dl
, "help")) {
3670 } else if (dl_argv_match(dl
, "show") ||
3671 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3673 return cmd_dev_show(dl
);
3674 } else if (dl_argv_match(dl
, "eswitch")) {
3676 return cmd_dev_eswitch(dl
);
3677 } else if (dl_argv_match(dl
, "reload")) {
3679 return cmd_dev_reload(dl
);
3680 } else if (dl_argv_match(dl
, "param")) {
3682 return cmd_dev_param(dl
);
3683 } else if (dl_argv_match(dl
, "info")) {
3685 return cmd_dev_info(dl
);
3686 } else if (dl_argv_match(dl
, "flash")) {
3688 return cmd_dev_flash(dl
);
3690 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3694 static void cmd_port_help(void)
3696 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3697 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3698 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3699 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3700 pr_err(" devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3701 pr_err(" devlink port health show [ DEV/PORT_INDEX reporter REPORTER_NAME ]\n");
3704 static const char *port_type_name(uint32_t type
)
3707 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3708 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3709 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3710 case DEVLINK_PORT_TYPE_IB
: return "ib";
3711 default: return "<unknown type>";
3715 static const char *port_flavour_name(uint16_t flavour
)
3718 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3720 case DEVLINK_PORT_FLAVOUR_CPU
:
3722 case DEVLINK_PORT_FLAVOUR_DSA
:
3724 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3726 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3728 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3731 return "<unknown flavour>";
3735 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3739 if (tb
[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
])
3740 print_uint(PRINT_ANY
, "controller", " controller %u",
3741 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
]));
3742 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3743 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3744 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3746 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3747 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3748 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3750 if (tb
[DEVLINK_ATTR_PORT_EXTERNAL
]) {
3753 external
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_PORT_EXTERNAL
]);
3754 print_bool(PRINT_ANY
, "external", " external %s", external
);
3758 static void pr_out_port_function(struct dl
*dl
, struct nlattr
**tb_port
)
3760 struct nlattr
*tb
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {};
3761 unsigned char *data
;
3762 SPRINT_BUF(hw_addr
);
3766 if (!tb_port
[DEVLINK_ATTR_PORT_FUNCTION
])
3769 err
= mnl_attr_parse_nested(tb_port
[DEVLINK_ATTR_PORT_FUNCTION
],
3770 function_attr_cb
, tb
);
3771 if (err
!= MNL_CB_OK
)
3774 if (!tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
])
3777 len
= mnl_attr_get_payload_len(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3778 data
= mnl_attr_get_payload(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3780 pr_out_object_start(dl
, "function");
3781 check_indent_newline(dl
);
3782 print_string(PRINT_ANY
, "hw_addr", "hw_addr %s",
3783 ll_addr_n2a(data
, len
, 0, hw_addr
, sizeof(hw_addr
)));
3784 if (!dl
->json_output
)
3785 __pr_out_indent_dec();
3786 pr_out_object_end(dl
);
3789 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3791 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3792 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3794 pr_out_port_handle_start(dl
, tb
, false);
3795 check_indent_newline(dl
);
3797 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3799 print_string(PRINT_ANY
, "type", "type %s",
3800 port_type_name(port_type
));
3802 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3804 if (port_type
!= des_port_type
)
3805 print_string(PRINT_ANY
, "des_type", " des_type %s",
3806 port_type_name(des_port_type
));
3809 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3810 print_string(PRINT_ANY
, "netdev", " netdev %s",
3811 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3813 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3814 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3815 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3817 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3818 uint16_t port_flavour
=
3819 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3821 print_string(PRINT_ANY
, "flavour", " flavour %s",
3822 port_flavour_name(port_flavour
));
3824 switch (port_flavour
) {
3825 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3826 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3827 pr_out_port_pfvf_num(dl
, tb
);
3833 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3834 uint32_t port_number
;
3836 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3837 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3839 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3840 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3841 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3842 if (tb
[DEVLINK_ATTR_PORT_SPLITTABLE
])
3843 print_bool(PRINT_ANY
, "splittable", " splittable %s",
3844 mnl_attr_get_u8(tb
[DEVLINK_ATTR_PORT_SPLITTABLE
]));
3845 if (tb
[DEVLINK_ATTR_PORT_LANES
])
3846 print_uint(PRINT_ANY
, "lanes", " lanes %u",
3847 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_LANES
]));
3849 pr_out_port_function(dl
, tb
);
3850 pr_out_port_handle_end(dl
);
3853 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3855 struct dl
*dl
= data
;
3856 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3857 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3859 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3860 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3861 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3862 return MNL_CB_ERROR
;
3863 pr_out_port(dl
, tb
);
3867 static int cmd_port_show(struct dl
*dl
)
3869 struct nlmsghdr
*nlh
;
3870 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3873 if (dl_argc(dl
) == 0)
3874 flags
|= NLM_F_DUMP
;
3876 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3878 if (dl_argc(dl
) > 0) {
3879 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3884 pr_out_section_start(dl
, "port");
3885 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3886 pr_out_section_end(dl
);
3890 static int cmd_port_set(struct dl
*dl
)
3892 struct nlmsghdr
*nlh
;
3895 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3896 NLM_F_REQUEST
| NLM_F_ACK
);
3898 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3902 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3905 static int cmd_port_split(struct dl
*dl
)
3907 struct nlmsghdr
*nlh
;
3910 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3911 NLM_F_REQUEST
| NLM_F_ACK
);
3913 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3917 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3920 static int cmd_port_unsplit(struct dl
*dl
)
3922 struct nlmsghdr
*nlh
;
3925 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3926 NLM_F_REQUEST
| NLM_F_ACK
);
3928 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3932 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3935 static void cmd_port_function_help(void)
3937 pr_err("Usage: devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3940 static int cmd_port_function_set(struct dl
*dl
)
3942 struct nlmsghdr
*nlh
;
3945 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
, NLM_F_REQUEST
| NLM_F_ACK
);
3947 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_FUNCTION_HW_ADDR
, 0);
3951 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3954 static int cmd_port_function(struct dl
*dl
)
3956 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3957 cmd_port_function_help();
3959 } else if (dl_argv_match(dl
, "set")) {
3961 return cmd_port_function_set(dl
);
3963 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3967 static int cmd_health(struct dl
*dl
);
3968 static int __cmd_health_show(struct dl
*dl
, bool show_device
, bool show_port
);
3970 static int cmd_port(struct dl
*dl
)
3972 if (dl_argv_match(dl
, "help")) {
3975 } else if (dl_argv_match(dl
, "show") ||
3976 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3978 return cmd_port_show(dl
);
3979 } else if (dl_argv_match(dl
, "set")) {
3981 return cmd_port_set(dl
);
3982 } else if (dl_argv_match(dl
, "split")) {
3984 return cmd_port_split(dl
);
3985 } else if (dl_argv_match(dl
, "unsplit")) {
3987 return cmd_port_unsplit(dl
);
3988 } else if (dl_argv_match(dl
, "function")) {
3990 return cmd_port_function(dl
);
3991 } else if (dl_argv_match(dl
, "health")) {
3993 if (dl_argv_match(dl
, "list") || dl_no_arg(dl
)
3994 || (dl_argv_match(dl
, "show") && dl_argc(dl
) == 1)) {
3996 return __cmd_health_show(dl
, false, true);
3998 return cmd_health(dl
);
4001 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4005 static void cmd_sb_help(void)
4007 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
4008 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
4009 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
4010 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
4011 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
4012 pr_err(" pool POOL_INDEX ]\n");
4013 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
4014 pr_err(" pool POOL_INDEX th THRESHOLD\n");
4015 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
4016 pr_err(" type { ingress | egress } ]\n");
4017 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
4018 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
4019 pr_err(" th THRESHOLD\n");
4020 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
4021 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
4022 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
4025 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
4027 pr_out_handle_start_arr(dl
, tb
);
4028 check_indent_newline(dl
);
4029 print_uint(PRINT_ANY
, "sb", "sb %u",
4030 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4031 print_uint(PRINT_ANY
, "size", " size %u",
4032 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
4033 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
4034 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
4035 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
4036 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
4037 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
4038 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
4039 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
4040 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
4041 pr_out_handle_end(dl
);
4044 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4046 struct dl
*dl
= data
;
4047 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4048 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4050 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4051 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4052 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
4053 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
4054 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
4055 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
4056 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
4057 return MNL_CB_ERROR
;
4062 static int cmd_sb_show(struct dl
*dl
)
4064 struct nlmsghdr
*nlh
;
4065 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4068 if (dl_argc(dl
) == 0)
4069 flags
|= NLM_F_DUMP
;
4071 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
4073 if (dl_argc(dl
) > 0) {
4074 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4079 pr_out_section_start(dl
, "sb");
4080 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
4081 pr_out_section_end(dl
);
4085 static const char *pool_type_name(uint8_t type
)
4088 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
4089 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
4090 default: return "<unknown type>";
4094 static const char *threshold_type_name(uint8_t type
)
4097 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
4098 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
4099 default: return "<unknown type>";
4103 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
4105 pr_out_handle_start_arr(dl
, tb
);
4106 check_indent_newline(dl
);
4107 print_uint(PRINT_ANY
, "sb", "sb %u",
4108 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4109 print_uint(PRINT_ANY
, "pool", " pool %u",
4110 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4111 print_string(PRINT_ANY
, "type", " type %s",
4112 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
4113 print_uint(PRINT_ANY
, "size", " size %u",
4114 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
4115 print_string(PRINT_ANY
, "thtype", " thtype %s",
4116 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
4117 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
4118 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
4119 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
4120 pr_out_handle_end(dl
);
4123 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4125 struct dl
*dl
= data
;
4126 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4127 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4129 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4130 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4131 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4132 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
4133 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
4134 return MNL_CB_ERROR
;
4135 pr_out_sb_pool(dl
, tb
);
4139 static int cmd_sb_pool_show(struct dl
*dl
)
4141 struct nlmsghdr
*nlh
;
4142 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4145 if (dl_argc(dl
) == 0)
4146 flags
|= NLM_F_DUMP
;
4148 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
4150 if (dl_argc(dl
) > 0) {
4151 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
4157 pr_out_section_start(dl
, "pool");
4158 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
4159 pr_out_section_end(dl
);
4163 static int cmd_sb_pool_set(struct dl
*dl
)
4165 struct nlmsghdr
*nlh
;
4168 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
4169 NLM_F_REQUEST
| NLM_F_ACK
);
4171 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
4172 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
4176 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4179 static int cmd_sb_pool(struct dl
*dl
)
4181 if (dl_argv_match(dl
, "help")) {
4184 } else if (dl_argv_match(dl
, "show") ||
4185 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4187 return cmd_sb_pool_show(dl
);
4188 } else if (dl_argv_match(dl
, "set")) {
4190 return cmd_sb_pool_set(dl
);
4192 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4196 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
4198 pr_out_port_handle_start_arr(dl
, tb
, true);
4199 check_indent_newline(dl
);
4200 print_uint(PRINT_ANY
, "sb", "sb %u",
4201 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4202 print_uint(PRINT_ANY
, "pool", " pool %u",
4203 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4204 print_uint(PRINT_ANY
, "threshold", " threshold %u",
4205 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
4206 pr_out_port_handle_end(dl
);
4209 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4211 struct dl
*dl
= data
;
4212 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4213 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4215 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4216 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4217 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4218 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
4219 return MNL_CB_ERROR
;
4220 pr_out_sb_port_pool(dl
, tb
);
4224 static int cmd_sb_port_pool_show(struct dl
*dl
)
4226 struct nlmsghdr
*nlh
;
4227 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4230 if (dl_argc(dl
) == 0)
4231 flags
|= NLM_F_DUMP
;
4233 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4235 if (dl_argc(dl
) > 0) {
4236 err
= dl_argv_parse_put(nlh
, dl
,
4237 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
4243 pr_out_section_start(dl
, "port_pool");
4244 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
4245 pr_out_section_end(dl
);
4249 static int cmd_sb_port_pool_set(struct dl
*dl
)
4251 struct nlmsghdr
*nlh
;
4254 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
4255 NLM_F_REQUEST
| NLM_F_ACK
);
4257 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
4258 DL_OPT_SB_TH
, DL_OPT_SB
);
4262 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4265 static int cmd_sb_port_pool(struct dl
*dl
)
4267 if (dl_argv_match(dl
, "help")) {
4270 } else if (dl_argv_match(dl
, "show") ||
4271 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4273 return cmd_sb_port_pool_show(dl
);
4274 } else if (dl_argv_match(dl
, "set")) {
4276 return cmd_sb_port_pool_set(dl
);
4278 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4282 static int cmd_sb_port(struct dl
*dl
)
4284 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4287 } else if (dl_argv_match(dl
, "pool")) {
4289 return cmd_sb_port_pool(dl
);
4291 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4295 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
4297 pr_out_port_handle_start_arr(dl
, tb
, true);
4298 check_indent_newline(dl
);
4299 print_uint(PRINT_ANY
, "sb", "sb %u",
4300 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4301 print_uint(PRINT_ANY
, "tc", " tc %u",
4302 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
4303 print_string(PRINT_ANY
, "type", " type %s",
4304 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
4305 print_uint(PRINT_ANY
, "pool", " pool %u",
4306 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4307 print_uint(PRINT_ANY
, "threshold", " threshold %u",
4308 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
4309 pr_out_port_handle_end(dl
);
4312 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4314 struct dl
*dl
= data
;
4315 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4316 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4318 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4319 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4320 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4321 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4322 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
4323 return MNL_CB_ERROR
;
4324 pr_out_sb_tc_bind(dl
, tb
);
4328 static int cmd_sb_tc_bind_show(struct dl
*dl
)
4330 struct nlmsghdr
*nlh
;
4331 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4334 if (dl_argc(dl
) == 0)
4335 flags
|= NLM_F_DUMP
;
4337 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4339 if (dl_argc(dl
) > 0) {
4340 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
4341 DL_OPT_SB_TYPE
, DL_OPT_SB
);
4346 pr_out_section_start(dl
, "tc_bind");
4347 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
4348 pr_out_section_end(dl
);
4352 static int cmd_sb_tc_bind_set(struct dl
*dl
)
4354 struct nlmsghdr
*nlh
;
4357 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
4358 NLM_F_REQUEST
| NLM_F_ACK
);
4360 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
4361 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
4366 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4369 static int cmd_sb_tc_bind(struct dl
*dl
)
4371 if (dl_argv_match(dl
, "help")) {
4374 } else if (dl_argv_match(dl
, "show") ||
4375 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4377 return cmd_sb_tc_bind_show(dl
);
4378 } else if (dl_argv_match(dl
, "set")) {
4380 return cmd_sb_tc_bind_set(dl
);
4382 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4386 static int cmd_sb_tc(struct dl
*dl
)
4388 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4391 } else if (dl_argv_match(dl
, "bind")) {
4393 return cmd_sb_tc_bind(dl
);
4395 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4400 struct list_head list
;
4404 uint32_t bound_pool_index
;
4408 struct list_head list
;
4411 uint32_t port_index
;
4413 struct list_head pool_list
;
4414 struct list_head ing_tc_list
;
4415 struct list_head eg_tc_list
;
4421 struct list_head port_list
;
4424 static struct occ_item
*occ_item_alloc(void)
4426 return calloc(1, sizeof(struct occ_item
));
4429 static void occ_item_free(struct occ_item
*occ_item
)
4434 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
4436 struct occ_port
*occ_port
;
4438 occ_port
= calloc(1, sizeof(*occ_port
));
4441 occ_port
->port_index
= port_index
;
4442 INIT_LIST_HEAD(&occ_port
->pool_list
);
4443 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
4444 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
4448 static void occ_port_free(struct occ_port
*occ_port
)
4450 struct occ_item
*occ_item
, *tmp
;
4452 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
4453 occ_item_free(occ_item
);
4454 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
4455 occ_item_free(occ_item
);
4456 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
4457 occ_item_free(occ_item
);
4460 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
4462 struct occ_show
*occ_show
;
4464 occ_show
= calloc(1, sizeof(*occ_show
));
4468 INIT_LIST_HEAD(&occ_show
->port_list
);
4472 static void occ_show_free(struct occ_show
*occ_show
)
4474 struct occ_port
*occ_port
, *tmp
;
4476 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
4477 occ_port_free(occ_port
);
4480 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
4483 struct occ_port
*occ_port
;
4484 uint32_t port_index
;
4486 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
4488 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
4489 if (occ_port
->port_index
== port_index
)
4492 occ_port
= occ_port_alloc(port_index
);
4495 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
4499 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
4502 struct occ_item
*occ_item
;
4505 pr_out_sp(7, " %s:", label
);
4506 list_for_each_entry(occ_item
, list
, list
) {
4507 if ((i
- 1) % 4 == 0 && i
!= 1)
4510 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
4511 occ_item
->bound_pool_index
);
4513 pr_out_sp(7, "%2u:", occ_item
->index
);
4514 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
4518 if ((i
- 1) % 4 != 0)
4522 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
4523 struct list_head
*list
,
4526 struct occ_item
*occ_item
;
4529 open_json_object(label
);
4530 list_for_each_entry(occ_item
, list
, list
) {
4531 sprintf(buf
, "%u", occ_item
->index
);
4532 open_json_object(buf
);
4534 print_uint(PRINT_JSON
, "bound_pool", NULL
,
4535 occ_item
->bound_pool_index
);
4536 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
4537 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
4538 close_json_object();
4540 close_json_object();
4543 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
4545 if (dl
->json_output
) {
4546 pr_out_json_occ_show_item_list(dl
, "pool",
4547 &occ_port
->pool_list
, false);
4548 pr_out_json_occ_show_item_list(dl
, "itc",
4549 &occ_port
->ing_tc_list
, true);
4550 pr_out_json_occ_show_item_list(dl
, "etc",
4551 &occ_port
->eg_tc_list
, true);
4554 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
4555 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
4556 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
4560 static void pr_out_occ_show(struct occ_show
*occ_show
)
4562 struct dl
*dl
= occ_show
->dl
;
4563 struct dl_opts
*opts
= &dl
->opts
;
4564 struct occ_port
*occ_port
;
4566 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
4567 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
4568 occ_port
->port_index
, true, false);
4569 pr_out_occ_show_port(dl
, occ_port
);
4570 pr_out_port_handle_end(dl
);
4574 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
4577 struct occ_port
*occ_port
;
4578 struct occ_item
*occ_item
;
4580 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4583 occ_port
= occ_port_get(occ_show
, tb
);
4585 occ_show
->err
= -ENOMEM
;
4589 occ_item
= occ_item_alloc();
4591 occ_show
->err
= -ENOMEM
;
4594 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4595 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4596 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4597 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4600 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4602 struct occ_show
*occ_show
= data
;
4603 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4604 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4606 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4607 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4608 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4609 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4610 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4611 return MNL_CB_ERROR
;
4612 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4616 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4619 struct occ_port
*occ_port
;
4620 struct occ_item
*occ_item
;
4623 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4626 occ_port
= occ_port_get(occ_show
, tb
);
4628 occ_show
->err
= -ENOMEM
;
4632 occ_item
= occ_item_alloc();
4634 occ_show
->err
= -ENOMEM
;
4637 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4638 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4639 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4640 occ_item
->bound_pool_index
=
4641 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4642 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4643 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4644 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4645 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4646 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4648 occ_item_free(occ_item
);
4651 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4653 struct occ_show
*occ_show
= data
;
4654 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4655 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4657 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4658 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4659 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4660 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4661 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4662 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4663 return MNL_CB_ERROR
;
4664 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4668 static int cmd_sb_occ_show(struct dl
*dl
)
4670 struct nlmsghdr
*nlh
;
4671 struct occ_show
*occ_show
;
4672 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4675 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4679 occ_show
= occ_show_alloc(dl
);
4683 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4685 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4686 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4690 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4692 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4693 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4697 pr_out_section_start(dl
, "occupancy");
4698 pr_out_occ_show(occ_show
);
4699 pr_out_section_end(dl
);
4702 occ_show_free(occ_show
);
4706 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4708 struct nlmsghdr
*nlh
;
4711 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4712 NLM_F_REQUEST
| NLM_F_ACK
);
4714 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4718 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4721 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4723 struct nlmsghdr
*nlh
;
4726 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4727 NLM_F_REQUEST
| NLM_F_ACK
);
4729 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4733 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4736 static int cmd_sb_occ(struct dl
*dl
)
4738 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4741 } else if (dl_argv_match(dl
, "show") ||
4742 dl_argv_match(dl
, "list")) {
4744 return cmd_sb_occ_show(dl
);
4745 } else if (dl_argv_match(dl
, "snapshot")) {
4747 return cmd_sb_occ_snapshot(dl
);
4748 } else if (dl_argv_match(dl
, "clearmax")) {
4750 return cmd_sb_occ_clearmax(dl
);
4752 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4756 static int cmd_sb(struct dl
*dl
)
4758 if (dl_argv_match(dl
, "help")) {
4761 } else if (dl_argv_match(dl
, "show") ||
4762 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4764 return cmd_sb_show(dl
);
4765 } else if (dl_argv_match(dl
, "pool")) {
4767 return cmd_sb_pool(dl
);
4768 } else if (dl_argv_match(dl
, "port")) {
4770 return cmd_sb_port(dl
);
4771 } else if (dl_argv_match(dl
, "tc")) {
4773 return cmd_sb_tc(dl
);
4774 } else if (dl_argv_match(dl
, "occupancy")) {
4776 return cmd_sb_occ(dl
);
4778 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4782 static const char *cmd_name(uint8_t cmd
)
4785 case DEVLINK_CMD_UNSPEC
: return "unspec";
4786 case DEVLINK_CMD_GET
: return "get";
4787 case DEVLINK_CMD_SET
: return "set";
4788 case DEVLINK_CMD_NEW
: return "new";
4789 case DEVLINK_CMD_DEL
: return "del";
4790 case DEVLINK_CMD_PORT_GET
: return "get";
4791 case DEVLINK_CMD_PORT_SET
: return "set";
4792 case DEVLINK_CMD_PORT_NEW
: return "new";
4793 case DEVLINK_CMD_PORT_DEL
: return "del";
4794 case DEVLINK_CMD_PARAM_GET
: return "get";
4795 case DEVLINK_CMD_PARAM_SET
: return "set";
4796 case DEVLINK_CMD_PARAM_NEW
: return "new";
4797 case DEVLINK_CMD_PARAM_DEL
: return "del";
4798 case DEVLINK_CMD_REGION_GET
: return "get";
4799 case DEVLINK_CMD_REGION_SET
: return "set";
4800 case DEVLINK_CMD_REGION_NEW
: return "new";
4801 case DEVLINK_CMD_REGION_DEL
: return "del";
4802 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4803 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4804 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4805 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4806 case DEVLINK_CMD_TRAP_GET
: return "get";
4807 case DEVLINK_CMD_TRAP_SET
: return "set";
4808 case DEVLINK_CMD_TRAP_NEW
: return "new";
4809 case DEVLINK_CMD_TRAP_DEL
: return "del";
4810 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4811 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4812 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4813 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4814 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4815 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4816 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4817 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4818 default: return "<unknown cmd>";
4822 static const char *cmd_obj(uint8_t cmd
)
4825 case DEVLINK_CMD_UNSPEC
: return "unspec";
4826 case DEVLINK_CMD_GET
:
4827 case DEVLINK_CMD_SET
:
4828 case DEVLINK_CMD_NEW
:
4829 case DEVLINK_CMD_DEL
:
4831 case DEVLINK_CMD_PORT_GET
:
4832 case DEVLINK_CMD_PORT_SET
:
4833 case DEVLINK_CMD_PORT_NEW
:
4834 case DEVLINK_CMD_PORT_DEL
:
4836 case DEVLINK_CMD_PARAM_GET
:
4837 case DEVLINK_CMD_PARAM_SET
:
4838 case DEVLINK_CMD_PARAM_NEW
:
4839 case DEVLINK_CMD_PARAM_DEL
:
4841 case DEVLINK_CMD_REGION_GET
:
4842 case DEVLINK_CMD_REGION_SET
:
4843 case DEVLINK_CMD_REGION_NEW
:
4844 case DEVLINK_CMD_REGION_DEL
:
4846 case DEVLINK_CMD_FLASH_UPDATE
:
4847 case DEVLINK_CMD_FLASH_UPDATE_END
:
4848 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4850 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4852 case DEVLINK_CMD_TRAP_GET
:
4853 case DEVLINK_CMD_TRAP_SET
:
4854 case DEVLINK_CMD_TRAP_NEW
:
4855 case DEVLINK_CMD_TRAP_DEL
:
4857 case DEVLINK_CMD_TRAP_GROUP_GET
:
4858 case DEVLINK_CMD_TRAP_GROUP_SET
:
4859 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4860 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4861 return "trap-group";
4862 case DEVLINK_CMD_TRAP_POLICER_GET
:
4863 case DEVLINK_CMD_TRAP_POLICER_SET
:
4864 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4865 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4866 return "trap-policer";
4867 default: return "<unknown obj>";
4871 static void pr_out_mon_header(uint8_t cmd
)
4873 if (!is_json_context()) {
4874 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4876 open_json_object(NULL
);
4877 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4878 open_json_object(cmd_obj(cmd
));
4882 static void pr_out_mon_footer(void)
4884 if (is_json_context()) {
4885 close_json_object();
4886 close_json_object();
4890 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4892 const char *obj
= cmd_obj(cmd
);
4893 unsigned int index
= 0;
4894 const char *cur_obj
;
4898 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4899 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4905 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4907 __pr_out_handle_start(dl
, tb
, true, false);
4909 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4910 check_indent_newline(dl
);
4911 print_string(PRINT_ANY
, "msg", "msg %s",
4912 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4914 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4915 check_indent_newline(dl
);
4916 print_string(PRINT_ANY
, "component", "component %s",
4917 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4920 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4921 pr_out_u64(dl
, "done",
4922 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4924 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4925 pr_out_u64(dl
, "total",
4926 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4928 pr_out_handle_end(dl
);
4931 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4932 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
,
4933 bool show_device
, bool show_port
);
4934 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4935 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4936 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4938 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4940 struct dl
*dl
= data
;
4941 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4942 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4943 uint8_t cmd
= genl
->cmd
;
4945 if (!cmd_filter_check(dl
, cmd
))
4949 case DEVLINK_CMD_GET
: /* fall through */
4950 case DEVLINK_CMD_SET
: /* fall through */
4951 case DEVLINK_CMD_NEW
: /* fall through */
4952 case DEVLINK_CMD_DEL
:
4953 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4954 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4955 return MNL_CB_ERROR
;
4956 pr_out_mon_header(genl
->cmd
);
4959 pr_out_mon_footer();
4961 case DEVLINK_CMD_PORT_GET
: /* fall through */
4962 case DEVLINK_CMD_PORT_SET
: /* fall through */
4963 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4964 case DEVLINK_CMD_PORT_DEL
:
4965 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4966 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4967 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4968 return MNL_CB_ERROR
;
4969 pr_out_mon_header(genl
->cmd
);
4970 pr_out_port(dl
, tb
);
4971 pr_out_mon_footer();
4973 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4974 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4975 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4976 case DEVLINK_CMD_PARAM_DEL
:
4977 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4978 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4979 !tb
[DEVLINK_ATTR_PARAM
])
4980 return MNL_CB_ERROR
;
4981 pr_out_mon_header(genl
->cmd
);
4982 pr_out_param(dl
, tb
, false);
4983 pr_out_mon_footer();
4985 case DEVLINK_CMD_REGION_GET
: /* fall through */
4986 case DEVLINK_CMD_REGION_SET
: /* fall through */
4987 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4988 case DEVLINK_CMD_REGION_DEL
:
4989 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4990 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4991 !tb
[DEVLINK_ATTR_REGION_NAME
])
4992 return MNL_CB_ERROR
;
4993 pr_out_mon_header(genl
->cmd
);
4994 pr_out_region(dl
, tb
);
4995 pr_out_mon_footer();
4997 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4998 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4999 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
5000 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5001 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
5002 return MNL_CB_ERROR
;
5003 pr_out_mon_header(genl
->cmd
);
5004 pr_out_flash_update(dl
, tb
);
5005 pr_out_mon_footer();
5007 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
5008 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5009 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5010 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
5011 return MNL_CB_ERROR
;
5012 pr_out_mon_header(genl
->cmd
);
5013 pr_out_health(dl
, tb
, true, true);
5014 pr_out_mon_footer();
5016 case DEVLINK_CMD_TRAP_GET
: /* fall through */
5017 case DEVLINK_CMD_TRAP_SET
: /* fall through */
5018 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
5019 case DEVLINK_CMD_TRAP_DEL
:
5020 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5021 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5022 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
5023 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
5024 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
5025 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
5026 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
5027 !tb
[DEVLINK_ATTR_STATS
])
5028 return MNL_CB_ERROR
;
5029 pr_out_mon_header(genl
->cmd
);
5030 pr_out_trap(dl
, tb
, false);
5031 pr_out_mon_footer();
5033 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
5034 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
5035 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
5036 case DEVLINK_CMD_TRAP_GROUP_DEL
:
5037 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5038 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5039 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
5040 !tb
[DEVLINK_ATTR_STATS
])
5041 return MNL_CB_ERROR
;
5042 pr_out_mon_header(genl
->cmd
);
5043 pr_out_trap_group(dl
, tb
, false);
5044 pr_out_mon_footer();
5046 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
5047 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
5048 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
5049 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
5050 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5051 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5052 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
5053 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
5054 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
5055 return MNL_CB_ERROR
;
5056 pr_out_mon_header(genl
->cmd
);
5057 pr_out_trap_policer(dl
, tb
, false);
5064 static int cmd_mon_show(struct dl
*dl
)
5067 unsigned int index
= 0;
5068 const char *cur_obj
;
5070 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
5071 if (strcmp(cur_obj
, "all") != 0 &&
5072 strcmp(cur_obj
, "dev") != 0 &&
5073 strcmp(cur_obj
, "port") != 0 &&
5074 strcmp(cur_obj
, "health") != 0 &&
5075 strcmp(cur_obj
, "trap") != 0 &&
5076 strcmp(cur_obj
, "trap-group") != 0 &&
5077 strcmp(cur_obj
, "trap-policer") != 0) {
5078 pr_err("Unknown object \"%s\"\n", cur_obj
);
5082 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
5085 open_json_object(NULL
);
5086 open_json_array(PRINT_JSON
, "mon");
5087 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
5088 close_json_array(PRINT_JSON
, NULL
);
5089 close_json_object();
5095 static void cmd_mon_help(void)
5097 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
5098 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
5101 static int cmd_mon(struct dl
*dl
)
5103 if (dl_argv_match(dl
, "help")) {
5107 return cmd_mon_show(dl
);
5110 struct dpipe_field
{
5113 unsigned int bitwidth
;
5114 enum devlink_dpipe_field_mapping_type mapping_type
;
5117 struct dpipe_header
{
5118 struct list_head list
;
5121 struct dpipe_field
*fields
;
5122 unsigned int fields_count
;
5125 struct dpipe_table
{
5126 struct list_head list
;
5128 unsigned int resource_id
;
5129 bool resource_valid
;
5132 struct dpipe_tables
{
5133 struct list_head table_list
;
5143 enum devlink_resource_unit unit
;
5148 struct list_head list
;
5149 struct list_head resource_list
;
5150 struct resource
*parent
;
5154 struct list_head resource_list
;
5157 struct resource_ctx
{
5160 struct resources
*resources
;
5161 struct dpipe_tables
*tables
;
5162 bool print_resources
;
5163 bool pending_change
;
5166 static struct resource
*resource_alloc(void)
5168 struct resource
*resource
;
5170 resource
= calloc(1, sizeof(struct resource
));
5173 INIT_LIST_HEAD(&resource
->resource_list
);
5177 static void resource_free(struct resource
*resource
)
5179 struct resource
*child_resource
, *tmp
;
5181 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
5183 free(child_resource
->name
);
5184 resource_free(child_resource
);
5189 static struct resources
*resources_alloc(void)
5191 struct resources
*resources
;
5193 resources
= calloc(1, sizeof(struct resources
));
5196 INIT_LIST_HEAD(&resources
->resource_list
);
5200 static void resources_free(struct resources
*resources
)
5202 struct resource
*resource
, *tmp
;
5204 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
5205 resource_free(resource
);
5208 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
5210 ctx
->resources
= resources_alloc();
5211 if (!ctx
->resources
)
5217 static void resource_ctx_fini(struct resource_ctx
*ctx
)
5219 resources_free(ctx
->resources
);
5225 struct list_head global_headers
;
5226 struct list_head local_headers
;
5227 struct dpipe_tables
*tables
;
5228 struct resources
*resources
;
5233 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
5235 struct dpipe_header
*header
;
5237 header
= calloc(1, sizeof(struct dpipe_header
));
5240 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
5241 if (!header
->fields
)
5242 goto err_fields_alloc
;
5243 header
->fields_count
= fields_count
;
5251 static void dpipe_header_free(struct dpipe_header
*header
)
5253 free(header
->fields
);
5257 static void dpipe_header_clear(struct dpipe_header
*header
)
5259 struct dpipe_field
*field
;
5262 for (i
= 0; i
< header
->fields_count
; i
++) {
5263 field
= &header
->fields
[i
];
5269 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
5270 struct dpipe_header
*header
, bool global
)
5273 list_add(&header
->list
, &ctx
->global_headers
);
5275 list_add(&header
->list
, &ctx
->local_headers
);
5278 static void dpipe_header_del(struct dpipe_header
*header
)
5280 list_del(&header
->list
);
5283 static struct dpipe_table
*dpipe_table_alloc(void)
5285 return calloc(1, sizeof(struct dpipe_table
));
5288 static void dpipe_table_free(struct dpipe_table
*table
)
5293 static struct dpipe_tables
*dpipe_tables_alloc(void)
5295 struct dpipe_tables
*tables
;
5297 tables
= calloc(1, sizeof(struct dpipe_tables
));
5300 INIT_LIST_HEAD(&tables
->table_list
);
5304 static void dpipe_tables_free(struct dpipe_tables
*tables
)
5306 struct dpipe_table
*table
, *tmp
;
5308 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
5309 dpipe_table_free(table
);
5313 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
5315 ctx
->tables
= dpipe_tables_alloc();
5320 INIT_LIST_HEAD(&ctx
->global_headers
);
5321 INIT_LIST_HEAD(&ctx
->local_headers
);
5325 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
5327 struct dpipe_header
*header
, *tmp
;
5329 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
5331 dpipe_header_del(header
);
5332 dpipe_header_clear(header
);
5333 dpipe_header_free(header
);
5335 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
5337 dpipe_header_del(header
);
5338 dpipe_header_clear(header
);
5339 dpipe_header_free(header
);
5341 dpipe_tables_free(ctx
->tables
);
5344 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
5345 uint32_t header_id
, bool global
)
5347 struct list_head
*header_list
;
5348 struct dpipe_header
*header
;
5351 header_list
= &ctx
->global_headers
;
5353 header_list
= &ctx
->local_headers
;
5354 list_for_each_entry(header
, header_list
, list
) {
5355 if (header
->id
!= header_id
)
5357 return header
->name
;
5362 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
5364 uint32_t field_id
, bool global
)
5366 struct list_head
*header_list
;
5367 struct dpipe_header
*header
;
5370 header_list
= &ctx
->global_headers
;
5372 header_list
= &ctx
->local_headers
;
5373 list_for_each_entry(header
, header_list
, list
) {
5374 if (header
->id
!= header_id
)
5376 return header
->fields
[field_id
].name
;
5382 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
5384 switch (mapping_type
) {
5385 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
5387 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
5395 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
5396 uint32_t field_id
, bool global
)
5398 enum devlink_dpipe_field_mapping_type mapping_type
;
5399 struct list_head
*header_list
;
5400 struct dpipe_header
*header
;
5403 header_list
= &ctx
->global_headers
;
5405 header_list
= &ctx
->local_headers
;
5406 list_for_each_entry(header
, header_list
, list
) {
5407 if (header
->id
!= header_id
)
5409 mapping_type
= header
->fields
[field_id
].mapping_type
;
5410 return dpipe_field_mapping_e2s(mapping_type
);
5415 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
5416 struct dpipe_field
*fields
,
5417 unsigned int field_count
)
5419 struct dpipe_field
*field
;
5422 for (i
= 0; i
< field_count
; i
++) {
5424 pr_out_entry_start(ctx
->dl
);
5425 check_indent_newline(ctx
->dl
);
5426 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
5427 if (ctx
->dl
->verbose
)
5428 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
5429 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
5430 if (field
->mapping_type
) {
5431 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
5432 dpipe_field_mapping_e2s(field
->mapping_type
));
5434 pr_out_entry_end(ctx
->dl
);
5439 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
5440 struct dpipe_header
*header
, bool global
)
5442 pr_out_handle_start_arr(ctx
->dl
, tb
);
5443 check_indent_newline(ctx
->dl
);
5444 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
5445 if (ctx
->dl
->verbose
) {
5446 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
5447 print_bool(PRINT_ANY
, "global", " global %s", global
);
5449 pr_out_array_start(ctx
->dl
, "field");
5450 pr_out_dpipe_fields(ctx
, header
->fields
,
5451 header
->fields_count
);
5452 pr_out_array_end(ctx
->dl
);
5453 pr_out_handle_end(ctx
->dl
);
5456 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
5459 struct dpipe_header
*header
;
5461 list_for_each_entry(header
, &ctx
->local_headers
, list
)
5462 pr_out_dpipe_header(ctx
, tb
, header
, false);
5464 list_for_each_entry(header
, &ctx
->global_headers
, list
)
5465 pr_out_dpipe_header(ctx
, tb
, header
, true);
5468 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
5470 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5474 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
5475 if (err
!= MNL_CB_OK
)
5477 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
5478 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
5479 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
5480 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
5483 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
5484 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5485 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
5486 field
->name
= strdup(name
);
5489 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
5493 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
5494 struct dpipe_field
*fields
)
5496 struct nlattr
*nla_field
;
5500 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
5501 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
5509 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
5511 struct nlattr
*nla_field
;
5512 unsigned int count
= 0;
5514 mnl_attr_for_each_nested(nla_field
, nla_fields
)
5519 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5521 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
5522 struct dpipe_header
*header
;
5523 unsigned int fields_count
;
5524 const char *header_name
;
5528 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
5529 if (err
!= MNL_CB_OK
)
5532 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
5533 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5534 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
5537 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
5538 header
= dpipe_header_alloc(fields_count
);
5542 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
5543 header
->name
= strdup(header_name
);
5544 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5545 header
->fields_count
= fields_count
;
5546 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5548 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
5552 dpipe_header_add(ctx
, header
, global
);
5556 dpipe_header_free(header
);
5560 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5562 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
5563 struct nlattr
*nla_header
;
5566 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
5567 err
= dpipe_header_get(ctx
, nla_header
);
5574 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
5576 struct dpipe_ctx
*ctx
= data
;
5577 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5578 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5581 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5582 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5583 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
5584 return MNL_CB_ERROR
;
5585 err
= dpipe_headers_get(ctx
, tb
);
5588 return MNL_CB_ERROR
;
5591 if (ctx
->print_headers
)
5592 pr_out_dpipe_headers(ctx
, tb
);
5596 static int cmd_dpipe_headers_show(struct dl
*dl
)
5598 struct nlmsghdr
*nlh
;
5599 struct dpipe_ctx ctx
= {};
5600 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5603 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5605 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5609 err
= dpipe_ctx_init(&ctx
, dl
);
5613 ctx
.print_headers
= true;
5615 pr_out_section_start(dl
, "header");
5616 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5618 pr_err("error get headers %s\n", strerror(ctx
.err
));
5619 pr_out_section_end(dl
);
5621 dpipe_ctx_fini(&ctx
);
5625 static void cmd_dpipe_help(void)
5627 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5628 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5629 pr_err(" [ counters_enabled { true | false } ]\n");
5630 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5631 pr_err(" devlink dpipe header show DEV\n");
5634 static int cmd_dpipe_header(struct dl
*dl
)
5636 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5639 } else if (dl_argv_match(dl
, "show")) {
5641 return cmd_dpipe_headers_show(dl
);
5643 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5648 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5650 switch (action_type
) {
5651 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5652 return "field_modify";
5658 struct dpipe_op_info
{
5664 struct dpipe_action
{
5665 struct dpipe_op_info info
;
5669 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5670 struct dpipe_ctx
*ctx
)
5672 struct dpipe_op_info
*op_info
= &action
->info
;
5673 const char *mapping
;
5675 check_indent_newline(ctx
->dl
);
5676 print_string(PRINT_ANY
, "type", "type %s",
5677 dpipe_action_type_e2s(action
->type
));
5678 print_string(PRINT_ANY
, "header", " header %s",
5679 dpipe_header_id2s(ctx
, op_info
->header_id
,
5680 op_info
->header_global
));
5681 print_string(PRINT_ANY
, "field", " field %s",
5682 dpipe_field_id2s(ctx
, op_info
->header_id
,
5684 op_info
->header_global
));
5685 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5687 op_info
->header_global
);
5689 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5692 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5694 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5697 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5698 if (err
!= MNL_CB_OK
)
5701 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5702 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5703 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5704 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5708 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5709 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5710 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5711 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5716 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5717 struct nlattr
*nla_actions
)
5719 struct nlattr
*nla_action
;
5720 struct dpipe_action action
;
5722 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5723 pr_out_entry_start(ctx
->dl
);
5724 if (dpipe_action_parse(&action
, nla_action
))
5725 goto err_action_parse
;
5726 pr_out_dpipe_action(&action
, ctx
);
5727 pr_out_entry_end(ctx
->dl
);
5732 pr_out_entry_end(ctx
->dl
);
5737 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5739 switch (match_type
) {
5740 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5741 return "field_exact";
5747 struct dpipe_match
{
5748 struct dpipe_op_info info
;
5752 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5753 struct dpipe_ctx
*ctx
)
5755 struct dpipe_op_info
*op_info
= &match
->info
;
5756 const char *mapping
;
5758 check_indent_newline(ctx
->dl
);
5759 print_string(PRINT_ANY
, "type", "type %s",
5760 dpipe_match_type_e2s(match
->type
));
5761 print_string(PRINT_ANY
, "header", " header %s",
5762 dpipe_header_id2s(ctx
, op_info
->header_id
,
5763 op_info
->header_global
));
5764 print_string(PRINT_ANY
, "field", " field %s",
5765 dpipe_field_id2s(ctx
, op_info
->header_id
,
5767 op_info
->header_global
));
5768 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5770 op_info
->header_global
);
5772 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5775 static int dpipe_match_parse(struct dpipe_match
*match
,
5779 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5782 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5783 if (err
!= MNL_CB_OK
)
5786 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5787 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5788 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5789 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5793 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5794 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5795 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5796 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5801 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5802 struct nlattr
*nla_matches
)
5804 struct nlattr
*nla_match
;
5805 struct dpipe_match match
;
5807 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5808 pr_out_entry_start(ctx
->dl
);
5809 if (dpipe_match_parse(&match
, nla_match
))
5810 goto err_match_parse
;
5811 pr_out_dpipe_match(&match
, ctx
);
5812 pr_out_entry_end(ctx
->dl
);
5817 pr_out_entry_end(ctx
->dl
);
5821 static struct resource
*
5822 resource_find(struct resources
*resources
, struct resource
*resource
,
5823 uint64_t resource_id
)
5825 struct list_head
*list_head
;
5828 list_head
= &resources
->resource_list
;
5830 list_head
= &resource
->resource_list
;
5832 list_for_each_entry(resource
, list_head
, list
) {
5833 struct resource
*child_resource
;
5835 if (resource
->id
== resource_id
)
5838 child_resource
= resource_find(resources
, resource
,
5841 return child_resource
;
5847 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5848 uint64_t resource_id
)
5850 struct resource
*resource
, *parent_resource
;
5851 const char del
[] = "/";
5855 resource
= resource_find(resources
, NULL
, resource_id
);
5859 for (parent_resource
= resource
; parent_resource
;
5860 parent_resource
= parent_resource
->parent
)
5861 path_len
+= strlen(parent_resource
->name
) + 1;
5864 path
= calloc(1, path_len
);
5868 path
+= path_len
- 1;
5869 for (parent_resource
= resource
; parent_resource
;
5870 parent_resource
= parent_resource
->parent
) {
5871 path
-= strlen(parent_resource
->name
);
5872 memcpy(path
, parent_resource
->name
,
5873 strlen(parent_resource
->name
));
5874 path
-= strlen(del
);
5875 memcpy(path
, del
, strlen(del
));
5877 check_indent_newline(dl
);
5878 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5882 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5884 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5885 struct dpipe_table
*table
;
5886 uint32_t resource_units
;
5887 bool counters_enabled
;
5888 bool resource_valid
;
5892 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5893 if (err
!= MNL_CB_OK
)
5896 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5897 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5898 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5899 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5900 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5904 table
= dpipe_table_alloc();
5908 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5909 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5910 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5912 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5914 if (resource_valid
) {
5915 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5916 table
->resource_valid
= true;
5919 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5920 if (!ctx
->print_tables
)
5923 check_indent_newline(ctx
->dl
);
5924 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5925 print_uint(PRINT_ANY
, "size", " size %u", size
);
5926 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5928 if (resource_valid
) {
5929 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5930 resource_path_print(ctx
->dl
, ctx
->resources
,
5931 table
->resource_id
);
5932 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5936 pr_out_array_start(ctx
->dl
, "match");
5937 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5938 goto err_matches_show
;
5939 pr_out_array_end(ctx
->dl
);
5941 pr_out_array_start(ctx
->dl
, "action");
5942 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5943 goto err_actions_show
;
5944 pr_out_array_end(ctx
->dl
);
5950 pr_out_array_end(ctx
->dl
);
5954 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5956 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5957 struct nlattr
*nla_table
;
5959 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5960 if (ctx
->print_tables
)
5961 pr_out_handle_start_arr(ctx
->dl
, tb
);
5962 if (dpipe_table_show(ctx
, nla_table
))
5963 goto err_table_show
;
5964 if (ctx
->print_tables
)
5965 pr_out_handle_end(ctx
->dl
);
5970 if (ctx
->print_tables
)
5971 pr_out_handle_end(ctx
->dl
);
5975 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5977 struct dpipe_ctx
*ctx
= data
;
5978 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5979 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5981 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5982 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5983 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5984 return MNL_CB_ERROR
;
5986 if (dpipe_tables_show(ctx
, tb
))
5987 return MNL_CB_ERROR
;
5991 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5993 static int cmd_dpipe_table_show(struct dl
*dl
)
5995 struct nlmsghdr
*nlh
;
5996 struct dpipe_ctx dpipe_ctx
= {};
5997 struct resource_ctx resource_ctx
= {};
5998 uint16_t flags
= NLM_F_REQUEST
;
6001 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
6005 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
6007 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6011 dpipe_ctx
.print_tables
= true;
6013 dl_opts_put(nlh
, dl
);
6014 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
6017 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
6018 goto err_headers_get
;
6021 err
= resource_ctx_init(&resource_ctx
, dl
);
6023 goto err_resource_ctx_init
;
6025 resource_ctx
.print_resources
= false;
6026 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
6027 dl_opts_put(nlh
, dl
);
6028 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6031 dpipe_ctx
.resources
= resource_ctx
.resources
;
6033 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6034 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
6035 dl_opts_put(nlh
, dl
);
6037 pr_out_section_start(dl
, "table");
6038 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
6039 pr_out_section_end(dl
);
6041 resource_ctx_fini(&resource_ctx
);
6042 dpipe_ctx_fini(&dpipe_ctx
);
6045 err_resource_ctx_init
:
6047 dpipe_ctx_fini(&dpipe_ctx
);
6051 static int cmd_dpipe_table_set(struct dl
*dl
)
6053 struct nlmsghdr
*nlh
;
6056 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
6057 NLM_F_REQUEST
| NLM_F_ACK
);
6059 err
= dl_argv_parse_put(nlh
, dl
,
6060 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
6061 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
6065 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6068 enum dpipe_value_type
{
6069 DPIPE_VALUE_TYPE_VALUE
,
6070 DPIPE_VALUE_TYPE_MASK
,
6074 dpipe_value_type_e2s(enum dpipe_value_type type
)
6077 case DPIPE_VALUE_TYPE_VALUE
:
6079 case DPIPE_VALUE_TYPE_MASK
:
6080 return "value_mask";
6086 struct dpipe_field_printer
{
6087 unsigned int field_id
;
6088 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
6091 struct dpipe_header_printer
{
6092 struct dpipe_field_printer
*printers
;
6093 unsigned int printers_count
;
6094 unsigned int header_id
;
6097 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
6098 enum dpipe_value_type type
,
6101 struct in_addr ip_addr
;
6103 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
6104 check_indent_newline(ctx
->dl
);
6105 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
6109 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
6110 enum dpipe_value_type type
,
6113 check_indent_newline(ctx
->dl
);
6114 print_string_name_value(dpipe_value_type_e2s(type
),
6115 ether_ntoa((struct ether_addr
*)value
));
6118 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
6119 enum dpipe_value_type type
,
6122 char str
[INET6_ADDRSTRLEN
];
6124 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
6125 check_indent_newline(ctx
->dl
);
6126 print_string_name_value(dpipe_value_type_e2s(type
), str
);
6129 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
6131 .printer
= dpipe_field_printer_ipv4_addr
,
6132 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
6136 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
6137 .printers
= dpipe_field_printers_ipv4
,
6138 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
6139 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
6142 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
6144 .printer
= dpipe_field_printer_ethernet_addr
,
6145 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
6149 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
6150 .printers
= dpipe_field_printers_ethernet
,
6151 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
6152 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
6155 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
6157 .printer
= dpipe_field_printer_ipv6_addr
,
6158 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
6162 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
6163 .printers
= dpipe_field_printers_ipv6
,
6164 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
6165 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
6168 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
6169 &dpipe_header_printer_ipv4
,
6170 &dpipe_header_printer_ethernet
,
6171 &dpipe_header_printer_ipv6
,
6174 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
6175 struct dpipe_op_info
*info
,
6176 enum dpipe_value_type type
,
6179 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
6180 struct dpipe_header_printer
*header_printer
;
6181 struct dpipe_field_printer
*field_printer
;
6182 unsigned int field_printers_count
;
6186 for (i
= 0; i
< header_printers_count
; i
++) {
6187 header_printer
= dpipe_header_printers
[i
];
6188 if (header_printer
->header_id
!= info
->header_id
)
6190 field_printers_count
= header_printer
->printers_count
;
6191 for (j
= 0; j
< field_printers_count
; j
++) {
6192 field_printer
= &header_printer
->printers
[j
];
6193 if (field_printer
->field_id
!= info
->field_id
)
6195 field_printer
->printer(ctx
, type
, value
);
6203 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
6205 unsigned int value_len
,
6206 struct dpipe_op_info
*info
,
6207 enum dpipe_value_type type
)
6209 if (info
->header_global
&&
6210 !dpipe_print_prot_header(ctx
, info
, type
, value
))
6213 if (value_len
== sizeof(uint32_t)) {
6214 uint32_t *value_32
= value
;
6216 check_indent_newline(ctx
->dl
);
6217 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
6221 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
6222 struct nlattr
**nla_match_value
,
6223 struct dpipe_op_info
*info
)
6225 void *value
, *value_mask
;
6226 uint32_t value_mapping
;
6230 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
6231 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
6233 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
6234 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
6237 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
6238 check_indent_newline(ctx
->dl
);
6239 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
6243 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
6244 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
6245 DPIPE_VALUE_TYPE_MASK
);
6248 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
6251 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
6254 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
6255 struct dpipe_match match
;
6258 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
6259 if (err
!= MNL_CB_OK
)
6262 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
6263 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
6267 pr_out_entry_start(ctx
->dl
);
6268 if (dpipe_match_parse(&match
,
6269 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
6270 goto err_match_parse
;
6271 pr_out_dpipe_match(&match
, ctx
);
6272 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
6273 pr_out_entry_end(ctx
->dl
);
6278 pr_out_entry_end(ctx
->dl
);
6282 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
6285 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
6286 struct dpipe_action action
;
6289 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
6290 if (err
!= MNL_CB_OK
)
6293 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
6294 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
6298 pr_out_entry_start(ctx
->dl
);
6299 if (dpipe_action_parse(&action
,
6300 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
6301 goto err_action_parse
;
6302 pr_out_dpipe_action(&action
, ctx
);
6303 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
6304 pr_out_entry_end(ctx
->dl
);
6309 pr_out_entry_end(ctx
->dl
);
6314 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
6315 struct nlattr
*nla_action_values
)
6317 struct nlattr
*nla_action_value
;
6319 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
6320 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
6327 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
6328 struct nlattr
*nla_match_values
)
6330 struct nlattr
*nla_match_value
;
6332 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
6333 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
6339 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
6341 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
6342 uint32_t entry_index
;
6346 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
6347 if (err
!= MNL_CB_OK
)
6350 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
6351 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
6352 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
6356 check_indent_newline(ctx
->dl
);
6357 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
6358 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
6360 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
6361 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
6362 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
6365 pr_out_array_start(ctx
->dl
, "match_value");
6366 if (dpipe_tables_match_values_show(ctx
,
6367 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
6368 goto err_match_values_show
;
6369 pr_out_array_end(ctx
->dl
);
6371 pr_out_array_start(ctx
->dl
, "action_value");
6372 if (dpipe_tables_action_values_show(ctx
,
6373 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
6374 goto err_action_values_show
;
6375 pr_out_array_end(ctx
->dl
);
6378 err_action_values_show
:
6379 err_match_values_show
:
6380 pr_out_array_end(ctx
->dl
);
6384 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
6386 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
6387 struct nlattr
*nla_entry
;
6389 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
6390 pr_out_handle_start_arr(ctx
->dl
, tb
);
6391 if (dpipe_entry_show(ctx
, nla_entry
))
6392 goto err_entry_show
;
6393 pr_out_handle_end(ctx
->dl
);
6398 pr_out_handle_end(ctx
->dl
);
6402 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6404 struct dpipe_ctx
*ctx
= data
;
6405 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6406 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6408 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6409 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6410 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
6411 return MNL_CB_ERROR
;
6413 if (dpipe_table_entries_show(ctx
, tb
))
6414 return MNL_CB_ERROR
;
6418 static int cmd_dpipe_table_dump(struct dl
*dl
)
6420 struct nlmsghdr
*nlh
;
6421 struct dpipe_ctx ctx
= {};
6422 uint16_t flags
= NLM_F_REQUEST
;
6425 err
= dpipe_ctx_init(&ctx
, dl
);
6429 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
6433 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
6434 dl_opts_put(nlh
, dl
);
6435 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
6437 pr_err("error get headers %s\n", strerror(ctx
.err
));
6441 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6442 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
6443 dl_opts_put(nlh
, dl
);
6445 pr_out_section_start(dl
, "table_entry");
6446 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
6447 pr_out_section_end(dl
);
6449 dpipe_ctx_fini(&ctx
);
6453 static int cmd_dpipe_table(struct dl
*dl
)
6455 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6458 } else if (dl_argv_match(dl
, "show")) {
6460 return cmd_dpipe_table_show(dl
);
6461 } else if (dl_argv_match(dl
, "set")) {
6463 return cmd_dpipe_table_set(dl
);
6464 } else if (dl_argv_match(dl
, "dump")) {
6466 return cmd_dpipe_table_dump(dl
);
6468 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6472 static int cmd_dpipe(struct dl
*dl
)
6474 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6477 } else if (dl_argv_match(dl
, "header")) {
6479 return cmd_dpipe_header(dl
);
6480 } else if (dl_argv_match(dl
, "table")) {
6482 return cmd_dpipe_table(dl
);
6484 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6489 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
6490 struct nlattr
**nla_resource
)
6492 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
6493 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
6494 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
6495 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
6496 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
6497 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
6498 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
6502 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
6503 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
6504 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
6505 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
6506 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
6507 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
6508 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
6510 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
6511 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
6513 resource
->size_new
= resource
->size
;
6515 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
6516 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
6517 resource
->occ_valid
= true;
6520 if (resource
->size_new
!= resource
->size
)
6521 ctx
->pending_change
= true;
6527 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
6528 struct resource
*parent_resource
, struct nlattr
*nl
)
6530 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
6531 struct nlattr
*nla_child_resource
;
6532 struct nlattr
*nla_resources
;
6542 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
6543 if (err
!= MNL_CB_OK
)
6546 err
= resource_parse(ctx
, resource
, nla_resource
);
6550 resource
->parent
= parent_resource
;
6551 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
6554 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
6555 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
6557 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
6558 struct resource
*child_resource
;
6559 struct list_head
*list
;
6561 child_resource
= resource_alloc();
6562 if (!child_resource
)
6566 list
= &ctx
->resources
->resource_list
;
6568 list
= &resource
->resource_list
;
6570 list_add_tail(&child_resource
->list
, list
);
6571 err
= resource_get(ctx
, child_resource
, resource
,
6572 nla_child_resource
);
6580 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
6583 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
6584 default: return "<unknown unit>";
6588 static void resource_show(struct resource
*resource
,
6589 struct resource_ctx
*ctx
)
6591 struct resource
*child_resource
;
6592 struct dpipe_table
*table
;
6593 struct dl
*dl
= ctx
->dl
;
6596 check_indent_newline(dl
);
6597 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6599 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6600 pr_out_u64(dl
, "size", resource
->size
);
6601 if (resource
->size
!= resource
->size_new
)
6602 pr_out_u64(dl
, "size_new", resource
->size_new
);
6603 if (resource
->occ_valid
)
6604 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6605 print_string(PRINT_ANY
, "unit", " unit %s",
6606 resource_unit_str_get(resource
->unit
));
6608 if (resource
->size_min
!= resource
->size_max
) {
6609 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6610 resource
->size_min
);
6611 pr_out_u64(dl
, "size_max", resource
->size_max
);
6612 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6613 resource
->size_gran
);
6616 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6617 if (table
->resource_id
== resource
->id
&&
6618 table
->resource_valid
)
6622 pr_out_array_start(dl
, "dpipe_tables");
6624 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6627 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6628 if (table
->resource_id
!= resource
->id
||
6629 !table
->resource_valid
)
6631 pr_out_entry_start(dl
);
6632 check_indent_newline(dl
);
6633 print_string(PRINT_ANY
, "table_name", "table_name %s",
6635 pr_out_entry_end(dl
);
6638 pr_out_array_end(dl
);
6640 if (list_empty(&resource
->resource_list
))
6643 if (ctx
->pending_change
) {
6644 check_indent_newline(dl
);
6645 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6646 resource
->size_valid
? "true" : "false");
6648 pr_out_array_start(dl
, "resources");
6649 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6650 pr_out_entry_start(dl
);
6651 resource_show(child_resource
, ctx
);
6652 pr_out_entry_end(dl
);
6654 pr_out_array_end(dl
);
6658 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6660 struct resources
*resources
= ctx
->resources
;
6661 struct resource
*resource
;
6663 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6664 pr_out_handle_start_arr(ctx
->dl
, tb
);
6665 resource_show(resource
, ctx
);
6666 pr_out_handle_end(ctx
->dl
);
6670 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6672 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6675 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6677 struct resource_ctx
*ctx
= data
;
6678 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6679 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6682 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6683 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6684 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6685 return MNL_CB_ERROR
;
6687 err
= resources_get(ctx
, tb
);
6690 return MNL_CB_ERROR
;
6693 if (ctx
->print_resources
)
6694 resources_show(ctx
, tb
);
6699 static int cmd_resource_show(struct dl
*dl
)
6701 struct nlmsghdr
*nlh
;
6702 struct dpipe_ctx dpipe_ctx
= {};
6703 struct resource_ctx resource_ctx
= {};
6706 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6710 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6712 dl_opts_put(nlh
, dl
);
6714 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6718 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6721 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6725 err
= resource_ctx_init(&resource_ctx
, dl
);
6729 resource_ctx
.print_resources
= true;
6730 resource_ctx
.tables
= dpipe_ctx
.tables
;
6731 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6732 NLM_F_REQUEST
| NLM_F_ACK
);
6733 dl_opts_put(nlh
, dl
);
6734 pr_out_section_start(dl
, "resources");
6735 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6737 pr_out_section_end(dl
);
6738 resource_ctx_fini(&resource_ctx
);
6740 dpipe_ctx_fini(&dpipe_ctx
);
6744 static void cmd_resource_help(void)
6746 pr_err("Usage: devlink resource show DEV\n"
6747 " devlink resource set DEV path PATH size SIZE\n");
6750 static struct resource
*
6751 resource_find_by_name(struct list_head
*list
, char *name
)
6753 struct resource
*resource
;
6755 list_for_each_entry(resource
, list
, list
) {
6756 if (!strcmp(resource
->name
, name
))
6763 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6764 uint32_t *p_resource_id
, bool *p_resource_valid
)
6766 struct resource
*resource
;
6767 uint32_t resource_id
= 0;
6768 char *resource_path_dup
;
6769 struct list_head
*list
;
6770 const char del
[] = "/";
6771 char *resource_name
;
6773 resource_path_dup
= strdup(resource_path
);
6774 list
= &ctx
->resources
->resource_list
;
6775 resource_name
= strtok(resource_path_dup
, del
);
6776 while (resource_name
!= NULL
) {
6777 resource
= resource_find_by_name(list
, resource_name
);
6779 goto err_resource_lookup
;
6781 list
= &resource
->resource_list
;
6782 resource_name
= strtok(NULL
, del
);
6783 resource_id
= resource
->id
;
6785 free(resource_path_dup
);
6786 *p_resource_valid
= true;
6787 *p_resource_id
= resource_id
;
6790 err_resource_lookup
:
6791 free(resource_path_dup
);
6795 static int cmd_resource_set(struct dl
*dl
)
6797 struct nlmsghdr
*nlh
;
6798 struct resource_ctx ctx
= {};
6801 err
= resource_ctx_init(&ctx
, dl
);
6805 ctx
.print_resources
= false;
6806 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6807 DL_OPT_RESOURCE_SIZE
, 0);
6811 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6813 dl_opts_put(nlh
, dl
);
6814 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6816 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6820 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6821 &dl
->opts
.resource_id
,
6822 &dl
->opts
.resource_id_valid
);
6824 pr_err("error parsing resource path %s\n", strerror(-err
));
6828 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6829 NLM_F_REQUEST
| NLM_F_ACK
);
6831 dl_opts_put(nlh
, dl
);
6832 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6834 resource_ctx_fini(&ctx
);
6838 static int cmd_resource(struct dl
*dl
)
6840 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6841 cmd_resource_help();
6843 } else if (dl_argv_match(dl
, "show")) {
6845 return cmd_resource_show(dl
);
6846 } else if (dl_argv_match(dl
, "set")) {
6848 return cmd_resource_set(dl
);
6850 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6854 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6856 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6857 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6858 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6861 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6862 if (dl
->json_output
)
6863 open_json_object(buf
);
6868 static void pr_out_region_handle_end(struct dl
*dl
)
6870 if (dl
->json_output
)
6871 close_json_object();
6876 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6878 __pr_out_indent_newline(dl
);
6879 if (dl
->json_output
)
6880 open_json_array(PRINT_JSON
, "snapshot");
6882 pr_out("snapshot %s", array
? "[" : "");
6885 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6887 if (dl
->json_output
)
6888 close_json_array(PRINT_JSON
, NULL
);
6893 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6895 uint32_t snapshot_id
;
6897 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6900 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6902 if (dl
->json_output
)
6903 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6905 pr_out("%s%u", index
? " " : "", snapshot_id
);
6908 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6910 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6911 struct nlattr
*nla_sanpshot
;
6914 pr_out_region_snapshots_start(dl
, true);
6915 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6916 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6917 if (err
!= MNL_CB_OK
)
6919 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6921 pr_out_region_snapshots_end(dl
, true);
6924 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6926 pr_out_region_snapshots_start(dl
, false);
6927 pr_out_region_snapshots_id(dl
, tb
, 0);
6928 pr_out_region_snapshots_end(dl
, false);
6931 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6933 pr_out_region_handle_start(dl
, tb
);
6935 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6936 pr_out_u64(dl
, "size",
6937 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6939 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6940 pr_out_snapshots(dl
, tb
);
6942 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6943 pr_out_snapshot(dl
, tb
);
6945 pr_out_region_handle_end(dl
);
6948 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6950 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6951 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6952 struct dl
*dl
= data
;
6954 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6955 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6956 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6957 return MNL_CB_ERROR
;
6959 pr_out_region(dl
, tb
);
6964 static int cmd_region_show(struct dl
*dl
)
6966 struct nlmsghdr
*nlh
;
6967 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6970 if (dl_argc(dl
) == 0)
6971 flags
|= NLM_F_DUMP
;
6973 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6975 if (dl_argc(dl
) > 0) {
6976 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6981 pr_out_section_start(dl
, "regions");
6982 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6983 pr_out_section_end(dl
);
6987 static int cmd_region_snapshot_del(struct dl
*dl
)
6989 struct nlmsghdr
*nlh
;
6992 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6993 NLM_F_REQUEST
| NLM_F_ACK
);
6995 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6996 DL_OPT_REGION_SNAPSHOT_ID
, 0);
7000 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7003 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
7005 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
7006 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7007 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
7008 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7009 struct dl
*dl
= data
;
7012 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7013 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7014 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
7015 return MNL_CB_ERROR
;
7017 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
7018 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
7019 if (err
!= MNL_CB_OK
)
7020 return MNL_CB_ERROR
;
7022 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
7023 if (!nla_chunk_data
)
7026 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
7027 if (!nla_chunk_addr
)
7030 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
7031 mnl_attr_get_payload_len(nla_chunk_data
),
7032 mnl_attr_get_u64(nla_chunk_addr
));
7037 static int cmd_region_dump(struct dl
*dl
)
7039 struct nlmsghdr
*nlh
;
7042 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
7043 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
7045 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
7046 DL_OPT_REGION_SNAPSHOT_ID
, 0);
7050 pr_out_section_start(dl
, "dump");
7051 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
7052 pr_out_section_end(dl
);
7053 if (!dl
->json_output
)
7058 static int cmd_region_read(struct dl
*dl
)
7060 struct nlmsghdr
*nlh
;
7063 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
7064 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
7066 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
7067 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
7068 DL_OPT_REGION_SNAPSHOT_ID
, 0);
7072 pr_out_section_start(dl
, "read");
7073 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
7074 pr_out_section_end(dl
);
7075 if (!dl
->json_output
)
7080 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
7082 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7083 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7084 struct dl
*dl
= data
;
7086 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7087 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7088 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
7089 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
7090 return MNL_CB_ERROR
;
7092 pr_out_region(dl
, tb
);
7097 static int cmd_region_snapshot_new(struct dl
*dl
)
7099 struct nlmsghdr
*nlh
;
7102 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
7103 NLM_F_REQUEST
| NLM_F_ACK
);
7105 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
7106 DL_OPT_REGION_SNAPSHOT_ID
);
7110 pr_out_section_start(dl
, "regions");
7111 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
7112 pr_out_section_end(dl
);
7116 static void cmd_region_help(void)
7118 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
7119 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
7120 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
7121 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
7122 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
7125 static int cmd_region(struct dl
*dl
)
7127 if (dl_no_arg(dl
)) {
7128 return cmd_region_show(dl
);
7129 } else if (dl_argv_match(dl
, "help")) {
7132 } else if (dl_argv_match(dl
, "show")) {
7134 return cmd_region_show(dl
);
7135 } else if (dl_argv_match(dl
, "del")) {
7137 return cmd_region_snapshot_del(dl
);
7138 } else if (dl_argv_match(dl
, "dump")) {
7140 return cmd_region_dump(dl
);
7141 } else if (dl_argv_match(dl
, "read")) {
7143 return cmd_region_read(dl
);
7144 } else if (dl_argv_match(dl
, "new")) {
7146 return cmd_region_snapshot_new(dl
);
7148 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7152 static int cmd_health_set_params(struct dl
*dl
)
7154 struct nlmsghdr
*nlh
;
7157 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
7158 NLM_F_REQUEST
| NLM_F_ACK
);
7159 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
| DL_OPT_HEALTH_REPORTER_NAME
,
7160 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
7161 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
7162 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
7166 dl_opts_put(nlh
, dl
);
7167 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7170 static int cmd_health_dump_clear(struct dl
*dl
)
7172 struct nlmsghdr
*nlh
;
7175 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
7176 NLM_F_REQUEST
| NLM_F_ACK
);
7178 err
= dl_argv_parse_put(nlh
, dl
,
7179 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7180 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7184 dl_opts_put(nlh
, dl
);
7185 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7188 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
7193 check_indent_newline(dl
);
7196 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
7199 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
7202 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
7205 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
7208 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
7210 case MNL_TYPE_NUL_STRING
:
7211 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
7213 case MNL_TYPE_BINARY
:
7214 len
= mnl_attr_get_payload_len(nl_data
);
7215 data
= mnl_attr_get_payload(nl_data
);
7216 pr_out_binary_value(dl
, data
, len
);
7224 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
7229 pr_out_name(dl
, *name
);
7236 struct list_head list
;
7239 struct fmsg_cb_data
{
7243 struct list_head entry_list
;
7246 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
7247 uint8_t *attr_value
, bool insert
)
7249 struct nest_entry
*entry
;
7252 entry
= malloc(sizeof(struct nest_entry
));
7256 entry
->attr_type
= *attr_value
;
7257 list_add(&entry
->list
, &fmsg_data
->entry_list
);
7259 if (list_empty(&fmsg_data
->entry_list
))
7260 return MNL_CB_ERROR
;
7261 entry
= list_first_entry(&fmsg_data
->entry_list
,
7262 struct nest_entry
, list
);
7263 *attr_value
= entry
->attr_type
;
7264 list_del(&entry
->list
);
7270 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
7273 pr_out_fmsg_name(dl
, name
);
7275 __pr_out_indent_inc();
7278 static void pr_out_fmsg_group_end(struct dl
*dl
)
7281 __pr_out_indent_dec();
7284 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
7286 if (dl
->json_output
) {
7287 pr_out_fmsg_name(dl
, name
);
7288 open_json_object(NULL
);
7290 pr_out_fmsg_group_start(dl
, name
);
7294 static void pr_out_fmsg_end_object(struct dl
*dl
)
7296 if (dl
->json_output
)
7297 close_json_object();
7299 pr_out_fmsg_group_end(dl
);
7302 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
7304 if (dl
->json_output
) {
7305 pr_out_fmsg_name(dl
, name
);
7306 open_json_array(PRINT_JSON
, NULL
);
7308 pr_out_fmsg_group_start(dl
, name
);
7312 static void pr_out_fmsg_end_array(struct dl
*dl
)
7314 if (dl
->json_output
)
7315 close_json_array(PRINT_JSON
, NULL
);
7317 pr_out_fmsg_group_end(dl
);
7320 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
7323 struct dl
*dl
= fmsg_data
->dl
;
7324 uint8_t value
= nest_value
;
7327 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
7328 if (err
!= MNL_CB_OK
)
7332 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
7334 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
7336 pr_out_fmsg_end_object(dl
);
7338 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
7340 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
7342 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
7344 pr_out_fmsg_end_array(dl
);
7352 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
7354 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7355 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7356 struct fmsg_cb_data
*fmsg_data
= data
;
7357 struct dl
*dl
= fmsg_data
->dl
;
7358 struct nlattr
*nla_object
;
7362 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7363 if (!tb
[DEVLINK_ATTR_FMSG
])
7364 return MNL_CB_ERROR
;
7366 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
7367 attr_type
= mnl_attr_get_type(nla_object
);
7368 switch (attr_type
) {
7369 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
7370 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
7371 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
7372 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
7373 if (err
!= MNL_CB_OK
)
7376 case DEVLINK_ATTR_FMSG_NEST_END
:
7377 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
7378 if (err
!= MNL_CB_OK
)
7381 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
7382 free(fmsg_data
->name
);
7383 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
7384 if (!fmsg_data
->name
)
7387 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
7388 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
7390 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
7391 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
7392 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
7394 if (err
!= MNL_CB_OK
)
7404 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
7406 /* FMSG is dynamic: opening of an object or array causes a
7407 * newline. JSON starts with an { or [, but plain text should
7408 * not start with a new line. Ensure this by setting
7409 * g_new_line_count to 1: avoiding newline before the first
7412 g_new_line_count
= 1;
7415 INIT_LIST_HEAD(&data
->entry_list
);
7418 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
7420 struct fmsg_cb_data data
;
7421 struct nlmsghdr
*nlh
;
7424 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
7426 err
= dl_argv_parse_put(nlh
, dl
,
7427 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7428 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7432 cmd_fmsg_init(dl
, &data
);
7433 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
7438 static int cmd_health_dump_show(struct dl
*dl
)
7440 return cmd_health_object_common(dl
,
7441 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
7445 static int cmd_health_diagnose(struct dl
*dl
)
7447 return cmd_health_object_common(dl
,
7448 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
7452 static int cmd_health_test(struct dl
*dl
)
7454 return cmd_health_object_common(dl
,
7455 DEVLINK_CMD_HEALTH_REPORTER_TEST
,
7459 static int cmd_health_recover(struct dl
*dl
)
7461 struct nlmsghdr
*nlh
;
7464 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
7465 NLM_F_REQUEST
| NLM_F_ACK
);
7467 err
= dl_argv_parse_put(nlh
, dl
,
7468 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7469 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7473 dl_opts_put(nlh
, dl
);
7474 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7477 enum devlink_health_reporter_state
{
7478 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
7479 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
7482 static const char *health_state_name(uint8_t state
)
7485 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
7486 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
7487 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
7488 return HEALTH_REPORTER_STATE_ERROR_STR
;
7490 return "<unknown state>";
7494 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
7496 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7497 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7498 uint64_t time_ms
= mnl_attr_get_u64(attr
);
7499 struct sysinfo s_info
;
7505 info
= localtime(&now
);
7506 err
= sysinfo(&s_info
);
7509 /* Subtract uptime in sec from now yields the time of system
7510 * uptime. To this, add time_ms which is the amount of
7511 * milliseconds elapsed between uptime and the dump taken.
7513 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
7514 info
= localtime(&sec
);
7516 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
7517 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
7518 check_indent_newline(dl
);
7519 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7520 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7523 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
7525 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7526 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7531 ts
= mnl_attr_get_u64(attr
);
7532 tv_sec
= ts
/ 1000000000;
7533 tm
= localtime(&tv_sec
);
7535 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
7536 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
7538 check_indent_newline(dl
);
7539 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7540 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7543 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
,
7544 bool print_device
, bool print_port
)
7546 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7547 enum devlink_health_reporter_state state
;
7550 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
7552 if (err
!= MNL_CB_OK
)
7555 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
7556 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
7557 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
7558 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
7561 if (!print_device
&& !print_port
)
7564 if (!print_device
&& !tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7566 else if (tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7567 pr_out_port_handle_start_arr(dl
, tb_health
, false);
7570 if (!print_port
&& tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7572 else if (!tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7573 pr_out_handle_start_arr(dl
, tb_health
);
7576 check_indent_newline(dl
);
7577 print_string(PRINT_ANY
, "reporter", "reporter %s",
7578 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
7579 if (!dl
->json_output
) {
7581 __pr_out_indent_inc();
7583 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
7584 check_indent_newline(dl
);
7585 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
7586 pr_out_u64(dl
, "error",
7587 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
7588 pr_out_u64(dl
, "recover",
7589 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
7590 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
7591 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
7592 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
7593 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
7594 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
7595 pr_out_u64(dl
, "grace_period",
7596 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
7597 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
7598 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
7599 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
7600 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
7601 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
7602 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
7604 __pr_out_indent_dec();
7605 pr_out_handle_end(dl
);
7614 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7616 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7617 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7618 struct health_ctx
*ctx
= data
;
7619 struct dl
*dl
= ctx
->dl
;
7621 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7622 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7623 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
7624 return MNL_CB_ERROR
;
7626 pr_out_health(dl
, tb
, ctx
->show_device
, ctx
->show_port
);
7631 static int __cmd_health_show(struct dl
*dl
, bool show_device
, bool show_port
)
7633 struct nlmsghdr
*nlh
;
7634 struct health_ctx ctx
= { dl
, show_device
, show_port
};
7635 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7638 if (dl_argc(dl
) == 0)
7639 flags
|= NLM_F_DUMP
;
7640 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7643 if (dl_argc(dl
) > 0) {
7644 ctx
.show_port
= true;
7645 err
= dl_argv_parse_put(nlh
, dl
,
7646 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7647 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7651 pr_out_section_start(dl
, "health");
7653 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, &ctx
);
7654 pr_out_section_end(dl
);
7658 static void cmd_health_help(void)
7660 pr_err("Usage: devlink health show [ { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME ]\n");
7661 pr_err(" devlink health recover { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7662 pr_err(" devlink health diagnose { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7663 pr_err(" devlink health test { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7664 pr_err(" devlink health dump show { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7665 pr_err(" devlink health dump clear { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7666 pr_err(" devlink health set { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7667 pr_err(" [ grace_period MSEC ]\n");
7668 pr_err(" [ auto_recover { true | false } ]\n");
7669 pr_err(" [ auto_dump { true | false } ]\n");
7672 static int cmd_health(struct dl
*dl
)
7674 if (dl_argv_match(dl
, "help")) {
7677 } else if (dl_argv_match(dl
, "show") ||
7678 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7680 return __cmd_health_show(dl
, true, true);
7681 } else if (dl_argv_match(dl
, "recover")) {
7683 return cmd_health_recover(dl
);
7684 } else if (dl_argv_match(dl
, "diagnose")) {
7686 return cmd_health_diagnose(dl
);
7687 } else if (dl_argv_match(dl
, "test")) {
7689 return cmd_health_test(dl
);
7690 } else if (dl_argv_match(dl
, "dump")) {
7692 if (dl_argv_match(dl
, "show")) {
7694 return cmd_health_dump_show(dl
);
7695 } else if (dl_argv_match(dl
, "clear")) {
7697 return cmd_health_dump_clear(dl
);
7699 } else if (dl_argv_match(dl
, "set")) {
7701 return cmd_health_set_params(dl
);
7703 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7707 static const char *trap_type_name(uint8_t type
)
7710 case DEVLINK_TRAP_TYPE_DROP
:
7712 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7714 case DEVLINK_TRAP_TYPE_CONTROL
:
7717 return "<unknown type>";
7721 static const char *trap_action_name(uint8_t action
)
7724 case DEVLINK_TRAP_ACTION_DROP
:
7726 case DEVLINK_TRAP_ACTION_TRAP
:
7728 case DEVLINK_TRAP_ACTION_MIRROR
:
7731 return "<unknown action>";
7735 static const char *trap_metadata_name(const struct nlattr
*attr
)
7737 switch (attr
->nla_type
) {
7738 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7739 return "input_port";
7740 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7741 return "flow_action_cookie";
7743 return "<unknown metadata type>";
7746 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7748 struct nlattr
*attr_metadata
;
7750 pr_out_array_start(dl
, "metadata");
7751 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7752 check_indent_newline(dl
);
7753 print_string(PRINT_ANY
, NULL
, "%s",
7754 trap_metadata_name(attr_metadata
));
7756 pr_out_array_end(dl
);
7759 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7761 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7762 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7765 pr_out_handle_start_arr(dl
, tb
);
7767 __pr_out_handle_start(dl
, tb
, true, false);
7769 check_indent_newline(dl
);
7770 print_string(PRINT_ANY
, "name", "name %s",
7771 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7772 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7773 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7774 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7775 print_string(PRINT_ANY
, "group", " group %s",
7776 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7778 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7779 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7780 pr_out_handle_end(dl
);
7783 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7785 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7786 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7787 struct dl
*dl
= data
;
7789 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7790 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7791 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7792 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7793 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7794 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7795 return MNL_CB_ERROR
;
7797 pr_out_trap(dl
, tb
, true);
7802 static void cmd_trap_help(void)
7804 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7805 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7806 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7807 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7808 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7809 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7810 pr_err(" devlink trap policer show DEV policer POLICER\n");
7813 static int cmd_trap_show(struct dl
*dl
)
7815 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7816 struct nlmsghdr
*nlh
;
7819 if (dl_argc(dl
) == 0)
7820 flags
|= NLM_F_DUMP
;
7822 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7824 if (dl_argc(dl
) > 0) {
7825 err
= dl_argv_parse_put(nlh
, dl
,
7826 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7831 pr_out_section_start(dl
, "trap");
7832 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7833 pr_out_section_end(dl
);
7838 static int cmd_trap_set(struct dl
*dl
)
7840 struct nlmsghdr
*nlh
;
7843 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7844 NLM_F_REQUEST
| NLM_F_ACK
);
7846 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7847 DL_OPT_TRAP_ACTION
);
7851 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7854 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7857 pr_out_handle_start_arr(dl
, tb
);
7859 __pr_out_handle_start(dl
, tb
, true, false);
7861 check_indent_newline(dl
);
7862 print_string(PRINT_ANY
, "name", "name %s",
7863 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7864 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7865 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7866 print_uint(PRINT_ANY
, "policer", " policer %u",
7867 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7868 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7869 pr_out_handle_end(dl
);
7872 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7874 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7875 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7876 struct dl
*dl
= data
;
7878 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7879 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7880 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7881 return MNL_CB_ERROR
;
7883 pr_out_trap_group(dl
, tb
, true);
7888 static int cmd_trap_group_show(struct dl
*dl
)
7890 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7891 struct nlmsghdr
*nlh
;
7894 if (dl_argc(dl
) == 0)
7895 flags
|= NLM_F_DUMP
;
7897 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7899 if (dl_argc(dl
) > 0) {
7900 err
= dl_argv_parse_put(nlh
, dl
,
7901 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7907 pr_out_section_start(dl
, "trap_group");
7908 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7909 pr_out_section_end(dl
);
7914 static int cmd_trap_group_set(struct dl
*dl
)
7916 struct nlmsghdr
*nlh
;
7919 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7920 NLM_F_REQUEST
| NLM_F_ACK
);
7922 err
= dl_argv_parse_put(nlh
, dl
,
7923 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7924 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7928 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7931 static int cmd_trap_group(struct dl
*dl
)
7933 if (dl_argv_match(dl
, "help")) {
7936 } else if (dl_argv_match(dl
, "show") ||
7937 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7939 return cmd_trap_group_show(dl
);
7940 } else if (dl_argv_match(dl
, "set")) {
7942 return cmd_trap_group_set(dl
);
7944 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7948 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7951 pr_out_handle_start_arr(dl
, tb
);
7953 __pr_out_handle_start(dl
, tb
, true, false);
7955 check_indent_newline(dl
);
7956 print_uint(PRINT_ANY
, "policer", "policer %u",
7957 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7958 print_u64(PRINT_ANY
, "rate", " rate %llu",
7959 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7960 print_u64(PRINT_ANY
, "burst", " burst %llu",
7961 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7962 if (tb
[DEVLINK_ATTR_STATS
])
7963 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7964 pr_out_handle_end(dl
);
7967 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7969 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7970 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7971 struct dl
*dl
= data
;
7973 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7974 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7975 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7976 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7977 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7978 return MNL_CB_ERROR
;
7980 pr_out_trap_policer(dl
, tb
, true);
7985 static int cmd_trap_policer_show(struct dl
*dl
)
7987 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7988 struct nlmsghdr
*nlh
;
7991 if (dl_argc(dl
) == 0)
7992 flags
|= NLM_F_DUMP
;
7994 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7996 if (dl_argc(dl
) > 0) {
7997 err
= dl_argv_parse_put(nlh
, dl
,
7998 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
8004 pr_out_section_start(dl
, "trap_policer");
8005 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
8006 pr_out_section_end(dl
);
8011 static int cmd_trap_policer_set(struct dl
*dl
)
8013 struct nlmsghdr
*nlh
;
8016 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
8017 NLM_F_REQUEST
| NLM_F_ACK
);
8019 err
= dl_argv_parse_put(nlh
, dl
,
8020 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
8021 DL_OPT_TRAP_POLICER_RATE
|
8022 DL_OPT_TRAP_POLICER_BURST
);
8026 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
8029 static int cmd_trap_policer(struct dl
*dl
)
8031 if (dl_argv_match(dl
, "help")) {
8034 } else if (dl_argv_match(dl
, "show") ||
8035 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
8037 return cmd_trap_policer_show(dl
);
8038 } else if (dl_argv_match(dl
, "set")) {
8040 return cmd_trap_policer_set(dl
);
8042 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
8046 static int cmd_trap(struct dl
*dl
)
8048 if (dl_argv_match(dl
, "help")) {
8051 } else if (dl_argv_match(dl
, "show") ||
8052 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
8054 return cmd_trap_show(dl
);
8055 } else if (dl_argv_match(dl
, "set")) {
8057 return cmd_trap_set(dl
);
8058 } else if (dl_argv_match(dl
, "group")) {
8060 return cmd_trap_group(dl
);
8061 } else if (dl_argv_match(dl
, "policer")) {
8063 return cmd_trap_policer(dl
);
8065 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
8069 static void help(void)
8071 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
8072 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
8073 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
8074 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
8077 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
8082 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
8085 } else if (dl_argv_match(dl
, "dev")) {
8088 } else if (dl_argv_match(dl
, "port")) {
8090 return cmd_port(dl
);
8091 } else if (dl_argv_match(dl
, "sb")) {
8094 } else if (dl_argv_match(dl
, "monitor")) {
8097 } else if (dl_argv_match(dl
, "dpipe")) {
8099 return cmd_dpipe(dl
);
8100 } else if (dl_argv_match(dl
, "resource")) {
8102 return cmd_resource(dl
);
8103 } else if (dl_argv_match(dl
, "region")) {
8105 return cmd_region(dl
);
8106 } else if (dl_argv_match(dl
, "health")) {
8108 return cmd_health(dl
);
8109 } else if (dl_argv_match(dl
, "trap")) {
8111 return cmd_trap(dl
);
8113 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
8117 static int dl_init(struct dl
*dl
)
8121 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
8123 pr_err("Failed to connect to devlink Netlink\n");
8127 err
= ifname_map_init(dl
);
8129 pr_err("Failed to create index map\n");
8130 goto err_ifname_map_create
;
8132 new_json_obj_plain(dl
->json_output
);
8135 err_ifname_map_create
:
8136 mnlg_socket_close(dl
->nlg
);
8140 static void dl_fini(struct dl
*dl
)
8142 delete_json_obj_plain();
8143 ifname_map_fini(dl
);
8144 mnlg_socket_close(dl
->nlg
);
8147 static struct dl
*dl_alloc(void)
8151 dl
= calloc(1, sizeof(*dl
));
8157 static void dl_free(struct dl
*dl
)
8162 static int dl_batch_cmd(int argc
, char *argv
[], void *data
)
8164 struct dl
*dl
= data
;
8166 return dl_cmd(dl
, argc
, argv
);
8169 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
8171 return do_batch(name
, force
, dl_batch_cmd
, dl
);
8174 int main(int argc
, char **argv
)
8176 static const struct option long_options
[] = {
8177 { "Version", no_argument
, NULL
, 'V' },
8178 { "force", no_argument
, NULL
, 'f' },
8179 { "batch", required_argument
, NULL
, 'b' },
8180 { "no-nice-names", no_argument
, NULL
, 'n' },
8181 { "json", no_argument
, NULL
, 'j' },
8182 { "pretty", no_argument
, NULL
, 'p' },
8183 { "verbose", no_argument
, NULL
, 'v' },
8184 { "statistics", no_argument
, NULL
, 's' },
8185 { "Netns", required_argument
, NULL
, 'N' },
8186 { NULL
, 0, NULL
, 0 }
8188 const char *batch_file
= NULL
;
8197 pr_err("Failed to allocate memory for devlink\n");
8198 return EXIT_FAILURE
;
8201 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
8202 long_options
, NULL
)) >= 0) {
8206 printf("devlink utility, iproute2-%s\n", version
);
8213 batch_file
= optarg
;
8216 dl
->no_nice_names
= true;
8219 dl
->json_output
= true;
8231 if (netns_switch(optarg
)) {
8237 pr_err("Unknown option.\n");
8254 err
= dl_batch(dl
, batch_file
, force
);
8256 err
= dl_cmd(dl
, argc
, argv
);