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 void dummy_signal_handler(int signum
)
159 static int _mnlg_socket_recv_run_intr(struct mnlg_socket
*nlg
,
160 mnl_cb_t data_cb
, void *data
)
162 struct sigaction act
, oact
;
165 act
.sa_handler
= dummy_signal_handler
;
166 sigemptyset(&act
.sa_mask
);
167 act
.sa_flags
= SA_NODEFER
;
169 sigaction(SIGINT
, &act
, &oact
);
170 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
171 sigaction(SIGINT
, &oact
, NULL
);
172 if (err
< 0 && errno
!= EINTR
) {
173 pr_err("devlink answers: %s\n", strerror(errno
));
179 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
180 const struct nlmsghdr
*nlh
)
184 err
= mnlg_socket_send(nlg
, nlh
);
186 pr_err("Failed to call mnlg_socket_send\n");
192 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
193 const struct nlmsghdr
*nlh
,
194 mnl_cb_t data_cb
, void *data
)
198 err
= _mnlg_socket_send(nlg
, nlh
);
201 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
204 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
205 const char *group_name
)
209 err
= mnlg_socket_group_add(nlg
, group_name
);
211 pr_err("Failed to call mnlg_socket_group_add\n");
218 struct list_head list
;
225 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
226 const char *dev_name
,
230 struct ifname_map
*ifname_map
;
232 ifname_map
= calloc(1, sizeof(*ifname_map
));
235 ifname_map
->bus_name
= strdup(bus_name
);
236 ifname_map
->dev_name
= strdup(dev_name
);
237 ifname_map
->port_index
= port_index
;
238 ifname_map
->ifname
= strdup(ifname
);
239 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
240 !ifname_map
->ifname
) {
241 free(ifname_map
->ifname
);
242 free(ifname_map
->dev_name
);
243 free(ifname_map
->bus_name
);
250 static void ifname_map_free(struct ifname_map
*ifname_map
)
252 free(ifname_map
->ifname
);
253 free(ifname_map
->dev_name
);
254 free(ifname_map
->bus_name
);
258 #define DL_OPT_HANDLE BIT(0)
259 #define DL_OPT_HANDLEP BIT(1)
260 #define DL_OPT_PORT_TYPE BIT(2)
261 #define DL_OPT_PORT_COUNT BIT(3)
262 #define DL_OPT_SB BIT(4)
263 #define DL_OPT_SB_POOL BIT(5)
264 #define DL_OPT_SB_SIZE BIT(6)
265 #define DL_OPT_SB_TYPE BIT(7)
266 #define DL_OPT_SB_THTYPE BIT(8)
267 #define DL_OPT_SB_TH BIT(9)
268 #define DL_OPT_SB_TC BIT(10)
269 #define DL_OPT_ESWITCH_MODE BIT(11)
270 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
271 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
272 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
273 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
274 #define DL_OPT_RESOURCE_PATH BIT(16)
275 #define DL_OPT_RESOURCE_SIZE BIT(17)
276 #define DL_OPT_PARAM_NAME BIT(18)
277 #define DL_OPT_PARAM_VALUE BIT(19)
278 #define DL_OPT_PARAM_CMODE BIT(20)
279 #define DL_OPT_HANDLE_REGION BIT(21)
280 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
281 #define DL_OPT_REGION_ADDRESS BIT(23)
282 #define DL_OPT_REGION_LENGTH BIT(24)
283 #define DL_OPT_FLASH_FILE_NAME BIT(25)
284 #define DL_OPT_FLASH_COMPONENT BIT(26)
285 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
286 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
287 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
288 #define DL_OPT_TRAP_NAME BIT(30)
289 #define DL_OPT_TRAP_ACTION BIT(31)
290 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
291 #define DL_OPT_NETNS BIT(33)
292 #define DL_OPT_TRAP_POLICER_ID BIT(34)
293 #define DL_OPT_TRAP_POLICER_RATE BIT(35)
294 #define DL_OPT_TRAP_POLICER_BURST BIT(36)
297 uint64_t present
; /* flags of present items */
301 enum devlink_port_type port_type
;
304 uint16_t sb_pool_index
;
305 uint32_t sb_pool_size
;
306 enum devlink_sb_pool_type sb_pool_type
;
307 enum devlink_sb_threshold_type sb_pool_thtype
;
308 uint32_t sb_threshold
;
309 uint16_t sb_tc_index
;
310 enum devlink_eswitch_mode eswitch_mode
;
311 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
312 const char *dpipe_table_name
;
313 bool dpipe_counters_enabled
;
314 enum devlink_eswitch_encap_mode eswitch_encap_mode
;
315 const char *resource_path
;
316 uint64_t resource_size
;
317 uint32_t resource_id
;
318 bool resource_id_valid
;
319 const char *param_name
;
320 const char *param_value
;
321 enum devlink_param_cmode cmode
;
323 uint32_t region_snapshot_id
;
324 uint64_t region_address
;
325 uint64_t region_length
;
326 const char *flash_file_name
;
327 const char *flash_component
;
328 const char *reporter_name
;
329 uint64_t reporter_graceful_period
;
330 bool reporter_auto_recover
;
331 const char *trap_name
;
332 const char *trap_group_name
;
333 enum devlink_trap_action trap_action
;
336 uint32_t trap_policer_id
;
337 uint64_t trap_policer_rate
;
338 uint64_t trap_policer_burst
;
342 struct mnlg_socket
*nlg
;
343 struct list_head ifname_map_list
;
360 static int dl_argc(struct dl
*dl
)
365 static char *dl_argv(struct dl
*dl
)
367 if (dl_argc(dl
) == 0)
372 static void dl_arg_inc(struct dl
*dl
)
374 if (dl_argc(dl
) == 0)
380 static void dl_arg_dec(struct dl
*dl
)
386 static char *dl_argv_next(struct dl
*dl
)
390 if (dl_argc(dl
) == 0)
398 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
400 if (index
>= dl_argc(dl
))
402 return dl
->argv
[index
];
405 static int strcmpx(const char *str1
, const char *str2
)
407 if (strlen(str1
) > strlen(str2
))
409 return strncmp(str1
, str2
, strlen(str1
));
412 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
414 if (dl_argc(dl
) == 0)
416 return strcmpx(dl_argv(dl
), pattern
) == 0;
419 static bool dl_no_arg(struct dl
*dl
)
421 return dl_argc(dl
) == 0;
424 static void __pr_out_indent_newline(struct dl
*dl
)
426 if (!g_indent_newline
&& !dl
->json_output
)
430 static void check_indent_newline(struct dl
*dl
)
432 __pr_out_indent_newline(dl
);
434 if (g_indent_newline
&& !is_json_context()) {
435 printf("%s", g_indent_str
);
436 g_indent_newline
= false;
438 g_new_line_count
= 0;
441 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
442 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
443 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
444 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
445 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
446 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
447 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
448 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
449 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
450 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
451 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
452 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
453 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
454 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
455 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
456 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
457 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
458 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
459 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
460 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
461 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
462 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
463 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
464 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
465 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
466 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
467 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
468 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
469 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
470 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
471 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
472 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
473 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
474 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
475 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
476 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
477 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
478 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
479 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
480 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
481 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
482 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
483 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
484 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
485 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
486 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
487 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
488 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
489 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
490 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
491 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
492 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
493 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
494 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
495 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
496 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
497 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
498 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
499 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
500 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
501 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
502 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
503 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
504 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
505 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
506 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
507 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
508 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
509 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
510 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
511 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
512 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
513 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
514 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
515 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
516 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
517 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
518 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
519 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
520 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
521 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
522 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
523 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
524 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
525 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
526 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
527 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
528 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
529 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
530 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
531 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
532 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
533 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
534 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
535 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
536 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
537 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
538 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
539 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
540 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
541 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
542 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
543 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
546 static const enum mnl_attr_data_type
547 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
548 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
549 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
550 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
553 static int attr_cb(const struct nlattr
*attr
, void *data
)
555 const struct nlattr
**tb
= data
;
558 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
561 type
= mnl_attr_get_type(attr
);
562 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
569 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
571 const struct nlattr
**tb
= data
;
574 /* Allow the tool to work on top of newer kernels that might contain
577 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
580 type
= mnl_attr_get_type(attr
);
581 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
588 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
590 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
591 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
592 struct dl
*dl
= data
;
593 struct ifname_map
*ifname_map
;
594 const char *bus_name
;
595 const char *dev_name
;
596 uint32_t port_ifindex
;
597 const char *port_ifname
;
599 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
600 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
601 !tb
[DEVLINK_ATTR_PORT_INDEX
])
604 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
607 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
608 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
609 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
610 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
611 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
612 port_ifindex
, port_ifname
);
615 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
620 static void ifname_map_fini(struct dl
*dl
)
622 struct ifname_map
*ifname_map
, *tmp
;
624 list_for_each_entry_safe(ifname_map
, tmp
,
625 &dl
->ifname_map_list
, list
) {
626 list_del(&ifname_map
->list
);
627 ifname_map_free(ifname_map
);
631 static int ifname_map_init(struct dl
*dl
)
633 struct nlmsghdr
*nlh
;
636 INIT_LIST_HEAD(&dl
->ifname_map_list
);
638 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
639 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
641 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
649 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
650 char **p_bus_name
, char **p_dev_name
,
651 uint32_t *p_port_index
)
653 struct ifname_map
*ifname_map
;
655 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
656 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
657 *p_bus_name
= ifname_map
->bus_name
;
658 *p_dev_name
= ifname_map
->dev_name
;
659 *p_port_index
= ifname_map
->port_index
;
666 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
667 const char *dev_name
, uint32_t port_index
,
670 struct ifname_map
*ifname_map
;
672 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
673 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
674 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
675 port_index
== ifname_map
->port_index
) {
676 *p_ifname
= ifname_map
->ifname
;
683 static unsigned int strslashcount(char *str
)
685 unsigned int count
= 0;
688 while ((pos
= strchr(pos
, '/'))) {
695 static int strslashrsplit(char *str
, char **before
, char **after
)
699 slash
= strrchr(str
, '/');
708 static int strtouint64_t(const char *str
, uint64_t *p_val
)
711 unsigned long long int val
;
713 val
= strtoull(str
, &endptr
, 10);
714 if (endptr
== str
|| *endptr
!= '\0')
722 static int strtouint32_t(const char *str
, uint32_t *p_val
)
725 unsigned long int val
;
727 val
= strtoul(str
, &endptr
, 10);
728 if (endptr
== str
|| *endptr
!= '\0')
736 static int strtouint16_t(const char *str
, uint16_t *p_val
)
739 unsigned long int val
;
741 val
= strtoul(str
, &endptr
, 10);
742 if (endptr
== str
|| *endptr
!= '\0')
750 static int strtouint8_t(const char *str
, uint8_t *p_val
)
753 unsigned long int val
;
755 val
= strtoul(str
, &endptr
, 10);
756 if (endptr
== str
|| *endptr
!= '\0')
764 static int strtobool(const char *str
, bool *p_val
)
768 if (!strcmp(str
, "true") || !strcmp(str
, "1") ||
769 !strcmp(str
, "enable"))
771 else if (!strcmp(str
, "false") || !strcmp(str
, "0") ||
772 !strcmp(str
, "disable"))
780 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
782 strslashrsplit(str
, p_bus_name
, p_dev_name
);
786 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
788 char *str
= dl_argv_next(dl
);
791 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
794 if (strslashcount(str
) != 1) {
795 pr_err("Wrong devlink identification string format.\n");
796 pr_err("Expected \"bus_name/dev_name\".\n");
799 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
802 static int __dl_argv_handle_port(char *str
,
803 char **p_bus_name
, char **p_dev_name
,
804 uint32_t *p_port_index
)
810 err
= strslashrsplit(str
, &handlestr
, &portstr
);
812 pr_err("Port identification \"%s\" is invalid\n", str
);
815 err
= strtouint32_t(portstr
, p_port_index
);
817 pr_err("Port index \"%s\" is not a number or not within range\n",
821 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
823 pr_err("Port identification \"%s\" is invalid\n", str
);
829 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
830 char **p_bus_name
, char **p_dev_name
,
831 uint32_t *p_port_index
)
835 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
838 pr_err("Netdevice \"%s\" not found\n", str
);
844 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
845 char **p_dev_name
, uint32_t *p_port_index
)
847 char *str
= dl_argv_next(dl
);
848 unsigned int slash_count
;
851 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
854 slash_count
= strslashcount(str
);
855 switch (slash_count
) {
857 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
858 p_dev_name
, p_port_index
);
860 return __dl_argv_handle_port(str
, p_bus_name
,
861 p_dev_name
, p_port_index
);
863 pr_err("Wrong port identification string format.\n");
864 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
869 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
870 char **p_dev_name
, uint32_t *p_port_index
,
871 uint64_t *p_handle_bit
)
873 char *str
= dl_argv_next(dl
);
874 unsigned int slash_count
;
878 pr_err("One of following identifications expected:\n"
879 "Devlink identification (\"bus_name/dev_name\")\n"
880 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
883 slash_count
= strslashcount(str
);
884 if (slash_count
== 1) {
885 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
888 *p_handle_bit
= DL_OPT_HANDLE
;
889 } else if (slash_count
== 2) {
890 err
= __dl_argv_handle_port(str
, p_bus_name
,
891 p_dev_name
, p_port_index
);
894 *p_handle_bit
= DL_OPT_HANDLEP
;
895 } else if (slash_count
== 0) {
896 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
897 p_dev_name
, p_port_index
);
900 *p_handle_bit
= DL_OPT_HANDLEP
;
902 pr_err("Wrong port identification string format.\n");
903 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
909 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
910 char **p_dev_name
, char **p_region
)
915 err
= strslashrsplit(str
, &handlestr
, p_region
);
917 pr_err("Region identification \"%s\" is invalid\n", str
);
920 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
922 pr_err("Region identification \"%s\" is invalid\n", str
);
928 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
929 char **p_dev_name
, char **p_region
)
931 char *str
= dl_argv_next(dl
);
932 unsigned int slash_count
;
935 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
939 slash_count
= strslashcount(str
);
940 if (slash_count
!= 2) {
941 pr_err("Wrong region identification string format.\n");
942 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
946 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
949 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
951 char *str
= dl_argv_next(dl
);
955 pr_err("Unsigned number argument expected\n");
959 err
= strtouint64_t(str
, p_val
);
961 pr_err("\"%s\" is not a number or not within range\n", str
);
967 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
969 char *str
= dl_argv_next(dl
);
973 pr_err("Unsigned number argument expected\n");
977 err
= strtouint32_t(str
, p_val
);
979 pr_err("\"%s\" is not a number or not within range\n", str
);
985 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
987 char *str
= dl_argv_next(dl
);
991 pr_err("Unsigned number argument expected\n");
995 err
= strtouint16_t(str
, p_val
);
997 pr_err("\"%s\" is not a number or not within range\n", str
);
1003 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
1005 char *str
= dl_argv_next(dl
);
1009 pr_err("Boolean argument expected\n");
1013 err
= strtobool(str
, p_val
);
1015 pr_err("\"%s\" is not a valid boolean value\n", str
);
1021 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
1023 const char *str
= dl_argv_next(dl
);
1026 pr_err("String parameter expected\n");
1033 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1035 if (strcmp(typestr
, "auto") == 0) {
1036 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1037 } else if (strcmp(typestr
, "eth") == 0) {
1038 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1039 } else if (strcmp(typestr
, "ib") == 0) {
1040 *p_type
= DEVLINK_PORT_TYPE_IB
;
1042 pr_err("Unknown port type \"%s\"\n", typestr
);
1048 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1050 if (strcmp(typestr
, "ingress") == 0) {
1051 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1052 } else if (strcmp(typestr
, "egress") == 0) {
1053 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1055 pr_err("Unknown pool type \"%s\"\n", typestr
);
1061 static int threshold_type_get(const char *typestr
,
1062 enum devlink_sb_threshold_type
*p_type
)
1064 if (strcmp(typestr
, "static") == 0) {
1065 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1066 } else if (strcmp(typestr
, "dynamic") == 0) {
1067 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1069 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1075 static int eswitch_mode_get(const char *typestr
,
1076 enum devlink_eswitch_mode
*p_mode
)
1078 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1079 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1080 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1081 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1083 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1089 static int eswitch_inline_mode_get(const char *typestr
,
1090 enum devlink_eswitch_inline_mode
*p_mode
)
1092 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1093 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1094 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1095 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1096 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1097 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1098 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1099 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1101 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1108 eswitch_encap_mode_get(const char *typestr
,
1109 enum devlink_eswitch_encap_mode
*p_encap_mode
)
1111 /* The initial implementation incorrectly accepted "enable"/"disable".
1112 * Carry it to maintain backward compatibility.
1114 if (strcmp(typestr
, "disable") == 0 ||
1115 strcmp(typestr
, ESWITCH_ENCAP_MODE_NONE
) == 0) {
1116 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_NONE
;
1117 } else if (strcmp(typestr
, "enable") == 0 ||
1118 strcmp(typestr
, ESWITCH_ENCAP_MODE_BASIC
) == 0) {
1119 *p_encap_mode
= DEVLINK_ESWITCH_ENCAP_MODE_BASIC
;
1121 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1127 static int param_cmode_get(const char *cmodestr
,
1128 enum devlink_param_cmode
*cmode
)
1130 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1131 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1132 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1133 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1134 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1135 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1137 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1143 static int trap_action_get(const char *actionstr
,
1144 enum devlink_trap_action
*p_action
)
1146 if (strcmp(actionstr
, "drop") == 0) {
1147 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1148 } else if (strcmp(actionstr
, "trap") == 0) {
1149 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1151 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1157 struct dl_args_metadata
{
1159 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1162 static const struct dl_args_metadata dl_args_required
[] = {
1163 {DL_OPT_PORT_TYPE
, "Port type not set."},
1164 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1165 {DL_OPT_SB_POOL
, "Pool index option expected."},
1166 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1167 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1168 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1169 {DL_OPT_SB_TH
, "Threshold option expected."},
1170 {DL_OPT_SB_TC
, "TC index option expected."},
1171 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1172 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1173 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1174 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1175 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1176 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1177 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1178 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1179 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1180 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1181 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1182 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1183 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1184 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1185 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1186 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1189 static int dl_args_finding_required_validate(uint64_t o_required
,
1195 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1196 o_flag
= dl_args_required
[i
].o_flag
;
1197 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1198 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1202 if (o_required
& ~o_found
) {
1203 pr_err("BUG: unknown argument required but not found\n");
1209 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1210 uint64_t o_optional
)
1212 struct dl_opts
*opts
= &dl
->opts
;
1213 uint64_t o_all
= o_required
| o_optional
;
1214 uint64_t o_found
= 0;
1217 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1218 uint64_t handle_bit
;
1220 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1221 &opts
->port_index
, &handle_bit
);
1224 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1225 o_found
|= handle_bit
;
1226 } else if (o_required
& DL_OPT_HANDLE
) {
1227 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1230 o_found
|= DL_OPT_HANDLE
;
1231 } else if (o_required
& DL_OPT_HANDLEP
) {
1232 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1236 o_found
|= DL_OPT_HANDLEP
;
1237 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1238 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1240 &opts
->region_name
);
1243 o_found
|= DL_OPT_HANDLE_REGION
;
1246 while (dl_argc(dl
)) {
1247 if (dl_argv_match(dl
, "type") &&
1248 (o_all
& DL_OPT_PORT_TYPE
)) {
1249 const char *typestr
;
1252 err
= dl_argv_str(dl
, &typestr
);
1255 err
= port_type_get(typestr
, &opts
->port_type
);
1258 o_found
|= DL_OPT_PORT_TYPE
;
1259 } else if (dl_argv_match(dl
, "count") &&
1260 (o_all
& DL_OPT_PORT_COUNT
)) {
1262 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1265 o_found
|= DL_OPT_PORT_COUNT
;
1266 } else if (dl_argv_match(dl
, "sb") &&
1267 (o_all
& DL_OPT_SB
)) {
1269 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1272 o_found
|= DL_OPT_SB
;
1273 } else if (dl_argv_match(dl
, "pool") &&
1274 (o_all
& DL_OPT_SB_POOL
)) {
1276 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1279 o_found
|= DL_OPT_SB_POOL
;
1280 } else if (dl_argv_match(dl
, "size") &&
1281 (o_all
& DL_OPT_SB_SIZE
)) {
1283 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1286 o_found
|= DL_OPT_SB_SIZE
;
1287 } else if (dl_argv_match(dl
, "type") &&
1288 (o_all
& DL_OPT_SB_TYPE
)) {
1289 const char *typestr
;
1292 err
= dl_argv_str(dl
, &typestr
);
1295 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1298 o_found
|= DL_OPT_SB_TYPE
;
1299 } else if (dl_argv_match(dl
, "thtype") &&
1300 (o_all
& DL_OPT_SB_THTYPE
)) {
1301 const char *typestr
;
1304 err
= dl_argv_str(dl
, &typestr
);
1307 err
= threshold_type_get(typestr
,
1308 &opts
->sb_pool_thtype
);
1311 o_found
|= DL_OPT_SB_THTYPE
;
1312 } else if (dl_argv_match(dl
, "th") &&
1313 (o_all
& DL_OPT_SB_TH
)) {
1315 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1318 o_found
|= DL_OPT_SB_TH
;
1319 } else if (dl_argv_match(dl
, "tc") &&
1320 (o_all
& DL_OPT_SB_TC
)) {
1322 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1325 o_found
|= DL_OPT_SB_TC
;
1326 } else if (dl_argv_match(dl
, "mode") &&
1327 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1328 const char *typestr
;
1331 err
= dl_argv_str(dl
, &typestr
);
1334 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1337 o_found
|= DL_OPT_ESWITCH_MODE
;
1338 } else if (dl_argv_match(dl
, "inline-mode") &&
1339 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1340 const char *typestr
;
1343 err
= dl_argv_str(dl
, &typestr
);
1346 err
= eswitch_inline_mode_get(
1347 typestr
, &opts
->eswitch_inline_mode
);
1350 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1351 } else if (dl_argv_match(dl
, "name") &&
1352 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1354 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1357 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1358 } else if ((dl_argv_match(dl
, "counters") ||
1359 dl_argv_match(dl
, "counters_enabled")) &&
1360 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1362 err
= dl_argv_bool(dl
, &opts
->dpipe_counters_enabled
);
1365 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1366 } else if ((dl_argv_match(dl
, "encap") || /* Original incorrect implementation */
1367 dl_argv_match(dl
, "encap-mode")) &&
1368 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1369 const char *typestr
;
1372 err
= dl_argv_str(dl
, &typestr
);
1375 err
= eswitch_encap_mode_get(typestr
,
1376 &opts
->eswitch_encap_mode
);
1379 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1380 } else if (dl_argv_match(dl
, "path") &&
1381 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1383 err
= dl_argv_str(dl
, &opts
->resource_path
);
1386 o_found
|= DL_OPT_RESOURCE_PATH
;
1387 } else if (dl_argv_match(dl
, "size") &&
1388 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1390 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1393 o_found
|= DL_OPT_RESOURCE_SIZE
;
1394 } else if (dl_argv_match(dl
, "name") &&
1395 (o_all
& DL_OPT_PARAM_NAME
)) {
1397 err
= dl_argv_str(dl
, &opts
->param_name
);
1400 o_found
|= DL_OPT_PARAM_NAME
;
1401 } else if (dl_argv_match(dl
, "value") &&
1402 (o_all
& DL_OPT_PARAM_VALUE
)) {
1404 err
= dl_argv_str(dl
, &opts
->param_value
);
1407 o_found
|= DL_OPT_PARAM_VALUE
;
1408 } else if (dl_argv_match(dl
, "cmode") &&
1409 (o_all
& DL_OPT_PARAM_CMODE
)) {
1410 const char *cmodestr
;
1413 err
= dl_argv_str(dl
, &cmodestr
);
1416 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1419 o_found
|= DL_OPT_PARAM_CMODE
;
1420 } else if (dl_argv_match(dl
, "snapshot") &&
1421 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1423 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1426 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1427 } else if (dl_argv_match(dl
, "address") &&
1428 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1430 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1433 o_found
|= DL_OPT_REGION_ADDRESS
;
1434 } else if (dl_argv_match(dl
, "length") &&
1435 (o_all
& DL_OPT_REGION_LENGTH
)) {
1437 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1440 o_found
|= DL_OPT_REGION_LENGTH
;
1441 } else if (dl_argv_match(dl
, "file") &&
1442 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1444 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1447 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1448 } else if (dl_argv_match(dl
, "component") &&
1449 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1451 err
= dl_argv_str(dl
, &opts
->flash_component
);
1454 o_found
|= DL_OPT_FLASH_COMPONENT
;
1455 } else if (dl_argv_match(dl
, "reporter") &&
1456 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1458 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1461 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1462 } else if (dl_argv_match(dl
, "grace_period") &&
1463 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1465 err
= dl_argv_uint64_t(dl
,
1466 &opts
->reporter_graceful_period
);
1469 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1470 } else if (dl_argv_match(dl
, "auto_recover") &&
1471 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1473 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1476 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1477 } else if (dl_argv_match(dl
, "trap") &&
1478 (o_all
& DL_OPT_TRAP_NAME
)) {
1480 err
= dl_argv_str(dl
, &opts
->trap_name
);
1483 o_found
|= DL_OPT_TRAP_NAME
;
1484 } else if (dl_argv_match(dl
, "group") &&
1485 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1487 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1490 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1491 } else if (dl_argv_match(dl
, "action") &&
1492 (o_all
& DL_OPT_TRAP_ACTION
)) {
1493 const char *actionstr
;
1496 err
= dl_argv_str(dl
, &actionstr
);
1499 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1502 o_found
|= DL_OPT_TRAP_ACTION
;
1503 } else if (dl_argv_match(dl
, "netns") &&
1504 (o_all
& DL_OPT_NETNS
)) {
1505 const char *netns_str
;
1508 err
= dl_argv_str(dl
, &netns_str
);
1511 opts
->netns
= netns_get_fd(netns_str
);
1512 if ((int)opts
->netns
< 0) {
1514 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1517 opts
->netns_is_pid
= true;
1519 o_found
|= DL_OPT_NETNS
;
1520 } else if (dl_argv_match(dl
, "policer") &&
1521 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1523 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1526 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1527 } else if (dl_argv_match(dl
, "nopolicer") &&
1528 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1530 opts
->trap_policer_id
= 0;
1531 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1532 } else if (dl_argv_match(dl
, "rate") &&
1533 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1535 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1538 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1539 } else if (dl_argv_match(dl
, "burst") &&
1540 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1542 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1545 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1547 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1552 opts
->present
= o_found
;
1554 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1556 opts
->present
|= DL_OPT_SB
;
1559 return dl_args_finding_required_validate(o_required
, o_found
);
1562 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1564 struct dl_opts
*opts
= &dl
->opts
;
1566 if (opts
->present
& DL_OPT_HANDLE
) {
1567 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1568 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1569 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1570 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1571 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1572 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1574 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1575 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1576 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1577 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1580 if (opts
->present
& DL_OPT_PORT_TYPE
)
1581 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1583 if (opts
->present
& DL_OPT_PORT_COUNT
)
1584 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1586 if (opts
->present
& DL_OPT_SB
)
1587 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1589 if (opts
->present
& DL_OPT_SB_POOL
)
1590 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1591 opts
->sb_pool_index
);
1592 if (opts
->present
& DL_OPT_SB_SIZE
)
1593 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1594 opts
->sb_pool_size
);
1595 if (opts
->present
& DL_OPT_SB_TYPE
)
1596 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1597 opts
->sb_pool_type
);
1598 if (opts
->present
& DL_OPT_SB_THTYPE
)
1599 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1600 opts
->sb_pool_thtype
);
1601 if (opts
->present
& DL_OPT_SB_TH
)
1602 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1603 opts
->sb_threshold
);
1604 if (opts
->present
& DL_OPT_SB_TC
)
1605 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1607 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1608 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1609 opts
->eswitch_mode
);
1610 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1611 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1612 opts
->eswitch_inline_mode
);
1613 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1614 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1615 opts
->dpipe_table_name
);
1616 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1617 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1618 opts
->dpipe_counters_enabled
);
1619 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1620 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1621 opts
->eswitch_encap_mode
);
1622 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1623 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1625 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1626 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1627 opts
->resource_size
);
1628 if (opts
->present
& DL_OPT_PARAM_NAME
)
1629 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1631 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1632 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1634 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1635 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1636 opts
->region_snapshot_id
);
1637 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1638 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1639 opts
->region_address
);
1640 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1641 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1642 opts
->region_length
);
1643 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1644 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1645 opts
->flash_file_name
);
1646 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1647 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1648 opts
->flash_component
);
1649 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1650 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1651 opts
->reporter_name
);
1652 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1653 mnl_attr_put_u64(nlh
,
1654 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1655 opts
->reporter_graceful_period
);
1656 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1657 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1658 opts
->reporter_auto_recover
);
1659 if (opts
->present
& DL_OPT_TRAP_NAME
)
1660 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1662 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1663 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1664 opts
->trap_group_name
);
1665 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1666 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1668 if (opts
->present
& DL_OPT_NETNS
)
1669 mnl_attr_put_u32(nlh
,
1670 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1671 DEVLINK_ATTR_NETNS_FD
,
1673 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1674 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1675 opts
->trap_policer_id
);
1676 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1677 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1678 opts
->trap_policer_rate
);
1679 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1680 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1681 opts
->trap_policer_burst
);
1684 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1685 uint64_t o_required
, uint64_t o_optional
)
1689 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1692 dl_opts_put(nlh
, dl
);
1696 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1698 struct dl_opts
*opts
= &dl
->opts
;
1699 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1700 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1701 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1702 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1704 if (opts
->present
& DL_OPT_HANDLE
&&
1705 attr_bus_name
&& attr_dev_name
) {
1706 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1707 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1709 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1710 strcmp(dev_name
, opts
->dev_name
) != 0)
1713 if (opts
->present
& DL_OPT_HANDLEP
&&
1714 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1715 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1716 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1717 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1719 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1720 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1721 port_index
!= opts
->port_index
)
1724 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1725 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1727 if (sb_index
!= opts
->sb_index
)
1733 static void cmd_dev_help(void)
1735 pr_err("Usage: devlink dev show [ DEV ]\n");
1736 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1737 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1738 pr_err(" [ encap-mode { none | basic } ]\n");
1739 pr_err(" devlink dev eswitch show DEV\n");
1740 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1741 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1742 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1743 pr_err(" devlink dev info [ DEV ]\n");
1744 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1747 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1748 const char *dev_name
)
1750 if (!dl
->arr_last
.present
)
1752 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1753 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1756 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1757 const char *dev_name
)
1759 dl
->arr_last
.present
= true;
1760 free(dl
->arr_last
.dev_name
);
1761 free(dl
->arr_last
.bus_name
);
1762 dl
->arr_last
.bus_name
= strdup(bus_name
);
1763 dl
->arr_last
.dev_name
= strdup(dev_name
);
1766 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1767 const char *dev_name
)
1769 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1772 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1773 const char *dev_name
)
1775 return dl
->arr_last
.present
&&
1776 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1779 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1780 bool content
, bool array
)
1782 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1783 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1786 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1788 if (dl
->json_output
) {
1790 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1791 close_json_array(PRINT_JSON
, NULL
);
1792 if (should_arr_last_handle_start(dl
, bus_name
,
1794 open_json_array(PRINT_JSON
, buf
);
1795 open_json_object(NULL
);
1796 arr_last_handle_set(dl
, bus_name
, dev_name
);
1798 open_json_object(NULL
);
1801 open_json_object(buf
);
1805 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1806 __pr_out_indent_dec();
1807 if (should_arr_last_handle_start(dl
, bus_name
,
1809 pr_out("%s%s", buf
, content
? ":" : "");
1811 __pr_out_indent_inc();
1812 arr_last_handle_set(dl
, bus_name
, dev_name
);
1815 pr_out("%s%s", buf
, content
? ":" : "");
1820 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1822 __pr_out_handle_start(dl
, tb
, true, true);
1825 static void pr_out_handle_end(struct dl
*dl
)
1827 if (dl
->json_output
)
1828 close_json_object();
1833 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1835 __pr_out_handle_start(dl
, tb
, false, false);
1836 pr_out_handle_end(dl
);
1839 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1840 const char *dev_name
, uint32_t port_index
)
1842 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1843 dl
->arr_last
.port_index
== port_index
;
1846 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1847 const char *dev_name
, uint32_t port_index
)
1849 arr_last_handle_set(dl
, bus_name
, dev_name
);
1850 dl
->arr_last
.port_index
= port_index
;
1853 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1854 const char *bus_name
,
1855 const char *dev_name
,
1856 uint32_t port_index
)
1858 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1861 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1862 const char *bus_name
,
1863 const char *dev_name
,
1864 uint32_t port_index
)
1866 return dl
->arr_last
.present
&&
1867 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1870 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1871 const char *dev_name
,
1872 uint32_t port_index
, bool try_nice
,
1875 static char buf
[64];
1876 char *ifname
= NULL
;
1878 if (dl
->no_nice_names
|| !try_nice
||
1879 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1880 port_index
, &ifname
) != 0)
1881 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1883 sprintf(buf
, "%s", ifname
);
1885 if (dl
->json_output
) {
1887 if (should_arr_last_port_handle_end(dl
, bus_name
,
1890 close_json_array(PRINT_JSON
, NULL
);
1891 if (should_arr_last_port_handle_start(dl
, bus_name
,
1894 open_json_array(PRINT_JSON
, buf
);
1895 open_json_object(NULL
);
1896 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1899 open_json_object(NULL
);
1902 open_json_object(buf
);
1909 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1911 const char *bus_name
;
1912 const char *dev_name
;
1913 uint32_t port_index
;
1915 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1916 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1917 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1918 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1921 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1923 const char *bus_name
;
1924 const char *dev_name
;
1925 uint32_t port_index
;
1927 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1928 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1929 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1930 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1933 static void pr_out_port_handle_end(struct dl
*dl
)
1935 if (dl
->json_output
)
1936 close_json_object();
1941 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1943 __pr_out_indent_newline(dl
);
1944 if (val
== (uint64_t) -1)
1945 return print_string_name_value(name
, "unlimited");
1947 if (dl
->json_output
)
1948 print_u64(PRINT_JSON
, name
, NULL
, val
);
1950 pr_out("%s %"PRIu64
, name
, val
);
1953 static bool is_binary_eol(int i
)
1958 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1963 if (dl
->json_output
)
1964 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1966 pr_out("%02x ", data
[i
]);
1968 if (!dl
->json_output
&& is_binary_eol(i
))
1971 if (!dl
->json_output
&& !is_binary_eol(i
))
1975 static void pr_out_name(struct dl
*dl
, const char *name
)
1977 __pr_out_indent_newline(dl
);
1978 if (dl
->json_output
)
1979 print_string(PRINT_JSON
, name
, NULL
, NULL
);
1981 pr_out("%s:", name
);
1984 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1986 if (dl
->json_output
) {
1987 print_uint(PRINT_JSON
, "address", NULL
, addr
);
1988 open_json_array(PRINT_JSON
, "data");
1992 static void pr_out_region_chunk_end(struct dl
*dl
)
1994 if (dl
->json_output
)
1995 close_json_array(PRINT_JSON
, NULL
);
1998 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
2001 static uint64_t align_val
;
2004 pr_out_region_chunk_start(dl
, addr
);
2006 if (!dl
->json_output
)
2007 if (!(align_val
% 16))
2008 pr_out("%s%016"PRIx64
" ",
2009 align_val
? "\n" : "",
2014 if (dl
->json_output
)
2015 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
2017 pr_out("%02x ", data
[i
]);
2022 pr_out_region_chunk_end(dl
);
2025 static void pr_out_section_start(struct dl
*dl
, const char *name
)
2027 if (dl
->json_output
) {
2028 open_json_object(NULL
);
2029 open_json_object(name
);
2033 static void pr_out_section_end(struct dl
*dl
)
2035 if (dl
->json_output
) {
2036 if (dl
->arr_last
.present
)
2037 close_json_array(PRINT_JSON
, NULL
);
2038 close_json_object();
2039 close_json_object();
2043 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2045 if (dl
->json_output
) {
2046 open_json_array(PRINT_JSON
, name
);
2048 __pr_out_indent_inc();
2050 pr_out("%s:", name
);
2051 __pr_out_indent_inc();
2056 static void pr_out_array_end(struct dl
*dl
)
2058 if (dl
->json_output
) {
2059 close_json_array(PRINT_JSON
, NULL
);
2061 __pr_out_indent_dec();
2062 __pr_out_indent_dec();
2066 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2068 if (dl
->json_output
) {
2069 open_json_object(name
);
2071 __pr_out_indent_inc();
2073 pr_out("%s:", name
);
2074 __pr_out_indent_inc();
2079 static void pr_out_object_end(struct dl
*dl
)
2081 if (dl
->json_output
) {
2082 close_json_object();
2084 __pr_out_indent_dec();
2085 __pr_out_indent_dec();
2089 static void pr_out_entry_start(struct dl
*dl
)
2091 if (dl
->json_output
)
2092 open_json_object(NULL
);
2095 static void pr_out_entry_end(struct dl
*dl
)
2097 if (dl
->json_output
)
2098 close_json_object();
2103 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2105 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2111 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2112 if (err
!= MNL_CB_OK
)
2115 pr_out_object_start(dl
, "stats");
2116 pr_out_object_start(dl
, "rx");
2117 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2118 pr_out_u64(dl
, "bytes",
2119 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2120 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2121 pr_out_u64(dl
, "packets",
2122 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2123 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2124 pr_out_u64(dl
, "dropped",
2125 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2126 pr_out_object_end(dl
);
2127 pr_out_object_end(dl
);
2130 static const char *param_cmode_name(uint8_t cmode
)
2133 case DEVLINK_PARAM_CMODE_RUNTIME
:
2134 return PARAM_CMODE_RUNTIME_STR
;
2135 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2136 return PARAM_CMODE_DRIVERINIT_STR
;
2137 case DEVLINK_PARAM_CMODE_PERMANENT
:
2138 return PARAM_CMODE_PERMANENT_STR
;
2139 default: return "<unknown type>";
2143 static const char *eswitch_mode_name(uint32_t mode
)
2146 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2147 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2148 default: return "<unknown mode>";
2152 static const char *eswitch_inline_mode_name(uint32_t mode
)
2155 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2156 return ESWITCH_INLINE_MODE_NONE
;
2157 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2158 return ESWITCH_INLINE_MODE_LINK
;
2159 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2160 return ESWITCH_INLINE_MODE_NETWORK
;
2161 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2162 return ESWITCH_INLINE_MODE_TRANSPORT
;
2164 return "<unknown mode>";
2168 static const char *eswitch_encap_mode_name(uint32_t mode
)
2171 case DEVLINK_ESWITCH_ENCAP_MODE_NONE
:
2172 return ESWITCH_ENCAP_MODE_NONE
;
2173 case DEVLINK_ESWITCH_ENCAP_MODE_BASIC
:
2174 return ESWITCH_ENCAP_MODE_BASIC
;
2176 return "<unknown mode>";
2180 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2182 __pr_out_handle_start(dl
, tb
, true, false);
2184 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2185 check_indent_newline(dl
);
2186 print_string(PRINT_ANY
, "mode", "mode %s",
2187 eswitch_mode_name(mnl_attr_get_u16(
2188 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2190 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2191 check_indent_newline(dl
);
2192 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2193 eswitch_inline_mode_name(mnl_attr_get_u8(
2194 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2196 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2197 check_indent_newline(dl
);
2198 print_string(PRINT_ANY
, "encap-mode", "encap-mode %s",
2199 eswitch_encap_mode_name(mnl_attr_get_u8(
2200 tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
])));
2203 pr_out_handle_end(dl
);
2206 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2208 struct dl
*dl
= data
;
2209 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2210 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2212 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2213 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2214 return MNL_CB_ERROR
;
2215 pr_out_eswitch(dl
, tb
);
2219 static int cmd_dev_eswitch_show(struct dl
*dl
)
2221 struct nlmsghdr
*nlh
;
2224 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2225 NLM_F_REQUEST
| NLM_F_ACK
);
2227 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2231 pr_out_section_start(dl
, "dev");
2232 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2233 pr_out_section_end(dl
);
2237 static int cmd_dev_eswitch_set(struct dl
*dl
)
2239 struct nlmsghdr
*nlh
;
2242 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2243 NLM_F_REQUEST
| NLM_F_ACK
);
2245 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2246 DL_OPT_ESWITCH_MODE
|
2247 DL_OPT_ESWITCH_INLINE_MODE
|
2248 DL_OPT_ESWITCH_ENCAP_MODE
);
2253 if (dl
->opts
.present
== 1) {
2254 pr_err("Need to set at least one option\n");
2258 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2261 static int cmd_dev_eswitch(struct dl
*dl
)
2263 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2266 } else if (dl_argv_match(dl
, "set")) {
2268 return cmd_dev_eswitch_set(dl
);
2269 } else if (dl_argv_match(dl
, "show")) {
2271 return cmd_dev_eswitch_show(dl
);
2273 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2277 struct param_val_conv
{
2283 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2284 uint32_t len
, const char *name
)
2288 for (i
= 0; i
< len
; i
++)
2289 if (!strcmp(param_val_conv
[i
].name
, name
))
2296 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2297 uint32_t len
, const char *name
, const char *vstr
,
2302 for (i
= 0; i
< len
; i
++)
2303 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2304 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2305 *vuint
= param_val_conv
[i
].vuint
;
2313 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2314 uint32_t len
, const char *name
, uint32_t vuint
,
2319 for (i
= 0; i
< len
; i
++)
2320 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2321 param_val_conv
[i
].vuint
== vuint
) {
2322 *vstr
= param_val_conv
[i
].vstr
;
2329 static const struct param_val_conv param_val_conv
[] = {
2331 .name
= "fw_load_policy",
2333 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2336 .name
= "fw_load_policy",
2338 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2341 .name
= "reset_dev_on_drv_probe",
2343 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2346 .name
= "fw_load_policy",
2348 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2351 .name
= "reset_dev_on_drv_probe",
2353 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2356 .name
= "reset_dev_on_drv_probe",
2358 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2361 .name
= "reset_dev_on_drv_probe",
2363 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2367 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2369 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2370 int nla_type
, struct nlattr
*nl
)
2372 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2373 struct nlattr
*val_attr
;
2378 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2379 if (err
!= MNL_CB_OK
)
2382 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2383 (nla_type
!= MNL_TYPE_FLAG
&&
2384 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2387 check_indent_newline(dl
);
2388 print_string(PRINT_ANY
, "cmode", "cmode %s",
2389 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2391 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2393 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2399 err
= param_val_conv_str_get(param_val_conv
,
2402 mnl_attr_get_u8(val_attr
),
2406 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2408 print_uint(PRINT_ANY
, "value", " value %u",
2409 mnl_attr_get_u8(val_attr
));
2414 err
= param_val_conv_str_get(param_val_conv
,
2417 mnl_attr_get_u16(val_attr
),
2421 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2423 print_uint(PRINT_ANY
, "value", " value %u",
2424 mnl_attr_get_u16(val_attr
));
2429 err
= param_val_conv_str_get(param_val_conv
,
2432 mnl_attr_get_u32(val_attr
),
2436 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2438 print_uint(PRINT_ANY
, "value", " value %u",
2439 mnl_attr_get_u32(val_attr
));
2442 case MNL_TYPE_STRING
:
2443 print_string(PRINT_ANY
, "value", " value %s",
2444 mnl_attr_get_str(val_attr
));
2447 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2452 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2454 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2455 struct nlattr
*param_value_attr
;
2456 const char *nla_name
;
2460 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2461 if (err
!= MNL_CB_OK
)
2463 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2464 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2465 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2469 pr_out_handle_start_arr(dl
, tb
);
2471 __pr_out_handle_start(dl
, tb
, true, false);
2473 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2475 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2476 check_indent_newline(dl
);
2477 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2478 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2479 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2481 print_string(PRINT_ANY
, "type", "type %s", "generic");
2483 pr_out_array_start(dl
, "values");
2484 mnl_attr_for_each_nested(param_value_attr
,
2485 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2486 pr_out_entry_start(dl
);
2487 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2488 pr_out_entry_end(dl
);
2490 pr_out_array_end(dl
);
2491 pr_out_handle_end(dl
);
2494 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2496 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2497 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2498 struct dl
*dl
= data
;
2500 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2501 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2502 !tb
[DEVLINK_ATTR_PARAM
])
2503 return MNL_CB_ERROR
;
2504 pr_out_param(dl
, tb
, true);
2520 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2522 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2523 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2524 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2525 struct nlattr
*param_value_attr
;
2526 enum devlink_param_cmode cmode
;
2527 struct param_ctx
*ctx
= data
;
2528 struct dl
*dl
= ctx
->dl
;
2532 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2533 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2534 !tb
[DEVLINK_ATTR_PARAM
])
2535 return MNL_CB_ERROR
;
2537 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2538 if (err
!= MNL_CB_OK
)
2539 return MNL_CB_ERROR
;
2541 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2542 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2543 return MNL_CB_ERROR
;
2545 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2546 mnl_attr_for_each_nested(param_value_attr
,
2547 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2548 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2549 struct nlattr
*val_attr
;
2551 err
= mnl_attr_parse_nested(param_value_attr
,
2552 attr_cb
, nla_value
);
2553 if (err
!= MNL_CB_OK
)
2554 return MNL_CB_ERROR
;
2556 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2557 (nla_type
!= MNL_TYPE_FLAG
&&
2558 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2559 return MNL_CB_ERROR
;
2561 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2562 if (cmode
== dl
->opts
.cmode
) {
2563 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2566 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2569 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2572 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2574 case MNL_TYPE_STRING
:
2575 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2578 ctx
->value
.vbool
= val_attr
? true : false;
2584 ctx
->nla_type
= nla_type
;
2588 static int cmd_dev_param_set(struct dl
*dl
)
2590 struct param_ctx ctx
= {};
2591 struct nlmsghdr
*nlh
;
2599 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2601 DL_OPT_PARAM_VALUE
|
2602 DL_OPT_PARAM_CMODE
, 0);
2606 /* Get value type */
2607 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2608 NLM_F_REQUEST
| NLM_F_ACK
);
2609 dl_opts_put(nlh
, dl
);
2612 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2616 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2617 NLM_F_REQUEST
| NLM_F_ACK
);
2618 dl_opts_put(nlh
, dl
);
2620 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2621 dl
->opts
.param_name
);
2623 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2624 switch (ctx
.nla_type
) {
2627 err
= param_val_conv_uint_get(param_val_conv
,
2629 dl
->opts
.param_name
,
2630 dl
->opts
.param_value
,
2634 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2637 goto err_param_value_parse
;
2638 if (val_u8
== ctx
.value
.vu8
)
2640 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2644 err
= param_val_conv_uint_get(param_val_conv
,
2646 dl
->opts
.param_name
,
2647 dl
->opts
.param_value
,
2651 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2654 goto err_param_value_parse
;
2655 if (val_u16
== ctx
.value
.vu16
)
2657 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2661 err
= param_val_conv_uint_get(param_val_conv
,
2663 dl
->opts
.param_name
,
2664 dl
->opts
.param_value
,
2667 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2669 goto err_param_value_parse
;
2670 if (val_u32
== ctx
.value
.vu32
)
2672 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2675 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2677 goto err_param_value_parse
;
2678 if (val_bool
== ctx
.value
.vbool
)
2681 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2684 case MNL_TYPE_STRING
:
2685 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2686 dl
->opts
.param_value
);
2687 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2691 printf("Value type not supported\n");
2694 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2696 err_param_value_parse
:
2697 pr_err("Value \"%s\" is not a number or not within range\n",
2698 dl
->opts
.param_value
);
2702 static int cmd_dev_param_show(struct dl
*dl
)
2704 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2705 struct nlmsghdr
*nlh
;
2708 if (dl_argc(dl
) == 0)
2709 flags
|= NLM_F_DUMP
;
2711 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2713 if (dl_argc(dl
) > 0) {
2714 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2715 DL_OPT_PARAM_NAME
, 0);
2720 pr_out_section_start(dl
, "param");
2721 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2722 pr_out_section_end(dl
);
2726 static int cmd_dev_param(struct dl
*dl
)
2728 if (dl_argv_match(dl
, "help")) {
2731 } else if (dl_argv_match(dl
, "show") ||
2732 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2734 return cmd_dev_param_show(dl
);
2735 } else if (dl_argv_match(dl
, "set")) {
2737 return cmd_dev_param_set(dl
);
2739 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2742 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2744 struct dl
*dl
= data
;
2745 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2746 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2747 uint8_t reload_failed
= 0;
2749 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2750 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2751 return MNL_CB_ERROR
;
2753 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2754 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2756 if (reload_failed
) {
2757 __pr_out_handle_start(dl
, tb
, true, false);
2758 check_indent_newline(dl
);
2759 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2760 pr_out_handle_end(dl
);
2762 pr_out_handle(dl
, tb
);
2768 static int cmd_dev_show(struct dl
*dl
)
2770 struct nlmsghdr
*nlh
;
2771 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2774 if (dl_argc(dl
) == 0)
2775 flags
|= NLM_F_DUMP
;
2777 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2779 if (dl_argc(dl
) > 0) {
2780 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2785 pr_out_section_start(dl
, "dev");
2786 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2787 pr_out_section_end(dl
);
2791 static int cmd_dev_reload(struct dl
*dl
)
2793 struct nlmsghdr
*nlh
;
2796 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2801 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2802 NLM_F_REQUEST
| NLM_F_ACK
);
2804 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2808 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2811 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2812 const char *name
, int type
)
2814 struct nlattr
*version
;
2816 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2817 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2818 const char *ver_value
;
2819 const char *ver_name
;
2822 if (mnl_attr_get_type(version
) != type
)
2825 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2826 if (err
!= MNL_CB_OK
)
2829 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2830 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2834 pr_out_object_start(dl
, name
);
2838 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2839 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2841 check_indent_newline(dl
);
2842 print_string_name_value(ver_name
, ver_value
);
2843 if (!dl
->json_output
)
2848 pr_out_object_end(dl
);
2851 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2852 struct nlattr
**tb
, bool has_versions
)
2854 __pr_out_handle_start(dl
, tb
, true, false);
2856 __pr_out_indent_inc();
2857 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2858 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2860 if (!dl
->json_output
)
2862 check_indent_newline(dl
);
2863 print_string(PRINT_ANY
, "driver", "driver %s",
2864 mnl_attr_get_str(nla_drv
));
2867 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2868 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2870 if (!dl
->json_output
)
2872 check_indent_newline(dl
);
2873 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2874 mnl_attr_get_str(nla_sn
));
2876 __pr_out_indent_dec();
2879 pr_out_object_start(dl
, "versions");
2881 pr_out_versions_single(dl
, nlh
, "fixed",
2882 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2883 pr_out_versions_single(dl
, nlh
, "running",
2884 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2885 pr_out_versions_single(dl
, nlh
, "stored",
2886 DEVLINK_ATTR_INFO_VERSION_STORED
);
2888 pr_out_object_end(dl
);
2891 pr_out_handle_end(dl
);
2894 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2896 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2897 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2898 bool has_versions
, has_info
;
2899 struct dl
*dl
= data
;
2901 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2903 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2904 return MNL_CB_ERROR
;
2906 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2907 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2908 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2909 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2910 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2914 pr_out_info(dl
, nlh
, tb
, has_versions
);
2919 static int cmd_dev_info(struct dl
*dl
)
2921 struct nlmsghdr
*nlh
;
2922 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2925 if (dl_argv_match(dl
, "help")) {
2930 if (dl_argc(dl
) == 0)
2931 flags
|= NLM_F_DUMP
;
2933 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2935 if (dl_argc(dl
) > 0) {
2936 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2941 pr_out_section_start(dl
, "info");
2942 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2943 pr_out_section_end(dl
);
2947 struct cmd_dev_flash_status_ctx
{
2950 char *last_component
;
2951 uint8_t not_first
:1,
2957 static int nullstrcmp(const char *str1
, const char *str2
)
2960 return strcmp(str1
, str2
);
2963 return str1
? 1 : -1;
2966 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2968 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2969 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2970 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2971 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2972 const char *component
= NULL
;
2973 uint64_t done
= 0, total
= 0;
2974 const char *msg
= NULL
;
2975 const char *bus_name
;
2976 const char *dev_name
;
2978 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2979 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2982 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2983 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2984 return MNL_CB_ERROR
;
2985 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2986 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2987 if (strcmp(bus_name
, opts
->bus_name
) ||
2988 strcmp(dev_name
, opts
->dev_name
))
2989 return MNL_CB_ERROR
;
2991 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2993 free(ctx
->last_msg
);
2994 free(ctx
->last_component
);
2995 ctx
->received_end
= 1;
2999 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
3000 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
3001 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
3002 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
3003 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
3004 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
3005 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
3006 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
3008 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
3009 !nullstrcmp(component
, ctx
->last_component
) &&
3010 ctx
->last_pc
&& ctx
->not_first
) {
3011 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3016 pr_out("[%s] ", component
);
3017 free(ctx
->last_component
);
3018 ctx
->last_component
= strdup(component
);
3022 free(ctx
->last_msg
);
3023 ctx
->last_msg
= strdup(msg
);
3027 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3038 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3039 struct mnlg_socket
*nlg_ntf
,
3042 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3049 for (i
= 0; i
< 3; i
++)
3051 FD_SET(pipe_r
, &fds
[0]);
3053 FD_SET(nlfd
, &fds
[0]);
3057 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3060 pr_err("select() failed\n");
3063 if (FD_ISSET(nlfd
, &fds
[0])) {
3064 err
= _mnlg_socket_recv_run(nlg_ntf
,
3065 cmd_dev_flash_status_cb
, ctx
);
3069 if (FD_ISSET(pipe_r
, &fds
[0])) {
3070 err
= read(pipe_r
, &err2
, sizeof(err2
));
3072 pr_err("Failed to read pipe\n");
3077 ctx
->flash_done
= 1;
3083 static int cmd_dev_flash(struct dl
*dl
)
3085 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3086 struct mnlg_socket
*nlg_ntf
;
3087 struct nlmsghdr
*nlh
;
3093 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3098 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3099 NLM_F_REQUEST
| NLM_F_ACK
);
3101 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3102 DL_OPT_FLASH_COMPONENT
);
3106 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3110 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3114 err
= pipe(pipe_fds
);
3117 pipe_r
= pipe_fds
[0];
3118 pipe_w
= pipe_fds
[1];
3126 /* In child, just execute the flash and pass returned
3127 * value through pipe once it is done.
3132 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3133 cc
= write(pipe_w
, &err
, sizeof(err
));
3135 exit(cc
!= sizeof(err
));
3140 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3143 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3145 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3148 mnlg_socket_close(nlg_ntf
);
3152 static int cmd_dev(struct dl
*dl
)
3154 if (dl_argv_match(dl
, "help")) {
3157 } else if (dl_argv_match(dl
, "show") ||
3158 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3160 return cmd_dev_show(dl
);
3161 } else if (dl_argv_match(dl
, "eswitch")) {
3163 return cmd_dev_eswitch(dl
);
3164 } else if (dl_argv_match(dl
, "reload")) {
3166 return cmd_dev_reload(dl
);
3167 } else if (dl_argv_match(dl
, "param")) {
3169 return cmd_dev_param(dl
);
3170 } else if (dl_argv_match(dl
, "info")) {
3172 return cmd_dev_info(dl
);
3173 } else if (dl_argv_match(dl
, "flash")) {
3175 return cmd_dev_flash(dl
);
3177 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3181 static void cmd_port_help(void)
3183 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3184 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3185 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3186 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3189 static const char *port_type_name(uint32_t type
)
3192 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3193 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3194 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3195 case DEVLINK_PORT_TYPE_IB
: return "ib";
3196 default: return "<unknown type>";
3200 static const char *port_flavour_name(uint16_t flavour
)
3203 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3205 case DEVLINK_PORT_FLAVOUR_CPU
:
3207 case DEVLINK_PORT_FLAVOUR_DSA
:
3209 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3211 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3213 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3216 return "<unknown flavour>";
3220 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3224 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3225 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3226 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3228 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3229 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3230 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3234 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3236 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3237 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3239 pr_out_port_handle_start(dl
, tb
, false);
3240 check_indent_newline(dl
);
3242 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3244 print_string(PRINT_ANY
, "type", "type %s",
3245 port_type_name(port_type
));
3247 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3249 if (port_type
!= des_port_type
)
3250 print_string(PRINT_ANY
, "des_type", " des_type %s",
3251 port_type_name(des_port_type
));
3254 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3255 print_string(PRINT_ANY
, "netdev", " netdev %s",
3256 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3258 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3259 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3260 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3262 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3263 uint16_t port_flavour
=
3264 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3266 print_string(PRINT_ANY
, "flavour", " flavour %s",
3267 port_flavour_name(port_flavour
));
3269 switch (port_flavour
) {
3270 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3271 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3272 pr_out_port_pfvf_num(dl
, tb
);
3278 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3279 uint32_t port_number
;
3281 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3282 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3284 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3285 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3286 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3287 pr_out_port_handle_end(dl
);
3290 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3292 struct dl
*dl
= data
;
3293 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3294 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3296 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3297 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3298 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3299 return MNL_CB_ERROR
;
3300 pr_out_port(dl
, tb
);
3304 static int cmd_port_show(struct dl
*dl
)
3306 struct nlmsghdr
*nlh
;
3307 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3310 if (dl_argc(dl
) == 0)
3311 flags
|= NLM_F_DUMP
;
3313 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3315 if (dl_argc(dl
) > 0) {
3316 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3321 pr_out_section_start(dl
, "port");
3322 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3323 pr_out_section_end(dl
);
3327 static int cmd_port_set(struct dl
*dl
)
3329 struct nlmsghdr
*nlh
;
3332 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3333 NLM_F_REQUEST
| NLM_F_ACK
);
3335 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3339 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3342 static int cmd_port_split(struct dl
*dl
)
3344 struct nlmsghdr
*nlh
;
3347 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3348 NLM_F_REQUEST
| NLM_F_ACK
);
3350 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3354 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3357 static int cmd_port_unsplit(struct dl
*dl
)
3359 struct nlmsghdr
*nlh
;
3362 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3363 NLM_F_REQUEST
| NLM_F_ACK
);
3365 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3369 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3372 static int cmd_port(struct dl
*dl
)
3374 if (dl_argv_match(dl
, "help")) {
3377 } else if (dl_argv_match(dl
, "show") ||
3378 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3380 return cmd_port_show(dl
);
3381 } else if (dl_argv_match(dl
, "set")) {
3383 return cmd_port_set(dl
);
3384 } else if (dl_argv_match(dl
, "split")) {
3386 return cmd_port_split(dl
);
3387 } else if (dl_argv_match(dl
, "unsplit")) {
3389 return cmd_port_unsplit(dl
);
3391 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3395 static void cmd_sb_help(void)
3397 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3398 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3399 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3400 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3401 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3402 pr_err(" pool POOL_INDEX ]\n");
3403 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3404 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3405 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3406 pr_err(" type { ingress | egress } ]\n");
3407 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3408 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3409 pr_err(" th THRESHOLD\n");
3410 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3411 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3412 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3415 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3417 pr_out_handle_start_arr(dl
, tb
);
3418 check_indent_newline(dl
);
3419 print_uint(PRINT_ANY
, "sb", "sb %u",
3420 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3421 print_uint(PRINT_ANY
, "size", " size %u",
3422 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3423 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3424 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3425 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3426 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3427 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3428 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3429 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3430 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3431 pr_out_handle_end(dl
);
3434 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3436 struct dl
*dl
= data
;
3437 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3438 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3440 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3441 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3442 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3443 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3444 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3445 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3446 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3447 return MNL_CB_ERROR
;
3452 static int cmd_sb_show(struct dl
*dl
)
3454 struct nlmsghdr
*nlh
;
3455 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3458 if (dl_argc(dl
) == 0)
3459 flags
|= NLM_F_DUMP
;
3461 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3463 if (dl_argc(dl
) > 0) {
3464 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3469 pr_out_section_start(dl
, "sb");
3470 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3471 pr_out_section_end(dl
);
3475 static const char *pool_type_name(uint8_t type
)
3478 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3479 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3480 default: return "<unknown type>";
3484 static const char *threshold_type_name(uint8_t type
)
3487 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3488 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3489 default: return "<unknown type>";
3493 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3495 pr_out_handle_start_arr(dl
, tb
);
3496 check_indent_newline(dl
);
3497 print_uint(PRINT_ANY
, "sb", "sb %u",
3498 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3499 print_uint(PRINT_ANY
, "pool", " pool %u",
3500 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3501 print_string(PRINT_ANY
, "type", " type %s",
3502 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3503 print_uint(PRINT_ANY
, "size", " size %u",
3504 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3505 print_string(PRINT_ANY
, "thtype", " thtype %s",
3506 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3507 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3508 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3509 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3510 pr_out_handle_end(dl
);
3513 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3515 struct dl
*dl
= data
;
3516 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3517 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3519 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3520 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3521 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3522 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3523 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3524 return MNL_CB_ERROR
;
3525 pr_out_sb_pool(dl
, tb
);
3529 static int cmd_sb_pool_show(struct dl
*dl
)
3531 struct nlmsghdr
*nlh
;
3532 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3535 if (dl_argc(dl
) == 0)
3536 flags
|= NLM_F_DUMP
;
3538 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3540 if (dl_argc(dl
) > 0) {
3541 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3547 pr_out_section_start(dl
, "pool");
3548 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3549 pr_out_section_end(dl
);
3553 static int cmd_sb_pool_set(struct dl
*dl
)
3555 struct nlmsghdr
*nlh
;
3558 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3559 NLM_F_REQUEST
| NLM_F_ACK
);
3561 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3562 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3566 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3569 static int cmd_sb_pool(struct dl
*dl
)
3571 if (dl_argv_match(dl
, "help")) {
3574 } else if (dl_argv_match(dl
, "show") ||
3575 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3577 return cmd_sb_pool_show(dl
);
3578 } else if (dl_argv_match(dl
, "set")) {
3580 return cmd_sb_pool_set(dl
);
3582 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3586 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3588 pr_out_port_handle_start_arr(dl
, tb
, true);
3589 check_indent_newline(dl
);
3590 print_uint(PRINT_ANY
, "sb", "sb %u",
3591 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3592 print_uint(PRINT_ANY
, "pool", " pool %u",
3593 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3594 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3595 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3596 pr_out_port_handle_end(dl
);
3599 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3601 struct dl
*dl
= data
;
3602 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3603 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3605 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3606 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3607 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3608 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3609 return MNL_CB_ERROR
;
3610 pr_out_sb_port_pool(dl
, tb
);
3614 static int cmd_sb_port_pool_show(struct dl
*dl
)
3616 struct nlmsghdr
*nlh
;
3617 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3620 if (dl_argc(dl
) == 0)
3621 flags
|= NLM_F_DUMP
;
3623 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3625 if (dl_argc(dl
) > 0) {
3626 err
= dl_argv_parse_put(nlh
, dl
,
3627 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3633 pr_out_section_start(dl
, "port_pool");
3634 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3635 pr_out_section_end(dl
);
3639 static int cmd_sb_port_pool_set(struct dl
*dl
)
3641 struct nlmsghdr
*nlh
;
3644 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3645 NLM_F_REQUEST
| NLM_F_ACK
);
3647 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3648 DL_OPT_SB_TH
, DL_OPT_SB
);
3652 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3655 static int cmd_sb_port_pool(struct dl
*dl
)
3657 if (dl_argv_match(dl
, "help")) {
3660 } else if (dl_argv_match(dl
, "show") ||
3661 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3663 return cmd_sb_port_pool_show(dl
);
3664 } else if (dl_argv_match(dl
, "set")) {
3666 return cmd_sb_port_pool_set(dl
);
3668 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3672 static int cmd_sb_port(struct dl
*dl
)
3674 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3677 } else if (dl_argv_match(dl
, "pool")) {
3679 return cmd_sb_port_pool(dl
);
3681 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3685 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3687 pr_out_port_handle_start_arr(dl
, tb
, true);
3688 check_indent_newline(dl
);
3689 print_uint(PRINT_ANY
, "sb", "sb %u",
3690 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3691 print_uint(PRINT_ANY
, "tc", " tc %u",
3692 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3693 print_string(PRINT_ANY
, "type", " type %s",
3694 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3695 print_uint(PRINT_ANY
, "pool", " pool %u",
3696 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3697 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3698 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3699 pr_out_port_handle_end(dl
);
3702 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3704 struct dl
*dl
= data
;
3705 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3706 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3708 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3709 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3710 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3711 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3712 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3713 return MNL_CB_ERROR
;
3714 pr_out_sb_tc_bind(dl
, tb
);
3718 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3720 struct nlmsghdr
*nlh
;
3721 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3724 if (dl_argc(dl
) == 0)
3725 flags
|= NLM_F_DUMP
;
3727 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3729 if (dl_argc(dl
) > 0) {
3730 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3731 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3736 pr_out_section_start(dl
, "tc_bind");
3737 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3738 pr_out_section_end(dl
);
3742 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3744 struct nlmsghdr
*nlh
;
3747 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3748 NLM_F_REQUEST
| NLM_F_ACK
);
3750 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3751 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3756 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3759 static int cmd_sb_tc_bind(struct dl
*dl
)
3761 if (dl_argv_match(dl
, "help")) {
3764 } else if (dl_argv_match(dl
, "show") ||
3765 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3767 return cmd_sb_tc_bind_show(dl
);
3768 } else if (dl_argv_match(dl
, "set")) {
3770 return cmd_sb_tc_bind_set(dl
);
3772 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3776 static int cmd_sb_tc(struct dl
*dl
)
3778 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3781 } else if (dl_argv_match(dl
, "bind")) {
3783 return cmd_sb_tc_bind(dl
);
3785 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3790 struct list_head list
;
3794 uint32_t bound_pool_index
;
3798 struct list_head list
;
3801 uint32_t port_index
;
3803 struct list_head pool_list
;
3804 struct list_head ing_tc_list
;
3805 struct list_head eg_tc_list
;
3811 struct list_head port_list
;
3814 static struct occ_item
*occ_item_alloc(void)
3816 return calloc(1, sizeof(struct occ_item
));
3819 static void occ_item_free(struct occ_item
*occ_item
)
3824 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3826 struct occ_port
*occ_port
;
3828 occ_port
= calloc(1, sizeof(*occ_port
));
3831 occ_port
->port_index
= port_index
;
3832 INIT_LIST_HEAD(&occ_port
->pool_list
);
3833 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3834 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3838 static void occ_port_free(struct occ_port
*occ_port
)
3840 struct occ_item
*occ_item
, *tmp
;
3842 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3843 occ_item_free(occ_item
);
3844 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3845 occ_item_free(occ_item
);
3846 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3847 occ_item_free(occ_item
);
3850 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3852 struct occ_show
*occ_show
;
3854 occ_show
= calloc(1, sizeof(*occ_show
));
3858 INIT_LIST_HEAD(&occ_show
->port_list
);
3862 static void occ_show_free(struct occ_show
*occ_show
)
3864 struct occ_port
*occ_port
, *tmp
;
3866 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3867 occ_port_free(occ_port
);
3870 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3873 struct occ_port
*occ_port
;
3874 uint32_t port_index
;
3876 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3878 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3879 if (occ_port
->port_index
== port_index
)
3882 occ_port
= occ_port_alloc(port_index
);
3885 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3889 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3892 struct occ_item
*occ_item
;
3895 pr_out_sp(7, " %s:", label
);
3896 list_for_each_entry(occ_item
, list
, list
) {
3897 if ((i
- 1) % 4 == 0 && i
!= 1)
3900 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3901 occ_item
->bound_pool_index
);
3903 pr_out_sp(7, "%2u:", occ_item
->index
);
3904 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3908 if ((i
- 1) % 4 != 0)
3912 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3913 struct list_head
*list
,
3916 struct occ_item
*occ_item
;
3919 open_json_object(label
);
3920 list_for_each_entry(occ_item
, list
, list
) {
3921 sprintf(buf
, "%u", occ_item
->index
);
3922 open_json_object(buf
);
3924 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3925 occ_item
->bound_pool_index
);
3926 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3927 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3928 close_json_object();
3930 close_json_object();
3933 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3935 if (dl
->json_output
) {
3936 pr_out_json_occ_show_item_list(dl
, "pool",
3937 &occ_port
->pool_list
, false);
3938 pr_out_json_occ_show_item_list(dl
, "itc",
3939 &occ_port
->ing_tc_list
, true);
3940 pr_out_json_occ_show_item_list(dl
, "etc",
3941 &occ_port
->eg_tc_list
, true);
3944 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3945 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3946 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3950 static void pr_out_occ_show(struct occ_show
*occ_show
)
3952 struct dl
*dl
= occ_show
->dl
;
3953 struct dl_opts
*opts
= &dl
->opts
;
3954 struct occ_port
*occ_port
;
3956 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3957 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3958 occ_port
->port_index
, true, false);
3959 pr_out_occ_show_port(dl
, occ_port
);
3960 pr_out_port_handle_end(dl
);
3964 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3967 struct occ_port
*occ_port
;
3968 struct occ_item
*occ_item
;
3970 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3973 occ_port
= occ_port_get(occ_show
, tb
);
3975 occ_show
->err
= -ENOMEM
;
3979 occ_item
= occ_item_alloc();
3981 occ_show
->err
= -ENOMEM
;
3984 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3985 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3986 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3987 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3990 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3992 struct occ_show
*occ_show
= data
;
3993 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3994 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3996 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3997 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3998 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3999 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4000 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4001 return MNL_CB_ERROR
;
4002 cmd_sb_occ_port_pool_process(occ_show
, tb
);
4006 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
4009 struct occ_port
*occ_port
;
4010 struct occ_item
*occ_item
;
4013 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4016 occ_port
= occ_port_get(occ_show
, tb
);
4018 occ_show
->err
= -ENOMEM
;
4022 occ_item
= occ_item_alloc();
4024 occ_show
->err
= -ENOMEM
;
4027 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4028 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4029 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4030 occ_item
->bound_pool_index
=
4031 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4032 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4033 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4034 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4035 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4036 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4038 occ_item_free(occ_item
);
4041 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4043 struct occ_show
*occ_show
= data
;
4044 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4045 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4047 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4048 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4049 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4050 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4051 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4052 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4053 return MNL_CB_ERROR
;
4054 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4058 static int cmd_sb_occ_show(struct dl
*dl
)
4060 struct nlmsghdr
*nlh
;
4061 struct occ_show
*occ_show
;
4062 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4065 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4069 occ_show
= occ_show_alloc(dl
);
4073 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4075 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4076 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4080 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4082 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4083 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4087 pr_out_section_start(dl
, "occupancy");
4088 pr_out_occ_show(occ_show
);
4089 pr_out_section_end(dl
);
4092 occ_show_free(occ_show
);
4096 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4098 struct nlmsghdr
*nlh
;
4101 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4102 NLM_F_REQUEST
| NLM_F_ACK
);
4104 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4108 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4111 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4113 struct nlmsghdr
*nlh
;
4116 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4117 NLM_F_REQUEST
| NLM_F_ACK
);
4119 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4123 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4126 static int cmd_sb_occ(struct dl
*dl
)
4128 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4131 } else if (dl_argv_match(dl
, "show") ||
4132 dl_argv_match(dl
, "list")) {
4134 return cmd_sb_occ_show(dl
);
4135 } else if (dl_argv_match(dl
, "snapshot")) {
4137 return cmd_sb_occ_snapshot(dl
);
4138 } else if (dl_argv_match(dl
, "clearmax")) {
4140 return cmd_sb_occ_clearmax(dl
);
4142 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4146 static int cmd_sb(struct dl
*dl
)
4148 if (dl_argv_match(dl
, "help")) {
4151 } else if (dl_argv_match(dl
, "show") ||
4152 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4154 return cmd_sb_show(dl
);
4155 } else if (dl_argv_match(dl
, "pool")) {
4157 return cmd_sb_pool(dl
);
4158 } else if (dl_argv_match(dl
, "port")) {
4160 return cmd_sb_port(dl
);
4161 } else if (dl_argv_match(dl
, "tc")) {
4163 return cmd_sb_tc(dl
);
4164 } else if (dl_argv_match(dl
, "occupancy")) {
4166 return cmd_sb_occ(dl
);
4168 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4172 static const char *cmd_name(uint8_t cmd
)
4175 case DEVLINK_CMD_UNSPEC
: return "unspec";
4176 case DEVLINK_CMD_GET
: return "get";
4177 case DEVLINK_CMD_SET
: return "set";
4178 case DEVLINK_CMD_NEW
: return "new";
4179 case DEVLINK_CMD_DEL
: return "del";
4180 case DEVLINK_CMD_PORT_GET
: return "get";
4181 case DEVLINK_CMD_PORT_SET
: return "set";
4182 case DEVLINK_CMD_PORT_NEW
: return "new";
4183 case DEVLINK_CMD_PORT_DEL
: return "del";
4184 case DEVLINK_CMD_PARAM_GET
: return "get";
4185 case DEVLINK_CMD_PARAM_SET
: return "set";
4186 case DEVLINK_CMD_PARAM_NEW
: return "new";
4187 case DEVLINK_CMD_PARAM_DEL
: return "del";
4188 case DEVLINK_CMD_REGION_GET
: return "get";
4189 case DEVLINK_CMD_REGION_SET
: return "set";
4190 case DEVLINK_CMD_REGION_NEW
: return "new";
4191 case DEVLINK_CMD_REGION_DEL
: return "del";
4192 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4193 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4194 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4195 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4196 case DEVLINK_CMD_TRAP_GET
: return "get";
4197 case DEVLINK_CMD_TRAP_SET
: return "set";
4198 case DEVLINK_CMD_TRAP_NEW
: return "new";
4199 case DEVLINK_CMD_TRAP_DEL
: return "del";
4200 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4201 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4202 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4203 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4204 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4205 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4206 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4207 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4208 default: return "<unknown cmd>";
4212 static const char *cmd_obj(uint8_t cmd
)
4215 case DEVLINK_CMD_UNSPEC
: return "unspec";
4216 case DEVLINK_CMD_GET
:
4217 case DEVLINK_CMD_SET
:
4218 case DEVLINK_CMD_NEW
:
4219 case DEVLINK_CMD_DEL
:
4221 case DEVLINK_CMD_PORT_GET
:
4222 case DEVLINK_CMD_PORT_SET
:
4223 case DEVLINK_CMD_PORT_NEW
:
4224 case DEVLINK_CMD_PORT_DEL
:
4226 case DEVLINK_CMD_PARAM_GET
:
4227 case DEVLINK_CMD_PARAM_SET
:
4228 case DEVLINK_CMD_PARAM_NEW
:
4229 case DEVLINK_CMD_PARAM_DEL
:
4231 case DEVLINK_CMD_REGION_GET
:
4232 case DEVLINK_CMD_REGION_SET
:
4233 case DEVLINK_CMD_REGION_NEW
:
4234 case DEVLINK_CMD_REGION_DEL
:
4236 case DEVLINK_CMD_FLASH_UPDATE
:
4237 case DEVLINK_CMD_FLASH_UPDATE_END
:
4238 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4240 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4242 case DEVLINK_CMD_TRAP_GET
:
4243 case DEVLINK_CMD_TRAP_SET
:
4244 case DEVLINK_CMD_TRAP_NEW
:
4245 case DEVLINK_CMD_TRAP_DEL
:
4247 case DEVLINK_CMD_TRAP_GROUP_GET
:
4248 case DEVLINK_CMD_TRAP_GROUP_SET
:
4249 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4250 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4251 return "trap-group";
4252 case DEVLINK_CMD_TRAP_POLICER_GET
:
4253 case DEVLINK_CMD_TRAP_POLICER_SET
:
4254 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4255 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4256 return "trap-policer";
4257 default: return "<unknown obj>";
4261 static void pr_out_mon_header(uint8_t cmd
)
4263 if (!is_json_context()) {
4264 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4266 open_json_object(NULL
);
4267 print_string(PRINT_JSON
, "command", NULL
, cmd_name(cmd
));
4268 open_json_object(cmd_obj(cmd
));
4272 static void pr_out_mon_footer(void)
4274 if (is_json_context()) {
4275 close_json_object();
4276 close_json_object();
4280 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4282 const char *obj
= cmd_obj(cmd
);
4283 unsigned int index
= 0;
4284 const char *cur_obj
;
4288 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4289 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4295 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4297 __pr_out_handle_start(dl
, tb
, true, false);
4299 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4300 check_indent_newline(dl
);
4301 print_string(PRINT_ANY
, "msg", "msg %s",
4302 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4304 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4305 check_indent_newline(dl
);
4306 print_string(PRINT_ANY
, "component", "component %s",
4307 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4310 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4311 pr_out_u64(dl
, "done",
4312 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4314 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4315 pr_out_u64(dl
, "total",
4316 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4318 pr_out_handle_end(dl
);
4321 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4322 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4323 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4324 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4325 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4327 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4329 struct dl
*dl
= data
;
4330 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4331 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4332 uint8_t cmd
= genl
->cmd
;
4334 if (!cmd_filter_check(dl
, cmd
))
4338 case DEVLINK_CMD_GET
: /* fall through */
4339 case DEVLINK_CMD_SET
: /* fall through */
4340 case DEVLINK_CMD_NEW
: /* fall through */
4341 case DEVLINK_CMD_DEL
:
4342 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4343 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4344 return MNL_CB_ERROR
;
4345 pr_out_mon_header(genl
->cmd
);
4346 pr_out_handle(dl
, tb
);
4347 pr_out_mon_footer();
4349 case DEVLINK_CMD_PORT_GET
: /* fall through */
4350 case DEVLINK_CMD_PORT_SET
: /* fall through */
4351 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4352 case DEVLINK_CMD_PORT_DEL
:
4353 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4354 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4355 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4356 return MNL_CB_ERROR
;
4357 pr_out_mon_header(genl
->cmd
);
4358 pr_out_port(dl
, tb
);
4359 pr_out_mon_footer();
4361 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4362 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4363 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4364 case DEVLINK_CMD_PARAM_DEL
:
4365 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4366 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4367 !tb
[DEVLINK_ATTR_PARAM
])
4368 return MNL_CB_ERROR
;
4369 pr_out_mon_header(genl
->cmd
);
4370 pr_out_param(dl
, tb
, false);
4371 pr_out_mon_footer();
4373 case DEVLINK_CMD_REGION_GET
: /* fall through */
4374 case DEVLINK_CMD_REGION_SET
: /* fall through */
4375 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4376 case DEVLINK_CMD_REGION_DEL
:
4377 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4378 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4379 !tb
[DEVLINK_ATTR_REGION_NAME
])
4380 return MNL_CB_ERROR
;
4381 pr_out_mon_header(genl
->cmd
);
4382 pr_out_region(dl
, tb
);
4383 pr_out_mon_footer();
4385 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4386 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4387 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4388 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4389 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4390 return MNL_CB_ERROR
;
4391 pr_out_mon_header(genl
->cmd
);
4392 pr_out_flash_update(dl
, tb
);
4393 pr_out_mon_footer();
4395 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
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_HEALTH_REPORTER
])
4399 return MNL_CB_ERROR
;
4400 pr_out_mon_header(genl
->cmd
);
4401 pr_out_health(dl
, tb
);
4402 pr_out_mon_footer();
4404 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4405 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4406 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4407 case DEVLINK_CMD_TRAP_DEL
:
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_NAME
] ||
4411 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4412 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4413 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4414 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4415 !tb
[DEVLINK_ATTR_STATS
])
4416 return MNL_CB_ERROR
;
4417 pr_out_mon_header(genl
->cmd
);
4418 pr_out_trap(dl
, tb
, false);
4419 pr_out_mon_footer();
4421 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4422 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4423 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4424 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4425 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4426 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4427 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4428 !tb
[DEVLINK_ATTR_STATS
])
4429 return MNL_CB_ERROR
;
4430 pr_out_mon_header(genl
->cmd
);
4431 pr_out_trap_group(dl
, tb
, false);
4432 pr_out_mon_footer();
4434 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4435 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4436 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4437 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4438 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4439 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4440 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4441 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4442 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4443 return MNL_CB_ERROR
;
4444 pr_out_mon_header(genl
->cmd
);
4445 pr_out_trap_policer(dl
, tb
, false);
4451 static int cmd_mon_show(struct dl
*dl
)
4454 unsigned int index
= 0;
4455 const char *cur_obj
;
4457 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4458 if (strcmp(cur_obj
, "all") != 0 &&
4459 strcmp(cur_obj
, "dev") != 0 &&
4460 strcmp(cur_obj
, "port") != 0 &&
4461 strcmp(cur_obj
, "health") != 0 &&
4462 strcmp(cur_obj
, "trap") != 0 &&
4463 strcmp(cur_obj
, "trap-group") != 0 &&
4464 strcmp(cur_obj
, "trap-policer") != 0) {
4465 pr_err("Unknown object \"%s\"\n", cur_obj
);
4469 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4472 open_json_object(NULL
);
4473 open_json_array(PRINT_JSON
, "mon");
4474 err
= _mnlg_socket_recv_run_intr(dl
->nlg
, cmd_mon_show_cb
, dl
);
4475 close_json_array(PRINT_JSON
, NULL
);
4476 close_json_object();
4482 static void cmd_mon_help(void)
4484 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4485 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4488 static int cmd_mon(struct dl
*dl
)
4490 if (dl_argv_match(dl
, "help")) {
4494 return cmd_mon_show(dl
);
4497 struct dpipe_field
{
4500 unsigned int bitwidth
;
4501 enum devlink_dpipe_field_mapping_type mapping_type
;
4504 struct dpipe_header
{
4505 struct list_head list
;
4508 struct dpipe_field
*fields
;
4509 unsigned int fields_count
;
4512 struct dpipe_table
{
4513 struct list_head list
;
4515 unsigned int resource_id
;
4516 bool resource_valid
;
4519 struct dpipe_tables
{
4520 struct list_head table_list
;
4530 enum devlink_resource_unit unit
;
4535 struct list_head list
;
4536 struct list_head resource_list
;
4537 struct resource
*parent
;
4541 struct list_head resource_list
;
4544 struct resource_ctx
{
4547 struct resources
*resources
;
4548 struct dpipe_tables
*tables
;
4549 bool print_resources
;
4550 bool pending_change
;
4553 static struct resource
*resource_alloc(void)
4555 struct resource
*resource
;
4557 resource
= calloc(1, sizeof(struct resource
));
4560 INIT_LIST_HEAD(&resource
->resource_list
);
4564 static void resource_free(struct resource
*resource
)
4566 struct resource
*child_resource
, *tmp
;
4568 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4570 free(child_resource
->name
);
4571 resource_free(child_resource
);
4576 static struct resources
*resources_alloc(void)
4578 struct resources
*resources
;
4580 resources
= calloc(1, sizeof(struct resources
));
4583 INIT_LIST_HEAD(&resources
->resource_list
);
4587 static void resources_free(struct resources
*resources
)
4589 struct resource
*resource
, *tmp
;
4591 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4592 resource_free(resource
);
4595 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4597 ctx
->resources
= resources_alloc();
4598 if (!ctx
->resources
)
4604 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4606 resources_free(ctx
->resources
);
4612 struct list_head global_headers
;
4613 struct list_head local_headers
;
4614 struct dpipe_tables
*tables
;
4615 struct resources
*resources
;
4620 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4622 struct dpipe_header
*header
;
4624 header
= calloc(1, sizeof(struct dpipe_header
));
4627 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4628 if (!header
->fields
)
4629 goto err_fields_alloc
;
4630 header
->fields_count
= fields_count
;
4638 static void dpipe_header_free(struct dpipe_header
*header
)
4640 free(header
->fields
);
4644 static void dpipe_header_clear(struct dpipe_header
*header
)
4646 struct dpipe_field
*field
;
4649 for (i
= 0; i
< header
->fields_count
; i
++) {
4650 field
= &header
->fields
[i
];
4656 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4657 struct dpipe_header
*header
, bool global
)
4660 list_add(&header
->list
, &ctx
->global_headers
);
4662 list_add(&header
->list
, &ctx
->local_headers
);
4665 static void dpipe_header_del(struct dpipe_header
*header
)
4667 list_del(&header
->list
);
4670 static struct dpipe_table
*dpipe_table_alloc(void)
4672 return calloc(1, sizeof(struct dpipe_table
));
4675 static void dpipe_table_free(struct dpipe_table
*table
)
4680 static struct dpipe_tables
*dpipe_tables_alloc(void)
4682 struct dpipe_tables
*tables
;
4684 tables
= calloc(1, sizeof(struct dpipe_tables
));
4687 INIT_LIST_HEAD(&tables
->table_list
);
4691 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4693 struct dpipe_table
*table
, *tmp
;
4695 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4696 dpipe_table_free(table
);
4700 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4702 ctx
->tables
= dpipe_tables_alloc();
4707 INIT_LIST_HEAD(&ctx
->global_headers
);
4708 INIT_LIST_HEAD(&ctx
->local_headers
);
4712 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4714 struct dpipe_header
*header
, *tmp
;
4716 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4718 dpipe_header_del(header
);
4719 dpipe_header_clear(header
);
4720 dpipe_header_free(header
);
4722 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4724 dpipe_header_del(header
);
4725 dpipe_header_clear(header
);
4726 dpipe_header_free(header
);
4728 dpipe_tables_free(ctx
->tables
);
4731 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4732 uint32_t header_id
, bool global
)
4734 struct list_head
*header_list
;
4735 struct dpipe_header
*header
;
4738 header_list
= &ctx
->global_headers
;
4740 header_list
= &ctx
->local_headers
;
4741 list_for_each_entry(header
, header_list
, list
) {
4742 if (header
->id
!= header_id
)
4744 return header
->name
;
4749 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4751 uint32_t field_id
, bool global
)
4753 struct list_head
*header_list
;
4754 struct dpipe_header
*header
;
4757 header_list
= &ctx
->global_headers
;
4759 header_list
= &ctx
->local_headers
;
4760 list_for_each_entry(header
, header_list
, list
) {
4761 if (header
->id
!= header_id
)
4763 return header
->fields
[field_id
].name
;
4769 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4771 switch (mapping_type
) {
4772 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4774 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4782 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4783 uint32_t field_id
, bool global
)
4785 enum devlink_dpipe_field_mapping_type mapping_type
;
4786 struct list_head
*header_list
;
4787 struct dpipe_header
*header
;
4790 header_list
= &ctx
->global_headers
;
4792 header_list
= &ctx
->local_headers
;
4793 list_for_each_entry(header
, header_list
, list
) {
4794 if (header
->id
!= header_id
)
4796 mapping_type
= header
->fields
[field_id
].mapping_type
;
4797 return dpipe_field_mapping_e2s(mapping_type
);
4802 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4803 struct dpipe_field
*fields
,
4804 unsigned int field_count
)
4806 struct dpipe_field
*field
;
4809 for (i
= 0; i
< field_count
; i
++) {
4811 pr_out_entry_start(ctx
->dl
);
4812 check_indent_newline(ctx
->dl
);
4813 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4814 if (ctx
->dl
->verbose
)
4815 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4816 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4817 if (field
->mapping_type
) {
4818 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4819 dpipe_field_mapping_e2s(field
->mapping_type
));
4821 pr_out_entry_end(ctx
->dl
);
4826 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4827 struct dpipe_header
*header
, bool global
)
4829 pr_out_handle_start_arr(ctx
->dl
, tb
);
4830 check_indent_newline(ctx
->dl
);
4831 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4832 if (ctx
->dl
->verbose
) {
4833 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4834 print_bool(PRINT_ANY
, "global", " global %s", global
);
4836 pr_out_array_start(ctx
->dl
, "field");
4837 pr_out_dpipe_fields(ctx
, header
->fields
,
4838 header
->fields_count
);
4839 pr_out_array_end(ctx
->dl
);
4840 pr_out_handle_end(ctx
->dl
);
4843 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4846 struct dpipe_header
*header
;
4848 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4849 pr_out_dpipe_header(ctx
, tb
, header
, false);
4851 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4852 pr_out_dpipe_header(ctx
, tb
, header
, true);
4855 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4857 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4861 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4862 if (err
!= MNL_CB_OK
)
4864 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4865 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4866 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4867 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4870 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4871 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4872 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4873 field
->name
= strdup(name
);
4876 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4880 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4881 struct dpipe_field
*fields
)
4883 struct nlattr
*nla_field
;
4887 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4888 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4896 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4898 struct nlattr
*nla_field
;
4899 unsigned int count
= 0;
4901 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4906 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4908 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4909 struct dpipe_header
*header
;
4910 unsigned int fields_count
;
4911 const char *header_name
;
4915 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4916 if (err
!= MNL_CB_OK
)
4919 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4920 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4921 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4924 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4925 header
= dpipe_header_alloc(fields_count
);
4929 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4930 header
->name
= strdup(header_name
);
4931 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4932 header
->fields_count
= fields_count
;
4933 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4935 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4939 dpipe_header_add(ctx
, header
, global
);
4943 dpipe_header_free(header
);
4947 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4949 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4950 struct nlattr
*nla_header
;
4953 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4954 err
= dpipe_header_get(ctx
, nla_header
);
4961 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4963 struct dpipe_ctx
*ctx
= data
;
4964 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4965 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4968 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4969 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4970 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4971 return MNL_CB_ERROR
;
4972 err
= dpipe_headers_get(ctx
, tb
);
4975 return MNL_CB_ERROR
;
4978 if (ctx
->print_headers
)
4979 pr_out_dpipe_headers(ctx
, tb
);
4983 static int cmd_dpipe_headers_show(struct dl
*dl
)
4985 struct nlmsghdr
*nlh
;
4986 struct dpipe_ctx ctx
= {};
4987 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4990 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4992 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4996 err
= dpipe_ctx_init(&ctx
, dl
);
5000 ctx
.print_headers
= true;
5002 pr_out_section_start(dl
, "header");
5003 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5005 pr_err("error get headers %s\n", strerror(ctx
.err
));
5006 pr_out_section_end(dl
);
5008 dpipe_ctx_fini(&ctx
);
5012 static void cmd_dpipe_help(void)
5014 pr_err("Usage: devlink dpipe table show DEV [ name TABLE_NAME ]\n");
5015 pr_err(" devlink dpipe table set DEV name TABLE_NAME\n");
5016 pr_err(" [ counters_enabled { true | false } ]\n");
5017 pr_err(" devlink dpipe table dump DEV name TABLE_NAME\n");
5018 pr_err(" devlink dpipe header show DEV\n");
5021 static int cmd_dpipe_header(struct dl
*dl
)
5023 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5026 } else if (dl_argv_match(dl
, "show")) {
5028 return cmd_dpipe_headers_show(dl
);
5030 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5035 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
5037 switch (action_type
) {
5038 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
5039 return "field_modify";
5045 struct dpipe_op_info
{
5051 struct dpipe_action
{
5052 struct dpipe_op_info info
;
5056 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5057 struct dpipe_ctx
*ctx
)
5059 struct dpipe_op_info
*op_info
= &action
->info
;
5060 const char *mapping
;
5062 check_indent_newline(ctx
->dl
);
5063 print_string(PRINT_ANY
, "type", "type %s",
5064 dpipe_action_type_e2s(action
->type
));
5065 print_string(PRINT_ANY
, "header", " header %s",
5066 dpipe_header_id2s(ctx
, op_info
->header_id
,
5067 op_info
->header_global
));
5068 print_string(PRINT_ANY
, "field", " field %s",
5069 dpipe_field_id2s(ctx
, op_info
->header_id
,
5071 op_info
->header_global
));
5072 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5074 op_info
->header_global
);
5076 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5079 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5081 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5084 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5085 if (err
!= MNL_CB_OK
)
5088 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5089 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5090 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5091 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5095 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5096 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5097 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5098 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5103 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5104 struct nlattr
*nla_actions
)
5106 struct nlattr
*nla_action
;
5107 struct dpipe_action action
;
5109 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5110 pr_out_entry_start(ctx
->dl
);
5111 if (dpipe_action_parse(&action
, nla_action
))
5112 goto err_action_parse
;
5113 pr_out_dpipe_action(&action
, ctx
);
5114 pr_out_entry_end(ctx
->dl
);
5119 pr_out_entry_end(ctx
->dl
);
5124 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5126 switch (match_type
) {
5127 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5128 return "field_exact";
5134 struct dpipe_match
{
5135 struct dpipe_op_info info
;
5139 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5140 struct dpipe_ctx
*ctx
)
5142 struct dpipe_op_info
*op_info
= &match
->info
;
5143 const char *mapping
;
5145 check_indent_newline(ctx
->dl
);
5146 print_string(PRINT_ANY
, "type", "type %s",
5147 dpipe_match_type_e2s(match
->type
));
5148 print_string(PRINT_ANY
, "header", " header %s",
5149 dpipe_header_id2s(ctx
, op_info
->header_id
,
5150 op_info
->header_global
));
5151 print_string(PRINT_ANY
, "field", " field %s",
5152 dpipe_field_id2s(ctx
, op_info
->header_id
,
5154 op_info
->header_global
));
5155 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5157 op_info
->header_global
);
5159 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5162 static int dpipe_match_parse(struct dpipe_match
*match
,
5166 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5169 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5170 if (err
!= MNL_CB_OK
)
5173 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5174 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5175 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5176 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5180 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5181 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5182 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5183 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5188 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5189 struct nlattr
*nla_matches
)
5191 struct nlattr
*nla_match
;
5192 struct dpipe_match match
;
5194 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5195 pr_out_entry_start(ctx
->dl
);
5196 if (dpipe_match_parse(&match
, nla_match
))
5197 goto err_match_parse
;
5198 pr_out_dpipe_match(&match
, ctx
);
5199 pr_out_entry_end(ctx
->dl
);
5204 pr_out_entry_end(ctx
->dl
);
5208 static struct resource
*
5209 resource_find(struct resources
*resources
, struct resource
*resource
,
5210 uint64_t resource_id
)
5212 struct list_head
*list_head
;
5215 list_head
= &resources
->resource_list
;
5217 list_head
= &resource
->resource_list
;
5219 list_for_each_entry(resource
, list_head
, list
) {
5220 struct resource
*child_resource
;
5222 if (resource
->id
== resource_id
)
5225 child_resource
= resource_find(resources
, resource
,
5228 return child_resource
;
5234 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5235 uint64_t resource_id
)
5237 struct resource
*resource
, *parent_resource
;
5238 const char del
[] = "/";
5242 resource
= resource_find(resources
, NULL
, resource_id
);
5246 for (parent_resource
= resource
; parent_resource
;
5247 parent_resource
= parent_resource
->parent
)
5248 path_len
+= strlen(parent_resource
->name
) + 1;
5251 path
= calloc(1, path_len
);
5255 path
+= path_len
- 1;
5256 for (parent_resource
= resource
; parent_resource
;
5257 parent_resource
= parent_resource
->parent
) {
5258 path
-= strlen(parent_resource
->name
);
5259 memcpy(path
, parent_resource
->name
,
5260 strlen(parent_resource
->name
));
5261 path
-= strlen(del
);
5262 memcpy(path
, del
, strlen(del
));
5264 check_indent_newline(dl
);
5265 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5269 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5271 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5272 struct dpipe_table
*table
;
5273 uint32_t resource_units
;
5274 bool counters_enabled
;
5275 bool resource_valid
;
5279 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5280 if (err
!= MNL_CB_OK
)
5283 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5284 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5285 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5286 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5287 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5291 table
= dpipe_table_alloc();
5295 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5296 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5297 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5299 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5301 if (resource_valid
) {
5302 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5303 table
->resource_valid
= true;
5306 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5307 if (!ctx
->print_tables
)
5310 check_indent_newline(ctx
->dl
);
5311 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5312 print_uint(PRINT_ANY
, "size", " size %u", size
);
5313 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5315 if (resource_valid
) {
5316 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5317 resource_path_print(ctx
->dl
, ctx
->resources
,
5318 table
->resource_id
);
5319 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5323 pr_out_array_start(ctx
->dl
, "match");
5324 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5325 goto err_matches_show
;
5326 pr_out_array_end(ctx
->dl
);
5328 pr_out_array_start(ctx
->dl
, "action");
5329 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5330 goto err_actions_show
;
5331 pr_out_array_end(ctx
->dl
);
5337 pr_out_array_end(ctx
->dl
);
5341 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5343 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5344 struct nlattr
*nla_table
;
5346 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5347 if (ctx
->print_tables
)
5348 pr_out_handle_start_arr(ctx
->dl
, tb
);
5349 if (dpipe_table_show(ctx
, nla_table
))
5350 goto err_table_show
;
5351 if (ctx
->print_tables
)
5352 pr_out_handle_end(ctx
->dl
);
5357 if (ctx
->print_tables
)
5358 pr_out_handle_end(ctx
->dl
);
5362 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5364 struct dpipe_ctx
*ctx
= data
;
5365 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5366 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5368 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5369 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5370 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5371 return MNL_CB_ERROR
;
5373 if (dpipe_tables_show(ctx
, tb
))
5374 return MNL_CB_ERROR
;
5378 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5380 static int cmd_dpipe_table_show(struct dl
*dl
)
5382 struct nlmsghdr
*nlh
;
5383 struct dpipe_ctx dpipe_ctx
= {};
5384 struct resource_ctx resource_ctx
= {};
5385 uint16_t flags
= NLM_F_REQUEST
;
5388 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5392 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5394 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5398 dpipe_ctx
.print_tables
= true;
5400 dl_opts_put(nlh
, dl
);
5401 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5404 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5405 goto err_headers_get
;
5408 err
= resource_ctx_init(&resource_ctx
, dl
);
5410 goto err_resource_ctx_init
;
5412 resource_ctx
.print_resources
= false;
5413 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5414 dl_opts_put(nlh
, dl
);
5415 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5418 dpipe_ctx
.resources
= resource_ctx
.resources
;
5420 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5421 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5422 dl_opts_put(nlh
, dl
);
5424 pr_out_section_start(dl
, "table");
5425 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5426 pr_out_section_end(dl
);
5428 resource_ctx_fini(&resource_ctx
);
5429 dpipe_ctx_fini(&dpipe_ctx
);
5432 err_resource_ctx_init
:
5434 dpipe_ctx_fini(&dpipe_ctx
);
5438 static int cmd_dpipe_table_set(struct dl
*dl
)
5440 struct nlmsghdr
*nlh
;
5443 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5444 NLM_F_REQUEST
| NLM_F_ACK
);
5446 err
= dl_argv_parse_put(nlh
, dl
,
5447 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5448 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5452 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5455 enum dpipe_value_type
{
5456 DPIPE_VALUE_TYPE_VALUE
,
5457 DPIPE_VALUE_TYPE_MASK
,
5461 dpipe_value_type_e2s(enum dpipe_value_type type
)
5464 case DPIPE_VALUE_TYPE_VALUE
:
5466 case DPIPE_VALUE_TYPE_MASK
:
5467 return "value_mask";
5473 struct dpipe_field_printer
{
5474 unsigned int field_id
;
5475 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5478 struct dpipe_header_printer
{
5479 struct dpipe_field_printer
*printers
;
5480 unsigned int printers_count
;
5481 unsigned int header_id
;
5484 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5485 enum dpipe_value_type type
,
5488 struct in_addr ip_addr
;
5490 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5491 check_indent_newline(ctx
->dl
);
5492 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5496 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5497 enum dpipe_value_type type
,
5500 check_indent_newline(ctx
->dl
);
5501 print_string_name_value(dpipe_value_type_e2s(type
),
5502 ether_ntoa((struct ether_addr
*)value
));
5505 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5506 enum dpipe_value_type type
,
5509 char str
[INET6_ADDRSTRLEN
];
5511 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5512 check_indent_newline(ctx
->dl
);
5513 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5516 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5518 .printer
= dpipe_field_printer_ipv4_addr
,
5519 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5523 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5524 .printers
= dpipe_field_printers_ipv4
,
5525 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5526 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5529 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5531 .printer
= dpipe_field_printer_ethernet_addr
,
5532 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5536 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5537 .printers
= dpipe_field_printers_ethernet
,
5538 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5539 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5542 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5544 .printer
= dpipe_field_printer_ipv6_addr
,
5545 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5549 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5550 .printers
= dpipe_field_printers_ipv6
,
5551 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5552 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5555 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5556 &dpipe_header_printer_ipv4
,
5557 &dpipe_header_printer_ethernet
,
5558 &dpipe_header_printer_ipv6
,
5561 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5562 struct dpipe_op_info
*info
,
5563 enum dpipe_value_type type
,
5566 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5567 struct dpipe_header_printer
*header_printer
;
5568 struct dpipe_field_printer
*field_printer
;
5569 unsigned int field_printers_count
;
5573 for (i
= 0; i
< header_printers_count
; i
++) {
5574 header_printer
= dpipe_header_printers
[i
];
5575 if (header_printer
->header_id
!= info
->header_id
)
5577 field_printers_count
= header_printer
->printers_count
;
5578 for (j
= 0; j
< field_printers_count
; j
++) {
5579 field_printer
= &header_printer
->printers
[j
];
5580 if (field_printer
->field_id
!= info
->field_id
)
5582 field_printer
->printer(ctx
, type
, value
);
5590 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5592 unsigned int value_len
,
5593 struct dpipe_op_info
*info
,
5594 enum dpipe_value_type type
)
5596 if (info
->header_global
&&
5597 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5600 if (value_len
== sizeof(uint32_t)) {
5601 uint32_t *value_32
= value
;
5603 check_indent_newline(ctx
->dl
);
5604 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5608 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5609 struct nlattr
**nla_match_value
,
5610 struct dpipe_op_info
*info
)
5612 void *value
, *value_mask
;
5613 uint32_t value_mapping
;
5617 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5618 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5620 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5621 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5624 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5625 check_indent_newline(ctx
->dl
);
5626 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5630 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5631 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5632 DPIPE_VALUE_TYPE_MASK
);
5635 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5638 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5641 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5642 struct dpipe_match match
;
5645 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5646 if (err
!= MNL_CB_OK
)
5649 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5650 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5654 pr_out_entry_start(ctx
->dl
);
5655 if (dpipe_match_parse(&match
,
5656 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5657 goto err_match_parse
;
5658 pr_out_dpipe_match(&match
, ctx
);
5659 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5660 pr_out_entry_end(ctx
->dl
);
5665 pr_out_entry_end(ctx
->dl
);
5669 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5672 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5673 struct dpipe_action action
;
5676 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5677 if (err
!= MNL_CB_OK
)
5680 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5681 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5685 pr_out_entry_start(ctx
->dl
);
5686 if (dpipe_action_parse(&action
,
5687 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5688 goto err_action_parse
;
5689 pr_out_dpipe_action(&action
, ctx
);
5690 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5691 pr_out_entry_end(ctx
->dl
);
5696 pr_out_entry_end(ctx
->dl
);
5701 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5702 struct nlattr
*nla_action_values
)
5704 struct nlattr
*nla_action_value
;
5706 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5707 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5714 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5715 struct nlattr
*nla_match_values
)
5717 struct nlattr
*nla_match_value
;
5719 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5720 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5726 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5728 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5729 uint32_t entry_index
;
5733 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5734 if (err
!= MNL_CB_OK
)
5737 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5738 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5739 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5743 check_indent_newline(ctx
->dl
);
5744 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5745 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5747 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5748 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5749 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5752 pr_out_array_start(ctx
->dl
, "match_value");
5753 if (dpipe_tables_match_values_show(ctx
,
5754 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5755 goto err_match_values_show
;
5756 pr_out_array_end(ctx
->dl
);
5758 pr_out_array_start(ctx
->dl
, "action_value");
5759 if (dpipe_tables_action_values_show(ctx
,
5760 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5761 goto err_action_values_show
;
5762 pr_out_array_end(ctx
->dl
);
5765 err_action_values_show
:
5766 err_match_values_show
:
5767 pr_out_array_end(ctx
->dl
);
5771 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5773 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5774 struct nlattr
*nla_entry
;
5776 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5777 pr_out_handle_start_arr(ctx
->dl
, tb
);
5778 if (dpipe_entry_show(ctx
, nla_entry
))
5779 goto err_entry_show
;
5780 pr_out_handle_end(ctx
->dl
);
5785 pr_out_handle_end(ctx
->dl
);
5789 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5791 struct dpipe_ctx
*ctx
= data
;
5792 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5793 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5795 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5796 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5797 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5798 return MNL_CB_ERROR
;
5800 if (dpipe_table_entries_show(ctx
, tb
))
5801 return MNL_CB_ERROR
;
5805 static int cmd_dpipe_table_dump(struct dl
*dl
)
5807 struct nlmsghdr
*nlh
;
5808 struct dpipe_ctx ctx
= {};
5809 uint16_t flags
= NLM_F_REQUEST
;
5812 err
= dpipe_ctx_init(&ctx
, dl
);
5816 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5820 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5821 dl_opts_put(nlh
, dl
);
5822 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5824 pr_err("error get headers %s\n", strerror(ctx
.err
));
5828 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5829 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5830 dl_opts_put(nlh
, dl
);
5832 pr_out_section_start(dl
, "table_entry");
5833 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5834 pr_out_section_end(dl
);
5836 dpipe_ctx_fini(&ctx
);
5840 static int cmd_dpipe_table(struct dl
*dl
)
5842 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5845 } else if (dl_argv_match(dl
, "show")) {
5847 return cmd_dpipe_table_show(dl
);
5848 } else if (dl_argv_match(dl
, "set")) {
5850 return cmd_dpipe_table_set(dl
);
5851 } else if (dl_argv_match(dl
, "dump")) {
5853 return cmd_dpipe_table_dump(dl
);
5855 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5859 static int cmd_dpipe(struct dl
*dl
)
5861 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5864 } else if (dl_argv_match(dl
, "header")) {
5866 return cmd_dpipe_header(dl
);
5867 } else if (dl_argv_match(dl
, "table")) {
5869 return cmd_dpipe_table(dl
);
5871 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5876 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5877 struct nlattr
**nla_resource
)
5879 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5880 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5881 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5882 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5883 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5884 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5885 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5889 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5890 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5891 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5892 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5893 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5894 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5895 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5897 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5898 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5900 resource
->size_new
= resource
->size
;
5902 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5903 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5904 resource
->occ_valid
= true;
5907 if (resource
->size_new
!= resource
->size
)
5908 ctx
->pending_change
= true;
5914 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5915 struct resource
*parent_resource
, struct nlattr
*nl
)
5917 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5918 struct nlattr
*nla_child_resource
;
5919 struct nlattr
*nla_resources
;
5929 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5930 if (err
!= MNL_CB_OK
)
5933 err
= resource_parse(ctx
, resource
, nla_resource
);
5937 resource
->parent
= parent_resource
;
5938 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5941 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5942 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5944 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5945 struct resource
*child_resource
;
5946 struct list_head
*list
;
5948 child_resource
= resource_alloc();
5949 if (!child_resource
)
5953 list
= &ctx
->resources
->resource_list
;
5955 list
= &resource
->resource_list
;
5957 list_add_tail(&child_resource
->list
, list
);
5958 err
= resource_get(ctx
, child_resource
, resource
,
5959 nla_child_resource
);
5967 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5970 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5971 default: return "<unknown unit>";
5975 static void resource_show(struct resource
*resource
,
5976 struct resource_ctx
*ctx
)
5978 struct resource
*child_resource
;
5979 struct dpipe_table
*table
;
5980 struct dl
*dl
= ctx
->dl
;
5983 check_indent_newline(dl
);
5984 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
5986 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5987 pr_out_u64(dl
, "size", resource
->size
);
5988 if (resource
->size
!= resource
->size_new
)
5989 pr_out_u64(dl
, "size_new", resource
->size_new
);
5990 if (resource
->occ_valid
)
5991 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
5992 print_string(PRINT_ANY
, "unit", " unit %s",
5993 resource_unit_str_get(resource
->unit
));
5995 if (resource
->size_min
!= resource
->size_max
) {
5996 print_uint(PRINT_ANY
, "size_min", " size_min %u",
5997 resource
->size_min
);
5998 pr_out_u64(dl
, "size_max", resource
->size_max
);
5999 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
6000 resource
->size_gran
);
6003 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
6004 if (table
->resource_id
== resource
->id
&&
6005 table
->resource_valid
)
6009 pr_out_array_start(dl
, "dpipe_tables");
6011 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
6014 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
6015 if (table
->resource_id
!= resource
->id
||
6016 !table
->resource_valid
)
6018 pr_out_entry_start(dl
);
6019 check_indent_newline(dl
);
6020 print_string(PRINT_ANY
, "table_name", "table_name %s",
6022 pr_out_entry_end(dl
);
6025 pr_out_array_end(dl
);
6027 if (list_empty(&resource
->resource_list
))
6030 if (ctx
->pending_change
) {
6031 check_indent_newline(dl
);
6032 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6033 resource
->size_valid
? "true" : "false");
6035 pr_out_array_start(dl
, "resources");
6036 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6037 pr_out_entry_start(dl
);
6038 resource_show(child_resource
, ctx
);
6039 pr_out_entry_end(dl
);
6041 pr_out_array_end(dl
);
6045 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6047 struct resources
*resources
= ctx
->resources
;
6048 struct resource
*resource
;
6050 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6051 pr_out_handle_start_arr(ctx
->dl
, tb
);
6052 resource_show(resource
, ctx
);
6053 pr_out_handle_end(ctx
->dl
);
6057 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6059 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6062 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6064 struct resource_ctx
*ctx
= data
;
6065 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6066 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6069 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6070 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6071 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6072 return MNL_CB_ERROR
;
6074 err
= resources_get(ctx
, tb
);
6077 return MNL_CB_ERROR
;
6080 if (ctx
->print_resources
)
6081 resources_show(ctx
, tb
);
6086 static int cmd_resource_show(struct dl
*dl
)
6088 struct nlmsghdr
*nlh
;
6089 struct dpipe_ctx dpipe_ctx
= {};
6090 struct resource_ctx resource_ctx
= {};
6093 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6097 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6099 dl_opts_put(nlh
, dl
);
6101 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6105 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6108 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6112 err
= resource_ctx_init(&resource_ctx
, dl
);
6116 resource_ctx
.print_resources
= true;
6117 resource_ctx
.tables
= dpipe_ctx
.tables
;
6118 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6119 NLM_F_REQUEST
| NLM_F_ACK
);
6120 dl_opts_put(nlh
, dl
);
6121 pr_out_section_start(dl
, "resources");
6122 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6124 pr_out_section_end(dl
);
6125 resource_ctx_fini(&resource_ctx
);
6127 dpipe_ctx_fini(&dpipe_ctx
);
6131 static void cmd_resource_help(void)
6133 pr_err("Usage: devlink resource show DEV\n"
6134 " devlink resource set DEV path PATH size SIZE\n");
6137 static struct resource
*
6138 resource_find_by_name(struct list_head
*list
, char *name
)
6140 struct resource
*resource
;
6142 list_for_each_entry(resource
, list
, list
) {
6143 if (!strcmp(resource
->name
, name
))
6150 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6151 uint32_t *p_resource_id
, bool *p_resource_valid
)
6153 struct resource
*resource
;
6154 uint32_t resource_id
= 0;
6155 char *resource_path_dup
;
6156 struct list_head
*list
;
6157 const char del
[] = "/";
6158 char *resource_name
;
6160 resource_path_dup
= strdup(resource_path
);
6161 list
= &ctx
->resources
->resource_list
;
6162 resource_name
= strtok(resource_path_dup
, del
);
6163 while (resource_name
!= NULL
) {
6164 resource
= resource_find_by_name(list
, resource_name
);
6166 goto err_resource_lookup
;
6168 list
= &resource
->resource_list
;
6169 resource_name
= strtok(NULL
, del
);
6170 resource_id
= resource
->id
;
6172 free(resource_path_dup
);
6173 *p_resource_valid
= true;
6174 *p_resource_id
= resource_id
;
6177 err_resource_lookup
:
6178 free(resource_path_dup
);
6182 static int cmd_resource_set(struct dl
*dl
)
6184 struct nlmsghdr
*nlh
;
6185 struct resource_ctx ctx
= {};
6188 err
= resource_ctx_init(&ctx
, dl
);
6192 ctx
.print_resources
= false;
6193 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6194 DL_OPT_RESOURCE_SIZE
, 0);
6198 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6200 dl_opts_put(nlh
, dl
);
6201 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6203 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6207 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6208 &dl
->opts
.resource_id
,
6209 &dl
->opts
.resource_id_valid
);
6211 pr_err("error parsing resource path %s\n", strerror(-err
));
6215 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6216 NLM_F_REQUEST
| NLM_F_ACK
);
6218 dl_opts_put(nlh
, dl
);
6219 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6221 resource_ctx_fini(&ctx
);
6225 static int cmd_resource(struct dl
*dl
)
6227 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6228 cmd_resource_help();
6230 } else if (dl_argv_match(dl
, "show")) {
6232 return cmd_resource_show(dl
);
6233 } else if (dl_argv_match(dl
, "set")) {
6235 return cmd_resource_set(dl
);
6237 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6241 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6243 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6244 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6245 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6248 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6249 if (dl
->json_output
)
6250 open_json_object(buf
);
6255 static void pr_out_region_handle_end(struct dl
*dl
)
6257 if (dl
->json_output
)
6258 close_json_object();
6263 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6265 __pr_out_indent_newline(dl
);
6266 if (dl
->json_output
)
6267 open_json_array(PRINT_JSON
, "snapshot");
6269 pr_out("snapshot %s", array
? "[" : "");
6272 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6274 if (dl
->json_output
)
6275 close_json_array(PRINT_JSON
, NULL
);
6280 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6282 uint32_t snapshot_id
;
6284 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6287 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6289 if (dl
->json_output
)
6290 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6292 pr_out("%s%u", index
? " " : "", snapshot_id
);
6295 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6297 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6298 struct nlattr
*nla_sanpshot
;
6301 pr_out_region_snapshots_start(dl
, true);
6302 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6303 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6304 if (err
!= MNL_CB_OK
)
6306 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6308 pr_out_region_snapshots_end(dl
, true);
6311 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6313 pr_out_region_snapshots_start(dl
, false);
6314 pr_out_region_snapshots_id(dl
, tb
, 0);
6315 pr_out_region_snapshots_end(dl
, false);
6318 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6320 pr_out_region_handle_start(dl
, tb
);
6322 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6323 pr_out_u64(dl
, "size",
6324 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6326 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6327 pr_out_snapshots(dl
, tb
);
6329 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6330 pr_out_snapshot(dl
, tb
);
6332 pr_out_region_handle_end(dl
);
6335 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6337 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6338 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6339 struct dl
*dl
= data
;
6341 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6342 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6343 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6344 return MNL_CB_ERROR
;
6346 pr_out_region(dl
, tb
);
6351 static int cmd_region_show(struct dl
*dl
)
6353 struct nlmsghdr
*nlh
;
6354 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6357 if (dl_argc(dl
) == 0)
6358 flags
|= NLM_F_DUMP
;
6360 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6362 if (dl_argc(dl
) > 0) {
6363 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6368 pr_out_section_start(dl
, "regions");
6369 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6370 pr_out_section_end(dl
);
6374 static int cmd_region_snapshot_del(struct dl
*dl
)
6376 struct nlmsghdr
*nlh
;
6379 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6380 NLM_F_REQUEST
| NLM_F_ACK
);
6382 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6383 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6387 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6390 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6392 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6393 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6394 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6395 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6396 struct dl
*dl
= data
;
6399 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6400 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6401 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6402 return MNL_CB_ERROR
;
6404 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6405 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6406 if (err
!= MNL_CB_OK
)
6407 return MNL_CB_ERROR
;
6409 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6410 if (!nla_chunk_data
)
6413 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6414 if (!nla_chunk_addr
)
6417 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6418 mnl_attr_get_payload_len(nla_chunk_data
),
6419 mnl_attr_get_u64(nla_chunk_addr
));
6424 static int cmd_region_dump(struct dl
*dl
)
6426 struct nlmsghdr
*nlh
;
6429 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6430 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6432 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6433 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6437 pr_out_section_start(dl
, "dump");
6438 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6439 pr_out_section_end(dl
);
6440 if (!dl
->json_output
)
6445 static int cmd_region_read(struct dl
*dl
)
6447 struct nlmsghdr
*nlh
;
6450 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6451 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6453 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6454 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6455 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6459 pr_out_section_start(dl
, "read");
6460 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6461 pr_out_section_end(dl
);
6462 if (!dl
->json_output
)
6467 static void cmd_region_help(void)
6469 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6470 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6471 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6472 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6475 static int cmd_region(struct dl
*dl
)
6477 if (dl_no_arg(dl
)) {
6478 return cmd_region_show(dl
);
6479 } else if (dl_argv_match(dl
, "help")) {
6482 } else if (dl_argv_match(dl
, "show")) {
6484 return cmd_region_show(dl
);
6485 } else if (dl_argv_match(dl
, "del")) {
6487 return cmd_region_snapshot_del(dl
);
6488 } else if (dl_argv_match(dl
, "dump")) {
6490 return cmd_region_dump(dl
);
6491 } else if (dl_argv_match(dl
, "read")) {
6493 return cmd_region_read(dl
);
6495 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6499 static int cmd_health_set_params(struct dl
*dl
)
6501 struct nlmsghdr
*nlh
;
6504 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6505 NLM_F_REQUEST
| NLM_F_ACK
);
6506 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6507 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6508 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6512 dl_opts_put(nlh
, dl
);
6513 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6516 static int cmd_health_dump_clear(struct dl
*dl
)
6518 struct nlmsghdr
*nlh
;
6521 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6522 NLM_F_REQUEST
| NLM_F_ACK
);
6524 err
= dl_argv_parse_put(nlh
, dl
,
6525 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6529 dl_opts_put(nlh
, dl
);
6530 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6533 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6538 check_indent_newline(dl
);
6541 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6544 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6547 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6550 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6553 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6555 case MNL_TYPE_NUL_STRING
:
6556 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6558 case MNL_TYPE_BINARY
:
6559 len
= mnl_attr_get_payload_len(nl_data
);
6560 data
= mnl_attr_get_payload(nl_data
);
6561 pr_out_binary_value(dl
, data
, len
);
6569 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6574 pr_out_name(dl
, *name
);
6581 struct list_head list
;
6584 struct fmsg_cb_data
{
6588 struct list_head entry_list
;
6591 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6592 uint8_t *attr_value
, bool insert
)
6594 struct nest_entry
*entry
;
6597 entry
= malloc(sizeof(struct nest_entry
));
6601 entry
->attr_type
= *attr_value
;
6602 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6604 if (list_empty(&fmsg_data
->entry_list
))
6605 return MNL_CB_ERROR
;
6606 entry
= list_first_entry(&fmsg_data
->entry_list
,
6607 struct nest_entry
, list
);
6608 *attr_value
= entry
->attr_type
;
6609 list_del(&entry
->list
);
6615 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6618 pr_out_fmsg_name(dl
, name
);
6620 __pr_out_indent_inc();
6623 static void pr_out_fmsg_group_end(struct dl
*dl
)
6626 __pr_out_indent_dec();
6629 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6631 if (dl
->json_output
) {
6632 pr_out_fmsg_name(dl
, name
);
6633 open_json_object(NULL
);
6635 pr_out_fmsg_group_start(dl
, name
);
6639 static void pr_out_fmsg_end_object(struct dl
*dl
)
6641 if (dl
->json_output
)
6642 close_json_object();
6644 pr_out_fmsg_group_end(dl
);
6647 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6649 if (dl
->json_output
) {
6650 pr_out_fmsg_name(dl
, name
);
6651 open_json_array(PRINT_JSON
, NULL
);
6653 pr_out_fmsg_group_start(dl
, name
);
6657 static void pr_out_fmsg_end_array(struct dl
*dl
)
6659 if (dl
->json_output
)
6660 close_json_array(PRINT_JSON
, NULL
);
6662 pr_out_fmsg_group_end(dl
);
6665 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6668 struct dl
*dl
= fmsg_data
->dl
;
6669 uint8_t value
= nest_value
;
6672 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6673 if (err
!= MNL_CB_OK
)
6677 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6679 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6681 pr_out_fmsg_end_object(dl
);
6683 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6685 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6687 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6689 pr_out_fmsg_end_array(dl
);
6697 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6699 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6700 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6701 struct fmsg_cb_data
*fmsg_data
= data
;
6702 struct dl
*dl
= fmsg_data
->dl
;
6703 struct nlattr
*nla_object
;
6707 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6708 if (!tb
[DEVLINK_ATTR_FMSG
])
6709 return MNL_CB_ERROR
;
6711 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6712 attr_type
= mnl_attr_get_type(nla_object
);
6713 switch (attr_type
) {
6714 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6715 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6716 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6717 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6718 if (err
!= MNL_CB_OK
)
6721 case DEVLINK_ATTR_FMSG_NEST_END
:
6722 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6723 if (err
!= MNL_CB_OK
)
6726 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6727 free(fmsg_data
->name
);
6728 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6729 if (!fmsg_data
->name
)
6732 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6733 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6735 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6736 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6737 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6739 if (err
!= MNL_CB_OK
)
6749 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6751 /* FMSG is dynamic: opening of an object or array causes a
6752 * newline. JSON starts with an { or [, but plain text should
6753 * not start with a new line. Ensure this by setting
6754 * g_new_line_count to 1: avoiding newline before the first
6757 g_new_line_count
= 1;
6760 INIT_LIST_HEAD(&data
->entry_list
);
6763 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6765 struct fmsg_cb_data data
;
6766 struct nlmsghdr
*nlh
;
6769 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6771 err
= dl_argv_parse_put(nlh
, dl
,
6772 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6776 cmd_fmsg_init(dl
, &data
);
6777 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6782 static int cmd_health_dump_show(struct dl
*dl
)
6784 return cmd_health_object_common(dl
,
6785 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6789 static int cmd_health_diagnose(struct dl
*dl
)
6791 return cmd_health_object_common(dl
,
6792 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6796 static int cmd_health_recover(struct dl
*dl
)
6798 struct nlmsghdr
*nlh
;
6801 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6802 NLM_F_REQUEST
| NLM_F_ACK
);
6804 err
= dl_argv_parse_put(nlh
, dl
,
6805 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6809 dl_opts_put(nlh
, dl
);
6810 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6813 enum devlink_health_reporter_state
{
6814 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6815 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6818 static const char *health_state_name(uint8_t state
)
6821 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6822 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6823 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6824 return HEALTH_REPORTER_STATE_ERROR_STR
;
6826 return "<unknown state>";
6830 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6832 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6833 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6834 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6835 struct sysinfo s_info
;
6841 info
= localtime(&now
);
6842 err
= sysinfo(&s_info
);
6845 /* Subtract uptime in sec from now yields the time of system
6846 * uptime. To this, add time_ms which is the amount of
6847 * milliseconds elapsed between uptime and the dump taken.
6849 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6850 info
= localtime(&sec
);
6852 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6853 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6854 check_indent_newline(dl
);
6855 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6856 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6859 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6861 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6862 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6867 ts
= mnl_attr_get_u64(attr
);
6868 tv_sec
= ts
/ 1000000000;
6869 tm
= localtime(&tv_sec
);
6871 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6872 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6874 check_indent_newline(dl
);
6875 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6876 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6879 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6881 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6882 enum devlink_health_reporter_state state
;
6885 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6887 if (err
!= MNL_CB_OK
)
6890 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6891 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6892 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6893 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6896 pr_out_handle_start_arr(dl
, tb_health
);
6898 check_indent_newline(dl
);
6899 print_string(PRINT_ANY
, "reporter", "reporter %s",
6900 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6901 if (!dl
->json_output
) {
6903 __pr_out_indent_inc();
6905 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6906 check_indent_newline(dl
);
6907 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6908 pr_out_u64(dl
, "error",
6909 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6910 pr_out_u64(dl
, "recover",
6911 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6912 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6913 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6914 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6915 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6916 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6917 pr_out_u64(dl
, "grace_period",
6918 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6919 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6920 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6921 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6923 __pr_out_indent_dec();
6924 pr_out_handle_end(dl
);
6927 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6929 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6930 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6931 struct dl
*dl
= data
;
6933 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6934 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6935 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6936 return MNL_CB_ERROR
;
6938 pr_out_health(dl
, tb
);
6943 static int cmd_health_show(struct dl
*dl
)
6945 struct nlmsghdr
*nlh
;
6946 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6949 if (dl_argc(dl
) == 0)
6950 flags
|= NLM_F_DUMP
;
6951 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6954 if (dl_argc(dl
) > 0) {
6955 err
= dl_argv_parse_put(nlh
, dl
,
6957 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6961 pr_out_section_start(dl
, "health");
6963 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6964 pr_out_section_end(dl
);
6968 static void cmd_health_help(void)
6970 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6971 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6972 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6973 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6974 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6975 pr_err(" devlink health set DEV reporter REPORTER_NAME\n");
6976 pr_err(" [ grace_period MSEC ]\n");
6977 pr_err(" [ auto_recover { true | false } ]\n");
6980 static int cmd_health(struct dl
*dl
)
6982 if (dl_argv_match(dl
, "help")) {
6985 } else if (dl_argv_match(dl
, "show") ||
6986 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6988 return cmd_health_show(dl
);
6989 } else if (dl_argv_match(dl
, "recover")) {
6991 return cmd_health_recover(dl
);
6992 } else if (dl_argv_match(dl
, "diagnose")) {
6994 return cmd_health_diagnose(dl
);
6995 } else if (dl_argv_match(dl
, "dump")) {
6997 if (dl_argv_match(dl
, "show")) {
6999 return cmd_health_dump_show(dl
);
7000 } else if (dl_argv_match(dl
, "clear")) {
7002 return cmd_health_dump_clear(dl
);
7004 } else if (dl_argv_match(dl
, "set")) {
7006 return cmd_health_set_params(dl
);
7008 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7012 static const char *trap_type_name(uint8_t type
)
7015 case DEVLINK_TRAP_TYPE_DROP
:
7017 case DEVLINK_TRAP_TYPE_EXCEPTION
:
7020 return "<unknown type>";
7024 static const char *trap_action_name(uint8_t action
)
7027 case DEVLINK_TRAP_ACTION_DROP
:
7029 case DEVLINK_TRAP_ACTION_TRAP
:
7032 return "<unknown action>";
7036 static const char *trap_metadata_name(const struct nlattr
*attr
)
7038 switch (attr
->nla_type
) {
7039 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7040 return "input_port";
7041 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7042 return "flow_action_cookie";
7044 return "<unknown metadata type>";
7047 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7049 struct nlattr
*attr_metadata
;
7051 pr_out_array_start(dl
, "metadata");
7052 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7053 check_indent_newline(dl
);
7054 print_string(PRINT_ANY
, NULL
, "%s",
7055 trap_metadata_name(attr_metadata
));
7057 pr_out_array_end(dl
);
7060 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7062 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7063 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7066 pr_out_handle_start_arr(dl
, tb
);
7068 __pr_out_handle_start(dl
, tb
, true, false);
7070 check_indent_newline(dl
);
7071 print_string(PRINT_ANY
, "name", "name %s",
7072 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7073 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7074 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7075 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7076 print_string(PRINT_ANY
, "group", " group %s",
7077 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7079 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7080 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7081 pr_out_handle_end(dl
);
7084 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7086 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7087 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7088 struct dl
*dl
= data
;
7090 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7091 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7092 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7093 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7094 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7095 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7096 return MNL_CB_ERROR
;
7098 pr_out_trap(dl
, tb
, true);
7103 static void cmd_trap_help(void)
7105 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
7106 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7107 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
7108 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7109 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7110 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7111 pr_err(" devlink trap policer show DEV policer POLICER\n");
7114 static int cmd_trap_show(struct dl
*dl
)
7116 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7117 struct nlmsghdr
*nlh
;
7120 if (dl_argc(dl
) == 0)
7121 flags
|= NLM_F_DUMP
;
7123 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7125 if (dl_argc(dl
) > 0) {
7126 err
= dl_argv_parse_put(nlh
, dl
,
7127 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7132 pr_out_section_start(dl
, "trap");
7133 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7134 pr_out_section_end(dl
);
7139 static int cmd_trap_set(struct dl
*dl
)
7141 struct nlmsghdr
*nlh
;
7144 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7145 NLM_F_REQUEST
| NLM_F_ACK
);
7147 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7148 DL_OPT_TRAP_ACTION
);
7152 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7155 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7158 pr_out_handle_start_arr(dl
, tb
);
7160 __pr_out_handle_start(dl
, tb
, true, false);
7162 check_indent_newline(dl
);
7163 print_string(PRINT_ANY
, "name", "name %s",
7164 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7165 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7166 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7167 print_uint(PRINT_ANY
, "policer", " policer %u",
7168 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7169 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7170 pr_out_handle_end(dl
);
7173 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7175 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7176 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7177 struct dl
*dl
= data
;
7179 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7180 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7181 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7182 return MNL_CB_ERROR
;
7184 pr_out_trap_group(dl
, tb
, true);
7189 static int cmd_trap_group_show(struct dl
*dl
)
7191 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7192 struct nlmsghdr
*nlh
;
7195 if (dl_argc(dl
) == 0)
7196 flags
|= NLM_F_DUMP
;
7198 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7200 if (dl_argc(dl
) > 0) {
7201 err
= dl_argv_parse_put(nlh
, dl
,
7202 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7208 pr_out_section_start(dl
, "trap_group");
7209 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7210 pr_out_section_end(dl
);
7215 static int cmd_trap_group_set(struct dl
*dl
)
7217 struct nlmsghdr
*nlh
;
7220 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7221 NLM_F_REQUEST
| NLM_F_ACK
);
7223 err
= dl_argv_parse_put(nlh
, dl
,
7224 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7225 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7229 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7232 static int cmd_trap_group(struct dl
*dl
)
7234 if (dl_argv_match(dl
, "help")) {
7237 } else if (dl_argv_match(dl
, "show") ||
7238 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7240 return cmd_trap_group_show(dl
);
7241 } else if (dl_argv_match(dl
, "set")) {
7243 return cmd_trap_group_set(dl
);
7245 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7249 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7252 pr_out_handle_start_arr(dl
, tb
);
7254 __pr_out_handle_start(dl
, tb
, true, false);
7256 check_indent_newline(dl
);
7257 print_uint(PRINT_ANY
, "policer", "policer %u",
7258 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7259 print_u64(PRINT_ANY
, "rate", " rate %llu",
7260 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7261 print_u64(PRINT_ANY
, "burst", " burst %llu",
7262 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7263 if (tb
[DEVLINK_ATTR_STATS
])
7264 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7265 pr_out_handle_end(dl
);
7268 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7270 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7271 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7272 struct dl
*dl
= data
;
7274 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7275 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7276 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7277 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7278 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7279 return MNL_CB_ERROR
;
7281 pr_out_trap_policer(dl
, tb
, true);
7286 static int cmd_trap_policer_show(struct dl
*dl
)
7288 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7289 struct nlmsghdr
*nlh
;
7292 if (dl_argc(dl
) == 0)
7293 flags
|= NLM_F_DUMP
;
7295 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7297 if (dl_argc(dl
) > 0) {
7298 err
= dl_argv_parse_put(nlh
, dl
,
7299 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7305 pr_out_section_start(dl
, "trap_policer");
7306 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7307 pr_out_section_end(dl
);
7312 static int cmd_trap_policer_set(struct dl
*dl
)
7314 struct nlmsghdr
*nlh
;
7317 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7318 NLM_F_REQUEST
| NLM_F_ACK
);
7320 err
= dl_argv_parse_put(nlh
, dl
,
7321 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7322 DL_OPT_TRAP_POLICER_RATE
|
7323 DL_OPT_TRAP_POLICER_BURST
);
7327 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7330 static int cmd_trap_policer(struct dl
*dl
)
7332 if (dl_argv_match(dl
, "help")) {
7335 } else if (dl_argv_match(dl
, "show") ||
7336 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7338 return cmd_trap_policer_show(dl
);
7339 } else if (dl_argv_match(dl
, "set")) {
7341 return cmd_trap_policer_set(dl
);
7343 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7347 static int cmd_trap(struct dl
*dl
)
7349 if (dl_argv_match(dl
, "help")) {
7352 } else if (dl_argv_match(dl
, "show") ||
7353 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7355 return cmd_trap_show(dl
);
7356 } else if (dl_argv_match(dl
, "set")) {
7358 return cmd_trap_set(dl
);
7359 } else if (dl_argv_match(dl
, "group")) {
7361 return cmd_trap_group(dl
);
7362 } else if (dl_argv_match(dl
, "policer")) {
7364 return cmd_trap_policer(dl
);
7366 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7370 static void help(void)
7372 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7373 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7374 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7375 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7378 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7383 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7386 } else if (dl_argv_match(dl
, "dev")) {
7389 } else if (dl_argv_match(dl
, "port")) {
7391 return cmd_port(dl
);
7392 } else if (dl_argv_match(dl
, "sb")) {
7395 } else if (dl_argv_match(dl
, "monitor")) {
7398 } else if (dl_argv_match(dl
, "dpipe")) {
7400 return cmd_dpipe(dl
);
7401 } else if (dl_argv_match(dl
, "resource")) {
7403 return cmd_resource(dl
);
7404 } else if (dl_argv_match(dl
, "region")) {
7406 return cmd_region(dl
);
7407 } else if (dl_argv_match(dl
, "health")) {
7409 return cmd_health(dl
);
7410 } else if (dl_argv_match(dl
, "trap")) {
7412 return cmd_trap(dl
);
7414 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7418 static int dl_init(struct dl
*dl
)
7422 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7424 pr_err("Failed to connect to devlink Netlink\n");
7428 err
= ifname_map_init(dl
);
7430 pr_err("Failed to create index map\n");
7431 goto err_ifname_map_create
;
7433 new_json_obj_plain(dl
->json_output
);
7436 err_ifname_map_create
:
7437 mnlg_socket_close(dl
->nlg
);
7441 static void dl_fini(struct dl
*dl
)
7443 delete_json_obj_plain();
7444 ifname_map_fini(dl
);
7445 mnlg_socket_close(dl
->nlg
);
7448 static struct dl
*dl_alloc(void)
7452 dl
= calloc(1, sizeof(*dl
));
7458 static void dl_free(struct dl
*dl
)
7463 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7467 int ret
= EXIT_SUCCESS
;
7469 if (name
&& strcmp(name
, "-") != 0) {
7470 if (freopen(name
, "r", stdin
) == NULL
) {
7472 "Cannot open file \"%s\" for reading: %s\n",
7473 name
, strerror(errno
));
7474 return EXIT_FAILURE
;
7479 while (getcmdline(&line
, &len
, stdin
) != -1) {
7483 largc
= makeargs(line
, largv
, 100);
7485 continue; /* blank line */
7487 if (dl_cmd(dl
, largc
, largv
)) {
7488 fprintf(stderr
, "Command failed %s:%d\n",
7502 int main(int argc
, char **argv
)
7504 static const struct option long_options
[] = {
7505 { "Version", no_argument
, NULL
, 'V' },
7506 { "force", no_argument
, NULL
, 'f' },
7507 { "batch", required_argument
, NULL
, 'b' },
7508 { "no-nice-names", no_argument
, NULL
, 'n' },
7509 { "json", no_argument
, NULL
, 'j' },
7510 { "pretty", no_argument
, NULL
, 'p' },
7511 { "verbose", no_argument
, NULL
, 'v' },
7512 { "statistics", no_argument
, NULL
, 's' },
7513 { "Netns", required_argument
, NULL
, 'N' },
7514 { NULL
, 0, NULL
, 0 }
7516 const char *batch_file
= NULL
;
7525 pr_err("Failed to allocate memory for devlink\n");
7526 return EXIT_FAILURE
;
7529 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7530 long_options
, NULL
)) >= 0) {
7534 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7541 batch_file
= optarg
;
7544 dl
->no_nice_names
= true;
7547 dl
->json_output
= true;
7559 if (netns_switch(optarg
)) {
7565 pr_err("Unknown option.\n");
7582 err
= dl_batch(dl
, batch_file
, force
);
7584 err
= dl_cmd(dl
, argc
, argv
);