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
;
1160 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1166 struct dl_args_metadata
{
1168 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1171 static const struct dl_args_metadata dl_args_required
[] = {
1172 {DL_OPT_PORT_TYPE
, "Port type not set."},
1173 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1174 {DL_OPT_SB_POOL
, "Pool index option expected."},
1175 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1176 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1177 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1178 {DL_OPT_SB_TH
, "Threshold option expected."},
1179 {DL_OPT_SB_TC
, "TC index option expected."},
1180 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1181 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1182 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1183 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1184 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1185 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1186 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1187 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1188 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1189 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1190 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1191 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1192 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1193 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1194 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1195 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1198 static int dl_args_finding_required_validate(uint64_t o_required
,
1204 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1205 o_flag
= dl_args_required
[i
].o_flag
;
1206 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1207 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1211 if (o_required
& ~o_found
) {
1212 pr_err("BUG: unknown argument required but not found\n");
1218 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1219 uint64_t o_optional
)
1221 struct dl_opts
*opts
= &dl
->opts
;
1222 uint64_t o_all
= o_required
| o_optional
;
1223 uint64_t o_found
= 0;
1226 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1227 uint64_t handle_bit
;
1229 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1230 &opts
->port_index
, &handle_bit
);
1233 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1234 o_found
|= handle_bit
;
1235 } else if (o_required
& DL_OPT_HANDLE
) {
1236 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1239 o_found
|= DL_OPT_HANDLE
;
1240 } else if (o_required
& DL_OPT_HANDLEP
) {
1241 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1245 o_found
|= DL_OPT_HANDLEP
;
1246 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1247 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1249 &opts
->region_name
);
1252 o_found
|= DL_OPT_HANDLE_REGION
;
1255 while (dl_argc(dl
)) {
1256 if (dl_argv_match(dl
, "type") &&
1257 (o_all
& DL_OPT_PORT_TYPE
)) {
1258 const char *typestr
;
1261 err
= dl_argv_str(dl
, &typestr
);
1264 err
= port_type_get(typestr
, &opts
->port_type
);
1267 o_found
|= DL_OPT_PORT_TYPE
;
1268 } else if (dl_argv_match(dl
, "count") &&
1269 (o_all
& DL_OPT_PORT_COUNT
)) {
1271 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1274 o_found
|= DL_OPT_PORT_COUNT
;
1275 } else if (dl_argv_match(dl
, "sb") &&
1276 (o_all
& DL_OPT_SB
)) {
1278 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1281 o_found
|= DL_OPT_SB
;
1282 } else if (dl_argv_match(dl
, "pool") &&
1283 (o_all
& DL_OPT_SB_POOL
)) {
1285 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1288 o_found
|= DL_OPT_SB_POOL
;
1289 } else if (dl_argv_match(dl
, "size") &&
1290 (o_all
& DL_OPT_SB_SIZE
)) {
1292 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1295 o_found
|= DL_OPT_SB_SIZE
;
1296 } else if (dl_argv_match(dl
, "type") &&
1297 (o_all
& DL_OPT_SB_TYPE
)) {
1298 const char *typestr
;
1301 err
= dl_argv_str(dl
, &typestr
);
1304 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1307 o_found
|= DL_OPT_SB_TYPE
;
1308 } else if (dl_argv_match(dl
, "thtype") &&
1309 (o_all
& DL_OPT_SB_THTYPE
)) {
1310 const char *typestr
;
1313 err
= dl_argv_str(dl
, &typestr
);
1316 err
= threshold_type_get(typestr
,
1317 &opts
->sb_pool_thtype
);
1320 o_found
|= DL_OPT_SB_THTYPE
;
1321 } else if (dl_argv_match(dl
, "th") &&
1322 (o_all
& DL_OPT_SB_TH
)) {
1324 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1327 o_found
|= DL_OPT_SB_TH
;
1328 } else if (dl_argv_match(dl
, "tc") &&
1329 (o_all
& DL_OPT_SB_TC
)) {
1331 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1334 o_found
|= DL_OPT_SB_TC
;
1335 } else if (dl_argv_match(dl
, "mode") &&
1336 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1337 const char *typestr
;
1340 err
= dl_argv_str(dl
, &typestr
);
1343 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1346 o_found
|= DL_OPT_ESWITCH_MODE
;
1347 } else if (dl_argv_match(dl
, "inline-mode") &&
1348 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1349 const char *typestr
;
1352 err
= dl_argv_str(dl
, &typestr
);
1355 err
= eswitch_inline_mode_get(
1356 typestr
, &opts
->eswitch_inline_mode
);
1359 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1360 } else if (dl_argv_match(dl
, "name") &&
1361 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1363 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1366 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1367 } else if ((dl_argv_match(dl
, "counters") ||
1368 dl_argv_match(dl
, "counters_enabled")) &&
1369 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1371 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1374 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1375 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1376 dl_argv_match(dl
, "encap-mode")) &&
1377 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1378 const char *typestr
;
1381 err
= dl_argv_str(dl
, &typestr
);
1384 err
= eswitch_encap_mode_get(typestr
,
1385 &opts
->eswitch_encap_mode
);
1388 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1389 } else if (dl_argv_match(dl
, "path") &&
1390 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1392 err
= dl_argv_str(dl
, &opts
->resource_path
);
1395 o_found
|= DL_OPT_RESOURCE_PATH
;
1396 } else if (dl_argv_match(dl
, "size") &&
1397 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1399 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1402 o_found
|= DL_OPT_RESOURCE_SIZE
;
1403 } else if (dl_argv_match(dl
, "name") &&
1404 (o_all
& DL_OPT_PARAM_NAME
)) {
1406 err
= dl_argv_str(dl
, &opts
->param_name
);
1409 o_found
|= DL_OPT_PARAM_NAME
;
1410 } else if (dl_argv_match(dl
, "value") &&
1411 (o_all
& DL_OPT_PARAM_VALUE
)) {
1413 err
= dl_argv_str(dl
, &opts
->param_value
);
1416 o_found
|= DL_OPT_PARAM_VALUE
;
1417 } else if (dl_argv_match(dl
, "cmode") &&
1418 (o_all
& DL_OPT_PARAM_CMODE
)) {
1419 const char *cmodestr
;
1422 err
= dl_argv_str(dl
, &cmodestr
);
1425 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1428 o_found
|= DL_OPT_PARAM_CMODE
;
1429 } else if (dl_argv_match(dl
, "snapshot") &&
1430 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1432 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1435 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1436 } else if (dl_argv_match(dl
, "address") &&
1437 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1439 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1442 o_found
|= DL_OPT_REGION_ADDRESS
;
1443 } else if (dl_argv_match(dl
, "length") &&
1444 (o_all
& DL_OPT_REGION_LENGTH
)) {
1446 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1449 o_found
|= DL_OPT_REGION_LENGTH
;
1450 } else if (dl_argv_match(dl
, "file") &&
1451 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1453 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1456 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1457 } else if (dl_argv_match(dl
, "component") &&
1458 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1460 err
= dl_argv_str(dl
, &opts
->flash_component
);
1463 o_found
|= DL_OPT_FLASH_COMPONENT
;
1464 } else if (dl_argv_match(dl
, "reporter") &&
1465 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1467 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1470 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1471 } else if (dl_argv_match(dl
, "grace_period") &&
1472 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1474 err
= dl_argv_uint64_t(dl
,
1475 &opts
->reporter_graceful_period
);
1478 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1479 } else if (dl_argv_match(dl
, "auto_recover") &&
1480 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1482 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1485 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1486 } else if (dl_argv_match(dl
, "auto_dump") &&
1487 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)) {
1489 err
= dl_argv_bool(dl
, &opts
->reporter_auto_dump
);
1492 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_DUMP
;
1493 } else if (dl_argv_match(dl
, "trap") &&
1494 (o_all
& DL_OPT_TRAP_NAME
)) {
1496 err
= dl_argv_str(dl
, &opts
->trap_name
);
1499 o_found
|= DL_OPT_TRAP_NAME
;
1500 } else if (dl_argv_match(dl
, "group") &&
1501 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1503 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1506 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1507 } else if (dl_argv_match(dl
, "action") &&
1508 (o_all
& DL_OPT_TRAP_ACTION
)) {
1509 const char *actionstr
;
1512 err
= dl_argv_str(dl
, &actionstr
);
1515 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1518 o_found
|= DL_OPT_TRAP_ACTION
;
1519 } else if (dl_argv_match(dl
, "netns") &&
1520 (o_all
& DL_OPT_NETNS
)) {
1521 const char *netns_str
;
1524 err
= dl_argv_str(dl
, &netns_str
);
1527 opts
->netns
= netns_get_fd(netns_str
);
1528 if ((int)opts
->netns
< 0) {
1530 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1533 opts
->netns_is_pid
= true;
1535 o_found
|= DL_OPT_NETNS
;
1536 } else if (dl_argv_match(dl
, "policer") &&
1537 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1539 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1542 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1543 } else if (dl_argv_match(dl
, "nopolicer") &&
1544 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1546 opts
->trap_policer_id
= 0;
1547 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1548 } else if (dl_argv_match(dl
, "rate") &&
1549 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1551 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1554 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1555 } else if (dl_argv_match(dl
, "burst") &&
1556 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1558 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1561 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1563 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1568 opts
->present
= o_found
;
1570 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1572 opts
->present
|= DL_OPT_SB
;
1575 return dl_args_finding_required_validate(o_required
, o_found
);
1578 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1580 struct dl_opts
*opts
= &dl
->opts
;
1582 if (opts
->present
& DL_OPT_HANDLE
) {
1583 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1584 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1585 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1586 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1587 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1588 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1590 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1591 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1592 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1593 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1596 if (opts
->present
& DL_OPT_PORT_TYPE
)
1597 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1599 if (opts
->present
& DL_OPT_PORT_COUNT
)
1600 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1602 if (opts
->present
& DL_OPT_SB
)
1603 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1605 if (opts
->present
& DL_OPT_SB_POOL
)
1606 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1607 opts
->sb_pool_index
);
1608 if (opts
->present
& DL_OPT_SB_SIZE
)
1609 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1610 opts
->sb_pool_size
);
1611 if (opts
->present
& DL_OPT_SB_TYPE
)
1612 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1613 opts
->sb_pool_type
);
1614 if (opts
->present
& DL_OPT_SB_THTYPE
)
1615 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1616 opts
->sb_pool_thtype
);
1617 if (opts
->present
& DL_OPT_SB_TH
)
1618 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1619 opts
->sb_threshold
);
1620 if (opts
->present
& DL_OPT_SB_TC
)
1621 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1623 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1624 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1625 opts
->eswitch_mode
);
1626 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1627 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1628 opts
->eswitch_inline_mode
);
1629 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1630 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1631 opts
->dpipe_table_name
);
1632 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1633 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1634 opts
->dpipe_counters_enabled
);
1635 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1636 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1637 opts
->eswitch_encap_mode
);
1638 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1639 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1641 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1642 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1643 opts
->resource_size
);
1644 if (opts
->present
& DL_OPT_PARAM_NAME
)
1645 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1647 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1648 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1650 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1651 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1652 opts
->region_snapshot_id
);
1653 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1654 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1655 opts
->region_address
);
1656 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1657 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1658 opts
->region_length
);
1659 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1660 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1661 opts
->flash_file_name
);
1662 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1663 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1664 opts
->flash_component
);
1665 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1666 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1667 opts
->reporter_name
);
1668 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1669 mnl_attr_put_u64(nlh
,
1670 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1671 opts
->reporter_graceful_period
);
1672 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1673 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1674 opts
->reporter_auto_recover
);
1675 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_DUMP
)
1676 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
1677 opts
->reporter_auto_dump
);
1678 if (opts
->present
& DL_OPT_TRAP_NAME
)
1679 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1681 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1682 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1683 opts
->trap_group_name
);
1684 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1685 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1687 if (opts
->present
& DL_OPT_NETNS
)
1688 mnl_attr_put_u32(nlh
,
1689 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1690 DEVLINK_ATTR_NETNS_FD
,
1692 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1693 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1694 opts
->trap_policer_id
);
1695 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1696 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1697 opts
->trap_policer_rate
);
1698 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1699 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1700 opts
->trap_policer_burst
);
1703 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1704 uint64_t o_required
, uint64_t o_optional
)
1708 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1711 dl_opts_put(nlh
, dl
);
1715 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1717 struct dl_opts
*opts
= &dl
->opts
;
1718 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1719 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1720 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1721 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1723 if (opts
->present
& DL_OPT_HANDLE
&&
1724 attr_bus_name
&& attr_dev_name
) {
1725 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1726 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1728 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1729 strcmp(dev_name
, opts
->dev_name
) != 0)
1732 if (opts
->present
& DL_OPT_HANDLEP
&&
1733 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1734 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1735 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1736 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1738 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1739 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1740 port_index
!= opts
->port_index
)
1743 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1744 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1746 if (sb_index
!= opts
->sb_index
)
1752 static void cmd_dev_help(void)
1754 pr_err("Usage: devlink dev show [ DEV ]\n");
1755 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1756 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1757 pr_err(" [ encap-mode { none | basic } ]\n");
1758 pr_err(" devlink dev eswitch show DEV\n");
1759 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1760 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1761 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1762 pr_err(" devlink dev info [ DEV ]\n");
1763 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1766 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1767 const char *dev_name
)
1769 if (!dl
->arr_last
.present
)
1771 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1772 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1775 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1776 const char *dev_name
)
1778 dl
->arr_last
.present
= true;
1779 free(dl
->arr_last
.dev_name
);
1780 free(dl
->arr_last
.bus_name
);
1781 dl
->arr_last
.bus_name
= strdup(bus_name
);
1782 dl
->arr_last
.dev_name
= strdup(dev_name
);
1785 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1786 const char *dev_name
)
1788 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1791 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1792 const char *dev_name
)
1794 return dl
->arr_last
.present
&&
1795 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1798 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1799 bool content
, bool array
)
1801 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1802 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1805 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1807 if (dl
->json_output
) {
1809 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1810 close_json_array(PRINT_JSON
, NULL
);
1811 if (should_arr_last_handle_start(dl
, bus_name
,
1813 open_json_array(PRINT_JSON
, buf
);
1814 open_json_object(NULL
);
1815 arr_last_handle_set(dl
, bus_name
, dev_name
);
1817 open_json_object(NULL
);
1820 open_json_object(buf
);
1824 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1825 __pr_out_indent_dec();
1826 if (should_arr_last_handle_start(dl
, bus_name
,
1828 pr_out("%s%s", buf
, content
? ":" : "");
1830 __pr_out_indent_inc();
1831 arr_last_handle_set(dl
, bus_name
, dev_name
);
1834 pr_out("%s%s", buf
, content
? ":" : "");
1839 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1841 __pr_out_handle_start(dl
, tb
, true, true);
1844 static void pr_out_handle_end(struct dl
*dl
)
1846 if (dl
->json_output
)
1847 close_json_object();
1852 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1854 __pr_out_handle_start(dl
, tb
, false, false);
1855 pr_out_handle_end(dl
);
1858 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1859 const char *dev_name
, uint32_t port_index
)
1861 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1862 dl
->arr_last
.port_index
== port_index
;
1865 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1866 const char *dev_name
, uint32_t port_index
)
1868 arr_last_handle_set(dl
, bus_name
, dev_name
);
1869 dl
->arr_last
.port_index
= port_index
;
1872 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1873 const char *bus_name
,
1874 const char *dev_name
,
1875 uint32_t port_index
)
1877 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1880 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1881 const char *bus_name
,
1882 const char *dev_name
,
1883 uint32_t port_index
)
1885 return dl
->arr_last
.present
&&
1886 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1889 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1890 const char *dev_name
,
1891 uint32_t port_index
, bool try_nice
,
1894 static char buf
[64];
1895 char *ifname
= NULL
;
1897 if (dl
->no_nice_names
|| !try_nice
||
1898 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1899 port_index
, &ifname
) != 0)
1900 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1902 sprintf(buf
, "%s", ifname
);
1904 if (dl
->json_output
) {
1906 if (should_arr_last_port_handle_end(dl
, bus_name
,
1909 close_json_array(PRINT_JSON
, NULL
);
1910 if (should_arr_last_port_handle_start(dl
, bus_name
,
1913 open_json_array(PRINT_JSON
, buf
);
1914 open_json_object(NULL
);
1915 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1918 open_json_object(NULL
);
1921 open_json_object(buf
);
1928 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1930 const char *bus_name
;
1931 const char *dev_name
;
1932 uint32_t port_index
;
1934 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1935 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1936 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1937 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1940 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1942 const char *bus_name
;
1943 const char *dev_name
;
1944 uint32_t port_index
;
1946 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1947 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1948 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1949 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1952 static void pr_out_port_handle_end(struct dl
*dl
)
1954 if (dl
->json_output
)
1955 close_json_object();
1960 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1962 __pr_out_indent_newline(dl
);
1963 if (val
== (uint64_t) -1)
1964 return print_string_name_value(name
, "unlimited");
1966 if (dl
->json_output
)
1967 print_u64(PRINT_JSON
, name
, NULL
, val
);
1969 pr_out("%s %"PRIu64
, name
, val
);
1972 static bool is_binary_eol(int i
)
1977 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1982 if (dl
->json_output
)
1983 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1985 pr_out("%02x ", data
[i
]);
1987 if (!dl
->json_output
&& is_binary_eol(i
))
1990 if (!dl
->json_output
&& !is_binary_eol(i
))
1994 static void pr_out_name(struct dl
*dl
, const char *name
)
1996 __pr_out_indent_newline(dl
);
1997 if (dl
->json_output
)
1998 print_string(PRINT_JSON
, name
, NULL
, NULL
);
2000 pr_out("%s:", name
);
2003 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
2005 if (dl
->json_output
) {
2006 print_uint(PRINT_JSON
, "address", NULL
, addr
);
2007 open_json_array(PRINT_JSON
, "data");
2011 static void pr_out_region_chunk_end(struct dl
*dl
)
2013 if (dl
->json_output
)
2014 close_json_array(PRINT_JSON
, NULL
);
2017 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2020 static uint64_t align_val
;
2023 pr_out_region_chunk_start(dl
, addr
);
2025 if (!dl
->json_output
)
2026 if (!(align_val
% 16))
2027 pr_out("%s%016"PRIx64
" ",
2028 align_val
? "\n" : "",
2033 if (dl
->json_output
)
2034 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2036 pr_out("%02x ", data
[i
]);
2041 pr_out_region_chunk_end(dl
);
2044 static void pr_out_section_start(struct dl
*dl
, const char *name
)
2046 if (dl
->json_output
) {
2047 open_json_object(NULL
);
2048 open_json_object(name
);
2052 static void pr_out_section_end(struct dl
*dl
)
2054 if (dl
->json_output
) {
2055 if (dl
->arr_last
.present
)
2056 close_json_array(PRINT_JSON
, NULL
);
2057 close_json_object();
2058 close_json_object();
2062 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2064 if (dl
->json_output
) {
2065 open_json_array(PRINT_JSON
, name
);
2067 __pr_out_indent_inc();
2069 pr_out("%s:", name
);
2070 __pr_out_indent_inc();
2075 static void pr_out_array_end(struct dl
*dl
)
2077 if (dl
->json_output
) {
2078 close_json_array(PRINT_JSON
, NULL
);
2080 __pr_out_indent_dec();
2081 __pr_out_indent_dec();
2085 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2087 if (dl
->json_output
) {
2088 open_json_object(name
);
2090 __pr_out_indent_inc();
2092 pr_out("%s:", name
);
2093 __pr_out_indent_inc();
2098 static void pr_out_object_end(struct dl
*dl
)
2100 if (dl
->json_output
) {
2101 close_json_object();
2103 __pr_out_indent_dec();
2104 __pr_out_indent_dec();
2108 static void pr_out_entry_start(struct dl
*dl
)
2110 if (dl
->json_output
)
2111 open_json_object(NULL
);
2114 static void pr_out_entry_end(struct dl
*dl
)
2116 if (dl
->json_output
)
2117 close_json_object();
2122 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2124 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2130 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2131 if (err
!= MNL_CB_OK
)
2134 pr_out_object_start(dl
, "stats");
2135 pr_out_object_start(dl
, "rx");
2136 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2137 pr_out_u64(dl
, "bytes",
2138 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2139 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2140 pr_out_u64(dl
, "packets",
2141 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2142 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2143 pr_out_u64(dl
, "dropped",
2144 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2145 pr_out_object_end(dl
);
2146 pr_out_object_end(dl
);
2149 static const char *param_cmode_name(uint8_t cmode
)
2152 case DEVLINK_PARAM_CMODE_RUNTIME
:
2153 return PARAM_CMODE_RUNTIME_STR
;
2154 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2155 return PARAM_CMODE_DRIVERINIT_STR
;
2156 case DEVLINK_PARAM_CMODE_PERMANENT
:
2157 return PARAM_CMODE_PERMANENT_STR
;
2158 default: return "<unknown type>";
2162 static const char *eswitch_mode_name(uint32_t mode
)
2165 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2166 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2167 default: return "<unknown mode>";
2171 static const char *eswitch_inline_mode_name(uint32_t mode
)
2174 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2175 return ESWITCH_INLINE_MODE_NONE
;
2176 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2177 return ESWITCH_INLINE_MODE_LINK
;
2178 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2179 return ESWITCH_INLINE_MODE_NETWORK
;
2180 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2181 return ESWITCH_INLINE_MODE_TRANSPORT
;
2183 return "<unknown mode>";
2187 static const char *eswitch_encap_mode_name(uint32_t mode
)
2190 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2191 return ESWITCH_ENCAP_MODE_NONE
;
2192 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2193 return ESWITCH_ENCAP_MODE_BASIC
;
2195 return "<unknown mode>";
2199 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2201 __pr_out_handle_start(dl
, tb
, true, false);
2203 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2204 check_indent_newline(dl
);
2205 print_string(PRINT_ANY
, "mode", "mode %s",
2206 eswitch_mode_name(mnl_attr_get_u16(
2207 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2209 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2210 check_indent_newline(dl
);
2211 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2212 eswitch_inline_mode_name(mnl_attr_get_u8(
2213 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2215 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2216 check_indent_newline(dl
);
2217 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2218 eswitch_encap_mode_name(mnl_attr_get_u8(
2219 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2222 pr_out_handle_end(dl
);
2225 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2227 struct dl
*dl
= data
;
2228 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2229 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2231 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2232 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2233 return MNL_CB_ERROR
;
2234 pr_out_eswitch(dl
, tb
);
2238 static int cmd_dev_eswitch_show(struct dl
*dl
)
2240 struct nlmsghdr
*nlh
;
2243 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2244 NLM_F_REQUEST
| NLM_F_ACK
);
2246 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2250 pr_out_section_start(dl
, "dev");
2251 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2252 pr_out_section_end(dl
);
2256 static int cmd_dev_eswitch_set(struct dl
*dl
)
2258 struct nlmsghdr
*nlh
;
2261 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2262 NLM_F_REQUEST
| NLM_F_ACK
);
2264 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2265 DL_OPT_ESWITCH_MODE
|
2266 DL_OPT_ESWITCH_INLINE_MODE
|
2267 DL_OPT_ESWITCH_ENCAP_MODE
);
2272 if (dl
->opts
.present
== 1) {
2273 pr_err("Need to set at least one option\n");
2277 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2280 static int cmd_dev_eswitch(struct dl
*dl
)
2282 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2285 } else if (dl_argv_match(dl
, "set")) {
2287 return cmd_dev_eswitch_set(dl
);
2288 } else if (dl_argv_match(dl
, "show")) {
2290 return cmd_dev_eswitch_show(dl
);
2292 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2296 struct param_val_conv
{
2302 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2303 uint32_t len
, const char *name
)
2307 for (i
= 0; i
< len
; i
++)
2308 if (!strcmp(param_val_conv
[i
].name
, name
))
2315 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2316 uint32_t len
, const char *name
, const char *vstr
,
2321 for (i
= 0; i
< len
; i
++)
2322 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2323 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2324 *vuint
= param_val_conv
[i
].vuint
;
2332 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2333 uint32_t len
, const char *name
, uint32_t vuint
,
2338 for (i
= 0; i
< len
; i
++)
2339 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2340 param_val_conv
[i
].vuint
== vuint
) {
2341 *vstr
= param_val_conv
[i
].vstr
;
2348 static const struct param_val_conv param_val_conv
[] = {
2350 .name
= "fw_load_policy",
2352 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2355 .name
= "fw_load_policy",
2357 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2360 .name
= "reset_dev_on_drv_probe",
2362 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2365 .name
= "fw_load_policy",
2367 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2370 .name
= "reset_dev_on_drv_probe",
2372 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2375 .name
= "reset_dev_on_drv_probe",
2377 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2380 .name
= "reset_dev_on_drv_probe",
2382 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2386 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2388 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2389 int nla_type
, struct nlattr
*nl
)
2391 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2392 struct nlattr
*val_attr
;
2397 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2398 if (err
!= MNL_CB_OK
)
2401 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2402 (nla_type
!= MNL_TYPE_FLAG
&&
2403 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2406 check_indent_newline(dl
);
2407 print_string(PRINT_ANY
, "cmode", "cmode %s",
2408 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2410 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2412 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2418 err
= param_val_conv_str_get(param_val_conv
,
2421 mnl_attr_get_u8(val_attr
),
2425 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2427 print_uint(PRINT_ANY
, "value", " value %u",
2428 mnl_attr_get_u8(val_attr
));
2433 err
= param_val_conv_str_get(param_val_conv
,
2436 mnl_attr_get_u16(val_attr
),
2440 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2442 print_uint(PRINT_ANY
, "value", " value %u",
2443 mnl_attr_get_u16(val_attr
));
2448 err
= param_val_conv_str_get(param_val_conv
,
2451 mnl_attr_get_u32(val_attr
),
2455 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2457 print_uint(PRINT_ANY
, "value", " value %u",
2458 mnl_attr_get_u32(val_attr
));
2461 case MNL_TYPE_STRING
:
2462 print_string(PRINT_ANY
, "value", " value %s",
2463 mnl_attr_get_str(val_attr
));
2466 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2471 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2473 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2474 struct nlattr
*param_value_attr
;
2475 const char *nla_name
;
2479 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2480 if (err
!= MNL_CB_OK
)
2482 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2483 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2484 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2488 pr_out_handle_start_arr(dl
, tb
);
2490 __pr_out_handle_start(dl
, tb
, true, false);
2492 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2494 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2495 check_indent_newline(dl
);
2496 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2497 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2498 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2500 print_string(PRINT_ANY
, "type", "type %s", "generic");
2502 pr_out_array_start(dl
, "values");
2503 mnl_attr_for_each_nested(param_value_attr
,
2504 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2505 pr_out_entry_start(dl
);
2506 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2507 pr_out_entry_end(dl
);
2509 pr_out_array_end(dl
);
2510 pr_out_handle_end(dl
);
2513 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2515 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2516 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2517 struct dl
*dl
= data
;
2519 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2520 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2521 !tb
[DEVLINK_ATTR_PARAM
])
2522 return MNL_CB_ERROR
;
2523 pr_out_param(dl
, tb
, true);
2539 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2541 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2542 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2543 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2544 struct nlattr
*param_value_attr
;
2545 enum devlink_param_cmode cmode
;
2546 struct param_ctx
*ctx
= data
;
2547 struct dl
*dl
= ctx
->dl
;
2551 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2552 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2553 !tb
[DEVLINK_ATTR_PARAM
])
2554 return MNL_CB_ERROR
;
2556 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2557 if (err
!= MNL_CB_OK
)
2558 return MNL_CB_ERROR
;
2560 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2561 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2562 return MNL_CB_ERROR
;
2564 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2565 mnl_attr_for_each_nested(param_value_attr
,
2566 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2567 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2568 struct nlattr
*val_attr
;
2570 err
= mnl_attr_parse_nested(param_value_attr
,
2571 attr_cb
, nla_value
);
2572 if (err
!= MNL_CB_OK
)
2573 return MNL_CB_ERROR
;
2575 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2576 (nla_type
!= MNL_TYPE_FLAG
&&
2577 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2578 return MNL_CB_ERROR
;
2580 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2581 if (cmode
== dl
->opts
.cmode
) {
2582 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2585 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2588 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2591 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2593 case MNL_TYPE_STRING
:
2594 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2597 ctx
->value
.vbool
= val_attr
? true : false;
2603 ctx
->nla_type
= nla_type
;
2607 static int cmd_dev_param_set(struct dl
*dl
)
2609 struct param_ctx ctx
= {};
2610 struct nlmsghdr
*nlh
;
2618 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2620 DL_OPT_PARAM_VALUE
|
2621 DL_OPT_PARAM_CMODE
, 0);
2625 /* Get value type */
2626 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2627 NLM_F_REQUEST
| NLM_F_ACK
);
2628 dl_opts_put(nlh
, dl
);
2631 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2635 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2636 NLM_F_REQUEST
| NLM_F_ACK
);
2637 dl_opts_put(nlh
, dl
);
2639 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2640 dl
->opts
.param_name
);
2642 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2643 switch (ctx
.nla_type
) {
2646 err
= param_val_conv_uint_get(param_val_conv
,
2648 dl
->opts
.param_name
,
2649 dl
->opts
.param_value
,
2653 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2656 goto err_param_value_parse
;
2657 if (val_u8
== ctx
.value
.vu8
)
2659 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2663 err
= param_val_conv_uint_get(param_val_conv
,
2665 dl
->opts
.param_name
,
2666 dl
->opts
.param_value
,
2670 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2673 goto err_param_value_parse
;
2674 if (val_u16
== ctx
.value
.vu16
)
2676 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2680 err
= param_val_conv_uint_get(param_val_conv
,
2682 dl
->opts
.param_name
,
2683 dl
->opts
.param_value
,
2686 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2688 goto err_param_value_parse
;
2689 if (val_u32
== ctx
.value
.vu32
)
2691 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2694 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2696 goto err_param_value_parse
;
2697 if (val_bool
== ctx
.value
.vbool
)
2700 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2703 case MNL_TYPE_STRING
:
2704 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2705 dl
->opts
.param_value
);
2706 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2710 printf("Value type not supported\n");
2713 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2715 err_param_value_parse
:
2716 pr_err("Value \"%s\" is not a number or not within range\n",
2717 dl
->opts
.param_value
);
2721 static int cmd_dev_param_show(struct dl
*dl
)
2723 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2724 struct nlmsghdr
*nlh
;
2727 if (dl_argc(dl
) == 0)
2728 flags
|= NLM_F_DUMP
;
2730 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2732 if (dl_argc(dl
) > 0) {
2733 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2734 DL_OPT_PARAM_NAME
, 0);
2739 pr_out_section_start(dl
, "param");
2740 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2741 pr_out_section_end(dl
);
2745 static int cmd_dev_param(struct dl
*dl
)
2747 if (dl_argv_match(dl
, "help")) {
2750 } else if (dl_argv_match(dl
, "show") ||
2751 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2753 return cmd_dev_param_show(dl
);
2754 } else if (dl_argv_match(dl
, "set")) {
2756 return cmd_dev_param_set(dl
);
2758 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2761 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2763 struct dl
*dl
= data
;
2764 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2765 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2766 uint8_t reload_failed
= 0;
2768 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2769 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2770 return MNL_CB_ERROR
;
2772 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2773 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2775 if (reload_failed
) {
2776 __pr_out_handle_start(dl
, tb
, true, false);
2777 check_indent_newline(dl
);
2778 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2779 pr_out_handle_end(dl
);
2781 pr_out_handle(dl
, tb
);
2787 static int cmd_dev_show(struct dl
*dl
)
2789 struct nlmsghdr
*nlh
;
2790 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2793 if (dl_argc(dl
) == 0)
2794 flags
|= NLM_F_DUMP
;
2796 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2798 if (dl_argc(dl
) > 0) {
2799 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2804 pr_out_section_start(dl
, "dev");
2805 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2806 pr_out_section_end(dl
);
2810 static int cmd_dev_reload(struct dl
*dl
)
2812 struct nlmsghdr
*nlh
;
2815 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2820 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2821 NLM_F_REQUEST
| NLM_F_ACK
);
2823 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2827 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2830 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2831 const char *name
, int type
)
2833 struct nlattr
*version
;
2835 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2836 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2837 const char *ver_value
;
2838 const char *ver_name
;
2841 if (mnl_attr_get_type(version
) != type
)
2844 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2845 if (err
!= MNL_CB_OK
)
2848 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2849 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2853 pr_out_object_start(dl
, name
);
2857 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2858 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2860 check_indent_newline(dl
);
2861 print_string_name_value(ver_name
, ver_value
);
2862 if (!dl
->json_output
)
2867 pr_out_object_end(dl
);
2870 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2871 struct nlattr
**tb
, bool has_versions
)
2873 __pr_out_handle_start(dl
, tb
, true, false);
2875 __pr_out_indent_inc();
2876 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2877 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2879 if (!dl
->json_output
)
2881 check_indent_newline(dl
);
2882 print_string(PRINT_ANY
, "driver", "driver %s",
2883 mnl_attr_get_str(nla_drv
));
2886 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2887 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2889 if (!dl
->json_output
)
2891 check_indent_newline(dl
);
2892 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2893 mnl_attr_get_str(nla_sn
));
2895 __pr_out_indent_dec();
2898 pr_out_object_start(dl
, "versions");
2900 pr_out_versions_single(dl
, nlh
, "fixed",
2901 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2902 pr_out_versions_single(dl
, nlh
, "running",
2903 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2904 pr_out_versions_single(dl
, nlh
, "stored",
2905 DEVLINK_ATTR_INFO_VERSION_STORED
);
2907 pr_out_object_end(dl
);
2910 pr_out_handle_end(dl
);
2913 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2915 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2916 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2917 bool has_versions
, has_info
;
2918 struct dl
*dl
= data
;
2920 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2922 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2923 return MNL_CB_ERROR
;
2925 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2926 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2927 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2928 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2929 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2933 pr_out_info(dl
, nlh
, tb
, has_versions
);
2938 static int cmd_dev_info(struct dl
*dl
)
2940 struct nlmsghdr
*nlh
;
2941 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2944 if (dl_argv_match(dl
, "help")) {
2949 if (dl_argc(dl
) == 0)
2950 flags
|= NLM_F_DUMP
;
2952 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2954 if (dl_argc(dl
) > 0) {
2955 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2960 pr_out_section_start(dl
, "info");
2961 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2962 pr_out_section_end(dl
);
2966 struct cmd_dev_flash_status_ctx
{
2969 char *last_component
;
2970 uint8_t not_first
:1,
2976 static int nullstrcmp(const char *str1
, const char *str2
)
2979 return strcmp(str1
, str2
);
2982 return str1
? 1 : -1;
2985 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2987 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2988 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2989 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2990 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2991 const char *component
= NULL
;
2992 uint64_t done
= 0, total
= 0;
2993 const char *msg
= NULL
;
2994 const char *bus_name
;
2995 const char *dev_name
;
2997 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2998 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
3001 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3002 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3003 return MNL_CB_ERROR
;
3004 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
3005 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
3006 if (strcmp(bus_name
, opts
->bus_name
) ||
3007 strcmp(dev_name
, opts
->dev_name
))
3008 return MNL_CB_ERROR
;
3010 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
3012 free(ctx
->last_msg
);
3013 free(ctx
->last_component
);
3014 ctx
->received_end
= 1;
3018 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3019 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3020 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3021 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3022 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3023 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3024 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3025 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3027 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3028 !nullstrcmp(component
, ctx
->last_component
) &&
3029 ctx
->last_pc
&& ctx
->not_first
) {
3030 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3035 pr_out("[%s] ", component
);
3036 free(ctx
->last_component
);
3037 ctx
->last_component
= strdup(component
);
3041 free(ctx
->last_msg
);
3042 ctx
->last_msg
= strdup(msg
);
3046 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3057 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3058 struct mnlg_socket
*nlg_ntf
,
3061 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3068 for (i
= 0; i
< 3; i
++)
3070 FD_SET(pipe_r
, &fds
[0]);
3072 FD_SET(nlfd
, &fds
[0]);
3076 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3079 pr_err("select() failed\n");
3082 if (FD_ISSET(nlfd
, &fds
[0])) {
3083 err
= _mnlg_socket_recv_run(nlg_ntf
,
3084 cmd_dev_flash_status_cb
, ctx
);
3088 if (FD_ISSET(pipe_r
, &fds
[0])) {
3089 err
= read(pipe_r
, &err2
, sizeof(err2
));
3091 pr_err("Failed to read pipe\n");
3096 ctx
->flash_done
= 1;
3102 static int cmd_dev_flash(struct dl
*dl
)
3104 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3105 struct mnlg_socket
*nlg_ntf
;
3106 struct nlmsghdr
*nlh
;
3112 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3117 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3118 NLM_F_REQUEST
| NLM_F_ACK
);
3120 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3121 DL_OPT_FLASH_COMPONENT
);
3125 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3129 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3133 err
= pipe(pipe_fds
);
3136 pipe_r
= pipe_fds
[0];
3137 pipe_w
= pipe_fds
[1];
3145 /* In child, just execute the flash and pass returned
3146 * value through pipe once it is done.
3151 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3152 cc
= write(pipe_w
, &err
, sizeof(err
));
3154 exit(cc
!= sizeof(err
));
3159 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3162 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3164 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3167 mnlg_socket_close(nlg_ntf
);
3171 static int cmd_dev(struct dl
*dl
)
3173 if (dl_argv_match(dl
, "help")) {
3176 } else if (dl_argv_match(dl
, "show") ||
3177 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3179 return cmd_dev_show(dl
);
3180 } else if (dl_argv_match(dl
, "eswitch")) {
3182 return cmd_dev_eswitch(dl
);
3183 } else if (dl_argv_match(dl
, "reload")) {
3185 return cmd_dev_reload(dl
);
3186 } else if (dl_argv_match(dl
, "param")) {
3188 return cmd_dev_param(dl
);
3189 } else if (dl_argv_match(dl
, "info")) {
3191 return cmd_dev_info(dl
);
3192 } else if (dl_argv_match(dl
, "flash")) {
3194 return cmd_dev_flash(dl
);
3196 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3200 static void cmd_port_help(void)
3202 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3203 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3204 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3205 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3208 static const char *port_type_name(uint32_t type
)
3211 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3212 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3213 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3214 case DEVLINK_PORT_TYPE_IB
: return "ib";
3215 default: return "<unknown type>";
3219 static const char *port_flavour_name(uint16_t flavour
)
3222 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3224 case DEVLINK_PORT_FLAVOUR_CPU
:
3226 case DEVLINK_PORT_FLAVOUR_DSA
:
3228 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3230 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3232 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3235 return "<unknown flavour>";
3239 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3243 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3244 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3245 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3247 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3248 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3249 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3253 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3255 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3256 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3258 pr_out_port_handle_start(dl
, tb
, false);
3259 check_indent_newline(dl
);
3261 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3263 print_string(PRINT_ANY
, "type", "type %s",
3264 port_type_name(port_type
));
3266 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3268 if (port_type
!= des_port_type
)
3269 print_string(PRINT_ANY
, "des_type", " des_type %s",
3270 port_type_name(des_port_type
));
3273 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3274 print_string(PRINT_ANY
, "netdev", " netdev %s",
3275 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3277 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3278 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3279 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3281 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3282 uint16_t port_flavour
=
3283 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3285 print_string(PRINT_ANY
, "flavour", " flavour %s",
3286 port_flavour_name(port_flavour
));
3288 switch (port_flavour
) {
3289 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3290 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3291 pr_out_port_pfvf_num(dl
, tb
);
3297 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3298 uint32_t port_number
;
3300 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3301 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3303 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3304 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3305 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3306 pr_out_port_handle_end(dl
);
3309 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3311 struct dl
*dl
= data
;
3312 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3313 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3315 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3316 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3317 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3318 return MNL_CB_ERROR
;
3319 pr_out_port(dl
, tb
);
3323 static int cmd_port_show(struct dl
*dl
)
3325 struct nlmsghdr
*nlh
;
3326 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3329 if (dl_argc(dl
) == 0)
3330 flags
|= NLM_F_DUMP
;
3332 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3334 if (dl_argc(dl
) > 0) {
3335 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3340 pr_out_section_start(dl
, "port");
3341 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3342 pr_out_section_end(dl
);
3346 static int cmd_port_set(struct dl
*dl
)
3348 struct nlmsghdr
*nlh
;
3351 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3352 NLM_F_REQUEST
| NLM_F_ACK
);
3354 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3358 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3361 static int cmd_port_split(struct dl
*dl
)
3363 struct nlmsghdr
*nlh
;
3366 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3367 NLM_F_REQUEST
| NLM_F_ACK
);
3369 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3373 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3376 static int cmd_port_unsplit(struct dl
*dl
)
3378 struct nlmsghdr
*nlh
;
3381 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3382 NLM_F_REQUEST
| NLM_F_ACK
);
3384 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3388 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3391 static int cmd_port(struct dl
*dl
)
3393 if (dl_argv_match(dl
, "help")) {
3396 } else if (dl_argv_match(dl
, "show") ||
3397 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3399 return cmd_port_show(dl
);
3400 } else if (dl_argv_match(dl
, "set")) {
3402 return cmd_port_set(dl
);
3403 } else if (dl_argv_match(dl
, "split")) {
3405 return cmd_port_split(dl
);
3406 } else if (dl_argv_match(dl
, "unsplit")) {
3408 return cmd_port_unsplit(dl
);
3410 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3414 static void cmd_sb_help(void)
3416 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3417 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3418 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3419 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3420 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3421 pr_err(" pool POOL_INDEX ]\n");
3422 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3423 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3424 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3425 pr_err(" type { ingress | egress } ]\n");
3426 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3427 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3428 pr_err(" th THRESHOLD\n");
3429 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3430 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3431 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3434 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3436 pr_out_handle_start_arr(dl
, tb
);
3437 check_indent_newline(dl
);
3438 print_uint(PRINT_ANY
, "sb", "sb %u",
3439 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3440 print_uint(PRINT_ANY
, "size", " size %u",
3441 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3442 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3443 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3444 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3445 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3446 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3447 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3448 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3449 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3450 pr_out_handle_end(dl
);
3453 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3455 struct dl
*dl
= data
;
3456 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3457 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3459 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3460 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3461 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3462 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3463 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3464 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3465 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3466 return MNL_CB_ERROR
;
3471 static int cmd_sb_show(struct dl
*dl
)
3473 struct nlmsghdr
*nlh
;
3474 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3477 if (dl_argc(dl
) == 0)
3478 flags
|= NLM_F_DUMP
;
3480 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3482 if (dl_argc(dl
) > 0) {
3483 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3488 pr_out_section_start(dl
, "sb");
3489 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3490 pr_out_section_end(dl
);
3494 static const char *pool_type_name(uint8_t type
)
3497 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3498 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3499 default: return "<unknown type>";
3503 static const char *threshold_type_name(uint8_t type
)
3506 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3507 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3508 default: return "<unknown type>";
3512 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3514 pr_out_handle_start_arr(dl
, tb
);
3515 check_indent_newline(dl
);
3516 print_uint(PRINT_ANY
, "sb", "sb %u",
3517 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3518 print_uint(PRINT_ANY
, "pool", " pool %u",
3519 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3520 print_string(PRINT_ANY
, "type", " type %s",
3521 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3522 print_uint(PRINT_ANY
, "size", " size %u",
3523 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3524 print_string(PRINT_ANY
, "thtype", " thtype %s",
3525 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3526 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3527 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3528 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3529 pr_out_handle_end(dl
);
3532 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3534 struct dl
*dl
= data
;
3535 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3536 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3538 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3539 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3540 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3541 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3542 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3543 return MNL_CB_ERROR
;
3544 pr_out_sb_pool(dl
, tb
);
3548 static int cmd_sb_pool_show(struct dl
*dl
)
3550 struct nlmsghdr
*nlh
;
3551 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3554 if (dl_argc(dl
) == 0)
3555 flags
|= NLM_F_DUMP
;
3557 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3559 if (dl_argc(dl
) > 0) {
3560 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3566 pr_out_section_start(dl
, "pool");
3567 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3568 pr_out_section_end(dl
);
3572 static int cmd_sb_pool_set(struct dl
*dl
)
3574 struct nlmsghdr
*nlh
;
3577 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3578 NLM_F_REQUEST
| NLM_F_ACK
);
3580 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3581 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3585 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3588 static int cmd_sb_pool(struct dl
*dl
)
3590 if (dl_argv_match(dl
, "help")) {
3593 } else if (dl_argv_match(dl
, "show") ||
3594 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3596 return cmd_sb_pool_show(dl
);
3597 } else if (dl_argv_match(dl
, "set")) {
3599 return cmd_sb_pool_set(dl
);
3601 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3605 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3607 pr_out_port_handle_start_arr(dl
, tb
, true);
3608 check_indent_newline(dl
);
3609 print_uint(PRINT_ANY
, "sb", "sb %u",
3610 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3611 print_uint(PRINT_ANY
, "pool", " pool %u",
3612 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3613 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3614 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3615 pr_out_port_handle_end(dl
);
3618 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3620 struct dl
*dl
= data
;
3621 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3622 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3624 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3625 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3626 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3627 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3628 return MNL_CB_ERROR
;
3629 pr_out_sb_port_pool(dl
, tb
);
3633 static int cmd_sb_port_pool_show(struct dl
*dl
)
3635 struct nlmsghdr
*nlh
;
3636 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3639 if (dl_argc(dl
) == 0)
3640 flags
|= NLM_F_DUMP
;
3642 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3644 if (dl_argc(dl
) > 0) {
3645 err
= dl_argv_parse_put(nlh
, dl
,
3646 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3652 pr_out_section_start(dl
, "port_pool");
3653 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3654 pr_out_section_end(dl
);
3658 static int cmd_sb_port_pool_set(struct dl
*dl
)
3660 struct nlmsghdr
*nlh
;
3663 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3664 NLM_F_REQUEST
| NLM_F_ACK
);
3666 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3667 DL_OPT_SB_TH
, DL_OPT_SB
);
3671 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3674 static int cmd_sb_port_pool(struct dl
*dl
)
3676 if (dl_argv_match(dl
, "help")) {
3679 } else if (dl_argv_match(dl
, "show") ||
3680 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3682 return cmd_sb_port_pool_show(dl
);
3683 } else if (dl_argv_match(dl
, "set")) {
3685 return cmd_sb_port_pool_set(dl
);
3687 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3691 static int cmd_sb_port(struct dl
*dl
)
3693 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3696 } else if (dl_argv_match(dl
, "pool")) {
3698 return cmd_sb_port_pool(dl
);
3700 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3704 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3706 pr_out_port_handle_start_arr(dl
, tb
, true);
3707 check_indent_newline(dl
);
3708 print_uint(PRINT_ANY
, "sb", "sb %u",
3709 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3710 print_uint(PRINT_ANY
, "tc", " tc %u",
3711 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3712 print_string(PRINT_ANY
, "type", " type %s",
3713 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3714 print_uint(PRINT_ANY
, "pool", " pool %u",
3715 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3716 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3717 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3718 pr_out_port_handle_end(dl
);
3721 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3723 struct dl
*dl
= data
;
3724 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3725 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3727 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3728 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3729 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3730 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3731 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3732 return MNL_CB_ERROR
;
3733 pr_out_sb_tc_bind(dl
, tb
);
3737 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3739 struct nlmsghdr
*nlh
;
3740 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3743 if (dl_argc(dl
) == 0)
3744 flags
|= NLM_F_DUMP
;
3746 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3748 if (dl_argc(dl
) > 0) {
3749 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3750 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3755 pr_out_section_start(dl
, "tc_bind");
3756 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3757 pr_out_section_end(dl
);
3761 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3763 struct nlmsghdr
*nlh
;
3766 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3767 NLM_F_REQUEST
| NLM_F_ACK
);
3769 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3770 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3775 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3778 static int cmd_sb_tc_bind(struct dl
*dl
)
3780 if (dl_argv_match(dl
, "help")) {
3783 } else if (dl_argv_match(dl
, "show") ||
3784 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3786 return cmd_sb_tc_bind_show(dl
);
3787 } else if (dl_argv_match(dl
, "set")) {
3789 return cmd_sb_tc_bind_set(dl
);
3791 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3795 static int cmd_sb_tc(struct dl
*dl
)
3797 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3800 } else if (dl_argv_match(dl
, "bind")) {
3802 return cmd_sb_tc_bind(dl
);
3804 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3809 struct list_head list
;
3813 uint32_t bound_pool_index
;
3817 struct list_head list
;
3820 uint32_t port_index
;
3822 struct list_head pool_list
;
3823 struct list_head ing_tc_list
;
3824 struct list_head eg_tc_list
;
3830 struct list_head port_list
;
3833 static struct occ_item
*occ_item_alloc(void)
3835 return calloc(1, sizeof(struct occ_item
));
3838 static void occ_item_free(struct occ_item
*occ_item
)
3843 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3845 struct occ_port
*occ_port
;
3847 occ_port
= calloc(1, sizeof(*occ_port
));
3850 occ_port
->port_index
= port_index
;
3851 INIT_LIST_HEAD(&occ_port
->pool_list
);
3852 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3853 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3857 static void occ_port_free(struct occ_port
*occ_port
)
3859 struct occ_item
*occ_item
, *tmp
;
3861 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3862 occ_item_free(occ_item
);
3863 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3864 occ_item_free(occ_item
);
3865 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3866 occ_item_free(occ_item
);
3869 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3871 struct occ_show
*occ_show
;
3873 occ_show
= calloc(1, sizeof(*occ_show
));
3877 INIT_LIST_HEAD(&occ_show
->port_list
);
3881 static void occ_show_free(struct occ_show
*occ_show
)
3883 struct occ_port
*occ_port
, *tmp
;
3885 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3886 occ_port_free(occ_port
);
3889 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3892 struct occ_port
*occ_port
;
3893 uint32_t port_index
;
3895 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3897 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3898 if (occ_port
->port_index
== port_index
)
3901 occ_port
= occ_port_alloc(port_index
);
3904 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3908 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3911 struct occ_item
*occ_item
;
3914 pr_out_sp(7, " %s:", label
);
3915 list_for_each_entry(occ_item
, list
, list
) {
3916 if ((i
- 1) % 4 == 0 && i
!= 1)
3919 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3920 occ_item
->bound_pool_index
);
3922 pr_out_sp(7, "%2u:", occ_item
->index
);
3923 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3927 if ((i
- 1) % 4 != 0)
3931 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3932 struct list_head
*list
,
3935 struct occ_item
*occ_item
;
3938 open_json_object(label
);
3939 list_for_each_entry(occ_item
, list
, list
) {
3940 sprintf(buf
, "%u", occ_item
->index
);
3941 open_json_object(buf
);
3943 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3944 occ_item
->bound_pool_index
);
3945 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3946 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3947 close_json_object();
3949 close_json_object();
3952 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3954 if (dl
->json_output
) {
3955 pr_out_json_occ_show_item_list(dl
, "pool",
3956 &occ_port
->pool_list
, false);
3957 pr_out_json_occ_show_item_list(dl
, "itc",
3958 &occ_port
->ing_tc_list
, true);
3959 pr_out_json_occ_show_item_list(dl
, "etc",
3960 &occ_port
->eg_tc_list
, true);
3963 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3964 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3965 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3969 static void pr_out_occ_show(struct occ_show
*occ_show
)
3971 struct dl
*dl
= occ_show
->dl
;
3972 struct dl_opts
*opts
= &dl
->opts
;
3973 struct occ_port
*occ_port
;
3975 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3976 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3977 occ_port
->port_index
, true, false);
3978 pr_out_occ_show_port(dl
, occ_port
);
3979 pr_out_port_handle_end(dl
);
3983 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3986 struct occ_port
*occ_port
;
3987 struct occ_item
*occ_item
;
3989 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3992 occ_port
= occ_port_get(occ_show
, tb
);
3994 occ_show
->err
= -ENOMEM
;
3998 occ_item
= occ_item_alloc();
4000 occ_show
->err
= -ENOMEM
;
4003 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4004 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4005 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4006 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
4009 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4011 struct occ_show
*occ_show
= data
;
4012 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4013 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4015 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4016 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4017 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4018 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4019 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4020 return MNL_CB_ERROR
;
4021 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4025 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4028 struct occ_port
*occ_port
;
4029 struct occ_item
*occ_item
;
4032 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4035 occ_port
= occ_port_get(occ_show
, tb
);
4037 occ_show
->err
= -ENOMEM
;
4041 occ_item
= occ_item_alloc();
4043 occ_show
->err
= -ENOMEM
;
4046 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4047 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4048 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4049 occ_item
->bound_pool_index
=
4050 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4051 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4052 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4053 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4054 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4055 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4057 occ_item_free(occ_item
);
4060 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4062 struct occ_show
*occ_show
= data
;
4063 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4064 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4066 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4067 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4068 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4069 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4070 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4071 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4072 return MNL_CB_ERROR
;
4073 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4077 static int cmd_sb_occ_show(struct dl
*dl
)
4079 struct nlmsghdr
*nlh
;
4080 struct occ_show
*occ_show
;
4081 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4084 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4088 occ_show
= occ_show_alloc(dl
);
4092 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4094 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4095 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4099 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4101 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4102 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4106 pr_out_section_start(dl
, "occupancy");
4107 pr_out_occ_show(occ_show
);
4108 pr_out_section_end(dl
);
4111 occ_show_free(occ_show
);
4115 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4117 struct nlmsghdr
*nlh
;
4120 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4121 NLM_F_REQUEST
| NLM_F_ACK
);
4123 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4127 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4130 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4132 struct nlmsghdr
*nlh
;
4135 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4136 NLM_F_REQUEST
| NLM_F_ACK
);
4138 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4142 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4145 static int cmd_sb_occ(struct dl
*dl
)
4147 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4150 } else if (dl_argv_match(dl
, "show") ||
4151 dl_argv_match(dl
, "list")) {
4153 return cmd_sb_occ_show(dl
);
4154 } else if (dl_argv_match(dl
, "snapshot")) {
4156 return cmd_sb_occ_snapshot(dl
);
4157 } else if (dl_argv_match(dl
, "clearmax")) {
4159 return cmd_sb_occ_clearmax(dl
);
4161 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4165 static int cmd_sb(struct dl
*dl
)
4167 if (dl_argv_match(dl
, "help")) {
4170 } else if (dl_argv_match(dl
, "show") ||
4171 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4173 return cmd_sb_show(dl
);
4174 } else if (dl_argv_match(dl
, "pool")) {
4176 return cmd_sb_pool(dl
);
4177 } else if (dl_argv_match(dl
, "port")) {
4179 return cmd_sb_port(dl
);
4180 } else if (dl_argv_match(dl
, "tc")) {
4182 return cmd_sb_tc(dl
);
4183 } else if (dl_argv_match(dl
, "occupancy")) {
4185 return cmd_sb_occ(dl
);
4187 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4191 static const char *cmd_name(uint8_t cmd
)
4194 case DEVLINK_CMD_UNSPEC
: return "unspec";
4195 case DEVLINK_CMD_GET
: return "get";
4196 case DEVLINK_CMD_SET
: return "set";
4197 case DEVLINK_CMD_NEW
: return "new";
4198 case DEVLINK_CMD_DEL
: return "del";
4199 case DEVLINK_CMD_PORT_GET
: return "get";
4200 case DEVLINK_CMD_PORT_SET
: return "set";
4201 case DEVLINK_CMD_PORT_NEW
: return "new";
4202 case DEVLINK_CMD_PORT_DEL
: return "del";
4203 case DEVLINK_CMD_PARAM_GET
: return "get";
4204 case DEVLINK_CMD_PARAM_SET
: return "set";
4205 case DEVLINK_CMD_PARAM_NEW
: return "new";
4206 case DEVLINK_CMD_PARAM_DEL
: return "del";
4207 case DEVLINK_CMD_REGION_GET
: return "get";
4208 case DEVLINK_CMD_REGION_SET
: return "set";
4209 case DEVLINK_CMD_REGION_NEW
: return "new";
4210 case DEVLINK_CMD_REGION_DEL
: return "del";
4211 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4212 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4213 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4214 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4215 case DEVLINK_CMD_TRAP_GET
: return "get";
4216 case DEVLINK_CMD_TRAP_SET
: return "set";
4217 case DEVLINK_CMD_TRAP_NEW
: return "new";
4218 case DEVLINK_CMD_TRAP_DEL
: return "del";
4219 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4220 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4221 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4222 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4223 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4224 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4225 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4226 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4227 default: return "<unknown cmd>";
4231 static const char *cmd_obj(uint8_t cmd
)
4234 case DEVLINK_CMD_UNSPEC
: return "unspec";
4235 case DEVLINK_CMD_GET
:
4236 case DEVLINK_CMD_SET
:
4237 case DEVLINK_CMD_NEW
:
4238 case DEVLINK_CMD_DEL
:
4240 case DEVLINK_CMD_PORT_GET
:
4241 case DEVLINK_CMD_PORT_SET
:
4242 case DEVLINK_CMD_PORT_NEW
:
4243 case DEVLINK_CMD_PORT_DEL
:
4245 case DEVLINK_CMD_PARAM_GET
:
4246 case DEVLINK_CMD_PARAM_SET
:
4247 case DEVLINK_CMD_PARAM_NEW
:
4248 case DEVLINK_CMD_PARAM_DEL
:
4250 case DEVLINK_CMD_REGION_GET
:
4251 case DEVLINK_CMD_REGION_SET
:
4252 case DEVLINK_CMD_REGION_NEW
:
4253 case DEVLINK_CMD_REGION_DEL
:
4255 case DEVLINK_CMD_FLASH_UPDATE
:
4256 case DEVLINK_CMD_FLASH_UPDATE_END
:
4257 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4259 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4261 case DEVLINK_CMD_TRAP_GET
:
4262 case DEVLINK_CMD_TRAP_SET
:
4263 case DEVLINK_CMD_TRAP_NEW
:
4264 case DEVLINK_CMD_TRAP_DEL
:
4266 case DEVLINK_CMD_TRAP_GROUP_GET
:
4267 case DEVLINK_CMD_TRAP_GROUP_SET
:
4268 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4269 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4270 return "trap-group";
4271 case DEVLINK_CMD_TRAP_POLICER_GET
:
4272 case DEVLINK_CMD_TRAP_POLICER_SET
:
4273 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4274 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4275 return "trap-policer";
4276 default: return "<unknown obj>";
4280 static void pr_out_mon_header(uint8_t cmd
)
4282 if (!is_json_context()) {
4283 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4285 open_json_object(NULL
);
4286 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4287 open_json_object(cmd_obj(cmd
));
4291 static void pr_out_mon_footer(void)
4293 if (is_json_context()) {
4294 close_json_object();
4295 close_json_object();
4299 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4301 const char *obj
= cmd_obj(cmd
);
4302 unsigned int index
= 0;
4303 const char *cur_obj
;
4307 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4308 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4314 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4316 __pr_out_handle_start(dl
, tb
, true, false);
4318 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4319 check_indent_newline(dl
);
4320 print_string(PRINT_ANY
, "msg", "msg %s",
4321 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4323 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4324 check_indent_newline(dl
);
4325 print_string(PRINT_ANY
, "component", "component %s",
4326 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4329 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4330 pr_out_u64(dl
, "done",
4331 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4333 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4334 pr_out_u64(dl
, "total",
4335 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4337 pr_out_handle_end(dl
);
4340 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4341 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4342 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4343 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4344 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4346 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4348 struct dl
*dl
= data
;
4349 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4350 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4351 uint8_t cmd
= genl
->cmd
;
4353 if (!cmd_filter_check(dl
, cmd
))
4357 case DEVLINK_CMD_GET
: /* fall through */
4358 case DEVLINK_CMD_SET
: /* fall through */
4359 case DEVLINK_CMD_NEW
: /* fall through */
4360 case DEVLINK_CMD_DEL
:
4361 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4362 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4363 return MNL_CB_ERROR
;
4364 pr_out_mon_header(genl
->cmd
);
4365 pr_out_handle(dl
, tb
);
4366 pr_out_mon_footer();
4368 case DEVLINK_CMD_PORT_GET
: /* fall through */
4369 case DEVLINK_CMD_PORT_SET
: /* fall through */
4370 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4371 case DEVLINK_CMD_PORT_DEL
:
4372 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4373 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4374 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4375 return MNL_CB_ERROR
;
4376 pr_out_mon_header(genl
->cmd
);
4377 pr_out_port(dl
, tb
);
4378 pr_out_mon_footer();
4380 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4381 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4382 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4383 case DEVLINK_CMD_PARAM_DEL
:
4384 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4385 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4386 !tb
[DEVLINK_ATTR_PARAM
])
4387 return MNL_CB_ERROR
;
4388 pr_out_mon_header(genl
->cmd
);
4389 pr_out_param(dl
, tb
, false);
4390 pr_out_mon_footer();
4392 case DEVLINK_CMD_REGION_GET
: /* fall through */
4393 case DEVLINK_CMD_REGION_SET
: /* fall through */
4394 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4395 case DEVLINK_CMD_REGION_DEL
:
4396 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4397 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4398 !tb
[DEVLINK_ATTR_REGION_NAME
])
4399 return MNL_CB_ERROR
;
4400 pr_out_mon_header(genl
->cmd
);
4401 pr_out_region(dl
, tb
);
4402 pr_out_mon_footer();
4404 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4405 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4406 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4407 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4408 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4409 return MNL_CB_ERROR
;
4410 pr_out_mon_header(genl
->cmd
);
4411 pr_out_flash_update(dl
, tb
);
4412 pr_out_mon_footer();
4414 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4415 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4416 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4417 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
4418 return MNL_CB_ERROR
;
4419 pr_out_mon_header(genl
->cmd
);
4420 pr_out_health(dl
, tb
);
4421 pr_out_mon_footer();
4423 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4424 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4425 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4426 case DEVLINK_CMD_TRAP_DEL
:
4427 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4428 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4429 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4430 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4431 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4432 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4433 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4434 !tb
[DEVLINK_ATTR_STATS
])
4435 return MNL_CB_ERROR
;
4436 pr_out_mon_header(genl
->cmd
);
4437 pr_out_trap(dl
, tb
, false);
4438 pr_out_mon_footer();
4440 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4441 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4442 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4443 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4444 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4445 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4446 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4447 !tb
[DEVLINK_ATTR_STATS
])
4448 return MNL_CB_ERROR
;
4449 pr_out_mon_header(genl
->cmd
);
4450 pr_out_trap_group(dl
, tb
, false);
4451 pr_out_mon_footer();
4453 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4454 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4455 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4456 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4457 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4458 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4459 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4460 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4461 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4462 return MNL_CB_ERROR
;
4463 pr_out_mon_header(genl
->cmd
);
4464 pr_out_trap_policer(dl
, tb
, false);
4470 static int cmd_mon_show(struct dl
*dl
)
4473 unsigned int index
= 0;
4474 const char *cur_obj
;
4476 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4477 if (strcmp(cur_obj
, "all") != 0 &&
4478 strcmp(cur_obj
, "dev") != 0 &&
4479 strcmp(cur_obj
, "port") != 0 &&
4480 strcmp(cur_obj
, "health") != 0 &&
4481 strcmp(cur_obj
, "trap") != 0 &&
4482 strcmp(cur_obj
, "trap-group") != 0 &&
4483 strcmp(cur_obj
, "trap-policer") != 0) {
4484 pr_err("Unknown object \"%s\"\n", cur_obj
);
4488 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4491 open_json_object(NULL
);
4492 open_json_array(PRINT_JSON
, "mon");
4493 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
4494 close_json_array(PRINT_JSON
, NULL
);
4495 close_json_object();
4501 static void cmd_mon_help(void)
4503 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4504 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4507 static int cmd_mon(struct dl
*dl
)
4509 if (dl_argv_match(dl
, "help")) {
4513 return cmd_mon_show(dl
);
4516 struct dpipe_field
{
4519 unsigned int bitwidth
;
4520 enum devlink_dpipe_field_mapping_type mapping_type
;
4523 struct dpipe_header
{
4524 struct list_head list
;
4527 struct dpipe_field
*fields
;
4528 unsigned int fields_count
;
4531 struct dpipe_table
{
4532 struct list_head list
;
4534 unsigned int resource_id
;
4535 bool resource_valid
;
4538 struct dpipe_tables
{
4539 struct list_head table_list
;
4549 enum devlink_resource_unit unit
;
4554 struct list_head list
;
4555 struct list_head resource_list
;
4556 struct resource
*parent
;
4560 struct list_head resource_list
;
4563 struct resource_ctx
{
4566 struct resources
*resources
;
4567 struct dpipe_tables
*tables
;
4568 bool print_resources
;
4569 bool pending_change
;
4572 static struct resource
*resource_alloc(void)
4574 struct resource
*resource
;
4576 resource
= calloc(1, sizeof(struct resource
));
4579 INIT_LIST_HEAD(&resource
->resource_list
);
4583 static void resource_free(struct resource
*resource
)
4585 struct resource
*child_resource
, *tmp
;
4587 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4589 free(child_resource
->name
);
4590 resource_free(child_resource
);
4595 static struct resources
*resources_alloc(void)
4597 struct resources
*resources
;
4599 resources
= calloc(1, sizeof(struct resources
));
4602 INIT_LIST_HEAD(&resources
->resource_list
);
4606 static void resources_free(struct resources
*resources
)
4608 struct resource
*resource
, *tmp
;
4610 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4611 resource_free(resource
);
4614 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4616 ctx
->resources
= resources_alloc();
4617 if (!ctx
->resources
)
4623 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4625 resources_free(ctx
->resources
);
4631 struct list_head global_headers
;
4632 struct list_head local_headers
;
4633 struct dpipe_tables
*tables
;
4634 struct resources
*resources
;
4639 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4641 struct dpipe_header
*header
;
4643 header
= calloc(1, sizeof(struct dpipe_header
));
4646 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4647 if (!header
->fields
)
4648 goto err_fields_alloc
;
4649 header
->fields_count
= fields_count
;
4657 static void dpipe_header_free(struct dpipe_header
*header
)
4659 free(header
->fields
);
4663 static void dpipe_header_clear(struct dpipe_header
*header
)
4665 struct dpipe_field
*field
;
4668 for (i
= 0; i
< header
->fields_count
; i
++) {
4669 field
= &header
->fields
[i
];
4675 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4676 struct dpipe_header
*header
, bool global
)
4679 list_add(&header
->list
, &ctx
->global_headers
);
4681 list_add(&header
->list
, &ctx
->local_headers
);
4684 static void dpipe_header_del(struct dpipe_header
*header
)
4686 list_del(&header
->list
);
4689 static struct dpipe_table
*dpipe_table_alloc(void)
4691 return calloc(1, sizeof(struct dpipe_table
));
4694 static void dpipe_table_free(struct dpipe_table
*table
)
4699 static struct dpipe_tables
*dpipe_tables_alloc(void)
4701 struct dpipe_tables
*tables
;
4703 tables
= calloc(1, sizeof(struct dpipe_tables
));
4706 INIT_LIST_HEAD(&tables
->table_list
);
4710 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4712 struct dpipe_table
*table
, *tmp
;
4714 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4715 dpipe_table_free(table
);
4719 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4721 ctx
->tables
= dpipe_tables_alloc();
4726 INIT_LIST_HEAD(&ctx
->global_headers
);
4727 INIT_LIST_HEAD(&ctx
->local_headers
);
4731 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4733 struct dpipe_header
*header
, *tmp
;
4735 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4737 dpipe_header_del(header
);
4738 dpipe_header_clear(header
);
4739 dpipe_header_free(header
);
4741 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4743 dpipe_header_del(header
);
4744 dpipe_header_clear(header
);
4745 dpipe_header_free(header
);
4747 dpipe_tables_free(ctx
->tables
);
4750 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4751 uint32_t header_id
, bool global
)
4753 struct list_head
*header_list
;
4754 struct dpipe_header
*header
;
4757 header_list
= &ctx
->global_headers
;
4759 header_list
= &ctx
->local_headers
;
4760 list_for_each_entry(header
, header_list
, list
) {
4761 if (header
->id
!= header_id
)
4763 return header
->name
;
4768 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4770 uint32_t field_id
, bool global
)
4772 struct list_head
*header_list
;
4773 struct dpipe_header
*header
;
4776 header_list
= &ctx
->global_headers
;
4778 header_list
= &ctx
->local_headers
;
4779 list_for_each_entry(header
, header_list
, list
) {
4780 if (header
->id
!= header_id
)
4782 return header
->fields
[field_id
].name
;
4788 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4790 switch (mapping_type
) {
4791 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4793 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4801 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4802 uint32_t field_id
, bool global
)
4804 enum devlink_dpipe_field_mapping_type mapping_type
;
4805 struct list_head
*header_list
;
4806 struct dpipe_header
*header
;
4809 header_list
= &ctx
->global_headers
;
4811 header_list
= &ctx
->local_headers
;
4812 list_for_each_entry(header
, header_list
, list
) {
4813 if (header
->id
!= header_id
)
4815 mapping_type
= header
->fields
[field_id
].mapping_type
;
4816 return dpipe_field_mapping_e2s(mapping_type
);
4821 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4822 struct dpipe_field
*fields
,
4823 unsigned int field_count
)
4825 struct dpipe_field
*field
;
4828 for (i
= 0; i
< field_count
; i
++) {
4830 pr_out_entry_start(ctx
->dl
);
4831 check_indent_newline(ctx
->dl
);
4832 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4833 if (ctx
->dl
->verbose
)
4834 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4835 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4836 if (field
->mapping_type
) {
4837 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4838 dpipe_field_mapping_e2s(field
->mapping_type
));
4840 pr_out_entry_end(ctx
->dl
);
4845 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4846 struct dpipe_header
*header
, bool global
)
4848 pr_out_handle_start_arr(ctx
->dl
, tb
);
4849 check_indent_newline(ctx
->dl
);
4850 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4851 if (ctx
->dl
->verbose
) {
4852 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4853 print_bool(PRINT_ANY
, "global", " global %s", global
);
4855 pr_out_array_start(ctx
->dl
, "field");
4856 pr_out_dpipe_fields(ctx
, header
->fields
,
4857 header
->fields_count
);
4858 pr_out_array_end(ctx
->dl
);
4859 pr_out_handle_end(ctx
->dl
);
4862 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4865 struct dpipe_header
*header
;
4867 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4868 pr_out_dpipe_header(ctx
, tb
, header
, false);
4870 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4871 pr_out_dpipe_header(ctx
, tb
, header
, true);
4874 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4876 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4880 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4881 if (err
!= MNL_CB_OK
)
4883 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4884 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4885 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4886 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4889 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4890 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4891 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4892 field
->name
= strdup(name
);
4895 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4899 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4900 struct dpipe_field
*fields
)
4902 struct nlattr
*nla_field
;
4906 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4907 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4915 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4917 struct nlattr
*nla_field
;
4918 unsigned int count
= 0;
4920 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4925 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4927 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4928 struct dpipe_header
*header
;
4929 unsigned int fields_count
;
4930 const char *header_name
;
4934 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4935 if (err
!= MNL_CB_OK
)
4938 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4939 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4940 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4943 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4944 header
= dpipe_header_alloc(fields_count
);
4948 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4949 header
->name
= strdup(header_name
);
4950 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4951 header
->fields_count
= fields_count
;
4952 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4954 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4958 dpipe_header_add(ctx
, header
, global
);
4962 dpipe_header_free(header
);
4966 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4968 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4969 struct nlattr
*nla_header
;
4972 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4973 err
= dpipe_header_get(ctx
, nla_header
);
4980 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4982 struct dpipe_ctx
*ctx
= data
;
4983 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4984 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4987 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4988 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4989 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4990 return MNL_CB_ERROR
;
4991 err
= dpipe_headers_get(ctx
, tb
);
4994 return MNL_CB_ERROR
;
4997 if (ctx
->print_headers
)
4998 pr_out_dpipe_headers(ctx
, tb
);
5002 static int cmd_dpipe_headers_show(struct dl
*dl
)
5004 struct nlmsghdr
*nlh
;
5005 struct dpipe_ctx ctx
= {};
5006 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5009 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5011 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
5015 err
= dpipe_ctx_init(&ctx
, dl
);
5019 ctx
.print_headers
= true;
5021 pr_out_section_start(dl
, "header");
5022 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5024 pr_err("error get headers %s\n", strerror(ctx
.err
));
5025 pr_out_section_end(dl
);
5027 dpipe_ctx_fini(&ctx
);
5031 static void cmd_dpipe_help(void)
5033 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5034 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5035 pr_err(" [ counters_enabled { true | false } ]\n");
5036 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5037 pr_err(" devlink dpipe header show DEV\n");
5040 static int cmd_dpipe_header(struct dl
*dl
)
5042 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5045 } else if (dl_argv_match(dl
, "show")) {
5047 return cmd_dpipe_headers_show(dl
);
5049 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5054 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5056 switch (action_type
) {
5057 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5058 return "field_modify";
5064 struct dpipe_op_info
{
5070 struct dpipe_action
{
5071 struct dpipe_op_info info
;
5075 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5076 struct dpipe_ctx
*ctx
)
5078 struct dpipe_op_info
*op_info
= &action
->info
;
5079 const char *mapping
;
5081 check_indent_newline(ctx
->dl
);
5082 print_string(PRINT_ANY
, "type", "type %s",
5083 dpipe_action_type_e2s(action
->type
));
5084 print_string(PRINT_ANY
, "header", " header %s",
5085 dpipe_header_id2s(ctx
, op_info
->header_id
,
5086 op_info
->header_global
));
5087 print_string(PRINT_ANY
, "field", " field %s",
5088 dpipe_field_id2s(ctx
, op_info
->header_id
,
5090 op_info
->header_global
));
5091 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5093 op_info
->header_global
);
5095 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5098 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5100 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5103 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5104 if (err
!= MNL_CB_OK
)
5107 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5108 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5109 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5110 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5114 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5115 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5116 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5117 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5122 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5123 struct nlattr
*nla_actions
)
5125 struct nlattr
*nla_action
;
5126 struct dpipe_action action
;
5128 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5129 pr_out_entry_start(ctx
->dl
);
5130 if (dpipe_action_parse(&action
, nla_action
))
5131 goto err_action_parse
;
5132 pr_out_dpipe_action(&action
, ctx
);
5133 pr_out_entry_end(ctx
->dl
);
5138 pr_out_entry_end(ctx
->dl
);
5143 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5145 switch (match_type
) {
5146 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5147 return "field_exact";
5153 struct dpipe_match
{
5154 struct dpipe_op_info info
;
5158 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5159 struct dpipe_ctx
*ctx
)
5161 struct dpipe_op_info
*op_info
= &match
->info
;
5162 const char *mapping
;
5164 check_indent_newline(ctx
->dl
);
5165 print_string(PRINT_ANY
, "type", "type %s",
5166 dpipe_match_type_e2s(match
->type
));
5167 print_string(PRINT_ANY
, "header", " header %s",
5168 dpipe_header_id2s(ctx
, op_info
->header_id
,
5169 op_info
->header_global
));
5170 print_string(PRINT_ANY
, "field", " field %s",
5171 dpipe_field_id2s(ctx
, op_info
->header_id
,
5173 op_info
->header_global
));
5174 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5176 op_info
->header_global
);
5178 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5181 static int dpipe_match_parse(struct dpipe_match
*match
,
5185 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5188 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5189 if (err
!= MNL_CB_OK
)
5192 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5193 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5194 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5195 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5199 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5200 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5201 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5202 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5207 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5208 struct nlattr
*nla_matches
)
5210 struct nlattr
*nla_match
;
5211 struct dpipe_match match
;
5213 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5214 pr_out_entry_start(ctx
->dl
);
5215 if (dpipe_match_parse(&match
, nla_match
))
5216 goto err_match_parse
;
5217 pr_out_dpipe_match(&match
, ctx
);
5218 pr_out_entry_end(ctx
->dl
);
5223 pr_out_entry_end(ctx
->dl
);
5227 static struct resource
*
5228 resource_find(struct resources
*resources
, struct resource
*resource
,
5229 uint64_t resource_id
)
5231 struct list_head
*list_head
;
5234 list_head
= &resources
->resource_list
;
5236 list_head
= &resource
->resource_list
;
5238 list_for_each_entry(resource
, list_head
, list
) {
5239 struct resource
*child_resource
;
5241 if (resource
->id
== resource_id
)
5244 child_resource
= resource_find(resources
, resource
,
5247 return child_resource
;
5253 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5254 uint64_t resource_id
)
5256 struct resource
*resource
, *parent_resource
;
5257 const char del
[] = "/";
5261 resource
= resource_find(resources
, NULL
, resource_id
);
5265 for (parent_resource
= resource
; parent_resource
;
5266 parent_resource
= parent_resource
->parent
)
5267 path_len
+= strlen(parent_resource
->name
) + 1;
5270 path
= calloc(1, path_len
);
5274 path
+= path_len
- 1;
5275 for (parent_resource
= resource
; parent_resource
;
5276 parent_resource
= parent_resource
->parent
) {
5277 path
-= strlen(parent_resource
->name
);
5278 memcpy(path
, parent_resource
->name
,
5279 strlen(parent_resource
->name
));
5280 path
-= strlen(del
);
5281 memcpy(path
, del
, strlen(del
));
5283 check_indent_newline(dl
);
5284 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5288 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5290 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5291 struct dpipe_table
*table
;
5292 uint32_t resource_units
;
5293 bool counters_enabled
;
5294 bool resource_valid
;
5298 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5299 if (err
!= MNL_CB_OK
)
5302 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5303 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5304 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5305 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5306 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5310 table
= dpipe_table_alloc();
5314 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5315 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5316 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5318 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5320 if (resource_valid
) {
5321 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5322 table
->resource_valid
= true;
5325 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5326 if (!ctx
->print_tables
)
5329 check_indent_newline(ctx
->dl
);
5330 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5331 print_uint(PRINT_ANY
, "size", " size %u", size
);
5332 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5334 if (resource_valid
) {
5335 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5336 resource_path_print(ctx
->dl
, ctx
->resources
,
5337 table
->resource_id
);
5338 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5342 pr_out_array_start(ctx
->dl
, "match");
5343 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5344 goto err_matches_show
;
5345 pr_out_array_end(ctx
->dl
);
5347 pr_out_array_start(ctx
->dl
, "action");
5348 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5349 goto err_actions_show
;
5350 pr_out_array_end(ctx
->dl
);
5356 pr_out_array_end(ctx
->dl
);
5360 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5362 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5363 struct nlattr
*nla_table
;
5365 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5366 if (ctx
->print_tables
)
5367 pr_out_handle_start_arr(ctx
->dl
, tb
);
5368 if (dpipe_table_show(ctx
, nla_table
))
5369 goto err_table_show
;
5370 if (ctx
->print_tables
)
5371 pr_out_handle_end(ctx
->dl
);
5376 if (ctx
->print_tables
)
5377 pr_out_handle_end(ctx
->dl
);
5381 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5383 struct dpipe_ctx
*ctx
= data
;
5384 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5385 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5387 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5388 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5389 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5390 return MNL_CB_ERROR
;
5392 if (dpipe_tables_show(ctx
, tb
))
5393 return MNL_CB_ERROR
;
5397 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5399 static int cmd_dpipe_table_show(struct dl
*dl
)
5401 struct nlmsghdr
*nlh
;
5402 struct dpipe_ctx dpipe_ctx
= {};
5403 struct resource_ctx resource_ctx
= {};
5404 uint16_t flags
= NLM_F_REQUEST
;
5407 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5411 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5413 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5417 dpipe_ctx
.print_tables
= true;
5419 dl_opts_put(nlh
, dl
);
5420 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5423 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5424 goto err_headers_get
;
5427 err
= resource_ctx_init(&resource_ctx
, dl
);
5429 goto err_resource_ctx_init
;
5431 resource_ctx
.print_resources
= false;
5432 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5433 dl_opts_put(nlh
, dl
);
5434 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5437 dpipe_ctx
.resources
= resource_ctx
.resources
;
5439 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5440 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5441 dl_opts_put(nlh
, dl
);
5443 pr_out_section_start(dl
, "table");
5444 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5445 pr_out_section_end(dl
);
5447 resource_ctx_fini(&resource_ctx
);
5448 dpipe_ctx_fini(&dpipe_ctx
);
5451 err_resource_ctx_init
:
5453 dpipe_ctx_fini(&dpipe_ctx
);
5457 static int cmd_dpipe_table_set(struct dl
*dl
)
5459 struct nlmsghdr
*nlh
;
5462 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5463 NLM_F_REQUEST
| NLM_F_ACK
);
5465 err
= dl_argv_parse_put(nlh
, dl
,
5466 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5467 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5471 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5474 enum dpipe_value_type
{
5475 DPIPE_VALUE_TYPE_VALUE
,
5476 DPIPE_VALUE_TYPE_MASK
,
5480 dpipe_value_type_e2s(enum dpipe_value_type type
)
5483 case DPIPE_VALUE_TYPE_VALUE
:
5485 case DPIPE_VALUE_TYPE_MASK
:
5486 return "value_mask";
5492 struct dpipe_field_printer
{
5493 unsigned int field_id
;
5494 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5497 struct dpipe_header_printer
{
5498 struct dpipe_field_printer
*printers
;
5499 unsigned int printers_count
;
5500 unsigned int header_id
;
5503 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5504 enum dpipe_value_type type
,
5507 struct in_addr ip_addr
;
5509 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5510 check_indent_newline(ctx
->dl
);
5511 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5515 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5516 enum dpipe_value_type type
,
5519 check_indent_newline(ctx
->dl
);
5520 print_string_name_value(dpipe_value_type_e2s(type
),
5521 ether_ntoa((struct ether_addr
*)value
));
5524 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5525 enum dpipe_value_type type
,
5528 char str
[INET6_ADDRSTRLEN
];
5530 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5531 check_indent_newline(ctx
->dl
);
5532 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5535 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5537 .printer
= dpipe_field_printer_ipv4_addr
,
5538 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5542 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5543 .printers
= dpipe_field_printers_ipv4
,
5544 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5545 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5548 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5550 .printer
= dpipe_field_printer_ethernet_addr
,
5551 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5555 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5556 .printers
= dpipe_field_printers_ethernet
,
5557 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5558 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5561 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5563 .printer
= dpipe_field_printer_ipv6_addr
,
5564 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5568 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5569 .printers
= dpipe_field_printers_ipv6
,
5570 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5571 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5574 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5575 &dpipe_header_printer_ipv4
,
5576 &dpipe_header_printer_ethernet
,
5577 &dpipe_header_printer_ipv6
,
5580 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5581 struct dpipe_op_info
*info
,
5582 enum dpipe_value_type type
,
5585 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5586 struct dpipe_header_printer
*header_printer
;
5587 struct dpipe_field_printer
*field_printer
;
5588 unsigned int field_printers_count
;
5592 for (i
= 0; i
< header_printers_count
; i
++) {
5593 header_printer
= dpipe_header_printers
[i
];
5594 if (header_printer
->header_id
!= info
->header_id
)
5596 field_printers_count
= header_printer
->printers_count
;
5597 for (j
= 0; j
< field_printers_count
; j
++) {
5598 field_printer
= &header_printer
->printers
[j
];
5599 if (field_printer
->field_id
!= info
->field_id
)
5601 field_printer
->printer(ctx
, type
, value
);
5609 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5611 unsigned int value_len
,
5612 struct dpipe_op_info
*info
,
5613 enum dpipe_value_type type
)
5615 if (info
->header_global
&&
5616 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5619 if (value_len
== sizeof(uint32_t)) {
5620 uint32_t *value_32
= value
;
5622 check_indent_newline(ctx
->dl
);
5623 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5627 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5628 struct nlattr
**nla_match_value
,
5629 struct dpipe_op_info
*info
)
5631 void *value
, *value_mask
;
5632 uint32_t value_mapping
;
5636 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5637 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5639 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5640 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5643 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5644 check_indent_newline(ctx
->dl
);
5645 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5649 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5650 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5651 DPIPE_VALUE_TYPE_MASK
);
5654 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5657 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5660 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5661 struct dpipe_match match
;
5664 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5665 if (err
!= MNL_CB_OK
)
5668 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5669 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5673 pr_out_entry_start(ctx
->dl
);
5674 if (dpipe_match_parse(&match
,
5675 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5676 goto err_match_parse
;
5677 pr_out_dpipe_match(&match
, ctx
);
5678 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5679 pr_out_entry_end(ctx
->dl
);
5684 pr_out_entry_end(ctx
->dl
);
5688 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5691 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5692 struct dpipe_action action
;
5695 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5696 if (err
!= MNL_CB_OK
)
5699 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5700 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5704 pr_out_entry_start(ctx
->dl
);
5705 if (dpipe_action_parse(&action
,
5706 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5707 goto err_action_parse
;
5708 pr_out_dpipe_action(&action
, ctx
);
5709 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5710 pr_out_entry_end(ctx
->dl
);
5715 pr_out_entry_end(ctx
->dl
);
5720 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5721 struct nlattr
*nla_action_values
)
5723 struct nlattr
*nla_action_value
;
5725 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5726 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5733 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5734 struct nlattr
*nla_match_values
)
5736 struct nlattr
*nla_match_value
;
5738 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5739 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5745 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5747 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5748 uint32_t entry_index
;
5752 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5753 if (err
!= MNL_CB_OK
)
5756 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5757 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5758 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5762 check_indent_newline(ctx
->dl
);
5763 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5764 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5766 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5767 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5768 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5771 pr_out_array_start(ctx
->dl
, "match_value");
5772 if (dpipe_tables_match_values_show(ctx
,
5773 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5774 goto err_match_values_show
;
5775 pr_out_array_end(ctx
->dl
);
5777 pr_out_array_start(ctx
->dl
, "action_value");
5778 if (dpipe_tables_action_values_show(ctx
,
5779 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5780 goto err_action_values_show
;
5781 pr_out_array_end(ctx
->dl
);
5784 err_action_values_show
:
5785 err_match_values_show
:
5786 pr_out_array_end(ctx
->dl
);
5790 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5792 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5793 struct nlattr
*nla_entry
;
5795 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5796 pr_out_handle_start_arr(ctx
->dl
, tb
);
5797 if (dpipe_entry_show(ctx
, nla_entry
))
5798 goto err_entry_show
;
5799 pr_out_handle_end(ctx
->dl
);
5804 pr_out_handle_end(ctx
->dl
);
5808 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5810 struct dpipe_ctx
*ctx
= data
;
5811 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5812 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5814 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5815 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5816 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5817 return MNL_CB_ERROR
;
5819 if (dpipe_table_entries_show(ctx
, tb
))
5820 return MNL_CB_ERROR
;
5824 static int cmd_dpipe_table_dump(struct dl
*dl
)
5826 struct nlmsghdr
*nlh
;
5827 struct dpipe_ctx ctx
= {};
5828 uint16_t flags
= NLM_F_REQUEST
;
5831 err
= dpipe_ctx_init(&ctx
, dl
);
5835 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5839 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5840 dl_opts_put(nlh
, dl
);
5841 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5843 pr_err("error get headers %s\n", strerror(ctx
.err
));
5847 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5848 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5849 dl_opts_put(nlh
, dl
);
5851 pr_out_section_start(dl
, "table_entry");
5852 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5853 pr_out_section_end(dl
);
5855 dpipe_ctx_fini(&ctx
);
5859 static int cmd_dpipe_table(struct dl
*dl
)
5861 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5864 } else if (dl_argv_match(dl
, "show")) {
5866 return cmd_dpipe_table_show(dl
);
5867 } else if (dl_argv_match(dl
, "set")) {
5869 return cmd_dpipe_table_set(dl
);
5870 } else if (dl_argv_match(dl
, "dump")) {
5872 return cmd_dpipe_table_dump(dl
);
5874 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5878 static int cmd_dpipe(struct dl
*dl
)
5880 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5883 } else if (dl_argv_match(dl
, "header")) {
5885 return cmd_dpipe_header(dl
);
5886 } else if (dl_argv_match(dl
, "table")) {
5888 return cmd_dpipe_table(dl
);
5890 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5895 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5896 struct nlattr
**nla_resource
)
5898 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5899 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5900 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5901 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5902 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5903 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5904 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5908 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5909 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5910 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5911 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5912 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5913 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5914 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5916 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5917 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5919 resource
->size_new
= resource
->size
;
5921 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5922 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5923 resource
->occ_valid
= true;
5926 if (resource
->size_new
!= resource
->size
)
5927 ctx
->pending_change
= true;
5933 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5934 struct resource
*parent_resource
, struct nlattr
*nl
)
5936 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5937 struct nlattr
*nla_child_resource
;
5938 struct nlattr
*nla_resources
;
5948 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5949 if (err
!= MNL_CB_OK
)
5952 err
= resource_parse(ctx
, resource
, nla_resource
);
5956 resource
->parent
= parent_resource
;
5957 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5960 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5961 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5963 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5964 struct resource
*child_resource
;
5965 struct list_head
*list
;
5967 child_resource
= resource_alloc();
5968 if (!child_resource
)
5972 list
= &ctx
->resources
->resource_list
;
5974 list
= &resource
->resource_list
;
5976 list_add_tail(&child_resource
->list
, list
);
5977 err
= resource_get(ctx
, child_resource
, resource
,
5978 nla_child_resource
);
5986 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5989 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5990 default: return "<unknown unit>";
5994 static void resource_show(struct resource
*resource
,
5995 struct resource_ctx
*ctx
)
5997 struct resource
*child_resource
;
5998 struct dpipe_table
*table
;
5999 struct dl
*dl
= ctx
->dl
;
6002 check_indent_newline(dl
);
6003 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
6005 resource_path_print(dl
, ctx
->resources
, resource
->id
);
6006 pr_out_u64(dl
, "size", resource
->size
);
6007 if (resource
->size
!= resource
->size_new
)
6008 pr_out_u64(dl
, "size_new", resource
->size_new
);
6009 if (resource
->occ_valid
)
6010 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
6011 print_string(PRINT_ANY
, "unit", " unit %s",
6012 resource_unit_str_get(resource
->unit
));
6014 if (resource
->size_min
!= resource
->size_max
) {
6015 print_uint(PRINT_ANY
, "size_min", " size_min %u",
6016 resource
->size_min
);
6017 pr_out_u64(dl
, "size_max", resource
->size_max
);
6018 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6019 resource
->size_gran
);
6022 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6023 if (table
->resource_id
== resource
->id
&&
6024 table
->resource_valid
)
6028 pr_out_array_start(dl
, "dpipe_tables");
6030 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6033 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6034 if (table
->resource_id
!= resource
->id
||
6035 !table
->resource_valid
)
6037 pr_out_entry_start(dl
);
6038 check_indent_newline(dl
);
6039 print_string(PRINT_ANY
, "table_name", "table_name %s",
6041 pr_out_entry_end(dl
);
6044 pr_out_array_end(dl
);
6046 if (list_empty(&resource
->resource_list
))
6049 if (ctx
->pending_change
) {
6050 check_indent_newline(dl
);
6051 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6052 resource
->size_valid
? "true" : "false");
6054 pr_out_array_start(dl
, "resources");
6055 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6056 pr_out_entry_start(dl
);
6057 resource_show(child_resource
, ctx
);
6058 pr_out_entry_end(dl
);
6060 pr_out_array_end(dl
);
6064 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6066 struct resources
*resources
= ctx
->resources
;
6067 struct resource
*resource
;
6069 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6070 pr_out_handle_start_arr(ctx
->dl
, tb
);
6071 resource_show(resource
, ctx
);
6072 pr_out_handle_end(ctx
->dl
);
6076 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6078 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6081 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6083 struct resource_ctx
*ctx
= data
;
6084 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6085 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6088 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6089 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6090 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6091 return MNL_CB_ERROR
;
6093 err
= resources_get(ctx
, tb
);
6096 return MNL_CB_ERROR
;
6099 if (ctx
->print_resources
)
6100 resources_show(ctx
, tb
);
6105 static int cmd_resource_show(struct dl
*dl
)
6107 struct nlmsghdr
*nlh
;
6108 struct dpipe_ctx dpipe_ctx
= {};
6109 struct resource_ctx resource_ctx
= {};
6112 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6116 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6118 dl_opts_put(nlh
, dl
);
6120 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6124 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6127 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6131 err
= resource_ctx_init(&resource_ctx
, dl
);
6135 resource_ctx
.print_resources
= true;
6136 resource_ctx
.tables
= dpipe_ctx
.tables
;
6137 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6138 NLM_F_REQUEST
| NLM_F_ACK
);
6139 dl_opts_put(nlh
, dl
);
6140 pr_out_section_start(dl
, "resources");
6141 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6143 pr_out_section_end(dl
);
6144 resource_ctx_fini(&resource_ctx
);
6146 dpipe_ctx_fini(&dpipe_ctx
);
6150 static void cmd_resource_help(void)
6152 pr_err("Usage: devlink resource show DEV\n"
6153 " devlink resource set DEV path PATH size SIZE\n");
6156 static struct resource
*
6157 resource_find_by_name(struct list_head
*list
, char *name
)
6159 struct resource
*resource
;
6161 list_for_each_entry(resource
, list
, list
) {
6162 if (!strcmp(resource
->name
, name
))
6169 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6170 uint32_t *p_resource_id
, bool *p_resource_valid
)
6172 struct resource
*resource
;
6173 uint32_t resource_id
= 0;
6174 char *resource_path_dup
;
6175 struct list_head
*list
;
6176 const char del
[] = "/";
6177 char *resource_name
;
6179 resource_path_dup
= strdup(resource_path
);
6180 list
= &ctx
->resources
->resource_list
;
6181 resource_name
= strtok(resource_path_dup
, del
);
6182 while (resource_name
!= NULL
) {
6183 resource
= resource_find_by_name(list
, resource_name
);
6185 goto err_resource_lookup
;
6187 list
= &resource
->resource_list
;
6188 resource_name
= strtok(NULL
, del
);
6189 resource_id
= resource
->id
;
6191 free(resource_path_dup
);
6192 *p_resource_valid
= true;
6193 *p_resource_id
= resource_id
;
6196 err_resource_lookup
:
6197 free(resource_path_dup
);
6201 static int cmd_resource_set(struct dl
*dl
)
6203 struct nlmsghdr
*nlh
;
6204 struct resource_ctx ctx
= {};
6207 err
= resource_ctx_init(&ctx
, dl
);
6211 ctx
.print_resources
= false;
6212 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6213 DL_OPT_RESOURCE_SIZE
, 0);
6217 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6219 dl_opts_put(nlh
, dl
);
6220 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6222 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6226 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6227 &dl
->opts
.resource_id
,
6228 &dl
->opts
.resource_id_valid
);
6230 pr_err("error parsing resource path %s\n", strerror(-err
));
6234 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6235 NLM_F_REQUEST
| NLM_F_ACK
);
6237 dl_opts_put(nlh
, dl
);
6238 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6240 resource_ctx_fini(&ctx
);
6244 static int cmd_resource(struct dl
*dl
)
6246 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6247 cmd_resource_help();
6249 } else if (dl_argv_match(dl
, "show")) {
6251 return cmd_resource_show(dl
);
6252 } else if (dl_argv_match(dl
, "set")) {
6254 return cmd_resource_set(dl
);
6256 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6260 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6262 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6263 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6264 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6267 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6268 if (dl
->json_output
)
6269 open_json_object(buf
);
6274 static void pr_out_region_handle_end(struct dl
*dl
)
6276 if (dl
->json_output
)
6277 close_json_object();
6282 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6284 __pr_out_indent_newline(dl
);
6285 if (dl
->json_output
)
6286 open_json_array(PRINT_JSON
, "snapshot");
6288 pr_out("snapshot %s", array
? "[" : "");
6291 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6293 if (dl
->json_output
)
6294 close_json_array(PRINT_JSON
, NULL
);
6299 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6301 uint32_t snapshot_id
;
6303 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6306 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6308 if (dl
->json_output
)
6309 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6311 pr_out("%s%u", index
? " " : "", snapshot_id
);
6314 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6316 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6317 struct nlattr
*nla_sanpshot
;
6320 pr_out_region_snapshots_start(dl
, true);
6321 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6322 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6323 if (err
!= MNL_CB_OK
)
6325 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6327 pr_out_region_snapshots_end(dl
, true);
6330 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6332 pr_out_region_snapshots_start(dl
, false);
6333 pr_out_region_snapshots_id(dl
, tb
, 0);
6334 pr_out_region_snapshots_end(dl
, false);
6337 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6339 pr_out_region_handle_start(dl
, tb
);
6341 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6342 pr_out_u64(dl
, "size",
6343 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6345 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6346 pr_out_snapshots(dl
, tb
);
6348 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6349 pr_out_snapshot(dl
, tb
);
6351 pr_out_region_handle_end(dl
);
6354 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6356 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6357 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6358 struct dl
*dl
= data
;
6360 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6361 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6362 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6363 return MNL_CB_ERROR
;
6365 pr_out_region(dl
, tb
);
6370 static int cmd_region_show(struct dl
*dl
)
6372 struct nlmsghdr
*nlh
;
6373 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6376 if (dl_argc(dl
) == 0)
6377 flags
|= NLM_F_DUMP
;
6379 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6381 if (dl_argc(dl
) > 0) {
6382 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6387 pr_out_section_start(dl
, "regions");
6388 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6389 pr_out_section_end(dl
);
6393 static int cmd_region_snapshot_del(struct dl
*dl
)
6395 struct nlmsghdr
*nlh
;
6398 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6399 NLM_F_REQUEST
| NLM_F_ACK
);
6401 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6402 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6406 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6409 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6411 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6412 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6413 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6414 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6415 struct dl
*dl
= data
;
6418 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6419 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6420 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6421 return MNL_CB_ERROR
;
6423 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6424 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6425 if (err
!= MNL_CB_OK
)
6426 return MNL_CB_ERROR
;
6428 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6429 if (!nla_chunk_data
)
6432 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6433 if (!nla_chunk_addr
)
6436 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6437 mnl_attr_get_payload_len(nla_chunk_data
),
6438 mnl_attr_get_u64(nla_chunk_addr
));
6443 static int cmd_region_dump(struct dl
*dl
)
6445 struct nlmsghdr
*nlh
;
6448 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6449 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6451 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6452 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6456 pr_out_section_start(dl
, "dump");
6457 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6458 pr_out_section_end(dl
);
6459 if (!dl
->json_output
)
6464 static int cmd_region_read(struct dl
*dl
)
6466 struct nlmsghdr
*nlh
;
6469 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6470 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6472 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6473 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6474 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6478 pr_out_section_start(dl
, "read");
6479 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6480 pr_out_section_end(dl
);
6481 if (!dl
->json_output
)
6486 static int cmd_region_snapshot_new_cb(const struct nlmsghdr
*nlh
, void *data
)
6488 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6489 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6490 struct dl
*dl
= data
;
6492 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6493 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6494 !tb
[DEVLINK_ATTR_REGION_NAME
] ||
6495 !tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6496 return MNL_CB_ERROR
;
6498 pr_out_region(dl
, tb
);
6503 static int cmd_region_snapshot_new(struct dl
*dl
)
6505 struct nlmsghdr
*nlh
;
6508 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_NEW
,
6509 NLM_F_REQUEST
| NLM_F_ACK
);
6511 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
,
6512 DL_OPT_REGION_SNAPSHOT_ID
);
6516 pr_out_section_start(dl
, "regions");
6517 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_snapshot_new_cb
, dl
);
6518 pr_out_section_end(dl
);
6522 static void cmd_region_help(void)
6524 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6525 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6526 pr_err(" devlink region new DEV/REGION snapshot SNAPSHOT_ID\n");
6527 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6528 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6531 static int cmd_region(struct dl
*dl
)
6533 if (dl_no_arg(dl
)) {
6534 return cmd_region_show(dl
);
6535 } else if (dl_argv_match(dl
, "help")) {
6538 } else if (dl_argv_match(dl
, "show")) {
6540 return cmd_region_show(dl
);
6541 } else if (dl_argv_match(dl
, "del")) {
6543 return cmd_region_snapshot_del(dl
);
6544 } else if (dl_argv_match(dl
, "dump")) {
6546 return cmd_region_dump(dl
);
6547 } else if (dl_argv_match(dl
, "read")) {
6549 return cmd_region_read(dl
);
6550 } else if (dl_argv_match(dl
, "new")) {
6552 return cmd_region_snapshot_new(dl
);
6554 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6558 static int cmd_health_set_params(struct dl
*dl
)
6560 struct nlmsghdr
*nlh
;
6563 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6564 NLM_F_REQUEST
| NLM_F_ACK
);
6565 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6566 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6567 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
|
6568 DL_OPT_HEALTH_REPORTER_AUTO_DUMP
);
6572 dl_opts_put(nlh
, dl
);
6573 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6576 static int cmd_health_dump_clear(struct dl
*dl
)
6578 struct nlmsghdr
*nlh
;
6581 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6582 NLM_F_REQUEST
| NLM_F_ACK
);
6584 err
= dl_argv_parse_put(nlh
, dl
,
6585 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6589 dl_opts_put(nlh
, dl
);
6590 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6593 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6598 check_indent_newline(dl
);
6601 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6604 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6607 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6610 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6613 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6615 case MNL_TYPE_NUL_STRING
:
6616 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6618 case MNL_TYPE_BINARY
:
6619 len
= mnl_attr_get_payload_len(nl_data
);
6620 data
= mnl_attr_get_payload(nl_data
);
6621 pr_out_binary_value(dl
, data
, len
);
6629 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6634 pr_out_name(dl
, *name
);
6641 struct list_head list
;
6644 struct fmsg_cb_data
{
6648 struct list_head entry_list
;
6651 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6652 uint8_t *attr_value
, bool insert
)
6654 struct nest_entry
*entry
;
6657 entry
= malloc(sizeof(struct nest_entry
));
6661 entry
->attr_type
= *attr_value
;
6662 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6664 if (list_empty(&fmsg_data
->entry_list
))
6665 return MNL_CB_ERROR
;
6666 entry
= list_first_entry(&fmsg_data
->entry_list
,
6667 struct nest_entry
, list
);
6668 *attr_value
= entry
->attr_type
;
6669 list_del(&entry
->list
);
6675 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6678 pr_out_fmsg_name(dl
, name
);
6680 __pr_out_indent_inc();
6683 static void pr_out_fmsg_group_end(struct dl
*dl
)
6686 __pr_out_indent_dec();
6689 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6691 if (dl
->json_output
) {
6692 pr_out_fmsg_name(dl
, name
);
6693 open_json_object(NULL
);
6695 pr_out_fmsg_group_start(dl
, name
);
6699 static void pr_out_fmsg_end_object(struct dl
*dl
)
6701 if (dl
->json_output
)
6702 close_json_object();
6704 pr_out_fmsg_group_end(dl
);
6707 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6709 if (dl
->json_output
) {
6710 pr_out_fmsg_name(dl
, name
);
6711 open_json_array(PRINT_JSON
, NULL
);
6713 pr_out_fmsg_group_start(dl
, name
);
6717 static void pr_out_fmsg_end_array(struct dl
*dl
)
6719 if (dl
->json_output
)
6720 close_json_array(PRINT_JSON
, NULL
);
6722 pr_out_fmsg_group_end(dl
);
6725 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6728 struct dl
*dl
= fmsg_data
->dl
;
6729 uint8_t value
= nest_value
;
6732 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6733 if (err
!= MNL_CB_OK
)
6737 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6739 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6741 pr_out_fmsg_end_object(dl
);
6743 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6745 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6747 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6749 pr_out_fmsg_end_array(dl
);
6757 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6759 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6760 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6761 struct fmsg_cb_data
*fmsg_data
= data
;
6762 struct dl
*dl
= fmsg_data
->dl
;
6763 struct nlattr
*nla_object
;
6767 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6768 if (!tb
[DEVLINK_ATTR_FMSG
])
6769 return MNL_CB_ERROR
;
6771 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6772 attr_type
= mnl_attr_get_type(nla_object
);
6773 switch (attr_type
) {
6774 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6775 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6776 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6777 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6778 if (err
!= MNL_CB_OK
)
6781 case DEVLINK_ATTR_FMSG_NEST_END
:
6782 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6783 if (err
!= MNL_CB_OK
)
6786 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6787 free(fmsg_data
->name
);
6788 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6789 if (!fmsg_data
->name
)
6792 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6793 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6795 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6796 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6797 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6799 if (err
!= MNL_CB_OK
)
6809 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6811 /* FMSG is dynamic: opening of an object or array causes a
6812 * newline. JSON starts with an { or [, but plain text should
6813 * not start with a new line. Ensure this by setting
6814 * g_new_line_count to 1: avoiding newline before the first
6817 g_new_line_count
= 1;
6820 INIT_LIST_HEAD(&data
->entry_list
);
6823 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6825 struct fmsg_cb_data data
;
6826 struct nlmsghdr
*nlh
;
6829 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6831 err
= dl_argv_parse_put(nlh
, dl
,
6832 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6836 cmd_fmsg_init(dl
, &data
);
6837 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6842 static int cmd_health_dump_show(struct dl
*dl
)
6844 return cmd_health_object_common(dl
,
6845 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6849 static int cmd_health_diagnose(struct dl
*dl
)
6851 return cmd_health_object_common(dl
,
6852 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6856 static int cmd_health_recover(struct dl
*dl
)
6858 struct nlmsghdr
*nlh
;
6861 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6862 NLM_F_REQUEST
| NLM_F_ACK
);
6864 err
= dl_argv_parse_put(nlh
, dl
,
6865 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6869 dl_opts_put(nlh
, dl
);
6870 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6873 enum devlink_health_reporter_state
{
6874 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6875 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6878 static const char *health_state_name(uint8_t state
)
6881 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6882 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6883 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6884 return HEALTH_REPORTER_STATE_ERROR_STR
;
6886 return "<unknown state>";
6890 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6892 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6893 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6894 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6895 struct sysinfo s_info
;
6901 info
= localtime(&now
);
6902 err
= sysinfo(&s_info
);
6905 /* Subtract uptime in sec from now yields the time of system
6906 * uptime. To this, add time_ms which is the amount of
6907 * milliseconds elapsed between uptime and the dump taken.
6909 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6910 info
= localtime(&sec
);
6912 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6913 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6914 check_indent_newline(dl
);
6915 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6916 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6919 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6921 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6922 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6927 ts
= mnl_attr_get_u64(attr
);
6928 tv_sec
= ts
/ 1000000000;
6929 tm
= localtime(&tv_sec
);
6931 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6932 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6934 check_indent_newline(dl
);
6935 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6936 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6939 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6941 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6942 enum devlink_health_reporter_state state
;
6945 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6947 if (err
!= MNL_CB_OK
)
6950 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6951 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6952 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6953 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6956 pr_out_handle_start_arr(dl
, tb_health
);
6958 check_indent_newline(dl
);
6959 print_string(PRINT_ANY
, "reporter", "reporter %s",
6960 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6961 if (!dl
->json_output
) {
6963 __pr_out_indent_inc();
6965 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6966 check_indent_newline(dl
);
6967 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6968 pr_out_u64(dl
, "error",
6969 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6970 pr_out_u64(dl
, "recover",
6971 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6972 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6973 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6974 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6975 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6976 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6977 pr_out_u64(dl
, "grace_period",
6978 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6979 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6980 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6981 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6982 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
6983 print_bool(PRINT_ANY
, "auto_dump", " auto_dump %s",
6984 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]));
6986 __pr_out_indent_dec();
6987 pr_out_handle_end(dl
);
6990 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6992 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6993 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6994 struct dl
*dl
= data
;
6996 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6997 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6998 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6999 return MNL_CB_ERROR
;
7001 pr_out_health(dl
, tb
);
7006 static int cmd_health_show(struct dl
*dl
)
7008 struct nlmsghdr
*nlh
;
7009 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7012 if (dl_argc(dl
) == 0)
7013 flags
|= NLM_F_DUMP
;
7014 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
7017 if (dl_argc(dl
) > 0) {
7018 err
= dl_argv_parse_put(nlh
, dl
,
7020 DL_OPT_HEALTH_REPORTER_NAME
, 0);
7024 pr_out_section_start(dl
, "health");
7026 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
7027 pr_out_section_end(dl
);
7031 static void cmd_health_help(void)
7033 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
7034 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
7035 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
7036 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
7037 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
7038 pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
7039 pr_err(" [ grace_period MSEC ]\n");
7040 pr_err(" [ auto_recover { true | false } ]\n");
7041 pr_err(" [ auto_dump { true | false } ]\n");
7044 static int cmd_health(struct dl
*dl
)
7046 if (dl_argv_match(dl
, "help")) {
7049 } else if (dl_argv_match(dl
, "show") ||
7050 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7052 return cmd_health_show(dl
);
7053 } else if (dl_argv_match(dl
, "recover")) {
7055 return cmd_health_recover(dl
);
7056 } else if (dl_argv_match(dl
, "diagnose")) {
7058 return cmd_health_diagnose(dl
);
7059 } else if (dl_argv_match(dl
, "dump")) {
7061 if (dl_argv_match(dl
, "show")) {
7063 return cmd_health_dump_show(dl
);
7064 } else if (dl_argv_match(dl
, "clear")) {
7066 return cmd_health_dump_clear(dl
);
7068 } else if (dl_argv_match(dl
, "set")) {
7070 return cmd_health_set_params(dl
);
7072 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7076 static const char *trap_type_name(uint8_t type
)
7079 case DEVLINK_TRAP_TYPE_DROP
:
7081 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7083 case DEVLINK_TRAP_TYPE_CONTROL
:
7086 return "<unknown type>";
7090 static const char *trap_action_name(uint8_t action
)
7093 case DEVLINK_TRAP_ACTION_DROP
:
7095 case DEVLINK_TRAP_ACTION_TRAP
:
7098 return "<unknown action>";
7102 static const char *trap_metadata_name(const struct nlattr
*attr
)
7104 switch (attr
->nla_type
) {
7105 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7106 return "input_port";
7107 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7108 return "flow_action_cookie";
7110 return "<unknown metadata type>";
7113 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7115 struct nlattr
*attr_metadata
;
7117 pr_out_array_start(dl
, "metadata");
7118 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7119 check_indent_newline(dl
);
7120 print_string(PRINT_ANY
, NULL
, "%s",
7121 trap_metadata_name(attr_metadata
));
7123 pr_out_array_end(dl
);
7126 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7128 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7129 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7132 pr_out_handle_start_arr(dl
, tb
);
7134 __pr_out_handle_start(dl
, tb
, true, false);
7136 check_indent_newline(dl
);
7137 print_string(PRINT_ANY
, "name", "name %s",
7138 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7139 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7140 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7141 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7142 print_string(PRINT_ANY
, "group", " group %s",
7143 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7145 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7146 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7147 pr_out_handle_end(dl
);
7150 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7152 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7153 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7154 struct dl
*dl
= data
;
7156 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7157 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7158 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7159 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7160 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7161 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7162 return MNL_CB_ERROR
;
7164 pr_out_trap(dl
, tb
, true);
7169 static void cmd_trap_help(void)
7171 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
7172 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7173 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
7174 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7175 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7176 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7177 pr_err(" devlink trap policer show DEV policer POLICER\n");
7180 static int cmd_trap_show(struct dl
*dl
)
7182 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7183 struct nlmsghdr
*nlh
;
7186 if (dl_argc(dl
) == 0)
7187 flags
|= NLM_F_DUMP
;
7189 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7191 if (dl_argc(dl
) > 0) {
7192 err
= dl_argv_parse_put(nlh
, dl
,
7193 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7198 pr_out_section_start(dl
, "trap");
7199 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7200 pr_out_section_end(dl
);
7205 static int cmd_trap_set(struct dl
*dl
)
7207 struct nlmsghdr
*nlh
;
7210 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7211 NLM_F_REQUEST
| NLM_F_ACK
);
7213 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7214 DL_OPT_TRAP_ACTION
);
7218 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7221 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7224 pr_out_handle_start_arr(dl
, tb
);
7226 __pr_out_handle_start(dl
, tb
, true, false);
7228 check_indent_newline(dl
);
7229 print_string(PRINT_ANY
, "name", "name %s",
7230 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7231 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7232 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7233 print_uint(PRINT_ANY
, "policer", " policer %u",
7234 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7235 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7236 pr_out_handle_end(dl
);
7239 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7241 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7242 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7243 struct dl
*dl
= data
;
7245 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7246 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7247 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7248 return MNL_CB_ERROR
;
7250 pr_out_trap_group(dl
, tb
, true);
7255 static int cmd_trap_group_show(struct dl
*dl
)
7257 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7258 struct nlmsghdr
*nlh
;
7261 if (dl_argc(dl
) == 0)
7262 flags
|= NLM_F_DUMP
;
7264 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7266 if (dl_argc(dl
) > 0) {
7267 err
= dl_argv_parse_put(nlh
, dl
,
7268 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7274 pr_out_section_start(dl
, "trap_group");
7275 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7276 pr_out_section_end(dl
);
7281 static int cmd_trap_group_set(struct dl
*dl
)
7283 struct nlmsghdr
*nlh
;
7286 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7287 NLM_F_REQUEST
| NLM_F_ACK
);
7289 err
= dl_argv_parse_put(nlh
, dl
,
7290 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7291 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7295 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7298 static int cmd_trap_group(struct dl
*dl
)
7300 if (dl_argv_match(dl
, "help")) {
7303 } else if (dl_argv_match(dl
, "show") ||
7304 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7306 return cmd_trap_group_show(dl
);
7307 } else if (dl_argv_match(dl
, "set")) {
7309 return cmd_trap_group_set(dl
);
7311 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7315 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7318 pr_out_handle_start_arr(dl
, tb
);
7320 __pr_out_handle_start(dl
, tb
, true, false);
7322 check_indent_newline(dl
);
7323 print_uint(PRINT_ANY
, "policer", "policer %u",
7324 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7325 print_u64(PRINT_ANY
, "rate", " rate %llu",
7326 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7327 print_u64(PRINT_ANY
, "burst", " burst %llu",
7328 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7329 if (tb
[DEVLINK_ATTR_STATS
])
7330 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7331 pr_out_handle_end(dl
);
7334 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7336 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7337 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7338 struct dl
*dl
= data
;
7340 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7341 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7342 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7343 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7344 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7345 return MNL_CB_ERROR
;
7347 pr_out_trap_policer(dl
, tb
, true);
7352 static int cmd_trap_policer_show(struct dl
*dl
)
7354 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7355 struct nlmsghdr
*nlh
;
7358 if (dl_argc(dl
) == 0)
7359 flags
|= NLM_F_DUMP
;
7361 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7363 if (dl_argc(dl
) > 0) {
7364 err
= dl_argv_parse_put(nlh
, dl
,
7365 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7371 pr_out_section_start(dl
, "trap_policer");
7372 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7373 pr_out_section_end(dl
);
7378 static int cmd_trap_policer_set(struct dl
*dl
)
7380 struct nlmsghdr
*nlh
;
7383 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7384 NLM_F_REQUEST
| NLM_F_ACK
);
7386 err
= dl_argv_parse_put(nlh
, dl
,
7387 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7388 DL_OPT_TRAP_POLICER_RATE
|
7389 DL_OPT_TRAP_POLICER_BURST
);
7393 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7396 static int cmd_trap_policer(struct dl
*dl
)
7398 if (dl_argv_match(dl
, "help")) {
7401 } else if (dl_argv_match(dl
, "show") ||
7402 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7404 return cmd_trap_policer_show(dl
);
7405 } else if (dl_argv_match(dl
, "set")) {
7407 return cmd_trap_policer_set(dl
);
7409 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7413 static int cmd_trap(struct dl
*dl
)
7415 if (dl_argv_match(dl
, "help")) {
7418 } else if (dl_argv_match(dl
, "show") ||
7419 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7421 return cmd_trap_show(dl
);
7422 } else if (dl_argv_match(dl
, "set")) {
7424 return cmd_trap_set(dl
);
7425 } else if (dl_argv_match(dl
, "group")) {
7427 return cmd_trap_group(dl
);
7428 } else if (dl_argv_match(dl
, "policer")) {
7430 return cmd_trap_policer(dl
);
7432 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7436 static void help(void)
7438 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7439 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7440 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7441 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7444 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7449 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7452 } else if (dl_argv_match(dl
, "dev")) {
7455 } else if (dl_argv_match(dl
, "port")) {
7457 return cmd_port(dl
);
7458 } else if (dl_argv_match(dl
, "sb")) {
7461 } else if (dl_argv_match(dl
, "monitor")) {
7464 } else if (dl_argv_match(dl
, "dpipe")) {
7466 return cmd_dpipe(dl
);
7467 } else if (dl_argv_match(dl
, "resource")) {
7469 return cmd_resource(dl
);
7470 } else if (dl_argv_match(dl
, "region")) {
7472 return cmd_region(dl
);
7473 } else if (dl_argv_match(dl
, "health")) {
7475 return cmd_health(dl
);
7476 } else if (dl_argv_match(dl
, "trap")) {
7478 return cmd_trap(dl
);
7480 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7484 static int dl_init(struct dl
*dl
)
7488 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7490 pr_err("Failed to connect to devlink Netlink\n");
7494 err
= ifname_map_init(dl
);
7496 pr_err("Failed to create index map\n");
7497 goto err_ifname_map_create
;
7499 new_json_obj_plain(dl
->json_output
);
7502 err_ifname_map_create
:
7503 mnlg_socket_close(dl
->nlg
);
7507 static void dl_fini(struct dl
*dl
)
7509 delete_json_obj_plain();
7510 ifname_map_fini(dl
);
7511 mnlg_socket_close(dl
->nlg
);
7514 static struct dl
*dl_alloc(void)
7518 dl
= calloc(1, sizeof(*dl
));
7524 static void dl_free(struct dl
*dl
)
7529 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7533 int ret
= EXIT_SUCCESS
;
7535 if (name
&& strcmp(name
, "-") != 0) {
7536 if (freopen(name
, "r", stdin
) == NULL
) {
7538 "Cannot open file \"%s\" for reading: %s\n",
7539 name
, strerror(errno
));
7540 return EXIT_FAILURE
;
7545 while (getcmdline(&line
, &len
, stdin
) != -1) {
7549 largc
= makeargs(line
, largv
, 100);
7551 continue; /* blank line */
7553 if (dl_cmd(dl
, largc
, largv
)) {
7554 fprintf(stderr
, "Command failed %s:%d\n",
7568 int main(int argc
, char **argv
)
7570 static const struct option long_options
[] = {
7571 { "Version", no_argument
, NULL
, 'V' },
7572 { "force", no_argument
, NULL
, 'f' },
7573 { "batch", required_argument
, NULL
, 'b' },
7574 { "no-nice-names", no_argument
, NULL
, 'n' },
7575 { "json", no_argument
, NULL
, 'j' },
7576 { "pretty", no_argument
, NULL
, 'p' },
7577 { "verbose", no_argument
, NULL
, 'v' },
7578 { "statistics", no_argument
, NULL
, 's' },
7579 { "Netns", required_argument
, NULL
, 'N' },
7580 { NULL
, 0, NULL
, 0 }
7582 const char *batch_file
= NULL
;
7591 pr_err("Failed to allocate memory for devlink\n");
7592 return EXIT_FAILURE
;
7595 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7596 long_options
, NULL
)) >= 0) {
7600 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7607 batch_file
= optarg
;
7610 dl
->no_nice_names
= true;
7613 dl
->json_output
= true;
7625 if (netns_switch(optarg
)) {
7631 pr_err("Unknown option.\n");
7648 err
= dl_batch(dl
, batch_file
, force
);
7650 err
= dl_cmd(dl
, argc
, argv
);