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>
22 #include <sys/sysinfo.h>
23 #define _LINUX_SYSINFO_H /* avoid collision with musl header */
24 #include <linux/genetlink.h>
25 #include <linux/devlink.h>
26 #include <libmnl/libmnl.h>
27 #include <netinet/ether.h>
28 #include <sys/types.h>
33 #include "json_writer.h"
35 #include "namespace.h"
37 #define ESWITCH_MODE_LEGACY "legacy"
38 #define ESWITCH_MODE_SWITCHDEV "switchdev"
39 #define ESWITCH_INLINE_MODE_NONE "none"
40 #define ESWITCH_INLINE_MODE_LINK "link"
41 #define ESWITCH_INLINE_MODE_NETWORK "network"
42 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
44 #define PARAM_CMODE_RUNTIME_STR "runtime"
45 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
46 #define PARAM_CMODE_PERMANENT_STR "permanent"
47 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
49 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
50 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
51 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
53 static int g_new_line_count
;
54 static int g_indent_level
;
55 static bool g_indent_newline
;
57 #define INDENT_STR_STEP 2
58 #define INDENT_STR_MAXLEN 32
59 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
61 static void __attribute__((format(printf
, 1, 2)))
62 pr_err(const char *fmt
, ...)
67 vfprintf(stderr
, fmt
, ap
);
71 static void __attribute__((format(printf
, 1, 2)))
72 pr_out(const char *fmt
, ...)
76 if (g_indent_newline
) {
77 printf("%s", g_indent_str
);
78 g_indent_newline
= false;
86 static void __attribute__((format(printf
, 2, 3)))
87 pr_out_sp(unsigned int num
, const char *fmt
, ...)
93 ret
= vprintf(fmt
, ap
);
97 printf("%*s", num
- ret
, "");
98 g_new_line_count
= 0; \
101 static void __attribute__((format(printf
, 1, 2)))
102 pr_out_tty(const char *fmt
, ...)
106 if (!isatty(STDOUT_FILENO
))
113 static void __pr_out_indent_inc(void)
115 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
117 g_indent_level
+= INDENT_STR_STEP
;
118 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
119 g_indent_str
[g_indent_level
] = '\0';
122 static void __pr_out_indent_dec(void)
124 if (g_indent_level
- INDENT_STR_STEP
< 0)
126 g_indent_level
-= INDENT_STR_STEP
;
127 g_indent_str
[g_indent_level
] = '\0';
130 static void __pr_out_newline(void)
132 if (g_new_line_count
< 1) {
134 g_indent_newline
= true;
139 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
140 mnl_cb_t data_cb
, void *data
)
144 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
146 pr_err("devlink answers: %s\n", strerror(errno
));
152 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
153 const struct nlmsghdr
*nlh
)
157 err
= mnlg_socket_send(nlg
, nlh
);
159 pr_err("Failed to call mnlg_socket_send\n");
165 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
166 const struct nlmsghdr
*nlh
,
167 mnl_cb_t data_cb
, void *data
)
171 err
= _mnlg_socket_send(nlg
, nlh
);
174 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
177 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
178 const char *group_name
)
182 err
= mnlg_socket_group_add(nlg
, group_name
);
184 pr_err("Failed to call mnlg_socket_group_add\n");
191 struct list_head list
;
198 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
199 const char *dev_name
,
203 struct ifname_map
*ifname_map
;
205 ifname_map
= calloc(1, sizeof(*ifname_map
));
208 ifname_map
->bus_name
= strdup(bus_name
);
209 ifname_map
->dev_name
= strdup(dev_name
);
210 ifname_map
->port_index
= port_index
;
211 ifname_map
->ifname
= strdup(ifname
);
212 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
213 !ifname_map
->ifname
) {
214 free(ifname_map
->ifname
);
215 free(ifname_map
->dev_name
);
216 free(ifname_map
->bus_name
);
223 static void ifname_map_free(struct ifname_map
*ifname_map
)
225 free(ifname_map
->ifname
);
226 free(ifname_map
->dev_name
);
227 free(ifname_map
->bus_name
);
231 #define DL_OPT_HANDLE BIT(0)
232 #define DL_OPT_HANDLEP BIT(1)
233 #define DL_OPT_PORT_TYPE BIT(2)
234 #define DL_OPT_PORT_COUNT BIT(3)
235 #define DL_OPT_SB BIT(4)
236 #define DL_OPT_SB_POOL BIT(5)
237 #define DL_OPT_SB_SIZE BIT(6)
238 #define DL_OPT_SB_TYPE BIT(7)
239 #define DL_OPT_SB_THTYPE BIT(8)
240 #define DL_OPT_SB_TH BIT(9)
241 #define DL_OPT_SB_TC BIT(10)
242 #define DL_OPT_ESWITCH_MODE BIT(11)
243 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
244 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
245 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
246 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
247 #define DL_OPT_RESOURCE_PATH BIT(16)
248 #define DL_OPT_RESOURCE_SIZE BIT(17)
249 #define DL_OPT_PARAM_NAME BIT(18)
250 #define DL_OPT_PARAM_VALUE BIT(19)
251 #define DL_OPT_PARAM_CMODE BIT(20)
252 #define DL_OPT_HANDLE_REGION BIT(21)
253 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
254 #define DL_OPT_REGION_ADDRESS BIT(23)
255 #define DL_OPT_REGION_LENGTH BIT(24)
256 #define DL_OPT_FLASH_FILE_NAME BIT(25)
257 #define DL_OPT_FLASH_COMPONENT BIT(26)
258 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
259 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
260 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
261 #define DL_OPT_TRAP_NAME BIT(30)
262 #define DL_OPT_TRAP_ACTION BIT(31)
263 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
264 #define DL_OPT_NETNS BIT(33)
267 uint64_t present
; /* flags of present items */
271 enum devlink_port_type port_type
;
274 uint16_t sb_pool_index
;
275 uint32_t sb_pool_size
;
276 enum devlink_sb_pool_type sb_pool_type
;
277 enum devlink_sb_threshold_type sb_pool_thtype
;
278 uint32_t sb_threshold
;
279 uint16_t sb_tc_index
;
280 enum devlink_eswitch_mode eswitch_mode
;
281 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
282 const char *dpipe_table_name
;
283 bool dpipe_counters_enable
;
284 bool eswitch_encap_mode
;
285 const char *resource_path
;
286 uint64_t resource_size
;
287 uint32_t resource_id
;
288 bool resource_id_valid
;
289 const char *param_name
;
290 const char *param_value
;
291 enum devlink_param_cmode cmode
;
293 uint32_t region_snapshot_id
;
294 uint64_t region_address
;
295 uint64_t region_length
;
296 const char *flash_file_name
;
297 const char *flash_component
;
298 const char *reporter_name
;
299 uint64_t reporter_graceful_period
;
300 bool reporter_auto_recover
;
301 const char *trap_name
;
302 const char *trap_group_name
;
303 enum devlink_trap_action trap_action
;
309 struct mnlg_socket
*nlg
;
310 struct list_head ifname_map_list
;
328 static int dl_argc(struct dl
*dl
)
333 static char *dl_argv(struct dl
*dl
)
335 if (dl_argc(dl
) == 0)
340 static void dl_arg_inc(struct dl
*dl
)
342 if (dl_argc(dl
) == 0)
348 static void dl_arg_dec(struct dl
*dl
)
354 static char *dl_argv_next(struct dl
*dl
)
358 if (dl_argc(dl
) == 0)
366 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
368 if (index
>= dl_argc(dl
))
370 return dl
->argv
[index
];
373 static int strcmpx(const char *str1
, const char *str2
)
375 if (strlen(str1
) > strlen(str2
))
377 return strncmp(str1
, str2
, strlen(str1
));
380 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
382 if (dl_argc(dl
) == 0)
384 return strcmpx(dl_argv(dl
), pattern
) == 0;
387 static bool dl_no_arg(struct dl
*dl
)
389 return dl_argc(dl
) == 0;
392 static void __pr_out_indent_newline(struct dl
*dl
)
394 if (!g_indent_newline
&& !dl
->json_output
)
398 static void check_indent_newline(struct dl
*dl
)
400 __pr_out_indent_newline(dl
);
402 if (g_indent_newline
&& !is_json_context()) {
403 printf("%s", g_indent_str
);
404 g_indent_newline
= false;
406 g_new_line_count
= 0;
409 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
410 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
411 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
412 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
413 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
414 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
415 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
416 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
417 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
418 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
419 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
420 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
421 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
422 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
423 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
424 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
425 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
426 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
427 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
428 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
429 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
430 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
431 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
432 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
433 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
434 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
435 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
436 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
437 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
438 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
439 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
440 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
441 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
442 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
443 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
444 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
445 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
446 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
447 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
448 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
449 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
450 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
451 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
452 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
453 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
454 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
455 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
456 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
457 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
458 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
459 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
460 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
461 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
462 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
463 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
464 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
465 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
466 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
467 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
468 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
469 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
470 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
471 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
472 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
473 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
474 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
475 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
476 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
477 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
478 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
479 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
480 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
481 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
482 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
483 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
484 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
485 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
486 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
487 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
488 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
489 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
490 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
491 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
492 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
493 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
494 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
495 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
496 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
497 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
498 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
499 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
500 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
501 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
502 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
503 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
504 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
505 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
506 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
507 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
508 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
511 static const enum mnl_attr_data_type
512 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
513 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
514 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
517 static int attr_cb(const struct nlattr
*attr
, void *data
)
519 const struct nlattr
**tb
= data
;
522 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
525 type
= mnl_attr_get_type(attr
);
526 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
533 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
535 const struct nlattr
**tb
= data
;
538 /* Allow the tool to work on top of newer kernels that might contain
541 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
544 type
= mnl_attr_get_type(attr
);
545 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
552 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
554 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
555 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
556 struct dl
*dl
= data
;
557 struct ifname_map
*ifname_map
;
558 const char *bus_name
;
559 const char *dev_name
;
560 uint32_t port_ifindex
;
561 const char *port_ifname
;
563 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
564 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
565 !tb
[DEVLINK_ATTR_PORT_INDEX
])
568 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
571 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
572 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
573 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
574 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
575 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
576 port_ifindex
, port_ifname
);
579 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
584 static void ifname_map_fini(struct dl
*dl
)
586 struct ifname_map
*ifname_map
, *tmp
;
588 list_for_each_entry_safe(ifname_map
, tmp
,
589 &dl
->ifname_map_list
, list
) {
590 list_del(&ifname_map
->list
);
591 ifname_map_free(ifname_map
);
595 static int ifname_map_init(struct dl
*dl
)
597 struct nlmsghdr
*nlh
;
600 INIT_LIST_HEAD(&dl
->ifname_map_list
);
602 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
603 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
605 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
613 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
614 char **p_bus_name
, char **p_dev_name
,
615 uint32_t *p_port_index
)
617 struct ifname_map
*ifname_map
;
619 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
620 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
621 *p_bus_name
= ifname_map
->bus_name
;
622 *p_dev_name
= ifname_map
->dev_name
;
623 *p_port_index
= ifname_map
->port_index
;
630 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
631 const char *dev_name
, uint32_t port_index
,
634 struct ifname_map
*ifname_map
;
636 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
637 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
638 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
639 port_index
== ifname_map
->port_index
) {
640 *p_ifname
= ifname_map
->ifname
;
647 static unsigned int strslashcount(char *str
)
649 unsigned int count
= 0;
652 while ((pos
= strchr(pos
, '/'))) {
659 static int strslashrsplit(char *str
, char **before
, char **after
)
663 slash
= strrchr(str
, '/');
672 static int strtouint64_t(const char *str
, uint64_t *p_val
)
675 unsigned long long int val
;
677 val
= strtoull(str
, &endptr
, 10);
678 if (endptr
== str
|| *endptr
!= '\0')
686 static int strtouint32_t(const char *str
, uint32_t *p_val
)
689 unsigned long int val
;
691 val
= strtoul(str
, &endptr
, 10);
692 if (endptr
== str
|| *endptr
!= '\0')
700 static int strtouint16_t(const char *str
, uint16_t *p_val
)
703 unsigned long int val
;
705 val
= strtoul(str
, &endptr
, 10);
706 if (endptr
== str
|| *endptr
!= '\0')
714 static int strtouint8_t(const char *str
, uint8_t *p_val
)
717 unsigned long int val
;
719 val
= strtoul(str
, &endptr
, 10);
720 if (endptr
== str
|| *endptr
!= '\0')
728 static int strtobool(const char *str
, bool *p_val
)
732 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
734 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
742 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
744 strslashrsplit(str
, p_bus_name
, p_dev_name
);
748 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
750 char *str
= dl_argv_next(dl
);
753 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
756 if (strslashcount(str
) != 1) {
757 pr_err("Wrong devlink identification string format.\n");
758 pr_err("Expected \"bus_name/dev_name\".\n");
761 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
764 static int __dl_argv_handle_port(char *str
,
765 char **p_bus_name
, char **p_dev_name
,
766 uint32_t *p_port_index
)
772 err
= strslashrsplit(str
, &handlestr
, &portstr
);
774 pr_err("Port identification \"%s\" is invalid\n", str
);
777 err
= strtouint32_t(portstr
, p_port_index
);
779 pr_err("Port index \"%s\" is not a number or not within range\n",
783 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
785 pr_err("Port identification \"%s\" is invalid\n", str
);
791 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
792 char **p_bus_name
, char **p_dev_name
,
793 uint32_t *p_port_index
)
797 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
800 pr_err("Netdevice \"%s\" not found\n", str
);
806 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
807 char **p_dev_name
, uint32_t *p_port_index
)
809 char *str
= dl_argv_next(dl
);
810 unsigned int slash_count
;
813 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
816 slash_count
= strslashcount(str
);
817 switch (slash_count
) {
819 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
820 p_dev_name
, p_port_index
);
822 return __dl_argv_handle_port(str
, p_bus_name
,
823 p_dev_name
, p_port_index
);
825 pr_err("Wrong port identification string format.\n");
826 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
831 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
832 char **p_dev_name
, uint32_t *p_port_index
,
833 uint64_t *p_handle_bit
)
835 char *str
= dl_argv_next(dl
);
836 unsigned int slash_count
;
840 pr_err("One of following identifications expected:\n"
841 "Devlink identification (\"bus_name/dev_name\")\n"
842 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
845 slash_count
= strslashcount(str
);
846 if (slash_count
== 1) {
847 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
850 *p_handle_bit
= DL_OPT_HANDLE
;
851 } else if (slash_count
== 2) {
852 err
= __dl_argv_handle_port(str
, p_bus_name
,
853 p_dev_name
, p_port_index
);
856 *p_handle_bit
= DL_OPT_HANDLEP
;
857 } else if (slash_count
== 0) {
858 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
859 p_dev_name
, p_port_index
);
862 *p_handle_bit
= DL_OPT_HANDLEP
;
864 pr_err("Wrong port identification string format.\n");
865 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
871 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
872 char **p_dev_name
, char **p_region
)
877 err
= strslashrsplit(str
, &handlestr
, p_region
);
879 pr_err("Region identification \"%s\" is invalid\n", str
);
882 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
884 pr_err("Region identification \"%s\" is invalid\n", str
);
890 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
891 char **p_dev_name
, char **p_region
)
893 char *str
= dl_argv_next(dl
);
894 unsigned int slash_count
;
897 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
901 slash_count
= strslashcount(str
);
902 if (slash_count
!= 2) {
903 pr_err("Wrong region identification string format.\n");
904 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
908 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
911 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
913 char *str
= dl_argv_next(dl
);
917 pr_err("Unsigned number argument expected\n");
921 err
= strtouint64_t(str
, p_val
);
923 pr_err("\"%s\" is not a number or not within range\n", str
);
929 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
931 char *str
= dl_argv_next(dl
);
935 pr_err("Unsigned number argument expected\n");
939 err
= strtouint32_t(str
, p_val
);
941 pr_err("\"%s\" is not a number or not within range\n", str
);
947 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
949 char *str
= dl_argv_next(dl
);
953 pr_err("Unsigned number argument expected\n");
957 err
= strtouint16_t(str
, p_val
);
959 pr_err("\"%s\" is not a number or not within range\n", str
);
965 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
967 char *str
= dl_argv_next(dl
);
971 pr_err("Boolean argument expected\n");
975 err
= strtobool(str
, p_val
);
977 pr_err("\"%s\" is not a valid boolean value\n", str
);
983 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
985 const char *str
= dl_argv_next(dl
);
988 pr_err("String parameter expected\n");
995 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
997 if (strcmp(typestr
, "auto") == 0) {
998 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
999 } else if (strcmp(typestr
, "eth") == 0) {
1000 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1001 } else if (strcmp(typestr
, "ib") == 0) {
1002 *p_type
= DEVLINK_PORT_TYPE_IB
;
1004 pr_err("Unknown port type \"%s\"\n", typestr
);
1010 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1012 if (strcmp(typestr
, "ingress") == 0) {
1013 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1014 } else if (strcmp(typestr
, "egress") == 0) {
1015 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1017 pr_err("Unknown pool type \"%s\"\n", typestr
);
1023 static int threshold_type_get(const char *typestr
,
1024 enum devlink_sb_threshold_type
*p_type
)
1026 if (strcmp(typestr
, "static") == 0) {
1027 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1028 } else if (strcmp(typestr
, "dynamic") == 0) {
1029 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1031 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1037 static int eswitch_mode_get(const char *typestr
,
1038 enum devlink_eswitch_mode
*p_mode
)
1040 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1041 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1042 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1043 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1045 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1051 static int eswitch_inline_mode_get(const char *typestr
,
1052 enum devlink_eswitch_inline_mode
*p_mode
)
1054 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1055 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1056 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1057 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1058 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1059 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1060 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1061 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1063 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1069 static int dpipe_counters_enable_get(const char *typestr
,
1070 bool *counters_enable
)
1072 if (strcmp(typestr
, "enable") == 0) {
1073 *counters_enable
= 1;
1074 } else if (strcmp(typestr
, "disable") == 0) {
1075 *counters_enable
= 0;
1077 pr_err("Unknown counter_state \"%s\"\n", typestr
);
1083 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
1085 if (strcmp(typestr
, "enable") == 0) {
1087 } else if (strcmp(typestr
, "disable") == 0) {
1090 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1096 static int param_cmode_get(const char *cmodestr
,
1097 enum devlink_param_cmode
*cmode
)
1099 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1100 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1101 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1102 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1103 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1104 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1106 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1112 static int trap_action_get(const char *actionstr
,
1113 enum devlink_trap_action
*p_action
)
1115 if (strcmp(actionstr
, "drop") == 0) {
1116 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1117 } else if (strcmp(actionstr
, "trap") == 0) {
1118 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1120 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1126 struct dl_args_metadata
{
1128 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1131 static const struct dl_args_metadata dl_args_required
[] = {
1132 {DL_OPT_PORT_TYPE
, "Port type not set."},
1133 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1134 {DL_OPT_SB_POOL
, "Pool index option expected."},
1135 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1136 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1137 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1138 {DL_OPT_SB_TH
, "Threshold option expected."},
1139 {DL_OPT_SB_TC
, "TC index option expected."},
1140 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1141 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1142 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1143 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1144 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1145 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1146 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1147 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1148 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1149 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1150 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1151 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1152 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1153 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1154 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1155 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1158 static int dl_args_finding_required_validate(uint64_t o_required
,
1164 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1165 o_flag
= dl_args_required
[i
].o_flag
;
1166 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1167 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1171 if (o_required
& ~o_found
) {
1172 pr_err("BUG: unknown argument required but not found\n");
1178 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1179 uint64_t o_optional
)
1181 struct dl_opts
*opts
= &dl
->opts
;
1182 uint64_t o_all
= o_required
| o_optional
;
1183 uint64_t o_found
= 0;
1186 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1187 uint64_t handle_bit
;
1189 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1190 &opts
->port_index
, &handle_bit
);
1193 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1194 o_found
|= handle_bit
;
1195 } else if (o_required
& DL_OPT_HANDLE
) {
1196 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1199 o_found
|= DL_OPT_HANDLE
;
1200 } else if (o_required
& DL_OPT_HANDLEP
) {
1201 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1205 o_found
|= DL_OPT_HANDLEP
;
1206 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1207 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1209 &opts
->region_name
);
1212 o_found
|= DL_OPT_HANDLE_REGION
;
1215 while (dl_argc(dl
)) {
1216 if (dl_argv_match(dl
, "type") &&
1217 (o_all
& DL_OPT_PORT_TYPE
)) {
1218 const char *typestr
;
1221 err
= dl_argv_str(dl
, &typestr
);
1224 err
= port_type_get(typestr
, &opts
->port_type
);
1227 o_found
|= DL_OPT_PORT_TYPE
;
1228 } else if (dl_argv_match(dl
, "count") &&
1229 (o_all
& DL_OPT_PORT_COUNT
)) {
1231 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1234 o_found
|= DL_OPT_PORT_COUNT
;
1235 } else if (dl_argv_match(dl
, "sb") &&
1236 (o_all
& DL_OPT_SB
)) {
1238 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1241 o_found
|= DL_OPT_SB
;
1242 } else if (dl_argv_match(dl
, "pool") &&
1243 (o_all
& DL_OPT_SB_POOL
)) {
1245 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1248 o_found
|= DL_OPT_SB_POOL
;
1249 } else if (dl_argv_match(dl
, "size") &&
1250 (o_all
& DL_OPT_SB_SIZE
)) {
1252 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1255 o_found
|= DL_OPT_SB_SIZE
;
1256 } else if (dl_argv_match(dl
, "type") &&
1257 (o_all
& DL_OPT_SB_TYPE
)) {
1258 const char *typestr
;
1261 err
= dl_argv_str(dl
, &typestr
);
1264 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1267 o_found
|= DL_OPT_SB_TYPE
;
1268 } else if (dl_argv_match(dl
, "thtype") &&
1269 (o_all
& DL_OPT_SB_THTYPE
)) {
1270 const char *typestr
;
1273 err
= dl_argv_str(dl
, &typestr
);
1276 err
= threshold_type_get(typestr
,
1277 &opts
->sb_pool_thtype
);
1280 o_found
|= DL_OPT_SB_THTYPE
;
1281 } else if (dl_argv_match(dl
, "th") &&
1282 (o_all
& DL_OPT_SB_TH
)) {
1284 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1287 o_found
|= DL_OPT_SB_TH
;
1288 } else if (dl_argv_match(dl
, "tc") &&
1289 (o_all
& DL_OPT_SB_TC
)) {
1291 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1294 o_found
|= DL_OPT_SB_TC
;
1295 } else if (dl_argv_match(dl
, "mode") &&
1296 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1297 const char *typestr
;
1300 err
= dl_argv_str(dl
, &typestr
);
1303 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1306 o_found
|= DL_OPT_ESWITCH_MODE
;
1307 } else if (dl_argv_match(dl
, "inline-mode") &&
1308 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1309 const char *typestr
;
1312 err
= dl_argv_str(dl
, &typestr
);
1315 err
= eswitch_inline_mode_get(
1316 typestr
, &opts
->eswitch_inline_mode
);
1319 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1320 } else if (dl_argv_match(dl
, "name") &&
1321 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1323 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1326 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1327 } else if (dl_argv_match(dl
, "counters") &&
1328 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1329 const char *typestr
;
1332 err
= dl_argv_str(dl
, &typestr
);
1335 err
= dpipe_counters_enable_get(typestr
,
1336 &opts
->dpipe_counters_enable
);
1339 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1340 } else if (dl_argv_match(dl
, "encap") &&
1341 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1342 const char *typestr
;
1345 err
= dl_argv_str(dl
, &typestr
);
1348 err
= eswitch_encap_mode_get(typestr
,
1349 &opts
->eswitch_encap_mode
);
1352 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1353 } else if (dl_argv_match(dl
, "path") &&
1354 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1356 err
= dl_argv_str(dl
, &opts
->resource_path
);
1359 o_found
|= DL_OPT_RESOURCE_PATH
;
1360 } else if (dl_argv_match(dl
, "size") &&
1361 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1363 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1366 o_found
|= DL_OPT_RESOURCE_SIZE
;
1367 } else if (dl_argv_match(dl
, "name") &&
1368 (o_all
& DL_OPT_PARAM_NAME
)) {
1370 err
= dl_argv_str(dl
, &opts
->param_name
);
1373 o_found
|= DL_OPT_PARAM_NAME
;
1374 } else if (dl_argv_match(dl
, "value") &&
1375 (o_all
& DL_OPT_PARAM_VALUE
)) {
1377 err
= dl_argv_str(dl
, &opts
->param_value
);
1380 o_found
|= DL_OPT_PARAM_VALUE
;
1381 } else if (dl_argv_match(dl
, "cmode") &&
1382 (o_all
& DL_OPT_PARAM_CMODE
)) {
1383 const char *cmodestr
;
1386 err
= dl_argv_str(dl
, &cmodestr
);
1389 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1392 o_found
|= DL_OPT_PARAM_CMODE
;
1393 } else if (dl_argv_match(dl
, "snapshot") &&
1394 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1396 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1399 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1400 } else if (dl_argv_match(dl
, "address") &&
1401 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1403 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1406 o_found
|= DL_OPT_REGION_ADDRESS
;
1407 } else if (dl_argv_match(dl
, "length") &&
1408 (o_all
& DL_OPT_REGION_LENGTH
)) {
1410 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1413 o_found
|= DL_OPT_REGION_LENGTH
;
1414 } else if (dl_argv_match(dl
, "file") &&
1415 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1417 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1420 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1421 } else if (dl_argv_match(dl
, "component") &&
1422 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1424 err
= dl_argv_str(dl
, &opts
->flash_component
);
1427 o_found
|= DL_OPT_FLASH_COMPONENT
;
1428 } else if (dl_argv_match(dl
, "reporter") &&
1429 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1431 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1434 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1435 } else if (dl_argv_match(dl
, "grace_period") &&
1436 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1438 err
= dl_argv_uint64_t(dl
,
1439 &opts
->reporter_graceful_period
);
1442 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1443 } else if (dl_argv_match(dl
, "auto_recover") &&
1444 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1446 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1449 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1450 } else if (dl_argv_match(dl
, "trap") &&
1451 (o_all
& DL_OPT_TRAP_NAME
)) {
1453 err
= dl_argv_str(dl
, &opts
->trap_name
);
1456 o_found
|= DL_OPT_TRAP_NAME
;
1457 } else if (dl_argv_match(dl
, "group") &&
1458 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1460 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1463 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1464 } else if (dl_argv_match(dl
, "action") &&
1465 (o_all
& DL_OPT_TRAP_ACTION
)) {
1466 const char *actionstr
;
1469 err
= dl_argv_str(dl
, &actionstr
);
1472 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1475 o_found
|= DL_OPT_TRAP_ACTION
;
1476 } else if (dl_argv_match(dl
, "netns") &&
1477 (o_all
& DL_OPT_NETNS
)) {
1478 const char *netns_str
;
1481 err
= dl_argv_str(dl
, &netns_str
);
1484 opts
->netns
= netns_get_fd(netns_str
);
1485 if ((int)opts
->netns
< 0) {
1487 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1490 opts
->netns_is_pid
= true;
1492 o_found
|= DL_OPT_NETNS
;
1494 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1499 opts
->present
= o_found
;
1501 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1503 opts
->present
|= DL_OPT_SB
;
1506 return dl_args_finding_required_validate(o_required
, o_found
);
1509 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1511 struct dl_opts
*opts
= &dl
->opts
;
1513 if (opts
->present
& DL_OPT_HANDLE
) {
1514 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1515 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1516 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1517 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1518 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1519 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1521 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1522 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1523 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1524 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1527 if (opts
->present
& DL_OPT_PORT_TYPE
)
1528 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1530 if (opts
->present
& DL_OPT_PORT_COUNT
)
1531 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1533 if (opts
->present
& DL_OPT_SB
)
1534 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1536 if (opts
->present
& DL_OPT_SB_POOL
)
1537 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1538 opts
->sb_pool_index
);
1539 if (opts
->present
& DL_OPT_SB_SIZE
)
1540 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1541 opts
->sb_pool_size
);
1542 if (opts
->present
& DL_OPT_SB_TYPE
)
1543 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1544 opts
->sb_pool_type
);
1545 if (opts
->present
& DL_OPT_SB_THTYPE
)
1546 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1547 opts
->sb_pool_thtype
);
1548 if (opts
->present
& DL_OPT_SB_TH
)
1549 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1550 opts
->sb_threshold
);
1551 if (opts
->present
& DL_OPT_SB_TC
)
1552 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1554 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1555 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1556 opts
->eswitch_mode
);
1557 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1558 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1559 opts
->eswitch_inline_mode
);
1560 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1561 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1562 opts
->dpipe_table_name
);
1563 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1564 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1565 opts
->dpipe_counters_enable
);
1566 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1567 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1568 opts
->eswitch_encap_mode
);
1569 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1570 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1572 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1573 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1574 opts
->resource_size
);
1575 if (opts
->present
& DL_OPT_PARAM_NAME
)
1576 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1578 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1579 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1581 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1582 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1583 opts
->region_snapshot_id
);
1584 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1585 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1586 opts
->region_address
);
1587 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1588 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1589 opts
->region_length
);
1590 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1591 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1592 opts
->flash_file_name
);
1593 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1594 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1595 opts
->flash_component
);
1596 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1597 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1598 opts
->reporter_name
);
1599 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1600 mnl_attr_put_u64(nlh
,
1601 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1602 opts
->reporter_graceful_period
);
1603 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1604 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1605 opts
->reporter_auto_recover
);
1606 if (opts
->present
& DL_OPT_TRAP_NAME
)
1607 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1609 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1610 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1611 opts
->trap_group_name
);
1612 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1613 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1615 if (opts
->present
& DL_OPT_NETNS
)
1616 mnl_attr_put_u32(nlh
,
1617 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1618 DEVLINK_ATTR_NETNS_FD
,
1622 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1623 uint64_t o_required
, uint64_t o_optional
)
1627 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1630 dl_opts_put(nlh
, dl
);
1634 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1636 struct dl_opts
*opts
= &dl
->opts
;
1637 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1638 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1639 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1640 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1642 if (opts
->present
& DL_OPT_HANDLE
&&
1643 attr_bus_name
&& attr_dev_name
) {
1644 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1645 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1647 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1648 strcmp(dev_name
, opts
->dev_name
) != 0)
1651 if (opts
->present
& DL_OPT_HANDLEP
&&
1652 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1653 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1654 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1655 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1657 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1658 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1659 port_index
!= opts
->port_index
)
1662 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1663 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1665 if (sb_index
!= opts
->sb_index
)
1671 static void cmd_dev_help(void)
1673 pr_err("Usage: devlink dev show [ DEV ]\n");
1674 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1675 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1676 pr_err(" [ encap { disable | enable } ]\n");
1677 pr_err(" devlink dev eswitch show DEV\n");
1678 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1679 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1680 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1681 pr_err(" devlink dev info [ DEV ]\n");
1682 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1685 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1686 const char *dev_name
)
1688 if (!dl
->arr_last
.present
)
1690 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1691 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1694 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1695 const char *dev_name
)
1697 dl
->arr_last
.present
= true;
1698 free(dl
->arr_last
.dev_name
);
1699 free(dl
->arr_last
.bus_name
);
1700 dl
->arr_last
.bus_name
= strdup(bus_name
);
1701 dl
->arr_last
.dev_name
= strdup(dev_name
);
1704 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1705 const char *dev_name
)
1707 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1710 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1711 const char *dev_name
)
1713 return dl
->arr_last
.present
&&
1714 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1717 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1718 bool content
, bool array
)
1720 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1721 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1724 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1726 if (dl
->json_output
) {
1728 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1729 close_json_array(PRINT_JSON
, NULL
);
1730 if (should_arr_last_handle_start(dl
, bus_name
,
1732 open_json_array(PRINT_JSON
, buf
);
1733 open_json_object(NULL
);
1734 arr_last_handle_set(dl
, bus_name
, dev_name
);
1736 open_json_object(NULL
);
1739 open_json_object(buf
);
1743 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1744 __pr_out_indent_dec();
1745 if (should_arr_last_handle_start(dl
, bus_name
,
1747 pr_out("%s%s", buf
, content
? ":" : "");
1749 __pr_out_indent_inc();
1750 arr_last_handle_set(dl
, bus_name
, dev_name
);
1753 pr_out("%s%s", buf
, content
? ":" : "");
1758 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1760 __pr_out_handle_start(dl
, tb
, true, true);
1763 static void pr_out_handle_end(struct dl
*dl
)
1765 if (dl
->json_output
)
1766 close_json_object();
1771 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1773 __pr_out_handle_start(dl
, tb
, false, false);
1774 pr_out_handle_end(dl
);
1777 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1778 const char *dev_name
, uint32_t port_index
)
1780 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1781 dl
->arr_last
.port_index
== port_index
;
1784 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1785 const char *dev_name
, uint32_t port_index
)
1787 arr_last_handle_set(dl
, bus_name
, dev_name
);
1788 dl
->arr_last
.port_index
= port_index
;
1791 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1792 const char *bus_name
,
1793 const char *dev_name
,
1794 uint32_t port_index
)
1796 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1799 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1800 const char *bus_name
,
1801 const char *dev_name
,
1802 uint32_t port_index
)
1804 return dl
->arr_last
.present
&&
1805 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1808 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1809 const char *dev_name
,
1810 uint32_t port_index
, bool try_nice
,
1813 static char buf
[64];
1814 char *ifname
= NULL
;
1816 if (dl
->no_nice_names
|| !try_nice
||
1817 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1818 port_index
, &ifname
) != 0)
1819 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1821 sprintf(buf
, "%s", ifname
);
1823 if (dl
->json_output
) {
1825 if (should_arr_last_port_handle_end(dl
, bus_name
,
1828 close_json_array(PRINT_JSON
, NULL
);
1829 if (should_arr_last_port_handle_start(dl
, bus_name
,
1832 open_json_array(PRINT_JSON
, buf
);
1833 open_json_object(NULL
);
1834 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1837 open_json_object(NULL
);
1840 open_json_object(buf
);
1847 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1849 const char *bus_name
;
1850 const char *dev_name
;
1851 uint32_t port_index
;
1853 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1854 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1855 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1856 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1859 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1861 const char *bus_name
;
1862 const char *dev_name
;
1863 uint32_t port_index
;
1865 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1866 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1867 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1868 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1871 static void pr_out_port_handle_end(struct dl
*dl
)
1873 if (dl
->json_output
)
1874 close_json_object();
1879 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1881 __pr_out_indent_newline(dl
);
1882 if (val
== (uint64_t) -1)
1883 return print_string_name_value(name
, "unlimited");
1885 if (dl
->json_output
)
1886 print_u64(PRINT_JSON
, name
, NULL
, val
);
1888 pr_out("%s %"PRIu64
, name
, val
);
1891 static bool is_binary_eol(int i
)
1896 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1901 if (dl
->json_output
)
1902 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1904 pr_out("%02x ", data
[i
]);
1906 if (!dl
->json_output
&& is_binary_eol(i
))
1909 if (!dl
->json_output
&& !is_binary_eol(i
))
1913 static void pr_out_name(struct dl
*dl
, const char *name
)
1915 __pr_out_indent_newline(dl
);
1916 if (dl
->json_output
)
1917 print_string(PRINT_JSON
, name
, NULL
, NULL
);
1919 pr_out("%s:", name
);
1922 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1924 if (dl
->json_output
) {
1925 print_uint(PRINT_JSON
, "address", NULL
, addr
);
1926 open_json_array(PRINT_JSON
, "data");
1930 static void pr_out_region_chunk_end(struct dl
*dl
)
1932 if (dl
->json_output
)
1933 close_json_array(PRINT_JSON
, NULL
);
1936 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1939 static uint64_t align_val
;
1942 pr_out_region_chunk_start(dl
, addr
);
1944 if (!dl
->json_output
)
1945 if (!(align_val
% 16))
1946 pr_out("%s%016"PRIx64
" ",
1947 align_val
? "\n" : "",
1952 if (dl
->json_output
)
1953 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1955 pr_out("%02x ", data
[i
]);
1960 pr_out_region_chunk_end(dl
);
1963 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1965 if (dl
->json_output
) {
1966 open_json_object(NULL
);
1967 open_json_object(name
);
1971 static void pr_out_section_end(struct dl
*dl
)
1973 if (dl
->json_output
) {
1974 if (dl
->arr_last
.present
)
1975 close_json_array(PRINT_JSON
, NULL
);
1976 close_json_object();
1977 close_json_object();
1981 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1983 if (dl
->json_output
) {
1984 open_json_array(PRINT_JSON
, name
);
1986 __pr_out_indent_inc();
1988 pr_out("%s:", name
);
1989 __pr_out_indent_inc();
1994 static void pr_out_array_end(struct dl
*dl
)
1996 if (dl
->json_output
) {
1997 close_json_array(PRINT_JSON
, NULL
);
1999 __pr_out_indent_dec();
2000 __pr_out_indent_dec();
2004 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2006 if (dl
->json_output
) {
2007 open_json_object(name
);
2009 __pr_out_indent_inc();
2011 pr_out("%s:", name
);
2012 __pr_out_indent_inc();
2017 static void pr_out_object_end(struct dl
*dl
)
2019 if (dl
->json_output
) {
2020 close_json_object();
2022 __pr_out_indent_dec();
2023 __pr_out_indent_dec();
2027 static void pr_out_entry_start(struct dl
*dl
)
2029 if (dl
->json_output
)
2030 open_json_object(NULL
);
2033 static void pr_out_entry_end(struct dl
*dl
)
2035 if (dl
->json_output
)
2036 close_json_object();
2041 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2043 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2049 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2050 if (err
!= MNL_CB_OK
)
2053 pr_out_object_start(dl
, "stats");
2054 pr_out_object_start(dl
, "rx");
2055 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2056 pr_out_u64(dl
, "bytes",
2057 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2058 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2059 pr_out_u64(dl
, "packets",
2060 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2061 pr_out_object_end(dl
);
2062 pr_out_object_end(dl
);
2065 static const char *param_cmode_name(uint8_t cmode
)
2068 case DEVLINK_PARAM_CMODE_RUNTIME
:
2069 return PARAM_CMODE_RUNTIME_STR
;
2070 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2071 return PARAM_CMODE_DRIVERINIT_STR
;
2072 case DEVLINK_PARAM_CMODE_PERMANENT
:
2073 return PARAM_CMODE_PERMANENT_STR
;
2074 default: return "<unknown type>";
2078 static const char *eswitch_mode_name(uint32_t mode
)
2081 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2082 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2083 default: return "<unknown mode>";
2087 static const char *eswitch_inline_mode_name(uint32_t mode
)
2090 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2091 return ESWITCH_INLINE_MODE_NONE
;
2092 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2093 return ESWITCH_INLINE_MODE_LINK
;
2094 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2095 return ESWITCH_INLINE_MODE_NETWORK
;
2096 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2097 return ESWITCH_INLINE_MODE_TRANSPORT
;
2099 return "<unknown mode>";
2103 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2105 __pr_out_handle_start(dl
, tb
, true, false);
2107 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2108 check_indent_newline(dl
);
2109 print_string(PRINT_ANY
, "mode", "mode %s",
2110 eswitch_mode_name(mnl_attr_get_u16(
2111 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2113 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2114 check_indent_newline(dl
);
2115 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2116 eswitch_inline_mode_name(mnl_attr_get_u8(
2117 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2119 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2120 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2122 check_indent_newline(dl
);
2123 print_string(PRINT_ANY
, "encap", "encap %s",
2124 encap_mode
? "enable" : "disable");
2127 pr_out_handle_end(dl
);
2130 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2132 struct dl
*dl
= data
;
2133 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2134 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2136 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2137 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2138 return MNL_CB_ERROR
;
2139 pr_out_eswitch(dl
, tb
);
2143 static int cmd_dev_eswitch_show(struct dl
*dl
)
2145 struct nlmsghdr
*nlh
;
2148 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2149 NLM_F_REQUEST
| NLM_F_ACK
);
2151 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2155 pr_out_section_start(dl
, "dev");
2156 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2157 pr_out_section_end(dl
);
2161 static int cmd_dev_eswitch_set(struct dl
*dl
)
2163 struct nlmsghdr
*nlh
;
2166 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2167 NLM_F_REQUEST
| NLM_F_ACK
);
2169 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2170 DL_OPT_ESWITCH_MODE
|
2171 DL_OPT_ESWITCH_INLINE_MODE
|
2172 DL_OPT_ESWITCH_ENCAP_MODE
);
2177 if (dl
->opts
.present
== 1) {
2178 pr_err("Need to set at least one option\n");
2182 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2185 static int cmd_dev_eswitch(struct dl
*dl
)
2187 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2190 } else if (dl_argv_match(dl
, "set")) {
2192 return cmd_dev_eswitch_set(dl
);
2193 } else if (dl_argv_match(dl
, "show")) {
2195 return cmd_dev_eswitch_show(dl
);
2197 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2201 struct param_val_conv
{
2207 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2208 uint32_t len
, const char *name
)
2212 for (i
= 0; i
< len
; i
++)
2213 if (!strcmp(param_val_conv
[i
].name
, name
))
2220 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2221 uint32_t len
, const char *name
, const char *vstr
,
2226 for (i
= 0; i
< len
; i
++)
2227 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2228 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2229 *vuint
= param_val_conv
[i
].vuint
;
2237 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2238 uint32_t len
, const char *name
, uint32_t vuint
,
2243 for (i
= 0; i
< len
; i
++)
2244 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2245 param_val_conv
[i
].vuint
== vuint
) {
2246 *vstr
= param_val_conv
[i
].vstr
;
2253 static const struct param_val_conv param_val_conv
[] = {
2255 .name
= "fw_load_policy",
2257 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2260 .name
= "fw_load_policy",
2262 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2265 .name
= "reset_dev_on_drv_probe",
2267 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2270 .name
= "fw_load_policy",
2272 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2275 .name
= "reset_dev_on_drv_probe",
2277 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2280 .name
= "reset_dev_on_drv_probe",
2282 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2285 .name
= "reset_dev_on_drv_probe",
2287 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2291 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2293 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2294 int nla_type
, struct nlattr
*nl
)
2296 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2297 struct nlattr
*val_attr
;
2302 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2303 if (err
!= MNL_CB_OK
)
2306 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2307 (nla_type
!= MNL_TYPE_FLAG
&&
2308 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2311 check_indent_newline(dl
);
2312 print_string(PRINT_ANY
, "cmode", "cmode %s",
2313 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2315 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2317 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2323 err
= param_val_conv_str_get(param_val_conv
,
2326 mnl_attr_get_u8(val_attr
),
2330 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2332 print_uint(PRINT_ANY
, "value", " value %u",
2333 mnl_attr_get_u8(val_attr
));
2338 err
= param_val_conv_str_get(param_val_conv
,
2341 mnl_attr_get_u16(val_attr
),
2345 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2347 print_uint(PRINT_ANY
, "value", " value %u",
2348 mnl_attr_get_u16(val_attr
));
2353 err
= param_val_conv_str_get(param_val_conv
,
2356 mnl_attr_get_u32(val_attr
),
2360 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2362 print_uint(PRINT_ANY
, "value", " value %u",
2363 mnl_attr_get_u32(val_attr
));
2366 case MNL_TYPE_STRING
:
2367 print_string(PRINT_ANY
, "value", " value %s",
2368 mnl_attr_get_str(val_attr
));
2371 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2376 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2378 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2379 struct nlattr
*param_value_attr
;
2380 const char *nla_name
;
2384 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2385 if (err
!= MNL_CB_OK
)
2387 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2388 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2389 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2393 pr_out_handle_start_arr(dl
, tb
);
2395 __pr_out_handle_start(dl
, tb
, true, false);
2397 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2399 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2400 check_indent_newline(dl
);
2401 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2402 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2403 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2405 print_string(PRINT_ANY
, "type", "type %s", "generic");
2407 pr_out_array_start(dl
, "values");
2408 mnl_attr_for_each_nested(param_value_attr
,
2409 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2410 pr_out_entry_start(dl
);
2411 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2412 pr_out_entry_end(dl
);
2414 pr_out_array_end(dl
);
2415 pr_out_handle_end(dl
);
2418 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2420 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2421 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2422 struct dl
*dl
= data
;
2424 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2425 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2426 !tb
[DEVLINK_ATTR_PARAM
])
2427 return MNL_CB_ERROR
;
2428 pr_out_param(dl
, tb
, true);
2444 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2446 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2447 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2448 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2449 struct nlattr
*param_value_attr
;
2450 enum devlink_param_cmode cmode
;
2451 struct param_ctx
*ctx
= data
;
2452 struct dl
*dl
= ctx
->dl
;
2456 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2457 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2458 !tb
[DEVLINK_ATTR_PARAM
])
2459 return MNL_CB_ERROR
;
2461 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2462 if (err
!= MNL_CB_OK
)
2463 return MNL_CB_ERROR
;
2465 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2466 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2467 return MNL_CB_ERROR
;
2469 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2470 mnl_attr_for_each_nested(param_value_attr
,
2471 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2472 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2473 struct nlattr
*val_attr
;
2475 err
= mnl_attr_parse_nested(param_value_attr
,
2476 attr_cb
, nla_value
);
2477 if (err
!= MNL_CB_OK
)
2478 return MNL_CB_ERROR
;
2480 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2481 (nla_type
!= MNL_TYPE_FLAG
&&
2482 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2483 return MNL_CB_ERROR
;
2485 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2486 if (cmode
== dl
->opts
.cmode
) {
2487 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2490 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2493 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2496 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2498 case MNL_TYPE_STRING
:
2499 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2502 ctx
->value
.vbool
= val_attr
? true : false;
2508 ctx
->nla_type
= nla_type
;
2512 static int cmd_dev_param_set(struct dl
*dl
)
2514 struct param_ctx ctx
= {};
2515 struct nlmsghdr
*nlh
;
2523 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2525 DL_OPT_PARAM_VALUE
|
2526 DL_OPT_PARAM_CMODE
, 0);
2530 /* Get value type */
2531 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2532 NLM_F_REQUEST
| NLM_F_ACK
);
2533 dl_opts_put(nlh
, dl
);
2536 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2540 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2541 NLM_F_REQUEST
| NLM_F_ACK
);
2542 dl_opts_put(nlh
, dl
);
2544 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2545 dl
->opts
.param_name
);
2547 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2548 switch (ctx
.nla_type
) {
2551 err
= param_val_conv_uint_get(param_val_conv
,
2553 dl
->opts
.param_name
,
2554 dl
->opts
.param_value
,
2558 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2561 goto err_param_value_parse
;
2562 if (val_u8
== ctx
.value
.vu8
)
2564 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2568 err
= param_val_conv_uint_get(param_val_conv
,
2570 dl
->opts
.param_name
,
2571 dl
->opts
.param_value
,
2575 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2578 goto err_param_value_parse
;
2579 if (val_u16
== ctx
.value
.vu16
)
2581 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2585 err
= param_val_conv_uint_get(param_val_conv
,
2587 dl
->opts
.param_name
,
2588 dl
->opts
.param_value
,
2591 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2593 goto err_param_value_parse
;
2594 if (val_u32
== ctx
.value
.vu32
)
2596 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2599 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2601 goto err_param_value_parse
;
2602 if (val_bool
== ctx
.value
.vbool
)
2605 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2608 case MNL_TYPE_STRING
:
2609 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2610 dl
->opts
.param_value
);
2611 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2615 printf("Value type not supported\n");
2618 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2620 err_param_value_parse
:
2621 pr_err("Value \"%s\" is not a number or not within range\n",
2622 dl
->opts
.param_value
);
2626 static int cmd_dev_param_show(struct dl
*dl
)
2628 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2629 struct nlmsghdr
*nlh
;
2632 if (dl_argc(dl
) == 0)
2633 flags
|= NLM_F_DUMP
;
2635 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2637 if (dl_argc(dl
) > 0) {
2638 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2639 DL_OPT_PARAM_NAME
, 0);
2644 pr_out_section_start(dl
, "param");
2645 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2646 pr_out_section_end(dl
);
2650 static int cmd_dev_param(struct dl
*dl
)
2652 if (dl_argv_match(dl
, "help")) {
2655 } else if (dl_argv_match(dl
, "show") ||
2656 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2658 return cmd_dev_param_show(dl
);
2659 } else if (dl_argv_match(dl
, "set")) {
2661 return cmd_dev_param_set(dl
);
2663 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2666 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2668 struct dl
*dl
= data
;
2669 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2670 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2671 uint8_t reload_failed
= 0;
2673 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2674 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2675 return MNL_CB_ERROR
;
2677 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2678 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2680 if (reload_failed
) {
2681 __pr_out_handle_start(dl
, tb
, true, false);
2682 check_indent_newline(dl
);
2683 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2684 pr_out_handle_end(dl
);
2686 pr_out_handle(dl
, tb
);
2692 static int cmd_dev_show(struct dl
*dl
)
2694 struct nlmsghdr
*nlh
;
2695 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2698 if (dl_argc(dl
) == 0)
2699 flags
|= NLM_F_DUMP
;
2701 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2703 if (dl_argc(dl
) > 0) {
2704 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2709 pr_out_section_start(dl
, "dev");
2710 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2711 pr_out_section_end(dl
);
2715 static void cmd_dev_reload_help(void)
2717 pr_err("Usage: devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2720 static int cmd_dev_reload(struct dl
*dl
)
2722 struct nlmsghdr
*nlh
;
2725 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2726 cmd_dev_reload_help();
2730 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2731 NLM_F_REQUEST
| NLM_F_ACK
);
2733 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2737 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2740 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2741 const char *name
, int type
)
2743 struct nlattr
*version
;
2745 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2746 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2747 const char *ver_value
;
2748 const char *ver_name
;
2751 if (mnl_attr_get_type(version
) != type
)
2754 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2755 if (err
!= MNL_CB_OK
)
2758 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2759 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2763 pr_out_object_start(dl
, name
);
2767 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2768 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2770 check_indent_newline(dl
);
2771 print_string_name_value(ver_name
, ver_value
);
2772 if (!dl
->json_output
)
2777 pr_out_object_end(dl
);
2780 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2781 struct nlattr
**tb
, bool has_versions
)
2783 __pr_out_handle_start(dl
, tb
, true, false);
2785 __pr_out_indent_inc();
2786 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2787 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2789 if (!dl
->json_output
)
2791 check_indent_newline(dl
);
2792 print_string(PRINT_ANY
, "driver", "driver %s",
2793 mnl_attr_get_str(nla_drv
));
2796 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2797 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2799 if (!dl
->json_output
)
2801 check_indent_newline(dl
);
2802 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2803 mnl_attr_get_str(nla_sn
));
2805 __pr_out_indent_dec();
2808 pr_out_object_start(dl
, "versions");
2810 pr_out_versions_single(dl
, nlh
, "fixed",
2811 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2812 pr_out_versions_single(dl
, nlh
, "running",
2813 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2814 pr_out_versions_single(dl
, nlh
, "stored",
2815 DEVLINK_ATTR_INFO_VERSION_STORED
);
2817 pr_out_object_end(dl
);
2820 pr_out_handle_end(dl
);
2823 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2825 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2826 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2827 bool has_versions
, has_info
;
2828 struct dl
*dl
= data
;
2830 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2832 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2833 return MNL_CB_ERROR
;
2835 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2836 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2837 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2838 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2839 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2843 pr_out_info(dl
, nlh
, tb
, has_versions
);
2848 static void cmd_dev_info_help(void)
2850 pr_err("Usage: devlink dev info [ DEV ]\n");
2853 static int cmd_dev_info(struct dl
*dl
)
2855 struct nlmsghdr
*nlh
;
2856 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2859 if (dl_argv_match(dl
, "help")) {
2860 cmd_dev_info_help();
2864 if (dl_argc(dl
) == 0)
2865 flags
|= NLM_F_DUMP
;
2867 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2869 if (dl_argc(dl
) > 0) {
2870 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2875 pr_out_section_start(dl
, "info");
2876 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2877 pr_out_section_end(dl
);
2881 static void cmd_dev_flash_help(void)
2883 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2887 struct cmd_dev_flash_status_ctx
{
2890 char *last_component
;
2891 uint8_t not_first
:1,
2897 static int nullstrcmp(const char *str1
, const char *str2
)
2900 return strcmp(str1
, str2
);
2903 return str1
? 1 : -1;
2906 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2908 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2909 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2910 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2911 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2912 const char *component
= NULL
;
2913 uint64_t done
= 0, total
= 0;
2914 const char *msg
= NULL
;
2915 const char *bus_name
;
2916 const char *dev_name
;
2918 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2919 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2922 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2923 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2924 return MNL_CB_ERROR
;
2925 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2926 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2927 if (strcmp(bus_name
, opts
->bus_name
) ||
2928 strcmp(dev_name
, opts
->dev_name
))
2929 return MNL_CB_ERROR
;
2931 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2933 free(ctx
->last_msg
);
2934 free(ctx
->last_component
);
2935 ctx
->received_end
= 1;
2939 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2940 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2941 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2942 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2943 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2944 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2945 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2946 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
2948 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
2949 !nullstrcmp(component
, ctx
->last_component
) &&
2950 ctx
->last_pc
&& ctx
->not_first
) {
2951 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
2956 pr_out("[%s] ", component
);
2957 free(ctx
->last_component
);
2958 ctx
->last_component
= strdup(component
);
2962 free(ctx
->last_msg
);
2963 ctx
->last_msg
= strdup(msg
);
2967 pr_out_tty(" %3lu%%", (done
* 100) / total
);
2978 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
2979 struct mnlg_socket
*nlg_ntf
,
2982 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
2989 for (i
= 0; i
< 3; i
++)
2991 FD_SET(pipe_r
, &fds
[0]);
2993 FD_SET(nlfd
, &fds
[0]);
2997 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3000 pr_err("select() failed\n");
3003 if (FD_ISSET(nlfd
, &fds
[0])) {
3004 err
= _mnlg_socket_recv_run(nlg_ntf
,
3005 cmd_dev_flash_status_cb
, ctx
);
3009 if (FD_ISSET(pipe_r
, &fds
[0])) {
3010 err
= read(pipe_r
, &err2
, sizeof(err2
));
3012 pr_err("Failed to read pipe\n");
3017 ctx
->flash_done
= 1;
3023 static int cmd_dev_flash(struct dl
*dl
)
3025 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3026 struct mnlg_socket
*nlg_ntf
;
3027 struct nlmsghdr
*nlh
;
3033 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3034 cmd_dev_flash_help();
3038 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3039 NLM_F_REQUEST
| NLM_F_ACK
);
3041 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3042 DL_OPT_FLASH_COMPONENT
);
3046 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3050 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3054 err
= pipe(pipe_fds
);
3057 pipe_r
= pipe_fds
[0];
3058 pipe_w
= pipe_fds
[1];
3066 /* In child, just execute the flash and pass returned
3067 * value through pipe once it is done.
3072 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3073 cc
= write(pipe_w
, &err
, sizeof(err
));
3075 exit(cc
!= sizeof(err
));
3080 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3083 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3085 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3088 mnlg_socket_close(nlg_ntf
);
3092 static int cmd_dev(struct dl
*dl
)
3094 if (dl_argv_match(dl
, "help")) {
3097 } else if (dl_argv_match(dl
, "show") ||
3098 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3100 return cmd_dev_show(dl
);
3101 } else if (dl_argv_match(dl
, "eswitch")) {
3103 return cmd_dev_eswitch(dl
);
3104 } else if (dl_argv_match(dl
, "reload")) {
3106 return cmd_dev_reload(dl
);
3107 } else if (dl_argv_match(dl
, "param")) {
3109 return cmd_dev_param(dl
);
3110 } else if (dl_argv_match(dl
, "info")) {
3112 return cmd_dev_info(dl
);
3113 } else if (dl_argv_match(dl
, "flash")) {
3115 return cmd_dev_flash(dl
);
3117 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3121 static void cmd_port_help(void)
3123 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3124 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3125 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3126 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3129 static const char *port_type_name(uint32_t type
)
3132 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3133 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3134 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3135 case DEVLINK_PORT_TYPE_IB
: return "ib";
3136 default: return "<unknown type>";
3140 static const char *port_flavour_name(uint16_t flavour
)
3143 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3145 case DEVLINK_PORT_FLAVOUR_CPU
:
3147 case DEVLINK_PORT_FLAVOUR_DSA
:
3149 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3151 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3154 return "<unknown flavour>";
3158 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3162 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3163 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3164 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3166 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3167 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3168 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3172 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3174 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3175 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3177 pr_out_port_handle_start(dl
, tb
, false);
3178 check_indent_newline(dl
);
3180 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3182 print_string(PRINT_ANY
, "type", "type %s",
3183 port_type_name(port_type
));
3185 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3187 if (port_type
!= des_port_type
)
3188 print_string(PRINT_ANY
, "des_type", " des_type %s",
3189 port_type_name(des_port_type
));
3192 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3193 print_string(PRINT_ANY
, "netdev", " netdev %s",
3194 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3196 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3197 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3198 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3200 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3201 uint16_t port_flavour
=
3202 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3204 print_string(PRINT_ANY
, "flavour", " flavour %s",
3205 port_flavour_name(port_flavour
));
3207 switch (port_flavour
) {
3208 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3209 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3210 pr_out_port_pfvf_num(dl
, tb
);
3216 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3217 uint32_t port_number
;
3219 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3220 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3222 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3223 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3224 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3225 pr_out_port_handle_end(dl
);
3228 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3230 struct dl
*dl
= data
;
3231 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3232 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3234 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3235 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3236 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3237 return MNL_CB_ERROR
;
3238 pr_out_port(dl
, tb
);
3242 static int cmd_port_show(struct dl
*dl
)
3244 struct nlmsghdr
*nlh
;
3245 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3248 if (dl_argc(dl
) == 0)
3249 flags
|= NLM_F_DUMP
;
3251 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3253 if (dl_argc(dl
) > 0) {
3254 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3259 pr_out_section_start(dl
, "port");
3260 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3261 pr_out_section_end(dl
);
3265 static int cmd_port_set(struct dl
*dl
)
3267 struct nlmsghdr
*nlh
;
3270 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3271 NLM_F_REQUEST
| NLM_F_ACK
);
3273 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3277 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3280 static int cmd_port_split(struct dl
*dl
)
3282 struct nlmsghdr
*nlh
;
3285 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3286 NLM_F_REQUEST
| NLM_F_ACK
);
3288 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3292 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3295 static int cmd_port_unsplit(struct dl
*dl
)
3297 struct nlmsghdr
*nlh
;
3300 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3301 NLM_F_REQUEST
| NLM_F_ACK
);
3303 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3307 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3310 static int cmd_port(struct dl
*dl
)
3312 if (dl_argv_match(dl
, "help")) {
3315 } else if (dl_argv_match(dl
, "show") ||
3316 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3318 return cmd_port_show(dl
);
3319 } else if (dl_argv_match(dl
, "set")) {
3321 return cmd_port_set(dl
);
3322 } else if (dl_argv_match(dl
, "split")) {
3324 return cmd_port_split(dl
);
3325 } else if (dl_argv_match(dl
, "unsplit")) {
3327 return cmd_port_unsplit(dl
);
3329 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3333 static void cmd_sb_help(void)
3335 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3336 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3337 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3338 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3339 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3340 pr_err(" pool POOL_INDEX ]\n");
3341 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3342 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3343 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3344 pr_err(" type { ingress | egress } ]\n");
3345 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3346 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3347 pr_err(" th THRESHOLD\n");
3348 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3349 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3350 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3353 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3355 pr_out_handle_start_arr(dl
, tb
);
3356 check_indent_newline(dl
);
3357 print_uint(PRINT_ANY
, "sb", "sb %u",
3358 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3359 print_uint(PRINT_ANY
, "size", " size %u",
3360 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3361 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3362 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3363 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3364 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3365 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3366 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3367 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3368 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3369 pr_out_handle_end(dl
);
3372 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3374 struct dl
*dl
= data
;
3375 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3376 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3378 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3379 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3380 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3381 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3382 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3383 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3384 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3385 return MNL_CB_ERROR
;
3390 static int cmd_sb_show(struct dl
*dl
)
3392 struct nlmsghdr
*nlh
;
3393 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3396 if (dl_argc(dl
) == 0)
3397 flags
|= NLM_F_DUMP
;
3399 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3401 if (dl_argc(dl
) > 0) {
3402 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3407 pr_out_section_start(dl
, "sb");
3408 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3409 pr_out_section_end(dl
);
3413 static const char *pool_type_name(uint8_t type
)
3416 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3417 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3418 default: return "<unknown type>";
3422 static const char *threshold_type_name(uint8_t type
)
3425 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3426 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3427 default: return "<unknown type>";
3431 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3433 pr_out_handle_start_arr(dl
, tb
);
3434 check_indent_newline(dl
);
3435 print_uint(PRINT_ANY
, "sb", "sb %u",
3436 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3437 print_uint(PRINT_ANY
, "pool", " pool %u",
3438 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3439 print_string(PRINT_ANY
, "type", " type %s",
3440 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3441 print_uint(PRINT_ANY
, "size", " size %u",
3442 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3443 print_string(PRINT_ANY
, "thtype", " thtype %s",
3444 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3445 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3446 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3447 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3448 pr_out_handle_end(dl
);
3451 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3453 struct dl
*dl
= data
;
3454 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3455 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3457 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3458 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3459 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3460 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3461 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3462 return MNL_CB_ERROR
;
3463 pr_out_sb_pool(dl
, tb
);
3467 static int cmd_sb_pool_show(struct dl
*dl
)
3469 struct nlmsghdr
*nlh
;
3470 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3473 if (dl_argc(dl
) == 0)
3474 flags
|= NLM_F_DUMP
;
3476 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3478 if (dl_argc(dl
) > 0) {
3479 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3485 pr_out_section_start(dl
, "pool");
3486 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3487 pr_out_section_end(dl
);
3491 static int cmd_sb_pool_set(struct dl
*dl
)
3493 struct nlmsghdr
*nlh
;
3496 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3497 NLM_F_REQUEST
| NLM_F_ACK
);
3499 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3500 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3504 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3507 static int cmd_sb_pool(struct dl
*dl
)
3509 if (dl_argv_match(dl
, "help")) {
3512 } else if (dl_argv_match(dl
, "show") ||
3513 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3515 return cmd_sb_pool_show(dl
);
3516 } else if (dl_argv_match(dl
, "set")) {
3518 return cmd_sb_pool_set(dl
);
3520 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3524 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3526 pr_out_port_handle_start_arr(dl
, tb
, true);
3527 check_indent_newline(dl
);
3528 print_uint(PRINT_ANY
, "sb", "sb %u",
3529 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3530 print_uint(PRINT_ANY
, "pool", " pool %u",
3531 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3532 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3533 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3534 pr_out_port_handle_end(dl
);
3537 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3539 struct dl
*dl
= data
;
3540 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3541 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3543 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3544 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3545 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3546 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3547 return MNL_CB_ERROR
;
3548 pr_out_sb_port_pool(dl
, tb
);
3552 static int cmd_sb_port_pool_show(struct dl
*dl
)
3554 struct nlmsghdr
*nlh
;
3555 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3558 if (dl_argc(dl
) == 0)
3559 flags
|= NLM_F_DUMP
;
3561 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3563 if (dl_argc(dl
) > 0) {
3564 err
= dl_argv_parse_put(nlh
, dl
,
3565 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3571 pr_out_section_start(dl
, "port_pool");
3572 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3573 pr_out_section_end(dl
);
3577 static int cmd_sb_port_pool_set(struct dl
*dl
)
3579 struct nlmsghdr
*nlh
;
3582 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3583 NLM_F_REQUEST
| NLM_F_ACK
);
3585 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3586 DL_OPT_SB_TH
, DL_OPT_SB
);
3590 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3593 static int cmd_sb_port_pool(struct dl
*dl
)
3595 if (dl_argv_match(dl
, "help")) {
3598 } else if (dl_argv_match(dl
, "show") ||
3599 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3601 return cmd_sb_port_pool_show(dl
);
3602 } else if (dl_argv_match(dl
, "set")) {
3604 return cmd_sb_port_pool_set(dl
);
3606 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3610 static int cmd_sb_port(struct dl
*dl
)
3612 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3615 } else if (dl_argv_match(dl
, "pool")) {
3617 return cmd_sb_port_pool(dl
);
3619 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3623 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3625 pr_out_port_handle_start_arr(dl
, tb
, true);
3626 check_indent_newline(dl
);
3627 print_uint(PRINT_ANY
, "sb", "sb %u",
3628 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3629 print_uint(PRINT_ANY
, "tc", " tc %u",
3630 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3631 print_string(PRINT_ANY
, "type", " type %s",
3632 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3633 print_uint(PRINT_ANY
, "pool", " pool %u",
3634 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3635 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3636 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3637 pr_out_port_handle_end(dl
);
3640 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3642 struct dl
*dl
= data
;
3643 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3644 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3646 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3647 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3648 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3649 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3650 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3651 return MNL_CB_ERROR
;
3652 pr_out_sb_tc_bind(dl
, tb
);
3656 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3658 struct nlmsghdr
*nlh
;
3659 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3662 if (dl_argc(dl
) == 0)
3663 flags
|= NLM_F_DUMP
;
3665 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3667 if (dl_argc(dl
) > 0) {
3668 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3669 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3674 pr_out_section_start(dl
, "tc_bind");
3675 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3676 pr_out_section_end(dl
);
3680 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3682 struct nlmsghdr
*nlh
;
3685 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3686 NLM_F_REQUEST
| NLM_F_ACK
);
3688 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3689 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3694 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3697 static int cmd_sb_tc_bind(struct dl
*dl
)
3699 if (dl_argv_match(dl
, "help")) {
3702 } else if (dl_argv_match(dl
, "show") ||
3703 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3705 return cmd_sb_tc_bind_show(dl
);
3706 } else if (dl_argv_match(dl
, "set")) {
3708 return cmd_sb_tc_bind_set(dl
);
3710 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3714 static int cmd_sb_tc(struct dl
*dl
)
3716 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3719 } else if (dl_argv_match(dl
, "bind")) {
3721 return cmd_sb_tc_bind(dl
);
3723 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3728 struct list_head list
;
3732 uint32_t bound_pool_index
;
3736 struct list_head list
;
3739 uint32_t port_index
;
3741 struct list_head pool_list
;
3742 struct list_head ing_tc_list
;
3743 struct list_head eg_tc_list
;
3749 struct list_head port_list
;
3752 static struct occ_item
*occ_item_alloc(void)
3754 return calloc(1, sizeof(struct occ_item
));
3757 static void occ_item_free(struct occ_item
*occ_item
)
3762 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3764 struct occ_port
*occ_port
;
3766 occ_port
= calloc(1, sizeof(*occ_port
));
3769 occ_port
->port_index
= port_index
;
3770 INIT_LIST_HEAD(&occ_port
->pool_list
);
3771 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3772 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3776 static void occ_port_free(struct occ_port
*occ_port
)
3778 struct occ_item
*occ_item
, *tmp
;
3780 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3781 occ_item_free(occ_item
);
3782 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3783 occ_item_free(occ_item
);
3784 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3785 occ_item_free(occ_item
);
3788 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3790 struct occ_show
*occ_show
;
3792 occ_show
= calloc(1, sizeof(*occ_show
));
3796 INIT_LIST_HEAD(&occ_show
->port_list
);
3800 static void occ_show_free(struct occ_show
*occ_show
)
3802 struct occ_port
*occ_port
, *tmp
;
3804 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3805 occ_port_free(occ_port
);
3808 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3811 struct occ_port
*occ_port
;
3812 uint32_t port_index
;
3814 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3816 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3817 if (occ_port
->port_index
== port_index
)
3820 occ_port
= occ_port_alloc(port_index
);
3823 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3827 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3830 struct occ_item
*occ_item
;
3833 pr_out_sp(7, " %s:", label
);
3834 list_for_each_entry(occ_item
, list
, list
) {
3835 if ((i
- 1) % 4 == 0 && i
!= 1)
3838 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3839 occ_item
->bound_pool_index
);
3841 pr_out_sp(7, "%2u:", occ_item
->index
);
3842 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3846 if ((i
- 1) % 4 != 0)
3850 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3851 struct list_head
*list
,
3854 struct occ_item
*occ_item
;
3857 open_json_object(label
);
3858 list_for_each_entry(occ_item
, list
, list
) {
3859 sprintf(buf
, "%u", occ_item
->index
);
3860 open_json_object(buf
);
3862 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3863 occ_item
->bound_pool_index
);
3864 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3865 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3866 close_json_object();
3868 close_json_object();
3871 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3873 if (dl
->json_output
) {
3874 pr_out_json_occ_show_item_list(dl
, "pool",
3875 &occ_port
->pool_list
, false);
3876 pr_out_json_occ_show_item_list(dl
, "itc",
3877 &occ_port
->ing_tc_list
, true);
3878 pr_out_json_occ_show_item_list(dl
, "etc",
3879 &occ_port
->eg_tc_list
, true);
3882 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3883 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3884 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3888 static void pr_out_occ_show(struct occ_show
*occ_show
)
3890 struct dl
*dl
= occ_show
->dl
;
3891 struct dl_opts
*opts
= &dl
->opts
;
3892 struct occ_port
*occ_port
;
3894 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3895 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3896 occ_port
->port_index
, true, false);
3897 pr_out_occ_show_port(dl
, occ_port
);
3898 pr_out_port_handle_end(dl
);
3902 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3905 struct occ_port
*occ_port
;
3906 struct occ_item
*occ_item
;
3908 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3911 occ_port
= occ_port_get(occ_show
, tb
);
3913 occ_show
->err
= -ENOMEM
;
3917 occ_item
= occ_item_alloc();
3919 occ_show
->err
= -ENOMEM
;
3922 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3923 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3924 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3925 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3928 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3930 struct occ_show
*occ_show
= data
;
3931 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3932 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3934 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3935 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3936 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3937 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3938 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3939 return MNL_CB_ERROR
;
3940 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3944 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3947 struct occ_port
*occ_port
;
3948 struct occ_item
*occ_item
;
3951 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3954 occ_port
= occ_port_get(occ_show
, tb
);
3956 occ_show
->err
= -ENOMEM
;
3960 occ_item
= occ_item_alloc();
3962 occ_show
->err
= -ENOMEM
;
3965 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3966 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3967 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3968 occ_item
->bound_pool_index
=
3969 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3970 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3971 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3972 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3973 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3974 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3976 occ_item_free(occ_item
);
3979 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3981 struct occ_show
*occ_show
= data
;
3982 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3983 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3985 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3986 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3987 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3988 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3989 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3990 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3991 return MNL_CB_ERROR
;
3992 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3996 static int cmd_sb_occ_show(struct dl
*dl
)
3998 struct nlmsghdr
*nlh
;
3999 struct occ_show
*occ_show
;
4000 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4003 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4007 occ_show
= occ_show_alloc(dl
);
4011 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4013 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4014 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4018 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4020 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4021 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4025 pr_out_section_start(dl
, "occupancy");
4026 pr_out_occ_show(occ_show
);
4027 pr_out_section_end(dl
);
4030 occ_show_free(occ_show
);
4034 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4036 struct nlmsghdr
*nlh
;
4039 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4040 NLM_F_REQUEST
| NLM_F_ACK
);
4042 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4046 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4049 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4051 struct nlmsghdr
*nlh
;
4054 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4055 NLM_F_REQUEST
| NLM_F_ACK
);
4057 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4061 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4064 static int cmd_sb_occ(struct dl
*dl
)
4066 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4069 } else if (dl_argv_match(dl
, "show") ||
4070 dl_argv_match(dl
, "list")) {
4072 return cmd_sb_occ_show(dl
);
4073 } else if (dl_argv_match(dl
, "snapshot")) {
4075 return cmd_sb_occ_snapshot(dl
);
4076 } else if (dl_argv_match(dl
, "clearmax")) {
4078 return cmd_sb_occ_clearmax(dl
);
4080 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4084 static int cmd_sb(struct dl
*dl
)
4086 if (dl_argv_match(dl
, "help")) {
4089 } else if (dl_argv_match(dl
, "show") ||
4090 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4092 return cmd_sb_show(dl
);
4093 } else if (dl_argv_match(dl
, "pool")) {
4095 return cmd_sb_pool(dl
);
4096 } else if (dl_argv_match(dl
, "port")) {
4098 return cmd_sb_port(dl
);
4099 } else if (dl_argv_match(dl
, "tc")) {
4101 return cmd_sb_tc(dl
);
4102 } else if (dl_argv_match(dl
, "occupancy")) {
4104 return cmd_sb_occ(dl
);
4106 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4110 static const char *cmd_name(uint8_t cmd
)
4113 case DEVLINK_CMD_UNSPEC
: return "unspec";
4114 case DEVLINK_CMD_GET
: return "get";
4115 case DEVLINK_CMD_SET
: return "set";
4116 case DEVLINK_CMD_NEW
: return "new";
4117 case DEVLINK_CMD_DEL
: return "del";
4118 case DEVLINK_CMD_PORT_GET
: return "get";
4119 case DEVLINK_CMD_PORT_SET
: return "set";
4120 case DEVLINK_CMD_PORT_NEW
: return "new";
4121 case DEVLINK_CMD_PORT_DEL
: return "del";
4122 case DEVLINK_CMD_PARAM_GET
: return "get";
4123 case DEVLINK_CMD_PARAM_SET
: return "set";
4124 case DEVLINK_CMD_PARAM_NEW
: return "new";
4125 case DEVLINK_CMD_PARAM_DEL
: return "del";
4126 case DEVLINK_CMD_REGION_GET
: return "get";
4127 case DEVLINK_CMD_REGION_SET
: return "set";
4128 case DEVLINK_CMD_REGION_NEW
: return "new";
4129 case DEVLINK_CMD_REGION_DEL
: return "del";
4130 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4131 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4132 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4133 case DEVLINK_CMD_TRAP_GET
: return "get";
4134 case DEVLINK_CMD_TRAP_SET
: return "set";
4135 case DEVLINK_CMD_TRAP_NEW
: return "new";
4136 case DEVLINK_CMD_TRAP_DEL
: return "del";
4137 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4138 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4139 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4140 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4141 default: return "<unknown cmd>";
4145 static const char *cmd_obj(uint8_t cmd
)
4148 case DEVLINK_CMD_UNSPEC
: return "unspec";
4149 case DEVLINK_CMD_GET
:
4150 case DEVLINK_CMD_SET
:
4151 case DEVLINK_CMD_NEW
:
4152 case DEVLINK_CMD_DEL
:
4154 case DEVLINK_CMD_PORT_GET
:
4155 case DEVLINK_CMD_PORT_SET
:
4156 case DEVLINK_CMD_PORT_NEW
:
4157 case DEVLINK_CMD_PORT_DEL
:
4159 case DEVLINK_CMD_PARAM_GET
:
4160 case DEVLINK_CMD_PARAM_SET
:
4161 case DEVLINK_CMD_PARAM_NEW
:
4162 case DEVLINK_CMD_PARAM_DEL
:
4164 case DEVLINK_CMD_REGION_GET
:
4165 case DEVLINK_CMD_REGION_SET
:
4166 case DEVLINK_CMD_REGION_NEW
:
4167 case DEVLINK_CMD_REGION_DEL
:
4169 case DEVLINK_CMD_FLASH_UPDATE
:
4170 case DEVLINK_CMD_FLASH_UPDATE_END
:
4171 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4173 case DEVLINK_CMD_TRAP_GET
:
4174 case DEVLINK_CMD_TRAP_SET
:
4175 case DEVLINK_CMD_TRAP_NEW
:
4176 case DEVLINK_CMD_TRAP_DEL
:
4178 case DEVLINK_CMD_TRAP_GROUP_GET
:
4179 case DEVLINK_CMD_TRAP_GROUP_SET
:
4180 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4181 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4182 return "trap-group";
4183 default: return "<unknown obj>";
4187 static void pr_out_mon_header(uint8_t cmd
)
4189 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4192 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4194 const char *obj
= cmd_obj(cmd
);
4195 unsigned int index
= 0;
4196 const char *cur_obj
;
4200 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4201 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4207 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4209 __pr_out_handle_start(dl
, tb
, true, false);
4211 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4212 check_indent_newline(dl
);
4213 print_string(PRINT_ANY
, "msg", "msg %s",
4214 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4216 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4217 check_indent_newline(dl
);
4218 print_string(PRINT_ANY
, "component", "component %s",
4219 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4222 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4223 pr_out_u64(dl
, "done",
4224 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4226 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4227 pr_out_u64(dl
, "total",
4228 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4230 pr_out_handle_end(dl
);
4233 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4234 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4235 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4237 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4239 struct dl
*dl
= data
;
4240 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4241 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4242 uint8_t cmd
= genl
->cmd
;
4244 if (!cmd_filter_check(dl
, cmd
))
4248 case DEVLINK_CMD_GET
: /* fall through */
4249 case DEVLINK_CMD_SET
: /* fall through */
4250 case DEVLINK_CMD_NEW
: /* fall through */
4251 case DEVLINK_CMD_DEL
:
4252 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4253 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4254 return MNL_CB_ERROR
;
4255 pr_out_mon_header(genl
->cmd
);
4256 pr_out_handle(dl
, tb
);
4258 case DEVLINK_CMD_PORT_GET
: /* fall through */
4259 case DEVLINK_CMD_PORT_SET
: /* fall through */
4260 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4261 case DEVLINK_CMD_PORT_DEL
:
4262 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4263 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4264 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4265 return MNL_CB_ERROR
;
4266 pr_out_mon_header(genl
->cmd
);
4267 pr_out_port(dl
, tb
);
4269 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4270 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4271 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4272 case DEVLINK_CMD_PARAM_DEL
:
4273 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4274 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4275 !tb
[DEVLINK_ATTR_PARAM
])
4276 return MNL_CB_ERROR
;
4277 pr_out_mon_header(genl
->cmd
);
4278 pr_out_param(dl
, tb
, false);
4280 case DEVLINK_CMD_REGION_GET
: /* fall through */
4281 case DEVLINK_CMD_REGION_SET
: /* fall through */
4282 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4283 case DEVLINK_CMD_REGION_DEL
:
4284 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4285 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4286 !tb
[DEVLINK_ATTR_REGION_NAME
])
4287 return MNL_CB_ERROR
;
4288 pr_out_mon_header(genl
->cmd
);
4289 pr_out_region(dl
, tb
);
4291 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4292 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4293 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4294 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4295 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4296 return MNL_CB_ERROR
;
4297 pr_out_mon_header(genl
->cmd
);
4298 pr_out_flash_update(dl
, tb
);
4300 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4301 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4302 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4303 case DEVLINK_CMD_TRAP_DEL
:
4304 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4305 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4306 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4307 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4308 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4309 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4310 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4311 !tb
[DEVLINK_ATTR_STATS
])
4312 return MNL_CB_ERROR
;
4313 pr_out_mon_header(genl
->cmd
);
4314 pr_out_trap(dl
, tb
, false);
4316 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4317 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4318 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4319 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4320 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4321 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4322 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4323 !tb
[DEVLINK_ATTR_STATS
])
4324 return MNL_CB_ERROR
;
4325 pr_out_mon_header(genl
->cmd
);
4326 pr_out_trap_group(dl
, tb
, false);
4332 static int cmd_mon_show(struct dl
*dl
)
4335 unsigned int index
= 0;
4336 const char *cur_obj
;
4338 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4339 if (strcmp(cur_obj
, "all") != 0 &&
4340 strcmp(cur_obj
, "dev") != 0 &&
4341 strcmp(cur_obj
, "port") != 0 &&
4342 strcmp(cur_obj
, "trap") != 0 &&
4343 strcmp(cur_obj
, "trap-group") != 0) {
4344 pr_err("Unknown object \"%s\"\n", cur_obj
);
4348 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4351 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4357 static void cmd_mon_help(void)
4359 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4360 "where OBJECT-LIST := { dev | port | trap | trap-group }\n");
4363 static int cmd_mon(struct dl
*dl
)
4365 if (dl_argv_match(dl
, "help")) {
4369 return cmd_mon_show(dl
);
4372 struct dpipe_field
{
4375 unsigned int bitwidth
;
4376 enum devlink_dpipe_field_mapping_type mapping_type
;
4379 struct dpipe_header
{
4380 struct list_head list
;
4383 struct dpipe_field
*fields
;
4384 unsigned int fields_count
;
4387 struct dpipe_table
{
4388 struct list_head list
;
4390 unsigned int resource_id
;
4391 bool resource_valid
;
4394 struct dpipe_tables
{
4395 struct list_head table_list
;
4405 enum devlink_resource_unit unit
;
4410 struct list_head list
;
4411 struct list_head resource_list
;
4412 struct resource
*parent
;
4416 struct list_head resource_list
;
4419 struct resource_ctx
{
4422 struct resources
*resources
;
4423 struct dpipe_tables
*tables
;
4424 bool print_resources
;
4425 bool pending_change
;
4428 static struct resource
*resource_alloc(void)
4430 struct resource
*resource
;
4432 resource
= calloc(1, sizeof(struct resource
));
4435 INIT_LIST_HEAD(&resource
->resource_list
);
4439 static void resource_free(struct resource
*resource
)
4441 struct resource
*child_resource
, *tmp
;
4443 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4445 free(child_resource
->name
);
4446 resource_free(child_resource
);
4451 static struct resources
*resources_alloc(void)
4453 struct resources
*resources
;
4455 resources
= calloc(1, sizeof(struct resources
));
4458 INIT_LIST_HEAD(&resources
->resource_list
);
4462 static void resources_free(struct resources
*resources
)
4464 struct resource
*resource
, *tmp
;
4466 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4467 resource_free(resource
);
4470 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4472 ctx
->resources
= resources_alloc();
4473 if (!ctx
->resources
)
4479 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4481 resources_free(ctx
->resources
);
4487 struct list_head global_headers
;
4488 struct list_head local_headers
;
4489 struct dpipe_tables
*tables
;
4490 struct resources
*resources
;
4495 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4497 struct dpipe_header
*header
;
4499 header
= calloc(1, sizeof(struct dpipe_header
));
4502 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4503 if (!header
->fields
)
4504 goto err_fields_alloc
;
4505 header
->fields_count
= fields_count
;
4513 static void dpipe_header_free(struct dpipe_header
*header
)
4515 free(header
->fields
);
4519 static void dpipe_header_clear(struct dpipe_header
*header
)
4521 struct dpipe_field
*field
;
4524 for (i
= 0; i
< header
->fields_count
; i
++) {
4525 field
= &header
->fields
[i
];
4531 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4532 struct dpipe_header
*header
, bool global
)
4535 list_add(&header
->list
, &ctx
->global_headers
);
4537 list_add(&header
->list
, &ctx
->local_headers
);
4540 static void dpipe_header_del(struct dpipe_header
*header
)
4542 list_del(&header
->list
);
4545 static struct dpipe_table
*dpipe_table_alloc(void)
4547 return calloc(1, sizeof(struct dpipe_table
));
4550 static void dpipe_table_free(struct dpipe_table
*table
)
4555 static struct dpipe_tables
*dpipe_tables_alloc(void)
4557 struct dpipe_tables
*tables
;
4559 tables
= calloc(1, sizeof(struct dpipe_tables
));
4562 INIT_LIST_HEAD(&tables
->table_list
);
4566 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4568 struct dpipe_table
*table
, *tmp
;
4570 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4571 dpipe_table_free(table
);
4575 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4577 ctx
->tables
= dpipe_tables_alloc();
4582 INIT_LIST_HEAD(&ctx
->global_headers
);
4583 INIT_LIST_HEAD(&ctx
->local_headers
);
4587 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4589 struct dpipe_header
*header
, *tmp
;
4591 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4593 dpipe_header_del(header
);
4594 dpipe_header_clear(header
);
4595 dpipe_header_free(header
);
4597 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4599 dpipe_header_del(header
);
4600 dpipe_header_clear(header
);
4601 dpipe_header_free(header
);
4603 dpipe_tables_free(ctx
->tables
);
4606 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4607 uint32_t header_id
, bool global
)
4609 struct list_head
*header_list
;
4610 struct dpipe_header
*header
;
4613 header_list
= &ctx
->global_headers
;
4615 header_list
= &ctx
->local_headers
;
4616 list_for_each_entry(header
, header_list
, list
) {
4617 if (header
->id
!= header_id
)
4619 return header
->name
;
4624 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4626 uint32_t field_id
, bool global
)
4628 struct list_head
*header_list
;
4629 struct dpipe_header
*header
;
4632 header_list
= &ctx
->global_headers
;
4634 header_list
= &ctx
->local_headers
;
4635 list_for_each_entry(header
, header_list
, list
) {
4636 if (header
->id
!= header_id
)
4638 return header
->fields
[field_id
].name
;
4644 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4646 switch (mapping_type
) {
4647 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4649 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4657 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4658 uint32_t field_id
, bool global
)
4660 enum devlink_dpipe_field_mapping_type mapping_type
;
4661 struct list_head
*header_list
;
4662 struct dpipe_header
*header
;
4665 header_list
= &ctx
->global_headers
;
4667 header_list
= &ctx
->local_headers
;
4668 list_for_each_entry(header
, header_list
, list
) {
4669 if (header
->id
!= header_id
)
4671 mapping_type
= header
->fields
[field_id
].mapping_type
;
4672 return dpipe_field_mapping_e2s(mapping_type
);
4677 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4678 struct dpipe_field
*fields
,
4679 unsigned int field_count
)
4681 struct dpipe_field
*field
;
4684 for (i
= 0; i
< field_count
; i
++) {
4686 pr_out_entry_start(ctx
->dl
);
4687 check_indent_newline(ctx
->dl
);
4688 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4689 if (ctx
->dl
->verbose
)
4690 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4691 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4692 if (field
->mapping_type
) {
4693 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4694 dpipe_field_mapping_e2s(field
->mapping_type
));
4696 pr_out_entry_end(ctx
->dl
);
4701 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4702 struct dpipe_header
*header
, bool global
)
4704 pr_out_handle_start_arr(ctx
->dl
, tb
);
4705 check_indent_newline(ctx
->dl
);
4706 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4707 if (ctx
->dl
->verbose
) {
4708 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4709 print_bool(PRINT_ANY
, "global", " global %s", global
);
4711 pr_out_array_start(ctx
->dl
, "field");
4712 pr_out_dpipe_fields(ctx
, header
->fields
,
4713 header
->fields_count
);
4714 pr_out_array_end(ctx
->dl
);
4715 pr_out_handle_end(ctx
->dl
);
4718 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4721 struct dpipe_header
*header
;
4723 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4724 pr_out_dpipe_header(ctx
, tb
, header
, false);
4726 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4727 pr_out_dpipe_header(ctx
, tb
, header
, true);
4730 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4732 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4736 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4737 if (err
!= MNL_CB_OK
)
4739 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4740 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4741 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4742 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4745 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4746 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4747 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4748 field
->name
= strdup(name
);
4751 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4755 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4756 struct dpipe_field
*fields
)
4758 struct nlattr
*nla_field
;
4762 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4763 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4771 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4773 struct nlattr
*nla_field
;
4774 unsigned int count
= 0;
4776 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4781 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4783 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4784 struct dpipe_header
*header
;
4785 unsigned int fields_count
;
4786 const char *header_name
;
4790 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4791 if (err
!= MNL_CB_OK
)
4794 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4795 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4796 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4799 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4800 header
= dpipe_header_alloc(fields_count
);
4804 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4805 header
->name
= strdup(header_name
);
4806 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4807 header
->fields_count
= fields_count
;
4808 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4810 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4814 dpipe_header_add(ctx
, header
, global
);
4818 dpipe_header_free(header
);
4822 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4824 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4825 struct nlattr
*nla_header
;
4828 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4829 err
= dpipe_header_get(ctx
, nla_header
);
4836 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4838 struct dpipe_ctx
*ctx
= data
;
4839 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4840 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4843 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4844 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4845 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4846 return MNL_CB_ERROR
;
4847 err
= dpipe_headers_get(ctx
, tb
);
4850 return MNL_CB_ERROR
;
4853 if (ctx
->print_headers
)
4854 pr_out_dpipe_headers(ctx
, tb
);
4858 static int cmd_dpipe_headers_show(struct dl
*dl
)
4860 struct nlmsghdr
*nlh
;
4861 struct dpipe_ctx ctx
= {};
4862 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4865 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4867 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4871 err
= dpipe_ctx_init(&ctx
, dl
);
4875 ctx
.print_headers
= true;
4877 pr_out_section_start(dl
, "header");
4878 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4880 pr_err("error get headers %s\n", strerror(ctx
.err
));
4881 pr_out_section_end(dl
);
4883 dpipe_ctx_fini(&ctx
);
4887 static void cmd_dpipe_header_help(void)
4889 pr_err("Usage: devlink dpipe headers show DEV\n");
4892 static int cmd_dpipe_header(struct dl
*dl
)
4894 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4895 cmd_dpipe_header_help();
4897 } else if (dl_argv_match(dl
, "show")) {
4899 return cmd_dpipe_headers_show(dl
);
4901 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4906 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4908 switch (action_type
) {
4909 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4910 return "field_modify";
4916 struct dpipe_op_info
{
4922 struct dpipe_action
{
4923 struct dpipe_op_info info
;
4927 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4928 struct dpipe_ctx
*ctx
)
4930 struct dpipe_op_info
*op_info
= &action
->info
;
4931 const char *mapping
;
4933 check_indent_newline(ctx
->dl
);
4934 print_string(PRINT_ANY
, "type", "type %s",
4935 dpipe_action_type_e2s(action
->type
));
4936 print_string(PRINT_ANY
, "header", " header %s",
4937 dpipe_header_id2s(ctx
, op_info
->header_id
,
4938 op_info
->header_global
));
4939 print_string(PRINT_ANY
, "field", " field %s",
4940 dpipe_field_id2s(ctx
, op_info
->header_id
,
4942 op_info
->header_global
));
4943 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4945 op_info
->header_global
);
4947 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
4950 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4952 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4955 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4956 if (err
!= MNL_CB_OK
)
4959 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4960 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4961 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4962 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4966 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4967 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4968 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4969 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4974 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4975 struct nlattr
*nla_actions
)
4977 struct nlattr
*nla_action
;
4978 struct dpipe_action action
;
4980 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4981 pr_out_entry_start(ctx
->dl
);
4982 if (dpipe_action_parse(&action
, nla_action
))
4983 goto err_action_parse
;
4984 pr_out_dpipe_action(&action
, ctx
);
4985 pr_out_entry_end(ctx
->dl
);
4990 pr_out_entry_end(ctx
->dl
);
4995 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4997 switch (match_type
) {
4998 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4999 return "field_exact";
5005 struct dpipe_match
{
5006 struct dpipe_op_info info
;
5010 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5011 struct dpipe_ctx
*ctx
)
5013 struct dpipe_op_info
*op_info
= &match
->info
;
5014 const char *mapping
;
5016 check_indent_newline(ctx
->dl
);
5017 print_string(PRINT_ANY
, "type", "type %s",
5018 dpipe_match_type_e2s(match
->type
));
5019 print_string(PRINT_ANY
, "header", " header %s",
5020 dpipe_header_id2s(ctx
, op_info
->header_id
,
5021 op_info
->header_global
));
5022 print_string(PRINT_ANY
, "field", " field %s",
5023 dpipe_field_id2s(ctx
, op_info
->header_id
,
5025 op_info
->header_global
));
5026 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5028 op_info
->header_global
);
5030 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5033 static int dpipe_match_parse(struct dpipe_match
*match
,
5037 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5040 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5041 if (err
!= MNL_CB_OK
)
5044 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5045 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5046 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5047 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5051 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5052 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5053 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5054 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5059 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5060 struct nlattr
*nla_matches
)
5062 struct nlattr
*nla_match
;
5063 struct dpipe_match match
;
5065 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5066 pr_out_entry_start(ctx
->dl
);
5067 if (dpipe_match_parse(&match
, nla_match
))
5068 goto err_match_parse
;
5069 pr_out_dpipe_match(&match
, ctx
);
5070 pr_out_entry_end(ctx
->dl
);
5075 pr_out_entry_end(ctx
->dl
);
5079 static struct resource
*
5080 resource_find(struct resources
*resources
, struct resource
*resource
,
5081 uint64_t resource_id
)
5083 struct list_head
*list_head
;
5086 list_head
= &resources
->resource_list
;
5088 list_head
= &resource
->resource_list
;
5090 list_for_each_entry(resource
, list_head
, list
) {
5091 struct resource
*child_resource
;
5093 if (resource
->id
== resource_id
)
5096 child_resource
= resource_find(resources
, resource
,
5099 return child_resource
;
5105 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5106 uint64_t resource_id
)
5108 struct resource
*resource
, *parent_resource
;
5109 const char del
[] = "/";
5113 resource
= resource_find(resources
, NULL
, resource_id
);
5117 for (parent_resource
= resource
; parent_resource
;
5118 parent_resource
= parent_resource
->parent
)
5119 path_len
+= strlen(parent_resource
->name
) + 1;
5122 path
= calloc(1, path_len
);
5126 path
+= path_len
- 1;
5127 for (parent_resource
= resource
; parent_resource
;
5128 parent_resource
= parent_resource
->parent
) {
5129 path
-= strlen(parent_resource
->name
);
5130 memcpy(path
, parent_resource
->name
,
5131 strlen(parent_resource
->name
));
5132 path
-= strlen(del
);
5133 memcpy(path
, del
, strlen(del
));
5135 check_indent_newline(dl
);
5136 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5140 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5142 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5143 struct dpipe_table
*table
;
5144 uint32_t resource_units
;
5145 bool counters_enabled
;
5146 bool resource_valid
;
5150 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5151 if (err
!= MNL_CB_OK
)
5154 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5155 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5156 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5157 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5158 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5162 table
= dpipe_table_alloc();
5166 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5167 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5168 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5170 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5172 if (resource_valid
) {
5173 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5174 table
->resource_valid
= true;
5177 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5178 if (!ctx
->print_tables
)
5181 check_indent_newline(ctx
->dl
);
5182 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5183 print_uint(PRINT_ANY
, "size", " size %u", size
);
5184 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5186 if (resource_valid
) {
5187 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5188 resource_path_print(ctx
->dl
, ctx
->resources
,
5189 table
->resource_id
);
5190 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5194 pr_out_array_start(ctx
->dl
, "match");
5195 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5196 goto err_matches_show
;
5197 pr_out_array_end(ctx
->dl
);
5199 pr_out_array_start(ctx
->dl
, "action");
5200 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5201 goto err_actions_show
;
5202 pr_out_array_end(ctx
->dl
);
5208 pr_out_array_end(ctx
->dl
);
5212 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5214 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5215 struct nlattr
*nla_table
;
5217 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5218 if (ctx
->print_tables
)
5219 pr_out_handle_start_arr(ctx
->dl
, tb
);
5220 if (dpipe_table_show(ctx
, nla_table
))
5221 goto err_table_show
;
5222 if (ctx
->print_tables
)
5223 pr_out_handle_end(ctx
->dl
);
5228 if (ctx
->print_tables
)
5229 pr_out_handle_end(ctx
->dl
);
5233 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5235 struct dpipe_ctx
*ctx
= data
;
5236 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5237 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5239 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5240 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5241 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5242 return MNL_CB_ERROR
;
5244 if (dpipe_tables_show(ctx
, tb
))
5245 return MNL_CB_ERROR
;
5249 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5251 static int cmd_dpipe_table_show(struct dl
*dl
)
5253 struct nlmsghdr
*nlh
;
5254 struct dpipe_ctx dpipe_ctx
= {};
5255 struct resource_ctx resource_ctx
= {};
5256 uint16_t flags
= NLM_F_REQUEST
;
5259 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5263 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5265 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5269 dpipe_ctx
.print_tables
= true;
5271 dl_opts_put(nlh
, dl
);
5272 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5275 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5276 goto err_headers_get
;
5279 err
= resource_ctx_init(&resource_ctx
, dl
);
5281 goto err_resource_ctx_init
;
5283 resource_ctx
.print_resources
= false;
5284 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5285 dl_opts_put(nlh
, dl
);
5286 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5289 dpipe_ctx
.resources
= resource_ctx
.resources
;
5291 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5292 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5293 dl_opts_put(nlh
, dl
);
5295 pr_out_section_start(dl
, "table");
5296 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5297 pr_out_section_end(dl
);
5299 resource_ctx_fini(&resource_ctx
);
5300 dpipe_ctx_fini(&dpipe_ctx
);
5303 err_resource_ctx_init
:
5305 dpipe_ctx_fini(&dpipe_ctx
);
5309 static int cmd_dpipe_table_set(struct dl
*dl
)
5311 struct nlmsghdr
*nlh
;
5314 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5315 NLM_F_REQUEST
| NLM_F_ACK
);
5317 err
= dl_argv_parse_put(nlh
, dl
,
5318 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5319 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5323 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5326 enum dpipe_value_type
{
5327 DPIPE_VALUE_TYPE_VALUE
,
5328 DPIPE_VALUE_TYPE_MASK
,
5332 dpipe_value_type_e2s(enum dpipe_value_type type
)
5335 case DPIPE_VALUE_TYPE_VALUE
:
5337 case DPIPE_VALUE_TYPE_MASK
:
5338 return "value_mask";
5344 struct dpipe_field_printer
{
5345 unsigned int field_id
;
5346 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5349 struct dpipe_header_printer
{
5350 struct dpipe_field_printer
*printers
;
5351 unsigned int printers_count
;
5352 unsigned int header_id
;
5355 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5356 enum dpipe_value_type type
,
5359 struct in_addr ip_addr
;
5361 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5362 check_indent_newline(ctx
->dl
);
5363 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5367 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5368 enum dpipe_value_type type
,
5371 check_indent_newline(ctx
->dl
);
5372 print_string_name_value(dpipe_value_type_e2s(type
),
5373 ether_ntoa((struct ether_addr
*)value
));
5376 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5377 enum dpipe_value_type type
,
5380 char str
[INET6_ADDRSTRLEN
];
5382 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5383 check_indent_newline(ctx
->dl
);
5384 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5387 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5389 .printer
= dpipe_field_printer_ipv4_addr
,
5390 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5394 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5395 .printers
= dpipe_field_printers_ipv4
,
5396 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5397 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5400 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5402 .printer
= dpipe_field_printer_ethernet_addr
,
5403 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5407 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5408 .printers
= dpipe_field_printers_ethernet
,
5409 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5410 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5413 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5415 .printer
= dpipe_field_printer_ipv6_addr
,
5416 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5420 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5421 .printers
= dpipe_field_printers_ipv6
,
5422 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5423 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5426 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5427 &dpipe_header_printer_ipv4
,
5428 &dpipe_header_printer_ethernet
,
5429 &dpipe_header_printer_ipv6
,
5432 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5433 struct dpipe_op_info
*info
,
5434 enum dpipe_value_type type
,
5437 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5438 struct dpipe_header_printer
*header_printer
;
5439 struct dpipe_field_printer
*field_printer
;
5440 unsigned int field_printers_count
;
5444 for (i
= 0; i
< header_printers_count
; i
++) {
5445 header_printer
= dpipe_header_printers
[i
];
5446 if (header_printer
->header_id
!= info
->header_id
)
5448 field_printers_count
= header_printer
->printers_count
;
5449 for (j
= 0; j
< field_printers_count
; j
++) {
5450 field_printer
= &header_printer
->printers
[j
];
5451 if (field_printer
->field_id
!= info
->field_id
)
5453 field_printer
->printer(ctx
, type
, value
);
5461 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5463 unsigned int value_len
,
5464 struct dpipe_op_info
*info
,
5465 enum dpipe_value_type type
)
5467 if (info
->header_global
&&
5468 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5471 if (value_len
== sizeof(uint32_t)) {
5472 uint32_t *value_32
= value
;
5474 check_indent_newline(ctx
->dl
);
5475 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5479 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5480 struct nlattr
**nla_match_value
,
5481 struct dpipe_op_info
*info
)
5483 void *value
, *value_mask
;
5484 uint32_t value_mapping
;
5488 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5489 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5491 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5492 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5495 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5496 check_indent_newline(ctx
->dl
);
5497 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5501 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5502 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5503 DPIPE_VALUE_TYPE_MASK
);
5506 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5509 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5512 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5513 struct dpipe_match match
;
5516 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5517 if (err
!= MNL_CB_OK
)
5520 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5521 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5525 pr_out_entry_start(ctx
->dl
);
5526 if (dpipe_match_parse(&match
,
5527 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5528 goto err_match_parse
;
5529 pr_out_dpipe_match(&match
, ctx
);
5530 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5531 pr_out_entry_end(ctx
->dl
);
5536 pr_out_entry_end(ctx
->dl
);
5540 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5543 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5544 struct dpipe_action action
;
5547 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5548 if (err
!= MNL_CB_OK
)
5551 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5552 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5556 pr_out_entry_start(ctx
->dl
);
5557 if (dpipe_action_parse(&action
,
5558 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5559 goto err_action_parse
;
5560 pr_out_dpipe_action(&action
, ctx
);
5561 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5562 pr_out_entry_end(ctx
->dl
);
5567 pr_out_entry_end(ctx
->dl
);
5572 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5573 struct nlattr
*nla_action_values
)
5575 struct nlattr
*nla_action_value
;
5577 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5578 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5585 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5586 struct nlattr
*nla_match_values
)
5588 struct nlattr
*nla_match_value
;
5590 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5591 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5597 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5599 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5600 uint32_t entry_index
;
5604 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5605 if (err
!= MNL_CB_OK
)
5608 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5609 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5610 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5614 check_indent_newline(ctx
->dl
);
5615 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5616 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5618 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5619 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5620 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5623 pr_out_array_start(ctx
->dl
, "match_value");
5624 if (dpipe_tables_match_values_show(ctx
,
5625 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5626 goto err_match_values_show
;
5627 pr_out_array_end(ctx
->dl
);
5629 pr_out_array_start(ctx
->dl
, "action_value");
5630 if (dpipe_tables_action_values_show(ctx
,
5631 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5632 goto err_action_values_show
;
5633 pr_out_array_end(ctx
->dl
);
5636 err_action_values_show
:
5637 err_match_values_show
:
5638 pr_out_array_end(ctx
->dl
);
5642 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5644 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5645 struct nlattr
*nla_entry
;
5647 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5648 pr_out_handle_start_arr(ctx
->dl
, tb
);
5649 if (dpipe_entry_show(ctx
, nla_entry
))
5650 goto err_entry_show
;
5651 pr_out_handle_end(ctx
->dl
);
5656 pr_out_handle_end(ctx
->dl
);
5660 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5662 struct dpipe_ctx
*ctx
= data
;
5663 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5664 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5666 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5667 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5668 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5669 return MNL_CB_ERROR
;
5671 if (dpipe_table_entries_show(ctx
, tb
))
5672 return MNL_CB_ERROR
;
5676 static int cmd_dpipe_table_dump(struct dl
*dl
)
5678 struct nlmsghdr
*nlh
;
5679 struct dpipe_ctx ctx
= {};
5680 uint16_t flags
= NLM_F_REQUEST
;
5683 err
= dpipe_ctx_init(&ctx
, dl
);
5687 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5691 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5692 dl_opts_put(nlh
, dl
);
5693 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5695 pr_err("error get headers %s\n", strerror(ctx
.err
));
5699 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5700 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5701 dl_opts_put(nlh
, dl
);
5703 pr_out_section_start(dl
, "table_entry");
5704 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5705 pr_out_section_end(dl
);
5707 dpipe_ctx_fini(&ctx
);
5711 static void cmd_dpipe_table_help(void)
5713 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5714 "where OBJECT-LIST := { show | set | dump }\n");
5717 static int cmd_dpipe_table(struct dl
*dl
)
5719 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5720 cmd_dpipe_table_help();
5722 } else if (dl_argv_match(dl
, "show")) {
5724 return cmd_dpipe_table_show(dl
);
5725 } else if (dl_argv_match(dl
, "set")) {
5727 return cmd_dpipe_table_set(dl
);
5728 } else if (dl_argv_match(dl
, "dump")) {
5730 return cmd_dpipe_table_dump(dl
);
5732 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5736 static void cmd_dpipe_help(void)
5738 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5739 "where OBJECT-LIST := { header | table }\n");
5742 static int cmd_dpipe(struct dl
*dl
)
5744 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5747 } else if (dl_argv_match(dl
, "header")) {
5749 return cmd_dpipe_header(dl
);
5750 } else if (dl_argv_match(dl
, "table")) {
5752 return cmd_dpipe_table(dl
);
5754 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5759 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5760 struct nlattr
**nla_resource
)
5762 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5763 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5764 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5765 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5766 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5767 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5768 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5772 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5773 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5774 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5775 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5776 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5777 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5778 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5780 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5781 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5783 resource
->size_new
= resource
->size
;
5785 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5786 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5787 resource
->occ_valid
= true;
5790 if (resource
->size_new
!= resource
->size
)
5791 ctx
->pending_change
= true;
5797 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5798 struct resource
*parent_resource
, struct nlattr
*nl
)
5800 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5801 struct nlattr
*nla_child_resource
;
5802 struct nlattr
*nla_resources
;
5812 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5813 if (err
!= MNL_CB_OK
)
5816 err
= resource_parse(ctx
, resource
, nla_resource
);
5820 resource
->parent
= parent_resource
;
5821 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5824 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5825 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5827 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5828 struct resource
*child_resource
;
5829 struct list_head
*list
;
5831 child_resource
= resource_alloc();
5832 if (!child_resource
)
5836 list
= &ctx
->resources
->resource_list
;
5838 list
= &resource
->resource_list
;
5840 list_add_tail(&child_resource
->list
, list
);
5841 err
= resource_get(ctx
, child_resource
, resource
,
5842 nla_child_resource
);
5850 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5853 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5854 default: return "<unknown unit>";
5858 static void resource_show(struct resource
*resource
,
5859 struct resource_ctx
*ctx
)
5861 struct resource
*child_resource
;
5862 struct dpipe_table
*table
;
5863 struct dl
*dl
= ctx
->dl
;
5866 check_indent_newline(dl
);
5867 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
5869 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5870 pr_out_u64(dl
, "size", resource
->size
);
5871 if (resource
->size
!= resource
->size_new
)
5872 pr_out_u64(dl
, "size_new", resource
->size_new
);
5873 if (resource
->occ_valid
)
5874 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
5875 print_string(PRINT_ANY
, "unit", " unit %s",
5876 resource_unit_str_get(resource
->unit
));
5878 if (resource
->size_min
!= resource
->size_max
) {
5879 print_uint(PRINT_ANY
, "size_min", " size_min %u",
5880 resource
->size_min
);
5881 pr_out_u64(dl
, "size_max", resource
->size_max
);
5882 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
5883 resource
->size_gran
);
5886 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5887 if (table
->resource_id
== resource
->id
&&
5888 table
->resource_valid
)
5892 pr_out_array_start(dl
, "dpipe_tables");
5894 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
5897 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5898 if (table
->resource_id
!= resource
->id
||
5899 !table
->resource_valid
)
5901 pr_out_entry_start(dl
);
5902 check_indent_newline(dl
);
5903 print_string(PRINT_ANY
, "table_name", "table_name %s",
5905 pr_out_entry_end(dl
);
5908 pr_out_array_end(dl
);
5910 if (list_empty(&resource
->resource_list
))
5913 if (ctx
->pending_change
) {
5914 check_indent_newline(dl
);
5915 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
5916 resource
->size_valid
? "true" : "false");
5918 pr_out_array_start(dl
, "resources");
5919 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5920 pr_out_entry_start(dl
);
5921 resource_show(child_resource
, ctx
);
5922 pr_out_entry_end(dl
);
5924 pr_out_array_end(dl
);
5928 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5930 struct resources
*resources
= ctx
->resources
;
5931 struct resource
*resource
;
5933 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5934 pr_out_handle_start_arr(ctx
->dl
, tb
);
5935 resource_show(resource
, ctx
);
5936 pr_out_handle_end(ctx
->dl
);
5940 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5942 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5945 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5947 struct resource_ctx
*ctx
= data
;
5948 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5949 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5952 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5953 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5954 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5955 return MNL_CB_ERROR
;
5957 err
= resources_get(ctx
, tb
);
5960 return MNL_CB_ERROR
;
5963 if (ctx
->print_resources
)
5964 resources_show(ctx
, tb
);
5969 static int cmd_resource_show(struct dl
*dl
)
5971 struct nlmsghdr
*nlh
;
5972 struct dpipe_ctx dpipe_ctx
= {};
5973 struct resource_ctx resource_ctx
= {};
5976 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5980 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5982 dl_opts_put(nlh
, dl
);
5984 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5988 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5991 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5995 err
= resource_ctx_init(&resource_ctx
, dl
);
5999 resource_ctx
.print_resources
= true;
6000 resource_ctx
.tables
= dpipe_ctx
.tables
;
6001 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6002 NLM_F_REQUEST
| NLM_F_ACK
);
6003 dl_opts_put(nlh
, dl
);
6004 pr_out_section_start(dl
, "resources");
6005 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6007 pr_out_section_end(dl
);
6008 resource_ctx_fini(&resource_ctx
);
6010 dpipe_ctx_fini(&dpipe_ctx
);
6014 static void cmd_resource_help(void)
6016 pr_err("Usage: devlink resource show DEV\n"
6017 " devlink resource set DEV path PATH size SIZE\n");
6020 static struct resource
*
6021 resource_find_by_name(struct list_head
*list
, char *name
)
6023 struct resource
*resource
;
6025 list_for_each_entry(resource
, list
, list
) {
6026 if (!strcmp(resource
->name
, name
))
6033 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6034 uint32_t *p_resource_id
, bool *p_resource_valid
)
6036 struct resource
*resource
;
6037 uint32_t resource_id
= 0;
6038 char *resource_path_dup
;
6039 struct list_head
*list
;
6040 const char del
[] = "/";
6041 char *resource_name
;
6043 resource_path_dup
= strdup(resource_path
);
6044 list
= &ctx
->resources
->resource_list
;
6045 resource_name
= strtok(resource_path_dup
, del
);
6046 while (resource_name
!= NULL
) {
6047 resource
= resource_find_by_name(list
, resource_name
);
6049 goto err_resource_lookup
;
6051 list
= &resource
->resource_list
;
6052 resource_name
= strtok(NULL
, del
);
6053 resource_id
= resource
->id
;
6055 free(resource_path_dup
);
6056 *p_resource_valid
= true;
6057 *p_resource_id
= resource_id
;
6060 err_resource_lookup
:
6061 free(resource_path_dup
);
6065 static int cmd_resource_set(struct dl
*dl
)
6067 struct nlmsghdr
*nlh
;
6068 struct resource_ctx ctx
= {};
6071 err
= resource_ctx_init(&ctx
, dl
);
6075 ctx
.print_resources
= false;
6076 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6077 DL_OPT_RESOURCE_SIZE
, 0);
6081 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6083 dl_opts_put(nlh
, dl
);
6084 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6086 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6090 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6091 &dl
->opts
.resource_id
,
6092 &dl
->opts
.resource_id_valid
);
6094 pr_err("error parsing resource path %s\n", strerror(-err
));
6098 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6099 NLM_F_REQUEST
| NLM_F_ACK
);
6101 dl_opts_put(nlh
, dl
);
6102 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6104 resource_ctx_fini(&ctx
);
6108 static int cmd_resource(struct dl
*dl
)
6110 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6111 cmd_resource_help();
6113 } else if (dl_argv_match(dl
, "show")) {
6115 return cmd_resource_show(dl
);
6116 } else if (dl_argv_match(dl
, "set")) {
6118 return cmd_resource_set(dl
);
6120 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6124 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6126 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6127 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6128 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6131 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6132 if (dl
->json_output
)
6133 open_json_object(buf
);
6138 static void pr_out_region_handle_end(struct dl
*dl
)
6140 if (dl
->json_output
)
6141 close_json_object();
6146 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6148 __pr_out_indent_newline(dl
);
6149 if (dl
->json_output
)
6150 open_json_array(PRINT_JSON
, "snapshot");
6152 pr_out("snapshot %s", array
? "[" : "");
6155 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6157 if (dl
->json_output
)
6158 close_json_array(PRINT_JSON
, NULL
);
6163 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6165 uint32_t snapshot_id
;
6167 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6170 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6172 if (dl
->json_output
)
6173 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6175 pr_out("%s%u", index
? " " : "", snapshot_id
);
6178 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6180 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6181 struct nlattr
*nla_sanpshot
;
6184 pr_out_region_snapshots_start(dl
, true);
6185 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6186 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6187 if (err
!= MNL_CB_OK
)
6189 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6191 pr_out_region_snapshots_end(dl
, true);
6194 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6196 pr_out_region_snapshots_start(dl
, false);
6197 pr_out_region_snapshots_id(dl
, tb
, 0);
6198 pr_out_region_snapshots_end(dl
, false);
6201 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6203 pr_out_region_handle_start(dl
, tb
);
6205 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6206 pr_out_u64(dl
, "size",
6207 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6209 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6210 pr_out_snapshots(dl
, tb
);
6212 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6213 pr_out_snapshot(dl
, tb
);
6215 pr_out_region_handle_end(dl
);
6218 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6220 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6221 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6222 struct dl
*dl
= data
;
6224 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6225 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6226 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6227 return MNL_CB_ERROR
;
6229 pr_out_region(dl
, tb
);
6234 static int cmd_region_show(struct dl
*dl
)
6236 struct nlmsghdr
*nlh
;
6237 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6240 if (dl_argc(dl
) == 0)
6241 flags
|= NLM_F_DUMP
;
6243 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6245 if (dl_argc(dl
) > 0) {
6246 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6251 pr_out_section_start(dl
, "regions");
6252 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6253 pr_out_section_end(dl
);
6257 static int cmd_region_snapshot_del(struct dl
*dl
)
6259 struct nlmsghdr
*nlh
;
6262 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6263 NLM_F_REQUEST
| NLM_F_ACK
);
6265 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6266 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6270 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6273 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6275 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6276 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6277 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6278 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6279 struct dl
*dl
= data
;
6282 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6283 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6284 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6285 return MNL_CB_ERROR
;
6287 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6288 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6289 if (err
!= MNL_CB_OK
)
6290 return MNL_CB_ERROR
;
6292 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6293 if (!nla_chunk_data
)
6296 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6297 if (!nla_chunk_addr
)
6300 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6301 mnl_attr_get_payload_len(nla_chunk_data
),
6302 mnl_attr_get_u64(nla_chunk_addr
));
6307 static int cmd_region_dump(struct dl
*dl
)
6309 struct nlmsghdr
*nlh
;
6312 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6313 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6315 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6316 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6320 pr_out_section_start(dl
, "dump");
6321 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6322 pr_out_section_end(dl
);
6323 if (!dl
->json_output
)
6328 static int cmd_region_read(struct dl
*dl
)
6330 struct nlmsghdr
*nlh
;
6333 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6334 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6336 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6337 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6338 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6342 pr_out_section_start(dl
, "read");
6343 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6344 pr_out_section_end(dl
);
6345 if (!dl
->json_output
)
6350 static void cmd_region_help(void)
6352 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6353 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6354 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6355 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6358 static int cmd_region(struct dl
*dl
)
6360 if (dl_no_arg(dl
)) {
6361 return cmd_region_show(dl
);
6362 } else if (dl_argv_match(dl
, "help")) {
6365 } else if (dl_argv_match(dl
, "show")) {
6367 return cmd_region_show(dl
);
6368 } else if (dl_argv_match(dl
, "del")) {
6370 return cmd_region_snapshot_del(dl
);
6371 } else if (dl_argv_match(dl
, "dump")) {
6373 return cmd_region_dump(dl
);
6374 } else if (dl_argv_match(dl
, "read")) {
6376 return cmd_region_read(dl
);
6378 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6382 static int cmd_health_set_params(struct dl
*dl
)
6384 struct nlmsghdr
*nlh
;
6387 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6388 NLM_F_REQUEST
| NLM_F_ACK
);
6389 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6390 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6391 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6395 dl_opts_put(nlh
, dl
);
6396 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6399 static int cmd_health_dump_clear(struct dl
*dl
)
6401 struct nlmsghdr
*nlh
;
6404 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6405 NLM_F_REQUEST
| NLM_F_ACK
);
6407 err
= dl_argv_parse_put(nlh
, dl
,
6408 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6412 dl_opts_put(nlh
, dl
);
6413 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6416 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6421 check_indent_newline(dl
);
6424 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6427 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6430 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6433 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6436 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6438 case MNL_TYPE_NUL_STRING
:
6439 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6441 case MNL_TYPE_BINARY
:
6442 len
= mnl_attr_get_payload_len(nl_data
);
6443 data
= mnl_attr_get_payload(nl_data
);
6444 pr_out_binary_value(dl
, data
, len
);
6452 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6457 pr_out_name(dl
, *name
);
6464 struct list_head list
;
6467 struct fmsg_cb_data
{
6471 struct list_head entry_list
;
6474 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6475 uint8_t *attr_value
, bool insert
)
6477 struct nest_entry
*entry
;
6480 entry
= malloc(sizeof(struct nest_entry
));
6484 entry
->attr_type
= *attr_value
;
6485 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6487 if (list_empty(&fmsg_data
->entry_list
))
6488 return MNL_CB_ERROR
;
6489 entry
= list_first_entry(&fmsg_data
->entry_list
,
6490 struct nest_entry
, list
);
6491 *attr_value
= entry
->attr_type
;
6492 list_del(&entry
->list
);
6498 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6501 pr_out_fmsg_name(dl
, name
);
6503 __pr_out_indent_inc();
6506 static void pr_out_fmsg_group_end(struct dl
*dl
)
6509 __pr_out_indent_dec();
6512 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6514 if (dl
->json_output
) {
6515 pr_out_fmsg_name(dl
, name
);
6516 open_json_object(NULL
);
6518 pr_out_fmsg_group_start(dl
, name
);
6522 static void pr_out_fmsg_end_object(struct dl
*dl
)
6524 if (dl
->json_output
)
6525 close_json_object();
6527 pr_out_fmsg_group_end(dl
);
6530 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6532 if (dl
->json_output
) {
6533 pr_out_fmsg_name(dl
, name
);
6534 open_json_array(PRINT_JSON
, NULL
);
6536 pr_out_fmsg_group_start(dl
, name
);
6540 static void pr_out_fmsg_end_array(struct dl
*dl
)
6542 if (dl
->json_output
)
6543 close_json_array(PRINT_JSON
, NULL
);
6545 pr_out_fmsg_group_end(dl
);
6548 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6551 struct dl
*dl
= fmsg_data
->dl
;
6552 uint8_t value
= nest_value
;
6555 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6556 if (err
!= MNL_CB_OK
)
6560 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6562 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6564 pr_out_fmsg_end_object(dl
);
6566 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6568 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6570 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6572 pr_out_fmsg_end_array(dl
);
6580 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6582 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6583 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6584 struct fmsg_cb_data
*fmsg_data
= data
;
6585 struct dl
*dl
= fmsg_data
->dl
;
6586 struct nlattr
*nla_object
;
6590 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6591 if (!tb
[DEVLINK_ATTR_FMSG
])
6592 return MNL_CB_ERROR
;
6594 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6595 attr_type
= mnl_attr_get_type(nla_object
);
6596 switch (attr_type
) {
6597 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6598 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6599 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6600 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6601 if (err
!= MNL_CB_OK
)
6604 case DEVLINK_ATTR_FMSG_NEST_END
:
6605 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6606 if (err
!= MNL_CB_OK
)
6609 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6610 free(fmsg_data
->name
);
6611 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6612 if (!fmsg_data
->name
)
6615 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6616 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6618 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6619 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6620 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6622 if (err
!= MNL_CB_OK
)
6632 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6634 /* FMSG is dynamic: opening of an object or array causes a
6635 * newline. JSON starts with an { or [, but plain text should
6636 * not start with a new line. Ensure this by setting
6637 * g_new_line_count to 1: avoiding newline before the first
6640 g_new_line_count
= 1;
6643 INIT_LIST_HEAD(&data
->entry_list
);
6646 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6648 struct fmsg_cb_data data
;
6649 struct nlmsghdr
*nlh
;
6652 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6654 err
= dl_argv_parse_put(nlh
, dl
,
6655 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6659 cmd_fmsg_init(dl
, &data
);
6660 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6665 static int cmd_health_dump_show(struct dl
*dl
)
6667 return cmd_health_object_common(dl
,
6668 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6672 static int cmd_health_diagnose(struct dl
*dl
)
6674 return cmd_health_object_common(dl
,
6675 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6679 static int cmd_health_recover(struct dl
*dl
)
6681 struct nlmsghdr
*nlh
;
6684 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6685 NLM_F_REQUEST
| NLM_F_ACK
);
6687 err
= dl_argv_parse_put(nlh
, dl
,
6688 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6692 dl_opts_put(nlh
, dl
);
6693 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6696 enum devlink_health_reporter_state
{
6697 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6698 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6701 static const char *health_state_name(uint8_t state
)
6704 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6705 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6706 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6707 return HEALTH_REPORTER_STATE_ERROR_STR
;
6709 return "<unknown state>";
6713 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6715 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6716 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6717 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6718 struct sysinfo s_info
;
6724 info
= localtime(&now
);
6725 err
= sysinfo(&s_info
);
6728 /* Subtract uptime in sec from now yields the time of system
6729 * uptime. To this, add time_ms which is the amount of
6730 * milliseconds elapsed between uptime and the dump taken.
6732 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6733 info
= localtime(&sec
);
6735 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6736 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6737 check_indent_newline(dl
);
6738 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6739 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6742 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6744 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6745 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6750 ts
= mnl_attr_get_u64(attr
);
6751 tv_sec
= ts
/ 1000000000;
6752 tm
= localtime(&tv_sec
);
6754 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6755 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6757 check_indent_newline(dl
);
6758 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6759 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6762 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6764 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6765 enum devlink_health_reporter_state state
;
6768 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6770 if (err
!= MNL_CB_OK
)
6773 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6774 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6775 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6776 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6779 pr_out_handle_start_arr(dl
, tb_health
);
6781 check_indent_newline(dl
);
6782 print_string(PRINT_ANY
, "reporter", "reporter %s",
6783 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6784 if (!dl
->json_output
) {
6786 __pr_out_indent_inc();
6788 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6789 check_indent_newline(dl
);
6790 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6791 pr_out_u64(dl
, "error",
6792 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6793 pr_out_u64(dl
, "recover",
6794 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6795 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6796 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6797 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6798 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6799 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6800 pr_out_u64(dl
, "grace_period",
6801 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6802 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6803 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6804 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6806 __pr_out_indent_dec();
6807 pr_out_handle_end(dl
);
6810 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6812 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6813 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6814 struct dl
*dl
= data
;
6816 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6817 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6818 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6819 return MNL_CB_ERROR
;
6821 pr_out_health(dl
, tb
);
6826 static int cmd_health_show(struct dl
*dl
)
6828 struct nlmsghdr
*nlh
;
6829 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6832 if (dl_argc(dl
) == 0)
6833 flags
|= NLM_F_DUMP
;
6834 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6837 if (dl_argc(dl
) > 0) {
6838 err
= dl_argv_parse_put(nlh
, dl
,
6840 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6844 pr_out_section_start(dl
, "health");
6846 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6847 pr_out_section_end(dl
);
6851 static void cmd_health_help(void)
6853 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6854 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6855 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6856 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6857 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6858 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6861 static int cmd_health(struct dl
*dl
)
6863 if (dl_argv_match(dl
, "help")) {
6866 } else if (dl_argv_match(dl
, "show") ||
6867 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6869 return cmd_health_show(dl
);
6870 } else if (dl_argv_match(dl
, "recover")) {
6872 return cmd_health_recover(dl
);
6873 } else if (dl_argv_match(dl
, "diagnose")) {
6875 return cmd_health_diagnose(dl
);
6876 } else if (dl_argv_match(dl
, "dump")) {
6878 if (dl_argv_match(dl
, "show")) {
6880 return cmd_health_dump_show(dl
);
6881 } else if (dl_argv_match(dl
, "clear")) {
6883 return cmd_health_dump_clear(dl
);
6885 } else if (dl_argv_match(dl
, "set")) {
6887 return cmd_health_set_params(dl
);
6889 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6893 static const char *trap_type_name(uint8_t type
)
6896 case DEVLINK_TRAP_TYPE_DROP
:
6898 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6901 return "<unknown type>";
6905 static const char *trap_action_name(uint8_t action
)
6908 case DEVLINK_TRAP_ACTION_DROP
:
6910 case DEVLINK_TRAP_ACTION_TRAP
:
6913 return "<unknown action>";
6917 static const char *trap_metadata_name(const struct nlattr
*attr
)
6919 switch (attr
->nla_type
) {
6920 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
6921 return "input_port";
6923 return "<unknown metadata type>";
6926 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
6928 struct nlattr
*attr_metadata
;
6930 pr_out_array_start(dl
, "metadata");
6931 mnl_attr_for_each_nested(attr_metadata
, attr
) {
6932 check_indent_newline(dl
);
6933 print_string(PRINT_ANY
, NULL
, "%s",
6934 trap_metadata_name(attr_metadata
));
6936 pr_out_array_end(dl
);
6939 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6941 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
6942 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
6945 pr_out_handle_start_arr(dl
, tb
);
6947 __pr_out_handle_start(dl
, tb
, true, false);
6949 check_indent_newline(dl
);
6950 print_string(PRINT_ANY
, "name", "name %s",
6951 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
6952 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
6953 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6954 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
6955 print_string(PRINT_ANY
, "group", " group %s",
6956 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6958 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
6959 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6960 pr_out_handle_end(dl
);
6963 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6965 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6966 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6967 struct dl
*dl
= data
;
6969 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6970 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6971 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
6972 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
6973 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
6974 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
6975 return MNL_CB_ERROR
;
6977 pr_out_trap(dl
, tb
, true);
6982 static void cmd_trap_help(void)
6984 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
6985 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
6986 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
6987 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
6990 static int cmd_trap_show(struct dl
*dl
)
6992 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6993 struct nlmsghdr
*nlh
;
6996 if (dl_argc(dl
) == 0)
6997 flags
|= NLM_F_DUMP
;
6999 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7001 if (dl_argc(dl
) > 0) {
7002 err
= dl_argv_parse_put(nlh
, dl
,
7003 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7008 pr_out_section_start(dl
, "trap");
7009 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7010 pr_out_section_end(dl
);
7015 static int cmd_trap_set(struct dl
*dl
)
7017 struct nlmsghdr
*nlh
;
7020 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7021 NLM_F_REQUEST
| NLM_F_ACK
);
7023 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7024 DL_OPT_TRAP_ACTION
);
7028 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7031 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7034 pr_out_handle_start_arr(dl
, tb
);
7036 __pr_out_handle_start(dl
, tb
, true, false);
7038 check_indent_newline(dl
);
7039 print_string(PRINT_ANY
, "name", "name %s",
7040 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7041 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7042 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7043 pr_out_handle_end(dl
);
7046 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7048 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7049 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7050 struct dl
*dl
= data
;
7052 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7053 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7054 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7055 return MNL_CB_ERROR
;
7057 pr_out_trap_group(dl
, tb
, true);
7062 static int cmd_trap_group_show(struct dl
*dl
)
7064 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7065 struct nlmsghdr
*nlh
;
7068 if (dl_argc(dl
) == 0)
7069 flags
|= NLM_F_DUMP
;
7071 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7073 if (dl_argc(dl
) > 0) {
7074 err
= dl_argv_parse_put(nlh
, dl
,
7075 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7081 pr_out_section_start(dl
, "trap_group");
7082 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7083 pr_out_section_end(dl
);
7088 static int cmd_trap_group_set(struct dl
*dl
)
7090 struct nlmsghdr
*nlh
;
7093 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7094 NLM_F_REQUEST
| NLM_F_ACK
);
7096 err
= dl_argv_parse_put(nlh
, dl
,
7097 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7098 DL_OPT_TRAP_ACTION
);
7102 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7105 static int cmd_trap_group(struct dl
*dl
)
7107 if (dl_argv_match(dl
, "help")) {
7110 } else if (dl_argv_match(dl
, "show") ||
7111 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7113 return cmd_trap_group_show(dl
);
7114 } else if (dl_argv_match(dl
, "set")) {
7116 return cmd_trap_group_set(dl
);
7118 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7122 static int cmd_trap(struct dl
*dl
)
7124 if (dl_argv_match(dl
, "help")) {
7127 } else if (dl_argv_match(dl
, "show") ||
7128 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7130 return cmd_trap_show(dl
);
7131 } else if (dl_argv_match(dl
, "set")) {
7133 return cmd_trap_set(dl
);
7134 } else if (dl_argv_match(dl
, "group")) {
7136 return cmd_trap_group(dl
);
7138 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7142 static void help(void)
7144 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7145 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7146 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7147 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7150 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7155 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7158 } else if (dl_argv_match(dl
, "dev")) {
7161 } else if (dl_argv_match(dl
, "port")) {
7163 return cmd_port(dl
);
7164 } else if (dl_argv_match(dl
, "sb")) {
7167 } else if (dl_argv_match(dl
, "monitor")) {
7170 } else if (dl_argv_match(dl
, "dpipe")) {
7172 return cmd_dpipe(dl
);
7173 } else if (dl_argv_match(dl
, "resource")) {
7175 return cmd_resource(dl
);
7176 } else if (dl_argv_match(dl
, "region")) {
7178 return cmd_region(dl
);
7179 } else if (dl_argv_match(dl
, "health")) {
7181 return cmd_health(dl
);
7182 } else if (dl_argv_match(dl
, "trap")) {
7184 return cmd_trap(dl
);
7186 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7190 static int dl_init(struct dl
*dl
)
7194 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7196 pr_err("Failed to connect to devlink Netlink\n");
7200 err
= ifname_map_init(dl
);
7202 pr_err("Failed to create index map\n");
7203 goto err_ifname_map_create
;
7205 new_json_obj_plain(dl
->json_output
);
7208 err_ifname_map_create
:
7209 mnlg_socket_close(dl
->nlg
);
7213 static void dl_fini(struct dl
*dl
)
7215 delete_json_obj_plain();
7216 ifname_map_fini(dl
);
7217 mnlg_socket_close(dl
->nlg
);
7220 static struct dl
*dl_alloc(void)
7224 dl
= calloc(1, sizeof(*dl
));
7230 static void dl_free(struct dl
*dl
)
7235 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7239 int ret
= EXIT_SUCCESS
;
7241 if (name
&& strcmp(name
, "-") != 0) {
7242 if (freopen(name
, "r", stdin
) == NULL
) {
7244 "Cannot open file \"%s\" for reading: %s\n",
7245 name
, strerror(errno
));
7246 return EXIT_FAILURE
;
7251 while (getcmdline(&line
, &len
, stdin
) != -1) {
7255 largc
= makeargs(line
, largv
, 100);
7257 continue; /* blank line */
7259 if (dl_cmd(dl
, largc
, largv
)) {
7260 fprintf(stderr
, "Command failed %s:%d\n",
7274 int main(int argc
, char **argv
)
7276 static const struct option long_options
[] = {
7277 { "Version", no_argument
, NULL
, 'V' },
7278 { "force", no_argument
, NULL
, 'f' },
7279 { "batch", required_argument
, NULL
, 'b' },
7280 { "no-nice-names", no_argument
, NULL
, 'n' },
7281 { "json", no_argument
, NULL
, 'j' },
7282 { "pretty", no_argument
, NULL
, 'p' },
7283 { "verbose", no_argument
, NULL
, 'v' },
7284 { "statistics", no_argument
, NULL
, 's' },
7285 { "Netns", required_argument
, NULL
, 'N' },
7286 { NULL
, 0, NULL
, 0 }
7288 const char *batch_file
= NULL
;
7297 pr_err("Failed to allocate memory for devlink\n");
7298 return EXIT_FAILURE
;
7301 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7302 long_options
, NULL
)) >= 0) {
7306 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7313 batch_file
= optarg
;
7316 dl
->no_nice_names
= true;
7319 dl
->json_output
= true;
7331 if (netns_switch(optarg
)) {
7337 pr_err("Unknown option.\n");
7354 err
= dl_batch(dl
, batch_file
, force
);
7356 err
= dl_cmd(dl
, argc
, argv
);