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(27)
259 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(28)
260 #define DL_OPT_TRAP_NAME BIT(29)
261 #define DL_OPT_TRAP_ACTION BIT(30)
262 #define DL_OPT_TRAP_GROUP_NAME BIT(31)
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 const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
383 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
384 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
385 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
386 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
387 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
388 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
389 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
390 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
391 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
392 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
393 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
394 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
395 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
396 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
397 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
398 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
399 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
400 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
401 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
402 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
403 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
404 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
405 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
406 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
407 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
408 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
409 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
410 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
411 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
412 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
413 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
414 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
415 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
416 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
417 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
418 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
419 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
420 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
421 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
422 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
423 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
424 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
425 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
426 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
427 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
428 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
429 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
430 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
431 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
432 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
433 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
434 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
435 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
436 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
437 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
438 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
439 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
440 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
441 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
442 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
443 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
444 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
445 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
446 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
447 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
448 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
449 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
450 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
451 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
452 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
453 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
454 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
455 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
456 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
457 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
458 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
459 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
460 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
461 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
462 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
463 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
464 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
465 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
466 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
467 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
468 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
469 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
470 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = MNL_TYPE_STRING
,
471 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
] = MNL_TYPE_STRING
,
472 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
] = MNL_TYPE_U64
,
473 [DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
] = MNL_TYPE_U64
,
474 [DEVLINK_ATTR_STATS
] = MNL_TYPE_NESTED
,
475 [DEVLINK_ATTR_TRAP_NAME
] = MNL_TYPE_STRING
,
476 [DEVLINK_ATTR_TRAP_ACTION
] = MNL_TYPE_U8
,
477 [DEVLINK_ATTR_TRAP_TYPE
] = MNL_TYPE_U8
,
478 [DEVLINK_ATTR_TRAP_GENERIC
] = MNL_TYPE_FLAG
,
479 [DEVLINK_ATTR_TRAP_METADATA
] = MNL_TYPE_NESTED
,
480 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = MNL_TYPE_STRING
,
483 static const enum mnl_attr_data_type
484 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
485 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
486 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
489 static int attr_cb(const struct nlattr
*attr
, void *data
)
491 const struct nlattr
**tb
= data
;
494 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
497 type
= mnl_attr_get_type(attr
);
498 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
505 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
507 const struct nlattr
**tb
= data
;
510 /* Allow the tool to work on top of newer kernels that might contain
513 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
516 type
= mnl_attr_get_type(attr
);
517 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
524 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
526 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
527 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
528 struct dl
*dl
= data
;
529 struct ifname_map
*ifname_map
;
530 const char *bus_name
;
531 const char *dev_name
;
532 uint32_t port_ifindex
;
533 const char *port_ifname
;
535 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
536 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
537 !tb
[DEVLINK_ATTR_PORT_INDEX
])
540 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
543 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
544 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
545 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
546 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
547 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
548 port_ifindex
, port_ifname
);
551 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
556 static void ifname_map_fini(struct dl
*dl
)
558 struct ifname_map
*ifname_map
, *tmp
;
560 list_for_each_entry_safe(ifname_map
, tmp
,
561 &dl
->ifname_map_list
, list
) {
562 list_del(&ifname_map
->list
);
563 ifname_map_free(ifname_map
);
567 static int ifname_map_init(struct dl
*dl
)
569 struct nlmsghdr
*nlh
;
572 INIT_LIST_HEAD(&dl
->ifname_map_list
);
574 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
575 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
577 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
585 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
586 char **p_bus_name
, char **p_dev_name
,
587 uint32_t *p_port_index
)
589 struct ifname_map
*ifname_map
;
591 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
592 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
593 *p_bus_name
= ifname_map
->bus_name
;
594 *p_dev_name
= ifname_map
->dev_name
;
595 *p_port_index
= ifname_map
->port_index
;
602 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
603 const char *dev_name
, uint32_t port_index
,
606 struct ifname_map
*ifname_map
;
608 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
609 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
610 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
611 port_index
== ifname_map
->port_index
) {
612 *p_ifname
= ifname_map
->ifname
;
619 static unsigned int strslashcount(char *str
)
621 unsigned int count
= 0;
624 while ((pos
= strchr(pos
, '/'))) {
631 static int strslashrsplit(char *str
, char **before
, char **after
)
635 slash
= strrchr(str
, '/');
644 static int strtouint64_t(const char *str
, uint64_t *p_val
)
647 unsigned long long int val
;
649 val
= strtoull(str
, &endptr
, 10);
650 if (endptr
== str
|| *endptr
!= '\0')
658 static int strtouint32_t(const char *str
, uint32_t *p_val
)
661 unsigned long int val
;
663 val
= strtoul(str
, &endptr
, 10);
664 if (endptr
== str
|| *endptr
!= '\0')
672 static int strtouint16_t(const char *str
, uint16_t *p_val
)
675 unsigned long int val
;
677 val
= strtoul(str
, &endptr
, 10);
678 if (endptr
== str
|| *endptr
!= '\0')
686 static int strtouint8_t(const char *str
, uint8_t *p_val
)
689 unsigned long int val
;
691 val
= strtoul(str
, &endptr
, 10);
692 if (endptr
== str
|| *endptr
!= '\0')
700 static int strtobool(const char *str
, bool *p_val
)
704 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
706 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
714 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
716 strslashrsplit(str
, p_bus_name
, p_dev_name
);
720 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
722 char *str
= dl_argv_next(dl
);
725 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
728 if (strslashcount(str
) != 1) {
729 pr_err("Wrong devlink identification string format.\n");
730 pr_err("Expected \"bus_name/dev_name\".\n");
733 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
736 static int __dl_argv_handle_port(char *str
,
737 char **p_bus_name
, char **p_dev_name
,
738 uint32_t *p_port_index
)
744 err
= strslashrsplit(str
, &handlestr
, &portstr
);
746 pr_err("Port identification \"%s\" is invalid\n", str
);
749 err
= strtouint32_t(portstr
, p_port_index
);
751 pr_err("Port index \"%s\" is not a number or not within range\n",
755 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
757 pr_err("Port identification \"%s\" is invalid\n", str
);
763 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
764 char **p_bus_name
, char **p_dev_name
,
765 uint32_t *p_port_index
)
769 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
772 pr_err("Netdevice \"%s\" not found\n", str
);
778 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
779 char **p_dev_name
, uint32_t *p_port_index
)
781 char *str
= dl_argv_next(dl
);
782 unsigned int slash_count
;
785 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
788 slash_count
= strslashcount(str
);
789 switch (slash_count
) {
791 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
792 p_dev_name
, p_port_index
);
794 return __dl_argv_handle_port(str
, p_bus_name
,
795 p_dev_name
, p_port_index
);
797 pr_err("Wrong port identification string format.\n");
798 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
803 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
804 char **p_dev_name
, uint32_t *p_port_index
,
805 uint64_t *p_handle_bit
)
807 char *str
= dl_argv_next(dl
);
808 unsigned int slash_count
;
812 pr_err("One of following identifications expected:\n"
813 "Devlink identification (\"bus_name/dev_name\")\n"
814 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
817 slash_count
= strslashcount(str
);
818 if (slash_count
== 1) {
819 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
822 *p_handle_bit
= DL_OPT_HANDLE
;
823 } else if (slash_count
== 2) {
824 err
= __dl_argv_handle_port(str
, p_bus_name
,
825 p_dev_name
, p_port_index
);
828 *p_handle_bit
= DL_OPT_HANDLEP
;
829 } else if (slash_count
== 0) {
830 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
831 p_dev_name
, p_port_index
);
834 *p_handle_bit
= DL_OPT_HANDLEP
;
836 pr_err("Wrong port identification string format.\n");
837 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
843 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
844 char **p_dev_name
, char **p_region
)
849 err
= strslashrsplit(str
, &handlestr
, p_region
);
851 pr_err("Region identification \"%s\" is invalid\n", str
);
854 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
856 pr_err("Region identification \"%s\" is invalid\n", str
);
862 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
863 char **p_dev_name
, char **p_region
)
865 char *str
= dl_argv_next(dl
);
866 unsigned int slash_count
;
869 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
873 slash_count
= strslashcount(str
);
874 if (slash_count
!= 2) {
875 pr_err("Wrong region identification string format.\n");
876 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
880 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
883 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
885 char *str
= dl_argv_next(dl
);
889 pr_err("Unsigned number argument expected\n");
893 err
= strtouint64_t(str
, p_val
);
895 pr_err("\"%s\" is not a number or not within range\n", str
);
901 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
903 char *str
= dl_argv_next(dl
);
907 pr_err("Unsigned number argument expected\n");
911 err
= strtouint32_t(str
, p_val
);
913 pr_err("\"%s\" is not a number or not within range\n", str
);
919 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
921 char *str
= dl_argv_next(dl
);
925 pr_err("Unsigned number argument expected\n");
929 err
= strtouint16_t(str
, p_val
);
931 pr_err("\"%s\" is not a number or not within range\n", str
);
937 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
939 char *str
= dl_argv_next(dl
);
943 pr_err("Boolean argument expected\n");
947 err
= strtobool(str
, p_val
);
949 pr_err("\"%s\" is not a valid boolean value\n", str
);
955 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
957 const char *str
= dl_argv_next(dl
);
960 pr_err("String parameter expected\n");
967 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
969 if (strcmp(typestr
, "auto") == 0) {
970 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
971 } else if (strcmp(typestr
, "eth") == 0) {
972 *p_type
= DEVLINK_PORT_TYPE_ETH
;
973 } else if (strcmp(typestr
, "ib") == 0) {
974 *p_type
= DEVLINK_PORT_TYPE_IB
;
976 pr_err("Unknown port type \"%s\"\n", typestr
);
982 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
984 if (strcmp(typestr
, "ingress") == 0) {
985 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
986 } else if (strcmp(typestr
, "egress") == 0) {
987 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
989 pr_err("Unknown pool type \"%s\"\n", typestr
);
995 static int threshold_type_get(const char *typestr
,
996 enum devlink_sb_threshold_type
*p_type
)
998 if (strcmp(typestr
, "static") == 0) {
999 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1000 } else if (strcmp(typestr
, "dynamic") == 0) {
1001 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1003 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1009 static int eswitch_mode_get(const char *typestr
,
1010 enum devlink_eswitch_mode
*p_mode
)
1012 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1013 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1014 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1015 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1017 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1023 static int eswitch_inline_mode_get(const char *typestr
,
1024 enum devlink_eswitch_inline_mode
*p_mode
)
1026 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1027 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1028 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1029 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1030 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1031 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1032 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1033 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1035 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1041 static int dpipe_counters_enable_get(const char *typestr
,
1042 bool *counters_enable
)
1044 if (strcmp(typestr
, "enable") == 0) {
1045 *counters_enable
= 1;
1046 } else if (strcmp(typestr
, "disable") == 0) {
1047 *counters_enable
= 0;
1049 pr_err("Unknown counter_state \"%s\"\n", typestr
);
1055 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
1057 if (strcmp(typestr
, "enable") == 0) {
1059 } else if (strcmp(typestr
, "disable") == 0) {
1062 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1068 static int param_cmode_get(const char *cmodestr
,
1069 enum devlink_param_cmode
*cmode
)
1071 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1072 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1073 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1074 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1075 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1076 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1078 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1084 static int trap_action_get(const char *actionstr
,
1085 enum devlink_trap_action
*p_action
)
1087 if (strcmp(actionstr
, "drop") == 0) {
1088 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1089 } else if (strcmp(actionstr
, "trap") == 0) {
1090 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1092 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1098 struct dl_args_metadata
{
1100 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1103 static const struct dl_args_metadata dl_args_required
[] = {
1104 {DL_OPT_PORT_TYPE
, "Port type not set."},
1105 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1106 {DL_OPT_SB_POOL
, "Pool index option expected."},
1107 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1108 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1109 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1110 {DL_OPT_SB_TH
, "Threshold option expected."},
1111 {DL_OPT_SB_TC
, "TC index option expected."},
1112 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1113 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1114 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1115 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1116 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1117 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1118 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1119 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1120 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1121 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1122 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1123 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1124 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1125 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1128 static int dl_args_finding_required_validate(uint64_t o_required
,
1134 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1135 o_flag
= dl_args_required
[i
].o_flag
;
1136 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1137 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1144 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1145 uint64_t o_optional
)
1147 struct dl_opts
*opts
= &dl
->opts
;
1148 uint64_t o_all
= o_required
| o_optional
;
1149 uint64_t o_found
= 0;
1152 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1153 uint64_t handle_bit
;
1155 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1156 &opts
->port_index
, &handle_bit
);
1159 o_found
|= handle_bit
;
1160 } else if (o_required
& DL_OPT_HANDLE
) {
1161 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1164 o_found
|= DL_OPT_HANDLE
;
1165 } else if (o_required
& DL_OPT_HANDLEP
) {
1166 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1170 o_found
|= DL_OPT_HANDLEP
;
1171 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1172 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1174 &opts
->region_name
);
1177 o_found
|= DL_OPT_HANDLE_REGION
;
1180 while (dl_argc(dl
)) {
1181 if (dl_argv_match(dl
, "type") &&
1182 (o_all
& DL_OPT_PORT_TYPE
)) {
1183 const char *typestr
;
1186 err
= dl_argv_str(dl
, &typestr
);
1189 err
= port_type_get(typestr
, &opts
->port_type
);
1192 o_found
|= DL_OPT_PORT_TYPE
;
1193 } else if (dl_argv_match(dl
, "count") &&
1194 (o_all
& DL_OPT_PORT_COUNT
)) {
1196 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1199 o_found
|= DL_OPT_PORT_COUNT
;
1200 } else if (dl_argv_match(dl
, "sb") &&
1201 (o_all
& DL_OPT_SB
)) {
1203 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1206 o_found
|= DL_OPT_SB
;
1207 } else if (dl_argv_match(dl
, "pool") &&
1208 (o_all
& DL_OPT_SB_POOL
)) {
1210 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1213 o_found
|= DL_OPT_SB_POOL
;
1214 } else if (dl_argv_match(dl
, "size") &&
1215 (o_all
& DL_OPT_SB_SIZE
)) {
1217 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1220 o_found
|= DL_OPT_SB_SIZE
;
1221 } else if (dl_argv_match(dl
, "type") &&
1222 (o_all
& DL_OPT_SB_TYPE
)) {
1223 const char *typestr
;
1226 err
= dl_argv_str(dl
, &typestr
);
1229 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1232 o_found
|= DL_OPT_SB_TYPE
;
1233 } else if (dl_argv_match(dl
, "thtype") &&
1234 (o_all
& DL_OPT_SB_THTYPE
)) {
1235 const char *typestr
;
1238 err
= dl_argv_str(dl
, &typestr
);
1241 err
= threshold_type_get(typestr
,
1242 &opts
->sb_pool_thtype
);
1245 o_found
|= DL_OPT_SB_THTYPE
;
1246 } else if (dl_argv_match(dl
, "th") &&
1247 (o_all
& DL_OPT_SB_TH
)) {
1249 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1252 o_found
|= DL_OPT_SB_TH
;
1253 } else if (dl_argv_match(dl
, "tc") &&
1254 (o_all
& DL_OPT_SB_TC
)) {
1256 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1259 o_found
|= DL_OPT_SB_TC
;
1260 } else if (dl_argv_match(dl
, "mode") &&
1261 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1262 const char *typestr
;
1265 err
= dl_argv_str(dl
, &typestr
);
1268 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1271 o_found
|= DL_OPT_ESWITCH_MODE
;
1272 } else if (dl_argv_match(dl
, "inline-mode") &&
1273 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1274 const char *typestr
;
1277 err
= dl_argv_str(dl
, &typestr
);
1280 err
= eswitch_inline_mode_get(
1281 typestr
, &opts
->eswitch_inline_mode
);
1284 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1285 } else if (dl_argv_match(dl
, "name") &&
1286 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1288 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1291 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1292 } else if (dl_argv_match(dl
, "counters") &&
1293 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1294 const char *typestr
;
1297 err
= dl_argv_str(dl
, &typestr
);
1300 err
= dpipe_counters_enable_get(typestr
,
1301 &opts
->dpipe_counters_enable
);
1304 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1305 } else if (dl_argv_match(dl
, "encap") &&
1306 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1307 const char *typestr
;
1310 err
= dl_argv_str(dl
, &typestr
);
1313 err
= eswitch_encap_mode_get(typestr
,
1314 &opts
->eswitch_encap_mode
);
1317 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1318 } else if (dl_argv_match(dl
, "path") &&
1319 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1321 err
= dl_argv_str(dl
, &opts
->resource_path
);
1324 o_found
|= DL_OPT_RESOURCE_PATH
;
1325 } else if (dl_argv_match(dl
, "size") &&
1326 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1328 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1331 o_found
|= DL_OPT_RESOURCE_SIZE
;
1332 } else if (dl_argv_match(dl
, "name") &&
1333 (o_all
& DL_OPT_PARAM_NAME
)) {
1335 err
= dl_argv_str(dl
, &opts
->param_name
);
1338 o_found
|= DL_OPT_PARAM_NAME
;
1339 } else if (dl_argv_match(dl
, "value") &&
1340 (o_all
& DL_OPT_PARAM_VALUE
)) {
1342 err
= dl_argv_str(dl
, &opts
->param_value
);
1345 o_found
|= DL_OPT_PARAM_VALUE
;
1346 } else if (dl_argv_match(dl
, "cmode") &&
1347 (o_all
& DL_OPT_PARAM_CMODE
)) {
1348 const char *cmodestr
;
1351 err
= dl_argv_str(dl
, &cmodestr
);
1354 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1357 o_found
|= DL_OPT_PARAM_CMODE
;
1358 } else if (dl_argv_match(dl
, "snapshot") &&
1359 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1361 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1364 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1365 } else if (dl_argv_match(dl
, "address") &&
1366 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1368 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1371 o_found
|= DL_OPT_REGION_ADDRESS
;
1372 } else if (dl_argv_match(dl
, "length") &&
1373 (o_all
& DL_OPT_REGION_LENGTH
)) {
1375 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1378 o_found
|= DL_OPT_REGION_LENGTH
;
1379 } else if (dl_argv_match(dl
, "file") &&
1380 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1382 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1385 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1386 } else if (dl_argv_match(dl
, "component") &&
1387 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1389 err
= dl_argv_str(dl
, &opts
->flash_component
);
1392 o_found
|= DL_OPT_FLASH_COMPONENT
;
1393 } else if (dl_argv_match(dl
, "reporter") &&
1394 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1396 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1399 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1400 } else if (dl_argv_match(dl
, "grace_period") &&
1401 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1403 err
= dl_argv_uint64_t(dl
,
1404 &opts
->reporter_graceful_period
);
1407 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1408 } else if (dl_argv_match(dl
, "auto_recover") &&
1409 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1411 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1414 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1415 } else if (dl_argv_match(dl
, "trap") &&
1416 (o_all
& DL_OPT_TRAP_NAME
)) {
1418 err
= dl_argv_str(dl
, &opts
->trap_name
);
1421 o_found
|= DL_OPT_TRAP_NAME
;
1422 } else if (dl_argv_match(dl
, "group") &&
1423 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1425 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1428 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1429 } else if (dl_argv_match(dl
, "action") &&
1430 (o_all
& DL_OPT_TRAP_ACTION
)) {
1431 const char *actionstr
;
1434 err
= dl_argv_str(dl
, &actionstr
);
1437 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1440 o_found
|= DL_OPT_TRAP_ACTION
;
1442 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1447 opts
->present
= o_found
;
1449 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1451 opts
->present
|= DL_OPT_SB
;
1454 return dl_args_finding_required_validate(o_required
, o_found
);
1457 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1459 struct dl_opts
*opts
= &dl
->opts
;
1461 if (opts
->present
& DL_OPT_HANDLE
) {
1462 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1463 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1464 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1465 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1466 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1467 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1469 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1470 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1471 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1472 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1475 if (opts
->present
& DL_OPT_PORT_TYPE
)
1476 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1478 if (opts
->present
& DL_OPT_PORT_COUNT
)
1479 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1481 if (opts
->present
& DL_OPT_SB
)
1482 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1484 if (opts
->present
& DL_OPT_SB_POOL
)
1485 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1486 opts
->sb_pool_index
);
1487 if (opts
->present
& DL_OPT_SB_SIZE
)
1488 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1489 opts
->sb_pool_size
);
1490 if (opts
->present
& DL_OPT_SB_TYPE
)
1491 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1492 opts
->sb_pool_type
);
1493 if (opts
->present
& DL_OPT_SB_THTYPE
)
1494 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1495 opts
->sb_pool_thtype
);
1496 if (opts
->present
& DL_OPT_SB_TH
)
1497 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1498 opts
->sb_threshold
);
1499 if (opts
->present
& DL_OPT_SB_TC
)
1500 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1502 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1503 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1504 opts
->eswitch_mode
);
1505 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1506 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1507 opts
->eswitch_inline_mode
);
1508 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1509 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1510 opts
->dpipe_table_name
);
1511 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1512 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1513 opts
->dpipe_counters_enable
);
1514 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1515 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1516 opts
->eswitch_encap_mode
);
1517 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1518 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1520 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1521 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1522 opts
->resource_size
);
1523 if (opts
->present
& DL_OPT_PARAM_NAME
)
1524 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1526 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1527 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1529 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1530 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1531 opts
->region_snapshot_id
);
1532 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1533 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1534 opts
->region_address
);
1535 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1536 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1537 opts
->region_length
);
1538 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1539 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1540 opts
->flash_file_name
);
1541 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1542 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1543 opts
->flash_component
);
1544 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1545 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1546 opts
->reporter_name
);
1547 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1548 mnl_attr_put_u64(nlh
,
1549 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1550 opts
->reporter_graceful_period
);
1551 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1552 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1553 opts
->reporter_auto_recover
);
1554 if (opts
->present
& DL_OPT_TRAP_NAME
)
1555 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1557 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1558 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1559 opts
->trap_group_name
);
1560 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1561 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1566 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1567 uint64_t o_required
, uint64_t o_optional
)
1571 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1574 dl_opts_put(nlh
, dl
);
1578 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1580 struct dl_opts
*opts
= &dl
->opts
;
1581 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1582 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1583 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1584 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1586 if (opts
->present
& DL_OPT_HANDLE
&&
1587 attr_bus_name
&& attr_dev_name
) {
1588 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1589 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1591 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1592 strcmp(dev_name
, opts
->dev_name
) != 0)
1595 if (opts
->present
& DL_OPT_HANDLEP
&&
1596 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1597 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1598 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1599 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1601 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1602 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1603 port_index
!= opts
->port_index
)
1606 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1607 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1609 if (sb_index
!= opts
->sb_index
)
1615 static void cmd_dev_help(void)
1617 pr_err("Usage: devlink dev show [ DEV ]\n");
1618 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1619 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1620 pr_err(" [ encap { disable | enable } ]\n");
1621 pr_err(" devlink dev eswitch show DEV\n");
1622 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1623 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1624 pr_err(" devlink dev reload DEV\n");
1625 pr_err(" devlink dev info [ DEV ]\n");
1626 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1629 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1630 const char *dev_name
)
1632 if (!dl
->arr_last
.present
)
1634 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1635 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1638 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1639 const char *dev_name
)
1641 dl
->arr_last
.present
= true;
1642 free(dl
->arr_last
.dev_name
);
1643 free(dl
->arr_last
.bus_name
);
1644 dl
->arr_last
.bus_name
= strdup(bus_name
);
1645 dl
->arr_last
.dev_name
= strdup(dev_name
);
1648 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1649 const char *dev_name
)
1651 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1654 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1655 const char *dev_name
)
1657 return dl
->arr_last
.present
&&
1658 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1661 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1662 bool content
, bool array
)
1664 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1665 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1668 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1670 if (dl
->json_output
) {
1672 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1673 jsonw_end_array(dl
->jw
);
1674 if (should_arr_last_handle_start(dl
, bus_name
,
1676 jsonw_name(dl
->jw
, buf
);
1677 jsonw_start_array(dl
->jw
);
1678 jsonw_start_object(dl
->jw
);
1679 arr_last_handle_set(dl
, bus_name
, dev_name
);
1681 jsonw_start_object(dl
->jw
);
1684 jsonw_name(dl
->jw
, buf
);
1685 jsonw_start_object(dl
->jw
);
1689 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1690 __pr_out_indent_dec();
1691 if (should_arr_last_handle_start(dl
, bus_name
,
1693 pr_out("%s%s", buf
, content
? ":" : "");
1695 __pr_out_indent_inc();
1696 arr_last_handle_set(dl
, bus_name
, dev_name
);
1699 pr_out("%s%s", buf
, content
? ":" : "");
1704 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1706 __pr_out_handle_start(dl
, tb
, true, true);
1709 static void pr_out_handle_end(struct dl
*dl
)
1711 if (dl
->json_output
)
1712 jsonw_end_object(dl
->jw
);
1717 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1719 __pr_out_handle_start(dl
, tb
, false, false);
1720 pr_out_handle_end(dl
);
1723 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1724 const char *dev_name
, uint32_t port_index
)
1726 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1727 dl
->arr_last
.port_index
== port_index
;
1730 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1731 const char *dev_name
, uint32_t port_index
)
1733 arr_last_handle_set(dl
, bus_name
, dev_name
);
1734 dl
->arr_last
.port_index
= port_index
;
1737 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1738 const char *bus_name
,
1739 const char *dev_name
,
1740 uint32_t port_index
)
1742 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1745 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1746 const char *bus_name
,
1747 const char *dev_name
,
1748 uint32_t port_index
)
1750 return dl
->arr_last
.present
&&
1751 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1754 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1755 const char *dev_name
,
1756 uint32_t port_index
, bool try_nice
,
1759 static char buf
[64];
1760 char *ifname
= NULL
;
1762 if (dl
->no_nice_names
|| !try_nice
||
1763 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1764 port_index
, &ifname
) != 0)
1765 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1767 sprintf(buf
, "%s", ifname
);
1769 if (dl
->json_output
) {
1771 if (should_arr_last_port_handle_end(dl
, bus_name
,
1774 jsonw_end_array(dl
->jw
);
1775 if (should_arr_last_port_handle_start(dl
, bus_name
,
1778 jsonw_name(dl
->jw
, buf
);
1779 jsonw_start_array(dl
->jw
);
1780 jsonw_start_object(dl
->jw
);
1781 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1784 jsonw_start_object(dl
->jw
);
1787 jsonw_name(dl
->jw
, buf
);
1788 jsonw_start_object(dl
->jw
);
1795 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1797 const char *bus_name
;
1798 const char *dev_name
;
1799 uint32_t port_index
;
1801 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1802 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1803 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1804 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1807 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1809 const char *bus_name
;
1810 const char *dev_name
;
1811 uint32_t port_index
;
1813 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1814 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1815 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1816 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1819 static void pr_out_port_handle_end(struct dl
*dl
)
1821 if (dl
->json_output
)
1822 jsonw_end_object(dl
->jw
);
1828 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1830 if (dl
->json_output
) {
1831 jsonw_string_field(dl
->jw
, name
, val
);
1833 if (g_indent_newline
)
1834 pr_out("%s %s", name
, val
);
1836 pr_out(" %s %s", name
, val
);
1840 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1842 if (dl
->json_output
)
1843 jsonw_bool_field(dl
->jw
, name
, val
);
1845 pr_out_str(dl
, name
, val
? "true" : "false");
1848 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1850 if (dl
->json_output
) {
1851 jsonw_uint_field(dl
->jw
, name
, val
);
1853 if (g_indent_newline
)
1854 pr_out("%s %u", name
, val
);
1856 pr_out(" %s %u", name
, val
);
1860 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1862 if (val
== (uint64_t) -1)
1863 return pr_out_str(dl
, name
, "unlimited");
1865 if (dl
->json_output
) {
1866 jsonw_u64_field(dl
->jw
, name
, val
);
1868 if (g_indent_newline
)
1869 pr_out("%s %"PRIu64
, name
, val
);
1871 pr_out(" %s %"PRIu64
, name
, val
);
1875 static void pr_out_bool_value(struct dl
*dl
, bool value
)
1877 if (dl
->json_output
)
1878 jsonw_bool(dl
->jw
, value
);
1880 pr_out(" %s", value
? "true" : "false");
1883 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
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 if (dl
->json_output
)
1894 jsonw_u64(dl
->jw
, value
);
1896 pr_out(" %"PRIu64
, value
);
1899 static bool is_binary_eol(int i
)
1904 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1909 if (dl
->json_output
)
1910 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1912 pr_out("%02x ", data
[i
]);
1914 if (!dl
->json_output
&& is_binary_eol(i
))
1917 if (!dl
->json_output
&& !is_binary_eol(i
))
1921 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1923 if (dl
->json_output
)
1924 jsonw_string(dl
->jw
, value
);
1926 pr_out(" %s", value
);
1929 static void pr_out_name(struct dl
*dl
, const char *name
)
1931 if (dl
->json_output
)
1932 jsonw_name(dl
->jw
, name
);
1934 pr_out(" %s:", name
);
1937 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1939 if (dl
->json_output
) {
1940 jsonw_name(dl
->jw
, "address");
1941 jsonw_uint(dl
->jw
, addr
);
1942 jsonw_name(dl
->jw
, "data");
1943 jsonw_start_array(dl
->jw
);
1947 static void pr_out_region_chunk_end(struct dl
*dl
)
1949 if (dl
->json_output
)
1950 jsonw_end_array(dl
->jw
);
1953 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1956 static uint64_t align_val
;
1959 pr_out_region_chunk_start(dl
, addr
);
1961 if (!dl
->json_output
)
1962 if (!(align_val
% 16))
1963 pr_out("%s%016"PRIx64
" ",
1964 align_val
? "\n" : "",
1969 if (dl
->json_output
)
1970 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1972 pr_out("%02x ", data
[i
]);
1977 pr_out_region_chunk_end(dl
);
1980 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1982 pr_out_handle(dl
, tb
);
1985 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1987 if (dl
->json_output
) {
1988 jsonw_start_object(dl
->jw
);
1989 jsonw_name(dl
->jw
, name
);
1990 jsonw_start_object(dl
->jw
);
1994 static void pr_out_section_end(struct dl
*dl
)
1996 if (dl
->json_output
) {
1997 if (dl
->arr_last
.present
)
1998 jsonw_end_array(dl
->jw
);
1999 jsonw_end_object(dl
->jw
);
2000 jsonw_end_object(dl
->jw
);
2004 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2006 if (dl
->json_output
) {
2007 jsonw_name(dl
->jw
, name
);
2008 jsonw_start_array(dl
->jw
);
2010 __pr_out_indent_inc();
2012 pr_out("%s:", name
);
2013 __pr_out_indent_inc();
2018 static void pr_out_array_end(struct dl
*dl
)
2020 if (dl
->json_output
) {
2021 jsonw_end_array(dl
->jw
);
2023 __pr_out_indent_dec();
2024 __pr_out_indent_dec();
2028 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2030 if (dl
->json_output
) {
2031 jsonw_name(dl
->jw
, name
);
2032 jsonw_start_object(dl
->jw
);
2034 __pr_out_indent_inc();
2036 pr_out("%s:", name
);
2037 __pr_out_indent_inc();
2042 static void pr_out_object_end(struct dl
*dl
)
2044 if (dl
->json_output
) {
2045 jsonw_end_object(dl
->jw
);
2047 __pr_out_indent_dec();
2048 __pr_out_indent_dec();
2052 static void pr_out_entry_start(struct dl
*dl
)
2054 if (dl
->json_output
)
2055 jsonw_start_object(dl
->jw
);
2058 static void pr_out_entry_end(struct dl
*dl
)
2060 if (dl
->json_output
)
2061 jsonw_end_object(dl
->jw
);
2066 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2068 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2074 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2075 if (err
!= MNL_CB_OK
)
2078 pr_out_object_start(dl
, "stats");
2079 pr_out_object_start(dl
, "rx");
2080 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2081 pr_out_u64(dl
, "bytes",
2082 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2083 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2084 pr_out_u64(dl
, "packets",
2085 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2086 pr_out_object_end(dl
);
2087 pr_out_object_end(dl
);
2090 static const char *param_cmode_name(uint8_t cmode
)
2093 case DEVLINK_PARAM_CMODE_RUNTIME
:
2094 return PARAM_CMODE_RUNTIME_STR
;
2095 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2096 return PARAM_CMODE_DRIVERINIT_STR
;
2097 case DEVLINK_PARAM_CMODE_PERMANENT
:
2098 return PARAM_CMODE_PERMANENT_STR
;
2099 default: return "<unknown type>";
2103 static const char *eswitch_mode_name(uint32_t mode
)
2106 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2107 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2108 default: return "<unknown mode>";
2112 static const char *eswitch_inline_mode_name(uint32_t mode
)
2115 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2116 return ESWITCH_INLINE_MODE_NONE
;
2117 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2118 return ESWITCH_INLINE_MODE_LINK
;
2119 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2120 return ESWITCH_INLINE_MODE_NETWORK
;
2121 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2122 return ESWITCH_INLINE_MODE_TRANSPORT
;
2124 return "<unknown mode>";
2128 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2130 __pr_out_handle_start(dl
, tb
, true, false);
2132 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
2133 pr_out_str(dl
, "mode",
2134 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2136 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
2137 pr_out_str(dl
, "inline-mode",
2138 eswitch_inline_mode_name(mnl_attr_get_u8(
2139 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2141 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2142 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2144 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
2147 pr_out_handle_end(dl
);
2150 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2152 struct dl
*dl
= data
;
2153 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2154 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2156 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2157 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2158 return MNL_CB_ERROR
;
2159 pr_out_eswitch(dl
, tb
);
2163 static int cmd_dev_eswitch_show(struct dl
*dl
)
2165 struct nlmsghdr
*nlh
;
2168 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2169 NLM_F_REQUEST
| NLM_F_ACK
);
2171 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2175 pr_out_section_start(dl
, "dev");
2176 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2177 pr_out_section_end(dl
);
2181 static int cmd_dev_eswitch_set(struct dl
*dl
)
2183 struct nlmsghdr
*nlh
;
2186 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2187 NLM_F_REQUEST
| NLM_F_ACK
);
2189 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2190 DL_OPT_ESWITCH_MODE
|
2191 DL_OPT_ESWITCH_INLINE_MODE
|
2192 DL_OPT_ESWITCH_ENCAP_MODE
);
2197 if (dl
->opts
.present
== 1) {
2198 pr_err("Need to set at least one option\n");
2202 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2205 static int cmd_dev_eswitch(struct dl
*dl
)
2207 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2210 } else if (dl_argv_match(dl
, "set")) {
2212 return cmd_dev_eswitch_set(dl
);
2213 } else if (dl_argv_match(dl
, "show")) {
2215 return cmd_dev_eswitch_show(dl
);
2217 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2221 struct param_val_conv
{
2227 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2228 uint32_t len
, const char *name
)
2232 for (i
= 0; i
< len
; i
++)
2233 if (!strcmp(param_val_conv
[i
].name
, name
))
2240 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2241 uint32_t len
, const char *name
, const char *vstr
,
2246 for (i
= 0; i
< len
; i
++)
2247 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2248 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2249 *vuint
= param_val_conv
[i
].vuint
;
2257 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2258 uint32_t len
, const char *name
, uint32_t vuint
,
2263 for (i
= 0; i
< len
; i
++)
2264 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2265 param_val_conv
[i
].vuint
== vuint
) {
2266 *vstr
= param_val_conv
[i
].vstr
;
2273 static const struct param_val_conv param_val_conv
[] = {
2275 .name
= "fw_load_policy",
2277 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2280 .name
= "fw_load_policy",
2282 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2285 .name
= "reset_dev_on_drv_probe",
2287 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2290 .name
= "fw_load_policy",
2292 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2295 .name
= "reset_dev_on_drv_probe",
2297 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2300 .name
= "reset_dev_on_drv_probe",
2302 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2305 .name
= "reset_dev_on_drv_probe",
2307 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2311 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2313 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2314 int nla_type
, struct nlattr
*nl
)
2316 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2317 struct nlattr
*val_attr
;
2322 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2323 if (err
!= MNL_CB_OK
)
2326 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2327 (nla_type
!= MNL_TYPE_FLAG
&&
2328 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2331 pr_out_str(dl
, "cmode",
2332 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2333 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2335 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2341 err
= param_val_conv_str_get(param_val_conv
,
2344 mnl_attr_get_u8(val_attr
),
2348 pr_out_str(dl
, "value", vstr
);
2350 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2355 err
= param_val_conv_str_get(param_val_conv
,
2358 mnl_attr_get_u16(val_attr
),
2362 pr_out_str(dl
, "value", vstr
);
2364 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2369 err
= param_val_conv_str_get(param_val_conv
,
2372 mnl_attr_get_u32(val_attr
),
2376 pr_out_str(dl
, "value", vstr
);
2378 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2381 case MNL_TYPE_STRING
:
2382 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2385 pr_out_bool(dl
, "value", val_attr
? true : false);
2390 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2392 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2393 struct nlattr
*param_value_attr
;
2394 const char *nla_name
;
2398 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2399 if (err
!= MNL_CB_OK
)
2401 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2402 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2403 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2407 pr_out_handle_start_arr(dl
, tb
);
2409 __pr_out_handle_start(dl
, tb
, true, false);
2411 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2413 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2414 pr_out_str(dl
, "name", nla_name
);
2416 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2417 pr_out_str(dl
, "type", "driver-specific");
2419 pr_out_str(dl
, "type", "generic");
2421 pr_out_array_start(dl
, "values");
2422 mnl_attr_for_each_nested(param_value_attr
,
2423 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2424 pr_out_entry_start(dl
);
2425 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2426 pr_out_entry_end(dl
);
2428 pr_out_array_end(dl
);
2429 pr_out_handle_end(dl
);
2432 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2434 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2435 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2436 struct dl
*dl
= data
;
2438 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2439 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2440 !tb
[DEVLINK_ATTR_PARAM
])
2441 return MNL_CB_ERROR
;
2442 pr_out_param(dl
, tb
, true);
2458 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2460 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2461 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2462 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2463 struct nlattr
*param_value_attr
;
2464 enum devlink_param_cmode cmode
;
2465 struct param_ctx
*ctx
= data
;
2466 struct dl
*dl
= ctx
->dl
;
2470 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2471 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2472 !tb
[DEVLINK_ATTR_PARAM
])
2473 return MNL_CB_ERROR
;
2475 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2476 if (err
!= MNL_CB_OK
)
2477 return MNL_CB_ERROR
;
2479 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2480 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2481 return MNL_CB_ERROR
;
2483 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2484 mnl_attr_for_each_nested(param_value_attr
,
2485 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2486 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2487 struct nlattr
*val_attr
;
2489 err
= mnl_attr_parse_nested(param_value_attr
,
2490 attr_cb
, nla_value
);
2491 if (err
!= MNL_CB_OK
)
2492 return MNL_CB_ERROR
;
2494 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2495 (nla_type
!= MNL_TYPE_FLAG
&&
2496 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2497 return MNL_CB_ERROR
;
2499 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2500 if (cmode
== dl
->opts
.cmode
) {
2501 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2504 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2507 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2510 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2512 case MNL_TYPE_STRING
:
2513 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2516 ctx
->value
.vbool
= val_attr
? true : false;
2522 ctx
->nla_type
= nla_type
;
2526 static int cmd_dev_param_set(struct dl
*dl
)
2528 struct param_ctx ctx
= {};
2529 struct nlmsghdr
*nlh
;
2537 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2539 DL_OPT_PARAM_VALUE
|
2540 DL_OPT_PARAM_CMODE
, 0);
2544 /* Get value type */
2545 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2546 NLM_F_REQUEST
| NLM_F_ACK
);
2547 dl_opts_put(nlh
, dl
);
2550 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2554 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2555 NLM_F_REQUEST
| NLM_F_ACK
);
2556 dl_opts_put(nlh
, dl
);
2558 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2559 dl
->opts
.param_name
);
2561 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2562 switch (ctx
.nla_type
) {
2565 err
= param_val_conv_uint_get(param_val_conv
,
2567 dl
->opts
.param_name
,
2568 dl
->opts
.param_value
,
2572 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2575 goto err_param_value_parse
;
2576 if (val_u8
== ctx
.value
.vu8
)
2578 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2582 err
= param_val_conv_uint_get(param_val_conv
,
2584 dl
->opts
.param_name
,
2585 dl
->opts
.param_value
,
2589 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2592 goto err_param_value_parse
;
2593 if (val_u16
== ctx
.value
.vu16
)
2595 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2599 err
= param_val_conv_uint_get(param_val_conv
,
2601 dl
->opts
.param_name
,
2602 dl
->opts
.param_value
,
2605 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2607 goto err_param_value_parse
;
2608 if (val_u32
== ctx
.value
.vu32
)
2610 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2613 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2615 goto err_param_value_parse
;
2616 if (val_bool
== ctx
.value
.vbool
)
2619 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2622 case MNL_TYPE_STRING
:
2623 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2624 dl
->opts
.param_value
);
2625 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2629 printf("Value type not supported\n");
2632 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2634 err_param_value_parse
:
2635 pr_err("Value \"%s\" is not a number or not within range\n",
2636 dl
->opts
.param_value
);
2640 static int cmd_dev_param_show(struct dl
*dl
)
2642 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2643 struct nlmsghdr
*nlh
;
2646 if (dl_argc(dl
) == 0)
2647 flags
|= NLM_F_DUMP
;
2649 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2651 if (dl_argc(dl
) > 0) {
2652 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2653 DL_OPT_PARAM_NAME
, 0);
2658 pr_out_section_start(dl
, "param");
2659 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2660 pr_out_section_end(dl
);
2664 static int cmd_dev_param(struct dl
*dl
)
2666 if (dl_argv_match(dl
, "help")) {
2669 } else if (dl_argv_match(dl
, "show") ||
2670 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2672 return cmd_dev_param_show(dl
);
2673 } else if (dl_argv_match(dl
, "set")) {
2675 return cmd_dev_param_set(dl
);
2677 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2680 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2682 struct dl
*dl
= data
;
2683 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2684 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2686 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2687 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2688 return MNL_CB_ERROR
;
2693 static int cmd_dev_show(struct dl
*dl
)
2695 struct nlmsghdr
*nlh
;
2696 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2699 if (dl_argc(dl
) == 0)
2700 flags
|= NLM_F_DUMP
;
2702 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2704 if (dl_argc(dl
) > 0) {
2705 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2710 pr_out_section_start(dl
, "dev");
2711 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2712 pr_out_section_end(dl
);
2716 static void cmd_dev_reload_help(void)
2718 pr_err("Usage: devlink dev reload [ DEV ]\n");
2721 static int cmd_dev_reload(struct dl
*dl
)
2723 struct nlmsghdr
*nlh
;
2726 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2727 cmd_dev_reload_help();
2731 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2732 NLM_F_REQUEST
| NLM_F_ACK
);
2734 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2738 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2741 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2742 const char *name
, int type
)
2744 struct nlattr
*version
;
2746 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2747 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2748 const char *ver_value
;
2749 const char *ver_name
;
2752 if (mnl_attr_get_type(version
) != type
)
2755 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2756 if (err
!= MNL_CB_OK
)
2759 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2760 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2764 pr_out_object_start(dl
, name
);
2768 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2769 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2771 pr_out_str(dl
, ver_name
, ver_value
);
2772 if (!dl
->json_output
)
2777 pr_out_object_end(dl
);
2780 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2781 struct nlattr
**tb
, bool has_versions
)
2783 __pr_out_handle_start(dl
, tb
, true, false);
2785 __pr_out_indent_inc();
2786 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2787 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2789 if (!dl
->json_output
)
2791 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2794 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2795 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2797 if (!dl
->json_output
)
2799 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2801 __pr_out_indent_dec();
2804 pr_out_object_start(dl
, "versions");
2806 pr_out_versions_single(dl
, nlh
, "fixed",
2807 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2808 pr_out_versions_single(dl
, nlh
, "running",
2809 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2810 pr_out_versions_single(dl
, nlh
, "stored",
2811 DEVLINK_ATTR_INFO_VERSION_STORED
);
2813 pr_out_object_end(dl
);
2816 pr_out_handle_end(dl
);
2819 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2821 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2822 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2823 bool has_versions
, has_info
;
2824 struct dl
*dl
= data
;
2826 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2828 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2829 return MNL_CB_ERROR
;
2831 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2832 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2833 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2834 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2835 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2839 pr_out_info(dl
, nlh
, tb
, has_versions
);
2844 static void cmd_dev_info_help(void)
2846 pr_err("Usage: devlink dev info [ DEV ]\n");
2849 static int cmd_dev_info(struct dl
*dl
)
2851 struct nlmsghdr
*nlh
;
2852 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2855 if (dl_argv_match(dl
, "help")) {
2856 cmd_dev_info_help();
2860 if (dl_argc(dl
) == 0)
2861 flags
|= NLM_F_DUMP
;
2863 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2865 if (dl_argc(dl
) > 0) {
2866 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2871 pr_out_section_start(dl
, "info");
2872 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2873 pr_out_section_end(dl
);
2877 static void cmd_dev_flash_help(void)
2879 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2883 struct cmd_dev_flash_status_ctx
{
2886 char *last_component
;
2887 uint8_t not_first
:1,
2893 static int nullstrcmp(const char *str1
, const char *str2
)
2896 return strcmp(str1
, str2
);
2899 return str1
? 1 : -1;
2902 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2904 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2905 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2906 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2907 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2908 const char *component
= NULL
;
2909 uint64_t done
= 0, total
= 0;
2910 const char *msg
= NULL
;
2911 const char *bus_name
;
2912 const char *dev_name
;
2914 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2915 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2918 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2919 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2920 return MNL_CB_ERROR
;
2921 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2922 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2923 if (strcmp(bus_name
, opts
->bus_name
) ||
2924 strcmp(dev_name
, opts
->dev_name
))
2925 return MNL_CB_ERROR
;
2927 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2929 free(ctx
->last_msg
);
2930 free(ctx
->last_component
);
2931 ctx
->received_end
= 1;
2935 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2936 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2937 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2938 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2939 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2940 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2941 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2942 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
2944 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
2945 !nullstrcmp(component
, ctx
->last_component
) &&
2946 ctx
->last_pc
&& ctx
->not_first
) {
2947 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
2952 pr_out("[%s] ", component
);
2953 free(ctx
->last_component
);
2954 ctx
->last_component
= strdup(component
);
2958 free(ctx
->last_msg
);
2959 ctx
->last_msg
= strdup(msg
);
2963 pr_out_tty(" %3lu%%", (done
* 100) / total
);
2974 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
2975 struct mnlg_socket
*nlg_ntf
,
2978 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
2985 for (i
= 0; i
< 3; i
++)
2987 FD_SET(pipe_r
, &fds
[0]);
2989 FD_SET(nlfd
, &fds
[0]);
2993 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
2996 pr_err("select() failed\n");
2999 if (FD_ISSET(nlfd
, &fds
[0])) {
3000 err
= _mnlg_socket_recv_run(nlg_ntf
,
3001 cmd_dev_flash_status_cb
, ctx
);
3005 if (FD_ISSET(pipe_r
, &fds
[0])) {
3006 err
= read(pipe_r
, &err2
, sizeof(err2
));
3008 pr_err("Failed to read pipe\n");
3013 ctx
->flash_done
= 1;
3019 static int cmd_dev_flash(struct dl
*dl
)
3021 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3022 struct mnlg_socket
*nlg_ntf
;
3023 struct nlmsghdr
*nlh
;
3029 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3030 cmd_dev_flash_help();
3034 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3035 NLM_F_REQUEST
| NLM_F_ACK
);
3037 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3038 DL_OPT_FLASH_COMPONENT
);
3042 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3046 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3050 err
= pipe(pipe_fds
);
3053 pipe_r
= pipe_fds
[0];
3054 pipe_w
= pipe_fds
[1];
3062 /* In child, just execute the flash and pass returned
3063 * value through pipe once it is done.
3066 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3067 write(pipe_w
, &err
, sizeof(err
));
3074 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3077 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3079 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3082 mnlg_socket_close(nlg_ntf
);
3086 static int cmd_dev(struct dl
*dl
)
3088 if (dl_argv_match(dl
, "help")) {
3091 } else if (dl_argv_match(dl
, "show") ||
3092 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3094 return cmd_dev_show(dl
);
3095 } else if (dl_argv_match(dl
, "eswitch")) {
3097 return cmd_dev_eswitch(dl
);
3098 } else if (dl_argv_match(dl
, "reload")) {
3100 return cmd_dev_reload(dl
);
3101 } else if (dl_argv_match(dl
, "param")) {
3103 return cmd_dev_param(dl
);
3104 } else if (dl_argv_match(dl
, "info")) {
3106 return cmd_dev_info(dl
);
3107 } else if (dl_argv_match(dl
, "flash")) {
3109 return cmd_dev_flash(dl
);
3111 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3115 static void cmd_port_help(void)
3117 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3118 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3119 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3120 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3123 static const char *port_type_name(uint32_t type
)
3126 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3127 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3128 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3129 case DEVLINK_PORT_TYPE_IB
: return "ib";
3130 default: return "<unknown type>";
3134 static const char *port_flavour_name(uint16_t flavour
)
3137 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3139 case DEVLINK_PORT_FLAVOUR_CPU
:
3141 case DEVLINK_PORT_FLAVOUR_DSA
:
3143 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3145 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3148 return "<unknown flavour>";
3152 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3156 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3157 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3158 pr_out_uint(dl
, "pfnum", fn_num
);
3160 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3161 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3162 pr_out_uint(dl
, "vfnum", fn_num
);
3166 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3168 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3169 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3171 pr_out_port_handle_start(dl
, tb
, false);
3173 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3175 pr_out_str(dl
, "type", port_type_name(port_type
));
3177 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3179 if (port_type
!= des_port_type
)
3180 pr_out_str(dl
, "des_type",
3181 port_type_name(des_port_type
));
3184 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
3185 pr_out_str(dl
, "netdev",
3186 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3187 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
3188 pr_out_str(dl
, "ibdev",
3189 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3190 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3191 uint16_t port_flavour
=
3192 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3194 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
3196 switch (port_flavour
) {
3197 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3198 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3199 pr_out_port_pfvf_num(dl
, tb
);
3205 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3206 uint32_t port_number
;
3208 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3209 pr_out_uint(dl
, "port", port_number
);
3211 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3212 pr_out_uint(dl
, "split_group",
3213 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3214 pr_out_port_handle_end(dl
);
3217 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3219 struct dl
*dl
= data
;
3220 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3221 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3223 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3224 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3225 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3226 return MNL_CB_ERROR
;
3227 pr_out_port(dl
, tb
);
3231 static int cmd_port_show(struct dl
*dl
)
3233 struct nlmsghdr
*nlh
;
3234 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3237 if (dl_argc(dl
) == 0)
3238 flags
|= NLM_F_DUMP
;
3240 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3242 if (dl_argc(dl
) > 0) {
3243 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3248 pr_out_section_start(dl
, "port");
3249 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3250 pr_out_section_end(dl
);
3254 static int cmd_port_set(struct dl
*dl
)
3256 struct nlmsghdr
*nlh
;
3259 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3260 NLM_F_REQUEST
| NLM_F_ACK
);
3262 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3266 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3269 static int cmd_port_split(struct dl
*dl
)
3271 struct nlmsghdr
*nlh
;
3274 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3275 NLM_F_REQUEST
| NLM_F_ACK
);
3277 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3281 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3284 static int cmd_port_unsplit(struct dl
*dl
)
3286 struct nlmsghdr
*nlh
;
3289 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3290 NLM_F_REQUEST
| NLM_F_ACK
);
3292 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3296 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3299 static int cmd_port(struct dl
*dl
)
3301 if (dl_argv_match(dl
, "help")) {
3304 } else if (dl_argv_match(dl
, "show") ||
3305 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3307 return cmd_port_show(dl
);
3308 } else if (dl_argv_match(dl
, "set")) {
3310 return cmd_port_set(dl
);
3311 } else if (dl_argv_match(dl
, "split")) {
3313 return cmd_port_split(dl
);
3314 } else if (dl_argv_match(dl
, "unsplit")) {
3316 return cmd_port_unsplit(dl
);
3318 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3322 static void cmd_sb_help(void)
3324 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3325 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3326 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3327 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3328 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3329 pr_err(" pool POOL_INDEX ]\n");
3330 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3331 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3332 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3333 pr_err(" type { ingress | egress } ]\n");
3334 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3335 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3336 pr_err(" th THRESHOLD\n");
3337 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3338 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3339 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3342 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3344 pr_out_handle_start_arr(dl
, tb
);
3345 pr_out_uint(dl
, "sb",
3346 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3347 pr_out_uint(dl
, "size",
3348 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3349 pr_out_uint(dl
, "ing_pools",
3350 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3351 pr_out_uint(dl
, "eg_pools",
3352 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3353 pr_out_uint(dl
, "ing_tcs",
3354 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3355 pr_out_uint(dl
, "eg_tcs",
3356 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3357 pr_out_handle_end(dl
);
3360 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3362 struct dl
*dl
= data
;
3363 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3364 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3366 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3367 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3368 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3369 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3370 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3371 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3372 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3373 return MNL_CB_ERROR
;
3378 static int cmd_sb_show(struct dl
*dl
)
3380 struct nlmsghdr
*nlh
;
3381 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3384 if (dl_argc(dl
) == 0)
3385 flags
|= NLM_F_DUMP
;
3387 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3389 if (dl_argc(dl
) > 0) {
3390 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3395 pr_out_section_start(dl
, "sb");
3396 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3397 pr_out_section_end(dl
);
3401 static const char *pool_type_name(uint8_t type
)
3404 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3405 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3406 default: return "<unknown type>";
3410 static const char *threshold_type_name(uint8_t type
)
3413 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3414 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3415 default: return "<unknown type>";
3419 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3421 pr_out_handle_start_arr(dl
, tb
);
3422 pr_out_uint(dl
, "sb",
3423 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3424 pr_out_uint(dl
, "pool",
3425 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3426 pr_out_str(dl
, "type",
3427 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3428 pr_out_uint(dl
, "size",
3429 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3430 pr_out_str(dl
, "thtype",
3431 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3432 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3433 pr_out_uint(dl
, "cell_size",
3434 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3435 pr_out_handle_end(dl
);
3438 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3440 struct dl
*dl
= data
;
3441 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3442 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3444 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3445 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3446 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3447 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3448 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3449 return MNL_CB_ERROR
;
3450 pr_out_sb_pool(dl
, tb
);
3454 static int cmd_sb_pool_show(struct dl
*dl
)
3456 struct nlmsghdr
*nlh
;
3457 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3460 if (dl_argc(dl
) == 0)
3461 flags
|= NLM_F_DUMP
;
3463 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3465 if (dl_argc(dl
) > 0) {
3466 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3472 pr_out_section_start(dl
, "pool");
3473 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3474 pr_out_section_end(dl
);
3478 static int cmd_sb_pool_set(struct dl
*dl
)
3480 struct nlmsghdr
*nlh
;
3483 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3484 NLM_F_REQUEST
| NLM_F_ACK
);
3486 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3487 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3491 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3494 static int cmd_sb_pool(struct dl
*dl
)
3496 if (dl_argv_match(dl
, "help")) {
3499 } else if (dl_argv_match(dl
, "show") ||
3500 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3502 return cmd_sb_pool_show(dl
);
3503 } else if (dl_argv_match(dl
, "set")) {
3505 return cmd_sb_pool_set(dl
);
3507 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3511 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3513 pr_out_port_handle_start_arr(dl
, tb
, true);
3514 pr_out_uint(dl
, "sb",
3515 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3516 pr_out_uint(dl
, "pool",
3517 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3518 pr_out_uint(dl
, "threshold",
3519 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3520 pr_out_port_handle_end(dl
);
3523 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3525 struct dl
*dl
= data
;
3526 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3527 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3529 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3530 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3531 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3532 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3533 return MNL_CB_ERROR
;
3534 pr_out_sb_port_pool(dl
, tb
);
3538 static int cmd_sb_port_pool_show(struct dl
*dl
)
3540 struct nlmsghdr
*nlh
;
3541 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3544 if (dl_argc(dl
) == 0)
3545 flags
|= NLM_F_DUMP
;
3547 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3549 if (dl_argc(dl
) > 0) {
3550 err
= dl_argv_parse_put(nlh
, dl
,
3551 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3557 pr_out_section_start(dl
, "port_pool");
3558 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3559 pr_out_section_end(dl
);
3563 static int cmd_sb_port_pool_set(struct dl
*dl
)
3565 struct nlmsghdr
*nlh
;
3568 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3569 NLM_F_REQUEST
| NLM_F_ACK
);
3571 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3572 DL_OPT_SB_TH
, DL_OPT_SB
);
3576 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3579 static int cmd_sb_port_pool(struct dl
*dl
)
3581 if (dl_argv_match(dl
, "help")) {
3584 } else if (dl_argv_match(dl
, "show") ||
3585 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3587 return cmd_sb_port_pool_show(dl
);
3588 } else if (dl_argv_match(dl
, "set")) {
3590 return cmd_sb_port_pool_set(dl
);
3592 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3596 static int cmd_sb_port(struct dl
*dl
)
3598 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3601 } else if (dl_argv_match(dl
, "pool")) {
3603 return cmd_sb_port_pool(dl
);
3605 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3609 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3611 pr_out_port_handle_start_arr(dl
, tb
, true);
3612 pr_out_uint(dl
, "sb",
3613 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3614 pr_out_uint(dl
, "tc",
3615 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3616 pr_out_str(dl
, "type",
3617 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3618 pr_out_uint(dl
, "pool",
3619 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3620 pr_out_uint(dl
, "threshold",
3621 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3622 pr_out_port_handle_end(dl
);
3625 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3627 struct dl
*dl
= data
;
3628 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3629 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3631 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3632 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3633 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3634 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3635 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3636 return MNL_CB_ERROR
;
3637 pr_out_sb_tc_bind(dl
, tb
);
3641 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3643 struct nlmsghdr
*nlh
;
3644 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3647 if (dl_argc(dl
) == 0)
3648 flags
|= NLM_F_DUMP
;
3650 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3652 if (dl_argc(dl
) > 0) {
3653 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3654 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3659 pr_out_section_start(dl
, "tc_bind");
3660 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3661 pr_out_section_end(dl
);
3665 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3667 struct nlmsghdr
*nlh
;
3670 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3671 NLM_F_REQUEST
| NLM_F_ACK
);
3673 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3674 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3679 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3682 static int cmd_sb_tc_bind(struct dl
*dl
)
3684 if (dl_argv_match(dl
, "help")) {
3687 } else if (dl_argv_match(dl
, "show") ||
3688 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3690 return cmd_sb_tc_bind_show(dl
);
3691 } else if (dl_argv_match(dl
, "set")) {
3693 return cmd_sb_tc_bind_set(dl
);
3695 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3699 static int cmd_sb_tc(struct dl
*dl
)
3701 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3704 } else if (dl_argv_match(dl
, "bind")) {
3706 return cmd_sb_tc_bind(dl
);
3708 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3713 struct list_head list
;
3717 uint32_t bound_pool_index
;
3721 struct list_head list
;
3724 uint32_t port_index
;
3726 struct list_head pool_list
;
3727 struct list_head ing_tc_list
;
3728 struct list_head eg_tc_list
;
3734 struct list_head port_list
;
3737 static struct occ_item
*occ_item_alloc(void)
3739 return calloc(1, sizeof(struct occ_item
));
3742 static void occ_item_free(struct occ_item
*occ_item
)
3747 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3749 struct occ_port
*occ_port
;
3751 occ_port
= calloc(1, sizeof(*occ_port
));
3754 occ_port
->port_index
= port_index
;
3755 INIT_LIST_HEAD(&occ_port
->pool_list
);
3756 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3757 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3761 static void occ_port_free(struct occ_port
*occ_port
)
3763 struct occ_item
*occ_item
, *tmp
;
3765 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3766 occ_item_free(occ_item
);
3767 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3768 occ_item_free(occ_item
);
3769 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3770 occ_item_free(occ_item
);
3773 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3775 struct occ_show
*occ_show
;
3777 occ_show
= calloc(1, sizeof(*occ_show
));
3781 INIT_LIST_HEAD(&occ_show
->port_list
);
3785 static void occ_show_free(struct occ_show
*occ_show
)
3787 struct occ_port
*occ_port
, *tmp
;
3789 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3790 occ_port_free(occ_port
);
3793 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3796 struct occ_port
*occ_port
;
3797 uint32_t port_index
;
3799 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3801 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3802 if (occ_port
->port_index
== port_index
)
3805 occ_port
= occ_port_alloc(port_index
);
3808 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3812 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3815 struct occ_item
*occ_item
;
3818 pr_out_sp(7, " %s:", label
);
3819 list_for_each_entry(occ_item
, list
, list
) {
3820 if ((i
- 1) % 4 == 0 && i
!= 1)
3823 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3824 occ_item
->bound_pool_index
);
3826 pr_out_sp(7, "%2u:", occ_item
->index
);
3827 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3831 if ((i
- 1) % 4 != 0)
3835 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3836 struct list_head
*list
,
3839 struct occ_item
*occ_item
;
3842 jsonw_name(dl
->jw
, label
);
3843 jsonw_start_object(dl
->jw
);
3844 list_for_each_entry(occ_item
, list
, list
) {
3845 sprintf(buf
, "%u", occ_item
->index
);
3846 jsonw_name(dl
->jw
, buf
);
3847 jsonw_start_object(dl
->jw
);
3849 jsonw_uint_field(dl
->jw
, "bound_pool",
3850 occ_item
->bound_pool_index
);
3851 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3852 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3853 jsonw_end_object(dl
->jw
);
3855 jsonw_end_object(dl
->jw
);
3858 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3860 if (dl
->json_output
) {
3861 pr_out_json_occ_show_item_list(dl
, "pool",
3862 &occ_port
->pool_list
, false);
3863 pr_out_json_occ_show_item_list(dl
, "itc",
3864 &occ_port
->ing_tc_list
, true);
3865 pr_out_json_occ_show_item_list(dl
, "etc",
3866 &occ_port
->eg_tc_list
, true);
3869 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3870 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3871 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3875 static void pr_out_occ_show(struct occ_show
*occ_show
)
3877 struct dl
*dl
= occ_show
->dl
;
3878 struct dl_opts
*opts
= &dl
->opts
;
3879 struct occ_port
*occ_port
;
3881 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3882 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3883 occ_port
->port_index
, true, false);
3884 pr_out_occ_show_port(dl
, occ_port
);
3885 pr_out_port_handle_end(dl
);
3889 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3892 struct occ_port
*occ_port
;
3893 struct occ_item
*occ_item
;
3895 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3898 occ_port
= occ_port_get(occ_show
, tb
);
3900 occ_show
->err
= -ENOMEM
;
3904 occ_item
= occ_item_alloc();
3906 occ_show
->err
= -ENOMEM
;
3909 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3910 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3911 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3912 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3915 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3917 struct occ_show
*occ_show
= data
;
3918 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3919 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3921 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3922 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3923 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3924 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3925 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3926 return MNL_CB_ERROR
;
3927 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3931 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3934 struct occ_port
*occ_port
;
3935 struct occ_item
*occ_item
;
3938 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3941 occ_port
= occ_port_get(occ_show
, tb
);
3943 occ_show
->err
= -ENOMEM
;
3947 occ_item
= occ_item_alloc();
3949 occ_show
->err
= -ENOMEM
;
3952 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3953 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3954 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3955 occ_item
->bound_pool_index
=
3956 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3957 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3958 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3959 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3960 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3961 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3963 occ_item_free(occ_item
);
3966 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3968 struct occ_show
*occ_show
= data
;
3969 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3970 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3972 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3973 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3974 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3975 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3976 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3977 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3978 return MNL_CB_ERROR
;
3979 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3983 static int cmd_sb_occ_show(struct dl
*dl
)
3985 struct nlmsghdr
*nlh
;
3986 struct occ_show
*occ_show
;
3987 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3990 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3994 occ_show
= occ_show_alloc(dl
);
3998 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4000 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4001 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4005 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4007 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4008 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4012 pr_out_section_start(dl
, "occupancy");
4013 pr_out_occ_show(occ_show
);
4014 pr_out_section_end(dl
);
4017 occ_show_free(occ_show
);
4021 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4023 struct nlmsghdr
*nlh
;
4026 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4027 NLM_F_REQUEST
| NLM_F_ACK
);
4029 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4033 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4036 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4038 struct nlmsghdr
*nlh
;
4041 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4042 NLM_F_REQUEST
| NLM_F_ACK
);
4044 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4048 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4051 static int cmd_sb_occ(struct dl
*dl
)
4053 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4056 } else if (dl_argv_match(dl
, "show") ||
4057 dl_argv_match(dl
, "list")) {
4059 return cmd_sb_occ_show(dl
);
4060 } else if (dl_argv_match(dl
, "snapshot")) {
4062 return cmd_sb_occ_snapshot(dl
);
4063 } else if (dl_argv_match(dl
, "clearmax")) {
4065 return cmd_sb_occ_clearmax(dl
);
4067 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4071 static int cmd_sb(struct dl
*dl
)
4073 if (dl_argv_match(dl
, "help")) {
4076 } else if (dl_argv_match(dl
, "show") ||
4077 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4079 return cmd_sb_show(dl
);
4080 } else if (dl_argv_match(dl
, "pool")) {
4082 return cmd_sb_pool(dl
);
4083 } else if (dl_argv_match(dl
, "port")) {
4085 return cmd_sb_port(dl
);
4086 } else if (dl_argv_match(dl
, "tc")) {
4088 return cmd_sb_tc(dl
);
4089 } else if (dl_argv_match(dl
, "occupancy")) {
4091 return cmd_sb_occ(dl
);
4093 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4097 static const char *cmd_name(uint8_t cmd
)
4100 case DEVLINK_CMD_UNSPEC
: return "unspec";
4101 case DEVLINK_CMD_GET
: return "get";
4102 case DEVLINK_CMD_SET
: return "set";
4103 case DEVLINK_CMD_NEW
: return "new";
4104 case DEVLINK_CMD_DEL
: return "del";
4105 case DEVLINK_CMD_PORT_GET
: return "get";
4106 case DEVLINK_CMD_PORT_SET
: return "set";
4107 case DEVLINK_CMD_PORT_NEW
: return "new";
4108 case DEVLINK_CMD_PORT_DEL
: return "del";
4109 case DEVLINK_CMD_PARAM_GET
: return "get";
4110 case DEVLINK_CMD_PARAM_SET
: return "set";
4111 case DEVLINK_CMD_PARAM_NEW
: return "new";
4112 case DEVLINK_CMD_PARAM_DEL
: return "del";
4113 case DEVLINK_CMD_REGION_GET
: return "get";
4114 case DEVLINK_CMD_REGION_SET
: return "set";
4115 case DEVLINK_CMD_REGION_NEW
: return "new";
4116 case DEVLINK_CMD_REGION_DEL
: return "del";
4117 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4118 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4119 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4120 case DEVLINK_CMD_TRAP_GET
: return "get";
4121 case DEVLINK_CMD_TRAP_SET
: return "set";
4122 case DEVLINK_CMD_TRAP_NEW
: return "new";
4123 case DEVLINK_CMD_TRAP_DEL
: return "del";
4124 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4125 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4126 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4127 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4128 default: return "<unknown cmd>";
4132 static const char *cmd_obj(uint8_t cmd
)
4135 case DEVLINK_CMD_UNSPEC
: return "unspec";
4136 case DEVLINK_CMD_GET
:
4137 case DEVLINK_CMD_SET
:
4138 case DEVLINK_CMD_NEW
:
4139 case DEVLINK_CMD_DEL
:
4141 case DEVLINK_CMD_PORT_GET
:
4142 case DEVLINK_CMD_PORT_SET
:
4143 case DEVLINK_CMD_PORT_NEW
:
4144 case DEVLINK_CMD_PORT_DEL
:
4146 case DEVLINK_CMD_PARAM_GET
:
4147 case DEVLINK_CMD_PARAM_SET
:
4148 case DEVLINK_CMD_PARAM_NEW
:
4149 case DEVLINK_CMD_PARAM_DEL
:
4151 case DEVLINK_CMD_REGION_GET
:
4152 case DEVLINK_CMD_REGION_SET
:
4153 case DEVLINK_CMD_REGION_NEW
:
4154 case DEVLINK_CMD_REGION_DEL
:
4156 case DEVLINK_CMD_FLASH_UPDATE
:
4157 case DEVLINK_CMD_FLASH_UPDATE_END
:
4158 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4160 case DEVLINK_CMD_TRAP_GET
:
4161 case DEVLINK_CMD_TRAP_SET
:
4162 case DEVLINK_CMD_TRAP_NEW
:
4163 case DEVLINK_CMD_TRAP_DEL
:
4165 case DEVLINK_CMD_TRAP_GROUP_GET
:
4166 case DEVLINK_CMD_TRAP_GROUP_SET
:
4167 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4168 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4169 return "trap-group";
4170 default: return "<unknown obj>";
4174 static void pr_out_mon_header(uint8_t cmd
)
4176 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4179 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4181 const char *obj
= cmd_obj(cmd
);
4182 unsigned int index
= 0;
4183 const char *cur_obj
;
4187 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4188 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4194 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4196 __pr_out_handle_start(dl
, tb
, true, false);
4198 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
4199 pr_out_str(dl
, "msg",
4200 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4202 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
4203 pr_out_str(dl
, "component",
4204 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4206 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4207 pr_out_u64(dl
, "done",
4208 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4210 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4211 pr_out_u64(dl
, "total",
4212 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4214 pr_out_handle_end(dl
);
4217 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4218 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4219 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4221 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4223 struct dl
*dl
= data
;
4224 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4225 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4226 uint8_t cmd
= genl
->cmd
;
4228 if (!cmd_filter_check(dl
, cmd
))
4232 case DEVLINK_CMD_GET
: /* fall through */
4233 case DEVLINK_CMD_SET
: /* fall through */
4234 case DEVLINK_CMD_NEW
: /* fall through */
4235 case DEVLINK_CMD_DEL
:
4236 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4237 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4238 return MNL_CB_ERROR
;
4239 pr_out_mon_header(genl
->cmd
);
4242 case DEVLINK_CMD_PORT_GET
: /* fall through */
4243 case DEVLINK_CMD_PORT_SET
: /* fall through */
4244 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4245 case DEVLINK_CMD_PORT_DEL
:
4246 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4247 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4248 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4249 return MNL_CB_ERROR
;
4250 pr_out_mon_header(genl
->cmd
);
4251 pr_out_port(dl
, tb
);
4253 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4254 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4255 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4256 case DEVLINK_CMD_PARAM_DEL
:
4257 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4258 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4259 !tb
[DEVLINK_ATTR_PARAM
])
4260 return MNL_CB_ERROR
;
4261 pr_out_mon_header(genl
->cmd
);
4262 pr_out_param(dl
, tb
, false);
4264 case DEVLINK_CMD_REGION_GET
: /* fall through */
4265 case DEVLINK_CMD_REGION_SET
: /* fall through */
4266 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4267 case DEVLINK_CMD_REGION_DEL
:
4268 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4269 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4270 !tb
[DEVLINK_ATTR_REGION_NAME
])
4271 return MNL_CB_ERROR
;
4272 pr_out_mon_header(genl
->cmd
);
4273 pr_out_region(dl
, tb
);
4275 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4276 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4277 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4278 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4279 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4280 return MNL_CB_ERROR
;
4281 pr_out_mon_header(genl
->cmd
);
4282 pr_out_flash_update(dl
, tb
);
4284 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4285 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4286 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4287 case DEVLINK_CMD_TRAP_DEL
:
4288 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4289 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4290 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4291 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4292 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4293 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4294 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4295 !tb
[DEVLINK_ATTR_STATS
])
4296 return MNL_CB_ERROR
;
4297 pr_out_mon_header(genl
->cmd
);
4298 pr_out_trap(dl
, tb
, false);
4300 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4301 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4302 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4303 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4304 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4305 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4306 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4307 !tb
[DEVLINK_ATTR_STATS
])
4308 return MNL_CB_ERROR
;
4309 pr_out_mon_header(genl
->cmd
);
4310 pr_out_trap_group(dl
, tb
, false);
4316 static int cmd_mon_show(struct dl
*dl
)
4319 unsigned int index
= 0;
4320 const char *cur_obj
;
4322 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4323 if (strcmp(cur_obj
, "all") != 0 &&
4324 strcmp(cur_obj
, "dev") != 0 &&
4325 strcmp(cur_obj
, "port") != 0 &&
4326 strcmp(cur_obj
, "trap") != 0 &&
4327 strcmp(cur_obj
, "trap-group") != 0) {
4328 pr_err("Unknown object \"%s\"\n", cur_obj
);
4332 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4335 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4341 static void cmd_mon_help(void)
4343 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4344 "where OBJECT-LIST := { dev | port | trap | trap-group }\n");
4347 static int cmd_mon(struct dl
*dl
)
4349 if (dl_argv_match(dl
, "help")) {
4353 return cmd_mon_show(dl
);
4356 struct dpipe_field
{
4359 unsigned int bitwidth
;
4360 enum devlink_dpipe_field_mapping_type mapping_type
;
4363 struct dpipe_header
{
4364 struct list_head list
;
4367 struct dpipe_field
*fields
;
4368 unsigned int fields_count
;
4371 struct dpipe_table
{
4372 struct list_head list
;
4374 unsigned int resource_id
;
4375 bool resource_valid
;
4378 struct dpipe_tables
{
4379 struct list_head table_list
;
4389 enum devlink_resource_unit unit
;
4394 struct list_head list
;
4395 struct list_head resource_list
;
4396 struct resource
*parent
;
4400 struct list_head resource_list
;
4403 struct resource_ctx
{
4406 struct resources
*resources
;
4407 struct dpipe_tables
*tables
;
4408 bool print_resources
;
4409 bool pending_change
;
4412 static struct resource
*resource_alloc(void)
4414 struct resource
*resource
;
4416 resource
= calloc(1, sizeof(struct resource
));
4419 INIT_LIST_HEAD(&resource
->resource_list
);
4423 static void resource_free(struct resource
*resource
)
4425 struct resource
*child_resource
, *tmp
;
4427 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4429 free(child_resource
->name
);
4430 resource_free(child_resource
);
4435 static struct resources
*resources_alloc(void)
4437 struct resources
*resources
;
4439 resources
= calloc(1, sizeof(struct resources
));
4442 INIT_LIST_HEAD(&resources
->resource_list
);
4446 static void resources_free(struct resources
*resources
)
4448 struct resource
*resource
, *tmp
;
4450 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4451 resource_free(resource
);
4454 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4456 ctx
->resources
= resources_alloc();
4457 if (!ctx
->resources
)
4463 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4465 resources_free(ctx
->resources
);
4471 struct list_head global_headers
;
4472 struct list_head local_headers
;
4473 struct dpipe_tables
*tables
;
4474 struct resources
*resources
;
4479 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4481 struct dpipe_header
*header
;
4483 header
= calloc(1, sizeof(struct dpipe_header
));
4486 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4487 if (!header
->fields
)
4488 goto err_fields_alloc
;
4489 header
->fields_count
= fields_count
;
4497 static void dpipe_header_free(struct dpipe_header
*header
)
4499 free(header
->fields
);
4503 static void dpipe_header_clear(struct dpipe_header
*header
)
4505 struct dpipe_field
*field
;
4508 for (i
= 0; i
< header
->fields_count
; i
++) {
4509 field
= &header
->fields
[i
];
4515 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4516 struct dpipe_header
*header
, bool global
)
4519 list_add(&header
->list
, &ctx
->global_headers
);
4521 list_add(&header
->list
, &ctx
->local_headers
);
4524 static void dpipe_header_del(struct dpipe_header
*header
)
4526 list_del(&header
->list
);
4529 static struct dpipe_table
*dpipe_table_alloc(void)
4531 return calloc(1, sizeof(struct dpipe_table
));
4534 static void dpipe_table_free(struct dpipe_table
*table
)
4539 static struct dpipe_tables
*dpipe_tables_alloc(void)
4541 struct dpipe_tables
*tables
;
4543 tables
= calloc(1, sizeof(struct dpipe_tables
));
4546 INIT_LIST_HEAD(&tables
->table_list
);
4550 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4552 struct dpipe_table
*table
, *tmp
;
4554 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4555 dpipe_table_free(table
);
4559 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4561 ctx
->tables
= dpipe_tables_alloc();
4566 INIT_LIST_HEAD(&ctx
->global_headers
);
4567 INIT_LIST_HEAD(&ctx
->local_headers
);
4571 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4573 struct dpipe_header
*header
, *tmp
;
4575 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4577 dpipe_header_del(header
);
4578 dpipe_header_clear(header
);
4579 dpipe_header_free(header
);
4581 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4583 dpipe_header_del(header
);
4584 dpipe_header_clear(header
);
4585 dpipe_header_free(header
);
4587 dpipe_tables_free(ctx
->tables
);
4590 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4591 uint32_t header_id
, bool global
)
4593 struct list_head
*header_list
;
4594 struct dpipe_header
*header
;
4597 header_list
= &ctx
->global_headers
;
4599 header_list
= &ctx
->local_headers
;
4600 list_for_each_entry(header
, header_list
, list
) {
4601 if (header
->id
!= header_id
)
4603 return header
->name
;
4608 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4610 uint32_t field_id
, bool global
)
4612 struct list_head
*header_list
;
4613 struct dpipe_header
*header
;
4616 header_list
= &ctx
->global_headers
;
4618 header_list
= &ctx
->local_headers
;
4619 list_for_each_entry(header
, header_list
, list
) {
4620 if (header
->id
!= header_id
)
4622 return header
->fields
[field_id
].name
;
4628 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4630 switch (mapping_type
) {
4631 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4633 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4641 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4642 uint32_t field_id
, bool global
)
4644 enum devlink_dpipe_field_mapping_type mapping_type
;
4645 struct list_head
*header_list
;
4646 struct dpipe_header
*header
;
4649 header_list
= &ctx
->global_headers
;
4651 header_list
= &ctx
->local_headers
;
4652 list_for_each_entry(header
, header_list
, list
) {
4653 if (header
->id
!= header_id
)
4655 mapping_type
= header
->fields
[field_id
].mapping_type
;
4656 return dpipe_field_mapping_e2s(mapping_type
);
4661 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4662 struct dpipe_field
*fields
,
4663 unsigned int field_count
)
4665 struct dpipe_field
*field
;
4668 for (i
= 0; i
< field_count
; i
++) {
4670 pr_out_entry_start(ctx
->dl
);
4671 pr_out_str(ctx
->dl
, "name", field
->name
);
4672 if (ctx
->dl
->verbose
)
4673 pr_out_uint(ctx
->dl
, "id", field
->id
);
4674 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4675 if (field
->mapping_type
)
4676 pr_out_str(ctx
->dl
, "mapping_type",
4677 dpipe_field_mapping_e2s(field
->mapping_type
));
4678 pr_out_entry_end(ctx
->dl
);
4683 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4684 struct dpipe_header
*header
, bool global
)
4686 pr_out_handle_start_arr(ctx
->dl
, tb
);
4687 pr_out_str(ctx
->dl
, "name", header
->name
);
4688 if (ctx
->dl
->verbose
) {
4689 pr_out_uint(ctx
->dl
, "id", header
->id
);
4690 pr_out_str(ctx
->dl
, "global",
4691 global
? "true" : "false");
4693 pr_out_array_start(ctx
->dl
, "field");
4694 pr_out_dpipe_fields(ctx
, header
->fields
,
4695 header
->fields_count
);
4696 pr_out_array_end(ctx
->dl
);
4697 pr_out_handle_end(ctx
->dl
);
4700 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4703 struct dpipe_header
*header
;
4705 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4706 pr_out_dpipe_header(ctx
, tb
, header
, false);
4708 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4709 pr_out_dpipe_header(ctx
, tb
, header
, true);
4712 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4714 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4718 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4719 if (err
!= MNL_CB_OK
)
4721 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4722 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4723 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4724 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4727 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4728 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4729 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4730 field
->name
= strdup(name
);
4733 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4737 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4738 struct dpipe_field
*fields
)
4740 struct nlattr
*nla_field
;
4744 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4745 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4753 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4755 struct nlattr
*nla_field
;
4756 unsigned int count
= 0;
4758 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4763 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4765 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4766 struct dpipe_header
*header
;
4767 unsigned int fields_count
;
4768 const char *header_name
;
4772 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4773 if (err
!= MNL_CB_OK
)
4776 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4777 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4778 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4781 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4782 header
= dpipe_header_alloc(fields_count
);
4786 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4787 header
->name
= strdup(header_name
);
4788 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4789 header
->fields_count
= fields_count
;
4790 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4792 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4796 dpipe_header_add(ctx
, header
, global
);
4800 dpipe_header_free(header
);
4804 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4806 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4807 struct nlattr
*nla_header
;
4810 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4811 err
= dpipe_header_get(ctx
, nla_header
);
4818 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4820 struct dpipe_ctx
*ctx
= data
;
4821 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4822 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4825 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4826 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4827 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4828 return MNL_CB_ERROR
;
4829 err
= dpipe_headers_get(ctx
, tb
);
4832 return MNL_CB_ERROR
;
4835 if (ctx
->print_headers
)
4836 pr_out_dpipe_headers(ctx
, tb
);
4840 static int cmd_dpipe_headers_show(struct dl
*dl
)
4842 struct nlmsghdr
*nlh
;
4843 struct dpipe_ctx ctx
= {};
4844 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4847 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4849 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4853 err
= dpipe_ctx_init(&ctx
, dl
);
4857 ctx
.print_headers
= true;
4859 pr_out_section_start(dl
, "header");
4860 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4862 pr_err("error get headers %s\n", strerror(ctx
.err
));
4863 pr_out_section_end(dl
);
4865 dpipe_ctx_fini(&ctx
);
4869 static void cmd_dpipe_header_help(void)
4871 pr_err("Usage: devlink dpipe headers show DEV\n");
4874 static int cmd_dpipe_header(struct dl
*dl
)
4876 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4877 cmd_dpipe_header_help();
4879 } else if (dl_argv_match(dl
, "show")) {
4881 return cmd_dpipe_headers_show(dl
);
4883 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4888 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4890 switch (action_type
) {
4891 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4892 return "field_modify";
4898 struct dpipe_op_info
{
4904 struct dpipe_action
{
4905 struct dpipe_op_info info
;
4909 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4910 struct dpipe_ctx
*ctx
)
4912 struct dpipe_op_info
*op_info
= &action
->info
;
4913 const char *mapping
;
4915 pr_out_str(ctx
->dl
, "type",
4916 dpipe_action_type_e2s(action
->type
));
4917 pr_out_str(ctx
->dl
, "header",
4918 dpipe_header_id2s(ctx
, op_info
->header_id
,
4919 op_info
->header_global
));
4920 pr_out_str(ctx
->dl
, "field",
4921 dpipe_field_id2s(ctx
, op_info
->header_id
,
4923 op_info
->header_global
));
4924 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4926 op_info
->header_global
);
4928 pr_out_str(ctx
->dl
, "mapping", mapping
);
4931 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4933 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4936 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4937 if (err
!= MNL_CB_OK
)
4940 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4941 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4942 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4943 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4947 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4948 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4949 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4950 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4955 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4956 struct nlattr
*nla_actions
)
4958 struct nlattr
*nla_action
;
4959 struct dpipe_action action
;
4961 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4962 pr_out_entry_start(ctx
->dl
);
4963 if (dpipe_action_parse(&action
, nla_action
))
4964 goto err_action_parse
;
4965 pr_out_dpipe_action(&action
, ctx
);
4966 pr_out_entry_end(ctx
->dl
);
4971 pr_out_entry_end(ctx
->dl
);
4976 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4978 switch (match_type
) {
4979 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4980 return "field_exact";
4986 struct dpipe_match
{
4987 struct dpipe_op_info info
;
4991 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4992 struct dpipe_ctx
*ctx
)
4994 struct dpipe_op_info
*op_info
= &match
->info
;
4995 const char *mapping
;
4997 pr_out_str(ctx
->dl
, "type",
4998 dpipe_match_type_e2s(match
->type
));
4999 pr_out_str(ctx
->dl
, "header",
5000 dpipe_header_id2s(ctx
, op_info
->header_id
,
5001 op_info
->header_global
));
5002 pr_out_str(ctx
->dl
, "field",
5003 dpipe_field_id2s(ctx
, op_info
->header_id
,
5005 op_info
->header_global
));
5006 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5008 op_info
->header_global
);
5010 pr_out_str(ctx
->dl
, "mapping", mapping
);
5013 static int dpipe_match_parse(struct dpipe_match
*match
,
5017 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5020 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5021 if (err
!= MNL_CB_OK
)
5024 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5025 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5026 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5027 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5031 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5032 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5033 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5034 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5039 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5040 struct nlattr
*nla_matches
)
5042 struct nlattr
*nla_match
;
5043 struct dpipe_match match
;
5045 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5046 pr_out_entry_start(ctx
->dl
);
5047 if (dpipe_match_parse(&match
, nla_match
))
5048 goto err_match_parse
;
5049 pr_out_dpipe_match(&match
, ctx
);
5050 pr_out_entry_end(ctx
->dl
);
5055 pr_out_entry_end(ctx
->dl
);
5059 static struct resource
*
5060 resource_find(struct resources
*resources
, struct resource
*resource
,
5061 uint64_t resource_id
)
5063 struct list_head
*list_head
;
5066 list_head
= &resources
->resource_list
;
5068 list_head
= &resource
->resource_list
;
5070 list_for_each_entry(resource
, list_head
, list
) {
5071 struct resource
*child_resource
;
5073 if (resource
->id
== resource_id
)
5076 child_resource
= resource_find(resources
, resource
,
5079 return child_resource
;
5085 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5086 uint64_t resource_id
)
5088 struct resource
*resource
, *parent_resource
;
5089 const char del
[] = "/";
5093 resource
= resource_find(resources
, NULL
, resource_id
);
5097 for (parent_resource
= resource
; parent_resource
;
5098 parent_resource
= parent_resource
->parent
)
5099 path_len
+= strlen(parent_resource
->name
) + 1;
5102 path
= calloc(1, path_len
);
5106 path
+= path_len
- 1;
5107 for (parent_resource
= resource
; parent_resource
;
5108 parent_resource
= parent_resource
->parent
) {
5109 path
-= strlen(parent_resource
->name
);
5110 memcpy(path
, parent_resource
->name
,
5111 strlen(parent_resource
->name
));
5112 path
-= strlen(del
);
5113 memcpy(path
, del
, strlen(del
));
5115 pr_out_str(dl
, "resource_path", path
);
5119 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5121 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5122 struct dpipe_table
*table
;
5123 uint32_t resource_units
;
5124 bool counters_enabled
;
5125 bool resource_valid
;
5129 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5130 if (err
!= MNL_CB_OK
)
5133 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5134 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5135 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5136 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5137 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5141 table
= dpipe_table_alloc();
5145 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5146 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5147 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5149 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5151 if (resource_valid
) {
5152 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5153 table
->resource_valid
= true;
5156 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5157 if (!ctx
->print_tables
)
5160 pr_out_str(ctx
->dl
, "name", table
->name
);
5161 pr_out_uint(ctx
->dl
, "size", size
);
5162 pr_out_str(ctx
->dl
, "counters_enabled",
5163 counters_enabled
? "true" : "false");
5165 if (resource_valid
) {
5166 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5167 resource_path_print(ctx
->dl
, ctx
->resources
,
5168 table
->resource_id
);
5169 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
5172 pr_out_array_start(ctx
->dl
, "match");
5173 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5174 goto err_matches_show
;
5175 pr_out_array_end(ctx
->dl
);
5177 pr_out_array_start(ctx
->dl
, "action");
5178 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5179 goto err_actions_show
;
5180 pr_out_array_end(ctx
->dl
);
5186 pr_out_array_end(ctx
->dl
);
5190 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5192 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5193 struct nlattr
*nla_table
;
5195 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5196 if (ctx
->print_tables
)
5197 pr_out_handle_start_arr(ctx
->dl
, tb
);
5198 if (dpipe_table_show(ctx
, nla_table
))
5199 goto err_table_show
;
5200 if (ctx
->print_tables
)
5201 pr_out_handle_end(ctx
->dl
);
5206 if (ctx
->print_tables
)
5207 pr_out_handle_end(ctx
->dl
);
5211 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5213 struct dpipe_ctx
*ctx
= data
;
5214 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5215 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5217 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5218 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5219 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5220 return MNL_CB_ERROR
;
5222 if (dpipe_tables_show(ctx
, tb
))
5223 return MNL_CB_ERROR
;
5227 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5229 static int cmd_dpipe_table_show(struct dl
*dl
)
5231 struct nlmsghdr
*nlh
;
5232 struct dpipe_ctx dpipe_ctx
= {};
5233 struct resource_ctx resource_ctx
= {};
5234 uint16_t flags
= NLM_F_REQUEST
;
5237 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5241 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5243 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5247 dpipe_ctx
.print_tables
= true;
5249 dl_opts_put(nlh
, dl
);
5250 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5253 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5254 goto err_headers_get
;
5257 err
= resource_ctx_init(&resource_ctx
, dl
);
5259 goto err_resource_ctx_init
;
5261 resource_ctx
.print_resources
= false;
5262 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5263 dl_opts_put(nlh
, dl
);
5264 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5267 dpipe_ctx
.resources
= resource_ctx
.resources
;
5269 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5270 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5271 dl_opts_put(nlh
, dl
);
5273 pr_out_section_start(dl
, "table");
5274 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5275 pr_out_section_end(dl
);
5277 resource_ctx_fini(&resource_ctx
);
5278 dpipe_ctx_fini(&dpipe_ctx
);
5281 err_resource_ctx_init
:
5283 dpipe_ctx_fini(&dpipe_ctx
);
5287 static int cmd_dpipe_table_set(struct dl
*dl
)
5289 struct nlmsghdr
*nlh
;
5292 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5293 NLM_F_REQUEST
| NLM_F_ACK
);
5295 err
= dl_argv_parse_put(nlh
, dl
,
5296 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5297 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5301 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5304 enum dpipe_value_type
{
5305 DPIPE_VALUE_TYPE_VALUE
,
5306 DPIPE_VALUE_TYPE_MASK
,
5310 dpipe_value_type_e2s(enum dpipe_value_type type
)
5313 case DPIPE_VALUE_TYPE_VALUE
:
5315 case DPIPE_VALUE_TYPE_MASK
:
5316 return "value_mask";
5322 struct dpipe_field_printer
{
5323 unsigned int field_id
;
5324 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5327 struct dpipe_header_printer
{
5328 struct dpipe_field_printer
*printers
;
5329 unsigned int printers_count
;
5330 unsigned int header_id
;
5333 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5334 enum dpipe_value_type type
,
5337 struct in_addr ip_addr
;
5339 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5340 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5344 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5345 enum dpipe_value_type type
,
5348 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
5349 ether_ntoa((struct ether_addr
*)value
));
5352 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5353 enum dpipe_value_type type
,
5356 char str
[INET6_ADDRSTRLEN
];
5358 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5359 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
5362 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5364 .printer
= dpipe_field_printer_ipv4_addr
,
5365 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5369 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5370 .printers
= dpipe_field_printers_ipv4
,
5371 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5372 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5375 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5377 .printer
= dpipe_field_printer_ethernet_addr
,
5378 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5382 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5383 .printers
= dpipe_field_printers_ethernet
,
5384 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5385 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5388 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5390 .printer
= dpipe_field_printer_ipv6_addr
,
5391 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5395 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5396 .printers
= dpipe_field_printers_ipv6
,
5397 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5398 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5401 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5402 &dpipe_header_printer_ipv4
,
5403 &dpipe_header_printer_ethernet
,
5404 &dpipe_header_printer_ipv6
,
5407 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5408 struct dpipe_op_info
*info
,
5409 enum dpipe_value_type type
,
5412 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5413 struct dpipe_header_printer
*header_printer
;
5414 struct dpipe_field_printer
*field_printer
;
5415 unsigned int field_printers_count
;
5419 for (i
= 0; i
< header_printers_count
; i
++) {
5420 header_printer
= dpipe_header_printers
[i
];
5421 if (header_printer
->header_id
!= info
->header_id
)
5423 field_printers_count
= header_printer
->printers_count
;
5424 for (j
= 0; j
< field_printers_count
; j
++) {
5425 field_printer
= &header_printer
->printers
[j
];
5426 if (field_printer
->field_id
!= info
->field_id
)
5428 field_printer
->printer(ctx
, type
, value
);
5436 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5438 unsigned int value_len
,
5439 struct dpipe_op_info
*info
,
5440 enum dpipe_value_type type
)
5442 if (info
->header_global
&&
5443 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5446 if (value_len
== sizeof(uint32_t)) {
5447 uint32_t *value_32
= value
;
5449 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
5453 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5454 struct nlattr
**nla_match_value
,
5455 struct dpipe_op_info
*info
)
5457 void *value
, *value_mask
;
5458 uint32_t value_mapping
;
5462 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5463 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5465 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5466 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5469 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5470 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
5474 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5475 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5476 DPIPE_VALUE_TYPE_MASK
);
5479 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5482 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5485 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5486 struct dpipe_match match
;
5489 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5490 if (err
!= MNL_CB_OK
)
5493 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5494 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5498 pr_out_entry_start(ctx
->dl
);
5499 if (dpipe_match_parse(&match
,
5500 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5501 goto err_match_parse
;
5502 pr_out_dpipe_match(&match
, ctx
);
5503 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5504 pr_out_entry_end(ctx
->dl
);
5509 pr_out_entry_end(ctx
->dl
);
5513 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5516 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5517 struct dpipe_action action
;
5520 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5521 if (err
!= MNL_CB_OK
)
5524 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5525 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5529 pr_out_entry_start(ctx
->dl
);
5530 if (dpipe_action_parse(&action
,
5531 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5532 goto err_action_parse
;
5533 pr_out_dpipe_action(&action
, ctx
);
5534 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5535 pr_out_entry_end(ctx
->dl
);
5540 pr_out_entry_end(ctx
->dl
);
5545 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5546 struct nlattr
*nla_action_values
)
5548 struct nlattr
*nla_action_value
;
5550 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5551 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5558 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5559 struct nlattr
*nla_match_values
)
5561 struct nlattr
*nla_match_value
;
5563 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5564 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5570 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5572 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5573 uint32_t entry_index
;
5577 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5578 if (err
!= MNL_CB_OK
)
5581 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5582 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5583 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5587 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5588 pr_out_uint(ctx
->dl
, "index", entry_index
);
5590 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5591 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5592 pr_out_uint(ctx
->dl
, "counter", counter
);
5595 pr_out_array_start(ctx
->dl
, "match_value");
5596 if (dpipe_tables_match_values_show(ctx
,
5597 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5598 goto err_match_values_show
;
5599 pr_out_array_end(ctx
->dl
);
5601 pr_out_array_start(ctx
->dl
, "action_value");
5602 if (dpipe_tables_action_values_show(ctx
,
5603 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5604 goto err_action_values_show
;
5605 pr_out_array_end(ctx
->dl
);
5608 err_action_values_show
:
5609 err_match_values_show
:
5610 pr_out_array_end(ctx
->dl
);
5614 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5616 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5617 struct nlattr
*nla_entry
;
5619 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5620 pr_out_handle_start_arr(ctx
->dl
, tb
);
5621 if (dpipe_entry_show(ctx
, nla_entry
))
5622 goto err_entry_show
;
5623 pr_out_handle_end(ctx
->dl
);
5628 pr_out_handle_end(ctx
->dl
);
5632 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5634 struct dpipe_ctx
*ctx
= data
;
5635 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5636 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5638 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5639 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5640 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5641 return MNL_CB_ERROR
;
5643 if (dpipe_table_entries_show(ctx
, tb
))
5644 return MNL_CB_ERROR
;
5648 static int cmd_dpipe_table_dump(struct dl
*dl
)
5650 struct nlmsghdr
*nlh
;
5651 struct dpipe_ctx ctx
= {};
5652 uint16_t flags
= NLM_F_REQUEST
;
5655 err
= dpipe_ctx_init(&ctx
, dl
);
5659 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5663 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5664 dl_opts_put(nlh
, dl
);
5665 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5667 pr_err("error get headers %s\n", strerror(ctx
.err
));
5671 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5672 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5673 dl_opts_put(nlh
, dl
);
5675 pr_out_section_start(dl
, "table_entry");
5676 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5677 pr_out_section_end(dl
);
5679 dpipe_ctx_fini(&ctx
);
5683 static void cmd_dpipe_table_help(void)
5685 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5686 "where OBJECT-LIST := { show | set | dump }\n");
5689 static int cmd_dpipe_table(struct dl
*dl
)
5691 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5692 cmd_dpipe_table_help();
5694 } else if (dl_argv_match(dl
, "show")) {
5696 return cmd_dpipe_table_show(dl
);
5697 } else if (dl_argv_match(dl
, "set")) {
5699 return cmd_dpipe_table_set(dl
);
5700 } else if (dl_argv_match(dl
, "dump")) {
5702 return cmd_dpipe_table_dump(dl
);
5704 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5708 static void cmd_dpipe_help(void)
5710 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5711 "where OBJECT-LIST := { header | table }\n");
5714 static int cmd_dpipe(struct dl
*dl
)
5716 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5719 } else if (dl_argv_match(dl
, "header")) {
5721 return cmd_dpipe_header(dl
);
5722 } else if (dl_argv_match(dl
, "table")) {
5724 return cmd_dpipe_table(dl
);
5726 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5731 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5732 struct nlattr
**nla_resource
)
5734 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5735 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5736 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5737 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5738 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5739 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5740 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5744 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5745 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5746 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5747 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5748 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5749 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5750 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5752 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5753 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5755 resource
->size_new
= resource
->size
;
5757 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5758 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5759 resource
->occ_valid
= true;
5762 if (resource
->size_new
!= resource
->size
)
5763 ctx
->pending_change
= true;
5769 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5770 struct resource
*parent_resource
, struct nlattr
*nl
)
5772 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5773 struct nlattr
*nla_child_resource
;
5774 struct nlattr
*nla_resources
;
5784 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5785 if (err
!= MNL_CB_OK
)
5788 err
= resource_parse(ctx
, resource
, nla_resource
);
5792 resource
->parent
= parent_resource
;
5793 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5796 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5797 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5799 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5800 struct resource
*child_resource
;
5801 struct list_head
*list
;
5803 child_resource
= resource_alloc();
5804 if (!child_resource
)
5808 list
= &ctx
->resources
->resource_list
;
5810 list
= &resource
->resource_list
;
5812 list_add_tail(&child_resource
->list
, list
);
5813 err
= resource_get(ctx
, child_resource
, resource
,
5814 nla_child_resource
);
5822 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5825 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5826 default: return "<unknown unit>";
5830 static void resource_show(struct resource
*resource
,
5831 struct resource_ctx
*ctx
)
5833 struct resource
*child_resource
;
5834 struct dpipe_table
*table
;
5835 struct dl
*dl
= ctx
->dl
;
5838 pr_out_str(dl
, "name", resource
->name
);
5840 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5841 pr_out_u64(dl
, "size", resource
->size
);
5842 if (resource
->size
!= resource
->size_new
)
5843 pr_out_u64(dl
, "size_new", resource
->size_new
);
5844 if (resource
->occ_valid
)
5845 pr_out_uint(dl
, "occ", resource
->size_occ
);
5846 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5848 if (resource
->size_min
!= resource
->size_max
) {
5849 pr_out_uint(dl
, "size_min", resource
->size_min
);
5850 pr_out_u64(dl
, "size_max", resource
->size_max
);
5851 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5854 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5855 if (table
->resource_id
== resource
->id
&&
5856 table
->resource_valid
)
5860 pr_out_array_start(dl
, "dpipe_tables");
5862 pr_out_str(dl
, "dpipe_tables", "none");
5864 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5865 if (table
->resource_id
!= resource
->id
||
5866 !table
->resource_valid
)
5868 pr_out_entry_start(dl
);
5869 pr_out_str(dl
, "table_name", table
->name
);
5870 pr_out_entry_end(dl
);
5873 pr_out_array_end(dl
);
5875 if (list_empty(&resource
->resource_list
))
5878 if (ctx
->pending_change
)
5879 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5881 pr_out_array_start(dl
, "resources");
5882 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5883 pr_out_entry_start(dl
);
5884 resource_show(child_resource
, ctx
);
5885 pr_out_entry_end(dl
);
5887 pr_out_array_end(dl
);
5891 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5893 struct resources
*resources
= ctx
->resources
;
5894 struct resource
*resource
;
5896 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5897 pr_out_handle_start_arr(ctx
->dl
, tb
);
5898 resource_show(resource
, ctx
);
5899 pr_out_handle_end(ctx
->dl
);
5903 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5905 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5908 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5910 struct resource_ctx
*ctx
= data
;
5911 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5912 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5915 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5916 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5917 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5918 return MNL_CB_ERROR
;
5920 err
= resources_get(ctx
, tb
);
5923 return MNL_CB_ERROR
;
5926 if (ctx
->print_resources
)
5927 resources_show(ctx
, tb
);
5932 static int cmd_resource_show(struct dl
*dl
)
5934 struct nlmsghdr
*nlh
;
5935 struct dpipe_ctx dpipe_ctx
= {};
5936 struct resource_ctx resource_ctx
= {};
5939 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5943 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5945 dl_opts_put(nlh
, dl
);
5947 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5951 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5954 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5958 err
= resource_ctx_init(&resource_ctx
, dl
);
5962 resource_ctx
.print_resources
= true;
5963 resource_ctx
.tables
= dpipe_ctx
.tables
;
5964 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5965 NLM_F_REQUEST
| NLM_F_ACK
);
5966 dl_opts_put(nlh
, dl
);
5967 pr_out_section_start(dl
, "resources");
5968 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5970 pr_out_section_end(dl
);
5971 resource_ctx_fini(&resource_ctx
);
5973 dpipe_ctx_fini(&dpipe_ctx
);
5977 static void cmd_resource_help(void)
5979 pr_err("Usage: devlink resource show DEV\n"
5980 " devlink resource set DEV path PATH size SIZE\n");
5983 static struct resource
*
5984 resource_find_by_name(struct list_head
*list
, char *name
)
5986 struct resource
*resource
;
5988 list_for_each_entry(resource
, list
, list
) {
5989 if (!strcmp(resource
->name
, name
))
5996 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5997 uint32_t *p_resource_id
, bool *p_resource_valid
)
5999 struct resource
*resource
;
6000 uint32_t resource_id
= 0;
6001 char *resource_path_dup
;
6002 struct list_head
*list
;
6003 const char del
[] = "/";
6004 char *resource_name
;
6006 resource_path_dup
= strdup(resource_path
);
6007 list
= &ctx
->resources
->resource_list
;
6008 resource_name
= strtok(resource_path_dup
, del
);
6009 while (resource_name
!= NULL
) {
6010 resource
= resource_find_by_name(list
, resource_name
);
6012 goto err_resource_lookup
;
6014 list
= &resource
->resource_list
;
6015 resource_name
= strtok(NULL
, del
);
6016 resource_id
= resource
->id
;
6018 free(resource_path_dup
);
6019 *p_resource_valid
= true;
6020 *p_resource_id
= resource_id
;
6023 err_resource_lookup
:
6024 free(resource_path_dup
);
6028 static int cmd_resource_set(struct dl
*dl
)
6030 struct nlmsghdr
*nlh
;
6031 struct resource_ctx ctx
= {};
6034 err
= resource_ctx_init(&ctx
, dl
);
6038 ctx
.print_resources
= false;
6039 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6040 DL_OPT_RESOURCE_SIZE
, 0);
6044 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6046 dl_opts_put(nlh
, dl
);
6047 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6049 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6053 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6054 &dl
->opts
.resource_id
,
6055 &dl
->opts
.resource_id_valid
);
6057 pr_err("error parsing resource path %s\n", strerror(-err
));
6061 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6062 NLM_F_REQUEST
| NLM_F_ACK
);
6064 dl_opts_put(nlh
, dl
);
6065 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6067 resource_ctx_fini(&ctx
);
6071 static int cmd_resource(struct dl
*dl
)
6073 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6074 cmd_resource_help();
6076 } else if (dl_argv_match(dl
, "show")) {
6078 return cmd_resource_show(dl
);
6079 } else if (dl_argv_match(dl
, "set")) {
6081 return cmd_resource_set(dl
);
6083 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6087 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6089 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6090 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6091 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6094 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6095 if (dl
->json_output
) {
6096 jsonw_name(dl
->jw
, buf
);
6097 jsonw_start_object(dl
->jw
);
6103 static void pr_out_region_handle_end(struct dl
*dl
)
6105 if (dl
->json_output
)
6106 jsonw_end_object(dl
->jw
);
6111 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6113 if (dl
->json_output
) {
6114 jsonw_name(dl
->jw
, "snapshot");
6115 jsonw_start_array(dl
->jw
);
6117 if (g_indent_newline
)
6118 pr_out("snapshot %s", array
? "[" : "");
6120 pr_out(" snapshot %s", array
? "[" : "");
6124 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6126 if (dl
->json_output
)
6127 jsonw_end_array(dl
->jw
);
6132 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6134 uint32_t snapshot_id
;
6136 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6139 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6141 if (dl
->json_output
)
6142 jsonw_uint(dl
->jw
, snapshot_id
);
6144 pr_out("%s%u", index
? " " : "", snapshot_id
);
6147 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6149 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6150 struct nlattr
*nla_sanpshot
;
6153 pr_out_region_snapshots_start(dl
, true);
6154 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6155 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6156 if (err
!= MNL_CB_OK
)
6158 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6160 pr_out_region_snapshots_end(dl
, true);
6163 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6165 pr_out_region_snapshots_start(dl
, false);
6166 pr_out_region_snapshots_id(dl
, tb
, 0);
6167 pr_out_region_snapshots_end(dl
, false);
6170 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6172 pr_out_region_handle_start(dl
, tb
);
6174 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6175 pr_out_u64(dl
, "size",
6176 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6178 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6179 pr_out_snapshots(dl
, tb
);
6181 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6182 pr_out_snapshot(dl
, tb
);
6184 pr_out_region_handle_end(dl
);
6187 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6189 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6190 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6191 struct dl
*dl
= data
;
6193 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6194 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6195 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6196 return MNL_CB_ERROR
;
6198 pr_out_region(dl
, tb
);
6203 static int cmd_region_show(struct dl
*dl
)
6205 struct nlmsghdr
*nlh
;
6206 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6209 if (dl_argc(dl
) == 0)
6210 flags
|= NLM_F_DUMP
;
6212 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6214 if (dl_argc(dl
) > 0) {
6215 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6220 pr_out_section_start(dl
, "regions");
6221 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6222 pr_out_section_end(dl
);
6226 static int cmd_region_snapshot_del(struct dl
*dl
)
6228 struct nlmsghdr
*nlh
;
6231 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6232 NLM_F_REQUEST
| NLM_F_ACK
);
6234 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6235 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6239 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6242 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6244 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6245 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6246 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6247 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6248 struct dl
*dl
= data
;
6251 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6252 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6253 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6254 return MNL_CB_ERROR
;
6256 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6257 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6258 if (err
!= MNL_CB_OK
)
6259 return MNL_CB_ERROR
;
6261 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6262 if (!nla_chunk_data
)
6265 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6266 if (!nla_chunk_addr
)
6269 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6270 mnl_attr_get_payload_len(nla_chunk_data
),
6271 mnl_attr_get_u64(nla_chunk_addr
));
6276 static int cmd_region_dump(struct dl
*dl
)
6278 struct nlmsghdr
*nlh
;
6281 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6282 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6284 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6285 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6289 pr_out_section_start(dl
, "dump");
6290 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6291 pr_out_section_end(dl
);
6292 if (!dl
->json_output
)
6297 static int cmd_region_read(struct dl
*dl
)
6299 struct nlmsghdr
*nlh
;
6302 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6303 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6305 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6306 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6307 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6311 pr_out_section_start(dl
, "read");
6312 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6313 pr_out_section_end(dl
);
6314 if (!dl
->json_output
)
6319 static void cmd_region_help(void)
6321 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6322 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6323 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6324 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6327 static int cmd_region(struct dl
*dl
)
6329 if (dl_no_arg(dl
)) {
6330 return cmd_region_show(dl
);
6331 } else if (dl_argv_match(dl
, "help")) {
6334 } else if (dl_argv_match(dl
, "show")) {
6336 return cmd_region_show(dl
);
6337 } else if (dl_argv_match(dl
, "del")) {
6339 return cmd_region_snapshot_del(dl
);
6340 } else if (dl_argv_match(dl
, "dump")) {
6342 return cmd_region_dump(dl
);
6343 } else if (dl_argv_match(dl
, "read")) {
6345 return cmd_region_read(dl
);
6347 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6351 static int cmd_health_set_params(struct dl
*dl
)
6353 struct nlmsghdr
*nlh
;
6356 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6357 NLM_F_REQUEST
| NLM_F_ACK
);
6358 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6359 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6360 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6364 dl_opts_put(nlh
, dl
);
6365 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6368 static int cmd_health_dump_clear(struct dl
*dl
)
6370 struct nlmsghdr
*nlh
;
6373 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6374 NLM_F_REQUEST
| NLM_F_ACK
);
6376 err
= dl_argv_parse_put(nlh
, dl
,
6377 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6381 dl_opts_put(nlh
, dl
);
6382 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6385 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6392 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
6395 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
6398 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
6401 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
6404 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
6406 case MNL_TYPE_NUL_STRING
:
6407 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
6409 case MNL_TYPE_BINARY
:
6410 len
= mnl_attr_get_payload_len(nl_data
);
6411 data
= mnl_attr_get_payload(nl_data
);
6412 pr_out_binary_value(dl
, data
, len
);
6422 struct list_head list
;
6425 struct fmsg_cb_data
{
6428 struct list_head entry_list
;
6431 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6432 uint8_t *attr_value
, bool insert
)
6434 struct nest_entry
*entry
;
6437 entry
= malloc(sizeof(struct nest_entry
));
6441 entry
->attr_type
= *attr_value
;
6442 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6444 if (list_empty(&fmsg_data
->entry_list
))
6445 return MNL_CB_ERROR
;
6446 entry
= list_first_entry(&fmsg_data
->entry_list
,
6447 struct nest_entry
, list
);
6448 *attr_value
= entry
->attr_type
;
6449 list_del(&entry
->list
);
6455 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6458 struct dl
*dl
= fmsg_data
->dl
;
6459 uint8_t value
= nest_value
;
6462 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6463 if (err
!= MNL_CB_OK
)
6467 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6469 pr_out_entry_start(dl
);
6471 pr_out_entry_end(dl
);
6473 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6475 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6476 if (dl
->json_output
) {
6478 jsonw_start_array(dl
->jw
);
6480 jsonw_end_array(dl
->jw
);
6484 __pr_out_indent_inc();
6486 __pr_out_indent_dec();
6496 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6498 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6499 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6500 struct fmsg_cb_data
*fmsg_data
= data
;
6501 struct dl
*dl
= fmsg_data
->dl
;
6502 struct nlattr
*nla_object
;
6506 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6507 if (!tb
[DEVLINK_ATTR_FMSG
])
6508 return MNL_CB_ERROR
;
6510 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6511 attr_type
= mnl_attr_get_type(nla_object
);
6512 switch (attr_type
) {
6513 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6514 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6515 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6516 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6517 if (err
!= MNL_CB_OK
)
6520 case DEVLINK_ATTR_FMSG_NEST_END
:
6521 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6522 if (err
!= MNL_CB_OK
)
6525 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6526 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6528 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6529 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6531 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6532 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6534 if (err
!= MNL_CB_OK
)
6544 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6546 struct fmsg_cb_data data
;
6547 struct nlmsghdr
*nlh
;
6550 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6552 err
= dl_argv_parse_put(nlh
, dl
,
6553 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6558 INIT_LIST_HEAD(&data
.entry_list
);
6559 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6563 static int cmd_health_dump_show(struct dl
*dl
)
6565 return cmd_health_object_common(dl
,
6566 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6570 static int cmd_health_diagnose(struct dl
*dl
)
6572 return cmd_health_object_common(dl
,
6573 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6577 static int cmd_health_recover(struct dl
*dl
)
6579 struct nlmsghdr
*nlh
;
6582 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6583 NLM_F_REQUEST
| NLM_F_ACK
);
6585 err
= dl_argv_parse_put(nlh
, dl
,
6586 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6590 dl_opts_put(nlh
, dl
);
6591 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6594 enum devlink_health_reporter_state
{
6595 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6596 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6599 static const char *health_state_name(uint8_t state
)
6602 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6603 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6604 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6605 return HEALTH_REPORTER_STATE_ERROR_STR
;
6607 return "<unknown state>";
6611 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6613 struct sysinfo s_info
;
6619 info
= localtime(&now
);
6620 err
= sysinfo(&s_info
);
6623 /* Subtract uptime in sec from now yields the time of system
6624 * uptime. To this, add time_ms which is the amount of
6625 * milliseconds elapsed between uptime and the dump taken.
6627 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6628 info
= localtime(&sec
);
6630 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6631 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6634 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6636 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6637 enum devlink_health_reporter_state state
;
6638 const struct nlattr
*attr
;
6642 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6644 if (err
!= MNL_CB_OK
)
6647 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6648 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6649 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6650 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6653 pr_out_handle_start_arr(dl
, tb_health
);
6655 pr_out_str(dl
, "name",
6656 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6657 if (!dl
->json_output
) {
6659 __pr_out_indent_inc();
6661 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6662 pr_out_str(dl
, "state", health_state_name(state
));
6663 pr_out_u64(dl
, "error",
6664 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6665 pr_out_u64(dl
, "recover",
6666 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6667 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6668 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6669 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6671 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6672 time_ms
= mnl_attr_get_u64(attr
);
6673 format_logtime(time_ms
, dump_date
, dump_time
);
6675 pr_out_str(dl
, "last_dump_date", dump_date
);
6676 pr_out_str(dl
, "last_dump_time", dump_time
);
6678 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6679 pr_out_u64(dl
, "grace_period",
6680 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6681 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6682 pr_out_bool(dl
, "auto_recover",
6683 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6685 __pr_out_indent_dec();
6686 pr_out_handle_end(dl
);
6689 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6691 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6692 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6693 struct dl
*dl
= data
;
6695 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6696 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6697 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6698 return MNL_CB_ERROR
;
6700 pr_out_health(dl
, tb
);
6705 static int cmd_health_show(struct dl
*dl
)
6707 struct nlmsghdr
*nlh
;
6708 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6711 if (dl_argc(dl
) == 0)
6712 flags
|= NLM_F_DUMP
;
6713 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6716 if (dl_argc(dl
) > 0) {
6717 err
= dl_argv_parse_put(nlh
, dl
,
6719 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6723 pr_out_section_start(dl
, "health");
6725 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6726 pr_out_section_end(dl
);
6730 static void cmd_health_help(void)
6732 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6733 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6734 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6735 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6736 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6737 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6740 static int cmd_health(struct dl
*dl
)
6742 if (dl_argv_match(dl
, "help")) {
6745 } else if (dl_argv_match(dl
, "show") ||
6746 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6748 return cmd_health_show(dl
);
6749 } else if (dl_argv_match(dl
, "recover")) {
6751 return cmd_health_recover(dl
);
6752 } else if (dl_argv_match(dl
, "diagnose")) {
6754 return cmd_health_diagnose(dl
);
6755 } else if (dl_argv_match(dl
, "dump")) {
6757 if (dl_argv_match(dl
, "show")) {
6759 return cmd_health_dump_show(dl
);
6760 } else if (dl_argv_match(dl
, "clear")) {
6762 return cmd_health_dump_clear(dl
);
6764 } else if (dl_argv_match(dl
, "set")) {
6766 return cmd_health_set_params(dl
);
6768 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6772 static const char *trap_type_name(uint8_t type
)
6775 case DEVLINK_TRAP_TYPE_DROP
:
6777 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6780 return "<unknown type>";
6784 static const char *trap_action_name(uint8_t action
)
6787 case DEVLINK_TRAP_ACTION_DROP
:
6789 case DEVLINK_TRAP_ACTION_TRAP
:
6792 return "<unknown action>";
6796 static const char *trap_metadata_name(const struct nlattr
*attr
)
6798 switch (attr
->nla_type
) {
6799 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
6800 return "input_port";
6802 return "<unknown metadata type>";
6805 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
6807 struct nlattr
*attr_metadata
;
6809 pr_out_array_start(dl
, "metadata");
6810 mnl_attr_for_each_nested(attr_metadata
, attr
)
6811 pr_out_str_value(dl
, trap_metadata_name(attr_metadata
));
6812 pr_out_array_end(dl
);
6815 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6817 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
6818 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
6821 pr_out_handle_start_arr(dl
, tb
);
6823 __pr_out_handle_start(dl
, tb
, true, false);
6825 pr_out_str(dl
, "name", mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
6826 pr_out_str(dl
, "type", trap_type_name(type
));
6827 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6828 pr_out_str(dl
, "action", trap_action_name(action
));
6829 pr_out_str(dl
, "group",
6830 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6832 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
6833 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6834 pr_out_handle_end(dl
);
6837 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6839 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6840 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6841 struct dl
*dl
= data
;
6843 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6844 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6845 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
6846 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
6847 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
6848 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
6849 return MNL_CB_ERROR
;
6851 pr_out_trap(dl
, tb
, true);
6856 static void cmd_trap_help(void)
6858 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
6859 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
6860 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
6861 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
6864 static int cmd_trap_show(struct dl
*dl
)
6866 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6867 struct nlmsghdr
*nlh
;
6870 if (dl_argc(dl
) == 0)
6871 flags
|= NLM_F_DUMP
;
6873 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
6875 if (dl_argc(dl
) > 0) {
6876 err
= dl_argv_parse_put(nlh
, dl
,
6877 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
6882 pr_out_section_start(dl
, "trap");
6883 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
6884 pr_out_section_end(dl
);
6889 static int cmd_trap_set(struct dl
*dl
)
6891 struct nlmsghdr
*nlh
;
6894 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
6895 NLM_F_REQUEST
| NLM_F_ACK
);
6897 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
6898 DL_OPT_TRAP_ACTION
);
6902 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6905 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6908 pr_out_handle_start_arr(dl
, tb
);
6910 __pr_out_handle_start(dl
, tb
, true, false);
6912 pr_out_str(dl
, "name",
6913 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6914 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6915 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6916 pr_out_handle_end(dl
);
6919 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6921 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6922 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6923 struct dl
*dl
= data
;
6925 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6926 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6927 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
6928 return MNL_CB_ERROR
;
6930 pr_out_trap_group(dl
, tb
, true);
6935 static int cmd_trap_group_show(struct dl
*dl
)
6937 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6938 struct nlmsghdr
*nlh
;
6941 if (dl_argc(dl
) == 0)
6942 flags
|= NLM_F_DUMP
;
6944 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
6946 if (dl_argc(dl
) > 0) {
6947 err
= dl_argv_parse_put(nlh
, dl
,
6948 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
6954 pr_out_section_start(dl
, "trap_group");
6955 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
6956 pr_out_section_end(dl
);
6961 static int cmd_trap_group_set(struct dl
*dl
)
6963 struct nlmsghdr
*nlh
;
6966 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
6967 NLM_F_REQUEST
| NLM_F_ACK
);
6969 err
= dl_argv_parse_put(nlh
, dl
,
6970 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
6971 DL_OPT_TRAP_ACTION
);
6975 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6978 static int cmd_trap_group(struct dl
*dl
)
6980 if (dl_argv_match(dl
, "help")) {
6983 } else if (dl_argv_match(dl
, "show") ||
6984 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6986 return cmd_trap_group_show(dl
);
6987 } else if (dl_argv_match(dl
, "set")) {
6989 return cmd_trap_group_set(dl
);
6991 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6995 static int cmd_trap(struct dl
*dl
)
6997 if (dl_argv_match(dl
, "help")) {
7000 } else if (dl_argv_match(dl
, "show") ||
7001 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7003 return cmd_trap_show(dl
);
7004 } else if (dl_argv_match(dl
, "set")) {
7006 return cmd_trap_set(dl
);
7007 } else if (dl_argv_match(dl
, "group")) {
7009 return cmd_trap_group(dl
);
7011 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7015 static void help(void)
7017 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7018 " devlink [ -f[orce] ] -b[atch] filename\n"
7019 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7020 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7023 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7028 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7031 } else if (dl_argv_match(dl
, "dev")) {
7034 } else if (dl_argv_match(dl
, "port")) {
7036 return cmd_port(dl
);
7037 } else if (dl_argv_match(dl
, "sb")) {
7040 } else if (dl_argv_match(dl
, "monitor")) {
7043 } else if (dl_argv_match(dl
, "dpipe")) {
7045 return cmd_dpipe(dl
);
7046 } else if (dl_argv_match(dl
, "resource")) {
7048 return cmd_resource(dl
);
7049 } else if (dl_argv_match(dl
, "region")) {
7051 return cmd_region(dl
);
7052 } else if (dl_argv_match(dl
, "health")) {
7054 return cmd_health(dl
);
7055 } else if (dl_argv_match(dl
, "trap")) {
7057 return cmd_trap(dl
);
7059 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7063 static int dl_init(struct dl
*dl
)
7067 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7069 pr_err("Failed to connect to devlink Netlink\n");
7073 err
= ifname_map_init(dl
);
7075 pr_err("Failed to create index map\n");
7076 goto err_ifname_map_create
;
7078 if (dl
->json_output
) {
7079 dl
->jw
= jsonw_new(stdout
);
7081 pr_err("Failed to create JSON writer\n");
7084 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
7089 ifname_map_fini(dl
);
7090 err_ifname_map_create
:
7091 mnlg_socket_close(dl
->nlg
);
7095 static void dl_fini(struct dl
*dl
)
7097 if (dl
->json_output
)
7098 jsonw_destroy(&dl
->jw
);
7099 ifname_map_fini(dl
);
7100 mnlg_socket_close(dl
->nlg
);
7103 static struct dl
*dl_alloc(void)
7107 dl
= calloc(1, sizeof(*dl
));
7113 static void dl_free(struct dl
*dl
)
7118 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7122 int ret
= EXIT_SUCCESS
;
7124 if (name
&& strcmp(name
, "-") != 0) {
7125 if (freopen(name
, "r", stdin
) == NULL
) {
7127 "Cannot open file \"%s\" for reading: %s\n",
7128 name
, strerror(errno
));
7129 return EXIT_FAILURE
;
7134 while (getcmdline(&line
, &len
, stdin
) != -1) {
7138 largc
= makeargs(line
, largv
, 100);
7140 continue; /* blank line */
7142 if (dl_cmd(dl
, largc
, largv
)) {
7143 fprintf(stderr
, "Command failed %s:%d\n",
7157 int main(int argc
, char **argv
)
7159 static const struct option long_options
[] = {
7160 { "Version", no_argument
, NULL
, 'V' },
7161 { "force", no_argument
, NULL
, 'f' },
7162 { "batch", required_argument
, NULL
, 'b' },
7163 { "no-nice-names", no_argument
, NULL
, 'n' },
7164 { "json", no_argument
, NULL
, 'j' },
7165 { "pretty", no_argument
, NULL
, 'p' },
7166 { "verbose", no_argument
, NULL
, 'v' },
7167 { "statistics", no_argument
, NULL
, 's' },
7168 { NULL
, 0, NULL
, 0 }
7170 const char *batch_file
= NULL
;
7179 pr_err("Failed to allocate memory for devlink\n");
7180 return EXIT_FAILURE
;
7183 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvs",
7184 long_options
, NULL
)) >= 0) {
7188 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7195 batch_file
= optarg
;
7198 dl
->no_nice_names
= true;
7201 dl
->json_output
= true;
7204 dl
->pretty_output
= true;
7213 pr_err("Unknown option.\n");
7230 err
= dl_batch(dl
, batch_file
, force
);
7232 err
= dl_cmd(dl
, argc
, argv
);