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 static struct str_num_map port_flavour_map
[] = {
1387 { .str
= "physical", .num
= DEVLINK_PORT_FLAVOUR_PHYSICAL
},
1388 { .str
= "cpu", .num
= DEVLINK_PORT_FLAVOUR_CPU
},
1389 { .str
= "dsa", .num
= DEVLINK_PORT_FLAVOUR_DSA
},
1390 { .str
= "pcipf", .num
= DEVLINK_PORT_FLAVOUR_PCI_PF
},
1391 { .str
= "pcivf", .num
= DEVLINK_PORT_FLAVOUR_PCI_VF
},
1392 { .str
= "virtual", .num
= DEVLINK_PORT_FLAVOUR_VIRTUAL
},
1396 struct dl_args_metadata
{
1398 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1401 static const struct dl_args_metadata dl_args_required
[] = {
1402 {DL_OPT_PORT_TYPE
, "Port type not set."},
1403 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1404 {DL_OPT_SB_POOL
, "Pool index option expected."},
1405 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1406 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1407 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1408 {DL_OPT_SB_TH
, "Threshold option expected."},
1409 {DL_OPT_SB_TC
, "TC index option expected."},
1410 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1411 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1412 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1413 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1414 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1415 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1416 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1417 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1418 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1419 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1420 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1421 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1422 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1423 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1424 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1425 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1426 {DL_OPT_PORT_FUNCTION_HW_ADDR
, "Port function's hardware address is expected."},
1429 static int dl_args_finding_required_validate(uint64_t o_required
,
1435 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1436 o_flag
= dl_args_required
[i
].o_flag
;
1437 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1438 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1442 if (o_required
& ~o_found
) {
1443 pr_err("BUG: unknown argument required but not found\n");
1449 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1450 uint64_t o_optional
)
1452 struct dl_opts
*opts
= &dl
->opts
;
1453 uint64_t o_all
= o_required
| o_optional
;
1454 uint64_t o_found
= 0;
1457 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1458 uint64_t handle_bit
;
1460 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1461 &opts
->port_index
, &handle_bit
);
1464 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1465 o_found
|= handle_bit
;
1466 } else if (o_required
& DL_OPT_HANDLE
) {
1467 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1470 o_found
|= DL_OPT_HANDLE
;
1471 } else if (o_required
& DL_OPT_HANDLEP
) {
1472 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1476 o_found
|= DL_OPT_HANDLEP
;
1477 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1478 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1480 &opts
->region_name
);
1483 o_found
|= DL_OPT_HANDLE_REGION
;
1486 while (dl_argc(dl
)) {
1487 if (dl_argv_match(dl
, "type") &&
1488 (o_all
& DL_OPT_PORT_TYPE
)) {
1489 const char *typestr
;
1492 err
= dl_argv_str(dl
, &typestr
);
1495 err
= port_type_get(typestr
, &opts
->port_type
);
1498 o_found
|= DL_OPT_PORT_TYPE
;
1499 } else if (dl_argv_match(dl
, "count") &&
1500 (o_all
& DL_OPT_PORT_COUNT
)) {
1502 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1505 o_found
|= DL_OPT_PORT_COUNT
;
1506 } else if (dl_argv_match(dl
, "sb") &&
1507 (o_all
& DL_OPT_SB
)) {
1509 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1512 o_found
|= DL_OPT_SB
;
1513 } else if (dl_argv_match(dl
, "pool") &&
1514 (o_all
& DL_OPT_SB_POOL
)) {
1516 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1519 o_found
|= DL_OPT_SB_POOL
;
1520 } else if (dl_argv_match(dl
, "size") &&
1521 (o_all
& DL_OPT_SB_SIZE
)) {
1523 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1526 o_found
|= DL_OPT_SB_SIZE
;
1527 } else if (dl_argv_match(dl
, "type") &&
1528 (o_all
& DL_OPT_SB_TYPE
)) {
1529 const char *typestr
;
1532 err
= dl_argv_str(dl
, &typestr
);
1535 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1538 o_found
|= DL_OPT_SB_TYPE
;
1539 } else if (dl_argv_match(dl
, "thtype") &&
1540 (o_all
& DL_OPT_SB_THTYPE
)) {
1541 const char *typestr
;
1544 err
= dl_argv_str(dl
, &typestr
);
1547 err
= threshold_type_get(typestr
,
1548 &opts
->sb_pool_thtype
);
1551 o_found
|= DL_OPT_SB_THTYPE
;
1552 } else if (dl_argv_match(dl
, "th") &&
1553 (o_all
& DL_OPT_SB_TH
)) {
1555 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1558 o_found
|= DL_OPT_SB_TH
;
1559 } else if (dl_argv_match(dl
, "tc") &&
1560 (o_all
& DL_OPT_SB_TC
)) {
1562 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1565 o_found
|= DL_OPT_SB_TC
;
1566 } else if (dl_argv_match(dl
, "mode") &&
1567 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1568 const char *typestr
;
1571 err
= dl_argv_str(dl
, &typestr
);
1574 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1577 o_found
|= DL_OPT_ESWITCH_MODE
;
1578 } else if (dl_argv_match(dl
, "inline-mode") &&
1579 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1580 const char *typestr
;
1583 err
= dl_argv_str(dl
, &typestr
);
1586 err
= eswitch_inline_mode_get(
1587 typestr
, &opts
->eswitch_inline_mode
);
1590 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1591 } else if (dl_argv_match(dl
, "name") &&
1592 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1594 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1597 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1598 } else if ((dl_argv_match(dl
, "counters") ||
1599 dl_argv_match(dl
, "counters_enabled")) &&
1600 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1602 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1605 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1606 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1607 dl_argv_match(dl
, "encap-mode")) &&
1608 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1609 const char *typestr
;
1612 err
= dl_argv_str(dl
, &typestr
);
1615 err
= eswitch_encap_mode_get(typestr
,
1616 &opts
->eswitch_encap_mode
);
1619 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1620 } else if (dl_argv_match(dl
, "path") &&
1621 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1623 err
= dl_argv_str(dl
, &opts
->resource_path
);
1626 o_found
|= DL_OPT_RESOURCE_PATH
;
1627 } else if (dl_argv_match(dl
, "size") &&
1628 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1630 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1633 o_found
|= DL_OPT_RESOURCE_SIZE
;
1634 } else if (dl_argv_match(dl
, "name") &&
1635 (o_all
& DL_OPT_PARAM_NAME
)) {
1637 err
= dl_argv_str(dl
, &opts
->param_name
);
1640 o_found
|= DL_OPT_PARAM_NAME
;
1641 } else if (dl_argv_match(dl
, "value") &&
1642 (o_all
& DL_OPT_PARAM_VALUE
)) {
1644 err
= dl_argv_str(dl
, &opts
->param_value
);
1647 o_found
|= DL_OPT_PARAM_VALUE
;
1648 } else if (dl_argv_match(dl
, "cmode") &&
1649 (o_all
& DL_OPT_PARAM_CMODE
)) {
1650 const char *cmodestr
;
1653 err
= dl_argv_str(dl
, &cmodestr
);
1656 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1659 o_found
|= DL_OPT_PARAM_CMODE
;
1660 } else if (dl_argv_match(dl
, "snapshot") &&
1661 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1663 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1666 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1667 } else if (dl_argv_match(dl
, "address") &&
1668 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1670 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1673 o_found
|= DL_OPT_REGION_ADDRESS
;
1674 } else if (dl_argv_match(dl
, "length") &&
1675 (o_all
& DL_OPT_REGION_LENGTH
)) {
1677 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1680 o_found
|= DL_OPT_REGION_LENGTH
;
1681 } else if (dl_argv_match(dl
, "file") &&
1682 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1684 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1687 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1688 } else if (dl_argv_match(dl
, "component") &&
1689 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1691 err
= dl_argv_str(dl
, &opts
->flash_component
);
1694 o_found
|= DL_OPT_FLASH_COMPONENT
;
1696 } else if (dl_argv_match(dl
, "overwrite") &&
1697 (o_all
& DL_OPT_FLASH_OVERWRITE
)) {
1698 const char *sectionstr
;
1701 err
= dl_argv_str(dl
, §ionstr
);
1704 err
= flash_overwrite_section_get(sectionstr
,
1705 &opts
->overwrite_mask
);
1708 o_found
|= DL_OPT_FLASH_OVERWRITE
;
1710 } else if (dl_argv_match(dl
, "reporter") &&
1711 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1713 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1716 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1717 } else if (dl_argv_match(dl
, "grace_period") &&
1718 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1720 err
= dl_argv_uint64_t(dl
,
1721 &opts
->reporter_graceful_period
);
1724 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1725 } else if (dl_argv_match(dl
, "auto_recover") &&
1726 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1728 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1731 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1732 } else if (dl_argv_match(dl
, "auto_dump") &&
1733 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)) {
1735 err
= dl_argv_bool(dl
, &opts
->reporter_auto_dump
);
1738 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_DUMP
;
1739 } else if (dl_argv_match(dl
, "trap") &&
1740 (o_all
& DL_OPT_TRAP_NAME
)) {
1742 err
= dl_argv_str(dl
, &opts
->trap_name
);
1745 o_found
|= DL_OPT_TRAP_NAME
;
1746 } else if (dl_argv_match(dl
, "group") &&
1747 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1749 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1752 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1753 } else if (dl_argv_match(dl
, "action") &&
1754 (o_all
& DL_OPT_TRAP_ACTION
)) {
1755 const char *actionstr
;
1758 err
= dl_argv_str(dl
, &actionstr
);
1761 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1764 o_found
|= DL_OPT_TRAP_ACTION
;
1765 } else if (dl_argv_match(dl
, "netns") &&
1766 (o_all
& DL_OPT_NETNS
)) {
1767 const char *netns_str
;
1770 err
= dl_argv_str(dl
, &netns_str
);
1773 opts
->netns
= netns_get_fd(netns_str
);
1774 if ((int)opts
->netns
< 0) {
1776 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1779 opts
->netns_is_pid
= true;
1781 o_found
|= DL_OPT_NETNS
;
1782 } else if (dl_argv_match(dl
, "action") &&
1783 (o_all
& DL_OPT_RELOAD_ACTION
)) {
1784 const char *actionstr
;
1787 err
= dl_argv_str(dl
, &actionstr
);
1790 err
= reload_action_get(dl
, actionstr
, &opts
->reload_action
);
1793 o_found
|= DL_OPT_RELOAD_ACTION
;
1794 } else if (dl_argv_match(dl
, "limit") &&
1795 (o_all
& DL_OPT_RELOAD_LIMIT
)) {
1796 const char *limitstr
;
1799 err
= dl_argv_str(dl
, &limitstr
);
1802 err
= reload_limit_get(dl
, limitstr
, &opts
->reload_limit
);
1805 o_found
|= DL_OPT_RELOAD_LIMIT
;
1806 } else if (dl_argv_match(dl
, "policer") &&
1807 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1809 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1812 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1813 } else if (dl_argv_match(dl
, "nopolicer") &&
1814 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1816 opts
->trap_policer_id
= 0;
1817 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1818 } else if (dl_argv_match(dl
, "rate") &&
1819 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1821 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1824 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1825 } else if (dl_argv_match(dl
, "burst") &&
1826 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1828 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1831 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1832 } else if (dl_argv_match(dl
, "hw_addr") &&
1833 (o_all
& DL_OPT_PORT_FUNCTION_HW_ADDR
)) {
1834 const char *addrstr
;
1837 err
= dl_argv_str(dl
, &addrstr
);
1840 err
= hw_addr_parse(addrstr
, opts
->port_function_hw_addr
,
1841 &opts
->port_function_hw_addr_len
);
1844 o_found
|= DL_OPT_PORT_FUNCTION_HW_ADDR
;
1847 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1852 opts
->present
= o_found
;
1854 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1856 opts
->present
|= DL_OPT_SB
;
1859 return dl_args_finding_required_validate(o_required
, o_found
);
1863 dl_function_attr_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1865 struct nlattr
*nest
;
1867 nest
= mnl_attr_nest_start(nlh
, DEVLINK_ATTR_PORT_FUNCTION
);
1868 mnl_attr_put(nlh
, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
,
1869 opts
->port_function_hw_addr_len
,
1870 opts
->port_function_hw_addr
);
1871 mnl_attr_nest_end(nlh
, nest
);
1875 dl_flash_update_overwrite_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1877 struct nla_bitfield32 overwrite_mask
;
1879 overwrite_mask
.selector
= DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS
;
1880 overwrite_mask
.value
= opts
->overwrite_mask
;
1882 mnl_attr_put(nlh
, DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK
,
1883 sizeof(overwrite_mask
), &overwrite_mask
);
1887 dl_reload_limits_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1889 struct nla_bitfield32 limits
;
1891 limits
.selector
= DEVLINK_RELOAD_LIMITS_VALID_MASK
;
1892 limits
.value
= BIT(opts
->reload_limit
);
1893 mnl_attr_put(nlh
, DEVLINK_ATTR_RELOAD_LIMITS
, sizeof(limits
), &limits
);
1896 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1898 struct dl_opts
*opts
= &dl
->opts
;
1900 if (opts
->present
& DL_OPT_HANDLE
) {
1901 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1902 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1903 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1904 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1905 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1906 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1908 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1909 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1910 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1911 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1914 if (opts
->present
& DL_OPT_PORT_TYPE
)
1915 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1917 if (opts
->present
& DL_OPT_PORT_COUNT
)
1918 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1920 if (opts
->present
& DL_OPT_SB
)
1921 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1923 if (opts
->present
& DL_OPT_SB_POOL
)
1924 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1925 opts
->sb_pool_index
);
1926 if (opts
->present
& DL_OPT_SB_SIZE
)
1927 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1928 opts
->sb_pool_size
);
1929 if (opts
->present
& DL_OPT_SB_TYPE
)
1930 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1931 opts
->sb_pool_type
);
1932 if (opts
->present
& DL_OPT_SB_THTYPE
)
1933 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1934 opts
->sb_pool_thtype
);
1935 if (opts
->present
& DL_OPT_SB_TH
)
1936 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1937 opts
->sb_threshold
);
1938 if (opts
->present
& DL_OPT_SB_TC
)
1939 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1941 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1942 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1943 opts
->eswitch_mode
);
1944 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1945 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1946 opts
->eswitch_inline_mode
);
1947 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1948 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1949 opts
->dpipe_table_name
);
1950 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1951 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1952 opts
->dpipe_counters_enabled
);
1953 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1954 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1955 opts
->eswitch_encap_mode
);
1956 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1957 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1959 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1960 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1961 opts
->resource_size
);
1962 if (opts
->present
& DL_OPT_PARAM_NAME
)
1963 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1965 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1966 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1968 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1969 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1970 opts
->region_snapshot_id
);
1971 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1972 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1973 opts
->region_address
);
1974 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1975 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1976 opts
->region_length
);
1977 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1978 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1979 opts
->flash_file_name
);
1980 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1981 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1982 opts
->flash_component
);
1983 if (opts
->present
& DL_OPT_FLASH_OVERWRITE
)
1984 dl_flash_update_overwrite_put(nlh
, opts
);
1985 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1986 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1987 opts
->reporter_name
);
1988 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1989 mnl_attr_put_u64(nlh
,
1990 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1991 opts
->reporter_graceful_period
);
1992 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1993 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1994 opts
->reporter_auto_recover
);
1995 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)
1996 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
1997 opts
->reporter_auto_dump
);
1998 if (opts
->present
& DL_OPT_TRAP_NAME
)
1999 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
2001 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
2002 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
2003 opts
->trap_group_name
);
2004 if (opts
->present
& DL_OPT_TRAP_ACTION
)
2005 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
2007 if (opts
->present
& DL_OPT_NETNS
)
2008 mnl_attr_put_u32(nlh
,
2009 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
2010 DEVLINK_ATTR_NETNS_FD
,
2012 if (opts
->present
& DL_OPT_RELOAD_ACTION
)
2013 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_RELOAD_ACTION
,
2014 opts
->reload_action
);
2015 if (opts
->present
& DL_OPT_RELOAD_LIMIT
)
2016 dl_reload_limits_put(nlh
, opts
);
2017 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
2018 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
2019 opts
->trap_policer_id
);
2020 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
2021 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
2022 opts
->trap_policer_rate
);
2023 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
2024 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
2025 opts
->trap_policer_burst
);
2026 if (opts
->present
& DL_OPT_PORT_FUNCTION_HW_ADDR
)
2027 dl_function_attr_put(nlh
, opts
);
2030 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
2031 uint64_t o_required
, uint64_t o_optional
)
2035 err
= dl_argv_parse(dl
, o_required
, o_optional
);
2038 dl_opts_put(nlh
, dl
);
2042 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
2044 struct dl_opts
*opts
= &dl
->opts
;
2045 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
2046 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
2047 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
2048 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
2050 if (opts
->present
& DL_OPT_HANDLE
&&
2051 attr_bus_name
&& attr_dev_name
) {
2052 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
2053 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
2055 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
2056 strcmp(dev_name
, opts
->dev_name
) != 0)
2059 if (opts
->present
& DL_OPT_HANDLEP
&&
2060 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
2061 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
2062 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
2063 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
2065 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
2066 strcmp(dev_name
, opts
->dev_name
) != 0 ||
2067 port_index
!= opts
->port_index
)
2070 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
2071 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
2073 if (sb_index
!= opts
->sb_index
)
2079 static void cmd_dev_help(void)
2081 pr_err("Usage: devlink dev show [ DEV ]\n");
2082 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
2083 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
2084 pr_err(" [ encap-mode { none | basic } ]\n");
2085 pr_err(" devlink dev eswitch show DEV\n");
2086 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
2087 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
2088 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2089 pr_err(" [ action { driver_reinit | fw_activate } ] [ limit no_reset ]\n");
2090 pr_err(" devlink dev info [ DEV ]\n");
2091 pr_err(" devlink dev flash DEV file PATH [ component NAME ] [ overwrite SECTION ]\n");
2094 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
2095 const char *dev_name
)
2097 if (!dl
->arr_last
.present
)
2099 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
2100 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
2103 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
2104 const char *dev_name
)
2106 dl
->arr_last
.present
= true;
2107 free(dl
->arr_last
.dev_name
);
2108 free(dl
->arr_last
.bus_name
);
2109 dl
->arr_last
.bus_name
= strdup(bus_name
);
2110 dl
->arr_last
.dev_name
= strdup(dev_name
);
2113 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
2114 const char *dev_name
)
2116 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
2119 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
2120 const char *dev_name
)
2122 return dl
->arr_last
.present
&&
2123 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
2126 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
2127 bool content
, bool array
)
2129 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2130 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2133 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
2135 if (dl
->json_output
) {
2137 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2138 close_json_array(PRINT_JSON
, NULL
);
2139 if (should_arr_last_handle_start(dl
, bus_name
,
2141 open_json_array(PRINT_JSON
, buf
);
2142 open_json_object(NULL
);
2143 arr_last_handle_set(dl
, bus_name
, dev_name
);
2145 open_json_object(NULL
);
2148 open_json_object(buf
);
2152 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2153 __pr_out_indent_dec();
2154 if (should_arr_last_handle_start(dl
, bus_name
,
2156 pr_out("%s%s", buf
, content
? ":" : "");
2158 __pr_out_indent_inc();
2159 arr_last_handle_set(dl
, bus_name
, dev_name
);
2162 pr_out("%s%s", buf
, content
? ":" : "");
2167 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
2169 __pr_out_handle_start(dl
, tb
, true, true);
2172 static void pr_out_handle_end(struct dl
*dl
)
2174 if (dl
->json_output
)
2175 close_json_object();
2180 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
2182 __pr_out_handle_start(dl
, tb
, false, false);
2183 pr_out_handle_end(dl
);
2186 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
2187 const char *dev_name
, uint32_t port_index
)
2189 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
2190 dl
->arr_last
.port_index
== port_index
;
2193 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
2194 const char *dev_name
, uint32_t port_index
)
2196 arr_last_handle_set(dl
, bus_name
, dev_name
);
2197 dl
->arr_last
.port_index
= port_index
;
2200 static bool should_arr_last_port_handle_start(struct dl
*dl
,
2201 const char *bus_name
,
2202 const char *dev_name
,
2203 uint32_t port_index
)
2205 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2208 static bool should_arr_last_port_handle_end(struct dl
*dl
,
2209 const char *bus_name
,
2210 const char *dev_name
,
2211 uint32_t port_index
)
2213 return dl
->arr_last
.present
&&
2214 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2217 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
2218 const char *dev_name
,
2219 uint32_t port_index
, bool try_nice
,
2222 static char buf
[64];
2223 char *ifname
= NULL
;
2225 if (dl
->no_nice_names
|| !try_nice
||
2226 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
2227 port_index
, &ifname
) != 0)
2228 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
2230 sprintf(buf
, "%s", ifname
);
2232 if (dl
->json_output
) {
2234 if (should_arr_last_port_handle_end(dl
, bus_name
,
2237 close_json_array(PRINT_JSON
, NULL
);
2238 if (should_arr_last_port_handle_start(dl
, bus_name
,
2241 open_json_array(PRINT_JSON
, buf
);
2242 open_json_object(NULL
);
2243 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
2246 open_json_object(NULL
);
2249 open_json_object(buf
);
2253 if (should_arr_last_port_handle_end(dl
, bus_name
, dev_name
, port_index
))
2254 __pr_out_indent_dec();
2255 if (should_arr_last_port_handle_start(dl
, bus_name
,
2256 dev_name
, port_index
)) {
2259 __pr_out_indent_inc();
2260 arr_last_port_handle_set(dl
, bus_name
, dev_name
, port_index
);
2268 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2270 const char *bus_name
;
2271 const char *dev_name
;
2272 uint32_t port_index
;
2274 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2275 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2276 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2277 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
2280 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2282 const char *bus_name
;
2283 const char *dev_name
;
2284 uint32_t port_index
;
2286 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2287 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2288 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2289 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
2292 static void pr_out_port_handle_end(struct dl
*dl
)
2294 if (dl
->json_output
)
2295 close_json_object();
2300 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
2302 if (dl
->json_output
) {
2303 print_uint(PRINT_JSON
, "address", NULL
, addr
);
2304 open_json_array(PRINT_JSON
, "data");
2308 static void pr_out_region_chunk_end(struct dl
*dl
)
2310 if (dl
->json_output
)
2311 close_json_array(PRINT_JSON
, NULL
);
2314 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2317 static uint64_t align_val
;
2320 pr_out_region_chunk_start(dl
, addr
);
2322 if (!dl
->json_output
)
2323 if (!(align_val
% 16))
2324 pr_out("%s%016"PRIx64
" ",
2325 align_val
? "\n" : "",
2330 if (dl
->json_output
)
2331 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2333 pr_out("%02x ", data
[i
]);
2338 pr_out_region_chunk_end(dl
);
2341 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2343 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2349 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2350 if (err
!= MNL_CB_OK
)
2353 pr_out_object_start(dl
, "stats");
2354 pr_out_object_start(dl
, "rx");
2355 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2356 pr_out_u64(dl
, "bytes",
2357 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2358 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2359 pr_out_u64(dl
, "packets",
2360 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2361 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2362 pr_out_u64(dl
, "dropped",
2363 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2364 pr_out_object_end(dl
);
2365 pr_out_object_end(dl
);
2368 static const char *param_cmode_name(uint8_t cmode
)
2371 case DEVLINK_PARAM_CMODE_RUNTIME
:
2372 return PARAM_CMODE_RUNTIME_STR
;
2373 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2374 return PARAM_CMODE_DRIVERINIT_STR
;
2375 case DEVLINK_PARAM_CMODE_PERMANENT
:
2376 return PARAM_CMODE_PERMANENT_STR
;
2377 default: return "<unknown type>";
2381 static const char *reload_action_name(uint8_t reload_action
)
2383 switch (reload_action
) {
2384 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT
:
2385 return "driver_reinit";
2386 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE
:
2387 return "fw_activate";
2389 return "<unknown reload action>";
2393 static const char *reload_limit_name(uint8_t reload_limit
)
2395 switch (reload_limit
) {
2396 case DEVLINK_RELOAD_LIMIT_UNSPEC
:
2397 return "unspecified";
2398 case DEVLINK_RELOAD_LIMIT_NO_RESET
:
2401 return "<unknown reload action>";
2405 static const char *eswitch_mode_name(uint32_t mode
)
2408 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2409 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2410 default: return "<unknown mode>";
2414 static const char *eswitch_inline_mode_name(uint32_t mode
)
2417 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2418 return ESWITCH_INLINE_MODE_NONE
;
2419 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2420 return ESWITCH_INLINE_MODE_LINK
;
2421 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2422 return ESWITCH_INLINE_MODE_NETWORK
;
2423 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2424 return ESWITCH_INLINE_MODE_TRANSPORT
;
2426 return "<unknown mode>";
2430 static const char *eswitch_encap_mode_name(uint32_t mode
)
2433 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2434 return ESWITCH_ENCAP_MODE_NONE
;
2435 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2436 return ESWITCH_ENCAP_MODE_BASIC
;
2438 return "<unknown mode>";
2442 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2444 __pr_out_handle_start(dl
, tb
, true, false);
2446 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2447 check_indent_newline(dl
);
2448 print_string(PRINT_ANY
, "mode", "mode %s",
2449 eswitch_mode_name(mnl_attr_get_u16(
2450 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2452 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2453 check_indent_newline(dl
);
2454 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2455 eswitch_inline_mode_name(mnl_attr_get_u8(
2456 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2458 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2459 check_indent_newline(dl
);
2460 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2461 eswitch_encap_mode_name(mnl_attr_get_u8(
2462 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2465 pr_out_handle_end(dl
);
2468 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2470 struct dl
*dl
= data
;
2471 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2472 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2474 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2475 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2476 return MNL_CB_ERROR
;
2477 pr_out_eswitch(dl
, tb
);
2481 static int cmd_dev_eswitch_show(struct dl
*dl
)
2483 struct nlmsghdr
*nlh
;
2486 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2487 NLM_F_REQUEST
| NLM_F_ACK
);
2489 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2493 pr_out_section_start(dl
, "dev");
2494 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2495 pr_out_section_end(dl
);
2499 static int cmd_dev_eswitch_set(struct dl
*dl
)
2501 struct nlmsghdr
*nlh
;
2504 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2505 NLM_F_REQUEST
| NLM_F_ACK
);
2507 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2508 DL_OPT_ESWITCH_MODE
|
2509 DL_OPT_ESWITCH_INLINE_MODE
|
2510 DL_OPT_ESWITCH_ENCAP_MODE
);
2515 if (dl
->opts
.present
== 1) {
2516 pr_err("Need to set at least one option\n");
2520 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2523 static int cmd_dev_eswitch(struct dl
*dl
)
2525 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2528 } else if (dl_argv_match(dl
, "set")) {
2530 return cmd_dev_eswitch_set(dl
);
2531 } else if (dl_argv_match(dl
, "show")) {
2533 return cmd_dev_eswitch_show(dl
);
2535 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2539 struct param_val_conv
{
2545 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2546 uint32_t len
, const char *name
)
2550 for (i
= 0; i
< len
; i
++)
2551 if (!strcmp(param_val_conv
[i
].name
, name
))
2558 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2559 uint32_t len
, const char *name
, const char *vstr
,
2564 for (i
= 0; i
< len
; i
++)
2565 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2566 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2567 *vuint
= param_val_conv
[i
].vuint
;
2575 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2576 uint32_t len
, const char *name
, uint32_t vuint
,
2581 for (i
= 0; i
< len
; i
++)
2582 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2583 param_val_conv
[i
].vuint
== vuint
) {
2584 *vstr
= param_val_conv
[i
].vstr
;
2591 static const struct param_val_conv param_val_conv
[] = {
2593 .name
= "fw_load_policy",
2595 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2598 .name
= "fw_load_policy",
2600 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2603 .name
= "fw_load_policy",
2605 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK
,
2608 .name
= "reset_dev_on_drv_probe",
2610 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2613 .name
= "fw_load_policy",
2615 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2618 .name
= "reset_dev_on_drv_probe",
2620 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2623 .name
= "reset_dev_on_drv_probe",
2625 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2628 .name
= "reset_dev_on_drv_probe",
2630 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2634 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2636 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2637 int nla_type
, struct nlattr
*nl
)
2639 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2640 struct nlattr
*val_attr
;
2645 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2646 if (err
!= MNL_CB_OK
)
2649 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2650 (nla_type
!= MNL_TYPE_FLAG
&&
2651 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2654 check_indent_newline(dl
);
2655 print_string(PRINT_ANY
, "cmode", "cmode %s",
2656 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2658 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2660 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2666 err
= param_val_conv_str_get(param_val_conv
,
2669 mnl_attr_get_u8(val_attr
),
2673 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2675 print_uint(PRINT_ANY
, "value", " value %u",
2676 mnl_attr_get_u8(val_attr
));
2681 err
= param_val_conv_str_get(param_val_conv
,
2684 mnl_attr_get_u16(val_attr
),
2688 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2690 print_uint(PRINT_ANY
, "value", " value %u",
2691 mnl_attr_get_u16(val_attr
));
2696 err
= param_val_conv_str_get(param_val_conv
,
2699 mnl_attr_get_u32(val_attr
),
2703 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2705 print_uint(PRINT_ANY
, "value", " value %u",
2706 mnl_attr_get_u32(val_attr
));
2709 case MNL_TYPE_STRING
:
2710 print_string(PRINT_ANY
, "value", " value %s",
2711 mnl_attr_get_str(val_attr
));
2714 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2719 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2721 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2722 struct nlattr
*param_value_attr
;
2723 const char *nla_name
;
2727 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2728 if (err
!= MNL_CB_OK
)
2730 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2731 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2732 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2736 pr_out_handle_start_arr(dl
, tb
);
2738 __pr_out_handle_start(dl
, tb
, true, false);
2740 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2742 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2743 check_indent_newline(dl
);
2744 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2745 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2746 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2748 print_string(PRINT_ANY
, "type", "type %s", "generic");
2750 pr_out_array_start(dl
, "values");
2751 mnl_attr_for_each_nested(param_value_attr
,
2752 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2753 pr_out_entry_start(dl
);
2754 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2755 pr_out_entry_end(dl
);
2757 pr_out_array_end(dl
);
2758 pr_out_handle_end(dl
);
2761 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2763 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2764 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2765 struct dl
*dl
= data
;
2767 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2768 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2769 !tb
[DEVLINK_ATTR_PARAM
])
2770 return MNL_CB_ERROR
;
2771 pr_out_param(dl
, tb
, true);
2787 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2789 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2790 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2791 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2792 struct nlattr
*param_value_attr
;
2793 enum devlink_param_cmode cmode
;
2794 struct param_ctx
*ctx
= data
;
2795 struct dl
*dl
= ctx
->dl
;
2799 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2800 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2801 !tb
[DEVLINK_ATTR_PARAM
])
2802 return MNL_CB_ERROR
;
2804 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2805 if (err
!= MNL_CB_OK
)
2806 return MNL_CB_ERROR
;
2808 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2809 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2810 return MNL_CB_ERROR
;
2812 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2813 mnl_attr_for_each_nested(param_value_attr
,
2814 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2815 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2816 struct nlattr
*val_attr
;
2818 err
= mnl_attr_parse_nested(param_value_attr
,
2819 attr_cb
, nla_value
);
2820 if (err
!= MNL_CB_OK
)
2821 return MNL_CB_ERROR
;
2823 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2824 (nla_type
!= MNL_TYPE_FLAG
&&
2825 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2826 return MNL_CB_ERROR
;
2828 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2829 if (cmode
== dl
->opts
.cmode
) {
2830 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2833 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2836 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2839 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2841 case MNL_TYPE_STRING
:
2842 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2845 ctx
->value
.vbool
= val_attr
? true : false;
2851 ctx
->nla_type
= nla_type
;
2855 static int cmd_dev_param_set(struct dl
*dl
)
2857 struct param_ctx ctx
= {};
2858 struct nlmsghdr
*nlh
;
2860 uint32_t val_u32
= 0;
2866 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2868 DL_OPT_PARAM_VALUE
|
2869 DL_OPT_PARAM_CMODE
, 0);
2873 /* Get value type */
2874 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2875 NLM_F_REQUEST
| NLM_F_ACK
);
2876 dl_opts_put(nlh
, dl
);
2879 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2883 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2884 NLM_F_REQUEST
| NLM_F_ACK
);
2885 dl_opts_put(nlh
, dl
);
2887 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2888 dl
->opts
.param_name
);
2890 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2891 switch (ctx
.nla_type
) {
2894 err
= param_val_conv_uint_get(param_val_conv
,
2896 dl
->opts
.param_name
,
2897 dl
->opts
.param_value
,
2901 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2904 goto err_param_value_parse
;
2905 if (val_u8
== ctx
.value
.vu8
)
2907 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2911 err
= param_val_conv_uint_get(param_val_conv
,
2913 dl
->opts
.param_name
,
2914 dl
->opts
.param_value
,
2918 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2921 goto err_param_value_parse
;
2922 if (val_u16
== ctx
.value
.vu16
)
2924 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2928 err
= param_val_conv_uint_get(param_val_conv
,
2930 dl
->opts
.param_name
,
2931 dl
->opts
.param_value
,
2934 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2936 goto err_param_value_parse
;
2937 if (val_u32
== ctx
.value
.vu32
)
2939 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2942 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2944 goto err_param_value_parse
;
2945 if (val_bool
== ctx
.value
.vbool
)
2948 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2951 case MNL_TYPE_STRING
:
2952 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2953 dl
->opts
.param_value
);
2954 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2958 printf("Value type not supported\n");
2961 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2963 err_param_value_parse
:
2964 pr_err("Value \"%s\" is not a number or not within range\n",
2965 dl
->opts
.param_value
);
2969 static int cmd_dev_param_show(struct dl
*dl
)
2971 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2972 struct nlmsghdr
*nlh
;
2975 if (dl_argc(dl
) == 0)
2976 flags
|= NLM_F_DUMP
;
2978 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2980 if (dl_argc(dl
) > 0) {
2981 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2982 DL_OPT_PARAM_NAME
, 0);
2987 pr_out_section_start(dl
, "param");
2988 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2989 pr_out_section_end(dl
);
2993 static int cmd_dev_param(struct dl
*dl
)
2995 if (dl_argv_match(dl
, "help")) {
2998 } else if (dl_argv_match(dl
, "show") ||
2999 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3001 return cmd_dev_param_show(dl
);
3002 } else if (dl_argv_match(dl
, "set")) {
3004 return cmd_dev_param_set(dl
);
3006 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3010 static void pr_out_action_stats(struct dl
*dl
, struct nlattr
*action_stats
)
3012 struct nlattr
*tb_stats_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
3013 struct nlattr
*nla_reload_stats_entry
, *nla_limit
, *nla_value
;
3014 enum devlink_reload_limit limit
;
3018 mnl_attr_for_each_nested(nla_reload_stats_entry
, action_stats
) {
3019 err
= mnl_attr_parse_nested(nla_reload_stats_entry
, attr_cb
,
3021 if (err
!= MNL_CB_OK
)
3024 nla_limit
= tb_stats_entry
[DEVLINK_ATTR_RELOAD_STATS_LIMIT
];
3025 nla_value
= tb_stats_entry
[DEVLINK_ATTR_RELOAD_STATS_VALUE
];
3026 if (!nla_limit
|| !nla_value
)
3029 check_indent_newline(dl
);
3030 limit
= mnl_attr_get_u8(nla_limit
);
3031 value
= mnl_attr_get_u32(nla_value
);
3032 print_uint_name_value(reload_limit_name(limit
), value
);
3036 static void pr_out_reload_stats(struct dl
*dl
, struct nlattr
*reload_stats
)
3038 struct nlattr
*nla_action_info
, *nla_action
, *nla_action_stats
;
3039 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3040 enum devlink_reload_action action
;
3043 mnl_attr_for_each_nested(nla_action_info
, reload_stats
) {
3044 err
= mnl_attr_parse_nested(nla_action_info
, attr_cb
, tb
);
3045 if (err
!= MNL_CB_OK
)
3047 nla_action
= tb
[DEVLINK_ATTR_RELOAD_ACTION
];
3048 nla_action_stats
= tb
[DEVLINK_ATTR_RELOAD_ACTION_STATS
];
3049 if (!nla_action
|| !nla_action_stats
)
3052 action
= mnl_attr_get_u8(nla_action
);
3053 pr_out_object_start(dl
, reload_action_name(action
));
3054 pr_out_action_stats(dl
, nla_action_stats
);
3055 pr_out_object_end(dl
);
3059 static void pr_out_reload_data(struct dl
*dl
, struct nlattr
**tb
)
3061 struct nlattr
*nla_reload_stats
, *nla_remote_reload_stats
;
3062 struct nlattr
*tb_stats
[DEVLINK_ATTR_MAX
+ 1] = {};
3063 uint8_t reload_failed
= 0;
3066 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
3067 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
3069 if (reload_failed
) {
3070 check_indent_newline(dl
);
3071 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
3073 if (!tb
[DEVLINK_ATTR_DEV_STATS
] || !dl
->stats
)
3075 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_DEV_STATS
], attr_cb
,
3077 if (err
!= MNL_CB_OK
)
3080 pr_out_object_start(dl
, "stats");
3082 nla_reload_stats
= tb_stats
[DEVLINK_ATTR_RELOAD_STATS
];
3083 if (nla_reload_stats
) {
3084 pr_out_object_start(dl
, "reload");
3085 pr_out_reload_stats(dl
, nla_reload_stats
);
3086 pr_out_object_end(dl
);
3088 nla_remote_reload_stats
= tb_stats
[DEVLINK_ATTR_REMOTE_RELOAD_STATS
];
3089 if (nla_remote_reload_stats
) {
3090 pr_out_object_start(dl
, "remote_reload");
3091 pr_out_reload_stats(dl
, nla_remote_reload_stats
);
3092 pr_out_object_end(dl
);
3095 pr_out_object_end(dl
);
3099 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
3101 if ((tb
[DEVLINK_ATTR_RELOAD_FAILED
] && mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
])) ||
3102 (tb
[DEVLINK_ATTR_DEV_STATS
] && dl
->stats
)) {
3103 __pr_out_handle_start(dl
, tb
, true, false);
3104 pr_out_reload_data(dl
, tb
);
3105 pr_out_handle_end(dl
);
3107 pr_out_handle(dl
, tb
);
3111 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3113 struct dl
*dl
= data
;
3114 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3115 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3117 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3118 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3119 return MNL_CB_ERROR
;
3125 static int cmd_dev_show(struct dl
*dl
)
3127 struct nlmsghdr
*nlh
;
3128 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3131 if (dl_argc(dl
) == 0)
3132 flags
|= NLM_F_DUMP
;
3134 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
3136 if (dl_argc(dl
) > 0) {
3137 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3142 pr_out_section_start(dl
, "dev");
3143 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
3144 pr_out_section_end(dl
);
3148 static void pr_out_reload_actions_performed(struct dl
*dl
, struct nlattr
**tb
)
3150 struct nlattr
*nla_actions_performed
;
3151 struct nla_bitfield32
*actions
;
3152 uint32_t actions_performed
;
3156 if (!tb
[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
])
3159 nla_actions_performed
= tb
[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
];
3160 len
= mnl_attr_get_payload_len(nla_actions_performed
);
3161 if (len
!= sizeof(*actions
))
3163 actions
= mnl_attr_get_payload(nla_actions_performed
);
3166 g_new_line_count
= 1; /* Avoid extra new line in non-json print */
3167 pr_out_array_start(dl
, "reload_actions_performed");
3168 actions_performed
= actions
->value
& actions
->selector
;
3169 for (action
= 0; action
<= DEVLINK_RELOAD_ACTION_MAX
; action
++) {
3170 if (BIT(action
) & actions_performed
) {
3171 check_indent_newline(dl
);
3172 print_string(PRINT_ANY
, NULL
, "%s",
3173 reload_action_name(action
));
3176 pr_out_array_end(dl
);
3177 if (!dl
->json_output
)
3181 static int cmd_dev_reload_cb(const struct nlmsghdr
*nlh
, void *data
)
3183 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3184 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3185 struct dl
*dl
= data
;
3187 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3188 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3189 !tb
[DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
])
3190 return MNL_CB_ERROR
;
3192 pr_out_section_start(dl
, "reload");
3193 pr_out_reload_actions_performed(dl
, tb
);
3194 pr_out_section_end(dl
);
3199 static int cmd_dev_reload(struct dl
*dl
)
3201 struct nlmsghdr
*nlh
;
3204 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3209 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
3210 NLM_F_REQUEST
| NLM_F_ACK
);
3212 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
3213 DL_OPT_NETNS
| DL_OPT_RELOAD_ACTION
|
3214 DL_OPT_RELOAD_LIMIT
);
3218 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_reload_cb
, dl
);
3221 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
3222 const char *name
, int type
)
3224 struct nlattr
*version
;
3226 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
3227 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3228 const char *ver_value
;
3229 const char *ver_name
;
3232 if (mnl_attr_get_type(version
) != type
)
3235 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
3236 if (err
!= MNL_CB_OK
)
3239 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
3240 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
3244 pr_out_object_start(dl
, name
);
3248 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
3249 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
3251 check_indent_newline(dl
);
3252 print_string_name_value(ver_name
, ver_value
);
3253 if (!dl
->json_output
)
3258 pr_out_object_end(dl
);
3261 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
3262 struct nlattr
**tb
, bool has_versions
)
3264 __pr_out_handle_start(dl
, tb
, true, false);
3266 __pr_out_indent_inc();
3267 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
3268 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
3270 if (!dl
->json_output
)
3272 check_indent_newline(dl
);
3273 print_string(PRINT_ANY
, "driver", "driver %s",
3274 mnl_attr_get_str(nla_drv
));
3277 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
3278 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
3280 if (!dl
->json_output
)
3282 check_indent_newline(dl
);
3283 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
3284 mnl_attr_get_str(nla_sn
));
3287 if (tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
]) {
3288 struct nlattr
*nla_bsn
= tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
];
3290 if (!dl
->json_output
)
3292 check_indent_newline(dl
);
3293 print_string(PRINT_ANY
, "board.serial_number", "board.serial_number %s",
3294 mnl_attr_get_str(nla_bsn
));
3296 __pr_out_indent_dec();
3299 pr_out_object_start(dl
, "versions");
3301 pr_out_versions_single(dl
, nlh
, "fixed",
3302 DEVLINK_ATTR_INFO_VERSION_FIXED
);
3303 pr_out_versions_single(dl
, nlh
, "running",
3304 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
3305 pr_out_versions_single(dl
, nlh
, "stored",
3306 DEVLINK_ATTR_INFO_VERSION_STORED
);
3308 pr_out_object_end(dl
);
3311 pr_out_handle_end(dl
);
3314 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3316 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3317 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3318 bool has_versions
, has_info
;
3319 struct dl
*dl
= data
;
3321 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3323 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3324 return MNL_CB_ERROR
;
3326 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
3327 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
3328 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
3329 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
3330 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
3331 tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
] ||
3335 pr_out_info(dl
, nlh
, tb
, has_versions
);
3340 static int cmd_dev_info(struct dl
*dl
)
3342 struct nlmsghdr
*nlh
;
3343 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3346 if (dl_argv_match(dl
, "help")) {
3351 if (dl_argc(dl
) == 0)
3352 flags
|= NLM_F_DUMP
;
3354 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
3356 if (dl_argc(dl
) > 0) {
3357 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3362 pr_out_section_start(dl
, "info");
3363 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
3364 pr_out_section_end(dl
);
3368 struct cmd_dev_flash_status_ctx
{
3370 struct timespec time_of_last_status
;
3371 uint64_t status_msg_timeout
;
3372 size_t elapsed_time_msg_len
;
3374 char *last_component
;
3375 uint8_t not_first
:1,
3381 static int nullstrcmp(const char *str1
, const char *str2
)
3384 return strcmp(str1
, str2
);
3387 return str1
? 1 : -1;
3390 static void cmd_dev_flash_clear_elapsed_time(struct cmd_dev_flash_status_ctx
*ctx
)
3394 for (i
= 0; i
< ctx
->elapsed_time_msg_len
; i
++)
3395 pr_out_tty("\b \b");
3397 ctx
->elapsed_time_msg_len
= 0;
3400 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
3402 struct cmd_dev_flash_status_ctx
*ctx
= data
;
3403 struct dl_opts
*opts
= &ctx
->dl
->opts
;
3404 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3405 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3406 const char *component
= NULL
;
3407 uint64_t done
= 0, total
= 0;
3408 const char *msg
= NULL
;
3409 const char *bus_name
;
3410 const char *dev_name
;
3412 cmd_dev_flash_clear_elapsed_time(ctx
);
3414 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
3415 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3418 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3419 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3420 return MNL_CB_ERROR
;
3421 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3422 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3423 if (strcmp(bus_name
, opts
->bus_name
) ||
3424 strcmp(dev_name
, opts
->dev_name
))
3425 return MNL_CB_ERROR
;
3427 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3429 free(ctx
->last_msg
);
3430 free(ctx
->last_component
);
3431 ctx
->received_end
= 1;
3435 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3436 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3437 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3438 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3439 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3440 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3441 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3442 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3443 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
])
3444 ctx
->status_msg_timeout
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
]);
3446 ctx
->status_msg_timeout
= 0;
3448 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3449 !nullstrcmp(component
, ctx
->last_component
) &&
3450 ctx
->last_pc
&& ctx
->not_first
) {
3451 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3453 /* only update the last status timestamp if the message changed */
3454 clock_gettime(CLOCK_MONOTONIC
, &ctx
->time_of_last_status
);
3459 pr_out("[%s] ", component
);
3460 free(ctx
->last_component
);
3461 ctx
->last_component
= strdup(component
);
3465 free(ctx
->last_msg
);
3466 ctx
->last_msg
= strdup(msg
);
3470 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3481 static void cmd_dev_flash_time_elapsed(struct cmd_dev_flash_status_ctx
*ctx
)
3483 struct timespec now
, res
;
3485 clock_gettime(CLOCK_MONOTONIC
, &now
);
3487 res
.tv_sec
= now
.tv_sec
- ctx
->time_of_last_status
.tv_sec
;
3488 res
.tv_nsec
= now
.tv_nsec
- ctx
->time_of_last_status
.tv_nsec
;
3489 if (res
.tv_nsec
< 0) {
3491 res
.tv_nsec
+= 1000000000L;
3494 /* Only begin displaying an elapsed time message if we've waited a few
3495 * seconds with no response, or the status message included a timeout
3498 if (res
.tv_sec
> 2 || ctx
->status_msg_timeout
) {
3499 uint64_t elapsed_m
, elapsed_s
;
3503 /* clear the last elapsed time message, if we have one */
3504 cmd_dev_flash_clear_elapsed_time(ctx
);
3506 elapsed_m
= res
.tv_sec
/ 60;
3507 elapsed_s
= res
.tv_sec
% 60;
3510 * If we've elapsed a few seconds without receiving any status
3511 * notification from the device, we display a time elapsed
3512 * message. This has a few possible formats:
3514 * 1) just time elapsed, when no timeout was provided
3516 * 2) time elapsed out of a timeout that came from the device
3517 * driver via DEVLINK_CMD_FLASH_UPDATE_STATUS_TIMEOUT
3518 * " ( Xm Ys : Am Ys)"
3519 * 3) time elapsed if we still receive no status after
3520 * reaching the provided timeout.
3521 * " ( Xm Ys : timeout reached )"
3523 if (!ctx
->status_msg_timeout
) {
3524 len
= snprintf(msg
, sizeof(msg
),
3525 " ( %lum %lus )", elapsed_m
, elapsed_s
);
3526 } else if (res
.tv_sec
<= ctx
->status_msg_timeout
) {
3527 uint64_t timeout_m
, timeout_s
;
3529 timeout_m
= ctx
->status_msg_timeout
/ 60;
3530 timeout_s
= ctx
->status_msg_timeout
% 60;
3532 len
= snprintf(msg
, sizeof(msg
),
3533 " ( %lum %lus : %lum %lus )",
3534 elapsed_m
, elapsed_s
, timeout_m
, timeout_s
);
3536 len
= snprintf(msg
, sizeof(msg
),
3537 " ( %lum %lus : timeout reached )", elapsed_m
, elapsed_s
);
3540 ctx
->elapsed_time_msg_len
= len
;
3542 pr_out_tty("%s", msg
);
3547 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3548 struct mnlg_socket
*nlg_ntf
,
3551 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3552 struct timeval timeout
;
3559 for (i
= 0; i
< 3; i
++)
3561 FD_SET(pipe_r
, &fds
[0]);
3563 FD_SET(nlfd
, &fds
[0]);
3567 /* select only for a short while (1/10th of a second) in order to
3568 * allow periodically updating the screen with an elapsed time
3572 timeout
.tv_usec
= 100000;
3574 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], &timeout
) < 0) {
3577 pr_err("select() failed\n");
3580 if (FD_ISSET(nlfd
, &fds
[0])) {
3581 err
= _mnlg_socket_recv_run(nlg_ntf
,
3582 cmd_dev_flash_status_cb
, ctx
);
3586 if (FD_ISSET(pipe_r
, &fds
[0])) {
3587 err
= read(pipe_r
, &err2
, sizeof(err2
));
3589 pr_err("Failed to read pipe\n");
3594 ctx
->flash_done
= 1;
3596 cmd_dev_flash_time_elapsed(ctx
);
3601 static int cmd_dev_flash(struct dl
*dl
)
3603 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3604 struct mnlg_socket
*nlg_ntf
;
3605 struct nlmsghdr
*nlh
;
3611 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3616 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3617 NLM_F_REQUEST
| NLM_F_ACK
);
3619 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3620 DL_OPT_FLASH_COMPONENT
| DL_OPT_FLASH_OVERWRITE
);
3624 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3628 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3632 err
= pipe(pipe_fds
);
3637 pipe_r
= pipe_fds
[0];
3638 pipe_w
= pipe_fds
[1];
3646 /* In child, just execute the flash and pass returned
3647 * value through pipe once it is done.
3652 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3653 cc
= write(pipe_w
, &err
, sizeof(err
));
3655 exit(cc
!= sizeof(err
));
3659 /* initialize starting time to allow comparison for when to begin
3660 * displaying a time elapsed message.
3662 clock_gettime(CLOCK_MONOTONIC
, &ctx
.time_of_last_status
);
3665 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3668 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3670 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3674 mnlg_socket_close(nlg_ntf
);
3678 static int cmd_dev(struct dl
*dl
)
3680 if (dl_argv_match(dl
, "help")) {
3683 } else if (dl_argv_match(dl
, "show") ||
3684 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3686 return cmd_dev_show(dl
);
3687 } else if (dl_argv_match(dl
, "eswitch")) {
3689 return cmd_dev_eswitch(dl
);
3690 } else if (dl_argv_match(dl
, "reload")) {
3692 return cmd_dev_reload(dl
);
3693 } else if (dl_argv_match(dl
, "param")) {
3695 return cmd_dev_param(dl
);
3696 } else if (dl_argv_match(dl
, "info")) {
3698 return cmd_dev_info(dl
);
3699 } else if (dl_argv_match(dl
, "flash")) {
3701 return cmd_dev_flash(dl
);
3703 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3707 static void cmd_port_help(void)
3709 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3710 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3711 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3712 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3713 pr_err(" devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3714 pr_err(" devlink port health show [ DEV/PORT_INDEX reporter REPORTER_NAME ]\n");
3717 static const char *port_type_name(uint32_t type
)
3720 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3721 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3722 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3723 case DEVLINK_PORT_TYPE_IB
: return "ib";
3724 default: return "<unknown type>";
3728 static const char *port_flavour_name(uint16_t flavour
)
3732 str
= str_map_lookup_u16(port_flavour_map
, flavour
);
3733 return str
? str
: "<unknown flavour>";
3736 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3740 if (tb
[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
])
3741 print_uint(PRINT_ANY
, "controller", " controller %u",
3742 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
]));
3743 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3744 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3745 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3747 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3748 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3749 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3751 if (tb
[DEVLINK_ATTR_PORT_EXTERNAL
]) {
3754 external
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_PORT_EXTERNAL
]);
3755 print_bool(PRINT_ANY
, "external", " external %s", external
);
3759 static void pr_out_port_function(struct dl
*dl
, struct nlattr
**tb_port
)
3761 struct nlattr
*tb
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {};
3762 unsigned char *data
;
3763 SPRINT_BUF(hw_addr
);
3767 if (!tb_port
[DEVLINK_ATTR_PORT_FUNCTION
])
3770 err
= mnl_attr_parse_nested(tb_port
[DEVLINK_ATTR_PORT_FUNCTION
],
3771 function_attr_cb
, tb
);
3772 if (err
!= MNL_CB_OK
)
3775 if (!tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
])
3778 len
= mnl_attr_get_payload_len(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3779 data
= mnl_attr_get_payload(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3781 pr_out_object_start(dl
, "function");
3782 check_indent_newline(dl
);
3783 print_string(PRINT_ANY
, "hw_addr", "hw_addr %s",
3784 ll_addr_n2a(data
, len
, 0, hw_addr
, sizeof(hw_addr
)));
3785 if (!dl
->json_output
)
3786 __pr_out_indent_dec();
3787 pr_out_object_end(dl
);
3790 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3792 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3793 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3795 pr_out_port_handle_start(dl
, tb
, false);
3796 check_indent_newline(dl
);
3798 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3800 print_string(PRINT_ANY
, "type", "type %s",
3801 port_type_name(port_type
));
3803 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3805 if (port_type
!= des_port_type
)
3806 print_string(PRINT_ANY
, "des_type", " des_type %s",
3807 port_type_name(des_port_type
));
3810 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3811 print_string(PRINT_ANY
, "netdev", " netdev %s",
3812 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3814 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3815 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3816 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3818 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3819 uint16_t port_flavour
=
3820 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3822 print_string(PRINT_ANY
, "flavour", " flavour %s",
3823 port_flavour_name(port_flavour
));
3825 switch (port_flavour
) {
3826 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3827 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3828 pr_out_port_pfvf_num(dl
, tb
);
3834 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3835 uint32_t port_number
;
3837 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3838 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3840 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3841 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3842 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3843 if (tb
[DEVLINK_ATTR_PORT_SPLITTABLE
])
3844 print_bool(PRINT_ANY
, "splittable", " splittable %s",
3845 mnl_attr_get_u8(tb
[DEVLINK_ATTR_PORT_SPLITTABLE
]));
3846 if (tb
[DEVLINK_ATTR_PORT_LANES
])
3847 print_uint(PRINT_ANY
, "lanes", " lanes %u",
3848 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_LANES
]));
3850 pr_out_port_function(dl
, tb
);
3851 pr_out_port_handle_end(dl
);
3854 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3856 struct dl
*dl
= data
;
3857 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3858 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3860 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3861 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3862 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3863 return MNL_CB_ERROR
;
3864 pr_out_port(dl
, tb
);
3868 static int cmd_port_show(struct dl
*dl
)
3870 struct nlmsghdr
*nlh
;
3871 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3874 if (dl_argc(dl
) == 0)
3875 flags
|= NLM_F_DUMP
;
3877 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3879 if (dl_argc(dl
) > 0) {
3880 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3885 pr_out_section_start(dl
, "port");
3886 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3887 pr_out_section_end(dl
);
3891 static int cmd_port_set(struct dl
*dl
)
3893 struct nlmsghdr
*nlh
;
3896 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3897 NLM_F_REQUEST
| NLM_F_ACK
);
3899 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3903 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3906 static int cmd_port_split(struct dl
*dl
)
3908 struct nlmsghdr
*nlh
;
3911 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3912 NLM_F_REQUEST
| NLM_F_ACK
);
3914 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3918 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3921 static int cmd_port_unsplit(struct dl
*dl
)
3923 struct nlmsghdr
*nlh
;
3926 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3927 NLM_F_REQUEST
| NLM_F_ACK
);
3929 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3933 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3936 static void cmd_port_function_help(void)
3938 pr_err("Usage: devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3941 static int cmd_port_function_set(struct dl
*dl
)
3943 struct nlmsghdr
*nlh
;
3946 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
, NLM_F_REQUEST
| NLM_F_ACK
);
3948 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_FUNCTION_HW_ADDR
, 0);
3952 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3955 static int cmd_port_function(struct dl
*dl
)
3957 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3958 cmd_port_function_help();
3960 } else if (dl_argv_match(dl
, "set")) {
3962 return cmd_port_function_set(dl
);
3964 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3968 static int cmd_health(struct dl
*dl
);
3969 static int __cmd_health_show(struct dl
*dl
, bool show_device
, bool show_port
);
3971 static int cmd_port(struct dl
*dl
)
3973 if (dl_argv_match(dl
, "help")) {
3976 } else if (dl_argv_match(dl
, "show") ||
3977 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3979 return cmd_port_show(dl
);
3980 } else if (dl_argv_match(dl
, "set")) {
3982 return cmd_port_set(dl
);
3983 } else if (dl_argv_match(dl
, "split")) {
3985 return cmd_port_split(dl
);
3986 } else if (dl_argv_match(dl
, "unsplit")) {
3988 return cmd_port_unsplit(dl
);
3989 } else if (dl_argv_match(dl
, "function")) {
3991 return cmd_port_function(dl
);
3992 } else if (dl_argv_match(dl
, "health")) {
3994 if (dl_argv_match(dl
, "list") || dl_no_arg(dl
)
3995 || (dl_argv_match(dl
, "show") && dl_argc(dl
) == 1)) {
3997 return __cmd_health_show(dl
, false, true);
3999 return cmd_health(dl
);
4002 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4006 static void cmd_sb_help(void)
4008 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
4009 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
4010 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
4011 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
4012 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
4013 pr_err(" pool POOL_INDEX ]\n");
4014 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
4015 pr_err(" pool POOL_INDEX th THRESHOLD\n");
4016 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
4017 pr_err(" type { ingress | egress } ]\n");
4018 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
4019 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
4020 pr_err(" th THRESHOLD\n");
4021 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
4022 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
4023 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
4026 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
4028 pr_out_handle_start_arr(dl
, tb
);
4029 check_indent_newline(dl
);
4030 print_uint(PRINT_ANY
, "sb", "sb %u",
4031 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4032 print_uint(PRINT_ANY
, "size", " size %u",
4033 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
4034 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
4035 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
4036 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
4037 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
4038 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
4039 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
4040 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
4041 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
4042 pr_out_handle_end(dl
);
4045 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4047 struct dl
*dl
= data
;
4048 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4049 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4051 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4052 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4053 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
4054 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
4055 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
4056 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
4057 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
4058 return MNL_CB_ERROR
;
4063 static int cmd_sb_show(struct dl
*dl
)
4065 struct nlmsghdr
*nlh
;
4066 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4069 if (dl_argc(dl
) == 0)
4070 flags
|= NLM_F_DUMP
;
4072 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
4074 if (dl_argc(dl
) > 0) {
4075 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4080 pr_out_section_start(dl
, "sb");
4081 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
4082 pr_out_section_end(dl
);
4086 static const char *pool_type_name(uint8_t type
)
4089 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
4090 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
4091 default: return "<unknown type>";
4095 static const char *threshold_type_name(uint8_t type
)
4098 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
4099 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
4100 default: return "<unknown type>";
4104 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
4106 pr_out_handle_start_arr(dl
, tb
);
4107 check_indent_newline(dl
);
4108 print_uint(PRINT_ANY
, "sb", "sb %u",
4109 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4110 print_uint(PRINT_ANY
, "pool", " pool %u",
4111 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4112 print_string(PRINT_ANY
, "type", " type %s",
4113 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
4114 print_uint(PRINT_ANY
, "size", " size %u",
4115 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
4116 print_string(PRINT_ANY
, "thtype", " thtype %s",
4117 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
4118 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
4119 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
4120 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
4121 pr_out_handle_end(dl
);
4124 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4126 struct dl
*dl
= data
;
4127 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4128 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4130 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4131 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4132 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4133 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
4134 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
4135 return MNL_CB_ERROR
;
4136 pr_out_sb_pool(dl
, tb
);
4140 static int cmd_sb_pool_show(struct dl
*dl
)
4142 struct nlmsghdr
*nlh
;
4143 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4146 if (dl_argc(dl
) == 0)
4147 flags
|= NLM_F_DUMP
;
4149 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
4151 if (dl_argc(dl
) > 0) {
4152 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
4158 pr_out_section_start(dl
, "pool");
4159 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
4160 pr_out_section_end(dl
);
4164 static int cmd_sb_pool_set(struct dl
*dl
)
4166 struct nlmsghdr
*nlh
;
4169 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
4170 NLM_F_REQUEST
| NLM_F_ACK
);
4172 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
4173 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
4177 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4180 static int cmd_sb_pool(struct dl
*dl
)
4182 if (dl_argv_match(dl
, "help")) {
4185 } else if (dl_argv_match(dl
, "show") ||
4186 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4188 return cmd_sb_pool_show(dl
);
4189 } else if (dl_argv_match(dl
, "set")) {
4191 return cmd_sb_pool_set(dl
);
4193 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4197 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
4199 pr_out_port_handle_start_arr(dl
, tb
, true);
4200 check_indent_newline(dl
);
4201 print_uint(PRINT_ANY
, "sb", "sb %u",
4202 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4203 print_uint(PRINT_ANY
, "pool", " pool %u",
4204 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4205 print_uint(PRINT_ANY
, "threshold", " threshold %u",
4206 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
4207 pr_out_port_handle_end(dl
);
4210 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4212 struct dl
*dl
= data
;
4213 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4214 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4216 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4217 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4218 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4219 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
4220 return MNL_CB_ERROR
;
4221 pr_out_sb_port_pool(dl
, tb
);
4225 static int cmd_sb_port_pool_show(struct dl
*dl
)
4227 struct nlmsghdr
*nlh
;
4228 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4231 if (dl_argc(dl
) == 0)
4232 flags
|= NLM_F_DUMP
;
4234 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4236 if (dl_argc(dl
) > 0) {
4237 err
= dl_argv_parse_put(nlh
, dl
,
4238 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
4244 pr_out_section_start(dl
, "port_pool");
4245 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
4246 pr_out_section_end(dl
);
4250 static int cmd_sb_port_pool_set(struct dl
*dl
)
4252 struct nlmsghdr
*nlh
;
4255 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
4256 NLM_F_REQUEST
| NLM_F_ACK
);
4258 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
4259 DL_OPT_SB_TH
, DL_OPT_SB
);
4263 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4266 static int cmd_sb_port_pool(struct dl
*dl
)
4268 if (dl_argv_match(dl
, "help")) {
4271 } else if (dl_argv_match(dl
, "show") ||
4272 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4274 return cmd_sb_port_pool_show(dl
);
4275 } else if (dl_argv_match(dl
, "set")) {
4277 return cmd_sb_port_pool_set(dl
);
4279 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4283 static int cmd_sb_port(struct dl
*dl
)
4285 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4288 } else if (dl_argv_match(dl
, "pool")) {
4290 return cmd_sb_port_pool(dl
);
4292 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4296 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
4298 pr_out_port_handle_start_arr(dl
, tb
, true);
4299 check_indent_newline(dl
);
4300 print_uint(PRINT_ANY
, "sb", "sb %u",
4301 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4302 print_uint(PRINT_ANY
, "tc", " tc %u",
4303 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
4304 print_string(PRINT_ANY
, "type", " type %s",
4305 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
4306 print_uint(PRINT_ANY
, "pool", " pool %u",
4307 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4308 print_uint(PRINT_ANY
, "threshold", " threshold %u",
4309 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
4310 pr_out_port_handle_end(dl
);
4313 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4315 struct dl
*dl
= data
;
4316 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4317 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4319 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4320 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4321 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4322 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4323 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
4324 return MNL_CB_ERROR
;
4325 pr_out_sb_tc_bind(dl
, tb
);
4329 static int cmd_sb_tc_bind_show(struct dl
*dl
)
4331 struct nlmsghdr
*nlh
;
4332 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4335 if (dl_argc(dl
) == 0)
4336 flags
|= NLM_F_DUMP
;
4338 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4340 if (dl_argc(dl
) > 0) {
4341 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
4342 DL_OPT_SB_TYPE
, DL_OPT_SB
);
4347 pr_out_section_start(dl
, "tc_bind");
4348 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
4349 pr_out_section_end(dl
);
4353 static int cmd_sb_tc_bind_set(struct dl
*dl
)
4355 struct nlmsghdr
*nlh
;
4358 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
4359 NLM_F_REQUEST
| NLM_F_ACK
);
4361 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
4362 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
4367 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4370 static int cmd_sb_tc_bind(struct dl
*dl
)
4372 if (dl_argv_match(dl
, "help")) {
4375 } else if (dl_argv_match(dl
, "show") ||
4376 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4378 return cmd_sb_tc_bind_show(dl
);
4379 } else if (dl_argv_match(dl
, "set")) {
4381 return cmd_sb_tc_bind_set(dl
);
4383 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4387 static int cmd_sb_tc(struct dl
*dl
)
4389 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4392 } else if (dl_argv_match(dl
, "bind")) {
4394 return cmd_sb_tc_bind(dl
);
4396 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4401 struct list_head list
;
4405 uint32_t bound_pool_index
;
4409 struct list_head list
;
4412 uint32_t port_index
;
4414 struct list_head pool_list
;
4415 struct list_head ing_tc_list
;
4416 struct list_head eg_tc_list
;
4422 struct list_head port_list
;
4425 static struct occ_item
*occ_item_alloc(void)
4427 return calloc(1, sizeof(struct occ_item
));
4430 static void occ_item_free(struct occ_item
*occ_item
)
4435 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
4437 struct occ_port
*occ_port
;
4439 occ_port
= calloc(1, sizeof(*occ_port
));
4442 occ_port
->port_index
= port_index
;
4443 INIT_LIST_HEAD(&occ_port
->pool_list
);
4444 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
4445 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
4449 static void occ_port_free(struct occ_port
*occ_port
)
4451 struct occ_item
*occ_item
, *tmp
;
4453 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
4454 occ_item_free(occ_item
);
4455 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
4456 occ_item_free(occ_item
);
4457 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
4458 occ_item_free(occ_item
);
4461 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
4463 struct occ_show
*occ_show
;
4465 occ_show
= calloc(1, sizeof(*occ_show
));
4469 INIT_LIST_HEAD(&occ_show
->port_list
);
4473 static void occ_show_free(struct occ_show
*occ_show
)
4475 struct occ_port
*occ_port
, *tmp
;
4477 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
4478 occ_port_free(occ_port
);
4481 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
4484 struct occ_port
*occ_port
;
4485 uint32_t port_index
;
4487 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
4489 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
4490 if (occ_port
->port_index
== port_index
)
4493 occ_port
= occ_port_alloc(port_index
);
4496 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
4500 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
4503 struct occ_item
*occ_item
;
4506 pr_out_sp(7, " %s:", label
);
4507 list_for_each_entry(occ_item
, list
, list
) {
4508 if ((i
- 1) % 4 == 0 && i
!= 1)
4511 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
4512 occ_item
->bound_pool_index
);
4514 pr_out_sp(7, "%2u:", occ_item
->index
);
4515 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
4519 if ((i
- 1) % 4 != 0)
4523 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
4524 struct list_head
*list
,
4527 struct occ_item
*occ_item
;
4530 open_json_object(label
);
4531 list_for_each_entry(occ_item
, list
, list
) {
4532 sprintf(buf
, "%u", occ_item
->index
);
4533 open_json_object(buf
);
4535 print_uint(PRINT_JSON
, "bound_pool", NULL
,
4536 occ_item
->bound_pool_index
);
4537 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
4538 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
4539 close_json_object();
4541 close_json_object();
4544 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
4546 if (dl
->json_output
) {
4547 pr_out_json_occ_show_item_list(dl
, "pool",
4548 &occ_port
->pool_list
, false);
4549 pr_out_json_occ_show_item_list(dl
, "itc",
4550 &occ_port
->ing_tc_list
, true);
4551 pr_out_json_occ_show_item_list(dl
, "etc",
4552 &occ_port
->eg_tc_list
, true);
4555 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
4556 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
4557 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
4561 static void pr_out_occ_show(struct occ_show
*occ_show
)
4563 struct dl
*dl
= occ_show
->dl
;
4564 struct dl_opts
*opts
= &dl
->opts
;
4565 struct occ_port
*occ_port
;
4567 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
4568 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
4569 occ_port
->port_index
, true, false);
4570 pr_out_occ_show_port(dl
, occ_port
);
4571 pr_out_port_handle_end(dl
);
4575 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
4578 struct occ_port
*occ_port
;
4579 struct occ_item
*occ_item
;
4581 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4584 occ_port
= occ_port_get(occ_show
, tb
);
4586 occ_show
->err
= -ENOMEM
;
4590 occ_item
= occ_item_alloc();
4592 occ_show
->err
= -ENOMEM
;
4595 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4596 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4597 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4598 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4601 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4603 struct occ_show
*occ_show
= data
;
4604 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4605 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4607 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4608 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4609 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4610 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4611 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4612 return MNL_CB_ERROR
;
4613 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4617 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4620 struct occ_port
*occ_port
;
4621 struct occ_item
*occ_item
;
4624 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4627 occ_port
= occ_port_get(occ_show
, tb
);
4629 occ_show
->err
= -ENOMEM
;
4633 occ_item
= occ_item_alloc();
4635 occ_show
->err
= -ENOMEM
;
4638 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4639 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4640 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4641 occ_item
->bound_pool_index
=
4642 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4643 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4644 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4645 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4646 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4647 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4649 occ_item_free(occ_item
);
4652 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4654 struct occ_show
*occ_show
= data
;
4655 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4656 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4658 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4659 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4660 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4661 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4662 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4663 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4664 return MNL_CB_ERROR
;
4665 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4669 static int cmd_sb_occ_show(struct dl
*dl
)
4671 struct nlmsghdr
*nlh
;
4672 struct occ_show
*occ_show
;
4673 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4676 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4680 occ_show
= occ_show_alloc(dl
);
4684 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4686 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4687 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4691 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4693 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4694 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4698 pr_out_section_start(dl
, "occupancy");
4699 pr_out_occ_show(occ_show
);
4700 pr_out_section_end(dl
);
4703 occ_show_free(occ_show
);
4707 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4709 struct nlmsghdr
*nlh
;
4712 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4713 NLM_F_REQUEST
| NLM_F_ACK
);
4715 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4719 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4722 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4724 struct nlmsghdr
*nlh
;
4727 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4728 NLM_F_REQUEST
| NLM_F_ACK
);
4730 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4734 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4737 static int cmd_sb_occ(struct dl
*dl
)
4739 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4742 } else if (dl_argv_match(dl
, "show") ||
4743 dl_argv_match(dl
, "list")) {
4745 return cmd_sb_occ_show(dl
);
4746 } else if (dl_argv_match(dl
, "snapshot")) {
4748 return cmd_sb_occ_snapshot(dl
);
4749 } else if (dl_argv_match(dl
, "clearmax")) {
4751 return cmd_sb_occ_clearmax(dl
);
4753 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4757 static int cmd_sb(struct dl
*dl
)
4759 if (dl_argv_match(dl
, "help")) {
4762 } else if (dl_argv_match(dl
, "show") ||
4763 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4765 return cmd_sb_show(dl
);
4766 } else if (dl_argv_match(dl
, "pool")) {
4768 return cmd_sb_pool(dl
);
4769 } else if (dl_argv_match(dl
, "port")) {
4771 return cmd_sb_port(dl
);
4772 } else if (dl_argv_match(dl
, "tc")) {
4774 return cmd_sb_tc(dl
);
4775 } else if (dl_argv_match(dl
, "occupancy")) {
4777 return cmd_sb_occ(dl
);
4779 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4783 static const char *cmd_name(uint8_t cmd
)
4786 case DEVLINK_CMD_UNSPEC
: return "unspec";
4787 case DEVLINK_CMD_GET
: return "get";
4788 case DEVLINK_CMD_SET
: return "set";
4789 case DEVLINK_CMD_NEW
: return "new";
4790 case DEVLINK_CMD_DEL
: return "del";
4791 case DEVLINK_CMD_PORT_GET
: return "get";
4792 case DEVLINK_CMD_PORT_SET
: return "set";
4793 case DEVLINK_CMD_PORT_NEW
: return "new";
4794 case DEVLINK_CMD_PORT_DEL
: return "del";
4795 case DEVLINK_CMD_PARAM_GET
: return "get";
4796 case DEVLINK_CMD_PARAM_SET
: return "set";
4797 case DEVLINK_CMD_PARAM_NEW
: return "new";
4798 case DEVLINK_CMD_PARAM_DEL
: return "del";
4799 case DEVLINK_CMD_REGION_GET
: return "get";
4800 case DEVLINK_CMD_REGION_SET
: return "set";
4801 case DEVLINK_CMD_REGION_NEW
: return "new";
4802 case DEVLINK_CMD_REGION_DEL
: return "del";
4803 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4804 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4805 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4806 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4807 case DEVLINK_CMD_TRAP_GET
: return "get";
4808 case DEVLINK_CMD_TRAP_SET
: return "set";
4809 case DEVLINK_CMD_TRAP_NEW
: return "new";
4810 case DEVLINK_CMD_TRAP_DEL
: return "del";
4811 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4812 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4813 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4814 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4815 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4816 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4817 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4818 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4819 default: return "<unknown cmd>";
4823 static const char *cmd_obj(uint8_t cmd
)
4826 case DEVLINK_CMD_UNSPEC
: return "unspec";
4827 case DEVLINK_CMD_GET
:
4828 case DEVLINK_CMD_SET
:
4829 case DEVLINK_CMD_NEW
:
4830 case DEVLINK_CMD_DEL
:
4832 case DEVLINK_CMD_PORT_GET
:
4833 case DEVLINK_CMD_PORT_SET
:
4834 case DEVLINK_CMD_PORT_NEW
:
4835 case DEVLINK_CMD_PORT_DEL
:
4837 case DEVLINK_CMD_PARAM_GET
:
4838 case DEVLINK_CMD_PARAM_SET
:
4839 case DEVLINK_CMD_PARAM_NEW
:
4840 case DEVLINK_CMD_PARAM_DEL
:
4842 case DEVLINK_CMD_REGION_GET
:
4843 case DEVLINK_CMD_REGION_SET
:
4844 case DEVLINK_CMD_REGION_NEW
:
4845 case DEVLINK_CMD_REGION_DEL
:
4847 case DEVLINK_CMD_FLASH_UPDATE
:
4848 case DEVLINK_CMD_FLASH_UPDATE_END
:
4849 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4851 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4853 case DEVLINK_CMD_TRAP_GET
:
4854 case DEVLINK_CMD_TRAP_SET
:
4855 case DEVLINK_CMD_TRAP_NEW
:
4856 case DEVLINK_CMD_TRAP_DEL
:
4858 case DEVLINK_CMD_TRAP_GROUP_GET
:
4859 case DEVLINK_CMD_TRAP_GROUP_SET
:
4860 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4861 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4862 return "trap-group";
4863 case DEVLINK_CMD_TRAP_POLICER_GET
:
4864 case DEVLINK_CMD_TRAP_POLICER_SET
:
4865 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4866 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4867 return "trap-policer";
4868 default: return "<unknown obj>";
4872 static void pr_out_mon_header(uint8_t cmd
)
4874 if (!is_json_context()) {
4875 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4877 open_json_object(NULL
);
4878 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4879 open_json_object(cmd_obj(cmd
));
4883 static void pr_out_mon_footer(void)
4885 if (is_json_context()) {
4886 close_json_object();
4887 close_json_object();
4891 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4893 const char *obj
= cmd_obj(cmd
);
4894 unsigned int index
= 0;
4895 const char *cur_obj
;
4899 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4900 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4906 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4908 __pr_out_handle_start(dl
, tb
, true, false);
4910 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4911 check_indent_newline(dl
);
4912 print_string(PRINT_ANY
, "msg", "msg %s",
4913 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4915 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4916 check_indent_newline(dl
);
4917 print_string(PRINT_ANY
, "component", "component %s",
4918 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4921 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4922 pr_out_u64(dl
, "done",
4923 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4925 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4926 pr_out_u64(dl
, "total",
4927 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4929 pr_out_handle_end(dl
);
4932 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4933 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
,
4934 bool show_device
, bool show_port
);
4935 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4936 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4937 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4939 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4941 struct dl
*dl
= data
;
4942 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4943 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4944 uint8_t cmd
= genl
->cmd
;
4946 if (!cmd_filter_check(dl
, cmd
))
4950 case DEVLINK_CMD_GET
: /* fall through */
4951 case DEVLINK_CMD_SET
: /* fall through */
4952 case DEVLINK_CMD_NEW
: /* fall through */
4953 case DEVLINK_CMD_DEL
:
4954 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4955 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4956 return MNL_CB_ERROR
;
4957 pr_out_mon_header(genl
->cmd
);
4960 pr_out_mon_footer();
4962 case DEVLINK_CMD_PORT_GET
: /* fall through */
4963 case DEVLINK_CMD_PORT_SET
: /* fall through */
4964 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4965 case DEVLINK_CMD_PORT_DEL
:
4966 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4967 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4968 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4969 return MNL_CB_ERROR
;
4970 pr_out_mon_header(genl
->cmd
);
4971 pr_out_port(dl
, tb
);
4972 pr_out_mon_footer();
4974 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4975 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4976 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4977 case DEVLINK_CMD_PARAM_DEL
:
4978 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4979 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4980 !tb
[DEVLINK_ATTR_PARAM
])
4981 return MNL_CB_ERROR
;
4982 pr_out_mon_header(genl
->cmd
);
4983 pr_out_param(dl
, tb
, false);
4984 pr_out_mon_footer();
4986 case DEVLINK_CMD_REGION_GET
: /* fall through */
4987 case DEVLINK_CMD_REGION_SET
: /* fall through */
4988 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4989 case DEVLINK_CMD_REGION_DEL
:
4990 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4991 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4992 !tb
[DEVLINK_ATTR_REGION_NAME
])
4993 return MNL_CB_ERROR
;
4994 pr_out_mon_header(genl
->cmd
);
4995 pr_out_region(dl
, tb
);
4996 pr_out_mon_footer();
4998 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4999 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
5000 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
5001 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5002 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
5003 return MNL_CB_ERROR
;
5004 pr_out_mon_header(genl
->cmd
);
5005 pr_out_flash_update(dl
, tb
);
5006 pr_out_mon_footer();
5008 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
5009 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5010 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5011 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
5012 return MNL_CB_ERROR
;
5013 pr_out_mon_header(genl
->cmd
);
5014 pr_out_health(dl
, tb
, true, true);
5015 pr_out_mon_footer();
5017 case DEVLINK_CMD_TRAP_GET
: /* fall through */
5018 case DEVLINK_CMD_TRAP_SET
: /* fall through */
5019 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
5020 case DEVLINK_CMD_TRAP_DEL
:
5021 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5022 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5023 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
5024 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
5025 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
5026 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
5027 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
5028 !tb
[DEVLINK_ATTR_STATS
])
5029 return MNL_CB_ERROR
;
5030 pr_out_mon_header(genl
->cmd
);
5031 pr_out_trap(dl
, tb
, false);
5032 pr_out_mon_footer();
5034 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
5035 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
5036 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
5037 case DEVLINK_CMD_TRAP_GROUP_DEL
:
5038 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5039 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5040 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
5041 !tb
[DEVLINK_ATTR_STATS
])
5042 return MNL_CB_ERROR
;
5043 pr_out_mon_header(genl
->cmd
);
5044 pr_out_trap_group(dl
, tb
, false);
5045 pr_out_mon_footer();
5047 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
5048 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
5049 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
5050 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
5051 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5052 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5053 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
5054 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
5055 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
5056 return MNL_CB_ERROR
;
5057 pr_out_mon_header(genl
->cmd
);
5058 pr_out_trap_policer(dl
, tb
, false);
5065 static int cmd_mon_show(struct dl
*dl
)
5068 unsigned int index
= 0;
5069 const char *cur_obj
;
5071 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
5072 if (strcmp(cur_obj
, "all") != 0 &&
5073 strcmp(cur_obj
, "dev") != 0 &&
5074 strcmp(cur_obj
, "port") != 0 &&
5075 strcmp(cur_obj
, "health") != 0 &&
5076 strcmp(cur_obj
, "trap") != 0 &&
5077 strcmp(cur_obj
, "trap-group") != 0 &&
5078 strcmp(cur_obj
, "trap-policer") != 0) {
5079 pr_err("Unknown object \"%s\"\n", cur_obj
);
5083 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
5086 open_json_object(NULL
);
5087 open_json_array(PRINT_JSON
, "mon");
5088 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
5089 close_json_array(PRINT_JSON
, NULL
);
5090 close_json_object();
5096 static void cmd_mon_help(void)
5098 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
5099 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
5102 static int cmd_mon(struct dl
*dl
)
5104 if (dl_argv_match(dl
, "help")) {
5108 return cmd_mon_show(dl
);
5111 struct dpipe_field
{
5114 unsigned int bitwidth
;
5115 enum devlink_dpipe_field_mapping_type mapping_type
;
5118 struct dpipe_header
{
5119 struct list_head list
;
5122 struct dpipe_field
*fields
;
5123 unsigned int fields_count
;
5126 struct dpipe_table
{
5127 struct list_head list
;
5129 unsigned int resource_id
;
5130 bool resource_valid
;
5133 struct dpipe_tables
{
5134 struct list_head table_list
;
5144 enum devlink_resource_unit unit
;
5149 struct list_head list
;
5150 struct list_head resource_list
;
5151 struct resource
*parent
;
5155 struct list_head resource_list
;
5158 struct resource_ctx
{
5161 struct resources
*resources
;
5162 struct dpipe_tables
*tables
;
5163 bool print_resources
;
5164 bool pending_change
;
5167 static struct resource
*resource_alloc(void)
5169 struct resource
*resource
;
5171 resource
= calloc(1, sizeof(struct resource
));
5174 INIT_LIST_HEAD(&resource
->resource_list
);
5178 static void resource_free(struct resource
*resource
)
5180 struct resource
*child_resource
, *tmp
;
5182 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
5184 free(child_resource
->name
);
5185 resource_free(child_resource
);
5190 static struct resources
*resources_alloc(void)
5192 struct resources
*resources
;
5194 resources
= calloc(1, sizeof(struct resources
));
5197 INIT_LIST_HEAD(&resources
->resource_list
);
5201 static void resources_free(struct resources
*resources
)
5203 struct resource
*resource
, *tmp
;
5205 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
5206 resource_free(resource
);
5209 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
5211 ctx
->resources
= resources_alloc();
5212 if (!ctx
->resources
)
5218 static void resource_ctx_fini(struct resource_ctx
*ctx
)
5220 resources_free(ctx
->resources
);
5226 struct list_head global_headers
;
5227 struct list_head local_headers
;
5228 struct dpipe_tables
*tables
;
5229 struct resources
*resources
;
5234 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
5236 struct dpipe_header
*header
;
5238 header
= calloc(1, sizeof(struct dpipe_header
));
5241 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
5242 if (!header
->fields
)
5243 goto err_fields_alloc
;
5244 header
->fields_count
= fields_count
;
5252 static void dpipe_header_free(struct dpipe_header
*header
)
5254 free(header
->fields
);
5258 static void dpipe_header_clear(struct dpipe_header
*header
)
5260 struct dpipe_field
*field
;
5263 for (i
= 0; i
< header
->fields_count
; i
++) {
5264 field
= &header
->fields
[i
];
5270 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
5271 struct dpipe_header
*header
, bool global
)
5274 list_add(&header
->list
, &ctx
->global_headers
);
5276 list_add(&header
->list
, &ctx
->local_headers
);
5279 static void dpipe_header_del(struct dpipe_header
*header
)
5281 list_del(&header
->list
);
5284 static struct dpipe_table
*dpipe_table_alloc(void)
5286 return calloc(1, sizeof(struct dpipe_table
));
5289 static void dpipe_table_free(struct dpipe_table
*table
)
5294 static struct dpipe_tables
*dpipe_tables_alloc(void)
5296 struct dpipe_tables
*tables
;
5298 tables
= calloc(1, sizeof(struct dpipe_tables
));
5301 INIT_LIST_HEAD(&tables
->table_list
);
5305 static void dpipe_tables_free(struct dpipe_tables
*tables
)
5307 struct dpipe_table
*table
, *tmp
;
5309 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
5310 dpipe_table_free(table
);
5314 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
5316 ctx
->tables
= dpipe_tables_alloc();
5321 INIT_LIST_HEAD(&ctx
->global_headers
);
5322 INIT_LIST_HEAD(&ctx
->local_headers
);
5326 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
5328 struct dpipe_header
*header
, *tmp
;
5330 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
5332 dpipe_header_del(header
);
5333 dpipe_header_clear(header
);
5334 dpipe_header_free(header
);
5336 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
5338 dpipe_header_del(header
);
5339 dpipe_header_clear(header
);
5340 dpipe_header_free(header
);
5342 dpipe_tables_free(ctx
->tables
);
5345 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
5346 uint32_t header_id
, bool global
)
5348 struct list_head
*header_list
;
5349 struct dpipe_header
*header
;
5352 header_list
= &ctx
->global_headers
;
5354 header_list
= &ctx
->local_headers
;
5355 list_for_each_entry(header
, header_list
, list
) {
5356 if (header
->id
!= header_id
)
5358 return header
->name
;
5363 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
5365 uint32_t field_id
, bool global
)
5367 struct list_head
*header_list
;
5368 struct dpipe_header
*header
;
5371 header_list
= &ctx
->global_headers
;
5373 header_list
= &ctx
->local_headers
;
5374 list_for_each_entry(header
, header_list
, list
) {
5375 if (header
->id
!= header_id
)
5377 return header
->fields
[field_id
].name
;
5383 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
5385 switch (mapping_type
) {
5386 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
5388 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
5396 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
5397 uint32_t field_id
, bool global
)
5399 enum devlink_dpipe_field_mapping_type mapping_type
;
5400 struct list_head
*header_list
;
5401 struct dpipe_header
*header
;
5404 header_list
= &ctx
->global_headers
;
5406 header_list
= &ctx
->local_headers
;
5407 list_for_each_entry(header
, header_list
, list
) {
5408 if (header
->id
!= header_id
)
5410 mapping_type
= header
->fields
[field_id
].mapping_type
;
5411 return dpipe_field_mapping_e2s(mapping_type
);
5416 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
5417 struct dpipe_field
*fields
,
5418 unsigned int field_count
)
5420 struct dpipe_field
*field
;
5423 for (i
= 0; i
< field_count
; i
++) {
5425 pr_out_entry_start(ctx
->dl
);
5426 check_indent_newline(ctx
->dl
);
5427 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
5428 if (ctx
->dl
->verbose
)
5429 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
5430 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
5431 if (field
->mapping_type
) {
5432 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
5433 dpipe_field_mapping_e2s(field
->mapping_type
));
5435 pr_out_entry_end(ctx
->dl
);
5440 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
5441 struct dpipe_header
*header
, bool global
)
5443 pr_out_handle_start_arr(ctx
->dl
, tb
);
5444 check_indent_newline(ctx
->dl
);
5445 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
5446 if (ctx
->dl
->verbose
) {
5447 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
5448 print_bool(PRINT_ANY
, "global", " global %s", global
);
5450 pr_out_array_start(ctx
->dl
, "field");
5451 pr_out_dpipe_fields(ctx
, header
->fields
,
5452 header
->fields_count
);
5453 pr_out_array_end(ctx
->dl
);
5454 pr_out_handle_end(ctx
->dl
);
5457 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
5460 struct dpipe_header
*header
;
5462 list_for_each_entry(header
, &ctx
->local_headers
, list
)
5463 pr_out_dpipe_header(ctx
, tb
, header
, false);
5465 list_for_each_entry(header
, &ctx
->global_headers
, list
)
5466 pr_out_dpipe_header(ctx
, tb
, header
, true);
5469 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
5471 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5475 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
5476 if (err
!= MNL_CB_OK
)
5478 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
5479 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
5480 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
5481 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
5484 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
5485 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5486 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
5487 field
->name
= strdup(name
);
5490 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
5494 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
5495 struct dpipe_field
*fields
)
5497 struct nlattr
*nla_field
;
5501 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
5502 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
5510 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
5512 struct nlattr
*nla_field
;
5513 unsigned int count
= 0;
5515 mnl_attr_for_each_nested(nla_field
, nla_fields
)
5520 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5522 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
5523 struct dpipe_header
*header
;
5524 unsigned int fields_count
;
5525 const char *header_name
;
5529 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
5530 if (err
!= MNL_CB_OK
)
5533 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
5534 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5535 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
5538 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
5539 header
= dpipe_header_alloc(fields_count
);
5543 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
5544 header
->name
= strdup(header_name
);
5545 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5546 header
->fields_count
= fields_count
;
5547 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5549 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
5553 dpipe_header_add(ctx
, header
, global
);
5557 dpipe_header_free(header
);
5561 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5563 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
5564 struct nlattr
*nla_header
;
5567 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
5568 err
= dpipe_header_get(ctx
, nla_header
);
5575 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
5577 struct dpipe_ctx
*ctx
= data
;
5578 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5579 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5582 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5583 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5584 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
5585 return MNL_CB_ERROR
;
5586 err
= dpipe_headers_get(ctx
, tb
);
5589 return MNL_CB_ERROR
;
5592 if (ctx
->print_headers
)
5593 pr_out_dpipe_headers(ctx
, tb
);
5597 static int cmd_dpipe_headers_show(struct dl
*dl
)
5599 struct nlmsghdr
*nlh
;
5600 struct dpipe_ctx ctx
= {};
5601 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5604 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5606 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5610 err
= dpipe_ctx_init(&ctx
, dl
);
5614 ctx
.print_headers
= true;
5616 pr_out_section_start(dl
, "header");
5617 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5619 pr_err("error get headers %s\n", strerror(ctx
.err
));
5620 pr_out_section_end(dl
);
5622 dpipe_ctx_fini(&ctx
);
5626 static void cmd_dpipe_help(void)
5628 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5629 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5630 pr_err(" [ counters_enabled { true | false } ]\n");
5631 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5632 pr_err(" devlink dpipe header show DEV\n");
5635 static int cmd_dpipe_header(struct dl
*dl
)
5637 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5640 } else if (dl_argv_match(dl
, "show")) {
5642 return cmd_dpipe_headers_show(dl
);
5644 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5649 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5651 switch (action_type
) {
5652 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5653 return "field_modify";
5659 struct dpipe_op_info
{
5665 struct dpipe_action
{
5666 struct dpipe_op_info info
;
5670 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5671 struct dpipe_ctx
*ctx
)
5673 struct dpipe_op_info
*op_info
= &action
->info
;
5674 const char *mapping
;
5676 check_indent_newline(ctx
->dl
);
5677 print_string(PRINT_ANY
, "type", "type %s",
5678 dpipe_action_type_e2s(action
->type
));
5679 print_string(PRINT_ANY
, "header", " header %s",
5680 dpipe_header_id2s(ctx
, op_info
->header_id
,
5681 op_info
->header_global
));
5682 print_string(PRINT_ANY
, "field", " field %s",
5683 dpipe_field_id2s(ctx
, op_info
->header_id
,
5685 op_info
->header_global
));
5686 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5688 op_info
->header_global
);
5690 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5693 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5695 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5698 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5699 if (err
!= MNL_CB_OK
)
5702 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5703 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5704 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5705 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5709 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5710 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5711 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5712 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5717 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5718 struct nlattr
*nla_actions
)
5720 struct nlattr
*nla_action
;
5721 struct dpipe_action action
;
5723 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5724 pr_out_entry_start(ctx
->dl
);
5725 if (dpipe_action_parse(&action
, nla_action
))
5726 goto err_action_parse
;
5727 pr_out_dpipe_action(&action
, ctx
);
5728 pr_out_entry_end(ctx
->dl
);
5733 pr_out_entry_end(ctx
->dl
);
5738 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5740 switch (match_type
) {
5741 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5742 return "field_exact";
5748 struct dpipe_match
{
5749 struct dpipe_op_info info
;
5753 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5754 struct dpipe_ctx
*ctx
)
5756 struct dpipe_op_info
*op_info
= &match
->info
;
5757 const char *mapping
;
5759 check_indent_newline(ctx
->dl
);
5760 print_string(PRINT_ANY
, "type", "type %s",
5761 dpipe_match_type_e2s(match
->type
));
5762 print_string(PRINT_ANY
, "header", " header %s",
5763 dpipe_header_id2s(ctx
, op_info
->header_id
,
5764 op_info
->header_global
));
5765 print_string(PRINT_ANY
, "field", " field %s",
5766 dpipe_field_id2s(ctx
, op_info
->header_id
,
5768 op_info
->header_global
));
5769 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5771 op_info
->header_global
);
5773 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5776 static int dpipe_match_parse(struct dpipe_match
*match
,
5780 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5783 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5784 if (err
!= MNL_CB_OK
)
5787 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5788 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5789 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5790 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5794 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5795 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5796 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5797 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5802 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5803 struct nlattr
*nla_matches
)
5805 struct nlattr
*nla_match
;
5806 struct dpipe_match match
;
5808 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5809 pr_out_entry_start(ctx
->dl
);
5810 if (dpipe_match_parse(&match
, nla_match
))
5811 goto err_match_parse
;
5812 pr_out_dpipe_match(&match
, ctx
);
5813 pr_out_entry_end(ctx
->dl
);
5818 pr_out_entry_end(ctx
->dl
);
5822 static struct resource
*
5823 resource_find(struct resources
*resources
, struct resource
*resource
,
5824 uint64_t resource_id
)
5826 struct list_head
*list_head
;
5829 list_head
= &resources
->resource_list
;
5831 list_head
= &resource
->resource_list
;
5833 list_for_each_entry(resource
, list_head
, list
) {
5834 struct resource
*child_resource
;
5836 if (resource
->id
== resource_id
)
5839 child_resource
= resource_find(resources
, resource
,
5842 return child_resource
;
5848 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5849 uint64_t resource_id
)
5851 struct resource
*resource
, *parent_resource
;
5852 const char del
[] = "/";
5856 resource
= resource_find(resources
, NULL
, resource_id
);
5860 for (parent_resource
= resource
; parent_resource
;
5861 parent_resource
= parent_resource
->parent
)
5862 path_len
+= strlen(parent_resource
->name
) + 1;
5865 path
= calloc(1, path_len
);
5869 path
+= path_len
- 1;
5870 for (parent_resource
= resource
; parent_resource
;
5871 parent_resource
= parent_resource
->parent
) {
5872 path
-= strlen(parent_resource
->name
);
5873 memcpy(path
, parent_resource
->name
,
5874 strlen(parent_resource
->name
));
5875 path
-= strlen(del
);
5876 memcpy(path
, del
, strlen(del
));
5878 check_indent_newline(dl
);
5879 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5883 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5885 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5886 struct dpipe_table
*table
;
5887 uint32_t resource_units
;
5888 bool counters_enabled
;
5889 bool resource_valid
;
5893 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5894 if (err
!= MNL_CB_OK
)
5897 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5898 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5899 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5900 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5901 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5905 table
= dpipe_table_alloc();
5909 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5910 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5911 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5913 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5915 if (resource_valid
) {
5916 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5917 table
->resource_valid
= true;
5920 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5921 if (!ctx
->print_tables
)
5924 check_indent_newline(ctx
->dl
);
5925 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5926 print_uint(PRINT_ANY
, "size", " size %u", size
);
5927 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5929 if (resource_valid
) {
5930 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5931 resource_path_print(ctx
->dl
, ctx
->resources
,
5932 table
->resource_id
);
5933 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5937 pr_out_array_start(ctx
->dl
, "match");
5938 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5939 goto err_matches_show
;
5940 pr_out_array_end(ctx
->dl
);
5942 pr_out_array_start(ctx
->dl
, "action");
5943 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5944 goto err_actions_show
;
5945 pr_out_array_end(ctx
->dl
);
5951 pr_out_array_end(ctx
->dl
);
5955 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5957 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5958 struct nlattr
*nla_table
;
5960 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5961 if (ctx
->print_tables
)
5962 pr_out_handle_start_arr(ctx
->dl
, tb
);
5963 if (dpipe_table_show(ctx
, nla_table
))
5964 goto err_table_show
;
5965 if (ctx
->print_tables
)
5966 pr_out_handle_end(ctx
->dl
);
5971 if (ctx
->print_tables
)
5972 pr_out_handle_end(ctx
->dl
);
5976 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5978 struct dpipe_ctx
*ctx
= data
;
5979 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5980 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5982 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5983 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5984 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5985 return MNL_CB_ERROR
;
5987 if (dpipe_tables_show(ctx
, tb
))
5988 return MNL_CB_ERROR
;
5992 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5994 static int cmd_dpipe_table_show(struct dl
*dl
)
5996 struct nlmsghdr
*nlh
;
5997 struct dpipe_ctx dpipe_ctx
= {};
5998 struct resource_ctx resource_ctx
= {};
5999 uint16_t flags
= NLM_F_REQUEST
;
6002 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
6006 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
6008 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6012 dpipe_ctx
.print_tables
= true;
6014 dl_opts_put(nlh
, dl
);
6015 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
6018 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
6019 goto err_headers_get
;
6022 err
= resource_ctx_init(&resource_ctx
, dl
);
6024 goto err_resource_ctx_init
;
6026 resource_ctx
.print_resources
= false;
6027 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
6028 dl_opts_put(nlh
, dl
);
6029 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6032 dpipe_ctx
.resources
= resource_ctx
.resources
;
6034 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6035 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
6036 dl_opts_put(nlh
, dl
);
6038 pr_out_section_start(dl
, "table");
6039 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
6040 pr_out_section_end(dl
);
6042 resource_ctx_fini(&resource_ctx
);
6043 dpipe_ctx_fini(&dpipe_ctx
);
6046 err_resource_ctx_init
:
6048 dpipe_ctx_fini(&dpipe_ctx
);
6052 static int cmd_dpipe_table_set(struct dl
*dl
)
6054 struct nlmsghdr
*nlh
;
6057 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
6058 NLM_F_REQUEST
| NLM_F_ACK
);
6060 err
= dl_argv_parse_put(nlh
, dl
,
6061 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
6062 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
6066 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6069 enum dpipe_value_type
{
6070 DPIPE_VALUE_TYPE_VALUE
,
6071 DPIPE_VALUE_TYPE_MASK
,
6075 dpipe_value_type_e2s(enum dpipe_value_type type
)
6078 case DPIPE_VALUE_TYPE_VALUE
:
6080 case DPIPE_VALUE_TYPE_MASK
:
6081 return "value_mask";
6087 struct dpipe_field_printer
{
6088 unsigned int field_id
;
6089 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
6092 struct dpipe_header_printer
{
6093 struct dpipe_field_printer
*printers
;
6094 unsigned int printers_count
;
6095 unsigned int header_id
;
6098 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
6099 enum dpipe_value_type type
,
6102 struct in_addr ip_addr
;
6104 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
6105 check_indent_newline(ctx
->dl
);
6106 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
6110 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
6111 enum dpipe_value_type type
,
6114 check_indent_newline(ctx
->dl
);
6115 print_string_name_value(dpipe_value_type_e2s(type
),
6116 ether_ntoa((struct ether_addr
*)value
));
6119 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
6120 enum dpipe_value_type type
,
6123 char str
[INET6_ADDRSTRLEN
];
6125 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
6126 check_indent_newline(ctx
->dl
);
6127 print_string_name_value(dpipe_value_type_e2s(type
), str
);
6130 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
6132 .printer
= dpipe_field_printer_ipv4_addr
,
6133 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
6137 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
6138 .printers
= dpipe_field_printers_ipv4
,
6139 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
6140 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
6143 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
6145 .printer
= dpipe_field_printer_ethernet_addr
,
6146 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
6150 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
6151 .printers
= dpipe_field_printers_ethernet
,
6152 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
6153 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
6156 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
6158 .printer
= dpipe_field_printer_ipv6_addr
,
6159 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
6163 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
6164 .printers
= dpipe_field_printers_ipv6
,
6165 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
6166 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
6169 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
6170 &dpipe_header_printer_ipv4
,
6171 &dpipe_header_printer_ethernet
,
6172 &dpipe_header_printer_ipv6
,
6175 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
6176 struct dpipe_op_info
*info
,
6177 enum dpipe_value_type type
,
6180 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
6181 struct dpipe_header_printer
*header_printer
;
6182 struct dpipe_field_printer
*field_printer
;
6183 unsigned int field_printers_count
;
6187 for (i
= 0; i
< header_printers_count
; i
++) {
6188 header_printer
= dpipe_header_printers
[i
];
6189 if (header_printer
->header_id
!= info
->header_id
)
6191 field_printers_count
= header_printer
->printers_count
;
6192 for (j
= 0; j
< field_printers_count
; j
++) {
6193 field_printer
= &header_printer
->printers
[j
];
6194 if (field_printer
->field_id
!= info
->field_id
)
6196 field_printer
->printer(ctx
, type
, value
);
6204 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
6206 unsigned int value_len
,
6207 struct dpipe_op_info
*info
,
6208 enum dpipe_value_type type
)
6210 if (info
->header_global
&&
6211 !dpipe_print_prot_header(ctx
, info
, type
, value
))
6214 if (value_len
== sizeof(uint32_t)) {
6215 uint32_t *value_32
= value
;
6217 check_indent_newline(ctx
->dl
);
6218 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
6222 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
6223 struct nlattr
**nla_match_value
,
6224 struct dpipe_op_info
*info
)
6226 void *value
, *value_mask
;
6227 uint32_t value_mapping
;
6231 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
6232 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
6234 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
6235 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
6238 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
6239 check_indent_newline(ctx
->dl
);
6240 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
6244 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
6245 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
6246 DPIPE_VALUE_TYPE_MASK
);
6249 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
6252 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
6255 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
6256 struct dpipe_match match
;
6259 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
6260 if (err
!= MNL_CB_OK
)
6263 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
6264 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
6268 pr_out_entry_start(ctx
->dl
);
6269 if (dpipe_match_parse(&match
,
6270 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
6271 goto err_match_parse
;
6272 pr_out_dpipe_match(&match
, ctx
);
6273 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
6274 pr_out_entry_end(ctx
->dl
);
6279 pr_out_entry_end(ctx
->dl
);
6283 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
6286 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
6287 struct dpipe_action action
;
6290 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
6291 if (err
!= MNL_CB_OK
)
6294 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
6295 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
6299 pr_out_entry_start(ctx
->dl
);
6300 if (dpipe_action_parse(&action
,
6301 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
6302 goto err_action_parse
;
6303 pr_out_dpipe_action(&action
, ctx
);
6304 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
6305 pr_out_entry_end(ctx
->dl
);
6310 pr_out_entry_end(ctx
->dl
);
6315 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
6316 struct nlattr
*nla_action_values
)
6318 struct nlattr
*nla_action_value
;
6320 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
6321 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
6328 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
6329 struct nlattr
*nla_match_values
)
6331 struct nlattr
*nla_match_value
;
6333 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
6334 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
6340 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
6342 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
6343 uint32_t entry_index
;
6347 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
6348 if (err
!= MNL_CB_OK
)
6351 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
6352 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
6353 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
6357 check_indent_newline(ctx
->dl
);
6358 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
6359 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
6361 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
6362 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
6363 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
6366 pr_out_array_start(ctx
->dl
, "match_value");
6367 if (dpipe_tables_match_values_show(ctx
,
6368 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
6369 goto err_match_values_show
;
6370 pr_out_array_end(ctx
->dl
);
6372 pr_out_array_start(ctx
->dl
, "action_value");
6373 if (dpipe_tables_action_values_show(ctx
,
6374 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
6375 goto err_action_values_show
;
6376 pr_out_array_end(ctx
->dl
);
6379 err_action_values_show
:
6380 err_match_values_show
:
6381 pr_out_array_end(ctx
->dl
);
6385 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
6387 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
6388 struct nlattr
*nla_entry
;
6390 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
6391 pr_out_handle_start_arr(ctx
->dl
, tb
);
6392 if (dpipe_entry_show(ctx
, nla_entry
))
6393 goto err_entry_show
;
6394 pr_out_handle_end(ctx
->dl
);
6399 pr_out_handle_end(ctx
->dl
);
6403 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6405 struct dpipe_ctx
*ctx
= data
;
6406 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6407 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6409 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6410 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6411 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
6412 return MNL_CB_ERROR
;
6414 if (dpipe_table_entries_show(ctx
, tb
))
6415 return MNL_CB_ERROR
;
6419 static int cmd_dpipe_table_dump(struct dl
*dl
)
6421 struct nlmsghdr
*nlh
;
6422 struct dpipe_ctx ctx
= {};
6423 uint16_t flags
= NLM_F_REQUEST
;
6426 err
= dpipe_ctx_init(&ctx
, dl
);
6430 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
6434 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
6435 dl_opts_put(nlh
, dl
);
6436 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
6438 pr_err("error get headers %s\n", strerror(ctx
.err
));
6442 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6443 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
6444 dl_opts_put(nlh
, dl
);
6446 pr_out_section_start(dl
, "table_entry");
6447 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
6448 pr_out_section_end(dl
);
6450 dpipe_ctx_fini(&ctx
);
6454 static int cmd_dpipe_table(struct dl
*dl
)
6456 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6459 } else if (dl_argv_match(dl
, "show")) {
6461 return cmd_dpipe_table_show(dl
);
6462 } else if (dl_argv_match(dl
, "set")) {
6464 return cmd_dpipe_table_set(dl
);
6465 } else if (dl_argv_match(dl
, "dump")) {
6467 return cmd_dpipe_table_dump(dl
);
6469 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6473 static int cmd_dpipe(struct dl
*dl
)
6475 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6478 } else if (dl_argv_match(dl
, "header")) {
6480 return cmd_dpipe_header(dl
);
6481 } else if (dl_argv_match(dl
, "table")) {
6483 return cmd_dpipe_table(dl
);
6485 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6490 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
6491 struct nlattr
**nla_resource
)
6493 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
6494 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
6495 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
6496 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
6497 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
6498 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
6499 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
6503 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
6504 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
6505 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
6506 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
6507 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
6508 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
6509 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
6511 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
6512 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
6514 resource
->size_new
= resource
->size
;
6516 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
6517 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
6518 resource
->occ_valid
= true;
6521 if (resource
->size_new
!= resource
->size
)
6522 ctx
->pending_change
= true;
6528 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
6529 struct resource
*parent_resource
, struct nlattr
*nl
)
6531 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
6532 struct nlattr
*nla_child_resource
;
6533 struct nlattr
*nla_resources
;
6543 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
6544 if (err
!= MNL_CB_OK
)
6547 err
= resource_parse(ctx
, resource
, nla_resource
);
6551 resource
->parent
= parent_resource
;
6552 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
6555 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
6556 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
6558 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
6559 struct resource
*child_resource
;
6560 struct list_head
*list
;
6562 child_resource
= resource_alloc();
6563 if (!child_resource
)
6567 list
= &ctx
->resources
->resource_list
;
6569 list
= &resource
->resource_list
;
6571 list_add_tail(&child_resource
->list
, list
);
6572 err
= resource_get(ctx
, child_resource
, resource
,
6573 nla_child_resource
);
6581 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
6584 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
6585 default: return "<unknown unit>";
6589 static void resource_show(struct resource
*resource
,
6590 struct resource_ctx
*ctx
)
6592 struct resource
*child_resource
;
6593 struct dpipe_table
*table
;
6594 struct dl
*dl
= ctx
->dl
;
6597 check_indent_newline(dl
);
6598 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6600 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6601 pr_out_u64(dl
, "size", resource
->size
);
6602 if (resource
->size
!= resource
->size_new
)
6603 pr_out_u64(dl
, "size_new", resource
->size_new
);
6604 if (resource
->occ_valid
)
6605 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6606 print_string(PRINT_ANY
, "unit", " unit %s",
6607 resource_unit_str_get(resource
->unit
));
6609 if (resource
->size_min
!= resource
->size_max
) {
6610 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6611 resource
->size_min
);
6612 pr_out_u64(dl
, "size_max", resource
->size_max
);
6613 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6614 resource
->size_gran
);
6617 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6618 if (table
->resource_id
== resource
->id
&&
6619 table
->resource_valid
)
6623 pr_out_array_start(dl
, "dpipe_tables");
6625 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6628 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6629 if (table
->resource_id
!= resource
->id
||
6630 !table
->resource_valid
)
6632 pr_out_entry_start(dl
);
6633 check_indent_newline(dl
);
6634 print_string(PRINT_ANY
, "table_name", "table_name %s",
6636 pr_out_entry_end(dl
);
6639 pr_out_array_end(dl
);
6641 if (list_empty(&resource
->resource_list
))
6644 if (ctx
->pending_change
) {
6645 check_indent_newline(dl
);
6646 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6647 resource
->size_valid
? "true" : "false");
6649 pr_out_array_start(dl
, "resources");
6650 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6651 pr_out_entry_start(dl
);
6652 resource_show(child_resource
, ctx
);
6653 pr_out_entry_end(dl
);
6655 pr_out_array_end(dl
);
6659 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6661 struct resources
*resources
= ctx
->resources
;
6662 struct resource
*resource
;
6664 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6665 pr_out_handle_start_arr(ctx
->dl
, tb
);
6666 resource_show(resource
, ctx
);
6667 pr_out_handle_end(ctx
->dl
);
6671 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6673 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6676 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6678 struct resource_ctx
*ctx
= data
;
6679 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6680 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6683 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6684 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6685 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6686 return MNL_CB_ERROR
;
6688 err
= resources_get(ctx
, tb
);
6691 return MNL_CB_ERROR
;
6694 if (ctx
->print_resources
)
6695 resources_show(ctx
, tb
);
6700 static int cmd_resource_show(struct dl
*dl
)
6702 struct nlmsghdr
*nlh
;
6703 struct dpipe_ctx dpipe_ctx
= {};
6704 struct resource_ctx resource_ctx
= {};
6707 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6711 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6713 dl_opts_put(nlh
, dl
);
6715 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6719 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6722 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6726 err
= resource_ctx_init(&resource_ctx
, dl
);
6730 resource_ctx
.print_resources
= true;
6731 resource_ctx
.tables
= dpipe_ctx
.tables
;
6732 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6733 NLM_F_REQUEST
| NLM_F_ACK
);
6734 dl_opts_put(nlh
, dl
);
6735 pr_out_section_start(dl
, "resources");
6736 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6738 pr_out_section_end(dl
);
6739 resource_ctx_fini(&resource_ctx
);
6741 dpipe_ctx_fini(&dpipe_ctx
);
6745 static void cmd_resource_help(void)
6747 pr_err("Usage: devlink resource show DEV\n"
6748 " devlink resource set DEV path PATH size SIZE\n");
6751 static struct resource
*
6752 resource_find_by_name(struct list_head
*list
, char *name
)
6754 struct resource
*resource
;
6756 list_for_each_entry(resource
, list
, list
) {
6757 if (!strcmp(resource
->name
, name
))
6764 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6765 uint32_t *p_resource_id
, bool *p_resource_valid
)
6767 struct resource
*resource
;
6768 uint32_t resource_id
= 0;
6769 char *resource_path_dup
;
6770 struct list_head
*list
;
6771 const char del
[] = "/";
6772 char *resource_name
;
6774 resource_path_dup
= strdup(resource_path
);
6775 list
= &ctx
->resources
->resource_list
;
6776 resource_name
= strtok(resource_path_dup
, del
);
6777 while (resource_name
!= NULL
) {
6778 resource
= resource_find_by_name(list
, resource_name
);
6780 goto err_resource_lookup
;
6782 list
= &resource
->resource_list
;
6783 resource_name
= strtok(NULL
, del
);
6784 resource_id
= resource
->id
;
6786 free(resource_path_dup
);
6787 *p_resource_valid
= true;
6788 *p_resource_id
= resource_id
;
6791 err_resource_lookup
:
6792 free(resource_path_dup
);
6796 static int cmd_resource_set(struct dl
*dl
)
6798 struct nlmsghdr
*nlh
;
6799 struct resource_ctx ctx
= {};
6802 err
= resource_ctx_init(&ctx
, dl
);
6806 ctx
.print_resources
= false;
6807 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6808 DL_OPT_RESOURCE_SIZE
, 0);
6812 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6814 dl_opts_put(nlh
, dl
);
6815 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6817 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6821 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6822 &dl
->opts
.resource_id
,
6823 &dl
->opts
.resource_id_valid
);
6825 pr_err("error parsing resource path %s\n", strerror(-err
));
6829 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6830 NLM_F_REQUEST
| NLM_F_ACK
);
6832 dl_opts_put(nlh
, dl
);
6833 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6835 resource_ctx_fini(&ctx
);
6839 static int cmd_resource(struct dl
*dl
)
6841 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6842 cmd_resource_help();
6844 } else if (dl_argv_match(dl
, "show")) {
6846 return cmd_resource_show(dl
);
6847 } else if (dl_argv_match(dl
, "set")) {
6849 return cmd_resource_set(dl
);
6851 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6855 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6857 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6858 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6859 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6862 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6863 if (dl
->json_output
)
6864 open_json_object(buf
);
6869 static void pr_out_region_handle_end(struct dl
*dl
)
6871 if (dl
->json_output
)
6872 close_json_object();
6877 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6879 __pr_out_indent_newline(dl
);
6880 if (dl
->json_output
)
6881 open_json_array(PRINT_JSON
, "snapshot");
6883 pr_out("snapshot %s", array
? "[" : "");
6886 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6888 if (dl
->json_output
)
6889 close_json_array(PRINT_JSON
, NULL
);
6894 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6896 uint32_t snapshot_id
;
6898 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6901 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6903 if (dl
->json_output
)
6904 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6906 pr_out("%s%u", index
? " " : "", snapshot_id
);
6909 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6911 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6912 struct nlattr
*nla_sanpshot
;
6915 pr_out_region_snapshots_start(dl
, true);
6916 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6917 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6918 if (err
!= MNL_CB_OK
)
6920 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6922 pr_out_region_snapshots_end(dl
, true);
6925 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6927 pr_out_region_snapshots_start(dl
, false);
6928 pr_out_region_snapshots_id(dl
, tb
, 0);
6929 pr_out_region_snapshots_end(dl
, false);
6932 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6934 pr_out_region_handle_start(dl
, tb
);
6936 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6937 pr_out_u64(dl
, "size",
6938 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6940 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6941 pr_out_snapshots(dl
, tb
);
6943 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6944 pr_out_snapshot(dl
, tb
);
6946 pr_out_region_handle_end(dl
);
6949 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6951 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6952 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6953 struct dl
*dl
= data
;
6955 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6956 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6957 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6958 return MNL_CB_ERROR
;
6960 pr_out_region(dl
, tb
);
6965 static int cmd_region_show(struct dl
*dl
)
6967 struct nlmsghdr
*nlh
;
6968 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6971 if (dl_argc(dl
) == 0)
6972 flags
|= NLM_F_DUMP
;
6974 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6976 if (dl_argc(dl
) > 0) {
6977 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6982 pr_out_section_start(dl
, "regions");
6983 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6984 pr_out_section_end(dl
);
6988 static int cmd_region_snapshot_del(struct dl
*dl
)
6990 struct nlmsghdr
*nlh
;
6993 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6994 NLM_F_REQUEST
| NLM_F_ACK
);
6996 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6997 DL_OPT_REGION_SNAPSHOT_ID
, 0);
7001 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7004 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
7006 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
7007 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7008 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
7009 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7010 struct dl
*dl
= data
;
7013 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7014 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7015 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
7016 return MNL_CB_ERROR
;
7018 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
7019 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
7020 if (err
!= MNL_CB_OK
)
7021 return MNL_CB_ERROR
;
7023 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
7024 if (!nla_chunk_data
)
7027 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
7028 if (!nla_chunk_addr
)
7031 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
7032 mnl_attr_get_payload_len(nla_chunk_data
),
7033 mnl_attr_get_u64(nla_chunk_addr
));
7038 static int cmd_region_dump(struct dl
*dl
)
7040 struct nlmsghdr
*nlh
;
7043 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
7044 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
7046 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
7047 DL_OPT_REGION_SNAPSHOT_ID
, 0);
7051 pr_out_section_start(dl
, "dump");
7052 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
7053 pr_out_section_end(dl
);
7054 if (!dl
->json_output
)
7059 static int cmd_region_read(struct dl
*dl
)
7061 struct nlmsghdr
*nlh
;
7064 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
7065 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
7067 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
7068 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
7069 DL_OPT_REGION_SNAPSHOT_ID
, 0);
7073 pr_out_section_start(dl
, "read");
7074 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
7075 pr_out_section_end(dl
);
7076 if (!dl
->json_output
)
7081 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
7083 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7084 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7085 struct dl
*dl
= data
;
7087 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7088 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7089 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
7090 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
7091 return MNL_CB_ERROR
;
7093 pr_out_region(dl
, tb
);
7098 static int cmd_region_snapshot_new(struct dl
*dl
)
7100 struct nlmsghdr
*nlh
;
7103 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
7104 NLM_F_REQUEST
| NLM_F_ACK
);
7106 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
7107 DL_OPT_REGION_SNAPSHOT_ID
);
7111 pr_out_section_start(dl
, "regions");
7112 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
7113 pr_out_section_end(dl
);
7117 static void cmd_region_help(void)
7119 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
7120 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
7121 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
7122 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
7123 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
7126 static int cmd_region(struct dl
*dl
)
7128 if (dl_no_arg(dl
)) {
7129 return cmd_region_show(dl
);
7130 } else if (dl_argv_match(dl
, "help")) {
7133 } else if (dl_argv_match(dl
, "show")) {
7135 return cmd_region_show(dl
);
7136 } else if (dl_argv_match(dl
, "del")) {
7138 return cmd_region_snapshot_del(dl
);
7139 } else if (dl_argv_match(dl
, "dump")) {
7141 return cmd_region_dump(dl
);
7142 } else if (dl_argv_match(dl
, "read")) {
7144 return cmd_region_read(dl
);
7145 } else if (dl_argv_match(dl
, "new")) {
7147 return cmd_region_snapshot_new(dl
);
7149 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7153 static int cmd_health_set_params(struct dl
*dl
)
7155 struct nlmsghdr
*nlh
;
7158 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
7159 NLM_F_REQUEST
| NLM_F_ACK
);
7160 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
| DL_OPT_HEALTH_REPORTER_NAME
,
7161 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
7162 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
7163 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
7167 dl_opts_put(nlh
, dl
);
7168 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7171 static int cmd_health_dump_clear(struct dl
*dl
)
7173 struct nlmsghdr
*nlh
;
7176 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
7177 NLM_F_REQUEST
| NLM_F_ACK
);
7179 err
= dl_argv_parse_put(nlh
, dl
,
7180 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7181 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7185 dl_opts_put(nlh
, dl
);
7186 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7189 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
7194 check_indent_newline(dl
);
7197 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
7200 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
7203 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
7206 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
7209 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
7211 case MNL_TYPE_NUL_STRING
:
7212 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
7214 case MNL_TYPE_BINARY
:
7215 len
= mnl_attr_get_payload_len(nl_data
);
7216 data
= mnl_attr_get_payload(nl_data
);
7217 pr_out_binary_value(dl
, data
, len
);
7225 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
7230 pr_out_name(dl
, *name
);
7237 struct list_head list
;
7240 struct fmsg_cb_data
{
7244 struct list_head entry_list
;
7247 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
7248 uint8_t *attr_value
, bool insert
)
7250 struct nest_entry
*entry
;
7253 entry
= malloc(sizeof(struct nest_entry
));
7257 entry
->attr_type
= *attr_value
;
7258 list_add(&entry
->list
, &fmsg_data
->entry_list
);
7260 if (list_empty(&fmsg_data
->entry_list
))
7261 return MNL_CB_ERROR
;
7262 entry
= list_first_entry(&fmsg_data
->entry_list
,
7263 struct nest_entry
, list
);
7264 *attr_value
= entry
->attr_type
;
7265 list_del(&entry
->list
);
7271 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
7274 pr_out_fmsg_name(dl
, name
);
7276 __pr_out_indent_inc();
7279 static void pr_out_fmsg_group_end(struct dl
*dl
)
7282 __pr_out_indent_dec();
7285 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
7287 if (dl
->json_output
) {
7288 pr_out_fmsg_name(dl
, name
);
7289 open_json_object(NULL
);
7291 pr_out_fmsg_group_start(dl
, name
);
7295 static void pr_out_fmsg_end_object(struct dl
*dl
)
7297 if (dl
->json_output
)
7298 close_json_object();
7300 pr_out_fmsg_group_end(dl
);
7303 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
7305 if (dl
->json_output
) {
7306 pr_out_fmsg_name(dl
, name
);
7307 open_json_array(PRINT_JSON
, NULL
);
7309 pr_out_fmsg_group_start(dl
, name
);
7313 static void pr_out_fmsg_end_array(struct dl
*dl
)
7315 if (dl
->json_output
)
7316 close_json_array(PRINT_JSON
, NULL
);
7318 pr_out_fmsg_group_end(dl
);
7321 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
7324 struct dl
*dl
= fmsg_data
->dl
;
7325 uint8_t value
= nest_value
;
7328 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
7329 if (err
!= MNL_CB_OK
)
7333 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
7335 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
7337 pr_out_fmsg_end_object(dl
);
7339 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
7341 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
7343 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
7345 pr_out_fmsg_end_array(dl
);
7353 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
7355 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7356 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7357 struct fmsg_cb_data
*fmsg_data
= data
;
7358 struct dl
*dl
= fmsg_data
->dl
;
7359 struct nlattr
*nla_object
;
7363 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7364 if (!tb
[DEVLINK_ATTR_FMSG
])
7365 return MNL_CB_ERROR
;
7367 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
7368 attr_type
= mnl_attr_get_type(nla_object
);
7369 switch (attr_type
) {
7370 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
7371 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
7372 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
7373 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
7374 if (err
!= MNL_CB_OK
)
7377 case DEVLINK_ATTR_FMSG_NEST_END
:
7378 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
7379 if (err
!= MNL_CB_OK
)
7382 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
7383 free(fmsg_data
->name
);
7384 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
7385 if (!fmsg_data
->name
)
7388 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
7389 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
7391 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
7392 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
7393 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
7395 if (err
!= MNL_CB_OK
)
7405 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
7407 /* FMSG is dynamic: opening of an object or array causes a
7408 * newline. JSON starts with an { or [, but plain text should
7409 * not start with a new line. Ensure this by setting
7410 * g_new_line_count to 1: avoiding newline before the first
7413 g_new_line_count
= 1;
7416 INIT_LIST_HEAD(&data
->entry_list
);
7419 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
7421 struct fmsg_cb_data data
;
7422 struct nlmsghdr
*nlh
;
7425 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
7427 err
= dl_argv_parse_put(nlh
, dl
,
7428 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7429 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7433 cmd_fmsg_init(dl
, &data
);
7434 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
7439 static int cmd_health_dump_show(struct dl
*dl
)
7441 return cmd_health_object_common(dl
,
7442 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
7446 static int cmd_health_diagnose(struct dl
*dl
)
7448 return cmd_health_object_common(dl
,
7449 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
7453 static int cmd_health_test(struct dl
*dl
)
7455 return cmd_health_object_common(dl
,
7456 DEVLINK_CMD_HEALTH_REPORTER_TEST
,
7460 static int cmd_health_recover(struct dl
*dl
)
7462 struct nlmsghdr
*nlh
;
7465 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
7466 NLM_F_REQUEST
| NLM_F_ACK
);
7468 err
= dl_argv_parse_put(nlh
, dl
,
7469 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7470 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7474 dl_opts_put(nlh
, dl
);
7475 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7478 enum devlink_health_reporter_state
{
7479 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
7480 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
7483 static const char *health_state_name(uint8_t state
)
7486 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
7487 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
7488 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
7489 return HEALTH_REPORTER_STATE_ERROR_STR
;
7491 return "<unknown state>";
7495 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
7497 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7498 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7499 uint64_t time_ms
= mnl_attr_get_u64(attr
);
7500 struct sysinfo s_info
;
7506 info
= localtime(&now
);
7507 err
= sysinfo(&s_info
);
7510 /* Subtract uptime in sec from now yields the time of system
7511 * uptime. To this, add time_ms which is the amount of
7512 * milliseconds elapsed between uptime and the dump taken.
7514 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
7515 info
= localtime(&sec
);
7517 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
7518 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
7519 check_indent_newline(dl
);
7520 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7521 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7524 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
7526 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7527 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7532 ts
= mnl_attr_get_u64(attr
);
7533 tv_sec
= ts
/ 1000000000;
7534 tm
= localtime(&tv_sec
);
7536 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
7537 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
7539 check_indent_newline(dl
);
7540 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7541 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7544 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
,
7545 bool print_device
, bool print_port
)
7547 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7548 enum devlink_health_reporter_state state
;
7551 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
7553 if (err
!= MNL_CB_OK
)
7556 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
7557 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
7558 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
7559 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
7562 if (!print_device
&& !print_port
)
7565 if (!print_device
&& !tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7567 else if (tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7568 pr_out_port_handle_start_arr(dl
, tb_health
, false);
7571 if (!print_port
&& tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7573 else if (!tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7574 pr_out_handle_start_arr(dl
, tb_health
);
7577 check_indent_newline(dl
);
7578 print_string(PRINT_ANY
, "reporter", "reporter %s",
7579 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
7580 if (!dl
->json_output
) {
7582 __pr_out_indent_inc();
7584 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
7585 check_indent_newline(dl
);
7586 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
7587 pr_out_u64(dl
, "error",
7588 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
7589 pr_out_u64(dl
, "recover",
7590 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
7591 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
7592 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
7593 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
7594 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
7595 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
7596 pr_out_u64(dl
, "grace_period",
7597 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
7598 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
7599 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
7600 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
7601 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
7602 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
7603 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
7605 __pr_out_indent_dec();
7606 pr_out_handle_end(dl
);
7615 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7617 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7618 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7619 struct health_ctx
*ctx
= data
;
7620 struct dl
*dl
= ctx
->dl
;
7622 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7623 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7624 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
7625 return MNL_CB_ERROR
;
7627 pr_out_health(dl
, tb
, ctx
->show_device
, ctx
->show_port
);
7632 static int __cmd_health_show(struct dl
*dl
, bool show_device
, bool show_port
)
7634 struct nlmsghdr
*nlh
;
7635 struct health_ctx ctx
= { dl
, show_device
, show_port
};
7636 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7639 if (dl_argc(dl
) == 0)
7640 flags
|= NLM_F_DUMP
;
7641 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7644 if (dl_argc(dl
) > 0) {
7645 ctx
.show_port
= true;
7646 err
= dl_argv_parse_put(nlh
, dl
,
7647 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7648 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7652 pr_out_section_start(dl
, "health");
7654 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, &ctx
);
7655 pr_out_section_end(dl
);
7659 static void cmd_health_help(void)
7661 pr_err("Usage: devlink health show [ { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME ]\n");
7662 pr_err(" devlink health recover { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7663 pr_err(" devlink health diagnose { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7664 pr_err(" devlink health test { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7665 pr_err(" devlink health dump show { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7666 pr_err(" devlink health dump clear { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7667 pr_err(" devlink health set { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7668 pr_err(" [ grace_period MSEC ]\n");
7669 pr_err(" [ auto_recover { true | false } ]\n");
7670 pr_err(" [ auto_dump { true | false } ]\n");
7673 static int cmd_health(struct dl
*dl
)
7675 if (dl_argv_match(dl
, "help")) {
7678 } else if (dl_argv_match(dl
, "show") ||
7679 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7681 return __cmd_health_show(dl
, true, true);
7682 } else if (dl_argv_match(dl
, "recover")) {
7684 return cmd_health_recover(dl
);
7685 } else if (dl_argv_match(dl
, "diagnose")) {
7687 return cmd_health_diagnose(dl
);
7688 } else if (dl_argv_match(dl
, "test")) {
7690 return cmd_health_test(dl
);
7691 } else if (dl_argv_match(dl
, "dump")) {
7693 if (dl_argv_match(dl
, "show")) {
7695 return cmd_health_dump_show(dl
);
7696 } else if (dl_argv_match(dl
, "clear")) {
7698 return cmd_health_dump_clear(dl
);
7700 } else if (dl_argv_match(dl
, "set")) {
7702 return cmd_health_set_params(dl
);
7704 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7708 static const char *trap_type_name(uint8_t type
)
7711 case DEVLINK_TRAP_TYPE_DROP
:
7713 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7715 case DEVLINK_TRAP_TYPE_CONTROL
:
7718 return "<unknown type>";
7722 static const char *trap_action_name(uint8_t action
)
7725 case DEVLINK_TRAP_ACTION_DROP
:
7727 case DEVLINK_TRAP_ACTION_TRAP
:
7729 case DEVLINK_TRAP_ACTION_MIRROR
:
7732 return "<unknown action>";
7736 static const char *trap_metadata_name(const struct nlattr
*attr
)
7738 switch (attr
->nla_type
) {
7739 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7740 return "input_port";
7741 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7742 return "flow_action_cookie";
7744 return "<unknown metadata type>";
7747 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7749 struct nlattr
*attr_metadata
;
7751 pr_out_array_start(dl
, "metadata");
7752 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7753 check_indent_newline(dl
);
7754 print_string(PRINT_ANY
, NULL
, "%s",
7755 trap_metadata_name(attr_metadata
));
7757 pr_out_array_end(dl
);
7760 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7762 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7763 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7766 pr_out_handle_start_arr(dl
, tb
);
7768 __pr_out_handle_start(dl
, tb
, true, false);
7770 check_indent_newline(dl
);
7771 print_string(PRINT_ANY
, "name", "name %s",
7772 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7773 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7774 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7775 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7776 print_string(PRINT_ANY
, "group", " group %s",
7777 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7779 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7780 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7781 pr_out_handle_end(dl
);
7784 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7786 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7787 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7788 struct dl
*dl
= data
;
7790 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7791 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7792 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7793 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7794 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7795 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7796 return MNL_CB_ERROR
;
7798 pr_out_trap(dl
, tb
, true);
7803 static void cmd_trap_help(void)
7805 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7806 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7807 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7808 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7809 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7810 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7811 pr_err(" devlink trap policer show DEV policer POLICER\n");
7814 static int cmd_trap_show(struct dl
*dl
)
7816 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7817 struct nlmsghdr
*nlh
;
7820 if (dl_argc(dl
) == 0)
7821 flags
|= NLM_F_DUMP
;
7823 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7825 if (dl_argc(dl
) > 0) {
7826 err
= dl_argv_parse_put(nlh
, dl
,
7827 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7832 pr_out_section_start(dl
, "trap");
7833 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7834 pr_out_section_end(dl
);
7839 static int cmd_trap_set(struct dl
*dl
)
7841 struct nlmsghdr
*nlh
;
7844 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7845 NLM_F_REQUEST
| NLM_F_ACK
);
7847 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7848 DL_OPT_TRAP_ACTION
);
7852 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7855 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7858 pr_out_handle_start_arr(dl
, tb
);
7860 __pr_out_handle_start(dl
, tb
, true, false);
7862 check_indent_newline(dl
);
7863 print_string(PRINT_ANY
, "name", "name %s",
7864 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7865 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7866 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7867 print_uint(PRINT_ANY
, "policer", " policer %u",
7868 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7869 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7870 pr_out_handle_end(dl
);
7873 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7875 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7876 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7877 struct dl
*dl
= data
;
7879 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7880 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7881 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7882 return MNL_CB_ERROR
;
7884 pr_out_trap_group(dl
, tb
, true);
7889 static int cmd_trap_group_show(struct dl
*dl
)
7891 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7892 struct nlmsghdr
*nlh
;
7895 if (dl_argc(dl
) == 0)
7896 flags
|= NLM_F_DUMP
;
7898 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7900 if (dl_argc(dl
) > 0) {
7901 err
= dl_argv_parse_put(nlh
, dl
,
7902 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7908 pr_out_section_start(dl
, "trap_group");
7909 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7910 pr_out_section_end(dl
);
7915 static int cmd_trap_group_set(struct dl
*dl
)
7917 struct nlmsghdr
*nlh
;
7920 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7921 NLM_F_REQUEST
| NLM_F_ACK
);
7923 err
= dl_argv_parse_put(nlh
, dl
,
7924 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7925 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7929 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7932 static int cmd_trap_group(struct dl
*dl
)
7934 if (dl_argv_match(dl
, "help")) {
7937 } else if (dl_argv_match(dl
, "show") ||
7938 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7940 return cmd_trap_group_show(dl
);
7941 } else if (dl_argv_match(dl
, "set")) {
7943 return cmd_trap_group_set(dl
);
7945 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7949 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7952 pr_out_handle_start_arr(dl
, tb
);
7954 __pr_out_handle_start(dl
, tb
, true, false);
7956 check_indent_newline(dl
);
7957 print_uint(PRINT_ANY
, "policer", "policer %u",
7958 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7959 print_u64(PRINT_ANY
, "rate", " rate %llu",
7960 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7961 print_u64(PRINT_ANY
, "burst", " burst %llu",
7962 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7963 if (tb
[DEVLINK_ATTR_STATS
])
7964 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7965 pr_out_handle_end(dl
);
7968 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7970 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7971 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7972 struct dl
*dl
= data
;
7974 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7975 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7976 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7977 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7978 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7979 return MNL_CB_ERROR
;
7981 pr_out_trap_policer(dl
, tb
, true);
7986 static int cmd_trap_policer_show(struct dl
*dl
)
7988 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7989 struct nlmsghdr
*nlh
;
7992 if (dl_argc(dl
) == 0)
7993 flags
|= NLM_F_DUMP
;
7995 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7997 if (dl_argc(dl
) > 0) {
7998 err
= dl_argv_parse_put(nlh
, dl
,
7999 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
8005 pr_out_section_start(dl
, "trap_policer");
8006 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
8007 pr_out_section_end(dl
);
8012 static int cmd_trap_policer_set(struct dl
*dl
)
8014 struct nlmsghdr
*nlh
;
8017 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
8018 NLM_F_REQUEST
| NLM_F_ACK
);
8020 err
= dl_argv_parse_put(nlh
, dl
,
8021 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
8022 DL_OPT_TRAP_POLICER_RATE
|
8023 DL_OPT_TRAP_POLICER_BURST
);
8027 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
8030 static int cmd_trap_policer(struct dl
*dl
)
8032 if (dl_argv_match(dl
, "help")) {
8035 } else if (dl_argv_match(dl
, "show") ||
8036 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
8038 return cmd_trap_policer_show(dl
);
8039 } else if (dl_argv_match(dl
, "set")) {
8041 return cmd_trap_policer_set(dl
);
8043 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
8047 static int cmd_trap(struct dl
*dl
)
8049 if (dl_argv_match(dl
, "help")) {
8052 } else if (dl_argv_match(dl
, "show") ||
8053 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
8055 return cmd_trap_show(dl
);
8056 } else if (dl_argv_match(dl
, "set")) {
8058 return cmd_trap_set(dl
);
8059 } else if (dl_argv_match(dl
, "group")) {
8061 return cmd_trap_group(dl
);
8062 } else if (dl_argv_match(dl
, "policer")) {
8064 return cmd_trap_policer(dl
);
8066 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
8070 static void help(void)
8072 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
8073 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
8074 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
8075 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
8078 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
8083 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
8086 } else if (dl_argv_match(dl
, "dev")) {
8089 } else if (dl_argv_match(dl
, "port")) {
8091 return cmd_port(dl
);
8092 } else if (dl_argv_match(dl
, "sb")) {
8095 } else if (dl_argv_match(dl
, "monitor")) {
8098 } else if (dl_argv_match(dl
, "dpipe")) {
8100 return cmd_dpipe(dl
);
8101 } else if (dl_argv_match(dl
, "resource")) {
8103 return cmd_resource(dl
);
8104 } else if (dl_argv_match(dl
, "region")) {
8106 return cmd_region(dl
);
8107 } else if (dl_argv_match(dl
, "health")) {
8109 return cmd_health(dl
);
8110 } else if (dl_argv_match(dl
, "trap")) {
8112 return cmd_trap(dl
);
8114 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
8118 static int dl_init(struct dl
*dl
)
8122 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
8124 pr_err("Failed to connect to devlink Netlink\n");
8128 err
= ifname_map_init(dl
);
8130 pr_err("Failed to create index map\n");
8131 goto err_ifname_map_create
;
8133 new_json_obj_plain(dl
->json_output
);
8136 err_ifname_map_create
:
8137 mnlg_socket_close(dl
->nlg
);
8141 static void dl_fini(struct dl
*dl
)
8143 delete_json_obj_plain();
8144 ifname_map_fini(dl
);
8145 mnlg_socket_close(dl
->nlg
);
8148 static struct dl
*dl_alloc(void)
8152 dl
= calloc(1, sizeof(*dl
));
8158 static void dl_free(struct dl
*dl
)
8163 static int dl_batch_cmd(int argc
, char *argv
[], void *data
)
8165 struct dl
*dl
= data
;
8167 return dl_cmd(dl
, argc
, argv
);
8170 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
8172 return do_batch(name
, force
, dl_batch_cmd
, dl
);
8175 int main(int argc
, char **argv
)
8177 static const struct option long_options
[] = {
8178 { "Version", no_argument
, NULL
, 'V' },
8179 { "force", no_argument
, NULL
, 'f' },
8180 { "batch", required_argument
, NULL
, 'b' },
8181 { "no-nice-names", no_argument
, NULL
, 'n' },
8182 { "json", no_argument
, NULL
, 'j' },
8183 { "pretty", no_argument
, NULL
, 'p' },
8184 { "verbose", no_argument
, NULL
, 'v' },
8185 { "statistics", no_argument
, NULL
, 's' },
8186 { "Netns", required_argument
, NULL
, 'N' },
8187 { NULL
, 0, NULL
, 0 }
8189 const char *batch_file
= NULL
;
8198 pr_err("Failed to allocate memory for devlink\n");
8199 return EXIT_FAILURE
;
8202 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
8203 long_options
, NULL
)) >= 0) {
8207 printf("devlink utility, iproute2-%s\n", version
);
8214 batch_file
= optarg
;
8217 dl
->no_nice_names
= true;
8220 dl
->json_output
= true;
8232 if (netns_switch(optarg
)) {
8238 pr_err("Unknown option.\n");
8255 err
= dl_batch(dl
, batch_file
, force
);
8257 err
= dl_cmd(dl
, argc
, argv
);