2 * devlink.c Devlink tool
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
22 #include <sys/sysinfo.h>
23 #define _LINUX_SYSINFO_H /* avoid collision with musl header */
24 #include <linux/genetlink.h>
25 #include <linux/devlink.h>
26 #include <libmnl/libmnl.h>
27 #include <netinet/ether.h>
28 #include <sys/types.h>
33 #include "json_writer.h"
35 #include "namespace.h"
37 #define ESWITCH_MODE_LEGACY "legacy"
38 #define ESWITCH_MODE_SWITCHDEV "switchdev"
39 #define ESWITCH_INLINE_MODE_NONE "none"
40 #define ESWITCH_INLINE_MODE_LINK "link"
41 #define ESWITCH_INLINE_MODE_NETWORK "network"
42 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
44 #define PARAM_CMODE_RUNTIME_STR "runtime"
45 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
46 #define PARAM_CMODE_PERMANENT_STR "permanent"
47 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
49 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
50 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
51 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
53 static int g_new_line_count
;
54 static int g_indent_level
;
55 static bool g_indent_newline
;
57 #define INDENT_STR_STEP 2
58 #define INDENT_STR_MAXLEN 32
59 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
61 static void __attribute__((format(printf
, 1, 2)))
62 pr_err(const char *fmt
, ...)
67 vfprintf(stderr
, fmt
, ap
);
71 static void __attribute__((format(printf
, 1, 2)))
72 pr_out(const char *fmt
, ...)
76 if (g_indent_newline
) {
77 printf("%s", g_indent_str
);
78 g_indent_newline
= false;
86 static void __attribute__((format(printf
, 2, 3)))
87 pr_out_sp(unsigned int num
, const char *fmt
, ...)
93 ret
= vprintf(fmt
, ap
);
97 printf("%*s", num
- ret
, "");
98 g_new_line_count
= 0; \
101 static void __attribute__((format(printf
, 1, 2)))
102 pr_out_tty(const char *fmt
, ...)
106 if (!isatty(STDOUT_FILENO
))
113 static void __pr_out_indent_inc(void)
115 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
117 g_indent_level
+= INDENT_STR_STEP
;
118 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
119 g_indent_str
[g_indent_level
] = '\0';
122 static void __pr_out_indent_dec(void)
124 if (g_indent_level
- INDENT_STR_STEP
< 0)
126 g_indent_level
-= INDENT_STR_STEP
;
127 g_indent_str
[g_indent_level
] = '\0';
130 static void __pr_out_newline(void)
132 if (g_new_line_count
< 1) {
134 g_indent_newline
= true;
139 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
140 mnl_cb_t data_cb
, void *data
)
144 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
146 pr_err("devlink answers: %s\n", strerror(errno
));
152 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
153 const struct nlmsghdr
*nlh
)
157 err
= mnlg_socket_send(nlg
, nlh
);
159 pr_err("Failed to call mnlg_socket_send\n");
165 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
166 const struct nlmsghdr
*nlh
,
167 mnl_cb_t data_cb
, void *data
)
171 err
= _mnlg_socket_send(nlg
, nlh
);
174 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
177 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
178 const char *group_name
)
182 err
= mnlg_socket_group_add(nlg
, group_name
);
184 pr_err("Failed to call mnlg_socket_group_add\n");
191 struct list_head list
;
198 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
199 const char *dev_name
,
203 struct ifname_map
*ifname_map
;
205 ifname_map
= calloc(1, sizeof(*ifname_map
));
208 ifname_map
->bus_name
= strdup(bus_name
);
209 ifname_map
->dev_name
= strdup(dev_name
);
210 ifname_map
->port_index
= port_index
;
211 ifname_map
->ifname
= strdup(ifname
);
212 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
213 !ifname_map
->ifname
) {
214 free(ifname_map
->ifname
);
215 free(ifname_map
->dev_name
);
216 free(ifname_map
->bus_name
);
223 static void ifname_map_free(struct ifname_map
*ifname_map
)
225 free(ifname_map
->ifname
);
226 free(ifname_map
->dev_name
);
227 free(ifname_map
->bus_name
);
231 #define DL_OPT_HANDLE BIT(0)
232 #define DL_OPT_HANDLEP BIT(1)
233 #define DL_OPT_PORT_TYPE BIT(2)
234 #define DL_OPT_PORT_COUNT BIT(3)
235 #define DL_OPT_SB BIT(4)
236 #define DL_OPT_SB_POOL BIT(5)
237 #define DL_OPT_SB_SIZE BIT(6)
238 #define DL_OPT_SB_TYPE BIT(7)
239 #define DL_OPT_SB_THTYPE BIT(8)
240 #define DL_OPT_SB_TH BIT(9)
241 #define DL_OPT_SB_TC BIT(10)
242 #define DL_OPT_ESWITCH_MODE BIT(11)
243 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
244 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
245 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
246 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
247 #define DL_OPT_RESOURCE_PATH BIT(16)
248 #define DL_OPT_RESOURCE_SIZE BIT(17)
249 #define DL_OPT_PARAM_NAME BIT(18)
250 #define DL_OPT_PARAM_VALUE BIT(19)
251 #define DL_OPT_PARAM_CMODE BIT(20)
252 #define DL_OPT_HANDLE_REGION BIT(21)
253 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
254 #define DL_OPT_REGION_ADDRESS BIT(23)
255 #define DL_OPT_REGION_LENGTH BIT(24)
256 #define DL_OPT_FLASH_FILE_NAME BIT(25)
257 #define DL_OPT_FLASH_COMPONENT BIT(26)
258 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
259 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
260 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
261 #define DL_OPT_TRAP_NAME BIT(30)
262 #define DL_OPT_TRAP_ACTION BIT(31)
263 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
264 #define DL_OPT_NETNS BIT(33)
265 #define DL_OPT_TRAP_POLICER_ID BIT(34)
266 #define DL_OPT_TRAP_POLICER_RATE BIT(35)
267 #define DL_OPT_TRAP_POLICER_BURST BIT(36)
270 uint64_t present
; /* flags of present items */
274 enum devlink_port_type port_type
;
277 uint16_t sb_pool_index
;
278 uint32_t sb_pool_size
;
279 enum devlink_sb_pool_type sb_pool_type
;
280 enum devlink_sb_threshold_type sb_pool_thtype
;
281 uint32_t sb_threshold
;
282 uint16_t sb_tc_index
;
283 enum devlink_eswitch_mode eswitch_mode
;
284 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
285 const char *dpipe_table_name
;
286 bool dpipe_counters_enable
;
287 bool eswitch_encap_mode
;
288 const char *resource_path
;
289 uint64_t resource_size
;
290 uint32_t resource_id
;
291 bool resource_id_valid
;
292 const char *param_name
;
293 const char *param_value
;
294 enum devlink_param_cmode cmode
;
296 uint32_t region_snapshot_id
;
297 uint64_t region_address
;
298 uint64_t region_length
;
299 const char *flash_file_name
;
300 const char *flash_component
;
301 const char *reporter_name
;
302 uint64_t reporter_graceful_period
;
303 bool reporter_auto_recover
;
304 const char *trap_name
;
305 const char *trap_group_name
;
306 enum devlink_trap_action trap_action
;
309 uint32_t trap_policer_id
;
310 uint64_t trap_policer_rate
;
311 uint64_t trap_policer_burst
;
315 struct mnlg_socket
*nlg
;
316 struct list_head ifname_map_list
;
333 static int dl_argc(struct dl
*dl
)
338 static char *dl_argv(struct dl
*dl
)
340 if (dl_argc(dl
) == 0)
345 static void dl_arg_inc(struct dl
*dl
)
347 if (dl_argc(dl
) == 0)
353 static void dl_arg_dec(struct dl
*dl
)
359 static char *dl_argv_next(struct dl
*dl
)
363 if (dl_argc(dl
) == 0)
371 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
373 if (index
>= dl_argc(dl
))
375 return dl
->argv
[index
];
378 static int strcmpx(const char *str1
, const char *str2
)
380 if (strlen(str1
) > strlen(str2
))
382 return strncmp(str1
, str2
, strlen(str1
));
385 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
387 if (dl_argc(dl
) == 0)
389 return strcmpx(dl_argv(dl
), pattern
) == 0;
392 static bool dl_no_arg(struct dl
*dl
)
394 return dl_argc(dl
) == 0;
397 static void __pr_out_indent_newline(struct dl
*dl
)
399 if (!g_indent_newline
&& !dl
->json_output
)
403 static void check_indent_newline(struct dl
*dl
)
405 __pr_out_indent_newline(dl
);
407 if (g_indent_newline
&& !is_json_context()) {
408 printf("%s", g_indent_str
);
409 g_indent_newline
= false;
411 g_new_line_count
= 0;
414 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
415 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
416 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
417 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
418 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
419 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
420 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
421 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
422 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
423 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
424 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
425 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
426 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
427 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
428 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
429 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
430 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
431 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
432 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
433 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
434 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
435 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
436 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
437 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
438 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
439 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
440 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
441 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
442 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
443 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
444 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
445 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
446 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
447 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
448 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
449 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
450 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
451 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
452 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
453 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
454 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
455 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
456 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
457 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
458 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
459 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
460 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
461 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
462 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
463 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
464 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
465 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
466 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
467 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
468 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
469 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
470 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
471 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
472 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
473 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
474 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
475 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
476 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
477 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
478 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
479 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
480 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
481 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
482 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
483 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
484 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
485 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
486 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
487 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
488 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
489 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
490 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
491 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
492 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
493 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
494 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
495 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
496 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
497 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
498 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
499 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
500 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
501 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
502 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
503 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
504 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
505 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
506 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
507 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
508 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
509 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
510 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
511 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
512 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
513 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
514 [DEVLINK_ATTR_TRAP_POLICER_ID
] = MNL_TYPE_U32
,
515 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = MNL_TYPE_U64
,
516 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = MNL_TYPE_U64
,
519 static const enum mnl_attr_data_type
520 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
521 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
522 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
523 [DEVLINK_ATTR_STATS_RX_DROPPED
] = MNL_TYPE_U64
,
526 static int attr_cb(const struct nlattr
*attr
, void *data
)
528 const struct nlattr
**tb
= data
;
531 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
534 type
= mnl_attr_get_type(attr
);
535 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
542 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
544 const struct nlattr
**tb
= data
;
547 /* Allow the tool to work on top of newer kernels that might contain
550 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
553 type
= mnl_attr_get_type(attr
);
554 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
561 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
563 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
564 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
565 struct dl
*dl
= data
;
566 struct ifname_map
*ifname_map
;
567 const char *bus_name
;
568 const char *dev_name
;
569 uint32_t port_ifindex
;
570 const char *port_ifname
;
572 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
573 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
574 !tb
[DEVLINK_ATTR_PORT_INDEX
])
577 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
580 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
581 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
582 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
583 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
584 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
585 port_ifindex
, port_ifname
);
588 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
593 static void ifname_map_fini(struct dl
*dl
)
595 struct ifname_map
*ifname_map
, *tmp
;
597 list_for_each_entry_safe(ifname_map
, tmp
,
598 &dl
->ifname_map_list
, list
) {
599 list_del(&ifname_map
->list
);
600 ifname_map_free(ifname_map
);
604 static int ifname_map_init(struct dl
*dl
)
606 struct nlmsghdr
*nlh
;
609 INIT_LIST_HEAD(&dl
->ifname_map_list
);
611 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
612 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
614 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
622 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
623 char **p_bus_name
, char **p_dev_name
,
624 uint32_t *p_port_index
)
626 struct ifname_map
*ifname_map
;
628 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
629 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
630 *p_bus_name
= ifname_map
->bus_name
;
631 *p_dev_name
= ifname_map
->dev_name
;
632 *p_port_index
= ifname_map
->port_index
;
639 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
640 const char *dev_name
, uint32_t port_index
,
643 struct ifname_map
*ifname_map
;
645 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
646 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
647 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
648 port_index
== ifname_map
->port_index
) {
649 *p_ifname
= ifname_map
->ifname
;
656 static unsigned int strslashcount(char *str
)
658 unsigned int count
= 0;
661 while ((pos
= strchr(pos
, '/'))) {
668 static int strslashrsplit(char *str
, char **before
, char **after
)
672 slash
= strrchr(str
, '/');
681 static int strtouint64_t(const char *str
, uint64_t *p_val
)
684 unsigned long long int val
;
686 val
= strtoull(str
, &endptr
, 10);
687 if (endptr
== str
|| *endptr
!= '\0')
695 static int strtouint32_t(const char *str
, uint32_t *p_val
)
698 unsigned long int val
;
700 val
= strtoul(str
, &endptr
, 10);
701 if (endptr
== str
|| *endptr
!= '\0')
709 static int strtouint16_t(const char *str
, uint16_t *p_val
)
712 unsigned long int val
;
714 val
= strtoul(str
, &endptr
, 10);
715 if (endptr
== str
|| *endptr
!= '\0')
723 static int strtouint8_t(const char *str
, uint8_t *p_val
)
726 unsigned long int val
;
728 val
= strtoul(str
, &endptr
, 10);
729 if (endptr
== str
|| *endptr
!= '\0')
737 static int strtobool(const char *str
, bool *p_val
)
741 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
743 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
751 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
753 strslashrsplit(str
, p_bus_name
, p_dev_name
);
757 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
759 char *str
= dl_argv_next(dl
);
762 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
765 if (strslashcount(str
) != 1) {
766 pr_err("Wrong devlink identification string format.\n");
767 pr_err("Expected \"bus_name/dev_name\".\n");
770 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
773 static int __dl_argv_handle_port(char *str
,
774 char **p_bus_name
, char **p_dev_name
,
775 uint32_t *p_port_index
)
781 err
= strslashrsplit(str
, &handlestr
, &portstr
);
783 pr_err("Port identification \"%s\" is invalid\n", str
);
786 err
= strtouint32_t(portstr
, p_port_index
);
788 pr_err("Port index \"%s\" is not a number or not within range\n",
792 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
794 pr_err("Port identification \"%s\" is invalid\n", str
);
800 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
801 char **p_bus_name
, char **p_dev_name
,
802 uint32_t *p_port_index
)
806 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
809 pr_err("Netdevice \"%s\" not found\n", str
);
815 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
816 char **p_dev_name
, uint32_t *p_port_index
)
818 char *str
= dl_argv_next(dl
);
819 unsigned int slash_count
;
822 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
825 slash_count
= strslashcount(str
);
826 switch (slash_count
) {
828 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
829 p_dev_name
, p_port_index
);
831 return __dl_argv_handle_port(str
, p_bus_name
,
832 p_dev_name
, p_port_index
);
834 pr_err("Wrong port identification string format.\n");
835 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
840 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
841 char **p_dev_name
, uint32_t *p_port_index
,
842 uint64_t *p_handle_bit
)
844 char *str
= dl_argv_next(dl
);
845 unsigned int slash_count
;
849 pr_err("One of following identifications expected:\n"
850 "Devlink identification (\"bus_name/dev_name\")\n"
851 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
854 slash_count
= strslashcount(str
);
855 if (slash_count
== 1) {
856 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
859 *p_handle_bit
= DL_OPT_HANDLE
;
860 } else if (slash_count
== 2) {
861 err
= __dl_argv_handle_port(str
, p_bus_name
,
862 p_dev_name
, p_port_index
);
865 *p_handle_bit
= DL_OPT_HANDLEP
;
866 } else if (slash_count
== 0) {
867 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
868 p_dev_name
, p_port_index
);
871 *p_handle_bit
= DL_OPT_HANDLEP
;
873 pr_err("Wrong port identification string format.\n");
874 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
880 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
881 char **p_dev_name
, char **p_region
)
886 err
= strslashrsplit(str
, &handlestr
, p_region
);
888 pr_err("Region identification \"%s\" is invalid\n", str
);
891 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
893 pr_err("Region identification \"%s\" is invalid\n", str
);
899 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
900 char **p_dev_name
, char **p_region
)
902 char *str
= dl_argv_next(dl
);
903 unsigned int slash_count
;
906 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
910 slash_count
= strslashcount(str
);
911 if (slash_count
!= 2) {
912 pr_err("Wrong region identification string format.\n");
913 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
917 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
920 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
922 char *str
= dl_argv_next(dl
);
926 pr_err("Unsigned number argument expected\n");
930 err
= strtouint64_t(str
, p_val
);
932 pr_err("\"%s\" is not a number or not within range\n", str
);
938 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
940 char *str
= dl_argv_next(dl
);
944 pr_err("Unsigned number argument expected\n");
948 err
= strtouint32_t(str
, p_val
);
950 pr_err("\"%s\" is not a number or not within range\n", str
);
956 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
958 char *str
= dl_argv_next(dl
);
962 pr_err("Unsigned number argument expected\n");
966 err
= strtouint16_t(str
, p_val
);
968 pr_err("\"%s\" is not a number or not within range\n", str
);
974 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
976 char *str
= dl_argv_next(dl
);
980 pr_err("Boolean argument expected\n");
984 err
= strtobool(str
, p_val
);
986 pr_err("\"%s\" is not a valid boolean value\n", str
);
992 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
994 const char *str
= dl_argv_next(dl
);
997 pr_err("String parameter expected\n");
1004 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
1006 if (strcmp(typestr
, "auto") == 0) {
1007 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
1008 } else if (strcmp(typestr
, "eth") == 0) {
1009 *p_type
= DEVLINK_PORT_TYPE_ETH
;
1010 } else if (strcmp(typestr
, "ib") == 0) {
1011 *p_type
= DEVLINK_PORT_TYPE_IB
;
1013 pr_err("Unknown port type \"%s\"\n", typestr
);
1019 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
1021 if (strcmp(typestr
, "ingress") == 0) {
1022 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
1023 } else if (strcmp(typestr
, "egress") == 0) {
1024 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
1026 pr_err("Unknown pool type \"%s\"\n", typestr
);
1032 static int threshold_type_get(const char *typestr
,
1033 enum devlink_sb_threshold_type
*p_type
)
1035 if (strcmp(typestr
, "static") == 0) {
1036 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1037 } else if (strcmp(typestr
, "dynamic") == 0) {
1038 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1040 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1046 static int eswitch_mode_get(const char *typestr
,
1047 enum devlink_eswitch_mode
*p_mode
)
1049 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1050 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1051 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1052 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1054 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1060 static int eswitch_inline_mode_get(const char *typestr
,
1061 enum devlink_eswitch_inline_mode
*p_mode
)
1063 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1064 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1065 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1066 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1067 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1068 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1069 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1070 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1072 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1078 static int dpipe_counters_enable_get(const char *typestr
,
1079 bool *counters_enable
)
1081 if (strcmp(typestr
, "enable") == 0) {
1082 *counters_enable
= 1;
1083 } else if (strcmp(typestr
, "disable") == 0) {
1084 *counters_enable
= 0;
1086 pr_err("Unknown counter_state \"%s\"\n", typestr
);
1092 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
1094 if (strcmp(typestr
, "enable") == 0) {
1096 } else if (strcmp(typestr
, "disable") == 0) {
1099 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1105 static int param_cmode_get(const char *cmodestr
,
1106 enum devlink_param_cmode
*cmode
)
1108 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1109 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1110 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1111 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1112 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1113 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1115 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1121 static int trap_action_get(const char *actionstr
,
1122 enum devlink_trap_action
*p_action
)
1124 if (strcmp(actionstr
, "drop") == 0) {
1125 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1126 } else if (strcmp(actionstr
, "trap") == 0) {
1127 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1129 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1135 struct dl_args_metadata
{
1137 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1140 static const struct dl_args_metadata dl_args_required
[] = {
1141 {DL_OPT_PORT_TYPE
, "Port type not set."},
1142 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1143 {DL_OPT_SB_POOL
, "Pool index option expected."},
1144 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1145 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1146 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1147 {DL_OPT_SB_TH
, "Threshold option expected."},
1148 {DL_OPT_SB_TC
, "TC index option expected."},
1149 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1150 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1151 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1152 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1153 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1154 {DL_OPT_RESOURCE_PATH
, "Resource path expected."},
1155 {DL_OPT_RESOURCE_SIZE
, "Resource size expected."},
1156 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1157 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1158 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1159 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1160 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1161 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1162 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1163 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1164 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1167 static int dl_args_finding_required_validate(uint64_t o_required
,
1173 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1174 o_flag
= dl_args_required
[i
].o_flag
;
1175 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1176 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1180 if (o_required
& ~o_found
) {
1181 pr_err("BUG: unknown argument required but not found\n");
1187 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1188 uint64_t o_optional
)
1190 struct dl_opts
*opts
= &dl
->opts
;
1191 uint64_t o_all
= o_required
| o_optional
;
1192 uint64_t o_found
= 0;
1195 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1196 uint64_t handle_bit
;
1198 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1199 &opts
->port_index
, &handle_bit
);
1202 o_required
&= ~(DL_OPT_HANDLE
| DL_OPT_HANDLEP
) | handle_bit
;
1203 o_found
|= handle_bit
;
1204 } else if (o_required
& DL_OPT_HANDLE
) {
1205 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1208 o_found
|= DL_OPT_HANDLE
;
1209 } else if (o_required
& DL_OPT_HANDLEP
) {
1210 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1214 o_found
|= DL_OPT_HANDLEP
;
1215 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1216 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1218 &opts
->region_name
);
1221 o_found
|= DL_OPT_HANDLE_REGION
;
1224 while (dl_argc(dl
)) {
1225 if (dl_argv_match(dl
, "type") &&
1226 (o_all
& DL_OPT_PORT_TYPE
)) {
1227 const char *typestr
;
1230 err
= dl_argv_str(dl
, &typestr
);
1233 err
= port_type_get(typestr
, &opts
->port_type
);
1236 o_found
|= DL_OPT_PORT_TYPE
;
1237 } else if (dl_argv_match(dl
, "count") &&
1238 (o_all
& DL_OPT_PORT_COUNT
)) {
1240 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1243 o_found
|= DL_OPT_PORT_COUNT
;
1244 } else if (dl_argv_match(dl
, "sb") &&
1245 (o_all
& DL_OPT_SB
)) {
1247 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1250 o_found
|= DL_OPT_SB
;
1251 } else if (dl_argv_match(dl
, "pool") &&
1252 (o_all
& DL_OPT_SB_POOL
)) {
1254 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1257 o_found
|= DL_OPT_SB_POOL
;
1258 } else if (dl_argv_match(dl
, "size") &&
1259 (o_all
& DL_OPT_SB_SIZE
)) {
1261 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1264 o_found
|= DL_OPT_SB_SIZE
;
1265 } else if (dl_argv_match(dl
, "type") &&
1266 (o_all
& DL_OPT_SB_TYPE
)) {
1267 const char *typestr
;
1270 err
= dl_argv_str(dl
, &typestr
);
1273 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1276 o_found
|= DL_OPT_SB_TYPE
;
1277 } else if (dl_argv_match(dl
, "thtype") &&
1278 (o_all
& DL_OPT_SB_THTYPE
)) {
1279 const char *typestr
;
1282 err
= dl_argv_str(dl
, &typestr
);
1285 err
= threshold_type_get(typestr
,
1286 &opts
->sb_pool_thtype
);
1289 o_found
|= DL_OPT_SB_THTYPE
;
1290 } else if (dl_argv_match(dl
, "th") &&
1291 (o_all
& DL_OPT_SB_TH
)) {
1293 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1296 o_found
|= DL_OPT_SB_TH
;
1297 } else if (dl_argv_match(dl
, "tc") &&
1298 (o_all
& DL_OPT_SB_TC
)) {
1300 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1303 o_found
|= DL_OPT_SB_TC
;
1304 } else if (dl_argv_match(dl
, "mode") &&
1305 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1306 const char *typestr
;
1309 err
= dl_argv_str(dl
, &typestr
);
1312 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1315 o_found
|= DL_OPT_ESWITCH_MODE
;
1316 } else if (dl_argv_match(dl
, "inline-mode") &&
1317 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1318 const char *typestr
;
1321 err
= dl_argv_str(dl
, &typestr
);
1324 err
= eswitch_inline_mode_get(
1325 typestr
, &opts
->eswitch_inline_mode
);
1328 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1329 } else if (dl_argv_match(dl
, "name") &&
1330 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1332 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1335 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1336 } else if (dl_argv_match(dl
, "counters") &&
1337 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1338 const char *typestr
;
1341 err
= dl_argv_str(dl
, &typestr
);
1344 err
= dpipe_counters_enable_get(typestr
,
1345 &opts
->dpipe_counters_enable
);
1348 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1349 } else if (dl_argv_match(dl
, "encap") &&
1350 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1351 const char *typestr
;
1354 err
= dl_argv_str(dl
, &typestr
);
1357 err
= eswitch_encap_mode_get(typestr
,
1358 &opts
->eswitch_encap_mode
);
1361 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1362 } else if (dl_argv_match(dl
, "path") &&
1363 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1365 err
= dl_argv_str(dl
, &opts
->resource_path
);
1368 o_found
|= DL_OPT_RESOURCE_PATH
;
1369 } else if (dl_argv_match(dl
, "size") &&
1370 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1372 err
= dl_argv_uint64_t(dl
, &opts
->resource_size
);
1375 o_found
|= DL_OPT_RESOURCE_SIZE
;
1376 } else if (dl_argv_match(dl
, "name") &&
1377 (o_all
& DL_OPT_PARAM_NAME
)) {
1379 err
= dl_argv_str(dl
, &opts
->param_name
);
1382 o_found
|= DL_OPT_PARAM_NAME
;
1383 } else if (dl_argv_match(dl
, "value") &&
1384 (o_all
& DL_OPT_PARAM_VALUE
)) {
1386 err
= dl_argv_str(dl
, &opts
->param_value
);
1389 o_found
|= DL_OPT_PARAM_VALUE
;
1390 } else if (dl_argv_match(dl
, "cmode") &&
1391 (o_all
& DL_OPT_PARAM_CMODE
)) {
1392 const char *cmodestr
;
1395 err
= dl_argv_str(dl
, &cmodestr
);
1398 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1401 o_found
|= DL_OPT_PARAM_CMODE
;
1402 } else if (dl_argv_match(dl
, "snapshot") &&
1403 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1405 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1408 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1409 } else if (dl_argv_match(dl
, "address") &&
1410 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1412 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1415 o_found
|= DL_OPT_REGION_ADDRESS
;
1416 } else if (dl_argv_match(dl
, "length") &&
1417 (o_all
& DL_OPT_REGION_LENGTH
)) {
1419 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1422 o_found
|= DL_OPT_REGION_LENGTH
;
1423 } else if (dl_argv_match(dl
, "file") &&
1424 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1426 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1429 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1430 } else if (dl_argv_match(dl
, "component") &&
1431 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1433 err
= dl_argv_str(dl
, &opts
->flash_component
);
1436 o_found
|= DL_OPT_FLASH_COMPONENT
;
1437 } else if (dl_argv_match(dl
, "reporter") &&
1438 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1440 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1443 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1444 } else if (dl_argv_match(dl
, "grace_period") &&
1445 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1447 err
= dl_argv_uint64_t(dl
,
1448 &opts
->reporter_graceful_period
);
1451 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1452 } else if (dl_argv_match(dl
, "auto_recover") &&
1453 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1455 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1458 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1459 } else if (dl_argv_match(dl
, "trap") &&
1460 (o_all
& DL_OPT_TRAP_NAME
)) {
1462 err
= dl_argv_str(dl
, &opts
->trap_name
);
1465 o_found
|= DL_OPT_TRAP_NAME
;
1466 } else if (dl_argv_match(dl
, "group") &&
1467 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1469 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1472 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1473 } else if (dl_argv_match(dl
, "action") &&
1474 (o_all
& DL_OPT_TRAP_ACTION
)) {
1475 const char *actionstr
;
1478 err
= dl_argv_str(dl
, &actionstr
);
1481 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1484 o_found
|= DL_OPT_TRAP_ACTION
;
1485 } else if (dl_argv_match(dl
, "netns") &&
1486 (o_all
& DL_OPT_NETNS
)) {
1487 const char *netns_str
;
1490 err
= dl_argv_str(dl
, &netns_str
);
1493 opts
->netns
= netns_get_fd(netns_str
);
1494 if ((int)opts
->netns
< 0) {
1496 err
= dl_argv_uint32_t(dl
, &opts
->netns
);
1499 opts
->netns_is_pid
= true;
1501 o_found
|= DL_OPT_NETNS
;
1502 } else if (dl_argv_match(dl
, "policer") &&
1503 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1505 err
= dl_argv_uint32_t(dl
, &opts
->trap_policer_id
);
1508 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1509 } else if (dl_argv_match(dl
, "nopolicer") &&
1510 (o_all
& DL_OPT_TRAP_POLICER_ID
)) {
1512 opts
->trap_policer_id
= 0;
1513 o_found
|= DL_OPT_TRAP_POLICER_ID
;
1514 } else if (dl_argv_match(dl
, "rate") &&
1515 (o_all
& DL_OPT_TRAP_POLICER_RATE
)) {
1517 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_rate
);
1520 o_found
|= DL_OPT_TRAP_POLICER_RATE
;
1521 } else if (dl_argv_match(dl
, "burst") &&
1522 (o_all
& DL_OPT_TRAP_POLICER_BURST
)) {
1524 err
= dl_argv_uint64_t(dl
, &opts
->trap_policer_burst
);
1527 o_found
|= DL_OPT_TRAP_POLICER_BURST
;
1529 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1534 opts
->present
= o_found
;
1536 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1538 opts
->present
|= DL_OPT_SB
;
1541 return dl_args_finding_required_validate(o_required
, o_found
);
1544 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1546 struct dl_opts
*opts
= &dl
->opts
;
1548 if (opts
->present
& DL_OPT_HANDLE
) {
1549 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1550 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1551 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1552 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1553 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1554 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1556 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1557 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1558 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1559 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1562 if (opts
->present
& DL_OPT_PORT_TYPE
)
1563 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1565 if (opts
->present
& DL_OPT_PORT_COUNT
)
1566 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1568 if (opts
->present
& DL_OPT_SB
)
1569 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1571 if (opts
->present
& DL_OPT_SB_POOL
)
1572 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1573 opts
->sb_pool_index
);
1574 if (opts
->present
& DL_OPT_SB_SIZE
)
1575 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1576 opts
->sb_pool_size
);
1577 if (opts
->present
& DL_OPT_SB_TYPE
)
1578 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1579 opts
->sb_pool_type
);
1580 if (opts
->present
& DL_OPT_SB_THTYPE
)
1581 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1582 opts
->sb_pool_thtype
);
1583 if (opts
->present
& DL_OPT_SB_TH
)
1584 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1585 opts
->sb_threshold
);
1586 if (opts
->present
& DL_OPT_SB_TC
)
1587 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1589 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1590 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1591 opts
->eswitch_mode
);
1592 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1593 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1594 opts
->eswitch_inline_mode
);
1595 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1596 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1597 opts
->dpipe_table_name
);
1598 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1599 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1600 opts
->dpipe_counters_enable
);
1601 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1602 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1603 opts
->eswitch_encap_mode
);
1604 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1605 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1607 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1608 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1609 opts
->resource_size
);
1610 if (opts
->present
& DL_OPT_PARAM_NAME
)
1611 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1613 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1614 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1616 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1617 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1618 opts
->region_snapshot_id
);
1619 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1620 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1621 opts
->region_address
);
1622 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1623 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1624 opts
->region_length
);
1625 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1626 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1627 opts
->flash_file_name
);
1628 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1629 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1630 opts
->flash_component
);
1631 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1632 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1633 opts
->reporter_name
);
1634 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1635 mnl_attr_put_u64(nlh
,
1636 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1637 opts
->reporter_graceful_period
);
1638 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1639 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1640 opts
->reporter_auto_recover
);
1641 if (opts
->present
& DL_OPT_TRAP_NAME
)
1642 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1644 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1645 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1646 opts
->trap_group_name
);
1647 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1648 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1650 if (opts
->present
& DL_OPT_NETNS
)
1651 mnl_attr_put_u32(nlh
,
1652 opts
->netns_is_pid
? DEVLINK_ATTR_NETNS_PID
:
1653 DEVLINK_ATTR_NETNS_FD
,
1655 if (opts
->present
& DL_OPT_TRAP_POLICER_ID
)
1656 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_TRAP_POLICER_ID
,
1657 opts
->trap_policer_id
);
1658 if (opts
->present
& DL_OPT_TRAP_POLICER_RATE
)
1659 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
1660 opts
->trap_policer_rate
);
1661 if (opts
->present
& DL_OPT_TRAP_POLICER_BURST
)
1662 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
1663 opts
->trap_policer_burst
);
1666 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1667 uint64_t o_required
, uint64_t o_optional
)
1671 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1674 dl_opts_put(nlh
, dl
);
1678 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1680 struct dl_opts
*opts
= &dl
->opts
;
1681 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1682 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1683 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1684 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1686 if (opts
->present
& DL_OPT_HANDLE
&&
1687 attr_bus_name
&& attr_dev_name
) {
1688 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1689 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1691 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1692 strcmp(dev_name
, opts
->dev_name
) != 0)
1695 if (opts
->present
& DL_OPT_HANDLEP
&&
1696 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1697 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1698 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1699 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1701 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1702 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1703 port_index
!= opts
->port_index
)
1706 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1707 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1709 if (sb_index
!= opts
->sb_index
)
1715 static void cmd_dev_help(void)
1717 pr_err("Usage: devlink dev show [ DEV ]\n");
1718 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1719 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1720 pr_err(" [ encap { disable | enable } ]\n");
1721 pr_err(" devlink dev eswitch show DEV\n");
1722 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1723 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1724 pr_err(" devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
1725 pr_err(" devlink dev info [ DEV ]\n");
1726 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1729 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1730 const char *dev_name
)
1732 if (!dl
->arr_last
.present
)
1734 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1735 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1738 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1739 const char *dev_name
)
1741 dl
->arr_last
.present
= true;
1742 free(dl
->arr_last
.dev_name
);
1743 free(dl
->arr_last
.bus_name
);
1744 dl
->arr_last
.bus_name
= strdup(bus_name
);
1745 dl
->arr_last
.dev_name
= strdup(dev_name
);
1748 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1749 const char *dev_name
)
1751 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1754 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1755 const char *dev_name
)
1757 return dl
->arr_last
.present
&&
1758 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1761 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1762 bool content
, bool array
)
1764 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1765 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1768 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1770 if (dl
->json_output
) {
1772 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1773 close_json_array(PRINT_JSON
, NULL
);
1774 if (should_arr_last_handle_start(dl
, bus_name
,
1776 open_json_array(PRINT_JSON
, buf
);
1777 open_json_object(NULL
);
1778 arr_last_handle_set(dl
, bus_name
, dev_name
);
1780 open_json_object(NULL
);
1783 open_json_object(buf
);
1787 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1788 __pr_out_indent_dec();
1789 if (should_arr_last_handle_start(dl
, bus_name
,
1791 pr_out("%s%s", buf
, content
? ":" : "");
1793 __pr_out_indent_inc();
1794 arr_last_handle_set(dl
, bus_name
, dev_name
);
1797 pr_out("%s%s", buf
, content
? ":" : "");
1802 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1804 __pr_out_handle_start(dl
, tb
, true, true);
1807 static void pr_out_handle_end(struct dl
*dl
)
1809 if (dl
->json_output
)
1810 close_json_object();
1815 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1817 __pr_out_handle_start(dl
, tb
, false, false);
1818 pr_out_handle_end(dl
);
1821 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1822 const char *dev_name
, uint32_t port_index
)
1824 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1825 dl
->arr_last
.port_index
== port_index
;
1828 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1829 const char *dev_name
, uint32_t port_index
)
1831 arr_last_handle_set(dl
, bus_name
, dev_name
);
1832 dl
->arr_last
.port_index
= port_index
;
1835 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1836 const char *bus_name
,
1837 const char *dev_name
,
1838 uint32_t port_index
)
1840 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1843 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1844 const char *bus_name
,
1845 const char *dev_name
,
1846 uint32_t port_index
)
1848 return dl
->arr_last
.present
&&
1849 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1852 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1853 const char *dev_name
,
1854 uint32_t port_index
, bool try_nice
,
1857 static char buf
[64];
1858 char *ifname
= NULL
;
1860 if (dl
->no_nice_names
|| !try_nice
||
1861 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1862 port_index
, &ifname
) != 0)
1863 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1865 sprintf(buf
, "%s", ifname
);
1867 if (dl
->json_output
) {
1869 if (should_arr_last_port_handle_end(dl
, bus_name
,
1872 close_json_array(PRINT_JSON
, NULL
);
1873 if (should_arr_last_port_handle_start(dl
, bus_name
,
1876 open_json_array(PRINT_JSON
, buf
);
1877 open_json_object(NULL
);
1878 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1881 open_json_object(NULL
);
1884 open_json_object(buf
);
1891 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1893 const char *bus_name
;
1894 const char *dev_name
;
1895 uint32_t port_index
;
1897 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1898 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1899 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1900 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1903 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1905 const char *bus_name
;
1906 const char *dev_name
;
1907 uint32_t port_index
;
1909 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1910 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1911 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1912 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1915 static void pr_out_port_handle_end(struct dl
*dl
)
1917 if (dl
->json_output
)
1918 close_json_object();
1923 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1925 __pr_out_indent_newline(dl
);
1926 if (val
== (uint64_t) -1)
1927 return print_string_name_value(name
, "unlimited");
1929 if (dl
->json_output
)
1930 print_u64(PRINT_JSON
, name
, NULL
, val
);
1932 pr_out("%s %"PRIu64
, name
, val
);
1935 static bool is_binary_eol(int i
)
1940 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1945 if (dl
->json_output
)
1946 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1948 pr_out("%02x ", data
[i
]);
1950 if (!dl
->json_output
&& is_binary_eol(i
))
1953 if (!dl
->json_output
&& !is_binary_eol(i
))
1957 static void pr_out_name(struct dl
*dl
, const char *name
)
1959 __pr_out_indent_newline(dl
);
1960 if (dl
->json_output
)
1961 print_string(PRINT_JSON
, name
, NULL
, NULL
);
1963 pr_out("%s:", name
);
1966 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1968 if (dl
->json_output
) {
1969 print_uint(PRINT_JSON
, "address", NULL
, addr
);
1970 open_json_array(PRINT_JSON
, "data");
1974 static void pr_out_region_chunk_end(struct dl
*dl
)
1976 if (dl
->json_output
)
1977 close_json_array(PRINT_JSON
, NULL
);
1980 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1983 static uint64_t align_val
;
1986 pr_out_region_chunk_start(dl
, addr
);
1988 if (!dl
->json_output
)
1989 if (!(align_val
% 16))
1990 pr_out("%s%016"PRIx64
" ",
1991 align_val
? "\n" : "",
1996 if (dl
->json_output
)
1997 print_int(PRINT_JSON
, NULL
, NULL
, data
[i
]);
1999 pr_out("%02x ", data
[i
]);
2004 pr_out_region_chunk_end(dl
);
2007 static void pr_out_section_start(struct dl
*dl
, const char *name
)
2009 if (dl
->json_output
) {
2010 open_json_object(NULL
);
2011 open_json_object(name
);
2015 static void pr_out_section_end(struct dl
*dl
)
2017 if (dl
->json_output
) {
2018 if (dl
->arr_last
.present
)
2019 close_json_array(PRINT_JSON
, NULL
);
2020 close_json_object();
2021 close_json_object();
2025 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2027 if (dl
->json_output
) {
2028 open_json_array(PRINT_JSON
, name
);
2030 __pr_out_indent_inc();
2032 pr_out("%s:", name
);
2033 __pr_out_indent_inc();
2038 static void pr_out_array_end(struct dl
*dl
)
2040 if (dl
->json_output
) {
2041 close_json_array(PRINT_JSON
, NULL
);
2043 __pr_out_indent_dec();
2044 __pr_out_indent_dec();
2048 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2050 if (dl
->json_output
) {
2051 open_json_object(name
);
2053 __pr_out_indent_inc();
2055 pr_out("%s:", name
);
2056 __pr_out_indent_inc();
2061 static void pr_out_object_end(struct dl
*dl
)
2063 if (dl
->json_output
) {
2064 close_json_object();
2066 __pr_out_indent_dec();
2067 __pr_out_indent_dec();
2071 static void pr_out_entry_start(struct dl
*dl
)
2073 if (dl
->json_output
)
2074 open_json_object(NULL
);
2077 static void pr_out_entry_end(struct dl
*dl
)
2079 if (dl
->json_output
)
2080 close_json_object();
2085 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2087 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2093 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2094 if (err
!= MNL_CB_OK
)
2097 pr_out_object_start(dl
, "stats");
2098 pr_out_object_start(dl
, "rx");
2099 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2100 pr_out_u64(dl
, "bytes",
2101 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2102 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2103 pr_out_u64(dl
, "packets",
2104 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2105 if (tb
[DEVLINK_ATTR_STATS_RX_DROPPED
])
2106 pr_out_u64(dl
, "dropped",
2107 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_DROPPED
]));
2108 pr_out_object_end(dl
);
2109 pr_out_object_end(dl
);
2112 static const char *param_cmode_name(uint8_t cmode
)
2115 case DEVLINK_PARAM_CMODE_RUNTIME
:
2116 return PARAM_CMODE_RUNTIME_STR
;
2117 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2118 return PARAM_CMODE_DRIVERINIT_STR
;
2119 case DEVLINK_PARAM_CMODE_PERMANENT
:
2120 return PARAM_CMODE_PERMANENT_STR
;
2121 default: return "<unknown type>";
2125 static const char *eswitch_mode_name(uint32_t mode
)
2128 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2129 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2130 default: return "<unknown mode>";
2134 static const char *eswitch_inline_mode_name(uint32_t mode
)
2137 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2138 return ESWITCH_INLINE_MODE_NONE
;
2139 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2140 return ESWITCH_INLINE_MODE_LINK
;
2141 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2142 return ESWITCH_INLINE_MODE_NETWORK
;
2143 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2144 return ESWITCH_INLINE_MODE_TRANSPORT
;
2146 return "<unknown mode>";
2150 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2152 __pr_out_handle_start(dl
, tb
, true, false);
2154 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2155 check_indent_newline(dl
);
2156 print_string(PRINT_ANY
, "mode", "mode %s",
2157 eswitch_mode_name(mnl_attr_get_u16(
2158 tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2160 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2161 check_indent_newline(dl
);
2162 print_string(PRINT_ANY
, "inline-mode", "inline-mode %s",
2163 eswitch_inline_mode_name(mnl_attr_get_u8(
2164 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2166 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2167 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2169 check_indent_newline(dl
);
2170 print_string(PRINT_ANY
, "encap", "encap %s",
2171 encap_mode
? "enable" : "disable");
2174 pr_out_handle_end(dl
);
2177 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2179 struct dl
*dl
= data
;
2180 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2181 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2183 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2184 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2185 return MNL_CB_ERROR
;
2186 pr_out_eswitch(dl
, tb
);
2190 static int cmd_dev_eswitch_show(struct dl
*dl
)
2192 struct nlmsghdr
*nlh
;
2195 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2196 NLM_F_REQUEST
| NLM_F_ACK
);
2198 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2202 pr_out_section_start(dl
, "dev");
2203 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2204 pr_out_section_end(dl
);
2208 static int cmd_dev_eswitch_set(struct dl
*dl
)
2210 struct nlmsghdr
*nlh
;
2213 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2214 NLM_F_REQUEST
| NLM_F_ACK
);
2216 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2217 DL_OPT_ESWITCH_MODE
|
2218 DL_OPT_ESWITCH_INLINE_MODE
|
2219 DL_OPT_ESWITCH_ENCAP_MODE
);
2224 if (dl
->opts
.present
== 1) {
2225 pr_err("Need to set at least one option\n");
2229 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2232 static int cmd_dev_eswitch(struct dl
*dl
)
2234 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2237 } else if (dl_argv_match(dl
, "set")) {
2239 return cmd_dev_eswitch_set(dl
);
2240 } else if (dl_argv_match(dl
, "show")) {
2242 return cmd_dev_eswitch_show(dl
);
2244 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2248 struct param_val_conv
{
2254 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2255 uint32_t len
, const char *name
)
2259 for (i
= 0; i
< len
; i
++)
2260 if (!strcmp(param_val_conv
[i
].name
, name
))
2267 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2268 uint32_t len
, const char *name
, const char *vstr
,
2273 for (i
= 0; i
< len
; i
++)
2274 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2275 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2276 *vuint
= param_val_conv
[i
].vuint
;
2284 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2285 uint32_t len
, const char *name
, uint32_t vuint
,
2290 for (i
= 0; i
< len
; i
++)
2291 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2292 param_val_conv
[i
].vuint
== vuint
) {
2293 *vstr
= param_val_conv
[i
].vstr
;
2300 static const struct param_val_conv param_val_conv
[] = {
2302 .name
= "fw_load_policy",
2304 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2307 .name
= "fw_load_policy",
2309 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2312 .name
= "reset_dev_on_drv_probe",
2314 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2317 .name
= "fw_load_policy",
2319 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2322 .name
= "reset_dev_on_drv_probe",
2324 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2327 .name
= "reset_dev_on_drv_probe",
2329 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2332 .name
= "reset_dev_on_drv_probe",
2334 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2338 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2340 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2341 int nla_type
, struct nlattr
*nl
)
2343 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2344 struct nlattr
*val_attr
;
2349 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2350 if (err
!= MNL_CB_OK
)
2353 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2354 (nla_type
!= MNL_TYPE_FLAG
&&
2355 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2358 check_indent_newline(dl
);
2359 print_string(PRINT_ANY
, "cmode", "cmode %s",
2360 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2362 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2364 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2370 err
= param_val_conv_str_get(param_val_conv
,
2373 mnl_attr_get_u8(val_attr
),
2377 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2379 print_uint(PRINT_ANY
, "value", " value %u",
2380 mnl_attr_get_u8(val_attr
));
2385 err
= param_val_conv_str_get(param_val_conv
,
2388 mnl_attr_get_u16(val_attr
),
2392 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2394 print_uint(PRINT_ANY
, "value", " value %u",
2395 mnl_attr_get_u16(val_attr
));
2400 err
= param_val_conv_str_get(param_val_conv
,
2403 mnl_attr_get_u32(val_attr
),
2407 print_string(PRINT_ANY
, "value", " value %s", vstr
);
2409 print_uint(PRINT_ANY
, "value", " value %u",
2410 mnl_attr_get_u32(val_attr
));
2413 case MNL_TYPE_STRING
:
2414 print_string(PRINT_ANY
, "value", " value %s",
2415 mnl_attr_get_str(val_attr
));
2418 print_bool(PRINT_ANY
, "value", " value %s", val_attr
);
2423 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2425 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2426 struct nlattr
*param_value_attr
;
2427 const char *nla_name
;
2431 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2432 if (err
!= MNL_CB_OK
)
2434 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2435 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2436 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2440 pr_out_handle_start_arr(dl
, tb
);
2442 __pr_out_handle_start(dl
, tb
, true, false);
2444 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2446 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2447 check_indent_newline(dl
);
2448 print_string(PRINT_ANY
, "name", "name %s ", nla_name
);
2449 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2450 print_string(PRINT_ANY
, "type", "type %s", "driver-specific");
2452 print_string(PRINT_ANY
, "type", "type %s", "generic");
2454 pr_out_array_start(dl
, "values");
2455 mnl_attr_for_each_nested(param_value_attr
,
2456 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2457 pr_out_entry_start(dl
);
2458 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2459 pr_out_entry_end(dl
);
2461 pr_out_array_end(dl
);
2462 pr_out_handle_end(dl
);
2465 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2467 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2468 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2469 struct dl
*dl
= data
;
2471 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2472 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2473 !tb
[DEVLINK_ATTR_PARAM
])
2474 return MNL_CB_ERROR
;
2475 pr_out_param(dl
, tb
, true);
2491 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2493 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2494 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2495 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2496 struct nlattr
*param_value_attr
;
2497 enum devlink_param_cmode cmode
;
2498 struct param_ctx
*ctx
= data
;
2499 struct dl
*dl
= ctx
->dl
;
2503 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2504 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2505 !tb
[DEVLINK_ATTR_PARAM
])
2506 return MNL_CB_ERROR
;
2508 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2509 if (err
!= MNL_CB_OK
)
2510 return MNL_CB_ERROR
;
2512 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2513 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2514 return MNL_CB_ERROR
;
2516 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2517 mnl_attr_for_each_nested(param_value_attr
,
2518 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2519 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2520 struct nlattr
*val_attr
;
2522 err
= mnl_attr_parse_nested(param_value_attr
,
2523 attr_cb
, nla_value
);
2524 if (err
!= MNL_CB_OK
)
2525 return MNL_CB_ERROR
;
2527 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2528 (nla_type
!= MNL_TYPE_FLAG
&&
2529 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2530 return MNL_CB_ERROR
;
2532 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2533 if (cmode
== dl
->opts
.cmode
) {
2534 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2537 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2540 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2543 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2545 case MNL_TYPE_STRING
:
2546 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2549 ctx
->value
.vbool
= val_attr
? true : false;
2555 ctx
->nla_type
= nla_type
;
2559 static int cmd_dev_param_set(struct dl
*dl
)
2561 struct param_ctx ctx
= {};
2562 struct nlmsghdr
*nlh
;
2570 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2572 DL_OPT_PARAM_VALUE
|
2573 DL_OPT_PARAM_CMODE
, 0);
2577 /* Get value type */
2578 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2579 NLM_F_REQUEST
| NLM_F_ACK
);
2580 dl_opts_put(nlh
, dl
);
2583 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2587 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2588 NLM_F_REQUEST
| NLM_F_ACK
);
2589 dl_opts_put(nlh
, dl
);
2591 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2592 dl
->opts
.param_name
);
2594 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2595 switch (ctx
.nla_type
) {
2598 err
= param_val_conv_uint_get(param_val_conv
,
2600 dl
->opts
.param_name
,
2601 dl
->opts
.param_value
,
2605 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2608 goto err_param_value_parse
;
2609 if (val_u8
== ctx
.value
.vu8
)
2611 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2615 err
= param_val_conv_uint_get(param_val_conv
,
2617 dl
->opts
.param_name
,
2618 dl
->opts
.param_value
,
2622 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2625 goto err_param_value_parse
;
2626 if (val_u16
== ctx
.value
.vu16
)
2628 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2632 err
= param_val_conv_uint_get(param_val_conv
,
2634 dl
->opts
.param_name
,
2635 dl
->opts
.param_value
,
2638 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2640 goto err_param_value_parse
;
2641 if (val_u32
== ctx
.value
.vu32
)
2643 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2646 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2648 goto err_param_value_parse
;
2649 if (val_bool
== ctx
.value
.vbool
)
2652 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2655 case MNL_TYPE_STRING
:
2656 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2657 dl
->opts
.param_value
);
2658 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2662 printf("Value type not supported\n");
2665 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2667 err_param_value_parse
:
2668 pr_err("Value \"%s\" is not a number or not within range\n",
2669 dl
->opts
.param_value
);
2673 static int cmd_dev_param_show(struct dl
*dl
)
2675 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2676 struct nlmsghdr
*nlh
;
2679 if (dl_argc(dl
) == 0)
2680 flags
|= NLM_F_DUMP
;
2682 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2684 if (dl_argc(dl
) > 0) {
2685 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2686 DL_OPT_PARAM_NAME
, 0);
2691 pr_out_section_start(dl
, "param");
2692 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2693 pr_out_section_end(dl
);
2697 static int cmd_dev_param(struct dl
*dl
)
2699 if (dl_argv_match(dl
, "help")) {
2702 } else if (dl_argv_match(dl
, "show") ||
2703 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2705 return cmd_dev_param_show(dl
);
2706 } else if (dl_argv_match(dl
, "set")) {
2708 return cmd_dev_param_set(dl
);
2710 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2713 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2715 struct dl
*dl
= data
;
2716 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2717 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2718 uint8_t reload_failed
= 0;
2720 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2721 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2722 return MNL_CB_ERROR
;
2724 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2725 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2727 if (reload_failed
) {
2728 __pr_out_handle_start(dl
, tb
, true, false);
2729 check_indent_newline(dl
);
2730 print_bool(PRINT_ANY
, "reload_failed", "reload_failed %s", true);
2731 pr_out_handle_end(dl
);
2733 pr_out_handle(dl
, tb
);
2739 static int cmd_dev_show(struct dl
*dl
)
2741 struct nlmsghdr
*nlh
;
2742 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2745 if (dl_argc(dl
) == 0)
2746 flags
|= NLM_F_DUMP
;
2748 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2750 if (dl_argc(dl
) > 0) {
2751 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2756 pr_out_section_start(dl
, "dev");
2757 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2758 pr_out_section_end(dl
);
2762 static void cmd_dev_reload_help(void)
2764 pr_err("Usage: devlink dev reload DEV [ netns { PID | NAME | ID } ]\n");
2767 static int cmd_dev_reload(struct dl
*dl
)
2769 struct nlmsghdr
*nlh
;
2772 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2773 cmd_dev_reload_help();
2777 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2778 NLM_F_REQUEST
| NLM_F_ACK
);
2780 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_NETNS
);
2784 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2787 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2788 const char *name
, int type
)
2790 struct nlattr
*version
;
2792 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2793 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2794 const char *ver_value
;
2795 const char *ver_name
;
2798 if (mnl_attr_get_type(version
) != type
)
2801 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2802 if (err
!= MNL_CB_OK
)
2805 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2806 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2810 pr_out_object_start(dl
, name
);
2814 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2815 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2817 check_indent_newline(dl
);
2818 print_string_name_value(ver_name
, ver_value
);
2819 if (!dl
->json_output
)
2824 pr_out_object_end(dl
);
2827 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2828 struct nlattr
**tb
, bool has_versions
)
2830 __pr_out_handle_start(dl
, tb
, true, false);
2832 __pr_out_indent_inc();
2833 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2834 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2836 if (!dl
->json_output
)
2838 check_indent_newline(dl
);
2839 print_string(PRINT_ANY
, "driver", "driver %s",
2840 mnl_attr_get_str(nla_drv
));
2843 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2844 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2846 if (!dl
->json_output
)
2848 check_indent_newline(dl
);
2849 print_string(PRINT_ANY
, "serial_number", "serial_number %s",
2850 mnl_attr_get_str(nla_sn
));
2852 __pr_out_indent_dec();
2855 pr_out_object_start(dl
, "versions");
2857 pr_out_versions_single(dl
, nlh
, "fixed",
2858 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2859 pr_out_versions_single(dl
, nlh
, "running",
2860 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2861 pr_out_versions_single(dl
, nlh
, "stored",
2862 DEVLINK_ATTR_INFO_VERSION_STORED
);
2864 pr_out_object_end(dl
);
2867 pr_out_handle_end(dl
);
2870 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2872 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2873 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2874 bool has_versions
, has_info
;
2875 struct dl
*dl
= data
;
2877 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2879 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2880 return MNL_CB_ERROR
;
2882 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2883 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2884 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2885 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2886 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2890 pr_out_info(dl
, nlh
, tb
, has_versions
);
2895 static void cmd_dev_info_help(void)
2897 pr_err("Usage: devlink dev info [ DEV ]\n");
2900 static int cmd_dev_info(struct dl
*dl
)
2902 struct nlmsghdr
*nlh
;
2903 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2906 if (dl_argv_match(dl
, "help")) {
2907 cmd_dev_info_help();
2911 if (dl_argc(dl
) == 0)
2912 flags
|= NLM_F_DUMP
;
2914 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2916 if (dl_argc(dl
) > 0) {
2917 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2922 pr_out_section_start(dl
, "info");
2923 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2924 pr_out_section_end(dl
);
2928 static void cmd_dev_flash_help(void)
2930 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2934 struct cmd_dev_flash_status_ctx
{
2937 char *last_component
;
2938 uint8_t not_first
:1,
2944 static int nullstrcmp(const char *str1
, const char *str2
)
2947 return strcmp(str1
, str2
);
2950 return str1
? 1 : -1;
2953 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2955 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2956 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2957 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2958 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2959 const char *component
= NULL
;
2960 uint64_t done
= 0, total
= 0;
2961 const char *msg
= NULL
;
2962 const char *bus_name
;
2963 const char *dev_name
;
2965 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2966 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2969 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2970 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2971 return MNL_CB_ERROR
;
2972 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2973 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2974 if (strcmp(bus_name
, opts
->bus_name
) ||
2975 strcmp(dev_name
, opts
->dev_name
))
2976 return MNL_CB_ERROR
;
2978 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2980 free(ctx
->last_msg
);
2981 free(ctx
->last_component
);
2982 ctx
->received_end
= 1;
2986 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2987 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2988 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2989 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2990 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2991 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2992 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2993 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
2995 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
2996 !nullstrcmp(component
, ctx
->last_component
) &&
2997 ctx
->last_pc
&& ctx
->not_first
) {
2998 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
3003 pr_out("[%s] ", component
);
3004 free(ctx
->last_component
);
3005 ctx
->last_component
= strdup(component
);
3009 free(ctx
->last_msg
);
3010 ctx
->last_msg
= strdup(msg
);
3014 pr_out_tty(" %3lu%%", (done
* 100) / total
);
3025 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
3026 struct mnlg_socket
*nlg_ntf
,
3029 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
3036 for (i
= 0; i
< 3; i
++)
3038 FD_SET(pipe_r
, &fds
[0]);
3040 FD_SET(nlfd
, &fds
[0]);
3044 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3047 pr_err("select() failed\n");
3050 if (FD_ISSET(nlfd
, &fds
[0])) {
3051 err
= _mnlg_socket_recv_run(nlg_ntf
,
3052 cmd_dev_flash_status_cb
, ctx
);
3056 if (FD_ISSET(pipe_r
, &fds
[0])) {
3057 err
= read(pipe_r
, &err2
, sizeof(err2
));
3059 pr_err("Failed to read pipe\n");
3064 ctx
->flash_done
= 1;
3070 static int cmd_dev_flash(struct dl
*dl
)
3072 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3073 struct mnlg_socket
*nlg_ntf
;
3074 struct nlmsghdr
*nlh
;
3080 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3081 cmd_dev_flash_help();
3085 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3086 NLM_F_REQUEST
| NLM_F_ACK
);
3088 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3089 DL_OPT_FLASH_COMPONENT
);
3093 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3097 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3101 err
= pipe(pipe_fds
);
3104 pipe_r
= pipe_fds
[0];
3105 pipe_w
= pipe_fds
[1];
3113 /* In child, just execute the flash and pass returned
3114 * value through pipe once it is done.
3119 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3120 cc
= write(pipe_w
, &err
, sizeof(err
));
3122 exit(cc
!= sizeof(err
));
3127 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3130 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3132 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3135 mnlg_socket_close(nlg_ntf
);
3139 static int cmd_dev(struct dl
*dl
)
3141 if (dl_argv_match(dl
, "help")) {
3144 } else if (dl_argv_match(dl
, "show") ||
3145 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3147 return cmd_dev_show(dl
);
3148 } else if (dl_argv_match(dl
, "eswitch")) {
3150 return cmd_dev_eswitch(dl
);
3151 } else if (dl_argv_match(dl
, "reload")) {
3153 return cmd_dev_reload(dl
);
3154 } else if (dl_argv_match(dl
, "param")) {
3156 return cmd_dev_param(dl
);
3157 } else if (dl_argv_match(dl
, "info")) {
3159 return cmd_dev_info(dl
);
3160 } else if (dl_argv_match(dl
, "flash")) {
3162 return cmd_dev_flash(dl
);
3164 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3168 static void cmd_port_help(void)
3170 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3171 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3172 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3173 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3176 static const char *port_type_name(uint32_t type
)
3179 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3180 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3181 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3182 case DEVLINK_PORT_TYPE_IB
: return "ib";
3183 default: return "<unknown type>";
3187 static const char *port_flavour_name(uint16_t flavour
)
3190 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3192 case DEVLINK_PORT_FLAVOUR_CPU
:
3194 case DEVLINK_PORT_FLAVOUR_DSA
:
3196 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3198 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3200 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
3203 return "<unknown flavour>";
3207 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3211 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3212 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3213 print_uint(PRINT_ANY
, "pfnum", " pfnum %u", fn_num
);
3215 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3216 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3217 print_uint(PRINT_ANY
, "vfnum", " vfnum %u", fn_num
);
3221 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3223 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3224 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3226 pr_out_port_handle_start(dl
, tb
, false);
3227 check_indent_newline(dl
);
3229 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3231 print_string(PRINT_ANY
, "type", "type %s",
3232 port_type_name(port_type
));
3234 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3236 if (port_type
!= des_port_type
)
3237 print_string(PRINT_ANY
, "des_type", " des_type %s",
3238 port_type_name(des_port_type
));
3241 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]) {
3242 print_string(PRINT_ANY
, "netdev", " netdev %s",
3243 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3245 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]) {
3246 print_string(PRINT_ANY
, "ibdev", " ibdev %s",
3247 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3249 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3250 uint16_t port_flavour
=
3251 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3253 print_string(PRINT_ANY
, "flavour", " flavour %s",
3254 port_flavour_name(port_flavour
));
3256 switch (port_flavour
) {
3257 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3258 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3259 pr_out_port_pfvf_num(dl
, tb
);
3265 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3266 uint32_t port_number
;
3268 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3269 print_uint(PRINT_ANY
, "port", " port %u", port_number
);
3271 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3272 print_uint(PRINT_ANY
, "split_group", " split_group %u",
3273 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3274 pr_out_port_handle_end(dl
);
3277 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3279 struct dl
*dl
= data
;
3280 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3281 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3283 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3284 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3285 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3286 return MNL_CB_ERROR
;
3287 pr_out_port(dl
, tb
);
3291 static int cmd_port_show(struct dl
*dl
)
3293 struct nlmsghdr
*nlh
;
3294 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3297 if (dl_argc(dl
) == 0)
3298 flags
|= NLM_F_DUMP
;
3300 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3302 if (dl_argc(dl
) > 0) {
3303 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3308 pr_out_section_start(dl
, "port");
3309 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3310 pr_out_section_end(dl
);
3314 static int cmd_port_set(struct dl
*dl
)
3316 struct nlmsghdr
*nlh
;
3319 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3320 NLM_F_REQUEST
| NLM_F_ACK
);
3322 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3326 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3329 static int cmd_port_split(struct dl
*dl
)
3331 struct nlmsghdr
*nlh
;
3334 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3335 NLM_F_REQUEST
| NLM_F_ACK
);
3337 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3341 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3344 static int cmd_port_unsplit(struct dl
*dl
)
3346 struct nlmsghdr
*nlh
;
3349 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3350 NLM_F_REQUEST
| NLM_F_ACK
);
3352 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3356 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3359 static int cmd_port(struct dl
*dl
)
3361 if (dl_argv_match(dl
, "help")) {
3364 } else if (dl_argv_match(dl
, "show") ||
3365 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3367 return cmd_port_show(dl
);
3368 } else if (dl_argv_match(dl
, "set")) {
3370 return cmd_port_set(dl
);
3371 } else if (dl_argv_match(dl
, "split")) {
3373 return cmd_port_split(dl
);
3374 } else if (dl_argv_match(dl
, "unsplit")) {
3376 return cmd_port_unsplit(dl
);
3378 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3382 static void cmd_sb_help(void)
3384 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3385 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3386 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3387 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3388 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3389 pr_err(" pool POOL_INDEX ]\n");
3390 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3391 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3392 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3393 pr_err(" type { ingress | egress } ]\n");
3394 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3395 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3396 pr_err(" th THRESHOLD\n");
3397 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3398 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3399 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3402 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3404 pr_out_handle_start_arr(dl
, tb
);
3405 check_indent_newline(dl
);
3406 print_uint(PRINT_ANY
, "sb", "sb %u",
3407 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3408 print_uint(PRINT_ANY
, "size", " size %u",
3409 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3410 print_uint(PRINT_ANY
, "ing_pools", " ing_pools %u",
3411 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3412 print_uint(PRINT_ANY
, "eg_pools", " eg_pools %u",
3413 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3414 print_uint(PRINT_ANY
, "ing_tcs", " ing_tcs %u",
3415 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3416 print_uint(PRINT_ANY
, "eg_tcs", " eg_tcs %u",
3417 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3418 pr_out_handle_end(dl
);
3421 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3423 struct dl
*dl
= data
;
3424 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3425 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3427 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3428 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3429 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3430 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3431 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3432 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3433 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3434 return MNL_CB_ERROR
;
3439 static int cmd_sb_show(struct dl
*dl
)
3441 struct nlmsghdr
*nlh
;
3442 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3445 if (dl_argc(dl
) == 0)
3446 flags
|= NLM_F_DUMP
;
3448 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3450 if (dl_argc(dl
) > 0) {
3451 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3456 pr_out_section_start(dl
, "sb");
3457 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3458 pr_out_section_end(dl
);
3462 static const char *pool_type_name(uint8_t type
)
3465 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3466 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3467 default: return "<unknown type>";
3471 static const char *threshold_type_name(uint8_t type
)
3474 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3475 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3476 default: return "<unknown type>";
3480 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3482 pr_out_handle_start_arr(dl
, tb
);
3483 check_indent_newline(dl
);
3484 print_uint(PRINT_ANY
, "sb", "sb %u",
3485 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3486 print_uint(PRINT_ANY
, "pool", " pool %u",
3487 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3488 print_string(PRINT_ANY
, "type", " type %s",
3489 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3490 print_uint(PRINT_ANY
, "size", " size %u",
3491 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3492 print_string(PRINT_ANY
, "thtype", " thtype %s",
3493 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3494 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3495 print_uint(PRINT_ANY
, "cell_size", " cell size %u",
3496 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3497 pr_out_handle_end(dl
);
3500 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3502 struct dl
*dl
= data
;
3503 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3504 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3506 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3507 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3508 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3509 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3510 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3511 return MNL_CB_ERROR
;
3512 pr_out_sb_pool(dl
, tb
);
3516 static int cmd_sb_pool_show(struct dl
*dl
)
3518 struct nlmsghdr
*nlh
;
3519 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3522 if (dl_argc(dl
) == 0)
3523 flags
|= NLM_F_DUMP
;
3525 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3527 if (dl_argc(dl
) > 0) {
3528 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3534 pr_out_section_start(dl
, "pool");
3535 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3536 pr_out_section_end(dl
);
3540 static int cmd_sb_pool_set(struct dl
*dl
)
3542 struct nlmsghdr
*nlh
;
3545 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3546 NLM_F_REQUEST
| NLM_F_ACK
);
3548 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3549 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3553 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3556 static int cmd_sb_pool(struct dl
*dl
)
3558 if (dl_argv_match(dl
, "help")) {
3561 } else if (dl_argv_match(dl
, "show") ||
3562 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3564 return cmd_sb_pool_show(dl
);
3565 } else if (dl_argv_match(dl
, "set")) {
3567 return cmd_sb_pool_set(dl
);
3569 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3573 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3575 pr_out_port_handle_start_arr(dl
, tb
, true);
3576 check_indent_newline(dl
);
3577 print_uint(PRINT_ANY
, "sb", "sb %u",
3578 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3579 print_uint(PRINT_ANY
, "pool", " pool %u",
3580 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3581 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3582 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3583 pr_out_port_handle_end(dl
);
3586 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3588 struct dl
*dl
= data
;
3589 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3590 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3592 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3593 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3594 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3595 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3596 return MNL_CB_ERROR
;
3597 pr_out_sb_port_pool(dl
, tb
);
3601 static int cmd_sb_port_pool_show(struct dl
*dl
)
3603 struct nlmsghdr
*nlh
;
3604 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3607 if (dl_argc(dl
) == 0)
3608 flags
|= NLM_F_DUMP
;
3610 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3612 if (dl_argc(dl
) > 0) {
3613 err
= dl_argv_parse_put(nlh
, dl
,
3614 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3620 pr_out_section_start(dl
, "port_pool");
3621 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3622 pr_out_section_end(dl
);
3626 static int cmd_sb_port_pool_set(struct dl
*dl
)
3628 struct nlmsghdr
*nlh
;
3631 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3632 NLM_F_REQUEST
| NLM_F_ACK
);
3634 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3635 DL_OPT_SB_TH
, DL_OPT_SB
);
3639 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3642 static int cmd_sb_port_pool(struct dl
*dl
)
3644 if (dl_argv_match(dl
, "help")) {
3647 } else if (dl_argv_match(dl
, "show") ||
3648 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3650 return cmd_sb_port_pool_show(dl
);
3651 } else if (dl_argv_match(dl
, "set")) {
3653 return cmd_sb_port_pool_set(dl
);
3655 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3659 static int cmd_sb_port(struct dl
*dl
)
3661 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3664 } else if (dl_argv_match(dl
, "pool")) {
3666 return cmd_sb_port_pool(dl
);
3668 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3672 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3674 pr_out_port_handle_start_arr(dl
, tb
, true);
3675 check_indent_newline(dl
);
3676 print_uint(PRINT_ANY
, "sb", "sb %u",
3677 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3678 print_uint(PRINT_ANY
, "tc", " tc %u",
3679 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3680 print_string(PRINT_ANY
, "type", " type %s",
3681 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3682 print_uint(PRINT_ANY
, "pool", " pool %u",
3683 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3684 print_uint(PRINT_ANY
, "threshold", " threshold %u",
3685 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3686 pr_out_port_handle_end(dl
);
3689 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3691 struct dl
*dl
= data
;
3692 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3693 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3695 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3696 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3697 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3698 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3699 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3700 return MNL_CB_ERROR
;
3701 pr_out_sb_tc_bind(dl
, tb
);
3705 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3707 struct nlmsghdr
*nlh
;
3708 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3711 if (dl_argc(dl
) == 0)
3712 flags
|= NLM_F_DUMP
;
3714 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3716 if (dl_argc(dl
) > 0) {
3717 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3718 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3723 pr_out_section_start(dl
, "tc_bind");
3724 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3725 pr_out_section_end(dl
);
3729 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3731 struct nlmsghdr
*nlh
;
3734 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3735 NLM_F_REQUEST
| NLM_F_ACK
);
3737 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3738 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3743 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3746 static int cmd_sb_tc_bind(struct dl
*dl
)
3748 if (dl_argv_match(dl
, "help")) {
3751 } else if (dl_argv_match(dl
, "show") ||
3752 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3754 return cmd_sb_tc_bind_show(dl
);
3755 } else if (dl_argv_match(dl
, "set")) {
3757 return cmd_sb_tc_bind_set(dl
);
3759 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3763 static int cmd_sb_tc(struct dl
*dl
)
3765 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3768 } else if (dl_argv_match(dl
, "bind")) {
3770 return cmd_sb_tc_bind(dl
);
3772 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3777 struct list_head list
;
3781 uint32_t bound_pool_index
;
3785 struct list_head list
;
3788 uint32_t port_index
;
3790 struct list_head pool_list
;
3791 struct list_head ing_tc_list
;
3792 struct list_head eg_tc_list
;
3798 struct list_head port_list
;
3801 static struct occ_item
*occ_item_alloc(void)
3803 return calloc(1, sizeof(struct occ_item
));
3806 static void occ_item_free(struct occ_item
*occ_item
)
3811 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3813 struct occ_port
*occ_port
;
3815 occ_port
= calloc(1, sizeof(*occ_port
));
3818 occ_port
->port_index
= port_index
;
3819 INIT_LIST_HEAD(&occ_port
->pool_list
);
3820 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3821 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3825 static void occ_port_free(struct occ_port
*occ_port
)
3827 struct occ_item
*occ_item
, *tmp
;
3829 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3830 occ_item_free(occ_item
);
3831 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3832 occ_item_free(occ_item
);
3833 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3834 occ_item_free(occ_item
);
3837 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3839 struct occ_show
*occ_show
;
3841 occ_show
= calloc(1, sizeof(*occ_show
));
3845 INIT_LIST_HEAD(&occ_show
->port_list
);
3849 static void occ_show_free(struct occ_show
*occ_show
)
3851 struct occ_port
*occ_port
, *tmp
;
3853 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3854 occ_port_free(occ_port
);
3857 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3860 struct occ_port
*occ_port
;
3861 uint32_t port_index
;
3863 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3865 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3866 if (occ_port
->port_index
== port_index
)
3869 occ_port
= occ_port_alloc(port_index
);
3872 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3876 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3879 struct occ_item
*occ_item
;
3882 pr_out_sp(7, " %s:", label
);
3883 list_for_each_entry(occ_item
, list
, list
) {
3884 if ((i
- 1) % 4 == 0 && i
!= 1)
3887 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3888 occ_item
->bound_pool_index
);
3890 pr_out_sp(7, "%2u:", occ_item
->index
);
3891 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3895 if ((i
- 1) % 4 != 0)
3899 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3900 struct list_head
*list
,
3903 struct occ_item
*occ_item
;
3906 open_json_object(label
);
3907 list_for_each_entry(occ_item
, list
, list
) {
3908 sprintf(buf
, "%u", occ_item
->index
);
3909 open_json_object(buf
);
3911 print_uint(PRINT_JSON
, "bound_pool", NULL
,
3912 occ_item
->bound_pool_index
);
3913 print_uint(PRINT_JSON
, "current", NULL
, occ_item
->cur
);
3914 print_uint(PRINT_JSON
, "max", NULL
, occ_item
->max
);
3915 close_json_object();
3917 close_json_object();
3920 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3922 if (dl
->json_output
) {
3923 pr_out_json_occ_show_item_list(dl
, "pool",
3924 &occ_port
->pool_list
, false);
3925 pr_out_json_occ_show_item_list(dl
, "itc",
3926 &occ_port
->ing_tc_list
, true);
3927 pr_out_json_occ_show_item_list(dl
, "etc",
3928 &occ_port
->eg_tc_list
, true);
3931 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3932 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3933 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3937 static void pr_out_occ_show(struct occ_show
*occ_show
)
3939 struct dl
*dl
= occ_show
->dl
;
3940 struct dl_opts
*opts
= &dl
->opts
;
3941 struct occ_port
*occ_port
;
3943 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3944 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3945 occ_port
->port_index
, true, false);
3946 pr_out_occ_show_port(dl
, occ_port
);
3947 pr_out_port_handle_end(dl
);
3951 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3954 struct occ_port
*occ_port
;
3955 struct occ_item
*occ_item
;
3957 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3960 occ_port
= occ_port_get(occ_show
, tb
);
3962 occ_show
->err
= -ENOMEM
;
3966 occ_item
= occ_item_alloc();
3968 occ_show
->err
= -ENOMEM
;
3971 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3972 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3973 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3974 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3977 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3979 struct occ_show
*occ_show
= data
;
3980 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3981 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3983 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3984 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3985 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3986 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3987 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3988 return MNL_CB_ERROR
;
3989 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3993 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3996 struct occ_port
*occ_port
;
3997 struct occ_item
*occ_item
;
4000 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
4003 occ_port
= occ_port_get(occ_show
, tb
);
4005 occ_show
->err
= -ENOMEM
;
4009 occ_item
= occ_item_alloc();
4011 occ_show
->err
= -ENOMEM
;
4014 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
4015 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
4016 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
4017 occ_item
->bound_pool_index
=
4018 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
4019 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
4020 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
4021 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
4022 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
4023 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
4025 occ_item_free(occ_item
);
4028 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
4030 struct occ_show
*occ_show
= data
;
4031 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4032 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4034 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4035 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4036 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
4037 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
4038 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
4039 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
4040 return MNL_CB_ERROR
;
4041 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
4045 static int cmd_sb_occ_show(struct dl
*dl
)
4047 struct nlmsghdr
*nlh
;
4048 struct occ_show
*occ_show
;
4049 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4052 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4056 occ_show
= occ_show_alloc(dl
);
4060 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4062 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4063 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4067 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4069 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4070 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4074 pr_out_section_start(dl
, "occupancy");
4075 pr_out_occ_show(occ_show
);
4076 pr_out_section_end(dl
);
4079 occ_show_free(occ_show
);
4083 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4085 struct nlmsghdr
*nlh
;
4088 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4089 NLM_F_REQUEST
| NLM_F_ACK
);
4091 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4095 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4098 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4100 struct nlmsghdr
*nlh
;
4103 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4104 NLM_F_REQUEST
| NLM_F_ACK
);
4106 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4110 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4113 static int cmd_sb_occ(struct dl
*dl
)
4115 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4118 } else if (dl_argv_match(dl
, "show") ||
4119 dl_argv_match(dl
, "list")) {
4121 return cmd_sb_occ_show(dl
);
4122 } else if (dl_argv_match(dl
, "snapshot")) {
4124 return cmd_sb_occ_snapshot(dl
);
4125 } else if (dl_argv_match(dl
, "clearmax")) {
4127 return cmd_sb_occ_clearmax(dl
);
4129 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4133 static int cmd_sb(struct dl
*dl
)
4135 if (dl_argv_match(dl
, "help")) {
4138 } else if (dl_argv_match(dl
, "show") ||
4139 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4141 return cmd_sb_show(dl
);
4142 } else if (dl_argv_match(dl
, "pool")) {
4144 return cmd_sb_pool(dl
);
4145 } else if (dl_argv_match(dl
, "port")) {
4147 return cmd_sb_port(dl
);
4148 } else if (dl_argv_match(dl
, "tc")) {
4150 return cmd_sb_tc(dl
);
4151 } else if (dl_argv_match(dl
, "occupancy")) {
4153 return cmd_sb_occ(dl
);
4155 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4159 static const char *cmd_name(uint8_t cmd
)
4162 case DEVLINK_CMD_UNSPEC
: return "unspec";
4163 case DEVLINK_CMD_GET
: return "get";
4164 case DEVLINK_CMD_SET
: return "set";
4165 case DEVLINK_CMD_NEW
: return "new";
4166 case DEVLINK_CMD_DEL
: return "del";
4167 case DEVLINK_CMD_PORT_GET
: return "get";
4168 case DEVLINK_CMD_PORT_SET
: return "set";
4169 case DEVLINK_CMD_PORT_NEW
: return "new";
4170 case DEVLINK_CMD_PORT_DEL
: return "del";
4171 case DEVLINK_CMD_PARAM_GET
: return "get";
4172 case DEVLINK_CMD_PARAM_SET
: return "set";
4173 case DEVLINK_CMD_PARAM_NEW
: return "new";
4174 case DEVLINK_CMD_PARAM_DEL
: return "del";
4175 case DEVLINK_CMD_REGION_GET
: return "get";
4176 case DEVLINK_CMD_REGION_SET
: return "set";
4177 case DEVLINK_CMD_REGION_NEW
: return "new";
4178 case DEVLINK_CMD_REGION_DEL
: return "del";
4179 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4180 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4181 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4182 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
: return "status";
4183 case DEVLINK_CMD_TRAP_GET
: return "get";
4184 case DEVLINK_CMD_TRAP_SET
: return "set";
4185 case DEVLINK_CMD_TRAP_NEW
: return "new";
4186 case DEVLINK_CMD_TRAP_DEL
: return "del";
4187 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4188 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4189 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4190 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4191 case DEVLINK_CMD_TRAP_POLICER_GET
: return "get";
4192 case DEVLINK_CMD_TRAP_POLICER_SET
: return "set";
4193 case DEVLINK_CMD_TRAP_POLICER_NEW
: return "new";
4194 case DEVLINK_CMD_TRAP_POLICER_DEL
: return "del";
4195 default: return "<unknown cmd>";
4199 static const char *cmd_obj(uint8_t cmd
)
4202 case DEVLINK_CMD_UNSPEC
: return "unspec";
4203 case DEVLINK_CMD_GET
:
4204 case DEVLINK_CMD_SET
:
4205 case DEVLINK_CMD_NEW
:
4206 case DEVLINK_CMD_DEL
:
4208 case DEVLINK_CMD_PORT_GET
:
4209 case DEVLINK_CMD_PORT_SET
:
4210 case DEVLINK_CMD_PORT_NEW
:
4211 case DEVLINK_CMD_PORT_DEL
:
4213 case DEVLINK_CMD_PARAM_GET
:
4214 case DEVLINK_CMD_PARAM_SET
:
4215 case DEVLINK_CMD_PARAM_NEW
:
4216 case DEVLINK_CMD_PARAM_DEL
:
4218 case DEVLINK_CMD_REGION_GET
:
4219 case DEVLINK_CMD_REGION_SET
:
4220 case DEVLINK_CMD_REGION_NEW
:
4221 case DEVLINK_CMD_REGION_DEL
:
4223 case DEVLINK_CMD_FLASH_UPDATE
:
4224 case DEVLINK_CMD_FLASH_UPDATE_END
:
4225 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4227 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4229 case DEVLINK_CMD_TRAP_GET
:
4230 case DEVLINK_CMD_TRAP_SET
:
4231 case DEVLINK_CMD_TRAP_NEW
:
4232 case DEVLINK_CMD_TRAP_DEL
:
4234 case DEVLINK_CMD_TRAP_GROUP_GET
:
4235 case DEVLINK_CMD_TRAP_GROUP_SET
:
4236 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4237 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4238 return "trap-group";
4239 case DEVLINK_CMD_TRAP_POLICER_GET
:
4240 case DEVLINK_CMD_TRAP_POLICER_SET
:
4241 case DEVLINK_CMD_TRAP_POLICER_NEW
:
4242 case DEVLINK_CMD_TRAP_POLICER_DEL
:
4243 return "trap-policer";
4244 default: return "<unknown obj>";
4248 static void pr_out_mon_header(uint8_t cmd
)
4250 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4253 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4255 const char *obj
= cmd_obj(cmd
);
4256 unsigned int index
= 0;
4257 const char *cur_obj
;
4261 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4262 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4268 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4270 __pr_out_handle_start(dl
, tb
, true, false);
4272 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]) {
4273 check_indent_newline(dl
);
4274 print_string(PRINT_ANY
, "msg", "msg %s",
4275 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4277 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]) {
4278 check_indent_newline(dl
);
4279 print_string(PRINT_ANY
, "component", "component %s",
4280 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4283 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4284 pr_out_u64(dl
, "done",
4285 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4287 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4288 pr_out_u64(dl
, "total",
4289 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4291 pr_out_handle_end(dl
);
4294 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4295 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
);
4296 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4297 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4298 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4300 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4302 struct dl
*dl
= data
;
4303 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4304 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4305 uint8_t cmd
= genl
->cmd
;
4307 if (!cmd_filter_check(dl
, cmd
))
4311 case DEVLINK_CMD_GET
: /* fall through */
4312 case DEVLINK_CMD_SET
: /* fall through */
4313 case DEVLINK_CMD_NEW
: /* fall through */
4314 case DEVLINK_CMD_DEL
:
4315 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4316 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4317 return MNL_CB_ERROR
;
4318 pr_out_mon_header(genl
->cmd
);
4319 pr_out_handle(dl
, tb
);
4321 case DEVLINK_CMD_PORT_GET
: /* fall through */
4322 case DEVLINK_CMD_PORT_SET
: /* fall through */
4323 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4324 case DEVLINK_CMD_PORT_DEL
:
4325 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4326 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4327 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4328 return MNL_CB_ERROR
;
4329 pr_out_mon_header(genl
->cmd
);
4330 pr_out_port(dl
, tb
);
4332 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4333 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4334 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4335 case DEVLINK_CMD_PARAM_DEL
:
4336 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4337 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4338 !tb
[DEVLINK_ATTR_PARAM
])
4339 return MNL_CB_ERROR
;
4340 pr_out_mon_header(genl
->cmd
);
4341 pr_out_param(dl
, tb
, false);
4343 case DEVLINK_CMD_REGION_GET
: /* fall through */
4344 case DEVLINK_CMD_REGION_SET
: /* fall through */
4345 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4346 case DEVLINK_CMD_REGION_DEL
:
4347 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4348 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4349 !tb
[DEVLINK_ATTR_REGION_NAME
])
4350 return MNL_CB_ERROR
;
4351 pr_out_mon_header(genl
->cmd
);
4352 pr_out_region(dl
, tb
);
4354 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4355 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4356 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4357 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4358 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4359 return MNL_CB_ERROR
;
4360 pr_out_mon_header(genl
->cmd
);
4361 pr_out_flash_update(dl
, tb
);
4363 case DEVLINK_CMD_HEALTH_REPORTER_RECOVER
:
4364 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4365 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4366 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
4367 return MNL_CB_ERROR
;
4368 pr_out_mon_header(genl
->cmd
);
4369 pr_out_health(dl
, tb
);
4371 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4372 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4373 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4374 case DEVLINK_CMD_TRAP_DEL
:
4375 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4376 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4377 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4378 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4379 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4380 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4381 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4382 !tb
[DEVLINK_ATTR_STATS
])
4383 return MNL_CB_ERROR
;
4384 pr_out_mon_header(genl
->cmd
);
4385 pr_out_trap(dl
, tb
, false);
4387 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4388 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4389 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4390 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4391 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4392 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4393 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4394 !tb
[DEVLINK_ATTR_STATS
])
4395 return MNL_CB_ERROR
;
4396 pr_out_mon_header(genl
->cmd
);
4397 pr_out_trap_group(dl
, tb
, false);
4399 case DEVLINK_CMD_TRAP_POLICER_GET
: /* fall through */
4400 case DEVLINK_CMD_TRAP_POLICER_SET
: /* fall through */
4401 case DEVLINK_CMD_TRAP_POLICER_NEW
: /* fall through */
4402 case DEVLINK_CMD_TRAP_POLICER_DEL
: /* fall through */
4403 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4404 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4405 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
4406 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
4407 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
4408 return MNL_CB_ERROR
;
4409 pr_out_mon_header(genl
->cmd
);
4410 pr_out_trap_policer(dl
, tb
, false);
4416 static int cmd_mon_show(struct dl
*dl
)
4419 unsigned int index
= 0;
4420 const char *cur_obj
;
4422 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4423 if (strcmp(cur_obj
, "all") != 0 &&
4424 strcmp(cur_obj
, "dev") != 0 &&
4425 strcmp(cur_obj
, "port") != 0 &&
4426 strcmp(cur_obj
, "health") != 0 &&
4427 strcmp(cur_obj
, "trap") != 0 &&
4428 strcmp(cur_obj
, "trap-group") != 0 &&
4429 strcmp(cur_obj
, "trap-policer") != 0) {
4430 pr_err("Unknown object \"%s\"\n", cur_obj
);
4434 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4437 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4443 static void cmd_mon_help(void)
4445 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4446 "where OBJECT-LIST := { dev | port | health | trap | trap-group | trap-policer }\n");
4449 static int cmd_mon(struct dl
*dl
)
4451 if (dl_argv_match(dl
, "help")) {
4455 return cmd_mon_show(dl
);
4458 struct dpipe_field
{
4461 unsigned int bitwidth
;
4462 enum devlink_dpipe_field_mapping_type mapping_type
;
4465 struct dpipe_header
{
4466 struct list_head list
;
4469 struct dpipe_field
*fields
;
4470 unsigned int fields_count
;
4473 struct dpipe_table
{
4474 struct list_head list
;
4476 unsigned int resource_id
;
4477 bool resource_valid
;
4480 struct dpipe_tables
{
4481 struct list_head table_list
;
4491 enum devlink_resource_unit unit
;
4496 struct list_head list
;
4497 struct list_head resource_list
;
4498 struct resource
*parent
;
4502 struct list_head resource_list
;
4505 struct resource_ctx
{
4508 struct resources
*resources
;
4509 struct dpipe_tables
*tables
;
4510 bool print_resources
;
4511 bool pending_change
;
4514 static struct resource
*resource_alloc(void)
4516 struct resource
*resource
;
4518 resource
= calloc(1, sizeof(struct resource
));
4521 INIT_LIST_HEAD(&resource
->resource_list
);
4525 static void resource_free(struct resource
*resource
)
4527 struct resource
*child_resource
, *tmp
;
4529 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4531 free(child_resource
->name
);
4532 resource_free(child_resource
);
4537 static struct resources
*resources_alloc(void)
4539 struct resources
*resources
;
4541 resources
= calloc(1, sizeof(struct resources
));
4544 INIT_LIST_HEAD(&resources
->resource_list
);
4548 static void resources_free(struct resources
*resources
)
4550 struct resource
*resource
, *tmp
;
4552 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4553 resource_free(resource
);
4556 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4558 ctx
->resources
= resources_alloc();
4559 if (!ctx
->resources
)
4565 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4567 resources_free(ctx
->resources
);
4573 struct list_head global_headers
;
4574 struct list_head local_headers
;
4575 struct dpipe_tables
*tables
;
4576 struct resources
*resources
;
4581 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4583 struct dpipe_header
*header
;
4585 header
= calloc(1, sizeof(struct dpipe_header
));
4588 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4589 if (!header
->fields
)
4590 goto err_fields_alloc
;
4591 header
->fields_count
= fields_count
;
4599 static void dpipe_header_free(struct dpipe_header
*header
)
4601 free(header
->fields
);
4605 static void dpipe_header_clear(struct dpipe_header
*header
)
4607 struct dpipe_field
*field
;
4610 for (i
= 0; i
< header
->fields_count
; i
++) {
4611 field
= &header
->fields
[i
];
4617 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4618 struct dpipe_header
*header
, bool global
)
4621 list_add(&header
->list
, &ctx
->global_headers
);
4623 list_add(&header
->list
, &ctx
->local_headers
);
4626 static void dpipe_header_del(struct dpipe_header
*header
)
4628 list_del(&header
->list
);
4631 static struct dpipe_table
*dpipe_table_alloc(void)
4633 return calloc(1, sizeof(struct dpipe_table
));
4636 static void dpipe_table_free(struct dpipe_table
*table
)
4641 static struct dpipe_tables
*dpipe_tables_alloc(void)
4643 struct dpipe_tables
*tables
;
4645 tables
= calloc(1, sizeof(struct dpipe_tables
));
4648 INIT_LIST_HEAD(&tables
->table_list
);
4652 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4654 struct dpipe_table
*table
, *tmp
;
4656 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4657 dpipe_table_free(table
);
4661 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4663 ctx
->tables
= dpipe_tables_alloc();
4668 INIT_LIST_HEAD(&ctx
->global_headers
);
4669 INIT_LIST_HEAD(&ctx
->local_headers
);
4673 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4675 struct dpipe_header
*header
, *tmp
;
4677 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4679 dpipe_header_del(header
);
4680 dpipe_header_clear(header
);
4681 dpipe_header_free(header
);
4683 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4685 dpipe_header_del(header
);
4686 dpipe_header_clear(header
);
4687 dpipe_header_free(header
);
4689 dpipe_tables_free(ctx
->tables
);
4692 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4693 uint32_t header_id
, bool global
)
4695 struct list_head
*header_list
;
4696 struct dpipe_header
*header
;
4699 header_list
= &ctx
->global_headers
;
4701 header_list
= &ctx
->local_headers
;
4702 list_for_each_entry(header
, header_list
, list
) {
4703 if (header
->id
!= header_id
)
4705 return header
->name
;
4710 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4712 uint32_t field_id
, bool global
)
4714 struct list_head
*header_list
;
4715 struct dpipe_header
*header
;
4718 header_list
= &ctx
->global_headers
;
4720 header_list
= &ctx
->local_headers
;
4721 list_for_each_entry(header
, header_list
, list
) {
4722 if (header
->id
!= header_id
)
4724 return header
->fields
[field_id
].name
;
4730 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4732 switch (mapping_type
) {
4733 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4735 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4743 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4744 uint32_t field_id
, bool global
)
4746 enum devlink_dpipe_field_mapping_type mapping_type
;
4747 struct list_head
*header_list
;
4748 struct dpipe_header
*header
;
4751 header_list
= &ctx
->global_headers
;
4753 header_list
= &ctx
->local_headers
;
4754 list_for_each_entry(header
, header_list
, list
) {
4755 if (header
->id
!= header_id
)
4757 mapping_type
= header
->fields
[field_id
].mapping_type
;
4758 return dpipe_field_mapping_e2s(mapping_type
);
4763 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4764 struct dpipe_field
*fields
,
4765 unsigned int field_count
)
4767 struct dpipe_field
*field
;
4770 for (i
= 0; i
< field_count
; i
++) {
4772 pr_out_entry_start(ctx
->dl
);
4773 check_indent_newline(ctx
->dl
);
4774 print_string(PRINT_ANY
, "name", "name %s", field
->name
);
4775 if (ctx
->dl
->verbose
)
4776 print_uint(PRINT_ANY
, "id", " id %u", field
->id
);
4777 print_uint(PRINT_ANY
, "bitwidth", " bitwidth %u", field
->bitwidth
);
4778 if (field
->mapping_type
) {
4779 print_string(PRINT_ANY
, "mapping_type", " mapping_type %s",
4780 dpipe_field_mapping_e2s(field
->mapping_type
));
4782 pr_out_entry_end(ctx
->dl
);
4787 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4788 struct dpipe_header
*header
, bool global
)
4790 pr_out_handle_start_arr(ctx
->dl
, tb
);
4791 check_indent_newline(ctx
->dl
);
4792 print_string(PRINT_ANY
, "name", "name %s", header
->name
);
4793 if (ctx
->dl
->verbose
) {
4794 print_uint(PRINT_ANY
, "id", " id %u", header
->id
);
4795 print_bool(PRINT_ANY
, "global", " global %s", global
);
4797 pr_out_array_start(ctx
->dl
, "field");
4798 pr_out_dpipe_fields(ctx
, header
->fields
,
4799 header
->fields_count
);
4800 pr_out_array_end(ctx
->dl
);
4801 pr_out_handle_end(ctx
->dl
);
4804 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4807 struct dpipe_header
*header
;
4809 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4810 pr_out_dpipe_header(ctx
, tb
, header
, false);
4812 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4813 pr_out_dpipe_header(ctx
, tb
, header
, true);
4816 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4818 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4822 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4823 if (err
!= MNL_CB_OK
)
4825 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4826 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4827 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4828 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4831 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4832 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4833 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4834 field
->name
= strdup(name
);
4837 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4841 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4842 struct dpipe_field
*fields
)
4844 struct nlattr
*nla_field
;
4848 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4849 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4857 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4859 struct nlattr
*nla_field
;
4860 unsigned int count
= 0;
4862 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4867 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4869 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4870 struct dpipe_header
*header
;
4871 unsigned int fields_count
;
4872 const char *header_name
;
4876 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4877 if (err
!= MNL_CB_OK
)
4880 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4881 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4882 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4885 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4886 header
= dpipe_header_alloc(fields_count
);
4890 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4891 header
->name
= strdup(header_name
);
4892 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4893 header
->fields_count
= fields_count
;
4894 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4896 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4900 dpipe_header_add(ctx
, header
, global
);
4904 dpipe_header_free(header
);
4908 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4910 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4911 struct nlattr
*nla_header
;
4914 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4915 err
= dpipe_header_get(ctx
, nla_header
);
4922 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4924 struct dpipe_ctx
*ctx
= data
;
4925 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4926 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4929 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4930 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4931 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4932 return MNL_CB_ERROR
;
4933 err
= dpipe_headers_get(ctx
, tb
);
4936 return MNL_CB_ERROR
;
4939 if (ctx
->print_headers
)
4940 pr_out_dpipe_headers(ctx
, tb
);
4944 static int cmd_dpipe_headers_show(struct dl
*dl
)
4946 struct nlmsghdr
*nlh
;
4947 struct dpipe_ctx ctx
= {};
4948 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4951 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4953 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4957 err
= dpipe_ctx_init(&ctx
, dl
);
4961 ctx
.print_headers
= true;
4963 pr_out_section_start(dl
, "header");
4964 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4966 pr_err("error get headers %s\n", strerror(ctx
.err
));
4967 pr_out_section_end(dl
);
4969 dpipe_ctx_fini(&ctx
);
4973 static void cmd_dpipe_header_help(void)
4975 pr_err("Usage: devlink dpipe headers show DEV\n");
4978 static int cmd_dpipe_header(struct dl
*dl
)
4980 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4981 cmd_dpipe_header_help();
4983 } else if (dl_argv_match(dl
, "show")) {
4985 return cmd_dpipe_headers_show(dl
);
4987 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4992 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4994 switch (action_type
) {
4995 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4996 return "field_modify";
5002 struct dpipe_op_info
{
5008 struct dpipe_action
{
5009 struct dpipe_op_info info
;
5013 static void pr_out_dpipe_action(struct dpipe_action
*action
,
5014 struct dpipe_ctx
*ctx
)
5016 struct dpipe_op_info
*op_info
= &action
->info
;
5017 const char *mapping
;
5019 check_indent_newline(ctx
->dl
);
5020 print_string(PRINT_ANY
, "type", "type %s",
5021 dpipe_action_type_e2s(action
->type
));
5022 print_string(PRINT_ANY
, "header", " header %s",
5023 dpipe_header_id2s(ctx
, op_info
->header_id
,
5024 op_info
->header_global
));
5025 print_string(PRINT_ANY
, "field", " field %s",
5026 dpipe_field_id2s(ctx
, op_info
->header_id
,
5028 op_info
->header_global
));
5029 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5031 op_info
->header_global
);
5033 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5036 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
5038 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
5041 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
5042 if (err
!= MNL_CB_OK
)
5045 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
5046 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5047 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5048 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5052 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
5053 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5054 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5055 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5060 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
5061 struct nlattr
*nla_actions
)
5063 struct nlattr
*nla_action
;
5064 struct dpipe_action action
;
5066 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
5067 pr_out_entry_start(ctx
->dl
);
5068 if (dpipe_action_parse(&action
, nla_action
))
5069 goto err_action_parse
;
5070 pr_out_dpipe_action(&action
, ctx
);
5071 pr_out_entry_end(ctx
->dl
);
5076 pr_out_entry_end(ctx
->dl
);
5081 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
5083 switch (match_type
) {
5084 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
5085 return "field_exact";
5091 struct dpipe_match
{
5092 struct dpipe_op_info info
;
5096 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5097 struct dpipe_ctx
*ctx
)
5099 struct dpipe_op_info
*op_info
= &match
->info
;
5100 const char *mapping
;
5102 check_indent_newline(ctx
->dl
);
5103 print_string(PRINT_ANY
, "type", "type %s",
5104 dpipe_match_type_e2s(match
->type
));
5105 print_string(PRINT_ANY
, "header", " header %s",
5106 dpipe_header_id2s(ctx
, op_info
->header_id
,
5107 op_info
->header_global
));
5108 print_string(PRINT_ANY
, "field", " field %s",
5109 dpipe_field_id2s(ctx
, op_info
->header_id
,
5111 op_info
->header_global
));
5112 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5114 op_info
->header_global
);
5116 print_string(PRINT_ANY
, "mapping", " mapping %s", mapping
);
5119 static int dpipe_match_parse(struct dpipe_match
*match
,
5123 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5126 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5127 if (err
!= MNL_CB_OK
)
5130 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5131 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5132 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5133 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5137 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5138 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5139 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5140 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5145 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5146 struct nlattr
*nla_matches
)
5148 struct nlattr
*nla_match
;
5149 struct dpipe_match match
;
5151 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5152 pr_out_entry_start(ctx
->dl
);
5153 if (dpipe_match_parse(&match
, nla_match
))
5154 goto err_match_parse
;
5155 pr_out_dpipe_match(&match
, ctx
);
5156 pr_out_entry_end(ctx
->dl
);
5161 pr_out_entry_end(ctx
->dl
);
5165 static struct resource
*
5166 resource_find(struct resources
*resources
, struct resource
*resource
,
5167 uint64_t resource_id
)
5169 struct list_head
*list_head
;
5172 list_head
= &resources
->resource_list
;
5174 list_head
= &resource
->resource_list
;
5176 list_for_each_entry(resource
, list_head
, list
) {
5177 struct resource
*child_resource
;
5179 if (resource
->id
== resource_id
)
5182 child_resource
= resource_find(resources
, resource
,
5185 return child_resource
;
5191 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5192 uint64_t resource_id
)
5194 struct resource
*resource
, *parent_resource
;
5195 const char del
[] = "/";
5199 resource
= resource_find(resources
, NULL
, resource_id
);
5203 for (parent_resource
= resource
; parent_resource
;
5204 parent_resource
= parent_resource
->parent
)
5205 path_len
+= strlen(parent_resource
->name
) + 1;
5208 path
= calloc(1, path_len
);
5212 path
+= path_len
- 1;
5213 for (parent_resource
= resource
; parent_resource
;
5214 parent_resource
= parent_resource
->parent
) {
5215 path
-= strlen(parent_resource
->name
);
5216 memcpy(path
, parent_resource
->name
,
5217 strlen(parent_resource
->name
));
5218 path
-= strlen(del
);
5219 memcpy(path
, del
, strlen(del
));
5221 check_indent_newline(dl
);
5222 print_string(PRINT_ANY
, "resource_path", "resource_path %s", path
);
5226 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5228 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5229 struct dpipe_table
*table
;
5230 uint32_t resource_units
;
5231 bool counters_enabled
;
5232 bool resource_valid
;
5236 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5237 if (err
!= MNL_CB_OK
)
5240 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5241 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5242 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5243 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5244 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5248 table
= dpipe_table_alloc();
5252 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5253 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5254 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5256 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5258 if (resource_valid
) {
5259 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5260 table
->resource_valid
= true;
5263 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5264 if (!ctx
->print_tables
)
5267 check_indent_newline(ctx
->dl
);
5268 print_string(PRINT_ANY
, "name", "name %s", table
->name
);
5269 print_uint(PRINT_ANY
, "size", " size %u", size
);
5270 print_bool(PRINT_ANY
, "counters_enabled", " counters_enabled %s", counters_enabled
);
5272 if (resource_valid
) {
5273 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5274 resource_path_print(ctx
->dl
, ctx
->resources
,
5275 table
->resource_id
);
5276 print_uint(PRINT_ANY
, "resource_units", " resource_units %u",
5280 pr_out_array_start(ctx
->dl
, "match");
5281 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5282 goto err_matches_show
;
5283 pr_out_array_end(ctx
->dl
);
5285 pr_out_array_start(ctx
->dl
, "action");
5286 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5287 goto err_actions_show
;
5288 pr_out_array_end(ctx
->dl
);
5294 pr_out_array_end(ctx
->dl
);
5298 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5300 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5301 struct nlattr
*nla_table
;
5303 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5304 if (ctx
->print_tables
)
5305 pr_out_handle_start_arr(ctx
->dl
, tb
);
5306 if (dpipe_table_show(ctx
, nla_table
))
5307 goto err_table_show
;
5308 if (ctx
->print_tables
)
5309 pr_out_handle_end(ctx
->dl
);
5314 if (ctx
->print_tables
)
5315 pr_out_handle_end(ctx
->dl
);
5319 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5321 struct dpipe_ctx
*ctx
= data
;
5322 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5323 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5325 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5326 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5327 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5328 return MNL_CB_ERROR
;
5330 if (dpipe_tables_show(ctx
, tb
))
5331 return MNL_CB_ERROR
;
5335 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5337 static int cmd_dpipe_table_show(struct dl
*dl
)
5339 struct nlmsghdr
*nlh
;
5340 struct dpipe_ctx dpipe_ctx
= {};
5341 struct resource_ctx resource_ctx
= {};
5342 uint16_t flags
= NLM_F_REQUEST
;
5345 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5349 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5351 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5355 dpipe_ctx
.print_tables
= true;
5357 dl_opts_put(nlh
, dl
);
5358 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5361 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5362 goto err_headers_get
;
5365 err
= resource_ctx_init(&resource_ctx
, dl
);
5367 goto err_resource_ctx_init
;
5369 resource_ctx
.print_resources
= false;
5370 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5371 dl_opts_put(nlh
, dl
);
5372 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5375 dpipe_ctx
.resources
= resource_ctx
.resources
;
5377 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5378 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5379 dl_opts_put(nlh
, dl
);
5381 pr_out_section_start(dl
, "table");
5382 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5383 pr_out_section_end(dl
);
5385 resource_ctx_fini(&resource_ctx
);
5386 dpipe_ctx_fini(&dpipe_ctx
);
5389 err_resource_ctx_init
:
5391 dpipe_ctx_fini(&dpipe_ctx
);
5395 static int cmd_dpipe_table_set(struct dl
*dl
)
5397 struct nlmsghdr
*nlh
;
5400 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5401 NLM_F_REQUEST
| NLM_F_ACK
);
5403 err
= dl_argv_parse_put(nlh
, dl
,
5404 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5405 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5409 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5412 enum dpipe_value_type
{
5413 DPIPE_VALUE_TYPE_VALUE
,
5414 DPIPE_VALUE_TYPE_MASK
,
5418 dpipe_value_type_e2s(enum dpipe_value_type type
)
5421 case DPIPE_VALUE_TYPE_VALUE
:
5423 case DPIPE_VALUE_TYPE_MASK
:
5424 return "value_mask";
5430 struct dpipe_field_printer
{
5431 unsigned int field_id
;
5432 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5435 struct dpipe_header_printer
{
5436 struct dpipe_field_printer
*printers
;
5437 unsigned int printers_count
;
5438 unsigned int header_id
;
5441 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5442 enum dpipe_value_type type
,
5445 struct in_addr ip_addr
;
5447 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5448 check_indent_newline(ctx
->dl
);
5449 print_string_name_value(dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5453 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5454 enum dpipe_value_type type
,
5457 check_indent_newline(ctx
->dl
);
5458 print_string_name_value(dpipe_value_type_e2s(type
),
5459 ether_ntoa((struct ether_addr
*)value
));
5462 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5463 enum dpipe_value_type type
,
5466 char str
[INET6_ADDRSTRLEN
];
5468 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5469 check_indent_newline(ctx
->dl
);
5470 print_string_name_value(dpipe_value_type_e2s(type
), str
);
5473 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5475 .printer
= dpipe_field_printer_ipv4_addr
,
5476 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5480 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5481 .printers
= dpipe_field_printers_ipv4
,
5482 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5483 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5486 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5488 .printer
= dpipe_field_printer_ethernet_addr
,
5489 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5493 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5494 .printers
= dpipe_field_printers_ethernet
,
5495 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5496 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5499 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5501 .printer
= dpipe_field_printer_ipv6_addr
,
5502 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5506 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5507 .printers
= dpipe_field_printers_ipv6
,
5508 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5509 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5512 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5513 &dpipe_header_printer_ipv4
,
5514 &dpipe_header_printer_ethernet
,
5515 &dpipe_header_printer_ipv6
,
5518 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5519 struct dpipe_op_info
*info
,
5520 enum dpipe_value_type type
,
5523 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5524 struct dpipe_header_printer
*header_printer
;
5525 struct dpipe_field_printer
*field_printer
;
5526 unsigned int field_printers_count
;
5530 for (i
= 0; i
< header_printers_count
; i
++) {
5531 header_printer
= dpipe_header_printers
[i
];
5532 if (header_printer
->header_id
!= info
->header_id
)
5534 field_printers_count
= header_printer
->printers_count
;
5535 for (j
= 0; j
< field_printers_count
; j
++) {
5536 field_printer
= &header_printer
->printers
[j
];
5537 if (field_printer
->field_id
!= info
->field_id
)
5539 field_printer
->printer(ctx
, type
, value
);
5547 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5549 unsigned int value_len
,
5550 struct dpipe_op_info
*info
,
5551 enum dpipe_value_type type
)
5553 if (info
->header_global
&&
5554 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5557 if (value_len
== sizeof(uint32_t)) {
5558 uint32_t *value_32
= value
;
5560 check_indent_newline(ctx
->dl
);
5561 print_uint_name_value(dpipe_value_type_e2s(type
), *value_32
);
5565 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5566 struct nlattr
**nla_match_value
,
5567 struct dpipe_op_info
*info
)
5569 void *value
, *value_mask
;
5570 uint32_t value_mapping
;
5574 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5575 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5577 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5578 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5581 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5582 check_indent_newline(ctx
->dl
);
5583 print_uint(PRINT_ANY
, "mapping_value", "mapping_value %u", value_mapping
);
5587 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5588 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5589 DPIPE_VALUE_TYPE_MASK
);
5592 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5595 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5598 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5599 struct dpipe_match match
;
5602 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5603 if (err
!= MNL_CB_OK
)
5606 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5607 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5611 pr_out_entry_start(ctx
->dl
);
5612 if (dpipe_match_parse(&match
,
5613 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5614 goto err_match_parse
;
5615 pr_out_dpipe_match(&match
, ctx
);
5616 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5617 pr_out_entry_end(ctx
->dl
);
5622 pr_out_entry_end(ctx
->dl
);
5626 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5629 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5630 struct dpipe_action action
;
5633 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5634 if (err
!= MNL_CB_OK
)
5637 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5638 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5642 pr_out_entry_start(ctx
->dl
);
5643 if (dpipe_action_parse(&action
,
5644 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5645 goto err_action_parse
;
5646 pr_out_dpipe_action(&action
, ctx
);
5647 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5648 pr_out_entry_end(ctx
->dl
);
5653 pr_out_entry_end(ctx
->dl
);
5658 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5659 struct nlattr
*nla_action_values
)
5661 struct nlattr
*nla_action_value
;
5663 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5664 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5671 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5672 struct nlattr
*nla_match_values
)
5674 struct nlattr
*nla_match_value
;
5676 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5677 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5683 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5685 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5686 uint32_t entry_index
;
5690 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5691 if (err
!= MNL_CB_OK
)
5694 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5695 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5696 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5700 check_indent_newline(ctx
->dl
);
5701 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5702 print_uint(PRINT_ANY
, "index", "index %u", entry_index
);
5704 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5705 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5706 print_uint(PRINT_ANY
, "counter", " counter %u", counter
);
5709 pr_out_array_start(ctx
->dl
, "match_value");
5710 if (dpipe_tables_match_values_show(ctx
,
5711 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5712 goto err_match_values_show
;
5713 pr_out_array_end(ctx
->dl
);
5715 pr_out_array_start(ctx
->dl
, "action_value");
5716 if (dpipe_tables_action_values_show(ctx
,
5717 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5718 goto err_action_values_show
;
5719 pr_out_array_end(ctx
->dl
);
5722 err_action_values_show
:
5723 err_match_values_show
:
5724 pr_out_array_end(ctx
->dl
);
5728 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5730 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5731 struct nlattr
*nla_entry
;
5733 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5734 pr_out_handle_start_arr(ctx
->dl
, tb
);
5735 if (dpipe_entry_show(ctx
, nla_entry
))
5736 goto err_entry_show
;
5737 pr_out_handle_end(ctx
->dl
);
5742 pr_out_handle_end(ctx
->dl
);
5746 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5748 struct dpipe_ctx
*ctx
= data
;
5749 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5750 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5752 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5753 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5754 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5755 return MNL_CB_ERROR
;
5757 if (dpipe_table_entries_show(ctx
, tb
))
5758 return MNL_CB_ERROR
;
5762 static int cmd_dpipe_table_dump(struct dl
*dl
)
5764 struct nlmsghdr
*nlh
;
5765 struct dpipe_ctx ctx
= {};
5766 uint16_t flags
= NLM_F_REQUEST
;
5769 err
= dpipe_ctx_init(&ctx
, dl
);
5773 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5777 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5778 dl_opts_put(nlh
, dl
);
5779 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5781 pr_err("error get headers %s\n", strerror(ctx
.err
));
5785 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5786 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5787 dl_opts_put(nlh
, dl
);
5789 pr_out_section_start(dl
, "table_entry");
5790 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5791 pr_out_section_end(dl
);
5793 dpipe_ctx_fini(&ctx
);
5797 static void cmd_dpipe_table_help(void)
5799 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5800 "where OBJECT-LIST := { show | set | dump }\n");
5803 static int cmd_dpipe_table(struct dl
*dl
)
5805 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5806 cmd_dpipe_table_help();
5808 } else if (dl_argv_match(dl
, "show")) {
5810 return cmd_dpipe_table_show(dl
);
5811 } else if (dl_argv_match(dl
, "set")) {
5813 return cmd_dpipe_table_set(dl
);
5814 } else if (dl_argv_match(dl
, "dump")) {
5816 return cmd_dpipe_table_dump(dl
);
5818 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5822 static void cmd_dpipe_help(void)
5824 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5825 "where OBJECT-LIST := { header | table }\n");
5828 static int cmd_dpipe(struct dl
*dl
)
5830 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5833 } else if (dl_argv_match(dl
, "header")) {
5835 return cmd_dpipe_header(dl
);
5836 } else if (dl_argv_match(dl
, "table")) {
5838 return cmd_dpipe_table(dl
);
5840 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5845 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5846 struct nlattr
**nla_resource
)
5848 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5849 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5850 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5851 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5852 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5853 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5854 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5858 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5859 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5860 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5861 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5862 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5863 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5864 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5866 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5867 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5869 resource
->size_new
= resource
->size
;
5871 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5872 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5873 resource
->occ_valid
= true;
5876 if (resource
->size_new
!= resource
->size
)
5877 ctx
->pending_change
= true;
5883 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5884 struct resource
*parent_resource
, struct nlattr
*nl
)
5886 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5887 struct nlattr
*nla_child_resource
;
5888 struct nlattr
*nla_resources
;
5898 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5899 if (err
!= MNL_CB_OK
)
5902 err
= resource_parse(ctx
, resource
, nla_resource
);
5906 resource
->parent
= parent_resource
;
5907 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5910 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5911 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5913 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5914 struct resource
*child_resource
;
5915 struct list_head
*list
;
5917 child_resource
= resource_alloc();
5918 if (!child_resource
)
5922 list
= &ctx
->resources
->resource_list
;
5924 list
= &resource
->resource_list
;
5926 list_add_tail(&child_resource
->list
, list
);
5927 err
= resource_get(ctx
, child_resource
, resource
,
5928 nla_child_resource
);
5936 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5939 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5940 default: return "<unknown unit>";
5944 static void resource_show(struct resource
*resource
,
5945 struct resource_ctx
*ctx
)
5947 struct resource
*child_resource
;
5948 struct dpipe_table
*table
;
5949 struct dl
*dl
= ctx
->dl
;
5952 check_indent_newline(dl
);
5953 print_string(PRINT_ANY
, "name", "name %s", resource
->name
);
5955 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5956 pr_out_u64(dl
, "size", resource
->size
);
5957 if (resource
->size
!= resource
->size_new
)
5958 pr_out_u64(dl
, "size_new", resource
->size_new
);
5959 if (resource
->occ_valid
)
5960 print_uint(PRINT_ANY
, "occ", " occ %u", resource
->size_occ
);
5961 print_string(PRINT_ANY
, "unit", " unit %s",
5962 resource_unit_str_get(resource
->unit
));
5964 if (resource
->size_min
!= resource
->size_max
) {
5965 print_uint(PRINT_ANY
, "size_min", " size_min %u",
5966 resource
->size_min
);
5967 pr_out_u64(dl
, "size_max", resource
->size_max
);
5968 print_uint(PRINT_ANY
, "size_gran", " size_gran %u",
5969 resource
->size_gran
);
5972 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5973 if (table
->resource_id
== resource
->id
&&
5974 table
->resource_valid
)
5978 pr_out_array_start(dl
, "dpipe_tables");
5980 print_string(PRINT_ANY
, "dpipe_tables", " dpipe_tables none",
5983 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5984 if (table
->resource_id
!= resource
->id
||
5985 !table
->resource_valid
)
5987 pr_out_entry_start(dl
);
5988 check_indent_newline(dl
);
5989 print_string(PRINT_ANY
, "table_name", "table_name %s",
5991 pr_out_entry_end(dl
);
5994 pr_out_array_end(dl
);
5996 if (list_empty(&resource
->resource_list
))
5999 if (ctx
->pending_change
) {
6000 check_indent_newline(dl
);
6001 print_string(PRINT_ANY
, "size_valid", "size_valid %s",
6002 resource
->size_valid
? "true" : "false");
6004 pr_out_array_start(dl
, "resources");
6005 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
6006 pr_out_entry_start(dl
);
6007 resource_show(child_resource
, ctx
);
6008 pr_out_entry_end(dl
);
6010 pr_out_array_end(dl
);
6014 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6016 struct resources
*resources
= ctx
->resources
;
6017 struct resource
*resource
;
6019 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
6020 pr_out_handle_start_arr(ctx
->dl
, tb
);
6021 resource_show(resource
, ctx
);
6022 pr_out_handle_end(ctx
->dl
);
6026 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
6028 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
6031 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
6033 struct resource_ctx
*ctx
= data
;
6034 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6035 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6038 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6039 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6040 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
6041 return MNL_CB_ERROR
;
6043 err
= resources_get(ctx
, tb
);
6046 return MNL_CB_ERROR
;
6049 if (ctx
->print_resources
)
6050 resources_show(ctx
, tb
);
6055 static int cmd_resource_show(struct dl
*dl
)
6057 struct nlmsghdr
*nlh
;
6058 struct dpipe_ctx dpipe_ctx
= {};
6059 struct resource_ctx resource_ctx
= {};
6062 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
6066 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
6068 dl_opts_put(nlh
, dl
);
6070 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
6074 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
6077 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
6081 err
= resource_ctx_init(&resource_ctx
, dl
);
6085 resource_ctx
.print_resources
= true;
6086 resource_ctx
.tables
= dpipe_ctx
.tables
;
6087 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6088 NLM_F_REQUEST
| NLM_F_ACK
);
6089 dl_opts_put(nlh
, dl
);
6090 pr_out_section_start(dl
, "resources");
6091 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
6093 pr_out_section_end(dl
);
6094 resource_ctx_fini(&resource_ctx
);
6096 dpipe_ctx_fini(&dpipe_ctx
);
6100 static void cmd_resource_help(void)
6102 pr_err("Usage: devlink resource show DEV\n"
6103 " devlink resource set DEV path PATH size SIZE\n");
6106 static struct resource
*
6107 resource_find_by_name(struct list_head
*list
, char *name
)
6109 struct resource
*resource
;
6111 list_for_each_entry(resource
, list
, list
) {
6112 if (!strcmp(resource
->name
, name
))
6119 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6120 uint32_t *p_resource_id
, bool *p_resource_valid
)
6122 struct resource
*resource
;
6123 uint32_t resource_id
= 0;
6124 char *resource_path_dup
;
6125 struct list_head
*list
;
6126 const char del
[] = "/";
6127 char *resource_name
;
6129 resource_path_dup
= strdup(resource_path
);
6130 list
= &ctx
->resources
->resource_list
;
6131 resource_name
= strtok(resource_path_dup
, del
);
6132 while (resource_name
!= NULL
) {
6133 resource
= resource_find_by_name(list
, resource_name
);
6135 goto err_resource_lookup
;
6137 list
= &resource
->resource_list
;
6138 resource_name
= strtok(NULL
, del
);
6139 resource_id
= resource
->id
;
6141 free(resource_path_dup
);
6142 *p_resource_valid
= true;
6143 *p_resource_id
= resource_id
;
6146 err_resource_lookup
:
6147 free(resource_path_dup
);
6151 static int cmd_resource_set(struct dl
*dl
)
6153 struct nlmsghdr
*nlh
;
6154 struct resource_ctx ctx
= {};
6157 err
= resource_ctx_init(&ctx
, dl
);
6161 ctx
.print_resources
= false;
6162 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6163 DL_OPT_RESOURCE_SIZE
, 0);
6167 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6169 dl_opts_put(nlh
, dl
);
6170 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6172 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6176 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6177 &dl
->opts
.resource_id
,
6178 &dl
->opts
.resource_id_valid
);
6180 pr_err("error parsing resource path %s\n", strerror(-err
));
6184 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6185 NLM_F_REQUEST
| NLM_F_ACK
);
6187 dl_opts_put(nlh
, dl
);
6188 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6190 resource_ctx_fini(&ctx
);
6194 static int cmd_resource(struct dl
*dl
)
6196 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6197 cmd_resource_help();
6199 } else if (dl_argv_match(dl
, "show")) {
6201 return cmd_resource_show(dl
);
6202 } else if (dl_argv_match(dl
, "set")) {
6204 return cmd_resource_set(dl
);
6206 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6210 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6212 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6213 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6214 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6217 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6218 if (dl
->json_output
)
6219 open_json_object(buf
);
6224 static void pr_out_region_handle_end(struct dl
*dl
)
6226 if (dl
->json_output
)
6227 close_json_object();
6232 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6234 __pr_out_indent_newline(dl
);
6235 if (dl
->json_output
)
6236 open_json_array(PRINT_JSON
, "snapshot");
6238 pr_out("snapshot %s", array
? "[" : "");
6241 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6243 if (dl
->json_output
)
6244 close_json_array(PRINT_JSON
, NULL
);
6249 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6251 uint32_t snapshot_id
;
6253 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6256 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6258 if (dl
->json_output
)
6259 print_uint(PRINT_JSON
, NULL
, NULL
, snapshot_id
);
6261 pr_out("%s%u", index
? " " : "", snapshot_id
);
6264 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6266 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6267 struct nlattr
*nla_sanpshot
;
6270 pr_out_region_snapshots_start(dl
, true);
6271 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6272 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6273 if (err
!= MNL_CB_OK
)
6275 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6277 pr_out_region_snapshots_end(dl
, true);
6280 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6282 pr_out_region_snapshots_start(dl
, false);
6283 pr_out_region_snapshots_id(dl
, tb
, 0);
6284 pr_out_region_snapshots_end(dl
, false);
6287 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6289 pr_out_region_handle_start(dl
, tb
);
6291 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6292 pr_out_u64(dl
, "size",
6293 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6295 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6296 pr_out_snapshots(dl
, tb
);
6298 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6299 pr_out_snapshot(dl
, tb
);
6301 pr_out_region_handle_end(dl
);
6304 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6306 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6307 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6308 struct dl
*dl
= data
;
6310 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6311 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6312 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6313 return MNL_CB_ERROR
;
6315 pr_out_region(dl
, tb
);
6320 static int cmd_region_show(struct dl
*dl
)
6322 struct nlmsghdr
*nlh
;
6323 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6326 if (dl_argc(dl
) == 0)
6327 flags
|= NLM_F_DUMP
;
6329 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6331 if (dl_argc(dl
) > 0) {
6332 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6337 pr_out_section_start(dl
, "regions");
6338 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6339 pr_out_section_end(dl
);
6343 static int cmd_region_snapshot_del(struct dl
*dl
)
6345 struct nlmsghdr
*nlh
;
6348 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6349 NLM_F_REQUEST
| NLM_F_ACK
);
6351 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6352 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6356 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6359 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6361 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6362 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6363 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6364 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6365 struct dl
*dl
= data
;
6368 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6369 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6370 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6371 return MNL_CB_ERROR
;
6373 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6374 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6375 if (err
!= MNL_CB_OK
)
6376 return MNL_CB_ERROR
;
6378 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6379 if (!nla_chunk_data
)
6382 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6383 if (!nla_chunk_addr
)
6386 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6387 mnl_attr_get_payload_len(nla_chunk_data
),
6388 mnl_attr_get_u64(nla_chunk_addr
));
6393 static int cmd_region_dump(struct dl
*dl
)
6395 struct nlmsghdr
*nlh
;
6398 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6399 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6401 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6402 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6406 pr_out_section_start(dl
, "dump");
6407 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6408 pr_out_section_end(dl
);
6409 if (!dl
->json_output
)
6414 static int cmd_region_read(struct dl
*dl
)
6416 struct nlmsghdr
*nlh
;
6419 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6420 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6422 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6423 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6424 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6428 pr_out_section_start(dl
, "read");
6429 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6430 pr_out_section_end(dl
);
6431 if (!dl
->json_output
)
6436 static void cmd_region_help(void)
6438 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6439 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6440 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6441 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6444 static int cmd_region(struct dl
*dl
)
6446 if (dl_no_arg(dl
)) {
6447 return cmd_region_show(dl
);
6448 } else if (dl_argv_match(dl
, "help")) {
6451 } else if (dl_argv_match(dl
, "show")) {
6453 return cmd_region_show(dl
);
6454 } else if (dl_argv_match(dl
, "del")) {
6456 return cmd_region_snapshot_del(dl
);
6457 } else if (dl_argv_match(dl
, "dump")) {
6459 return cmd_region_dump(dl
);
6460 } else if (dl_argv_match(dl
, "read")) {
6462 return cmd_region_read(dl
);
6464 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6468 static int cmd_health_set_params(struct dl
*dl
)
6470 struct nlmsghdr
*nlh
;
6473 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6474 NLM_F_REQUEST
| NLM_F_ACK
);
6475 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6476 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6477 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6481 dl_opts_put(nlh
, dl
);
6482 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6485 static int cmd_health_dump_clear(struct dl
*dl
)
6487 struct nlmsghdr
*nlh
;
6490 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6491 NLM_F_REQUEST
| NLM_F_ACK
);
6493 err
= dl_argv_parse_put(nlh
, dl
,
6494 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6498 dl_opts_put(nlh
, dl
);
6499 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6502 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6507 check_indent_newline(dl
);
6510 print_bool(PRINT_ANY
, NULL
, "%s", mnl_attr_get_u8(nl_data
));
6513 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u8(nl_data
));
6516 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u16(nl_data
));
6519 print_uint(PRINT_ANY
, NULL
, "%u", mnl_attr_get_u32(nl_data
));
6522 print_u64(PRINT_ANY
, NULL
, "%"PRIu64
, mnl_attr_get_u64(nl_data
));
6524 case MNL_TYPE_NUL_STRING
:
6525 print_string(PRINT_ANY
, NULL
, "%s", mnl_attr_get_str(nl_data
));
6527 case MNL_TYPE_BINARY
:
6528 len
= mnl_attr_get_payload_len(nl_data
);
6529 data
= mnl_attr_get_payload(nl_data
);
6530 pr_out_binary_value(dl
, data
, len
);
6538 static void pr_out_fmsg_name(struct dl
*dl
, char **name
)
6543 pr_out_name(dl
, *name
);
6550 struct list_head list
;
6553 struct fmsg_cb_data
{
6557 struct list_head entry_list
;
6560 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6561 uint8_t *attr_value
, bool insert
)
6563 struct nest_entry
*entry
;
6566 entry
= malloc(sizeof(struct nest_entry
));
6570 entry
->attr_type
= *attr_value
;
6571 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6573 if (list_empty(&fmsg_data
->entry_list
))
6574 return MNL_CB_ERROR
;
6575 entry
= list_first_entry(&fmsg_data
->entry_list
,
6576 struct nest_entry
, list
);
6577 *attr_value
= entry
->attr_type
;
6578 list_del(&entry
->list
);
6584 static void pr_out_fmsg_group_start(struct dl
*dl
, char **name
)
6587 pr_out_fmsg_name(dl
, name
);
6589 __pr_out_indent_inc();
6592 static void pr_out_fmsg_group_end(struct dl
*dl
)
6595 __pr_out_indent_dec();
6598 static void pr_out_fmsg_start_object(struct dl
*dl
, char **name
)
6600 if (dl
->json_output
) {
6601 pr_out_fmsg_name(dl
, name
);
6602 open_json_object(NULL
);
6604 pr_out_fmsg_group_start(dl
, name
);
6608 static void pr_out_fmsg_end_object(struct dl
*dl
)
6610 if (dl
->json_output
)
6611 close_json_object();
6613 pr_out_fmsg_group_end(dl
);
6616 static void pr_out_fmsg_start_array(struct dl
*dl
, char **name
)
6618 if (dl
->json_output
) {
6619 pr_out_fmsg_name(dl
, name
);
6620 open_json_array(PRINT_JSON
, NULL
);
6622 pr_out_fmsg_group_start(dl
, name
);
6626 static void pr_out_fmsg_end_array(struct dl
*dl
)
6628 if (dl
->json_output
)
6629 close_json_array(PRINT_JSON
, NULL
);
6631 pr_out_fmsg_group_end(dl
);
6634 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6637 struct dl
*dl
= fmsg_data
->dl
;
6638 uint8_t value
= nest_value
;
6641 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6642 if (err
!= MNL_CB_OK
)
6646 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6648 pr_out_fmsg_start_object(dl
, &fmsg_data
->name
);
6650 pr_out_fmsg_end_object(dl
);
6652 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6654 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6656 pr_out_fmsg_start_array(dl
, &fmsg_data
->name
);
6658 pr_out_fmsg_end_array(dl
);
6666 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6668 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6669 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6670 struct fmsg_cb_data
*fmsg_data
= data
;
6671 struct dl
*dl
= fmsg_data
->dl
;
6672 struct nlattr
*nla_object
;
6676 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6677 if (!tb
[DEVLINK_ATTR_FMSG
])
6678 return MNL_CB_ERROR
;
6680 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6681 attr_type
= mnl_attr_get_type(nla_object
);
6682 switch (attr_type
) {
6683 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6684 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6685 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6686 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6687 if (err
!= MNL_CB_OK
)
6690 case DEVLINK_ATTR_FMSG_NEST_END
:
6691 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6692 if (err
!= MNL_CB_OK
)
6695 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6696 free(fmsg_data
->name
);
6697 fmsg_data
->name
= strdup(mnl_attr_get_str(nla_object
));
6698 if (!fmsg_data
->name
)
6701 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6702 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6704 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6705 pr_out_fmsg_name(dl
, &fmsg_data
->name
);
6706 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6708 if (err
!= MNL_CB_OK
)
6718 static void cmd_fmsg_init(struct dl
*dl
, struct fmsg_cb_data
*data
)
6720 /* FMSG is dynamic: opening of an object or array causes a
6721 * newline. JSON starts with an { or [, but plain text should
6722 * not start with a new line. Ensure this by setting
6723 * g_new_line_count to 1: avoiding newline before the first
6726 g_new_line_count
= 1;
6729 INIT_LIST_HEAD(&data
->entry_list
);
6732 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6734 struct fmsg_cb_data data
;
6735 struct nlmsghdr
*nlh
;
6738 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6740 err
= dl_argv_parse_put(nlh
, dl
,
6741 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6745 cmd_fmsg_init(dl
, &data
);
6746 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6751 static int cmd_health_dump_show(struct dl
*dl
)
6753 return cmd_health_object_common(dl
,
6754 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6758 static int cmd_health_diagnose(struct dl
*dl
)
6760 return cmd_health_object_common(dl
,
6761 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6765 static int cmd_health_recover(struct dl
*dl
)
6767 struct nlmsghdr
*nlh
;
6770 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6771 NLM_F_REQUEST
| NLM_F_ACK
);
6773 err
= dl_argv_parse_put(nlh
, dl
,
6774 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6778 dl_opts_put(nlh
, dl
);
6779 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6782 enum devlink_health_reporter_state
{
6783 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6784 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6787 static const char *health_state_name(uint8_t state
)
6790 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6791 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6792 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6793 return HEALTH_REPORTER_STATE_ERROR_STR
;
6795 return "<unknown state>";
6799 static void pr_out_dump_reporter_format_logtime(struct dl
*dl
, const struct nlattr
*attr
)
6801 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6802 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6803 uint64_t time_ms
= mnl_attr_get_u64(attr
);
6804 struct sysinfo s_info
;
6810 info
= localtime(&now
);
6811 err
= sysinfo(&s_info
);
6814 /* Subtract uptime in sec from now yields the time of system
6815 * uptime. To this, add time_ms which is the amount of
6816 * milliseconds elapsed between uptime and the dump taken.
6818 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6819 info
= localtime(&sec
);
6821 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6822 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6823 check_indent_newline(dl
);
6824 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6825 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6828 static void pr_out_dump_report_timestamp(struct dl
*dl
, const struct nlattr
*attr
)
6830 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6831 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6836 ts
= mnl_attr_get_u64(attr
);
6837 tv_sec
= ts
/ 1000000000;
6838 tm
= localtime(&tv_sec
);
6840 strftime(dump_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", tm
);
6841 strftime(dump_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", tm
);
6843 check_indent_newline(dl
);
6844 print_string(PRINT_ANY
, "last_dump_date", "last_dump_date %s", dump_date
);
6845 print_string(PRINT_ANY
, "last_dump_time", " last_dump_time %s", dump_time
);
6848 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6850 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6851 enum devlink_health_reporter_state state
;
6854 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6856 if (err
!= MNL_CB_OK
)
6859 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6860 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6861 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6862 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6865 pr_out_handle_start_arr(dl
, tb_health
);
6867 check_indent_newline(dl
);
6868 print_string(PRINT_ANY
, "reporter", "reporter %s",
6869 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6870 if (!dl
->json_output
) {
6872 __pr_out_indent_inc();
6874 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6875 check_indent_newline(dl
);
6876 print_string(PRINT_ANY
, "state", "state %s", health_state_name(state
));
6877 pr_out_u64(dl
, "error",
6878 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6879 pr_out_u64(dl
, "recover",
6880 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6881 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
])
6882 pr_out_dump_report_timestamp(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
]);
6883 else if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
])
6884 pr_out_dump_reporter_format_logtime(dl
, tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]);
6885 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6886 pr_out_u64(dl
, "grace_period",
6887 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6888 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6889 print_bool(PRINT_ANY
, "auto_recover", " auto_recover %s",
6890 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6892 __pr_out_indent_dec();
6893 pr_out_handle_end(dl
);
6896 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6898 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6899 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6900 struct dl
*dl
= data
;
6902 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6903 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6904 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6905 return MNL_CB_ERROR
;
6907 pr_out_health(dl
, tb
);
6912 static int cmd_health_show(struct dl
*dl
)
6914 struct nlmsghdr
*nlh
;
6915 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6918 if (dl_argc(dl
) == 0)
6919 flags
|= NLM_F_DUMP
;
6920 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6923 if (dl_argc(dl
) > 0) {
6924 err
= dl_argv_parse_put(nlh
, dl
,
6926 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6930 pr_out_section_start(dl
, "health");
6932 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6933 pr_out_section_end(dl
);
6937 static void cmd_health_help(void)
6939 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6940 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6941 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6942 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6943 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6944 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6947 static int cmd_health(struct dl
*dl
)
6949 if (dl_argv_match(dl
, "help")) {
6952 } else if (dl_argv_match(dl
, "show") ||
6953 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6955 return cmd_health_show(dl
);
6956 } else if (dl_argv_match(dl
, "recover")) {
6958 return cmd_health_recover(dl
);
6959 } else if (dl_argv_match(dl
, "diagnose")) {
6961 return cmd_health_diagnose(dl
);
6962 } else if (dl_argv_match(dl
, "dump")) {
6964 if (dl_argv_match(dl
, "show")) {
6966 return cmd_health_dump_show(dl
);
6967 } else if (dl_argv_match(dl
, "clear")) {
6969 return cmd_health_dump_clear(dl
);
6971 } else if (dl_argv_match(dl
, "set")) {
6973 return cmd_health_set_params(dl
);
6975 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6979 static const char *trap_type_name(uint8_t type
)
6982 case DEVLINK_TRAP_TYPE_DROP
:
6984 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6987 return "<unknown type>";
6991 static const char *trap_action_name(uint8_t action
)
6994 case DEVLINK_TRAP_ACTION_DROP
:
6996 case DEVLINK_TRAP_ACTION_TRAP
:
6999 return "<unknown action>";
7003 static const char *trap_metadata_name(const struct nlattr
*attr
)
7005 switch (attr
->nla_type
) {
7006 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
7007 return "input_port";
7008 case DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
:
7009 return "flow_action_cookie";
7011 return "<unknown metadata type>";
7014 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
7016 struct nlattr
*attr_metadata
;
7018 pr_out_array_start(dl
, "metadata");
7019 mnl_attr_for_each_nested(attr_metadata
, attr
) {
7020 check_indent_newline(dl
);
7021 print_string(PRINT_ANY
, NULL
, "%s",
7022 trap_metadata_name(attr_metadata
));
7024 pr_out_array_end(dl
);
7027 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7029 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
7030 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
7033 pr_out_handle_start_arr(dl
, tb
);
7035 __pr_out_handle_start(dl
, tb
, true, false);
7037 check_indent_newline(dl
);
7038 print_string(PRINT_ANY
, "name", "name %s",
7039 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
7040 print_string(PRINT_ANY
, "type", " type %s", trap_type_name(type
));
7041 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7042 print_string(PRINT_ANY
, "action", " action %s", trap_action_name(action
));
7043 print_string(PRINT_ANY
, "group", " group %s",
7044 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7046 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
7047 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7048 pr_out_handle_end(dl
);
7051 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7053 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7054 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7055 struct dl
*dl
= data
;
7057 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7058 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7059 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
7060 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
7061 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
7062 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
7063 return MNL_CB_ERROR
;
7065 pr_out_trap(dl
, tb
, true);
7070 static void cmd_trap_help(void)
7072 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
7073 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
7074 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
7075 pr_err(" [ policer POLICER ] [ nopolicer ]\n");
7076 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
7077 pr_err(" devlink trap policer set DEV policer POLICER [ rate RATE ] [ burst BURST ]\n");
7078 pr_err(" devlink trap policer show DEV policer POLICER\n");
7081 static int cmd_trap_show(struct dl
*dl
)
7083 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7084 struct nlmsghdr
*nlh
;
7087 if (dl_argc(dl
) == 0)
7088 flags
|= NLM_F_DUMP
;
7090 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
7092 if (dl_argc(dl
) > 0) {
7093 err
= dl_argv_parse_put(nlh
, dl
,
7094 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
7099 pr_out_section_start(dl
, "trap");
7100 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
7101 pr_out_section_end(dl
);
7106 static int cmd_trap_set(struct dl
*dl
)
7108 struct nlmsghdr
*nlh
;
7111 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
7112 NLM_F_REQUEST
| NLM_F_ACK
);
7114 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
7115 DL_OPT_TRAP_ACTION
);
7119 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7122 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7125 pr_out_handle_start_arr(dl
, tb
);
7127 __pr_out_handle_start(dl
, tb
, true, false);
7129 check_indent_newline(dl
);
7130 print_string(PRINT_ANY
, "name", "name %s",
7131 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
7132 print_bool(PRINT_ANY
, "generic", " generic %s", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
7133 if (tb
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7134 print_uint(PRINT_ANY
, "policer", " policer %u",
7135 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7136 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7137 pr_out_handle_end(dl
);
7140 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7142 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7143 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7144 struct dl
*dl
= data
;
7146 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7147 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7148 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
7149 return MNL_CB_ERROR
;
7151 pr_out_trap_group(dl
, tb
, true);
7156 static int cmd_trap_group_show(struct dl
*dl
)
7158 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7159 struct nlmsghdr
*nlh
;
7162 if (dl_argc(dl
) == 0)
7163 flags
|= NLM_F_DUMP
;
7165 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
7167 if (dl_argc(dl
) > 0) {
7168 err
= dl_argv_parse_put(nlh
, dl
,
7169 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7175 pr_out_section_start(dl
, "trap_group");
7176 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
7177 pr_out_section_end(dl
);
7182 static int cmd_trap_group_set(struct dl
*dl
)
7184 struct nlmsghdr
*nlh
;
7187 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
7188 NLM_F_REQUEST
| NLM_F_ACK
);
7190 err
= dl_argv_parse_put(nlh
, dl
,
7191 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
7192 DL_OPT_TRAP_ACTION
| DL_OPT_TRAP_POLICER_ID
);
7196 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7199 static int cmd_trap_group(struct dl
*dl
)
7201 if (dl_argv_match(dl
, "help")) {
7204 } else if (dl_argv_match(dl
, "show") ||
7205 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7207 return cmd_trap_group_show(dl
);
7208 } else if (dl_argv_match(dl
, "set")) {
7210 return cmd_trap_group_set(dl
);
7212 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7216 static void pr_out_trap_policer(struct dl
*dl
, struct nlattr
**tb
, bool array
)
7219 pr_out_handle_start_arr(dl
, tb
);
7221 __pr_out_handle_start(dl
, tb
, true, false);
7223 check_indent_newline(dl
);
7224 print_uint(PRINT_ANY
, "policer", "policer %u",
7225 mnl_attr_get_u32(tb
[DEVLINK_ATTR_TRAP_POLICER_ID
]));
7226 print_u64(PRINT_ANY
, "rate", " rate %llu",
7227 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
]));
7228 print_u64(PRINT_ANY
, "burst", " burst %llu",
7229 mnl_attr_get_u64(tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
]));
7230 if (tb
[DEVLINK_ATTR_STATS
])
7231 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
7232 pr_out_handle_end(dl
);
7235 static int cmd_trap_policer_show_cb(const struct nlmsghdr
*nlh
, void *data
)
7237 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
7238 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
7239 struct dl
*dl
= data
;
7241 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
7242 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
7243 !tb
[DEVLINK_ATTR_TRAP_POLICER_ID
] ||
7244 !tb
[DEVLINK_ATTR_TRAP_POLICER_RATE
] ||
7245 !tb
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7246 return MNL_CB_ERROR
;
7248 pr_out_trap_policer(dl
, tb
, true);
7253 static int cmd_trap_policer_show(struct dl
*dl
)
7255 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
7256 struct nlmsghdr
*nlh
;
7259 if (dl_argc(dl
) == 0)
7260 flags
|= NLM_F_DUMP
;
7262 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_GET
, flags
);
7264 if (dl_argc(dl
) > 0) {
7265 err
= dl_argv_parse_put(nlh
, dl
,
7266 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7272 pr_out_section_start(dl
, "trap_policer");
7273 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_policer_show_cb
, dl
);
7274 pr_out_section_end(dl
);
7279 static int cmd_trap_policer_set(struct dl
*dl
)
7281 struct nlmsghdr
*nlh
;
7284 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_POLICER_SET
,
7285 NLM_F_REQUEST
| NLM_F_ACK
);
7287 err
= dl_argv_parse_put(nlh
, dl
,
7288 DL_OPT_HANDLE
| DL_OPT_TRAP_POLICER_ID
,
7289 DL_OPT_TRAP_POLICER_RATE
|
7290 DL_OPT_TRAP_POLICER_BURST
);
7294 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
7297 static int cmd_trap_policer(struct dl
*dl
)
7299 if (dl_argv_match(dl
, "help")) {
7302 } else if (dl_argv_match(dl
, "show") ||
7303 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7305 return cmd_trap_policer_show(dl
);
7306 } else if (dl_argv_match(dl
, "set")) {
7308 return cmd_trap_policer_set(dl
);
7310 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7314 static int cmd_trap(struct dl
*dl
)
7316 if (dl_argv_match(dl
, "help")) {
7319 } else if (dl_argv_match(dl
, "show") ||
7320 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7322 return cmd_trap_show(dl
);
7323 } else if (dl_argv_match(dl
, "set")) {
7325 return cmd_trap_set(dl
);
7326 } else if (dl_argv_match(dl
, "group")) {
7328 return cmd_trap_group(dl
);
7329 } else if (dl_argv_match(dl
, "policer")) {
7331 return cmd_trap_policer(dl
);
7333 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7337 static void help(void)
7339 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7340 " devlink [ -f[orce] ] -b[atch] filename -N[etns] netnsname\n"
7341 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7342 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7345 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7350 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7353 } else if (dl_argv_match(dl
, "dev")) {
7356 } else if (dl_argv_match(dl
, "port")) {
7358 return cmd_port(dl
);
7359 } else if (dl_argv_match(dl
, "sb")) {
7362 } else if (dl_argv_match(dl
, "monitor")) {
7365 } else if (dl_argv_match(dl
, "dpipe")) {
7367 return cmd_dpipe(dl
);
7368 } else if (dl_argv_match(dl
, "resource")) {
7370 return cmd_resource(dl
);
7371 } else if (dl_argv_match(dl
, "region")) {
7373 return cmd_region(dl
);
7374 } else if (dl_argv_match(dl
, "health")) {
7376 return cmd_health(dl
);
7377 } else if (dl_argv_match(dl
, "trap")) {
7379 return cmd_trap(dl
);
7381 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7385 static int dl_init(struct dl
*dl
)
7389 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7391 pr_err("Failed to connect to devlink Netlink\n");
7395 err
= ifname_map_init(dl
);
7397 pr_err("Failed to create index map\n");
7398 goto err_ifname_map_create
;
7400 new_json_obj_plain(dl
->json_output
);
7403 err_ifname_map_create
:
7404 mnlg_socket_close(dl
->nlg
);
7408 static void dl_fini(struct dl
*dl
)
7410 delete_json_obj_plain();
7411 ifname_map_fini(dl
);
7412 mnlg_socket_close(dl
->nlg
);
7415 static struct dl
*dl_alloc(void)
7419 dl
= calloc(1, sizeof(*dl
));
7425 static void dl_free(struct dl
*dl
)
7430 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7434 int ret
= EXIT_SUCCESS
;
7436 if (name
&& strcmp(name
, "-") != 0) {
7437 if (freopen(name
, "r", stdin
) == NULL
) {
7439 "Cannot open file \"%s\" for reading: %s\n",
7440 name
, strerror(errno
));
7441 return EXIT_FAILURE
;
7446 while (getcmdline(&line
, &len
, stdin
) != -1) {
7450 largc
= makeargs(line
, largv
, 100);
7452 continue; /* blank line */
7454 if (dl_cmd(dl
, largc
, largv
)) {
7455 fprintf(stderr
, "Command failed %s:%d\n",
7469 int main(int argc
, char **argv
)
7471 static const struct option long_options
[] = {
7472 { "Version", no_argument
, NULL
, 'V' },
7473 { "force", no_argument
, NULL
, 'f' },
7474 { "batch", required_argument
, NULL
, 'b' },
7475 { "no-nice-names", no_argument
, NULL
, 'n' },
7476 { "json", no_argument
, NULL
, 'j' },
7477 { "pretty", no_argument
, NULL
, 'p' },
7478 { "verbose", no_argument
, NULL
, 'v' },
7479 { "statistics", no_argument
, NULL
, 's' },
7480 { "Netns", required_argument
, NULL
, 'N' },
7481 { NULL
, 0, NULL
, 0 }
7483 const char *batch_file
= NULL
;
7492 pr_err("Failed to allocate memory for devlink\n");
7493 return EXIT_FAILURE
;
7496 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvsN:",
7497 long_options
, NULL
)) >= 0) {
7501 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7508 batch_file
= optarg
;
7511 dl
->no_nice_names
= true;
7514 dl
->json_output
= true;
7526 if (netns_switch(optarg
)) {
7532 pr_err("Unknown option.\n");
7549 err
= dl_batch(dl
, batch_file
, force
);
7551 err
= dl_cmd(dl
, argc
, argv
);