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_bool(struct dl
*dl
, const char *name
, bool val
)
1881 if (dl
->json_output
)
1882 print_bool(PRINT_JSON
, name
, NULL
, val
);
1884 print_string_name_value(name
, val
? "true" : "false");
1887 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1889 __pr_out_indent_newline(dl
);
1890 if (dl
->json_output
)
1891 print_uint(PRINT_JSON
, name
, NULL
, val
);
1893 pr_out("%s %u", name
, val
);
1896 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1898 __pr_out_indent_newline(dl
);
1899 if (val
== (uint64_t) -1)
1900 return print_string_name_value(name
, "unlimited");
1902 if (dl
->json_output
)
1903 print_u64(PRINT_JSON
, name
, NULL
, val
);
1905 pr_out("%s %"PRIu64
, name
, val
);
1908 static bool is_binary_eol(int i
)
1913 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1918 if (dl
->json_output
)
1919 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1921 pr_out("%02x ", data
[i
]);
1923 if (!dl
->json_output
&& is_binary_eol(i
))
1926 if (!dl
->json_output
&& !is_binary_eol(i
))
1930 static void pr_out_name(struct dl
*dl
, const char *name
)
1932 __pr_out_indent_newline(dl
);
1933 if (dl
->json_output
)
1934 print_string(PRINT_JSON
, name
, NULL
, NULL
);
1936 pr_out("%s:", name
);
1939 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1941 if (dl
->json_output
) {
1942 print_uint(PRINT_JSON
, "address", NULL
, addr
);
1943 open_json_array(PRINT_JSON
, "data");
1947 static void pr_out_region_chunk_end(struct dl
*dl
)
1949 if (dl
->json_output
)
1950 close_json_array(PRINT_JSON
, NULL
);
1953 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1956 static uint64_t align_val
;
1959 pr_out_region_chunk_start(dl
, addr
);
1961 if (!dl
->json_output
)
1962 if (!(align_val
% 16))
1963 pr_out("%s%016"PRIx64
" ",
1964 align_val
? "\n" : "",
1969 if (dl
->json_output
)
1970 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1972 pr_out("%02x ", data
[i
]);
1977 pr_out_region_chunk_end(dl
);
1980 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1982 if (dl
->json_output
) {
1983 open_json_object(NULL
);
1984 open_json_object(name
);
1988 static void pr_out_section_end(struct dl
*dl
)
1990 if (dl
->json_output
) {
1991 if (dl
->arr_last
.present
)
1992 close_json_array(PRINT_JSON
, NULL
);
1993 close_json_object();
1994 close_json_object();
1998 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2000 if (dl
->json_output
) {
2001 open_json_array(PRINT_JSON
, name
);
2003 __pr_out_indent_inc();
2005 pr_out("%s:", name
);
2006 __pr_out_indent_inc();
2011 static void pr_out_array_end(struct dl
*dl
)
2013 if (dl
->json_output
) {
2014 close_json_array(PRINT_JSON
, NULL
);
2016 __pr_out_indent_dec();
2017 __pr_out_indent_dec();
2021 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2023 if (dl
->json_output
) {
2024 open_json_object(name
);
2026 __pr_out_indent_inc();
2028 pr_out("%s:", name
);
2029 __pr_out_indent_inc();
2034 static void pr_out_object_end(struct dl
*dl
)
2036 if (dl
->json_output
) {
2037 close_json_object();
2039 __pr_out_indent_dec();
2040 __pr_out_indent_dec();
2044 static void pr_out_entry_start(struct dl
*dl
)
2046 if (dl
->json_output
)
2047 open_json_object(NULL
);
2050 static void pr_out_entry_end(struct dl
*dl
)
2052 if (dl
->json_output
)
2053 close_json_object();
2058 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2060 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2066 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2067 if (err
!= MNL_CB_OK
)
2070 pr_out_object_start(dl
, "stats");
2071 pr_out_object_start(dl
, "rx");
2072 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2073 pr_out_u64(dl
, "bytes",
2074 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2075 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2076 pr_out_u64(dl
, "packets",
2077 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2078 pr_out_object_end(dl
);
2079 pr_out_object_end(dl
);
2082 static const char *param_cmode_name(uint8_t cmode
)
2085 case DEVLINK_PARAM_CMODE_RUNTIME
:
2086 return PARAM_CMODE_RUNTIME_STR
;
2087 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2088 return PARAM_CMODE_DRIVERINIT_STR
;
2089 case DEVLINK_PARAM_CMODE_PERMANENT
:
2090 return PARAM_CMODE_PERMANENT_STR
;
2091 default: return "<unknown type>";
2095 static const char *eswitch_mode_name(uint32_t mode
)
2098 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2099 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2100 default: return "<unknown mode>";
2104 static const char *eswitch_inline_mode_name(uint32_t mode
)
2107 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2108 return ESWITCH_INLINE_MODE_NONE
;
2109 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2110 return ESWITCH_INLINE_MODE_LINK
;
2111 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2112 return ESWITCH_INLINE_MODE_NETWORK
;
2113 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2114 return ESWITCH_INLINE_MODE_TRANSPORT
;
2116 return "<unknown mode>";
2120 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2122 __pr_out_handle_start(dl
, tb
, true, false);
2124 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2125 check_indent_newline(dl
);
2126 print_string(PRINT_ANY
, "mode", "mode %s",
2127 eswitch_mode_name(mnl_attr_get_u16(
2128 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2130 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2131 check_indent_newline(dl
);
2132 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2133 eswitch_inline_mode_name(mnl_attr_get_u8(
2134 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2136 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2137 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2139 check_indent_newline(dl
);
2140 print_string(PRINT_ANY
, "encap", "encap %s",
2141 encap_mode
? "enable" : "disable");
2144 pr_out_handle_end(dl
);
2147 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2149 struct dl
*dl
= data
;
2150 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2151 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2153 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2154 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2155 return MNL_CB_ERROR
;
2156 pr_out_eswitch(dl
, tb
);
2160 static int cmd_dev_eswitch_show(struct dl
*dl
)
2162 struct nlmsghdr
*nlh
;
2165 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2166 NLM_F_REQUEST
| NLM_F_ACK
);
2168 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2172 pr_out_section_start(dl
, "dev");
2173 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2174 pr_out_section_end(dl
);
2178 static int cmd_dev_eswitch_set(struct dl
*dl
)
2180 struct nlmsghdr
*nlh
;
2183 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2184 NLM_F_REQUEST
| NLM_F_ACK
);
2186 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2187 DL_OPT_ESWITCH_MODE
|
2188 DL_OPT_ESWITCH_INLINE_MODE
|
2189 DL_OPT_ESWITCH_ENCAP_MODE
);
2194 if (dl
->opts
.present
== 1) {
2195 pr_err("Need to set at least one option\n");
2199 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2202 static int cmd_dev_eswitch(struct dl
*dl
)
2204 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2207 } else if (dl_argv_match(dl
, "set")) {
2209 return cmd_dev_eswitch_set(dl
);
2210 } else if (dl_argv_match(dl
, "show")) {
2212 return cmd_dev_eswitch_show(dl
);
2214 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2218 struct param_val_conv
{
2224 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2225 uint32_t len
, const char *name
)
2229 for (i
= 0; i
< len
; i
++)
2230 if (!strcmp(param_val_conv
[i
].name
, name
))
2237 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2238 uint32_t len
, const char *name
, const char *vstr
,
2243 for (i
= 0; i
< len
; i
++)
2244 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2245 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2246 *vuint
= param_val_conv
[i
].vuint
;
2254 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2255 uint32_t len
, const char *name
, uint32_t vuint
,
2260 for (i
= 0; i
< len
; i
++)
2261 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2262 param_val_conv
[i
].vuint
== vuint
) {
2263 *vstr
= param_val_conv
[i
].vstr
;
2270 static const struct param_val_conv param_val_conv
[] = {
2272 .name
= "fw_load_policy",
2274 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2277 .name
= "fw_load_policy",
2279 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2282 .name
= "reset_dev_on_drv_probe",
2284 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2287 .name
= "fw_load_policy",
2289 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2292 .name
= "reset_dev_on_drv_probe",
2294 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2297 .name
= "reset_dev_on_drv_probe",
2299 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2302 .name
= "reset_dev_on_drv_probe",
2304 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2308 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2310 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2311 int nla_type
, struct nlattr
*nl
)
2313 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2314 struct nlattr
*val_attr
;
2319 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2320 if (err
!= MNL_CB_OK
)
2323 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2324 (nla_type
!= MNL_TYPE_FLAG
&&
2325 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2328 check_indent_newline(dl
);
2329 print_string(PRINT_ANY
, "cmode", "cmode %s",
2330 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2332 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2334 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2340 err
= param_val_conv_str_get(param_val_conv
,
2343 mnl_attr_get_u8(val_attr
),
2347 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2349 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2354 err
= param_val_conv_str_get(param_val_conv
,
2357 mnl_attr_get_u16(val_attr
),
2361 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2363 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2368 err
= param_val_conv_str_get(param_val_conv
,
2371 mnl_attr_get_u32(val_attr
),
2375 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2377 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2380 case MNL_TYPE_STRING
:
2381 print_string(PRINT_ANY
, "value", " value %s",
2382 mnl_attr_get_str(val_attr
));
2385 pr_out_bool(dl
, "value", val_attr
? true : false);
2390 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2392 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2393 struct nlattr
*param_value_attr
;
2394 const char *nla_name
;
2398 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2399 if (err
!= MNL_CB_OK
)
2401 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2402 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2403 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2407 pr_out_handle_start_arr(dl
, tb
);
2409 __pr_out_handle_start(dl
, tb
, true, false);
2411 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2413 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2414 check_indent_newline(dl
);
2415 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2416 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2417 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2419 print_string(PRINT_ANY
, "type", "type %s", "generic");
2421 pr_out_array_start(dl
, "values");
2422 mnl_attr_for_each_nested(param_value_attr
,
2423 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2424 pr_out_entry_start(dl
);
2425 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2426 pr_out_entry_end(dl
);
2428 pr_out_array_end(dl
);
2429 pr_out_handle_end(dl
);
2432 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2434 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2435 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2436 struct dl
*dl
= data
;
2438 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2439 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2440 !tb
[DEVLINK_ATTR_PARAM
])
2441 return MNL_CB_ERROR
;
2442 pr_out_param(dl
, tb
, true);
2458 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2460 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2461 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2462 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2463 struct nlattr
*param_value_attr
;
2464 enum devlink_param_cmode cmode
;
2465 struct param_ctx
*ctx
= data
;
2466 struct dl
*dl
= ctx
->dl
;
2470 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2471 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2472 !tb
[DEVLINK_ATTR_PARAM
])
2473 return MNL_CB_ERROR
;
2475 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2476 if (err
!= MNL_CB_OK
)
2477 return MNL_CB_ERROR
;
2479 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2480 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2481 return MNL_CB_ERROR
;
2483 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2484 mnl_attr_for_each_nested(param_value_attr
,
2485 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2486 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2487 struct nlattr
*val_attr
;
2489 err
= mnl_attr_parse_nested(param_value_attr
,
2490 attr_cb
, nla_value
);
2491 if (err
!= MNL_CB_OK
)
2492 return MNL_CB_ERROR
;
2494 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2495 (nla_type
!= MNL_TYPE_FLAG
&&
2496 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2497 return MNL_CB_ERROR
;
2499 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2500 if (cmode
== dl
->opts
.cmode
) {
2501 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2504 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2507 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2510 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2512 case MNL_TYPE_STRING
:
2513 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2516 ctx
->value
.vbool
= val_attr
? true : false;
2522 ctx
->nla_type
= nla_type
;
2526 static int cmd_dev_param_set(struct dl
*dl
)
2528 struct param_ctx ctx
= {};
2529 struct nlmsghdr
*nlh
;
2537 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2539 DL_OPT_PARAM_VALUE
|
2540 DL_OPT_PARAM_CMODE
, 0);
2544 /* Get value type */
2545 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2546 NLM_F_REQUEST
| NLM_F_ACK
);
2547 dl_opts_put(nlh
, dl
);
2550 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2554 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2555 NLM_F_REQUEST
| NLM_F_ACK
);
2556 dl_opts_put(nlh
, dl
);
2558 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2559 dl
->opts
.param_name
);
2561 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2562 switch (ctx
.nla_type
) {
2565 err
= param_val_conv_uint_get(param_val_conv
,
2567 dl
->opts
.param_name
,
2568 dl
->opts
.param_value
,
2572 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2575 goto err_param_value_parse
;
2576 if (val_u8
== ctx
.value
.vu8
)
2578 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2582 err
= param_val_conv_uint_get(param_val_conv
,
2584 dl
->opts
.param_name
,
2585 dl
->opts
.param_value
,
2589 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2592 goto err_param_value_parse
;
2593 if (val_u16
== ctx
.value
.vu16
)
2595 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2599 err
= param_val_conv_uint_get(param_val_conv
,
2601 dl
->opts
.param_name
,
2602 dl
->opts
.param_value
,
2605 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2607 goto err_param_value_parse
;
2608 if (val_u32
== ctx
.value
.vu32
)
2610 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2613 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2615 goto err_param_value_parse
;
2616 if (val_bool
== ctx
.value
.vbool
)
2619 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2622 case MNL_TYPE_STRING
:
2623 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2624 dl
->opts
.param_value
);
2625 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2629 printf("Value type not supported\n");
2632 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2634 err_param_value_parse
:
2635 pr_err("Value \"%s\" is not a number or not within range\n",
2636 dl
->opts
.param_value
);
2640 static int cmd_dev_param_show(struct dl
*dl
)
2642 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2643 struct nlmsghdr
*nlh
;
2646 if (dl_argc(dl
) == 0)
2647 flags
|= NLM_F_DUMP
;
2649 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2651 if (dl_argc(dl
) > 0) {
2652 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2653 DL_OPT_PARAM_NAME
, 0);
2658 pr_out_section_start(dl
, "param");
2659 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2660 pr_out_section_end(dl
);
2664 static int cmd_dev_param(struct dl
*dl
)
2666 if (dl_argv_match(dl
, "help")) {
2669 } else if (dl_argv_match(dl
, "show") ||
2670 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2672 return cmd_dev_param_show(dl
);
2673 } else if (dl_argv_match(dl
, "set")) {
2675 return cmd_dev_param_set(dl
);
2677 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2680 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2682 struct dl
*dl
= data
;
2683 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2684 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2685 uint8_t reload_failed
= 0;
2687 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2688 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2689 return MNL_CB_ERROR
;
2691 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2692 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2694 if (reload_failed
) {
2695 __pr_out_handle_start(dl
, tb
, true, false);
2696 pr_out_bool(dl
, "reload_failed", true);
2697 pr_out_handle_end(dl
);
2699 pr_out_handle(dl
, tb
);
2705 static int cmd_dev_show(struct dl
*dl
)
2707 struct nlmsghdr
*nlh
;
2708 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2711 if (dl_argc(dl
) == 0)
2712 flags
|= NLM_F_DUMP
;
2714 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2716 if (dl_argc(dl
) > 0) {
2717 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2722 pr_out_section_start(dl
, "dev");
2723 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2724 pr_out_section_end(dl
);
2728 static void cmd_dev_reload_help(void)
2730 pr_err("Usage: devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2733 static int cmd_dev_reload(struct dl
*dl
)
2735 struct nlmsghdr
*nlh
;
2738 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2739 cmd_dev_reload_help();
2743 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2744 NLM_F_REQUEST
| NLM_F_ACK
);
2746 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2750 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2753 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2754 const char *name
, int type
)
2756 struct nlattr
*version
;
2758 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2759 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2760 const char *ver_value
;
2761 const char *ver_name
;
2764 if (mnl_attr_get_type(version
) != type
)
2767 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2768 if (err
!= MNL_CB_OK
)
2771 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2772 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2776 pr_out_object_start(dl
, name
);
2780 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2781 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2783 check_indent_newline(dl
);
2784 print_string_name_value(ver_name
, ver_value
);
2785 if (!dl
->json_output
)
2790 pr_out_object_end(dl
);
2793 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2794 struct nlattr
**tb
, bool has_versions
)
2796 __pr_out_handle_start(dl
, tb
, true, false);
2798 __pr_out_indent_inc();
2799 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2800 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2802 if (!dl
->json_output
)
2804 check_indent_newline(dl
);
2805 print_string(PRINT_ANY
, "driver", "driver %s",
2806 mnl_attr_get_str(nla_drv
));
2809 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2810 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2812 if (!dl
->json_output
)
2814 check_indent_newline(dl
);
2815 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2816 mnl_attr_get_str(nla_sn
));
2818 __pr_out_indent_dec();
2821 pr_out_object_start(dl
, "versions");
2823 pr_out_versions_single(dl
, nlh
, "fixed",
2824 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2825 pr_out_versions_single(dl
, nlh
, "running",
2826 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2827 pr_out_versions_single(dl
, nlh
, "stored",
2828 DEVLINK_ATTR_INFO_VERSION_STORED
);
2830 pr_out_object_end(dl
);
2833 pr_out_handle_end(dl
);
2836 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2838 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2839 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2840 bool has_versions
, has_info
;
2841 struct dl
*dl
= data
;
2843 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2845 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2846 return MNL_CB_ERROR
;
2848 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2849 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2850 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2851 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2852 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2856 pr_out_info(dl
, nlh
, tb
, has_versions
);
2861 static void cmd_dev_info_help(void)
2863 pr_err("Usage: devlink dev info [ DEV ]\n");
2866 static int cmd_dev_info(struct dl
*dl
)
2868 struct nlmsghdr
*nlh
;
2869 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2872 if (dl_argv_match(dl
, "help")) {
2873 cmd_dev_info_help();
2877 if (dl_argc(dl
) == 0)
2878 flags
|= NLM_F_DUMP
;
2880 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2882 if (dl_argc(dl
) > 0) {
2883 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2888 pr_out_section_start(dl
, "info");
2889 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2890 pr_out_section_end(dl
);
2894 static void cmd_dev_flash_help(void)
2896 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2900 struct cmd_dev_flash_status_ctx
{
2903 char *last_component
;
2904 uint8_t not_first
:1,
2910 static int nullstrcmp(const char *str1
, const char *str2
)
2913 return strcmp(str1
, str2
);
2916 return str1
? 1 : -1;
2919 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2921 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2922 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2923 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2924 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2925 const char *component
= NULL
;
2926 uint64_t done
= 0, total
= 0;
2927 const char *msg
= NULL
;
2928 const char *bus_name
;
2929 const char *dev_name
;
2931 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2932 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2935 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2936 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2937 return MNL_CB_ERROR
;
2938 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2939 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2940 if (strcmp(bus_name
, opts
->bus_name
) ||
2941 strcmp(dev_name
, opts
->dev_name
))
2942 return MNL_CB_ERROR
;
2944 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2946 free(ctx
->last_msg
);
2947 free(ctx
->last_component
);
2948 ctx
->received_end
= 1;
2952 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2953 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2954 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2955 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2956 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2957 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2958 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2959 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
2961 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
2962 !nullstrcmp(component
, ctx
->last_component
) &&
2963 ctx
->last_pc
&& ctx
->not_first
) {
2964 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
2969 pr_out("[%s] ", component
);
2970 free(ctx
->last_component
);
2971 ctx
->last_component
= strdup(component
);
2975 free(ctx
->last_msg
);
2976 ctx
->last_msg
= strdup(msg
);
2980 pr_out_tty(" %3lu%%", (done
* 100) / total
);
2991 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
2992 struct mnlg_socket
*nlg_ntf
,
2995 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3002 for (i
= 0; i
< 3; i
++)
3004 FD_SET(pipe_r
, &fds
[0]);
3006 FD_SET(nlfd
, &fds
[0]);
3010 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3013 pr_err("select() failed\n");
3016 if (FD_ISSET(nlfd
, &fds
[0])) {
3017 err
= _mnlg_socket_recv_run(nlg_ntf
,
3018 cmd_dev_flash_status_cb
, ctx
);
3022 if (FD_ISSET(pipe_r
, &fds
[0])) {
3023 err
= read(pipe_r
, &err2
, sizeof(err2
));
3025 pr_err("Failed to read pipe\n");
3030 ctx
->flash_done
= 1;
3036 static int cmd_dev_flash(struct dl
*dl
)
3038 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3039 struct mnlg_socket
*nlg_ntf
;
3040 struct nlmsghdr
*nlh
;
3046 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3047 cmd_dev_flash_help();
3051 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3052 NLM_F_REQUEST
| NLM_F_ACK
);
3054 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3055 DL_OPT_FLASH_COMPONENT
);
3059 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3063 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3067 err
= pipe(pipe_fds
);
3070 pipe_r
= pipe_fds
[0];
3071 pipe_w
= pipe_fds
[1];
3079 /* In child, just execute the flash and pass returned
3080 * value through pipe once it is done.
3083 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3084 write(pipe_w
, &err
, sizeof(err
));
3091 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3094 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3096 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3099 mnlg_socket_close(nlg_ntf
);
3103 static int cmd_dev(struct dl
*dl
)
3105 if (dl_argv_match(dl
, "help")) {
3108 } else if (dl_argv_match(dl
, "show") ||
3109 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3111 return cmd_dev_show(dl
);
3112 } else if (dl_argv_match(dl
, "eswitch")) {
3114 return cmd_dev_eswitch(dl
);
3115 } else if (dl_argv_match(dl
, "reload")) {
3117 return cmd_dev_reload(dl
);
3118 } else if (dl_argv_match(dl
, "param")) {
3120 return cmd_dev_param(dl
);
3121 } else if (dl_argv_match(dl
, "info")) {
3123 return cmd_dev_info(dl
);
3124 } else if (dl_argv_match(dl
, "flash")) {
3126 return cmd_dev_flash(dl
);
3128 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3132 static void cmd_port_help(void)
3134 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3135 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3136 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3137 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3140 static const char *port_type_name(uint32_t type
)
3143 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3144 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3145 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3146 case DEVLINK_PORT_TYPE_IB
: return "ib";
3147 default: return "<unknown type>";
3151 static const char *port_flavour_name(uint16_t flavour
)
3154 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3156 case DEVLINK_PORT_FLAVOUR_CPU
:
3158 case DEVLINK_PORT_FLAVOUR_DSA
:
3160 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3162 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3165 return "<unknown flavour>";
3169 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3173 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3174 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3175 pr_out_uint(dl
, "pfnum", fn_num
);
3177 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3178 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3179 pr_out_uint(dl
, "vfnum", fn_num
);
3183 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3185 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3186 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3188 pr_out_port_handle_start(dl
, tb
, false);
3189 check_indent_newline(dl
);
3191 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3193 print_string(PRINT_ANY
, "type", "type %s",
3194 port_type_name(port_type
));
3196 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3198 if (port_type
!= des_port_type
)
3199 print_string(PRINT_ANY
, "des_type", " des_type %s",
3200 port_type_name(des_port_type
));
3203 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3204 print_string(PRINT_ANY
, "netdev", " netdev %s",
3205 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3207 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3208 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3209 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3211 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3212 uint16_t port_flavour
=
3213 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3215 print_string(PRINT_ANY
, "flavour", " flavour %s",
3216 port_flavour_name(port_flavour
));
3218 switch (port_flavour
) {
3219 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3220 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3221 pr_out_port_pfvf_num(dl
, tb
);
3227 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3228 uint32_t port_number
;
3230 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3231 pr_out_uint(dl
, "port", port_number
);
3233 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3234 pr_out_uint(dl
, "split_group",
3235 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3236 pr_out_port_handle_end(dl
);
3239 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3241 struct dl
*dl
= data
;
3242 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3243 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3245 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3246 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3247 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3248 return MNL_CB_ERROR
;
3249 pr_out_port(dl
, tb
);
3253 static int cmd_port_show(struct dl
*dl
)
3255 struct nlmsghdr
*nlh
;
3256 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3259 if (dl_argc(dl
) == 0)
3260 flags
|= NLM_F_DUMP
;
3262 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3264 if (dl_argc(dl
) > 0) {
3265 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3270 pr_out_section_start(dl
, "port");
3271 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3272 pr_out_section_end(dl
);
3276 static int cmd_port_set(struct dl
*dl
)
3278 struct nlmsghdr
*nlh
;
3281 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3282 NLM_F_REQUEST
| NLM_F_ACK
);
3284 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3288 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3291 static int cmd_port_split(struct dl
*dl
)
3293 struct nlmsghdr
*nlh
;
3296 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3297 NLM_F_REQUEST
| NLM_F_ACK
);
3299 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3303 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3306 static int cmd_port_unsplit(struct dl
*dl
)
3308 struct nlmsghdr
*nlh
;
3311 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3312 NLM_F_REQUEST
| NLM_F_ACK
);
3314 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3318 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3321 static int cmd_port(struct dl
*dl
)
3323 if (dl_argv_match(dl
, "help")) {
3326 } else if (dl_argv_match(dl
, "show") ||
3327 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3329 return cmd_port_show(dl
);
3330 } else if (dl_argv_match(dl
, "set")) {
3332 return cmd_port_set(dl
);
3333 } else if (dl_argv_match(dl
, "split")) {
3335 return cmd_port_split(dl
);
3336 } else if (dl_argv_match(dl
, "unsplit")) {
3338 return cmd_port_unsplit(dl
);
3340 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3344 static void cmd_sb_help(void)
3346 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3347 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3348 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3349 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3350 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3351 pr_err(" pool POOL_INDEX ]\n");
3352 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3353 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3354 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3355 pr_err(" type { ingress | egress } ]\n");
3356 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3357 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3358 pr_err(" th THRESHOLD\n");
3359 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3360 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3361 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3364 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3366 pr_out_handle_start_arr(dl
, tb
);
3367 check_indent_newline(dl
);
3368 pr_out_uint(dl
, "sb",
3369 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3370 pr_out_uint(dl
, "size",
3371 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3372 pr_out_uint(dl
, "ing_pools",
3373 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3374 pr_out_uint(dl
, "eg_pools",
3375 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3376 pr_out_uint(dl
, "ing_tcs",
3377 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3378 pr_out_uint(dl
, "eg_tcs",
3379 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3380 pr_out_handle_end(dl
);
3383 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3385 struct dl
*dl
= data
;
3386 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3387 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3389 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3390 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3391 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3392 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3393 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3394 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3395 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3396 return MNL_CB_ERROR
;
3401 static int cmd_sb_show(struct dl
*dl
)
3403 struct nlmsghdr
*nlh
;
3404 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3407 if (dl_argc(dl
) == 0)
3408 flags
|= NLM_F_DUMP
;
3410 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3412 if (dl_argc(dl
) > 0) {
3413 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3418 pr_out_section_start(dl
, "sb");
3419 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3420 pr_out_section_end(dl
);
3424 static const char *pool_type_name(uint8_t type
)
3427 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3428 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3429 default: return "<unknown type>";
3433 static const char *threshold_type_name(uint8_t type
)
3436 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3437 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3438 default: return "<unknown type>";
3442 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3444 pr_out_handle_start_arr(dl
, tb
);
3445 pr_out_uint(dl
, "sb",
3446 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3447 pr_out_uint(dl
, "pool",
3448 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3449 print_string(PRINT_ANY
, "type", " type %s",
3450 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3451 pr_out_uint(dl
, "size",
3452 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3453 print_string(PRINT_ANY
, "thtype", " thtype %s",
3454 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3455 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3456 pr_out_uint(dl
, "cell_size",
3457 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3458 pr_out_handle_end(dl
);
3461 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3463 struct dl
*dl
= data
;
3464 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3465 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3467 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3468 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3469 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3470 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3471 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3472 return MNL_CB_ERROR
;
3473 pr_out_sb_pool(dl
, tb
);
3477 static int cmd_sb_pool_show(struct dl
*dl
)
3479 struct nlmsghdr
*nlh
;
3480 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3483 if (dl_argc(dl
) == 0)
3484 flags
|= NLM_F_DUMP
;
3486 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3488 if (dl_argc(dl
) > 0) {
3489 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3495 pr_out_section_start(dl
, "pool");
3496 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3497 pr_out_section_end(dl
);
3501 static int cmd_sb_pool_set(struct dl
*dl
)
3503 struct nlmsghdr
*nlh
;
3506 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3507 NLM_F_REQUEST
| NLM_F_ACK
);
3509 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3510 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3514 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3517 static int cmd_sb_pool(struct dl
*dl
)
3519 if (dl_argv_match(dl
, "help")) {
3522 } else if (dl_argv_match(dl
, "show") ||
3523 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3525 return cmd_sb_pool_show(dl
);
3526 } else if (dl_argv_match(dl
, "set")) {
3528 return cmd_sb_pool_set(dl
);
3530 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3534 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3536 pr_out_port_handle_start_arr(dl
, tb
, true);
3537 pr_out_uint(dl
, "sb",
3538 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3539 pr_out_uint(dl
, "pool",
3540 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3541 pr_out_uint(dl
, "threshold",
3542 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3543 pr_out_port_handle_end(dl
);
3546 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3548 struct dl
*dl
= data
;
3549 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3550 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3552 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3553 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3554 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3555 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3556 return MNL_CB_ERROR
;
3557 pr_out_sb_port_pool(dl
, tb
);
3561 static int cmd_sb_port_pool_show(struct dl
*dl
)
3563 struct nlmsghdr
*nlh
;
3564 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3567 if (dl_argc(dl
) == 0)
3568 flags
|= NLM_F_DUMP
;
3570 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3572 if (dl_argc(dl
) > 0) {
3573 err
= dl_argv_parse_put(nlh
, dl
,
3574 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3580 pr_out_section_start(dl
, "port_pool");
3581 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3582 pr_out_section_end(dl
);
3586 static int cmd_sb_port_pool_set(struct dl
*dl
)
3588 struct nlmsghdr
*nlh
;
3591 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3592 NLM_F_REQUEST
| NLM_F_ACK
);
3594 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3595 DL_OPT_SB_TH
, DL_OPT_SB
);
3599 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3602 static int cmd_sb_port_pool(struct dl
*dl
)
3604 if (dl_argv_match(dl
, "help")) {
3607 } else if (dl_argv_match(dl
, "show") ||
3608 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3610 return cmd_sb_port_pool_show(dl
);
3611 } else if (dl_argv_match(dl
, "set")) {
3613 return cmd_sb_port_pool_set(dl
);
3615 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3619 static int cmd_sb_port(struct dl
*dl
)
3621 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3624 } else if (dl_argv_match(dl
, "pool")) {
3626 return cmd_sb_port_pool(dl
);
3628 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3632 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3634 pr_out_port_handle_start_arr(dl
, tb
, true);
3635 pr_out_uint(dl
, "sb",
3636 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3637 pr_out_uint(dl
, "tc",
3638 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3639 print_string(PRINT_ANY
, "type", " type %s",
3640 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3641 pr_out_uint(dl
, "pool",
3642 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3643 pr_out_uint(dl
, "threshold",
3644 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3645 pr_out_port_handle_end(dl
);
3648 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3650 struct dl
*dl
= data
;
3651 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3652 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3654 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3655 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3656 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3657 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3658 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3659 return MNL_CB_ERROR
;
3660 pr_out_sb_tc_bind(dl
, tb
);
3664 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3666 struct nlmsghdr
*nlh
;
3667 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3670 if (dl_argc(dl
) == 0)
3671 flags
|= NLM_F_DUMP
;
3673 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3675 if (dl_argc(dl
) > 0) {
3676 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3677 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3682 pr_out_section_start(dl
, "tc_bind");
3683 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3684 pr_out_section_end(dl
);
3688 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3690 struct nlmsghdr
*nlh
;
3693 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3694 NLM_F_REQUEST
| NLM_F_ACK
);
3696 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3697 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3702 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3705 static int cmd_sb_tc_bind(struct dl
*dl
)
3707 if (dl_argv_match(dl
, "help")) {
3710 } else if (dl_argv_match(dl
, "show") ||
3711 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3713 return cmd_sb_tc_bind_show(dl
);
3714 } else if (dl_argv_match(dl
, "set")) {
3716 return cmd_sb_tc_bind_set(dl
);
3718 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3722 static int cmd_sb_tc(struct dl
*dl
)
3724 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3727 } else if (dl_argv_match(dl
, "bind")) {
3729 return cmd_sb_tc_bind(dl
);
3731 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3736 struct list_head list
;
3740 uint32_t bound_pool_index
;
3744 struct list_head list
;
3747 uint32_t port_index
;
3749 struct list_head pool_list
;
3750 struct list_head ing_tc_list
;
3751 struct list_head eg_tc_list
;
3757 struct list_head port_list
;
3760 static struct occ_item
*occ_item_alloc(void)
3762 return calloc(1, sizeof(struct occ_item
));
3765 static void occ_item_free(struct occ_item
*occ_item
)
3770 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3772 struct occ_port
*occ_port
;
3774 occ_port
= calloc(1, sizeof(*occ_port
));
3777 occ_port
->port_index
= port_index
;
3778 INIT_LIST_HEAD(&occ_port
->pool_list
);
3779 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3780 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3784 static void occ_port_free(struct occ_port
*occ_port
)
3786 struct occ_item
*occ_item
, *tmp
;
3788 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3789 occ_item_free(occ_item
);
3790 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3791 occ_item_free(occ_item
);
3792 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3793 occ_item_free(occ_item
);
3796 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3798 struct occ_show
*occ_show
;
3800 occ_show
= calloc(1, sizeof(*occ_show
));
3804 INIT_LIST_HEAD(&occ_show
->port_list
);
3808 static void occ_show_free(struct occ_show
*occ_show
)
3810 struct occ_port
*occ_port
, *tmp
;
3812 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3813 occ_port_free(occ_port
);
3816 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3819 struct occ_port
*occ_port
;
3820 uint32_t port_index
;
3822 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3824 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3825 if (occ_port
->port_index
== port_index
)
3828 occ_port
= occ_port_alloc(port_index
);
3831 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3835 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3838 struct occ_item
*occ_item
;
3841 pr_out_sp(7, " %s:", label
);
3842 list_for_each_entry(occ_item
, list
, list
) {
3843 if ((i
- 1) % 4 == 0 && i
!= 1)
3846 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3847 occ_item
->bound_pool_index
);
3849 pr_out_sp(7, "%2u:", occ_item
->index
);
3850 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3854 if ((i
- 1) % 4 != 0)
3858 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3859 struct list_head
*list
,
3862 struct occ_item
*occ_item
;
3865 open_json_object(label
);
3866 list_for_each_entry(occ_item
, list
, list
) {
3867 sprintf(buf
, "%u", occ_item
->index
);
3868 open_json_object(buf
);
3870 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3871 occ_item
->bound_pool_index
);
3872 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3873 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3874 close_json_object();
3876 close_json_object();
3879 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3881 if (dl
->json_output
) {
3882 pr_out_json_occ_show_item_list(dl
, "pool",
3883 &occ_port
->pool_list
, false);
3884 pr_out_json_occ_show_item_list(dl
, "itc",
3885 &occ_port
->ing_tc_list
, true);
3886 pr_out_json_occ_show_item_list(dl
, "etc",
3887 &occ_port
->eg_tc_list
, true);
3890 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3891 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3892 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3896 static void pr_out_occ_show(struct occ_show
*occ_show
)
3898 struct dl
*dl
= occ_show
->dl
;
3899 struct dl_opts
*opts
= &dl
->opts
;
3900 struct occ_port
*occ_port
;
3902 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3903 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3904 occ_port
->port_index
, true, false);
3905 pr_out_occ_show_port(dl
, occ_port
);
3906 pr_out_port_handle_end(dl
);
3910 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3913 struct occ_port
*occ_port
;
3914 struct occ_item
*occ_item
;
3916 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3919 occ_port
= occ_port_get(occ_show
, tb
);
3921 occ_show
->err
= -ENOMEM
;
3925 occ_item
= occ_item_alloc();
3927 occ_show
->err
= -ENOMEM
;
3930 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3931 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3932 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3933 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3936 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3938 struct occ_show
*occ_show
= data
;
3939 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3940 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3942 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3943 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3944 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3945 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3946 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3947 return MNL_CB_ERROR
;
3948 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3952 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3955 struct occ_port
*occ_port
;
3956 struct occ_item
*occ_item
;
3959 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3962 occ_port
= occ_port_get(occ_show
, tb
);
3964 occ_show
->err
= -ENOMEM
;
3968 occ_item
= occ_item_alloc();
3970 occ_show
->err
= -ENOMEM
;
3973 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3974 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3975 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3976 occ_item
->bound_pool_index
=
3977 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3978 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3979 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3980 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3981 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3982 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3984 occ_item_free(occ_item
);
3987 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3989 struct occ_show
*occ_show
= data
;
3990 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3991 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3993 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3994 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3995 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3996 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3997 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3998 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3999 return MNL_CB_ERROR
;
4000 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4004 static int cmd_sb_occ_show(struct dl
*dl
)
4006 struct nlmsghdr
*nlh
;
4007 struct occ_show
*occ_show
;
4008 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4011 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4015 occ_show
= occ_show_alloc(dl
);
4019 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4021 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4022 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4026 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4028 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4029 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4033 pr_out_section_start(dl
, "occupancy");
4034 pr_out_occ_show(occ_show
);
4035 pr_out_section_end(dl
);
4038 occ_show_free(occ_show
);
4042 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4044 struct nlmsghdr
*nlh
;
4047 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4048 NLM_F_REQUEST
| NLM_F_ACK
);
4050 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4054 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4057 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4059 struct nlmsghdr
*nlh
;
4062 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4063 NLM_F_REQUEST
| NLM_F_ACK
);
4065 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4069 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4072 static int cmd_sb_occ(struct dl
*dl
)
4074 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4077 } else if (dl_argv_match(dl
, "show") ||
4078 dl_argv_match(dl
, "list")) {
4080 return cmd_sb_occ_show(dl
);
4081 } else if (dl_argv_match(dl
, "snapshot")) {
4083 return cmd_sb_occ_snapshot(dl
);
4084 } else if (dl_argv_match(dl
, "clearmax")) {
4086 return cmd_sb_occ_clearmax(dl
);
4088 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4092 static int cmd_sb(struct dl
*dl
)
4094 if (dl_argv_match(dl
, "help")) {
4097 } else if (dl_argv_match(dl
, "show") ||
4098 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4100 return cmd_sb_show(dl
);
4101 } else if (dl_argv_match(dl
, "pool")) {
4103 return cmd_sb_pool(dl
);
4104 } else if (dl_argv_match(dl
, "port")) {
4106 return cmd_sb_port(dl
);
4107 } else if (dl_argv_match(dl
, "tc")) {
4109 return cmd_sb_tc(dl
);
4110 } else if (dl_argv_match(dl
, "occupancy")) {
4112 return cmd_sb_occ(dl
);
4114 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4118 static const char *cmd_name(uint8_t cmd
)
4121 case DEVLINK_CMD_UNSPEC
: return "unspec";
4122 case DEVLINK_CMD_GET
: return "get";
4123 case DEVLINK_CMD_SET
: return "set";
4124 case DEVLINK_CMD_NEW
: return "new";
4125 case DEVLINK_CMD_DEL
: return "del";
4126 case DEVLINK_CMD_PORT_GET
: return "get";
4127 case DEVLINK_CMD_PORT_SET
: return "set";
4128 case DEVLINK_CMD_PORT_NEW
: return "new";
4129 case DEVLINK_CMD_PORT_DEL
: return "del";
4130 case DEVLINK_CMD_PARAM_GET
: return "get";
4131 case DEVLINK_CMD_PARAM_SET
: return "set";
4132 case DEVLINK_CMD_PARAM_NEW
: return "new";
4133 case DEVLINK_CMD_PARAM_DEL
: return "del";
4134 case DEVLINK_CMD_REGION_GET
: return "get";
4135 case DEVLINK_CMD_REGION_SET
: return "set";
4136 case DEVLINK_CMD_REGION_NEW
: return "new";
4137 case DEVLINK_CMD_REGION_DEL
: return "del";
4138 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4139 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4140 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4141 case DEVLINK_CMD_TRAP_GET
: return "get";
4142 case DEVLINK_CMD_TRAP_SET
: return "set";
4143 case DEVLINK_CMD_TRAP_NEW
: return "new";
4144 case DEVLINK_CMD_TRAP_DEL
: return "del";
4145 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4146 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4147 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4148 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4149 default: return "<unknown cmd>";
4153 static const char *cmd_obj(uint8_t cmd
)
4156 case DEVLINK_CMD_UNSPEC
: return "unspec";
4157 case DEVLINK_CMD_GET
:
4158 case DEVLINK_CMD_SET
:
4159 case DEVLINK_CMD_NEW
:
4160 case DEVLINK_CMD_DEL
:
4162 case DEVLINK_CMD_PORT_GET
:
4163 case DEVLINK_CMD_PORT_SET
:
4164 case DEVLINK_CMD_PORT_NEW
:
4165 case DEVLINK_CMD_PORT_DEL
:
4167 case DEVLINK_CMD_PARAM_GET
:
4168 case DEVLINK_CMD_PARAM_SET
:
4169 case DEVLINK_CMD_PARAM_NEW
:
4170 case DEVLINK_CMD_PARAM_DEL
:
4172 case DEVLINK_CMD_REGION_GET
:
4173 case DEVLINK_CMD_REGION_SET
:
4174 case DEVLINK_CMD_REGION_NEW
:
4175 case DEVLINK_CMD_REGION_DEL
:
4177 case DEVLINK_CMD_FLASH_UPDATE
:
4178 case DEVLINK_CMD_FLASH_UPDATE_END
:
4179 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4181 case DEVLINK_CMD_TRAP_GET
:
4182 case DEVLINK_CMD_TRAP_SET
:
4183 case DEVLINK_CMD_TRAP_NEW
:
4184 case DEVLINK_CMD_TRAP_DEL
:
4186 case DEVLINK_CMD_TRAP_GROUP_GET
:
4187 case DEVLINK_CMD_TRAP_GROUP_SET
:
4188 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4189 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4190 return "trap-group";
4191 default: return "<unknown obj>";
4195 static void pr_out_mon_header(uint8_t cmd
)
4197 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4200 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4202 const char *obj
= cmd_obj(cmd
);
4203 unsigned int index
= 0;
4204 const char *cur_obj
;
4208 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4209 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4215 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4217 __pr_out_handle_start(dl
, tb
, true, false);
4219 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4220 check_indent_newline(dl
);
4221 print_string(PRINT_ANY
, "msg", "msg %s",
4222 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4224 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4225 check_indent_newline(dl
);
4226 print_string(PRINT_ANY
, "component", "component %s",
4227 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4230 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4231 pr_out_u64(dl
, "done",
4232 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4234 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4235 pr_out_u64(dl
, "total",
4236 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4238 pr_out_handle_end(dl
);
4241 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4242 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4243 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4245 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4247 struct dl
*dl
= data
;
4248 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4249 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4250 uint8_t cmd
= genl
->cmd
;
4252 if (!cmd_filter_check(dl
, cmd
))
4256 case DEVLINK_CMD_GET
: /* fall through */
4257 case DEVLINK_CMD_SET
: /* fall through */
4258 case DEVLINK_CMD_NEW
: /* fall through */
4259 case DEVLINK_CMD_DEL
:
4260 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4261 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4262 return MNL_CB_ERROR
;
4263 pr_out_mon_header(genl
->cmd
);
4264 pr_out_handle(dl
, tb
);
4266 case DEVLINK_CMD_PORT_GET
: /* fall through */
4267 case DEVLINK_CMD_PORT_SET
: /* fall through */
4268 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4269 case DEVLINK_CMD_PORT_DEL
:
4270 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4271 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4272 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4273 return MNL_CB_ERROR
;
4274 pr_out_mon_header(genl
->cmd
);
4275 pr_out_port(dl
, tb
);
4277 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4278 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4279 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4280 case DEVLINK_CMD_PARAM_DEL
:
4281 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4282 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4283 !tb
[DEVLINK_ATTR_PARAM
])
4284 return MNL_CB_ERROR
;
4285 pr_out_mon_header(genl
->cmd
);
4286 pr_out_param(dl
, tb
, false);
4288 case DEVLINK_CMD_REGION_GET
: /* fall through */
4289 case DEVLINK_CMD_REGION_SET
: /* fall through */
4290 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4291 case DEVLINK_CMD_REGION_DEL
:
4292 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4293 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4294 !tb
[DEVLINK_ATTR_REGION_NAME
])
4295 return MNL_CB_ERROR
;
4296 pr_out_mon_header(genl
->cmd
);
4297 pr_out_region(dl
, tb
);
4299 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4300 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4301 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4302 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4303 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4304 return MNL_CB_ERROR
;
4305 pr_out_mon_header(genl
->cmd
);
4306 pr_out_flash_update(dl
, tb
);
4308 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4309 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4310 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4311 case DEVLINK_CMD_TRAP_DEL
:
4312 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4313 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4314 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4315 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4316 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4317 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4318 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4319 !tb
[DEVLINK_ATTR_STATS
])
4320 return MNL_CB_ERROR
;
4321 pr_out_mon_header(genl
->cmd
);
4322 pr_out_trap(dl
, tb
, false);
4324 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4325 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4326 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4327 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4328 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4329 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4330 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4331 !tb
[DEVLINK_ATTR_STATS
])
4332 return MNL_CB_ERROR
;
4333 pr_out_mon_header(genl
->cmd
);
4334 pr_out_trap_group(dl
, tb
, false);
4340 static int cmd_mon_show(struct dl
*dl
)
4343 unsigned int index
= 0;
4344 const char *cur_obj
;
4346 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4347 if (strcmp(cur_obj
, "all") != 0 &&
4348 strcmp(cur_obj
, "dev") != 0 &&
4349 strcmp(cur_obj
, "port") != 0 &&
4350 strcmp(cur_obj
, "trap") != 0 &&
4351 strcmp(cur_obj
, "trap-group") != 0) {
4352 pr_err("Unknown object \"%s\"\n", cur_obj
);
4356 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4359 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4365 static void cmd_mon_help(void)
4367 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4368 "where OBJECT-LIST := { dev | port | trap | trap-group }\n");
4371 static int cmd_mon(struct dl
*dl
)
4373 if (dl_argv_match(dl
, "help")) {
4377 return cmd_mon_show(dl
);
4380 struct dpipe_field
{
4383 unsigned int bitwidth
;
4384 enum devlink_dpipe_field_mapping_type mapping_type
;
4387 struct dpipe_header
{
4388 struct list_head list
;
4391 struct dpipe_field
*fields
;
4392 unsigned int fields_count
;
4395 struct dpipe_table
{
4396 struct list_head list
;
4398 unsigned int resource_id
;
4399 bool resource_valid
;
4402 struct dpipe_tables
{
4403 struct list_head table_list
;
4413 enum devlink_resource_unit unit
;
4418 struct list_head list
;
4419 struct list_head resource_list
;
4420 struct resource
*parent
;
4424 struct list_head resource_list
;
4427 struct resource_ctx
{
4430 struct resources
*resources
;
4431 struct dpipe_tables
*tables
;
4432 bool print_resources
;
4433 bool pending_change
;
4436 static struct resource
*resource_alloc(void)
4438 struct resource
*resource
;
4440 resource
= calloc(1, sizeof(struct resource
));
4443 INIT_LIST_HEAD(&resource
->resource_list
);
4447 static void resource_free(struct resource
*resource
)
4449 struct resource
*child_resource
, *tmp
;
4451 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4453 free(child_resource
->name
);
4454 resource_free(child_resource
);
4459 static struct resources
*resources_alloc(void)
4461 struct resources
*resources
;
4463 resources
= calloc(1, sizeof(struct resources
));
4466 INIT_LIST_HEAD(&resources
->resource_list
);
4470 static void resources_free(struct resources
*resources
)
4472 struct resource
*resource
, *tmp
;
4474 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4475 resource_free(resource
);
4478 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4480 ctx
->resources
= resources_alloc();
4481 if (!ctx
->resources
)
4487 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4489 resources_free(ctx
->resources
);
4495 struct list_head global_headers
;
4496 struct list_head local_headers
;
4497 struct dpipe_tables
*tables
;
4498 struct resources
*resources
;
4503 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4505 struct dpipe_header
*header
;
4507 header
= calloc(1, sizeof(struct dpipe_header
));
4510 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4511 if (!header
->fields
)
4512 goto err_fields_alloc
;
4513 header
->fields_count
= fields_count
;
4521 static void dpipe_header_free(struct dpipe_header
*header
)
4523 free(header
->fields
);
4527 static void dpipe_header_clear(struct dpipe_header
*header
)
4529 struct dpipe_field
*field
;
4532 for (i
= 0; i
< header
->fields_count
; i
++) {
4533 field
= &header
->fields
[i
];
4539 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4540 struct dpipe_header
*header
, bool global
)
4543 list_add(&header
->list
, &ctx
->global_headers
);
4545 list_add(&header
->list
, &ctx
->local_headers
);
4548 static void dpipe_header_del(struct dpipe_header
*header
)
4550 list_del(&header
->list
);
4553 static struct dpipe_table
*dpipe_table_alloc(void)
4555 return calloc(1, sizeof(struct dpipe_table
));
4558 static void dpipe_table_free(struct dpipe_table
*table
)
4563 static struct dpipe_tables
*dpipe_tables_alloc(void)
4565 struct dpipe_tables
*tables
;
4567 tables
= calloc(1, sizeof(struct dpipe_tables
));
4570 INIT_LIST_HEAD(&tables
->table_list
);
4574 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4576 struct dpipe_table
*table
, *tmp
;
4578 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4579 dpipe_table_free(table
);
4583 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4585 ctx
->tables
= dpipe_tables_alloc();
4590 INIT_LIST_HEAD(&ctx
->global_headers
);
4591 INIT_LIST_HEAD(&ctx
->local_headers
);
4595 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4597 struct dpipe_header
*header
, *tmp
;
4599 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4601 dpipe_header_del(header
);
4602 dpipe_header_clear(header
);
4603 dpipe_header_free(header
);
4605 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4607 dpipe_header_del(header
);
4608 dpipe_header_clear(header
);
4609 dpipe_header_free(header
);
4611 dpipe_tables_free(ctx
->tables
);
4614 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4615 uint32_t header_id
, bool global
)
4617 struct list_head
*header_list
;
4618 struct dpipe_header
*header
;
4621 header_list
= &ctx
->global_headers
;
4623 header_list
= &ctx
->local_headers
;
4624 list_for_each_entry(header
, header_list
, list
) {
4625 if (header
->id
!= header_id
)
4627 return header
->name
;
4632 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4634 uint32_t field_id
, bool global
)
4636 struct list_head
*header_list
;
4637 struct dpipe_header
*header
;
4640 header_list
= &ctx
->global_headers
;
4642 header_list
= &ctx
->local_headers
;
4643 list_for_each_entry(header
, header_list
, list
) {
4644 if (header
->id
!= header_id
)
4646 return header
->fields
[field_id
].name
;
4652 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4654 switch (mapping_type
) {
4655 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4657 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4665 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4666 uint32_t field_id
, bool global
)
4668 enum devlink_dpipe_field_mapping_type mapping_type
;
4669 struct list_head
*header_list
;
4670 struct dpipe_header
*header
;
4673 header_list
= &ctx
->global_headers
;
4675 header_list
= &ctx
->local_headers
;
4676 list_for_each_entry(header
, header_list
, list
) {
4677 if (header
->id
!= header_id
)
4679 mapping_type
= header
->fields
[field_id
].mapping_type
;
4680 return dpipe_field_mapping_e2s(mapping_type
);
4685 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4686 struct dpipe_field
*fields
,
4687 unsigned int field_count
)
4689 struct dpipe_field
*field
;
4692 for (i
= 0; i
< field_count
; i
++) {
4694 pr_out_entry_start(ctx
->dl
);
4695 check_indent_newline(ctx
->dl
);
4696 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4697 if (ctx
->dl
->verbose
)
4698 pr_out_uint(ctx
->dl
, "id", field
->id
);
4699 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4700 if (field
->mapping_type
) {
4701 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4702 dpipe_field_mapping_e2s(field
->mapping_type
));
4704 pr_out_entry_end(ctx
->dl
);
4709 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4710 struct dpipe_header
*header
, bool global
)
4712 pr_out_handle_start_arr(ctx
->dl
, tb
);
4713 check_indent_newline(ctx
->dl
);
4714 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4715 if (ctx
->dl
->verbose
) {
4716 pr_out_uint(ctx
->dl
, "id", header
->id
);
4717 print_bool(PRINT_ANY
, "global", " global %s", global
);
4719 pr_out_array_start(ctx
->dl
, "field");
4720 pr_out_dpipe_fields(ctx
, header
->fields
,
4721 header
->fields_count
);
4722 pr_out_array_end(ctx
->dl
);
4723 pr_out_handle_end(ctx
->dl
);
4726 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4729 struct dpipe_header
*header
;
4731 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4732 pr_out_dpipe_header(ctx
, tb
, header
, false);
4734 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4735 pr_out_dpipe_header(ctx
, tb
, header
, true);
4738 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4740 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4744 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4745 if (err
!= MNL_CB_OK
)
4747 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4748 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4749 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4750 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4753 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4754 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4755 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4756 field
->name
= strdup(name
);
4759 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4763 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4764 struct dpipe_field
*fields
)
4766 struct nlattr
*nla_field
;
4770 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4771 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4779 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4781 struct nlattr
*nla_field
;
4782 unsigned int count
= 0;
4784 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4789 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4791 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4792 struct dpipe_header
*header
;
4793 unsigned int fields_count
;
4794 const char *header_name
;
4798 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4799 if (err
!= MNL_CB_OK
)
4802 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4803 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4804 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4807 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4808 header
= dpipe_header_alloc(fields_count
);
4812 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4813 header
->name
= strdup(header_name
);
4814 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4815 header
->fields_count
= fields_count
;
4816 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4818 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4822 dpipe_header_add(ctx
, header
, global
);
4826 dpipe_header_free(header
);
4830 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4832 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4833 struct nlattr
*nla_header
;
4836 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4837 err
= dpipe_header_get(ctx
, nla_header
);
4844 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4846 struct dpipe_ctx
*ctx
= data
;
4847 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4848 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4851 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4852 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4853 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4854 return MNL_CB_ERROR
;
4855 err
= dpipe_headers_get(ctx
, tb
);
4858 return MNL_CB_ERROR
;
4861 if (ctx
->print_headers
)
4862 pr_out_dpipe_headers(ctx
, tb
);
4866 static int cmd_dpipe_headers_show(struct dl
*dl
)
4868 struct nlmsghdr
*nlh
;
4869 struct dpipe_ctx ctx
= {};
4870 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4873 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4875 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4879 err
= dpipe_ctx_init(&ctx
, dl
);
4883 ctx
.print_headers
= true;
4885 pr_out_section_start(dl
, "header");
4886 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4888 pr_err("error get headers %s\n", strerror(ctx
.err
));
4889 pr_out_section_end(dl
);
4891 dpipe_ctx_fini(&ctx
);
4895 static void cmd_dpipe_header_help(void)
4897 pr_err("Usage: devlink dpipe headers show DEV\n");
4900 static int cmd_dpipe_header(struct dl
*dl
)
4902 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4903 cmd_dpipe_header_help();
4905 } else if (dl_argv_match(dl
, "show")) {
4907 return cmd_dpipe_headers_show(dl
);
4909 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4914 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4916 switch (action_type
) {
4917 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4918 return "field_modify";
4924 struct dpipe_op_info
{
4930 struct dpipe_action
{
4931 struct dpipe_op_info info
;
4935 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4936 struct dpipe_ctx
*ctx
)
4938 struct dpipe_op_info
*op_info
= &action
->info
;
4939 const char *mapping
;
4941 check_indent_newline(ctx
->dl
);
4942 print_string(PRINT_ANY
, "type", "type %s",
4943 dpipe_action_type_e2s(action
->type
));
4944 print_string(PRINT_ANY
, "header", " header %s",
4945 dpipe_header_id2s(ctx
, op_info
->header_id
,
4946 op_info
->header_global
));
4947 print_string(PRINT_ANY
, "field", " field %s",
4948 dpipe_field_id2s(ctx
, op_info
->header_id
,
4950 op_info
->header_global
));
4951 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4953 op_info
->header_global
);
4955 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
4958 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4960 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4963 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4964 if (err
!= MNL_CB_OK
)
4967 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4968 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4969 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4970 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4974 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4975 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4976 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4977 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4982 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4983 struct nlattr
*nla_actions
)
4985 struct nlattr
*nla_action
;
4986 struct dpipe_action action
;
4988 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4989 pr_out_entry_start(ctx
->dl
);
4990 if (dpipe_action_parse(&action
, nla_action
))
4991 goto err_action_parse
;
4992 pr_out_dpipe_action(&action
, ctx
);
4993 pr_out_entry_end(ctx
->dl
);
4998 pr_out_entry_end(ctx
->dl
);
5003 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5005 switch (match_type
) {
5006 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5007 return "field_exact";
5013 struct dpipe_match
{
5014 struct dpipe_op_info info
;
5018 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5019 struct dpipe_ctx
*ctx
)
5021 struct dpipe_op_info
*op_info
= &match
->info
;
5022 const char *mapping
;
5024 check_indent_newline(ctx
->dl
);
5025 print_string(PRINT_ANY
, "type", "type %s",
5026 dpipe_match_type_e2s(match
->type
));
5027 print_string(PRINT_ANY
, "header", " header %s",
5028 dpipe_header_id2s(ctx
, op_info
->header_id
,
5029 op_info
->header_global
));
5030 print_string(PRINT_ANY
, "field", " field %s",
5031 dpipe_field_id2s(ctx
, op_info
->header_id
,
5033 op_info
->header_global
));
5034 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5036 op_info
->header_global
);
5038 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5041 static int dpipe_match_parse(struct dpipe_match
*match
,
5045 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5048 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5049 if (err
!= MNL_CB_OK
)
5052 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5053 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5054 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5055 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5059 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5060 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5061 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5062 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5067 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5068 struct nlattr
*nla_matches
)
5070 struct nlattr
*nla_match
;
5071 struct dpipe_match match
;
5073 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5074 pr_out_entry_start(ctx
->dl
);
5075 if (dpipe_match_parse(&match
, nla_match
))
5076 goto err_match_parse
;
5077 pr_out_dpipe_match(&match
, ctx
);
5078 pr_out_entry_end(ctx
->dl
);
5083 pr_out_entry_end(ctx
->dl
);
5087 static struct resource
*
5088 resource_find(struct resources
*resources
, struct resource
*resource
,
5089 uint64_t resource_id
)
5091 struct list_head
*list_head
;
5094 list_head
= &resources
->resource_list
;
5096 list_head
= &resource
->resource_list
;
5098 list_for_each_entry(resource
, list_head
, list
) {
5099 struct resource
*child_resource
;
5101 if (resource
->id
== resource_id
)
5104 child_resource
= resource_find(resources
, resource
,
5107 return child_resource
;
5113 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5114 uint64_t resource_id
)
5116 struct resource
*resource
, *parent_resource
;
5117 const char del
[] = "/";
5121 resource
= resource_find(resources
, NULL
, resource_id
);
5125 for (parent_resource
= resource
; parent_resource
;
5126 parent_resource
= parent_resource
->parent
)
5127 path_len
+= strlen(parent_resource
->name
) + 1;
5130 path
= calloc(1, path_len
);
5134 path
+= path_len
- 1;
5135 for (parent_resource
= resource
; parent_resource
;
5136 parent_resource
= parent_resource
->parent
) {
5137 path
-= strlen(parent_resource
->name
);
5138 memcpy(path
, parent_resource
->name
,
5139 strlen(parent_resource
->name
));
5140 path
-= strlen(del
);
5141 memcpy(path
, del
, strlen(del
));
5143 check_indent_newline(dl
);
5144 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5148 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5150 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5151 struct dpipe_table
*table
;
5152 uint32_t resource_units
;
5153 bool counters_enabled
;
5154 bool resource_valid
;
5158 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5159 if (err
!= MNL_CB_OK
)
5162 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5163 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5164 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5165 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5166 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5170 table
= dpipe_table_alloc();
5174 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5175 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5176 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5178 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5180 if (resource_valid
) {
5181 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5182 table
->resource_valid
= true;
5185 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5186 if (!ctx
->print_tables
)
5189 check_indent_newline(ctx
->dl
);
5190 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5191 pr_out_uint(ctx
->dl
, "size", size
);
5192 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5194 if (resource_valid
) {
5195 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5196 resource_path_print(ctx
->dl
, ctx
->resources
,
5197 table
->resource_id
);
5198 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
5201 pr_out_array_start(ctx
->dl
, "match");
5202 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5203 goto err_matches_show
;
5204 pr_out_array_end(ctx
->dl
);
5206 pr_out_array_start(ctx
->dl
, "action");
5207 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5208 goto err_actions_show
;
5209 pr_out_array_end(ctx
->dl
);
5215 pr_out_array_end(ctx
->dl
);
5219 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5221 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5222 struct nlattr
*nla_table
;
5224 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5225 if (ctx
->print_tables
)
5226 pr_out_handle_start_arr(ctx
->dl
, tb
);
5227 if (dpipe_table_show(ctx
, nla_table
))
5228 goto err_table_show
;
5229 if (ctx
->print_tables
)
5230 pr_out_handle_end(ctx
->dl
);
5235 if (ctx
->print_tables
)
5236 pr_out_handle_end(ctx
->dl
);
5240 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5242 struct dpipe_ctx
*ctx
= data
;
5243 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5244 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5246 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5247 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5248 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5249 return MNL_CB_ERROR
;
5251 if (dpipe_tables_show(ctx
, tb
))
5252 return MNL_CB_ERROR
;
5256 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5258 static int cmd_dpipe_table_show(struct dl
*dl
)
5260 struct nlmsghdr
*nlh
;
5261 struct dpipe_ctx dpipe_ctx
= {};
5262 struct resource_ctx resource_ctx
= {};
5263 uint16_t flags
= NLM_F_REQUEST
;
5266 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5270 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5272 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5276 dpipe_ctx
.print_tables
= true;
5278 dl_opts_put(nlh
, dl
);
5279 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5282 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5283 goto err_headers_get
;
5286 err
= resource_ctx_init(&resource_ctx
, dl
);
5288 goto err_resource_ctx_init
;
5290 resource_ctx
.print_resources
= false;
5291 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5292 dl_opts_put(nlh
, dl
);
5293 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5296 dpipe_ctx
.resources
= resource_ctx
.resources
;
5298 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5299 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5300 dl_opts_put(nlh
, dl
);
5302 pr_out_section_start(dl
, "table");
5303 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5304 pr_out_section_end(dl
);
5306 resource_ctx_fini(&resource_ctx
);
5307 dpipe_ctx_fini(&dpipe_ctx
);
5310 err_resource_ctx_init
:
5312 dpipe_ctx_fini(&dpipe_ctx
);
5316 static int cmd_dpipe_table_set(struct dl
*dl
)
5318 struct nlmsghdr
*nlh
;
5321 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5322 NLM_F_REQUEST
| NLM_F_ACK
);
5324 err
= dl_argv_parse_put(nlh
, dl
,
5325 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5326 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5330 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5333 enum dpipe_value_type
{
5334 DPIPE_VALUE_TYPE_VALUE
,
5335 DPIPE_VALUE_TYPE_MASK
,
5339 dpipe_value_type_e2s(enum dpipe_value_type type
)
5342 case DPIPE_VALUE_TYPE_VALUE
:
5344 case DPIPE_VALUE_TYPE_MASK
:
5345 return "value_mask";
5351 struct dpipe_field_printer
{
5352 unsigned int field_id
;
5353 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5356 struct dpipe_header_printer
{
5357 struct dpipe_field_printer
*printers
;
5358 unsigned int printers_count
;
5359 unsigned int header_id
;
5362 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5363 enum dpipe_value_type type
,
5366 struct in_addr ip_addr
;
5368 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5369 check_indent_newline(ctx
->dl
);
5370 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5374 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5375 enum dpipe_value_type type
,
5378 check_indent_newline(ctx
->dl
);
5379 print_string_name_value(dpipe_value_type_e2s(type
),
5380 ether_ntoa((struct ether_addr
*)value
));
5383 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5384 enum dpipe_value_type type
,
5387 char str
[INET6_ADDRSTRLEN
];
5389 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5390 check_indent_newline(ctx
->dl
);
5391 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5394 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5396 .printer
= dpipe_field_printer_ipv4_addr
,
5397 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5401 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5402 .printers
= dpipe_field_printers_ipv4
,
5403 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5404 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5407 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5409 .printer
= dpipe_field_printer_ethernet_addr
,
5410 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5414 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5415 .printers
= dpipe_field_printers_ethernet
,
5416 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5417 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5420 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5422 .printer
= dpipe_field_printer_ipv6_addr
,
5423 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5427 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5428 .printers
= dpipe_field_printers_ipv6
,
5429 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5430 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5433 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5434 &dpipe_header_printer_ipv4
,
5435 &dpipe_header_printer_ethernet
,
5436 &dpipe_header_printer_ipv6
,
5439 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5440 struct dpipe_op_info
*info
,
5441 enum dpipe_value_type type
,
5444 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5445 struct dpipe_header_printer
*header_printer
;
5446 struct dpipe_field_printer
*field_printer
;
5447 unsigned int field_printers_count
;
5451 for (i
= 0; i
< header_printers_count
; i
++) {
5452 header_printer
= dpipe_header_printers
[i
];
5453 if (header_printer
->header_id
!= info
->header_id
)
5455 field_printers_count
= header_printer
->printers_count
;
5456 for (j
= 0; j
< field_printers_count
; j
++) {
5457 field_printer
= &header_printer
->printers
[j
];
5458 if (field_printer
->field_id
!= info
->field_id
)
5460 field_printer
->printer(ctx
, type
, value
);
5468 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5470 unsigned int value_len
,
5471 struct dpipe_op_info
*info
,
5472 enum dpipe_value_type type
)
5474 if (info
->header_global
&&
5475 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5478 if (value_len
== sizeof(uint32_t)) {
5479 uint32_t *value_32
= value
;
5481 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
5485 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5486 struct nlattr
**nla_match_value
,
5487 struct dpipe_op_info
*info
)
5489 void *value
, *value_mask
;
5490 uint32_t value_mapping
;
5494 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5495 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5497 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5498 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5501 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5502 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
5506 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5507 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5508 DPIPE_VALUE_TYPE_MASK
);
5511 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5514 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5517 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5518 struct dpipe_match match
;
5521 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5522 if (err
!= MNL_CB_OK
)
5525 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5526 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5530 pr_out_entry_start(ctx
->dl
);
5531 if (dpipe_match_parse(&match
,
5532 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5533 goto err_match_parse
;
5534 pr_out_dpipe_match(&match
, ctx
);
5535 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5536 pr_out_entry_end(ctx
->dl
);
5541 pr_out_entry_end(ctx
->dl
);
5545 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5548 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5549 struct dpipe_action action
;
5552 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5553 if (err
!= MNL_CB_OK
)
5556 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5557 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5561 pr_out_entry_start(ctx
->dl
);
5562 if (dpipe_action_parse(&action
,
5563 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5564 goto err_action_parse
;
5565 pr_out_dpipe_action(&action
, ctx
);
5566 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5567 pr_out_entry_end(ctx
->dl
);
5572 pr_out_entry_end(ctx
->dl
);
5577 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5578 struct nlattr
*nla_action_values
)
5580 struct nlattr
*nla_action_value
;
5582 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5583 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5590 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5591 struct nlattr
*nla_match_values
)
5593 struct nlattr
*nla_match_value
;
5595 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5596 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5602 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5604 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5605 uint32_t entry_index
;
5609 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5610 if (err
!= MNL_CB_OK
)
5613 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5614 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5615 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5619 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5620 pr_out_uint(ctx
->dl
, "index", entry_index
);
5622 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5623 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5624 pr_out_uint(ctx
->dl
, "counter", counter
);
5627 pr_out_array_start(ctx
->dl
, "match_value");
5628 if (dpipe_tables_match_values_show(ctx
,
5629 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5630 goto err_match_values_show
;
5631 pr_out_array_end(ctx
->dl
);
5633 pr_out_array_start(ctx
->dl
, "action_value");
5634 if (dpipe_tables_action_values_show(ctx
,
5635 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5636 goto err_action_values_show
;
5637 pr_out_array_end(ctx
->dl
);
5640 err_action_values_show
:
5641 err_match_values_show
:
5642 pr_out_array_end(ctx
->dl
);
5646 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5648 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5649 struct nlattr
*nla_entry
;
5651 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5652 pr_out_handle_start_arr(ctx
->dl
, tb
);
5653 if (dpipe_entry_show(ctx
, nla_entry
))
5654 goto err_entry_show
;
5655 pr_out_handle_end(ctx
->dl
);
5660 pr_out_handle_end(ctx
->dl
);
5664 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5666 struct dpipe_ctx
*ctx
= data
;
5667 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5668 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5670 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5671 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5672 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5673 return MNL_CB_ERROR
;
5675 if (dpipe_table_entries_show(ctx
, tb
))
5676 return MNL_CB_ERROR
;
5680 static int cmd_dpipe_table_dump(struct dl
*dl
)
5682 struct nlmsghdr
*nlh
;
5683 struct dpipe_ctx ctx
= {};
5684 uint16_t flags
= NLM_F_REQUEST
;
5687 err
= dpipe_ctx_init(&ctx
, dl
);
5691 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5695 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5696 dl_opts_put(nlh
, dl
);
5697 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5699 pr_err("error get headers %s\n", strerror(ctx
.err
));
5703 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5704 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5705 dl_opts_put(nlh
, dl
);
5707 pr_out_section_start(dl
, "table_entry");
5708 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5709 pr_out_section_end(dl
);
5711 dpipe_ctx_fini(&ctx
);
5715 static void cmd_dpipe_table_help(void)
5717 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5718 "where OBJECT-LIST := { show | set | dump }\n");
5721 static int cmd_dpipe_table(struct dl
*dl
)
5723 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5724 cmd_dpipe_table_help();
5726 } else if (dl_argv_match(dl
, "show")) {
5728 return cmd_dpipe_table_show(dl
);
5729 } else if (dl_argv_match(dl
, "set")) {
5731 return cmd_dpipe_table_set(dl
);
5732 } else if (dl_argv_match(dl
, "dump")) {
5734 return cmd_dpipe_table_dump(dl
);
5736 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5740 static void cmd_dpipe_help(void)
5742 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5743 "where OBJECT-LIST := { header | table }\n");
5746 static int cmd_dpipe(struct dl
*dl
)
5748 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5751 } else if (dl_argv_match(dl
, "header")) {
5753 return cmd_dpipe_header(dl
);
5754 } else if (dl_argv_match(dl
, "table")) {
5756 return cmd_dpipe_table(dl
);
5758 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5763 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5764 struct nlattr
**nla_resource
)
5766 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5767 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5768 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5769 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5770 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5771 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5772 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5776 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5777 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5778 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5779 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5780 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5781 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5782 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5784 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5785 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5787 resource
->size_new
= resource
->size
;
5789 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5790 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5791 resource
->occ_valid
= true;
5794 if (resource
->size_new
!= resource
->size
)
5795 ctx
->pending_change
= true;
5801 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5802 struct resource
*parent_resource
, struct nlattr
*nl
)
5804 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5805 struct nlattr
*nla_child_resource
;
5806 struct nlattr
*nla_resources
;
5816 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5817 if (err
!= MNL_CB_OK
)
5820 err
= resource_parse(ctx
, resource
, nla_resource
);
5824 resource
->parent
= parent_resource
;
5825 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5828 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5829 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5831 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5832 struct resource
*child_resource
;
5833 struct list_head
*list
;
5835 child_resource
= resource_alloc();
5836 if (!child_resource
)
5840 list
= &ctx
->resources
->resource_list
;
5842 list
= &resource
->resource_list
;
5844 list_add_tail(&child_resource
->list
, list
);
5845 err
= resource_get(ctx
, child_resource
, resource
,
5846 nla_child_resource
);
5854 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5857 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5858 default: return "<unknown unit>";
5862 static void resource_show(struct resource
*resource
,
5863 struct resource_ctx
*ctx
)
5865 struct resource
*child_resource
;
5866 struct dpipe_table
*table
;
5867 struct dl
*dl
= ctx
->dl
;
5870 check_indent_newline(dl
);
5871 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
5873 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5874 pr_out_u64(dl
, "size", resource
->size
);
5875 if (resource
->size
!= resource
->size_new
)
5876 pr_out_u64(dl
, "size_new", resource
->size_new
);
5877 if (resource
->occ_valid
)
5878 pr_out_uint(dl
, "occ", resource
->size_occ
);
5879 print_string(PRINT_ANY
, "unit", " unit %s",
5880 resource_unit_str_get(resource
->unit
));
5882 if (resource
->size_min
!= resource
->size_max
) {
5883 pr_out_uint(dl
, "size_min", resource
->size_min
);
5884 pr_out_u64(dl
, "size_max", resource
->size_max
);
5885 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5888 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5889 if (table
->resource_id
== resource
->id
&&
5890 table
->resource_valid
)
5894 pr_out_array_start(dl
, "dpipe_tables");
5896 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
5899 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5900 if (table
->resource_id
!= resource
->id
||
5901 !table
->resource_valid
)
5903 pr_out_entry_start(dl
);
5904 check_indent_newline(dl
);
5905 print_string(PRINT_ANY
, "table_name", "table_name %s",
5907 pr_out_entry_end(dl
);
5910 pr_out_array_end(dl
);
5912 if (list_empty(&resource
->resource_list
))
5915 if (ctx
->pending_change
) {
5916 check_indent_newline(dl
);
5917 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
5918 resource
->size_valid
? "true" : "false");
5920 pr_out_array_start(dl
, "resources");
5921 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5922 pr_out_entry_start(dl
);
5923 resource_show(child_resource
, ctx
);
5924 pr_out_entry_end(dl
);
5926 pr_out_array_end(dl
);
5930 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5932 struct resources
*resources
= ctx
->resources
;
5933 struct resource
*resource
;
5935 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5936 pr_out_handle_start_arr(ctx
->dl
, tb
);
5937 resource_show(resource
, ctx
);
5938 pr_out_handle_end(ctx
->dl
);
5942 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5944 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5947 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5949 struct resource_ctx
*ctx
= data
;
5950 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5951 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5954 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5955 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5956 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5957 return MNL_CB_ERROR
;
5959 err
= resources_get(ctx
, tb
);
5962 return MNL_CB_ERROR
;
5965 if (ctx
->print_resources
)
5966 resources_show(ctx
, tb
);
5971 static int cmd_resource_show(struct dl
*dl
)
5973 struct nlmsghdr
*nlh
;
5974 struct dpipe_ctx dpipe_ctx
= {};
5975 struct resource_ctx resource_ctx
= {};
5978 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5982 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5984 dl_opts_put(nlh
, dl
);
5986 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5990 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5993 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5997 err
= resource_ctx_init(&resource_ctx
, dl
);
6001 resource_ctx
.print_resources
= true;
6002 resource_ctx
.tables
= dpipe_ctx
.tables
;
6003 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6004 NLM_F_REQUEST
| NLM_F_ACK
);
6005 dl_opts_put(nlh
, dl
);
6006 pr_out_section_start(dl
, "resources");
6007 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6009 pr_out_section_end(dl
);
6010 resource_ctx_fini(&resource_ctx
);
6012 dpipe_ctx_fini(&dpipe_ctx
);
6016 static void cmd_resource_help(void)
6018 pr_err("Usage: devlink resource show DEV\n"
6019 " devlink resource set DEV path PATH size SIZE\n");
6022 static struct resource
*
6023 resource_find_by_name(struct list_head
*list
, char *name
)
6025 struct resource
*resource
;
6027 list_for_each_entry(resource
, list
, list
) {
6028 if (!strcmp(resource
->name
, name
))
6035 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6036 uint32_t *p_resource_id
, bool *p_resource_valid
)
6038 struct resource
*resource
;
6039 uint32_t resource_id
= 0;
6040 char *resource_path_dup
;
6041 struct list_head
*list
;
6042 const char del
[] = "/";
6043 char *resource_name
;
6045 resource_path_dup
= strdup(resource_path
);
6046 list
= &ctx
->resources
->resource_list
;
6047 resource_name
= strtok(resource_path_dup
, del
);
6048 while (resource_name
!= NULL
) {
6049 resource
= resource_find_by_name(list
, resource_name
);
6051 goto err_resource_lookup
;
6053 list
= &resource
->resource_list
;
6054 resource_name
= strtok(NULL
, del
);
6055 resource_id
= resource
->id
;
6057 free(resource_path_dup
);
6058 *p_resource_valid
= true;
6059 *p_resource_id
= resource_id
;
6062 err_resource_lookup
:
6063 free(resource_path_dup
);
6067 static int cmd_resource_set(struct dl
*dl
)
6069 struct nlmsghdr
*nlh
;
6070 struct resource_ctx ctx
= {};
6073 err
= resource_ctx_init(&ctx
, dl
);
6077 ctx
.print_resources
= false;
6078 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6079 DL_OPT_RESOURCE_SIZE
, 0);
6083 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6085 dl_opts_put(nlh
, dl
);
6086 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6088 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6092 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6093 &dl
->opts
.resource_id
,
6094 &dl
->opts
.resource_id_valid
);
6096 pr_err("error parsing resource path %s\n", strerror(-err
));
6100 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6101 NLM_F_REQUEST
| NLM_F_ACK
);
6103 dl_opts_put(nlh
, dl
);
6104 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6106 resource_ctx_fini(&ctx
);
6110 static int cmd_resource(struct dl
*dl
)
6112 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6113 cmd_resource_help();
6115 } else if (dl_argv_match(dl
, "show")) {
6117 return cmd_resource_show(dl
);
6118 } else if (dl_argv_match(dl
, "set")) {
6120 return cmd_resource_set(dl
);
6122 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6126 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6128 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6129 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6130 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6133 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6134 if (dl
->json_output
)
6135 open_json_object(buf
);
6140 static void pr_out_region_handle_end(struct dl
*dl
)
6142 if (dl
->json_output
)
6143 close_json_object();
6148 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6150 __pr_out_indent_newline(dl
);
6151 if (dl
->json_output
)
6152 open_json_array(PRINT_JSON
, "snapshot");
6154 pr_out("snapshot %s", array
? "[" : "");
6157 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6159 if (dl
->json_output
)
6160 close_json_array(PRINT_JSON
, NULL
);
6165 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6167 uint32_t snapshot_id
;
6169 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6172 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6174 if (dl
->json_output
)
6175 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6177 pr_out("%s%u", index
? " " : "", snapshot_id
);
6180 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6182 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6183 struct nlattr
*nla_sanpshot
;
6186 pr_out_region_snapshots_start(dl
, true);
6187 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6188 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6189 if (err
!= MNL_CB_OK
)
6191 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6193 pr_out_region_snapshots_end(dl
, true);
6196 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6198 pr_out_region_snapshots_start(dl
, false);
6199 pr_out_region_snapshots_id(dl
, tb
, 0);
6200 pr_out_region_snapshots_end(dl
, false);
6203 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6205 pr_out_region_handle_start(dl
, tb
);
6207 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6208 pr_out_u64(dl
, "size",
6209 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6211 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6212 pr_out_snapshots(dl
, tb
);
6214 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6215 pr_out_snapshot(dl
, tb
);
6217 pr_out_region_handle_end(dl
);
6220 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6222 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6223 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6224 struct dl
*dl
= data
;
6226 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6227 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6228 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6229 return MNL_CB_ERROR
;
6231 pr_out_region(dl
, tb
);
6236 static int cmd_region_show(struct dl
*dl
)
6238 struct nlmsghdr
*nlh
;
6239 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6242 if (dl_argc(dl
) == 0)
6243 flags
|= NLM_F_DUMP
;
6245 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6247 if (dl_argc(dl
) > 0) {
6248 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6253 pr_out_section_start(dl
, "regions");
6254 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6255 pr_out_section_end(dl
);
6259 static int cmd_region_snapshot_del(struct dl
*dl
)
6261 struct nlmsghdr
*nlh
;
6264 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6265 NLM_F_REQUEST
| NLM_F_ACK
);
6267 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6268 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6272 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6275 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6277 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6278 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6279 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6280 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6281 struct dl
*dl
= data
;
6284 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6285 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6286 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6287 return MNL_CB_ERROR
;
6289 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6290 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6291 if (err
!= MNL_CB_OK
)
6292 return MNL_CB_ERROR
;
6294 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6295 if (!nla_chunk_data
)
6298 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6299 if (!nla_chunk_addr
)
6302 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6303 mnl_attr_get_payload_len(nla_chunk_data
),
6304 mnl_attr_get_u64(nla_chunk_addr
));
6309 static int cmd_region_dump(struct dl
*dl
)
6311 struct nlmsghdr
*nlh
;
6314 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6315 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6317 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6318 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6322 pr_out_section_start(dl
, "dump");
6323 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6324 pr_out_section_end(dl
);
6325 if (!dl
->json_output
)
6330 static int cmd_region_read(struct dl
*dl
)
6332 struct nlmsghdr
*nlh
;
6335 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6336 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6338 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6339 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6340 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6344 pr_out_section_start(dl
, "read");
6345 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6346 pr_out_section_end(dl
);
6347 if (!dl
->json_output
)
6352 static void cmd_region_help(void)
6354 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6355 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6356 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6357 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6360 static int cmd_region(struct dl
*dl
)
6362 if (dl_no_arg(dl
)) {
6363 return cmd_region_show(dl
);
6364 } else if (dl_argv_match(dl
, "help")) {
6367 } else if (dl_argv_match(dl
, "show")) {
6369 return cmd_region_show(dl
);
6370 } else if (dl_argv_match(dl
, "del")) {
6372 return cmd_region_snapshot_del(dl
);
6373 } else if (dl_argv_match(dl
, "dump")) {
6375 return cmd_region_dump(dl
);
6376 } else if (dl_argv_match(dl
, "read")) {
6378 return cmd_region_read(dl
);
6380 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6384 static int cmd_health_set_params(struct dl
*dl
)
6386 struct nlmsghdr
*nlh
;
6389 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6390 NLM_F_REQUEST
| NLM_F_ACK
);
6391 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6392 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6393 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6397 dl_opts_put(nlh
, dl
);
6398 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6401 static int cmd_health_dump_clear(struct dl
*dl
)
6403 struct nlmsghdr
*nlh
;
6406 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6407 NLM_F_REQUEST
| NLM_F_ACK
);
6409 err
= dl_argv_parse_put(nlh
, dl
,
6410 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6414 dl_opts_put(nlh
, dl
);
6415 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6418 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6423 check_indent_newline(dl
);
6426 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6429 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6432 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6435 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6438 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6440 case MNL_TYPE_NUL_STRING
:
6441 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6443 case MNL_TYPE_BINARY
:
6444 len
= mnl_attr_get_payload_len(nl_data
);
6445 data
= mnl_attr_get_payload(nl_data
);
6446 pr_out_binary_value(dl
, data
, len
);
6454 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6459 pr_out_name(dl
, *name
);
6466 struct list_head list
;
6469 struct fmsg_cb_data
{
6473 struct list_head entry_list
;
6476 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6477 uint8_t *attr_value
, bool insert
)
6479 struct nest_entry
*entry
;
6482 entry
= malloc(sizeof(struct nest_entry
));
6486 entry
->attr_type
= *attr_value
;
6487 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6489 if (list_empty(&fmsg_data
->entry_list
))
6490 return MNL_CB_ERROR
;
6491 entry
= list_first_entry(&fmsg_data
->entry_list
,
6492 struct nest_entry
, list
);
6493 *attr_value
= entry
->attr_type
;
6494 list_del(&entry
->list
);
6500 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6503 pr_out_fmsg_name(dl
, name
);
6505 __pr_out_indent_inc();
6508 static void pr_out_fmsg_group_end(struct dl
*dl
)
6511 __pr_out_indent_dec();
6514 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6516 if (dl
->json_output
) {
6517 pr_out_fmsg_name(dl
, name
);
6518 open_json_object(NULL
);
6520 pr_out_fmsg_group_start(dl
, name
);
6524 static void pr_out_fmsg_end_object(struct dl
*dl
)
6526 if (dl
->json_output
)
6527 close_json_object();
6529 pr_out_fmsg_group_end(dl
);
6532 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6534 if (dl
->json_output
) {
6535 pr_out_fmsg_name(dl
, name
);
6536 open_json_array(PRINT_JSON
, NULL
);
6538 pr_out_fmsg_group_start(dl
, name
);
6542 static void pr_out_fmsg_end_array(struct dl
*dl
)
6544 if (dl
->json_output
)
6545 close_json_array(PRINT_JSON
, NULL
);
6547 pr_out_fmsg_group_end(dl
);
6550 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6553 struct dl
*dl
= fmsg_data
->dl
;
6554 uint8_t value
= nest_value
;
6557 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6558 if (err
!= MNL_CB_OK
)
6562 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6564 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6566 pr_out_fmsg_end_object(dl
);
6568 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6570 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6572 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6574 pr_out_fmsg_end_array(dl
);
6582 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6584 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6585 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6586 struct fmsg_cb_data
*fmsg_data
= data
;
6587 struct dl
*dl
= fmsg_data
->dl
;
6588 struct nlattr
*nla_object
;
6592 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6593 if (!tb
[DEVLINK_ATTR_FMSG
])
6594 return MNL_CB_ERROR
;
6596 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6597 attr_type
= mnl_attr_get_type(nla_object
);
6598 switch (attr_type
) {
6599 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6600 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6601 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6602 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6603 if (err
!= MNL_CB_OK
)
6606 case DEVLINK_ATTR_FMSG_NEST_END
:
6607 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6608 if (err
!= MNL_CB_OK
)
6611 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6612 free(fmsg_data
->name
);
6613 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6614 if (!fmsg_data
->name
)
6617 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6618 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6620 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6621 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6622 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6624 if (err
!= MNL_CB_OK
)
6634 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6636 /* FMSG is dynamic: opening of an object or array causes a
6637 * newline. JSON starts with an { or [, but plain text should
6638 * not start with a new line. Ensure this by setting
6639 * g_new_line_count to 1: avoiding newline before the first
6642 g_new_line_count
= 1;
6645 INIT_LIST_HEAD(&data
->entry_list
);
6648 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6650 struct fmsg_cb_data data
;
6651 struct nlmsghdr
*nlh
;
6654 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6656 err
= dl_argv_parse_put(nlh
, dl
,
6657 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6661 cmd_fmsg_init(dl
, &data
);
6662 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6667 static int cmd_health_dump_show(struct dl
*dl
)
6669 return cmd_health_object_common(dl
,
6670 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6674 static int cmd_health_diagnose(struct dl
*dl
)
6676 return cmd_health_object_common(dl
,
6677 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6681 static int cmd_health_recover(struct dl
*dl
)
6683 struct nlmsghdr
*nlh
;
6686 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6687 NLM_F_REQUEST
| NLM_F_ACK
);
6689 err
= dl_argv_parse_put(nlh
, dl
,
6690 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6694 dl_opts_put(nlh
, dl
);
6695 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6698 enum devlink_health_reporter_state
{
6699 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6700 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6703 static const char *health_state_name(uint8_t state
)
6706 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6707 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6708 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6709 return HEALTH_REPORTER_STATE_ERROR_STR
;
6711 return "<unknown state>";
6715 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6717 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6718 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6719 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6720 struct sysinfo s_info
;
6726 info
= localtime(&now
);
6727 err
= sysinfo(&s_info
);
6730 /* Subtract uptime in sec from now yields the time of system
6731 * uptime. To this, add time_ms which is the amount of
6732 * milliseconds elapsed between uptime and the dump taken.
6734 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6735 info
= localtime(&sec
);
6737 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6738 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6739 check_indent_newline(dl
);
6740 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6741 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6744 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6746 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6747 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6752 ts
= mnl_attr_get_u64(attr
);
6753 tv_sec
= ts
/ 1000000000;
6754 tm
= localtime(&tv_sec
);
6756 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6757 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6759 check_indent_newline(dl
);
6760 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6761 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6764 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6766 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6767 enum devlink_health_reporter_state state
;
6770 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6772 if (err
!= MNL_CB_OK
)
6775 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6776 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6777 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6778 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6781 pr_out_handle_start_arr(dl
, tb_health
);
6783 check_indent_newline(dl
);
6784 print_string(PRINT_ANY
, "reporter", "reporter %s",
6785 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6786 if (!dl
->json_output
) {
6788 __pr_out_indent_inc();
6790 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6791 check_indent_newline(dl
);
6792 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6793 pr_out_u64(dl
, "error",
6794 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6795 pr_out_u64(dl
, "recover",
6796 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6797 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6798 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6799 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6800 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6801 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6802 pr_out_u64(dl
, "grace_period",
6803 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6804 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6805 pr_out_bool(dl
, "auto_recover",
6806 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6808 __pr_out_indent_dec();
6809 pr_out_handle_end(dl
);
6812 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6814 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6815 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6816 struct dl
*dl
= data
;
6818 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6819 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6820 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6821 return MNL_CB_ERROR
;
6823 pr_out_health(dl
, tb
);
6828 static int cmd_health_show(struct dl
*dl
)
6830 struct nlmsghdr
*nlh
;
6831 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6834 if (dl_argc(dl
) == 0)
6835 flags
|= NLM_F_DUMP
;
6836 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6839 if (dl_argc(dl
) > 0) {
6840 err
= dl_argv_parse_put(nlh
, dl
,
6842 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6846 pr_out_section_start(dl
, "health");
6848 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6849 pr_out_section_end(dl
);
6853 static void cmd_health_help(void)
6855 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6856 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6857 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6858 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6859 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6860 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6863 static int cmd_health(struct dl
*dl
)
6865 if (dl_argv_match(dl
, "help")) {
6868 } else if (dl_argv_match(dl
, "show") ||
6869 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6871 return cmd_health_show(dl
);
6872 } else if (dl_argv_match(dl
, "recover")) {
6874 return cmd_health_recover(dl
);
6875 } else if (dl_argv_match(dl
, "diagnose")) {
6877 return cmd_health_diagnose(dl
);
6878 } else if (dl_argv_match(dl
, "dump")) {
6880 if (dl_argv_match(dl
, "show")) {
6882 return cmd_health_dump_show(dl
);
6883 } else if (dl_argv_match(dl
, "clear")) {
6885 return cmd_health_dump_clear(dl
);
6887 } else if (dl_argv_match(dl
, "set")) {
6889 return cmd_health_set_params(dl
);
6891 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6895 static const char *trap_type_name(uint8_t type
)
6898 case DEVLINK_TRAP_TYPE_DROP
:
6900 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6903 return "<unknown type>";
6907 static const char *trap_action_name(uint8_t action
)
6910 case DEVLINK_TRAP_ACTION_DROP
:
6912 case DEVLINK_TRAP_ACTION_TRAP
:
6915 return "<unknown action>";
6919 static const char *trap_metadata_name(const struct nlattr
*attr
)
6921 switch (attr
->nla_type
) {
6922 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
6923 return "input_port";
6925 return "<unknown metadata type>";
6928 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
6930 struct nlattr
*attr_metadata
;
6932 pr_out_array_start(dl
, "metadata");
6933 mnl_attr_for_each_nested(attr_metadata
, attr
) {
6934 check_indent_newline(dl
);
6935 print_string(PRINT_ANY
, NULL
, "%s",
6936 trap_metadata_name(attr_metadata
));
6938 pr_out_array_end(dl
);
6941 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6943 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
6944 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
6947 pr_out_handle_start_arr(dl
, tb
);
6949 __pr_out_handle_start(dl
, tb
, true, false);
6951 check_indent_newline(dl
);
6952 print_string(PRINT_ANY
, "name", "name %s",
6953 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
6954 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
6955 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6956 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
6957 print_string(PRINT_ANY
, "group", " group %s",
6958 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6960 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
6961 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6962 pr_out_handle_end(dl
);
6965 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6967 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6968 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6969 struct dl
*dl
= data
;
6971 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6972 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6973 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
6974 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
6975 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
6976 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
6977 return MNL_CB_ERROR
;
6979 pr_out_trap(dl
, tb
, true);
6984 static void cmd_trap_help(void)
6986 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
6987 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
6988 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
6989 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
6992 static int cmd_trap_show(struct dl
*dl
)
6994 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6995 struct nlmsghdr
*nlh
;
6998 if (dl_argc(dl
) == 0)
6999 flags
|= NLM_F_DUMP
;
7001 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7003 if (dl_argc(dl
) > 0) {
7004 err
= dl_argv_parse_put(nlh
, dl
,
7005 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7010 pr_out_section_start(dl
, "trap");
7011 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7012 pr_out_section_end(dl
);
7017 static int cmd_trap_set(struct dl
*dl
)
7019 struct nlmsghdr
*nlh
;
7022 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7023 NLM_F_REQUEST
| NLM_F_ACK
);
7025 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7026 DL_OPT_TRAP_ACTION
);
7030 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7033 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7036 pr_out_handle_start_arr(dl
, tb
);
7038 __pr_out_handle_start(dl
, tb
, true, false);
7040 check_indent_newline(dl
);
7041 print_string(PRINT_ANY
, "name", "name %s",
7042 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7043 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7044 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7045 pr_out_handle_end(dl
);
7048 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7050 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7051 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7052 struct dl
*dl
= data
;
7054 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7055 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7056 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7057 return MNL_CB_ERROR
;
7059 pr_out_trap_group(dl
, tb
, true);
7064 static int cmd_trap_group_show(struct dl
*dl
)
7066 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7067 struct nlmsghdr
*nlh
;
7070 if (dl_argc(dl
) == 0)
7071 flags
|= NLM_F_DUMP
;
7073 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7075 if (dl_argc(dl
) > 0) {
7076 err
= dl_argv_parse_put(nlh
, dl
,
7077 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7083 pr_out_section_start(dl
, "trap_group");
7084 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7085 pr_out_section_end(dl
);
7090 static int cmd_trap_group_set(struct dl
*dl
)
7092 struct nlmsghdr
*nlh
;
7095 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7096 NLM_F_REQUEST
| NLM_F_ACK
);
7098 err
= dl_argv_parse_put(nlh
, dl
,
7099 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7100 DL_OPT_TRAP_ACTION
);
7104 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7107 static int cmd_trap_group(struct dl
*dl
)
7109 if (dl_argv_match(dl
, "help")) {
7112 } else if (dl_argv_match(dl
, "show") ||
7113 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7115 return cmd_trap_group_show(dl
);
7116 } else if (dl_argv_match(dl
, "set")) {
7118 return cmd_trap_group_set(dl
);
7120 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7124 static int cmd_trap(struct dl
*dl
)
7126 if (dl_argv_match(dl
, "help")) {
7129 } else if (dl_argv_match(dl
, "show") ||
7130 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7132 return cmd_trap_show(dl
);
7133 } else if (dl_argv_match(dl
, "set")) {
7135 return cmd_trap_set(dl
);
7136 } else if (dl_argv_match(dl
, "group")) {
7138 return cmd_trap_group(dl
);
7140 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7144 static void help(void)
7146 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7147 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7148 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7149 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7152 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7157 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7160 } else if (dl_argv_match(dl
, "dev")) {
7163 } else if (dl_argv_match(dl
, "port")) {
7165 return cmd_port(dl
);
7166 } else if (dl_argv_match(dl
, "sb")) {
7169 } else if (dl_argv_match(dl
, "monitor")) {
7172 } else if (dl_argv_match(dl
, "dpipe")) {
7174 return cmd_dpipe(dl
);
7175 } else if (dl_argv_match(dl
, "resource")) {
7177 return cmd_resource(dl
);
7178 } else if (dl_argv_match(dl
, "region")) {
7180 return cmd_region(dl
);
7181 } else if (dl_argv_match(dl
, "health")) {
7183 return cmd_health(dl
);
7184 } else if (dl_argv_match(dl
, "trap")) {
7186 return cmd_trap(dl
);
7188 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7192 static int dl_init(struct dl
*dl
)
7196 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7198 pr_err("Failed to connect to devlink Netlink\n");
7202 err
= ifname_map_init(dl
);
7204 pr_err("Failed to create index map\n");
7205 goto err_ifname_map_create
;
7207 new_json_obj_plain(dl
->json_output
);
7210 err_ifname_map_create
:
7211 mnlg_socket_close(dl
->nlg
);
7215 static void dl_fini(struct dl
*dl
)
7217 delete_json_obj_plain();
7218 ifname_map_fini(dl
);
7219 mnlg_socket_close(dl
->nlg
);
7222 static struct dl
*dl_alloc(void)
7226 dl
= calloc(1, sizeof(*dl
));
7232 static void dl_free(struct dl
*dl
)
7237 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7241 int ret
= EXIT_SUCCESS
;
7243 if (name
&& strcmp(name
, "-") != 0) {
7244 if (freopen(name
, "r", stdin
) == NULL
) {
7246 "Cannot open file \"%s\" for reading: %s\n",
7247 name
, strerror(errno
));
7248 return EXIT_FAILURE
;
7253 while (getcmdline(&line
, &len
, stdin
) != -1) {
7257 largc
= makeargs(line
, largv
, 100);
7259 continue; /* blank line */
7261 if (dl_cmd(dl
, largc
, largv
)) {
7262 fprintf(stderr
, "Command failed %s:%d\n",
7276 int main(int argc
, char **argv
)
7278 static const struct option long_options
[] = {
7279 { "Version", no_argument
, NULL
, 'V' },
7280 { "force", no_argument
, NULL
, 'f' },
7281 { "batch", required_argument
, NULL
, 'b' },
7282 { "no-nice-names", no_argument
, NULL
, 'n' },
7283 { "json", no_argument
, NULL
, 'j' },
7284 { "pretty", no_argument
, NULL
, 'p' },
7285 { "verbose", no_argument
, NULL
, 'v' },
7286 { "statistics", no_argument
, NULL
, 's' },
7287 { "Netns", required_argument
, NULL
, 'N' },
7288 { NULL
, 0, NULL
, 0 }
7290 const char *batch_file
= NULL
;
7299 pr_err("Failed to allocate memory for devlink\n");
7300 return EXIT_FAILURE
;
7303 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7304 long_options
, NULL
)) >= 0) {
7308 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7315 batch_file
= optarg
;
7318 dl
->no_nice_names
= true;
7321 dl
->json_output
= true;
7333 if (netns_switch(optarg
)) {
7339 pr_err("Unknown option.\n");
7356 err
= dl_batch(dl
, batch_file
, force
);
7358 err
= dl_cmd(dl
, argc
, argv
);