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
= "fw_load_policy",
2364 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DISK
,
2367 .name
= "reset_dev_on_drv_probe",
2369 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2372 .name
= "fw_load_policy",
2374 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2377 .name
= "reset_dev_on_drv_probe",
2379 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2382 .name
= "reset_dev_on_drv_probe",
2384 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2387 .name
= "reset_dev_on_drv_probe",
2389 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2393 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2395 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2396 int nla_type
, struct nlattr
*nl
)
2398 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2399 struct nlattr
*val_attr
;
2404 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2405 if (err
!= MNL_CB_OK
)
2408 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2409 (nla_type
!= MNL_TYPE_FLAG
&&
2410 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2413 check_indent_newline(dl
);
2414 print_string(PRINT_ANY
, "cmode", "cmode %s",
2415 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2417 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2419 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2425 err
= param_val_conv_str_get(param_val_conv
,
2428 mnl_attr_get_u8(val_attr
),
2432 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2434 print_uint(PRINT_ANY
, "value", " value %u",
2435 mnl_attr_get_u8(val_attr
));
2440 err
= param_val_conv_str_get(param_val_conv
,
2443 mnl_attr_get_u16(val_attr
),
2447 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2449 print_uint(PRINT_ANY
, "value", " value %u",
2450 mnl_attr_get_u16(val_attr
));
2455 err
= param_val_conv_str_get(param_val_conv
,
2458 mnl_attr_get_u32(val_attr
),
2462 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2464 print_uint(PRINT_ANY
, "value", " value %u",
2465 mnl_attr_get_u32(val_attr
));
2468 case MNL_TYPE_STRING
:
2469 print_string(PRINT_ANY
, "value", " value %s",
2470 mnl_attr_get_str(val_attr
));
2473 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2478 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2480 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2481 struct nlattr
*param_value_attr
;
2482 const char *nla_name
;
2486 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2487 if (err
!= MNL_CB_OK
)
2489 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2490 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2491 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2495 pr_out_handle_start_arr(dl
, tb
);
2497 __pr_out_handle_start(dl
, tb
, true, false);
2499 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2501 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2502 check_indent_newline(dl
);
2503 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2504 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2505 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2507 print_string(PRINT_ANY
, "type", "type %s", "generic");
2509 pr_out_array_start(dl
, "values");
2510 mnl_attr_for_each_nested(param_value_attr
,
2511 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2512 pr_out_entry_start(dl
);
2513 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2514 pr_out_entry_end(dl
);
2516 pr_out_array_end(dl
);
2517 pr_out_handle_end(dl
);
2520 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2522 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2523 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2524 struct dl
*dl
= data
;
2526 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2527 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2528 !tb
[DEVLINK_ATTR_PARAM
])
2529 return MNL_CB_ERROR
;
2530 pr_out_param(dl
, tb
, true);
2546 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2548 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2549 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2550 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2551 struct nlattr
*param_value_attr
;
2552 enum devlink_param_cmode cmode
;
2553 struct param_ctx
*ctx
= data
;
2554 struct dl
*dl
= ctx
->dl
;
2558 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2559 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2560 !tb
[DEVLINK_ATTR_PARAM
])
2561 return MNL_CB_ERROR
;
2563 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2564 if (err
!= MNL_CB_OK
)
2565 return MNL_CB_ERROR
;
2567 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2568 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2569 return MNL_CB_ERROR
;
2571 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2572 mnl_attr_for_each_nested(param_value_attr
,
2573 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2574 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2575 struct nlattr
*val_attr
;
2577 err
= mnl_attr_parse_nested(param_value_attr
,
2578 attr_cb
, nla_value
);
2579 if (err
!= MNL_CB_OK
)
2580 return MNL_CB_ERROR
;
2582 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2583 (nla_type
!= MNL_TYPE_FLAG
&&
2584 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2585 return MNL_CB_ERROR
;
2587 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2588 if (cmode
== dl
->opts
.cmode
) {
2589 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2592 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2595 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2598 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2600 case MNL_TYPE_STRING
:
2601 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2604 ctx
->value
.vbool
= val_attr
? true : false;
2610 ctx
->nla_type
= nla_type
;
2614 static int cmd_dev_param_set(struct dl
*dl
)
2616 struct param_ctx ctx
= {};
2617 struct nlmsghdr
*nlh
;
2625 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2627 DL_OPT_PARAM_VALUE
|
2628 DL_OPT_PARAM_CMODE
, 0);
2632 /* Get value type */
2633 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2634 NLM_F_REQUEST
| NLM_F_ACK
);
2635 dl_opts_put(nlh
, dl
);
2638 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2642 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2643 NLM_F_REQUEST
| NLM_F_ACK
);
2644 dl_opts_put(nlh
, dl
);
2646 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2647 dl
->opts
.param_name
);
2649 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2650 switch (ctx
.nla_type
) {
2653 err
= param_val_conv_uint_get(param_val_conv
,
2655 dl
->opts
.param_name
,
2656 dl
->opts
.param_value
,
2660 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2663 goto err_param_value_parse
;
2664 if (val_u8
== ctx
.value
.vu8
)
2666 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2670 err
= param_val_conv_uint_get(param_val_conv
,
2672 dl
->opts
.param_name
,
2673 dl
->opts
.param_value
,
2677 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2680 goto err_param_value_parse
;
2681 if (val_u16
== ctx
.value
.vu16
)
2683 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2687 err
= param_val_conv_uint_get(param_val_conv
,
2689 dl
->opts
.param_name
,
2690 dl
->opts
.param_value
,
2693 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2695 goto err_param_value_parse
;
2696 if (val_u32
== ctx
.value
.vu32
)
2698 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2701 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2703 goto err_param_value_parse
;
2704 if (val_bool
== ctx
.value
.vbool
)
2707 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2710 case MNL_TYPE_STRING
:
2711 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2712 dl
->opts
.param_value
);
2713 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2717 printf("Value type not supported\n");
2720 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2722 err_param_value_parse
:
2723 pr_err("Value \"%s\" is not a number or not within range\n",
2724 dl
->opts
.param_value
);
2728 static int cmd_dev_param_show(struct dl
*dl
)
2730 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2731 struct nlmsghdr
*nlh
;
2734 if (dl_argc(dl
) == 0)
2735 flags
|= NLM_F_DUMP
;
2737 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2739 if (dl_argc(dl
) > 0) {
2740 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2741 DL_OPT_PARAM_NAME
, 0);
2746 pr_out_section_start(dl
, "param");
2747 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2748 pr_out_section_end(dl
);
2752 static int cmd_dev_param(struct dl
*dl
)
2754 if (dl_argv_match(dl
, "help")) {
2757 } else if (dl_argv_match(dl
, "show") ||
2758 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2760 return cmd_dev_param_show(dl
);
2761 } else if (dl_argv_match(dl
, "set")) {
2763 return cmd_dev_param_set(dl
);
2765 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2768 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2770 struct dl
*dl
= data
;
2771 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2772 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2773 uint8_t reload_failed
= 0;
2775 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2776 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2777 return MNL_CB_ERROR
;
2779 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2780 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2782 if (reload_failed
) {
2783 __pr_out_handle_start(dl
, tb
, true, false);
2784 check_indent_newline(dl
);
2785 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2786 pr_out_handle_end(dl
);
2788 pr_out_handle(dl
, tb
);
2794 static int cmd_dev_show(struct dl
*dl
)
2796 struct nlmsghdr
*nlh
;
2797 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2800 if (dl_argc(dl
) == 0)
2801 flags
|= NLM_F_DUMP
;
2803 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2805 if (dl_argc(dl
) > 0) {
2806 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2811 pr_out_section_start(dl
, "dev");
2812 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2813 pr_out_section_end(dl
);
2817 static int cmd_dev_reload(struct dl
*dl
)
2819 struct nlmsghdr
*nlh
;
2822 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2827 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2828 NLM_F_REQUEST
| NLM_F_ACK
);
2830 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2834 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2837 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2838 const char *name
, int type
)
2840 struct nlattr
*version
;
2842 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2843 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2844 const char *ver_value
;
2845 const char *ver_name
;
2848 if (mnl_attr_get_type(version
) != type
)
2851 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2852 if (err
!= MNL_CB_OK
)
2855 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2856 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2860 pr_out_object_start(dl
, name
);
2864 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2865 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2867 check_indent_newline(dl
);
2868 print_string_name_value(ver_name
, ver_value
);
2869 if (!dl
->json_output
)
2874 pr_out_object_end(dl
);
2877 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2878 struct nlattr
**tb
, bool has_versions
)
2880 __pr_out_handle_start(dl
, tb
, true, false);
2882 __pr_out_indent_inc();
2883 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2884 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2886 if (!dl
->json_output
)
2888 check_indent_newline(dl
);
2889 print_string(PRINT_ANY
, "driver", "driver %s",
2890 mnl_attr_get_str(nla_drv
));
2893 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2894 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2896 if (!dl
->json_output
)
2898 check_indent_newline(dl
);
2899 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2900 mnl_attr_get_str(nla_sn
));
2902 __pr_out_indent_dec();
2905 pr_out_object_start(dl
, "versions");
2907 pr_out_versions_single(dl
, nlh
, "fixed",
2908 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2909 pr_out_versions_single(dl
, nlh
, "running",
2910 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2911 pr_out_versions_single(dl
, nlh
, "stored",
2912 DEVLINK_ATTR_INFO_VERSION_STORED
);
2914 pr_out_object_end(dl
);
2917 pr_out_handle_end(dl
);
2920 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2922 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2923 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2924 bool has_versions
, has_info
;
2925 struct dl
*dl
= data
;
2927 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2929 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2930 return MNL_CB_ERROR
;
2932 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2933 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2934 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2935 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2936 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2940 pr_out_info(dl
, nlh
, tb
, has_versions
);
2945 static int cmd_dev_info(struct dl
*dl
)
2947 struct nlmsghdr
*nlh
;
2948 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2951 if (dl_argv_match(dl
, "help")) {
2956 if (dl_argc(dl
) == 0)
2957 flags
|= NLM_F_DUMP
;
2959 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2961 if (dl_argc(dl
) > 0) {
2962 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2967 pr_out_section_start(dl
, "info");
2968 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2969 pr_out_section_end(dl
);
2973 struct cmd_dev_flash_status_ctx
{
2976 char *last_component
;
2977 uint8_t not_first
:1,
2983 static int nullstrcmp(const char *str1
, const char *str2
)
2986 return strcmp(str1
, str2
);
2989 return str1
? 1 : -1;
2992 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2994 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2995 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2996 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2997 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2998 const char *component
= NULL
;
2999 uint64_t done
= 0, total
= 0;
3000 const char *msg
= NULL
;
3001 const char *bus_name
;
3002 const char *dev_name
;
3004 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
3005 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3008 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3009 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3010 return MNL_CB_ERROR
;
3011 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3012 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3013 if (strcmp(bus_name
, opts
->bus_name
) ||
3014 strcmp(dev_name
, opts
->dev_name
))
3015 return MNL_CB_ERROR
;
3017 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3019 free(ctx
->last_msg
);
3020 free(ctx
->last_component
);
3021 ctx
->received_end
= 1;
3025 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3026 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3027 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3028 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3029 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3030 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3031 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3032 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3034 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3035 !nullstrcmp(component
, ctx
->last_component
) &&
3036 ctx
->last_pc
&& ctx
->not_first
) {
3037 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3042 pr_out("[%s] ", component
);
3043 free(ctx
->last_component
);
3044 ctx
->last_component
= strdup(component
);
3048 free(ctx
->last_msg
);
3049 ctx
->last_msg
= strdup(msg
);
3053 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3064 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3065 struct mnlg_socket
*nlg_ntf
,
3068 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3075 for (i
= 0; i
< 3; i
++)
3077 FD_SET(pipe_r
, &fds
[0]);
3079 FD_SET(nlfd
, &fds
[0]);
3083 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3086 pr_err("select() failed\n");
3089 if (FD_ISSET(nlfd
, &fds
[0])) {
3090 err
= _mnlg_socket_recv_run(nlg_ntf
,
3091 cmd_dev_flash_status_cb
, ctx
);
3095 if (FD_ISSET(pipe_r
, &fds
[0])) {
3096 err
= read(pipe_r
, &err2
, sizeof(err2
));
3098 pr_err("Failed to read pipe\n");
3103 ctx
->flash_done
= 1;
3109 static int cmd_dev_flash(struct dl
*dl
)
3111 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3112 struct mnlg_socket
*nlg_ntf
;
3113 struct nlmsghdr
*nlh
;
3119 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3124 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3125 NLM_F_REQUEST
| NLM_F_ACK
);
3127 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3128 DL_OPT_FLASH_COMPONENT
);
3132 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3136 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3140 err
= pipe(pipe_fds
);
3143 pipe_r
= pipe_fds
[0];
3144 pipe_w
= pipe_fds
[1];
3152 /* In child, just execute the flash and pass returned
3153 * value through pipe once it is done.
3158 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3159 cc
= write(pipe_w
, &err
, sizeof(err
));
3161 exit(cc
!= sizeof(err
));
3166 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3169 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3171 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3174 mnlg_socket_close(nlg_ntf
);
3178 static int cmd_dev(struct dl
*dl
)
3180 if (dl_argv_match(dl
, "help")) {
3183 } else if (dl_argv_match(dl
, "show") ||
3184 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3186 return cmd_dev_show(dl
);
3187 } else if (dl_argv_match(dl
, "eswitch")) {
3189 return cmd_dev_eswitch(dl
);
3190 } else if (dl_argv_match(dl
, "reload")) {
3192 return cmd_dev_reload(dl
);
3193 } else if (dl_argv_match(dl
, "param")) {
3195 return cmd_dev_param(dl
);
3196 } else if (dl_argv_match(dl
, "info")) {
3198 return cmd_dev_info(dl
);
3199 } else if (dl_argv_match(dl
, "flash")) {
3201 return cmd_dev_flash(dl
);
3203 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3207 static void cmd_port_help(void)
3209 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3210 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3211 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3212 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3215 static const char *port_type_name(uint32_t type
)
3218 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3219 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3220 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3221 case DEVLINK_PORT_TYPE_IB
: return "ib";
3222 default: return "<unknown type>";
3226 static const char *port_flavour_name(uint16_t flavour
)
3229 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3231 case DEVLINK_PORT_FLAVOUR_CPU
:
3233 case DEVLINK_PORT_FLAVOUR_DSA
:
3235 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3237 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3239 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3242 return "<unknown flavour>";
3246 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3250 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3251 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3252 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3254 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3255 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3256 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3260 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3262 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3263 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3265 pr_out_port_handle_start(dl
, tb
, false);
3266 check_indent_newline(dl
);
3268 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3270 print_string(PRINT_ANY
, "type", "type %s",
3271 port_type_name(port_type
));
3273 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3275 if (port_type
!= des_port_type
)
3276 print_string(PRINT_ANY
, "des_type", " des_type %s",
3277 port_type_name(des_port_type
));
3280 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3281 print_string(PRINT_ANY
, "netdev", " netdev %s",
3282 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3284 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3285 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3286 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3288 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3289 uint16_t port_flavour
=
3290 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3292 print_string(PRINT_ANY
, "flavour", " flavour %s",
3293 port_flavour_name(port_flavour
));
3295 switch (port_flavour
) {
3296 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3297 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3298 pr_out_port_pfvf_num(dl
, tb
);
3304 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3305 uint32_t port_number
;
3307 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3308 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3310 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3311 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3312 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3313 pr_out_port_handle_end(dl
);
3316 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3318 struct dl
*dl
= data
;
3319 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3320 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3322 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3323 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3324 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3325 return MNL_CB_ERROR
;
3326 pr_out_port(dl
, tb
);
3330 static int cmd_port_show(struct dl
*dl
)
3332 struct nlmsghdr
*nlh
;
3333 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3336 if (dl_argc(dl
) == 0)
3337 flags
|= NLM_F_DUMP
;
3339 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3341 if (dl_argc(dl
) > 0) {
3342 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3347 pr_out_section_start(dl
, "port");
3348 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3349 pr_out_section_end(dl
);
3353 static int cmd_port_set(struct dl
*dl
)
3355 struct nlmsghdr
*nlh
;
3358 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3359 NLM_F_REQUEST
| NLM_F_ACK
);
3361 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3365 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3368 static int cmd_port_split(struct dl
*dl
)
3370 struct nlmsghdr
*nlh
;
3373 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3374 NLM_F_REQUEST
| NLM_F_ACK
);
3376 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3380 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3383 static int cmd_port_unsplit(struct dl
*dl
)
3385 struct nlmsghdr
*nlh
;
3388 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3389 NLM_F_REQUEST
| NLM_F_ACK
);
3391 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3395 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3398 static int cmd_port(struct dl
*dl
)
3400 if (dl_argv_match(dl
, "help")) {
3403 } else if (dl_argv_match(dl
, "show") ||
3404 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3406 return cmd_port_show(dl
);
3407 } else if (dl_argv_match(dl
, "set")) {
3409 return cmd_port_set(dl
);
3410 } else if (dl_argv_match(dl
, "split")) {
3412 return cmd_port_split(dl
);
3413 } else if (dl_argv_match(dl
, "unsplit")) {
3415 return cmd_port_unsplit(dl
);
3417 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3421 static void cmd_sb_help(void)
3423 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3424 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3425 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3426 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3427 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3428 pr_err(" pool POOL_INDEX ]\n");
3429 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3430 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3431 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3432 pr_err(" type { ingress | egress } ]\n");
3433 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3434 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3435 pr_err(" th THRESHOLD\n");
3436 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3437 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3438 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3441 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3443 pr_out_handle_start_arr(dl
, tb
);
3444 check_indent_newline(dl
);
3445 print_uint(PRINT_ANY
, "sb", "sb %u",
3446 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3447 print_uint(PRINT_ANY
, "size", " size %u",
3448 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3449 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3450 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3451 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3452 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3453 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3454 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3455 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3456 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3457 pr_out_handle_end(dl
);
3460 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3462 struct dl
*dl
= data
;
3463 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3464 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3466 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3467 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3468 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3469 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3470 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3471 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3472 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3473 return MNL_CB_ERROR
;
3478 static int cmd_sb_show(struct dl
*dl
)
3480 struct nlmsghdr
*nlh
;
3481 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3484 if (dl_argc(dl
) == 0)
3485 flags
|= NLM_F_DUMP
;
3487 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3489 if (dl_argc(dl
) > 0) {
3490 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3495 pr_out_section_start(dl
, "sb");
3496 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3497 pr_out_section_end(dl
);
3501 static const char *pool_type_name(uint8_t type
)
3504 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3505 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3506 default: return "<unknown type>";
3510 static const char *threshold_type_name(uint8_t type
)
3513 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3514 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3515 default: return "<unknown type>";
3519 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3521 pr_out_handle_start_arr(dl
, tb
);
3522 check_indent_newline(dl
);
3523 print_uint(PRINT_ANY
, "sb", "sb %u",
3524 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3525 print_uint(PRINT_ANY
, "pool", " pool %u",
3526 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3527 print_string(PRINT_ANY
, "type", " type %s",
3528 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3529 print_uint(PRINT_ANY
, "size", " size %u",
3530 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3531 print_string(PRINT_ANY
, "thtype", " thtype %s",
3532 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3533 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3534 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3535 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3536 pr_out_handle_end(dl
);
3539 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3541 struct dl
*dl
= data
;
3542 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3543 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3545 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3546 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3547 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3548 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3549 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3550 return MNL_CB_ERROR
;
3551 pr_out_sb_pool(dl
, tb
);
3555 static int cmd_sb_pool_show(struct dl
*dl
)
3557 struct nlmsghdr
*nlh
;
3558 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3561 if (dl_argc(dl
) == 0)
3562 flags
|= NLM_F_DUMP
;
3564 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3566 if (dl_argc(dl
) > 0) {
3567 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3573 pr_out_section_start(dl
, "pool");
3574 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3575 pr_out_section_end(dl
);
3579 static int cmd_sb_pool_set(struct dl
*dl
)
3581 struct nlmsghdr
*nlh
;
3584 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3585 NLM_F_REQUEST
| NLM_F_ACK
);
3587 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3588 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3592 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3595 static int cmd_sb_pool(struct dl
*dl
)
3597 if (dl_argv_match(dl
, "help")) {
3600 } else if (dl_argv_match(dl
, "show") ||
3601 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3603 return cmd_sb_pool_show(dl
);
3604 } else if (dl_argv_match(dl
, "set")) {
3606 return cmd_sb_pool_set(dl
);
3608 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3612 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3614 pr_out_port_handle_start_arr(dl
, tb
, true);
3615 check_indent_newline(dl
);
3616 print_uint(PRINT_ANY
, "sb", "sb %u",
3617 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3618 print_uint(PRINT_ANY
, "pool", " pool %u",
3619 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3620 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3621 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3622 pr_out_port_handle_end(dl
);
3625 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3627 struct dl
*dl
= data
;
3628 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3629 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3631 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3632 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3633 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3634 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3635 return MNL_CB_ERROR
;
3636 pr_out_sb_port_pool(dl
, tb
);
3640 static int cmd_sb_port_pool_show(struct dl
*dl
)
3642 struct nlmsghdr
*nlh
;
3643 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3646 if (dl_argc(dl
) == 0)
3647 flags
|= NLM_F_DUMP
;
3649 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3651 if (dl_argc(dl
) > 0) {
3652 err
= dl_argv_parse_put(nlh
, dl
,
3653 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3659 pr_out_section_start(dl
, "port_pool");
3660 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3661 pr_out_section_end(dl
);
3665 static int cmd_sb_port_pool_set(struct dl
*dl
)
3667 struct nlmsghdr
*nlh
;
3670 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3671 NLM_F_REQUEST
| NLM_F_ACK
);
3673 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3674 DL_OPT_SB_TH
, DL_OPT_SB
);
3678 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3681 static int cmd_sb_port_pool(struct dl
*dl
)
3683 if (dl_argv_match(dl
, "help")) {
3686 } else if (dl_argv_match(dl
, "show") ||
3687 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3689 return cmd_sb_port_pool_show(dl
);
3690 } else if (dl_argv_match(dl
, "set")) {
3692 return cmd_sb_port_pool_set(dl
);
3694 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3698 static int cmd_sb_port(struct dl
*dl
)
3700 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3703 } else if (dl_argv_match(dl
, "pool")) {
3705 return cmd_sb_port_pool(dl
);
3707 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3711 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3713 pr_out_port_handle_start_arr(dl
, tb
, true);
3714 check_indent_newline(dl
);
3715 print_uint(PRINT_ANY
, "sb", "sb %u",
3716 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3717 print_uint(PRINT_ANY
, "tc", " tc %u",
3718 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3719 print_string(PRINT_ANY
, "type", " type %s",
3720 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3721 print_uint(PRINT_ANY
, "pool", " pool %u",
3722 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3723 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3724 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3725 pr_out_port_handle_end(dl
);
3728 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3730 struct dl
*dl
= data
;
3731 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3732 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3734 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3735 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3736 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3737 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3738 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3739 return MNL_CB_ERROR
;
3740 pr_out_sb_tc_bind(dl
, tb
);
3744 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3746 struct nlmsghdr
*nlh
;
3747 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3750 if (dl_argc(dl
) == 0)
3751 flags
|= NLM_F_DUMP
;
3753 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3755 if (dl_argc(dl
) > 0) {
3756 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3757 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3762 pr_out_section_start(dl
, "tc_bind");
3763 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3764 pr_out_section_end(dl
);
3768 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3770 struct nlmsghdr
*nlh
;
3773 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3774 NLM_F_REQUEST
| NLM_F_ACK
);
3776 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3777 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3782 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3785 static int cmd_sb_tc_bind(struct dl
*dl
)
3787 if (dl_argv_match(dl
, "help")) {
3790 } else if (dl_argv_match(dl
, "show") ||
3791 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3793 return cmd_sb_tc_bind_show(dl
);
3794 } else if (dl_argv_match(dl
, "set")) {
3796 return cmd_sb_tc_bind_set(dl
);
3798 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3802 static int cmd_sb_tc(struct dl
*dl
)
3804 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3807 } else if (dl_argv_match(dl
, "bind")) {
3809 return cmd_sb_tc_bind(dl
);
3811 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3816 struct list_head list
;
3820 uint32_t bound_pool_index
;
3824 struct list_head list
;
3827 uint32_t port_index
;
3829 struct list_head pool_list
;
3830 struct list_head ing_tc_list
;
3831 struct list_head eg_tc_list
;
3837 struct list_head port_list
;
3840 static struct occ_item
*occ_item_alloc(void)
3842 return calloc(1, sizeof(struct occ_item
));
3845 static void occ_item_free(struct occ_item
*occ_item
)
3850 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3852 struct occ_port
*occ_port
;
3854 occ_port
= calloc(1, sizeof(*occ_port
));
3857 occ_port
->port_index
= port_index
;
3858 INIT_LIST_HEAD(&occ_port
->pool_list
);
3859 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3860 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3864 static void occ_port_free(struct occ_port
*occ_port
)
3866 struct occ_item
*occ_item
, *tmp
;
3868 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3869 occ_item_free(occ_item
);
3870 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3871 occ_item_free(occ_item
);
3872 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3873 occ_item_free(occ_item
);
3876 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3878 struct occ_show
*occ_show
;
3880 occ_show
= calloc(1, sizeof(*occ_show
));
3884 INIT_LIST_HEAD(&occ_show
->port_list
);
3888 static void occ_show_free(struct occ_show
*occ_show
)
3890 struct occ_port
*occ_port
, *tmp
;
3892 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3893 occ_port_free(occ_port
);
3896 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3899 struct occ_port
*occ_port
;
3900 uint32_t port_index
;
3902 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3904 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3905 if (occ_port
->port_index
== port_index
)
3908 occ_port
= occ_port_alloc(port_index
);
3911 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3915 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3918 struct occ_item
*occ_item
;
3921 pr_out_sp(7, " %s:", label
);
3922 list_for_each_entry(occ_item
, list
, list
) {
3923 if ((i
- 1) % 4 == 0 && i
!= 1)
3926 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3927 occ_item
->bound_pool_index
);
3929 pr_out_sp(7, "%2u:", occ_item
->index
);
3930 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3934 if ((i
- 1) % 4 != 0)
3938 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3939 struct list_head
*list
,
3942 struct occ_item
*occ_item
;
3945 open_json_object(label
);
3946 list_for_each_entry(occ_item
, list
, list
) {
3947 sprintf(buf
, "%u", occ_item
->index
);
3948 open_json_object(buf
);
3950 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3951 occ_item
->bound_pool_index
);
3952 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3953 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3954 close_json_object();
3956 close_json_object();
3959 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3961 if (dl
->json_output
) {
3962 pr_out_json_occ_show_item_list(dl
, "pool",
3963 &occ_port
->pool_list
, false);
3964 pr_out_json_occ_show_item_list(dl
, "itc",
3965 &occ_port
->ing_tc_list
, true);
3966 pr_out_json_occ_show_item_list(dl
, "etc",
3967 &occ_port
->eg_tc_list
, true);
3970 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3971 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3972 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3976 static void pr_out_occ_show(struct occ_show
*occ_show
)
3978 struct dl
*dl
= occ_show
->dl
;
3979 struct dl_opts
*opts
= &dl
->opts
;
3980 struct occ_port
*occ_port
;
3982 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3983 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3984 occ_port
->port_index
, true, false);
3985 pr_out_occ_show_port(dl
, occ_port
);
3986 pr_out_port_handle_end(dl
);
3990 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3993 struct occ_port
*occ_port
;
3994 struct occ_item
*occ_item
;
3996 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3999 occ_port
= occ_port_get(occ_show
, tb
);
4001 occ_show
->err
= -ENOMEM
;
4005 occ_item
= occ_item_alloc();
4007 occ_show
->err
= -ENOMEM
;
4010 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4011 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4012 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4013 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4016 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4018 struct occ_show
*occ_show
= data
;
4019 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4020 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4022 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4023 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4024 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4025 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4026 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4027 return MNL_CB_ERROR
;
4028 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4032 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4035 struct occ_port
*occ_port
;
4036 struct occ_item
*occ_item
;
4039 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4042 occ_port
= occ_port_get(occ_show
, tb
);
4044 occ_show
->err
= -ENOMEM
;
4048 occ_item
= occ_item_alloc();
4050 occ_show
->err
= -ENOMEM
;
4053 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4054 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4055 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4056 occ_item
->bound_pool_index
=
4057 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4058 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4059 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4060 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4061 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4062 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4064 occ_item_free(occ_item
);
4067 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4069 struct occ_show
*occ_show
= data
;
4070 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4071 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4073 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4074 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4075 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4076 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4077 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4078 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4079 return MNL_CB_ERROR
;
4080 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4084 static int cmd_sb_occ_show(struct dl
*dl
)
4086 struct nlmsghdr
*nlh
;
4087 struct occ_show
*occ_show
;
4088 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4091 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4095 occ_show
= occ_show_alloc(dl
);
4099 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4101 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4102 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4106 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4108 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4109 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4113 pr_out_section_start(dl
, "occupancy");
4114 pr_out_occ_show(occ_show
);
4115 pr_out_section_end(dl
);
4118 occ_show_free(occ_show
);
4122 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4124 struct nlmsghdr
*nlh
;
4127 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4128 NLM_F_REQUEST
| NLM_F_ACK
);
4130 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4134 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4137 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4139 struct nlmsghdr
*nlh
;
4142 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4143 NLM_F_REQUEST
| NLM_F_ACK
);
4145 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4149 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4152 static int cmd_sb_occ(struct dl
*dl
)
4154 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4157 } else if (dl_argv_match(dl
, "show") ||
4158 dl_argv_match(dl
, "list")) {
4160 return cmd_sb_occ_show(dl
);
4161 } else if (dl_argv_match(dl
, "snapshot")) {
4163 return cmd_sb_occ_snapshot(dl
);
4164 } else if (dl_argv_match(dl
, "clearmax")) {
4166 return cmd_sb_occ_clearmax(dl
);
4168 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4172 static int cmd_sb(struct dl
*dl
)
4174 if (dl_argv_match(dl
, "help")) {
4177 } else if (dl_argv_match(dl
, "show") ||
4178 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4180 return cmd_sb_show(dl
);
4181 } else if (dl_argv_match(dl
, "pool")) {
4183 return cmd_sb_pool(dl
);
4184 } else if (dl_argv_match(dl
, "port")) {
4186 return cmd_sb_port(dl
);
4187 } else if (dl_argv_match(dl
, "tc")) {
4189 return cmd_sb_tc(dl
);
4190 } else if (dl_argv_match(dl
, "occupancy")) {
4192 return cmd_sb_occ(dl
);
4194 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4198 static const char *cmd_name(uint8_t cmd
)
4201 case DEVLINK_CMD_UNSPEC
: return "unspec";
4202 case DEVLINK_CMD_GET
: return "get";
4203 case DEVLINK_CMD_SET
: return "set";
4204 case DEVLINK_CMD_NEW
: return "new";
4205 case DEVLINK_CMD_DEL
: return "del";
4206 case DEVLINK_CMD_PORT_GET
: return "get";
4207 case DEVLINK_CMD_PORT_SET
: return "set";
4208 case DEVLINK_CMD_PORT_NEW
: return "new";
4209 case DEVLINK_CMD_PORT_DEL
: return "del";
4210 case DEVLINK_CMD_PARAM_GET
: return "get";
4211 case DEVLINK_CMD_PARAM_SET
: return "set";
4212 case DEVLINK_CMD_PARAM_NEW
: return "new";
4213 case DEVLINK_CMD_PARAM_DEL
: return "del";
4214 case DEVLINK_CMD_REGION_GET
: return "get";
4215 case DEVLINK_CMD_REGION_SET
: return "set";
4216 case DEVLINK_CMD_REGION_NEW
: return "new";
4217 case DEVLINK_CMD_REGION_DEL
: return "del";
4218 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4219 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4220 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4221 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4222 case DEVLINK_CMD_TRAP_GET
: return "get";
4223 case DEVLINK_CMD_TRAP_SET
: return "set";
4224 case DEVLINK_CMD_TRAP_NEW
: return "new";
4225 case DEVLINK_CMD_TRAP_DEL
: return "del";
4226 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4227 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4228 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4229 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4230 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4231 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4232 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4233 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4234 default: return "<unknown cmd>";
4238 static const char *cmd_obj(uint8_t cmd
)
4241 case DEVLINK_CMD_UNSPEC
: return "unspec";
4242 case DEVLINK_CMD_GET
:
4243 case DEVLINK_CMD_SET
:
4244 case DEVLINK_CMD_NEW
:
4245 case DEVLINK_CMD_DEL
:
4247 case DEVLINK_CMD_PORT_GET
:
4248 case DEVLINK_CMD_PORT_SET
:
4249 case DEVLINK_CMD_PORT_NEW
:
4250 case DEVLINK_CMD_PORT_DEL
:
4252 case DEVLINK_CMD_PARAM_GET
:
4253 case DEVLINK_CMD_PARAM_SET
:
4254 case DEVLINK_CMD_PARAM_NEW
:
4255 case DEVLINK_CMD_PARAM_DEL
:
4257 case DEVLINK_CMD_REGION_GET
:
4258 case DEVLINK_CMD_REGION_SET
:
4259 case DEVLINK_CMD_REGION_NEW
:
4260 case DEVLINK_CMD_REGION_DEL
:
4262 case DEVLINK_CMD_FLASH_UPDATE
:
4263 case DEVLINK_CMD_FLASH_UPDATE_END
:
4264 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4266 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4268 case DEVLINK_CMD_TRAP_GET
:
4269 case DEVLINK_CMD_TRAP_SET
:
4270 case DEVLINK_CMD_TRAP_NEW
:
4271 case DEVLINK_CMD_TRAP_DEL
:
4273 case DEVLINK_CMD_TRAP_GROUP_GET
:
4274 case DEVLINK_CMD_TRAP_GROUP_SET
:
4275 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4276 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4277 return "trap-group";
4278 case DEVLINK_CMD_TRAP_POLICER_GET
:
4279 case DEVLINK_CMD_TRAP_POLICER_SET
:
4280 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4281 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4282 return "trap-policer";
4283 default: return "<unknown obj>";
4287 static void pr_out_mon_header(uint8_t cmd
)
4289 if (!is_json_context()) {
4290 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4292 open_json_object(NULL
);
4293 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4294 open_json_object(cmd_obj(cmd
));
4298 static void pr_out_mon_footer(void)
4300 if (is_json_context()) {
4301 close_json_object();
4302 close_json_object();
4306 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4308 const char *obj
= cmd_obj(cmd
);
4309 unsigned int index
= 0;
4310 const char *cur_obj
;
4314 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4315 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4321 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4323 __pr_out_handle_start(dl
, tb
, true, false);
4325 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4326 check_indent_newline(dl
);
4327 print_string(PRINT_ANY
, "msg", "msg %s",
4328 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4330 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4331 check_indent_newline(dl
);
4332 print_string(PRINT_ANY
, "component", "component %s",
4333 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4336 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4337 pr_out_u64(dl
, "done",
4338 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4340 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4341 pr_out_u64(dl
, "total",
4342 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4344 pr_out_handle_end(dl
);
4347 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4348 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4349 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4350 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4351 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4353 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4355 struct dl
*dl
= data
;
4356 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4357 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4358 uint8_t cmd
= genl
->cmd
;
4360 if (!cmd_filter_check(dl
, cmd
))
4364 case DEVLINK_CMD_GET
: /* fall through */
4365 case DEVLINK_CMD_SET
: /* fall through */
4366 case DEVLINK_CMD_NEW
: /* fall through */
4367 case DEVLINK_CMD_DEL
:
4368 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4369 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4370 return MNL_CB_ERROR
;
4371 pr_out_mon_header(genl
->cmd
);
4372 pr_out_handle(dl
, tb
);
4373 pr_out_mon_footer();
4375 case DEVLINK_CMD_PORT_GET
: /* fall through */
4376 case DEVLINK_CMD_PORT_SET
: /* fall through */
4377 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4378 case DEVLINK_CMD_PORT_DEL
:
4379 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4380 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4381 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4382 return MNL_CB_ERROR
;
4383 pr_out_mon_header(genl
->cmd
);
4384 pr_out_port(dl
, tb
);
4385 pr_out_mon_footer();
4387 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4388 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4389 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4390 case DEVLINK_CMD_PARAM_DEL
:
4391 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4392 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4393 !tb
[DEVLINK_ATTR_PARAM
])
4394 return MNL_CB_ERROR
;
4395 pr_out_mon_header(genl
->cmd
);
4396 pr_out_param(dl
, tb
, false);
4397 pr_out_mon_footer();
4399 case DEVLINK_CMD_REGION_GET
: /* fall through */
4400 case DEVLINK_CMD_REGION_SET
: /* fall through */
4401 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4402 case DEVLINK_CMD_REGION_DEL
:
4403 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4404 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4405 !tb
[DEVLINK_ATTR_REGION_NAME
])
4406 return MNL_CB_ERROR
;
4407 pr_out_mon_header(genl
->cmd
);
4408 pr_out_region(dl
, tb
);
4409 pr_out_mon_footer();
4411 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4412 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4413 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4414 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4415 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4416 return MNL_CB_ERROR
;
4417 pr_out_mon_header(genl
->cmd
);
4418 pr_out_flash_update(dl
, tb
);
4419 pr_out_mon_footer();
4421 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4422 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4423 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4424 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
4425 return MNL_CB_ERROR
;
4426 pr_out_mon_header(genl
->cmd
);
4427 pr_out_health(dl
, tb
);
4428 pr_out_mon_footer();
4430 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4431 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4432 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4433 case DEVLINK_CMD_TRAP_DEL
:
4434 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4435 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4436 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4437 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4438 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4439 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4440 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4441 !tb
[DEVLINK_ATTR_STATS
])
4442 return MNL_CB_ERROR
;
4443 pr_out_mon_header(genl
->cmd
);
4444 pr_out_trap(dl
, tb
, false);
4445 pr_out_mon_footer();
4447 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4448 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4449 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4450 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4451 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4452 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4453 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4454 !tb
[DEVLINK_ATTR_STATS
])
4455 return MNL_CB_ERROR
;
4456 pr_out_mon_header(genl
->cmd
);
4457 pr_out_trap_group(dl
, tb
, false);
4458 pr_out_mon_footer();
4460 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4461 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4462 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4463 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4464 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4465 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4466 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4467 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4468 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4469 return MNL_CB_ERROR
;
4470 pr_out_mon_header(genl
->cmd
);
4471 pr_out_trap_policer(dl
, tb
, false);
4477 static int cmd_mon_show(struct dl
*dl
)
4480 unsigned int index
= 0;
4481 const char *cur_obj
;
4483 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4484 if (strcmp(cur_obj
, "all") != 0 &&
4485 strcmp(cur_obj
, "dev") != 0 &&
4486 strcmp(cur_obj
, "port") != 0 &&
4487 strcmp(cur_obj
, "health") != 0 &&
4488 strcmp(cur_obj
, "trap") != 0 &&
4489 strcmp(cur_obj
, "trap-group") != 0 &&
4490 strcmp(cur_obj
, "trap-policer") != 0) {
4491 pr_err("Unknown object \"%s\"\n", cur_obj
);
4495 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4498 open_json_object(NULL
);
4499 open_json_array(PRINT_JSON
, "mon");
4500 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
4501 close_json_array(PRINT_JSON
, NULL
);
4502 close_json_object();
4508 static void cmd_mon_help(void)
4510 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4511 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4514 static int cmd_mon(struct dl
*dl
)
4516 if (dl_argv_match(dl
, "help")) {
4520 return cmd_mon_show(dl
);
4523 struct dpipe_field
{
4526 unsigned int bitwidth
;
4527 enum devlink_dpipe_field_mapping_type mapping_type
;
4530 struct dpipe_header
{
4531 struct list_head list
;
4534 struct dpipe_field
*fields
;
4535 unsigned int fields_count
;
4538 struct dpipe_table
{
4539 struct list_head list
;
4541 unsigned int resource_id
;
4542 bool resource_valid
;
4545 struct dpipe_tables
{
4546 struct list_head table_list
;
4556 enum devlink_resource_unit unit
;
4561 struct list_head list
;
4562 struct list_head resource_list
;
4563 struct resource
*parent
;
4567 struct list_head resource_list
;
4570 struct resource_ctx
{
4573 struct resources
*resources
;
4574 struct dpipe_tables
*tables
;
4575 bool print_resources
;
4576 bool pending_change
;
4579 static struct resource
*resource_alloc(void)
4581 struct resource
*resource
;
4583 resource
= calloc(1, sizeof(struct resource
));
4586 INIT_LIST_HEAD(&resource
->resource_list
);
4590 static void resource_free(struct resource
*resource
)
4592 struct resource
*child_resource
, *tmp
;
4594 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4596 free(child_resource
->name
);
4597 resource_free(child_resource
);
4602 static struct resources
*resources_alloc(void)
4604 struct resources
*resources
;
4606 resources
= calloc(1, sizeof(struct resources
));
4609 INIT_LIST_HEAD(&resources
->resource_list
);
4613 static void resources_free(struct resources
*resources
)
4615 struct resource
*resource
, *tmp
;
4617 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4618 resource_free(resource
);
4621 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4623 ctx
->resources
= resources_alloc();
4624 if (!ctx
->resources
)
4630 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4632 resources_free(ctx
->resources
);
4638 struct list_head global_headers
;
4639 struct list_head local_headers
;
4640 struct dpipe_tables
*tables
;
4641 struct resources
*resources
;
4646 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4648 struct dpipe_header
*header
;
4650 header
= calloc(1, sizeof(struct dpipe_header
));
4653 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4654 if (!header
->fields
)
4655 goto err_fields_alloc
;
4656 header
->fields_count
= fields_count
;
4664 static void dpipe_header_free(struct dpipe_header
*header
)
4666 free(header
->fields
);
4670 static void dpipe_header_clear(struct dpipe_header
*header
)
4672 struct dpipe_field
*field
;
4675 for (i
= 0; i
< header
->fields_count
; i
++) {
4676 field
= &header
->fields
[i
];
4682 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4683 struct dpipe_header
*header
, bool global
)
4686 list_add(&header
->list
, &ctx
->global_headers
);
4688 list_add(&header
->list
, &ctx
->local_headers
);
4691 static void dpipe_header_del(struct dpipe_header
*header
)
4693 list_del(&header
->list
);
4696 static struct dpipe_table
*dpipe_table_alloc(void)
4698 return calloc(1, sizeof(struct dpipe_table
));
4701 static void dpipe_table_free(struct dpipe_table
*table
)
4706 static struct dpipe_tables
*dpipe_tables_alloc(void)
4708 struct dpipe_tables
*tables
;
4710 tables
= calloc(1, sizeof(struct dpipe_tables
));
4713 INIT_LIST_HEAD(&tables
->table_list
);
4717 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4719 struct dpipe_table
*table
, *tmp
;
4721 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4722 dpipe_table_free(table
);
4726 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4728 ctx
->tables
= dpipe_tables_alloc();
4733 INIT_LIST_HEAD(&ctx
->global_headers
);
4734 INIT_LIST_HEAD(&ctx
->local_headers
);
4738 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4740 struct dpipe_header
*header
, *tmp
;
4742 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4744 dpipe_header_del(header
);
4745 dpipe_header_clear(header
);
4746 dpipe_header_free(header
);
4748 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4750 dpipe_header_del(header
);
4751 dpipe_header_clear(header
);
4752 dpipe_header_free(header
);
4754 dpipe_tables_free(ctx
->tables
);
4757 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4758 uint32_t header_id
, bool global
)
4760 struct list_head
*header_list
;
4761 struct dpipe_header
*header
;
4764 header_list
= &ctx
->global_headers
;
4766 header_list
= &ctx
->local_headers
;
4767 list_for_each_entry(header
, header_list
, list
) {
4768 if (header
->id
!= header_id
)
4770 return header
->name
;
4775 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4777 uint32_t field_id
, bool global
)
4779 struct list_head
*header_list
;
4780 struct dpipe_header
*header
;
4783 header_list
= &ctx
->global_headers
;
4785 header_list
= &ctx
->local_headers
;
4786 list_for_each_entry(header
, header_list
, list
) {
4787 if (header
->id
!= header_id
)
4789 return header
->fields
[field_id
].name
;
4795 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4797 switch (mapping_type
) {
4798 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4800 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4808 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4809 uint32_t field_id
, bool global
)
4811 enum devlink_dpipe_field_mapping_type mapping_type
;
4812 struct list_head
*header_list
;
4813 struct dpipe_header
*header
;
4816 header_list
= &ctx
->global_headers
;
4818 header_list
= &ctx
->local_headers
;
4819 list_for_each_entry(header
, header_list
, list
) {
4820 if (header
->id
!= header_id
)
4822 mapping_type
= header
->fields
[field_id
].mapping_type
;
4823 return dpipe_field_mapping_e2s(mapping_type
);
4828 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4829 struct dpipe_field
*fields
,
4830 unsigned int field_count
)
4832 struct dpipe_field
*field
;
4835 for (i
= 0; i
< field_count
; i
++) {
4837 pr_out_entry_start(ctx
->dl
);
4838 check_indent_newline(ctx
->dl
);
4839 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4840 if (ctx
->dl
->verbose
)
4841 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4842 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4843 if (field
->mapping_type
) {
4844 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4845 dpipe_field_mapping_e2s(field
->mapping_type
));
4847 pr_out_entry_end(ctx
->dl
);
4852 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4853 struct dpipe_header
*header
, bool global
)
4855 pr_out_handle_start_arr(ctx
->dl
, tb
);
4856 check_indent_newline(ctx
->dl
);
4857 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4858 if (ctx
->dl
->verbose
) {
4859 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4860 print_bool(PRINT_ANY
, "global", " global %s", global
);
4862 pr_out_array_start(ctx
->dl
, "field");
4863 pr_out_dpipe_fields(ctx
, header
->fields
,
4864 header
->fields_count
);
4865 pr_out_array_end(ctx
->dl
);
4866 pr_out_handle_end(ctx
->dl
);
4869 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4872 struct dpipe_header
*header
;
4874 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4875 pr_out_dpipe_header(ctx
, tb
, header
, false);
4877 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4878 pr_out_dpipe_header(ctx
, tb
, header
, true);
4881 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4883 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4887 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4888 if (err
!= MNL_CB_OK
)
4890 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4891 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4892 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4893 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4896 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4897 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4898 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4899 field
->name
= strdup(name
);
4902 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4906 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4907 struct dpipe_field
*fields
)
4909 struct nlattr
*nla_field
;
4913 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4914 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4922 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4924 struct nlattr
*nla_field
;
4925 unsigned int count
= 0;
4927 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4932 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4934 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4935 struct dpipe_header
*header
;
4936 unsigned int fields_count
;
4937 const char *header_name
;
4941 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4942 if (err
!= MNL_CB_OK
)
4945 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4946 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4947 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4950 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4951 header
= dpipe_header_alloc(fields_count
);
4955 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4956 header
->name
= strdup(header_name
);
4957 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4958 header
->fields_count
= fields_count
;
4959 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4961 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4965 dpipe_header_add(ctx
, header
, global
);
4969 dpipe_header_free(header
);
4973 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4975 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4976 struct nlattr
*nla_header
;
4979 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4980 err
= dpipe_header_get(ctx
, nla_header
);
4987 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4989 struct dpipe_ctx
*ctx
= data
;
4990 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4991 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4994 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4995 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4996 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4997 return MNL_CB_ERROR
;
4998 err
= dpipe_headers_get(ctx
, tb
);
5001 return MNL_CB_ERROR
;
5004 if (ctx
->print_headers
)
5005 pr_out_dpipe_headers(ctx
, tb
);
5009 static int cmd_dpipe_headers_show(struct dl
*dl
)
5011 struct nlmsghdr
*nlh
;
5012 struct dpipe_ctx ctx
= {};
5013 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5016 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5018 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5022 err
= dpipe_ctx_init(&ctx
, dl
);
5026 ctx
.print_headers
= true;
5028 pr_out_section_start(dl
, "header");
5029 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5031 pr_err("error get headers %s\n", strerror(ctx
.err
));
5032 pr_out_section_end(dl
);
5034 dpipe_ctx_fini(&ctx
);
5038 static void cmd_dpipe_help(void)
5040 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5041 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5042 pr_err(" [ counters_enabled { true | false } ]\n");
5043 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5044 pr_err(" devlink dpipe header show DEV\n");
5047 static int cmd_dpipe_header(struct dl
*dl
)
5049 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5052 } else if (dl_argv_match(dl
, "show")) {
5054 return cmd_dpipe_headers_show(dl
);
5056 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5061 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5063 switch (action_type
) {
5064 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5065 return "field_modify";
5071 struct dpipe_op_info
{
5077 struct dpipe_action
{
5078 struct dpipe_op_info info
;
5082 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5083 struct dpipe_ctx
*ctx
)
5085 struct dpipe_op_info
*op_info
= &action
->info
;
5086 const char *mapping
;
5088 check_indent_newline(ctx
->dl
);
5089 print_string(PRINT_ANY
, "type", "type %s",
5090 dpipe_action_type_e2s(action
->type
));
5091 print_string(PRINT_ANY
, "header", " header %s",
5092 dpipe_header_id2s(ctx
, op_info
->header_id
,
5093 op_info
->header_global
));
5094 print_string(PRINT_ANY
, "field", " field %s",
5095 dpipe_field_id2s(ctx
, op_info
->header_id
,
5097 op_info
->header_global
));
5098 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5100 op_info
->header_global
);
5102 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5105 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5107 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5110 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5111 if (err
!= MNL_CB_OK
)
5114 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5115 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5116 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5117 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5121 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5122 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5123 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5124 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5129 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5130 struct nlattr
*nla_actions
)
5132 struct nlattr
*nla_action
;
5133 struct dpipe_action action
;
5135 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5136 pr_out_entry_start(ctx
->dl
);
5137 if (dpipe_action_parse(&action
, nla_action
))
5138 goto err_action_parse
;
5139 pr_out_dpipe_action(&action
, ctx
);
5140 pr_out_entry_end(ctx
->dl
);
5145 pr_out_entry_end(ctx
->dl
);
5150 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5152 switch (match_type
) {
5153 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5154 return "field_exact";
5160 struct dpipe_match
{
5161 struct dpipe_op_info info
;
5165 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5166 struct dpipe_ctx
*ctx
)
5168 struct dpipe_op_info
*op_info
= &match
->info
;
5169 const char *mapping
;
5171 check_indent_newline(ctx
->dl
);
5172 print_string(PRINT_ANY
, "type", "type %s",
5173 dpipe_match_type_e2s(match
->type
));
5174 print_string(PRINT_ANY
, "header", " header %s",
5175 dpipe_header_id2s(ctx
, op_info
->header_id
,
5176 op_info
->header_global
));
5177 print_string(PRINT_ANY
, "field", " field %s",
5178 dpipe_field_id2s(ctx
, op_info
->header_id
,
5180 op_info
->header_global
));
5181 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5183 op_info
->header_global
);
5185 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5188 static int dpipe_match_parse(struct dpipe_match
*match
,
5192 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5195 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5196 if (err
!= MNL_CB_OK
)
5199 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5200 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5201 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5202 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5206 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5207 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5208 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5209 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5214 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5215 struct nlattr
*nla_matches
)
5217 struct nlattr
*nla_match
;
5218 struct dpipe_match match
;
5220 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5221 pr_out_entry_start(ctx
->dl
);
5222 if (dpipe_match_parse(&match
, nla_match
))
5223 goto err_match_parse
;
5224 pr_out_dpipe_match(&match
, ctx
);
5225 pr_out_entry_end(ctx
->dl
);
5230 pr_out_entry_end(ctx
->dl
);
5234 static struct resource
*
5235 resource_find(struct resources
*resources
, struct resource
*resource
,
5236 uint64_t resource_id
)
5238 struct list_head
*list_head
;
5241 list_head
= &resources
->resource_list
;
5243 list_head
= &resource
->resource_list
;
5245 list_for_each_entry(resource
, list_head
, list
) {
5246 struct resource
*child_resource
;
5248 if (resource
->id
== resource_id
)
5251 child_resource
= resource_find(resources
, resource
,
5254 return child_resource
;
5260 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5261 uint64_t resource_id
)
5263 struct resource
*resource
, *parent_resource
;
5264 const char del
[] = "/";
5268 resource
= resource_find(resources
, NULL
, resource_id
);
5272 for (parent_resource
= resource
; parent_resource
;
5273 parent_resource
= parent_resource
->parent
)
5274 path_len
+= strlen(parent_resource
->name
) + 1;
5277 path
= calloc(1, path_len
);
5281 path
+= path_len
- 1;
5282 for (parent_resource
= resource
; parent_resource
;
5283 parent_resource
= parent_resource
->parent
) {
5284 path
-= strlen(parent_resource
->name
);
5285 memcpy(path
, parent_resource
->name
,
5286 strlen(parent_resource
->name
));
5287 path
-= strlen(del
);
5288 memcpy(path
, del
, strlen(del
));
5290 check_indent_newline(dl
);
5291 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5295 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5297 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5298 struct dpipe_table
*table
;
5299 uint32_t resource_units
;
5300 bool counters_enabled
;
5301 bool resource_valid
;
5305 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5306 if (err
!= MNL_CB_OK
)
5309 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5310 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5311 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5312 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5313 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5317 table
= dpipe_table_alloc();
5321 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5322 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5323 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5325 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5327 if (resource_valid
) {
5328 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5329 table
->resource_valid
= true;
5332 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5333 if (!ctx
->print_tables
)
5336 check_indent_newline(ctx
->dl
);
5337 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5338 print_uint(PRINT_ANY
, "size", " size %u", size
);
5339 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5341 if (resource_valid
) {
5342 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5343 resource_path_print(ctx
->dl
, ctx
->resources
,
5344 table
->resource_id
);
5345 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5349 pr_out_array_start(ctx
->dl
, "match");
5350 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5351 goto err_matches_show
;
5352 pr_out_array_end(ctx
->dl
);
5354 pr_out_array_start(ctx
->dl
, "action");
5355 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5356 goto err_actions_show
;
5357 pr_out_array_end(ctx
->dl
);
5363 pr_out_array_end(ctx
->dl
);
5367 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5369 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5370 struct nlattr
*nla_table
;
5372 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5373 if (ctx
->print_tables
)
5374 pr_out_handle_start_arr(ctx
->dl
, tb
);
5375 if (dpipe_table_show(ctx
, nla_table
))
5376 goto err_table_show
;
5377 if (ctx
->print_tables
)
5378 pr_out_handle_end(ctx
->dl
);
5383 if (ctx
->print_tables
)
5384 pr_out_handle_end(ctx
->dl
);
5388 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5390 struct dpipe_ctx
*ctx
= data
;
5391 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5392 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5394 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5395 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5396 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5397 return MNL_CB_ERROR
;
5399 if (dpipe_tables_show(ctx
, tb
))
5400 return MNL_CB_ERROR
;
5404 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5406 static int cmd_dpipe_table_show(struct dl
*dl
)
5408 struct nlmsghdr
*nlh
;
5409 struct dpipe_ctx dpipe_ctx
= {};
5410 struct resource_ctx resource_ctx
= {};
5411 uint16_t flags
= NLM_F_REQUEST
;
5414 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5418 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5420 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5424 dpipe_ctx
.print_tables
= true;
5426 dl_opts_put(nlh
, dl
);
5427 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5430 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5431 goto err_headers_get
;
5434 err
= resource_ctx_init(&resource_ctx
, dl
);
5436 goto err_resource_ctx_init
;
5438 resource_ctx
.print_resources
= false;
5439 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5440 dl_opts_put(nlh
, dl
);
5441 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5444 dpipe_ctx
.resources
= resource_ctx
.resources
;
5446 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5447 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5448 dl_opts_put(nlh
, dl
);
5450 pr_out_section_start(dl
, "table");
5451 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5452 pr_out_section_end(dl
);
5454 resource_ctx_fini(&resource_ctx
);
5455 dpipe_ctx_fini(&dpipe_ctx
);
5458 err_resource_ctx_init
:
5460 dpipe_ctx_fini(&dpipe_ctx
);
5464 static int cmd_dpipe_table_set(struct dl
*dl
)
5466 struct nlmsghdr
*nlh
;
5469 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5470 NLM_F_REQUEST
| NLM_F_ACK
);
5472 err
= dl_argv_parse_put(nlh
, dl
,
5473 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5474 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5478 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5481 enum dpipe_value_type
{
5482 DPIPE_VALUE_TYPE_VALUE
,
5483 DPIPE_VALUE_TYPE_MASK
,
5487 dpipe_value_type_e2s(enum dpipe_value_type type
)
5490 case DPIPE_VALUE_TYPE_VALUE
:
5492 case DPIPE_VALUE_TYPE_MASK
:
5493 return "value_mask";
5499 struct dpipe_field_printer
{
5500 unsigned int field_id
;
5501 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5504 struct dpipe_header_printer
{
5505 struct dpipe_field_printer
*printers
;
5506 unsigned int printers_count
;
5507 unsigned int header_id
;
5510 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5511 enum dpipe_value_type type
,
5514 struct in_addr ip_addr
;
5516 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5517 check_indent_newline(ctx
->dl
);
5518 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5522 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5523 enum dpipe_value_type type
,
5526 check_indent_newline(ctx
->dl
);
5527 print_string_name_value(dpipe_value_type_e2s(type
),
5528 ether_ntoa((struct ether_addr
*)value
));
5531 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5532 enum dpipe_value_type type
,
5535 char str
[INET6_ADDRSTRLEN
];
5537 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5538 check_indent_newline(ctx
->dl
);
5539 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5542 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5544 .printer
= dpipe_field_printer_ipv4_addr
,
5545 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5549 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5550 .printers
= dpipe_field_printers_ipv4
,
5551 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5552 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5555 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5557 .printer
= dpipe_field_printer_ethernet_addr
,
5558 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5562 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5563 .printers
= dpipe_field_printers_ethernet
,
5564 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5565 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5568 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5570 .printer
= dpipe_field_printer_ipv6_addr
,
5571 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5575 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5576 .printers
= dpipe_field_printers_ipv6
,
5577 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5578 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5581 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5582 &dpipe_header_printer_ipv4
,
5583 &dpipe_header_printer_ethernet
,
5584 &dpipe_header_printer_ipv6
,
5587 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5588 struct dpipe_op_info
*info
,
5589 enum dpipe_value_type type
,
5592 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5593 struct dpipe_header_printer
*header_printer
;
5594 struct dpipe_field_printer
*field_printer
;
5595 unsigned int field_printers_count
;
5599 for (i
= 0; i
< header_printers_count
; i
++) {
5600 header_printer
= dpipe_header_printers
[i
];
5601 if (header_printer
->header_id
!= info
->header_id
)
5603 field_printers_count
= header_printer
->printers_count
;
5604 for (j
= 0; j
< field_printers_count
; j
++) {
5605 field_printer
= &header_printer
->printers
[j
];
5606 if (field_printer
->field_id
!= info
->field_id
)
5608 field_printer
->printer(ctx
, type
, value
);
5616 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5618 unsigned int value_len
,
5619 struct dpipe_op_info
*info
,
5620 enum dpipe_value_type type
)
5622 if (info
->header_global
&&
5623 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5626 if (value_len
== sizeof(uint32_t)) {
5627 uint32_t *value_32
= value
;
5629 check_indent_newline(ctx
->dl
);
5630 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5634 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5635 struct nlattr
**nla_match_value
,
5636 struct dpipe_op_info
*info
)
5638 void *value
, *value_mask
;
5639 uint32_t value_mapping
;
5643 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5644 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5646 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5647 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5650 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5651 check_indent_newline(ctx
->dl
);
5652 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5656 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5657 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5658 DPIPE_VALUE_TYPE_MASK
);
5661 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5664 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5667 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5668 struct dpipe_match match
;
5671 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5672 if (err
!= MNL_CB_OK
)
5675 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5676 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5680 pr_out_entry_start(ctx
->dl
);
5681 if (dpipe_match_parse(&match
,
5682 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5683 goto err_match_parse
;
5684 pr_out_dpipe_match(&match
, ctx
);
5685 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5686 pr_out_entry_end(ctx
->dl
);
5691 pr_out_entry_end(ctx
->dl
);
5695 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5698 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5699 struct dpipe_action action
;
5702 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5703 if (err
!= MNL_CB_OK
)
5706 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5707 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5711 pr_out_entry_start(ctx
->dl
);
5712 if (dpipe_action_parse(&action
,
5713 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5714 goto err_action_parse
;
5715 pr_out_dpipe_action(&action
, ctx
);
5716 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5717 pr_out_entry_end(ctx
->dl
);
5722 pr_out_entry_end(ctx
->dl
);
5727 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5728 struct nlattr
*nla_action_values
)
5730 struct nlattr
*nla_action_value
;
5732 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5733 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5740 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5741 struct nlattr
*nla_match_values
)
5743 struct nlattr
*nla_match_value
;
5745 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5746 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5752 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5754 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5755 uint32_t entry_index
;
5759 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5760 if (err
!= MNL_CB_OK
)
5763 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5764 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5765 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5769 check_indent_newline(ctx
->dl
);
5770 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5771 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5773 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5774 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5775 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5778 pr_out_array_start(ctx
->dl
, "match_value");
5779 if (dpipe_tables_match_values_show(ctx
,
5780 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5781 goto err_match_values_show
;
5782 pr_out_array_end(ctx
->dl
);
5784 pr_out_array_start(ctx
->dl
, "action_value");
5785 if (dpipe_tables_action_values_show(ctx
,
5786 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5787 goto err_action_values_show
;
5788 pr_out_array_end(ctx
->dl
);
5791 err_action_values_show
:
5792 err_match_values_show
:
5793 pr_out_array_end(ctx
->dl
);
5797 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5799 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5800 struct nlattr
*nla_entry
;
5802 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5803 pr_out_handle_start_arr(ctx
->dl
, tb
);
5804 if (dpipe_entry_show(ctx
, nla_entry
))
5805 goto err_entry_show
;
5806 pr_out_handle_end(ctx
->dl
);
5811 pr_out_handle_end(ctx
->dl
);
5815 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5817 struct dpipe_ctx
*ctx
= data
;
5818 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5819 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5821 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5822 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5823 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5824 return MNL_CB_ERROR
;
5826 if (dpipe_table_entries_show(ctx
, tb
))
5827 return MNL_CB_ERROR
;
5831 static int cmd_dpipe_table_dump(struct dl
*dl
)
5833 struct nlmsghdr
*nlh
;
5834 struct dpipe_ctx ctx
= {};
5835 uint16_t flags
= NLM_F_REQUEST
;
5838 err
= dpipe_ctx_init(&ctx
, dl
);
5842 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5846 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5847 dl_opts_put(nlh
, dl
);
5848 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5850 pr_err("error get headers %s\n", strerror(ctx
.err
));
5854 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5855 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5856 dl_opts_put(nlh
, dl
);
5858 pr_out_section_start(dl
, "table_entry");
5859 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5860 pr_out_section_end(dl
);
5862 dpipe_ctx_fini(&ctx
);
5866 static int cmd_dpipe_table(struct dl
*dl
)
5868 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5871 } else if (dl_argv_match(dl
, "show")) {
5873 return cmd_dpipe_table_show(dl
);
5874 } else if (dl_argv_match(dl
, "set")) {
5876 return cmd_dpipe_table_set(dl
);
5877 } else if (dl_argv_match(dl
, "dump")) {
5879 return cmd_dpipe_table_dump(dl
);
5881 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5885 static int cmd_dpipe(struct dl
*dl
)
5887 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5890 } else if (dl_argv_match(dl
, "header")) {
5892 return cmd_dpipe_header(dl
);
5893 } else if (dl_argv_match(dl
, "table")) {
5895 return cmd_dpipe_table(dl
);
5897 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5902 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5903 struct nlattr
**nla_resource
)
5905 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5906 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5907 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5908 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5909 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5910 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5911 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5915 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5916 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5917 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5918 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5919 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5920 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5921 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5923 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5924 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5926 resource
->size_new
= resource
->size
;
5928 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5929 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5930 resource
->occ_valid
= true;
5933 if (resource
->size_new
!= resource
->size
)
5934 ctx
->pending_change
= true;
5940 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5941 struct resource
*parent_resource
, struct nlattr
*nl
)
5943 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5944 struct nlattr
*nla_child_resource
;
5945 struct nlattr
*nla_resources
;
5955 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5956 if (err
!= MNL_CB_OK
)
5959 err
= resource_parse(ctx
, resource
, nla_resource
);
5963 resource
->parent
= parent_resource
;
5964 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5967 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5968 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5970 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5971 struct resource
*child_resource
;
5972 struct list_head
*list
;
5974 child_resource
= resource_alloc();
5975 if (!child_resource
)
5979 list
= &ctx
->resources
->resource_list
;
5981 list
= &resource
->resource_list
;
5983 list_add_tail(&child_resource
->list
, list
);
5984 err
= resource_get(ctx
, child_resource
, resource
,
5985 nla_child_resource
);
5993 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5996 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5997 default: return "<unknown unit>";
6001 static void resource_show(struct resource
*resource
,
6002 struct resource_ctx
*ctx
)
6004 struct resource
*child_resource
;
6005 struct dpipe_table
*table
;
6006 struct dl
*dl
= ctx
->dl
;
6009 check_indent_newline(dl
);
6010 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6012 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6013 pr_out_u64(dl
, "size", resource
->size
);
6014 if (resource
->size
!= resource
->size_new
)
6015 pr_out_u64(dl
, "size_new", resource
->size_new
);
6016 if (resource
->occ_valid
)
6017 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6018 print_string(PRINT_ANY
, "unit", " unit %s",
6019 resource_unit_str_get(resource
->unit
));
6021 if (resource
->size_min
!= resource
->size_max
) {
6022 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6023 resource
->size_min
);
6024 pr_out_u64(dl
, "size_max", resource
->size_max
);
6025 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6026 resource
->size_gran
);
6029 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6030 if (table
->resource_id
== resource
->id
&&
6031 table
->resource_valid
)
6035 pr_out_array_start(dl
, "dpipe_tables");
6037 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6040 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6041 if (table
->resource_id
!= resource
->id
||
6042 !table
->resource_valid
)
6044 pr_out_entry_start(dl
);
6045 check_indent_newline(dl
);
6046 print_string(PRINT_ANY
, "table_name", "table_name %s",
6048 pr_out_entry_end(dl
);
6051 pr_out_array_end(dl
);
6053 if (list_empty(&resource
->resource_list
))
6056 if (ctx
->pending_change
) {
6057 check_indent_newline(dl
);
6058 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6059 resource
->size_valid
? "true" : "false");
6061 pr_out_array_start(dl
, "resources");
6062 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6063 pr_out_entry_start(dl
);
6064 resource_show(child_resource
, ctx
);
6065 pr_out_entry_end(dl
);
6067 pr_out_array_end(dl
);
6071 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6073 struct resources
*resources
= ctx
->resources
;
6074 struct resource
*resource
;
6076 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6077 pr_out_handle_start_arr(ctx
->dl
, tb
);
6078 resource_show(resource
, ctx
);
6079 pr_out_handle_end(ctx
->dl
);
6083 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6085 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6088 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6090 struct resource_ctx
*ctx
= data
;
6091 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6092 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6095 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6096 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6097 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6098 return MNL_CB_ERROR
;
6100 err
= resources_get(ctx
, tb
);
6103 return MNL_CB_ERROR
;
6106 if (ctx
->print_resources
)
6107 resources_show(ctx
, tb
);
6112 static int cmd_resource_show(struct dl
*dl
)
6114 struct nlmsghdr
*nlh
;
6115 struct dpipe_ctx dpipe_ctx
= {};
6116 struct resource_ctx resource_ctx
= {};
6119 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6123 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6125 dl_opts_put(nlh
, dl
);
6127 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6131 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6134 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6138 err
= resource_ctx_init(&resource_ctx
, dl
);
6142 resource_ctx
.print_resources
= true;
6143 resource_ctx
.tables
= dpipe_ctx
.tables
;
6144 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6145 NLM_F_REQUEST
| NLM_F_ACK
);
6146 dl_opts_put(nlh
, dl
);
6147 pr_out_section_start(dl
, "resources");
6148 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6150 pr_out_section_end(dl
);
6151 resource_ctx_fini(&resource_ctx
);
6153 dpipe_ctx_fini(&dpipe_ctx
);
6157 static void cmd_resource_help(void)
6159 pr_err("Usage: devlink resource show DEV\n"
6160 " devlink resource set DEV path PATH size SIZE\n");
6163 static struct resource
*
6164 resource_find_by_name(struct list_head
*list
, char *name
)
6166 struct resource
*resource
;
6168 list_for_each_entry(resource
, list
, list
) {
6169 if (!strcmp(resource
->name
, name
))
6176 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6177 uint32_t *p_resource_id
, bool *p_resource_valid
)
6179 struct resource
*resource
;
6180 uint32_t resource_id
= 0;
6181 char *resource_path_dup
;
6182 struct list_head
*list
;
6183 const char del
[] = "/";
6184 char *resource_name
;
6186 resource_path_dup
= strdup(resource_path
);
6187 list
= &ctx
->resources
->resource_list
;
6188 resource_name
= strtok(resource_path_dup
, del
);
6189 while (resource_name
!= NULL
) {
6190 resource
= resource_find_by_name(list
, resource_name
);
6192 goto err_resource_lookup
;
6194 list
= &resource
->resource_list
;
6195 resource_name
= strtok(NULL
, del
);
6196 resource_id
= resource
->id
;
6198 free(resource_path_dup
);
6199 *p_resource_valid
= true;
6200 *p_resource_id
= resource_id
;
6203 err_resource_lookup
:
6204 free(resource_path_dup
);
6208 static int cmd_resource_set(struct dl
*dl
)
6210 struct nlmsghdr
*nlh
;
6211 struct resource_ctx ctx
= {};
6214 err
= resource_ctx_init(&ctx
, dl
);
6218 ctx
.print_resources
= false;
6219 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6220 DL_OPT_RESOURCE_SIZE
, 0);
6224 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6226 dl_opts_put(nlh
, dl
);
6227 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6229 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6233 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6234 &dl
->opts
.resource_id
,
6235 &dl
->opts
.resource_id_valid
);
6237 pr_err("error parsing resource path %s\n", strerror(-err
));
6241 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6242 NLM_F_REQUEST
| NLM_F_ACK
);
6244 dl_opts_put(nlh
, dl
);
6245 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6247 resource_ctx_fini(&ctx
);
6251 static int cmd_resource(struct dl
*dl
)
6253 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6254 cmd_resource_help();
6256 } else if (dl_argv_match(dl
, "show")) {
6258 return cmd_resource_show(dl
);
6259 } else if (dl_argv_match(dl
, "set")) {
6261 return cmd_resource_set(dl
);
6263 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6267 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6269 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6270 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6271 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6274 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6275 if (dl
->json_output
)
6276 open_json_object(buf
);
6281 static void pr_out_region_handle_end(struct dl
*dl
)
6283 if (dl
->json_output
)
6284 close_json_object();
6289 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6291 __pr_out_indent_newline(dl
);
6292 if (dl
->json_output
)
6293 open_json_array(PRINT_JSON
, "snapshot");
6295 pr_out("snapshot %s", array
? "[" : "");
6298 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6300 if (dl
->json_output
)
6301 close_json_array(PRINT_JSON
, NULL
);
6306 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6308 uint32_t snapshot_id
;
6310 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6313 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6315 if (dl
->json_output
)
6316 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6318 pr_out("%s%u", index
? " " : "", snapshot_id
);
6321 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6323 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6324 struct nlattr
*nla_sanpshot
;
6327 pr_out_region_snapshots_start(dl
, true);
6328 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6329 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6330 if (err
!= MNL_CB_OK
)
6332 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6334 pr_out_region_snapshots_end(dl
, true);
6337 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6339 pr_out_region_snapshots_start(dl
, false);
6340 pr_out_region_snapshots_id(dl
, tb
, 0);
6341 pr_out_region_snapshots_end(dl
, false);
6344 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6346 pr_out_region_handle_start(dl
, tb
);
6348 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6349 pr_out_u64(dl
, "size",
6350 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6352 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6353 pr_out_snapshots(dl
, tb
);
6355 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6356 pr_out_snapshot(dl
, tb
);
6358 pr_out_region_handle_end(dl
);
6361 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6363 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6364 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6365 struct dl
*dl
= data
;
6367 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6368 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6369 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6370 return MNL_CB_ERROR
;
6372 pr_out_region(dl
, tb
);
6377 static int cmd_region_show(struct dl
*dl
)
6379 struct nlmsghdr
*nlh
;
6380 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6383 if (dl_argc(dl
) == 0)
6384 flags
|= NLM_F_DUMP
;
6386 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6388 if (dl_argc(dl
) > 0) {
6389 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6394 pr_out_section_start(dl
, "regions");
6395 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6396 pr_out_section_end(dl
);
6400 static int cmd_region_snapshot_del(struct dl
*dl
)
6402 struct nlmsghdr
*nlh
;
6405 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6406 NLM_F_REQUEST
| NLM_F_ACK
);
6408 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6409 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6413 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6416 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6418 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6419 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6420 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6421 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6422 struct dl
*dl
= data
;
6425 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6426 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6427 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6428 return MNL_CB_ERROR
;
6430 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6431 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6432 if (err
!= MNL_CB_OK
)
6433 return MNL_CB_ERROR
;
6435 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6436 if (!nla_chunk_data
)
6439 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6440 if (!nla_chunk_addr
)
6443 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6444 mnl_attr_get_payload_len(nla_chunk_data
),
6445 mnl_attr_get_u64(nla_chunk_addr
));
6450 static int cmd_region_dump(struct dl
*dl
)
6452 struct nlmsghdr
*nlh
;
6455 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6456 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6458 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6459 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6463 pr_out_section_start(dl
, "dump");
6464 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6465 pr_out_section_end(dl
);
6466 if (!dl
->json_output
)
6471 static int cmd_region_read(struct dl
*dl
)
6473 struct nlmsghdr
*nlh
;
6476 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6477 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6479 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6480 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6481 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6485 pr_out_section_start(dl
, "read");
6486 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6487 pr_out_section_end(dl
);
6488 if (!dl
->json_output
)
6493 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
6495 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6496 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6497 struct dl
*dl
= data
;
6499 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6500 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6501 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
6502 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6503 return MNL_CB_ERROR
;
6505 pr_out_region(dl
, tb
);
6510 static int cmd_region_snapshot_new(struct dl
*dl
)
6512 struct nlmsghdr
*nlh
;
6515 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
6516 NLM_F_REQUEST
| NLM_F_ACK
);
6518 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
6519 DL_OPT_REGION_SNAPSHOT_ID
);
6523 pr_out_section_start(dl
, "regions");
6524 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
6525 pr_out_section_end(dl
);
6529 static void cmd_region_help(void)
6531 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6532 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6533 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
6534 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6535 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6538 static int cmd_region(struct dl
*dl
)
6540 if (dl_no_arg(dl
)) {
6541 return cmd_region_show(dl
);
6542 } else if (dl_argv_match(dl
, "help")) {
6545 } else if (dl_argv_match(dl
, "show")) {
6547 return cmd_region_show(dl
);
6548 } else if (dl_argv_match(dl
, "del")) {
6550 return cmd_region_snapshot_del(dl
);
6551 } else if (dl_argv_match(dl
, "dump")) {
6553 return cmd_region_dump(dl
);
6554 } else if (dl_argv_match(dl
, "read")) {
6556 return cmd_region_read(dl
);
6557 } else if (dl_argv_match(dl
, "new")) {
6559 return cmd_region_snapshot_new(dl
);
6561 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6565 static int cmd_health_set_params(struct dl
*dl
)
6567 struct nlmsghdr
*nlh
;
6570 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6571 NLM_F_REQUEST
| NLM_F_ACK
);
6572 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6573 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6574 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
6575 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
6579 dl_opts_put(nlh
, dl
);
6580 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6583 static int cmd_health_dump_clear(struct dl
*dl
)
6585 struct nlmsghdr
*nlh
;
6588 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6589 NLM_F_REQUEST
| NLM_F_ACK
);
6591 err
= dl_argv_parse_put(nlh
, dl
,
6592 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6596 dl_opts_put(nlh
, dl
);
6597 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6600 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6605 check_indent_newline(dl
);
6608 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6611 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6614 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6617 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6620 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6622 case MNL_TYPE_NUL_STRING
:
6623 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6625 case MNL_TYPE_BINARY
:
6626 len
= mnl_attr_get_payload_len(nl_data
);
6627 data
= mnl_attr_get_payload(nl_data
);
6628 pr_out_binary_value(dl
, data
, len
);
6636 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6641 pr_out_name(dl
, *name
);
6648 struct list_head list
;
6651 struct fmsg_cb_data
{
6655 struct list_head entry_list
;
6658 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6659 uint8_t *attr_value
, bool insert
)
6661 struct nest_entry
*entry
;
6664 entry
= malloc(sizeof(struct nest_entry
));
6668 entry
->attr_type
= *attr_value
;
6669 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6671 if (list_empty(&fmsg_data
->entry_list
))
6672 return MNL_CB_ERROR
;
6673 entry
= list_first_entry(&fmsg_data
->entry_list
,
6674 struct nest_entry
, list
);
6675 *attr_value
= entry
->attr_type
;
6676 list_del(&entry
->list
);
6682 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6685 pr_out_fmsg_name(dl
, name
);
6687 __pr_out_indent_inc();
6690 static void pr_out_fmsg_group_end(struct dl
*dl
)
6693 __pr_out_indent_dec();
6696 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6698 if (dl
->json_output
) {
6699 pr_out_fmsg_name(dl
, name
);
6700 open_json_object(NULL
);
6702 pr_out_fmsg_group_start(dl
, name
);
6706 static void pr_out_fmsg_end_object(struct dl
*dl
)
6708 if (dl
->json_output
)
6709 close_json_object();
6711 pr_out_fmsg_group_end(dl
);
6714 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6716 if (dl
->json_output
) {
6717 pr_out_fmsg_name(dl
, name
);
6718 open_json_array(PRINT_JSON
, NULL
);
6720 pr_out_fmsg_group_start(dl
, name
);
6724 static void pr_out_fmsg_end_array(struct dl
*dl
)
6726 if (dl
->json_output
)
6727 close_json_array(PRINT_JSON
, NULL
);
6729 pr_out_fmsg_group_end(dl
);
6732 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6735 struct dl
*dl
= fmsg_data
->dl
;
6736 uint8_t value
= nest_value
;
6739 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6740 if (err
!= MNL_CB_OK
)
6744 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6746 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6748 pr_out_fmsg_end_object(dl
);
6750 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6752 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6754 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6756 pr_out_fmsg_end_array(dl
);
6764 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6766 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6767 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6768 struct fmsg_cb_data
*fmsg_data
= data
;
6769 struct dl
*dl
= fmsg_data
->dl
;
6770 struct nlattr
*nla_object
;
6774 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6775 if (!tb
[DEVLINK_ATTR_FMSG
])
6776 return MNL_CB_ERROR
;
6778 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6779 attr_type
= mnl_attr_get_type(nla_object
);
6780 switch (attr_type
) {
6781 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6782 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6783 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6784 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6785 if (err
!= MNL_CB_OK
)
6788 case DEVLINK_ATTR_FMSG_NEST_END
:
6789 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6790 if (err
!= MNL_CB_OK
)
6793 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6794 free(fmsg_data
->name
);
6795 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6796 if (!fmsg_data
->name
)
6799 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6800 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6802 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6803 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6804 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6806 if (err
!= MNL_CB_OK
)
6816 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6818 /* FMSG is dynamic: opening of an object or array causes a
6819 * newline. JSON starts with an { or [, but plain text should
6820 * not start with a new line. Ensure this by setting
6821 * g_new_line_count to 1: avoiding newline before the first
6824 g_new_line_count
= 1;
6827 INIT_LIST_HEAD(&data
->entry_list
);
6830 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6832 struct fmsg_cb_data data
;
6833 struct nlmsghdr
*nlh
;
6836 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6838 err
= dl_argv_parse_put(nlh
, dl
,
6839 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6843 cmd_fmsg_init(dl
, &data
);
6844 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6849 static int cmd_health_dump_show(struct dl
*dl
)
6851 return cmd_health_object_common(dl
,
6852 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6856 static int cmd_health_diagnose(struct dl
*dl
)
6858 return cmd_health_object_common(dl
,
6859 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6863 static int cmd_health_recover(struct dl
*dl
)
6865 struct nlmsghdr
*nlh
;
6868 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6869 NLM_F_REQUEST
| NLM_F_ACK
);
6871 err
= dl_argv_parse_put(nlh
, dl
,
6872 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6876 dl_opts_put(nlh
, dl
);
6877 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6880 enum devlink_health_reporter_state
{
6881 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6882 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6885 static const char *health_state_name(uint8_t state
)
6888 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6889 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6890 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6891 return HEALTH_REPORTER_STATE_ERROR_STR
;
6893 return "<unknown state>";
6897 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6899 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6900 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6901 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6902 struct sysinfo s_info
;
6908 info
= localtime(&now
);
6909 err
= sysinfo(&s_info
);
6912 /* Subtract uptime in sec from now yields the time of system
6913 * uptime. To this, add time_ms which is the amount of
6914 * milliseconds elapsed between uptime and the dump taken.
6916 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6917 info
= localtime(&sec
);
6919 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6920 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6921 check_indent_newline(dl
);
6922 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6923 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6926 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6928 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6929 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6934 ts
= mnl_attr_get_u64(attr
);
6935 tv_sec
= ts
/ 1000000000;
6936 tm
= localtime(&tv_sec
);
6938 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6939 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6941 check_indent_newline(dl
);
6942 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6943 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6946 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6948 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6949 enum devlink_health_reporter_state state
;
6952 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6954 if (err
!= MNL_CB_OK
)
6957 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6958 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6959 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6960 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6963 pr_out_handle_start_arr(dl
, tb_health
);
6965 check_indent_newline(dl
);
6966 print_string(PRINT_ANY
, "reporter", "reporter %s",
6967 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6968 if (!dl
->json_output
) {
6970 __pr_out_indent_inc();
6972 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6973 check_indent_newline(dl
);
6974 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6975 pr_out_u64(dl
, "error",
6976 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6977 pr_out_u64(dl
, "recover",
6978 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6979 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6980 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6981 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6982 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6983 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6984 pr_out_u64(dl
, "grace_period",
6985 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6986 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6987 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6988 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6989 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
6990 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
6991 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
6993 __pr_out_indent_dec();
6994 pr_out_handle_end(dl
);
6997 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6999 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7000 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7001 struct dl
*dl
= data
;
7003 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7004 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7005 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
7006 return MNL_CB_ERROR
;
7008 pr_out_health(dl
, tb
);
7013 static int cmd_health_show(struct dl
*dl
)
7015 struct nlmsghdr
*nlh
;
7016 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7019 if (dl_argc(dl
) == 0)
7020 flags
|= NLM_F_DUMP
;
7021 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7024 if (dl_argc(dl
) > 0) {
7025 err
= dl_argv_parse_put(nlh
, dl
,
7027 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7031 pr_out_section_start(dl
, "health");
7033 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
7034 pr_out_section_end(dl
);
7038 static void cmd_health_help(void)
7040 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
7041 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
7042 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
7043 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
7044 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
7045 pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
7046 pr_err(" [ grace_period MSEC ]\n");
7047 pr_err(" [ auto_recover { true | false } ]\n");
7048 pr_err(" [ auto_dump { true | false } ]\n");
7051 static int cmd_health(struct dl
*dl
)
7053 if (dl_argv_match(dl
, "help")) {
7056 } else if (dl_argv_match(dl
, "show") ||
7057 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7059 return cmd_health_show(dl
);
7060 } else if (dl_argv_match(dl
, "recover")) {
7062 return cmd_health_recover(dl
);
7063 } else if (dl_argv_match(dl
, "diagnose")) {
7065 return cmd_health_diagnose(dl
);
7066 } else if (dl_argv_match(dl
, "dump")) {
7068 if (dl_argv_match(dl
, "show")) {
7070 return cmd_health_dump_show(dl
);
7071 } else if (dl_argv_match(dl
, "clear")) {
7073 return cmd_health_dump_clear(dl
);
7075 } else if (dl_argv_match(dl
, "set")) {
7077 return cmd_health_set_params(dl
);
7079 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7083 static const char *trap_type_name(uint8_t type
)
7086 case DEVLINK_TRAP_TYPE_DROP
:
7088 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7090 case DEVLINK_TRAP_TYPE_CONTROL
:
7093 return "<unknown type>";
7097 static const char *trap_action_name(uint8_t action
)
7100 case DEVLINK_TRAP_ACTION_DROP
:
7102 case DEVLINK_TRAP_ACTION_TRAP
:
7104 case DEVLINK_TRAP_ACTION_MIRROR
:
7107 return "<unknown action>";
7111 static const char *trap_metadata_name(const struct nlattr
*attr
)
7113 switch (attr
->nla_type
) {
7114 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7115 return "input_port";
7116 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7117 return "flow_action_cookie";
7119 return "<unknown metadata type>";
7122 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7124 struct nlattr
*attr_metadata
;
7126 pr_out_array_start(dl
, "metadata");
7127 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7128 check_indent_newline(dl
);
7129 print_string(PRINT_ANY
, NULL
, "%s",
7130 trap_metadata_name(attr_metadata
));
7132 pr_out_array_end(dl
);
7135 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7137 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7138 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7141 pr_out_handle_start_arr(dl
, tb
);
7143 __pr_out_handle_start(dl
, tb
, true, false);
7145 check_indent_newline(dl
);
7146 print_string(PRINT_ANY
, "name", "name %s",
7147 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7148 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7149 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7150 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7151 print_string(PRINT_ANY
, "group", " group %s",
7152 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7154 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7155 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7156 pr_out_handle_end(dl
);
7159 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7161 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7162 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7163 struct dl
*dl
= data
;
7165 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7166 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7167 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7168 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7169 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7170 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7171 return MNL_CB_ERROR
;
7173 pr_out_trap(dl
, tb
, true);
7178 static void cmd_trap_help(void)
7180 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop | mirror } ]\n");
7181 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7182 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop | mirror } ]\n");
7183 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7184 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7185 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7186 pr_err(" devlink trap policer show DEV policer POLICER\n");
7189 static int cmd_trap_show(struct dl
*dl
)
7191 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7192 struct nlmsghdr
*nlh
;
7195 if (dl_argc(dl
) == 0)
7196 flags
|= NLM_F_DUMP
;
7198 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7200 if (dl_argc(dl
) > 0) {
7201 err
= dl_argv_parse_put(nlh
, dl
,
7202 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7207 pr_out_section_start(dl
, "trap");
7208 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7209 pr_out_section_end(dl
);
7214 static int cmd_trap_set(struct dl
*dl
)
7216 struct nlmsghdr
*nlh
;
7219 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7220 NLM_F_REQUEST
| NLM_F_ACK
);
7222 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7223 DL_OPT_TRAP_ACTION
);
7227 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7230 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7233 pr_out_handle_start_arr(dl
, tb
);
7235 __pr_out_handle_start(dl
, tb
, true, false);
7237 check_indent_newline(dl
);
7238 print_string(PRINT_ANY
, "name", "name %s",
7239 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7240 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7241 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7242 print_uint(PRINT_ANY
, "policer", " policer %u",
7243 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7244 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7245 pr_out_handle_end(dl
);
7248 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7250 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7251 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7252 struct dl
*dl
= data
;
7254 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7255 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7256 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7257 return MNL_CB_ERROR
;
7259 pr_out_trap_group(dl
, tb
, true);
7264 static int cmd_trap_group_show(struct dl
*dl
)
7266 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7267 struct nlmsghdr
*nlh
;
7270 if (dl_argc(dl
) == 0)
7271 flags
|= NLM_F_DUMP
;
7273 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7275 if (dl_argc(dl
) > 0) {
7276 err
= dl_argv_parse_put(nlh
, dl
,
7277 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7283 pr_out_section_start(dl
, "trap_group");
7284 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7285 pr_out_section_end(dl
);
7290 static int cmd_trap_group_set(struct dl
*dl
)
7292 struct nlmsghdr
*nlh
;
7295 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7296 NLM_F_REQUEST
| NLM_F_ACK
);
7298 err
= dl_argv_parse_put(nlh
, dl
,
7299 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7300 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7304 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7307 static int cmd_trap_group(struct dl
*dl
)
7309 if (dl_argv_match(dl
, "help")) {
7312 } else if (dl_argv_match(dl
, "show") ||
7313 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7315 return cmd_trap_group_show(dl
);
7316 } else if (dl_argv_match(dl
, "set")) {
7318 return cmd_trap_group_set(dl
);
7320 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7324 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7327 pr_out_handle_start_arr(dl
, tb
);
7329 __pr_out_handle_start(dl
, tb
, true, false);
7331 check_indent_newline(dl
);
7332 print_uint(PRINT_ANY
, "policer", "policer %u",
7333 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7334 print_u64(PRINT_ANY
, "rate", " rate %llu",
7335 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7336 print_u64(PRINT_ANY
, "burst", " burst %llu",
7337 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7338 if (tb
[DEVLINK_ATTR_STATS
])
7339 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7340 pr_out_handle_end(dl
);
7343 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7345 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7346 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7347 struct dl
*dl
= data
;
7349 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7350 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7351 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7352 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7353 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7354 return MNL_CB_ERROR
;
7356 pr_out_trap_policer(dl
, tb
, true);
7361 static int cmd_trap_policer_show(struct dl
*dl
)
7363 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7364 struct nlmsghdr
*nlh
;
7367 if (dl_argc(dl
) == 0)
7368 flags
|= NLM_F_DUMP
;
7370 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7372 if (dl_argc(dl
) > 0) {
7373 err
= dl_argv_parse_put(nlh
, dl
,
7374 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7380 pr_out_section_start(dl
, "trap_policer");
7381 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7382 pr_out_section_end(dl
);
7387 static int cmd_trap_policer_set(struct dl
*dl
)
7389 struct nlmsghdr
*nlh
;
7392 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7393 NLM_F_REQUEST
| NLM_F_ACK
);
7395 err
= dl_argv_parse_put(nlh
, dl
,
7396 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7397 DL_OPT_TRAP_POLICER_RATE
|
7398 DL_OPT_TRAP_POLICER_BURST
);
7402 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7405 static int cmd_trap_policer(struct dl
*dl
)
7407 if (dl_argv_match(dl
, "help")) {
7410 } else if (dl_argv_match(dl
, "show") ||
7411 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7413 return cmd_trap_policer_show(dl
);
7414 } else if (dl_argv_match(dl
, "set")) {
7416 return cmd_trap_policer_set(dl
);
7418 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7422 static int cmd_trap(struct dl
*dl
)
7424 if (dl_argv_match(dl
, "help")) {
7427 } else if (dl_argv_match(dl
, "show") ||
7428 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7430 return cmd_trap_show(dl
);
7431 } else if (dl_argv_match(dl
, "set")) {
7433 return cmd_trap_set(dl
);
7434 } else if (dl_argv_match(dl
, "group")) {
7436 return cmd_trap_group(dl
);
7437 } else if (dl_argv_match(dl
, "policer")) {
7439 return cmd_trap_policer(dl
);
7441 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7445 static void help(void)
7447 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7448 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7449 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7450 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7453 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7458 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7461 } else if (dl_argv_match(dl
, "dev")) {
7464 } else if (dl_argv_match(dl
, "port")) {
7466 return cmd_port(dl
);
7467 } else if (dl_argv_match(dl
, "sb")) {
7470 } else if (dl_argv_match(dl
, "monitor")) {
7473 } else if (dl_argv_match(dl
, "dpipe")) {
7475 return cmd_dpipe(dl
);
7476 } else if (dl_argv_match(dl
, "resource")) {
7478 return cmd_resource(dl
);
7479 } else if (dl_argv_match(dl
, "region")) {
7481 return cmd_region(dl
);
7482 } else if (dl_argv_match(dl
, "health")) {
7484 return cmd_health(dl
);
7485 } else if (dl_argv_match(dl
, "trap")) {
7487 return cmd_trap(dl
);
7489 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7493 static int dl_init(struct dl
*dl
)
7497 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7499 pr_err("Failed to connect to devlink Netlink\n");
7503 err
= ifname_map_init(dl
);
7505 pr_err("Failed to create index map\n");
7506 goto err_ifname_map_create
;
7508 new_json_obj_plain(dl
->json_output
);
7511 err_ifname_map_create
:
7512 mnlg_socket_close(dl
->nlg
);
7516 static void dl_fini(struct dl
*dl
)
7518 delete_json_obj_plain();
7519 ifname_map_fini(dl
);
7520 mnlg_socket_close(dl
->nlg
);
7523 static struct dl
*dl_alloc(void)
7527 dl
= calloc(1, sizeof(*dl
));
7533 static void dl_free(struct dl
*dl
)
7538 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7542 int ret
= EXIT_SUCCESS
;
7544 if (name
&& strcmp(name
, "-") != 0) {
7545 if (freopen(name
, "r", stdin
) == NULL
) {
7547 "Cannot open file \"%s\" for reading: %s\n",
7548 name
, strerror(errno
));
7549 return EXIT_FAILURE
;
7554 while (getcmdline(&line
, &len
, stdin
) != -1) {
7558 largc
= makeargs(line
, largv
, 100);
7560 continue; /* blank line */
7562 if (dl_cmd(dl
, largc
, largv
)) {
7563 fprintf(stderr
, "Command failed %s:%d\n",
7577 int main(int argc
, char **argv
)
7579 static const struct option long_options
[] = {
7580 { "Version", no_argument
, NULL
, 'V' },
7581 { "force", no_argument
, NULL
, 'f' },
7582 { "batch", required_argument
, NULL
, 'b' },
7583 { "no-nice-names", no_argument
, NULL
, 'n' },
7584 { "json", no_argument
, NULL
, 'j' },
7585 { "pretty", no_argument
, NULL
, 'p' },
7586 { "verbose", no_argument
, NULL
, 'v' },
7587 { "statistics", no_argument
, NULL
, 's' },
7588 { "Netns", required_argument
, NULL
, 'N' },
7589 { NULL
, 0, NULL
, 0 }
7591 const char *batch_file
= NULL
;
7600 pr_err("Failed to allocate memory for devlink\n");
7601 return EXIT_FAILURE
;
7604 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7605 long_options
, NULL
)) >= 0) {
7609 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7616 batch_file
= optarg
;
7619 dl
->no_nice_names
= true;
7622 dl
->json_output
= true;
7634 if (netns_switch(optarg
)) {
7640 pr_err("Unknown option.\n");
7657 err
= dl_batch(dl
, batch_file
, force
);
7659 err
= dl_cmd(dl
, argc
, argv
);