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>
40 #include "json_print.h"
42 #include "namespace.h"
44 #define ESWITCH_MODE_LEGACY "legacy"
45 #define ESWITCH_MODE_SWITCHDEV "switchdev"
46 #define ESWITCH_INLINE_MODE_NONE "none"
47 #define ESWITCH_INLINE_MODE_LINK "link"
48 #define ESWITCH_INLINE_MODE_NETWORK "network"
49 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
51 #define ESWITCH_ENCAP_MODE_NONE "none"
52 #define ESWITCH_ENCAP_MODE_BASIC "basic"
54 #define PARAM_CMODE_RUNTIME_STR "runtime"
55 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
56 #define PARAM_CMODE_PERMANENT_STR "permanent"
57 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
59 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
60 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
61 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
63 static int g_new_line_count
;
64 static int g_indent_level
;
65 static bool g_indent_newline
;
67 #define INDENT_STR_STEP 2
68 #define INDENT_STR_MAXLEN 32
69 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
71 static void __attribute__((format(printf
, 1, 2)))
72 pr_err(const char *fmt
, ...)
77 vfprintf(stderr
, fmt
, ap
);
81 static void __attribute__((format(printf
, 1, 2)))
82 pr_out(const char *fmt
, ...)
86 if (g_indent_newline
) {
87 printf("%s", g_indent_str
);
88 g_indent_newline
= false;
96 static void __attribute__((format(printf
, 2, 3)))
97 pr_out_sp(unsigned int num
, const char *fmt
, ...)
103 ret
= vprintf(fmt
, ap
);
107 printf("%*s", num
- ret
, "");
108 g_new_line_count
= 0; \
111 static void __attribute__((format(printf
, 1, 2)))
112 pr_out_tty(const char *fmt
, ...)
116 if (!isatty(STDOUT_FILENO
))
123 static void __pr_out_indent_inc(void)
125 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
127 g_indent_level
+= INDENT_STR_STEP
;
128 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
129 g_indent_str
[g_indent_level
] = '\0';
132 static void __pr_out_indent_dec(void)
134 if (g_indent_level
- INDENT_STR_STEP
< 0)
136 g_indent_level
-= INDENT_STR_STEP
;
137 g_indent_str
[g_indent_level
] = '\0';
140 static void __pr_out_newline(void)
142 if (g_new_line_count
< 1) {
144 g_indent_newline
= true;
149 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
150 mnl_cb_t data_cb
, void *data
)
154 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
156 pr_err("devlink answers: %s\n", strerror(errno
));
162 static void dummy_signal_handler(int signum
)
166 static int _mnlg_socket_recv_run_intr(struct mnlg_socket
*nlg
,
167 mnl_cb_t data_cb
, void *data
)
169 struct sigaction act
, oact
;
172 act
.sa_handler
= dummy_signal_handler
;
173 sigemptyset(&act
.sa_mask
);
174 act
.sa_flags
= SA_NODEFER
;
176 sigaction(SIGINT
, &act
, &oact
);
177 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
178 sigaction(SIGINT
, &oact
, NULL
);
179 if (err
< 0 && errno
!= EINTR
) {
180 pr_err("devlink answers: %s\n", strerror(errno
));
186 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
187 const struct nlmsghdr
*nlh
)
191 err
= mnlg_socket_send(nlg
, nlh
);
193 pr_err("Failed to call mnlg_socket_send\n");
199 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
200 const struct nlmsghdr
*nlh
,
201 mnl_cb_t data_cb
, void *data
)
205 err
= _mnlg_socket_send(nlg
, nlh
);
208 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
211 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
212 const char *group_name
)
216 err
= mnlg_socket_group_add(nlg
, group_name
);
218 pr_err("Failed to call mnlg_socket_group_add\n");
225 struct list_head list
;
232 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
233 const char *dev_name
,
237 struct ifname_map
*ifname_map
;
239 ifname_map
= calloc(1, sizeof(*ifname_map
));
242 ifname_map
->bus_name
= strdup(bus_name
);
243 ifname_map
->dev_name
= strdup(dev_name
);
244 ifname_map
->port_index
= port_index
;
245 ifname_map
->ifname
= strdup(ifname
);
246 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
247 !ifname_map
->ifname
) {
248 free(ifname_map
->ifname
);
249 free(ifname_map
->dev_name
);
250 free(ifname_map
->bus_name
);
257 static void ifname_map_free(struct ifname_map
*ifname_map
)
259 free(ifname_map
->ifname
);
260 free(ifname_map
->dev_name
);
261 free(ifname_map
->bus_name
);
265 #define DL_OPT_HANDLE BIT(0)
266 #define DL_OPT_HANDLEP BIT(1)
267 #define DL_OPT_PORT_TYPE BIT(2)
268 #define DL_OPT_PORT_COUNT BIT(3)
269 #define DL_OPT_SB BIT(4)
270 #define DL_OPT_SB_POOL BIT(5)
271 #define DL_OPT_SB_SIZE BIT(6)
272 #define DL_OPT_SB_TYPE BIT(7)
273 #define DL_OPT_SB_THTYPE BIT(8)
274 #define DL_OPT_SB_TH BIT(9)
275 #define DL_OPT_SB_TC BIT(10)
276 #define DL_OPT_ESWITCH_MODE BIT(11)
277 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
278 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
279 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
280 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
281 #define DL_OPT_RESOURCE_PATH BIT(16)
282 #define DL_OPT_RESOURCE_SIZE BIT(17)
283 #define DL_OPT_PARAM_NAME BIT(18)
284 #define DL_OPT_PARAM_VALUE BIT(19)
285 #define DL_OPT_PARAM_CMODE BIT(20)
286 #define DL_OPT_HANDLE_REGION BIT(21)
287 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
288 #define DL_OPT_REGION_ADDRESS BIT(23)
289 #define DL_OPT_REGION_LENGTH BIT(24)
290 #define DL_OPT_FLASH_FILE_NAME BIT(25)
291 #define DL_OPT_FLASH_COMPONENT BIT(26)
292 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
293 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
294 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
295 #define DL_OPT_TRAP_NAME BIT(30)
296 #define DL_OPT_TRAP_ACTION BIT(31)
297 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
298 #define DL_OPT_NETNS BIT(33)
299 #define DL_OPT_TRAP_POLICER_ID BIT(34)
300 #define DL_OPT_TRAP_POLICER_RATE BIT(35)
301 #define DL_OPT_TRAP_POLICER_BURST BIT(36)
302 #define DL_OPT_HEALTH_REPORTER_AUTO_DUMP BIT(37)
305 uint64_t present
; /* flags of present items */
309 enum devlink_port_type port_type
;
312 uint16_t sb_pool_index
;
313 uint32_t sb_pool_size
;
314 enum devlink_sb_pool_type sb_pool_type
;
315 enum devlink_sb_threshold_type sb_pool_thtype
;
316 uint32_t sb_threshold
;
317 uint16_t sb_tc_index
;
318 enum devlink_eswitch_mode eswitch_mode
;
319 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
320 const char *dpipe_table_name
;
321 bool dpipe_counters_enabled
;
322 enum devlink_eswitch_encap_mode eswitch_encap_mode
;
323 const char *resource_path
;
324 uint64_t resource_size
;
325 uint32_t resource_id
;
326 bool resource_id_valid
;
327 const char *param_name
;
328 const char *param_value
;
329 enum devlink_param_cmode cmode
;
331 uint32_t region_snapshot_id
;
332 uint64_t region_address
;
333 uint64_t region_length
;
334 const char *flash_file_name
;
335 const char *flash_component
;
336 const char *reporter_name
;
337 uint64_t reporter_graceful_period
;
338 bool reporter_auto_recover
;
339 bool reporter_auto_dump
;
340 const char *trap_name
;
341 const char *trap_group_name
;
342 enum devlink_trap_action trap_action
;
345 uint32_t trap_policer_id
;
346 uint64_t trap_policer_rate
;
347 uint64_t trap_policer_burst
;
351 struct mnlg_socket
*nlg
;
352 struct list_head ifname_map_list
;
369 static int dl_argc(struct dl
*dl
)
374 static char *dl_argv(struct dl
*dl
)
376 if (dl_argc(dl
) == 0)
381 static void dl_arg_inc(struct dl
*dl
)
383 if (dl_argc(dl
) == 0)
389 static void dl_arg_dec(struct dl
*dl
)
395 static char *dl_argv_next(struct dl
*dl
)
399 if (dl_argc(dl
) == 0)
407 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
409 if (index
>= dl_argc(dl
))
411 return dl
->argv
[index
];
414 static int strcmpx(const char *str1
, const char *str2
)
416 if (strlen(str1
) > strlen(str2
))
418 return strncmp(str1
, str2
, strlen(str1
));
421 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
423 if (dl_argc(dl
) == 0)
425 return strcmpx(dl_argv(dl
), pattern
) == 0;
428 static bool dl_no_arg(struct dl
*dl
)
430 return dl_argc(dl
) == 0;
433 static void __pr_out_indent_newline(struct dl
*dl
)
435 if (!g_indent_newline
&& !dl
->json_output
)
439 static void check_indent_newline(struct dl
*dl
)
441 __pr_out_indent_newline(dl
);
443 if (g_indent_newline
&& !is_json_context()) {
444 printf("%s", g_indent_str
);
445 g_indent_newline
= false;
447 g_new_line_count
= 0;
450 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
451 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
452 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
453 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
454 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
455 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
456 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
457 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
458 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
459 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
460 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
461 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
462 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
463 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
464 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
465 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
466 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
467 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
468 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
469 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
470 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
471 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
472 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
473 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
474 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
475 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
476 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
477 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
478 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
479 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
480 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
481 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
482 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
483 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
484 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
485 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
486 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
487 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
488 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
489 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
490 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
491 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
492 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
493 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
494 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
495 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
496 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
497 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
498 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
499 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
500 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
501 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
502 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
503 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
504 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
505 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
506 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
507 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
508 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
509 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
510 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
511 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
512 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
513 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
514 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
515 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
516 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
517 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
518 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
519 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
520 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
521 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
522 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
523 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
524 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
525 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
526 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
527 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
528 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
529 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
530 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
531 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
532 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
533 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
534 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
535 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
536 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
537 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
538 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
539 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
540 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
541 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
542 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
543 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
544 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
545 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
546 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
547 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
548 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
549 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
550 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
551 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
552 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
555 static const enum mnl_attr_data_type
556 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
557 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
558 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
559 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
562 static int attr_cb(const struct nlattr
*attr
, void *data
)
564 const struct nlattr
**tb
= data
;
567 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
570 type
= mnl_attr_get_type(attr
);
571 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
578 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
580 const struct nlattr
**tb
= data
;
583 /* Allow the tool to work on top of newer kernels that might contain
586 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
589 type
= mnl_attr_get_type(attr
);
590 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
597 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
599 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
600 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
601 struct dl
*dl
= data
;
602 struct ifname_map
*ifname_map
;
603 const char *bus_name
;
604 const char *dev_name
;
605 uint32_t port_ifindex
;
606 const char *port_ifname
;
608 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
609 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
610 !tb
[DEVLINK_ATTR_PORT_INDEX
])
613 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
616 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
617 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
618 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
619 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
620 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
621 port_ifindex
, port_ifname
);
624 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
629 static void ifname_map_fini(struct dl
*dl
)
631 struct ifname_map
*ifname_map
, *tmp
;
633 list_for_each_entry_safe(ifname_map
, tmp
,
634 &dl
->ifname_map_list
, list
) {
635 list_del(&ifname_map
->list
);
636 ifname_map_free(ifname_map
);
640 static int ifname_map_init(struct dl
*dl
)
642 struct nlmsghdr
*nlh
;
645 INIT_LIST_HEAD(&dl
->ifname_map_list
);
647 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
648 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
650 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
658 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
659 char **p_bus_name
, char **p_dev_name
,
660 uint32_t *p_port_index
)
662 struct ifname_map
*ifname_map
;
664 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
665 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
666 *p_bus_name
= ifname_map
->bus_name
;
667 *p_dev_name
= ifname_map
->dev_name
;
668 *p_port_index
= ifname_map
->port_index
;
675 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
676 const char *dev_name
, uint32_t port_index
,
679 struct ifname_map
*ifname_map
;
681 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
682 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
683 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
684 port_index
== ifname_map
->port_index
) {
685 *p_ifname
= ifname_map
->ifname
;
692 static unsigned int strslashcount(char *str
)
694 unsigned int count
= 0;
697 while ((pos
= strchr(pos
, '/'))) {
704 static int strslashrsplit(char *str
, char **before
, char **after
)
708 slash
= strrchr(str
, '/');
717 static int strtouint64_t(const char *str
, uint64_t *p_val
)
720 unsigned long long int val
;
722 val
= strtoull(str
, &endptr
, 10);
723 if (endptr
== str
|| *endptr
!= '\0')
731 static int strtouint32_t(const char *str
, uint32_t *p_val
)
734 unsigned long int val
;
736 val
= strtoul(str
, &endptr
, 10);
737 if (endptr
== str
|| *endptr
!= '\0')
745 static int strtouint16_t(const char *str
, uint16_t *p_val
)
748 unsigned long int val
;
750 val
= strtoul(str
, &endptr
, 10);
751 if (endptr
== str
|| *endptr
!= '\0')
759 static int strtouint8_t(const char *str
, uint8_t *p_val
)
762 unsigned long int val
;
764 val
= strtoul(str
, &endptr
, 10);
765 if (endptr
== str
|| *endptr
!= '\0')
773 static int strtobool(const char *str
, bool *p_val
)
777 if (!strcmp(str
, "true") || !strcmp(str
, "1") ||
778 !strcmp(str
, "enable"))
780 else if (!strcmp(str
, "false") || !strcmp(str
, "0") ||
781 !strcmp(str
, "disable"))
789 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
791 strslashrsplit(str
, p_bus_name
, p_dev_name
);
795 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
797 char *str
= dl_argv_next(dl
);
800 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
803 if (strslashcount(str
) != 1) {
804 pr_err("Wrong devlink identification string format.\n");
805 pr_err("Expected \"bus_name/dev_name\".\n");
808 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
811 static int __dl_argv_handle_port(char *str
,
812 char **p_bus_name
, char **p_dev_name
,
813 uint32_t *p_port_index
)
819 err
= strslashrsplit(str
, &handlestr
, &portstr
);
821 pr_err("Port identification \"%s\" is invalid\n", str
);
824 err
= strtouint32_t(portstr
, p_port_index
);
826 pr_err("Port index \"%s\" is not a number or not within range\n",
830 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
832 pr_err("Port identification \"%s\" is invalid\n", str
);
838 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
839 char **p_bus_name
, char **p_dev_name
,
840 uint32_t *p_port_index
)
844 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
847 pr_err("Netdevice \"%s\" not found\n", str
);
853 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
854 char **p_dev_name
, uint32_t *p_port_index
)
856 char *str
= dl_argv_next(dl
);
857 unsigned int slash_count
;
860 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
863 slash_count
= strslashcount(str
);
864 switch (slash_count
) {
866 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
867 p_dev_name
, p_port_index
);
869 return __dl_argv_handle_port(str
, p_bus_name
,
870 p_dev_name
, p_port_index
);
872 pr_err("Wrong port identification string format.\n");
873 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
878 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
879 char **p_dev_name
, uint32_t *p_port_index
,
880 uint64_t *p_handle_bit
)
882 char *str
= dl_argv_next(dl
);
883 unsigned int slash_count
;
887 pr_err("One of following identifications expected:\n"
888 "Devlink identification (\"bus_name/dev_name\")\n"
889 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
892 slash_count
= strslashcount(str
);
893 if (slash_count
== 1) {
894 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
897 *p_handle_bit
= DL_OPT_HANDLE
;
898 } else if (slash_count
== 2) {
899 err
= __dl_argv_handle_port(str
, p_bus_name
,
900 p_dev_name
, p_port_index
);
903 *p_handle_bit
= DL_OPT_HANDLEP
;
904 } else if (slash_count
== 0) {
905 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
906 p_dev_name
, p_port_index
);
909 *p_handle_bit
= DL_OPT_HANDLEP
;
911 pr_err("Wrong port identification string format.\n");
912 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
918 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
919 char **p_dev_name
, char **p_region
)
924 err
= strslashrsplit(str
, &handlestr
, p_region
);
926 pr_err("Region identification \"%s\" is invalid\n", str
);
929 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
931 pr_err("Region identification \"%s\" is invalid\n", str
);
937 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
938 char **p_dev_name
, char **p_region
)
940 char *str
= dl_argv_next(dl
);
941 unsigned int slash_count
;
944 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
948 slash_count
= strslashcount(str
);
949 if (slash_count
!= 2) {
950 pr_err("Wrong region identification string format.\n");
951 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
955 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
958 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
960 char *str
= dl_argv_next(dl
);
964 pr_err("Unsigned number argument expected\n");
968 err
= strtouint64_t(str
, p_val
);
970 pr_err("\"%s\" is not a number or not within range\n", str
);
976 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
978 char *str
= dl_argv_next(dl
);
982 pr_err("Unsigned number argument expected\n");
986 err
= strtouint32_t(str
, p_val
);
988 pr_err("\"%s\" is not a number or not within range\n", str
);
994 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
996 char *str
= dl_argv_next(dl
);
1000 pr_err("Unsigned number argument expected\n");
1004 err
= strtouint16_t(str
, p_val
);
1006 pr_err("\"%s\" is not a number or not within range\n", str
);
1012 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
1014 char *str
= dl_argv_next(dl
);
1018 pr_err("Boolean argument expected\n");
1022 err
= strtobool(str
, p_val
);
1024 pr_err("\"%s\" is not a valid boolean value\n", str
);
1030 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
1032 const char *str
= dl_argv_next(dl
);
1035 pr_err("String parameter expected\n");
1042 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1044 if (strcmp(typestr
, "auto") == 0) {
1045 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1046 } else if (strcmp(typestr
, "eth") == 0) {
1047 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1048 } else if (strcmp(typestr
, "ib") == 0) {
1049 *p_type
= DEVLINK_PORT_TYPE_IB
;
1051 pr_err("Unknown port type \"%s\"\n", typestr
);
1057 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1059 if (strcmp(typestr
, "ingress") == 0) {
1060 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1061 } else if (strcmp(typestr
, "egress") == 0) {
1062 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1064 pr_err("Unknown pool type \"%s\"\n", typestr
);
1070 static int threshold_type_get(const char *typestr
,
1071 enum devlink_sb_threshold_type
*p_type
)
1073 if (strcmp(typestr
, "static") == 0) {
1074 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1075 } else if (strcmp(typestr
, "dynamic") == 0) {
1076 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1078 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1084 static int eswitch_mode_get(const char *typestr
,
1085 enum devlink_eswitch_mode
*p_mode
)
1087 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1088 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1089 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1090 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1092 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1098 static int eswitch_inline_mode_get(const char *typestr
,
1099 enum devlink_eswitch_inline_mode
*p_mode
)
1101 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1102 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1103 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1104 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1105 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1106 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1107 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1108 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1110 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1117 eswitch_encap_mode_get(const char *typestr
,
1118 enum devlink_eswitch_encap_mode
*p_encap_mode
)
1120 /* The initial implementation incorrectly accepted "enable"/"disable".
1121 * Carry it to maintain backward compatibility.
1123 if (strcmp(typestr
, "disable") == 0 ||
1124 strcmp(typestr
, ESWITCH_ENCAP_MODE_NONE
) == 0) {
1125 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1126 } else if (strcmp(typestr
, "enable") == 0 ||
1127 strcmp(typestr
, ESWITCH_ENCAP_MODE_BASIC
) == 0) {
1128 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1130 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1136 static int param_cmode_get(const char *cmodestr
,
1137 enum devlink_param_cmode
*cmode
)
1139 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1140 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1141 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1142 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1143 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1144 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1146 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1152 static int trap_action_get(const char *actionstr
,
1153 enum devlink_trap_action
*p_action
)
1155 if (strcmp(actionstr
, "drop") == 0) {
1156 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1157 } else if (strcmp(actionstr
, "trap") == 0) {
1158 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1159 } else if (strcmp(actionstr
, "mirror") == 0) {
1160 *p_action
= DEVLINK_TRAP_ACTION_MIRROR
;
1162 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1168 struct dl_args_metadata
{
1170 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1173 static const struct dl_args_metadata dl_args_required
[] = {
1174 {DL_OPT_PORT_TYPE
, "Port type not set."},
1175 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1176 {DL_OPT_SB_POOL
, "Pool index option expected."},
1177 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1178 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1179 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1180 {DL_OPT_SB_TH
, "Threshold option expected."},
1181 {DL_OPT_SB_TC
, "TC index option expected."},
1182 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1183 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1184 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1185 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1186 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1187 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1188 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1189 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1190 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1191 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1192 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1193 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1194 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1195 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1196 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1197 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1200 static int dl_args_finding_required_validate(uint64_t o_required
,
1206 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1207 o_flag
= dl_args_required
[i
].o_flag
;
1208 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1209 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1213 if (o_required
& ~o_found
) {
1214 pr_err("BUG: unknown argument required but not found\n");
1220 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1221 uint64_t o_optional
)
1223 struct dl_opts
*opts
= &dl
->opts
;
1224 uint64_t o_all
= o_required
| o_optional
;
1225 uint64_t o_found
= 0;
1228 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1229 uint64_t handle_bit
;
1231 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1232 &opts
->port_index
, &handle_bit
);
1235 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1236 o_found
|= handle_bit
;
1237 } else if (o_required
& DL_OPT_HANDLE
) {
1238 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1241 o_found
|= DL_OPT_HANDLE
;
1242 } else if (o_required
& DL_OPT_HANDLEP
) {
1243 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1247 o_found
|= DL_OPT_HANDLEP
;
1248 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1249 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1251 &opts
->region_name
);
1254 o_found
|= DL_OPT_HANDLE_REGION
;
1257 while (dl_argc(dl
)) {
1258 if (dl_argv_match(dl
, "type") &&
1259 (o_all
& DL_OPT_PORT_TYPE
)) {
1260 const char *typestr
;
1263 err
= dl_argv_str(dl
, &typestr
);
1266 err
= port_type_get(typestr
, &opts
->port_type
);
1269 o_found
|= DL_OPT_PORT_TYPE
;
1270 } else if (dl_argv_match(dl
, "count") &&
1271 (o_all
& DL_OPT_PORT_COUNT
)) {
1273 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1276 o_found
|= DL_OPT_PORT_COUNT
;
1277 } else if (dl_argv_match(dl
, "sb") &&
1278 (o_all
& DL_OPT_SB
)) {
1280 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1283 o_found
|= DL_OPT_SB
;
1284 } else if (dl_argv_match(dl
, "pool") &&
1285 (o_all
& DL_OPT_SB_POOL
)) {
1287 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1290 o_found
|= DL_OPT_SB_POOL
;
1291 } else if (dl_argv_match(dl
, "size") &&
1292 (o_all
& DL_OPT_SB_SIZE
)) {
1294 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1297 o_found
|= DL_OPT_SB_SIZE
;
1298 } else if (dl_argv_match(dl
, "type") &&
1299 (o_all
& DL_OPT_SB_TYPE
)) {
1300 const char *typestr
;
1303 err
= dl_argv_str(dl
, &typestr
);
1306 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1309 o_found
|= DL_OPT_SB_TYPE
;
1310 } else if (dl_argv_match(dl
, "thtype") &&
1311 (o_all
& DL_OPT_SB_THTYPE
)) {
1312 const char *typestr
;
1315 err
= dl_argv_str(dl
, &typestr
);
1318 err
= threshold_type_get(typestr
,
1319 &opts
->sb_pool_thtype
);
1322 o_found
|= DL_OPT_SB_THTYPE
;
1323 } else if (dl_argv_match(dl
, "th") &&
1324 (o_all
& DL_OPT_SB_TH
)) {
1326 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1329 o_found
|= DL_OPT_SB_TH
;
1330 } else if (dl_argv_match(dl
, "tc") &&
1331 (o_all
& DL_OPT_SB_TC
)) {
1333 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1336 o_found
|= DL_OPT_SB_TC
;
1337 } else if (dl_argv_match(dl
, "mode") &&
1338 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1339 const char *typestr
;
1342 err
= dl_argv_str(dl
, &typestr
);
1345 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1348 o_found
|= DL_OPT_ESWITCH_MODE
;
1349 } else if (dl_argv_match(dl
, "inline-mode") &&
1350 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1351 const char *typestr
;
1354 err
= dl_argv_str(dl
, &typestr
);
1357 err
= eswitch_inline_mode_get(
1358 typestr
, &opts
->eswitch_inline_mode
);
1361 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1362 } else if (dl_argv_match(dl
, "name") &&
1363 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1365 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1368 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1369 } else if ((dl_argv_match(dl
, "counters") ||
1370 dl_argv_match(dl
, "counters_enabled")) &&
1371 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1373 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1376 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1377 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1378 dl_argv_match(dl
, "encap-mode")) &&
1379 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1380 const char *typestr
;
1383 err
= dl_argv_str(dl
, &typestr
);
1386 err
= eswitch_encap_mode_get(typestr
,
1387 &opts
->eswitch_encap_mode
);
1390 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1391 } else if (dl_argv_match(dl
, "path") &&
1392 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1394 err
= dl_argv_str(dl
, &opts
->resource_path
);
1397 o_found
|= DL_OPT_RESOURCE_PATH
;
1398 } else if (dl_argv_match(dl
, "size") &&
1399 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1401 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1404 o_found
|= DL_OPT_RESOURCE_SIZE
;
1405 } else if (dl_argv_match(dl
, "name") &&
1406 (o_all
& DL_OPT_PARAM_NAME
)) {
1408 err
= dl_argv_str(dl
, &opts
->param_name
);
1411 o_found
|= DL_OPT_PARAM_NAME
;
1412 } else if (dl_argv_match(dl
, "value") &&
1413 (o_all
& DL_OPT_PARAM_VALUE
)) {
1415 err
= dl_argv_str(dl
, &opts
->param_value
);
1418 o_found
|= DL_OPT_PARAM_VALUE
;
1419 } else if (dl_argv_match(dl
, "cmode") &&
1420 (o_all
& DL_OPT_PARAM_CMODE
)) {
1421 const char *cmodestr
;
1424 err
= dl_argv_str(dl
, &cmodestr
);
1427 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1430 o_found
|= DL_OPT_PARAM_CMODE
;
1431 } else if (dl_argv_match(dl
, "snapshot") &&
1432 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1434 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1437 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1438 } else if (dl_argv_match(dl
, "address") &&
1439 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1441 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1444 o_found
|= DL_OPT_REGION_ADDRESS
;
1445 } else if (dl_argv_match(dl
, "length") &&
1446 (o_all
& DL_OPT_REGION_LENGTH
)) {
1448 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1451 o_found
|= DL_OPT_REGION_LENGTH
;
1452 } else if (dl_argv_match(dl
, "file") &&
1453 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1455 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1458 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1459 } else if (dl_argv_match(dl
, "component") &&
1460 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1462 err
= dl_argv_str(dl
, &opts
->flash_component
);
1465 o_found
|= DL_OPT_FLASH_COMPONENT
;
1466 } else if (dl_argv_match(dl
, "reporter") &&
1467 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1469 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1472 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1473 } else if (dl_argv_match(dl
, "grace_period") &&
1474 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1476 err
= dl_argv_uint64_t(dl
,
1477 &opts
->reporter_graceful_period
);
1480 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1481 } else if (dl_argv_match(dl
, "auto_recover") &&
1482 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1484 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1487 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1488 } else if (dl_argv_match(dl
, "auto_dump") &&
1489 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)) {
1491 err
= dl_argv_bool(dl
, &opts
->reporter_auto_dump
);
1494 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_DUMP
;
1495 } else if (dl_argv_match(dl
, "trap") &&
1496 (o_all
& DL_OPT_TRAP_NAME
)) {
1498 err
= dl_argv_str(dl
, &opts
->trap_name
);
1501 o_found
|= DL_OPT_TRAP_NAME
;
1502 } else if (dl_argv_match(dl
, "group") &&
1503 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1505 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1508 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1509 } else if (dl_argv_match(dl
, "action") &&
1510 (o_all
& DL_OPT_TRAP_ACTION
)) {
1511 const char *actionstr
;
1514 err
= dl_argv_str(dl
, &actionstr
);
1517 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1520 o_found
|= DL_OPT_TRAP_ACTION
;
1521 } else if (dl_argv_match(dl
, "netns") &&
1522 (o_all
& DL_OPT_NETNS
)) {
1523 const char *netns_str
;
1526 err
= dl_argv_str(dl
, &netns_str
);
1529 opts
->netns
= netns_get_fd(netns_str
);
1530 if ((int)opts
->netns
< 0) {
1532 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1535 opts
->netns_is_pid
= true;
1537 o_found
|= DL_OPT_NETNS
;
1538 } else if (dl_argv_match(dl
, "policer") &&
1539 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1541 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1544 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1545 } else if (dl_argv_match(dl
, "nopolicer") &&
1546 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1548 opts
->trap_policer_id
= 0;
1549 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1550 } else if (dl_argv_match(dl
, "rate") &&
1551 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1553 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1556 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1557 } else if (dl_argv_match(dl
, "burst") &&
1558 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1560 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1563 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1565 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1570 opts
->present
= o_found
;
1572 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1574 opts
->present
|= DL_OPT_SB
;
1577 return dl_args_finding_required_validate(o_required
, o_found
);
1580 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1582 struct dl_opts
*opts
= &dl
->opts
;
1584 if (opts
->present
& DL_OPT_HANDLE
) {
1585 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1586 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1587 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1588 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1589 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1590 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1592 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1593 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1594 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1595 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1598 if (opts
->present
& DL_OPT_PORT_TYPE
)
1599 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1601 if (opts
->present
& DL_OPT_PORT_COUNT
)
1602 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1604 if (opts
->present
& DL_OPT_SB
)
1605 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1607 if (opts
->present
& DL_OPT_SB_POOL
)
1608 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1609 opts
->sb_pool_index
);
1610 if (opts
->present
& DL_OPT_SB_SIZE
)
1611 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1612 opts
->sb_pool_size
);
1613 if (opts
->present
& DL_OPT_SB_TYPE
)
1614 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1615 opts
->sb_pool_type
);
1616 if (opts
->present
& DL_OPT_SB_THTYPE
)
1617 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1618 opts
->sb_pool_thtype
);
1619 if (opts
->present
& DL_OPT_SB_TH
)
1620 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1621 opts
->sb_threshold
);
1622 if (opts
->present
& DL_OPT_SB_TC
)
1623 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1625 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1626 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1627 opts
->eswitch_mode
);
1628 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1629 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1630 opts
->eswitch_inline_mode
);
1631 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1632 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1633 opts
->dpipe_table_name
);
1634 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1635 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1636 opts
->dpipe_counters_enabled
);
1637 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1638 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1639 opts
->eswitch_encap_mode
);
1640 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1641 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1643 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1644 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1645 opts
->resource_size
);
1646 if (opts
->present
& DL_OPT_PARAM_NAME
)
1647 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1649 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1650 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1652 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1653 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1654 opts
->region_snapshot_id
);
1655 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1656 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1657 opts
->region_address
);
1658 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1659 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1660 opts
->region_length
);
1661 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1662 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1663 opts
->flash_file_name
);
1664 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1665 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1666 opts
->flash_component
);
1667 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1668 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1669 opts
->reporter_name
);
1670 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1671 mnl_attr_put_u64(nlh
,
1672 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1673 opts
->reporter_graceful_period
);
1674 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1675 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1676 opts
->reporter_auto_recover
);
1677 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)
1678 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
1679 opts
->reporter_auto_dump
);
1680 if (opts
->present
& DL_OPT_TRAP_NAME
)
1681 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1683 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1684 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1685 opts
->trap_group_name
);
1686 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1687 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1689 if (opts
->present
& DL_OPT_NETNS
)
1690 mnl_attr_put_u32(nlh
,
1691 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1692 DEVLINK_ATTR_NETNS_FD
,
1694 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1695 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1696 opts
->trap_policer_id
);
1697 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1698 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1699 opts
->trap_policer_rate
);
1700 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1701 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1702 opts
->trap_policer_burst
);
1705 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1706 uint64_t o_required
, uint64_t o_optional
)
1710 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1713 dl_opts_put(nlh
, dl
);
1717 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1719 struct dl_opts
*opts
= &dl
->opts
;
1720 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1721 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1722 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1723 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1725 if (opts
->present
& DL_OPT_HANDLE
&&
1726 attr_bus_name
&& attr_dev_name
) {
1727 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1728 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1730 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1731 strcmp(dev_name
, opts
->dev_name
) != 0)
1734 if (opts
->present
& DL_OPT_HANDLEP
&&
1735 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1736 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1737 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1738 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1740 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1741 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1742 port_index
!= opts
->port_index
)
1745 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1746 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1748 if (sb_index
!= opts
->sb_index
)
1754 static void cmd_dev_help(void)
1756 pr_err("Usage: devlink dev show [ DEV ]\n");
1757 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1758 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1759 pr_err(" [ encap-mode { none | basic } ]\n");
1760 pr_err(" devlink dev eswitch show DEV\n");
1761 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1762 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1763 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1764 pr_err(" devlink dev info [ DEV ]\n");
1765 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1768 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1769 const char *dev_name
)
1771 if (!dl
->arr_last
.present
)
1773 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1774 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1777 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1778 const char *dev_name
)
1780 dl
->arr_last
.present
= true;
1781 free(dl
->arr_last
.dev_name
);
1782 free(dl
->arr_last
.bus_name
);
1783 dl
->arr_last
.bus_name
= strdup(bus_name
);
1784 dl
->arr_last
.dev_name
= strdup(dev_name
);
1787 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1788 const char *dev_name
)
1790 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1793 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1794 const char *dev_name
)
1796 return dl
->arr_last
.present
&&
1797 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1800 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1801 bool content
, bool array
)
1803 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1804 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1807 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1809 if (dl
->json_output
) {
1811 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1812 close_json_array(PRINT_JSON
, NULL
);
1813 if (should_arr_last_handle_start(dl
, bus_name
,
1815 open_json_array(PRINT_JSON
, buf
);
1816 open_json_object(NULL
);
1817 arr_last_handle_set(dl
, bus_name
, dev_name
);
1819 open_json_object(NULL
);
1822 open_json_object(buf
);
1826 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1827 __pr_out_indent_dec();
1828 if (should_arr_last_handle_start(dl
, bus_name
,
1830 pr_out("%s%s", buf
, content
? ":" : "");
1832 __pr_out_indent_inc();
1833 arr_last_handle_set(dl
, bus_name
, dev_name
);
1836 pr_out("%s%s", buf
, content
? ":" : "");
1841 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1843 __pr_out_handle_start(dl
, tb
, true, true);
1846 static void pr_out_handle_end(struct dl
*dl
)
1848 if (dl
->json_output
)
1849 close_json_object();
1854 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1856 __pr_out_handle_start(dl
, tb
, false, false);
1857 pr_out_handle_end(dl
);
1860 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1861 const char *dev_name
, uint32_t port_index
)
1863 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1864 dl
->arr_last
.port_index
== port_index
;
1867 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1868 const char *dev_name
, uint32_t port_index
)
1870 arr_last_handle_set(dl
, bus_name
, dev_name
);
1871 dl
->arr_last
.port_index
= port_index
;
1874 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1875 const char *bus_name
,
1876 const char *dev_name
,
1877 uint32_t port_index
)
1879 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1882 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1883 const char *bus_name
,
1884 const char *dev_name
,
1885 uint32_t port_index
)
1887 return dl
->arr_last
.present
&&
1888 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1891 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1892 const char *dev_name
,
1893 uint32_t port_index
, bool try_nice
,
1896 static char buf
[64];
1897 char *ifname
= NULL
;
1899 if (dl
->no_nice_names
|| !try_nice
||
1900 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1901 port_index
, &ifname
) != 0)
1902 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1904 sprintf(buf
, "%s", ifname
);
1906 if (dl
->json_output
) {
1908 if (should_arr_last_port_handle_end(dl
, bus_name
,
1911 close_json_array(PRINT_JSON
, NULL
);
1912 if (should_arr_last_port_handle_start(dl
, bus_name
,
1915 open_json_array(PRINT_JSON
, buf
);
1916 open_json_object(NULL
);
1917 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1920 open_json_object(NULL
);
1923 open_json_object(buf
);
1930 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1932 const char *bus_name
;
1933 const char *dev_name
;
1934 uint32_t port_index
;
1936 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1937 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1938 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1939 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1942 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1944 const char *bus_name
;
1945 const char *dev_name
;
1946 uint32_t port_index
;
1948 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1949 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1950 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1951 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1954 static void pr_out_port_handle_end(struct dl
*dl
)
1956 if (dl
->json_output
)
1957 close_json_object();
1962 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1964 __pr_out_indent_newline(dl
);
1965 if (val
== (uint64_t) -1)
1966 return print_string_name_value(name
, "unlimited");
1968 if (dl
->json_output
)
1969 print_u64(PRINT_JSON
, name
, NULL
, val
);
1971 pr_out("%s %"PRIu64
, name
, val
);
1974 static bool is_binary_eol(int i
)
1979 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1984 if (dl
->json_output
)
1985 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1987 pr_out("%02x ", data
[i
]);
1989 if (!dl
->json_output
&& is_binary_eol(i
))
1992 if (!dl
->json_output
&& !is_binary_eol(i
))
1996 static void pr_out_name(struct dl
*dl
, const char *name
)
1998 __pr_out_indent_newline(dl
);
1999 if (dl
->json_output
)
2000 print_string(PRINT_JSON
, name
, NULL
, NULL
);
2002 pr_out("%s:", name
);
2005 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
2007 if (dl
->json_output
) {
2008 print_uint(PRINT_JSON
, "address", NULL
, addr
);
2009 open_json_array(PRINT_JSON
, "data");
2013 static void pr_out_region_chunk_end(struct dl
*dl
)
2015 if (dl
->json_output
)
2016 close_json_array(PRINT_JSON
, NULL
);
2019 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2022 static uint64_t align_val
;
2025 pr_out_region_chunk_start(dl
, addr
);
2027 if (!dl
->json_output
)
2028 if (!(align_val
% 16))
2029 pr_out("%s%016"PRIx64
" ",
2030 align_val
? "\n" : "",
2035 if (dl
->json_output
)
2036 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2038 pr_out("%02x ", data
[i
]);
2043 pr_out_region_chunk_end(dl
);
2046 static void pr_out_section_start(struct dl
*dl
, const char *name
)
2048 if (dl
->json_output
) {
2049 open_json_object(NULL
);
2050 open_json_object(name
);
2054 static void pr_out_section_end(struct dl
*dl
)
2056 if (dl
->json_output
) {
2057 if (dl
->arr_last
.present
)
2058 close_json_array(PRINT_JSON
, NULL
);
2059 close_json_object();
2060 close_json_object();
2064 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2066 if (dl
->json_output
) {
2067 open_json_array(PRINT_JSON
, name
);
2069 __pr_out_indent_inc();
2071 pr_out("%s:", name
);
2072 __pr_out_indent_inc();
2077 static void pr_out_array_end(struct dl
*dl
)
2079 if (dl
->json_output
) {
2080 close_json_array(PRINT_JSON
, NULL
);
2082 __pr_out_indent_dec();
2083 __pr_out_indent_dec();
2087 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2089 if (dl
->json_output
) {
2090 open_json_object(name
);
2092 __pr_out_indent_inc();
2094 pr_out("%s:", name
);
2095 __pr_out_indent_inc();
2100 static void pr_out_object_end(struct dl
*dl
)
2102 if (dl
->json_output
) {
2103 close_json_object();
2105 __pr_out_indent_dec();
2106 __pr_out_indent_dec();
2110 static void pr_out_entry_start(struct dl
*dl
)
2112 if (dl
->json_output
)
2113 open_json_object(NULL
);
2116 static void pr_out_entry_end(struct dl
*dl
)
2118 if (dl
->json_output
)
2119 close_json_object();
2124 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2126 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2132 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2133 if (err
!= MNL_CB_OK
)
2136 pr_out_object_start(dl
, "stats");
2137 pr_out_object_start(dl
, "rx");
2138 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2139 pr_out_u64(dl
, "bytes",
2140 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2141 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2142 pr_out_u64(dl
, "packets",
2143 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2144 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2145 pr_out_u64(dl
, "dropped",
2146 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2147 pr_out_object_end(dl
);
2148 pr_out_object_end(dl
);
2151 static const char *param_cmode_name(uint8_t cmode
)
2154 case DEVLINK_PARAM_CMODE_RUNTIME
:
2155 return PARAM_CMODE_RUNTIME_STR
;
2156 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2157 return PARAM_CMODE_DRIVERINIT_STR
;
2158 case DEVLINK_PARAM_CMODE_PERMANENT
:
2159 return PARAM_CMODE_PERMANENT_STR
;
2160 default: return "<unknown type>";
2164 static const char *eswitch_mode_name(uint32_t mode
)
2167 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2168 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2169 default: return "<unknown mode>";
2173 static const char *eswitch_inline_mode_name(uint32_t mode
)
2176 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2177 return ESWITCH_INLINE_MODE_NONE
;
2178 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2179 return ESWITCH_INLINE_MODE_LINK
;
2180 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2181 return ESWITCH_INLINE_MODE_NETWORK
;
2182 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2183 return ESWITCH_INLINE_MODE_TRANSPORT
;
2185 return "<unknown mode>";
2189 static const char *eswitch_encap_mode_name(uint32_t mode
)
2192 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2193 return ESWITCH_ENCAP_MODE_NONE
;
2194 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2195 return ESWITCH_ENCAP_MODE_BASIC
;
2197 return "<unknown mode>";
2201 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2203 __pr_out_handle_start(dl
, tb
, true, false);
2205 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2206 check_indent_newline(dl
);
2207 print_string(PRINT_ANY
, "mode", "mode %s",
2208 eswitch_mode_name(mnl_attr_get_u16(
2209 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2211 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2212 check_indent_newline(dl
);
2213 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2214 eswitch_inline_mode_name(mnl_attr_get_u8(
2215 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2217 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2218 check_indent_newline(dl
);
2219 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2220 eswitch_encap_mode_name(mnl_attr_get_u8(
2221 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2224 pr_out_handle_end(dl
);
2227 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2229 struct dl
*dl
= data
;
2230 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2231 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2233 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2234 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2235 return MNL_CB_ERROR
;
2236 pr_out_eswitch(dl
, tb
);
2240 static int cmd_dev_eswitch_show(struct dl
*dl
)
2242 struct nlmsghdr
*nlh
;
2245 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2246 NLM_F_REQUEST
| NLM_F_ACK
);
2248 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2252 pr_out_section_start(dl
, "dev");
2253 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2254 pr_out_section_end(dl
);
2258 static int cmd_dev_eswitch_set(struct dl
*dl
)
2260 struct nlmsghdr
*nlh
;
2263 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2264 NLM_F_REQUEST
| NLM_F_ACK
);
2266 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2267 DL_OPT_ESWITCH_MODE
|
2268 DL_OPT_ESWITCH_INLINE_MODE
|
2269 DL_OPT_ESWITCH_ENCAP_MODE
);
2274 if (dl
->opts
.present
== 1) {
2275 pr_err("Need to set at least one option\n");
2279 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2282 static int cmd_dev_eswitch(struct dl
*dl
)
2284 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2287 } else if (dl_argv_match(dl
, "set")) {
2289 return cmd_dev_eswitch_set(dl
);
2290 } else if (dl_argv_match(dl
, "show")) {
2292 return cmd_dev_eswitch_show(dl
);
2294 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2298 struct param_val_conv
{
2304 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2305 uint32_t len
, const char *name
)
2309 for (i
= 0; i
< len
; i
++)
2310 if (!strcmp(param_val_conv
[i
].name
, name
))
2317 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2318 uint32_t len
, const char *name
, const char *vstr
,
2323 for (i
= 0; i
< len
; i
++)
2324 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2325 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2326 *vuint
= param_val_conv
[i
].vuint
;
2334 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2335 uint32_t len
, const char *name
, uint32_t vuint
,
2340 for (i
= 0; i
< len
; i
++)
2341 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2342 param_val_conv
[i
].vuint
== vuint
) {
2343 *vstr
= param_val_conv
[i
].vstr
;
2350 static const struct param_val_conv param_val_conv
[] = {
2352 .name
= "fw_load_policy",
2354 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2357 .name
= "fw_load_policy",
2359 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2362 .name
= "reset_dev_on_drv_probe",
2364 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2367 .name
= "fw_load_policy",
2369 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2372 .name
= "reset_dev_on_drv_probe",
2374 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2377 .name
= "reset_dev_on_drv_probe",
2379 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2382 .name
= "reset_dev_on_drv_probe",
2384 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2388 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2390 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2391 int nla_type
, struct nlattr
*nl
)
2393 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2394 struct nlattr
*val_attr
;
2399 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2400 if (err
!= MNL_CB_OK
)
2403 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2404 (nla_type
!= MNL_TYPE_FLAG
&&
2405 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2408 check_indent_newline(dl
);
2409 print_string(PRINT_ANY
, "cmode", "cmode %s",
2410 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2412 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2414 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2420 err
= param_val_conv_str_get(param_val_conv
,
2423 mnl_attr_get_u8(val_attr
),
2427 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2429 print_uint(PRINT_ANY
, "value", " value %u",
2430 mnl_attr_get_u8(val_attr
));
2435 err
= param_val_conv_str_get(param_val_conv
,
2438 mnl_attr_get_u16(val_attr
),
2442 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2444 print_uint(PRINT_ANY
, "value", " value %u",
2445 mnl_attr_get_u16(val_attr
));
2450 err
= param_val_conv_str_get(param_val_conv
,
2453 mnl_attr_get_u32(val_attr
),
2457 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2459 print_uint(PRINT_ANY
, "value", " value %u",
2460 mnl_attr_get_u32(val_attr
));
2463 case MNL_TYPE_STRING
:
2464 print_string(PRINT_ANY
, "value", " value %s",
2465 mnl_attr_get_str(val_attr
));
2468 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2473 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2475 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2476 struct nlattr
*param_value_attr
;
2477 const char *nla_name
;
2481 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2482 if (err
!= MNL_CB_OK
)
2484 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2485 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2486 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2490 pr_out_handle_start_arr(dl
, tb
);
2492 __pr_out_handle_start(dl
, tb
, true, false);
2494 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2496 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2497 check_indent_newline(dl
);
2498 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2499 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2500 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2502 print_string(PRINT_ANY
, "type", "type %s", "generic");
2504 pr_out_array_start(dl
, "values");
2505 mnl_attr_for_each_nested(param_value_attr
,
2506 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2507 pr_out_entry_start(dl
);
2508 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2509 pr_out_entry_end(dl
);
2511 pr_out_array_end(dl
);
2512 pr_out_handle_end(dl
);
2515 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2517 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2518 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2519 struct dl
*dl
= data
;
2521 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2522 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2523 !tb
[DEVLINK_ATTR_PARAM
])
2524 return MNL_CB_ERROR
;
2525 pr_out_param(dl
, tb
, true);
2541 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2543 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2544 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2545 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2546 struct nlattr
*param_value_attr
;
2547 enum devlink_param_cmode cmode
;
2548 struct param_ctx
*ctx
= data
;
2549 struct dl
*dl
= ctx
->dl
;
2553 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2554 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2555 !tb
[DEVLINK_ATTR_PARAM
])
2556 return MNL_CB_ERROR
;
2558 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2559 if (err
!= MNL_CB_OK
)
2560 return MNL_CB_ERROR
;
2562 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2563 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2564 return MNL_CB_ERROR
;
2566 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2567 mnl_attr_for_each_nested(param_value_attr
,
2568 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2569 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2570 struct nlattr
*val_attr
;
2572 err
= mnl_attr_parse_nested(param_value_attr
,
2573 attr_cb
, nla_value
);
2574 if (err
!= MNL_CB_OK
)
2575 return MNL_CB_ERROR
;
2577 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2578 (nla_type
!= MNL_TYPE_FLAG
&&
2579 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2580 return MNL_CB_ERROR
;
2582 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2583 if (cmode
== dl
->opts
.cmode
) {
2584 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2587 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2590 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2593 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2595 case MNL_TYPE_STRING
:
2596 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2599 ctx
->value
.vbool
= val_attr
? true : false;
2605 ctx
->nla_type
= nla_type
;
2609 static int cmd_dev_param_set(struct dl
*dl
)
2611 struct param_ctx ctx
= {};
2612 struct nlmsghdr
*nlh
;
2620 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2622 DL_OPT_PARAM_VALUE
|
2623 DL_OPT_PARAM_CMODE
, 0);
2627 /* Get value type */
2628 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2629 NLM_F_REQUEST
| NLM_F_ACK
);
2630 dl_opts_put(nlh
, dl
);
2633 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2637 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2638 NLM_F_REQUEST
| NLM_F_ACK
);
2639 dl_opts_put(nlh
, dl
);
2641 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2642 dl
->opts
.param_name
);
2644 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2645 switch (ctx
.nla_type
) {
2648 err
= param_val_conv_uint_get(param_val_conv
,
2650 dl
->opts
.param_name
,
2651 dl
->opts
.param_value
,
2655 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2658 goto err_param_value_parse
;
2659 if (val_u8
== ctx
.value
.vu8
)
2661 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2665 err
= param_val_conv_uint_get(param_val_conv
,
2667 dl
->opts
.param_name
,
2668 dl
->opts
.param_value
,
2672 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2675 goto err_param_value_parse
;
2676 if (val_u16
== ctx
.value
.vu16
)
2678 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2682 err
= param_val_conv_uint_get(param_val_conv
,
2684 dl
->opts
.param_name
,
2685 dl
->opts
.param_value
,
2688 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2690 goto err_param_value_parse
;
2691 if (val_u32
== ctx
.value
.vu32
)
2693 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2696 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2698 goto err_param_value_parse
;
2699 if (val_bool
== ctx
.value
.vbool
)
2702 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2705 case MNL_TYPE_STRING
:
2706 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2707 dl
->opts
.param_value
);
2708 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2712 printf("Value type not supported\n");
2715 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2717 err_param_value_parse
:
2718 pr_err("Value \"%s\" is not a number or not within range\n",
2719 dl
->opts
.param_value
);
2723 static int cmd_dev_param_show(struct dl
*dl
)
2725 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2726 struct nlmsghdr
*nlh
;
2729 if (dl_argc(dl
) == 0)
2730 flags
|= NLM_F_DUMP
;
2732 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2734 if (dl_argc(dl
) > 0) {
2735 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2736 DL_OPT_PARAM_NAME
, 0);
2741 pr_out_section_start(dl
, "param");
2742 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2743 pr_out_section_end(dl
);
2747 static int cmd_dev_param(struct dl
*dl
)
2749 if (dl_argv_match(dl
, "help")) {
2752 } else if (dl_argv_match(dl
, "show") ||
2753 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2755 return cmd_dev_param_show(dl
);
2756 } else if (dl_argv_match(dl
, "set")) {
2758 return cmd_dev_param_set(dl
);
2760 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2763 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2765 struct dl
*dl
= data
;
2766 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2767 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2768 uint8_t reload_failed
= 0;
2770 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2771 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2772 return MNL_CB_ERROR
;
2774 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2775 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2777 if (reload_failed
) {
2778 __pr_out_handle_start(dl
, tb
, true, false);
2779 check_indent_newline(dl
);
2780 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2781 pr_out_handle_end(dl
);
2783 pr_out_handle(dl
, tb
);
2789 static int cmd_dev_show(struct dl
*dl
)
2791 struct nlmsghdr
*nlh
;
2792 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2795 if (dl_argc(dl
) == 0)
2796 flags
|= NLM_F_DUMP
;
2798 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2800 if (dl_argc(dl
) > 0) {
2801 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2806 pr_out_section_start(dl
, "dev");
2807 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2808 pr_out_section_end(dl
);
2812 static int cmd_dev_reload(struct dl
*dl
)
2814 struct nlmsghdr
*nlh
;
2817 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2822 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2823 NLM_F_REQUEST
| NLM_F_ACK
);
2825 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2829 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2832 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2833 const char *name
, int type
)
2835 struct nlattr
*version
;
2837 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2838 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2839 const char *ver_value
;
2840 const char *ver_name
;
2843 if (mnl_attr_get_type(version
) != type
)
2846 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2847 if (err
!= MNL_CB_OK
)
2850 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2851 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2855 pr_out_object_start(dl
, name
);
2859 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2860 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2862 check_indent_newline(dl
);
2863 print_string_name_value(ver_name
, ver_value
);
2864 if (!dl
->json_output
)
2869 pr_out_object_end(dl
);
2872 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2873 struct nlattr
**tb
, bool has_versions
)
2875 __pr_out_handle_start(dl
, tb
, true, false);
2877 __pr_out_indent_inc();
2878 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2879 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2881 if (!dl
->json_output
)
2883 check_indent_newline(dl
);
2884 print_string(PRINT_ANY
, "driver", "driver %s",
2885 mnl_attr_get_str(nla_drv
));
2888 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2889 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2891 if (!dl
->json_output
)
2893 check_indent_newline(dl
);
2894 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2895 mnl_attr_get_str(nla_sn
));
2897 __pr_out_indent_dec();
2900 pr_out_object_start(dl
, "versions");
2902 pr_out_versions_single(dl
, nlh
, "fixed",
2903 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2904 pr_out_versions_single(dl
, nlh
, "running",
2905 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2906 pr_out_versions_single(dl
, nlh
, "stored",
2907 DEVLINK_ATTR_INFO_VERSION_STORED
);
2909 pr_out_object_end(dl
);
2912 pr_out_handle_end(dl
);
2915 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2917 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2918 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2919 bool has_versions
, has_info
;
2920 struct dl
*dl
= data
;
2922 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2924 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2925 return MNL_CB_ERROR
;
2927 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2928 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2929 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2930 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2931 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2935 pr_out_info(dl
, nlh
, tb
, has_versions
);
2940 static int cmd_dev_info(struct dl
*dl
)
2942 struct nlmsghdr
*nlh
;
2943 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2946 if (dl_argv_match(dl
, "help")) {
2951 if (dl_argc(dl
) == 0)
2952 flags
|= NLM_F_DUMP
;
2954 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2956 if (dl_argc(dl
) > 0) {
2957 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2962 pr_out_section_start(dl
, "info");
2963 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2964 pr_out_section_end(dl
);
2968 struct cmd_dev_flash_status_ctx
{
2971 char *last_component
;
2972 uint8_t not_first
:1,
2978 static int nullstrcmp(const char *str1
, const char *str2
)
2981 return strcmp(str1
, str2
);
2984 return str1
? 1 : -1;
2987 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2989 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2990 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2991 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2992 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2993 const char *component
= NULL
;
2994 uint64_t done
= 0, total
= 0;
2995 const char *msg
= NULL
;
2996 const char *bus_name
;
2997 const char *dev_name
;
2999 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
3000 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3003 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3004 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3005 return MNL_CB_ERROR
;
3006 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3007 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3008 if (strcmp(bus_name
, opts
->bus_name
) ||
3009 strcmp(dev_name
, opts
->dev_name
))
3010 return MNL_CB_ERROR
;
3012 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3014 free(ctx
->last_msg
);
3015 free(ctx
->last_component
);
3016 ctx
->received_end
= 1;
3020 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3021 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3022 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3023 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3024 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3025 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3026 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3027 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3029 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3030 !nullstrcmp(component
, ctx
->last_component
) &&
3031 ctx
->last_pc
&& ctx
->not_first
) {
3032 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3037 pr_out("[%s] ", component
);
3038 free(ctx
->last_component
);
3039 ctx
->last_component
= strdup(component
);
3043 free(ctx
->last_msg
);
3044 ctx
->last_msg
= strdup(msg
);
3048 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3059 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3060 struct mnlg_socket
*nlg_ntf
,
3063 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3070 for (i
= 0; i
< 3; i
++)
3072 FD_SET(pipe_r
, &fds
[0]);
3074 FD_SET(nlfd
, &fds
[0]);
3078 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3081 pr_err("select() failed\n");
3084 if (FD_ISSET(nlfd
, &fds
[0])) {
3085 err
= _mnlg_socket_recv_run(nlg_ntf
,
3086 cmd_dev_flash_status_cb
, ctx
);
3090 if (FD_ISSET(pipe_r
, &fds
[0])) {
3091 err
= read(pipe_r
, &err2
, sizeof(err2
));
3093 pr_err("Failed to read pipe\n");
3098 ctx
->flash_done
= 1;
3104 static int cmd_dev_flash(struct dl
*dl
)
3106 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3107 struct mnlg_socket
*nlg_ntf
;
3108 struct nlmsghdr
*nlh
;
3114 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3119 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3120 NLM_F_REQUEST
| NLM_F_ACK
);
3122 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3123 DL_OPT_FLASH_COMPONENT
);
3127 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3131 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3135 err
= pipe(pipe_fds
);
3138 pipe_r
= pipe_fds
[0];
3139 pipe_w
= pipe_fds
[1];
3147 /* In child, just execute the flash and pass returned
3148 * value through pipe once it is done.
3153 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3154 cc
= write(pipe_w
, &err
, sizeof(err
));
3156 exit(cc
!= sizeof(err
));
3161 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3164 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3166 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3169 mnlg_socket_close(nlg_ntf
);
3173 static int cmd_dev(struct dl
*dl
)
3175 if (dl_argv_match(dl
, "help")) {
3178 } else if (dl_argv_match(dl
, "show") ||
3179 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3181 return cmd_dev_show(dl
);
3182 } else if (dl_argv_match(dl
, "eswitch")) {
3184 return cmd_dev_eswitch(dl
);
3185 } else if (dl_argv_match(dl
, "reload")) {
3187 return cmd_dev_reload(dl
);
3188 } else if (dl_argv_match(dl
, "param")) {
3190 return cmd_dev_param(dl
);
3191 } else if (dl_argv_match(dl
, "info")) {
3193 return cmd_dev_info(dl
);
3194 } else if (dl_argv_match(dl
, "flash")) {
3196 return cmd_dev_flash(dl
);
3198 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3202 static void cmd_port_help(void)
3204 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3205 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3206 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3207 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3210 static const char *port_type_name(uint32_t type
)
3213 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3214 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3215 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3216 case DEVLINK_PORT_TYPE_IB
: return "ib";
3217 default: return "<unknown type>";
3221 static const char *port_flavour_name(uint16_t flavour
)
3224 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3226 case DEVLINK_PORT_FLAVOUR_CPU
:
3228 case DEVLINK_PORT_FLAVOUR_DSA
:
3230 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3232 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3234 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3237 return "<unknown flavour>";
3241 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3245 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3246 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3247 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3249 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3250 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3251 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3255 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3257 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3258 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3260 pr_out_port_handle_start(dl
, tb
, false);
3261 check_indent_newline(dl
);
3263 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3265 print_string(PRINT_ANY
, "type", "type %s",
3266 port_type_name(port_type
));
3268 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3270 if (port_type
!= des_port_type
)
3271 print_string(PRINT_ANY
, "des_type", " des_type %s",
3272 port_type_name(des_port_type
));
3275 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3276 print_string(PRINT_ANY
, "netdev", " netdev %s",
3277 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3279 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3280 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3281 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3283 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3284 uint16_t port_flavour
=
3285 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3287 print_string(PRINT_ANY
, "flavour", " flavour %s",
3288 port_flavour_name(port_flavour
));
3290 switch (port_flavour
) {
3291 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3292 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3293 pr_out_port_pfvf_num(dl
, tb
);
3299 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3300 uint32_t port_number
;
3302 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3303 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3305 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3306 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3307 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3308 pr_out_port_handle_end(dl
);
3311 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3313 struct dl
*dl
= data
;
3314 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3315 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3317 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3318 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3319 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3320 return MNL_CB_ERROR
;
3321 pr_out_port(dl
, tb
);
3325 static int cmd_port_show(struct dl
*dl
)
3327 struct nlmsghdr
*nlh
;
3328 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3331 if (dl_argc(dl
) == 0)
3332 flags
|= NLM_F_DUMP
;
3334 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3336 if (dl_argc(dl
) > 0) {
3337 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3342 pr_out_section_start(dl
, "port");
3343 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3344 pr_out_section_end(dl
);
3348 static int cmd_port_set(struct dl
*dl
)
3350 struct nlmsghdr
*nlh
;
3353 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3354 NLM_F_REQUEST
| NLM_F_ACK
);
3356 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3360 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3363 static int cmd_port_split(struct dl
*dl
)
3365 struct nlmsghdr
*nlh
;
3368 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3369 NLM_F_REQUEST
| NLM_F_ACK
);
3371 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3375 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3378 static int cmd_port_unsplit(struct dl
*dl
)
3380 struct nlmsghdr
*nlh
;
3383 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3384 NLM_F_REQUEST
| NLM_F_ACK
);
3386 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3390 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3393 static int cmd_port(struct dl
*dl
)
3395 if (dl_argv_match(dl
, "help")) {
3398 } else if (dl_argv_match(dl
, "show") ||
3399 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3401 return cmd_port_show(dl
);
3402 } else if (dl_argv_match(dl
, "set")) {
3404 return cmd_port_set(dl
);
3405 } else if (dl_argv_match(dl
, "split")) {
3407 return cmd_port_split(dl
);
3408 } else if (dl_argv_match(dl
, "unsplit")) {
3410 return cmd_port_unsplit(dl
);
3412 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3416 static void cmd_sb_help(void)
3418 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3419 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3420 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3421 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3422 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3423 pr_err(" pool POOL_INDEX ]\n");
3424 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3425 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3426 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3427 pr_err(" type { ingress | egress } ]\n");
3428 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3429 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3430 pr_err(" th THRESHOLD\n");
3431 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3432 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3433 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3436 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3438 pr_out_handle_start_arr(dl
, tb
);
3439 check_indent_newline(dl
);
3440 print_uint(PRINT_ANY
, "sb", "sb %u",
3441 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3442 print_uint(PRINT_ANY
, "size", " size %u",
3443 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3444 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3445 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3446 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3447 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3448 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3449 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3450 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3451 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3452 pr_out_handle_end(dl
);
3455 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3457 struct dl
*dl
= data
;
3458 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3459 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3461 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3462 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3463 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3464 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3465 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3466 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3467 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3468 return MNL_CB_ERROR
;
3473 static int cmd_sb_show(struct dl
*dl
)
3475 struct nlmsghdr
*nlh
;
3476 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3479 if (dl_argc(dl
) == 0)
3480 flags
|= NLM_F_DUMP
;
3482 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3484 if (dl_argc(dl
) > 0) {
3485 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3490 pr_out_section_start(dl
, "sb");
3491 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3492 pr_out_section_end(dl
);
3496 static const char *pool_type_name(uint8_t type
)
3499 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3500 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3501 default: return "<unknown type>";
3505 static const char *threshold_type_name(uint8_t type
)
3508 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3509 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3510 default: return "<unknown type>";
3514 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3516 pr_out_handle_start_arr(dl
, tb
);
3517 check_indent_newline(dl
);
3518 print_uint(PRINT_ANY
, "sb", "sb %u",
3519 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3520 print_uint(PRINT_ANY
, "pool", " pool %u",
3521 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3522 print_string(PRINT_ANY
, "type", " type %s",
3523 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3524 print_uint(PRINT_ANY
, "size", " size %u",
3525 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3526 print_string(PRINT_ANY
, "thtype", " thtype %s",
3527 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3528 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3529 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3530 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3531 pr_out_handle_end(dl
);
3534 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3536 struct dl
*dl
= data
;
3537 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3538 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3540 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3541 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3542 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3543 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3544 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3545 return MNL_CB_ERROR
;
3546 pr_out_sb_pool(dl
, tb
);
3550 static int cmd_sb_pool_show(struct dl
*dl
)
3552 struct nlmsghdr
*nlh
;
3553 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3556 if (dl_argc(dl
) == 0)
3557 flags
|= NLM_F_DUMP
;
3559 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3561 if (dl_argc(dl
) > 0) {
3562 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3568 pr_out_section_start(dl
, "pool");
3569 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3570 pr_out_section_end(dl
);
3574 static int cmd_sb_pool_set(struct dl
*dl
)
3576 struct nlmsghdr
*nlh
;
3579 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3580 NLM_F_REQUEST
| NLM_F_ACK
);
3582 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3583 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3587 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3590 static int cmd_sb_pool(struct dl
*dl
)
3592 if (dl_argv_match(dl
, "help")) {
3595 } else if (dl_argv_match(dl
, "show") ||
3596 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3598 return cmd_sb_pool_show(dl
);
3599 } else if (dl_argv_match(dl
, "set")) {
3601 return cmd_sb_pool_set(dl
);
3603 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3607 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3609 pr_out_port_handle_start_arr(dl
, tb
, true);
3610 check_indent_newline(dl
);
3611 print_uint(PRINT_ANY
, "sb", "sb %u",
3612 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3613 print_uint(PRINT_ANY
, "pool", " pool %u",
3614 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3615 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3616 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3617 pr_out_port_handle_end(dl
);
3620 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3622 struct dl
*dl
= data
;
3623 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3624 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3626 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3627 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3628 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3629 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3630 return MNL_CB_ERROR
;
3631 pr_out_sb_port_pool(dl
, tb
);
3635 static int cmd_sb_port_pool_show(struct dl
*dl
)
3637 struct nlmsghdr
*nlh
;
3638 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3641 if (dl_argc(dl
) == 0)
3642 flags
|= NLM_F_DUMP
;
3644 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3646 if (dl_argc(dl
) > 0) {
3647 err
= dl_argv_parse_put(nlh
, dl
,
3648 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3654 pr_out_section_start(dl
, "port_pool");
3655 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3656 pr_out_section_end(dl
);
3660 static int cmd_sb_port_pool_set(struct dl
*dl
)
3662 struct nlmsghdr
*nlh
;
3665 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3666 NLM_F_REQUEST
| NLM_F_ACK
);
3668 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3669 DL_OPT_SB_TH
, DL_OPT_SB
);
3673 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3676 static int cmd_sb_port_pool(struct dl
*dl
)
3678 if (dl_argv_match(dl
, "help")) {
3681 } else if (dl_argv_match(dl
, "show") ||
3682 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3684 return cmd_sb_port_pool_show(dl
);
3685 } else if (dl_argv_match(dl
, "set")) {
3687 return cmd_sb_port_pool_set(dl
);
3689 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3693 static int cmd_sb_port(struct dl
*dl
)
3695 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3698 } else if (dl_argv_match(dl
, "pool")) {
3700 return cmd_sb_port_pool(dl
);
3702 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3706 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3708 pr_out_port_handle_start_arr(dl
, tb
, true);
3709 check_indent_newline(dl
);
3710 print_uint(PRINT_ANY
, "sb", "sb %u",
3711 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3712 print_uint(PRINT_ANY
, "tc", " tc %u",
3713 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3714 print_string(PRINT_ANY
, "type", " type %s",
3715 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3716 print_uint(PRINT_ANY
, "pool", " pool %u",
3717 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3718 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3719 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3720 pr_out_port_handle_end(dl
);
3723 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3725 struct dl
*dl
= data
;
3726 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3727 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3729 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3730 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3731 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3732 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3733 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3734 return MNL_CB_ERROR
;
3735 pr_out_sb_tc_bind(dl
, tb
);
3739 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3741 struct nlmsghdr
*nlh
;
3742 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3745 if (dl_argc(dl
) == 0)
3746 flags
|= NLM_F_DUMP
;
3748 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3750 if (dl_argc(dl
) > 0) {
3751 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3752 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3757 pr_out_section_start(dl
, "tc_bind");
3758 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3759 pr_out_section_end(dl
);
3763 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3765 struct nlmsghdr
*nlh
;
3768 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3769 NLM_F_REQUEST
| NLM_F_ACK
);
3771 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3772 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3777 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3780 static int cmd_sb_tc_bind(struct dl
*dl
)
3782 if (dl_argv_match(dl
, "help")) {
3785 } else if (dl_argv_match(dl
, "show") ||
3786 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3788 return cmd_sb_tc_bind_show(dl
);
3789 } else if (dl_argv_match(dl
, "set")) {
3791 return cmd_sb_tc_bind_set(dl
);
3793 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3797 static int cmd_sb_tc(struct dl
*dl
)
3799 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3802 } else if (dl_argv_match(dl
, "bind")) {
3804 return cmd_sb_tc_bind(dl
);
3806 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3811 struct list_head list
;
3815 uint32_t bound_pool_index
;
3819 struct list_head list
;
3822 uint32_t port_index
;
3824 struct list_head pool_list
;
3825 struct list_head ing_tc_list
;
3826 struct list_head eg_tc_list
;
3832 struct list_head port_list
;
3835 static struct occ_item
*occ_item_alloc(void)
3837 return calloc(1, sizeof(struct occ_item
));
3840 static void occ_item_free(struct occ_item
*occ_item
)
3845 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3847 struct occ_port
*occ_port
;
3849 occ_port
= calloc(1, sizeof(*occ_port
));
3852 occ_port
->port_index
= port_index
;
3853 INIT_LIST_HEAD(&occ_port
->pool_list
);
3854 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3855 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3859 static void occ_port_free(struct occ_port
*occ_port
)
3861 struct occ_item
*occ_item
, *tmp
;
3863 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3864 occ_item_free(occ_item
);
3865 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3866 occ_item_free(occ_item
);
3867 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3868 occ_item_free(occ_item
);
3871 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3873 struct occ_show
*occ_show
;
3875 occ_show
= calloc(1, sizeof(*occ_show
));
3879 INIT_LIST_HEAD(&occ_show
->port_list
);
3883 static void occ_show_free(struct occ_show
*occ_show
)
3885 struct occ_port
*occ_port
, *tmp
;
3887 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3888 occ_port_free(occ_port
);
3891 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3894 struct occ_port
*occ_port
;
3895 uint32_t port_index
;
3897 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3899 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3900 if (occ_port
->port_index
== port_index
)
3903 occ_port
= occ_port_alloc(port_index
);
3906 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3910 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3913 struct occ_item
*occ_item
;
3916 pr_out_sp(7, " %s:", label
);
3917 list_for_each_entry(occ_item
, list
, list
) {
3918 if ((i
- 1) % 4 == 0 && i
!= 1)
3921 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3922 occ_item
->bound_pool_index
);
3924 pr_out_sp(7, "%2u:", occ_item
->index
);
3925 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3929 if ((i
- 1) % 4 != 0)
3933 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3934 struct list_head
*list
,
3937 struct occ_item
*occ_item
;
3940 open_json_object(label
);
3941 list_for_each_entry(occ_item
, list
, list
) {
3942 sprintf(buf
, "%u", occ_item
->index
);
3943 open_json_object(buf
);
3945 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3946 occ_item
->bound_pool_index
);
3947 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3948 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3949 close_json_object();
3951 close_json_object();
3954 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3956 if (dl
->json_output
) {
3957 pr_out_json_occ_show_item_list(dl
, "pool",
3958 &occ_port
->pool_list
, false);
3959 pr_out_json_occ_show_item_list(dl
, "itc",
3960 &occ_port
->ing_tc_list
, true);
3961 pr_out_json_occ_show_item_list(dl
, "etc",
3962 &occ_port
->eg_tc_list
, true);
3965 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3966 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3967 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3971 static void pr_out_occ_show(struct occ_show
*occ_show
)
3973 struct dl
*dl
= occ_show
->dl
;
3974 struct dl_opts
*opts
= &dl
->opts
;
3975 struct occ_port
*occ_port
;
3977 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3978 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3979 occ_port
->port_index
, true, false);
3980 pr_out_occ_show_port(dl
, occ_port
);
3981 pr_out_port_handle_end(dl
);
3985 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3988 struct occ_port
*occ_port
;
3989 struct occ_item
*occ_item
;
3991 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3994 occ_port
= occ_port_get(occ_show
, tb
);
3996 occ_show
->err
= -ENOMEM
;
4000 occ_item
= occ_item_alloc();
4002 occ_show
->err
= -ENOMEM
;
4005 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4006 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4007 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4008 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4011 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4013 struct occ_show
*occ_show
= data
;
4014 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4015 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4017 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4018 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4019 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4020 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4021 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4022 return MNL_CB_ERROR
;
4023 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4027 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4030 struct occ_port
*occ_port
;
4031 struct occ_item
*occ_item
;
4034 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4037 occ_port
= occ_port_get(occ_show
, tb
);
4039 occ_show
->err
= -ENOMEM
;
4043 occ_item
= occ_item_alloc();
4045 occ_show
->err
= -ENOMEM
;
4048 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4049 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4050 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4051 occ_item
->bound_pool_index
=
4052 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4053 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4054 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4055 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4056 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4057 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4059 occ_item_free(occ_item
);
4062 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4064 struct occ_show
*occ_show
= data
;
4065 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4066 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4068 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4069 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4070 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4071 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4072 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4073 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4074 return MNL_CB_ERROR
;
4075 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4079 static int cmd_sb_occ_show(struct dl
*dl
)
4081 struct nlmsghdr
*nlh
;
4082 struct occ_show
*occ_show
;
4083 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4086 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4090 occ_show
= occ_show_alloc(dl
);
4094 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4096 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4097 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4101 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4103 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4104 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4108 pr_out_section_start(dl
, "occupancy");
4109 pr_out_occ_show(occ_show
);
4110 pr_out_section_end(dl
);
4113 occ_show_free(occ_show
);
4117 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4119 struct nlmsghdr
*nlh
;
4122 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4123 NLM_F_REQUEST
| NLM_F_ACK
);
4125 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4129 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4132 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4134 struct nlmsghdr
*nlh
;
4137 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4138 NLM_F_REQUEST
| NLM_F_ACK
);
4140 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4144 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4147 static int cmd_sb_occ(struct dl
*dl
)
4149 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4152 } else if (dl_argv_match(dl
, "show") ||
4153 dl_argv_match(dl
, "list")) {
4155 return cmd_sb_occ_show(dl
);
4156 } else if (dl_argv_match(dl
, "snapshot")) {
4158 return cmd_sb_occ_snapshot(dl
);
4159 } else if (dl_argv_match(dl
, "clearmax")) {
4161 return cmd_sb_occ_clearmax(dl
);
4163 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4167 static int cmd_sb(struct dl
*dl
)
4169 if (dl_argv_match(dl
, "help")) {
4172 } else if (dl_argv_match(dl
, "show") ||
4173 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4175 return cmd_sb_show(dl
);
4176 } else if (dl_argv_match(dl
, "pool")) {
4178 return cmd_sb_pool(dl
);
4179 } else if (dl_argv_match(dl
, "port")) {
4181 return cmd_sb_port(dl
);
4182 } else if (dl_argv_match(dl
, "tc")) {
4184 return cmd_sb_tc(dl
);
4185 } else if (dl_argv_match(dl
, "occupancy")) {
4187 return cmd_sb_occ(dl
);
4189 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4193 static const char *cmd_name(uint8_t cmd
)
4196 case DEVLINK_CMD_UNSPEC
: return "unspec";
4197 case DEVLINK_CMD_GET
: return "get";
4198 case DEVLINK_CMD_SET
: return "set";
4199 case DEVLINK_CMD_NEW
: return "new";
4200 case DEVLINK_CMD_DEL
: return "del";
4201 case DEVLINK_CMD_PORT_GET
: return "get";
4202 case DEVLINK_CMD_PORT_SET
: return "set";
4203 case DEVLINK_CMD_PORT_NEW
: return "new";
4204 case DEVLINK_CMD_PORT_DEL
: return "del";
4205 case DEVLINK_CMD_PARAM_GET
: return "get";
4206 case DEVLINK_CMD_PARAM_SET
: return "set";
4207 case DEVLINK_CMD_PARAM_NEW
: return "new";
4208 case DEVLINK_CMD_PARAM_DEL
: return "del";
4209 case DEVLINK_CMD_REGION_GET
: return "get";
4210 case DEVLINK_CMD_REGION_SET
: return "set";
4211 case DEVLINK_CMD_REGION_NEW
: return "new";
4212 case DEVLINK_CMD_REGION_DEL
: return "del";
4213 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4214 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4215 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4216 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4217 case DEVLINK_CMD_TRAP_GET
: return "get";
4218 case DEVLINK_CMD_TRAP_SET
: return "set";
4219 case DEVLINK_CMD_TRAP_NEW
: return "new";
4220 case DEVLINK_CMD_TRAP_DEL
: return "del";
4221 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4222 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4223 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4224 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4225 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4226 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4227 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4228 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4229 default: return "<unknown cmd>";
4233 static const char *cmd_obj(uint8_t cmd
)
4236 case DEVLINK_CMD_UNSPEC
: return "unspec";
4237 case DEVLINK_CMD_GET
:
4238 case DEVLINK_CMD_SET
:
4239 case DEVLINK_CMD_NEW
:
4240 case DEVLINK_CMD_DEL
:
4242 case DEVLINK_CMD_PORT_GET
:
4243 case DEVLINK_CMD_PORT_SET
:
4244 case DEVLINK_CMD_PORT_NEW
:
4245 case DEVLINK_CMD_PORT_DEL
:
4247 case DEVLINK_CMD_PARAM_GET
:
4248 case DEVLINK_CMD_PARAM_SET
:
4249 case DEVLINK_CMD_PARAM_NEW
:
4250 case DEVLINK_CMD_PARAM_DEL
:
4252 case DEVLINK_CMD_REGION_GET
:
4253 case DEVLINK_CMD_REGION_SET
:
4254 case DEVLINK_CMD_REGION_NEW
:
4255 case DEVLINK_CMD_REGION_DEL
:
4257 case DEVLINK_CMD_FLASH_UPDATE
:
4258 case DEVLINK_CMD_FLASH_UPDATE_END
:
4259 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4261 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4263 case DEVLINK_CMD_TRAP_GET
:
4264 case DEVLINK_CMD_TRAP_SET
:
4265 case DEVLINK_CMD_TRAP_NEW
:
4266 case DEVLINK_CMD_TRAP_DEL
:
4268 case DEVLINK_CMD_TRAP_GROUP_GET
:
4269 case DEVLINK_CMD_TRAP_GROUP_SET
:
4270 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4271 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4272 return "trap-group";
4273 case DEVLINK_CMD_TRAP_POLICER_GET
:
4274 case DEVLINK_CMD_TRAP_POLICER_SET
:
4275 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4276 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4277 return "trap-policer";
4278 default: return "<unknown obj>";
4282 static void pr_out_mon_header(uint8_t cmd
)
4284 if (!is_json_context()) {
4285 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4287 open_json_object(NULL
);
4288 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4289 open_json_object(cmd_obj(cmd
));
4293 static void pr_out_mon_footer(void)
4295 if (is_json_context()) {
4296 close_json_object();
4297 close_json_object();
4301 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4303 const char *obj
= cmd_obj(cmd
);
4304 unsigned int index
= 0;
4305 const char *cur_obj
;
4309 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4310 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4316 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4318 __pr_out_handle_start(dl
, tb
, true, false);
4320 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4321 check_indent_newline(dl
);
4322 print_string(PRINT_ANY
, "msg", "msg %s",
4323 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4325 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4326 check_indent_newline(dl
);
4327 print_string(PRINT_ANY
, "component", "component %s",
4328 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4331 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4332 pr_out_u64(dl
, "done",
4333 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4335 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4336 pr_out_u64(dl
, "total",
4337 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4339 pr_out_handle_end(dl
);
4342 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4343 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4344 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4345 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4346 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4348 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4350 struct dl
*dl
= data
;
4351 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4352 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4353 uint8_t cmd
= genl
->cmd
;
4355 if (!cmd_filter_check(dl
, cmd
))
4359 case DEVLINK_CMD_GET
: /* fall through */
4360 case DEVLINK_CMD_SET
: /* fall through */
4361 case DEVLINK_CMD_NEW
: /* fall through */
4362 case DEVLINK_CMD_DEL
:
4363 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4364 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4365 return MNL_CB_ERROR
;
4366 pr_out_mon_header(genl
->cmd
);
4367 pr_out_handle(dl
, tb
);
4368 pr_out_mon_footer();
4370 case DEVLINK_CMD_PORT_GET
: /* fall through */
4371 case DEVLINK_CMD_PORT_SET
: /* fall through */
4372 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4373 case DEVLINK_CMD_PORT_DEL
:
4374 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4375 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4376 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4377 return MNL_CB_ERROR
;
4378 pr_out_mon_header(genl
->cmd
);
4379 pr_out_port(dl
, tb
);
4380 pr_out_mon_footer();
4382 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4383 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4384 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4385 case DEVLINK_CMD_PARAM_DEL
:
4386 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4387 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4388 !tb
[DEVLINK_ATTR_PARAM
])
4389 return MNL_CB_ERROR
;
4390 pr_out_mon_header(genl
->cmd
);
4391 pr_out_param(dl
, tb
, false);
4392 pr_out_mon_footer();
4394 case DEVLINK_CMD_REGION_GET
: /* fall through */
4395 case DEVLINK_CMD_REGION_SET
: /* fall through */
4396 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4397 case DEVLINK_CMD_REGION_DEL
:
4398 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4399 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4400 !tb
[DEVLINK_ATTR_REGION_NAME
])
4401 return MNL_CB_ERROR
;
4402 pr_out_mon_header(genl
->cmd
);
4403 pr_out_region(dl
, tb
);
4404 pr_out_mon_footer();
4406 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4407 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4408 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4409 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4410 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4411 return MNL_CB_ERROR
;
4412 pr_out_mon_header(genl
->cmd
);
4413 pr_out_flash_update(dl
, tb
);
4414 pr_out_mon_footer();
4416 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4417 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4418 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4419 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
4420 return MNL_CB_ERROR
;
4421 pr_out_mon_header(genl
->cmd
);
4422 pr_out_health(dl
, tb
);
4423 pr_out_mon_footer();
4425 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4426 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4427 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4428 case DEVLINK_CMD_TRAP_DEL
:
4429 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4430 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4431 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4432 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4433 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4434 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4435 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4436 !tb
[DEVLINK_ATTR_STATS
])
4437 return MNL_CB_ERROR
;
4438 pr_out_mon_header(genl
->cmd
);
4439 pr_out_trap(dl
, tb
, false);
4440 pr_out_mon_footer();
4442 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4443 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4444 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4445 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4446 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4447 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4448 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4449 !tb
[DEVLINK_ATTR_STATS
])
4450 return MNL_CB_ERROR
;
4451 pr_out_mon_header(genl
->cmd
);
4452 pr_out_trap_group(dl
, tb
, false);
4453 pr_out_mon_footer();
4455 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4456 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4457 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4458 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4459 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4460 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4461 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4462 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4463 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4464 return MNL_CB_ERROR
;
4465 pr_out_mon_header(genl
->cmd
);
4466 pr_out_trap_policer(dl
, tb
, false);
4472 static int cmd_mon_show(struct dl
*dl
)
4475 unsigned int index
= 0;
4476 const char *cur_obj
;
4478 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4479 if (strcmp(cur_obj
, "all") != 0 &&
4480 strcmp(cur_obj
, "dev") != 0 &&
4481 strcmp(cur_obj
, "port") != 0 &&
4482 strcmp(cur_obj
, "health") != 0 &&
4483 strcmp(cur_obj
, "trap") != 0 &&
4484 strcmp(cur_obj
, "trap-group") != 0 &&
4485 strcmp(cur_obj
, "trap-policer") != 0) {
4486 pr_err("Unknown object \"%s\"\n", cur_obj
);
4490 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4493 open_json_object(NULL
);
4494 open_json_array(PRINT_JSON
, "mon");
4495 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
4496 close_json_array(PRINT_JSON
, NULL
);
4497 close_json_object();
4503 static void cmd_mon_help(void)
4505 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4506 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4509 static int cmd_mon(struct dl
*dl
)
4511 if (dl_argv_match(dl
, "help")) {
4515 return cmd_mon_show(dl
);
4518 struct dpipe_field
{
4521 unsigned int bitwidth
;
4522 enum devlink_dpipe_field_mapping_type mapping_type
;
4525 struct dpipe_header
{
4526 struct list_head list
;
4529 struct dpipe_field
*fields
;
4530 unsigned int fields_count
;
4533 struct dpipe_table
{
4534 struct list_head list
;
4536 unsigned int resource_id
;
4537 bool resource_valid
;
4540 struct dpipe_tables
{
4541 struct list_head table_list
;
4551 enum devlink_resource_unit unit
;
4556 struct list_head list
;
4557 struct list_head resource_list
;
4558 struct resource
*parent
;
4562 struct list_head resource_list
;
4565 struct resource_ctx
{
4568 struct resources
*resources
;
4569 struct dpipe_tables
*tables
;
4570 bool print_resources
;
4571 bool pending_change
;
4574 static struct resource
*resource_alloc(void)
4576 struct resource
*resource
;
4578 resource
= calloc(1, sizeof(struct resource
));
4581 INIT_LIST_HEAD(&resource
->resource_list
);
4585 static void resource_free(struct resource
*resource
)
4587 struct resource
*child_resource
, *tmp
;
4589 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4591 free(child_resource
->name
);
4592 resource_free(child_resource
);
4597 static struct resources
*resources_alloc(void)
4599 struct resources
*resources
;
4601 resources
= calloc(1, sizeof(struct resources
));
4604 INIT_LIST_HEAD(&resources
->resource_list
);
4608 static void resources_free(struct resources
*resources
)
4610 struct resource
*resource
, *tmp
;
4612 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4613 resource_free(resource
);
4616 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4618 ctx
->resources
= resources_alloc();
4619 if (!ctx
->resources
)
4625 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4627 resources_free(ctx
->resources
);
4633 struct list_head global_headers
;
4634 struct list_head local_headers
;
4635 struct dpipe_tables
*tables
;
4636 struct resources
*resources
;
4641 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4643 struct dpipe_header
*header
;
4645 header
= calloc(1, sizeof(struct dpipe_header
));
4648 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4649 if (!header
->fields
)
4650 goto err_fields_alloc
;
4651 header
->fields_count
= fields_count
;
4659 static void dpipe_header_free(struct dpipe_header
*header
)
4661 free(header
->fields
);
4665 static void dpipe_header_clear(struct dpipe_header
*header
)
4667 struct dpipe_field
*field
;
4670 for (i
= 0; i
< header
->fields_count
; i
++) {
4671 field
= &header
->fields
[i
];
4677 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4678 struct dpipe_header
*header
, bool global
)
4681 list_add(&header
->list
, &ctx
->global_headers
);
4683 list_add(&header
->list
, &ctx
->local_headers
);
4686 static void dpipe_header_del(struct dpipe_header
*header
)
4688 list_del(&header
->list
);
4691 static struct dpipe_table
*dpipe_table_alloc(void)
4693 return calloc(1, sizeof(struct dpipe_table
));
4696 static void dpipe_table_free(struct dpipe_table
*table
)
4701 static struct dpipe_tables
*dpipe_tables_alloc(void)
4703 struct dpipe_tables
*tables
;
4705 tables
= calloc(1, sizeof(struct dpipe_tables
));
4708 INIT_LIST_HEAD(&tables
->table_list
);
4712 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4714 struct dpipe_table
*table
, *tmp
;
4716 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4717 dpipe_table_free(table
);
4721 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4723 ctx
->tables
= dpipe_tables_alloc();
4728 INIT_LIST_HEAD(&ctx
->global_headers
);
4729 INIT_LIST_HEAD(&ctx
->local_headers
);
4733 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4735 struct dpipe_header
*header
, *tmp
;
4737 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4739 dpipe_header_del(header
);
4740 dpipe_header_clear(header
);
4741 dpipe_header_free(header
);
4743 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4745 dpipe_header_del(header
);
4746 dpipe_header_clear(header
);
4747 dpipe_header_free(header
);
4749 dpipe_tables_free(ctx
->tables
);
4752 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4753 uint32_t header_id
, bool global
)
4755 struct list_head
*header_list
;
4756 struct dpipe_header
*header
;
4759 header_list
= &ctx
->global_headers
;
4761 header_list
= &ctx
->local_headers
;
4762 list_for_each_entry(header
, header_list
, list
) {
4763 if (header
->id
!= header_id
)
4765 return header
->name
;
4770 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4772 uint32_t field_id
, bool global
)
4774 struct list_head
*header_list
;
4775 struct dpipe_header
*header
;
4778 header_list
= &ctx
->global_headers
;
4780 header_list
= &ctx
->local_headers
;
4781 list_for_each_entry(header
, header_list
, list
) {
4782 if (header
->id
!= header_id
)
4784 return header
->fields
[field_id
].name
;
4790 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4792 switch (mapping_type
) {
4793 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4795 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4803 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4804 uint32_t field_id
, bool global
)
4806 enum devlink_dpipe_field_mapping_type mapping_type
;
4807 struct list_head
*header_list
;
4808 struct dpipe_header
*header
;
4811 header_list
= &ctx
->global_headers
;
4813 header_list
= &ctx
->local_headers
;
4814 list_for_each_entry(header
, header_list
, list
) {
4815 if (header
->id
!= header_id
)
4817 mapping_type
= header
->fields
[field_id
].mapping_type
;
4818 return dpipe_field_mapping_e2s(mapping_type
);
4823 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4824 struct dpipe_field
*fields
,
4825 unsigned int field_count
)
4827 struct dpipe_field
*field
;
4830 for (i
= 0; i
< field_count
; i
++) {
4832 pr_out_entry_start(ctx
->dl
);
4833 check_indent_newline(ctx
->dl
);
4834 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4835 if (ctx
->dl
->verbose
)
4836 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4837 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4838 if (field
->mapping_type
) {
4839 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4840 dpipe_field_mapping_e2s(field
->mapping_type
));
4842 pr_out_entry_end(ctx
->dl
);
4847 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4848 struct dpipe_header
*header
, bool global
)
4850 pr_out_handle_start_arr(ctx
->dl
, tb
);
4851 check_indent_newline(ctx
->dl
);
4852 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4853 if (ctx
->dl
->verbose
) {
4854 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4855 print_bool(PRINT_ANY
, "global", " global %s", global
);
4857 pr_out_array_start(ctx
->dl
, "field");
4858 pr_out_dpipe_fields(ctx
, header
->fields
,
4859 header
->fields_count
);
4860 pr_out_array_end(ctx
->dl
);
4861 pr_out_handle_end(ctx
->dl
);
4864 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4867 struct dpipe_header
*header
;
4869 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4870 pr_out_dpipe_header(ctx
, tb
, header
, false);
4872 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4873 pr_out_dpipe_header(ctx
, tb
, header
, true);
4876 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4878 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4882 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4883 if (err
!= MNL_CB_OK
)
4885 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4886 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4887 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4888 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4891 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4892 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4893 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4894 field
->name
= strdup(name
);
4897 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4901 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4902 struct dpipe_field
*fields
)
4904 struct nlattr
*nla_field
;
4908 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4909 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4917 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4919 struct nlattr
*nla_field
;
4920 unsigned int count
= 0;
4922 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4927 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4929 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4930 struct dpipe_header
*header
;
4931 unsigned int fields_count
;
4932 const char *header_name
;
4936 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4937 if (err
!= MNL_CB_OK
)
4940 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4941 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4942 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4945 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4946 header
= dpipe_header_alloc(fields_count
);
4950 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4951 header
->name
= strdup(header_name
);
4952 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4953 header
->fields_count
= fields_count
;
4954 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4956 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4960 dpipe_header_add(ctx
, header
, global
);
4964 dpipe_header_free(header
);
4968 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4970 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4971 struct nlattr
*nla_header
;
4974 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4975 err
= dpipe_header_get(ctx
, nla_header
);
4982 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4984 struct dpipe_ctx
*ctx
= data
;
4985 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4986 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4989 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4990 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4991 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4992 return MNL_CB_ERROR
;
4993 err
= dpipe_headers_get(ctx
, tb
);
4996 return MNL_CB_ERROR
;
4999 if (ctx
->print_headers
)
5000 pr_out_dpipe_headers(ctx
, tb
);
5004 static int cmd_dpipe_headers_show(struct dl
*dl
)
5006 struct nlmsghdr
*nlh
;
5007 struct dpipe_ctx ctx
= {};
5008 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5011 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5013 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5017 err
= dpipe_ctx_init(&ctx
, dl
);
5021 ctx
.print_headers
= true;
5023 pr_out_section_start(dl
, "header");
5024 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5026 pr_err("error get headers %s\n", strerror(ctx
.err
));
5027 pr_out_section_end(dl
);
5029 dpipe_ctx_fini(&ctx
);
5033 static void cmd_dpipe_help(void)
5035 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5036 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5037 pr_err(" [ counters_enabled { true | false } ]\n");
5038 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5039 pr_err(" devlink dpipe header show DEV\n");
5042 static int cmd_dpipe_header(struct dl
*dl
)
5044 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5047 } else if (dl_argv_match(dl
, "show")) {
5049 return cmd_dpipe_headers_show(dl
);
5051 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5056 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5058 switch (action_type
) {
5059 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5060 return "field_modify";
5066 struct dpipe_op_info
{
5072 struct dpipe_action
{
5073 struct dpipe_op_info info
;
5077 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5078 struct dpipe_ctx
*ctx
)
5080 struct dpipe_op_info
*op_info
= &action
->info
;
5081 const char *mapping
;
5083 check_indent_newline(ctx
->dl
);
5084 print_string(PRINT_ANY
, "type", "type %s",
5085 dpipe_action_type_e2s(action
->type
));
5086 print_string(PRINT_ANY
, "header", " header %s",
5087 dpipe_header_id2s(ctx
, op_info
->header_id
,
5088 op_info
->header_global
));
5089 print_string(PRINT_ANY
, "field", " field %s",
5090 dpipe_field_id2s(ctx
, op_info
->header_id
,
5092 op_info
->header_global
));
5093 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5095 op_info
->header_global
);
5097 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5100 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5102 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5105 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5106 if (err
!= MNL_CB_OK
)
5109 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5110 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5111 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5112 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5116 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5117 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5118 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5119 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5124 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5125 struct nlattr
*nla_actions
)
5127 struct nlattr
*nla_action
;
5128 struct dpipe_action action
;
5130 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5131 pr_out_entry_start(ctx
->dl
);
5132 if (dpipe_action_parse(&action
, nla_action
))
5133 goto err_action_parse
;
5134 pr_out_dpipe_action(&action
, ctx
);
5135 pr_out_entry_end(ctx
->dl
);
5140 pr_out_entry_end(ctx
->dl
);
5145 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5147 switch (match_type
) {
5148 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5149 return "field_exact";
5155 struct dpipe_match
{
5156 struct dpipe_op_info info
;
5160 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5161 struct dpipe_ctx
*ctx
)
5163 struct dpipe_op_info
*op_info
= &match
->info
;
5164 const char *mapping
;
5166 check_indent_newline(ctx
->dl
);
5167 print_string(PRINT_ANY
, "type", "type %s",
5168 dpipe_match_type_e2s(match
->type
));
5169 print_string(PRINT_ANY
, "header", " header %s",
5170 dpipe_header_id2s(ctx
, op_info
->header_id
,
5171 op_info
->header_global
));
5172 print_string(PRINT_ANY
, "field", " field %s",
5173 dpipe_field_id2s(ctx
, op_info
->header_id
,
5175 op_info
->header_global
));
5176 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5178 op_info
->header_global
);
5180 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5183 static int dpipe_match_parse(struct dpipe_match
*match
,
5187 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5190 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5191 if (err
!= MNL_CB_OK
)
5194 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5195 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5196 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5197 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5201 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5202 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5203 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5204 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5209 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5210 struct nlattr
*nla_matches
)
5212 struct nlattr
*nla_match
;
5213 struct dpipe_match match
;
5215 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5216 pr_out_entry_start(ctx
->dl
);
5217 if (dpipe_match_parse(&match
, nla_match
))
5218 goto err_match_parse
;
5219 pr_out_dpipe_match(&match
, ctx
);
5220 pr_out_entry_end(ctx
->dl
);
5225 pr_out_entry_end(ctx
->dl
);
5229 static struct resource
*
5230 resource_find(struct resources
*resources
, struct resource
*resource
,
5231 uint64_t resource_id
)
5233 struct list_head
*list_head
;
5236 list_head
= &resources
->resource_list
;
5238 list_head
= &resource
->resource_list
;
5240 list_for_each_entry(resource
, list_head
, list
) {
5241 struct resource
*child_resource
;
5243 if (resource
->id
== resource_id
)
5246 child_resource
= resource_find(resources
, resource
,
5249 return child_resource
;
5255 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5256 uint64_t resource_id
)
5258 struct resource
*resource
, *parent_resource
;
5259 const char del
[] = "/";
5263 resource
= resource_find(resources
, NULL
, resource_id
);
5267 for (parent_resource
= resource
; parent_resource
;
5268 parent_resource
= parent_resource
->parent
)
5269 path_len
+= strlen(parent_resource
->name
) + 1;
5272 path
= calloc(1, path_len
);
5276 path
+= path_len
- 1;
5277 for (parent_resource
= resource
; parent_resource
;
5278 parent_resource
= parent_resource
->parent
) {
5279 path
-= strlen(parent_resource
->name
);
5280 memcpy(path
, parent_resource
->name
,
5281 strlen(parent_resource
->name
));
5282 path
-= strlen(del
);
5283 memcpy(path
, del
, strlen(del
));
5285 check_indent_newline(dl
);
5286 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5290 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5292 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5293 struct dpipe_table
*table
;
5294 uint32_t resource_units
;
5295 bool counters_enabled
;
5296 bool resource_valid
;
5300 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5301 if (err
!= MNL_CB_OK
)
5304 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5305 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5306 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5307 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5308 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5312 table
= dpipe_table_alloc();
5316 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5317 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5318 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5320 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5322 if (resource_valid
) {
5323 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5324 table
->resource_valid
= true;
5327 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5328 if (!ctx
->print_tables
)
5331 check_indent_newline(ctx
->dl
);
5332 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5333 print_uint(PRINT_ANY
, "size", " size %u", size
);
5334 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5336 if (resource_valid
) {
5337 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5338 resource_path_print(ctx
->dl
, ctx
->resources
,
5339 table
->resource_id
);
5340 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5344 pr_out_array_start(ctx
->dl
, "match");
5345 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5346 goto err_matches_show
;
5347 pr_out_array_end(ctx
->dl
);
5349 pr_out_array_start(ctx
->dl
, "action");
5350 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5351 goto err_actions_show
;
5352 pr_out_array_end(ctx
->dl
);
5358 pr_out_array_end(ctx
->dl
);
5362 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5364 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5365 struct nlattr
*nla_table
;
5367 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5368 if (ctx
->print_tables
)
5369 pr_out_handle_start_arr(ctx
->dl
, tb
);
5370 if (dpipe_table_show(ctx
, nla_table
))
5371 goto err_table_show
;
5372 if (ctx
->print_tables
)
5373 pr_out_handle_end(ctx
->dl
);
5378 if (ctx
->print_tables
)
5379 pr_out_handle_end(ctx
->dl
);
5383 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5385 struct dpipe_ctx
*ctx
= data
;
5386 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5387 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5389 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5390 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5391 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5392 return MNL_CB_ERROR
;
5394 if (dpipe_tables_show(ctx
, tb
))
5395 return MNL_CB_ERROR
;
5399 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5401 static int cmd_dpipe_table_show(struct dl
*dl
)
5403 struct nlmsghdr
*nlh
;
5404 struct dpipe_ctx dpipe_ctx
= {};
5405 struct resource_ctx resource_ctx
= {};
5406 uint16_t flags
= NLM_F_REQUEST
;
5409 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5413 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5415 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5419 dpipe_ctx
.print_tables
= true;
5421 dl_opts_put(nlh
, dl
);
5422 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5425 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5426 goto err_headers_get
;
5429 err
= resource_ctx_init(&resource_ctx
, dl
);
5431 goto err_resource_ctx_init
;
5433 resource_ctx
.print_resources
= false;
5434 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5435 dl_opts_put(nlh
, dl
);
5436 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5439 dpipe_ctx
.resources
= resource_ctx
.resources
;
5441 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5442 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5443 dl_opts_put(nlh
, dl
);
5445 pr_out_section_start(dl
, "table");
5446 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5447 pr_out_section_end(dl
);
5449 resource_ctx_fini(&resource_ctx
);
5450 dpipe_ctx_fini(&dpipe_ctx
);
5453 err_resource_ctx_init
:
5455 dpipe_ctx_fini(&dpipe_ctx
);
5459 static int cmd_dpipe_table_set(struct dl
*dl
)
5461 struct nlmsghdr
*nlh
;
5464 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5465 NLM_F_REQUEST
| NLM_F_ACK
);
5467 err
= dl_argv_parse_put(nlh
, dl
,
5468 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5469 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5473 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5476 enum dpipe_value_type
{
5477 DPIPE_VALUE_TYPE_VALUE
,
5478 DPIPE_VALUE_TYPE_MASK
,
5482 dpipe_value_type_e2s(enum dpipe_value_type type
)
5485 case DPIPE_VALUE_TYPE_VALUE
:
5487 case DPIPE_VALUE_TYPE_MASK
:
5488 return "value_mask";
5494 struct dpipe_field_printer
{
5495 unsigned int field_id
;
5496 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5499 struct dpipe_header_printer
{
5500 struct dpipe_field_printer
*printers
;
5501 unsigned int printers_count
;
5502 unsigned int header_id
;
5505 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5506 enum dpipe_value_type type
,
5509 struct in_addr ip_addr
;
5511 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5512 check_indent_newline(ctx
->dl
);
5513 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5517 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5518 enum dpipe_value_type type
,
5521 check_indent_newline(ctx
->dl
);
5522 print_string_name_value(dpipe_value_type_e2s(type
),
5523 ether_ntoa((struct ether_addr
*)value
));
5526 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5527 enum dpipe_value_type type
,
5530 char str
[INET6_ADDRSTRLEN
];
5532 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5533 check_indent_newline(ctx
->dl
);
5534 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5537 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5539 .printer
= dpipe_field_printer_ipv4_addr
,
5540 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5544 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5545 .printers
= dpipe_field_printers_ipv4
,
5546 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5547 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5550 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5552 .printer
= dpipe_field_printer_ethernet_addr
,
5553 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5557 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5558 .printers
= dpipe_field_printers_ethernet
,
5559 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5560 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5563 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5565 .printer
= dpipe_field_printer_ipv6_addr
,
5566 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5570 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5571 .printers
= dpipe_field_printers_ipv6
,
5572 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5573 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5576 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5577 &dpipe_header_printer_ipv4
,
5578 &dpipe_header_printer_ethernet
,
5579 &dpipe_header_printer_ipv6
,
5582 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5583 struct dpipe_op_info
*info
,
5584 enum dpipe_value_type type
,
5587 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5588 struct dpipe_header_printer
*header_printer
;
5589 struct dpipe_field_printer
*field_printer
;
5590 unsigned int field_printers_count
;
5594 for (i
= 0; i
< header_printers_count
; i
++) {
5595 header_printer
= dpipe_header_printers
[i
];
5596 if (header_printer
->header_id
!= info
->header_id
)
5598 field_printers_count
= header_printer
->printers_count
;
5599 for (j
= 0; j
< field_printers_count
; j
++) {
5600 field_printer
= &header_printer
->printers
[j
];
5601 if (field_printer
->field_id
!= info
->field_id
)
5603 field_printer
->printer(ctx
, type
, value
);
5611 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5613 unsigned int value_len
,
5614 struct dpipe_op_info
*info
,
5615 enum dpipe_value_type type
)
5617 if (info
->header_global
&&
5618 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5621 if (value_len
== sizeof(uint32_t)) {
5622 uint32_t *value_32
= value
;
5624 check_indent_newline(ctx
->dl
);
5625 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5629 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5630 struct nlattr
**nla_match_value
,
5631 struct dpipe_op_info
*info
)
5633 void *value
, *value_mask
;
5634 uint32_t value_mapping
;
5638 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5639 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5641 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5642 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5645 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5646 check_indent_newline(ctx
->dl
);
5647 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5651 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5652 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5653 DPIPE_VALUE_TYPE_MASK
);
5656 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5659 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5662 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5663 struct dpipe_match match
;
5666 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5667 if (err
!= MNL_CB_OK
)
5670 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5671 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5675 pr_out_entry_start(ctx
->dl
);
5676 if (dpipe_match_parse(&match
,
5677 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5678 goto err_match_parse
;
5679 pr_out_dpipe_match(&match
, ctx
);
5680 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5681 pr_out_entry_end(ctx
->dl
);
5686 pr_out_entry_end(ctx
->dl
);
5690 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5693 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5694 struct dpipe_action action
;
5697 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5698 if (err
!= MNL_CB_OK
)
5701 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5702 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5706 pr_out_entry_start(ctx
->dl
);
5707 if (dpipe_action_parse(&action
,
5708 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5709 goto err_action_parse
;
5710 pr_out_dpipe_action(&action
, ctx
);
5711 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5712 pr_out_entry_end(ctx
->dl
);
5717 pr_out_entry_end(ctx
->dl
);
5722 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5723 struct nlattr
*nla_action_values
)
5725 struct nlattr
*nla_action_value
;
5727 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5728 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5735 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5736 struct nlattr
*nla_match_values
)
5738 struct nlattr
*nla_match_value
;
5740 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5741 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5747 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5749 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5750 uint32_t entry_index
;
5754 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5755 if (err
!= MNL_CB_OK
)
5758 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5759 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5760 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5764 check_indent_newline(ctx
->dl
);
5765 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5766 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5768 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5769 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5770 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5773 pr_out_array_start(ctx
->dl
, "match_value");
5774 if (dpipe_tables_match_values_show(ctx
,
5775 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5776 goto err_match_values_show
;
5777 pr_out_array_end(ctx
->dl
);
5779 pr_out_array_start(ctx
->dl
, "action_value");
5780 if (dpipe_tables_action_values_show(ctx
,
5781 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5782 goto err_action_values_show
;
5783 pr_out_array_end(ctx
->dl
);
5786 err_action_values_show
:
5787 err_match_values_show
:
5788 pr_out_array_end(ctx
->dl
);
5792 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5794 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5795 struct nlattr
*nla_entry
;
5797 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5798 pr_out_handle_start_arr(ctx
->dl
, tb
);
5799 if (dpipe_entry_show(ctx
, nla_entry
))
5800 goto err_entry_show
;
5801 pr_out_handle_end(ctx
->dl
);
5806 pr_out_handle_end(ctx
->dl
);
5810 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5812 struct dpipe_ctx
*ctx
= data
;
5813 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5814 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5816 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5817 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5818 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5819 return MNL_CB_ERROR
;
5821 if (dpipe_table_entries_show(ctx
, tb
))
5822 return MNL_CB_ERROR
;
5826 static int cmd_dpipe_table_dump(struct dl
*dl
)
5828 struct nlmsghdr
*nlh
;
5829 struct dpipe_ctx ctx
= {};
5830 uint16_t flags
= NLM_F_REQUEST
;
5833 err
= dpipe_ctx_init(&ctx
, dl
);
5837 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5841 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5842 dl_opts_put(nlh
, dl
);
5843 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5845 pr_err("error get headers %s\n", strerror(ctx
.err
));
5849 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5850 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5851 dl_opts_put(nlh
, dl
);
5853 pr_out_section_start(dl
, "table_entry");
5854 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5855 pr_out_section_end(dl
);
5857 dpipe_ctx_fini(&ctx
);
5861 static int cmd_dpipe_table(struct dl
*dl
)
5863 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5866 } else if (dl_argv_match(dl
, "show")) {
5868 return cmd_dpipe_table_show(dl
);
5869 } else if (dl_argv_match(dl
, "set")) {
5871 return cmd_dpipe_table_set(dl
);
5872 } else if (dl_argv_match(dl
, "dump")) {
5874 return cmd_dpipe_table_dump(dl
);
5876 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5880 static int cmd_dpipe(struct dl
*dl
)
5882 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5885 } else if (dl_argv_match(dl
, "header")) {
5887 return cmd_dpipe_header(dl
);
5888 } else if (dl_argv_match(dl
, "table")) {
5890 return cmd_dpipe_table(dl
);
5892 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5897 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5898 struct nlattr
**nla_resource
)
5900 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5901 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5902 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5903 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5904 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5905 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5906 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5910 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5911 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5912 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5913 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5914 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5915 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5916 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5918 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5919 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5921 resource
->size_new
= resource
->size
;
5923 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5924 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5925 resource
->occ_valid
= true;
5928 if (resource
->size_new
!= resource
->size
)
5929 ctx
->pending_change
= true;
5935 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5936 struct resource
*parent_resource
, struct nlattr
*nl
)
5938 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5939 struct nlattr
*nla_child_resource
;
5940 struct nlattr
*nla_resources
;
5950 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5951 if (err
!= MNL_CB_OK
)
5954 err
= resource_parse(ctx
, resource
, nla_resource
);
5958 resource
->parent
= parent_resource
;
5959 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5962 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5963 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5965 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5966 struct resource
*child_resource
;
5967 struct list_head
*list
;
5969 child_resource
= resource_alloc();
5970 if (!child_resource
)
5974 list
= &ctx
->resources
->resource_list
;
5976 list
= &resource
->resource_list
;
5978 list_add_tail(&child_resource
->list
, list
);
5979 err
= resource_get(ctx
, child_resource
, resource
,
5980 nla_child_resource
);
5988 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5991 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5992 default: return "<unknown unit>";
5996 static void resource_show(struct resource
*resource
,
5997 struct resource_ctx
*ctx
)
5999 struct resource
*child_resource
;
6000 struct dpipe_table
*table
;
6001 struct dl
*dl
= ctx
->dl
;
6004 check_indent_newline(dl
);
6005 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6007 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6008 pr_out_u64(dl
, "size", resource
->size
);
6009 if (resource
->size
!= resource
->size_new
)
6010 pr_out_u64(dl
, "size_new", resource
->size_new
);
6011 if (resource
->occ_valid
)
6012 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6013 print_string(PRINT_ANY
, "unit", " unit %s",
6014 resource_unit_str_get(resource
->unit
));
6016 if (resource
->size_min
!= resource
->size_max
) {
6017 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6018 resource
->size_min
);
6019 pr_out_u64(dl
, "size_max", resource
->size_max
);
6020 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6021 resource
->size_gran
);
6024 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6025 if (table
->resource_id
== resource
->id
&&
6026 table
->resource_valid
)
6030 pr_out_array_start(dl
, "dpipe_tables");
6032 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6035 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6036 if (table
->resource_id
!= resource
->id
||
6037 !table
->resource_valid
)
6039 pr_out_entry_start(dl
);
6040 check_indent_newline(dl
);
6041 print_string(PRINT_ANY
, "table_name", "table_name %s",
6043 pr_out_entry_end(dl
);
6046 pr_out_array_end(dl
);
6048 if (list_empty(&resource
->resource_list
))
6051 if (ctx
->pending_change
) {
6052 check_indent_newline(dl
);
6053 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6054 resource
->size_valid
? "true" : "false");
6056 pr_out_array_start(dl
, "resources");
6057 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6058 pr_out_entry_start(dl
);
6059 resource_show(child_resource
, ctx
);
6060 pr_out_entry_end(dl
);
6062 pr_out_array_end(dl
);
6066 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6068 struct resources
*resources
= ctx
->resources
;
6069 struct resource
*resource
;
6071 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6072 pr_out_handle_start_arr(ctx
->dl
, tb
);
6073 resource_show(resource
, ctx
);
6074 pr_out_handle_end(ctx
->dl
);
6078 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6080 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6083 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6085 struct resource_ctx
*ctx
= data
;
6086 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6087 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6090 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6091 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6092 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6093 return MNL_CB_ERROR
;
6095 err
= resources_get(ctx
, tb
);
6098 return MNL_CB_ERROR
;
6101 if (ctx
->print_resources
)
6102 resources_show(ctx
, tb
);
6107 static int cmd_resource_show(struct dl
*dl
)
6109 struct nlmsghdr
*nlh
;
6110 struct dpipe_ctx dpipe_ctx
= {};
6111 struct resource_ctx resource_ctx
= {};
6114 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6118 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6120 dl_opts_put(nlh
, dl
);
6122 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6126 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6129 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6133 err
= resource_ctx_init(&resource_ctx
, dl
);
6137 resource_ctx
.print_resources
= true;
6138 resource_ctx
.tables
= dpipe_ctx
.tables
;
6139 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6140 NLM_F_REQUEST
| NLM_F_ACK
);
6141 dl_opts_put(nlh
, dl
);
6142 pr_out_section_start(dl
, "resources");
6143 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6145 pr_out_section_end(dl
);
6146 resource_ctx_fini(&resource_ctx
);
6148 dpipe_ctx_fini(&dpipe_ctx
);
6152 static void cmd_resource_help(void)
6154 pr_err("Usage: devlink resource show DEV\n"
6155 " devlink resource set DEV path PATH size SIZE\n");
6158 static struct resource
*
6159 resource_find_by_name(struct list_head
*list
, char *name
)
6161 struct resource
*resource
;
6163 list_for_each_entry(resource
, list
, list
) {
6164 if (!strcmp(resource
->name
, name
))
6171 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6172 uint32_t *p_resource_id
, bool *p_resource_valid
)
6174 struct resource
*resource
;
6175 uint32_t resource_id
= 0;
6176 char *resource_path_dup
;
6177 struct list_head
*list
;
6178 const char del
[] = "/";
6179 char *resource_name
;
6181 resource_path_dup
= strdup(resource_path
);
6182 list
= &ctx
->resources
->resource_list
;
6183 resource_name
= strtok(resource_path_dup
, del
);
6184 while (resource_name
!= NULL
) {
6185 resource
= resource_find_by_name(list
, resource_name
);
6187 goto err_resource_lookup
;
6189 list
= &resource
->resource_list
;
6190 resource_name
= strtok(NULL
, del
);
6191 resource_id
= resource
->id
;
6193 free(resource_path_dup
);
6194 *p_resource_valid
= true;
6195 *p_resource_id
= resource_id
;
6198 err_resource_lookup
:
6199 free(resource_path_dup
);
6203 static int cmd_resource_set(struct dl
*dl
)
6205 struct nlmsghdr
*nlh
;
6206 struct resource_ctx ctx
= {};
6209 err
= resource_ctx_init(&ctx
, dl
);
6213 ctx
.print_resources
= false;
6214 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6215 DL_OPT_RESOURCE_SIZE
, 0);
6219 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6221 dl_opts_put(nlh
, dl
);
6222 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6224 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6228 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6229 &dl
->opts
.resource_id
,
6230 &dl
->opts
.resource_id_valid
);
6232 pr_err("error parsing resource path %s\n", strerror(-err
));
6236 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6237 NLM_F_REQUEST
| NLM_F_ACK
);
6239 dl_opts_put(nlh
, dl
);
6240 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6242 resource_ctx_fini(&ctx
);
6246 static int cmd_resource(struct dl
*dl
)
6248 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6249 cmd_resource_help();
6251 } else if (dl_argv_match(dl
, "show")) {
6253 return cmd_resource_show(dl
);
6254 } else if (dl_argv_match(dl
, "set")) {
6256 return cmd_resource_set(dl
);
6258 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6262 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6264 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6265 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6266 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6269 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6270 if (dl
->json_output
)
6271 open_json_object(buf
);
6276 static void pr_out_region_handle_end(struct dl
*dl
)
6278 if (dl
->json_output
)
6279 close_json_object();
6284 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6286 __pr_out_indent_newline(dl
);
6287 if (dl
->json_output
)
6288 open_json_array(PRINT_JSON
, "snapshot");
6290 pr_out("snapshot %s", array
? "[" : "");
6293 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6295 if (dl
->json_output
)
6296 close_json_array(PRINT_JSON
, NULL
);
6301 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6303 uint32_t snapshot_id
;
6305 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6308 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6310 if (dl
->json_output
)
6311 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6313 pr_out("%s%u", index
? " " : "", snapshot_id
);
6316 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6318 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6319 struct nlattr
*nla_sanpshot
;
6322 pr_out_region_snapshots_start(dl
, true);
6323 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6324 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6325 if (err
!= MNL_CB_OK
)
6327 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6329 pr_out_region_snapshots_end(dl
, true);
6332 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6334 pr_out_region_snapshots_start(dl
, false);
6335 pr_out_region_snapshots_id(dl
, tb
, 0);
6336 pr_out_region_snapshots_end(dl
, false);
6339 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6341 pr_out_region_handle_start(dl
, tb
);
6343 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6344 pr_out_u64(dl
, "size",
6345 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6347 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6348 pr_out_snapshots(dl
, tb
);
6350 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6351 pr_out_snapshot(dl
, tb
);
6353 pr_out_region_handle_end(dl
);
6356 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6358 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6359 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6360 struct dl
*dl
= data
;
6362 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6363 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6364 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6365 return MNL_CB_ERROR
;
6367 pr_out_region(dl
, tb
);
6372 static int cmd_region_show(struct dl
*dl
)
6374 struct nlmsghdr
*nlh
;
6375 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6378 if (dl_argc(dl
) == 0)
6379 flags
|= NLM_F_DUMP
;
6381 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6383 if (dl_argc(dl
) > 0) {
6384 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6389 pr_out_section_start(dl
, "regions");
6390 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6391 pr_out_section_end(dl
);
6395 static int cmd_region_snapshot_del(struct dl
*dl
)
6397 struct nlmsghdr
*nlh
;
6400 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6401 NLM_F_REQUEST
| NLM_F_ACK
);
6403 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6404 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6408 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6411 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6413 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6414 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6415 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6416 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6417 struct dl
*dl
= data
;
6420 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6421 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6422 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6423 return MNL_CB_ERROR
;
6425 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6426 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6427 if (err
!= MNL_CB_OK
)
6428 return MNL_CB_ERROR
;
6430 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6431 if (!nla_chunk_data
)
6434 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6435 if (!nla_chunk_addr
)
6438 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6439 mnl_attr_get_payload_len(nla_chunk_data
),
6440 mnl_attr_get_u64(nla_chunk_addr
));
6445 static int cmd_region_dump(struct dl
*dl
)
6447 struct nlmsghdr
*nlh
;
6450 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6451 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6453 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6454 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6458 pr_out_section_start(dl
, "dump");
6459 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6460 pr_out_section_end(dl
);
6461 if (!dl
->json_output
)
6466 static int cmd_region_read(struct dl
*dl
)
6468 struct nlmsghdr
*nlh
;
6471 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6472 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6474 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6475 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6476 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6480 pr_out_section_start(dl
, "read");
6481 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6482 pr_out_section_end(dl
);
6483 if (!dl
->json_output
)
6488 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
6490 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6491 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6492 struct dl
*dl
= data
;
6494 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6495 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6496 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
6497 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6498 return MNL_CB_ERROR
;
6500 pr_out_region(dl
, tb
);
6505 static int cmd_region_snapshot_new(struct dl
*dl
)
6507 struct nlmsghdr
*nlh
;
6510 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
6511 NLM_F_REQUEST
| NLM_F_ACK
);
6513 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
6514 DL_OPT_REGION_SNAPSHOT_ID
);
6518 pr_out_section_start(dl
, "regions");
6519 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
6520 pr_out_section_end(dl
);
6524 static void cmd_region_help(void)
6526 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6527 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6528 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
6529 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6530 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6533 static int cmd_region(struct dl
*dl
)
6535 if (dl_no_arg(dl
)) {
6536 return cmd_region_show(dl
);
6537 } else if (dl_argv_match(dl
, "help")) {
6540 } else if (dl_argv_match(dl
, "show")) {
6542 return cmd_region_show(dl
);
6543 } else if (dl_argv_match(dl
, "del")) {
6545 return cmd_region_snapshot_del(dl
);
6546 } else if (dl_argv_match(dl
, "dump")) {
6548 return cmd_region_dump(dl
);
6549 } else if (dl_argv_match(dl
, "read")) {
6551 return cmd_region_read(dl
);
6552 } else if (dl_argv_match(dl
, "new")) {
6554 return cmd_region_snapshot_new(dl
);
6556 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6560 static int cmd_health_set_params(struct dl
*dl
)
6562 struct nlmsghdr
*nlh
;
6565 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6566 NLM_F_REQUEST
| NLM_F_ACK
);
6567 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6568 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6569 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
6570 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
6574 dl_opts_put(nlh
, dl
);
6575 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6578 static int cmd_health_dump_clear(struct dl
*dl
)
6580 struct nlmsghdr
*nlh
;
6583 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6584 NLM_F_REQUEST
| NLM_F_ACK
);
6586 err
= dl_argv_parse_put(nlh
, dl
,
6587 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6591 dl_opts_put(nlh
, dl
);
6592 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6595 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6600 check_indent_newline(dl
);
6603 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6606 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6609 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6612 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6615 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6617 case MNL_TYPE_NUL_STRING
:
6618 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6620 case MNL_TYPE_BINARY
:
6621 len
= mnl_attr_get_payload_len(nl_data
);
6622 data
= mnl_attr_get_payload(nl_data
);
6623 pr_out_binary_value(dl
, data
, len
);
6631 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6636 pr_out_name(dl
, *name
);
6643 struct list_head list
;
6646 struct fmsg_cb_data
{
6650 struct list_head entry_list
;
6653 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6654 uint8_t *attr_value
, bool insert
)
6656 struct nest_entry
*entry
;
6659 entry
= malloc(sizeof(struct nest_entry
));
6663 entry
->attr_type
= *attr_value
;
6664 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6666 if (list_empty(&fmsg_data
->entry_list
))
6667 return MNL_CB_ERROR
;
6668 entry
= list_first_entry(&fmsg_data
->entry_list
,
6669 struct nest_entry
, list
);
6670 *attr_value
= entry
->attr_type
;
6671 list_del(&entry
->list
);
6677 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6680 pr_out_fmsg_name(dl
, name
);
6682 __pr_out_indent_inc();
6685 static void pr_out_fmsg_group_end(struct dl
*dl
)
6688 __pr_out_indent_dec();
6691 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6693 if (dl
->json_output
) {
6694 pr_out_fmsg_name(dl
, name
);
6695 open_json_object(NULL
);
6697 pr_out_fmsg_group_start(dl
, name
);
6701 static void pr_out_fmsg_end_object(struct dl
*dl
)
6703 if (dl
->json_output
)
6704 close_json_object();
6706 pr_out_fmsg_group_end(dl
);
6709 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6711 if (dl
->json_output
) {
6712 pr_out_fmsg_name(dl
, name
);
6713 open_json_array(PRINT_JSON
, NULL
);
6715 pr_out_fmsg_group_start(dl
, name
);
6719 static void pr_out_fmsg_end_array(struct dl
*dl
)
6721 if (dl
->json_output
)
6722 close_json_array(PRINT_JSON
, NULL
);
6724 pr_out_fmsg_group_end(dl
);
6727 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6730 struct dl
*dl
= fmsg_data
->dl
;
6731 uint8_t value
= nest_value
;
6734 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6735 if (err
!= MNL_CB_OK
)
6739 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6741 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6743 pr_out_fmsg_end_object(dl
);
6745 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6747 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6749 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6751 pr_out_fmsg_end_array(dl
);
6759 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6761 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6762 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6763 struct fmsg_cb_data
*fmsg_data
= data
;
6764 struct dl
*dl
= fmsg_data
->dl
;
6765 struct nlattr
*nla_object
;
6769 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6770 if (!tb
[DEVLINK_ATTR_FMSG
])
6771 return MNL_CB_ERROR
;
6773 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6774 attr_type
= mnl_attr_get_type(nla_object
);
6775 switch (attr_type
) {
6776 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6777 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6778 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6779 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6780 if (err
!= MNL_CB_OK
)
6783 case DEVLINK_ATTR_FMSG_NEST_END
:
6784 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6785 if (err
!= MNL_CB_OK
)
6788 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6789 free(fmsg_data
->name
);
6790 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6791 if (!fmsg_data
->name
)
6794 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6795 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6797 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6798 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6799 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6801 if (err
!= MNL_CB_OK
)
6811 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6813 /* FMSG is dynamic: opening of an object or array causes a
6814 * newline. JSON starts with an { or [, but plain text should
6815 * not start with a new line. Ensure this by setting
6816 * g_new_line_count to 1: avoiding newline before the first
6819 g_new_line_count
= 1;
6822 INIT_LIST_HEAD(&data
->entry_list
);
6825 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6827 struct fmsg_cb_data data
;
6828 struct nlmsghdr
*nlh
;
6831 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6833 err
= dl_argv_parse_put(nlh
, dl
,
6834 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6838 cmd_fmsg_init(dl
, &data
);
6839 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6844 static int cmd_health_dump_show(struct dl
*dl
)
6846 return cmd_health_object_common(dl
,
6847 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6851 static int cmd_health_diagnose(struct dl
*dl
)
6853 return cmd_health_object_common(dl
,
6854 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6858 static int cmd_health_recover(struct dl
*dl
)
6860 struct nlmsghdr
*nlh
;
6863 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6864 NLM_F_REQUEST
| NLM_F_ACK
);
6866 err
= dl_argv_parse_put(nlh
, dl
,
6867 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6871 dl_opts_put(nlh
, dl
);
6872 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6875 enum devlink_health_reporter_state
{
6876 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6877 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6880 static const char *health_state_name(uint8_t state
)
6883 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6884 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6885 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6886 return HEALTH_REPORTER_STATE_ERROR_STR
;
6888 return "<unknown state>";
6892 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6894 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6895 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6896 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6897 struct sysinfo s_info
;
6903 info
= localtime(&now
);
6904 err
= sysinfo(&s_info
);
6907 /* Subtract uptime in sec from now yields the time of system
6908 * uptime. To this, add time_ms which is the amount of
6909 * milliseconds elapsed between uptime and the dump taken.
6911 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6912 info
= localtime(&sec
);
6914 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6915 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6916 check_indent_newline(dl
);
6917 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6918 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6921 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6923 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6924 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6929 ts
= mnl_attr_get_u64(attr
);
6930 tv_sec
= ts
/ 1000000000;
6931 tm
= localtime(&tv_sec
);
6933 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6934 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6936 check_indent_newline(dl
);
6937 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6938 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6941 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6943 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6944 enum devlink_health_reporter_state state
;
6947 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6949 if (err
!= MNL_CB_OK
)
6952 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6953 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6954 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6955 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6958 pr_out_handle_start_arr(dl
, tb_health
);
6960 check_indent_newline(dl
);
6961 print_string(PRINT_ANY
, "reporter", "reporter %s",
6962 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6963 if (!dl
->json_output
) {
6965 __pr_out_indent_inc();
6967 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6968 check_indent_newline(dl
);
6969 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6970 pr_out_u64(dl
, "error",
6971 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6972 pr_out_u64(dl
, "recover",
6973 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6974 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6975 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6976 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6977 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6978 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6979 pr_out_u64(dl
, "grace_period",
6980 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6981 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6982 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6983 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6984 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
6985 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
6986 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
6988 __pr_out_indent_dec();
6989 pr_out_handle_end(dl
);
6992 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6994 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6995 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6996 struct dl
*dl
= data
;
6998 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6999 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7000 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
7001 return MNL_CB_ERROR
;
7003 pr_out_health(dl
, tb
);
7008 static int cmd_health_show(struct dl
*dl
)
7010 struct nlmsghdr
*nlh
;
7011 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7014 if (dl_argc(dl
) == 0)
7015 flags
|= NLM_F_DUMP
;
7016 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7019 if (dl_argc(dl
) > 0) {
7020 err
= dl_argv_parse_put(nlh
, dl
,
7022 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7026 pr_out_section_start(dl
, "health");
7028 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
7029 pr_out_section_end(dl
);
7033 static void cmd_health_help(void)
7035 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
7036 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
7037 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
7038 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
7039 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
7040 pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
7041 pr_err(" [ grace_period MSEC ]\n");
7042 pr_err(" [ auto_recover { true | false } ]\n");
7043 pr_err(" [ auto_dump { true | false } ]\n");
7046 static int cmd_health(struct dl
*dl
)
7048 if (dl_argv_match(dl
, "help")) {
7051 } else if (dl_argv_match(dl
, "show") ||
7052 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7054 return cmd_health_show(dl
);
7055 } else if (dl_argv_match(dl
, "recover")) {
7057 return cmd_health_recover(dl
);
7058 } else if (dl_argv_match(dl
, "diagnose")) {
7060 return cmd_health_diagnose(dl
);
7061 } else if (dl_argv_match(dl
, "dump")) {
7063 if (dl_argv_match(dl
, "show")) {
7065 return cmd_health_dump_show(dl
);
7066 } else if (dl_argv_match(dl
, "clear")) {
7068 return cmd_health_dump_clear(dl
);
7070 } else if (dl_argv_match(dl
, "set")) {
7072 return cmd_health_set_params(dl
);
7074 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7078 static const char *trap_type_name(uint8_t type
)
7081 case DEVLINK_TRAP_TYPE_DROP
:
7083 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7085 case DEVLINK_TRAP_TYPE_CONTROL
:
7088 return "<unknown type>";
7092 static const char *trap_action_name(uint8_t action
)
7095 case DEVLINK_TRAP_ACTION_DROP
:
7097 case DEVLINK_TRAP_ACTION_TRAP
:
7099 case DEVLINK_TRAP_ACTION_MIRROR
:
7102 return "<unknown action>";
7106 static const char *trap_metadata_name(const struct nlattr
*attr
)
7108 switch (attr
->nla_type
) {
7109 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7110 return "input_port";
7111 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7112 return "flow_action_cookie";
7114 return "<unknown metadata type>";
7117 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7119 struct nlattr
*attr_metadata
;
7121 pr_out_array_start(dl
, "metadata");
7122 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7123 check_indent_newline(dl
);
7124 print_string(PRINT_ANY
, NULL
, "%s",
7125 trap_metadata_name(attr_metadata
));
7127 pr_out_array_end(dl
);
7130 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7132 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7133 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7136 pr_out_handle_start_arr(dl
, tb
);
7138 __pr_out_handle_start(dl
, tb
, true, false);
7140 check_indent_newline(dl
);
7141 print_string(PRINT_ANY
, "name", "name %s",
7142 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7143 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7144 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7145 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7146 print_string(PRINT_ANY
, "group", " group %s",
7147 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7149 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7150 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7151 pr_out_handle_end(dl
);
7154 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7156 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7157 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7158 struct dl
*dl
= data
;
7160 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7161 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7162 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7163 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7164 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7165 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7166 return MNL_CB_ERROR
;
7168 pr_out_trap(dl
, tb
, true);
7173 static void cmd_trap_help(void)
7175 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7176 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7177 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7178 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7179 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7180 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7181 pr_err(" devlink trap policer show DEV policer POLICER\n");
7184 static int cmd_trap_show(struct dl
*dl
)
7186 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7187 struct nlmsghdr
*nlh
;
7190 if (dl_argc(dl
) == 0)
7191 flags
|= NLM_F_DUMP
;
7193 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7195 if (dl_argc(dl
) > 0) {
7196 err
= dl_argv_parse_put(nlh
, dl
,
7197 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7202 pr_out_section_start(dl
, "trap");
7203 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7204 pr_out_section_end(dl
);
7209 static int cmd_trap_set(struct dl
*dl
)
7211 struct nlmsghdr
*nlh
;
7214 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7215 NLM_F_REQUEST
| NLM_F_ACK
);
7217 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7218 DL_OPT_TRAP_ACTION
);
7222 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7225 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7228 pr_out_handle_start_arr(dl
, tb
);
7230 __pr_out_handle_start(dl
, tb
, true, false);
7232 check_indent_newline(dl
);
7233 print_string(PRINT_ANY
, "name", "name %s",
7234 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7235 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7236 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7237 print_uint(PRINT_ANY
, "policer", " policer %u",
7238 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7239 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7240 pr_out_handle_end(dl
);
7243 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7245 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7246 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7247 struct dl
*dl
= data
;
7249 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7250 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7251 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7252 return MNL_CB_ERROR
;
7254 pr_out_trap_group(dl
, tb
, true);
7259 static int cmd_trap_group_show(struct dl
*dl
)
7261 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7262 struct nlmsghdr
*nlh
;
7265 if (dl_argc(dl
) == 0)
7266 flags
|= NLM_F_DUMP
;
7268 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7270 if (dl_argc(dl
) > 0) {
7271 err
= dl_argv_parse_put(nlh
, dl
,
7272 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7278 pr_out_section_start(dl
, "trap_group");
7279 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7280 pr_out_section_end(dl
);
7285 static int cmd_trap_group_set(struct dl
*dl
)
7287 struct nlmsghdr
*nlh
;
7290 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7291 NLM_F_REQUEST
| NLM_F_ACK
);
7293 err
= dl_argv_parse_put(nlh
, dl
,
7294 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7295 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7299 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7302 static int cmd_trap_group(struct dl
*dl
)
7304 if (dl_argv_match(dl
, "help")) {
7307 } else if (dl_argv_match(dl
, "show") ||
7308 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7310 return cmd_trap_group_show(dl
);
7311 } else if (dl_argv_match(dl
, "set")) {
7313 return cmd_trap_group_set(dl
);
7315 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7319 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7322 pr_out_handle_start_arr(dl
, tb
);
7324 __pr_out_handle_start(dl
, tb
, true, false);
7326 check_indent_newline(dl
);
7327 print_uint(PRINT_ANY
, "policer", "policer %u",
7328 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7329 print_u64(PRINT_ANY
, "rate", " rate %llu",
7330 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7331 print_u64(PRINT_ANY
, "burst", " burst %llu",
7332 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7333 if (tb
[DEVLINK_ATTR_STATS
])
7334 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7335 pr_out_handle_end(dl
);
7338 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7340 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7341 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7342 struct dl
*dl
= data
;
7344 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7345 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7346 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7347 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7348 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7349 return MNL_CB_ERROR
;
7351 pr_out_trap_policer(dl
, tb
, true);
7356 static int cmd_trap_policer_show(struct dl
*dl
)
7358 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7359 struct nlmsghdr
*nlh
;
7362 if (dl_argc(dl
) == 0)
7363 flags
|= NLM_F_DUMP
;
7365 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7367 if (dl_argc(dl
) > 0) {
7368 err
= dl_argv_parse_put(nlh
, dl
,
7369 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7375 pr_out_section_start(dl
, "trap_policer");
7376 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7377 pr_out_section_end(dl
);
7382 static int cmd_trap_policer_set(struct dl
*dl
)
7384 struct nlmsghdr
*nlh
;
7387 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7388 NLM_F_REQUEST
| NLM_F_ACK
);
7390 err
= dl_argv_parse_put(nlh
, dl
,
7391 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7392 DL_OPT_TRAP_POLICER_RATE
|
7393 DL_OPT_TRAP_POLICER_BURST
);
7397 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7400 static int cmd_trap_policer(struct dl
*dl
)
7402 if (dl_argv_match(dl
, "help")) {
7405 } else if (dl_argv_match(dl
, "show") ||
7406 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7408 return cmd_trap_policer_show(dl
);
7409 } else if (dl_argv_match(dl
, "set")) {
7411 return cmd_trap_policer_set(dl
);
7413 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7417 static int cmd_trap(struct dl
*dl
)
7419 if (dl_argv_match(dl
, "help")) {
7422 } else if (dl_argv_match(dl
, "show") ||
7423 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7425 return cmd_trap_show(dl
);
7426 } else if (dl_argv_match(dl
, "set")) {
7428 return cmd_trap_set(dl
);
7429 } else if (dl_argv_match(dl
, "group")) {
7431 return cmd_trap_group(dl
);
7432 } else if (dl_argv_match(dl
, "policer")) {
7434 return cmd_trap_policer(dl
);
7436 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7440 static void help(void)
7442 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7443 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7444 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7445 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7448 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7453 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7456 } else if (dl_argv_match(dl
, "dev")) {
7459 } else if (dl_argv_match(dl
, "port")) {
7461 return cmd_port(dl
);
7462 } else if (dl_argv_match(dl
, "sb")) {
7465 } else if (dl_argv_match(dl
, "monitor")) {
7468 } else if (dl_argv_match(dl
, "dpipe")) {
7470 return cmd_dpipe(dl
);
7471 } else if (dl_argv_match(dl
, "resource")) {
7473 return cmd_resource(dl
);
7474 } else if (dl_argv_match(dl
, "region")) {
7476 return cmd_region(dl
);
7477 } else if (dl_argv_match(dl
, "health")) {
7479 return cmd_health(dl
);
7480 } else if (dl_argv_match(dl
, "trap")) {
7482 return cmd_trap(dl
);
7484 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7488 static int dl_init(struct dl
*dl
)
7492 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7494 pr_err("Failed to connect to devlink Netlink\n");
7498 err
= ifname_map_init(dl
);
7500 pr_err("Failed to create index map\n");
7501 goto err_ifname_map_create
;
7503 new_json_obj_plain(dl
->json_output
);
7506 err_ifname_map_create
:
7507 mnlg_socket_close(dl
->nlg
);
7511 static void dl_fini(struct dl
*dl
)
7513 delete_json_obj_plain();
7514 ifname_map_fini(dl
);
7515 mnlg_socket_close(dl
->nlg
);
7518 static struct dl
*dl_alloc(void)
7522 dl
= calloc(1, sizeof(*dl
));
7528 static void dl_free(struct dl
*dl
)
7533 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7537 int ret
= EXIT_SUCCESS
;
7539 if (name
&& strcmp(name
, "-") != 0) {
7540 if (freopen(name
, "r", stdin
) == NULL
) {
7542 "Cannot open file \"%s\" for reading: %s\n",
7543 name
, strerror(errno
));
7544 return EXIT_FAILURE
;
7549 while (getcmdline(&line
, &len
, stdin
) != -1) {
7553 largc
= makeargs(line
, largv
, 100);
7555 continue; /* blank line */
7557 if (dl_cmd(dl
, largc
, largv
)) {
7558 fprintf(stderr
, "Command failed %s:%d\n",
7572 int main(int argc
, char **argv
)
7574 static const struct option long_options
[] = {
7575 { "Version", no_argument
, NULL
, 'V' },
7576 { "force", no_argument
, NULL
, 'f' },
7577 { "batch", required_argument
, NULL
, 'b' },
7578 { "no-nice-names", no_argument
, NULL
, 'n' },
7579 { "json", no_argument
, NULL
, 'j' },
7580 { "pretty", no_argument
, NULL
, 'p' },
7581 { "verbose", no_argument
, NULL
, 'v' },
7582 { "statistics", no_argument
, NULL
, 's' },
7583 { "Netns", required_argument
, NULL
, 'N' },
7584 { NULL
, 0, NULL
, 0 }
7586 const char *batch_file
= NULL
;
7595 pr_err("Failed to allocate memory for devlink\n");
7596 return EXIT_FAILURE
;
7599 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7600 long_options
, NULL
)) >= 0) {
7604 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7611 batch_file
= optarg
;
7614 dl
->no_nice_names
= true;
7617 dl
->json_output
= true;
7629 if (netns_switch(optarg
)) {
7635 pr_err("Unknown option.\n");
7652 err
= dl_batch(dl
, batch_file
, force
);
7654 err
= dl_cmd(dl
, argc
, argv
);