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"
36 #define ESWITCH_MODE_LEGACY "legacy"
37 #define ESWITCH_MODE_SWITCHDEV "switchdev"
38 #define ESWITCH_INLINE_MODE_NONE "none"
39 #define ESWITCH_INLINE_MODE_LINK "link"
40 #define ESWITCH_INLINE_MODE_NETWORK "network"
41 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
43 #define PARAM_CMODE_RUNTIME_STR "runtime"
44 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
45 #define PARAM_CMODE_PERMANENT_STR "permanent"
46 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
48 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
49 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
50 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
52 static int g_new_line_count
;
53 static int g_indent_level
;
54 static bool g_indent_newline
;
56 #define INDENT_STR_STEP 2
57 #define INDENT_STR_MAXLEN 32
58 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
60 static void __attribute__((format(printf
, 1, 2)))
61 pr_err(const char *fmt
, ...)
66 vfprintf(stderr
, fmt
, ap
);
70 static void __attribute__((format(printf
, 1, 2)))
71 pr_out(const char *fmt
, ...)
75 if (g_indent_newline
) {
76 printf("%s", g_indent_str
);
77 g_indent_newline
= false;
85 static void __attribute__((format(printf
, 2, 3)))
86 pr_out_sp(unsigned int num
, const char *fmt
, ...)
92 ret
= vprintf(fmt
, ap
);
96 printf("%*s", num
- ret
, "");
97 g_new_line_count
= 0; \
100 static void __attribute__((format(printf
, 1, 2)))
101 pr_out_tty(const char *fmt
, ...)
105 if (!isatty(STDOUT_FILENO
))
112 static void __pr_out_indent_inc(void)
114 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
116 g_indent_level
+= INDENT_STR_STEP
;
117 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
118 g_indent_str
[g_indent_level
] = '\0';
121 static void __pr_out_indent_dec(void)
123 if (g_indent_level
- INDENT_STR_STEP
< 0)
125 g_indent_level
-= INDENT_STR_STEP
;
126 g_indent_str
[g_indent_level
] = '\0';
129 static void __pr_out_newline(void)
131 if (g_new_line_count
< 1) {
133 g_indent_newline
= true;
138 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
139 mnl_cb_t data_cb
, void *data
)
143 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
145 pr_err("devlink answers: %s\n", strerror(errno
));
151 static int _mnlg_socket_send(struct mnlg_socket
*nlg
,
152 const struct nlmsghdr
*nlh
)
156 err
= mnlg_socket_send(nlg
, nlh
);
158 pr_err("Failed to call mnlg_socket_send\n");
164 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
165 const struct nlmsghdr
*nlh
,
166 mnl_cb_t data_cb
, void *data
)
170 err
= _mnlg_socket_send(nlg
, nlh
);
173 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
176 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
177 const char *group_name
)
181 err
= mnlg_socket_group_add(nlg
, group_name
);
183 pr_err("Failed to call mnlg_socket_group_add\n");
190 struct list_head list
;
197 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
198 const char *dev_name
,
202 struct ifname_map
*ifname_map
;
204 ifname_map
= calloc(1, sizeof(*ifname_map
));
207 ifname_map
->bus_name
= strdup(bus_name
);
208 ifname_map
->dev_name
= strdup(dev_name
);
209 ifname_map
->port_index
= port_index
;
210 ifname_map
->ifname
= strdup(ifname
);
211 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
212 !ifname_map
->ifname
) {
213 free(ifname_map
->ifname
);
214 free(ifname_map
->dev_name
);
215 free(ifname_map
->bus_name
);
222 static void ifname_map_free(struct ifname_map
*ifname_map
)
224 free(ifname_map
->ifname
);
225 free(ifname_map
->dev_name
);
226 free(ifname_map
->bus_name
);
230 #define DL_OPT_HANDLE BIT(0)
231 #define DL_OPT_HANDLEP BIT(1)
232 #define DL_OPT_PORT_TYPE BIT(2)
233 #define DL_OPT_PORT_COUNT BIT(3)
234 #define DL_OPT_SB BIT(4)
235 #define DL_OPT_SB_POOL BIT(5)
236 #define DL_OPT_SB_SIZE BIT(6)
237 #define DL_OPT_SB_TYPE BIT(7)
238 #define DL_OPT_SB_THTYPE BIT(8)
239 #define DL_OPT_SB_TH BIT(9)
240 #define DL_OPT_SB_TC BIT(10)
241 #define DL_OPT_ESWITCH_MODE BIT(11)
242 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
243 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
244 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
245 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
246 #define DL_OPT_RESOURCE_PATH BIT(16)
247 #define DL_OPT_RESOURCE_SIZE BIT(17)
248 #define DL_OPT_PARAM_NAME BIT(18)
249 #define DL_OPT_PARAM_VALUE BIT(19)
250 #define DL_OPT_PARAM_CMODE BIT(20)
251 #define DL_OPT_HANDLE_REGION BIT(21)
252 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
253 #define DL_OPT_REGION_ADDRESS BIT(23)
254 #define DL_OPT_REGION_LENGTH BIT(24)
255 #define DL_OPT_FLASH_FILE_NAME BIT(25)
256 #define DL_OPT_FLASH_COMPONENT BIT(26)
257 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
258 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(28)
259 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(29)
260 #define DL_OPT_TRAP_NAME BIT(30)
261 #define DL_OPT_TRAP_ACTION BIT(31)
262 #define DL_OPT_TRAP_GROUP_NAME BIT(32)
265 uint64_t present
; /* flags of present items */
269 enum devlink_port_type port_type
;
272 uint16_t sb_pool_index
;
273 uint32_t sb_pool_size
;
274 enum devlink_sb_pool_type sb_pool_type
;
275 enum devlink_sb_threshold_type sb_pool_thtype
;
276 uint32_t sb_threshold
;
277 uint16_t sb_tc_index
;
278 enum devlink_eswitch_mode eswitch_mode
;
279 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
280 const char *dpipe_table_name
;
281 bool dpipe_counters_enable
;
282 bool eswitch_encap_mode
;
283 const char *resource_path
;
284 uint32_t resource_size
;
285 uint32_t resource_id
;
286 bool resource_id_valid
;
287 const char *param_name
;
288 const char *param_value
;
289 enum devlink_param_cmode cmode
;
291 uint32_t region_snapshot_id
;
292 uint64_t region_address
;
293 uint64_t region_length
;
294 const char *flash_file_name
;
295 const char *flash_component
;
296 const char *reporter_name
;
297 uint64_t reporter_graceful_period
;
298 bool reporter_auto_recover
;
299 const char *trap_name
;
300 const char *trap_group_name
;
301 enum devlink_trap_action trap_action
;
305 struct mnlg_socket
*nlg
;
306 struct list_head ifname_map_list
;
324 static int dl_argc(struct dl
*dl
)
329 static char *dl_argv(struct dl
*dl
)
331 if (dl_argc(dl
) == 0)
336 static void dl_arg_inc(struct dl
*dl
)
338 if (dl_argc(dl
) == 0)
344 static char *dl_argv_next(struct dl
*dl
)
348 if (dl_argc(dl
) == 0)
356 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
358 if (index
>= dl_argc(dl
))
360 return dl
->argv
[index
];
363 static int strcmpx(const char *str1
, const char *str2
)
365 if (strlen(str1
) > strlen(str2
))
367 return strncmp(str1
, str2
, strlen(str1
));
370 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
372 if (dl_argc(dl
) == 0)
374 return strcmpx(dl_argv(dl
), pattern
) == 0;
377 static bool dl_no_arg(struct dl
*dl
)
379 return dl_argc(dl
) == 0;
382 static void __pr_out_indent_newline(struct dl
*dl
)
384 if (!g_indent_newline
&& !dl
->json_output
)
388 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
389 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
390 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
391 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
392 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
393 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
394 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
395 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
396 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
397 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
398 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
399 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
400 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
401 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
402 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
403 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
404 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
405 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
406 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
407 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
408 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
409 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
410 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
411 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
412 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
413 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
414 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
415 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
416 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
417 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
418 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
419 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
420 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
421 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
422 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
423 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
424 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
425 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
426 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
427 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
428 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
429 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
430 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
431 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
432 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
433 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
434 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
435 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
436 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
437 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
438 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
439 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
440 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
441 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
442 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
443 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
444 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
445 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
446 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
447 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
448 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
449 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
450 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
451 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
452 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
453 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
454 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
455 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
456 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
457 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
458 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
459 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
460 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
461 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
462 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
463 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
464 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
465 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
466 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
467 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
468 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
469 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
470 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
471 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
472 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
473 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
474 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
475 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
476 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
477 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
478 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
479 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
480 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
481 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
482 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
483 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
484 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
485 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
486 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
487 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
490 static const enum mnl_attr_data_type
491 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
492 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
493 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
496 static int attr_cb(const struct nlattr
*attr
, void *data
)
498 const struct nlattr
**tb
= data
;
501 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
504 type
= mnl_attr_get_type(attr
);
505 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
512 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
514 const struct nlattr
**tb
= data
;
517 /* Allow the tool to work on top of newer kernels that might contain
520 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
523 type
= mnl_attr_get_type(attr
);
524 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
531 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
533 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
534 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
535 struct dl
*dl
= data
;
536 struct ifname_map
*ifname_map
;
537 const char *bus_name
;
538 const char *dev_name
;
539 uint32_t port_ifindex
;
540 const char *port_ifname
;
542 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
543 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
544 !tb
[DEVLINK_ATTR_PORT_INDEX
])
547 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
550 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
551 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
552 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
553 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
554 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
555 port_ifindex
, port_ifname
);
558 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
563 static void ifname_map_fini(struct dl
*dl
)
565 struct ifname_map
*ifname_map
, *tmp
;
567 list_for_each_entry_safe(ifname_map
, tmp
,
568 &dl
->ifname_map_list
, list
) {
569 list_del(&ifname_map
->list
);
570 ifname_map_free(ifname_map
);
574 static int ifname_map_init(struct dl
*dl
)
576 struct nlmsghdr
*nlh
;
579 INIT_LIST_HEAD(&dl
->ifname_map_list
);
581 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
582 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
584 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
592 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
593 char **p_bus_name
, char **p_dev_name
,
594 uint32_t *p_port_index
)
596 struct ifname_map
*ifname_map
;
598 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
599 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
600 *p_bus_name
= ifname_map
->bus_name
;
601 *p_dev_name
= ifname_map
->dev_name
;
602 *p_port_index
= ifname_map
->port_index
;
609 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
610 const char *dev_name
, uint32_t port_index
,
613 struct ifname_map
*ifname_map
;
615 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
616 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
617 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
618 port_index
== ifname_map
->port_index
) {
619 *p_ifname
= ifname_map
->ifname
;
626 static unsigned int strslashcount(char *str
)
628 unsigned int count
= 0;
631 while ((pos
= strchr(pos
, '/'))) {
638 static int strslashrsplit(char *str
, char **before
, char **after
)
642 slash
= strrchr(str
, '/');
651 static int strtouint64_t(const char *str
, uint64_t *p_val
)
654 unsigned long long int val
;
656 val
= strtoull(str
, &endptr
, 10);
657 if (endptr
== str
|| *endptr
!= '\0')
665 static int strtouint32_t(const char *str
, uint32_t *p_val
)
668 unsigned long int val
;
670 val
= strtoul(str
, &endptr
, 10);
671 if (endptr
== str
|| *endptr
!= '\0')
679 static int strtouint16_t(const char *str
, uint16_t *p_val
)
682 unsigned long int val
;
684 val
= strtoul(str
, &endptr
, 10);
685 if (endptr
== str
|| *endptr
!= '\0')
693 static int strtouint8_t(const char *str
, uint8_t *p_val
)
696 unsigned long int val
;
698 val
= strtoul(str
, &endptr
, 10);
699 if (endptr
== str
|| *endptr
!= '\0')
707 static int strtobool(const char *str
, bool *p_val
)
711 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
713 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
721 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
723 strslashrsplit(str
, p_bus_name
, p_dev_name
);
727 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
729 char *str
= dl_argv_next(dl
);
732 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
735 if (strslashcount(str
) != 1) {
736 pr_err("Wrong devlink identification string format.\n");
737 pr_err("Expected \"bus_name/dev_name\".\n");
740 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
743 static int __dl_argv_handle_port(char *str
,
744 char **p_bus_name
, char **p_dev_name
,
745 uint32_t *p_port_index
)
751 err
= strslashrsplit(str
, &handlestr
, &portstr
);
753 pr_err("Port identification \"%s\" is invalid\n", str
);
756 err
= strtouint32_t(portstr
, p_port_index
);
758 pr_err("Port index \"%s\" is not a number or not within range\n",
762 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
764 pr_err("Port identification \"%s\" is invalid\n", str
);
770 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
771 char **p_bus_name
, char **p_dev_name
,
772 uint32_t *p_port_index
)
776 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
779 pr_err("Netdevice \"%s\" not found\n", str
);
785 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
786 char **p_dev_name
, uint32_t *p_port_index
)
788 char *str
= dl_argv_next(dl
);
789 unsigned int slash_count
;
792 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
795 slash_count
= strslashcount(str
);
796 switch (slash_count
) {
798 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
799 p_dev_name
, p_port_index
);
801 return __dl_argv_handle_port(str
, p_bus_name
,
802 p_dev_name
, p_port_index
);
804 pr_err("Wrong port identification string format.\n");
805 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
810 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
811 char **p_dev_name
, uint32_t *p_port_index
,
812 uint64_t *p_handle_bit
)
814 char *str
= dl_argv_next(dl
);
815 unsigned int slash_count
;
819 pr_err("One of following identifications expected:\n"
820 "Devlink identification (\"bus_name/dev_name\")\n"
821 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
824 slash_count
= strslashcount(str
);
825 if (slash_count
== 1) {
826 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
829 *p_handle_bit
= DL_OPT_HANDLE
;
830 } else if (slash_count
== 2) {
831 err
= __dl_argv_handle_port(str
, p_bus_name
,
832 p_dev_name
, p_port_index
);
835 *p_handle_bit
= DL_OPT_HANDLEP
;
836 } else if (slash_count
== 0) {
837 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
838 p_dev_name
, p_port_index
);
841 *p_handle_bit
= DL_OPT_HANDLEP
;
843 pr_err("Wrong port identification string format.\n");
844 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
850 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
851 char **p_dev_name
, char **p_region
)
856 err
= strslashrsplit(str
, &handlestr
, p_region
);
858 pr_err("Region identification \"%s\" is invalid\n", str
);
861 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
863 pr_err("Region identification \"%s\" is invalid\n", str
);
869 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
870 char **p_dev_name
, char **p_region
)
872 char *str
= dl_argv_next(dl
);
873 unsigned int slash_count
;
876 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
880 slash_count
= strslashcount(str
);
881 if (slash_count
!= 2) {
882 pr_err("Wrong region identification string format.\n");
883 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
887 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
890 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
892 char *str
= dl_argv_next(dl
);
896 pr_err("Unsigned number argument expected\n");
900 err
= strtouint64_t(str
, p_val
);
902 pr_err("\"%s\" is not a number or not within range\n", str
);
908 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
910 char *str
= dl_argv_next(dl
);
914 pr_err("Unsigned number argument expected\n");
918 err
= strtouint32_t(str
, p_val
);
920 pr_err("\"%s\" is not a number or not within range\n", str
);
926 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
928 char *str
= dl_argv_next(dl
);
932 pr_err("Unsigned number argument expected\n");
936 err
= strtouint16_t(str
, p_val
);
938 pr_err("\"%s\" is not a number or not within range\n", str
);
944 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
946 char *str
= dl_argv_next(dl
);
950 pr_err("Boolean argument expected\n");
954 err
= strtobool(str
, p_val
);
956 pr_err("\"%s\" is not a valid boolean value\n", str
);
962 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
964 const char *str
= dl_argv_next(dl
);
967 pr_err("String parameter expected\n");
974 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
976 if (strcmp(typestr
, "auto") == 0) {
977 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
978 } else if (strcmp(typestr
, "eth") == 0) {
979 *p_type
= DEVLINK_PORT_TYPE_ETH
;
980 } else if (strcmp(typestr
, "ib") == 0) {
981 *p_type
= DEVLINK_PORT_TYPE_IB
;
983 pr_err("Unknown port type \"%s\"\n", typestr
);
989 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
991 if (strcmp(typestr
, "ingress") == 0) {
992 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
993 } else if (strcmp(typestr
, "egress") == 0) {
994 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
996 pr_err("Unknown pool type \"%s\"\n", typestr
);
1002 static int threshold_type_get(const char *typestr
,
1003 enum devlink_sb_threshold_type
*p_type
)
1005 if (strcmp(typestr
, "static") == 0) {
1006 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1007 } else if (strcmp(typestr
, "dynamic") == 0) {
1008 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1010 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1016 static int eswitch_mode_get(const char *typestr
,
1017 enum devlink_eswitch_mode
*p_mode
)
1019 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1020 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1021 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1022 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1024 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1030 static int eswitch_inline_mode_get(const char *typestr
,
1031 enum devlink_eswitch_inline_mode
*p_mode
)
1033 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1034 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1035 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1036 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1037 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1038 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1039 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1040 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1042 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1048 static int dpipe_counters_enable_get(const char *typestr
,
1049 bool *counters_enable
)
1051 if (strcmp(typestr
, "enable") == 0) {
1052 *counters_enable
= 1;
1053 } else if (strcmp(typestr
, "disable") == 0) {
1054 *counters_enable
= 0;
1056 pr_err("Unknown counter_state \"%s\"\n", typestr
);
1062 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
1064 if (strcmp(typestr
, "enable") == 0) {
1066 } else if (strcmp(typestr
, "disable") == 0) {
1069 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1075 static int param_cmode_get(const char *cmodestr
,
1076 enum devlink_param_cmode
*cmode
)
1078 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1079 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1080 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1081 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1082 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1083 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1085 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1091 static int trap_action_get(const char *actionstr
,
1092 enum devlink_trap_action
*p_action
)
1094 if (strcmp(actionstr
, "drop") == 0) {
1095 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1096 } else if (strcmp(actionstr
, "trap") == 0) {
1097 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1099 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1105 struct dl_args_metadata
{
1107 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1110 static const struct dl_args_metadata dl_args_required
[] = {
1111 {DL_OPT_PORT_TYPE
, "Port type not set."},
1112 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1113 {DL_OPT_SB_POOL
, "Pool index option expected."},
1114 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1115 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1116 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1117 {DL_OPT_SB_TH
, "Threshold option expected."},
1118 {DL_OPT_SB_TC
, "TC index option expected."},
1119 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1120 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1121 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1122 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1123 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1124 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1125 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1126 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1127 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1128 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1129 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1130 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1131 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1132 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1135 static int dl_args_finding_required_validate(uint64_t o_required
,
1141 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1142 o_flag
= dl_args_required
[i
].o_flag
;
1143 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1144 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1151 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1152 uint64_t o_optional
)
1154 struct dl_opts
*opts
= &dl
->opts
;
1155 uint64_t o_all
= o_required
| o_optional
;
1156 uint64_t o_found
= 0;
1159 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1160 uint64_t handle_bit
;
1162 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1163 &opts
->port_index
, &handle_bit
);
1166 o_found
|= handle_bit
;
1167 } else if (o_required
& DL_OPT_HANDLE
) {
1168 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1171 o_found
|= DL_OPT_HANDLE
;
1172 } else if (o_required
& DL_OPT_HANDLEP
) {
1173 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1177 o_found
|= DL_OPT_HANDLEP
;
1178 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1179 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1181 &opts
->region_name
);
1184 o_found
|= DL_OPT_HANDLE_REGION
;
1187 while (dl_argc(dl
)) {
1188 if (dl_argv_match(dl
, "type") &&
1189 (o_all
& DL_OPT_PORT_TYPE
)) {
1190 const char *typestr
;
1193 err
= dl_argv_str(dl
, &typestr
);
1196 err
= port_type_get(typestr
, &opts
->port_type
);
1199 o_found
|= DL_OPT_PORT_TYPE
;
1200 } else if (dl_argv_match(dl
, "count") &&
1201 (o_all
& DL_OPT_PORT_COUNT
)) {
1203 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1206 o_found
|= DL_OPT_PORT_COUNT
;
1207 } else if (dl_argv_match(dl
, "sb") &&
1208 (o_all
& DL_OPT_SB
)) {
1210 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1213 o_found
|= DL_OPT_SB
;
1214 } else if (dl_argv_match(dl
, "pool") &&
1215 (o_all
& DL_OPT_SB_POOL
)) {
1217 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1220 o_found
|= DL_OPT_SB_POOL
;
1221 } else if (dl_argv_match(dl
, "size") &&
1222 (o_all
& DL_OPT_SB_SIZE
)) {
1224 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1227 o_found
|= DL_OPT_SB_SIZE
;
1228 } else if (dl_argv_match(dl
, "type") &&
1229 (o_all
& DL_OPT_SB_TYPE
)) {
1230 const char *typestr
;
1233 err
= dl_argv_str(dl
, &typestr
);
1236 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1239 o_found
|= DL_OPT_SB_TYPE
;
1240 } else if (dl_argv_match(dl
, "thtype") &&
1241 (o_all
& DL_OPT_SB_THTYPE
)) {
1242 const char *typestr
;
1245 err
= dl_argv_str(dl
, &typestr
);
1248 err
= threshold_type_get(typestr
,
1249 &opts
->sb_pool_thtype
);
1252 o_found
|= DL_OPT_SB_THTYPE
;
1253 } else if (dl_argv_match(dl
, "th") &&
1254 (o_all
& DL_OPT_SB_TH
)) {
1256 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1259 o_found
|= DL_OPT_SB_TH
;
1260 } else if (dl_argv_match(dl
, "tc") &&
1261 (o_all
& DL_OPT_SB_TC
)) {
1263 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1266 o_found
|= DL_OPT_SB_TC
;
1267 } else if (dl_argv_match(dl
, "mode") &&
1268 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1269 const char *typestr
;
1272 err
= dl_argv_str(dl
, &typestr
);
1275 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1278 o_found
|= DL_OPT_ESWITCH_MODE
;
1279 } else if (dl_argv_match(dl
, "inline-mode") &&
1280 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1281 const char *typestr
;
1284 err
= dl_argv_str(dl
, &typestr
);
1287 err
= eswitch_inline_mode_get(
1288 typestr
, &opts
->eswitch_inline_mode
);
1291 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1292 } else if (dl_argv_match(dl
, "name") &&
1293 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1295 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1298 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1299 } else if (dl_argv_match(dl
, "counters") &&
1300 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1301 const char *typestr
;
1304 err
= dl_argv_str(dl
, &typestr
);
1307 err
= dpipe_counters_enable_get(typestr
,
1308 &opts
->dpipe_counters_enable
);
1311 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1312 } else if (dl_argv_match(dl
, "encap") &&
1313 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1314 const char *typestr
;
1317 err
= dl_argv_str(dl
, &typestr
);
1320 err
= eswitch_encap_mode_get(typestr
,
1321 &opts
->eswitch_encap_mode
);
1324 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1325 } else if (dl_argv_match(dl
, "path") &&
1326 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1328 err
= dl_argv_str(dl
, &opts
->resource_path
);
1331 o_found
|= DL_OPT_RESOURCE_PATH
;
1332 } else if (dl_argv_match(dl
, "size") &&
1333 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1335 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1338 o_found
|= DL_OPT_RESOURCE_SIZE
;
1339 } else if (dl_argv_match(dl
, "name") &&
1340 (o_all
& DL_OPT_PARAM_NAME
)) {
1342 err
= dl_argv_str(dl
, &opts
->param_name
);
1345 o_found
|= DL_OPT_PARAM_NAME
;
1346 } else if (dl_argv_match(dl
, "value") &&
1347 (o_all
& DL_OPT_PARAM_VALUE
)) {
1349 err
= dl_argv_str(dl
, &opts
->param_value
);
1352 o_found
|= DL_OPT_PARAM_VALUE
;
1353 } else if (dl_argv_match(dl
, "cmode") &&
1354 (o_all
& DL_OPT_PARAM_CMODE
)) {
1355 const char *cmodestr
;
1358 err
= dl_argv_str(dl
, &cmodestr
);
1361 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1364 o_found
|= DL_OPT_PARAM_CMODE
;
1365 } else if (dl_argv_match(dl
, "snapshot") &&
1366 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1368 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1371 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1372 } else if (dl_argv_match(dl
, "address") &&
1373 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1375 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1378 o_found
|= DL_OPT_REGION_ADDRESS
;
1379 } else if (dl_argv_match(dl
, "length") &&
1380 (o_all
& DL_OPT_REGION_LENGTH
)) {
1382 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1385 o_found
|= DL_OPT_REGION_LENGTH
;
1386 } else if (dl_argv_match(dl
, "file") &&
1387 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1389 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1392 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1393 } else if (dl_argv_match(dl
, "component") &&
1394 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1396 err
= dl_argv_str(dl
, &opts
->flash_component
);
1399 o_found
|= DL_OPT_FLASH_COMPONENT
;
1400 } else if (dl_argv_match(dl
, "reporter") &&
1401 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1403 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1406 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1407 } else if (dl_argv_match(dl
, "grace_period") &&
1408 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1410 err
= dl_argv_uint64_t(dl
,
1411 &opts
->reporter_graceful_period
);
1414 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1415 } else if (dl_argv_match(dl
, "auto_recover") &&
1416 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1418 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1421 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1422 } else if (dl_argv_match(dl
, "trap") &&
1423 (o_all
& DL_OPT_TRAP_NAME
)) {
1425 err
= dl_argv_str(dl
, &opts
->trap_name
);
1428 o_found
|= DL_OPT_TRAP_NAME
;
1429 } else if (dl_argv_match(dl
, "group") &&
1430 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1432 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1435 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1436 } else if (dl_argv_match(dl
, "action") &&
1437 (o_all
& DL_OPT_TRAP_ACTION
)) {
1438 const char *actionstr
;
1441 err
= dl_argv_str(dl
, &actionstr
);
1444 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1447 o_found
|= DL_OPT_TRAP_ACTION
;
1449 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1454 opts
->present
= o_found
;
1456 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1458 opts
->present
|= DL_OPT_SB
;
1461 return dl_args_finding_required_validate(o_required
, o_found
);
1464 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1466 struct dl_opts
*opts
= &dl
->opts
;
1468 if (opts
->present
& DL_OPT_HANDLE
) {
1469 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1470 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1471 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1472 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1473 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1474 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1476 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1477 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1478 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1479 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1482 if (opts
->present
& DL_OPT_PORT_TYPE
)
1483 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1485 if (opts
->present
& DL_OPT_PORT_COUNT
)
1486 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1488 if (opts
->present
& DL_OPT_SB
)
1489 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1491 if (opts
->present
& DL_OPT_SB_POOL
)
1492 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1493 opts
->sb_pool_index
);
1494 if (opts
->present
& DL_OPT_SB_SIZE
)
1495 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1496 opts
->sb_pool_size
);
1497 if (opts
->present
& DL_OPT_SB_TYPE
)
1498 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1499 opts
->sb_pool_type
);
1500 if (opts
->present
& DL_OPT_SB_THTYPE
)
1501 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1502 opts
->sb_pool_thtype
);
1503 if (opts
->present
& DL_OPT_SB_TH
)
1504 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1505 opts
->sb_threshold
);
1506 if (opts
->present
& DL_OPT_SB_TC
)
1507 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1509 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1510 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1511 opts
->eswitch_mode
);
1512 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1513 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1514 opts
->eswitch_inline_mode
);
1515 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1516 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1517 opts
->dpipe_table_name
);
1518 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1519 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1520 opts
->dpipe_counters_enable
);
1521 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1522 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1523 opts
->eswitch_encap_mode
);
1524 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1525 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1527 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1528 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1529 opts
->resource_size
);
1530 if (opts
->present
& DL_OPT_PARAM_NAME
)
1531 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1533 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1534 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1536 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1537 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1538 opts
->region_snapshot_id
);
1539 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1540 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1541 opts
->region_address
);
1542 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1543 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1544 opts
->region_length
);
1545 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1546 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1547 opts
->flash_file_name
);
1548 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1549 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1550 opts
->flash_component
);
1551 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1552 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1553 opts
->reporter_name
);
1554 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1555 mnl_attr_put_u64(nlh
,
1556 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1557 opts
->reporter_graceful_period
);
1558 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1559 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1560 opts
->reporter_auto_recover
);
1561 if (opts
->present
& DL_OPT_TRAP_NAME
)
1562 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1564 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1565 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1566 opts
->trap_group_name
);
1567 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1568 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1573 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1574 uint64_t o_required
, uint64_t o_optional
)
1578 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1581 dl_opts_put(nlh
, dl
);
1585 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1587 struct dl_opts
*opts
= &dl
->opts
;
1588 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1589 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1590 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1591 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1593 if (opts
->present
& DL_OPT_HANDLE
&&
1594 attr_bus_name
&& attr_dev_name
) {
1595 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1596 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1598 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1599 strcmp(dev_name
, opts
->dev_name
) != 0)
1602 if (opts
->present
& DL_OPT_HANDLEP
&&
1603 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1604 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1605 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1606 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1608 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1609 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1610 port_index
!= opts
->port_index
)
1613 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1614 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1616 if (sb_index
!= opts
->sb_index
)
1622 static void cmd_dev_help(void)
1624 pr_err("Usage: devlink dev show [ DEV ]\n");
1625 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1626 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1627 pr_err(" [ encap { disable | enable } ]\n");
1628 pr_err(" devlink dev eswitch show DEV\n");
1629 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1630 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1631 pr_err(" devlink dev reload DEV\n");
1632 pr_err(" devlink dev info [ DEV ]\n");
1633 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1636 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1637 const char *dev_name
)
1639 if (!dl
->arr_last
.present
)
1641 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1642 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1645 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1646 const char *dev_name
)
1648 dl
->arr_last
.present
= true;
1649 free(dl
->arr_last
.dev_name
);
1650 free(dl
->arr_last
.bus_name
);
1651 dl
->arr_last
.bus_name
= strdup(bus_name
);
1652 dl
->arr_last
.dev_name
= strdup(dev_name
);
1655 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1656 const char *dev_name
)
1658 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1661 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1662 const char *dev_name
)
1664 return dl
->arr_last
.present
&&
1665 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1668 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1669 bool content
, bool array
)
1671 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1672 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1675 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1677 if (dl
->json_output
) {
1679 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1680 jsonw_end_array(dl
->jw
);
1681 if (should_arr_last_handle_start(dl
, bus_name
,
1683 jsonw_name(dl
->jw
, buf
);
1684 jsonw_start_array(dl
->jw
);
1685 jsonw_start_object(dl
->jw
);
1686 arr_last_handle_set(dl
, bus_name
, dev_name
);
1688 jsonw_start_object(dl
->jw
);
1691 jsonw_name(dl
->jw
, buf
);
1692 jsonw_start_object(dl
->jw
);
1696 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1697 __pr_out_indent_dec();
1698 if (should_arr_last_handle_start(dl
, bus_name
,
1700 pr_out("%s%s", buf
, content
? ":" : "");
1702 __pr_out_indent_inc();
1703 arr_last_handle_set(dl
, bus_name
, dev_name
);
1706 pr_out("%s%s", buf
, content
? ":" : "");
1711 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1713 __pr_out_handle_start(dl
, tb
, true, true);
1716 static void pr_out_handle_end(struct dl
*dl
)
1718 if (dl
->json_output
)
1719 jsonw_end_object(dl
->jw
);
1724 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1726 __pr_out_handle_start(dl
, tb
, false, false);
1727 pr_out_handle_end(dl
);
1730 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1731 const char *dev_name
, uint32_t port_index
)
1733 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1734 dl
->arr_last
.port_index
== port_index
;
1737 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1738 const char *dev_name
, uint32_t port_index
)
1740 arr_last_handle_set(dl
, bus_name
, dev_name
);
1741 dl
->arr_last
.port_index
= port_index
;
1744 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1745 const char *bus_name
,
1746 const char *dev_name
,
1747 uint32_t port_index
)
1749 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1752 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1753 const char *bus_name
,
1754 const char *dev_name
,
1755 uint32_t port_index
)
1757 return dl
->arr_last
.present
&&
1758 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1761 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1762 const char *dev_name
,
1763 uint32_t port_index
, bool try_nice
,
1766 static char buf
[64];
1767 char *ifname
= NULL
;
1769 if (dl
->no_nice_names
|| !try_nice
||
1770 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1771 port_index
, &ifname
) != 0)
1772 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1774 sprintf(buf
, "%s", ifname
);
1776 if (dl
->json_output
) {
1778 if (should_arr_last_port_handle_end(dl
, bus_name
,
1781 jsonw_end_array(dl
->jw
);
1782 if (should_arr_last_port_handle_start(dl
, bus_name
,
1785 jsonw_name(dl
->jw
, buf
);
1786 jsonw_start_array(dl
->jw
);
1787 jsonw_start_object(dl
->jw
);
1788 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1791 jsonw_start_object(dl
->jw
);
1794 jsonw_name(dl
->jw
, buf
);
1795 jsonw_start_object(dl
->jw
);
1802 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1804 const char *bus_name
;
1805 const char *dev_name
;
1806 uint32_t port_index
;
1808 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1809 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1810 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1811 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1814 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1816 const char *bus_name
;
1817 const char *dev_name
;
1818 uint32_t port_index
;
1820 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1821 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1822 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1823 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1826 static void pr_out_port_handle_end(struct dl
*dl
)
1828 if (dl
->json_output
)
1829 jsonw_end_object(dl
->jw
);
1835 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1837 __pr_out_indent_newline(dl
);
1838 if (dl
->json_output
)
1839 jsonw_string_field(dl
->jw
, name
, val
);
1841 pr_out("%s %s", name
, val
);
1844 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1846 if (dl
->json_output
)
1847 jsonw_bool_field(dl
->jw
, name
, val
);
1849 pr_out_str(dl
, name
, val
? "true" : "false");
1852 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1854 __pr_out_indent_newline(dl
);
1855 if (dl
->json_output
)
1856 jsonw_uint_field(dl
->jw
, name
, val
);
1858 pr_out("%s %u", name
, val
);
1861 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1863 __pr_out_indent_newline(dl
);
1864 if (val
== (uint64_t) -1)
1865 return pr_out_str(dl
, name
, "unlimited");
1867 if (dl
->json_output
)
1868 jsonw_u64_field(dl
->jw
, name
, val
);
1870 pr_out("%s %"PRIu64
, name
, val
);
1873 static void pr_out_bool_value(struct dl
*dl
, bool value
)
1875 __pr_out_indent_newline(dl
);
1876 if (dl
->json_output
)
1877 jsonw_bool(dl
->jw
, value
);
1879 pr_out("%s", value
? "true" : "false");
1882 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
1884 __pr_out_indent_newline(dl
);
1885 if (dl
->json_output
)
1886 jsonw_uint(dl
->jw
, value
);
1888 pr_out("%u", value
);
1891 static void pr_out_uint64_value(struct dl
*dl
, uint64_t value
)
1893 __pr_out_indent_newline(dl
);
1894 if (dl
->json_output
)
1895 jsonw_u64(dl
->jw
, value
);
1897 pr_out("%"PRIu64
, value
);
1900 static bool is_binary_eol(int i
)
1905 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1910 if (dl
->json_output
)
1911 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1913 pr_out("%02x ", data
[i
]);
1915 if (!dl
->json_output
&& is_binary_eol(i
))
1918 if (!dl
->json_output
&& !is_binary_eol(i
))
1922 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1924 __pr_out_indent_newline(dl
);
1925 if (dl
->json_output
)
1926 jsonw_string(dl
->jw
, value
);
1928 pr_out("%s", value
);
1931 static void pr_out_name(struct dl
*dl
, const char *name
)
1933 __pr_out_indent_newline(dl
);
1934 if (dl
->json_output
)
1935 jsonw_name(dl
->jw
, name
);
1937 pr_out("%s:", name
);
1940 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1942 if (dl
->json_output
) {
1943 jsonw_name(dl
->jw
, "address");
1944 jsonw_uint(dl
->jw
, addr
);
1945 jsonw_name(dl
->jw
, "data");
1946 jsonw_start_array(dl
->jw
);
1950 static void pr_out_region_chunk_end(struct dl
*dl
)
1952 if (dl
->json_output
)
1953 jsonw_end_array(dl
->jw
);
1956 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1959 static uint64_t align_val
;
1962 pr_out_region_chunk_start(dl
, addr
);
1964 if (!dl
->json_output
)
1965 if (!(align_val
% 16))
1966 pr_out("%s%016"PRIx64
" ",
1967 align_val
? "\n" : "",
1972 if (dl
->json_output
)
1973 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1975 pr_out("%02x ", data
[i
]);
1980 pr_out_region_chunk_end(dl
);
1983 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1985 if (dl
->json_output
) {
1986 jsonw_start_object(dl
->jw
);
1987 jsonw_name(dl
->jw
, name
);
1988 jsonw_start_object(dl
->jw
);
1992 static void pr_out_section_end(struct dl
*dl
)
1994 if (dl
->json_output
) {
1995 if (dl
->arr_last
.present
)
1996 jsonw_end_array(dl
->jw
);
1997 jsonw_end_object(dl
->jw
);
1998 jsonw_end_object(dl
->jw
);
2002 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2004 if (dl
->json_output
) {
2005 jsonw_name(dl
->jw
, name
);
2006 jsonw_start_array(dl
->jw
);
2008 __pr_out_indent_inc();
2010 pr_out("%s:", name
);
2011 __pr_out_indent_inc();
2016 static void pr_out_array_end(struct dl
*dl
)
2018 if (dl
->json_output
) {
2019 jsonw_end_array(dl
->jw
);
2021 __pr_out_indent_dec();
2022 __pr_out_indent_dec();
2026 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2028 if (dl
->json_output
) {
2029 jsonw_name(dl
->jw
, name
);
2030 jsonw_start_object(dl
->jw
);
2032 __pr_out_indent_inc();
2034 pr_out("%s:", name
);
2035 __pr_out_indent_inc();
2040 static void pr_out_object_end(struct dl
*dl
)
2042 if (dl
->json_output
) {
2043 jsonw_end_object(dl
->jw
);
2045 __pr_out_indent_dec();
2046 __pr_out_indent_dec();
2050 static void pr_out_entry_start(struct dl
*dl
)
2052 if (dl
->json_output
)
2053 jsonw_start_object(dl
->jw
);
2056 static void pr_out_entry_end(struct dl
*dl
)
2058 if (dl
->json_output
)
2059 jsonw_end_object(dl
->jw
);
2064 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2066 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2072 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2073 if (err
!= MNL_CB_OK
)
2076 pr_out_object_start(dl
, "stats");
2077 pr_out_object_start(dl
, "rx");
2078 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2079 pr_out_u64(dl
, "bytes",
2080 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2081 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2082 pr_out_u64(dl
, "packets",
2083 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2084 pr_out_object_end(dl
);
2085 pr_out_object_end(dl
);
2088 static const char *param_cmode_name(uint8_t cmode
)
2091 case DEVLINK_PARAM_CMODE_RUNTIME
:
2092 return PARAM_CMODE_RUNTIME_STR
;
2093 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2094 return PARAM_CMODE_DRIVERINIT_STR
;
2095 case DEVLINK_PARAM_CMODE_PERMANENT
:
2096 return PARAM_CMODE_PERMANENT_STR
;
2097 default: return "<unknown type>";
2101 static const char *eswitch_mode_name(uint32_t mode
)
2104 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2105 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2106 default: return "<unknown mode>";
2110 static const char *eswitch_inline_mode_name(uint32_t mode
)
2113 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2114 return ESWITCH_INLINE_MODE_NONE
;
2115 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2116 return ESWITCH_INLINE_MODE_LINK
;
2117 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2118 return ESWITCH_INLINE_MODE_NETWORK
;
2119 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2120 return ESWITCH_INLINE_MODE_TRANSPORT
;
2122 return "<unknown mode>";
2126 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2128 __pr_out_handle_start(dl
, tb
, true, false);
2130 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
2131 pr_out_str(dl
, "mode",
2132 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2134 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
2135 pr_out_str(dl
, "inline-mode",
2136 eswitch_inline_mode_name(mnl_attr_get_u8(
2137 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2139 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2140 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2142 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
2145 pr_out_handle_end(dl
);
2148 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2150 struct dl
*dl
= data
;
2151 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2152 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2154 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2155 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2156 return MNL_CB_ERROR
;
2157 pr_out_eswitch(dl
, tb
);
2161 static int cmd_dev_eswitch_show(struct dl
*dl
)
2163 struct nlmsghdr
*nlh
;
2166 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2167 NLM_F_REQUEST
| NLM_F_ACK
);
2169 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2173 pr_out_section_start(dl
, "dev");
2174 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2175 pr_out_section_end(dl
);
2179 static int cmd_dev_eswitch_set(struct dl
*dl
)
2181 struct nlmsghdr
*nlh
;
2184 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2185 NLM_F_REQUEST
| NLM_F_ACK
);
2187 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2188 DL_OPT_ESWITCH_MODE
|
2189 DL_OPT_ESWITCH_INLINE_MODE
|
2190 DL_OPT_ESWITCH_ENCAP_MODE
);
2195 if (dl
->opts
.present
== 1) {
2196 pr_err("Need to set at least one option\n");
2200 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2203 static int cmd_dev_eswitch(struct dl
*dl
)
2205 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2208 } else if (dl_argv_match(dl
, "set")) {
2210 return cmd_dev_eswitch_set(dl
);
2211 } else if (dl_argv_match(dl
, "show")) {
2213 return cmd_dev_eswitch_show(dl
);
2215 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2219 struct param_val_conv
{
2225 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2226 uint32_t len
, const char *name
)
2230 for (i
= 0; i
< len
; i
++)
2231 if (!strcmp(param_val_conv
[i
].name
, name
))
2238 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2239 uint32_t len
, const char *name
, const char *vstr
,
2244 for (i
= 0; i
< len
; i
++)
2245 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2246 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2247 *vuint
= param_val_conv
[i
].vuint
;
2255 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2256 uint32_t len
, const char *name
, uint32_t vuint
,
2261 for (i
= 0; i
< len
; i
++)
2262 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2263 param_val_conv
[i
].vuint
== vuint
) {
2264 *vstr
= param_val_conv
[i
].vstr
;
2271 static const struct param_val_conv param_val_conv
[] = {
2273 .name
= "fw_load_policy",
2275 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2278 .name
= "fw_load_policy",
2280 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2283 .name
= "reset_dev_on_drv_probe",
2285 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2288 .name
= "fw_load_policy",
2290 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2293 .name
= "reset_dev_on_drv_probe",
2295 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2298 .name
= "reset_dev_on_drv_probe",
2300 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2303 .name
= "reset_dev_on_drv_probe",
2305 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2309 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2311 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2312 int nla_type
, struct nlattr
*nl
)
2314 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2315 struct nlattr
*val_attr
;
2320 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2321 if (err
!= MNL_CB_OK
)
2324 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2325 (nla_type
!= MNL_TYPE_FLAG
&&
2326 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2329 pr_out_str(dl
, "cmode",
2330 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2331 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2333 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2339 err
= param_val_conv_str_get(param_val_conv
,
2342 mnl_attr_get_u8(val_attr
),
2346 pr_out_str(dl
, "value", vstr
);
2348 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2353 err
= param_val_conv_str_get(param_val_conv
,
2356 mnl_attr_get_u16(val_attr
),
2360 pr_out_str(dl
, "value", vstr
);
2362 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2367 err
= param_val_conv_str_get(param_val_conv
,
2370 mnl_attr_get_u32(val_attr
),
2374 pr_out_str(dl
, "value", vstr
);
2376 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2379 case MNL_TYPE_STRING
:
2380 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2383 pr_out_bool(dl
, "value", val_attr
? true : false);
2388 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2390 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2391 struct nlattr
*param_value_attr
;
2392 const char *nla_name
;
2396 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2397 if (err
!= MNL_CB_OK
)
2399 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2400 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2401 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2405 pr_out_handle_start_arr(dl
, tb
);
2407 __pr_out_handle_start(dl
, tb
, true, false);
2409 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2411 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2412 pr_out_str(dl
, "name", nla_name
);
2414 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2415 pr_out_str(dl
, "type", "driver-specific");
2417 pr_out_str(dl
, "type", "generic");
2419 pr_out_array_start(dl
, "values");
2420 mnl_attr_for_each_nested(param_value_attr
,
2421 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2422 pr_out_entry_start(dl
);
2423 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2424 pr_out_entry_end(dl
);
2426 pr_out_array_end(dl
);
2427 pr_out_handle_end(dl
);
2430 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2432 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2433 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2434 struct dl
*dl
= data
;
2436 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2437 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2438 !tb
[DEVLINK_ATTR_PARAM
])
2439 return MNL_CB_ERROR
;
2440 pr_out_param(dl
, tb
, true);
2456 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2458 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2459 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2460 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2461 struct nlattr
*param_value_attr
;
2462 enum devlink_param_cmode cmode
;
2463 struct param_ctx
*ctx
= data
;
2464 struct dl
*dl
= ctx
->dl
;
2468 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2469 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2470 !tb
[DEVLINK_ATTR_PARAM
])
2471 return MNL_CB_ERROR
;
2473 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2474 if (err
!= MNL_CB_OK
)
2475 return MNL_CB_ERROR
;
2477 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2478 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2479 return MNL_CB_ERROR
;
2481 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2482 mnl_attr_for_each_nested(param_value_attr
,
2483 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2484 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2485 struct nlattr
*val_attr
;
2487 err
= mnl_attr_parse_nested(param_value_attr
,
2488 attr_cb
, nla_value
);
2489 if (err
!= MNL_CB_OK
)
2490 return MNL_CB_ERROR
;
2492 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2493 (nla_type
!= MNL_TYPE_FLAG
&&
2494 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2495 return MNL_CB_ERROR
;
2497 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2498 if (cmode
== dl
->opts
.cmode
) {
2499 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2502 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2505 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2508 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2510 case MNL_TYPE_STRING
:
2511 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2514 ctx
->value
.vbool
= val_attr
? true : false;
2520 ctx
->nla_type
= nla_type
;
2524 static int cmd_dev_param_set(struct dl
*dl
)
2526 struct param_ctx ctx
= {};
2527 struct nlmsghdr
*nlh
;
2535 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2537 DL_OPT_PARAM_VALUE
|
2538 DL_OPT_PARAM_CMODE
, 0);
2542 /* Get value type */
2543 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2544 NLM_F_REQUEST
| NLM_F_ACK
);
2545 dl_opts_put(nlh
, dl
);
2548 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2552 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2553 NLM_F_REQUEST
| NLM_F_ACK
);
2554 dl_opts_put(nlh
, dl
);
2556 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2557 dl
->opts
.param_name
);
2559 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2560 switch (ctx
.nla_type
) {
2563 err
= param_val_conv_uint_get(param_val_conv
,
2565 dl
->opts
.param_name
,
2566 dl
->opts
.param_value
,
2570 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2573 goto err_param_value_parse
;
2574 if (val_u8
== ctx
.value
.vu8
)
2576 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2580 err
= param_val_conv_uint_get(param_val_conv
,
2582 dl
->opts
.param_name
,
2583 dl
->opts
.param_value
,
2587 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2590 goto err_param_value_parse
;
2591 if (val_u16
== ctx
.value
.vu16
)
2593 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2597 err
= param_val_conv_uint_get(param_val_conv
,
2599 dl
->opts
.param_name
,
2600 dl
->opts
.param_value
,
2603 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2605 goto err_param_value_parse
;
2606 if (val_u32
== ctx
.value
.vu32
)
2608 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2611 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2613 goto err_param_value_parse
;
2614 if (val_bool
== ctx
.value
.vbool
)
2617 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2620 case MNL_TYPE_STRING
:
2621 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2622 dl
->opts
.param_value
);
2623 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2627 printf("Value type not supported\n");
2630 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2632 err_param_value_parse
:
2633 pr_err("Value \"%s\" is not a number or not within range\n",
2634 dl
->opts
.param_value
);
2638 static int cmd_dev_param_show(struct dl
*dl
)
2640 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2641 struct nlmsghdr
*nlh
;
2644 if (dl_argc(dl
) == 0)
2645 flags
|= NLM_F_DUMP
;
2647 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2649 if (dl_argc(dl
) > 0) {
2650 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2651 DL_OPT_PARAM_NAME
, 0);
2656 pr_out_section_start(dl
, "param");
2657 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2658 pr_out_section_end(dl
);
2662 static int cmd_dev_param(struct dl
*dl
)
2664 if (dl_argv_match(dl
, "help")) {
2667 } else if (dl_argv_match(dl
, "show") ||
2668 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2670 return cmd_dev_param_show(dl
);
2671 } else if (dl_argv_match(dl
, "set")) {
2673 return cmd_dev_param_set(dl
);
2675 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2678 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2680 struct dl
*dl
= data
;
2681 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2682 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2683 uint8_t reload_failed
= 0;
2685 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2686 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2687 return MNL_CB_ERROR
;
2689 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2690 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2692 if (reload_failed
) {
2693 __pr_out_handle_start(dl
, tb
, true, false);
2694 pr_out_bool(dl
, "reload_failed", true);
2695 pr_out_handle_end(dl
);
2697 pr_out_handle(dl
, tb
);
2703 static int cmd_dev_show(struct dl
*dl
)
2705 struct nlmsghdr
*nlh
;
2706 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2709 if (dl_argc(dl
) == 0)
2710 flags
|= NLM_F_DUMP
;
2712 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2714 if (dl_argc(dl
) > 0) {
2715 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2720 pr_out_section_start(dl
, "dev");
2721 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2722 pr_out_section_end(dl
);
2726 static void cmd_dev_reload_help(void)
2728 pr_err("Usage: devlink dev reload [ DEV ]\n");
2731 static int cmd_dev_reload(struct dl
*dl
)
2733 struct nlmsghdr
*nlh
;
2736 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2737 cmd_dev_reload_help();
2741 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2742 NLM_F_REQUEST
| NLM_F_ACK
);
2744 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2748 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2751 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2752 const char *name
, int type
)
2754 struct nlattr
*version
;
2756 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2757 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2758 const char *ver_value
;
2759 const char *ver_name
;
2762 if (mnl_attr_get_type(version
) != type
)
2765 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2766 if (err
!= MNL_CB_OK
)
2769 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2770 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2774 pr_out_object_start(dl
, name
);
2778 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2779 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2781 pr_out_str(dl
, ver_name
, ver_value
);
2782 if (!dl
->json_output
)
2787 pr_out_object_end(dl
);
2790 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2791 struct nlattr
**tb
, bool has_versions
)
2793 __pr_out_handle_start(dl
, tb
, true, false);
2795 __pr_out_indent_inc();
2796 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2797 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2799 if (!dl
->json_output
)
2801 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2804 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2805 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2807 if (!dl
->json_output
)
2809 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2811 __pr_out_indent_dec();
2814 pr_out_object_start(dl
, "versions");
2816 pr_out_versions_single(dl
, nlh
, "fixed",
2817 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2818 pr_out_versions_single(dl
, nlh
, "running",
2819 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2820 pr_out_versions_single(dl
, nlh
, "stored",
2821 DEVLINK_ATTR_INFO_VERSION_STORED
);
2823 pr_out_object_end(dl
);
2826 pr_out_handle_end(dl
);
2829 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2831 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2832 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2833 bool has_versions
, has_info
;
2834 struct dl
*dl
= data
;
2836 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2838 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2839 return MNL_CB_ERROR
;
2841 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2842 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2843 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2844 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2845 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2849 pr_out_info(dl
, nlh
, tb
, has_versions
);
2854 static void cmd_dev_info_help(void)
2856 pr_err("Usage: devlink dev info [ DEV ]\n");
2859 static int cmd_dev_info(struct dl
*dl
)
2861 struct nlmsghdr
*nlh
;
2862 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2865 if (dl_argv_match(dl
, "help")) {
2866 cmd_dev_info_help();
2870 if (dl_argc(dl
) == 0)
2871 flags
|= NLM_F_DUMP
;
2873 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2875 if (dl_argc(dl
) > 0) {
2876 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2881 pr_out_section_start(dl
, "info");
2882 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2883 pr_out_section_end(dl
);
2887 static void cmd_dev_flash_help(void)
2889 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2893 struct cmd_dev_flash_status_ctx
{
2896 char *last_component
;
2897 uint8_t not_first
:1,
2903 static int nullstrcmp(const char *str1
, const char *str2
)
2906 return strcmp(str1
, str2
);
2909 return str1
? 1 : -1;
2912 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2914 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2915 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2916 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2917 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2918 const char *component
= NULL
;
2919 uint64_t done
= 0, total
= 0;
2920 const char *msg
= NULL
;
2921 const char *bus_name
;
2922 const char *dev_name
;
2924 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2925 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2928 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2929 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2930 return MNL_CB_ERROR
;
2931 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2932 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2933 if (strcmp(bus_name
, opts
->bus_name
) ||
2934 strcmp(dev_name
, opts
->dev_name
))
2935 return MNL_CB_ERROR
;
2937 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2939 free(ctx
->last_msg
);
2940 free(ctx
->last_component
);
2941 ctx
->received_end
= 1;
2945 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2946 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2947 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2948 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2949 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2950 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2951 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2952 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
2954 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
2955 !nullstrcmp(component
, ctx
->last_component
) &&
2956 ctx
->last_pc
&& ctx
->not_first
) {
2957 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
2962 pr_out("[%s] ", component
);
2963 free(ctx
->last_component
);
2964 ctx
->last_component
= strdup(component
);
2968 free(ctx
->last_msg
);
2969 ctx
->last_msg
= strdup(msg
);
2973 pr_out_tty(" %3lu%%", (done
* 100) / total
);
2984 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
2985 struct mnlg_socket
*nlg_ntf
,
2988 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
2995 for (i
= 0; i
< 3; i
++)
2997 FD_SET(pipe_r
, &fds
[0]);
2999 FD_SET(nlfd
, &fds
[0]);
3003 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3006 pr_err("select() failed\n");
3009 if (FD_ISSET(nlfd
, &fds
[0])) {
3010 err
= _mnlg_socket_recv_run(nlg_ntf
,
3011 cmd_dev_flash_status_cb
, ctx
);
3015 if (FD_ISSET(pipe_r
, &fds
[0])) {
3016 err
= read(pipe_r
, &err2
, sizeof(err2
));
3018 pr_err("Failed to read pipe\n");
3023 ctx
->flash_done
= 1;
3029 static int cmd_dev_flash(struct dl
*dl
)
3031 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3032 struct mnlg_socket
*nlg_ntf
;
3033 struct nlmsghdr
*nlh
;
3039 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3040 cmd_dev_flash_help();
3044 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3045 NLM_F_REQUEST
| NLM_F_ACK
);
3047 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3048 DL_OPT_FLASH_COMPONENT
);
3052 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3056 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3060 err
= pipe(pipe_fds
);
3063 pipe_r
= pipe_fds
[0];
3064 pipe_w
= pipe_fds
[1];
3072 /* In child, just execute the flash and pass returned
3073 * value through pipe once it is done.
3076 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3077 write(pipe_w
, &err
, sizeof(err
));
3084 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3087 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3089 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3092 mnlg_socket_close(nlg_ntf
);
3096 static int cmd_dev(struct dl
*dl
)
3098 if (dl_argv_match(dl
, "help")) {
3101 } else if (dl_argv_match(dl
, "show") ||
3102 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3104 return cmd_dev_show(dl
);
3105 } else if (dl_argv_match(dl
, "eswitch")) {
3107 return cmd_dev_eswitch(dl
);
3108 } else if (dl_argv_match(dl
, "reload")) {
3110 return cmd_dev_reload(dl
);
3111 } else if (dl_argv_match(dl
, "param")) {
3113 return cmd_dev_param(dl
);
3114 } else if (dl_argv_match(dl
, "info")) {
3116 return cmd_dev_info(dl
);
3117 } else if (dl_argv_match(dl
, "flash")) {
3119 return cmd_dev_flash(dl
);
3121 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3125 static void cmd_port_help(void)
3127 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3128 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3129 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3130 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3133 static const char *port_type_name(uint32_t type
)
3136 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3137 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3138 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3139 case DEVLINK_PORT_TYPE_IB
: return "ib";
3140 default: return "<unknown type>";
3144 static const char *port_flavour_name(uint16_t flavour
)
3147 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3149 case DEVLINK_PORT_FLAVOUR_CPU
:
3151 case DEVLINK_PORT_FLAVOUR_DSA
:
3153 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3155 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3158 return "<unknown flavour>";
3162 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3166 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3167 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3168 pr_out_uint(dl
, "pfnum", fn_num
);
3170 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3171 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3172 pr_out_uint(dl
, "vfnum", fn_num
);
3176 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3178 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3179 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3181 pr_out_port_handle_start(dl
, tb
, false);
3183 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3185 pr_out_str(dl
, "type", port_type_name(port_type
));
3187 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3189 if (port_type
!= des_port_type
)
3190 pr_out_str(dl
, "des_type",
3191 port_type_name(des_port_type
));
3194 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
3195 pr_out_str(dl
, "netdev",
3196 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3197 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
3198 pr_out_str(dl
, "ibdev",
3199 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3200 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3201 uint16_t port_flavour
=
3202 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3204 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
3206 switch (port_flavour
) {
3207 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3208 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3209 pr_out_port_pfvf_num(dl
, tb
);
3215 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3216 uint32_t port_number
;
3218 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3219 pr_out_uint(dl
, "port", port_number
);
3221 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3222 pr_out_uint(dl
, "split_group",
3223 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3224 pr_out_port_handle_end(dl
);
3227 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3229 struct dl
*dl
= data
;
3230 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3231 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3233 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3234 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3235 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3236 return MNL_CB_ERROR
;
3237 pr_out_port(dl
, tb
);
3241 static int cmd_port_show(struct dl
*dl
)
3243 struct nlmsghdr
*nlh
;
3244 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3247 if (dl_argc(dl
) == 0)
3248 flags
|= NLM_F_DUMP
;
3250 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3252 if (dl_argc(dl
) > 0) {
3253 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3258 pr_out_section_start(dl
, "port");
3259 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3260 pr_out_section_end(dl
);
3264 static int cmd_port_set(struct dl
*dl
)
3266 struct nlmsghdr
*nlh
;
3269 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3270 NLM_F_REQUEST
| NLM_F_ACK
);
3272 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3276 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3279 static int cmd_port_split(struct dl
*dl
)
3281 struct nlmsghdr
*nlh
;
3284 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3285 NLM_F_REQUEST
| NLM_F_ACK
);
3287 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3291 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3294 static int cmd_port_unsplit(struct dl
*dl
)
3296 struct nlmsghdr
*nlh
;
3299 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3300 NLM_F_REQUEST
| NLM_F_ACK
);
3302 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3306 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3309 static int cmd_port(struct dl
*dl
)
3311 if (dl_argv_match(dl
, "help")) {
3314 } else if (dl_argv_match(dl
, "show") ||
3315 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3317 return cmd_port_show(dl
);
3318 } else if (dl_argv_match(dl
, "set")) {
3320 return cmd_port_set(dl
);
3321 } else if (dl_argv_match(dl
, "split")) {
3323 return cmd_port_split(dl
);
3324 } else if (dl_argv_match(dl
, "unsplit")) {
3326 return cmd_port_unsplit(dl
);
3328 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3332 static void cmd_sb_help(void)
3334 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3335 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3336 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3337 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3338 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3339 pr_err(" pool POOL_INDEX ]\n");
3340 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3341 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3342 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3343 pr_err(" type { ingress | egress } ]\n");
3344 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3345 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3346 pr_err(" th THRESHOLD\n");
3347 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3348 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3349 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3352 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3354 pr_out_handle_start_arr(dl
, tb
);
3355 pr_out_uint(dl
, "sb",
3356 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3357 pr_out_uint(dl
, "size",
3358 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3359 pr_out_uint(dl
, "ing_pools",
3360 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3361 pr_out_uint(dl
, "eg_pools",
3362 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3363 pr_out_uint(dl
, "ing_tcs",
3364 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3365 pr_out_uint(dl
, "eg_tcs",
3366 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3367 pr_out_handle_end(dl
);
3370 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3372 struct dl
*dl
= data
;
3373 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3374 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3376 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3377 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3378 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3379 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3380 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3381 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3382 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3383 return MNL_CB_ERROR
;
3388 static int cmd_sb_show(struct dl
*dl
)
3390 struct nlmsghdr
*nlh
;
3391 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3394 if (dl_argc(dl
) == 0)
3395 flags
|= NLM_F_DUMP
;
3397 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3399 if (dl_argc(dl
) > 0) {
3400 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3405 pr_out_section_start(dl
, "sb");
3406 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3407 pr_out_section_end(dl
);
3411 static const char *pool_type_name(uint8_t type
)
3414 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3415 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3416 default: return "<unknown type>";
3420 static const char *threshold_type_name(uint8_t type
)
3423 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3424 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3425 default: return "<unknown type>";
3429 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3431 pr_out_handle_start_arr(dl
, tb
);
3432 pr_out_uint(dl
, "sb",
3433 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3434 pr_out_uint(dl
, "pool",
3435 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3436 pr_out_str(dl
, "type",
3437 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3438 pr_out_uint(dl
, "size",
3439 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3440 pr_out_str(dl
, "thtype",
3441 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3442 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3443 pr_out_uint(dl
, "cell_size",
3444 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3445 pr_out_handle_end(dl
);
3448 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3450 struct dl
*dl
= data
;
3451 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3452 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3454 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3455 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3456 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3457 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3458 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3459 return MNL_CB_ERROR
;
3460 pr_out_sb_pool(dl
, tb
);
3464 static int cmd_sb_pool_show(struct dl
*dl
)
3466 struct nlmsghdr
*nlh
;
3467 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3470 if (dl_argc(dl
) == 0)
3471 flags
|= NLM_F_DUMP
;
3473 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3475 if (dl_argc(dl
) > 0) {
3476 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3482 pr_out_section_start(dl
, "pool");
3483 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3484 pr_out_section_end(dl
);
3488 static int cmd_sb_pool_set(struct dl
*dl
)
3490 struct nlmsghdr
*nlh
;
3493 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3494 NLM_F_REQUEST
| NLM_F_ACK
);
3496 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3497 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3501 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3504 static int cmd_sb_pool(struct dl
*dl
)
3506 if (dl_argv_match(dl
, "help")) {
3509 } else if (dl_argv_match(dl
, "show") ||
3510 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3512 return cmd_sb_pool_show(dl
);
3513 } else if (dl_argv_match(dl
, "set")) {
3515 return cmd_sb_pool_set(dl
);
3517 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3521 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3523 pr_out_port_handle_start_arr(dl
, tb
, true);
3524 pr_out_uint(dl
, "sb",
3525 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3526 pr_out_uint(dl
, "pool",
3527 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3528 pr_out_uint(dl
, "threshold",
3529 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3530 pr_out_port_handle_end(dl
);
3533 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3535 struct dl
*dl
= data
;
3536 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3537 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3539 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3540 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3541 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3542 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3543 return MNL_CB_ERROR
;
3544 pr_out_sb_port_pool(dl
, tb
);
3548 static int cmd_sb_port_pool_show(struct dl
*dl
)
3550 struct nlmsghdr
*nlh
;
3551 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3554 if (dl_argc(dl
) == 0)
3555 flags
|= NLM_F_DUMP
;
3557 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3559 if (dl_argc(dl
) > 0) {
3560 err
= dl_argv_parse_put(nlh
, dl
,
3561 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3567 pr_out_section_start(dl
, "port_pool");
3568 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3569 pr_out_section_end(dl
);
3573 static int cmd_sb_port_pool_set(struct dl
*dl
)
3575 struct nlmsghdr
*nlh
;
3578 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3579 NLM_F_REQUEST
| NLM_F_ACK
);
3581 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3582 DL_OPT_SB_TH
, DL_OPT_SB
);
3586 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3589 static int cmd_sb_port_pool(struct dl
*dl
)
3591 if (dl_argv_match(dl
, "help")) {
3594 } else if (dl_argv_match(dl
, "show") ||
3595 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3597 return cmd_sb_port_pool_show(dl
);
3598 } else if (dl_argv_match(dl
, "set")) {
3600 return cmd_sb_port_pool_set(dl
);
3602 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3606 static int cmd_sb_port(struct dl
*dl
)
3608 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3611 } else if (dl_argv_match(dl
, "pool")) {
3613 return cmd_sb_port_pool(dl
);
3615 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3619 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3621 pr_out_port_handle_start_arr(dl
, tb
, true);
3622 pr_out_uint(dl
, "sb",
3623 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3624 pr_out_uint(dl
, "tc",
3625 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3626 pr_out_str(dl
, "type",
3627 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3628 pr_out_uint(dl
, "pool",
3629 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3630 pr_out_uint(dl
, "threshold",
3631 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3632 pr_out_port_handle_end(dl
);
3635 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3637 struct dl
*dl
= data
;
3638 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3639 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3641 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3642 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3643 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3644 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3645 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3646 return MNL_CB_ERROR
;
3647 pr_out_sb_tc_bind(dl
, tb
);
3651 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3653 struct nlmsghdr
*nlh
;
3654 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3657 if (dl_argc(dl
) == 0)
3658 flags
|= NLM_F_DUMP
;
3660 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3662 if (dl_argc(dl
) > 0) {
3663 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3664 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3669 pr_out_section_start(dl
, "tc_bind");
3670 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3671 pr_out_section_end(dl
);
3675 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3677 struct nlmsghdr
*nlh
;
3680 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3681 NLM_F_REQUEST
| NLM_F_ACK
);
3683 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3684 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3689 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3692 static int cmd_sb_tc_bind(struct dl
*dl
)
3694 if (dl_argv_match(dl
, "help")) {
3697 } else if (dl_argv_match(dl
, "show") ||
3698 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3700 return cmd_sb_tc_bind_show(dl
);
3701 } else if (dl_argv_match(dl
, "set")) {
3703 return cmd_sb_tc_bind_set(dl
);
3705 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3709 static int cmd_sb_tc(struct dl
*dl
)
3711 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3714 } else if (dl_argv_match(dl
, "bind")) {
3716 return cmd_sb_tc_bind(dl
);
3718 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3723 struct list_head list
;
3727 uint32_t bound_pool_index
;
3731 struct list_head list
;
3734 uint32_t port_index
;
3736 struct list_head pool_list
;
3737 struct list_head ing_tc_list
;
3738 struct list_head eg_tc_list
;
3744 struct list_head port_list
;
3747 static struct occ_item
*occ_item_alloc(void)
3749 return calloc(1, sizeof(struct occ_item
));
3752 static void occ_item_free(struct occ_item
*occ_item
)
3757 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3759 struct occ_port
*occ_port
;
3761 occ_port
= calloc(1, sizeof(*occ_port
));
3764 occ_port
->port_index
= port_index
;
3765 INIT_LIST_HEAD(&occ_port
->pool_list
);
3766 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3767 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3771 static void occ_port_free(struct occ_port
*occ_port
)
3773 struct occ_item
*occ_item
, *tmp
;
3775 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3776 occ_item_free(occ_item
);
3777 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3778 occ_item_free(occ_item
);
3779 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3780 occ_item_free(occ_item
);
3783 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3785 struct occ_show
*occ_show
;
3787 occ_show
= calloc(1, sizeof(*occ_show
));
3791 INIT_LIST_HEAD(&occ_show
->port_list
);
3795 static void occ_show_free(struct occ_show
*occ_show
)
3797 struct occ_port
*occ_port
, *tmp
;
3799 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3800 occ_port_free(occ_port
);
3803 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3806 struct occ_port
*occ_port
;
3807 uint32_t port_index
;
3809 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3811 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3812 if (occ_port
->port_index
== port_index
)
3815 occ_port
= occ_port_alloc(port_index
);
3818 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3822 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3825 struct occ_item
*occ_item
;
3828 pr_out_sp(7, " %s:", label
);
3829 list_for_each_entry(occ_item
, list
, list
) {
3830 if ((i
- 1) % 4 == 0 && i
!= 1)
3833 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3834 occ_item
->bound_pool_index
);
3836 pr_out_sp(7, "%2u:", occ_item
->index
);
3837 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3841 if ((i
- 1) % 4 != 0)
3845 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3846 struct list_head
*list
,
3849 struct occ_item
*occ_item
;
3852 jsonw_name(dl
->jw
, label
);
3853 jsonw_start_object(dl
->jw
);
3854 list_for_each_entry(occ_item
, list
, list
) {
3855 sprintf(buf
, "%u", occ_item
->index
);
3856 jsonw_name(dl
->jw
, buf
);
3857 jsonw_start_object(dl
->jw
);
3859 jsonw_uint_field(dl
->jw
, "bound_pool",
3860 occ_item
->bound_pool_index
);
3861 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3862 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3863 jsonw_end_object(dl
->jw
);
3865 jsonw_end_object(dl
->jw
);
3868 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3870 if (dl
->json_output
) {
3871 pr_out_json_occ_show_item_list(dl
, "pool",
3872 &occ_port
->pool_list
, false);
3873 pr_out_json_occ_show_item_list(dl
, "itc",
3874 &occ_port
->ing_tc_list
, true);
3875 pr_out_json_occ_show_item_list(dl
, "etc",
3876 &occ_port
->eg_tc_list
, true);
3879 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3880 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3881 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3885 static void pr_out_occ_show(struct occ_show
*occ_show
)
3887 struct dl
*dl
= occ_show
->dl
;
3888 struct dl_opts
*opts
= &dl
->opts
;
3889 struct occ_port
*occ_port
;
3891 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3892 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3893 occ_port
->port_index
, true, false);
3894 pr_out_occ_show_port(dl
, occ_port
);
3895 pr_out_port_handle_end(dl
);
3899 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3902 struct occ_port
*occ_port
;
3903 struct occ_item
*occ_item
;
3905 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3908 occ_port
= occ_port_get(occ_show
, tb
);
3910 occ_show
->err
= -ENOMEM
;
3914 occ_item
= occ_item_alloc();
3916 occ_show
->err
= -ENOMEM
;
3919 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3920 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3921 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3922 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3925 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3927 struct occ_show
*occ_show
= data
;
3928 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3929 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3931 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3932 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3933 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3934 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3935 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3936 return MNL_CB_ERROR
;
3937 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3941 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3944 struct occ_port
*occ_port
;
3945 struct occ_item
*occ_item
;
3948 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3951 occ_port
= occ_port_get(occ_show
, tb
);
3953 occ_show
->err
= -ENOMEM
;
3957 occ_item
= occ_item_alloc();
3959 occ_show
->err
= -ENOMEM
;
3962 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3963 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3964 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3965 occ_item
->bound_pool_index
=
3966 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3967 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3968 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3969 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3970 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3971 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3973 occ_item_free(occ_item
);
3976 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3978 struct occ_show
*occ_show
= data
;
3979 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3980 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3982 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3983 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3984 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3985 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
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_tc_pool_process(occ_show
, tb
);
3993 static int cmd_sb_occ_show(struct dl
*dl
)
3995 struct nlmsghdr
*nlh
;
3996 struct occ_show
*occ_show
;
3997 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
4000 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4004 occ_show
= occ_show_alloc(dl
);
4008 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4010 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4011 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4015 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4017 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4018 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4022 pr_out_section_start(dl
, "occupancy");
4023 pr_out_occ_show(occ_show
);
4024 pr_out_section_end(dl
);
4027 occ_show_free(occ_show
);
4031 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4033 struct nlmsghdr
*nlh
;
4036 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4037 NLM_F_REQUEST
| NLM_F_ACK
);
4039 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4043 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4046 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4048 struct nlmsghdr
*nlh
;
4051 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4052 NLM_F_REQUEST
| NLM_F_ACK
);
4054 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4058 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4061 static int cmd_sb_occ(struct dl
*dl
)
4063 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4066 } else if (dl_argv_match(dl
, "show") ||
4067 dl_argv_match(dl
, "list")) {
4069 return cmd_sb_occ_show(dl
);
4070 } else if (dl_argv_match(dl
, "snapshot")) {
4072 return cmd_sb_occ_snapshot(dl
);
4073 } else if (dl_argv_match(dl
, "clearmax")) {
4075 return cmd_sb_occ_clearmax(dl
);
4077 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4081 static int cmd_sb(struct dl
*dl
)
4083 if (dl_argv_match(dl
, "help")) {
4086 } else if (dl_argv_match(dl
, "show") ||
4087 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4089 return cmd_sb_show(dl
);
4090 } else if (dl_argv_match(dl
, "pool")) {
4092 return cmd_sb_pool(dl
);
4093 } else if (dl_argv_match(dl
, "port")) {
4095 return cmd_sb_port(dl
);
4096 } else if (dl_argv_match(dl
, "tc")) {
4098 return cmd_sb_tc(dl
);
4099 } else if (dl_argv_match(dl
, "occupancy")) {
4101 return cmd_sb_occ(dl
);
4103 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4107 static const char *cmd_name(uint8_t cmd
)
4110 case DEVLINK_CMD_UNSPEC
: return "unspec";
4111 case DEVLINK_CMD_GET
: return "get";
4112 case DEVLINK_CMD_SET
: return "set";
4113 case DEVLINK_CMD_NEW
: return "new";
4114 case DEVLINK_CMD_DEL
: return "del";
4115 case DEVLINK_CMD_PORT_GET
: return "get";
4116 case DEVLINK_CMD_PORT_SET
: return "set";
4117 case DEVLINK_CMD_PORT_NEW
: return "new";
4118 case DEVLINK_CMD_PORT_DEL
: return "del";
4119 case DEVLINK_CMD_PARAM_GET
: return "get";
4120 case DEVLINK_CMD_PARAM_SET
: return "set";
4121 case DEVLINK_CMD_PARAM_NEW
: return "new";
4122 case DEVLINK_CMD_PARAM_DEL
: return "del";
4123 case DEVLINK_CMD_REGION_GET
: return "get";
4124 case DEVLINK_CMD_REGION_SET
: return "set";
4125 case DEVLINK_CMD_REGION_NEW
: return "new";
4126 case DEVLINK_CMD_REGION_DEL
: return "del";
4127 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4128 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4129 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4130 case DEVLINK_CMD_TRAP_GET
: return "get";
4131 case DEVLINK_CMD_TRAP_SET
: return "set";
4132 case DEVLINK_CMD_TRAP_NEW
: return "new";
4133 case DEVLINK_CMD_TRAP_DEL
: return "del";
4134 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4135 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4136 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4137 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4138 default: return "<unknown cmd>";
4142 static const char *cmd_obj(uint8_t cmd
)
4145 case DEVLINK_CMD_UNSPEC
: return "unspec";
4146 case DEVLINK_CMD_GET
:
4147 case DEVLINK_CMD_SET
:
4148 case DEVLINK_CMD_NEW
:
4149 case DEVLINK_CMD_DEL
:
4151 case DEVLINK_CMD_PORT_GET
:
4152 case DEVLINK_CMD_PORT_SET
:
4153 case DEVLINK_CMD_PORT_NEW
:
4154 case DEVLINK_CMD_PORT_DEL
:
4156 case DEVLINK_CMD_PARAM_GET
:
4157 case DEVLINK_CMD_PARAM_SET
:
4158 case DEVLINK_CMD_PARAM_NEW
:
4159 case DEVLINK_CMD_PARAM_DEL
:
4161 case DEVLINK_CMD_REGION_GET
:
4162 case DEVLINK_CMD_REGION_SET
:
4163 case DEVLINK_CMD_REGION_NEW
:
4164 case DEVLINK_CMD_REGION_DEL
:
4166 case DEVLINK_CMD_FLASH_UPDATE
:
4167 case DEVLINK_CMD_FLASH_UPDATE_END
:
4168 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4170 case DEVLINK_CMD_TRAP_GET
:
4171 case DEVLINK_CMD_TRAP_SET
:
4172 case DEVLINK_CMD_TRAP_NEW
:
4173 case DEVLINK_CMD_TRAP_DEL
:
4175 case DEVLINK_CMD_TRAP_GROUP_GET
:
4176 case DEVLINK_CMD_TRAP_GROUP_SET
:
4177 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4178 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4179 return "trap-group";
4180 default: return "<unknown obj>";
4184 static void pr_out_mon_header(uint8_t cmd
)
4186 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4189 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4191 const char *obj
= cmd_obj(cmd
);
4192 unsigned int index
= 0;
4193 const char *cur_obj
;
4197 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4198 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4204 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4206 __pr_out_handle_start(dl
, tb
, true, false);
4208 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
4209 pr_out_str(dl
, "msg",
4210 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4212 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
4213 pr_out_str(dl
, "component",
4214 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4216 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4217 pr_out_u64(dl
, "done",
4218 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4220 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4221 pr_out_u64(dl
, "total",
4222 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4224 pr_out_handle_end(dl
);
4227 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4228 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4229 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4231 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4233 struct dl
*dl
= data
;
4234 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4235 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4236 uint8_t cmd
= genl
->cmd
;
4238 if (!cmd_filter_check(dl
, cmd
))
4242 case DEVLINK_CMD_GET
: /* fall through */
4243 case DEVLINK_CMD_SET
: /* fall through */
4244 case DEVLINK_CMD_NEW
: /* fall through */
4245 case DEVLINK_CMD_DEL
:
4246 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4247 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4248 return MNL_CB_ERROR
;
4249 pr_out_mon_header(genl
->cmd
);
4250 pr_out_handle(dl
, tb
);
4252 case DEVLINK_CMD_PORT_GET
: /* fall through */
4253 case DEVLINK_CMD_PORT_SET
: /* fall through */
4254 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4255 case DEVLINK_CMD_PORT_DEL
:
4256 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4257 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4258 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4259 return MNL_CB_ERROR
;
4260 pr_out_mon_header(genl
->cmd
);
4261 pr_out_port(dl
, tb
);
4263 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4264 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4265 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4266 case DEVLINK_CMD_PARAM_DEL
:
4267 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4268 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4269 !tb
[DEVLINK_ATTR_PARAM
])
4270 return MNL_CB_ERROR
;
4271 pr_out_mon_header(genl
->cmd
);
4272 pr_out_param(dl
, tb
, false);
4274 case DEVLINK_CMD_REGION_GET
: /* fall through */
4275 case DEVLINK_CMD_REGION_SET
: /* fall through */
4276 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4277 case DEVLINK_CMD_REGION_DEL
:
4278 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4279 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4280 !tb
[DEVLINK_ATTR_REGION_NAME
])
4281 return MNL_CB_ERROR
;
4282 pr_out_mon_header(genl
->cmd
);
4283 pr_out_region(dl
, tb
);
4285 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4286 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4287 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4288 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4289 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4290 return MNL_CB_ERROR
;
4291 pr_out_mon_header(genl
->cmd
);
4292 pr_out_flash_update(dl
, tb
);
4294 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4295 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4296 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4297 case DEVLINK_CMD_TRAP_DEL
:
4298 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4299 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4300 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4301 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4302 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4303 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4304 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4305 !tb
[DEVLINK_ATTR_STATS
])
4306 return MNL_CB_ERROR
;
4307 pr_out_mon_header(genl
->cmd
);
4308 pr_out_trap(dl
, tb
, false);
4310 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4311 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4312 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4313 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4314 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4315 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4316 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4317 !tb
[DEVLINK_ATTR_STATS
])
4318 return MNL_CB_ERROR
;
4319 pr_out_mon_header(genl
->cmd
);
4320 pr_out_trap_group(dl
, tb
, false);
4326 static int cmd_mon_show(struct dl
*dl
)
4329 unsigned int index
= 0;
4330 const char *cur_obj
;
4332 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4333 if (strcmp(cur_obj
, "all") != 0 &&
4334 strcmp(cur_obj
, "dev") != 0 &&
4335 strcmp(cur_obj
, "port") != 0 &&
4336 strcmp(cur_obj
, "trap") != 0 &&
4337 strcmp(cur_obj
, "trap-group") != 0) {
4338 pr_err("Unknown object \"%s\"\n", cur_obj
);
4342 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4345 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4351 static void cmd_mon_help(void)
4353 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4354 "where OBJECT-LIST := { dev | port | trap | trap-group }\n");
4357 static int cmd_mon(struct dl
*dl
)
4359 if (dl_argv_match(dl
, "help")) {
4363 return cmd_mon_show(dl
);
4366 struct dpipe_field
{
4369 unsigned int bitwidth
;
4370 enum devlink_dpipe_field_mapping_type mapping_type
;
4373 struct dpipe_header
{
4374 struct list_head list
;
4377 struct dpipe_field
*fields
;
4378 unsigned int fields_count
;
4381 struct dpipe_table
{
4382 struct list_head list
;
4384 unsigned int resource_id
;
4385 bool resource_valid
;
4388 struct dpipe_tables
{
4389 struct list_head table_list
;
4399 enum devlink_resource_unit unit
;
4404 struct list_head list
;
4405 struct list_head resource_list
;
4406 struct resource
*parent
;
4410 struct list_head resource_list
;
4413 struct resource_ctx
{
4416 struct resources
*resources
;
4417 struct dpipe_tables
*tables
;
4418 bool print_resources
;
4419 bool pending_change
;
4422 static struct resource
*resource_alloc(void)
4424 struct resource
*resource
;
4426 resource
= calloc(1, sizeof(struct resource
));
4429 INIT_LIST_HEAD(&resource
->resource_list
);
4433 static void resource_free(struct resource
*resource
)
4435 struct resource
*child_resource
, *tmp
;
4437 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4439 free(child_resource
->name
);
4440 resource_free(child_resource
);
4445 static struct resources
*resources_alloc(void)
4447 struct resources
*resources
;
4449 resources
= calloc(1, sizeof(struct resources
));
4452 INIT_LIST_HEAD(&resources
->resource_list
);
4456 static void resources_free(struct resources
*resources
)
4458 struct resource
*resource
, *tmp
;
4460 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4461 resource_free(resource
);
4464 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4466 ctx
->resources
= resources_alloc();
4467 if (!ctx
->resources
)
4473 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4475 resources_free(ctx
->resources
);
4481 struct list_head global_headers
;
4482 struct list_head local_headers
;
4483 struct dpipe_tables
*tables
;
4484 struct resources
*resources
;
4489 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4491 struct dpipe_header
*header
;
4493 header
= calloc(1, sizeof(struct dpipe_header
));
4496 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4497 if (!header
->fields
)
4498 goto err_fields_alloc
;
4499 header
->fields_count
= fields_count
;
4507 static void dpipe_header_free(struct dpipe_header
*header
)
4509 free(header
->fields
);
4513 static void dpipe_header_clear(struct dpipe_header
*header
)
4515 struct dpipe_field
*field
;
4518 for (i
= 0; i
< header
->fields_count
; i
++) {
4519 field
= &header
->fields
[i
];
4525 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4526 struct dpipe_header
*header
, bool global
)
4529 list_add(&header
->list
, &ctx
->global_headers
);
4531 list_add(&header
->list
, &ctx
->local_headers
);
4534 static void dpipe_header_del(struct dpipe_header
*header
)
4536 list_del(&header
->list
);
4539 static struct dpipe_table
*dpipe_table_alloc(void)
4541 return calloc(1, sizeof(struct dpipe_table
));
4544 static void dpipe_table_free(struct dpipe_table
*table
)
4549 static struct dpipe_tables
*dpipe_tables_alloc(void)
4551 struct dpipe_tables
*tables
;
4553 tables
= calloc(1, sizeof(struct dpipe_tables
));
4556 INIT_LIST_HEAD(&tables
->table_list
);
4560 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4562 struct dpipe_table
*table
, *tmp
;
4564 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4565 dpipe_table_free(table
);
4569 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4571 ctx
->tables
= dpipe_tables_alloc();
4576 INIT_LIST_HEAD(&ctx
->global_headers
);
4577 INIT_LIST_HEAD(&ctx
->local_headers
);
4581 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4583 struct dpipe_header
*header
, *tmp
;
4585 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4587 dpipe_header_del(header
);
4588 dpipe_header_clear(header
);
4589 dpipe_header_free(header
);
4591 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4593 dpipe_header_del(header
);
4594 dpipe_header_clear(header
);
4595 dpipe_header_free(header
);
4597 dpipe_tables_free(ctx
->tables
);
4600 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4601 uint32_t header_id
, bool global
)
4603 struct list_head
*header_list
;
4604 struct dpipe_header
*header
;
4607 header_list
= &ctx
->global_headers
;
4609 header_list
= &ctx
->local_headers
;
4610 list_for_each_entry(header
, header_list
, list
) {
4611 if (header
->id
!= header_id
)
4613 return header
->name
;
4618 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4620 uint32_t field_id
, bool global
)
4622 struct list_head
*header_list
;
4623 struct dpipe_header
*header
;
4626 header_list
= &ctx
->global_headers
;
4628 header_list
= &ctx
->local_headers
;
4629 list_for_each_entry(header
, header_list
, list
) {
4630 if (header
->id
!= header_id
)
4632 return header
->fields
[field_id
].name
;
4638 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4640 switch (mapping_type
) {
4641 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4643 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4651 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4652 uint32_t field_id
, bool global
)
4654 enum devlink_dpipe_field_mapping_type mapping_type
;
4655 struct list_head
*header_list
;
4656 struct dpipe_header
*header
;
4659 header_list
= &ctx
->global_headers
;
4661 header_list
= &ctx
->local_headers
;
4662 list_for_each_entry(header
, header_list
, list
) {
4663 if (header
->id
!= header_id
)
4665 mapping_type
= header
->fields
[field_id
].mapping_type
;
4666 return dpipe_field_mapping_e2s(mapping_type
);
4671 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4672 struct dpipe_field
*fields
,
4673 unsigned int field_count
)
4675 struct dpipe_field
*field
;
4678 for (i
= 0; i
< field_count
; i
++) {
4680 pr_out_entry_start(ctx
->dl
);
4681 pr_out_str(ctx
->dl
, "name", field
->name
);
4682 if (ctx
->dl
->verbose
)
4683 pr_out_uint(ctx
->dl
, "id", field
->id
);
4684 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4685 if (field
->mapping_type
)
4686 pr_out_str(ctx
->dl
, "mapping_type",
4687 dpipe_field_mapping_e2s(field
->mapping_type
));
4688 pr_out_entry_end(ctx
->dl
);
4693 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4694 struct dpipe_header
*header
, bool global
)
4696 pr_out_handle_start_arr(ctx
->dl
, tb
);
4697 pr_out_str(ctx
->dl
, "name", header
->name
);
4698 if (ctx
->dl
->verbose
) {
4699 pr_out_uint(ctx
->dl
, "id", header
->id
);
4700 pr_out_str(ctx
->dl
, "global",
4701 global
? "true" : "false");
4703 pr_out_array_start(ctx
->dl
, "field");
4704 pr_out_dpipe_fields(ctx
, header
->fields
,
4705 header
->fields_count
);
4706 pr_out_array_end(ctx
->dl
);
4707 pr_out_handle_end(ctx
->dl
);
4710 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4713 struct dpipe_header
*header
;
4715 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4716 pr_out_dpipe_header(ctx
, tb
, header
, false);
4718 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4719 pr_out_dpipe_header(ctx
, tb
, header
, true);
4722 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4724 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4728 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4729 if (err
!= MNL_CB_OK
)
4731 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4732 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4733 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4734 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4737 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4738 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4739 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4740 field
->name
= strdup(name
);
4743 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4747 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4748 struct dpipe_field
*fields
)
4750 struct nlattr
*nla_field
;
4754 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4755 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4763 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4765 struct nlattr
*nla_field
;
4766 unsigned int count
= 0;
4768 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4773 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4775 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4776 struct dpipe_header
*header
;
4777 unsigned int fields_count
;
4778 const char *header_name
;
4782 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4783 if (err
!= MNL_CB_OK
)
4786 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4787 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4788 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4791 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4792 header
= dpipe_header_alloc(fields_count
);
4796 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4797 header
->name
= strdup(header_name
);
4798 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4799 header
->fields_count
= fields_count
;
4800 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4802 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4806 dpipe_header_add(ctx
, header
, global
);
4810 dpipe_header_free(header
);
4814 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4816 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4817 struct nlattr
*nla_header
;
4820 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4821 err
= dpipe_header_get(ctx
, nla_header
);
4828 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4830 struct dpipe_ctx
*ctx
= data
;
4831 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4832 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4835 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4836 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4837 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4838 return MNL_CB_ERROR
;
4839 err
= dpipe_headers_get(ctx
, tb
);
4842 return MNL_CB_ERROR
;
4845 if (ctx
->print_headers
)
4846 pr_out_dpipe_headers(ctx
, tb
);
4850 static int cmd_dpipe_headers_show(struct dl
*dl
)
4852 struct nlmsghdr
*nlh
;
4853 struct dpipe_ctx ctx
= {};
4854 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4857 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4859 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4863 err
= dpipe_ctx_init(&ctx
, dl
);
4867 ctx
.print_headers
= true;
4869 pr_out_section_start(dl
, "header");
4870 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4872 pr_err("error get headers %s\n", strerror(ctx
.err
));
4873 pr_out_section_end(dl
);
4875 dpipe_ctx_fini(&ctx
);
4879 static void cmd_dpipe_header_help(void)
4881 pr_err("Usage: devlink dpipe headers show DEV\n");
4884 static int cmd_dpipe_header(struct dl
*dl
)
4886 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4887 cmd_dpipe_header_help();
4889 } else if (dl_argv_match(dl
, "show")) {
4891 return cmd_dpipe_headers_show(dl
);
4893 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4898 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4900 switch (action_type
) {
4901 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4902 return "field_modify";
4908 struct dpipe_op_info
{
4914 struct dpipe_action
{
4915 struct dpipe_op_info info
;
4919 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4920 struct dpipe_ctx
*ctx
)
4922 struct dpipe_op_info
*op_info
= &action
->info
;
4923 const char *mapping
;
4925 pr_out_str(ctx
->dl
, "type",
4926 dpipe_action_type_e2s(action
->type
));
4927 pr_out_str(ctx
->dl
, "header",
4928 dpipe_header_id2s(ctx
, op_info
->header_id
,
4929 op_info
->header_global
));
4930 pr_out_str(ctx
->dl
, "field",
4931 dpipe_field_id2s(ctx
, op_info
->header_id
,
4933 op_info
->header_global
));
4934 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4936 op_info
->header_global
);
4938 pr_out_str(ctx
->dl
, "mapping", mapping
);
4941 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4943 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4946 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4947 if (err
!= MNL_CB_OK
)
4950 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4951 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4952 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4953 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4957 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4958 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4959 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4960 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4965 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4966 struct nlattr
*nla_actions
)
4968 struct nlattr
*nla_action
;
4969 struct dpipe_action action
;
4971 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4972 pr_out_entry_start(ctx
->dl
);
4973 if (dpipe_action_parse(&action
, nla_action
))
4974 goto err_action_parse
;
4975 pr_out_dpipe_action(&action
, ctx
);
4976 pr_out_entry_end(ctx
->dl
);
4981 pr_out_entry_end(ctx
->dl
);
4986 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4988 switch (match_type
) {
4989 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4990 return "field_exact";
4996 struct dpipe_match
{
4997 struct dpipe_op_info info
;
5001 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5002 struct dpipe_ctx
*ctx
)
5004 struct dpipe_op_info
*op_info
= &match
->info
;
5005 const char *mapping
;
5007 pr_out_str(ctx
->dl
, "type",
5008 dpipe_match_type_e2s(match
->type
));
5009 pr_out_str(ctx
->dl
, "header",
5010 dpipe_header_id2s(ctx
, op_info
->header_id
,
5011 op_info
->header_global
));
5012 pr_out_str(ctx
->dl
, "field",
5013 dpipe_field_id2s(ctx
, op_info
->header_id
,
5015 op_info
->header_global
));
5016 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5018 op_info
->header_global
);
5020 pr_out_str(ctx
->dl
, "mapping", mapping
);
5023 static int dpipe_match_parse(struct dpipe_match
*match
,
5027 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5030 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5031 if (err
!= MNL_CB_OK
)
5034 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5035 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5036 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5037 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5041 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5042 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5043 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5044 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5049 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5050 struct nlattr
*nla_matches
)
5052 struct nlattr
*nla_match
;
5053 struct dpipe_match match
;
5055 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5056 pr_out_entry_start(ctx
->dl
);
5057 if (dpipe_match_parse(&match
, nla_match
))
5058 goto err_match_parse
;
5059 pr_out_dpipe_match(&match
, ctx
);
5060 pr_out_entry_end(ctx
->dl
);
5065 pr_out_entry_end(ctx
->dl
);
5069 static struct resource
*
5070 resource_find(struct resources
*resources
, struct resource
*resource
,
5071 uint64_t resource_id
)
5073 struct list_head
*list_head
;
5076 list_head
= &resources
->resource_list
;
5078 list_head
= &resource
->resource_list
;
5080 list_for_each_entry(resource
, list_head
, list
) {
5081 struct resource
*child_resource
;
5083 if (resource
->id
== resource_id
)
5086 child_resource
= resource_find(resources
, resource
,
5089 return child_resource
;
5095 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5096 uint64_t resource_id
)
5098 struct resource
*resource
, *parent_resource
;
5099 const char del
[] = "/";
5103 resource
= resource_find(resources
, NULL
, resource_id
);
5107 for (parent_resource
= resource
; parent_resource
;
5108 parent_resource
= parent_resource
->parent
)
5109 path_len
+= strlen(parent_resource
->name
) + 1;
5112 path
= calloc(1, path_len
);
5116 path
+= path_len
- 1;
5117 for (parent_resource
= resource
; parent_resource
;
5118 parent_resource
= parent_resource
->parent
) {
5119 path
-= strlen(parent_resource
->name
);
5120 memcpy(path
, parent_resource
->name
,
5121 strlen(parent_resource
->name
));
5122 path
-= strlen(del
);
5123 memcpy(path
, del
, strlen(del
));
5125 pr_out_str(dl
, "resource_path", path
);
5129 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5131 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5132 struct dpipe_table
*table
;
5133 uint32_t resource_units
;
5134 bool counters_enabled
;
5135 bool resource_valid
;
5139 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5140 if (err
!= MNL_CB_OK
)
5143 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5144 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5145 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5146 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5147 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5151 table
= dpipe_table_alloc();
5155 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5156 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5157 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5159 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5161 if (resource_valid
) {
5162 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5163 table
->resource_valid
= true;
5166 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5167 if (!ctx
->print_tables
)
5170 pr_out_str(ctx
->dl
, "name", table
->name
);
5171 pr_out_uint(ctx
->dl
, "size", size
);
5172 pr_out_str(ctx
->dl
, "counters_enabled",
5173 counters_enabled
? "true" : "false");
5175 if (resource_valid
) {
5176 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5177 resource_path_print(ctx
->dl
, ctx
->resources
,
5178 table
->resource_id
);
5179 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
5182 pr_out_array_start(ctx
->dl
, "match");
5183 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5184 goto err_matches_show
;
5185 pr_out_array_end(ctx
->dl
);
5187 pr_out_array_start(ctx
->dl
, "action");
5188 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5189 goto err_actions_show
;
5190 pr_out_array_end(ctx
->dl
);
5196 pr_out_array_end(ctx
->dl
);
5200 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5202 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5203 struct nlattr
*nla_table
;
5205 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5206 if (ctx
->print_tables
)
5207 pr_out_handle_start_arr(ctx
->dl
, tb
);
5208 if (dpipe_table_show(ctx
, nla_table
))
5209 goto err_table_show
;
5210 if (ctx
->print_tables
)
5211 pr_out_handle_end(ctx
->dl
);
5216 if (ctx
->print_tables
)
5217 pr_out_handle_end(ctx
->dl
);
5221 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5223 struct dpipe_ctx
*ctx
= data
;
5224 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5225 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5227 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5228 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5229 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5230 return MNL_CB_ERROR
;
5232 if (dpipe_tables_show(ctx
, tb
))
5233 return MNL_CB_ERROR
;
5237 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5239 static int cmd_dpipe_table_show(struct dl
*dl
)
5241 struct nlmsghdr
*nlh
;
5242 struct dpipe_ctx dpipe_ctx
= {};
5243 struct resource_ctx resource_ctx
= {};
5244 uint16_t flags
= NLM_F_REQUEST
;
5247 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5251 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5253 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5257 dpipe_ctx
.print_tables
= true;
5259 dl_opts_put(nlh
, dl
);
5260 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5263 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5264 goto err_headers_get
;
5267 err
= resource_ctx_init(&resource_ctx
, dl
);
5269 goto err_resource_ctx_init
;
5271 resource_ctx
.print_resources
= false;
5272 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5273 dl_opts_put(nlh
, dl
);
5274 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5277 dpipe_ctx
.resources
= resource_ctx
.resources
;
5279 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5280 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5281 dl_opts_put(nlh
, dl
);
5283 pr_out_section_start(dl
, "table");
5284 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5285 pr_out_section_end(dl
);
5287 resource_ctx_fini(&resource_ctx
);
5288 dpipe_ctx_fini(&dpipe_ctx
);
5291 err_resource_ctx_init
:
5293 dpipe_ctx_fini(&dpipe_ctx
);
5297 static int cmd_dpipe_table_set(struct dl
*dl
)
5299 struct nlmsghdr
*nlh
;
5302 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5303 NLM_F_REQUEST
| NLM_F_ACK
);
5305 err
= dl_argv_parse_put(nlh
, dl
,
5306 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5307 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5311 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5314 enum dpipe_value_type
{
5315 DPIPE_VALUE_TYPE_VALUE
,
5316 DPIPE_VALUE_TYPE_MASK
,
5320 dpipe_value_type_e2s(enum dpipe_value_type type
)
5323 case DPIPE_VALUE_TYPE_VALUE
:
5325 case DPIPE_VALUE_TYPE_MASK
:
5326 return "value_mask";
5332 struct dpipe_field_printer
{
5333 unsigned int field_id
;
5334 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5337 struct dpipe_header_printer
{
5338 struct dpipe_field_printer
*printers
;
5339 unsigned int printers_count
;
5340 unsigned int header_id
;
5343 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5344 enum dpipe_value_type type
,
5347 struct in_addr ip_addr
;
5349 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5350 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5354 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5355 enum dpipe_value_type type
,
5358 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
5359 ether_ntoa((struct ether_addr
*)value
));
5362 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5363 enum dpipe_value_type type
,
5366 char str
[INET6_ADDRSTRLEN
];
5368 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5369 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
5372 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5374 .printer
= dpipe_field_printer_ipv4_addr
,
5375 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5379 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5380 .printers
= dpipe_field_printers_ipv4
,
5381 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5382 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5385 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5387 .printer
= dpipe_field_printer_ethernet_addr
,
5388 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5392 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5393 .printers
= dpipe_field_printers_ethernet
,
5394 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5395 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5398 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5400 .printer
= dpipe_field_printer_ipv6_addr
,
5401 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5405 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5406 .printers
= dpipe_field_printers_ipv6
,
5407 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5408 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5411 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5412 &dpipe_header_printer_ipv4
,
5413 &dpipe_header_printer_ethernet
,
5414 &dpipe_header_printer_ipv6
,
5417 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5418 struct dpipe_op_info
*info
,
5419 enum dpipe_value_type type
,
5422 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5423 struct dpipe_header_printer
*header_printer
;
5424 struct dpipe_field_printer
*field_printer
;
5425 unsigned int field_printers_count
;
5429 for (i
= 0; i
< header_printers_count
; i
++) {
5430 header_printer
= dpipe_header_printers
[i
];
5431 if (header_printer
->header_id
!= info
->header_id
)
5433 field_printers_count
= header_printer
->printers_count
;
5434 for (j
= 0; j
< field_printers_count
; j
++) {
5435 field_printer
= &header_printer
->printers
[j
];
5436 if (field_printer
->field_id
!= info
->field_id
)
5438 field_printer
->printer(ctx
, type
, value
);
5446 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5448 unsigned int value_len
,
5449 struct dpipe_op_info
*info
,
5450 enum dpipe_value_type type
)
5452 if (info
->header_global
&&
5453 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5456 if (value_len
== sizeof(uint32_t)) {
5457 uint32_t *value_32
= value
;
5459 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
5463 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5464 struct nlattr
**nla_match_value
,
5465 struct dpipe_op_info
*info
)
5467 void *value
, *value_mask
;
5468 uint32_t value_mapping
;
5472 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5473 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5475 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5476 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5479 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5480 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
5484 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5485 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5486 DPIPE_VALUE_TYPE_MASK
);
5489 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5492 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5495 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5496 struct dpipe_match match
;
5499 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5500 if (err
!= MNL_CB_OK
)
5503 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5504 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5508 pr_out_entry_start(ctx
->dl
);
5509 if (dpipe_match_parse(&match
,
5510 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5511 goto err_match_parse
;
5512 pr_out_dpipe_match(&match
, ctx
);
5513 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5514 pr_out_entry_end(ctx
->dl
);
5519 pr_out_entry_end(ctx
->dl
);
5523 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5526 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5527 struct dpipe_action action
;
5530 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5531 if (err
!= MNL_CB_OK
)
5534 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5535 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5539 pr_out_entry_start(ctx
->dl
);
5540 if (dpipe_action_parse(&action
,
5541 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5542 goto err_action_parse
;
5543 pr_out_dpipe_action(&action
, ctx
);
5544 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5545 pr_out_entry_end(ctx
->dl
);
5550 pr_out_entry_end(ctx
->dl
);
5555 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5556 struct nlattr
*nla_action_values
)
5558 struct nlattr
*nla_action_value
;
5560 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5561 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5568 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5569 struct nlattr
*nla_match_values
)
5571 struct nlattr
*nla_match_value
;
5573 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5574 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5580 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5582 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5583 uint32_t entry_index
;
5587 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5588 if (err
!= MNL_CB_OK
)
5591 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5592 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5593 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5597 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5598 pr_out_uint(ctx
->dl
, "index", entry_index
);
5600 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5601 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5602 pr_out_uint(ctx
->dl
, "counter", counter
);
5605 pr_out_array_start(ctx
->dl
, "match_value");
5606 if (dpipe_tables_match_values_show(ctx
,
5607 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5608 goto err_match_values_show
;
5609 pr_out_array_end(ctx
->dl
);
5611 pr_out_array_start(ctx
->dl
, "action_value");
5612 if (dpipe_tables_action_values_show(ctx
,
5613 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5614 goto err_action_values_show
;
5615 pr_out_array_end(ctx
->dl
);
5618 err_action_values_show
:
5619 err_match_values_show
:
5620 pr_out_array_end(ctx
->dl
);
5624 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5626 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5627 struct nlattr
*nla_entry
;
5629 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5630 pr_out_handle_start_arr(ctx
->dl
, tb
);
5631 if (dpipe_entry_show(ctx
, nla_entry
))
5632 goto err_entry_show
;
5633 pr_out_handle_end(ctx
->dl
);
5638 pr_out_handle_end(ctx
->dl
);
5642 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5644 struct dpipe_ctx
*ctx
= data
;
5645 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5646 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5648 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5649 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5650 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5651 return MNL_CB_ERROR
;
5653 if (dpipe_table_entries_show(ctx
, tb
))
5654 return MNL_CB_ERROR
;
5658 static int cmd_dpipe_table_dump(struct dl
*dl
)
5660 struct nlmsghdr
*nlh
;
5661 struct dpipe_ctx ctx
= {};
5662 uint16_t flags
= NLM_F_REQUEST
;
5665 err
= dpipe_ctx_init(&ctx
, dl
);
5669 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5673 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5674 dl_opts_put(nlh
, dl
);
5675 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5677 pr_err("error get headers %s\n", strerror(ctx
.err
));
5681 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5682 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5683 dl_opts_put(nlh
, dl
);
5685 pr_out_section_start(dl
, "table_entry");
5686 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5687 pr_out_section_end(dl
);
5689 dpipe_ctx_fini(&ctx
);
5693 static void cmd_dpipe_table_help(void)
5695 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5696 "where OBJECT-LIST := { show | set | dump }\n");
5699 static int cmd_dpipe_table(struct dl
*dl
)
5701 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5702 cmd_dpipe_table_help();
5704 } else if (dl_argv_match(dl
, "show")) {
5706 return cmd_dpipe_table_show(dl
);
5707 } else if (dl_argv_match(dl
, "set")) {
5709 return cmd_dpipe_table_set(dl
);
5710 } else if (dl_argv_match(dl
, "dump")) {
5712 return cmd_dpipe_table_dump(dl
);
5714 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5718 static void cmd_dpipe_help(void)
5720 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5721 "where OBJECT-LIST := { header | table }\n");
5724 static int cmd_dpipe(struct dl
*dl
)
5726 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5729 } else if (dl_argv_match(dl
, "header")) {
5731 return cmd_dpipe_header(dl
);
5732 } else if (dl_argv_match(dl
, "table")) {
5734 return cmd_dpipe_table(dl
);
5736 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5741 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5742 struct nlattr
**nla_resource
)
5744 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5745 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5746 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5747 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5748 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5749 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5750 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5754 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5755 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5756 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5757 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5758 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5759 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5760 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5762 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5763 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5765 resource
->size_new
= resource
->size
;
5767 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5768 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5769 resource
->occ_valid
= true;
5772 if (resource
->size_new
!= resource
->size
)
5773 ctx
->pending_change
= true;
5779 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5780 struct resource
*parent_resource
, struct nlattr
*nl
)
5782 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5783 struct nlattr
*nla_child_resource
;
5784 struct nlattr
*nla_resources
;
5794 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5795 if (err
!= MNL_CB_OK
)
5798 err
= resource_parse(ctx
, resource
, nla_resource
);
5802 resource
->parent
= parent_resource
;
5803 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5806 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5807 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5809 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5810 struct resource
*child_resource
;
5811 struct list_head
*list
;
5813 child_resource
= resource_alloc();
5814 if (!child_resource
)
5818 list
= &ctx
->resources
->resource_list
;
5820 list
= &resource
->resource_list
;
5822 list_add_tail(&child_resource
->list
, list
);
5823 err
= resource_get(ctx
, child_resource
, resource
,
5824 nla_child_resource
);
5832 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5835 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5836 default: return "<unknown unit>";
5840 static void resource_show(struct resource
*resource
,
5841 struct resource_ctx
*ctx
)
5843 struct resource
*child_resource
;
5844 struct dpipe_table
*table
;
5845 struct dl
*dl
= ctx
->dl
;
5848 pr_out_str(dl
, "name", resource
->name
);
5850 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5851 pr_out_u64(dl
, "size", resource
->size
);
5852 if (resource
->size
!= resource
->size_new
)
5853 pr_out_u64(dl
, "size_new", resource
->size_new
);
5854 if (resource
->occ_valid
)
5855 pr_out_uint(dl
, "occ", resource
->size_occ
);
5856 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5858 if (resource
->size_min
!= resource
->size_max
) {
5859 pr_out_uint(dl
, "size_min", resource
->size_min
);
5860 pr_out_u64(dl
, "size_max", resource
->size_max
);
5861 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5864 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5865 if (table
->resource_id
== resource
->id
&&
5866 table
->resource_valid
)
5870 pr_out_array_start(dl
, "dpipe_tables");
5872 pr_out_str(dl
, "dpipe_tables", "none");
5874 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5875 if (table
->resource_id
!= resource
->id
||
5876 !table
->resource_valid
)
5878 pr_out_entry_start(dl
);
5879 pr_out_str(dl
, "table_name", table
->name
);
5880 pr_out_entry_end(dl
);
5883 pr_out_array_end(dl
);
5885 if (list_empty(&resource
->resource_list
))
5888 if (ctx
->pending_change
)
5889 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5891 pr_out_array_start(dl
, "resources");
5892 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5893 pr_out_entry_start(dl
);
5894 resource_show(child_resource
, ctx
);
5895 pr_out_entry_end(dl
);
5897 pr_out_array_end(dl
);
5901 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5903 struct resources
*resources
= ctx
->resources
;
5904 struct resource
*resource
;
5906 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5907 pr_out_handle_start_arr(ctx
->dl
, tb
);
5908 resource_show(resource
, ctx
);
5909 pr_out_handle_end(ctx
->dl
);
5913 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5915 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5918 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5920 struct resource_ctx
*ctx
= data
;
5921 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5922 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5925 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5926 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5927 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5928 return MNL_CB_ERROR
;
5930 err
= resources_get(ctx
, tb
);
5933 return MNL_CB_ERROR
;
5936 if (ctx
->print_resources
)
5937 resources_show(ctx
, tb
);
5942 static int cmd_resource_show(struct dl
*dl
)
5944 struct nlmsghdr
*nlh
;
5945 struct dpipe_ctx dpipe_ctx
= {};
5946 struct resource_ctx resource_ctx
= {};
5949 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5953 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5955 dl_opts_put(nlh
, dl
);
5957 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5961 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5964 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5968 err
= resource_ctx_init(&resource_ctx
, dl
);
5972 resource_ctx
.print_resources
= true;
5973 resource_ctx
.tables
= dpipe_ctx
.tables
;
5974 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5975 NLM_F_REQUEST
| NLM_F_ACK
);
5976 dl_opts_put(nlh
, dl
);
5977 pr_out_section_start(dl
, "resources");
5978 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5980 pr_out_section_end(dl
);
5981 resource_ctx_fini(&resource_ctx
);
5983 dpipe_ctx_fini(&dpipe_ctx
);
5987 static void cmd_resource_help(void)
5989 pr_err("Usage: devlink resource show DEV\n"
5990 " devlink resource set DEV path PATH size SIZE\n");
5993 static struct resource
*
5994 resource_find_by_name(struct list_head
*list
, char *name
)
5996 struct resource
*resource
;
5998 list_for_each_entry(resource
, list
, list
) {
5999 if (!strcmp(resource
->name
, name
))
6006 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6007 uint32_t *p_resource_id
, bool *p_resource_valid
)
6009 struct resource
*resource
;
6010 uint32_t resource_id
= 0;
6011 char *resource_path_dup
;
6012 struct list_head
*list
;
6013 const char del
[] = "/";
6014 char *resource_name
;
6016 resource_path_dup
= strdup(resource_path
);
6017 list
= &ctx
->resources
->resource_list
;
6018 resource_name
= strtok(resource_path_dup
, del
);
6019 while (resource_name
!= NULL
) {
6020 resource
= resource_find_by_name(list
, resource_name
);
6022 goto err_resource_lookup
;
6024 list
= &resource
->resource_list
;
6025 resource_name
= strtok(NULL
, del
);
6026 resource_id
= resource
->id
;
6028 free(resource_path_dup
);
6029 *p_resource_valid
= true;
6030 *p_resource_id
= resource_id
;
6033 err_resource_lookup
:
6034 free(resource_path_dup
);
6038 static int cmd_resource_set(struct dl
*dl
)
6040 struct nlmsghdr
*nlh
;
6041 struct resource_ctx ctx
= {};
6044 err
= resource_ctx_init(&ctx
, dl
);
6048 ctx
.print_resources
= false;
6049 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6050 DL_OPT_RESOURCE_SIZE
, 0);
6054 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6056 dl_opts_put(nlh
, dl
);
6057 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6059 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6063 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6064 &dl
->opts
.resource_id
,
6065 &dl
->opts
.resource_id_valid
);
6067 pr_err("error parsing resource path %s\n", strerror(-err
));
6071 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6072 NLM_F_REQUEST
| NLM_F_ACK
);
6074 dl_opts_put(nlh
, dl
);
6075 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6077 resource_ctx_fini(&ctx
);
6081 static int cmd_resource(struct dl
*dl
)
6083 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6084 cmd_resource_help();
6086 } else if (dl_argv_match(dl
, "show")) {
6088 return cmd_resource_show(dl
);
6089 } else if (dl_argv_match(dl
, "set")) {
6091 return cmd_resource_set(dl
);
6093 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6097 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6099 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6100 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6101 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6104 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6105 if (dl
->json_output
) {
6106 jsonw_name(dl
->jw
, buf
);
6107 jsonw_start_object(dl
->jw
);
6113 static void pr_out_region_handle_end(struct dl
*dl
)
6115 if (dl
->json_output
)
6116 jsonw_end_object(dl
->jw
);
6121 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6123 __pr_out_indent_newline(dl
);
6124 if (dl
->json_output
) {
6125 jsonw_name(dl
->jw
, "snapshot");
6126 jsonw_start_array(dl
->jw
);
6128 pr_out("snapshot %s", array
? "[" : "");
6132 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6134 if (dl
->json_output
)
6135 jsonw_end_array(dl
->jw
);
6140 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6142 uint32_t snapshot_id
;
6144 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6147 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6149 if (dl
->json_output
)
6150 jsonw_uint(dl
->jw
, snapshot_id
);
6152 pr_out("%s%u", index
? " " : "", snapshot_id
);
6155 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6157 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6158 struct nlattr
*nla_sanpshot
;
6161 pr_out_region_snapshots_start(dl
, true);
6162 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6163 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6164 if (err
!= MNL_CB_OK
)
6166 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6168 pr_out_region_snapshots_end(dl
, true);
6171 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6173 pr_out_region_snapshots_start(dl
, false);
6174 pr_out_region_snapshots_id(dl
, tb
, 0);
6175 pr_out_region_snapshots_end(dl
, false);
6178 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6180 pr_out_region_handle_start(dl
, tb
);
6182 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6183 pr_out_u64(dl
, "size",
6184 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6186 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6187 pr_out_snapshots(dl
, tb
);
6189 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6190 pr_out_snapshot(dl
, tb
);
6192 pr_out_region_handle_end(dl
);
6195 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6197 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6198 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6199 struct dl
*dl
= data
;
6201 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6202 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6203 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6204 return MNL_CB_ERROR
;
6206 pr_out_region(dl
, tb
);
6211 static int cmd_region_show(struct dl
*dl
)
6213 struct nlmsghdr
*nlh
;
6214 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6217 if (dl_argc(dl
) == 0)
6218 flags
|= NLM_F_DUMP
;
6220 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6222 if (dl_argc(dl
) > 0) {
6223 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6228 pr_out_section_start(dl
, "regions");
6229 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6230 pr_out_section_end(dl
);
6234 static int cmd_region_snapshot_del(struct dl
*dl
)
6236 struct nlmsghdr
*nlh
;
6239 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6240 NLM_F_REQUEST
| NLM_F_ACK
);
6242 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6243 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6247 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6250 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6252 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6253 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6254 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6255 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6256 struct dl
*dl
= data
;
6259 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6260 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6261 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6262 return MNL_CB_ERROR
;
6264 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6265 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6266 if (err
!= MNL_CB_OK
)
6267 return MNL_CB_ERROR
;
6269 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6270 if (!nla_chunk_data
)
6273 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6274 if (!nla_chunk_addr
)
6277 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6278 mnl_attr_get_payload_len(nla_chunk_data
),
6279 mnl_attr_get_u64(nla_chunk_addr
));
6284 static int cmd_region_dump(struct dl
*dl
)
6286 struct nlmsghdr
*nlh
;
6289 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6290 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6292 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6293 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6297 pr_out_section_start(dl
, "dump");
6298 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6299 pr_out_section_end(dl
);
6300 if (!dl
->json_output
)
6305 static int cmd_region_read(struct dl
*dl
)
6307 struct nlmsghdr
*nlh
;
6310 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6311 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6313 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6314 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6315 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6319 pr_out_section_start(dl
, "read");
6320 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6321 pr_out_section_end(dl
);
6322 if (!dl
->json_output
)
6327 static void cmd_region_help(void)
6329 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6330 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6331 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6332 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6335 static int cmd_region(struct dl
*dl
)
6337 if (dl_no_arg(dl
)) {
6338 return cmd_region_show(dl
);
6339 } else if (dl_argv_match(dl
, "help")) {
6342 } else if (dl_argv_match(dl
, "show")) {
6344 return cmd_region_show(dl
);
6345 } else if (dl_argv_match(dl
, "del")) {
6347 return cmd_region_snapshot_del(dl
);
6348 } else if (dl_argv_match(dl
, "dump")) {
6350 return cmd_region_dump(dl
);
6351 } else if (dl_argv_match(dl
, "read")) {
6353 return cmd_region_read(dl
);
6355 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6359 static int cmd_health_set_params(struct dl
*dl
)
6361 struct nlmsghdr
*nlh
;
6364 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6365 NLM_F_REQUEST
| NLM_F_ACK
);
6366 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6367 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6368 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6372 dl_opts_put(nlh
, dl
);
6373 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6376 static int cmd_health_dump_clear(struct dl
*dl
)
6378 struct nlmsghdr
*nlh
;
6381 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6382 NLM_F_REQUEST
| NLM_F_ACK
);
6384 err
= dl_argv_parse_put(nlh
, dl
,
6385 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6389 dl_opts_put(nlh
, dl
);
6390 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6393 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6400 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
6403 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
6406 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
6409 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
6412 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
6414 case MNL_TYPE_NUL_STRING
:
6415 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
6417 case MNL_TYPE_BINARY
:
6418 len
= mnl_attr_get_payload_len(nl_data
);
6419 data
= mnl_attr_get_payload(nl_data
);
6420 pr_out_binary_value(dl
, data
, len
);
6430 struct list_head list
;
6433 struct fmsg_cb_data
{
6436 struct list_head entry_list
;
6439 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6440 uint8_t *attr_value
, bool insert
)
6442 struct nest_entry
*entry
;
6445 entry
= malloc(sizeof(struct nest_entry
));
6449 entry
->attr_type
= *attr_value
;
6450 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6452 if (list_empty(&fmsg_data
->entry_list
))
6453 return MNL_CB_ERROR
;
6454 entry
= list_first_entry(&fmsg_data
->entry_list
,
6455 struct nest_entry
, list
);
6456 *attr_value
= entry
->attr_type
;
6457 list_del(&entry
->list
);
6463 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6466 struct dl
*dl
= fmsg_data
->dl
;
6467 uint8_t value
= nest_value
;
6470 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6471 if (err
!= MNL_CB_OK
)
6475 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6477 pr_out_entry_start(dl
);
6479 pr_out_entry_end(dl
);
6481 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6483 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6484 if (dl
->json_output
) {
6486 jsonw_start_array(dl
->jw
);
6488 jsonw_end_array(dl
->jw
);
6492 __pr_out_indent_inc();
6494 __pr_out_indent_dec();
6504 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6506 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6507 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6508 struct fmsg_cb_data
*fmsg_data
= data
;
6509 struct dl
*dl
= fmsg_data
->dl
;
6510 struct nlattr
*nla_object
;
6514 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6515 if (!tb
[DEVLINK_ATTR_FMSG
])
6516 return MNL_CB_ERROR
;
6518 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6519 attr_type
= mnl_attr_get_type(nla_object
);
6520 switch (attr_type
) {
6521 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6522 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6523 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6524 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6525 if (err
!= MNL_CB_OK
)
6528 case DEVLINK_ATTR_FMSG_NEST_END
:
6529 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6530 if (err
!= MNL_CB_OK
)
6533 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6534 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6536 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6537 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6539 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6540 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6542 if (err
!= MNL_CB_OK
)
6552 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6554 struct fmsg_cb_data data
;
6555 struct nlmsghdr
*nlh
;
6558 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6560 err
= dl_argv_parse_put(nlh
, dl
,
6561 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6566 INIT_LIST_HEAD(&data
.entry_list
);
6567 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6571 static int cmd_health_dump_show(struct dl
*dl
)
6573 return cmd_health_object_common(dl
,
6574 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6578 static int cmd_health_diagnose(struct dl
*dl
)
6580 return cmd_health_object_common(dl
,
6581 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6585 static int cmd_health_recover(struct dl
*dl
)
6587 struct nlmsghdr
*nlh
;
6590 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6591 NLM_F_REQUEST
| NLM_F_ACK
);
6593 err
= dl_argv_parse_put(nlh
, dl
,
6594 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6598 dl_opts_put(nlh
, dl
);
6599 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6602 enum devlink_health_reporter_state
{
6603 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6604 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6607 static const char *health_state_name(uint8_t state
)
6610 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6611 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6612 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6613 return HEALTH_REPORTER_STATE_ERROR_STR
;
6615 return "<unknown state>";
6619 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6621 struct sysinfo s_info
;
6627 info
= localtime(&now
);
6628 err
= sysinfo(&s_info
);
6631 /* Subtract uptime in sec from now yields the time of system
6632 * uptime. To this, add time_ms which is the amount of
6633 * milliseconds elapsed between uptime and the dump taken.
6635 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6636 info
= localtime(&sec
);
6638 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6639 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6642 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6644 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6645 enum devlink_health_reporter_state state
;
6646 const struct nlattr
*attr
;
6650 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6652 if (err
!= MNL_CB_OK
)
6655 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6656 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6657 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6658 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6661 pr_out_handle_start_arr(dl
, tb_health
);
6663 pr_out_str(dl
, "reporter",
6664 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6665 if (!dl
->json_output
) {
6667 __pr_out_indent_inc();
6669 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6670 pr_out_str(dl
, "state", health_state_name(state
));
6671 pr_out_u64(dl
, "error",
6672 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6673 pr_out_u64(dl
, "recover",
6674 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6675 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6676 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6677 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6679 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6680 time_ms
= mnl_attr_get_u64(attr
);
6681 format_logtime(time_ms
, dump_date
, dump_time
);
6683 pr_out_str(dl
, "last_dump_date", dump_date
);
6684 pr_out_str(dl
, "last_dump_time", dump_time
);
6686 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6687 pr_out_u64(dl
, "grace_period",
6688 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6689 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6690 pr_out_bool(dl
, "auto_recover",
6691 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6693 __pr_out_indent_dec();
6694 pr_out_handle_end(dl
);
6697 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6699 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6700 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6701 struct dl
*dl
= data
;
6703 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6704 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6705 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6706 return MNL_CB_ERROR
;
6708 pr_out_health(dl
, tb
);
6713 static int cmd_health_show(struct dl
*dl
)
6715 struct nlmsghdr
*nlh
;
6716 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6719 if (dl_argc(dl
) == 0)
6720 flags
|= NLM_F_DUMP
;
6721 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6724 if (dl_argc(dl
) > 0) {
6725 err
= dl_argv_parse_put(nlh
, dl
,
6727 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6731 pr_out_section_start(dl
, "health");
6733 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6734 pr_out_section_end(dl
);
6738 static void cmd_health_help(void)
6740 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6741 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6742 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6743 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6744 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6745 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6748 static int cmd_health(struct dl
*dl
)
6750 if (dl_argv_match(dl
, "help")) {
6753 } else if (dl_argv_match(dl
, "show") ||
6754 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6756 return cmd_health_show(dl
);
6757 } else if (dl_argv_match(dl
, "recover")) {
6759 return cmd_health_recover(dl
);
6760 } else if (dl_argv_match(dl
, "diagnose")) {
6762 return cmd_health_diagnose(dl
);
6763 } else if (dl_argv_match(dl
, "dump")) {
6765 if (dl_argv_match(dl
, "show")) {
6767 return cmd_health_dump_show(dl
);
6768 } else if (dl_argv_match(dl
, "clear")) {
6770 return cmd_health_dump_clear(dl
);
6772 } else if (dl_argv_match(dl
, "set")) {
6774 return cmd_health_set_params(dl
);
6776 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6780 static const char *trap_type_name(uint8_t type
)
6783 case DEVLINK_TRAP_TYPE_DROP
:
6785 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6788 return "<unknown type>";
6792 static const char *trap_action_name(uint8_t action
)
6795 case DEVLINK_TRAP_ACTION_DROP
:
6797 case DEVLINK_TRAP_ACTION_TRAP
:
6800 return "<unknown action>";
6804 static const char *trap_metadata_name(const struct nlattr
*attr
)
6806 switch (attr
->nla_type
) {
6807 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
6808 return "input_port";
6810 return "<unknown metadata type>";
6813 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
6815 struct nlattr
*attr_metadata
;
6817 pr_out_array_start(dl
, "metadata");
6818 mnl_attr_for_each_nested(attr_metadata
, attr
)
6819 pr_out_str_value(dl
, trap_metadata_name(attr_metadata
));
6820 pr_out_array_end(dl
);
6823 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6825 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
6826 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
6829 pr_out_handle_start_arr(dl
, tb
);
6831 __pr_out_handle_start(dl
, tb
, true, false);
6833 pr_out_str(dl
, "name", mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
6834 pr_out_str(dl
, "type", trap_type_name(type
));
6835 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6836 pr_out_str(dl
, "action", trap_action_name(action
));
6837 pr_out_str(dl
, "group",
6838 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6840 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
6841 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6842 pr_out_handle_end(dl
);
6845 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6847 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6848 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6849 struct dl
*dl
= data
;
6851 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6852 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6853 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
6854 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
6855 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
6856 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
6857 return MNL_CB_ERROR
;
6859 pr_out_trap(dl
, tb
, true);
6864 static void cmd_trap_help(void)
6866 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
6867 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
6868 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
6869 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
6872 static int cmd_trap_show(struct dl
*dl
)
6874 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6875 struct nlmsghdr
*nlh
;
6878 if (dl_argc(dl
) == 0)
6879 flags
|= NLM_F_DUMP
;
6881 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
6883 if (dl_argc(dl
) > 0) {
6884 err
= dl_argv_parse_put(nlh
, dl
,
6885 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
6890 pr_out_section_start(dl
, "trap");
6891 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
6892 pr_out_section_end(dl
);
6897 static int cmd_trap_set(struct dl
*dl
)
6899 struct nlmsghdr
*nlh
;
6902 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
6903 NLM_F_REQUEST
| NLM_F_ACK
);
6905 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
6906 DL_OPT_TRAP_ACTION
);
6910 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6913 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6916 pr_out_handle_start_arr(dl
, tb
);
6918 __pr_out_handle_start(dl
, tb
, true, false);
6920 pr_out_str(dl
, "name",
6921 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6922 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6923 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6924 pr_out_handle_end(dl
);
6927 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6929 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6930 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6931 struct dl
*dl
= data
;
6933 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6934 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6935 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
6936 return MNL_CB_ERROR
;
6938 pr_out_trap_group(dl
, tb
, true);
6943 static int cmd_trap_group_show(struct dl
*dl
)
6945 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6946 struct nlmsghdr
*nlh
;
6949 if (dl_argc(dl
) == 0)
6950 flags
|= NLM_F_DUMP
;
6952 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
6954 if (dl_argc(dl
) > 0) {
6955 err
= dl_argv_parse_put(nlh
, dl
,
6956 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
6962 pr_out_section_start(dl
, "trap_group");
6963 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
6964 pr_out_section_end(dl
);
6969 static int cmd_trap_group_set(struct dl
*dl
)
6971 struct nlmsghdr
*nlh
;
6974 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
6975 NLM_F_REQUEST
| NLM_F_ACK
);
6977 err
= dl_argv_parse_put(nlh
, dl
,
6978 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
6979 DL_OPT_TRAP_ACTION
);
6983 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6986 static int cmd_trap_group(struct dl
*dl
)
6988 if (dl_argv_match(dl
, "help")) {
6991 } else if (dl_argv_match(dl
, "show") ||
6992 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6994 return cmd_trap_group_show(dl
);
6995 } else if (dl_argv_match(dl
, "set")) {
6997 return cmd_trap_group_set(dl
);
6999 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7003 static int cmd_trap(struct dl
*dl
)
7005 if (dl_argv_match(dl
, "help")) {
7008 } else if (dl_argv_match(dl
, "show") ||
7009 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7011 return cmd_trap_show(dl
);
7012 } else if (dl_argv_match(dl
, "set")) {
7014 return cmd_trap_set(dl
);
7015 } else if (dl_argv_match(dl
, "group")) {
7017 return cmd_trap_group(dl
);
7019 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7023 static void help(void)
7025 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7026 " devlink [ -f[orce] ] -b[atch] filename\n"
7027 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7028 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7031 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7036 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7039 } else if (dl_argv_match(dl
, "dev")) {
7042 } else if (dl_argv_match(dl
, "port")) {
7044 return cmd_port(dl
);
7045 } else if (dl_argv_match(dl
, "sb")) {
7048 } else if (dl_argv_match(dl
, "monitor")) {
7051 } else if (dl_argv_match(dl
, "dpipe")) {
7053 return cmd_dpipe(dl
);
7054 } else if (dl_argv_match(dl
, "resource")) {
7056 return cmd_resource(dl
);
7057 } else if (dl_argv_match(dl
, "region")) {
7059 return cmd_region(dl
);
7060 } else if (dl_argv_match(dl
, "health")) {
7062 return cmd_health(dl
);
7063 } else if (dl_argv_match(dl
, "trap")) {
7065 return cmd_trap(dl
);
7067 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7071 static int dl_init(struct dl
*dl
)
7075 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7077 pr_err("Failed to connect to devlink Netlink\n");
7081 err
= ifname_map_init(dl
);
7083 pr_err("Failed to create index map\n");
7084 goto err_ifname_map_create
;
7086 if (dl
->json_output
) {
7087 dl
->jw
= jsonw_new(stdout
);
7089 pr_err("Failed to create JSON writer\n");
7092 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
7097 ifname_map_fini(dl
);
7098 err_ifname_map_create
:
7099 mnlg_socket_close(dl
->nlg
);
7103 static void dl_fini(struct dl
*dl
)
7105 if (dl
->json_output
)
7106 jsonw_destroy(&dl
->jw
);
7107 ifname_map_fini(dl
);
7108 mnlg_socket_close(dl
->nlg
);
7111 static struct dl
*dl_alloc(void)
7115 dl
= calloc(1, sizeof(*dl
));
7121 static void dl_free(struct dl
*dl
)
7126 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7130 int ret
= EXIT_SUCCESS
;
7132 if (name
&& strcmp(name
, "-") != 0) {
7133 if (freopen(name
, "r", stdin
) == NULL
) {
7135 "Cannot open file \"%s\" for reading: %s\n",
7136 name
, strerror(errno
));
7137 return EXIT_FAILURE
;
7142 while (getcmdline(&line
, &len
, stdin
) != -1) {
7146 largc
= makeargs(line
, largv
, 100);
7148 continue; /* blank line */
7150 if (dl_cmd(dl
, largc
, largv
)) {
7151 fprintf(stderr
, "Command failed %s:%d\n",
7165 int main(int argc
, char **argv
)
7167 static const struct option long_options
[] = {
7168 { "Version", no_argument
, NULL
, 'V' },
7169 { "force", no_argument
, NULL
, 'f' },
7170 { "batch", required_argument
, NULL
, 'b' },
7171 { "no-nice-names", no_argument
, NULL
, 'n' },
7172 { "json", no_argument
, NULL
, 'j' },
7173 { "pretty", no_argument
, NULL
, 'p' },
7174 { "verbose", no_argument
, NULL
, 'v' },
7175 { "statistics", no_argument
, NULL
, 's' },
7176 { NULL
, 0, NULL
, 0 }
7178 const char *batch_file
= NULL
;
7187 pr_err("Failed to allocate memory for devlink\n");
7188 return EXIT_FAILURE
;
7191 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvs",
7192 long_options
, NULL
)) >= 0) {
7196 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7203 batch_file
= optarg
;
7206 dl
->no_nice_names
= true;
7209 dl
->json_output
= true;
7212 dl
->pretty_output
= true;
7221 pr_err("Unknown option.\n");
7238 err
= dl_batch(dl
, batch_file
, force
);
7240 err
= dl_cmd(dl
, argc
, argv
);