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)
309 uint64_t present
; /* flags of present items */
313 enum devlink_port_type port_type
;
316 uint16_t sb_pool_index
;
317 uint32_t sb_pool_size
;
318 enum devlink_sb_pool_type sb_pool_type
;
319 enum devlink_sb_threshold_type sb_pool_thtype
;
320 uint32_t sb_threshold
;
321 uint16_t sb_tc_index
;
322 enum devlink_eswitch_mode eswitch_mode
;
323 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
324 const char *dpipe_table_name
;
325 bool dpipe_counters_enabled
;
326 enum devlink_eswitch_encap_mode eswitch_encap_mode
;
327 const char *resource_path
;
328 uint64_t resource_size
;
329 uint32_t resource_id
;
330 bool resource_id_valid
;
331 const char *param_name
;
332 const char *param_value
;
333 enum devlink_param_cmode cmode
;
335 uint32_t region_snapshot_id
;
336 uint64_t region_address
;
337 uint64_t region_length
;
338 const char *flash_file_name
;
339 const char *flash_component
;
340 const char *reporter_name
;
341 uint64_t reporter_graceful_period
;
342 bool reporter_auto_recover
;
343 bool reporter_auto_dump
;
344 const char *trap_name
;
345 const char *trap_group_name
;
346 enum devlink_trap_action trap_action
;
349 uint32_t trap_policer_id
;
350 uint64_t trap_policer_rate
;
351 uint64_t trap_policer_burst
;
352 char port_function_hw_addr
[MAX_ADDR_LEN
];
353 uint32_t port_function_hw_addr_len
;
354 uint32_t overwrite_mask
;
358 struct mnlg_socket
*nlg
;
359 struct list_head ifname_map_list
;
376 static int dl_argc(struct dl
*dl
)
381 static char *dl_argv(struct dl
*dl
)
383 if (dl_argc(dl
) == 0)
388 static void dl_arg_inc(struct dl
*dl
)
390 if (dl_argc(dl
) == 0)
396 static void dl_arg_dec(struct dl
*dl
)
402 static char *dl_argv_next(struct dl
*dl
)
406 if (dl_argc(dl
) == 0)
414 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
416 if (index
>= dl_argc(dl
))
418 return dl
->argv
[index
];
421 static int strcmpx(const char *str1
, const char *str2
)
423 if (strlen(str1
) > strlen(str2
))
425 return strncmp(str1
, str2
, strlen(str1
));
428 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
430 if (dl_argc(dl
) == 0)
432 return strcmpx(dl_argv(dl
), pattern
) == 0;
435 static bool dl_no_arg(struct dl
*dl
)
437 return dl_argc(dl
) == 0;
440 static void __pr_out_indent_newline(struct dl
*dl
)
442 if (!g_indent_newline
&& !dl
->json_output
)
446 static bool is_binary_eol(int i
)
451 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
457 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
459 pr_out("%02x ", data
[i
]);
461 if (!dl
->json_output
&& is_binary_eol(i
))
464 if (!dl
->json_output
&& !is_binary_eol(i
))
468 static void pr_out_name(struct dl
*dl
, const char *name
)
470 __pr_out_indent_newline(dl
);
472 print_string(PRINT_JSON
, name
, NULL
, NULL
);
477 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
479 __pr_out_indent_newline(dl
);
480 if (val
== (uint64_t) -1)
481 return print_string_name_value(name
, "unlimited");
484 print_u64(PRINT_JSON
, name
, NULL
, val
);
486 pr_out("%s %"PRIu64
, name
, val
);
489 static void pr_out_section_start(struct dl
*dl
, const char *name
)
491 if (dl
->json_output
) {
492 open_json_object(NULL
);
493 open_json_object(name
);
497 static void pr_out_section_end(struct dl
*dl
)
499 if (dl
->json_output
) {
500 if (dl
->arr_last
.present
)
501 close_json_array(PRINT_JSON
, NULL
);
507 static void pr_out_array_start(struct dl
*dl
, const char *name
)
509 if (dl
->json_output
) {
510 open_json_array(PRINT_JSON
, name
);
512 __pr_out_indent_inc();
515 __pr_out_indent_inc();
520 static void pr_out_array_end(struct dl
*dl
)
522 if (dl
->json_output
) {
523 close_json_array(PRINT_JSON
, NULL
);
525 __pr_out_indent_dec();
526 __pr_out_indent_dec();
530 static void pr_out_object_start(struct dl
*dl
, const char *name
)
532 if (dl
->json_output
) {
533 open_json_object(name
);
535 __pr_out_indent_inc();
538 __pr_out_indent_inc();
543 static void pr_out_object_end(struct dl
*dl
)
545 if (dl
->json_output
) {
548 __pr_out_indent_dec();
549 __pr_out_indent_dec();
553 static void pr_out_entry_start(struct dl
*dl
)
556 open_json_object(NULL
);
559 static void pr_out_entry_end(struct dl
*dl
)
567 static void check_indent_newline(struct dl
*dl
)
569 __pr_out_indent_newline(dl
);
571 if (g_indent_newline
&& !is_json_context()) {
572 printf("%s", g_indent_str
);
573 g_indent_newline
= false;
575 g_new_line_count
= 0;
578 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
579 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
580 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
581 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
582 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
583 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
584 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
585 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
586 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
587 [DEVLINK_ATTR_PORT_LANES
] = MNL_TYPE_U32
,
588 [DEVLINK_ATTR_PORT_SPLITTABLE
] = MNL_TYPE_U8
,
589 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
590 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
591 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
592 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
593 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
594 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
595 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
596 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
597 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
598 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
599 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
600 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
601 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
602 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
603 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
604 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
605 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
606 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
607 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
608 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
609 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
610 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
611 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
612 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
613 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
614 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
615 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
616 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
617 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
618 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
619 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
620 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
621 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
622 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
623 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
624 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
625 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
626 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
627 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
628 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
629 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
630 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
631 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
632 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
633 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
634 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
635 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
636 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
637 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
638 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
639 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
640 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
641 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
642 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
643 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
644 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
645 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
646 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
647 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
648 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
649 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
650 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
651 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
652 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
653 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
654 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
655 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
656 [DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
657 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
658 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
659 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
660 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
661 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
662 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
663 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
664 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
665 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
666 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
667 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
668 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
669 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
670 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
671 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
672 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
673 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
674 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
675 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
676 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
677 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
678 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
679 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
680 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
681 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
682 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
683 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
686 static const enum mnl_attr_data_type
687 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
688 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
689 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
690 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
693 static int attr_cb(const struct nlattr
*attr
, void *data
)
695 const struct nlattr
**tb
= data
;
698 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
701 type
= mnl_attr_get_type(attr
);
702 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
709 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
711 const struct nlattr
**tb
= data
;
714 /* Allow the tool to work on top of newer kernels that might contain
717 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
720 type
= mnl_attr_get_type(attr
);
721 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
728 static const enum mnl_attr_data_type
729 devlink_function_policy
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {
730 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
] = MNL_TYPE_BINARY
,
733 static int function_attr_cb(const struct nlattr
*attr
, void *data
)
735 const struct nlattr
**tb
= data
;
738 /* Allow the tool to work on top of newer kernels that might contain
741 if (mnl_attr_type_valid(attr
, DEVLINK_PORT_FUNCTION_ATTR_MAX
) < 0)
744 type
= mnl_attr_get_type(attr
);
745 if (mnl_attr_validate(attr
, devlink_function_policy
[type
]) < 0)
752 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
754 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
755 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
756 struct dl
*dl
= data
;
757 struct ifname_map
*ifname_map
;
758 const char *bus_name
;
759 const char *dev_name
;
760 uint32_t port_ifindex
;
761 const char *port_ifname
;
763 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
764 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
765 !tb
[DEVLINK_ATTR_PORT_INDEX
])
768 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
771 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
772 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
773 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
774 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
775 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
776 port_ifindex
, port_ifname
);
779 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
784 static void ifname_map_fini(struct dl
*dl
)
786 struct ifname_map
*ifname_map
, *tmp
;
788 list_for_each_entry_safe(ifname_map
, tmp
,
789 &dl
->ifname_map_list
, list
) {
790 list_del(&ifname_map
->list
);
791 ifname_map_free(ifname_map
);
795 static int ifname_map_init(struct dl
*dl
)
797 struct nlmsghdr
*nlh
;
800 INIT_LIST_HEAD(&dl
->ifname_map_list
);
802 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
803 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
805 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
813 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
814 char **p_bus_name
, char **p_dev_name
,
815 uint32_t *p_port_index
)
817 struct ifname_map
*ifname_map
;
819 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
820 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
821 *p_bus_name
= ifname_map
->bus_name
;
822 *p_dev_name
= ifname_map
->dev_name
;
823 *p_port_index
= ifname_map
->port_index
;
830 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
831 const char *dev_name
, uint32_t port_index
,
834 struct ifname_map
*ifname_map
;
836 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
837 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
838 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
839 port_index
== ifname_map
->port_index
) {
840 *p_ifname
= ifname_map
->ifname
;
847 static unsigned int strslashcount(char *str
)
849 unsigned int count
= 0;
852 while ((pos
= strchr(pos
, '/'))) {
859 static int strslashrsplit(char *str
, char **before
, char **after
)
863 slash
= strrchr(str
, '/');
872 static int strtouint64_t(const char *str
, uint64_t *p_val
)
875 unsigned long long int val
;
877 val
= strtoull(str
, &endptr
, 10);
878 if (endptr
== str
|| *endptr
!= '\0')
886 static int strtouint32_t(const char *str
, uint32_t *p_val
)
889 unsigned long int val
;
891 val
= strtoul(str
, &endptr
, 10);
892 if (endptr
== str
|| *endptr
!= '\0')
900 static int strtouint16_t(const char *str
, uint16_t *p_val
)
903 unsigned long int val
;
905 val
= strtoul(str
, &endptr
, 10);
906 if (endptr
== str
|| *endptr
!= '\0')
914 static int strtouint8_t(const char *str
, uint8_t *p_val
)
917 unsigned long int val
;
919 val
= strtoul(str
, &endptr
, 10);
920 if (endptr
== str
|| *endptr
!= '\0')
928 static int strtobool(const char *str
, bool *p_val
)
932 if (!strcmp(str
, "true") || !strcmp(str
, "1") ||
933 !strcmp(str
, "enable"))
935 else if (!strcmp(str
, "false") || !strcmp(str
, "0") ||
936 !strcmp(str
, "disable"))
944 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
946 strslashrsplit(str
, p_bus_name
, p_dev_name
);
950 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
952 char *str
= dl_argv_next(dl
);
955 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
958 if (strslashcount(str
) != 1) {
959 pr_err("Wrong devlink identification string format.\n");
960 pr_err("Expected \"bus_name/dev_name\".\n");
963 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
966 static int __dl_argv_handle_port(char *str
,
967 char **p_bus_name
, char **p_dev_name
,
968 uint32_t *p_port_index
)
974 err
= strslashrsplit(str
, &handlestr
, &portstr
);
976 pr_err("Port identification \"%s\" is invalid\n", str
);
979 err
= strtouint32_t(portstr
, p_port_index
);
981 pr_err("Port index \"%s\" is not a number or not within range\n",
985 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
987 pr_err("Port identification \"%s\" is invalid\n", str
);
993 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
994 char **p_bus_name
, char **p_dev_name
,
995 uint32_t *p_port_index
)
999 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
1002 pr_err("Netdevice \"%s\" not found\n", str
);
1008 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
1009 char **p_dev_name
, uint32_t *p_port_index
)
1011 char *str
= dl_argv_next(dl
);
1012 unsigned int slash_count
;
1015 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
1018 slash_count
= strslashcount(str
);
1019 switch (slash_count
) {
1021 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
1022 p_dev_name
, p_port_index
);
1024 return __dl_argv_handle_port(str
, p_bus_name
,
1025 p_dev_name
, p_port_index
);
1027 pr_err("Wrong port identification string format.\n");
1028 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1033 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
1034 char **p_dev_name
, uint32_t *p_port_index
,
1035 uint64_t *p_handle_bit
)
1037 char *str
= dl_argv_next(dl
);
1038 unsigned int slash_count
;
1042 pr_err("One of following identifications expected:\n"
1043 "Devlink identification (\"bus_name/dev_name\")\n"
1044 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
1047 slash_count
= strslashcount(str
);
1048 if (slash_count
== 1) {
1049 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
1052 *p_handle_bit
= DL_OPT_HANDLE
;
1053 } else if (slash_count
== 2) {
1054 err
= __dl_argv_handle_port(str
, p_bus_name
,
1055 p_dev_name
, p_port_index
);
1058 *p_handle_bit
= DL_OPT_HANDLEP
;
1059 } else if (slash_count
== 0) {
1060 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
1061 p_dev_name
, p_port_index
);
1064 *p_handle_bit
= DL_OPT_HANDLEP
;
1066 pr_err("Wrong port identification string format.\n");
1067 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
1073 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
1074 char **p_dev_name
, char **p_region
)
1079 err
= strslashrsplit(str
, &handlestr
, p_region
);
1081 pr_err("Region identification \"%s\" is invalid\n", str
);
1084 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
1086 pr_err("Region identification \"%s\" is invalid\n", str
);
1092 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
1093 char **p_dev_name
, char **p_region
)
1095 char *str
= dl_argv_next(dl
);
1096 unsigned int slash_count
;
1099 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
1103 slash_count
= strslashcount(str
);
1104 if (slash_count
!= 2) {
1105 pr_err("Wrong region identification string format.\n");
1106 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
1110 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
1113 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
1115 char *str
= dl_argv_next(dl
);
1119 pr_err("Unsigned number argument expected\n");
1123 err
= strtouint64_t(str
, p_val
);
1125 pr_err("\"%s\" is not a number or not within range\n", str
);
1131 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
1133 char *str
= dl_argv_next(dl
);
1137 pr_err("Unsigned number argument expected\n");
1141 err
= strtouint32_t(str
, p_val
);
1143 pr_err("\"%s\" is not a number or not within range\n", str
);
1149 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
1151 char *str
= dl_argv_next(dl
);
1155 pr_err("Unsigned number argument expected\n");
1159 err
= strtouint16_t(str
, p_val
);
1161 pr_err("\"%s\" is not a number or not within range\n", str
);
1167 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
1169 char *str
= dl_argv_next(dl
);
1173 pr_err("Boolean argument expected\n");
1177 err
= strtobool(str
, p_val
);
1179 pr_err("\"%s\" is not a valid boolean value\n", str
);
1185 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
1187 const char *str
= dl_argv_next(dl
);
1190 pr_err("String parameter expected\n");
1197 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1199 if (strcmp(typestr
, "auto") == 0) {
1200 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1201 } else if (strcmp(typestr
, "eth") == 0) {
1202 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1203 } else if (strcmp(typestr
, "ib") == 0) {
1204 *p_type
= DEVLINK_PORT_TYPE_IB
;
1206 pr_err("Unknown port type \"%s\"\n", typestr
);
1212 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1214 if (strcmp(typestr
, "ingress") == 0) {
1215 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1216 } else if (strcmp(typestr
, "egress") == 0) {
1217 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1219 pr_err("Unknown pool type \"%s\"\n", typestr
);
1225 static int threshold_type_get(const char *typestr
,
1226 enum devlink_sb_threshold_type
*p_type
)
1228 if (strcmp(typestr
, "static") == 0) {
1229 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1230 } else if (strcmp(typestr
, "dynamic") == 0) {
1231 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1233 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1239 static int eswitch_mode_get(const char *typestr
,
1240 enum devlink_eswitch_mode
*p_mode
)
1242 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1243 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1244 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1245 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1247 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1253 static int eswitch_inline_mode_get(const char *typestr
,
1254 enum devlink_eswitch_inline_mode
*p_mode
)
1256 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1257 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1258 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1259 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1260 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1261 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1262 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1263 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1265 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1272 eswitch_encap_mode_get(const char *typestr
,
1273 enum devlink_eswitch_encap_mode
*p_encap_mode
)
1275 /* The initial implementation incorrectly accepted "enable"/"disable".
1276 * Carry it to maintain backward compatibility.
1278 if (strcmp(typestr
, "disable") == 0 ||
1279 strcmp(typestr
, ESWITCH_ENCAP_MODE_NONE
) == 0) {
1280 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1281 } else if (strcmp(typestr
, "enable") == 0 ||
1282 strcmp(typestr
, ESWITCH_ENCAP_MODE_BASIC
) == 0) {
1283 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1285 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1291 static int flash_overwrite_section_get(const char *sectionstr
, uint32_t *mask
)
1293 if (strcmp(sectionstr
, "settings") == 0) {
1294 *mask
|= DEVLINK_FLASH_OVERWRITE_SETTINGS
;
1295 } else if (strcmp(sectionstr
, "identifiers") == 0) {
1296 *mask
|= DEVLINK_FLASH_OVERWRITE_IDENTIFIERS
;
1298 pr_err("Unknown overwrite section \"%s\"\n", sectionstr
);
1304 static int param_cmode_get(const char *cmodestr
,
1305 enum devlink_param_cmode
*cmode
)
1307 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1308 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1309 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1310 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1311 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1312 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1314 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1320 static int trap_action_get(const char *actionstr
,
1321 enum devlink_trap_action
*p_action
)
1323 if (strcmp(actionstr
, "drop") == 0) {
1324 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1325 } else if (strcmp(actionstr
, "trap") == 0) {
1326 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1327 } else if (strcmp(actionstr
, "mirror") == 0) {
1328 *p_action
= DEVLINK_TRAP_ACTION_MIRROR
;
1330 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1336 static int hw_addr_parse(const char *addrstr
, char *hw_addr
, uint32_t *len
)
1340 alen
= ll_addr_a2n(hw_addr
, MAX_ADDR_LEN
, addrstr
);
1347 struct dl_args_metadata
{
1349 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1352 static const struct dl_args_metadata dl_args_required
[] = {
1353 {DL_OPT_PORT_TYPE
, "Port type not set."},
1354 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1355 {DL_OPT_SB_POOL
, "Pool index option expected."},
1356 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1357 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1358 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1359 {DL_OPT_SB_TH
, "Threshold option expected."},
1360 {DL_OPT_SB_TC
, "TC index option expected."},
1361 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1362 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1363 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1364 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1365 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1366 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1367 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1368 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1369 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1370 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1371 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1372 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1373 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1374 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1375 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1376 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1377 {DL_OPT_PORT_FUNCTION_HW_ADDR
, "Port function's hardware address is expected."},
1380 static int dl_args_finding_required_validate(uint64_t o_required
,
1386 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1387 o_flag
= dl_args_required
[i
].o_flag
;
1388 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1389 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1393 if (o_required
& ~o_found
) {
1394 pr_err("BUG: unknown argument required but not found\n");
1400 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1401 uint64_t o_optional
)
1403 struct dl_opts
*opts
= &dl
->opts
;
1404 uint64_t o_all
= o_required
| o_optional
;
1405 uint64_t o_found
= 0;
1408 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1409 uint64_t handle_bit
;
1411 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1412 &opts
->port_index
, &handle_bit
);
1415 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1416 o_found
|= handle_bit
;
1417 } else if (o_required
& DL_OPT_HANDLE
) {
1418 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1421 o_found
|= DL_OPT_HANDLE
;
1422 } else if (o_required
& DL_OPT_HANDLEP
) {
1423 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1427 o_found
|= DL_OPT_HANDLEP
;
1428 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1429 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1431 &opts
->region_name
);
1434 o_found
|= DL_OPT_HANDLE_REGION
;
1437 while (dl_argc(dl
)) {
1438 if (dl_argv_match(dl
, "type") &&
1439 (o_all
& DL_OPT_PORT_TYPE
)) {
1440 const char *typestr
;
1443 err
= dl_argv_str(dl
, &typestr
);
1446 err
= port_type_get(typestr
, &opts
->port_type
);
1449 o_found
|= DL_OPT_PORT_TYPE
;
1450 } else if (dl_argv_match(dl
, "count") &&
1451 (o_all
& DL_OPT_PORT_COUNT
)) {
1453 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1456 o_found
|= DL_OPT_PORT_COUNT
;
1457 } else if (dl_argv_match(dl
, "sb") &&
1458 (o_all
& DL_OPT_SB
)) {
1460 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1463 o_found
|= DL_OPT_SB
;
1464 } else if (dl_argv_match(dl
, "pool") &&
1465 (o_all
& DL_OPT_SB_POOL
)) {
1467 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1470 o_found
|= DL_OPT_SB_POOL
;
1471 } else if (dl_argv_match(dl
, "size") &&
1472 (o_all
& DL_OPT_SB_SIZE
)) {
1474 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1477 o_found
|= DL_OPT_SB_SIZE
;
1478 } else if (dl_argv_match(dl
, "type") &&
1479 (o_all
& DL_OPT_SB_TYPE
)) {
1480 const char *typestr
;
1483 err
= dl_argv_str(dl
, &typestr
);
1486 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1489 o_found
|= DL_OPT_SB_TYPE
;
1490 } else if (dl_argv_match(dl
, "thtype") &&
1491 (o_all
& DL_OPT_SB_THTYPE
)) {
1492 const char *typestr
;
1495 err
= dl_argv_str(dl
, &typestr
);
1498 err
= threshold_type_get(typestr
,
1499 &opts
->sb_pool_thtype
);
1502 o_found
|= DL_OPT_SB_THTYPE
;
1503 } else if (dl_argv_match(dl
, "th") &&
1504 (o_all
& DL_OPT_SB_TH
)) {
1506 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1509 o_found
|= DL_OPT_SB_TH
;
1510 } else if (dl_argv_match(dl
, "tc") &&
1511 (o_all
& DL_OPT_SB_TC
)) {
1513 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1516 o_found
|= DL_OPT_SB_TC
;
1517 } else if (dl_argv_match(dl
, "mode") &&
1518 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1519 const char *typestr
;
1522 err
= dl_argv_str(dl
, &typestr
);
1525 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1528 o_found
|= DL_OPT_ESWITCH_MODE
;
1529 } else if (dl_argv_match(dl
, "inline-mode") &&
1530 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1531 const char *typestr
;
1534 err
= dl_argv_str(dl
, &typestr
);
1537 err
= eswitch_inline_mode_get(
1538 typestr
, &opts
->eswitch_inline_mode
);
1541 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1542 } else if (dl_argv_match(dl
, "name") &&
1543 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1545 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1548 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1549 } else if ((dl_argv_match(dl
, "counters") ||
1550 dl_argv_match(dl
, "counters_enabled")) &&
1551 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1553 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1556 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1557 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1558 dl_argv_match(dl
, "encap-mode")) &&
1559 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1560 const char *typestr
;
1563 err
= dl_argv_str(dl
, &typestr
);
1566 err
= eswitch_encap_mode_get(typestr
,
1567 &opts
->eswitch_encap_mode
);
1570 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1571 } else if (dl_argv_match(dl
, "path") &&
1572 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1574 err
= dl_argv_str(dl
, &opts
->resource_path
);
1577 o_found
|= DL_OPT_RESOURCE_PATH
;
1578 } else if (dl_argv_match(dl
, "size") &&
1579 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1581 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1584 o_found
|= DL_OPT_RESOURCE_SIZE
;
1585 } else if (dl_argv_match(dl
, "name") &&
1586 (o_all
& DL_OPT_PARAM_NAME
)) {
1588 err
= dl_argv_str(dl
, &opts
->param_name
);
1591 o_found
|= DL_OPT_PARAM_NAME
;
1592 } else if (dl_argv_match(dl
, "value") &&
1593 (o_all
& DL_OPT_PARAM_VALUE
)) {
1595 err
= dl_argv_str(dl
, &opts
->param_value
);
1598 o_found
|= DL_OPT_PARAM_VALUE
;
1599 } else if (dl_argv_match(dl
, "cmode") &&
1600 (o_all
& DL_OPT_PARAM_CMODE
)) {
1601 const char *cmodestr
;
1604 err
= dl_argv_str(dl
, &cmodestr
);
1607 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1610 o_found
|= DL_OPT_PARAM_CMODE
;
1611 } else if (dl_argv_match(dl
, "snapshot") &&
1612 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1614 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1617 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1618 } else if (dl_argv_match(dl
, "address") &&
1619 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1621 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1624 o_found
|= DL_OPT_REGION_ADDRESS
;
1625 } else if (dl_argv_match(dl
, "length") &&
1626 (o_all
& DL_OPT_REGION_LENGTH
)) {
1628 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1631 o_found
|= DL_OPT_REGION_LENGTH
;
1632 } else if (dl_argv_match(dl
, "file") &&
1633 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1635 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1638 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1639 } else if (dl_argv_match(dl
, "component") &&
1640 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1642 err
= dl_argv_str(dl
, &opts
->flash_component
);
1645 o_found
|= DL_OPT_FLASH_COMPONENT
;
1647 } else if (dl_argv_match(dl
, "overwrite") &&
1648 (o_all
& DL_OPT_FLASH_OVERWRITE
)) {
1649 const char *sectionstr
;
1652 err
= dl_argv_str(dl
, §ionstr
);
1655 err
= flash_overwrite_section_get(sectionstr
,
1656 &opts
->overwrite_mask
);
1659 o_found
|= DL_OPT_FLASH_OVERWRITE
;
1661 } else if (dl_argv_match(dl
, "reporter") &&
1662 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1664 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1667 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1668 } else if (dl_argv_match(dl
, "grace_period") &&
1669 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1671 err
= dl_argv_uint64_t(dl
,
1672 &opts
->reporter_graceful_period
);
1675 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1676 } else if (dl_argv_match(dl
, "auto_recover") &&
1677 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1679 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1682 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1683 } else if (dl_argv_match(dl
, "auto_dump") &&
1684 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)) {
1686 err
= dl_argv_bool(dl
, &opts
->reporter_auto_dump
);
1689 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_DUMP
;
1690 } else if (dl_argv_match(dl
, "trap") &&
1691 (o_all
& DL_OPT_TRAP_NAME
)) {
1693 err
= dl_argv_str(dl
, &opts
->trap_name
);
1696 o_found
|= DL_OPT_TRAP_NAME
;
1697 } else if (dl_argv_match(dl
, "group") &&
1698 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1700 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1703 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1704 } else if (dl_argv_match(dl
, "action") &&
1705 (o_all
& DL_OPT_TRAP_ACTION
)) {
1706 const char *actionstr
;
1709 err
= dl_argv_str(dl
, &actionstr
);
1712 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1715 o_found
|= DL_OPT_TRAP_ACTION
;
1716 } else if (dl_argv_match(dl
, "netns") &&
1717 (o_all
& DL_OPT_NETNS
)) {
1718 const char *netns_str
;
1721 err
= dl_argv_str(dl
, &netns_str
);
1724 opts
->netns
= netns_get_fd(netns_str
);
1725 if ((int)opts
->netns
< 0) {
1727 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1730 opts
->netns_is_pid
= true;
1732 o_found
|= DL_OPT_NETNS
;
1733 } else if (dl_argv_match(dl
, "policer") &&
1734 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1736 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1739 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1740 } else if (dl_argv_match(dl
, "nopolicer") &&
1741 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1743 opts
->trap_policer_id
= 0;
1744 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1745 } else if (dl_argv_match(dl
, "rate") &&
1746 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1748 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1751 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1752 } else if (dl_argv_match(dl
, "burst") &&
1753 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1755 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1758 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1759 } else if (dl_argv_match(dl
, "hw_addr") &&
1760 (o_all
& DL_OPT_PORT_FUNCTION_HW_ADDR
)) {
1761 const char *addrstr
;
1764 err
= dl_argv_str(dl
, &addrstr
);
1767 err
= hw_addr_parse(addrstr
, opts
->port_function_hw_addr
,
1768 &opts
->port_function_hw_addr_len
);
1771 o_found
|= DL_OPT_PORT_FUNCTION_HW_ADDR
;
1774 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1779 opts
->present
= o_found
;
1781 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1783 opts
->present
|= DL_OPT_SB
;
1786 return dl_args_finding_required_validate(o_required
, o_found
);
1790 dl_function_attr_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1792 struct nlattr
*nest
;
1794 nest
= mnl_attr_nest_start(nlh
, DEVLINK_ATTR_PORT_FUNCTION
);
1795 mnl_attr_put(nlh
, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
,
1796 opts
->port_function_hw_addr_len
,
1797 opts
->port_function_hw_addr
);
1798 mnl_attr_nest_end(nlh
, nest
);
1802 dl_flash_update_overwrite_put(struct nlmsghdr
*nlh
, const struct dl_opts
*opts
)
1804 struct nla_bitfield32 overwrite_mask
;
1806 overwrite_mask
.selector
= DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS
;
1807 overwrite_mask
.value
= opts
->overwrite_mask
;
1809 mnl_attr_put(nlh
, DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK
,
1810 sizeof(overwrite_mask
), &overwrite_mask
);
1813 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1815 struct dl_opts
*opts
= &dl
->opts
;
1817 if (opts
->present
& DL_OPT_HANDLE
) {
1818 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1819 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1820 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1821 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1822 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1823 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1825 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1826 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1827 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1828 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1831 if (opts
->present
& DL_OPT_PORT_TYPE
)
1832 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1834 if (opts
->present
& DL_OPT_PORT_COUNT
)
1835 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1837 if (opts
->present
& DL_OPT_SB
)
1838 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1840 if (opts
->present
& DL_OPT_SB_POOL
)
1841 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1842 opts
->sb_pool_index
);
1843 if (opts
->present
& DL_OPT_SB_SIZE
)
1844 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1845 opts
->sb_pool_size
);
1846 if (opts
->present
& DL_OPT_SB_TYPE
)
1847 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1848 opts
->sb_pool_type
);
1849 if (opts
->present
& DL_OPT_SB_THTYPE
)
1850 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1851 opts
->sb_pool_thtype
);
1852 if (opts
->present
& DL_OPT_SB_TH
)
1853 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1854 opts
->sb_threshold
);
1855 if (opts
->present
& DL_OPT_SB_TC
)
1856 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1858 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1859 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1860 opts
->eswitch_mode
);
1861 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1862 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1863 opts
->eswitch_inline_mode
);
1864 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1865 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1866 opts
->dpipe_table_name
);
1867 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1868 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1869 opts
->dpipe_counters_enabled
);
1870 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1871 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1872 opts
->eswitch_encap_mode
);
1873 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1874 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1876 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1877 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1878 opts
->resource_size
);
1879 if (opts
->present
& DL_OPT_PARAM_NAME
)
1880 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1882 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1883 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1885 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1886 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1887 opts
->region_snapshot_id
);
1888 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1889 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1890 opts
->region_address
);
1891 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1892 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1893 opts
->region_length
);
1894 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1895 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1896 opts
->flash_file_name
);
1897 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1898 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1899 opts
->flash_component
);
1900 if (opts
->present
& DL_OPT_FLASH_OVERWRITE
)
1901 dl_flash_update_overwrite_put(nlh
, opts
);
1902 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1903 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1904 opts
->reporter_name
);
1905 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1906 mnl_attr_put_u64(nlh
,
1907 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1908 opts
->reporter_graceful_period
);
1909 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1910 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1911 opts
->reporter_auto_recover
);
1912 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)
1913 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
1914 opts
->reporter_auto_dump
);
1915 if (opts
->present
& DL_OPT_TRAP_NAME
)
1916 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1918 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1919 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1920 opts
->trap_group_name
);
1921 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1922 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1924 if (opts
->present
& DL_OPT_NETNS
)
1925 mnl_attr_put_u32(nlh
,
1926 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1927 DEVLINK_ATTR_NETNS_FD
,
1929 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1930 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1931 opts
->trap_policer_id
);
1932 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1933 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1934 opts
->trap_policer_rate
);
1935 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1936 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1937 opts
->trap_policer_burst
);
1938 if (opts
->present
& DL_OPT_PORT_FUNCTION_HW_ADDR
)
1939 dl_function_attr_put(nlh
, opts
);
1942 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1943 uint64_t o_required
, uint64_t o_optional
)
1947 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1950 dl_opts_put(nlh
, dl
);
1954 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1956 struct dl_opts
*opts
= &dl
->opts
;
1957 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1958 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1959 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1960 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1962 if (opts
->present
& DL_OPT_HANDLE
&&
1963 attr_bus_name
&& attr_dev_name
) {
1964 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1965 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1967 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1968 strcmp(dev_name
, opts
->dev_name
) != 0)
1971 if (opts
->present
& DL_OPT_HANDLEP
&&
1972 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1973 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1974 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1975 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1977 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1978 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1979 port_index
!= opts
->port_index
)
1982 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1983 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1985 if (sb_index
!= opts
->sb_index
)
1991 static void cmd_dev_help(void)
1993 pr_err("Usage: devlink dev show [ DEV ]\n");
1994 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1995 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1996 pr_err(" [ encap-mode { none | basic } ]\n");
1997 pr_err(" devlink dev eswitch show DEV\n");
1998 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1999 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
2000 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2001 pr_err(" devlink dev info [ DEV ]\n");
2002 pr_err(" devlink dev flash DEV file PATH [ component NAME ] [ overwrite SECTION ]\n");
2005 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
2006 const char *dev_name
)
2008 if (!dl
->arr_last
.present
)
2010 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
2011 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
2014 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
2015 const char *dev_name
)
2017 dl
->arr_last
.present
= true;
2018 free(dl
->arr_last
.dev_name
);
2019 free(dl
->arr_last
.bus_name
);
2020 dl
->arr_last
.bus_name
= strdup(bus_name
);
2021 dl
->arr_last
.dev_name
= strdup(dev_name
);
2024 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
2025 const char *dev_name
)
2027 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
2030 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
2031 const char *dev_name
)
2033 return dl
->arr_last
.present
&&
2034 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
2037 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
2038 bool content
, bool array
)
2040 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2041 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2044 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
2046 if (dl
->json_output
) {
2048 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2049 close_json_array(PRINT_JSON
, NULL
);
2050 if (should_arr_last_handle_start(dl
, bus_name
,
2052 open_json_array(PRINT_JSON
, buf
);
2053 open_json_object(NULL
);
2054 arr_last_handle_set(dl
, bus_name
, dev_name
);
2056 open_json_object(NULL
);
2059 open_json_object(buf
);
2063 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
2064 __pr_out_indent_dec();
2065 if (should_arr_last_handle_start(dl
, bus_name
,
2067 pr_out("%s%s", buf
, content
? ":" : "");
2069 __pr_out_indent_inc();
2070 arr_last_handle_set(dl
, bus_name
, dev_name
);
2073 pr_out("%s%s", buf
, content
? ":" : "");
2078 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
2080 __pr_out_handle_start(dl
, tb
, true, true);
2083 static void pr_out_handle_end(struct dl
*dl
)
2085 if (dl
->json_output
)
2086 close_json_object();
2091 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
2093 __pr_out_handle_start(dl
, tb
, false, false);
2094 pr_out_handle_end(dl
);
2097 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
2098 const char *dev_name
, uint32_t port_index
)
2100 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
2101 dl
->arr_last
.port_index
== port_index
;
2104 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
2105 const char *dev_name
, uint32_t port_index
)
2107 arr_last_handle_set(dl
, bus_name
, dev_name
);
2108 dl
->arr_last
.port_index
= port_index
;
2111 static bool should_arr_last_port_handle_start(struct dl
*dl
,
2112 const char *bus_name
,
2113 const char *dev_name
,
2114 uint32_t port_index
)
2116 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2119 static bool should_arr_last_port_handle_end(struct dl
*dl
,
2120 const char *bus_name
,
2121 const char *dev_name
,
2122 uint32_t port_index
)
2124 return dl
->arr_last
.present
&&
2125 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
2128 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
2129 const char *dev_name
,
2130 uint32_t port_index
, bool try_nice
,
2133 static char buf
[64];
2134 char *ifname
= NULL
;
2136 if (dl
->no_nice_names
|| !try_nice
||
2137 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
2138 port_index
, &ifname
) != 0)
2139 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
2141 sprintf(buf
, "%s", ifname
);
2143 if (dl
->json_output
) {
2145 if (should_arr_last_port_handle_end(dl
, bus_name
,
2148 close_json_array(PRINT_JSON
, NULL
);
2149 if (should_arr_last_port_handle_start(dl
, bus_name
,
2152 open_json_array(PRINT_JSON
, buf
);
2153 open_json_object(NULL
);
2154 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
2157 open_json_object(NULL
);
2160 open_json_object(buf
);
2164 if (should_arr_last_port_handle_end(dl
, bus_name
, dev_name
, port_index
))
2165 __pr_out_indent_dec();
2166 if (should_arr_last_port_handle_start(dl
, bus_name
,
2167 dev_name
, port_index
)) {
2170 __pr_out_indent_inc();
2171 arr_last_port_handle_set(dl
, bus_name
, dev_name
, port_index
);
2179 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2181 const char *bus_name
;
2182 const char *dev_name
;
2183 uint32_t port_index
;
2185 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2186 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2187 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2188 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
2191 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
2193 const char *bus_name
;
2194 const char *dev_name
;
2195 uint32_t port_index
;
2197 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2198 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2199 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2200 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
2203 static void pr_out_port_handle_end(struct dl
*dl
)
2205 if (dl
->json_output
)
2206 close_json_object();
2211 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
2213 if (dl
->json_output
) {
2214 print_uint(PRINT_JSON
, "address", NULL
, addr
);
2215 open_json_array(PRINT_JSON
, "data");
2219 static void pr_out_region_chunk_end(struct dl
*dl
)
2221 if (dl
->json_output
)
2222 close_json_array(PRINT_JSON
, NULL
);
2225 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2228 static uint64_t align_val
;
2231 pr_out_region_chunk_start(dl
, addr
);
2233 if (!dl
->json_output
)
2234 if (!(align_val
% 16))
2235 pr_out("%s%016"PRIx64
" ",
2236 align_val
? "\n" : "",
2241 if (dl
->json_output
)
2242 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2244 pr_out("%02x ", data
[i
]);
2249 pr_out_region_chunk_end(dl
);
2252 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2254 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2260 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2261 if (err
!= MNL_CB_OK
)
2264 pr_out_object_start(dl
, "stats");
2265 pr_out_object_start(dl
, "rx");
2266 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2267 pr_out_u64(dl
, "bytes",
2268 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2269 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2270 pr_out_u64(dl
, "packets",
2271 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2272 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2273 pr_out_u64(dl
, "dropped",
2274 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2275 pr_out_object_end(dl
);
2276 pr_out_object_end(dl
);
2279 static const char *param_cmode_name(uint8_t cmode
)
2282 case DEVLINK_PARAM_CMODE_RUNTIME
:
2283 return PARAM_CMODE_RUNTIME_STR
;
2284 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2285 return PARAM_CMODE_DRIVERINIT_STR
;
2286 case DEVLINK_PARAM_CMODE_PERMANENT
:
2287 return PARAM_CMODE_PERMANENT_STR
;
2288 default: return "<unknown type>";
2292 static const char *eswitch_mode_name(uint32_t mode
)
2295 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2296 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2297 default: return "<unknown mode>";
2301 static const char *eswitch_inline_mode_name(uint32_t mode
)
2304 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2305 return ESWITCH_INLINE_MODE_NONE
;
2306 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2307 return ESWITCH_INLINE_MODE_LINK
;
2308 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2309 return ESWITCH_INLINE_MODE_NETWORK
;
2310 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2311 return ESWITCH_INLINE_MODE_TRANSPORT
;
2313 return "<unknown mode>";
2317 static const char *eswitch_encap_mode_name(uint32_t mode
)
2320 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2321 return ESWITCH_ENCAP_MODE_NONE
;
2322 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2323 return ESWITCH_ENCAP_MODE_BASIC
;
2325 return "<unknown mode>";
2329 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2331 __pr_out_handle_start(dl
, tb
, true, false);
2333 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2334 check_indent_newline(dl
);
2335 print_string(PRINT_ANY
, "mode", "mode %s",
2336 eswitch_mode_name(mnl_attr_get_u16(
2337 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2339 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2340 check_indent_newline(dl
);
2341 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2342 eswitch_inline_mode_name(mnl_attr_get_u8(
2343 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2345 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2346 check_indent_newline(dl
);
2347 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2348 eswitch_encap_mode_name(mnl_attr_get_u8(
2349 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2352 pr_out_handle_end(dl
);
2355 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2357 struct dl
*dl
= data
;
2358 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2359 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2361 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2362 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2363 return MNL_CB_ERROR
;
2364 pr_out_eswitch(dl
, tb
);
2368 static int cmd_dev_eswitch_show(struct dl
*dl
)
2370 struct nlmsghdr
*nlh
;
2373 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2374 NLM_F_REQUEST
| NLM_F_ACK
);
2376 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2380 pr_out_section_start(dl
, "dev");
2381 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2382 pr_out_section_end(dl
);
2386 static int cmd_dev_eswitch_set(struct dl
*dl
)
2388 struct nlmsghdr
*nlh
;
2391 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2392 NLM_F_REQUEST
| NLM_F_ACK
);
2394 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2395 DL_OPT_ESWITCH_MODE
|
2396 DL_OPT_ESWITCH_INLINE_MODE
|
2397 DL_OPT_ESWITCH_ENCAP_MODE
);
2402 if (dl
->opts
.present
== 1) {
2403 pr_err("Need to set at least one option\n");
2407 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2410 static int cmd_dev_eswitch(struct dl
*dl
)
2412 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2415 } else if (dl_argv_match(dl
, "set")) {
2417 return cmd_dev_eswitch_set(dl
);
2418 } else if (dl_argv_match(dl
, "show")) {
2420 return cmd_dev_eswitch_show(dl
);
2422 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2426 struct param_val_conv
{
2432 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2433 uint32_t len
, const char *name
)
2437 for (i
= 0; i
< len
; i
++)
2438 if (!strcmp(param_val_conv
[i
].name
, name
))
2445 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2446 uint32_t len
, const char *name
, const char *vstr
,
2451 for (i
= 0; i
< len
; i
++)
2452 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2453 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2454 *vuint
= param_val_conv
[i
].vuint
;
2462 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2463 uint32_t len
, const char *name
, uint32_t vuint
,
2468 for (i
= 0; i
< len
; i
++)
2469 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2470 param_val_conv
[i
].vuint
== vuint
) {
2471 *vstr
= param_val_conv
[i
].vstr
;
2478 static const struct param_val_conv param_val_conv
[] = {
2480 .name
= "fw_load_policy",
2482 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2485 .name
= "fw_load_policy",
2487 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2490 .name
= "fw_load_policy",
2492 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK
,
2495 .name
= "reset_dev_on_drv_probe",
2497 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2500 .name
= "fw_load_policy",
2502 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2505 .name
= "reset_dev_on_drv_probe",
2507 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2510 .name
= "reset_dev_on_drv_probe",
2512 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2515 .name
= "reset_dev_on_drv_probe",
2517 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2521 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2523 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2524 int nla_type
, struct nlattr
*nl
)
2526 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2527 struct nlattr
*val_attr
;
2532 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2533 if (err
!= MNL_CB_OK
)
2536 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2537 (nla_type
!= MNL_TYPE_FLAG
&&
2538 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2541 check_indent_newline(dl
);
2542 print_string(PRINT_ANY
, "cmode", "cmode %s",
2543 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2545 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2547 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2553 err
= param_val_conv_str_get(param_val_conv
,
2556 mnl_attr_get_u8(val_attr
),
2560 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2562 print_uint(PRINT_ANY
, "value", " value %u",
2563 mnl_attr_get_u8(val_attr
));
2568 err
= param_val_conv_str_get(param_val_conv
,
2571 mnl_attr_get_u16(val_attr
),
2575 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2577 print_uint(PRINT_ANY
, "value", " value %u",
2578 mnl_attr_get_u16(val_attr
));
2583 err
= param_val_conv_str_get(param_val_conv
,
2586 mnl_attr_get_u32(val_attr
),
2590 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2592 print_uint(PRINT_ANY
, "value", " value %u",
2593 mnl_attr_get_u32(val_attr
));
2596 case MNL_TYPE_STRING
:
2597 print_string(PRINT_ANY
, "value", " value %s",
2598 mnl_attr_get_str(val_attr
));
2601 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2606 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2608 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2609 struct nlattr
*param_value_attr
;
2610 const char *nla_name
;
2614 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2615 if (err
!= MNL_CB_OK
)
2617 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2618 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2619 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2623 pr_out_handle_start_arr(dl
, tb
);
2625 __pr_out_handle_start(dl
, tb
, true, false);
2627 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2629 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2630 check_indent_newline(dl
);
2631 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2632 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2633 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2635 print_string(PRINT_ANY
, "type", "type %s", "generic");
2637 pr_out_array_start(dl
, "values");
2638 mnl_attr_for_each_nested(param_value_attr
,
2639 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2640 pr_out_entry_start(dl
);
2641 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2642 pr_out_entry_end(dl
);
2644 pr_out_array_end(dl
);
2645 pr_out_handle_end(dl
);
2648 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2650 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2651 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2652 struct dl
*dl
= data
;
2654 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2655 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2656 !tb
[DEVLINK_ATTR_PARAM
])
2657 return MNL_CB_ERROR
;
2658 pr_out_param(dl
, tb
, true);
2674 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2676 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2677 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2678 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2679 struct nlattr
*param_value_attr
;
2680 enum devlink_param_cmode cmode
;
2681 struct param_ctx
*ctx
= data
;
2682 struct dl
*dl
= ctx
->dl
;
2686 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2687 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2688 !tb
[DEVLINK_ATTR_PARAM
])
2689 return MNL_CB_ERROR
;
2691 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2692 if (err
!= MNL_CB_OK
)
2693 return MNL_CB_ERROR
;
2695 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2696 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2697 return MNL_CB_ERROR
;
2699 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2700 mnl_attr_for_each_nested(param_value_attr
,
2701 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2702 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2703 struct nlattr
*val_attr
;
2705 err
= mnl_attr_parse_nested(param_value_attr
,
2706 attr_cb
, nla_value
);
2707 if (err
!= MNL_CB_OK
)
2708 return MNL_CB_ERROR
;
2710 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2711 (nla_type
!= MNL_TYPE_FLAG
&&
2712 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2713 return MNL_CB_ERROR
;
2715 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2716 if (cmode
== dl
->opts
.cmode
) {
2717 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2720 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2723 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2726 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2728 case MNL_TYPE_STRING
:
2729 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2732 ctx
->value
.vbool
= val_attr
? true : false;
2738 ctx
->nla_type
= nla_type
;
2742 static int cmd_dev_param_set(struct dl
*dl
)
2744 struct param_ctx ctx
= {};
2745 struct nlmsghdr
*nlh
;
2747 uint32_t val_u32
= 0;
2753 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2755 DL_OPT_PARAM_VALUE
|
2756 DL_OPT_PARAM_CMODE
, 0);
2760 /* Get value type */
2761 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2762 NLM_F_REQUEST
| NLM_F_ACK
);
2763 dl_opts_put(nlh
, dl
);
2766 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2770 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2771 NLM_F_REQUEST
| NLM_F_ACK
);
2772 dl_opts_put(nlh
, dl
);
2774 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2775 dl
->opts
.param_name
);
2777 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2778 switch (ctx
.nla_type
) {
2781 err
= param_val_conv_uint_get(param_val_conv
,
2783 dl
->opts
.param_name
,
2784 dl
->opts
.param_value
,
2788 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2791 goto err_param_value_parse
;
2792 if (val_u8
== ctx
.value
.vu8
)
2794 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2798 err
= param_val_conv_uint_get(param_val_conv
,
2800 dl
->opts
.param_name
,
2801 dl
->opts
.param_value
,
2805 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2808 goto err_param_value_parse
;
2809 if (val_u16
== ctx
.value
.vu16
)
2811 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2815 err
= param_val_conv_uint_get(param_val_conv
,
2817 dl
->opts
.param_name
,
2818 dl
->opts
.param_value
,
2821 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2823 goto err_param_value_parse
;
2824 if (val_u32
== ctx
.value
.vu32
)
2826 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2829 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2831 goto err_param_value_parse
;
2832 if (val_bool
== ctx
.value
.vbool
)
2835 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2838 case MNL_TYPE_STRING
:
2839 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2840 dl
->opts
.param_value
);
2841 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2845 printf("Value type not supported\n");
2848 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2850 err_param_value_parse
:
2851 pr_err("Value \"%s\" is not a number or not within range\n",
2852 dl
->opts
.param_value
);
2856 static int cmd_dev_param_show(struct dl
*dl
)
2858 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2859 struct nlmsghdr
*nlh
;
2862 if (dl_argc(dl
) == 0)
2863 flags
|= NLM_F_DUMP
;
2865 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2867 if (dl_argc(dl
) > 0) {
2868 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2869 DL_OPT_PARAM_NAME
, 0);
2874 pr_out_section_start(dl
, "param");
2875 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2876 pr_out_section_end(dl
);
2880 static int cmd_dev_param(struct dl
*dl
)
2882 if (dl_argv_match(dl
, "help")) {
2885 } else if (dl_argv_match(dl
, "show") ||
2886 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2888 return cmd_dev_param_show(dl
);
2889 } else if (dl_argv_match(dl
, "set")) {
2891 return cmd_dev_param_set(dl
);
2893 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2896 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2898 struct dl
*dl
= data
;
2899 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2900 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2901 uint8_t reload_failed
= 0;
2903 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2904 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2905 return MNL_CB_ERROR
;
2907 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2908 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2910 if (reload_failed
) {
2911 __pr_out_handle_start(dl
, tb
, true, false);
2912 check_indent_newline(dl
);
2913 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2914 pr_out_handle_end(dl
);
2916 pr_out_handle(dl
, tb
);
2922 static int cmd_dev_show(struct dl
*dl
)
2924 struct nlmsghdr
*nlh
;
2925 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2928 if (dl_argc(dl
) == 0)
2929 flags
|= NLM_F_DUMP
;
2931 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2933 if (dl_argc(dl
) > 0) {
2934 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2939 pr_out_section_start(dl
, "dev");
2940 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2941 pr_out_section_end(dl
);
2945 static int cmd_dev_reload(struct dl
*dl
)
2947 struct nlmsghdr
*nlh
;
2950 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2955 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2956 NLM_F_REQUEST
| NLM_F_ACK
);
2958 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2962 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2965 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2966 const char *name
, int type
)
2968 struct nlattr
*version
;
2970 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2971 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2972 const char *ver_value
;
2973 const char *ver_name
;
2976 if (mnl_attr_get_type(version
) != type
)
2979 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2980 if (err
!= MNL_CB_OK
)
2983 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2984 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2988 pr_out_object_start(dl
, name
);
2992 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2993 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2995 check_indent_newline(dl
);
2996 print_string_name_value(ver_name
, ver_value
);
2997 if (!dl
->json_output
)
3002 pr_out_object_end(dl
);
3005 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
3006 struct nlattr
**tb
, bool has_versions
)
3008 __pr_out_handle_start(dl
, tb
, true, false);
3010 __pr_out_indent_inc();
3011 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
3012 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
3014 if (!dl
->json_output
)
3016 check_indent_newline(dl
);
3017 print_string(PRINT_ANY
, "driver", "driver %s",
3018 mnl_attr_get_str(nla_drv
));
3021 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
3022 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
3024 if (!dl
->json_output
)
3026 check_indent_newline(dl
);
3027 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
3028 mnl_attr_get_str(nla_sn
));
3031 if (tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
]) {
3032 struct nlattr
*nla_bsn
= tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
];
3034 if (!dl
->json_output
)
3036 check_indent_newline(dl
);
3037 print_string(PRINT_ANY
, "board.serial_number", "board.serial_number %s",
3038 mnl_attr_get_str(nla_bsn
));
3040 __pr_out_indent_dec();
3043 pr_out_object_start(dl
, "versions");
3045 pr_out_versions_single(dl
, nlh
, "fixed",
3046 DEVLINK_ATTR_INFO_VERSION_FIXED
);
3047 pr_out_versions_single(dl
, nlh
, "running",
3048 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
3049 pr_out_versions_single(dl
, nlh
, "stored",
3050 DEVLINK_ATTR_INFO_VERSION_STORED
);
3052 pr_out_object_end(dl
);
3055 pr_out_handle_end(dl
);
3058 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3060 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3061 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3062 bool has_versions
, has_info
;
3063 struct dl
*dl
= data
;
3065 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3067 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3068 return MNL_CB_ERROR
;
3070 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
3071 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
3072 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
3073 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
3074 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
3075 tb
[DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
] ||
3079 pr_out_info(dl
, nlh
, tb
, has_versions
);
3084 static int cmd_dev_info(struct dl
*dl
)
3086 struct nlmsghdr
*nlh
;
3087 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3090 if (dl_argv_match(dl
, "help")) {
3095 if (dl_argc(dl
) == 0)
3096 flags
|= NLM_F_DUMP
;
3098 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
3100 if (dl_argc(dl
) > 0) {
3101 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3106 pr_out_section_start(dl
, "info");
3107 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
3108 pr_out_section_end(dl
);
3112 struct cmd_dev_flash_status_ctx
{
3114 struct timespec time_of_last_status
;
3115 uint64_t status_msg_timeout
;
3116 size_t elapsed_time_msg_len
;
3118 char *last_component
;
3119 uint8_t not_first
:1,
3125 static int nullstrcmp(const char *str1
, const char *str2
)
3128 return strcmp(str1
, str2
);
3131 return str1
? 1 : -1;
3134 static void cmd_dev_flash_clear_elapsed_time(struct cmd_dev_flash_status_ctx
*ctx
)
3138 for (i
= 0; i
< ctx
->elapsed_time_msg_len
; i
++)
3139 pr_out_tty("\b \b");
3141 ctx
->elapsed_time_msg_len
= 0;
3144 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
3146 struct cmd_dev_flash_status_ctx
*ctx
= data
;
3147 struct dl_opts
*opts
= &ctx
->dl
->opts
;
3148 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3149 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3150 const char *component
= NULL
;
3151 uint64_t done
= 0, total
= 0;
3152 const char *msg
= NULL
;
3153 const char *bus_name
;
3154 const char *dev_name
;
3156 cmd_dev_flash_clear_elapsed_time(ctx
);
3158 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
3159 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3162 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3163 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3164 return MNL_CB_ERROR
;
3165 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3166 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3167 if (strcmp(bus_name
, opts
->bus_name
) ||
3168 strcmp(dev_name
, opts
->dev_name
))
3169 return MNL_CB_ERROR
;
3171 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3173 free(ctx
->last_msg
);
3174 free(ctx
->last_component
);
3175 ctx
->received_end
= 1;
3179 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3180 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3181 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3182 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3183 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3184 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3185 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3186 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3187 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
])
3188 ctx
->status_msg_timeout
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
]);
3190 ctx
->status_msg_timeout
= 0;
3192 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3193 !nullstrcmp(component
, ctx
->last_component
) &&
3194 ctx
->last_pc
&& ctx
->not_first
) {
3195 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3197 /* only update the last status timestamp if the message changed */
3198 clock_gettime(CLOCK_MONOTONIC
, &ctx
->time_of_last_status
);
3203 pr_out("[%s] ", component
);
3204 free(ctx
->last_component
);
3205 ctx
->last_component
= strdup(component
);
3209 free(ctx
->last_msg
);
3210 ctx
->last_msg
= strdup(msg
);
3214 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3225 static void cmd_dev_flash_time_elapsed(struct cmd_dev_flash_status_ctx
*ctx
)
3227 struct timespec now
, res
;
3229 clock_gettime(CLOCK_MONOTONIC
, &now
);
3231 res
.tv_sec
= now
.tv_sec
- ctx
->time_of_last_status
.tv_sec
;
3232 res
.tv_nsec
= now
.tv_nsec
- ctx
->time_of_last_status
.tv_nsec
;
3233 if (res
.tv_nsec
< 0) {
3235 res
.tv_nsec
+= 1000000000L;
3238 /* Only begin displaying an elapsed time message if we've waited a few
3239 * seconds with no response, or the status message included a timeout
3242 if (res
.tv_sec
> 2 || ctx
->status_msg_timeout
) {
3243 uint64_t elapsed_m
, elapsed_s
;
3247 /* clear the last elapsed time message, if we have one */
3248 cmd_dev_flash_clear_elapsed_time(ctx
);
3250 elapsed_m
= res
.tv_sec
/ 60;
3251 elapsed_s
= res
.tv_sec
% 60;
3254 * If we've elapsed a few seconds without receiving any status
3255 * notification from the device, we display a time elapsed
3256 * message. This has a few possible formats:
3258 * 1) just time elapsed, when no timeout was provided
3260 * 2) time elapsed out of a timeout that came from the device
3261 * driver via DEVLINK_CMD_FLASH_UPDATE_STATUS_TIMEOUT
3262 * " ( Xm Ys : Am Ys)"
3263 * 3) time elapsed if we still receive no status after
3264 * reaching the provided timeout.
3265 * " ( Xm Ys : timeout reached )"
3267 if (!ctx
->status_msg_timeout
) {
3268 len
= snprintf(msg
, sizeof(msg
),
3269 " ( %lum %lus )", elapsed_m
, elapsed_s
);
3270 } else if (res
.tv_sec
<= ctx
->status_msg_timeout
) {
3271 uint64_t timeout_m
, timeout_s
;
3273 timeout_m
= ctx
->status_msg_timeout
/ 60;
3274 timeout_s
= ctx
->status_msg_timeout
% 60;
3276 len
= snprintf(msg
, sizeof(msg
),
3277 " ( %lum %lus : %lum %lus )",
3278 elapsed_m
, elapsed_s
, timeout_m
, timeout_s
);
3280 len
= snprintf(msg
, sizeof(msg
),
3281 " ( %lum %lus : timeout reached )", elapsed_m
, elapsed_s
);
3284 ctx
->elapsed_time_msg_len
= len
;
3286 pr_out_tty("%s", msg
);
3291 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3292 struct mnlg_socket
*nlg_ntf
,
3295 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3296 struct timeval timeout
;
3303 for (i
= 0; i
< 3; i
++)
3305 FD_SET(pipe_r
, &fds
[0]);
3307 FD_SET(nlfd
, &fds
[0]);
3311 /* select only for a short while (1/10th of a second) in order to
3312 * allow periodically updating the screen with an elapsed time
3316 timeout
.tv_usec
= 100000;
3318 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], &timeout
) < 0) {
3321 pr_err("select() failed\n");
3324 if (FD_ISSET(nlfd
, &fds
[0])) {
3325 err
= _mnlg_socket_recv_run(nlg_ntf
,
3326 cmd_dev_flash_status_cb
, ctx
);
3330 if (FD_ISSET(pipe_r
, &fds
[0])) {
3331 err
= read(pipe_r
, &err2
, sizeof(err2
));
3333 pr_err("Failed to read pipe\n");
3338 ctx
->flash_done
= 1;
3340 cmd_dev_flash_time_elapsed(ctx
);
3345 static int cmd_dev_flash(struct dl
*dl
)
3347 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3348 struct mnlg_socket
*nlg_ntf
;
3349 struct nlmsghdr
*nlh
;
3355 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3360 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3361 NLM_F_REQUEST
| NLM_F_ACK
);
3363 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3364 DL_OPT_FLASH_COMPONENT
| DL_OPT_FLASH_OVERWRITE
);
3368 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3372 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3376 err
= pipe(pipe_fds
);
3379 pipe_r
= pipe_fds
[0];
3380 pipe_w
= pipe_fds
[1];
3388 /* In child, just execute the flash and pass returned
3389 * value through pipe once it is done.
3394 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3395 cc
= write(pipe_w
, &err
, sizeof(err
));
3397 exit(cc
!= sizeof(err
));
3401 /* initialize starting time to allow comparison for when to begin
3402 * displaying a time elapsed message.
3404 clock_gettime(CLOCK_MONOTONIC
, &ctx
.time_of_last_status
);
3407 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3410 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3412 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3415 mnlg_socket_close(nlg_ntf
);
3419 static int cmd_dev(struct dl
*dl
)
3421 if (dl_argv_match(dl
, "help")) {
3424 } else if (dl_argv_match(dl
, "show") ||
3425 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3427 return cmd_dev_show(dl
);
3428 } else if (dl_argv_match(dl
, "eswitch")) {
3430 return cmd_dev_eswitch(dl
);
3431 } else if (dl_argv_match(dl
, "reload")) {
3433 return cmd_dev_reload(dl
);
3434 } else if (dl_argv_match(dl
, "param")) {
3436 return cmd_dev_param(dl
);
3437 } else if (dl_argv_match(dl
, "info")) {
3439 return cmd_dev_info(dl
);
3440 } else if (dl_argv_match(dl
, "flash")) {
3442 return cmd_dev_flash(dl
);
3444 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3448 static void cmd_port_help(void)
3450 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3451 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3452 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3453 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3454 pr_err(" devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3455 pr_err(" devlink port health show [ DEV/PORT_INDEX reporter REPORTER_NAME ]\n");
3458 static const char *port_type_name(uint32_t type
)
3461 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3462 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3463 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3464 case DEVLINK_PORT_TYPE_IB
: return "ib";
3465 default: return "<unknown type>";
3469 static const char *port_flavour_name(uint16_t flavour
)
3472 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3474 case DEVLINK_PORT_FLAVOUR_CPU
:
3476 case DEVLINK_PORT_FLAVOUR_DSA
:
3478 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3480 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3482 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3485 return "<unknown flavour>";
3489 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3493 if (tb
[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
])
3494 print_uint(PRINT_ANY
, "controller", " controller %u",
3495 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
]));
3496 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3497 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3498 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3500 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3501 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3502 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3504 if (tb
[DEVLINK_ATTR_PORT_EXTERNAL
]) {
3507 external
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_PORT_EXTERNAL
]);
3508 print_bool(PRINT_ANY
, "external", " external %s", external
);
3512 static void pr_out_port_function(struct dl
*dl
, struct nlattr
**tb_port
)
3514 struct nlattr
*tb
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {};
3515 unsigned char *data
;
3516 SPRINT_BUF(hw_addr
);
3520 if (!tb_port
[DEVLINK_ATTR_PORT_FUNCTION
])
3523 err
= mnl_attr_parse_nested(tb_port
[DEVLINK_ATTR_PORT_FUNCTION
],
3524 function_attr_cb
, tb
);
3525 if (err
!= MNL_CB_OK
)
3528 if (!tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
])
3531 len
= mnl_attr_get_payload_len(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3532 data
= mnl_attr_get_payload(tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
]);
3534 pr_out_object_start(dl
, "function");
3535 check_indent_newline(dl
);
3536 print_string(PRINT_ANY
, "hw_addr", "hw_addr %s",
3537 ll_addr_n2a(data
, len
, 0, hw_addr
, sizeof(hw_addr
)));
3538 if (!dl
->json_output
)
3539 __pr_out_indent_dec();
3540 pr_out_object_end(dl
);
3543 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3545 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3546 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3548 pr_out_port_handle_start(dl
, tb
, false);
3549 check_indent_newline(dl
);
3551 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3553 print_string(PRINT_ANY
, "type", "type %s",
3554 port_type_name(port_type
));
3556 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3558 if (port_type
!= des_port_type
)
3559 print_string(PRINT_ANY
, "des_type", " des_type %s",
3560 port_type_name(des_port_type
));
3563 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3564 print_string(PRINT_ANY
, "netdev", " netdev %s",
3565 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3567 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3568 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3569 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3571 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3572 uint16_t port_flavour
=
3573 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3575 print_string(PRINT_ANY
, "flavour", " flavour %s",
3576 port_flavour_name(port_flavour
));
3578 switch (port_flavour
) {
3579 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3580 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3581 pr_out_port_pfvf_num(dl
, tb
);
3587 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3588 uint32_t port_number
;
3590 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3591 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3593 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3594 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3595 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3596 if (tb
[DEVLINK_ATTR_PORT_SPLITTABLE
])
3597 print_bool(PRINT_ANY
, "splittable", " splittable %s",
3598 mnl_attr_get_u8(tb
[DEVLINK_ATTR_PORT_SPLITTABLE
]));
3599 if (tb
[DEVLINK_ATTR_PORT_LANES
])
3600 print_uint(PRINT_ANY
, "lanes", " lanes %u",
3601 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_LANES
]));
3603 pr_out_port_function(dl
, tb
);
3604 pr_out_port_handle_end(dl
);
3607 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3609 struct dl
*dl
= data
;
3610 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3611 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3613 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3614 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3615 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3616 return MNL_CB_ERROR
;
3617 pr_out_port(dl
, tb
);
3621 static int cmd_port_show(struct dl
*dl
)
3623 struct nlmsghdr
*nlh
;
3624 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3627 if (dl_argc(dl
) == 0)
3628 flags
|= NLM_F_DUMP
;
3630 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3632 if (dl_argc(dl
) > 0) {
3633 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3638 pr_out_section_start(dl
, "port");
3639 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3640 pr_out_section_end(dl
);
3644 static int cmd_port_set(struct dl
*dl
)
3646 struct nlmsghdr
*nlh
;
3649 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3650 NLM_F_REQUEST
| NLM_F_ACK
);
3652 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3656 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3659 static int cmd_port_split(struct dl
*dl
)
3661 struct nlmsghdr
*nlh
;
3664 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3665 NLM_F_REQUEST
| NLM_F_ACK
);
3667 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3671 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3674 static int cmd_port_unsplit(struct dl
*dl
)
3676 struct nlmsghdr
*nlh
;
3679 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3680 NLM_F_REQUEST
| NLM_F_ACK
);
3682 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3686 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3689 static void cmd_port_function_help(void)
3691 pr_err("Usage: devlink port function set DEV/PORT_INDEX [ hw_addr ADDR ]\n");
3694 static int cmd_port_function_set(struct dl
*dl
)
3696 struct nlmsghdr
*nlh
;
3699 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
, NLM_F_REQUEST
| NLM_F_ACK
);
3701 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_FUNCTION_HW_ADDR
, 0);
3705 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3708 static int cmd_port_function(struct dl
*dl
)
3710 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3711 cmd_port_function_help();
3713 } else if (dl_argv_match(dl
, "set")) {
3715 return cmd_port_function_set(dl
);
3717 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3721 static int cmd_health(struct dl
*dl
);
3722 static int __cmd_health_show(struct dl
*dl
, bool show_device
, bool show_port
);
3724 static int cmd_port(struct dl
*dl
)
3726 if (dl_argv_match(dl
, "help")) {
3729 } else if (dl_argv_match(dl
, "show") ||
3730 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3732 return cmd_port_show(dl
);
3733 } else if (dl_argv_match(dl
, "set")) {
3735 return cmd_port_set(dl
);
3736 } else if (dl_argv_match(dl
, "split")) {
3738 return cmd_port_split(dl
);
3739 } else if (dl_argv_match(dl
, "unsplit")) {
3741 return cmd_port_unsplit(dl
);
3742 } else if (dl_argv_match(dl
, "function")) {
3744 return cmd_port_function(dl
);
3745 } else if (dl_argv_match(dl
, "health")) {
3747 if (dl_argv_match(dl
, "list") || dl_no_arg(dl
)
3748 || (dl_argv_match(dl
, "show") && dl_argc(dl
) == 1)) {
3750 return __cmd_health_show(dl
, false, true);
3752 return cmd_health(dl
);
3755 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3759 static void cmd_sb_help(void)
3761 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3762 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3763 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3764 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3765 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3766 pr_err(" pool POOL_INDEX ]\n");
3767 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3768 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3769 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3770 pr_err(" type { ingress | egress } ]\n");
3771 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3772 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3773 pr_err(" th THRESHOLD\n");
3774 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3775 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3776 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3779 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3781 pr_out_handle_start_arr(dl
, tb
);
3782 check_indent_newline(dl
);
3783 print_uint(PRINT_ANY
, "sb", "sb %u",
3784 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3785 print_uint(PRINT_ANY
, "size", " size %u",
3786 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3787 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3788 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3789 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3790 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3791 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3792 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3793 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3794 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3795 pr_out_handle_end(dl
);
3798 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3800 struct dl
*dl
= data
;
3801 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3802 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3804 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3805 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3806 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3807 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3808 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3809 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3810 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3811 return MNL_CB_ERROR
;
3816 static int cmd_sb_show(struct dl
*dl
)
3818 struct nlmsghdr
*nlh
;
3819 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3822 if (dl_argc(dl
) == 0)
3823 flags
|= NLM_F_DUMP
;
3825 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3827 if (dl_argc(dl
) > 0) {
3828 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3833 pr_out_section_start(dl
, "sb");
3834 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3835 pr_out_section_end(dl
);
3839 static const char *pool_type_name(uint8_t type
)
3842 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3843 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3844 default: return "<unknown type>";
3848 static const char *threshold_type_name(uint8_t type
)
3851 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3852 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3853 default: return "<unknown type>";
3857 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3859 pr_out_handle_start_arr(dl
, tb
);
3860 check_indent_newline(dl
);
3861 print_uint(PRINT_ANY
, "sb", "sb %u",
3862 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3863 print_uint(PRINT_ANY
, "pool", " pool %u",
3864 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3865 print_string(PRINT_ANY
, "type", " type %s",
3866 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3867 print_uint(PRINT_ANY
, "size", " size %u",
3868 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3869 print_string(PRINT_ANY
, "thtype", " thtype %s",
3870 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3871 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3872 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3873 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3874 pr_out_handle_end(dl
);
3877 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3879 struct dl
*dl
= data
;
3880 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3881 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3883 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3884 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3885 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3886 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3887 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3888 return MNL_CB_ERROR
;
3889 pr_out_sb_pool(dl
, tb
);
3893 static int cmd_sb_pool_show(struct dl
*dl
)
3895 struct nlmsghdr
*nlh
;
3896 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3899 if (dl_argc(dl
) == 0)
3900 flags
|= NLM_F_DUMP
;
3902 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3904 if (dl_argc(dl
) > 0) {
3905 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3911 pr_out_section_start(dl
, "pool");
3912 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3913 pr_out_section_end(dl
);
3917 static int cmd_sb_pool_set(struct dl
*dl
)
3919 struct nlmsghdr
*nlh
;
3922 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3923 NLM_F_REQUEST
| NLM_F_ACK
);
3925 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3926 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3930 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3933 static int cmd_sb_pool(struct dl
*dl
)
3935 if (dl_argv_match(dl
, "help")) {
3938 } else if (dl_argv_match(dl
, "show") ||
3939 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3941 return cmd_sb_pool_show(dl
);
3942 } else if (dl_argv_match(dl
, "set")) {
3944 return cmd_sb_pool_set(dl
);
3946 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3950 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3952 pr_out_port_handle_start_arr(dl
, tb
, true);
3953 check_indent_newline(dl
);
3954 print_uint(PRINT_ANY
, "sb", "sb %u",
3955 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3956 print_uint(PRINT_ANY
, "pool", " pool %u",
3957 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3958 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3959 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3960 pr_out_port_handle_end(dl
);
3963 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3965 struct dl
*dl
= data
;
3966 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3967 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3969 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3970 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3971 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3972 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3973 return MNL_CB_ERROR
;
3974 pr_out_sb_port_pool(dl
, tb
);
3978 static int cmd_sb_port_pool_show(struct dl
*dl
)
3980 struct nlmsghdr
*nlh
;
3981 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3984 if (dl_argc(dl
) == 0)
3985 flags
|= NLM_F_DUMP
;
3987 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3989 if (dl_argc(dl
) > 0) {
3990 err
= dl_argv_parse_put(nlh
, dl
,
3991 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3997 pr_out_section_start(dl
, "port_pool");
3998 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3999 pr_out_section_end(dl
);
4003 static int cmd_sb_port_pool_set(struct dl
*dl
)
4005 struct nlmsghdr
*nlh
;
4008 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
4009 NLM_F_REQUEST
| NLM_F_ACK
);
4011 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
4012 DL_OPT_SB_TH
, DL_OPT_SB
);
4016 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4019 static int cmd_sb_port_pool(struct dl
*dl
)
4021 if (dl_argv_match(dl
, "help")) {
4024 } else if (dl_argv_match(dl
, "show") ||
4025 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4027 return cmd_sb_port_pool_show(dl
);
4028 } else if (dl_argv_match(dl
, "set")) {
4030 return cmd_sb_port_pool_set(dl
);
4032 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4036 static int cmd_sb_port(struct dl
*dl
)
4038 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4041 } else if (dl_argv_match(dl
, "pool")) {
4043 return cmd_sb_port_pool(dl
);
4045 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4049 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
4051 pr_out_port_handle_start_arr(dl
, tb
, true);
4052 check_indent_newline(dl
);
4053 print_uint(PRINT_ANY
, "sb", "sb %u",
4054 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
4055 print_uint(PRINT_ANY
, "tc", " tc %u",
4056 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
4057 print_string(PRINT_ANY
, "type", " type %s",
4058 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
4059 print_uint(PRINT_ANY
, "pool", " pool %u",
4060 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
4061 print_uint(PRINT_ANY
, "threshold", " threshold %u",
4062 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
4063 pr_out_port_handle_end(dl
);
4066 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4068 struct dl
*dl
= data
;
4069 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4070 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4072 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4073 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4074 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4075 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4076 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
4077 return MNL_CB_ERROR
;
4078 pr_out_sb_tc_bind(dl
, tb
);
4082 static int cmd_sb_tc_bind_show(struct dl
*dl
)
4084 struct nlmsghdr
*nlh
;
4085 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4088 if (dl_argc(dl
) == 0)
4089 flags
|= NLM_F_DUMP
;
4091 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4093 if (dl_argc(dl
) > 0) {
4094 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
4095 DL_OPT_SB_TYPE
, DL_OPT_SB
);
4100 pr_out_section_start(dl
, "tc_bind");
4101 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
4102 pr_out_section_end(dl
);
4106 static int cmd_sb_tc_bind_set(struct dl
*dl
)
4108 struct nlmsghdr
*nlh
;
4111 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
4112 NLM_F_REQUEST
| NLM_F_ACK
);
4114 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
4115 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
4120 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4123 static int cmd_sb_tc_bind(struct dl
*dl
)
4125 if (dl_argv_match(dl
, "help")) {
4128 } else if (dl_argv_match(dl
, "show") ||
4129 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4131 return cmd_sb_tc_bind_show(dl
);
4132 } else if (dl_argv_match(dl
, "set")) {
4134 return cmd_sb_tc_bind_set(dl
);
4136 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4140 static int cmd_sb_tc(struct dl
*dl
)
4142 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4145 } else if (dl_argv_match(dl
, "bind")) {
4147 return cmd_sb_tc_bind(dl
);
4149 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4154 struct list_head list
;
4158 uint32_t bound_pool_index
;
4162 struct list_head list
;
4165 uint32_t port_index
;
4167 struct list_head pool_list
;
4168 struct list_head ing_tc_list
;
4169 struct list_head eg_tc_list
;
4175 struct list_head port_list
;
4178 static struct occ_item
*occ_item_alloc(void)
4180 return calloc(1, sizeof(struct occ_item
));
4183 static void occ_item_free(struct occ_item
*occ_item
)
4188 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
4190 struct occ_port
*occ_port
;
4192 occ_port
= calloc(1, sizeof(*occ_port
));
4195 occ_port
->port_index
= port_index
;
4196 INIT_LIST_HEAD(&occ_port
->pool_list
);
4197 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
4198 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
4202 static void occ_port_free(struct occ_port
*occ_port
)
4204 struct occ_item
*occ_item
, *tmp
;
4206 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
4207 occ_item_free(occ_item
);
4208 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
4209 occ_item_free(occ_item
);
4210 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
4211 occ_item_free(occ_item
);
4214 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
4216 struct occ_show
*occ_show
;
4218 occ_show
= calloc(1, sizeof(*occ_show
));
4222 INIT_LIST_HEAD(&occ_show
->port_list
);
4226 static void occ_show_free(struct occ_show
*occ_show
)
4228 struct occ_port
*occ_port
, *tmp
;
4230 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
4231 occ_port_free(occ_port
);
4234 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
4237 struct occ_port
*occ_port
;
4238 uint32_t port_index
;
4240 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
4242 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
4243 if (occ_port
->port_index
== port_index
)
4246 occ_port
= occ_port_alloc(port_index
);
4249 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
4253 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
4256 struct occ_item
*occ_item
;
4259 pr_out_sp(7, " %s:", label
);
4260 list_for_each_entry(occ_item
, list
, list
) {
4261 if ((i
- 1) % 4 == 0 && i
!= 1)
4264 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
4265 occ_item
->bound_pool_index
);
4267 pr_out_sp(7, "%2u:", occ_item
->index
);
4268 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
4272 if ((i
- 1) % 4 != 0)
4276 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
4277 struct list_head
*list
,
4280 struct occ_item
*occ_item
;
4283 open_json_object(label
);
4284 list_for_each_entry(occ_item
, list
, list
) {
4285 sprintf(buf
, "%u", occ_item
->index
);
4286 open_json_object(buf
);
4288 print_uint(PRINT_JSON
, "bound_pool", NULL
,
4289 occ_item
->bound_pool_index
);
4290 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
4291 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
4292 close_json_object();
4294 close_json_object();
4297 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
4299 if (dl
->json_output
) {
4300 pr_out_json_occ_show_item_list(dl
, "pool",
4301 &occ_port
->pool_list
, false);
4302 pr_out_json_occ_show_item_list(dl
, "itc",
4303 &occ_port
->ing_tc_list
, true);
4304 pr_out_json_occ_show_item_list(dl
, "etc",
4305 &occ_port
->eg_tc_list
, true);
4308 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
4309 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
4310 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
4314 static void pr_out_occ_show(struct occ_show
*occ_show
)
4316 struct dl
*dl
= occ_show
->dl
;
4317 struct dl_opts
*opts
= &dl
->opts
;
4318 struct occ_port
*occ_port
;
4320 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
4321 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
4322 occ_port
->port_index
, true, false);
4323 pr_out_occ_show_port(dl
, occ_port
);
4324 pr_out_port_handle_end(dl
);
4328 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
4331 struct occ_port
*occ_port
;
4332 struct occ_item
*occ_item
;
4334 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4337 occ_port
= occ_port_get(occ_show
, tb
);
4339 occ_show
->err
= -ENOMEM
;
4343 occ_item
= occ_item_alloc();
4345 occ_show
->err
= -ENOMEM
;
4348 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4349 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4350 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4351 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4354 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4356 struct occ_show
*occ_show
= data
;
4357 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4358 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4360 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4361 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4362 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4363 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4364 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4365 return MNL_CB_ERROR
;
4366 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4370 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4373 struct occ_port
*occ_port
;
4374 struct occ_item
*occ_item
;
4377 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4380 occ_port
= occ_port_get(occ_show
, tb
);
4382 occ_show
->err
= -ENOMEM
;
4386 occ_item
= occ_item_alloc();
4388 occ_show
->err
= -ENOMEM
;
4391 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4392 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4393 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4394 occ_item
->bound_pool_index
=
4395 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4396 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4397 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4398 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4399 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4400 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4402 occ_item_free(occ_item
);
4405 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4407 struct occ_show
*occ_show
= data
;
4408 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4409 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4411 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4412 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4413 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4414 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4415 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4416 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4417 return MNL_CB_ERROR
;
4418 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4422 static int cmd_sb_occ_show(struct dl
*dl
)
4424 struct nlmsghdr
*nlh
;
4425 struct occ_show
*occ_show
;
4426 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4429 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4433 occ_show
= occ_show_alloc(dl
);
4437 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4439 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4440 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4444 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4446 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4447 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4451 pr_out_section_start(dl
, "occupancy");
4452 pr_out_occ_show(occ_show
);
4453 pr_out_section_end(dl
);
4456 occ_show_free(occ_show
);
4460 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4462 struct nlmsghdr
*nlh
;
4465 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4466 NLM_F_REQUEST
| NLM_F_ACK
);
4468 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4472 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4475 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4477 struct nlmsghdr
*nlh
;
4480 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4481 NLM_F_REQUEST
| NLM_F_ACK
);
4483 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4487 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4490 static int cmd_sb_occ(struct dl
*dl
)
4492 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4495 } else if (dl_argv_match(dl
, "show") ||
4496 dl_argv_match(dl
, "list")) {
4498 return cmd_sb_occ_show(dl
);
4499 } else if (dl_argv_match(dl
, "snapshot")) {
4501 return cmd_sb_occ_snapshot(dl
);
4502 } else if (dl_argv_match(dl
, "clearmax")) {
4504 return cmd_sb_occ_clearmax(dl
);
4506 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4510 static int cmd_sb(struct dl
*dl
)
4512 if (dl_argv_match(dl
, "help")) {
4515 } else if (dl_argv_match(dl
, "show") ||
4516 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4518 return cmd_sb_show(dl
);
4519 } else if (dl_argv_match(dl
, "pool")) {
4521 return cmd_sb_pool(dl
);
4522 } else if (dl_argv_match(dl
, "port")) {
4524 return cmd_sb_port(dl
);
4525 } else if (dl_argv_match(dl
, "tc")) {
4527 return cmd_sb_tc(dl
);
4528 } else if (dl_argv_match(dl
, "occupancy")) {
4530 return cmd_sb_occ(dl
);
4532 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4536 static const char *cmd_name(uint8_t cmd
)
4539 case DEVLINK_CMD_UNSPEC
: return "unspec";
4540 case DEVLINK_CMD_GET
: return "get";
4541 case DEVLINK_CMD_SET
: return "set";
4542 case DEVLINK_CMD_NEW
: return "new";
4543 case DEVLINK_CMD_DEL
: return "del";
4544 case DEVLINK_CMD_PORT_GET
: return "get";
4545 case DEVLINK_CMD_PORT_SET
: return "set";
4546 case DEVLINK_CMD_PORT_NEW
: return "new";
4547 case DEVLINK_CMD_PORT_DEL
: return "del";
4548 case DEVLINK_CMD_PARAM_GET
: return "get";
4549 case DEVLINK_CMD_PARAM_SET
: return "set";
4550 case DEVLINK_CMD_PARAM_NEW
: return "new";
4551 case DEVLINK_CMD_PARAM_DEL
: return "del";
4552 case DEVLINK_CMD_REGION_GET
: return "get";
4553 case DEVLINK_CMD_REGION_SET
: return "set";
4554 case DEVLINK_CMD_REGION_NEW
: return "new";
4555 case DEVLINK_CMD_REGION_DEL
: return "del";
4556 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4557 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4558 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4559 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4560 case DEVLINK_CMD_TRAP_GET
: return "get";
4561 case DEVLINK_CMD_TRAP_SET
: return "set";
4562 case DEVLINK_CMD_TRAP_NEW
: return "new";
4563 case DEVLINK_CMD_TRAP_DEL
: return "del";
4564 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4565 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4566 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4567 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4568 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4569 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4570 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4571 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4572 default: return "<unknown cmd>";
4576 static const char *cmd_obj(uint8_t cmd
)
4579 case DEVLINK_CMD_UNSPEC
: return "unspec";
4580 case DEVLINK_CMD_GET
:
4581 case DEVLINK_CMD_SET
:
4582 case DEVLINK_CMD_NEW
:
4583 case DEVLINK_CMD_DEL
:
4585 case DEVLINK_CMD_PORT_GET
:
4586 case DEVLINK_CMD_PORT_SET
:
4587 case DEVLINK_CMD_PORT_NEW
:
4588 case DEVLINK_CMD_PORT_DEL
:
4590 case DEVLINK_CMD_PARAM_GET
:
4591 case DEVLINK_CMD_PARAM_SET
:
4592 case DEVLINK_CMD_PARAM_NEW
:
4593 case DEVLINK_CMD_PARAM_DEL
:
4595 case DEVLINK_CMD_REGION_GET
:
4596 case DEVLINK_CMD_REGION_SET
:
4597 case DEVLINK_CMD_REGION_NEW
:
4598 case DEVLINK_CMD_REGION_DEL
:
4600 case DEVLINK_CMD_FLASH_UPDATE
:
4601 case DEVLINK_CMD_FLASH_UPDATE_END
:
4602 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4604 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4606 case DEVLINK_CMD_TRAP_GET
:
4607 case DEVLINK_CMD_TRAP_SET
:
4608 case DEVLINK_CMD_TRAP_NEW
:
4609 case DEVLINK_CMD_TRAP_DEL
:
4611 case DEVLINK_CMD_TRAP_GROUP_GET
:
4612 case DEVLINK_CMD_TRAP_GROUP_SET
:
4613 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4614 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4615 return "trap-group";
4616 case DEVLINK_CMD_TRAP_POLICER_GET
:
4617 case DEVLINK_CMD_TRAP_POLICER_SET
:
4618 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4619 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4620 return "trap-policer";
4621 default: return "<unknown obj>";
4625 static void pr_out_mon_header(uint8_t cmd
)
4627 if (!is_json_context()) {
4628 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4630 open_json_object(NULL
);
4631 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4632 open_json_object(cmd_obj(cmd
));
4636 static void pr_out_mon_footer(void)
4638 if (is_json_context()) {
4639 close_json_object();
4640 close_json_object();
4644 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4646 const char *obj
= cmd_obj(cmd
);
4647 unsigned int index
= 0;
4648 const char *cur_obj
;
4652 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4653 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4659 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4661 __pr_out_handle_start(dl
, tb
, true, false);
4663 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4664 check_indent_newline(dl
);
4665 print_string(PRINT_ANY
, "msg", "msg %s",
4666 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4668 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4669 check_indent_newline(dl
);
4670 print_string(PRINT_ANY
, "component", "component %s",
4671 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4674 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4675 pr_out_u64(dl
, "done",
4676 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4678 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4679 pr_out_u64(dl
, "total",
4680 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4682 pr_out_handle_end(dl
);
4685 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4686 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
,
4687 bool show_device
, bool show_port
);
4688 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4689 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4690 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4692 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4694 struct dl
*dl
= data
;
4695 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4696 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4697 uint8_t cmd
= genl
->cmd
;
4699 if (!cmd_filter_check(dl
, cmd
))
4703 case DEVLINK_CMD_GET
: /* fall through */
4704 case DEVLINK_CMD_SET
: /* fall through */
4705 case DEVLINK_CMD_NEW
: /* fall through */
4706 case DEVLINK_CMD_DEL
:
4707 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4708 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4709 return MNL_CB_ERROR
;
4710 pr_out_mon_header(genl
->cmd
);
4711 pr_out_handle(dl
, tb
);
4712 pr_out_mon_footer();
4714 case DEVLINK_CMD_PORT_GET
: /* fall through */
4715 case DEVLINK_CMD_PORT_SET
: /* fall through */
4716 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4717 case DEVLINK_CMD_PORT_DEL
:
4718 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4719 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4720 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4721 return MNL_CB_ERROR
;
4722 pr_out_mon_header(genl
->cmd
);
4723 pr_out_port(dl
, tb
);
4724 pr_out_mon_footer();
4726 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4727 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4728 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4729 case DEVLINK_CMD_PARAM_DEL
:
4730 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4731 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4732 !tb
[DEVLINK_ATTR_PARAM
])
4733 return MNL_CB_ERROR
;
4734 pr_out_mon_header(genl
->cmd
);
4735 pr_out_param(dl
, tb
, false);
4736 pr_out_mon_footer();
4738 case DEVLINK_CMD_REGION_GET
: /* fall through */
4739 case DEVLINK_CMD_REGION_SET
: /* fall through */
4740 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4741 case DEVLINK_CMD_REGION_DEL
:
4742 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4743 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4744 !tb
[DEVLINK_ATTR_REGION_NAME
])
4745 return MNL_CB_ERROR
;
4746 pr_out_mon_header(genl
->cmd
);
4747 pr_out_region(dl
, tb
);
4748 pr_out_mon_footer();
4750 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4751 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4752 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4753 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4754 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4755 return MNL_CB_ERROR
;
4756 pr_out_mon_header(genl
->cmd
);
4757 pr_out_flash_update(dl
, tb
);
4758 pr_out_mon_footer();
4760 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4761 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4762 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4763 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
4764 return MNL_CB_ERROR
;
4765 pr_out_mon_header(genl
->cmd
);
4766 pr_out_health(dl
, tb
, true, true);
4767 pr_out_mon_footer();
4769 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4770 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4771 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4772 case DEVLINK_CMD_TRAP_DEL
:
4773 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4774 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4775 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4776 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4777 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4778 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4779 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4780 !tb
[DEVLINK_ATTR_STATS
])
4781 return MNL_CB_ERROR
;
4782 pr_out_mon_header(genl
->cmd
);
4783 pr_out_trap(dl
, tb
, false);
4784 pr_out_mon_footer();
4786 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4787 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4788 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4789 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4790 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4791 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4792 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4793 !tb
[DEVLINK_ATTR_STATS
])
4794 return MNL_CB_ERROR
;
4795 pr_out_mon_header(genl
->cmd
);
4796 pr_out_trap_group(dl
, tb
, false);
4797 pr_out_mon_footer();
4799 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4800 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4801 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4802 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4803 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4804 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4805 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4806 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4807 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4808 return MNL_CB_ERROR
;
4809 pr_out_mon_header(genl
->cmd
);
4810 pr_out_trap_policer(dl
, tb
, false);
4817 static int cmd_mon_show(struct dl
*dl
)
4820 unsigned int index
= 0;
4821 const char *cur_obj
;
4823 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4824 if (strcmp(cur_obj
, "all") != 0 &&
4825 strcmp(cur_obj
, "dev") != 0 &&
4826 strcmp(cur_obj
, "port") != 0 &&
4827 strcmp(cur_obj
, "health") != 0 &&
4828 strcmp(cur_obj
, "trap") != 0 &&
4829 strcmp(cur_obj
, "trap-group") != 0 &&
4830 strcmp(cur_obj
, "trap-policer") != 0) {
4831 pr_err("Unknown object \"%s\"\n", cur_obj
);
4835 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4838 open_json_object(NULL
);
4839 open_json_array(PRINT_JSON
, "mon");
4840 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
4841 close_json_array(PRINT_JSON
, NULL
);
4842 close_json_object();
4848 static void cmd_mon_help(void)
4850 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4851 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4854 static int cmd_mon(struct dl
*dl
)
4856 if (dl_argv_match(dl
, "help")) {
4860 return cmd_mon_show(dl
);
4863 struct dpipe_field
{
4866 unsigned int bitwidth
;
4867 enum devlink_dpipe_field_mapping_type mapping_type
;
4870 struct dpipe_header
{
4871 struct list_head list
;
4874 struct dpipe_field
*fields
;
4875 unsigned int fields_count
;
4878 struct dpipe_table
{
4879 struct list_head list
;
4881 unsigned int resource_id
;
4882 bool resource_valid
;
4885 struct dpipe_tables
{
4886 struct list_head table_list
;
4896 enum devlink_resource_unit unit
;
4901 struct list_head list
;
4902 struct list_head resource_list
;
4903 struct resource
*parent
;
4907 struct list_head resource_list
;
4910 struct resource_ctx
{
4913 struct resources
*resources
;
4914 struct dpipe_tables
*tables
;
4915 bool print_resources
;
4916 bool pending_change
;
4919 static struct resource
*resource_alloc(void)
4921 struct resource
*resource
;
4923 resource
= calloc(1, sizeof(struct resource
));
4926 INIT_LIST_HEAD(&resource
->resource_list
);
4930 static void resource_free(struct resource
*resource
)
4932 struct resource
*child_resource
, *tmp
;
4934 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4936 free(child_resource
->name
);
4937 resource_free(child_resource
);
4942 static struct resources
*resources_alloc(void)
4944 struct resources
*resources
;
4946 resources
= calloc(1, sizeof(struct resources
));
4949 INIT_LIST_HEAD(&resources
->resource_list
);
4953 static void resources_free(struct resources
*resources
)
4955 struct resource
*resource
, *tmp
;
4957 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4958 resource_free(resource
);
4961 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4963 ctx
->resources
= resources_alloc();
4964 if (!ctx
->resources
)
4970 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4972 resources_free(ctx
->resources
);
4978 struct list_head global_headers
;
4979 struct list_head local_headers
;
4980 struct dpipe_tables
*tables
;
4981 struct resources
*resources
;
4986 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4988 struct dpipe_header
*header
;
4990 header
= calloc(1, sizeof(struct dpipe_header
));
4993 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4994 if (!header
->fields
)
4995 goto err_fields_alloc
;
4996 header
->fields_count
= fields_count
;
5004 static void dpipe_header_free(struct dpipe_header
*header
)
5006 free(header
->fields
);
5010 static void dpipe_header_clear(struct dpipe_header
*header
)
5012 struct dpipe_field
*field
;
5015 for (i
= 0; i
< header
->fields_count
; i
++) {
5016 field
= &header
->fields
[i
];
5022 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
5023 struct dpipe_header
*header
, bool global
)
5026 list_add(&header
->list
, &ctx
->global_headers
);
5028 list_add(&header
->list
, &ctx
->local_headers
);
5031 static void dpipe_header_del(struct dpipe_header
*header
)
5033 list_del(&header
->list
);
5036 static struct dpipe_table
*dpipe_table_alloc(void)
5038 return calloc(1, sizeof(struct dpipe_table
));
5041 static void dpipe_table_free(struct dpipe_table
*table
)
5046 static struct dpipe_tables
*dpipe_tables_alloc(void)
5048 struct dpipe_tables
*tables
;
5050 tables
= calloc(1, sizeof(struct dpipe_tables
));
5053 INIT_LIST_HEAD(&tables
->table_list
);
5057 static void dpipe_tables_free(struct dpipe_tables
*tables
)
5059 struct dpipe_table
*table
, *tmp
;
5061 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
5062 dpipe_table_free(table
);
5066 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
5068 ctx
->tables
= dpipe_tables_alloc();
5073 INIT_LIST_HEAD(&ctx
->global_headers
);
5074 INIT_LIST_HEAD(&ctx
->local_headers
);
5078 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
5080 struct dpipe_header
*header
, *tmp
;
5082 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
5084 dpipe_header_del(header
);
5085 dpipe_header_clear(header
);
5086 dpipe_header_free(header
);
5088 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
5090 dpipe_header_del(header
);
5091 dpipe_header_clear(header
);
5092 dpipe_header_free(header
);
5094 dpipe_tables_free(ctx
->tables
);
5097 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
5098 uint32_t header_id
, bool global
)
5100 struct list_head
*header_list
;
5101 struct dpipe_header
*header
;
5104 header_list
= &ctx
->global_headers
;
5106 header_list
= &ctx
->local_headers
;
5107 list_for_each_entry(header
, header_list
, list
) {
5108 if (header
->id
!= header_id
)
5110 return header
->name
;
5115 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
5117 uint32_t field_id
, bool global
)
5119 struct list_head
*header_list
;
5120 struct dpipe_header
*header
;
5123 header_list
= &ctx
->global_headers
;
5125 header_list
= &ctx
->local_headers
;
5126 list_for_each_entry(header
, header_list
, list
) {
5127 if (header
->id
!= header_id
)
5129 return header
->fields
[field_id
].name
;
5135 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
5137 switch (mapping_type
) {
5138 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
5140 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
5148 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
5149 uint32_t field_id
, bool global
)
5151 enum devlink_dpipe_field_mapping_type mapping_type
;
5152 struct list_head
*header_list
;
5153 struct dpipe_header
*header
;
5156 header_list
= &ctx
->global_headers
;
5158 header_list
= &ctx
->local_headers
;
5159 list_for_each_entry(header
, header_list
, list
) {
5160 if (header
->id
!= header_id
)
5162 mapping_type
= header
->fields
[field_id
].mapping_type
;
5163 return dpipe_field_mapping_e2s(mapping_type
);
5168 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
5169 struct dpipe_field
*fields
,
5170 unsigned int field_count
)
5172 struct dpipe_field
*field
;
5175 for (i
= 0; i
< field_count
; i
++) {
5177 pr_out_entry_start(ctx
->dl
);
5178 check_indent_newline(ctx
->dl
);
5179 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
5180 if (ctx
->dl
->verbose
)
5181 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
5182 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
5183 if (field
->mapping_type
) {
5184 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
5185 dpipe_field_mapping_e2s(field
->mapping_type
));
5187 pr_out_entry_end(ctx
->dl
);
5192 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
5193 struct dpipe_header
*header
, bool global
)
5195 pr_out_handle_start_arr(ctx
->dl
, tb
);
5196 check_indent_newline(ctx
->dl
);
5197 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
5198 if (ctx
->dl
->verbose
) {
5199 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
5200 print_bool(PRINT_ANY
, "global", " global %s", global
);
5202 pr_out_array_start(ctx
->dl
, "field");
5203 pr_out_dpipe_fields(ctx
, header
->fields
,
5204 header
->fields_count
);
5205 pr_out_array_end(ctx
->dl
);
5206 pr_out_handle_end(ctx
->dl
);
5209 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
5212 struct dpipe_header
*header
;
5214 list_for_each_entry(header
, &ctx
->local_headers
, list
)
5215 pr_out_dpipe_header(ctx
, tb
, header
, false);
5217 list_for_each_entry(header
, &ctx
->global_headers
, list
)
5218 pr_out_dpipe_header(ctx
, tb
, header
, true);
5221 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
5223 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5227 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
5228 if (err
!= MNL_CB_OK
)
5230 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
5231 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
5232 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
5233 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
5236 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
5237 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5238 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
5239 field
->name
= strdup(name
);
5242 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
5246 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
5247 struct dpipe_field
*fields
)
5249 struct nlattr
*nla_field
;
5253 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
5254 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
5262 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
5264 struct nlattr
*nla_field
;
5265 unsigned int count
= 0;
5267 mnl_attr_for_each_nested(nla_field
, nla_fields
)
5272 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5274 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
5275 struct dpipe_header
*header
;
5276 unsigned int fields_count
;
5277 const char *header_name
;
5281 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
5282 if (err
!= MNL_CB_OK
)
5285 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
5286 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5287 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
5290 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
5291 header
= dpipe_header_alloc(fields_count
);
5295 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
5296 header
->name
= strdup(header_name
);
5297 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5298 header
->fields_count
= fields_count
;
5299 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5301 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
5305 dpipe_header_add(ctx
, header
, global
);
5309 dpipe_header_free(header
);
5313 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5315 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
5316 struct nlattr
*nla_header
;
5319 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
5320 err
= dpipe_header_get(ctx
, nla_header
);
5327 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
5329 struct dpipe_ctx
*ctx
= data
;
5330 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5331 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5334 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5335 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5336 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
5337 return MNL_CB_ERROR
;
5338 err
= dpipe_headers_get(ctx
, tb
);
5341 return MNL_CB_ERROR
;
5344 if (ctx
->print_headers
)
5345 pr_out_dpipe_headers(ctx
, tb
);
5349 static int cmd_dpipe_headers_show(struct dl
*dl
)
5351 struct nlmsghdr
*nlh
;
5352 struct dpipe_ctx ctx
= {};
5353 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5356 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5358 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5362 err
= dpipe_ctx_init(&ctx
, dl
);
5366 ctx
.print_headers
= true;
5368 pr_out_section_start(dl
, "header");
5369 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5371 pr_err("error get headers %s\n", strerror(ctx
.err
));
5372 pr_out_section_end(dl
);
5374 dpipe_ctx_fini(&ctx
);
5378 static void cmd_dpipe_help(void)
5380 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5381 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5382 pr_err(" [ counters_enabled { true | false } ]\n");
5383 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5384 pr_err(" devlink dpipe header show DEV\n");
5387 static int cmd_dpipe_header(struct dl
*dl
)
5389 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5392 } else if (dl_argv_match(dl
, "show")) {
5394 return cmd_dpipe_headers_show(dl
);
5396 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5401 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5403 switch (action_type
) {
5404 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5405 return "field_modify";
5411 struct dpipe_op_info
{
5417 struct dpipe_action
{
5418 struct dpipe_op_info info
;
5422 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5423 struct dpipe_ctx
*ctx
)
5425 struct dpipe_op_info
*op_info
= &action
->info
;
5426 const char *mapping
;
5428 check_indent_newline(ctx
->dl
);
5429 print_string(PRINT_ANY
, "type", "type %s",
5430 dpipe_action_type_e2s(action
->type
));
5431 print_string(PRINT_ANY
, "header", " header %s",
5432 dpipe_header_id2s(ctx
, op_info
->header_id
,
5433 op_info
->header_global
));
5434 print_string(PRINT_ANY
, "field", " field %s",
5435 dpipe_field_id2s(ctx
, op_info
->header_id
,
5437 op_info
->header_global
));
5438 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5440 op_info
->header_global
);
5442 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5445 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5447 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5450 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5451 if (err
!= MNL_CB_OK
)
5454 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5455 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5456 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5457 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5461 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5462 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5463 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5464 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5469 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5470 struct nlattr
*nla_actions
)
5472 struct nlattr
*nla_action
;
5473 struct dpipe_action action
;
5475 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5476 pr_out_entry_start(ctx
->dl
);
5477 if (dpipe_action_parse(&action
, nla_action
))
5478 goto err_action_parse
;
5479 pr_out_dpipe_action(&action
, ctx
);
5480 pr_out_entry_end(ctx
->dl
);
5485 pr_out_entry_end(ctx
->dl
);
5490 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5492 switch (match_type
) {
5493 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5494 return "field_exact";
5500 struct dpipe_match
{
5501 struct dpipe_op_info info
;
5505 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5506 struct dpipe_ctx
*ctx
)
5508 struct dpipe_op_info
*op_info
= &match
->info
;
5509 const char *mapping
;
5511 check_indent_newline(ctx
->dl
);
5512 print_string(PRINT_ANY
, "type", "type %s",
5513 dpipe_match_type_e2s(match
->type
));
5514 print_string(PRINT_ANY
, "header", " header %s",
5515 dpipe_header_id2s(ctx
, op_info
->header_id
,
5516 op_info
->header_global
));
5517 print_string(PRINT_ANY
, "field", " field %s",
5518 dpipe_field_id2s(ctx
, op_info
->header_id
,
5520 op_info
->header_global
));
5521 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5523 op_info
->header_global
);
5525 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5528 static int dpipe_match_parse(struct dpipe_match
*match
,
5532 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5535 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5536 if (err
!= MNL_CB_OK
)
5539 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5540 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5541 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5542 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5546 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5547 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5548 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5549 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5554 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5555 struct nlattr
*nla_matches
)
5557 struct nlattr
*nla_match
;
5558 struct dpipe_match match
;
5560 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5561 pr_out_entry_start(ctx
->dl
);
5562 if (dpipe_match_parse(&match
, nla_match
))
5563 goto err_match_parse
;
5564 pr_out_dpipe_match(&match
, ctx
);
5565 pr_out_entry_end(ctx
->dl
);
5570 pr_out_entry_end(ctx
->dl
);
5574 static struct resource
*
5575 resource_find(struct resources
*resources
, struct resource
*resource
,
5576 uint64_t resource_id
)
5578 struct list_head
*list_head
;
5581 list_head
= &resources
->resource_list
;
5583 list_head
= &resource
->resource_list
;
5585 list_for_each_entry(resource
, list_head
, list
) {
5586 struct resource
*child_resource
;
5588 if (resource
->id
== resource_id
)
5591 child_resource
= resource_find(resources
, resource
,
5594 return child_resource
;
5600 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5601 uint64_t resource_id
)
5603 struct resource
*resource
, *parent_resource
;
5604 const char del
[] = "/";
5608 resource
= resource_find(resources
, NULL
, resource_id
);
5612 for (parent_resource
= resource
; parent_resource
;
5613 parent_resource
= parent_resource
->parent
)
5614 path_len
+= strlen(parent_resource
->name
) + 1;
5617 path
= calloc(1, path_len
);
5621 path
+= path_len
- 1;
5622 for (parent_resource
= resource
; parent_resource
;
5623 parent_resource
= parent_resource
->parent
) {
5624 path
-= strlen(parent_resource
->name
);
5625 memcpy(path
, parent_resource
->name
,
5626 strlen(parent_resource
->name
));
5627 path
-= strlen(del
);
5628 memcpy(path
, del
, strlen(del
));
5630 check_indent_newline(dl
);
5631 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5635 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5637 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5638 struct dpipe_table
*table
;
5639 uint32_t resource_units
;
5640 bool counters_enabled
;
5641 bool resource_valid
;
5645 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5646 if (err
!= MNL_CB_OK
)
5649 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5650 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5651 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5652 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5653 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5657 table
= dpipe_table_alloc();
5661 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5662 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5663 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5665 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5667 if (resource_valid
) {
5668 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5669 table
->resource_valid
= true;
5672 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5673 if (!ctx
->print_tables
)
5676 check_indent_newline(ctx
->dl
);
5677 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5678 print_uint(PRINT_ANY
, "size", " size %u", size
);
5679 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5681 if (resource_valid
) {
5682 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5683 resource_path_print(ctx
->dl
, ctx
->resources
,
5684 table
->resource_id
);
5685 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5689 pr_out_array_start(ctx
->dl
, "match");
5690 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5691 goto err_matches_show
;
5692 pr_out_array_end(ctx
->dl
);
5694 pr_out_array_start(ctx
->dl
, "action");
5695 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5696 goto err_actions_show
;
5697 pr_out_array_end(ctx
->dl
);
5703 pr_out_array_end(ctx
->dl
);
5707 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5709 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5710 struct nlattr
*nla_table
;
5712 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5713 if (ctx
->print_tables
)
5714 pr_out_handle_start_arr(ctx
->dl
, tb
);
5715 if (dpipe_table_show(ctx
, nla_table
))
5716 goto err_table_show
;
5717 if (ctx
->print_tables
)
5718 pr_out_handle_end(ctx
->dl
);
5723 if (ctx
->print_tables
)
5724 pr_out_handle_end(ctx
->dl
);
5728 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5730 struct dpipe_ctx
*ctx
= data
;
5731 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5732 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5734 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5735 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5736 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5737 return MNL_CB_ERROR
;
5739 if (dpipe_tables_show(ctx
, tb
))
5740 return MNL_CB_ERROR
;
5744 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5746 static int cmd_dpipe_table_show(struct dl
*dl
)
5748 struct nlmsghdr
*nlh
;
5749 struct dpipe_ctx dpipe_ctx
= {};
5750 struct resource_ctx resource_ctx
= {};
5751 uint16_t flags
= NLM_F_REQUEST
;
5754 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5758 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5760 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5764 dpipe_ctx
.print_tables
= true;
5766 dl_opts_put(nlh
, dl
);
5767 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5770 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5771 goto err_headers_get
;
5774 err
= resource_ctx_init(&resource_ctx
, dl
);
5776 goto err_resource_ctx_init
;
5778 resource_ctx
.print_resources
= false;
5779 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5780 dl_opts_put(nlh
, dl
);
5781 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5784 dpipe_ctx
.resources
= resource_ctx
.resources
;
5786 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5787 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5788 dl_opts_put(nlh
, dl
);
5790 pr_out_section_start(dl
, "table");
5791 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5792 pr_out_section_end(dl
);
5794 resource_ctx_fini(&resource_ctx
);
5795 dpipe_ctx_fini(&dpipe_ctx
);
5798 err_resource_ctx_init
:
5800 dpipe_ctx_fini(&dpipe_ctx
);
5804 static int cmd_dpipe_table_set(struct dl
*dl
)
5806 struct nlmsghdr
*nlh
;
5809 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5810 NLM_F_REQUEST
| NLM_F_ACK
);
5812 err
= dl_argv_parse_put(nlh
, dl
,
5813 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5814 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5818 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5821 enum dpipe_value_type
{
5822 DPIPE_VALUE_TYPE_VALUE
,
5823 DPIPE_VALUE_TYPE_MASK
,
5827 dpipe_value_type_e2s(enum dpipe_value_type type
)
5830 case DPIPE_VALUE_TYPE_VALUE
:
5832 case DPIPE_VALUE_TYPE_MASK
:
5833 return "value_mask";
5839 struct dpipe_field_printer
{
5840 unsigned int field_id
;
5841 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5844 struct dpipe_header_printer
{
5845 struct dpipe_field_printer
*printers
;
5846 unsigned int printers_count
;
5847 unsigned int header_id
;
5850 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5851 enum dpipe_value_type type
,
5854 struct in_addr ip_addr
;
5856 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5857 check_indent_newline(ctx
->dl
);
5858 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5862 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5863 enum dpipe_value_type type
,
5866 check_indent_newline(ctx
->dl
);
5867 print_string_name_value(dpipe_value_type_e2s(type
),
5868 ether_ntoa((struct ether_addr
*)value
));
5871 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5872 enum dpipe_value_type type
,
5875 char str
[INET6_ADDRSTRLEN
];
5877 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5878 check_indent_newline(ctx
->dl
);
5879 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5882 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5884 .printer
= dpipe_field_printer_ipv4_addr
,
5885 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5889 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5890 .printers
= dpipe_field_printers_ipv4
,
5891 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5892 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5895 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5897 .printer
= dpipe_field_printer_ethernet_addr
,
5898 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5902 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5903 .printers
= dpipe_field_printers_ethernet
,
5904 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5905 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5908 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5910 .printer
= dpipe_field_printer_ipv6_addr
,
5911 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5915 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5916 .printers
= dpipe_field_printers_ipv6
,
5917 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5918 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5921 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5922 &dpipe_header_printer_ipv4
,
5923 &dpipe_header_printer_ethernet
,
5924 &dpipe_header_printer_ipv6
,
5927 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5928 struct dpipe_op_info
*info
,
5929 enum dpipe_value_type type
,
5932 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5933 struct dpipe_header_printer
*header_printer
;
5934 struct dpipe_field_printer
*field_printer
;
5935 unsigned int field_printers_count
;
5939 for (i
= 0; i
< header_printers_count
; i
++) {
5940 header_printer
= dpipe_header_printers
[i
];
5941 if (header_printer
->header_id
!= info
->header_id
)
5943 field_printers_count
= header_printer
->printers_count
;
5944 for (j
= 0; j
< field_printers_count
; j
++) {
5945 field_printer
= &header_printer
->printers
[j
];
5946 if (field_printer
->field_id
!= info
->field_id
)
5948 field_printer
->printer(ctx
, type
, value
);
5956 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5958 unsigned int value_len
,
5959 struct dpipe_op_info
*info
,
5960 enum dpipe_value_type type
)
5962 if (info
->header_global
&&
5963 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5966 if (value_len
== sizeof(uint32_t)) {
5967 uint32_t *value_32
= value
;
5969 check_indent_newline(ctx
->dl
);
5970 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5974 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5975 struct nlattr
**nla_match_value
,
5976 struct dpipe_op_info
*info
)
5978 void *value
, *value_mask
;
5979 uint32_t value_mapping
;
5983 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5984 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5986 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5987 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5990 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5991 check_indent_newline(ctx
->dl
);
5992 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5996 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5997 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5998 DPIPE_VALUE_TYPE_MASK
);
6001 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
6004 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
6007 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
6008 struct dpipe_match match
;
6011 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
6012 if (err
!= MNL_CB_OK
)
6015 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
6016 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
6020 pr_out_entry_start(ctx
->dl
);
6021 if (dpipe_match_parse(&match
,
6022 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
6023 goto err_match_parse
;
6024 pr_out_dpipe_match(&match
, ctx
);
6025 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
6026 pr_out_entry_end(ctx
->dl
);
6031 pr_out_entry_end(ctx
->dl
);
6035 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
6038 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
6039 struct dpipe_action action
;
6042 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
6043 if (err
!= MNL_CB_OK
)
6046 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
6047 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
6051 pr_out_entry_start(ctx
->dl
);
6052 if (dpipe_action_parse(&action
,
6053 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
6054 goto err_action_parse
;
6055 pr_out_dpipe_action(&action
, ctx
);
6056 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
6057 pr_out_entry_end(ctx
->dl
);
6062 pr_out_entry_end(ctx
->dl
);
6067 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
6068 struct nlattr
*nla_action_values
)
6070 struct nlattr
*nla_action_value
;
6072 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
6073 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
6080 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
6081 struct nlattr
*nla_match_values
)
6083 struct nlattr
*nla_match_value
;
6085 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
6086 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
6092 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
6094 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
6095 uint32_t entry_index
;
6099 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
6100 if (err
!= MNL_CB_OK
)
6103 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
6104 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
6105 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
6109 check_indent_newline(ctx
->dl
);
6110 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
6111 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
6113 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
6114 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
6115 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
6118 pr_out_array_start(ctx
->dl
, "match_value");
6119 if (dpipe_tables_match_values_show(ctx
,
6120 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
6121 goto err_match_values_show
;
6122 pr_out_array_end(ctx
->dl
);
6124 pr_out_array_start(ctx
->dl
, "action_value");
6125 if (dpipe_tables_action_values_show(ctx
,
6126 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
6127 goto err_action_values_show
;
6128 pr_out_array_end(ctx
->dl
);
6131 err_action_values_show
:
6132 err_match_values_show
:
6133 pr_out_array_end(ctx
->dl
);
6137 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
6139 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
6140 struct nlattr
*nla_entry
;
6142 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
6143 pr_out_handle_start_arr(ctx
->dl
, tb
);
6144 if (dpipe_entry_show(ctx
, nla_entry
))
6145 goto err_entry_show
;
6146 pr_out_handle_end(ctx
->dl
);
6151 pr_out_handle_end(ctx
->dl
);
6155 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6157 struct dpipe_ctx
*ctx
= data
;
6158 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6159 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6161 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6162 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6163 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
6164 return MNL_CB_ERROR
;
6166 if (dpipe_table_entries_show(ctx
, tb
))
6167 return MNL_CB_ERROR
;
6171 static int cmd_dpipe_table_dump(struct dl
*dl
)
6173 struct nlmsghdr
*nlh
;
6174 struct dpipe_ctx ctx
= {};
6175 uint16_t flags
= NLM_F_REQUEST
;
6178 err
= dpipe_ctx_init(&ctx
, dl
);
6182 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
6186 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
6187 dl_opts_put(nlh
, dl
);
6188 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
6190 pr_err("error get headers %s\n", strerror(ctx
.err
));
6194 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6195 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
6196 dl_opts_put(nlh
, dl
);
6198 pr_out_section_start(dl
, "table_entry");
6199 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
6200 pr_out_section_end(dl
);
6202 dpipe_ctx_fini(&ctx
);
6206 static int cmd_dpipe_table(struct dl
*dl
)
6208 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6211 } else if (dl_argv_match(dl
, "show")) {
6213 return cmd_dpipe_table_show(dl
);
6214 } else if (dl_argv_match(dl
, "set")) {
6216 return cmd_dpipe_table_set(dl
);
6217 } else if (dl_argv_match(dl
, "dump")) {
6219 return cmd_dpipe_table_dump(dl
);
6221 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6225 static int cmd_dpipe(struct dl
*dl
)
6227 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6230 } else if (dl_argv_match(dl
, "header")) {
6232 return cmd_dpipe_header(dl
);
6233 } else if (dl_argv_match(dl
, "table")) {
6235 return cmd_dpipe_table(dl
);
6237 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6242 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
6243 struct nlattr
**nla_resource
)
6245 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
6246 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
6247 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
6248 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
6249 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
6250 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
6251 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
6255 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
6256 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
6257 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
6258 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
6259 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
6260 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
6261 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
6263 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
6264 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
6266 resource
->size_new
= resource
->size
;
6268 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
6269 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
6270 resource
->occ_valid
= true;
6273 if (resource
->size_new
!= resource
->size
)
6274 ctx
->pending_change
= true;
6280 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
6281 struct resource
*parent_resource
, struct nlattr
*nl
)
6283 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
6284 struct nlattr
*nla_child_resource
;
6285 struct nlattr
*nla_resources
;
6295 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
6296 if (err
!= MNL_CB_OK
)
6299 err
= resource_parse(ctx
, resource
, nla_resource
);
6303 resource
->parent
= parent_resource
;
6304 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
6307 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
6308 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
6310 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
6311 struct resource
*child_resource
;
6312 struct list_head
*list
;
6314 child_resource
= resource_alloc();
6315 if (!child_resource
)
6319 list
= &ctx
->resources
->resource_list
;
6321 list
= &resource
->resource_list
;
6323 list_add_tail(&child_resource
->list
, list
);
6324 err
= resource_get(ctx
, child_resource
, resource
,
6325 nla_child_resource
);
6333 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
6336 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
6337 default: return "<unknown unit>";
6341 static void resource_show(struct resource
*resource
,
6342 struct resource_ctx
*ctx
)
6344 struct resource
*child_resource
;
6345 struct dpipe_table
*table
;
6346 struct dl
*dl
= ctx
->dl
;
6349 check_indent_newline(dl
);
6350 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6352 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6353 pr_out_u64(dl
, "size", resource
->size
);
6354 if (resource
->size
!= resource
->size_new
)
6355 pr_out_u64(dl
, "size_new", resource
->size_new
);
6356 if (resource
->occ_valid
)
6357 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6358 print_string(PRINT_ANY
, "unit", " unit %s",
6359 resource_unit_str_get(resource
->unit
));
6361 if (resource
->size_min
!= resource
->size_max
) {
6362 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6363 resource
->size_min
);
6364 pr_out_u64(dl
, "size_max", resource
->size_max
);
6365 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6366 resource
->size_gran
);
6369 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6370 if (table
->resource_id
== resource
->id
&&
6371 table
->resource_valid
)
6375 pr_out_array_start(dl
, "dpipe_tables");
6377 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6380 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6381 if (table
->resource_id
!= resource
->id
||
6382 !table
->resource_valid
)
6384 pr_out_entry_start(dl
);
6385 check_indent_newline(dl
);
6386 print_string(PRINT_ANY
, "table_name", "table_name %s",
6388 pr_out_entry_end(dl
);
6391 pr_out_array_end(dl
);
6393 if (list_empty(&resource
->resource_list
))
6396 if (ctx
->pending_change
) {
6397 check_indent_newline(dl
);
6398 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6399 resource
->size_valid
? "true" : "false");
6401 pr_out_array_start(dl
, "resources");
6402 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6403 pr_out_entry_start(dl
);
6404 resource_show(child_resource
, ctx
);
6405 pr_out_entry_end(dl
);
6407 pr_out_array_end(dl
);
6411 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6413 struct resources
*resources
= ctx
->resources
;
6414 struct resource
*resource
;
6416 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6417 pr_out_handle_start_arr(ctx
->dl
, tb
);
6418 resource_show(resource
, ctx
);
6419 pr_out_handle_end(ctx
->dl
);
6423 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6425 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6428 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6430 struct resource_ctx
*ctx
= data
;
6431 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6432 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6435 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6436 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6437 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6438 return MNL_CB_ERROR
;
6440 err
= resources_get(ctx
, tb
);
6443 return MNL_CB_ERROR
;
6446 if (ctx
->print_resources
)
6447 resources_show(ctx
, tb
);
6452 static int cmd_resource_show(struct dl
*dl
)
6454 struct nlmsghdr
*nlh
;
6455 struct dpipe_ctx dpipe_ctx
= {};
6456 struct resource_ctx resource_ctx
= {};
6459 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6463 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6465 dl_opts_put(nlh
, dl
);
6467 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6471 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6474 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6478 err
= resource_ctx_init(&resource_ctx
, dl
);
6482 resource_ctx
.print_resources
= true;
6483 resource_ctx
.tables
= dpipe_ctx
.tables
;
6484 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6485 NLM_F_REQUEST
| NLM_F_ACK
);
6486 dl_opts_put(nlh
, dl
);
6487 pr_out_section_start(dl
, "resources");
6488 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6490 pr_out_section_end(dl
);
6491 resource_ctx_fini(&resource_ctx
);
6493 dpipe_ctx_fini(&dpipe_ctx
);
6497 static void cmd_resource_help(void)
6499 pr_err("Usage: devlink resource show DEV\n"
6500 " devlink resource set DEV path PATH size SIZE\n");
6503 static struct resource
*
6504 resource_find_by_name(struct list_head
*list
, char *name
)
6506 struct resource
*resource
;
6508 list_for_each_entry(resource
, list
, list
) {
6509 if (!strcmp(resource
->name
, name
))
6516 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6517 uint32_t *p_resource_id
, bool *p_resource_valid
)
6519 struct resource
*resource
;
6520 uint32_t resource_id
= 0;
6521 char *resource_path_dup
;
6522 struct list_head
*list
;
6523 const char del
[] = "/";
6524 char *resource_name
;
6526 resource_path_dup
= strdup(resource_path
);
6527 list
= &ctx
->resources
->resource_list
;
6528 resource_name
= strtok(resource_path_dup
, del
);
6529 while (resource_name
!= NULL
) {
6530 resource
= resource_find_by_name(list
, resource_name
);
6532 goto err_resource_lookup
;
6534 list
= &resource
->resource_list
;
6535 resource_name
= strtok(NULL
, del
);
6536 resource_id
= resource
->id
;
6538 free(resource_path_dup
);
6539 *p_resource_valid
= true;
6540 *p_resource_id
= resource_id
;
6543 err_resource_lookup
:
6544 free(resource_path_dup
);
6548 static int cmd_resource_set(struct dl
*dl
)
6550 struct nlmsghdr
*nlh
;
6551 struct resource_ctx ctx
= {};
6554 err
= resource_ctx_init(&ctx
, dl
);
6558 ctx
.print_resources
= false;
6559 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6560 DL_OPT_RESOURCE_SIZE
, 0);
6564 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6566 dl_opts_put(nlh
, dl
);
6567 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6569 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6573 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6574 &dl
->opts
.resource_id
,
6575 &dl
->opts
.resource_id_valid
);
6577 pr_err("error parsing resource path %s\n", strerror(-err
));
6581 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6582 NLM_F_REQUEST
| NLM_F_ACK
);
6584 dl_opts_put(nlh
, dl
);
6585 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6587 resource_ctx_fini(&ctx
);
6591 static int cmd_resource(struct dl
*dl
)
6593 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6594 cmd_resource_help();
6596 } else if (dl_argv_match(dl
, "show")) {
6598 return cmd_resource_show(dl
);
6599 } else if (dl_argv_match(dl
, "set")) {
6601 return cmd_resource_set(dl
);
6603 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6607 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6609 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6610 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6611 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6614 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6615 if (dl
->json_output
)
6616 open_json_object(buf
);
6621 static void pr_out_region_handle_end(struct dl
*dl
)
6623 if (dl
->json_output
)
6624 close_json_object();
6629 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6631 __pr_out_indent_newline(dl
);
6632 if (dl
->json_output
)
6633 open_json_array(PRINT_JSON
, "snapshot");
6635 pr_out("snapshot %s", array
? "[" : "");
6638 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6640 if (dl
->json_output
)
6641 close_json_array(PRINT_JSON
, NULL
);
6646 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6648 uint32_t snapshot_id
;
6650 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6653 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6655 if (dl
->json_output
)
6656 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6658 pr_out("%s%u", index
? " " : "", snapshot_id
);
6661 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6663 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6664 struct nlattr
*nla_sanpshot
;
6667 pr_out_region_snapshots_start(dl
, true);
6668 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6669 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6670 if (err
!= MNL_CB_OK
)
6672 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6674 pr_out_region_snapshots_end(dl
, true);
6677 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6679 pr_out_region_snapshots_start(dl
, false);
6680 pr_out_region_snapshots_id(dl
, tb
, 0);
6681 pr_out_region_snapshots_end(dl
, false);
6684 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6686 pr_out_region_handle_start(dl
, tb
);
6688 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6689 pr_out_u64(dl
, "size",
6690 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6692 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6693 pr_out_snapshots(dl
, tb
);
6695 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6696 pr_out_snapshot(dl
, tb
);
6698 pr_out_region_handle_end(dl
);
6701 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6703 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6704 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6705 struct dl
*dl
= data
;
6707 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6708 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6709 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6710 return MNL_CB_ERROR
;
6712 pr_out_region(dl
, tb
);
6717 static int cmd_region_show(struct dl
*dl
)
6719 struct nlmsghdr
*nlh
;
6720 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6723 if (dl_argc(dl
) == 0)
6724 flags
|= NLM_F_DUMP
;
6726 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6728 if (dl_argc(dl
) > 0) {
6729 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6734 pr_out_section_start(dl
, "regions");
6735 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6736 pr_out_section_end(dl
);
6740 static int cmd_region_snapshot_del(struct dl
*dl
)
6742 struct nlmsghdr
*nlh
;
6745 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6746 NLM_F_REQUEST
| NLM_F_ACK
);
6748 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6749 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6753 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6756 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6758 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6759 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6760 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6761 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6762 struct dl
*dl
= data
;
6765 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6766 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6767 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6768 return MNL_CB_ERROR
;
6770 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6771 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6772 if (err
!= MNL_CB_OK
)
6773 return MNL_CB_ERROR
;
6775 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6776 if (!nla_chunk_data
)
6779 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6780 if (!nla_chunk_addr
)
6783 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6784 mnl_attr_get_payload_len(nla_chunk_data
),
6785 mnl_attr_get_u64(nla_chunk_addr
));
6790 static int cmd_region_dump(struct dl
*dl
)
6792 struct nlmsghdr
*nlh
;
6795 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6796 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6798 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6799 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6803 pr_out_section_start(dl
, "dump");
6804 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6805 pr_out_section_end(dl
);
6806 if (!dl
->json_output
)
6811 static int cmd_region_read(struct dl
*dl
)
6813 struct nlmsghdr
*nlh
;
6816 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6817 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6819 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6820 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6821 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6825 pr_out_section_start(dl
, "read");
6826 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6827 pr_out_section_end(dl
);
6828 if (!dl
->json_output
)
6833 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
6835 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6836 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6837 struct dl
*dl
= data
;
6839 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6840 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6841 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
6842 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6843 return MNL_CB_ERROR
;
6845 pr_out_region(dl
, tb
);
6850 static int cmd_region_snapshot_new(struct dl
*dl
)
6852 struct nlmsghdr
*nlh
;
6855 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
6856 NLM_F_REQUEST
| NLM_F_ACK
);
6858 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
6859 DL_OPT_REGION_SNAPSHOT_ID
);
6863 pr_out_section_start(dl
, "regions");
6864 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
6865 pr_out_section_end(dl
);
6869 static void cmd_region_help(void)
6871 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6872 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6873 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
6874 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6875 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6878 static int cmd_region(struct dl
*dl
)
6880 if (dl_no_arg(dl
)) {
6881 return cmd_region_show(dl
);
6882 } else if (dl_argv_match(dl
, "help")) {
6885 } else if (dl_argv_match(dl
, "show")) {
6887 return cmd_region_show(dl
);
6888 } else if (dl_argv_match(dl
, "del")) {
6890 return cmd_region_snapshot_del(dl
);
6891 } else if (dl_argv_match(dl
, "dump")) {
6893 return cmd_region_dump(dl
);
6894 } else if (dl_argv_match(dl
, "read")) {
6896 return cmd_region_read(dl
);
6897 } else if (dl_argv_match(dl
, "new")) {
6899 return cmd_region_snapshot_new(dl
);
6901 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6905 static int cmd_health_set_params(struct dl
*dl
)
6907 struct nlmsghdr
*nlh
;
6910 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6911 NLM_F_REQUEST
| NLM_F_ACK
);
6912 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
| DL_OPT_HEALTH_REPORTER_NAME
,
6913 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6914 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
6915 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
6919 dl_opts_put(nlh
, dl
);
6920 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6923 static int cmd_health_dump_clear(struct dl
*dl
)
6925 struct nlmsghdr
*nlh
;
6928 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6929 NLM_F_REQUEST
| NLM_F_ACK
);
6931 err
= dl_argv_parse_put(nlh
, dl
,
6932 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
6933 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6937 dl_opts_put(nlh
, dl
);
6938 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6941 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6946 check_indent_newline(dl
);
6949 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6952 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6955 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6958 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6961 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6963 case MNL_TYPE_NUL_STRING
:
6964 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6966 case MNL_TYPE_BINARY
:
6967 len
= mnl_attr_get_payload_len(nl_data
);
6968 data
= mnl_attr_get_payload(nl_data
);
6969 pr_out_binary_value(dl
, data
, len
);
6977 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6982 pr_out_name(dl
, *name
);
6989 struct list_head list
;
6992 struct fmsg_cb_data
{
6996 struct list_head entry_list
;
6999 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
7000 uint8_t *attr_value
, bool insert
)
7002 struct nest_entry
*entry
;
7005 entry
= malloc(sizeof(struct nest_entry
));
7009 entry
->attr_type
= *attr_value
;
7010 list_add(&entry
->list
, &fmsg_data
->entry_list
);
7012 if (list_empty(&fmsg_data
->entry_list
))
7013 return MNL_CB_ERROR
;
7014 entry
= list_first_entry(&fmsg_data
->entry_list
,
7015 struct nest_entry
, list
);
7016 *attr_value
= entry
->attr_type
;
7017 list_del(&entry
->list
);
7023 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
7026 pr_out_fmsg_name(dl
, name
);
7028 __pr_out_indent_inc();
7031 static void pr_out_fmsg_group_end(struct dl
*dl
)
7034 __pr_out_indent_dec();
7037 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
7039 if (dl
->json_output
) {
7040 pr_out_fmsg_name(dl
, name
);
7041 open_json_object(NULL
);
7043 pr_out_fmsg_group_start(dl
, name
);
7047 static void pr_out_fmsg_end_object(struct dl
*dl
)
7049 if (dl
->json_output
)
7050 close_json_object();
7052 pr_out_fmsg_group_end(dl
);
7055 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
7057 if (dl
->json_output
) {
7058 pr_out_fmsg_name(dl
, name
);
7059 open_json_array(PRINT_JSON
, NULL
);
7061 pr_out_fmsg_group_start(dl
, name
);
7065 static void pr_out_fmsg_end_array(struct dl
*dl
)
7067 if (dl
->json_output
)
7068 close_json_array(PRINT_JSON
, NULL
);
7070 pr_out_fmsg_group_end(dl
);
7073 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
7076 struct dl
*dl
= fmsg_data
->dl
;
7077 uint8_t value
= nest_value
;
7080 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
7081 if (err
!= MNL_CB_OK
)
7085 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
7087 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
7089 pr_out_fmsg_end_object(dl
);
7091 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
7093 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
7095 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
7097 pr_out_fmsg_end_array(dl
);
7105 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
7107 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7108 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7109 struct fmsg_cb_data
*fmsg_data
= data
;
7110 struct dl
*dl
= fmsg_data
->dl
;
7111 struct nlattr
*nla_object
;
7115 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7116 if (!tb
[DEVLINK_ATTR_FMSG
])
7117 return MNL_CB_ERROR
;
7119 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
7120 attr_type
= mnl_attr_get_type(nla_object
);
7121 switch (attr_type
) {
7122 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
7123 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
7124 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
7125 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
7126 if (err
!= MNL_CB_OK
)
7129 case DEVLINK_ATTR_FMSG_NEST_END
:
7130 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
7131 if (err
!= MNL_CB_OK
)
7134 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
7135 free(fmsg_data
->name
);
7136 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
7137 if (!fmsg_data
->name
)
7140 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
7141 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
7143 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
7144 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
7145 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
7147 if (err
!= MNL_CB_OK
)
7157 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
7159 /* FMSG is dynamic: opening of an object or array causes a
7160 * newline. JSON starts with an { or [, but plain text should
7161 * not start with a new line. Ensure this by setting
7162 * g_new_line_count to 1: avoiding newline before the first
7165 g_new_line_count
= 1;
7168 INIT_LIST_HEAD(&data
->entry_list
);
7171 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
7173 struct fmsg_cb_data data
;
7174 struct nlmsghdr
*nlh
;
7177 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| 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 cmd_fmsg_init(dl
, &data
);
7186 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
7191 static int cmd_health_dump_show(struct dl
*dl
)
7193 return cmd_health_object_common(dl
,
7194 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
7198 static int cmd_health_diagnose(struct dl
*dl
)
7200 return cmd_health_object_common(dl
,
7201 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
7205 static int cmd_health_test(struct dl
*dl
)
7207 return cmd_health_object_common(dl
,
7208 DEVLINK_CMD_HEALTH_REPORTER_TEST
,
7212 static int cmd_health_recover(struct dl
*dl
)
7214 struct nlmsghdr
*nlh
;
7217 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
7218 NLM_F_REQUEST
| NLM_F_ACK
);
7220 err
= dl_argv_parse_put(nlh
, dl
,
7221 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7222 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7226 dl_opts_put(nlh
, dl
);
7227 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7230 enum devlink_health_reporter_state
{
7231 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
7232 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
7235 static const char *health_state_name(uint8_t state
)
7238 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
7239 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
7240 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
7241 return HEALTH_REPORTER_STATE_ERROR_STR
;
7243 return "<unknown state>";
7247 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
7249 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7250 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7251 uint64_t time_ms
= mnl_attr_get_u64(attr
);
7252 struct sysinfo s_info
;
7258 info
= localtime(&now
);
7259 err
= sysinfo(&s_info
);
7262 /* Subtract uptime in sec from now yields the time of system
7263 * uptime. To this, add time_ms which is the amount of
7264 * milliseconds elapsed between uptime and the dump taken.
7266 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
7267 info
= localtime(&sec
);
7269 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
7270 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
7271 check_indent_newline(dl
);
7272 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7273 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7276 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
7278 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7279 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
7284 ts
= mnl_attr_get_u64(attr
);
7285 tv_sec
= ts
/ 1000000000;
7286 tm
= localtime(&tv_sec
);
7288 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
7289 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
7291 check_indent_newline(dl
);
7292 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
7293 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
7296 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
,
7297 bool print_device
, bool print_port
)
7299 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7300 enum devlink_health_reporter_state state
;
7303 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
7305 if (err
!= MNL_CB_OK
)
7308 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
7309 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
7310 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
7311 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
7314 if (!print_device
&& !print_port
)
7317 if (!print_device
&& !tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7319 else if (tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7320 pr_out_port_handle_start_arr(dl
, tb_health
, false);
7323 if (!print_port
&& tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7325 else if (!tb_health
[DEVLINK_ATTR_PORT_INDEX
])
7326 pr_out_handle_start_arr(dl
, tb_health
);
7329 check_indent_newline(dl
);
7330 print_string(PRINT_ANY
, "reporter", "reporter %s",
7331 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
7332 if (!dl
->json_output
) {
7334 __pr_out_indent_inc();
7336 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
7337 check_indent_newline(dl
);
7338 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
7339 pr_out_u64(dl
, "error",
7340 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
7341 pr_out_u64(dl
, "recover",
7342 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
7343 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
7344 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
7345 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
7346 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
7347 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
7348 pr_out_u64(dl
, "grace_period",
7349 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
7350 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
7351 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
7352 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
7353 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
7354 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
7355 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
7357 __pr_out_indent_dec();
7358 pr_out_handle_end(dl
);
7367 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7369 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7370 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7371 struct health_ctx
*ctx
= data
;
7372 struct dl
*dl
= ctx
->dl
;
7374 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7375 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7376 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
7377 return MNL_CB_ERROR
;
7379 pr_out_health(dl
, tb
, ctx
->show_device
, ctx
->show_port
);
7384 static int __cmd_health_show(struct dl
*dl
, bool show_device
, bool show_port
)
7386 struct nlmsghdr
*nlh
;
7387 struct health_ctx ctx
= { dl
, show_device
, show_port
};
7388 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7391 if (dl_argc(dl
) == 0)
7392 flags
|= NLM_F_DUMP
;
7393 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7396 if (dl_argc(dl
) > 0) {
7397 ctx
.show_port
= true;
7398 err
= dl_argv_parse_put(nlh
, dl
,
7399 DL_OPT_HANDLE
| DL_OPT_HANDLEP
|
7400 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7404 pr_out_section_start(dl
, "health");
7406 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, &ctx
);
7407 pr_out_section_end(dl
);
7411 static void cmd_health_help(void)
7413 pr_err("Usage: devlink health show [ { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME ]\n");
7414 pr_err(" devlink health recover { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7415 pr_err(" devlink health diagnose { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7416 pr_err(" devlink health test { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7417 pr_err(" devlink health dump show { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7418 pr_err(" devlink health dump clear { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7419 pr_err(" devlink health set { DEV | DEV/PORT_INDEX } reporter REPORTER_NAME\n");
7420 pr_err(" [ grace_period MSEC ]\n");
7421 pr_err(" [ auto_recover { true | false } ]\n");
7422 pr_err(" [ auto_dump { true | false } ]\n");
7425 static int cmd_health(struct dl
*dl
)
7427 if (dl_argv_match(dl
, "help")) {
7430 } else if (dl_argv_match(dl
, "show") ||
7431 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7433 return __cmd_health_show(dl
, true, true);
7434 } else if (dl_argv_match(dl
, "recover")) {
7436 return cmd_health_recover(dl
);
7437 } else if (dl_argv_match(dl
, "diagnose")) {
7439 return cmd_health_diagnose(dl
);
7440 } else if (dl_argv_match(dl
, "test")) {
7442 return cmd_health_test(dl
);
7443 } else if (dl_argv_match(dl
, "dump")) {
7445 if (dl_argv_match(dl
, "show")) {
7447 return cmd_health_dump_show(dl
);
7448 } else if (dl_argv_match(dl
, "clear")) {
7450 return cmd_health_dump_clear(dl
);
7452 } else if (dl_argv_match(dl
, "set")) {
7454 return cmd_health_set_params(dl
);
7456 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7460 static const char *trap_type_name(uint8_t type
)
7463 case DEVLINK_TRAP_TYPE_DROP
:
7465 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7467 case DEVLINK_TRAP_TYPE_CONTROL
:
7470 return "<unknown type>";
7474 static const char *trap_action_name(uint8_t action
)
7477 case DEVLINK_TRAP_ACTION_DROP
:
7479 case DEVLINK_TRAP_ACTION_TRAP
:
7481 case DEVLINK_TRAP_ACTION_MIRROR
:
7484 return "<unknown action>";
7488 static const char *trap_metadata_name(const struct nlattr
*attr
)
7490 switch (attr
->nla_type
) {
7491 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7492 return "input_port";
7493 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7494 return "flow_action_cookie";
7496 return "<unknown metadata type>";
7499 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7501 struct nlattr
*attr_metadata
;
7503 pr_out_array_start(dl
, "metadata");
7504 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7505 check_indent_newline(dl
);
7506 print_string(PRINT_ANY
, NULL
, "%s",
7507 trap_metadata_name(attr_metadata
));
7509 pr_out_array_end(dl
);
7512 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7514 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7515 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7518 pr_out_handle_start_arr(dl
, tb
);
7520 __pr_out_handle_start(dl
, tb
, true, false);
7522 check_indent_newline(dl
);
7523 print_string(PRINT_ANY
, "name", "name %s",
7524 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7525 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7526 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7527 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7528 print_string(PRINT_ANY
, "group", " group %s",
7529 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7531 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7532 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7533 pr_out_handle_end(dl
);
7536 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7538 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7539 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7540 struct dl
*dl
= data
;
7542 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7543 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7544 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7545 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7546 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7547 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7548 return MNL_CB_ERROR
;
7550 pr_out_trap(dl
, tb
, true);
7555 static void cmd_trap_help(void)
7557 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7558 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7559 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7560 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7561 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7562 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7563 pr_err(" devlink trap policer show DEV policer POLICER\n");
7566 static int cmd_trap_show(struct dl
*dl
)
7568 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7569 struct nlmsghdr
*nlh
;
7572 if (dl_argc(dl
) == 0)
7573 flags
|= NLM_F_DUMP
;
7575 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7577 if (dl_argc(dl
) > 0) {
7578 err
= dl_argv_parse_put(nlh
, dl
,
7579 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7584 pr_out_section_start(dl
, "trap");
7585 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7586 pr_out_section_end(dl
);
7591 static int cmd_trap_set(struct dl
*dl
)
7593 struct nlmsghdr
*nlh
;
7596 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7597 NLM_F_REQUEST
| NLM_F_ACK
);
7599 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7600 DL_OPT_TRAP_ACTION
);
7604 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7607 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7610 pr_out_handle_start_arr(dl
, tb
);
7612 __pr_out_handle_start(dl
, tb
, true, false);
7614 check_indent_newline(dl
);
7615 print_string(PRINT_ANY
, "name", "name %s",
7616 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7617 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7618 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7619 print_uint(PRINT_ANY
, "policer", " policer %u",
7620 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7621 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7622 pr_out_handle_end(dl
);
7625 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7627 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7628 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7629 struct dl
*dl
= data
;
7631 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7632 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7633 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7634 return MNL_CB_ERROR
;
7636 pr_out_trap_group(dl
, tb
, true);
7641 static int cmd_trap_group_show(struct dl
*dl
)
7643 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7644 struct nlmsghdr
*nlh
;
7647 if (dl_argc(dl
) == 0)
7648 flags
|= NLM_F_DUMP
;
7650 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7652 if (dl_argc(dl
) > 0) {
7653 err
= dl_argv_parse_put(nlh
, dl
,
7654 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7660 pr_out_section_start(dl
, "trap_group");
7661 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7662 pr_out_section_end(dl
);
7667 static int cmd_trap_group_set(struct dl
*dl
)
7669 struct nlmsghdr
*nlh
;
7672 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7673 NLM_F_REQUEST
| NLM_F_ACK
);
7675 err
= dl_argv_parse_put(nlh
, dl
,
7676 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7677 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7681 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7684 static int cmd_trap_group(struct dl
*dl
)
7686 if (dl_argv_match(dl
, "help")) {
7689 } else if (dl_argv_match(dl
, "show") ||
7690 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7692 return cmd_trap_group_show(dl
);
7693 } else if (dl_argv_match(dl
, "set")) {
7695 return cmd_trap_group_set(dl
);
7697 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7701 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7704 pr_out_handle_start_arr(dl
, tb
);
7706 __pr_out_handle_start(dl
, tb
, true, false);
7708 check_indent_newline(dl
);
7709 print_uint(PRINT_ANY
, "policer", "policer %u",
7710 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7711 print_u64(PRINT_ANY
, "rate", " rate %llu",
7712 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7713 print_u64(PRINT_ANY
, "burst", " burst %llu",
7714 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7715 if (tb
[DEVLINK_ATTR_STATS
])
7716 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7717 pr_out_handle_end(dl
);
7720 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7722 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7723 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7724 struct dl
*dl
= data
;
7726 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7727 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7728 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7729 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7730 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7731 return MNL_CB_ERROR
;
7733 pr_out_trap_policer(dl
, tb
, true);
7738 static int cmd_trap_policer_show(struct dl
*dl
)
7740 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7741 struct nlmsghdr
*nlh
;
7744 if (dl_argc(dl
) == 0)
7745 flags
|= NLM_F_DUMP
;
7747 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7749 if (dl_argc(dl
) > 0) {
7750 err
= dl_argv_parse_put(nlh
, dl
,
7751 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7757 pr_out_section_start(dl
, "trap_policer");
7758 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7759 pr_out_section_end(dl
);
7764 static int cmd_trap_policer_set(struct dl
*dl
)
7766 struct nlmsghdr
*nlh
;
7769 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7770 NLM_F_REQUEST
| NLM_F_ACK
);
7772 err
= dl_argv_parse_put(nlh
, dl
,
7773 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7774 DL_OPT_TRAP_POLICER_RATE
|
7775 DL_OPT_TRAP_POLICER_BURST
);
7779 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7782 static int cmd_trap_policer(struct dl
*dl
)
7784 if (dl_argv_match(dl
, "help")) {
7787 } else if (dl_argv_match(dl
, "show") ||
7788 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7790 return cmd_trap_policer_show(dl
);
7791 } else if (dl_argv_match(dl
, "set")) {
7793 return cmd_trap_policer_set(dl
);
7795 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7799 static int cmd_trap(struct dl
*dl
)
7801 if (dl_argv_match(dl
, "help")) {
7804 } else if (dl_argv_match(dl
, "show") ||
7805 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7807 return cmd_trap_show(dl
);
7808 } else if (dl_argv_match(dl
, "set")) {
7810 return cmd_trap_set(dl
);
7811 } else if (dl_argv_match(dl
, "group")) {
7813 return cmd_trap_group(dl
);
7814 } else if (dl_argv_match(dl
, "policer")) {
7816 return cmd_trap_policer(dl
);
7818 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7822 static void help(void)
7824 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7825 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7826 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7827 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7830 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7835 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7838 } else if (dl_argv_match(dl
, "dev")) {
7841 } else if (dl_argv_match(dl
, "port")) {
7843 return cmd_port(dl
);
7844 } else if (dl_argv_match(dl
, "sb")) {
7847 } else if (dl_argv_match(dl
, "monitor")) {
7850 } else if (dl_argv_match(dl
, "dpipe")) {
7852 return cmd_dpipe(dl
);
7853 } else if (dl_argv_match(dl
, "resource")) {
7855 return cmd_resource(dl
);
7856 } else if (dl_argv_match(dl
, "region")) {
7858 return cmd_region(dl
);
7859 } else if (dl_argv_match(dl
, "health")) {
7861 return cmd_health(dl
);
7862 } else if (dl_argv_match(dl
, "trap")) {
7864 return cmd_trap(dl
);
7866 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7870 static int dl_init(struct dl
*dl
)
7874 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7876 pr_err("Failed to connect to devlink Netlink\n");
7880 err
= ifname_map_init(dl
);
7882 pr_err("Failed to create index map\n");
7883 goto err_ifname_map_create
;
7885 new_json_obj_plain(dl
->json_output
);
7888 err_ifname_map_create
:
7889 mnlg_socket_close(dl
->nlg
);
7893 static void dl_fini(struct dl
*dl
)
7895 delete_json_obj_plain();
7896 ifname_map_fini(dl
);
7897 mnlg_socket_close(dl
->nlg
);
7900 static struct dl
*dl_alloc(void)
7904 dl
= calloc(1, sizeof(*dl
));
7910 static void dl_free(struct dl
*dl
)
7915 static int dl_batch_cmd(int argc
, char *argv
[], void *data
)
7917 struct dl
*dl
= data
;
7919 return dl_cmd(dl
, argc
, argv
);
7922 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7924 return do_batch(name
, force
, dl_batch_cmd
, dl
);
7927 int main(int argc
, char **argv
)
7929 static const struct option long_options
[] = {
7930 { "Version", no_argument
, NULL
, 'V' },
7931 { "force", no_argument
, NULL
, 'f' },
7932 { "batch", required_argument
, NULL
, 'b' },
7933 { "no-nice-names", no_argument
, NULL
, 'n' },
7934 { "json", no_argument
, NULL
, 'j' },
7935 { "pretty", no_argument
, NULL
, 'p' },
7936 { "verbose", no_argument
, NULL
, 'v' },
7937 { "statistics", no_argument
, NULL
, 's' },
7938 { "Netns", required_argument
, NULL
, 'N' },
7939 { NULL
, 0, NULL
, 0 }
7941 const char *batch_file
= NULL
;
7950 pr_err("Failed to allocate memory for devlink\n");
7951 return EXIT_FAILURE
;
7954 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7955 long_options
, NULL
)) >= 0) {
7959 printf("devlink utility, iproute2-%s\n", version
);
7966 batch_file
= optarg
;
7969 dl
->no_nice_names
= true;
7972 dl
->json_output
= true;
7984 if (netns_switch(optarg
)) {
7990 pr_err("Unknown option.\n");
8007 err
= dl_batch(dl
, batch_file
, force
);
8009 err
= dl_cmd(dl
, argc
, argv
);