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 ESWITCH_ENCAP_MODE_NONE "none"
45 #define ESWITCH_ENCAP_MODE_BASIC "basic"
47 #define PARAM_CMODE_RUNTIME_STR "runtime"
48 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
49 #define PARAM_CMODE_PERMANENT_STR "permanent"
50 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
52 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
53 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
54 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
56 static int g_new_line_count
;
57 static int g_indent_level
;
58 static bool g_indent_newline
;
60 #define INDENT_STR_STEP 2
61 #define INDENT_STR_MAXLEN 32
62 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
64 static void __attribute__((format(printf
, 1, 2)))
65 pr_err(const char *fmt
, ...)
70 vfprintf(stderr
, fmt
, ap
);
74 static void __attribute__((format(printf
, 1, 2)))
75 pr_out(const char *fmt
, ...)
79 if (g_indent_newline
) {
80 printf("%s", g_indent_str
);
81 g_indent_newline
= false;
89 static void __attribute__((format(printf
, 2, 3)))
90 pr_out_sp(unsigned int num
, const char *fmt
, ...)
96 ret
= vprintf(fmt
, ap
);
100 printf("%*s", num
- ret
, "");
101 g_new_line_count
= 0; \
104 static void __attribute__((format(printf
, 1, 2)))
105 pr_out_tty(const char *fmt
, ...)
109 if (!isatty(STDOUT_FILENO
))
116 static void __pr_out_indent_inc(void)
118 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
120 g_indent_level
+= INDENT_STR_STEP
;
121 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
122 g_indent_str
[g_indent_level
] = '\0';
125 static void __pr_out_indent_dec(void)
127 if (g_indent_level
- INDENT_STR_STEP
< 0)
129 g_indent_level
-= INDENT_STR_STEP
;
130 g_indent_str
[g_indent_level
] = '\0';
133 static void __pr_out_newline(void)
135 if (g_new_line_count
< 1) {
137 g_indent_newline
= true;
142 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
143 mnl_cb_t data_cb
, void *data
)
147 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
149 pr_err("devlink answers: %s\n", strerror(errno
));
155 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
156 const struct nlmsghdr
*nlh
)
160 err
= mnlg_socket_send(nlg
, nlh
);
162 pr_err("Failed to call mnlg_socket_send\n");
168 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
169 const struct nlmsghdr
*nlh
,
170 mnl_cb_t data_cb
, void *data
)
174 err
= _mnlg_socket_send(nlg
, nlh
);
177 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
180 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
181 const char *group_name
)
185 err
= mnlg_socket_group_add(nlg
, group_name
);
187 pr_err("Failed to call mnlg_socket_group_add\n");
194 struct list_head list
;
201 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
202 const char *dev_name
,
206 struct ifname_map
*ifname_map
;
208 ifname_map
= calloc(1, sizeof(*ifname_map
));
211 ifname_map
->bus_name
= strdup(bus_name
);
212 ifname_map
->dev_name
= strdup(dev_name
);
213 ifname_map
->port_index
= port_index
;
214 ifname_map
->ifname
= strdup(ifname
);
215 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
216 !ifname_map
->ifname
) {
217 free(ifname_map
->ifname
);
218 free(ifname_map
->dev_name
);
219 free(ifname_map
->bus_name
);
226 static void ifname_map_free(struct ifname_map
*ifname_map
)
228 free(ifname_map
->ifname
);
229 free(ifname_map
->dev_name
);
230 free(ifname_map
->bus_name
);
234 #define DL_OPT_HANDLE BIT(0)
235 #define DL_OPT_HANDLEP BIT(1)
236 #define DL_OPT_PORT_TYPE BIT(2)
237 #define DL_OPT_PORT_COUNT BIT(3)
238 #define DL_OPT_SB BIT(4)
239 #define DL_OPT_SB_POOL BIT(5)
240 #define DL_OPT_SB_SIZE BIT(6)
241 #define DL_OPT_SB_TYPE BIT(7)
242 #define DL_OPT_SB_THTYPE BIT(8)
243 #define DL_OPT_SB_TH BIT(9)
244 #define DL_OPT_SB_TC BIT(10)
245 #define DL_OPT_ESWITCH_MODE BIT(11)
246 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
247 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
248 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
249 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
250 #define DL_OPT_RESOURCE_PATH BIT(16)
251 #define DL_OPT_RESOURCE_SIZE BIT(17)
252 #define DL_OPT_PARAM_NAME BIT(18)
253 #define DL_OPT_PARAM_VALUE BIT(19)
254 #define DL_OPT_PARAM_CMODE BIT(20)
255 #define DL_OPT_HANDLE_REGION BIT(21)
256 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
257 #define DL_OPT_REGION_ADDRESS BIT(23)
258 #define DL_OPT_REGION_LENGTH BIT(24)
259 #define DL_OPT_FLASH_FILE_NAME BIT(25)
260 #define DL_OPT_FLASH_COMPONENT BIT(26)
261 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
262 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
263 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
264 #define DL_OPT_TRAP_NAME BIT(30)
265 #define DL_OPT_TRAP_ACTION BIT(31)
266 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
267 #define DL_OPT_NETNS BIT(33)
268 #define DL_OPT_TRAP_POLICER_ID BIT(34)
269 #define DL_OPT_TRAP_POLICER_RATE BIT(35)
270 #define DL_OPT_TRAP_POLICER_BURST BIT(36)
273 uint64_t present
; /* flags of present items */
277 enum devlink_port_type port_type
;
280 uint16_t sb_pool_index
;
281 uint32_t sb_pool_size
;
282 enum devlink_sb_pool_type sb_pool_type
;
283 enum devlink_sb_threshold_type sb_pool_thtype
;
284 uint32_t sb_threshold
;
285 uint16_t sb_tc_index
;
286 enum devlink_eswitch_mode eswitch_mode
;
287 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
288 const char *dpipe_table_name
;
289 bool dpipe_counters_enable
;
290 enum devlink_eswitch_encap_mode eswitch_encap_mode
;
291 const char *resource_path
;
292 uint64_t resource_size
;
293 uint32_t resource_id
;
294 bool resource_id_valid
;
295 const char *param_name
;
296 const char *param_value
;
297 enum devlink_param_cmode cmode
;
299 uint32_t region_snapshot_id
;
300 uint64_t region_address
;
301 uint64_t region_length
;
302 const char *flash_file_name
;
303 const char *flash_component
;
304 const char *reporter_name
;
305 uint64_t reporter_graceful_period
;
306 bool reporter_auto_recover
;
307 const char *trap_name
;
308 const char *trap_group_name
;
309 enum devlink_trap_action trap_action
;
312 uint32_t trap_policer_id
;
313 uint64_t trap_policer_rate
;
314 uint64_t trap_policer_burst
;
318 struct mnlg_socket
*nlg
;
319 struct list_head ifname_map_list
;
337 static int dl_argc(struct dl
*dl
)
342 static char *dl_argv(struct dl
*dl
)
344 if (dl_argc(dl
) == 0)
349 static void dl_arg_inc(struct dl
*dl
)
351 if (dl_argc(dl
) == 0)
357 static void dl_arg_dec(struct dl
*dl
)
363 static char *dl_argv_next(struct dl
*dl
)
367 if (dl_argc(dl
) == 0)
375 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
377 if (index
>= dl_argc(dl
))
379 return dl
->argv
[index
];
382 static int strcmpx(const char *str1
, const char *str2
)
384 if (strlen(str1
) > strlen(str2
))
386 return strncmp(str1
, str2
, strlen(str1
));
389 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
391 if (dl_argc(dl
) == 0)
393 return strcmpx(dl_argv(dl
), pattern
) == 0;
396 static bool dl_no_arg(struct dl
*dl
)
398 return dl_argc(dl
) == 0;
401 static void __pr_out_indent_newline(struct dl
*dl
)
403 if (!g_indent_newline
&& !dl
->json_output
)
407 static void check_indent_newline(struct dl
*dl
)
409 __pr_out_indent_newline(dl
);
411 if (g_indent_newline
&& !is_json_context()) {
412 printf("%s", g_indent_str
);
413 g_indent_newline
= false;
415 g_new_line_count
= 0;
418 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
419 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
420 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
421 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
422 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
423 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
424 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
425 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
426 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
427 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
428 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
429 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
430 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
431 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
432 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
433 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
434 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
435 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
436 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
437 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
438 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
439 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
440 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
441 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
442 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
443 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
444 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
445 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
446 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
447 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
448 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
449 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
450 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
451 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
452 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
453 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
454 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
455 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
456 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
457 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
458 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
459 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
460 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
461 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
462 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
463 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
464 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
465 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
466 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
467 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
468 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
469 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
470 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
471 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
472 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
473 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
474 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
475 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
476 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
477 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
478 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
479 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
480 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
481 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
482 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
483 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
484 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
485 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
486 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
487 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
488 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
489 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
490 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
491 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
492 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
493 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
494 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
495 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
496 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
497 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
498 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
499 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
500 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
501 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
502 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
503 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
504 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
505 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
506 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
507 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
508 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
509 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
510 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
511 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
512 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
513 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
514 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
515 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
516 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
517 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
518 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
519 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
520 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
523 static const enum mnl_attr_data_type
524 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
525 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
526 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
527 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
530 static int attr_cb(const struct nlattr
*attr
, void *data
)
532 const struct nlattr
**tb
= data
;
535 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
538 type
= mnl_attr_get_type(attr
);
539 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
546 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
548 const struct nlattr
**tb
= data
;
551 /* Allow the tool to work on top of newer kernels that might contain
554 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
557 type
= mnl_attr_get_type(attr
);
558 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
565 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
567 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
568 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
569 struct dl
*dl
= data
;
570 struct ifname_map
*ifname_map
;
571 const char *bus_name
;
572 const char *dev_name
;
573 uint32_t port_ifindex
;
574 const char *port_ifname
;
576 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
577 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
578 !tb
[DEVLINK_ATTR_PORT_INDEX
])
581 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
584 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
585 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
586 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
587 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
588 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
589 port_ifindex
, port_ifname
);
592 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
597 static void ifname_map_fini(struct dl
*dl
)
599 struct ifname_map
*ifname_map
, *tmp
;
601 list_for_each_entry_safe(ifname_map
, tmp
,
602 &dl
->ifname_map_list
, list
) {
603 list_del(&ifname_map
->list
);
604 ifname_map_free(ifname_map
);
608 static int ifname_map_init(struct dl
*dl
)
610 struct nlmsghdr
*nlh
;
613 INIT_LIST_HEAD(&dl
->ifname_map_list
);
615 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
616 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
618 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
626 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
627 char **p_bus_name
, char **p_dev_name
,
628 uint32_t *p_port_index
)
630 struct ifname_map
*ifname_map
;
632 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
633 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
634 *p_bus_name
= ifname_map
->bus_name
;
635 *p_dev_name
= ifname_map
->dev_name
;
636 *p_port_index
= ifname_map
->port_index
;
643 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
644 const char *dev_name
, uint32_t port_index
,
647 struct ifname_map
*ifname_map
;
649 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
650 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
651 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
652 port_index
== ifname_map
->port_index
) {
653 *p_ifname
= ifname_map
->ifname
;
660 static unsigned int strslashcount(char *str
)
662 unsigned int count
= 0;
665 while ((pos
= strchr(pos
, '/'))) {
672 static int strslashrsplit(char *str
, char **before
, char **after
)
676 slash
= strrchr(str
, '/');
685 static int strtouint64_t(const char *str
, uint64_t *p_val
)
688 unsigned long long int val
;
690 val
= strtoull(str
, &endptr
, 10);
691 if (endptr
== str
|| *endptr
!= '\0')
699 static int strtouint32_t(const char *str
, uint32_t *p_val
)
702 unsigned long int val
;
704 val
= strtoul(str
, &endptr
, 10);
705 if (endptr
== str
|| *endptr
!= '\0')
713 static int strtouint16_t(const char *str
, uint16_t *p_val
)
716 unsigned long int val
;
718 val
= strtoul(str
, &endptr
, 10);
719 if (endptr
== str
|| *endptr
!= '\0')
727 static int strtouint8_t(const char *str
, uint8_t *p_val
)
730 unsigned long int val
;
732 val
= strtoul(str
, &endptr
, 10);
733 if (endptr
== str
|| *endptr
!= '\0')
741 static int strtobool(const char *str
, bool *p_val
)
745 if (!strcmp(str
, "true") || !strcmp(str
, "1") ||
746 !strcmp(str
, "enable"))
748 else if (!strcmp(str
, "false") || !strcmp(str
, "0") ||
749 !strcmp(str
, "disable"))
757 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
759 strslashrsplit(str
, p_bus_name
, p_dev_name
);
763 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
765 char *str
= dl_argv_next(dl
);
768 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
771 if (strslashcount(str
) != 1) {
772 pr_err("Wrong devlink identification string format.\n");
773 pr_err("Expected \"bus_name/dev_name\".\n");
776 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
779 static int __dl_argv_handle_port(char *str
,
780 char **p_bus_name
, char **p_dev_name
,
781 uint32_t *p_port_index
)
787 err
= strslashrsplit(str
, &handlestr
, &portstr
);
789 pr_err("Port identification \"%s\" is invalid\n", str
);
792 err
= strtouint32_t(portstr
, p_port_index
);
794 pr_err("Port index \"%s\" is not a number or not within range\n",
798 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
800 pr_err("Port identification \"%s\" is invalid\n", str
);
806 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
807 char **p_bus_name
, char **p_dev_name
,
808 uint32_t *p_port_index
)
812 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
815 pr_err("Netdevice \"%s\" not found\n", str
);
821 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
822 char **p_dev_name
, uint32_t *p_port_index
)
824 char *str
= dl_argv_next(dl
);
825 unsigned int slash_count
;
828 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
831 slash_count
= strslashcount(str
);
832 switch (slash_count
) {
834 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
835 p_dev_name
, p_port_index
);
837 return __dl_argv_handle_port(str
, p_bus_name
,
838 p_dev_name
, p_port_index
);
840 pr_err("Wrong port identification string format.\n");
841 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
846 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
847 char **p_dev_name
, uint32_t *p_port_index
,
848 uint64_t *p_handle_bit
)
850 char *str
= dl_argv_next(dl
);
851 unsigned int slash_count
;
855 pr_err("One of following identifications expected:\n"
856 "Devlink identification (\"bus_name/dev_name\")\n"
857 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
860 slash_count
= strslashcount(str
);
861 if (slash_count
== 1) {
862 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
865 *p_handle_bit
= DL_OPT_HANDLE
;
866 } else if (slash_count
== 2) {
867 err
= __dl_argv_handle_port(str
, p_bus_name
,
868 p_dev_name
, p_port_index
);
871 *p_handle_bit
= DL_OPT_HANDLEP
;
872 } else if (slash_count
== 0) {
873 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
874 p_dev_name
, p_port_index
);
877 *p_handle_bit
= DL_OPT_HANDLEP
;
879 pr_err("Wrong port identification string format.\n");
880 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
886 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
887 char **p_dev_name
, char **p_region
)
892 err
= strslashrsplit(str
, &handlestr
, p_region
);
894 pr_err("Region identification \"%s\" is invalid\n", str
);
897 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
899 pr_err("Region identification \"%s\" is invalid\n", str
);
905 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
906 char **p_dev_name
, char **p_region
)
908 char *str
= dl_argv_next(dl
);
909 unsigned int slash_count
;
912 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
916 slash_count
= strslashcount(str
);
917 if (slash_count
!= 2) {
918 pr_err("Wrong region identification string format.\n");
919 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
923 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
926 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
928 char *str
= dl_argv_next(dl
);
932 pr_err("Unsigned number argument expected\n");
936 err
= strtouint64_t(str
, p_val
);
938 pr_err("\"%s\" is not a number or not within range\n", str
);
944 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
946 char *str
= dl_argv_next(dl
);
950 pr_err("Unsigned number argument expected\n");
954 err
= strtouint32_t(str
, p_val
);
956 pr_err("\"%s\" is not a number or not within range\n", str
);
962 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
964 char *str
= dl_argv_next(dl
);
968 pr_err("Unsigned number argument expected\n");
972 err
= strtouint16_t(str
, p_val
);
974 pr_err("\"%s\" is not a number or not within range\n", str
);
980 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
982 char *str
= dl_argv_next(dl
);
986 pr_err("Boolean argument expected\n");
990 err
= strtobool(str
, p_val
);
992 pr_err("\"%s\" is not a valid boolean value\n", str
);
998 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
1000 const char *str
= dl_argv_next(dl
);
1003 pr_err("String parameter expected\n");
1010 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1012 if (strcmp(typestr
, "auto") == 0) {
1013 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1014 } else if (strcmp(typestr
, "eth") == 0) {
1015 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1016 } else if (strcmp(typestr
, "ib") == 0) {
1017 *p_type
= DEVLINK_PORT_TYPE_IB
;
1019 pr_err("Unknown port type \"%s\"\n", typestr
);
1025 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1027 if (strcmp(typestr
, "ingress") == 0) {
1028 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1029 } else if (strcmp(typestr
, "egress") == 0) {
1030 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1032 pr_err("Unknown pool type \"%s\"\n", typestr
);
1038 static int threshold_type_get(const char *typestr
,
1039 enum devlink_sb_threshold_type
*p_type
)
1041 if (strcmp(typestr
, "static") == 0) {
1042 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1043 } else if (strcmp(typestr
, "dynamic") == 0) {
1044 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1046 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1052 static int eswitch_mode_get(const char *typestr
,
1053 enum devlink_eswitch_mode
*p_mode
)
1055 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1056 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1057 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1058 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1060 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1066 static int eswitch_inline_mode_get(const char *typestr
,
1067 enum devlink_eswitch_inline_mode
*p_mode
)
1069 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1070 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1071 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1072 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1073 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1074 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1075 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1076 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1078 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1085 eswitch_encap_mode_get(const char *typestr
,
1086 enum devlink_eswitch_encap_mode
*p_encap_mode
)
1088 /* The initial implementation incorrectly accepted "enable"/"disable".
1089 * Carry it to maintain backward compatibility.
1091 if (strcmp(typestr
, "disable") == 0 ||
1092 strcmp(typestr
, ESWITCH_ENCAP_MODE_NONE
) == 0) {
1093 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1094 } else if (strcmp(typestr
, "enable") == 0 ||
1095 strcmp(typestr
, ESWITCH_ENCAP_MODE_BASIC
) == 0) {
1096 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1098 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1104 static int param_cmode_get(const char *cmodestr
,
1105 enum devlink_param_cmode
*cmode
)
1107 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1108 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1109 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1110 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1111 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1112 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1114 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1120 static int trap_action_get(const char *actionstr
,
1121 enum devlink_trap_action
*p_action
)
1123 if (strcmp(actionstr
, "drop") == 0) {
1124 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1125 } else if (strcmp(actionstr
, "trap") == 0) {
1126 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1128 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1134 struct dl_args_metadata
{
1136 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1139 static const struct dl_args_metadata dl_args_required
[] = {
1140 {DL_OPT_PORT_TYPE
, "Port type not set."},
1141 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1142 {DL_OPT_SB_POOL
, "Pool index option expected."},
1143 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1144 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1145 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1146 {DL_OPT_SB_TH
, "Threshold option expected."},
1147 {DL_OPT_SB_TC
, "TC index option expected."},
1148 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1149 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1150 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1151 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1152 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1153 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1154 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1155 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1156 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1157 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1158 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1159 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1160 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1161 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1162 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1163 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1166 static int dl_args_finding_required_validate(uint64_t o_required
,
1172 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1173 o_flag
= dl_args_required
[i
].o_flag
;
1174 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1175 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1179 if (o_required
& ~o_found
) {
1180 pr_err("BUG: unknown argument required but not found\n");
1186 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1187 uint64_t o_optional
)
1189 struct dl_opts
*opts
= &dl
->opts
;
1190 uint64_t o_all
= o_required
| o_optional
;
1191 uint64_t o_found
= 0;
1194 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1195 uint64_t handle_bit
;
1197 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1198 &opts
->port_index
, &handle_bit
);
1201 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1202 o_found
|= handle_bit
;
1203 } else if (o_required
& DL_OPT_HANDLE
) {
1204 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1207 o_found
|= DL_OPT_HANDLE
;
1208 } else if (o_required
& DL_OPT_HANDLEP
) {
1209 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1213 o_found
|= DL_OPT_HANDLEP
;
1214 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1215 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1217 &opts
->region_name
);
1220 o_found
|= DL_OPT_HANDLE_REGION
;
1223 while (dl_argc(dl
)) {
1224 if (dl_argv_match(dl
, "type") &&
1225 (o_all
& DL_OPT_PORT_TYPE
)) {
1226 const char *typestr
;
1229 err
= dl_argv_str(dl
, &typestr
);
1232 err
= port_type_get(typestr
, &opts
->port_type
);
1235 o_found
|= DL_OPT_PORT_TYPE
;
1236 } else if (dl_argv_match(dl
, "count") &&
1237 (o_all
& DL_OPT_PORT_COUNT
)) {
1239 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1242 o_found
|= DL_OPT_PORT_COUNT
;
1243 } else if (dl_argv_match(dl
, "sb") &&
1244 (o_all
& DL_OPT_SB
)) {
1246 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1249 o_found
|= DL_OPT_SB
;
1250 } else if (dl_argv_match(dl
, "pool") &&
1251 (o_all
& DL_OPT_SB_POOL
)) {
1253 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1256 o_found
|= DL_OPT_SB_POOL
;
1257 } else if (dl_argv_match(dl
, "size") &&
1258 (o_all
& DL_OPT_SB_SIZE
)) {
1260 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1263 o_found
|= DL_OPT_SB_SIZE
;
1264 } else if (dl_argv_match(dl
, "type") &&
1265 (o_all
& DL_OPT_SB_TYPE
)) {
1266 const char *typestr
;
1269 err
= dl_argv_str(dl
, &typestr
);
1272 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1275 o_found
|= DL_OPT_SB_TYPE
;
1276 } else if (dl_argv_match(dl
, "thtype") &&
1277 (o_all
& DL_OPT_SB_THTYPE
)) {
1278 const char *typestr
;
1281 err
= dl_argv_str(dl
, &typestr
);
1284 err
= threshold_type_get(typestr
,
1285 &opts
->sb_pool_thtype
);
1288 o_found
|= DL_OPT_SB_THTYPE
;
1289 } else if (dl_argv_match(dl
, "th") &&
1290 (o_all
& DL_OPT_SB_TH
)) {
1292 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1295 o_found
|= DL_OPT_SB_TH
;
1296 } else if (dl_argv_match(dl
, "tc") &&
1297 (o_all
& DL_OPT_SB_TC
)) {
1299 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1302 o_found
|= DL_OPT_SB_TC
;
1303 } else if (dl_argv_match(dl
, "mode") &&
1304 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1305 const char *typestr
;
1308 err
= dl_argv_str(dl
, &typestr
);
1311 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1314 o_found
|= DL_OPT_ESWITCH_MODE
;
1315 } else if (dl_argv_match(dl
, "inline-mode") &&
1316 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1317 const char *typestr
;
1320 err
= dl_argv_str(dl
, &typestr
);
1323 err
= eswitch_inline_mode_get(
1324 typestr
, &opts
->eswitch_inline_mode
);
1327 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1328 } else if (dl_argv_match(dl
, "name") &&
1329 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1331 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1334 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1335 } else if (dl_argv_match(dl
, "counters") &&
1336 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1338 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enable
);
1341 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1342 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1343 dl_argv_match(dl
, "encap-mode")) &&
1344 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1345 const char *typestr
;
1348 err
= dl_argv_str(dl
, &typestr
);
1351 err
= eswitch_encap_mode_get(typestr
,
1352 &opts
->eswitch_encap_mode
);
1355 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1356 } else if (dl_argv_match(dl
, "path") &&
1357 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1359 err
= dl_argv_str(dl
, &opts
->resource_path
);
1362 o_found
|= DL_OPT_RESOURCE_PATH
;
1363 } else if (dl_argv_match(dl
, "size") &&
1364 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1366 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1369 o_found
|= DL_OPT_RESOURCE_SIZE
;
1370 } else if (dl_argv_match(dl
, "name") &&
1371 (o_all
& DL_OPT_PARAM_NAME
)) {
1373 err
= dl_argv_str(dl
, &opts
->param_name
);
1376 o_found
|= DL_OPT_PARAM_NAME
;
1377 } else if (dl_argv_match(dl
, "value") &&
1378 (o_all
& DL_OPT_PARAM_VALUE
)) {
1380 err
= dl_argv_str(dl
, &opts
->param_value
);
1383 o_found
|= DL_OPT_PARAM_VALUE
;
1384 } else if (dl_argv_match(dl
, "cmode") &&
1385 (o_all
& DL_OPT_PARAM_CMODE
)) {
1386 const char *cmodestr
;
1389 err
= dl_argv_str(dl
, &cmodestr
);
1392 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1395 o_found
|= DL_OPT_PARAM_CMODE
;
1396 } else if (dl_argv_match(dl
, "snapshot") &&
1397 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1399 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1402 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1403 } else if (dl_argv_match(dl
, "address") &&
1404 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1406 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1409 o_found
|= DL_OPT_REGION_ADDRESS
;
1410 } else if (dl_argv_match(dl
, "length") &&
1411 (o_all
& DL_OPT_REGION_LENGTH
)) {
1413 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1416 o_found
|= DL_OPT_REGION_LENGTH
;
1417 } else if (dl_argv_match(dl
, "file") &&
1418 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1420 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1423 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1424 } else if (dl_argv_match(dl
, "component") &&
1425 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1427 err
= dl_argv_str(dl
, &opts
->flash_component
);
1430 o_found
|= DL_OPT_FLASH_COMPONENT
;
1431 } else if (dl_argv_match(dl
, "reporter") &&
1432 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1434 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1437 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1438 } else if (dl_argv_match(dl
, "grace_period") &&
1439 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1441 err
= dl_argv_uint64_t(dl
,
1442 &opts
->reporter_graceful_period
);
1445 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1446 } else if (dl_argv_match(dl
, "auto_recover") &&
1447 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1449 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1452 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1453 } else if (dl_argv_match(dl
, "trap") &&
1454 (o_all
& DL_OPT_TRAP_NAME
)) {
1456 err
= dl_argv_str(dl
, &opts
->trap_name
);
1459 o_found
|= DL_OPT_TRAP_NAME
;
1460 } else if (dl_argv_match(dl
, "group") &&
1461 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1463 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1466 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1467 } else if (dl_argv_match(dl
, "action") &&
1468 (o_all
& DL_OPT_TRAP_ACTION
)) {
1469 const char *actionstr
;
1472 err
= dl_argv_str(dl
, &actionstr
);
1475 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1478 o_found
|= DL_OPT_TRAP_ACTION
;
1479 } else if (dl_argv_match(dl
, "netns") &&
1480 (o_all
& DL_OPT_NETNS
)) {
1481 const char *netns_str
;
1484 err
= dl_argv_str(dl
, &netns_str
);
1487 opts
->netns
= netns_get_fd(netns_str
);
1488 if ((int)opts
->netns
< 0) {
1490 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1493 opts
->netns_is_pid
= true;
1495 o_found
|= DL_OPT_NETNS
;
1496 } else if (dl_argv_match(dl
, "policer") &&
1497 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1499 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1502 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1503 } else if (dl_argv_match(dl
, "nopolicer") &&
1504 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1506 opts
->trap_policer_id
= 0;
1507 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1508 } else if (dl_argv_match(dl
, "rate") &&
1509 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1511 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1514 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1515 } else if (dl_argv_match(dl
, "burst") &&
1516 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1518 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1521 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1523 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1528 opts
->present
= o_found
;
1530 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1532 opts
->present
|= DL_OPT_SB
;
1535 return dl_args_finding_required_validate(o_required
, o_found
);
1538 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1540 struct dl_opts
*opts
= &dl
->opts
;
1542 if (opts
->present
& DL_OPT_HANDLE
) {
1543 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1544 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1545 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1546 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1547 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1548 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1550 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1551 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1552 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1553 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1556 if (opts
->present
& DL_OPT_PORT_TYPE
)
1557 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1559 if (opts
->present
& DL_OPT_PORT_COUNT
)
1560 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1562 if (opts
->present
& DL_OPT_SB
)
1563 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1565 if (opts
->present
& DL_OPT_SB_POOL
)
1566 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1567 opts
->sb_pool_index
);
1568 if (opts
->present
& DL_OPT_SB_SIZE
)
1569 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1570 opts
->sb_pool_size
);
1571 if (opts
->present
& DL_OPT_SB_TYPE
)
1572 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1573 opts
->sb_pool_type
);
1574 if (opts
->present
& DL_OPT_SB_THTYPE
)
1575 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1576 opts
->sb_pool_thtype
);
1577 if (opts
->present
& DL_OPT_SB_TH
)
1578 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1579 opts
->sb_threshold
);
1580 if (opts
->present
& DL_OPT_SB_TC
)
1581 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1583 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1584 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1585 opts
->eswitch_mode
);
1586 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1587 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1588 opts
->eswitch_inline_mode
);
1589 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1590 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1591 opts
->dpipe_table_name
);
1592 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1593 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1594 opts
->dpipe_counters_enable
);
1595 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1596 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1597 opts
->eswitch_encap_mode
);
1598 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1599 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1601 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1602 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1603 opts
->resource_size
);
1604 if (opts
->present
& DL_OPT_PARAM_NAME
)
1605 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1607 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1608 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1610 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1611 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1612 opts
->region_snapshot_id
);
1613 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1614 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1615 opts
->region_address
);
1616 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1617 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1618 opts
->region_length
);
1619 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1620 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1621 opts
->flash_file_name
);
1622 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1623 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1624 opts
->flash_component
);
1625 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1626 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1627 opts
->reporter_name
);
1628 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1629 mnl_attr_put_u64(nlh
,
1630 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1631 opts
->reporter_graceful_period
);
1632 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1633 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1634 opts
->reporter_auto_recover
);
1635 if (opts
->present
& DL_OPT_TRAP_NAME
)
1636 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1638 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1639 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1640 opts
->trap_group_name
);
1641 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1642 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1644 if (opts
->present
& DL_OPT_NETNS
)
1645 mnl_attr_put_u32(nlh
,
1646 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1647 DEVLINK_ATTR_NETNS_FD
,
1649 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1650 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1651 opts
->trap_policer_id
);
1652 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1653 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1654 opts
->trap_policer_rate
);
1655 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1656 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1657 opts
->trap_policer_burst
);
1660 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1661 uint64_t o_required
, uint64_t o_optional
)
1665 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1668 dl_opts_put(nlh
, dl
);
1672 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1674 struct dl_opts
*opts
= &dl
->opts
;
1675 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1676 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1677 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1678 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1680 if (opts
->present
& DL_OPT_HANDLE
&&
1681 attr_bus_name
&& attr_dev_name
) {
1682 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1683 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1685 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1686 strcmp(dev_name
, opts
->dev_name
) != 0)
1689 if (opts
->present
& DL_OPT_HANDLEP
&&
1690 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1691 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1692 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1693 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1695 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1696 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1697 port_index
!= opts
->port_index
)
1700 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1701 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1703 if (sb_index
!= opts
->sb_index
)
1709 static void cmd_dev_help(void)
1711 pr_err("Usage: devlink dev show [ DEV ]\n");
1712 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1713 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1714 pr_err(" [ encap-mode { none | basic } ]\n");
1715 pr_err(" devlink dev eswitch show DEV\n");
1716 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1717 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1718 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1719 pr_err(" devlink dev info [ DEV ]\n");
1720 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1723 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1724 const char *dev_name
)
1726 if (!dl
->arr_last
.present
)
1728 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1729 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1732 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1733 const char *dev_name
)
1735 dl
->arr_last
.present
= true;
1736 free(dl
->arr_last
.dev_name
);
1737 free(dl
->arr_last
.bus_name
);
1738 dl
->arr_last
.bus_name
= strdup(bus_name
);
1739 dl
->arr_last
.dev_name
= strdup(dev_name
);
1742 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1743 const char *dev_name
)
1745 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1748 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1749 const char *dev_name
)
1751 return dl
->arr_last
.present
&&
1752 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1755 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1756 bool content
, bool array
)
1758 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1759 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1762 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1764 if (dl
->json_output
) {
1766 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1767 close_json_array(PRINT_JSON
, NULL
);
1768 if (should_arr_last_handle_start(dl
, bus_name
,
1770 open_json_array(PRINT_JSON
, buf
);
1771 open_json_object(NULL
);
1772 arr_last_handle_set(dl
, bus_name
, dev_name
);
1774 open_json_object(NULL
);
1777 open_json_object(buf
);
1781 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1782 __pr_out_indent_dec();
1783 if (should_arr_last_handle_start(dl
, bus_name
,
1785 pr_out("%s%s", buf
, content
? ":" : "");
1787 __pr_out_indent_inc();
1788 arr_last_handle_set(dl
, bus_name
, dev_name
);
1791 pr_out("%s%s", buf
, content
? ":" : "");
1796 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1798 __pr_out_handle_start(dl
, tb
, true, true);
1801 static void pr_out_handle_end(struct dl
*dl
)
1803 if (dl
->json_output
)
1804 close_json_object();
1809 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1811 __pr_out_handle_start(dl
, tb
, false, false);
1812 pr_out_handle_end(dl
);
1815 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1816 const char *dev_name
, uint32_t port_index
)
1818 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1819 dl
->arr_last
.port_index
== port_index
;
1822 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1823 const char *dev_name
, uint32_t port_index
)
1825 arr_last_handle_set(dl
, bus_name
, dev_name
);
1826 dl
->arr_last
.port_index
= port_index
;
1829 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1830 const char *bus_name
,
1831 const char *dev_name
,
1832 uint32_t port_index
)
1834 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1837 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1838 const char *bus_name
,
1839 const char *dev_name
,
1840 uint32_t port_index
)
1842 return dl
->arr_last
.present
&&
1843 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1846 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1847 const char *dev_name
,
1848 uint32_t port_index
, bool try_nice
,
1851 static char buf
[64];
1852 char *ifname
= NULL
;
1854 if (dl
->no_nice_names
|| !try_nice
||
1855 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1856 port_index
, &ifname
) != 0)
1857 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1859 sprintf(buf
, "%s", ifname
);
1861 if (dl
->json_output
) {
1863 if (should_arr_last_port_handle_end(dl
, bus_name
,
1866 close_json_array(PRINT_JSON
, NULL
);
1867 if (should_arr_last_port_handle_start(dl
, bus_name
,
1870 open_json_array(PRINT_JSON
, buf
);
1871 open_json_object(NULL
);
1872 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1875 open_json_object(NULL
);
1878 open_json_object(buf
);
1885 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1887 const char *bus_name
;
1888 const char *dev_name
;
1889 uint32_t port_index
;
1891 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1892 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1893 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1894 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1897 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1899 const char *bus_name
;
1900 const char *dev_name
;
1901 uint32_t port_index
;
1903 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1904 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1905 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1906 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1909 static void pr_out_port_handle_end(struct dl
*dl
)
1911 if (dl
->json_output
)
1912 close_json_object();
1917 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1919 __pr_out_indent_newline(dl
);
1920 if (val
== (uint64_t) -1)
1921 return print_string_name_value(name
, "unlimited");
1923 if (dl
->json_output
)
1924 print_u64(PRINT_JSON
, name
, NULL
, val
);
1926 pr_out("%s %"PRIu64
, name
, val
);
1929 static bool is_binary_eol(int i
)
1934 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1939 if (dl
->json_output
)
1940 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1942 pr_out("%02x ", data
[i
]);
1944 if (!dl
->json_output
&& is_binary_eol(i
))
1947 if (!dl
->json_output
&& !is_binary_eol(i
))
1951 static void pr_out_name(struct dl
*dl
, const char *name
)
1953 __pr_out_indent_newline(dl
);
1954 if (dl
->json_output
)
1955 print_string(PRINT_JSON
, name
, NULL
, NULL
);
1957 pr_out("%s:", name
);
1960 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1962 if (dl
->json_output
) {
1963 print_uint(PRINT_JSON
, "address", NULL
, addr
);
1964 open_json_array(PRINT_JSON
, "data");
1968 static void pr_out_region_chunk_end(struct dl
*dl
)
1970 if (dl
->json_output
)
1971 close_json_array(PRINT_JSON
, NULL
);
1974 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1977 static uint64_t align_val
;
1980 pr_out_region_chunk_start(dl
, addr
);
1982 if (!dl
->json_output
)
1983 if (!(align_val
% 16))
1984 pr_out("%s%016"PRIx64
" ",
1985 align_val
? "\n" : "",
1990 if (dl
->json_output
)
1991 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1993 pr_out("%02x ", data
[i
]);
1998 pr_out_region_chunk_end(dl
);
2001 static void pr_out_section_start(struct dl
*dl
, const char *name
)
2003 if (dl
->json_output
) {
2004 open_json_object(NULL
);
2005 open_json_object(name
);
2009 static void pr_out_section_end(struct dl
*dl
)
2011 if (dl
->json_output
) {
2012 if (dl
->arr_last
.present
)
2013 close_json_array(PRINT_JSON
, NULL
);
2014 close_json_object();
2015 close_json_object();
2019 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2021 if (dl
->json_output
) {
2022 open_json_array(PRINT_JSON
, name
);
2024 __pr_out_indent_inc();
2026 pr_out("%s:", name
);
2027 __pr_out_indent_inc();
2032 static void pr_out_array_end(struct dl
*dl
)
2034 if (dl
->json_output
) {
2035 close_json_array(PRINT_JSON
, NULL
);
2037 __pr_out_indent_dec();
2038 __pr_out_indent_dec();
2042 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2044 if (dl
->json_output
) {
2045 open_json_object(name
);
2047 __pr_out_indent_inc();
2049 pr_out("%s:", name
);
2050 __pr_out_indent_inc();
2055 static void pr_out_object_end(struct dl
*dl
)
2057 if (dl
->json_output
) {
2058 close_json_object();
2060 __pr_out_indent_dec();
2061 __pr_out_indent_dec();
2065 static void pr_out_entry_start(struct dl
*dl
)
2067 if (dl
->json_output
)
2068 open_json_object(NULL
);
2071 static void pr_out_entry_end(struct dl
*dl
)
2073 if (dl
->json_output
)
2074 close_json_object();
2079 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2081 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2087 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2088 if (err
!= MNL_CB_OK
)
2091 pr_out_object_start(dl
, "stats");
2092 pr_out_object_start(dl
, "rx");
2093 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2094 pr_out_u64(dl
, "bytes",
2095 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2096 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2097 pr_out_u64(dl
, "packets",
2098 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2099 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2100 pr_out_u64(dl
, "dropped",
2101 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2102 pr_out_object_end(dl
);
2103 pr_out_object_end(dl
);
2106 static const char *param_cmode_name(uint8_t cmode
)
2109 case DEVLINK_PARAM_CMODE_RUNTIME
:
2110 return PARAM_CMODE_RUNTIME_STR
;
2111 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2112 return PARAM_CMODE_DRIVERINIT_STR
;
2113 case DEVLINK_PARAM_CMODE_PERMANENT
:
2114 return PARAM_CMODE_PERMANENT_STR
;
2115 default: return "<unknown type>";
2119 static const char *eswitch_mode_name(uint32_t mode
)
2122 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2123 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2124 default: return "<unknown mode>";
2128 static const char *eswitch_inline_mode_name(uint32_t mode
)
2131 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2132 return ESWITCH_INLINE_MODE_NONE
;
2133 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2134 return ESWITCH_INLINE_MODE_LINK
;
2135 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2136 return ESWITCH_INLINE_MODE_NETWORK
;
2137 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2138 return ESWITCH_INLINE_MODE_TRANSPORT
;
2140 return "<unknown mode>";
2144 static const char *eswitch_encap_mode_name(uint32_t mode
)
2147 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2148 return ESWITCH_ENCAP_MODE_NONE
;
2149 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2150 return ESWITCH_ENCAP_MODE_BASIC
;
2152 return "<unknown mode>";
2156 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2158 __pr_out_handle_start(dl
, tb
, true, false);
2160 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2161 check_indent_newline(dl
);
2162 print_string(PRINT_ANY
, "mode", "mode %s",
2163 eswitch_mode_name(mnl_attr_get_u16(
2164 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2166 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2167 check_indent_newline(dl
);
2168 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2169 eswitch_inline_mode_name(mnl_attr_get_u8(
2170 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2172 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2173 check_indent_newline(dl
);
2174 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2175 eswitch_encap_mode_name(mnl_attr_get_u8(
2176 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2179 pr_out_handle_end(dl
);
2182 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2184 struct dl
*dl
= data
;
2185 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2186 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2188 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2189 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2190 return MNL_CB_ERROR
;
2191 pr_out_eswitch(dl
, tb
);
2195 static int cmd_dev_eswitch_show(struct dl
*dl
)
2197 struct nlmsghdr
*nlh
;
2200 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2201 NLM_F_REQUEST
| NLM_F_ACK
);
2203 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2207 pr_out_section_start(dl
, "dev");
2208 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2209 pr_out_section_end(dl
);
2213 static int cmd_dev_eswitch_set(struct dl
*dl
)
2215 struct nlmsghdr
*nlh
;
2218 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2219 NLM_F_REQUEST
| NLM_F_ACK
);
2221 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2222 DL_OPT_ESWITCH_MODE
|
2223 DL_OPT_ESWITCH_INLINE_MODE
|
2224 DL_OPT_ESWITCH_ENCAP_MODE
);
2229 if (dl
->opts
.present
== 1) {
2230 pr_err("Need to set at least one option\n");
2234 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2237 static int cmd_dev_eswitch(struct dl
*dl
)
2239 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2242 } else if (dl_argv_match(dl
, "set")) {
2244 return cmd_dev_eswitch_set(dl
);
2245 } else if (dl_argv_match(dl
, "show")) {
2247 return cmd_dev_eswitch_show(dl
);
2249 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2253 struct param_val_conv
{
2259 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2260 uint32_t len
, const char *name
)
2264 for (i
= 0; i
< len
; i
++)
2265 if (!strcmp(param_val_conv
[i
].name
, name
))
2272 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2273 uint32_t len
, const char *name
, const char *vstr
,
2278 for (i
= 0; i
< len
; i
++)
2279 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2280 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2281 *vuint
= param_val_conv
[i
].vuint
;
2289 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2290 uint32_t len
, const char *name
, uint32_t vuint
,
2295 for (i
= 0; i
< len
; i
++)
2296 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2297 param_val_conv
[i
].vuint
== vuint
) {
2298 *vstr
= param_val_conv
[i
].vstr
;
2305 static const struct param_val_conv param_val_conv
[] = {
2307 .name
= "fw_load_policy",
2309 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2312 .name
= "fw_load_policy",
2314 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2317 .name
= "reset_dev_on_drv_probe",
2319 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2322 .name
= "fw_load_policy",
2324 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2327 .name
= "reset_dev_on_drv_probe",
2329 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2332 .name
= "reset_dev_on_drv_probe",
2334 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2337 .name
= "reset_dev_on_drv_probe",
2339 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2343 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2345 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2346 int nla_type
, struct nlattr
*nl
)
2348 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2349 struct nlattr
*val_attr
;
2354 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2355 if (err
!= MNL_CB_OK
)
2358 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2359 (nla_type
!= MNL_TYPE_FLAG
&&
2360 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2363 check_indent_newline(dl
);
2364 print_string(PRINT_ANY
, "cmode", "cmode %s",
2365 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2367 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2369 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2375 err
= param_val_conv_str_get(param_val_conv
,
2378 mnl_attr_get_u8(val_attr
),
2382 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2384 print_uint(PRINT_ANY
, "value", " value %u",
2385 mnl_attr_get_u8(val_attr
));
2390 err
= param_val_conv_str_get(param_val_conv
,
2393 mnl_attr_get_u16(val_attr
),
2397 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2399 print_uint(PRINT_ANY
, "value", " value %u",
2400 mnl_attr_get_u16(val_attr
));
2405 err
= param_val_conv_str_get(param_val_conv
,
2408 mnl_attr_get_u32(val_attr
),
2412 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2414 print_uint(PRINT_ANY
, "value", " value %u",
2415 mnl_attr_get_u32(val_attr
));
2418 case MNL_TYPE_STRING
:
2419 print_string(PRINT_ANY
, "value", " value %s",
2420 mnl_attr_get_str(val_attr
));
2423 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2428 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2430 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2431 struct nlattr
*param_value_attr
;
2432 const char *nla_name
;
2436 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2437 if (err
!= MNL_CB_OK
)
2439 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2440 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2441 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2445 pr_out_handle_start_arr(dl
, tb
);
2447 __pr_out_handle_start(dl
, tb
, true, false);
2449 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2451 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2452 check_indent_newline(dl
);
2453 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2454 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2455 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2457 print_string(PRINT_ANY
, "type", "type %s", "generic");
2459 pr_out_array_start(dl
, "values");
2460 mnl_attr_for_each_nested(param_value_attr
,
2461 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2462 pr_out_entry_start(dl
);
2463 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2464 pr_out_entry_end(dl
);
2466 pr_out_array_end(dl
);
2467 pr_out_handle_end(dl
);
2470 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2472 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2473 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2474 struct dl
*dl
= data
;
2476 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2477 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2478 !tb
[DEVLINK_ATTR_PARAM
])
2479 return MNL_CB_ERROR
;
2480 pr_out_param(dl
, tb
, true);
2496 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2498 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2499 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2500 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2501 struct nlattr
*param_value_attr
;
2502 enum devlink_param_cmode cmode
;
2503 struct param_ctx
*ctx
= data
;
2504 struct dl
*dl
= ctx
->dl
;
2508 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2509 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2510 !tb
[DEVLINK_ATTR_PARAM
])
2511 return MNL_CB_ERROR
;
2513 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2514 if (err
!= MNL_CB_OK
)
2515 return MNL_CB_ERROR
;
2517 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2518 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2519 return MNL_CB_ERROR
;
2521 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2522 mnl_attr_for_each_nested(param_value_attr
,
2523 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2524 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2525 struct nlattr
*val_attr
;
2527 err
= mnl_attr_parse_nested(param_value_attr
,
2528 attr_cb
, nla_value
);
2529 if (err
!= MNL_CB_OK
)
2530 return MNL_CB_ERROR
;
2532 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2533 (nla_type
!= MNL_TYPE_FLAG
&&
2534 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2535 return MNL_CB_ERROR
;
2537 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2538 if (cmode
== dl
->opts
.cmode
) {
2539 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2542 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2545 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2548 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2550 case MNL_TYPE_STRING
:
2551 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2554 ctx
->value
.vbool
= val_attr
? true : false;
2560 ctx
->nla_type
= nla_type
;
2564 static int cmd_dev_param_set(struct dl
*dl
)
2566 struct param_ctx ctx
= {};
2567 struct nlmsghdr
*nlh
;
2575 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2577 DL_OPT_PARAM_VALUE
|
2578 DL_OPT_PARAM_CMODE
, 0);
2582 /* Get value type */
2583 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2584 NLM_F_REQUEST
| NLM_F_ACK
);
2585 dl_opts_put(nlh
, dl
);
2588 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2592 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2593 NLM_F_REQUEST
| NLM_F_ACK
);
2594 dl_opts_put(nlh
, dl
);
2596 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2597 dl
->opts
.param_name
);
2599 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2600 switch (ctx
.nla_type
) {
2603 err
= param_val_conv_uint_get(param_val_conv
,
2605 dl
->opts
.param_name
,
2606 dl
->opts
.param_value
,
2610 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2613 goto err_param_value_parse
;
2614 if (val_u8
== ctx
.value
.vu8
)
2616 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2620 err
= param_val_conv_uint_get(param_val_conv
,
2622 dl
->opts
.param_name
,
2623 dl
->opts
.param_value
,
2627 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2630 goto err_param_value_parse
;
2631 if (val_u16
== ctx
.value
.vu16
)
2633 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2637 err
= param_val_conv_uint_get(param_val_conv
,
2639 dl
->opts
.param_name
,
2640 dl
->opts
.param_value
,
2643 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2645 goto err_param_value_parse
;
2646 if (val_u32
== ctx
.value
.vu32
)
2648 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2651 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2653 goto err_param_value_parse
;
2654 if (val_bool
== ctx
.value
.vbool
)
2657 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2660 case MNL_TYPE_STRING
:
2661 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2662 dl
->opts
.param_value
);
2663 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2667 printf("Value type not supported\n");
2670 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2672 err_param_value_parse
:
2673 pr_err("Value \"%s\" is not a number or not within range\n",
2674 dl
->opts
.param_value
);
2678 static int cmd_dev_param_show(struct dl
*dl
)
2680 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2681 struct nlmsghdr
*nlh
;
2684 if (dl_argc(dl
) == 0)
2685 flags
|= NLM_F_DUMP
;
2687 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2689 if (dl_argc(dl
) > 0) {
2690 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2691 DL_OPT_PARAM_NAME
, 0);
2696 pr_out_section_start(dl
, "param");
2697 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2698 pr_out_section_end(dl
);
2702 static int cmd_dev_param(struct dl
*dl
)
2704 if (dl_argv_match(dl
, "help")) {
2707 } else if (dl_argv_match(dl
, "show") ||
2708 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2710 return cmd_dev_param_show(dl
);
2711 } else if (dl_argv_match(dl
, "set")) {
2713 return cmd_dev_param_set(dl
);
2715 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2718 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2720 struct dl
*dl
= data
;
2721 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2722 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2723 uint8_t reload_failed
= 0;
2725 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2726 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2727 return MNL_CB_ERROR
;
2729 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2730 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2732 if (reload_failed
) {
2733 __pr_out_handle_start(dl
, tb
, true, false);
2734 check_indent_newline(dl
);
2735 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2736 pr_out_handle_end(dl
);
2738 pr_out_handle(dl
, tb
);
2744 static int cmd_dev_show(struct dl
*dl
)
2746 struct nlmsghdr
*nlh
;
2747 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2750 if (dl_argc(dl
) == 0)
2751 flags
|= NLM_F_DUMP
;
2753 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2755 if (dl_argc(dl
) > 0) {
2756 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2761 pr_out_section_start(dl
, "dev");
2762 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2763 pr_out_section_end(dl
);
2767 static void cmd_dev_reload_help(void)
2769 pr_err("Usage: devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2772 static int cmd_dev_reload(struct dl
*dl
)
2774 struct nlmsghdr
*nlh
;
2777 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2778 cmd_dev_reload_help();
2782 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2783 NLM_F_REQUEST
| NLM_F_ACK
);
2785 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2789 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2792 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2793 const char *name
, int type
)
2795 struct nlattr
*version
;
2797 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2798 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2799 const char *ver_value
;
2800 const char *ver_name
;
2803 if (mnl_attr_get_type(version
) != type
)
2806 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2807 if (err
!= MNL_CB_OK
)
2810 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2811 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2815 pr_out_object_start(dl
, name
);
2819 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2820 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2822 check_indent_newline(dl
);
2823 print_string_name_value(ver_name
, ver_value
);
2824 if (!dl
->json_output
)
2829 pr_out_object_end(dl
);
2832 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2833 struct nlattr
**tb
, bool has_versions
)
2835 __pr_out_handle_start(dl
, tb
, true, false);
2837 __pr_out_indent_inc();
2838 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2839 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2841 if (!dl
->json_output
)
2843 check_indent_newline(dl
);
2844 print_string(PRINT_ANY
, "driver", "driver %s",
2845 mnl_attr_get_str(nla_drv
));
2848 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2849 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2851 if (!dl
->json_output
)
2853 check_indent_newline(dl
);
2854 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2855 mnl_attr_get_str(nla_sn
));
2857 __pr_out_indent_dec();
2860 pr_out_object_start(dl
, "versions");
2862 pr_out_versions_single(dl
, nlh
, "fixed",
2863 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2864 pr_out_versions_single(dl
, nlh
, "running",
2865 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2866 pr_out_versions_single(dl
, nlh
, "stored",
2867 DEVLINK_ATTR_INFO_VERSION_STORED
);
2869 pr_out_object_end(dl
);
2872 pr_out_handle_end(dl
);
2875 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2877 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2878 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2879 bool has_versions
, has_info
;
2880 struct dl
*dl
= data
;
2882 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2884 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2885 return MNL_CB_ERROR
;
2887 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2888 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2889 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2890 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2891 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2895 pr_out_info(dl
, nlh
, tb
, has_versions
);
2900 static void cmd_dev_info_help(void)
2902 pr_err("Usage: devlink dev info [ DEV ]\n");
2905 static int cmd_dev_info(struct dl
*dl
)
2907 struct nlmsghdr
*nlh
;
2908 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2911 if (dl_argv_match(dl
, "help")) {
2912 cmd_dev_info_help();
2916 if (dl_argc(dl
) == 0)
2917 flags
|= NLM_F_DUMP
;
2919 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2921 if (dl_argc(dl
) > 0) {
2922 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2927 pr_out_section_start(dl
, "info");
2928 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2929 pr_out_section_end(dl
);
2933 static void cmd_dev_flash_help(void)
2935 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2939 struct cmd_dev_flash_status_ctx
{
2942 char *last_component
;
2943 uint8_t not_first
:1,
2949 static int nullstrcmp(const char *str1
, const char *str2
)
2952 return strcmp(str1
, str2
);
2955 return str1
? 1 : -1;
2958 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2960 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2961 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2962 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2963 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2964 const char *component
= NULL
;
2965 uint64_t done
= 0, total
= 0;
2966 const char *msg
= NULL
;
2967 const char *bus_name
;
2968 const char *dev_name
;
2970 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2971 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2974 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2975 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2976 return MNL_CB_ERROR
;
2977 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2978 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2979 if (strcmp(bus_name
, opts
->bus_name
) ||
2980 strcmp(dev_name
, opts
->dev_name
))
2981 return MNL_CB_ERROR
;
2983 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2985 free(ctx
->last_msg
);
2986 free(ctx
->last_component
);
2987 ctx
->received_end
= 1;
2991 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2992 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2993 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2994 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2995 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2996 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2997 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2998 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3000 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3001 !nullstrcmp(component
, ctx
->last_component
) &&
3002 ctx
->last_pc
&& ctx
->not_first
) {
3003 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3008 pr_out("[%s] ", component
);
3009 free(ctx
->last_component
);
3010 ctx
->last_component
= strdup(component
);
3014 free(ctx
->last_msg
);
3015 ctx
->last_msg
= strdup(msg
);
3019 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3030 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3031 struct mnlg_socket
*nlg_ntf
,
3034 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3041 for (i
= 0; i
< 3; i
++)
3043 FD_SET(pipe_r
, &fds
[0]);
3045 FD_SET(nlfd
, &fds
[0]);
3049 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3052 pr_err("select() failed\n");
3055 if (FD_ISSET(nlfd
, &fds
[0])) {
3056 err
= _mnlg_socket_recv_run(nlg_ntf
,
3057 cmd_dev_flash_status_cb
, ctx
);
3061 if (FD_ISSET(pipe_r
, &fds
[0])) {
3062 err
= read(pipe_r
, &err2
, sizeof(err2
));
3064 pr_err("Failed to read pipe\n");
3069 ctx
->flash_done
= 1;
3075 static int cmd_dev_flash(struct dl
*dl
)
3077 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3078 struct mnlg_socket
*nlg_ntf
;
3079 struct nlmsghdr
*nlh
;
3085 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3086 cmd_dev_flash_help();
3090 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3091 NLM_F_REQUEST
| NLM_F_ACK
);
3093 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3094 DL_OPT_FLASH_COMPONENT
);
3098 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3102 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3106 err
= pipe(pipe_fds
);
3109 pipe_r
= pipe_fds
[0];
3110 pipe_w
= pipe_fds
[1];
3118 /* In child, just execute the flash and pass returned
3119 * value through pipe once it is done.
3124 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3125 cc
= write(pipe_w
, &err
, sizeof(err
));
3127 exit(cc
!= sizeof(err
));
3132 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3135 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3137 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3140 mnlg_socket_close(nlg_ntf
);
3144 static int cmd_dev(struct dl
*dl
)
3146 if (dl_argv_match(dl
, "help")) {
3149 } else if (dl_argv_match(dl
, "show") ||
3150 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3152 return cmd_dev_show(dl
);
3153 } else if (dl_argv_match(dl
, "eswitch")) {
3155 return cmd_dev_eswitch(dl
);
3156 } else if (dl_argv_match(dl
, "reload")) {
3158 return cmd_dev_reload(dl
);
3159 } else if (dl_argv_match(dl
, "param")) {
3161 return cmd_dev_param(dl
);
3162 } else if (dl_argv_match(dl
, "info")) {
3164 return cmd_dev_info(dl
);
3165 } else if (dl_argv_match(dl
, "flash")) {
3167 return cmd_dev_flash(dl
);
3169 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3173 static void cmd_port_help(void)
3175 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3176 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3177 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3178 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3181 static const char *port_type_name(uint32_t type
)
3184 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3185 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3186 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3187 case DEVLINK_PORT_TYPE_IB
: return "ib";
3188 default: return "<unknown type>";
3192 static const char *port_flavour_name(uint16_t flavour
)
3195 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3197 case DEVLINK_PORT_FLAVOUR_CPU
:
3199 case DEVLINK_PORT_FLAVOUR_DSA
:
3201 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3203 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3205 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3208 return "<unknown flavour>";
3212 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3216 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3217 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3218 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3220 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3221 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3222 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3226 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3228 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3229 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3231 pr_out_port_handle_start(dl
, tb
, false);
3232 check_indent_newline(dl
);
3234 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3236 print_string(PRINT_ANY
, "type", "type %s",
3237 port_type_name(port_type
));
3239 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3241 if (port_type
!= des_port_type
)
3242 print_string(PRINT_ANY
, "des_type", " des_type %s",
3243 port_type_name(des_port_type
));
3246 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3247 print_string(PRINT_ANY
, "netdev", " netdev %s",
3248 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3250 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3251 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3252 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3254 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3255 uint16_t port_flavour
=
3256 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3258 print_string(PRINT_ANY
, "flavour", " flavour %s",
3259 port_flavour_name(port_flavour
));
3261 switch (port_flavour
) {
3262 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3263 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3264 pr_out_port_pfvf_num(dl
, tb
);
3270 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3271 uint32_t port_number
;
3273 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3274 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3276 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3277 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3278 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3279 pr_out_port_handle_end(dl
);
3282 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3284 struct dl
*dl
= data
;
3285 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3286 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3288 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3289 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3290 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3291 return MNL_CB_ERROR
;
3292 pr_out_port(dl
, tb
);
3296 static int cmd_port_show(struct dl
*dl
)
3298 struct nlmsghdr
*nlh
;
3299 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3302 if (dl_argc(dl
) == 0)
3303 flags
|= NLM_F_DUMP
;
3305 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3307 if (dl_argc(dl
) > 0) {
3308 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3313 pr_out_section_start(dl
, "port");
3314 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3315 pr_out_section_end(dl
);
3319 static int cmd_port_set(struct dl
*dl
)
3321 struct nlmsghdr
*nlh
;
3324 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3325 NLM_F_REQUEST
| NLM_F_ACK
);
3327 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3331 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3334 static int cmd_port_split(struct dl
*dl
)
3336 struct nlmsghdr
*nlh
;
3339 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3340 NLM_F_REQUEST
| NLM_F_ACK
);
3342 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3346 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3349 static int cmd_port_unsplit(struct dl
*dl
)
3351 struct nlmsghdr
*nlh
;
3354 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3355 NLM_F_REQUEST
| NLM_F_ACK
);
3357 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3361 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3364 static int cmd_port(struct dl
*dl
)
3366 if (dl_argv_match(dl
, "help")) {
3369 } else if (dl_argv_match(dl
, "show") ||
3370 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3372 return cmd_port_show(dl
);
3373 } else if (dl_argv_match(dl
, "set")) {
3375 return cmd_port_set(dl
);
3376 } else if (dl_argv_match(dl
, "split")) {
3378 return cmd_port_split(dl
);
3379 } else if (dl_argv_match(dl
, "unsplit")) {
3381 return cmd_port_unsplit(dl
);
3383 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3387 static void cmd_sb_help(void)
3389 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3390 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3391 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3392 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3393 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3394 pr_err(" pool POOL_INDEX ]\n");
3395 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3396 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3397 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3398 pr_err(" type { ingress | egress } ]\n");
3399 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3400 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3401 pr_err(" th THRESHOLD\n");
3402 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3403 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3404 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3407 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3409 pr_out_handle_start_arr(dl
, tb
);
3410 check_indent_newline(dl
);
3411 print_uint(PRINT_ANY
, "sb", "sb %u",
3412 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3413 print_uint(PRINT_ANY
, "size", " size %u",
3414 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3415 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3416 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3417 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3418 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3419 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3420 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3421 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3422 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3423 pr_out_handle_end(dl
);
3426 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3428 struct dl
*dl
= data
;
3429 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3430 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3432 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3433 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3434 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3435 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3436 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3437 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3438 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3439 return MNL_CB_ERROR
;
3444 static int cmd_sb_show(struct dl
*dl
)
3446 struct nlmsghdr
*nlh
;
3447 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3450 if (dl_argc(dl
) == 0)
3451 flags
|= NLM_F_DUMP
;
3453 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3455 if (dl_argc(dl
) > 0) {
3456 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3461 pr_out_section_start(dl
, "sb");
3462 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3463 pr_out_section_end(dl
);
3467 static const char *pool_type_name(uint8_t type
)
3470 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3471 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3472 default: return "<unknown type>";
3476 static const char *threshold_type_name(uint8_t type
)
3479 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3480 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3481 default: return "<unknown type>";
3485 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3487 pr_out_handle_start_arr(dl
, tb
);
3488 check_indent_newline(dl
);
3489 print_uint(PRINT_ANY
, "sb", "sb %u",
3490 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3491 print_uint(PRINT_ANY
, "pool", " pool %u",
3492 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3493 print_string(PRINT_ANY
, "type", " type %s",
3494 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3495 print_uint(PRINT_ANY
, "size", " size %u",
3496 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3497 print_string(PRINT_ANY
, "thtype", " thtype %s",
3498 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3499 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3500 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3501 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3502 pr_out_handle_end(dl
);
3505 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3507 struct dl
*dl
= data
;
3508 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3509 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3511 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3512 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3513 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3514 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3515 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3516 return MNL_CB_ERROR
;
3517 pr_out_sb_pool(dl
, tb
);
3521 static int cmd_sb_pool_show(struct dl
*dl
)
3523 struct nlmsghdr
*nlh
;
3524 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3527 if (dl_argc(dl
) == 0)
3528 flags
|= NLM_F_DUMP
;
3530 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3532 if (dl_argc(dl
) > 0) {
3533 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3539 pr_out_section_start(dl
, "pool");
3540 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3541 pr_out_section_end(dl
);
3545 static int cmd_sb_pool_set(struct dl
*dl
)
3547 struct nlmsghdr
*nlh
;
3550 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3551 NLM_F_REQUEST
| NLM_F_ACK
);
3553 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3554 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3558 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3561 static int cmd_sb_pool(struct dl
*dl
)
3563 if (dl_argv_match(dl
, "help")) {
3566 } else if (dl_argv_match(dl
, "show") ||
3567 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3569 return cmd_sb_pool_show(dl
);
3570 } else if (dl_argv_match(dl
, "set")) {
3572 return cmd_sb_pool_set(dl
);
3574 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3578 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3580 pr_out_port_handle_start_arr(dl
, tb
, true);
3581 check_indent_newline(dl
);
3582 print_uint(PRINT_ANY
, "sb", "sb %u",
3583 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3584 print_uint(PRINT_ANY
, "pool", " pool %u",
3585 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3586 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3587 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3588 pr_out_port_handle_end(dl
);
3591 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3593 struct dl
*dl
= data
;
3594 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3595 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3597 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3598 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3599 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3600 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3601 return MNL_CB_ERROR
;
3602 pr_out_sb_port_pool(dl
, tb
);
3606 static int cmd_sb_port_pool_show(struct dl
*dl
)
3608 struct nlmsghdr
*nlh
;
3609 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3612 if (dl_argc(dl
) == 0)
3613 flags
|= NLM_F_DUMP
;
3615 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3617 if (dl_argc(dl
) > 0) {
3618 err
= dl_argv_parse_put(nlh
, dl
,
3619 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3625 pr_out_section_start(dl
, "port_pool");
3626 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3627 pr_out_section_end(dl
);
3631 static int cmd_sb_port_pool_set(struct dl
*dl
)
3633 struct nlmsghdr
*nlh
;
3636 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3637 NLM_F_REQUEST
| NLM_F_ACK
);
3639 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3640 DL_OPT_SB_TH
, DL_OPT_SB
);
3644 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3647 static int cmd_sb_port_pool(struct dl
*dl
)
3649 if (dl_argv_match(dl
, "help")) {
3652 } else if (dl_argv_match(dl
, "show") ||
3653 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3655 return cmd_sb_port_pool_show(dl
);
3656 } else if (dl_argv_match(dl
, "set")) {
3658 return cmd_sb_port_pool_set(dl
);
3660 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3664 static int cmd_sb_port(struct dl
*dl
)
3666 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3669 } else if (dl_argv_match(dl
, "pool")) {
3671 return cmd_sb_port_pool(dl
);
3673 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3677 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3679 pr_out_port_handle_start_arr(dl
, tb
, true);
3680 check_indent_newline(dl
);
3681 print_uint(PRINT_ANY
, "sb", "sb %u",
3682 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3683 print_uint(PRINT_ANY
, "tc", " tc %u",
3684 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3685 print_string(PRINT_ANY
, "type", " type %s",
3686 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3687 print_uint(PRINT_ANY
, "pool", " pool %u",
3688 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3689 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3690 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3691 pr_out_port_handle_end(dl
);
3694 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3696 struct dl
*dl
= data
;
3697 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3698 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3700 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3701 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3702 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3703 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3704 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3705 return MNL_CB_ERROR
;
3706 pr_out_sb_tc_bind(dl
, tb
);
3710 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3712 struct nlmsghdr
*nlh
;
3713 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3716 if (dl_argc(dl
) == 0)
3717 flags
|= NLM_F_DUMP
;
3719 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3721 if (dl_argc(dl
) > 0) {
3722 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3723 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3728 pr_out_section_start(dl
, "tc_bind");
3729 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3730 pr_out_section_end(dl
);
3734 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3736 struct nlmsghdr
*nlh
;
3739 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3740 NLM_F_REQUEST
| NLM_F_ACK
);
3742 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3743 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3748 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3751 static int cmd_sb_tc_bind(struct dl
*dl
)
3753 if (dl_argv_match(dl
, "help")) {
3756 } else if (dl_argv_match(dl
, "show") ||
3757 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3759 return cmd_sb_tc_bind_show(dl
);
3760 } else if (dl_argv_match(dl
, "set")) {
3762 return cmd_sb_tc_bind_set(dl
);
3764 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3768 static int cmd_sb_tc(struct dl
*dl
)
3770 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3773 } else if (dl_argv_match(dl
, "bind")) {
3775 return cmd_sb_tc_bind(dl
);
3777 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3782 struct list_head list
;
3786 uint32_t bound_pool_index
;
3790 struct list_head list
;
3793 uint32_t port_index
;
3795 struct list_head pool_list
;
3796 struct list_head ing_tc_list
;
3797 struct list_head eg_tc_list
;
3803 struct list_head port_list
;
3806 static struct occ_item
*occ_item_alloc(void)
3808 return calloc(1, sizeof(struct occ_item
));
3811 static void occ_item_free(struct occ_item
*occ_item
)
3816 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3818 struct occ_port
*occ_port
;
3820 occ_port
= calloc(1, sizeof(*occ_port
));
3823 occ_port
->port_index
= port_index
;
3824 INIT_LIST_HEAD(&occ_port
->pool_list
);
3825 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3826 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3830 static void occ_port_free(struct occ_port
*occ_port
)
3832 struct occ_item
*occ_item
, *tmp
;
3834 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3835 occ_item_free(occ_item
);
3836 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3837 occ_item_free(occ_item
);
3838 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3839 occ_item_free(occ_item
);
3842 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3844 struct occ_show
*occ_show
;
3846 occ_show
= calloc(1, sizeof(*occ_show
));
3850 INIT_LIST_HEAD(&occ_show
->port_list
);
3854 static void occ_show_free(struct occ_show
*occ_show
)
3856 struct occ_port
*occ_port
, *tmp
;
3858 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3859 occ_port_free(occ_port
);
3862 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3865 struct occ_port
*occ_port
;
3866 uint32_t port_index
;
3868 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3870 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3871 if (occ_port
->port_index
== port_index
)
3874 occ_port
= occ_port_alloc(port_index
);
3877 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3881 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3884 struct occ_item
*occ_item
;
3887 pr_out_sp(7, " %s:", label
);
3888 list_for_each_entry(occ_item
, list
, list
) {
3889 if ((i
- 1) % 4 == 0 && i
!= 1)
3892 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3893 occ_item
->bound_pool_index
);
3895 pr_out_sp(7, "%2u:", occ_item
->index
);
3896 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3900 if ((i
- 1) % 4 != 0)
3904 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3905 struct list_head
*list
,
3908 struct occ_item
*occ_item
;
3911 open_json_object(label
);
3912 list_for_each_entry(occ_item
, list
, list
) {
3913 sprintf(buf
, "%u", occ_item
->index
);
3914 open_json_object(buf
);
3916 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3917 occ_item
->bound_pool_index
);
3918 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3919 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3920 close_json_object();
3922 close_json_object();
3925 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3927 if (dl
->json_output
) {
3928 pr_out_json_occ_show_item_list(dl
, "pool",
3929 &occ_port
->pool_list
, false);
3930 pr_out_json_occ_show_item_list(dl
, "itc",
3931 &occ_port
->ing_tc_list
, true);
3932 pr_out_json_occ_show_item_list(dl
, "etc",
3933 &occ_port
->eg_tc_list
, true);
3936 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3937 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3938 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3942 static void pr_out_occ_show(struct occ_show
*occ_show
)
3944 struct dl
*dl
= occ_show
->dl
;
3945 struct dl_opts
*opts
= &dl
->opts
;
3946 struct occ_port
*occ_port
;
3948 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3949 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3950 occ_port
->port_index
, true, false);
3951 pr_out_occ_show_port(dl
, occ_port
);
3952 pr_out_port_handle_end(dl
);
3956 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3959 struct occ_port
*occ_port
;
3960 struct occ_item
*occ_item
;
3962 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3965 occ_port
= occ_port_get(occ_show
, tb
);
3967 occ_show
->err
= -ENOMEM
;
3971 occ_item
= occ_item_alloc();
3973 occ_show
->err
= -ENOMEM
;
3976 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3977 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3978 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3979 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3982 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3984 struct occ_show
*occ_show
= data
;
3985 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3986 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3988 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3989 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3990 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3991 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3992 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3993 return MNL_CB_ERROR
;
3994 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3998 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4001 struct occ_port
*occ_port
;
4002 struct occ_item
*occ_item
;
4005 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4008 occ_port
= occ_port_get(occ_show
, tb
);
4010 occ_show
->err
= -ENOMEM
;
4014 occ_item
= occ_item_alloc();
4016 occ_show
->err
= -ENOMEM
;
4019 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4020 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4021 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4022 occ_item
->bound_pool_index
=
4023 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4024 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4025 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4026 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4027 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4028 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4030 occ_item_free(occ_item
);
4033 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4035 struct occ_show
*occ_show
= data
;
4036 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4037 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4039 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4040 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4041 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4042 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4043 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4044 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4045 return MNL_CB_ERROR
;
4046 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4050 static int cmd_sb_occ_show(struct dl
*dl
)
4052 struct nlmsghdr
*nlh
;
4053 struct occ_show
*occ_show
;
4054 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4057 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4061 occ_show
= occ_show_alloc(dl
);
4065 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4067 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4068 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4072 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4074 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4075 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4079 pr_out_section_start(dl
, "occupancy");
4080 pr_out_occ_show(occ_show
);
4081 pr_out_section_end(dl
);
4084 occ_show_free(occ_show
);
4088 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4090 struct nlmsghdr
*nlh
;
4093 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4094 NLM_F_REQUEST
| NLM_F_ACK
);
4096 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4100 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4103 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4105 struct nlmsghdr
*nlh
;
4108 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4109 NLM_F_REQUEST
| NLM_F_ACK
);
4111 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4115 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4118 static int cmd_sb_occ(struct dl
*dl
)
4120 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4123 } else if (dl_argv_match(dl
, "show") ||
4124 dl_argv_match(dl
, "list")) {
4126 return cmd_sb_occ_show(dl
);
4127 } else if (dl_argv_match(dl
, "snapshot")) {
4129 return cmd_sb_occ_snapshot(dl
);
4130 } else if (dl_argv_match(dl
, "clearmax")) {
4132 return cmd_sb_occ_clearmax(dl
);
4134 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4138 static int cmd_sb(struct dl
*dl
)
4140 if (dl_argv_match(dl
, "help")) {
4143 } else if (dl_argv_match(dl
, "show") ||
4144 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4146 return cmd_sb_show(dl
);
4147 } else if (dl_argv_match(dl
, "pool")) {
4149 return cmd_sb_pool(dl
);
4150 } else if (dl_argv_match(dl
, "port")) {
4152 return cmd_sb_port(dl
);
4153 } else if (dl_argv_match(dl
, "tc")) {
4155 return cmd_sb_tc(dl
);
4156 } else if (dl_argv_match(dl
, "occupancy")) {
4158 return cmd_sb_occ(dl
);
4160 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4164 static const char *cmd_name(uint8_t cmd
)
4167 case DEVLINK_CMD_UNSPEC
: return "unspec";
4168 case DEVLINK_CMD_GET
: return "get";
4169 case DEVLINK_CMD_SET
: return "set";
4170 case DEVLINK_CMD_NEW
: return "new";
4171 case DEVLINK_CMD_DEL
: return "del";
4172 case DEVLINK_CMD_PORT_GET
: return "get";
4173 case DEVLINK_CMD_PORT_SET
: return "set";
4174 case DEVLINK_CMD_PORT_NEW
: return "new";
4175 case DEVLINK_CMD_PORT_DEL
: return "del";
4176 case DEVLINK_CMD_PARAM_GET
: return "get";
4177 case DEVLINK_CMD_PARAM_SET
: return "set";
4178 case DEVLINK_CMD_PARAM_NEW
: return "new";
4179 case DEVLINK_CMD_PARAM_DEL
: return "del";
4180 case DEVLINK_CMD_REGION_GET
: return "get";
4181 case DEVLINK_CMD_REGION_SET
: return "set";
4182 case DEVLINK_CMD_REGION_NEW
: return "new";
4183 case DEVLINK_CMD_REGION_DEL
: return "del";
4184 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4185 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4186 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4187 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4188 case DEVLINK_CMD_TRAP_GET
: return "get";
4189 case DEVLINK_CMD_TRAP_SET
: return "set";
4190 case DEVLINK_CMD_TRAP_NEW
: return "new";
4191 case DEVLINK_CMD_TRAP_DEL
: return "del";
4192 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4193 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4194 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4195 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4196 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4197 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4198 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4199 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4200 default: return "<unknown cmd>";
4204 static const char *cmd_obj(uint8_t cmd
)
4207 case DEVLINK_CMD_UNSPEC
: return "unspec";
4208 case DEVLINK_CMD_GET
:
4209 case DEVLINK_CMD_SET
:
4210 case DEVLINK_CMD_NEW
:
4211 case DEVLINK_CMD_DEL
:
4213 case DEVLINK_CMD_PORT_GET
:
4214 case DEVLINK_CMD_PORT_SET
:
4215 case DEVLINK_CMD_PORT_NEW
:
4216 case DEVLINK_CMD_PORT_DEL
:
4218 case DEVLINK_CMD_PARAM_GET
:
4219 case DEVLINK_CMD_PARAM_SET
:
4220 case DEVLINK_CMD_PARAM_NEW
:
4221 case DEVLINK_CMD_PARAM_DEL
:
4223 case DEVLINK_CMD_REGION_GET
:
4224 case DEVLINK_CMD_REGION_SET
:
4225 case DEVLINK_CMD_REGION_NEW
:
4226 case DEVLINK_CMD_REGION_DEL
:
4228 case DEVLINK_CMD_FLASH_UPDATE
:
4229 case DEVLINK_CMD_FLASH_UPDATE_END
:
4230 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4232 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4234 case DEVLINK_CMD_TRAP_GET
:
4235 case DEVLINK_CMD_TRAP_SET
:
4236 case DEVLINK_CMD_TRAP_NEW
:
4237 case DEVLINK_CMD_TRAP_DEL
:
4239 case DEVLINK_CMD_TRAP_GROUP_GET
:
4240 case DEVLINK_CMD_TRAP_GROUP_SET
:
4241 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4242 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4243 return "trap-group";
4244 case DEVLINK_CMD_TRAP_POLICER_GET
:
4245 case DEVLINK_CMD_TRAP_POLICER_SET
:
4246 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4247 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4248 return "trap-policer";
4249 default: return "<unknown obj>";
4253 static void pr_out_mon_header(uint8_t cmd
)
4255 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4258 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4260 const char *obj
= cmd_obj(cmd
);
4261 unsigned int index
= 0;
4262 const char *cur_obj
;
4266 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4267 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4273 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4275 __pr_out_handle_start(dl
, tb
, true, false);
4277 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4278 check_indent_newline(dl
);
4279 print_string(PRINT_ANY
, "msg", "msg %s",
4280 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4282 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4283 check_indent_newline(dl
);
4284 print_string(PRINT_ANY
, "component", "component %s",
4285 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4288 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4289 pr_out_u64(dl
, "done",
4290 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4292 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4293 pr_out_u64(dl
, "total",
4294 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4296 pr_out_handle_end(dl
);
4299 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4300 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4301 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4302 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4303 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4305 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4307 struct dl
*dl
= data
;
4308 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4309 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4310 uint8_t cmd
= genl
->cmd
;
4312 if (!cmd_filter_check(dl
, cmd
))
4316 case DEVLINK_CMD_GET
: /* fall through */
4317 case DEVLINK_CMD_SET
: /* fall through */
4318 case DEVLINK_CMD_NEW
: /* fall through */
4319 case DEVLINK_CMD_DEL
:
4320 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4321 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4322 return MNL_CB_ERROR
;
4323 pr_out_mon_header(genl
->cmd
);
4324 pr_out_handle(dl
, tb
);
4326 case DEVLINK_CMD_PORT_GET
: /* fall through */
4327 case DEVLINK_CMD_PORT_SET
: /* fall through */
4328 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4329 case DEVLINK_CMD_PORT_DEL
:
4330 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4331 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4332 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4333 return MNL_CB_ERROR
;
4334 pr_out_mon_header(genl
->cmd
);
4335 pr_out_port(dl
, tb
);
4337 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4338 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4339 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4340 case DEVLINK_CMD_PARAM_DEL
:
4341 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4342 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4343 !tb
[DEVLINK_ATTR_PARAM
])
4344 return MNL_CB_ERROR
;
4345 pr_out_mon_header(genl
->cmd
);
4346 pr_out_param(dl
, tb
, false);
4348 case DEVLINK_CMD_REGION_GET
: /* fall through */
4349 case DEVLINK_CMD_REGION_SET
: /* fall through */
4350 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4351 case DEVLINK_CMD_REGION_DEL
:
4352 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4353 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4354 !tb
[DEVLINK_ATTR_REGION_NAME
])
4355 return MNL_CB_ERROR
;
4356 pr_out_mon_header(genl
->cmd
);
4357 pr_out_region(dl
, tb
);
4359 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4360 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4361 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4362 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4363 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4364 return MNL_CB_ERROR
;
4365 pr_out_mon_header(genl
->cmd
);
4366 pr_out_flash_update(dl
, tb
);
4368 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4369 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4370 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4371 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
4372 return MNL_CB_ERROR
;
4373 pr_out_mon_header(genl
->cmd
);
4374 pr_out_health(dl
, tb
);
4376 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4377 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4378 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4379 case DEVLINK_CMD_TRAP_DEL
:
4380 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4381 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4382 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4383 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4384 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4385 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4386 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4387 !tb
[DEVLINK_ATTR_STATS
])
4388 return MNL_CB_ERROR
;
4389 pr_out_mon_header(genl
->cmd
);
4390 pr_out_trap(dl
, tb
, false);
4392 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4393 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4394 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4395 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4396 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4397 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4398 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4399 !tb
[DEVLINK_ATTR_STATS
])
4400 return MNL_CB_ERROR
;
4401 pr_out_mon_header(genl
->cmd
);
4402 pr_out_trap_group(dl
, tb
, false);
4404 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4405 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4406 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4407 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4408 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4409 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4410 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4411 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4412 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4413 return MNL_CB_ERROR
;
4414 pr_out_mon_header(genl
->cmd
);
4415 pr_out_trap_policer(dl
, tb
, false);
4421 static int cmd_mon_show(struct dl
*dl
)
4424 unsigned int index
= 0;
4425 const char *cur_obj
;
4427 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4428 if (strcmp(cur_obj
, "all") != 0 &&
4429 strcmp(cur_obj
, "dev") != 0 &&
4430 strcmp(cur_obj
, "port") != 0 &&
4431 strcmp(cur_obj
, "health") != 0 &&
4432 strcmp(cur_obj
, "trap") != 0 &&
4433 strcmp(cur_obj
, "trap-group") != 0 &&
4434 strcmp(cur_obj
, "trap-policer") != 0) {
4435 pr_err("Unknown object \"%s\"\n", cur_obj
);
4439 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4442 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4448 static void cmd_mon_help(void)
4450 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4451 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4454 static int cmd_mon(struct dl
*dl
)
4456 if (dl_argv_match(dl
, "help")) {
4460 return cmd_mon_show(dl
);
4463 struct dpipe_field
{
4466 unsigned int bitwidth
;
4467 enum devlink_dpipe_field_mapping_type mapping_type
;
4470 struct dpipe_header
{
4471 struct list_head list
;
4474 struct dpipe_field
*fields
;
4475 unsigned int fields_count
;
4478 struct dpipe_table
{
4479 struct list_head list
;
4481 unsigned int resource_id
;
4482 bool resource_valid
;
4485 struct dpipe_tables
{
4486 struct list_head table_list
;
4496 enum devlink_resource_unit unit
;
4501 struct list_head list
;
4502 struct list_head resource_list
;
4503 struct resource
*parent
;
4507 struct list_head resource_list
;
4510 struct resource_ctx
{
4513 struct resources
*resources
;
4514 struct dpipe_tables
*tables
;
4515 bool print_resources
;
4516 bool pending_change
;
4519 static struct resource
*resource_alloc(void)
4521 struct resource
*resource
;
4523 resource
= calloc(1, sizeof(struct resource
));
4526 INIT_LIST_HEAD(&resource
->resource_list
);
4530 static void resource_free(struct resource
*resource
)
4532 struct resource
*child_resource
, *tmp
;
4534 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4536 free(child_resource
->name
);
4537 resource_free(child_resource
);
4542 static struct resources
*resources_alloc(void)
4544 struct resources
*resources
;
4546 resources
= calloc(1, sizeof(struct resources
));
4549 INIT_LIST_HEAD(&resources
->resource_list
);
4553 static void resources_free(struct resources
*resources
)
4555 struct resource
*resource
, *tmp
;
4557 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4558 resource_free(resource
);
4561 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4563 ctx
->resources
= resources_alloc();
4564 if (!ctx
->resources
)
4570 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4572 resources_free(ctx
->resources
);
4578 struct list_head global_headers
;
4579 struct list_head local_headers
;
4580 struct dpipe_tables
*tables
;
4581 struct resources
*resources
;
4586 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4588 struct dpipe_header
*header
;
4590 header
= calloc(1, sizeof(struct dpipe_header
));
4593 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4594 if (!header
->fields
)
4595 goto err_fields_alloc
;
4596 header
->fields_count
= fields_count
;
4604 static void dpipe_header_free(struct dpipe_header
*header
)
4606 free(header
->fields
);
4610 static void dpipe_header_clear(struct dpipe_header
*header
)
4612 struct dpipe_field
*field
;
4615 for (i
= 0; i
< header
->fields_count
; i
++) {
4616 field
= &header
->fields
[i
];
4622 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4623 struct dpipe_header
*header
, bool global
)
4626 list_add(&header
->list
, &ctx
->global_headers
);
4628 list_add(&header
->list
, &ctx
->local_headers
);
4631 static void dpipe_header_del(struct dpipe_header
*header
)
4633 list_del(&header
->list
);
4636 static struct dpipe_table
*dpipe_table_alloc(void)
4638 return calloc(1, sizeof(struct dpipe_table
));
4641 static void dpipe_table_free(struct dpipe_table
*table
)
4646 static struct dpipe_tables
*dpipe_tables_alloc(void)
4648 struct dpipe_tables
*tables
;
4650 tables
= calloc(1, sizeof(struct dpipe_tables
));
4653 INIT_LIST_HEAD(&tables
->table_list
);
4657 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4659 struct dpipe_table
*table
, *tmp
;
4661 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4662 dpipe_table_free(table
);
4666 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4668 ctx
->tables
= dpipe_tables_alloc();
4673 INIT_LIST_HEAD(&ctx
->global_headers
);
4674 INIT_LIST_HEAD(&ctx
->local_headers
);
4678 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4680 struct dpipe_header
*header
, *tmp
;
4682 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4684 dpipe_header_del(header
);
4685 dpipe_header_clear(header
);
4686 dpipe_header_free(header
);
4688 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4690 dpipe_header_del(header
);
4691 dpipe_header_clear(header
);
4692 dpipe_header_free(header
);
4694 dpipe_tables_free(ctx
->tables
);
4697 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4698 uint32_t header_id
, bool global
)
4700 struct list_head
*header_list
;
4701 struct dpipe_header
*header
;
4704 header_list
= &ctx
->global_headers
;
4706 header_list
= &ctx
->local_headers
;
4707 list_for_each_entry(header
, header_list
, list
) {
4708 if (header
->id
!= header_id
)
4710 return header
->name
;
4715 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4717 uint32_t field_id
, bool global
)
4719 struct list_head
*header_list
;
4720 struct dpipe_header
*header
;
4723 header_list
= &ctx
->global_headers
;
4725 header_list
= &ctx
->local_headers
;
4726 list_for_each_entry(header
, header_list
, list
) {
4727 if (header
->id
!= header_id
)
4729 return header
->fields
[field_id
].name
;
4735 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4737 switch (mapping_type
) {
4738 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4740 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4748 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4749 uint32_t field_id
, bool global
)
4751 enum devlink_dpipe_field_mapping_type mapping_type
;
4752 struct list_head
*header_list
;
4753 struct dpipe_header
*header
;
4756 header_list
= &ctx
->global_headers
;
4758 header_list
= &ctx
->local_headers
;
4759 list_for_each_entry(header
, header_list
, list
) {
4760 if (header
->id
!= header_id
)
4762 mapping_type
= header
->fields
[field_id
].mapping_type
;
4763 return dpipe_field_mapping_e2s(mapping_type
);
4768 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4769 struct dpipe_field
*fields
,
4770 unsigned int field_count
)
4772 struct dpipe_field
*field
;
4775 for (i
= 0; i
< field_count
; i
++) {
4777 pr_out_entry_start(ctx
->dl
);
4778 check_indent_newline(ctx
->dl
);
4779 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4780 if (ctx
->dl
->verbose
)
4781 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4782 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4783 if (field
->mapping_type
) {
4784 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4785 dpipe_field_mapping_e2s(field
->mapping_type
));
4787 pr_out_entry_end(ctx
->dl
);
4792 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4793 struct dpipe_header
*header
, bool global
)
4795 pr_out_handle_start_arr(ctx
->dl
, tb
);
4796 check_indent_newline(ctx
->dl
);
4797 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4798 if (ctx
->dl
->verbose
) {
4799 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4800 print_bool(PRINT_ANY
, "global", " global %s", global
);
4802 pr_out_array_start(ctx
->dl
, "field");
4803 pr_out_dpipe_fields(ctx
, header
->fields
,
4804 header
->fields_count
);
4805 pr_out_array_end(ctx
->dl
);
4806 pr_out_handle_end(ctx
->dl
);
4809 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4812 struct dpipe_header
*header
;
4814 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4815 pr_out_dpipe_header(ctx
, tb
, header
, false);
4817 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4818 pr_out_dpipe_header(ctx
, tb
, header
, true);
4821 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4823 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4827 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4828 if (err
!= MNL_CB_OK
)
4830 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4831 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4832 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4833 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4836 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4837 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4838 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4839 field
->name
= strdup(name
);
4842 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4846 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4847 struct dpipe_field
*fields
)
4849 struct nlattr
*nla_field
;
4853 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4854 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4862 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4864 struct nlattr
*nla_field
;
4865 unsigned int count
= 0;
4867 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4872 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4874 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4875 struct dpipe_header
*header
;
4876 unsigned int fields_count
;
4877 const char *header_name
;
4881 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4882 if (err
!= MNL_CB_OK
)
4885 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4886 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4887 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4890 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4891 header
= dpipe_header_alloc(fields_count
);
4895 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4896 header
->name
= strdup(header_name
);
4897 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4898 header
->fields_count
= fields_count
;
4899 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4901 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4905 dpipe_header_add(ctx
, header
, global
);
4909 dpipe_header_free(header
);
4913 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4915 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4916 struct nlattr
*nla_header
;
4919 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4920 err
= dpipe_header_get(ctx
, nla_header
);
4927 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4929 struct dpipe_ctx
*ctx
= data
;
4930 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4931 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4934 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4935 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4936 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4937 return MNL_CB_ERROR
;
4938 err
= dpipe_headers_get(ctx
, tb
);
4941 return MNL_CB_ERROR
;
4944 if (ctx
->print_headers
)
4945 pr_out_dpipe_headers(ctx
, tb
);
4949 static int cmd_dpipe_headers_show(struct dl
*dl
)
4951 struct nlmsghdr
*nlh
;
4952 struct dpipe_ctx ctx
= {};
4953 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4956 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4958 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4962 err
= dpipe_ctx_init(&ctx
, dl
);
4966 ctx
.print_headers
= true;
4968 pr_out_section_start(dl
, "header");
4969 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4971 pr_err("error get headers %s\n", strerror(ctx
.err
));
4972 pr_out_section_end(dl
);
4974 dpipe_ctx_fini(&ctx
);
4978 static void cmd_dpipe_header_help(void)
4980 pr_err("Usage: devlink dpipe headers show DEV\n");
4983 static int cmd_dpipe_header(struct dl
*dl
)
4985 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4986 cmd_dpipe_header_help();
4988 } else if (dl_argv_match(dl
, "show")) {
4990 return cmd_dpipe_headers_show(dl
);
4992 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4997 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4999 switch (action_type
) {
5000 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5001 return "field_modify";
5007 struct dpipe_op_info
{
5013 struct dpipe_action
{
5014 struct dpipe_op_info info
;
5018 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5019 struct dpipe_ctx
*ctx
)
5021 struct dpipe_op_info
*op_info
= &action
->info
;
5022 const char *mapping
;
5024 check_indent_newline(ctx
->dl
);
5025 print_string(PRINT_ANY
, "type", "type %s",
5026 dpipe_action_type_e2s(action
->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_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5043 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5046 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5047 if (err
!= MNL_CB_OK
)
5050 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5051 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5052 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5053 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5057 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5058 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5059 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5060 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5065 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5066 struct nlattr
*nla_actions
)
5068 struct nlattr
*nla_action
;
5069 struct dpipe_action action
;
5071 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5072 pr_out_entry_start(ctx
->dl
);
5073 if (dpipe_action_parse(&action
, nla_action
))
5074 goto err_action_parse
;
5075 pr_out_dpipe_action(&action
, ctx
);
5076 pr_out_entry_end(ctx
->dl
);
5081 pr_out_entry_end(ctx
->dl
);
5086 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5088 switch (match_type
) {
5089 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5090 return "field_exact";
5096 struct dpipe_match
{
5097 struct dpipe_op_info info
;
5101 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5102 struct dpipe_ctx
*ctx
)
5104 struct dpipe_op_info
*op_info
= &match
->info
;
5105 const char *mapping
;
5107 check_indent_newline(ctx
->dl
);
5108 print_string(PRINT_ANY
, "type", "type %s",
5109 dpipe_match_type_e2s(match
->type
));
5110 print_string(PRINT_ANY
, "header", " header %s",
5111 dpipe_header_id2s(ctx
, op_info
->header_id
,
5112 op_info
->header_global
));
5113 print_string(PRINT_ANY
, "field", " field %s",
5114 dpipe_field_id2s(ctx
, op_info
->header_id
,
5116 op_info
->header_global
));
5117 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5119 op_info
->header_global
);
5121 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5124 static int dpipe_match_parse(struct dpipe_match
*match
,
5128 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5131 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5132 if (err
!= MNL_CB_OK
)
5135 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5136 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5137 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5138 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5142 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5143 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5144 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5145 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5150 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5151 struct nlattr
*nla_matches
)
5153 struct nlattr
*nla_match
;
5154 struct dpipe_match match
;
5156 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5157 pr_out_entry_start(ctx
->dl
);
5158 if (dpipe_match_parse(&match
, nla_match
))
5159 goto err_match_parse
;
5160 pr_out_dpipe_match(&match
, ctx
);
5161 pr_out_entry_end(ctx
->dl
);
5166 pr_out_entry_end(ctx
->dl
);
5170 static struct resource
*
5171 resource_find(struct resources
*resources
, struct resource
*resource
,
5172 uint64_t resource_id
)
5174 struct list_head
*list_head
;
5177 list_head
= &resources
->resource_list
;
5179 list_head
= &resource
->resource_list
;
5181 list_for_each_entry(resource
, list_head
, list
) {
5182 struct resource
*child_resource
;
5184 if (resource
->id
== resource_id
)
5187 child_resource
= resource_find(resources
, resource
,
5190 return child_resource
;
5196 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5197 uint64_t resource_id
)
5199 struct resource
*resource
, *parent_resource
;
5200 const char del
[] = "/";
5204 resource
= resource_find(resources
, NULL
, resource_id
);
5208 for (parent_resource
= resource
; parent_resource
;
5209 parent_resource
= parent_resource
->parent
)
5210 path_len
+= strlen(parent_resource
->name
) + 1;
5213 path
= calloc(1, path_len
);
5217 path
+= path_len
- 1;
5218 for (parent_resource
= resource
; parent_resource
;
5219 parent_resource
= parent_resource
->parent
) {
5220 path
-= strlen(parent_resource
->name
);
5221 memcpy(path
, parent_resource
->name
,
5222 strlen(parent_resource
->name
));
5223 path
-= strlen(del
);
5224 memcpy(path
, del
, strlen(del
));
5226 check_indent_newline(dl
);
5227 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5231 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5233 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5234 struct dpipe_table
*table
;
5235 uint32_t resource_units
;
5236 bool counters_enabled
;
5237 bool resource_valid
;
5241 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5242 if (err
!= MNL_CB_OK
)
5245 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5246 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5247 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5248 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5249 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5253 table
= dpipe_table_alloc();
5257 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5258 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5259 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5261 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5263 if (resource_valid
) {
5264 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5265 table
->resource_valid
= true;
5268 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5269 if (!ctx
->print_tables
)
5272 check_indent_newline(ctx
->dl
);
5273 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5274 print_uint(PRINT_ANY
, "size", " size %u", size
);
5275 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5277 if (resource_valid
) {
5278 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5279 resource_path_print(ctx
->dl
, ctx
->resources
,
5280 table
->resource_id
);
5281 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5285 pr_out_array_start(ctx
->dl
, "match");
5286 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5287 goto err_matches_show
;
5288 pr_out_array_end(ctx
->dl
);
5290 pr_out_array_start(ctx
->dl
, "action");
5291 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5292 goto err_actions_show
;
5293 pr_out_array_end(ctx
->dl
);
5299 pr_out_array_end(ctx
->dl
);
5303 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5305 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5306 struct nlattr
*nla_table
;
5308 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5309 if (ctx
->print_tables
)
5310 pr_out_handle_start_arr(ctx
->dl
, tb
);
5311 if (dpipe_table_show(ctx
, nla_table
))
5312 goto err_table_show
;
5313 if (ctx
->print_tables
)
5314 pr_out_handle_end(ctx
->dl
);
5319 if (ctx
->print_tables
)
5320 pr_out_handle_end(ctx
->dl
);
5324 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5326 struct dpipe_ctx
*ctx
= data
;
5327 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5328 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5330 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5331 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5332 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5333 return MNL_CB_ERROR
;
5335 if (dpipe_tables_show(ctx
, tb
))
5336 return MNL_CB_ERROR
;
5340 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5342 static int cmd_dpipe_table_show(struct dl
*dl
)
5344 struct nlmsghdr
*nlh
;
5345 struct dpipe_ctx dpipe_ctx
= {};
5346 struct resource_ctx resource_ctx
= {};
5347 uint16_t flags
= NLM_F_REQUEST
;
5350 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5354 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5356 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5360 dpipe_ctx
.print_tables
= true;
5362 dl_opts_put(nlh
, dl
);
5363 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5366 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5367 goto err_headers_get
;
5370 err
= resource_ctx_init(&resource_ctx
, dl
);
5372 goto err_resource_ctx_init
;
5374 resource_ctx
.print_resources
= false;
5375 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5376 dl_opts_put(nlh
, dl
);
5377 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5380 dpipe_ctx
.resources
= resource_ctx
.resources
;
5382 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5383 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5384 dl_opts_put(nlh
, dl
);
5386 pr_out_section_start(dl
, "table");
5387 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5388 pr_out_section_end(dl
);
5390 resource_ctx_fini(&resource_ctx
);
5391 dpipe_ctx_fini(&dpipe_ctx
);
5394 err_resource_ctx_init
:
5396 dpipe_ctx_fini(&dpipe_ctx
);
5400 static int cmd_dpipe_table_set(struct dl
*dl
)
5402 struct nlmsghdr
*nlh
;
5405 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5406 NLM_F_REQUEST
| NLM_F_ACK
);
5408 err
= dl_argv_parse_put(nlh
, dl
,
5409 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5410 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5414 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5417 enum dpipe_value_type
{
5418 DPIPE_VALUE_TYPE_VALUE
,
5419 DPIPE_VALUE_TYPE_MASK
,
5423 dpipe_value_type_e2s(enum dpipe_value_type type
)
5426 case DPIPE_VALUE_TYPE_VALUE
:
5428 case DPIPE_VALUE_TYPE_MASK
:
5429 return "value_mask";
5435 struct dpipe_field_printer
{
5436 unsigned int field_id
;
5437 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5440 struct dpipe_header_printer
{
5441 struct dpipe_field_printer
*printers
;
5442 unsigned int printers_count
;
5443 unsigned int header_id
;
5446 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5447 enum dpipe_value_type type
,
5450 struct in_addr ip_addr
;
5452 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5453 check_indent_newline(ctx
->dl
);
5454 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5458 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5459 enum dpipe_value_type type
,
5462 check_indent_newline(ctx
->dl
);
5463 print_string_name_value(dpipe_value_type_e2s(type
),
5464 ether_ntoa((struct ether_addr
*)value
));
5467 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5468 enum dpipe_value_type type
,
5471 char str
[INET6_ADDRSTRLEN
];
5473 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5474 check_indent_newline(ctx
->dl
);
5475 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5478 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5480 .printer
= dpipe_field_printer_ipv4_addr
,
5481 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5485 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5486 .printers
= dpipe_field_printers_ipv4
,
5487 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5488 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5491 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5493 .printer
= dpipe_field_printer_ethernet_addr
,
5494 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5498 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5499 .printers
= dpipe_field_printers_ethernet
,
5500 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5501 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5504 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5506 .printer
= dpipe_field_printer_ipv6_addr
,
5507 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5511 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5512 .printers
= dpipe_field_printers_ipv6
,
5513 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5514 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5517 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5518 &dpipe_header_printer_ipv4
,
5519 &dpipe_header_printer_ethernet
,
5520 &dpipe_header_printer_ipv6
,
5523 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5524 struct dpipe_op_info
*info
,
5525 enum dpipe_value_type type
,
5528 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5529 struct dpipe_header_printer
*header_printer
;
5530 struct dpipe_field_printer
*field_printer
;
5531 unsigned int field_printers_count
;
5535 for (i
= 0; i
< header_printers_count
; i
++) {
5536 header_printer
= dpipe_header_printers
[i
];
5537 if (header_printer
->header_id
!= info
->header_id
)
5539 field_printers_count
= header_printer
->printers_count
;
5540 for (j
= 0; j
< field_printers_count
; j
++) {
5541 field_printer
= &header_printer
->printers
[j
];
5542 if (field_printer
->field_id
!= info
->field_id
)
5544 field_printer
->printer(ctx
, type
, value
);
5552 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5554 unsigned int value_len
,
5555 struct dpipe_op_info
*info
,
5556 enum dpipe_value_type type
)
5558 if (info
->header_global
&&
5559 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5562 if (value_len
== sizeof(uint32_t)) {
5563 uint32_t *value_32
= value
;
5565 check_indent_newline(ctx
->dl
);
5566 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5570 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5571 struct nlattr
**nla_match_value
,
5572 struct dpipe_op_info
*info
)
5574 void *value
, *value_mask
;
5575 uint32_t value_mapping
;
5579 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5580 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5582 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5583 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5586 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5587 check_indent_newline(ctx
->dl
);
5588 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5592 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5593 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5594 DPIPE_VALUE_TYPE_MASK
);
5597 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5600 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5603 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5604 struct dpipe_match match
;
5607 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5608 if (err
!= MNL_CB_OK
)
5611 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5612 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5616 pr_out_entry_start(ctx
->dl
);
5617 if (dpipe_match_parse(&match
,
5618 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5619 goto err_match_parse
;
5620 pr_out_dpipe_match(&match
, ctx
);
5621 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5622 pr_out_entry_end(ctx
->dl
);
5627 pr_out_entry_end(ctx
->dl
);
5631 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5634 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5635 struct dpipe_action action
;
5638 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5639 if (err
!= MNL_CB_OK
)
5642 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5643 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5647 pr_out_entry_start(ctx
->dl
);
5648 if (dpipe_action_parse(&action
,
5649 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5650 goto err_action_parse
;
5651 pr_out_dpipe_action(&action
, ctx
);
5652 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5653 pr_out_entry_end(ctx
->dl
);
5658 pr_out_entry_end(ctx
->dl
);
5663 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5664 struct nlattr
*nla_action_values
)
5666 struct nlattr
*nla_action_value
;
5668 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5669 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5676 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5677 struct nlattr
*nla_match_values
)
5679 struct nlattr
*nla_match_value
;
5681 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5682 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5688 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5690 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5691 uint32_t entry_index
;
5695 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5696 if (err
!= MNL_CB_OK
)
5699 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5700 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5701 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5705 check_indent_newline(ctx
->dl
);
5706 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5707 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5709 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5710 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5711 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5714 pr_out_array_start(ctx
->dl
, "match_value");
5715 if (dpipe_tables_match_values_show(ctx
,
5716 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5717 goto err_match_values_show
;
5718 pr_out_array_end(ctx
->dl
);
5720 pr_out_array_start(ctx
->dl
, "action_value");
5721 if (dpipe_tables_action_values_show(ctx
,
5722 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5723 goto err_action_values_show
;
5724 pr_out_array_end(ctx
->dl
);
5727 err_action_values_show
:
5728 err_match_values_show
:
5729 pr_out_array_end(ctx
->dl
);
5733 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5735 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5736 struct nlattr
*nla_entry
;
5738 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5739 pr_out_handle_start_arr(ctx
->dl
, tb
);
5740 if (dpipe_entry_show(ctx
, nla_entry
))
5741 goto err_entry_show
;
5742 pr_out_handle_end(ctx
->dl
);
5747 pr_out_handle_end(ctx
->dl
);
5751 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5753 struct dpipe_ctx
*ctx
= data
;
5754 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5755 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5757 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5758 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5759 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5760 return MNL_CB_ERROR
;
5762 if (dpipe_table_entries_show(ctx
, tb
))
5763 return MNL_CB_ERROR
;
5767 static int cmd_dpipe_table_dump(struct dl
*dl
)
5769 struct nlmsghdr
*nlh
;
5770 struct dpipe_ctx ctx
= {};
5771 uint16_t flags
= NLM_F_REQUEST
;
5774 err
= dpipe_ctx_init(&ctx
, dl
);
5778 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5782 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5783 dl_opts_put(nlh
, dl
);
5784 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5786 pr_err("error get headers %s\n", strerror(ctx
.err
));
5790 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5791 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5792 dl_opts_put(nlh
, dl
);
5794 pr_out_section_start(dl
, "table_entry");
5795 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5796 pr_out_section_end(dl
);
5798 dpipe_ctx_fini(&ctx
);
5802 static void cmd_dpipe_table_help(void)
5804 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5805 "where OBJECT-LIST := { show | set | dump }\n");
5808 static int cmd_dpipe_table(struct dl
*dl
)
5810 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5811 cmd_dpipe_table_help();
5813 } else if (dl_argv_match(dl
, "show")) {
5815 return cmd_dpipe_table_show(dl
);
5816 } else if (dl_argv_match(dl
, "set")) {
5818 return cmd_dpipe_table_set(dl
);
5819 } else if (dl_argv_match(dl
, "dump")) {
5821 return cmd_dpipe_table_dump(dl
);
5823 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5827 static void cmd_dpipe_help(void)
5829 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5830 "where OBJECT-LIST := { header | table }\n");
5833 static int cmd_dpipe(struct dl
*dl
)
5835 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5838 } else if (dl_argv_match(dl
, "header")) {
5840 return cmd_dpipe_header(dl
);
5841 } else if (dl_argv_match(dl
, "table")) {
5843 return cmd_dpipe_table(dl
);
5845 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5850 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5851 struct nlattr
**nla_resource
)
5853 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5854 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5855 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5856 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5857 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5858 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5859 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5863 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5864 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5865 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5866 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5867 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5868 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5869 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5871 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5872 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5874 resource
->size_new
= resource
->size
;
5876 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5877 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5878 resource
->occ_valid
= true;
5881 if (resource
->size_new
!= resource
->size
)
5882 ctx
->pending_change
= true;
5888 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5889 struct resource
*parent_resource
, struct nlattr
*nl
)
5891 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5892 struct nlattr
*nla_child_resource
;
5893 struct nlattr
*nla_resources
;
5903 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5904 if (err
!= MNL_CB_OK
)
5907 err
= resource_parse(ctx
, resource
, nla_resource
);
5911 resource
->parent
= parent_resource
;
5912 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5915 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5916 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5918 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5919 struct resource
*child_resource
;
5920 struct list_head
*list
;
5922 child_resource
= resource_alloc();
5923 if (!child_resource
)
5927 list
= &ctx
->resources
->resource_list
;
5929 list
= &resource
->resource_list
;
5931 list_add_tail(&child_resource
->list
, list
);
5932 err
= resource_get(ctx
, child_resource
, resource
,
5933 nla_child_resource
);
5941 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5944 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5945 default: return "<unknown unit>";
5949 static void resource_show(struct resource
*resource
,
5950 struct resource_ctx
*ctx
)
5952 struct resource
*child_resource
;
5953 struct dpipe_table
*table
;
5954 struct dl
*dl
= ctx
->dl
;
5957 check_indent_newline(dl
);
5958 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
5960 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5961 pr_out_u64(dl
, "size", resource
->size
);
5962 if (resource
->size
!= resource
->size_new
)
5963 pr_out_u64(dl
, "size_new", resource
->size_new
);
5964 if (resource
->occ_valid
)
5965 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
5966 print_string(PRINT_ANY
, "unit", " unit %s",
5967 resource_unit_str_get(resource
->unit
));
5969 if (resource
->size_min
!= resource
->size_max
) {
5970 print_uint(PRINT_ANY
, "size_min", " size_min %u",
5971 resource
->size_min
);
5972 pr_out_u64(dl
, "size_max", resource
->size_max
);
5973 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
5974 resource
->size_gran
);
5977 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5978 if (table
->resource_id
== resource
->id
&&
5979 table
->resource_valid
)
5983 pr_out_array_start(dl
, "dpipe_tables");
5985 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
5988 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5989 if (table
->resource_id
!= resource
->id
||
5990 !table
->resource_valid
)
5992 pr_out_entry_start(dl
);
5993 check_indent_newline(dl
);
5994 print_string(PRINT_ANY
, "table_name", "table_name %s",
5996 pr_out_entry_end(dl
);
5999 pr_out_array_end(dl
);
6001 if (list_empty(&resource
->resource_list
))
6004 if (ctx
->pending_change
) {
6005 check_indent_newline(dl
);
6006 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6007 resource
->size_valid
? "true" : "false");
6009 pr_out_array_start(dl
, "resources");
6010 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6011 pr_out_entry_start(dl
);
6012 resource_show(child_resource
, ctx
);
6013 pr_out_entry_end(dl
);
6015 pr_out_array_end(dl
);
6019 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6021 struct resources
*resources
= ctx
->resources
;
6022 struct resource
*resource
;
6024 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6025 pr_out_handle_start_arr(ctx
->dl
, tb
);
6026 resource_show(resource
, ctx
);
6027 pr_out_handle_end(ctx
->dl
);
6031 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6033 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6036 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6038 struct resource_ctx
*ctx
= data
;
6039 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6040 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6043 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6044 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6045 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6046 return MNL_CB_ERROR
;
6048 err
= resources_get(ctx
, tb
);
6051 return MNL_CB_ERROR
;
6054 if (ctx
->print_resources
)
6055 resources_show(ctx
, tb
);
6060 static int cmd_resource_show(struct dl
*dl
)
6062 struct nlmsghdr
*nlh
;
6063 struct dpipe_ctx dpipe_ctx
= {};
6064 struct resource_ctx resource_ctx
= {};
6067 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6071 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6073 dl_opts_put(nlh
, dl
);
6075 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6079 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6082 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6086 err
= resource_ctx_init(&resource_ctx
, dl
);
6090 resource_ctx
.print_resources
= true;
6091 resource_ctx
.tables
= dpipe_ctx
.tables
;
6092 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6093 NLM_F_REQUEST
| NLM_F_ACK
);
6094 dl_opts_put(nlh
, dl
);
6095 pr_out_section_start(dl
, "resources");
6096 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6098 pr_out_section_end(dl
);
6099 resource_ctx_fini(&resource_ctx
);
6101 dpipe_ctx_fini(&dpipe_ctx
);
6105 static void cmd_resource_help(void)
6107 pr_err("Usage: devlink resource show DEV\n"
6108 " devlink resource set DEV path PATH size SIZE\n");
6111 static struct resource
*
6112 resource_find_by_name(struct list_head
*list
, char *name
)
6114 struct resource
*resource
;
6116 list_for_each_entry(resource
, list
, list
) {
6117 if (!strcmp(resource
->name
, name
))
6124 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6125 uint32_t *p_resource_id
, bool *p_resource_valid
)
6127 struct resource
*resource
;
6128 uint32_t resource_id
= 0;
6129 char *resource_path_dup
;
6130 struct list_head
*list
;
6131 const char del
[] = "/";
6132 char *resource_name
;
6134 resource_path_dup
= strdup(resource_path
);
6135 list
= &ctx
->resources
->resource_list
;
6136 resource_name
= strtok(resource_path_dup
, del
);
6137 while (resource_name
!= NULL
) {
6138 resource
= resource_find_by_name(list
, resource_name
);
6140 goto err_resource_lookup
;
6142 list
= &resource
->resource_list
;
6143 resource_name
= strtok(NULL
, del
);
6144 resource_id
= resource
->id
;
6146 free(resource_path_dup
);
6147 *p_resource_valid
= true;
6148 *p_resource_id
= resource_id
;
6151 err_resource_lookup
:
6152 free(resource_path_dup
);
6156 static int cmd_resource_set(struct dl
*dl
)
6158 struct nlmsghdr
*nlh
;
6159 struct resource_ctx ctx
= {};
6162 err
= resource_ctx_init(&ctx
, dl
);
6166 ctx
.print_resources
= false;
6167 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6168 DL_OPT_RESOURCE_SIZE
, 0);
6172 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6174 dl_opts_put(nlh
, dl
);
6175 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6177 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6181 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6182 &dl
->opts
.resource_id
,
6183 &dl
->opts
.resource_id_valid
);
6185 pr_err("error parsing resource path %s\n", strerror(-err
));
6189 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6190 NLM_F_REQUEST
| NLM_F_ACK
);
6192 dl_opts_put(nlh
, dl
);
6193 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6195 resource_ctx_fini(&ctx
);
6199 static int cmd_resource(struct dl
*dl
)
6201 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6202 cmd_resource_help();
6204 } else if (dl_argv_match(dl
, "show")) {
6206 return cmd_resource_show(dl
);
6207 } else if (dl_argv_match(dl
, "set")) {
6209 return cmd_resource_set(dl
);
6211 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6215 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6217 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6218 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6219 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6222 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6223 if (dl
->json_output
)
6224 open_json_object(buf
);
6229 static void pr_out_region_handle_end(struct dl
*dl
)
6231 if (dl
->json_output
)
6232 close_json_object();
6237 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6239 __pr_out_indent_newline(dl
);
6240 if (dl
->json_output
)
6241 open_json_array(PRINT_JSON
, "snapshot");
6243 pr_out("snapshot %s", array
? "[" : "");
6246 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6248 if (dl
->json_output
)
6249 close_json_array(PRINT_JSON
, NULL
);
6254 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6256 uint32_t snapshot_id
;
6258 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6261 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6263 if (dl
->json_output
)
6264 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6266 pr_out("%s%u", index
? " " : "", snapshot_id
);
6269 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6271 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6272 struct nlattr
*nla_sanpshot
;
6275 pr_out_region_snapshots_start(dl
, true);
6276 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6277 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6278 if (err
!= MNL_CB_OK
)
6280 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6282 pr_out_region_snapshots_end(dl
, true);
6285 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6287 pr_out_region_snapshots_start(dl
, false);
6288 pr_out_region_snapshots_id(dl
, tb
, 0);
6289 pr_out_region_snapshots_end(dl
, false);
6292 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6294 pr_out_region_handle_start(dl
, tb
);
6296 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6297 pr_out_u64(dl
, "size",
6298 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6300 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6301 pr_out_snapshots(dl
, tb
);
6303 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6304 pr_out_snapshot(dl
, tb
);
6306 pr_out_region_handle_end(dl
);
6309 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6311 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6312 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6313 struct dl
*dl
= data
;
6315 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6316 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6317 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6318 return MNL_CB_ERROR
;
6320 pr_out_region(dl
, tb
);
6325 static int cmd_region_show(struct dl
*dl
)
6327 struct nlmsghdr
*nlh
;
6328 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6331 if (dl_argc(dl
) == 0)
6332 flags
|= NLM_F_DUMP
;
6334 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6336 if (dl_argc(dl
) > 0) {
6337 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6342 pr_out_section_start(dl
, "regions");
6343 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6344 pr_out_section_end(dl
);
6348 static int cmd_region_snapshot_del(struct dl
*dl
)
6350 struct nlmsghdr
*nlh
;
6353 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6354 NLM_F_REQUEST
| NLM_F_ACK
);
6356 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6357 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6361 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6364 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6366 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6367 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6368 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6369 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6370 struct dl
*dl
= data
;
6373 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6374 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6375 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6376 return MNL_CB_ERROR
;
6378 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6379 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6380 if (err
!= MNL_CB_OK
)
6381 return MNL_CB_ERROR
;
6383 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6384 if (!nla_chunk_data
)
6387 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6388 if (!nla_chunk_addr
)
6391 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6392 mnl_attr_get_payload_len(nla_chunk_data
),
6393 mnl_attr_get_u64(nla_chunk_addr
));
6398 static int cmd_region_dump(struct dl
*dl
)
6400 struct nlmsghdr
*nlh
;
6403 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6404 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6406 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6407 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6411 pr_out_section_start(dl
, "dump");
6412 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6413 pr_out_section_end(dl
);
6414 if (!dl
->json_output
)
6419 static int cmd_region_read(struct dl
*dl
)
6421 struct nlmsghdr
*nlh
;
6424 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6425 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6427 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6428 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6429 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6433 pr_out_section_start(dl
, "read");
6434 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6435 pr_out_section_end(dl
);
6436 if (!dl
->json_output
)
6441 static void cmd_region_help(void)
6443 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6444 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6445 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6446 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6449 static int cmd_region(struct dl
*dl
)
6451 if (dl_no_arg(dl
)) {
6452 return cmd_region_show(dl
);
6453 } else if (dl_argv_match(dl
, "help")) {
6456 } else if (dl_argv_match(dl
, "show")) {
6458 return cmd_region_show(dl
);
6459 } else if (dl_argv_match(dl
, "del")) {
6461 return cmd_region_snapshot_del(dl
);
6462 } else if (dl_argv_match(dl
, "dump")) {
6464 return cmd_region_dump(dl
);
6465 } else if (dl_argv_match(dl
, "read")) {
6467 return cmd_region_read(dl
);
6469 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6473 static int cmd_health_set_params(struct dl
*dl
)
6475 struct nlmsghdr
*nlh
;
6478 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6479 NLM_F_REQUEST
| NLM_F_ACK
);
6480 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6481 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6482 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6486 dl_opts_put(nlh
, dl
);
6487 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6490 static int cmd_health_dump_clear(struct dl
*dl
)
6492 struct nlmsghdr
*nlh
;
6495 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6496 NLM_F_REQUEST
| NLM_F_ACK
);
6498 err
= dl_argv_parse_put(nlh
, dl
,
6499 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6503 dl_opts_put(nlh
, dl
);
6504 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6507 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6512 check_indent_newline(dl
);
6515 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6518 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6521 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6524 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6527 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6529 case MNL_TYPE_NUL_STRING
:
6530 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6532 case MNL_TYPE_BINARY
:
6533 len
= mnl_attr_get_payload_len(nl_data
);
6534 data
= mnl_attr_get_payload(nl_data
);
6535 pr_out_binary_value(dl
, data
, len
);
6543 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6548 pr_out_name(dl
, *name
);
6555 struct list_head list
;
6558 struct fmsg_cb_data
{
6562 struct list_head entry_list
;
6565 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6566 uint8_t *attr_value
, bool insert
)
6568 struct nest_entry
*entry
;
6571 entry
= malloc(sizeof(struct nest_entry
));
6575 entry
->attr_type
= *attr_value
;
6576 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6578 if (list_empty(&fmsg_data
->entry_list
))
6579 return MNL_CB_ERROR
;
6580 entry
= list_first_entry(&fmsg_data
->entry_list
,
6581 struct nest_entry
, list
);
6582 *attr_value
= entry
->attr_type
;
6583 list_del(&entry
->list
);
6589 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6592 pr_out_fmsg_name(dl
, name
);
6594 __pr_out_indent_inc();
6597 static void pr_out_fmsg_group_end(struct dl
*dl
)
6600 __pr_out_indent_dec();
6603 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6605 if (dl
->json_output
) {
6606 pr_out_fmsg_name(dl
, name
);
6607 open_json_object(NULL
);
6609 pr_out_fmsg_group_start(dl
, name
);
6613 static void pr_out_fmsg_end_object(struct dl
*dl
)
6615 if (dl
->json_output
)
6616 close_json_object();
6618 pr_out_fmsg_group_end(dl
);
6621 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6623 if (dl
->json_output
) {
6624 pr_out_fmsg_name(dl
, name
);
6625 open_json_array(PRINT_JSON
, NULL
);
6627 pr_out_fmsg_group_start(dl
, name
);
6631 static void pr_out_fmsg_end_array(struct dl
*dl
)
6633 if (dl
->json_output
)
6634 close_json_array(PRINT_JSON
, NULL
);
6636 pr_out_fmsg_group_end(dl
);
6639 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6642 struct dl
*dl
= fmsg_data
->dl
;
6643 uint8_t value
= nest_value
;
6646 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6647 if (err
!= MNL_CB_OK
)
6651 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6653 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6655 pr_out_fmsg_end_object(dl
);
6657 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6659 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6661 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6663 pr_out_fmsg_end_array(dl
);
6671 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6673 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6674 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6675 struct fmsg_cb_data
*fmsg_data
= data
;
6676 struct dl
*dl
= fmsg_data
->dl
;
6677 struct nlattr
*nla_object
;
6681 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6682 if (!tb
[DEVLINK_ATTR_FMSG
])
6683 return MNL_CB_ERROR
;
6685 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6686 attr_type
= mnl_attr_get_type(nla_object
);
6687 switch (attr_type
) {
6688 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6689 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6690 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6691 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6692 if (err
!= MNL_CB_OK
)
6695 case DEVLINK_ATTR_FMSG_NEST_END
:
6696 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6697 if (err
!= MNL_CB_OK
)
6700 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6701 free(fmsg_data
->name
);
6702 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6703 if (!fmsg_data
->name
)
6706 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6707 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6709 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6710 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6711 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6713 if (err
!= MNL_CB_OK
)
6723 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6725 /* FMSG is dynamic: opening of an object or array causes a
6726 * newline. JSON starts with an { or [, but plain text should
6727 * not start with a new line. Ensure this by setting
6728 * g_new_line_count to 1: avoiding newline before the first
6731 g_new_line_count
= 1;
6734 INIT_LIST_HEAD(&data
->entry_list
);
6737 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6739 struct fmsg_cb_data data
;
6740 struct nlmsghdr
*nlh
;
6743 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6745 err
= dl_argv_parse_put(nlh
, dl
,
6746 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6750 cmd_fmsg_init(dl
, &data
);
6751 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6756 static int cmd_health_dump_show(struct dl
*dl
)
6758 return cmd_health_object_common(dl
,
6759 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6763 static int cmd_health_diagnose(struct dl
*dl
)
6765 return cmd_health_object_common(dl
,
6766 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6770 static int cmd_health_recover(struct dl
*dl
)
6772 struct nlmsghdr
*nlh
;
6775 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6776 NLM_F_REQUEST
| NLM_F_ACK
);
6778 err
= dl_argv_parse_put(nlh
, dl
,
6779 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6783 dl_opts_put(nlh
, dl
);
6784 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6787 enum devlink_health_reporter_state
{
6788 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6789 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6792 static const char *health_state_name(uint8_t state
)
6795 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6796 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6797 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6798 return HEALTH_REPORTER_STATE_ERROR_STR
;
6800 return "<unknown state>";
6804 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6806 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6807 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6808 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6809 struct sysinfo s_info
;
6815 info
= localtime(&now
);
6816 err
= sysinfo(&s_info
);
6819 /* Subtract uptime in sec from now yields the time of system
6820 * uptime. To this, add time_ms which is the amount of
6821 * milliseconds elapsed between uptime and the dump taken.
6823 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6824 info
= localtime(&sec
);
6826 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6827 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6828 check_indent_newline(dl
);
6829 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6830 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6833 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6835 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6836 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6841 ts
= mnl_attr_get_u64(attr
);
6842 tv_sec
= ts
/ 1000000000;
6843 tm
= localtime(&tv_sec
);
6845 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6846 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6848 check_indent_newline(dl
);
6849 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6850 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6853 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6855 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6856 enum devlink_health_reporter_state state
;
6859 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6861 if (err
!= MNL_CB_OK
)
6864 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6865 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6866 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6867 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6870 pr_out_handle_start_arr(dl
, tb_health
);
6872 check_indent_newline(dl
);
6873 print_string(PRINT_ANY
, "reporter", "reporter %s",
6874 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6875 if (!dl
->json_output
) {
6877 __pr_out_indent_inc();
6879 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6880 check_indent_newline(dl
);
6881 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6882 pr_out_u64(dl
, "error",
6883 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6884 pr_out_u64(dl
, "recover",
6885 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6886 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6887 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6888 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6889 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6890 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6891 pr_out_u64(dl
, "grace_period",
6892 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6893 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6894 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6895 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6897 __pr_out_indent_dec();
6898 pr_out_handle_end(dl
);
6901 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6903 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6904 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6905 struct dl
*dl
= data
;
6907 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6908 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6909 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6910 return MNL_CB_ERROR
;
6912 pr_out_health(dl
, tb
);
6917 static int cmd_health_show(struct dl
*dl
)
6919 struct nlmsghdr
*nlh
;
6920 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6923 if (dl_argc(dl
) == 0)
6924 flags
|= NLM_F_DUMP
;
6925 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6928 if (dl_argc(dl
) > 0) {
6929 err
= dl_argv_parse_put(nlh
, dl
,
6931 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6935 pr_out_section_start(dl
, "health");
6937 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6938 pr_out_section_end(dl
);
6942 static void cmd_health_help(void)
6944 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6945 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6946 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6947 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6948 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6949 pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
6950 pr_err(" [ grace_period MSEC ]\n");
6951 pr_err(" [ auto_recover { true | false } ]\n");
6954 static int cmd_health(struct dl
*dl
)
6956 if (dl_argv_match(dl
, "help")) {
6959 } else if (dl_argv_match(dl
, "show") ||
6960 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6962 return cmd_health_show(dl
);
6963 } else if (dl_argv_match(dl
, "recover")) {
6965 return cmd_health_recover(dl
);
6966 } else if (dl_argv_match(dl
, "diagnose")) {
6968 return cmd_health_diagnose(dl
);
6969 } else if (dl_argv_match(dl
, "dump")) {
6971 if (dl_argv_match(dl
, "show")) {
6973 return cmd_health_dump_show(dl
);
6974 } else if (dl_argv_match(dl
, "clear")) {
6976 return cmd_health_dump_clear(dl
);
6978 } else if (dl_argv_match(dl
, "set")) {
6980 return cmd_health_set_params(dl
);
6982 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6986 static const char *trap_type_name(uint8_t type
)
6989 case DEVLINK_TRAP_TYPE_DROP
:
6991 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6994 return "<unknown type>";
6998 static const char *trap_action_name(uint8_t action
)
7001 case DEVLINK_TRAP_ACTION_DROP
:
7003 case DEVLINK_TRAP_ACTION_TRAP
:
7006 return "<unknown action>";
7010 static const char *trap_metadata_name(const struct nlattr
*attr
)
7012 switch (attr
->nla_type
) {
7013 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7014 return "input_port";
7015 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7016 return "flow_action_cookie";
7018 return "<unknown metadata type>";
7021 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7023 struct nlattr
*attr_metadata
;
7025 pr_out_array_start(dl
, "metadata");
7026 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7027 check_indent_newline(dl
);
7028 print_string(PRINT_ANY
, NULL
, "%s",
7029 trap_metadata_name(attr_metadata
));
7031 pr_out_array_end(dl
);
7034 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7036 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7037 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7040 pr_out_handle_start_arr(dl
, tb
);
7042 __pr_out_handle_start(dl
, tb
, true, false);
7044 check_indent_newline(dl
);
7045 print_string(PRINT_ANY
, "name", "name %s",
7046 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7047 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7048 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7049 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7050 print_string(PRINT_ANY
, "group", " group %s",
7051 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7053 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7054 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7055 pr_out_handle_end(dl
);
7058 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7060 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7061 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7062 struct dl
*dl
= data
;
7064 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7065 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7066 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7067 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7068 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7069 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7070 return MNL_CB_ERROR
;
7072 pr_out_trap(dl
, tb
, true);
7077 static void cmd_trap_help(void)
7079 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
7080 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7081 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
7082 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7083 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7084 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7085 pr_err(" devlink trap policer show DEV policer POLICER\n");
7088 static int cmd_trap_show(struct dl
*dl
)
7090 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7091 struct nlmsghdr
*nlh
;
7094 if (dl_argc(dl
) == 0)
7095 flags
|= NLM_F_DUMP
;
7097 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7099 if (dl_argc(dl
) > 0) {
7100 err
= dl_argv_parse_put(nlh
, dl
,
7101 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7106 pr_out_section_start(dl
, "trap");
7107 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7108 pr_out_section_end(dl
);
7113 static int cmd_trap_set(struct dl
*dl
)
7115 struct nlmsghdr
*nlh
;
7118 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7119 NLM_F_REQUEST
| NLM_F_ACK
);
7121 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7122 DL_OPT_TRAP_ACTION
);
7126 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7129 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7132 pr_out_handle_start_arr(dl
, tb
);
7134 __pr_out_handle_start(dl
, tb
, true, false);
7136 check_indent_newline(dl
);
7137 print_string(PRINT_ANY
, "name", "name %s",
7138 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7139 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7140 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7141 print_uint(PRINT_ANY
, "policer", " policer %u",
7142 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7143 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7144 pr_out_handle_end(dl
);
7147 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7149 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7150 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7151 struct dl
*dl
= data
;
7153 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7154 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7155 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7156 return MNL_CB_ERROR
;
7158 pr_out_trap_group(dl
, tb
, true);
7163 static int cmd_trap_group_show(struct dl
*dl
)
7165 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7166 struct nlmsghdr
*nlh
;
7169 if (dl_argc(dl
) == 0)
7170 flags
|= NLM_F_DUMP
;
7172 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7174 if (dl_argc(dl
) > 0) {
7175 err
= dl_argv_parse_put(nlh
, dl
,
7176 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7182 pr_out_section_start(dl
, "trap_group");
7183 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7184 pr_out_section_end(dl
);
7189 static int cmd_trap_group_set(struct dl
*dl
)
7191 struct nlmsghdr
*nlh
;
7194 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7195 NLM_F_REQUEST
| NLM_F_ACK
);
7197 err
= dl_argv_parse_put(nlh
, dl
,
7198 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7199 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7203 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7206 static int cmd_trap_group(struct dl
*dl
)
7208 if (dl_argv_match(dl
, "help")) {
7211 } else if (dl_argv_match(dl
, "show") ||
7212 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7214 return cmd_trap_group_show(dl
);
7215 } else if (dl_argv_match(dl
, "set")) {
7217 return cmd_trap_group_set(dl
);
7219 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7223 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7226 pr_out_handle_start_arr(dl
, tb
);
7228 __pr_out_handle_start(dl
, tb
, true, false);
7230 check_indent_newline(dl
);
7231 print_uint(PRINT_ANY
, "policer", "policer %u",
7232 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7233 print_u64(PRINT_ANY
, "rate", " rate %llu",
7234 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7235 print_u64(PRINT_ANY
, "burst", " burst %llu",
7236 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7237 if (tb
[DEVLINK_ATTR_STATS
])
7238 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7239 pr_out_handle_end(dl
);
7242 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7244 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7245 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7246 struct dl
*dl
= data
;
7248 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7249 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7250 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7251 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7252 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7253 return MNL_CB_ERROR
;
7255 pr_out_trap_policer(dl
, tb
, true);
7260 static int cmd_trap_policer_show(struct dl
*dl
)
7262 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7263 struct nlmsghdr
*nlh
;
7266 if (dl_argc(dl
) == 0)
7267 flags
|= NLM_F_DUMP
;
7269 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7271 if (dl_argc(dl
) > 0) {
7272 err
= dl_argv_parse_put(nlh
, dl
,
7273 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7279 pr_out_section_start(dl
, "trap_policer");
7280 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7281 pr_out_section_end(dl
);
7286 static int cmd_trap_policer_set(struct dl
*dl
)
7288 struct nlmsghdr
*nlh
;
7291 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7292 NLM_F_REQUEST
| NLM_F_ACK
);
7294 err
= dl_argv_parse_put(nlh
, dl
,
7295 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7296 DL_OPT_TRAP_POLICER_RATE
|
7297 DL_OPT_TRAP_POLICER_BURST
);
7301 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7304 static int cmd_trap_policer(struct dl
*dl
)
7306 if (dl_argv_match(dl
, "help")) {
7309 } else if (dl_argv_match(dl
, "show") ||
7310 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7312 return cmd_trap_policer_show(dl
);
7313 } else if (dl_argv_match(dl
, "set")) {
7315 return cmd_trap_policer_set(dl
);
7317 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7321 static int cmd_trap(struct dl
*dl
)
7323 if (dl_argv_match(dl
, "help")) {
7326 } else if (dl_argv_match(dl
, "show") ||
7327 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7329 return cmd_trap_show(dl
);
7330 } else if (dl_argv_match(dl
, "set")) {
7332 return cmd_trap_set(dl
);
7333 } else if (dl_argv_match(dl
, "group")) {
7335 return cmd_trap_group(dl
);
7336 } else if (dl_argv_match(dl
, "policer")) {
7338 return cmd_trap_policer(dl
);
7340 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7344 static void help(void)
7346 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7347 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7348 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7349 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7352 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7357 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7360 } else if (dl_argv_match(dl
, "dev")) {
7363 } else if (dl_argv_match(dl
, "port")) {
7365 return cmd_port(dl
);
7366 } else if (dl_argv_match(dl
, "sb")) {
7369 } else if (dl_argv_match(dl
, "monitor")) {
7372 } else if (dl_argv_match(dl
, "dpipe")) {
7374 return cmd_dpipe(dl
);
7375 } else if (dl_argv_match(dl
, "resource")) {
7377 return cmd_resource(dl
);
7378 } else if (dl_argv_match(dl
, "region")) {
7380 return cmd_region(dl
);
7381 } else if (dl_argv_match(dl
, "health")) {
7383 return cmd_health(dl
);
7384 } else if (dl_argv_match(dl
, "trap")) {
7386 return cmd_trap(dl
);
7388 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7392 static int dl_init(struct dl
*dl
)
7396 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7398 pr_err("Failed to connect to devlink Netlink\n");
7402 err
= ifname_map_init(dl
);
7404 pr_err("Failed to create index map\n");
7405 goto err_ifname_map_create
;
7407 new_json_obj_plain(dl
->json_output
);
7410 err_ifname_map_create
:
7411 mnlg_socket_close(dl
->nlg
);
7415 static void dl_fini(struct dl
*dl
)
7417 delete_json_obj_plain();
7418 ifname_map_fini(dl
);
7419 mnlg_socket_close(dl
->nlg
);
7422 static struct dl
*dl_alloc(void)
7426 dl
= calloc(1, sizeof(*dl
));
7432 static void dl_free(struct dl
*dl
)
7437 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7441 int ret
= EXIT_SUCCESS
;
7443 if (name
&& strcmp(name
, "-") != 0) {
7444 if (freopen(name
, "r", stdin
) == NULL
) {
7446 "Cannot open file \"%s\" for reading: %s\n",
7447 name
, strerror(errno
));
7448 return EXIT_FAILURE
;
7453 while (getcmdline(&line
, &len
, stdin
) != -1) {
7457 largc
= makeargs(line
, largv
, 100);
7459 continue; /* blank line */
7461 if (dl_cmd(dl
, largc
, largv
)) {
7462 fprintf(stderr
, "Command failed %s:%d\n",
7476 int main(int argc
, char **argv
)
7478 static const struct option long_options
[] = {
7479 { "Version", no_argument
, NULL
, 'V' },
7480 { "force", no_argument
, NULL
, 'f' },
7481 { "batch", required_argument
, NULL
, 'b' },
7482 { "no-nice-names", no_argument
, NULL
, 'n' },
7483 { "json", no_argument
, NULL
, 'j' },
7484 { "pretty", no_argument
, NULL
, 'p' },
7485 { "verbose", no_argument
, NULL
, 'v' },
7486 { "statistics", no_argument
, NULL
, 's' },
7487 { "Netns", required_argument
, NULL
, 'N' },
7488 { NULL
, 0, NULL
, 0 }
7490 const char *batch_file
= NULL
;
7499 pr_err("Failed to allocate memory for devlink\n");
7500 return EXIT_FAILURE
;
7503 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7504 long_options
, NULL
)) >= 0) {
7508 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7515 batch_file
= optarg
;
7518 dl
->no_nice_names
= true;
7521 dl
->json_output
= true;
7533 if (netns_switch(optarg
)) {
7539 pr_err("Unknown option.\n");
7556 err
= dl_batch(dl
, batch_file
, force
);
7558 err
= dl_cmd(dl
, argc
, argv
);