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
,
481 [DEVLINK_ATTR_RELOAD_FAILED
] = MNL_TYPE_U8
,
484 static const enum mnl_attr_data_type
485 devlink_stats_policy
[DEVLINK_ATTR_STATS_MAX
+ 1] = {
486 [DEVLINK_ATTR_STATS_RX_PACKETS
] = MNL_TYPE_U64
,
487 [DEVLINK_ATTR_STATS_RX_BYTES
] = MNL_TYPE_U64
,
490 static int attr_cb(const struct nlattr
*attr
, void *data
)
492 const struct nlattr
**tb
= data
;
495 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
498 type
= mnl_attr_get_type(attr
);
499 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
506 static int attr_stats_cb(const struct nlattr
*attr
, void *data
)
508 const struct nlattr
**tb
= data
;
511 /* Allow the tool to work on top of newer kernels that might contain
514 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_STATS_MAX
) < 0)
517 type
= mnl_attr_get_type(attr
);
518 if (mnl_attr_validate(attr
, devlink_stats_policy
[type
]) < 0)
525 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
527 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
528 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
529 struct dl
*dl
= data
;
530 struct ifname_map
*ifname_map
;
531 const char *bus_name
;
532 const char *dev_name
;
533 uint32_t port_ifindex
;
534 const char *port_ifname
;
536 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
537 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
538 !tb
[DEVLINK_ATTR_PORT_INDEX
])
541 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
544 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
545 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
546 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
547 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
548 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
549 port_ifindex
, port_ifname
);
552 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
557 static void ifname_map_fini(struct dl
*dl
)
559 struct ifname_map
*ifname_map
, *tmp
;
561 list_for_each_entry_safe(ifname_map
, tmp
,
562 &dl
->ifname_map_list
, list
) {
563 list_del(&ifname_map
->list
);
564 ifname_map_free(ifname_map
);
568 static int ifname_map_init(struct dl
*dl
)
570 struct nlmsghdr
*nlh
;
573 INIT_LIST_HEAD(&dl
->ifname_map_list
);
575 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
576 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
578 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
586 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
587 char **p_bus_name
, char **p_dev_name
,
588 uint32_t *p_port_index
)
590 struct ifname_map
*ifname_map
;
592 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
593 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
594 *p_bus_name
= ifname_map
->bus_name
;
595 *p_dev_name
= ifname_map
->dev_name
;
596 *p_port_index
= ifname_map
->port_index
;
603 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
604 const char *dev_name
, uint32_t port_index
,
607 struct ifname_map
*ifname_map
;
609 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
610 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
611 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
612 port_index
== ifname_map
->port_index
) {
613 *p_ifname
= ifname_map
->ifname
;
620 static unsigned int strslashcount(char *str
)
622 unsigned int count
= 0;
625 while ((pos
= strchr(pos
, '/'))) {
632 static int strslashrsplit(char *str
, char **before
, char **after
)
636 slash
= strrchr(str
, '/');
645 static int strtouint64_t(const char *str
, uint64_t *p_val
)
648 unsigned long long int val
;
650 val
= strtoull(str
, &endptr
, 10);
651 if (endptr
== str
|| *endptr
!= '\0')
659 static int strtouint32_t(const char *str
, uint32_t *p_val
)
662 unsigned long int val
;
664 val
= strtoul(str
, &endptr
, 10);
665 if (endptr
== str
|| *endptr
!= '\0')
673 static int strtouint16_t(const char *str
, uint16_t *p_val
)
676 unsigned long int val
;
678 val
= strtoul(str
, &endptr
, 10);
679 if (endptr
== str
|| *endptr
!= '\0')
687 static int strtouint8_t(const char *str
, uint8_t *p_val
)
690 unsigned long int val
;
692 val
= strtoul(str
, &endptr
, 10);
693 if (endptr
== str
|| *endptr
!= '\0')
701 static int strtobool(const char *str
, bool *p_val
)
705 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
707 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
715 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
717 strslashrsplit(str
, p_bus_name
, p_dev_name
);
721 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
723 char *str
= dl_argv_next(dl
);
726 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
729 if (strslashcount(str
) != 1) {
730 pr_err("Wrong devlink identification string format.\n");
731 pr_err("Expected \"bus_name/dev_name\".\n");
734 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
737 static int __dl_argv_handle_port(char *str
,
738 char **p_bus_name
, char **p_dev_name
,
739 uint32_t *p_port_index
)
745 err
= strslashrsplit(str
, &handlestr
, &portstr
);
747 pr_err("Port identification \"%s\" is invalid\n", str
);
750 err
= strtouint32_t(portstr
, p_port_index
);
752 pr_err("Port index \"%s\" is not a number or not within range\n",
756 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
758 pr_err("Port identification \"%s\" is invalid\n", str
);
764 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
765 char **p_bus_name
, char **p_dev_name
,
766 uint32_t *p_port_index
)
770 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
773 pr_err("Netdevice \"%s\" not found\n", str
);
779 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
780 char **p_dev_name
, uint32_t *p_port_index
)
782 char *str
= dl_argv_next(dl
);
783 unsigned int slash_count
;
786 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
789 slash_count
= strslashcount(str
);
790 switch (slash_count
) {
792 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
793 p_dev_name
, p_port_index
);
795 return __dl_argv_handle_port(str
, p_bus_name
,
796 p_dev_name
, p_port_index
);
798 pr_err("Wrong port identification string format.\n");
799 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
804 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
805 char **p_dev_name
, uint32_t *p_port_index
,
806 uint64_t *p_handle_bit
)
808 char *str
= dl_argv_next(dl
);
809 unsigned int slash_count
;
813 pr_err("One of following identifications expected:\n"
814 "Devlink identification (\"bus_name/dev_name\")\n"
815 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
818 slash_count
= strslashcount(str
);
819 if (slash_count
== 1) {
820 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
823 *p_handle_bit
= DL_OPT_HANDLE
;
824 } else if (slash_count
== 2) {
825 err
= __dl_argv_handle_port(str
, p_bus_name
,
826 p_dev_name
, p_port_index
);
829 *p_handle_bit
= DL_OPT_HANDLEP
;
830 } else if (slash_count
== 0) {
831 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
832 p_dev_name
, p_port_index
);
835 *p_handle_bit
= DL_OPT_HANDLEP
;
837 pr_err("Wrong port identification string format.\n");
838 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
844 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
845 char **p_dev_name
, char **p_region
)
850 err
= strslashrsplit(str
, &handlestr
, p_region
);
852 pr_err("Region identification \"%s\" is invalid\n", str
);
855 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
857 pr_err("Region identification \"%s\" is invalid\n", str
);
863 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
864 char **p_dev_name
, char **p_region
)
866 char *str
= dl_argv_next(dl
);
867 unsigned int slash_count
;
870 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
874 slash_count
= strslashcount(str
);
875 if (slash_count
!= 2) {
876 pr_err("Wrong region identification string format.\n");
877 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
881 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
884 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
886 char *str
= dl_argv_next(dl
);
890 pr_err("Unsigned number argument expected\n");
894 err
= strtouint64_t(str
, p_val
);
896 pr_err("\"%s\" is not a number or not within range\n", str
);
902 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
904 char *str
= dl_argv_next(dl
);
908 pr_err("Unsigned number argument expected\n");
912 err
= strtouint32_t(str
, p_val
);
914 pr_err("\"%s\" is not a number or not within range\n", str
);
920 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
922 char *str
= dl_argv_next(dl
);
926 pr_err("Unsigned number argument expected\n");
930 err
= strtouint16_t(str
, p_val
);
932 pr_err("\"%s\" is not a number or not within range\n", str
);
938 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
940 char *str
= dl_argv_next(dl
);
944 pr_err("Boolean argument expected\n");
948 err
= strtobool(str
, p_val
);
950 pr_err("\"%s\" is not a valid boolean value\n", str
);
956 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
958 const char *str
= dl_argv_next(dl
);
961 pr_err("String parameter expected\n");
968 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
970 if (strcmp(typestr
, "auto") == 0) {
971 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
972 } else if (strcmp(typestr
, "eth") == 0) {
973 *p_type
= DEVLINK_PORT_TYPE_ETH
;
974 } else if (strcmp(typestr
, "ib") == 0) {
975 *p_type
= DEVLINK_PORT_TYPE_IB
;
977 pr_err("Unknown port type \"%s\"\n", typestr
);
983 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
985 if (strcmp(typestr
, "ingress") == 0) {
986 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
987 } else if (strcmp(typestr
, "egress") == 0) {
988 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
990 pr_err("Unknown pool type \"%s\"\n", typestr
);
996 static int threshold_type_get(const char *typestr
,
997 enum devlink_sb_threshold_type
*p_type
)
999 if (strcmp(typestr
, "static") == 0) {
1000 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
1001 } else if (strcmp(typestr
, "dynamic") == 0) {
1002 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
1004 pr_err("Unknown threshold type \"%s\"\n", typestr
);
1010 static int eswitch_mode_get(const char *typestr
,
1011 enum devlink_eswitch_mode
*p_mode
)
1013 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
1014 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
1015 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
1016 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
1018 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
1024 static int eswitch_inline_mode_get(const char *typestr
,
1025 enum devlink_eswitch_inline_mode
*p_mode
)
1027 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
1028 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
1029 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
1030 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
1031 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
1032 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
1033 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
1034 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
1036 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
1042 static int dpipe_counters_enable_get(const char *typestr
,
1043 bool *counters_enable
)
1045 if (strcmp(typestr
, "enable") == 0) {
1046 *counters_enable
= 1;
1047 } else if (strcmp(typestr
, "disable") == 0) {
1048 *counters_enable
= 0;
1050 pr_err("Unknown counter_state \"%s\"\n", typestr
);
1056 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
1058 if (strcmp(typestr
, "enable") == 0) {
1060 } else if (strcmp(typestr
, "disable") == 0) {
1063 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1069 static int param_cmode_get(const char *cmodestr
,
1070 enum devlink_param_cmode
*cmode
)
1072 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1073 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1074 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1075 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1076 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1077 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1079 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1085 static int trap_action_get(const char *actionstr
,
1086 enum devlink_trap_action
*p_action
)
1088 if (strcmp(actionstr
, "drop") == 0) {
1089 *p_action
= DEVLINK_TRAP_ACTION_DROP
;
1090 } else if (strcmp(actionstr
, "trap") == 0) {
1091 *p_action
= DEVLINK_TRAP_ACTION_TRAP
;
1093 pr_err("Unknown trap action \"%s\"\n", actionstr
);
1099 struct dl_args_metadata
{
1101 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1104 static const struct dl_args_metadata dl_args_required
[] = {
1105 {DL_OPT_PORT_TYPE
, "Port type not set."},
1106 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1107 {DL_OPT_SB_POOL
, "Pool index option expected."},
1108 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1109 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1110 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1111 {DL_OPT_SB_TH
, "Threshold option expected."},
1112 {DL_OPT_SB_TC
, "TC index option expected."},
1113 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1114 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1115 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1116 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1117 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1118 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1119 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1120 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1121 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1122 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1123 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1124 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1125 {DL_OPT_TRAP_NAME
, "Trap's name is expected."},
1126 {DL_OPT_TRAP_GROUP_NAME
, "Trap group's name is expected."},
1129 static int dl_args_finding_required_validate(uint64_t o_required
,
1135 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1136 o_flag
= dl_args_required
[i
].o_flag
;
1137 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1138 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1145 static int dl_argv_parse(struct dl
*dl
, uint64_t o_required
,
1146 uint64_t o_optional
)
1148 struct dl_opts
*opts
= &dl
->opts
;
1149 uint64_t o_all
= o_required
| o_optional
;
1150 uint64_t o_found
= 0;
1153 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1154 uint64_t handle_bit
;
1156 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1157 &opts
->port_index
, &handle_bit
);
1160 o_found
|= handle_bit
;
1161 } else if (o_required
& DL_OPT_HANDLE
) {
1162 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1165 o_found
|= DL_OPT_HANDLE
;
1166 } else if (o_required
& DL_OPT_HANDLEP
) {
1167 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1171 o_found
|= DL_OPT_HANDLEP
;
1172 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1173 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1175 &opts
->region_name
);
1178 o_found
|= DL_OPT_HANDLE_REGION
;
1181 while (dl_argc(dl
)) {
1182 if (dl_argv_match(dl
, "type") &&
1183 (o_all
& DL_OPT_PORT_TYPE
)) {
1184 const char *typestr
;
1187 err
= dl_argv_str(dl
, &typestr
);
1190 err
= port_type_get(typestr
, &opts
->port_type
);
1193 o_found
|= DL_OPT_PORT_TYPE
;
1194 } else if (dl_argv_match(dl
, "count") &&
1195 (o_all
& DL_OPT_PORT_COUNT
)) {
1197 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1200 o_found
|= DL_OPT_PORT_COUNT
;
1201 } else if (dl_argv_match(dl
, "sb") &&
1202 (o_all
& DL_OPT_SB
)) {
1204 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1207 o_found
|= DL_OPT_SB
;
1208 } else if (dl_argv_match(dl
, "pool") &&
1209 (o_all
& DL_OPT_SB_POOL
)) {
1211 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1214 o_found
|= DL_OPT_SB_POOL
;
1215 } else if (dl_argv_match(dl
, "size") &&
1216 (o_all
& DL_OPT_SB_SIZE
)) {
1218 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1221 o_found
|= DL_OPT_SB_SIZE
;
1222 } else if (dl_argv_match(dl
, "type") &&
1223 (o_all
& DL_OPT_SB_TYPE
)) {
1224 const char *typestr
;
1227 err
= dl_argv_str(dl
, &typestr
);
1230 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1233 o_found
|= DL_OPT_SB_TYPE
;
1234 } else if (dl_argv_match(dl
, "thtype") &&
1235 (o_all
& DL_OPT_SB_THTYPE
)) {
1236 const char *typestr
;
1239 err
= dl_argv_str(dl
, &typestr
);
1242 err
= threshold_type_get(typestr
,
1243 &opts
->sb_pool_thtype
);
1246 o_found
|= DL_OPT_SB_THTYPE
;
1247 } else if (dl_argv_match(dl
, "th") &&
1248 (o_all
& DL_OPT_SB_TH
)) {
1250 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1253 o_found
|= DL_OPT_SB_TH
;
1254 } else if (dl_argv_match(dl
, "tc") &&
1255 (o_all
& DL_OPT_SB_TC
)) {
1257 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1260 o_found
|= DL_OPT_SB_TC
;
1261 } else if (dl_argv_match(dl
, "mode") &&
1262 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1263 const char *typestr
;
1266 err
= dl_argv_str(dl
, &typestr
);
1269 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1272 o_found
|= DL_OPT_ESWITCH_MODE
;
1273 } else if (dl_argv_match(dl
, "inline-mode") &&
1274 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1275 const char *typestr
;
1278 err
= dl_argv_str(dl
, &typestr
);
1281 err
= eswitch_inline_mode_get(
1282 typestr
, &opts
->eswitch_inline_mode
);
1285 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1286 } else if (dl_argv_match(dl
, "name") &&
1287 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1289 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1292 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1293 } else if (dl_argv_match(dl
, "counters") &&
1294 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1295 const char *typestr
;
1298 err
= dl_argv_str(dl
, &typestr
);
1301 err
= dpipe_counters_enable_get(typestr
,
1302 &opts
->dpipe_counters_enable
);
1305 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1306 } else if (dl_argv_match(dl
, "encap") &&
1307 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1308 const char *typestr
;
1311 err
= dl_argv_str(dl
, &typestr
);
1314 err
= eswitch_encap_mode_get(typestr
,
1315 &opts
->eswitch_encap_mode
);
1318 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1319 } else if (dl_argv_match(dl
, "path") &&
1320 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1322 err
= dl_argv_str(dl
, &opts
->resource_path
);
1325 o_found
|= DL_OPT_RESOURCE_PATH
;
1326 } else if (dl_argv_match(dl
, "size") &&
1327 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1329 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1332 o_found
|= DL_OPT_RESOURCE_SIZE
;
1333 } else if (dl_argv_match(dl
, "name") &&
1334 (o_all
& DL_OPT_PARAM_NAME
)) {
1336 err
= dl_argv_str(dl
, &opts
->param_name
);
1339 o_found
|= DL_OPT_PARAM_NAME
;
1340 } else if (dl_argv_match(dl
, "value") &&
1341 (o_all
& DL_OPT_PARAM_VALUE
)) {
1343 err
= dl_argv_str(dl
, &opts
->param_value
);
1346 o_found
|= DL_OPT_PARAM_VALUE
;
1347 } else if (dl_argv_match(dl
, "cmode") &&
1348 (o_all
& DL_OPT_PARAM_CMODE
)) {
1349 const char *cmodestr
;
1352 err
= dl_argv_str(dl
, &cmodestr
);
1355 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1358 o_found
|= DL_OPT_PARAM_CMODE
;
1359 } else if (dl_argv_match(dl
, "snapshot") &&
1360 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1362 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1365 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1366 } else if (dl_argv_match(dl
, "address") &&
1367 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1369 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1372 o_found
|= DL_OPT_REGION_ADDRESS
;
1373 } else if (dl_argv_match(dl
, "length") &&
1374 (o_all
& DL_OPT_REGION_LENGTH
)) {
1376 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1379 o_found
|= DL_OPT_REGION_LENGTH
;
1380 } else if (dl_argv_match(dl
, "file") &&
1381 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1383 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1386 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1387 } else if (dl_argv_match(dl
, "component") &&
1388 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1390 err
= dl_argv_str(dl
, &opts
->flash_component
);
1393 o_found
|= DL_OPT_FLASH_COMPONENT
;
1394 } else if (dl_argv_match(dl
, "reporter") &&
1395 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1397 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1400 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1401 } else if (dl_argv_match(dl
, "grace_period") &&
1402 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1404 err
= dl_argv_uint64_t(dl
,
1405 &opts
->reporter_graceful_period
);
1408 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1409 } else if (dl_argv_match(dl
, "auto_recover") &&
1410 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1412 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1415 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1416 } else if (dl_argv_match(dl
, "trap") &&
1417 (o_all
& DL_OPT_TRAP_NAME
)) {
1419 err
= dl_argv_str(dl
, &opts
->trap_name
);
1422 o_found
|= DL_OPT_TRAP_NAME
;
1423 } else if (dl_argv_match(dl
, "group") &&
1424 (o_all
& DL_OPT_TRAP_GROUP_NAME
)) {
1426 err
= dl_argv_str(dl
, &opts
->trap_group_name
);
1429 o_found
|= DL_OPT_TRAP_GROUP_NAME
;
1430 } else if (dl_argv_match(dl
, "action") &&
1431 (o_all
& DL_OPT_TRAP_ACTION
)) {
1432 const char *actionstr
;
1435 err
= dl_argv_str(dl
, &actionstr
);
1438 err
= trap_action_get(actionstr
, &opts
->trap_action
);
1441 o_found
|= DL_OPT_TRAP_ACTION
;
1443 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1448 opts
->present
= o_found
;
1450 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1452 opts
->present
|= DL_OPT_SB
;
1455 return dl_args_finding_required_validate(o_required
, o_found
);
1458 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1460 struct dl_opts
*opts
= &dl
->opts
;
1462 if (opts
->present
& DL_OPT_HANDLE
) {
1463 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1464 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1465 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1466 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1467 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1468 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1470 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1471 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1472 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1473 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1476 if (opts
->present
& DL_OPT_PORT_TYPE
)
1477 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1479 if (opts
->present
& DL_OPT_PORT_COUNT
)
1480 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1482 if (opts
->present
& DL_OPT_SB
)
1483 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1485 if (opts
->present
& DL_OPT_SB_POOL
)
1486 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1487 opts
->sb_pool_index
);
1488 if (opts
->present
& DL_OPT_SB_SIZE
)
1489 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1490 opts
->sb_pool_size
);
1491 if (opts
->present
& DL_OPT_SB_TYPE
)
1492 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1493 opts
->sb_pool_type
);
1494 if (opts
->present
& DL_OPT_SB_THTYPE
)
1495 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1496 opts
->sb_pool_thtype
);
1497 if (opts
->present
& DL_OPT_SB_TH
)
1498 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1499 opts
->sb_threshold
);
1500 if (opts
->present
& DL_OPT_SB_TC
)
1501 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1503 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1504 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1505 opts
->eswitch_mode
);
1506 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1507 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1508 opts
->eswitch_inline_mode
);
1509 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1510 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1511 opts
->dpipe_table_name
);
1512 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1513 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1514 opts
->dpipe_counters_enable
);
1515 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1516 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1517 opts
->eswitch_encap_mode
);
1518 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1519 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1521 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1522 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1523 opts
->resource_size
);
1524 if (opts
->present
& DL_OPT_PARAM_NAME
)
1525 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1527 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1528 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1530 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1531 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1532 opts
->region_snapshot_id
);
1533 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1534 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1535 opts
->region_address
);
1536 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1537 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1538 opts
->region_length
);
1539 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1540 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1541 opts
->flash_file_name
);
1542 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1543 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1544 opts
->flash_component
);
1545 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1546 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1547 opts
->reporter_name
);
1548 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1549 mnl_attr_put_u64(nlh
,
1550 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1551 opts
->reporter_graceful_period
);
1552 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1553 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1554 opts
->reporter_auto_recover
);
1555 if (opts
->present
& DL_OPT_TRAP_NAME
)
1556 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_NAME
,
1558 if (opts
->present
& DL_OPT_TRAP_GROUP_NAME
)
1559 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
1560 opts
->trap_group_name
);
1561 if (opts
->present
& DL_OPT_TRAP_ACTION
)
1562 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_TRAP_ACTION
,
1567 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1568 uint64_t o_required
, uint64_t o_optional
)
1572 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1575 dl_opts_put(nlh
, dl
);
1579 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1581 struct dl_opts
*opts
= &dl
->opts
;
1582 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1583 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1584 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1585 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1587 if (opts
->present
& DL_OPT_HANDLE
&&
1588 attr_bus_name
&& attr_dev_name
) {
1589 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1590 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1592 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1593 strcmp(dev_name
, opts
->dev_name
) != 0)
1596 if (opts
->present
& DL_OPT_HANDLEP
&&
1597 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1598 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1599 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1600 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1602 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1603 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1604 port_index
!= opts
->port_index
)
1607 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1608 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1610 if (sb_index
!= opts
->sb_index
)
1616 static void cmd_dev_help(void)
1618 pr_err("Usage: devlink dev show [ DEV ]\n");
1619 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1620 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1621 pr_err(" [ encap { disable | enable } ]\n");
1622 pr_err(" devlink dev eswitch show DEV\n");
1623 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1624 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1625 pr_err(" devlink dev reload DEV\n");
1626 pr_err(" devlink dev info [ DEV ]\n");
1627 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1630 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1631 const char *dev_name
)
1633 if (!dl
->arr_last
.present
)
1635 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1636 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1639 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1640 const char *dev_name
)
1642 dl
->arr_last
.present
= true;
1643 free(dl
->arr_last
.dev_name
);
1644 free(dl
->arr_last
.bus_name
);
1645 dl
->arr_last
.bus_name
= strdup(bus_name
);
1646 dl
->arr_last
.dev_name
= strdup(dev_name
);
1649 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1650 const char *dev_name
)
1652 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1655 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1656 const char *dev_name
)
1658 return dl
->arr_last
.present
&&
1659 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1662 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1663 bool content
, bool array
)
1665 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1666 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1669 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1671 if (dl
->json_output
) {
1673 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1674 jsonw_end_array(dl
->jw
);
1675 if (should_arr_last_handle_start(dl
, bus_name
,
1677 jsonw_name(dl
->jw
, buf
);
1678 jsonw_start_array(dl
->jw
);
1679 jsonw_start_object(dl
->jw
);
1680 arr_last_handle_set(dl
, bus_name
, dev_name
);
1682 jsonw_start_object(dl
->jw
);
1685 jsonw_name(dl
->jw
, buf
);
1686 jsonw_start_object(dl
->jw
);
1690 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1691 __pr_out_indent_dec();
1692 if (should_arr_last_handle_start(dl
, bus_name
,
1694 pr_out("%s%s", buf
, content
? ":" : "");
1696 __pr_out_indent_inc();
1697 arr_last_handle_set(dl
, bus_name
, dev_name
);
1700 pr_out("%s%s", buf
, content
? ":" : "");
1705 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1707 __pr_out_handle_start(dl
, tb
, true, true);
1710 static void pr_out_handle_end(struct dl
*dl
)
1712 if (dl
->json_output
)
1713 jsonw_end_object(dl
->jw
);
1718 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1720 __pr_out_handle_start(dl
, tb
, false, false);
1721 pr_out_handle_end(dl
);
1724 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1725 const char *dev_name
, uint32_t port_index
)
1727 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1728 dl
->arr_last
.port_index
== port_index
;
1731 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1732 const char *dev_name
, uint32_t port_index
)
1734 arr_last_handle_set(dl
, bus_name
, dev_name
);
1735 dl
->arr_last
.port_index
= port_index
;
1738 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1739 const char *bus_name
,
1740 const char *dev_name
,
1741 uint32_t port_index
)
1743 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1746 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1747 const char *bus_name
,
1748 const char *dev_name
,
1749 uint32_t port_index
)
1751 return dl
->arr_last
.present
&&
1752 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1755 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1756 const char *dev_name
,
1757 uint32_t port_index
, bool try_nice
,
1760 static char buf
[64];
1761 char *ifname
= NULL
;
1763 if (dl
->no_nice_names
|| !try_nice
||
1764 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1765 port_index
, &ifname
) != 0)
1766 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1768 sprintf(buf
, "%s", ifname
);
1770 if (dl
->json_output
) {
1772 if (should_arr_last_port_handle_end(dl
, bus_name
,
1775 jsonw_end_array(dl
->jw
);
1776 if (should_arr_last_port_handle_start(dl
, bus_name
,
1779 jsonw_name(dl
->jw
, buf
);
1780 jsonw_start_array(dl
->jw
);
1781 jsonw_start_object(dl
->jw
);
1782 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1785 jsonw_start_object(dl
->jw
);
1788 jsonw_name(dl
->jw
, buf
);
1789 jsonw_start_object(dl
->jw
);
1796 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1798 const char *bus_name
;
1799 const char *dev_name
;
1800 uint32_t port_index
;
1802 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1803 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1804 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1805 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1808 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1810 const char *bus_name
;
1811 const char *dev_name
;
1812 uint32_t port_index
;
1814 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1815 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1816 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1817 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1820 static void pr_out_port_handle_end(struct dl
*dl
)
1822 if (dl
->json_output
)
1823 jsonw_end_object(dl
->jw
);
1829 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1831 if (dl
->json_output
) {
1832 jsonw_string_field(dl
->jw
, name
, val
);
1834 if (g_indent_newline
)
1835 pr_out("%s %s", name
, val
);
1837 pr_out(" %s %s", name
, val
);
1841 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1843 if (dl
->json_output
)
1844 jsonw_bool_field(dl
->jw
, name
, val
);
1846 pr_out_str(dl
, name
, val
? "true" : "false");
1849 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1851 if (dl
->json_output
) {
1852 jsonw_uint_field(dl
->jw
, name
, val
);
1854 if (g_indent_newline
)
1855 pr_out("%s %u", name
, val
);
1857 pr_out(" %s %u", name
, val
);
1861 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1863 if (val
== (uint64_t) -1)
1864 return pr_out_str(dl
, name
, "unlimited");
1866 if (dl
->json_output
) {
1867 jsonw_u64_field(dl
->jw
, name
, val
);
1869 if (g_indent_newline
)
1870 pr_out("%s %"PRIu64
, name
, val
);
1872 pr_out(" %s %"PRIu64
, name
, val
);
1876 static void pr_out_bool_value(struct dl
*dl
, bool value
)
1878 if (dl
->json_output
)
1879 jsonw_bool(dl
->jw
, value
);
1881 pr_out(" %s", value
? "true" : "false");
1884 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
1886 if (dl
->json_output
)
1887 jsonw_uint(dl
->jw
, value
);
1889 pr_out(" %u", value
);
1892 static void pr_out_uint64_value(struct dl
*dl
, uint64_t value
)
1894 if (dl
->json_output
)
1895 jsonw_u64(dl
->jw
, value
);
1897 pr_out(" %"PRIu64
, value
);
1900 static bool is_binary_eol(int i
)
1905 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1910 if (dl
->json_output
)
1911 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1913 pr_out("%02x ", data
[i
]);
1915 if (!dl
->json_output
&& is_binary_eol(i
))
1918 if (!dl
->json_output
&& !is_binary_eol(i
))
1922 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1924 if (dl
->json_output
)
1925 jsonw_string(dl
->jw
, value
);
1927 pr_out(" %s", value
);
1930 static void pr_out_name(struct dl
*dl
, const char *name
)
1932 if (dl
->json_output
)
1933 jsonw_name(dl
->jw
, name
);
1935 pr_out(" %s:", name
);
1938 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1940 if (dl
->json_output
) {
1941 jsonw_name(dl
->jw
, "address");
1942 jsonw_uint(dl
->jw
, addr
);
1943 jsonw_name(dl
->jw
, "data");
1944 jsonw_start_array(dl
->jw
);
1948 static void pr_out_region_chunk_end(struct dl
*dl
)
1950 if (dl
->json_output
)
1951 jsonw_end_array(dl
->jw
);
1954 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1957 static uint64_t align_val
;
1960 pr_out_region_chunk_start(dl
, addr
);
1962 if (!dl
->json_output
)
1963 if (!(align_val
% 16))
1964 pr_out("%s%016"PRIx64
" ",
1965 align_val
? "\n" : "",
1970 if (dl
->json_output
)
1971 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1973 pr_out("%02x ", data
[i
]);
1978 pr_out_region_chunk_end(dl
);
1981 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1983 if (dl
->json_output
) {
1984 jsonw_start_object(dl
->jw
);
1985 jsonw_name(dl
->jw
, name
);
1986 jsonw_start_object(dl
->jw
);
1990 static void pr_out_section_end(struct dl
*dl
)
1992 if (dl
->json_output
) {
1993 if (dl
->arr_last
.present
)
1994 jsonw_end_array(dl
->jw
);
1995 jsonw_end_object(dl
->jw
);
1996 jsonw_end_object(dl
->jw
);
2000 static void pr_out_array_start(struct dl
*dl
, const char *name
)
2002 if (dl
->json_output
) {
2003 jsonw_name(dl
->jw
, name
);
2004 jsonw_start_array(dl
->jw
);
2006 __pr_out_indent_inc();
2008 pr_out("%s:", name
);
2009 __pr_out_indent_inc();
2014 static void pr_out_array_end(struct dl
*dl
)
2016 if (dl
->json_output
) {
2017 jsonw_end_array(dl
->jw
);
2019 __pr_out_indent_dec();
2020 __pr_out_indent_dec();
2024 static void pr_out_object_start(struct dl
*dl
, const char *name
)
2026 if (dl
->json_output
) {
2027 jsonw_name(dl
->jw
, name
);
2028 jsonw_start_object(dl
->jw
);
2030 __pr_out_indent_inc();
2032 pr_out("%s:", name
);
2033 __pr_out_indent_inc();
2038 static void pr_out_object_end(struct dl
*dl
)
2040 if (dl
->json_output
) {
2041 jsonw_end_object(dl
->jw
);
2043 __pr_out_indent_dec();
2044 __pr_out_indent_dec();
2048 static void pr_out_entry_start(struct dl
*dl
)
2050 if (dl
->json_output
)
2051 jsonw_start_object(dl
->jw
);
2054 static void pr_out_entry_end(struct dl
*dl
)
2056 if (dl
->json_output
)
2057 jsonw_end_object(dl
->jw
);
2062 static void pr_out_stats(struct dl
*dl
, struct nlattr
*nla_stats
)
2064 struct nlattr
*tb
[DEVLINK_ATTR_STATS_MAX
+ 1] = {};
2070 err
= mnl_attr_parse_nested(nla_stats
, attr_stats_cb
, tb
);
2071 if (err
!= MNL_CB_OK
)
2074 pr_out_object_start(dl
, "stats");
2075 pr_out_object_start(dl
, "rx");
2076 if (tb
[DEVLINK_ATTR_STATS_RX_BYTES
])
2077 pr_out_u64(dl
, "bytes",
2078 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_BYTES
]));
2079 if (tb
[DEVLINK_ATTR_STATS_RX_PACKETS
])
2080 pr_out_u64(dl
, "packets",
2081 mnl_attr_get_u64(tb
[DEVLINK_ATTR_STATS_RX_PACKETS
]));
2082 pr_out_object_end(dl
);
2083 pr_out_object_end(dl
);
2086 static const char *param_cmode_name(uint8_t cmode
)
2089 case DEVLINK_PARAM_CMODE_RUNTIME
:
2090 return PARAM_CMODE_RUNTIME_STR
;
2091 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
2092 return PARAM_CMODE_DRIVERINIT_STR
;
2093 case DEVLINK_PARAM_CMODE_PERMANENT
:
2094 return PARAM_CMODE_PERMANENT_STR
;
2095 default: return "<unknown type>";
2099 static const char *eswitch_mode_name(uint32_t mode
)
2102 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
2103 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
2104 default: return "<unknown mode>";
2108 static const char *eswitch_inline_mode_name(uint32_t mode
)
2111 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
2112 return ESWITCH_INLINE_MODE_NONE
;
2113 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
2114 return ESWITCH_INLINE_MODE_LINK
;
2115 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
2116 return ESWITCH_INLINE_MODE_NETWORK
;
2117 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
2118 return ESWITCH_INLINE_MODE_TRANSPORT
;
2120 return "<unknown mode>";
2124 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
2126 __pr_out_handle_start(dl
, tb
, true, false);
2128 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
2129 pr_out_str(dl
, "mode",
2130 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
2132 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
2133 pr_out_str(dl
, "inline-mode",
2134 eswitch_inline_mode_name(mnl_attr_get_u8(
2135 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2137 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2138 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2140 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
2143 pr_out_handle_end(dl
);
2146 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2148 struct dl
*dl
= data
;
2149 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2150 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2152 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2153 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2154 return MNL_CB_ERROR
;
2155 pr_out_eswitch(dl
, tb
);
2159 static int cmd_dev_eswitch_show(struct dl
*dl
)
2161 struct nlmsghdr
*nlh
;
2164 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2165 NLM_F_REQUEST
| NLM_F_ACK
);
2167 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2171 pr_out_section_start(dl
, "dev");
2172 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2173 pr_out_section_end(dl
);
2177 static int cmd_dev_eswitch_set(struct dl
*dl
)
2179 struct nlmsghdr
*nlh
;
2182 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2183 NLM_F_REQUEST
| NLM_F_ACK
);
2185 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2186 DL_OPT_ESWITCH_MODE
|
2187 DL_OPT_ESWITCH_INLINE_MODE
|
2188 DL_OPT_ESWITCH_ENCAP_MODE
);
2193 if (dl
->opts
.present
== 1) {
2194 pr_err("Need to set at least one option\n");
2198 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2201 static int cmd_dev_eswitch(struct dl
*dl
)
2203 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2206 } else if (dl_argv_match(dl
, "set")) {
2208 return cmd_dev_eswitch_set(dl
);
2209 } else if (dl_argv_match(dl
, "show")) {
2211 return cmd_dev_eswitch_show(dl
);
2213 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2217 struct param_val_conv
{
2223 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2224 uint32_t len
, const char *name
)
2228 for (i
= 0; i
< len
; i
++)
2229 if (!strcmp(param_val_conv
[i
].name
, name
))
2236 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2237 uint32_t len
, const char *name
, const char *vstr
,
2242 for (i
= 0; i
< len
; i
++)
2243 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2244 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2245 *vuint
= param_val_conv
[i
].vuint
;
2253 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2254 uint32_t len
, const char *name
, uint32_t vuint
,
2259 for (i
= 0; i
< len
; i
++)
2260 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2261 param_val_conv
[i
].vuint
== vuint
) {
2262 *vstr
= param_val_conv
[i
].vstr
;
2269 static const struct param_val_conv param_val_conv
[] = {
2271 .name
= "fw_load_policy",
2273 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2276 .name
= "fw_load_policy",
2278 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2281 .name
= "reset_dev_on_drv_probe",
2283 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_UNKNOWN
,
2286 .name
= "fw_load_policy",
2288 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_UNKNOWN
,
2291 .name
= "reset_dev_on_drv_probe",
2293 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_ALWAYS
,
2296 .name
= "reset_dev_on_drv_probe",
2298 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_NEVER
,
2301 .name
= "reset_dev_on_drv_probe",
2303 .vuint
= DEVLINK_PARAM_RESET_DEV_ON_DRV_PROBE_VALUE_DISK
,
2307 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2309 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2310 int nla_type
, struct nlattr
*nl
)
2312 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2313 struct nlattr
*val_attr
;
2318 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2319 if (err
!= MNL_CB_OK
)
2322 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2323 (nla_type
!= MNL_TYPE_FLAG
&&
2324 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2327 pr_out_str(dl
, "cmode",
2328 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2329 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2331 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2337 err
= param_val_conv_str_get(param_val_conv
,
2340 mnl_attr_get_u8(val_attr
),
2344 pr_out_str(dl
, "value", vstr
);
2346 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2351 err
= param_val_conv_str_get(param_val_conv
,
2354 mnl_attr_get_u16(val_attr
),
2358 pr_out_str(dl
, "value", vstr
);
2360 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2365 err
= param_val_conv_str_get(param_val_conv
,
2368 mnl_attr_get_u32(val_attr
),
2372 pr_out_str(dl
, "value", vstr
);
2374 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2377 case MNL_TYPE_STRING
:
2378 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2381 pr_out_bool(dl
, "value", val_attr
? true : false);
2386 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2388 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2389 struct nlattr
*param_value_attr
;
2390 const char *nla_name
;
2394 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2395 if (err
!= MNL_CB_OK
)
2397 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2398 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2399 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2403 pr_out_handle_start_arr(dl
, tb
);
2405 __pr_out_handle_start(dl
, tb
, true, false);
2407 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2409 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2410 pr_out_str(dl
, "name", nla_name
);
2412 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2413 pr_out_str(dl
, "type", "driver-specific");
2415 pr_out_str(dl
, "type", "generic");
2417 pr_out_array_start(dl
, "values");
2418 mnl_attr_for_each_nested(param_value_attr
,
2419 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2420 pr_out_entry_start(dl
);
2421 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2422 pr_out_entry_end(dl
);
2424 pr_out_array_end(dl
);
2425 pr_out_handle_end(dl
);
2428 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2430 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2431 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2432 struct dl
*dl
= data
;
2434 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2435 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2436 !tb
[DEVLINK_ATTR_PARAM
])
2437 return MNL_CB_ERROR
;
2438 pr_out_param(dl
, tb
, true);
2454 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2456 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2457 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2458 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2459 struct nlattr
*param_value_attr
;
2460 enum devlink_param_cmode cmode
;
2461 struct param_ctx
*ctx
= data
;
2462 struct dl
*dl
= ctx
->dl
;
2466 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2467 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2468 !tb
[DEVLINK_ATTR_PARAM
])
2469 return MNL_CB_ERROR
;
2471 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2472 if (err
!= MNL_CB_OK
)
2473 return MNL_CB_ERROR
;
2475 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2476 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2477 return MNL_CB_ERROR
;
2479 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2480 mnl_attr_for_each_nested(param_value_attr
,
2481 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2482 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2483 struct nlattr
*val_attr
;
2485 err
= mnl_attr_parse_nested(param_value_attr
,
2486 attr_cb
, nla_value
);
2487 if (err
!= MNL_CB_OK
)
2488 return MNL_CB_ERROR
;
2490 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2491 (nla_type
!= MNL_TYPE_FLAG
&&
2492 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2493 return MNL_CB_ERROR
;
2495 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2496 if (cmode
== dl
->opts
.cmode
) {
2497 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2500 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2503 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2506 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2508 case MNL_TYPE_STRING
:
2509 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2512 ctx
->value
.vbool
= val_attr
? true : false;
2518 ctx
->nla_type
= nla_type
;
2522 static int cmd_dev_param_set(struct dl
*dl
)
2524 struct param_ctx ctx
= {};
2525 struct nlmsghdr
*nlh
;
2533 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2535 DL_OPT_PARAM_VALUE
|
2536 DL_OPT_PARAM_CMODE
, 0);
2540 /* Get value type */
2541 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2542 NLM_F_REQUEST
| NLM_F_ACK
);
2543 dl_opts_put(nlh
, dl
);
2546 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2550 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2551 NLM_F_REQUEST
| NLM_F_ACK
);
2552 dl_opts_put(nlh
, dl
);
2554 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2555 dl
->opts
.param_name
);
2557 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2558 switch (ctx
.nla_type
) {
2561 err
= param_val_conv_uint_get(param_val_conv
,
2563 dl
->opts
.param_name
,
2564 dl
->opts
.param_value
,
2568 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2571 goto err_param_value_parse
;
2572 if (val_u8
== ctx
.value
.vu8
)
2574 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2578 err
= param_val_conv_uint_get(param_val_conv
,
2580 dl
->opts
.param_name
,
2581 dl
->opts
.param_value
,
2585 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2588 goto err_param_value_parse
;
2589 if (val_u16
== ctx
.value
.vu16
)
2591 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2595 err
= param_val_conv_uint_get(param_val_conv
,
2597 dl
->opts
.param_name
,
2598 dl
->opts
.param_value
,
2601 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2603 goto err_param_value_parse
;
2604 if (val_u32
== ctx
.value
.vu32
)
2606 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2609 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2611 goto err_param_value_parse
;
2612 if (val_bool
== ctx
.value
.vbool
)
2615 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2618 case MNL_TYPE_STRING
:
2619 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2620 dl
->opts
.param_value
);
2621 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2625 printf("Value type not supported\n");
2628 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2630 err_param_value_parse
:
2631 pr_err("Value \"%s\" is not a number or not within range\n",
2632 dl
->opts
.param_value
);
2636 static int cmd_dev_param_show(struct dl
*dl
)
2638 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2639 struct nlmsghdr
*nlh
;
2642 if (dl_argc(dl
) == 0)
2643 flags
|= NLM_F_DUMP
;
2645 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2647 if (dl_argc(dl
) > 0) {
2648 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2649 DL_OPT_PARAM_NAME
, 0);
2654 pr_out_section_start(dl
, "param");
2655 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2656 pr_out_section_end(dl
);
2660 static int cmd_dev_param(struct dl
*dl
)
2662 if (dl_argv_match(dl
, "help")) {
2665 } else if (dl_argv_match(dl
, "show") ||
2666 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2668 return cmd_dev_param_show(dl
);
2669 } else if (dl_argv_match(dl
, "set")) {
2671 return cmd_dev_param_set(dl
);
2673 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2676 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2678 struct dl
*dl
= data
;
2679 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2680 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2681 uint8_t reload_failed
= 0;
2683 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2684 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2685 return MNL_CB_ERROR
;
2687 if (tb
[DEVLINK_ATTR_RELOAD_FAILED
])
2688 reload_failed
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_RELOAD_FAILED
]);
2690 if (reload_failed
) {
2691 __pr_out_handle_start(dl
, tb
, true, false);
2692 pr_out_bool(dl
, "reload_failed", true);
2693 pr_out_handle_end(dl
);
2695 pr_out_handle(dl
, tb
);
2701 static int cmd_dev_show(struct dl
*dl
)
2703 struct nlmsghdr
*nlh
;
2704 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2707 if (dl_argc(dl
) == 0)
2708 flags
|= NLM_F_DUMP
;
2710 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2712 if (dl_argc(dl
) > 0) {
2713 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2718 pr_out_section_start(dl
, "dev");
2719 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2720 pr_out_section_end(dl
);
2724 static void cmd_dev_reload_help(void)
2726 pr_err("Usage: devlink dev reload [ DEV ]\n");
2729 static int cmd_dev_reload(struct dl
*dl
)
2731 struct nlmsghdr
*nlh
;
2734 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2735 cmd_dev_reload_help();
2739 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2740 NLM_F_REQUEST
| NLM_F_ACK
);
2742 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2746 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2749 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2750 const char *name
, int type
)
2752 struct nlattr
*version
;
2754 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2755 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2756 const char *ver_value
;
2757 const char *ver_name
;
2760 if (mnl_attr_get_type(version
) != type
)
2763 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2764 if (err
!= MNL_CB_OK
)
2767 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2768 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2772 pr_out_object_start(dl
, name
);
2776 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2777 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2779 pr_out_str(dl
, ver_name
, ver_value
);
2780 if (!dl
->json_output
)
2785 pr_out_object_end(dl
);
2788 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2789 struct nlattr
**tb
, bool has_versions
)
2791 __pr_out_handle_start(dl
, tb
, true, false);
2793 __pr_out_indent_inc();
2794 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2795 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2797 if (!dl
->json_output
)
2799 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2802 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2803 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2805 if (!dl
->json_output
)
2807 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2809 __pr_out_indent_dec();
2812 pr_out_object_start(dl
, "versions");
2814 pr_out_versions_single(dl
, nlh
, "fixed",
2815 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2816 pr_out_versions_single(dl
, nlh
, "running",
2817 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2818 pr_out_versions_single(dl
, nlh
, "stored",
2819 DEVLINK_ATTR_INFO_VERSION_STORED
);
2821 pr_out_object_end(dl
);
2824 pr_out_handle_end(dl
);
2827 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2829 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2830 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2831 bool has_versions
, has_info
;
2832 struct dl
*dl
= data
;
2834 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2836 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2837 return MNL_CB_ERROR
;
2839 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2840 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2841 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2842 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2843 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2847 pr_out_info(dl
, nlh
, tb
, has_versions
);
2852 static void cmd_dev_info_help(void)
2854 pr_err("Usage: devlink dev info [ DEV ]\n");
2857 static int cmd_dev_info(struct dl
*dl
)
2859 struct nlmsghdr
*nlh
;
2860 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2863 if (dl_argv_match(dl
, "help")) {
2864 cmd_dev_info_help();
2868 if (dl_argc(dl
) == 0)
2869 flags
|= NLM_F_DUMP
;
2871 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2873 if (dl_argc(dl
) > 0) {
2874 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2879 pr_out_section_start(dl
, "info");
2880 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2881 pr_out_section_end(dl
);
2885 static void cmd_dev_flash_help(void)
2887 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2891 struct cmd_dev_flash_status_ctx
{
2894 char *last_component
;
2895 uint8_t not_first
:1,
2901 static int nullstrcmp(const char *str1
, const char *str2
)
2904 return strcmp(str1
, str2
);
2907 return str1
? 1 : -1;
2910 static int cmd_dev_flash_status_cb(const struct nlmsghdr
*nlh
, void *data
)
2912 struct cmd_dev_flash_status_ctx
*ctx
= data
;
2913 struct dl_opts
*opts
= &ctx
->dl
->opts
;
2914 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2915 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2916 const char *component
= NULL
;
2917 uint64_t done
= 0, total
= 0;
2918 const char *msg
= NULL
;
2919 const char *bus_name
;
2920 const char *dev_name
;
2922 if (genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
&&
2923 genl
->cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
)
2926 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2927 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2928 return MNL_CB_ERROR
;
2929 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
2930 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
2931 if (strcmp(bus_name
, opts
->bus_name
) ||
2932 strcmp(dev_name
, opts
->dev_name
))
2933 return MNL_CB_ERROR
;
2935 if (genl
->cmd
== DEVLINK_CMD_FLASH_UPDATE_END
&& ctx
->not_first
) {
2937 free(ctx
->last_msg
);
2938 free(ctx
->last_component
);
2939 ctx
->received_end
= 1;
2943 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
2944 msg
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]);
2945 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
2946 component
= mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]);
2947 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
2948 done
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]);
2949 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
2950 total
= mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]);
2952 if (!nullstrcmp(msg
, ctx
->last_msg
) &&
2953 !nullstrcmp(component
, ctx
->last_component
) &&
2954 ctx
->last_pc
&& ctx
->not_first
) {
2955 pr_out_tty("\b\b\b\b\b"); /* clean percentage */
2960 pr_out("[%s] ", component
);
2961 free(ctx
->last_component
);
2962 ctx
->last_component
= strdup(component
);
2966 free(ctx
->last_msg
);
2967 ctx
->last_msg
= strdup(msg
);
2971 pr_out_tty(" %3lu%%", (done
* 100) / total
);
2982 static int cmd_dev_flash_fds_process(struct cmd_dev_flash_status_ctx
*ctx
,
2983 struct mnlg_socket
*nlg_ntf
,
2986 int nlfd
= mnlg_socket_get_fd(nlg_ntf
);
2993 for (i
= 0; i
< 3; i
++)
2995 FD_SET(pipe_r
, &fds
[0]);
2997 FD_SET(nlfd
, &fds
[0]);
3001 while (select(fdmax
, &fds
[0], &fds
[1], &fds
[2], NULL
) < 0) {
3004 pr_err("select() failed\n");
3007 if (FD_ISSET(nlfd
, &fds
[0])) {
3008 err
= _mnlg_socket_recv_run(nlg_ntf
,
3009 cmd_dev_flash_status_cb
, ctx
);
3013 if (FD_ISSET(pipe_r
, &fds
[0])) {
3014 err
= read(pipe_r
, &err2
, sizeof(err2
));
3016 pr_err("Failed to read pipe\n");
3021 ctx
->flash_done
= 1;
3027 static int cmd_dev_flash(struct dl
*dl
)
3029 struct cmd_dev_flash_status_ctx ctx
= {.dl
= dl
,};
3030 struct mnlg_socket
*nlg_ntf
;
3031 struct nlmsghdr
*nlh
;
3037 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3038 cmd_dev_flash_help();
3042 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
3043 NLM_F_REQUEST
| NLM_F_ACK
);
3045 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
3046 DL_OPT_FLASH_COMPONENT
);
3050 nlg_ntf
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3054 err
= _mnlg_socket_group_add(nlg_ntf
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3058 err
= pipe(pipe_fds
);
3061 pipe_r
= pipe_fds
[0];
3062 pipe_w
= pipe_fds
[1];
3070 /* In child, just execute the flash and pass returned
3071 * value through pipe once it is done.
3074 err
= _mnlg_socket_send(dl
->nlg
, nlh
);
3075 write(pipe_w
, &err
, sizeof(err
));
3082 err
= cmd_dev_flash_fds_process(&ctx
, nlg_ntf
, pipe_r
);
3085 } while (!ctx
.flash_done
|| (ctx
.not_first
&& !ctx
.received_end
));
3087 err
= _mnlg_socket_recv_run(dl
->nlg
, NULL
, NULL
);
3090 mnlg_socket_close(nlg_ntf
);
3094 static int cmd_dev(struct dl
*dl
)
3096 if (dl_argv_match(dl
, "help")) {
3099 } else if (dl_argv_match(dl
, "show") ||
3100 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3102 return cmd_dev_show(dl
);
3103 } else if (dl_argv_match(dl
, "eswitch")) {
3105 return cmd_dev_eswitch(dl
);
3106 } else if (dl_argv_match(dl
, "reload")) {
3108 return cmd_dev_reload(dl
);
3109 } else if (dl_argv_match(dl
, "param")) {
3111 return cmd_dev_param(dl
);
3112 } else if (dl_argv_match(dl
, "info")) {
3114 return cmd_dev_info(dl
);
3115 } else if (dl_argv_match(dl
, "flash")) {
3117 return cmd_dev_flash(dl
);
3119 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3123 static void cmd_port_help(void)
3125 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
3126 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
3127 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
3128 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
3131 static const char *port_type_name(uint32_t type
)
3134 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
3135 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
3136 case DEVLINK_PORT_TYPE_ETH
: return "eth";
3137 case DEVLINK_PORT_TYPE_IB
: return "ib";
3138 default: return "<unknown type>";
3142 static const char *port_flavour_name(uint16_t flavour
)
3145 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
3147 case DEVLINK_PORT_FLAVOUR_CPU
:
3149 case DEVLINK_PORT_FLAVOUR_DSA
:
3151 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3153 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3156 return "<unknown flavour>";
3160 static void pr_out_port_pfvf_num(struct dl
*dl
, struct nlattr
**tb
)
3164 if (tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]) {
3165 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_PF_NUMBER
]);
3166 pr_out_uint(dl
, "pfnum", fn_num
);
3168 if (tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]) {
3169 fn_num
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_PCI_VF_NUMBER
]);
3170 pr_out_uint(dl
, "vfnum", fn_num
);
3174 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
3176 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
3177 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
3179 pr_out_port_handle_start(dl
, tb
, false);
3181 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
3183 pr_out_str(dl
, "type", port_type_name(port_type
));
3185 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
3187 if (port_type
!= des_port_type
)
3188 pr_out_str(dl
, "des_type",
3189 port_type_name(des_port_type
));
3192 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
3193 pr_out_str(dl
, "netdev",
3194 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
3195 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
3196 pr_out_str(dl
, "ibdev",
3197 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
3198 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
3199 uint16_t port_flavour
=
3200 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
3202 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
3204 switch (port_flavour
) {
3205 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
3206 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
3207 pr_out_port_pfvf_num(dl
, tb
);
3213 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
3214 uint32_t port_number
;
3216 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
3217 pr_out_uint(dl
, "port", port_number
);
3219 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
3220 pr_out_uint(dl
, "split_group",
3221 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
3222 pr_out_port_handle_end(dl
);
3225 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3227 struct dl
*dl
= data
;
3228 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3229 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3231 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3232 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3233 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3234 return MNL_CB_ERROR
;
3235 pr_out_port(dl
, tb
);
3239 static int cmd_port_show(struct dl
*dl
)
3241 struct nlmsghdr
*nlh
;
3242 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3245 if (dl_argc(dl
) == 0)
3246 flags
|= NLM_F_DUMP
;
3248 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
3250 if (dl_argc(dl
) > 0) {
3251 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3256 pr_out_section_start(dl
, "port");
3257 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
3258 pr_out_section_end(dl
);
3262 static int cmd_port_set(struct dl
*dl
)
3264 struct nlmsghdr
*nlh
;
3267 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
3268 NLM_F_REQUEST
| NLM_F_ACK
);
3270 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
3274 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3277 static int cmd_port_split(struct dl
*dl
)
3279 struct nlmsghdr
*nlh
;
3282 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
3283 NLM_F_REQUEST
| NLM_F_ACK
);
3285 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
3289 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3292 static int cmd_port_unsplit(struct dl
*dl
)
3294 struct nlmsghdr
*nlh
;
3297 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
3298 NLM_F_REQUEST
| NLM_F_ACK
);
3300 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
3304 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3307 static int cmd_port(struct dl
*dl
)
3309 if (dl_argv_match(dl
, "help")) {
3312 } else if (dl_argv_match(dl
, "show") ||
3313 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3315 return cmd_port_show(dl
);
3316 } else if (dl_argv_match(dl
, "set")) {
3318 return cmd_port_set(dl
);
3319 } else if (dl_argv_match(dl
, "split")) {
3321 return cmd_port_split(dl
);
3322 } else if (dl_argv_match(dl
, "unsplit")) {
3324 return cmd_port_unsplit(dl
);
3326 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3330 static void cmd_sb_help(void)
3332 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
3333 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
3334 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
3335 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
3336 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3337 pr_err(" pool POOL_INDEX ]\n");
3338 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
3339 pr_err(" pool POOL_INDEX th THRESHOLD\n");
3340 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3341 pr_err(" type { ingress | egress } ]\n");
3342 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
3343 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
3344 pr_err(" th THRESHOLD\n");
3345 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
3346 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
3347 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
3350 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
3352 pr_out_handle_start_arr(dl
, tb
);
3353 pr_out_uint(dl
, "sb",
3354 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3355 pr_out_uint(dl
, "size",
3356 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
3357 pr_out_uint(dl
, "ing_pools",
3358 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
3359 pr_out_uint(dl
, "eg_pools",
3360 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
3361 pr_out_uint(dl
, "ing_tcs",
3362 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
3363 pr_out_uint(dl
, "eg_tcs",
3364 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
3365 pr_out_handle_end(dl
);
3368 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3370 struct dl
*dl
= data
;
3371 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3372 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3374 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3375 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3376 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
3377 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
3378 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
3379 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
3380 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3381 return MNL_CB_ERROR
;
3386 static int cmd_sb_show(struct dl
*dl
)
3388 struct nlmsghdr
*nlh
;
3389 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3392 if (dl_argc(dl
) == 0)
3393 flags
|= NLM_F_DUMP
;
3395 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3397 if (dl_argc(dl
) > 0) {
3398 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3403 pr_out_section_start(dl
, "sb");
3404 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3405 pr_out_section_end(dl
);
3409 static const char *pool_type_name(uint8_t type
)
3412 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3413 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3414 default: return "<unknown type>";
3418 static const char *threshold_type_name(uint8_t type
)
3421 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3422 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3423 default: return "<unknown type>";
3427 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3429 pr_out_handle_start_arr(dl
, tb
);
3430 pr_out_uint(dl
, "sb",
3431 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3432 pr_out_uint(dl
, "pool",
3433 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3434 pr_out_str(dl
, "type",
3435 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3436 pr_out_uint(dl
, "size",
3437 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3438 pr_out_str(dl
, "thtype",
3439 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3440 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3441 pr_out_uint(dl
, "cell_size",
3442 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3443 pr_out_handle_end(dl
);
3446 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3448 struct dl
*dl
= data
;
3449 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3450 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3452 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3453 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3454 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3455 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3456 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3457 return MNL_CB_ERROR
;
3458 pr_out_sb_pool(dl
, tb
);
3462 static int cmd_sb_pool_show(struct dl
*dl
)
3464 struct nlmsghdr
*nlh
;
3465 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3468 if (dl_argc(dl
) == 0)
3469 flags
|= NLM_F_DUMP
;
3471 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3473 if (dl_argc(dl
) > 0) {
3474 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3480 pr_out_section_start(dl
, "pool");
3481 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3482 pr_out_section_end(dl
);
3486 static int cmd_sb_pool_set(struct dl
*dl
)
3488 struct nlmsghdr
*nlh
;
3491 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3492 NLM_F_REQUEST
| NLM_F_ACK
);
3494 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3495 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3499 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3502 static int cmd_sb_pool(struct dl
*dl
)
3504 if (dl_argv_match(dl
, "help")) {
3507 } else if (dl_argv_match(dl
, "show") ||
3508 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3510 return cmd_sb_pool_show(dl
);
3511 } else if (dl_argv_match(dl
, "set")) {
3513 return cmd_sb_pool_set(dl
);
3515 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3519 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3521 pr_out_port_handle_start_arr(dl
, tb
, true);
3522 pr_out_uint(dl
, "sb",
3523 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3524 pr_out_uint(dl
, "pool",
3525 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3526 pr_out_uint(dl
, "threshold",
3527 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3528 pr_out_port_handle_end(dl
);
3531 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3533 struct dl
*dl
= data
;
3534 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3535 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3537 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3538 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3539 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3540 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3541 return MNL_CB_ERROR
;
3542 pr_out_sb_port_pool(dl
, tb
);
3546 static int cmd_sb_port_pool_show(struct dl
*dl
)
3548 struct nlmsghdr
*nlh
;
3549 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3552 if (dl_argc(dl
) == 0)
3553 flags
|= NLM_F_DUMP
;
3555 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3557 if (dl_argc(dl
) > 0) {
3558 err
= dl_argv_parse_put(nlh
, dl
,
3559 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3565 pr_out_section_start(dl
, "port_pool");
3566 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3567 pr_out_section_end(dl
);
3571 static int cmd_sb_port_pool_set(struct dl
*dl
)
3573 struct nlmsghdr
*nlh
;
3576 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3577 NLM_F_REQUEST
| NLM_F_ACK
);
3579 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3580 DL_OPT_SB_TH
, DL_OPT_SB
);
3584 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3587 static int cmd_sb_port_pool(struct dl
*dl
)
3589 if (dl_argv_match(dl
, "help")) {
3592 } else if (dl_argv_match(dl
, "show") ||
3593 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3595 return cmd_sb_port_pool_show(dl
);
3596 } else if (dl_argv_match(dl
, "set")) {
3598 return cmd_sb_port_pool_set(dl
);
3600 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3604 static int cmd_sb_port(struct dl
*dl
)
3606 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3609 } else if (dl_argv_match(dl
, "pool")) {
3611 return cmd_sb_port_pool(dl
);
3613 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3617 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3619 pr_out_port_handle_start_arr(dl
, tb
, true);
3620 pr_out_uint(dl
, "sb",
3621 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3622 pr_out_uint(dl
, "tc",
3623 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3624 pr_out_str(dl
, "type",
3625 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3626 pr_out_uint(dl
, "pool",
3627 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3628 pr_out_uint(dl
, "threshold",
3629 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3630 pr_out_port_handle_end(dl
);
3633 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3635 struct dl
*dl
= data
;
3636 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3637 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3639 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3640 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3641 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3642 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3643 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3644 return MNL_CB_ERROR
;
3645 pr_out_sb_tc_bind(dl
, tb
);
3649 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3651 struct nlmsghdr
*nlh
;
3652 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3655 if (dl_argc(dl
) == 0)
3656 flags
|= NLM_F_DUMP
;
3658 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3660 if (dl_argc(dl
) > 0) {
3661 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3662 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3667 pr_out_section_start(dl
, "tc_bind");
3668 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3669 pr_out_section_end(dl
);
3673 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3675 struct nlmsghdr
*nlh
;
3678 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3679 NLM_F_REQUEST
| NLM_F_ACK
);
3681 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3682 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3687 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3690 static int cmd_sb_tc_bind(struct dl
*dl
)
3692 if (dl_argv_match(dl
, "help")) {
3695 } else if (dl_argv_match(dl
, "show") ||
3696 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3698 return cmd_sb_tc_bind_show(dl
);
3699 } else if (dl_argv_match(dl
, "set")) {
3701 return cmd_sb_tc_bind_set(dl
);
3703 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3707 static int cmd_sb_tc(struct dl
*dl
)
3709 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3712 } else if (dl_argv_match(dl
, "bind")) {
3714 return cmd_sb_tc_bind(dl
);
3716 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3721 struct list_head list
;
3725 uint32_t bound_pool_index
;
3729 struct list_head list
;
3732 uint32_t port_index
;
3734 struct list_head pool_list
;
3735 struct list_head ing_tc_list
;
3736 struct list_head eg_tc_list
;
3742 struct list_head port_list
;
3745 static struct occ_item
*occ_item_alloc(void)
3747 return calloc(1, sizeof(struct occ_item
));
3750 static void occ_item_free(struct occ_item
*occ_item
)
3755 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3757 struct occ_port
*occ_port
;
3759 occ_port
= calloc(1, sizeof(*occ_port
));
3762 occ_port
->port_index
= port_index
;
3763 INIT_LIST_HEAD(&occ_port
->pool_list
);
3764 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3765 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3769 static void occ_port_free(struct occ_port
*occ_port
)
3771 struct occ_item
*occ_item
, *tmp
;
3773 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3774 occ_item_free(occ_item
);
3775 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3776 occ_item_free(occ_item
);
3777 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3778 occ_item_free(occ_item
);
3781 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3783 struct occ_show
*occ_show
;
3785 occ_show
= calloc(1, sizeof(*occ_show
));
3789 INIT_LIST_HEAD(&occ_show
->port_list
);
3793 static void occ_show_free(struct occ_show
*occ_show
)
3795 struct occ_port
*occ_port
, *tmp
;
3797 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3798 occ_port_free(occ_port
);
3801 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3804 struct occ_port
*occ_port
;
3805 uint32_t port_index
;
3807 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3809 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3810 if (occ_port
->port_index
== port_index
)
3813 occ_port
= occ_port_alloc(port_index
);
3816 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3820 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3823 struct occ_item
*occ_item
;
3826 pr_out_sp(7, " %s:", label
);
3827 list_for_each_entry(occ_item
, list
, list
) {
3828 if ((i
- 1) % 4 == 0 && i
!= 1)
3831 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3832 occ_item
->bound_pool_index
);
3834 pr_out_sp(7, "%2u:", occ_item
->index
);
3835 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3839 if ((i
- 1) % 4 != 0)
3843 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3844 struct list_head
*list
,
3847 struct occ_item
*occ_item
;
3850 jsonw_name(dl
->jw
, label
);
3851 jsonw_start_object(dl
->jw
);
3852 list_for_each_entry(occ_item
, list
, list
) {
3853 sprintf(buf
, "%u", occ_item
->index
);
3854 jsonw_name(dl
->jw
, buf
);
3855 jsonw_start_object(dl
->jw
);
3857 jsonw_uint_field(dl
->jw
, "bound_pool",
3858 occ_item
->bound_pool_index
);
3859 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3860 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3861 jsonw_end_object(dl
->jw
);
3863 jsonw_end_object(dl
->jw
);
3866 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3868 if (dl
->json_output
) {
3869 pr_out_json_occ_show_item_list(dl
, "pool",
3870 &occ_port
->pool_list
, false);
3871 pr_out_json_occ_show_item_list(dl
, "itc",
3872 &occ_port
->ing_tc_list
, true);
3873 pr_out_json_occ_show_item_list(dl
, "etc",
3874 &occ_port
->eg_tc_list
, true);
3877 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3878 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3879 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3883 static void pr_out_occ_show(struct occ_show
*occ_show
)
3885 struct dl
*dl
= occ_show
->dl
;
3886 struct dl_opts
*opts
= &dl
->opts
;
3887 struct occ_port
*occ_port
;
3889 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3890 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3891 occ_port
->port_index
, true, false);
3892 pr_out_occ_show_port(dl
, occ_port
);
3893 pr_out_port_handle_end(dl
);
3897 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3900 struct occ_port
*occ_port
;
3901 struct occ_item
*occ_item
;
3903 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3906 occ_port
= occ_port_get(occ_show
, tb
);
3908 occ_show
->err
= -ENOMEM
;
3912 occ_item
= occ_item_alloc();
3914 occ_show
->err
= -ENOMEM
;
3917 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3918 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3919 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3920 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3923 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3925 struct occ_show
*occ_show
= data
;
3926 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3927 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3929 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3930 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3931 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3932 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3933 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3934 return MNL_CB_ERROR
;
3935 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3939 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3942 struct occ_port
*occ_port
;
3943 struct occ_item
*occ_item
;
3946 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3949 occ_port
= occ_port_get(occ_show
, tb
);
3951 occ_show
->err
= -ENOMEM
;
3955 occ_item
= occ_item_alloc();
3957 occ_show
->err
= -ENOMEM
;
3960 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3961 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3962 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3963 occ_item
->bound_pool_index
=
3964 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3965 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3966 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3967 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3968 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3969 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3971 occ_item_free(occ_item
);
3974 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3976 struct occ_show
*occ_show
= data
;
3977 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3978 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3980 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3981 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3982 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3983 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3984 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3985 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3986 return MNL_CB_ERROR
;
3987 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3991 static int cmd_sb_occ_show(struct dl
*dl
)
3993 struct nlmsghdr
*nlh
;
3994 struct occ_show
*occ_show
;
3995 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3998 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
4002 occ_show
= occ_show_alloc(dl
);
4006 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
4008 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4009 cmd_sb_occ_port_pool_process_cb
, occ_show
);
4013 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
4015 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
4016 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
4020 pr_out_section_start(dl
, "occupancy");
4021 pr_out_occ_show(occ_show
);
4022 pr_out_section_end(dl
);
4025 occ_show_free(occ_show
);
4029 static int cmd_sb_occ_snapshot(struct dl
*dl
)
4031 struct nlmsghdr
*nlh
;
4034 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
4035 NLM_F_REQUEST
| NLM_F_ACK
);
4037 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4041 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4044 static int cmd_sb_occ_clearmax(struct dl
*dl
)
4046 struct nlmsghdr
*nlh
;
4049 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
4050 NLM_F_REQUEST
| NLM_F_ACK
);
4052 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
4056 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4059 static int cmd_sb_occ(struct dl
*dl
)
4061 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4064 } else if (dl_argv_match(dl
, "show") ||
4065 dl_argv_match(dl
, "list")) {
4067 return cmd_sb_occ_show(dl
);
4068 } else if (dl_argv_match(dl
, "snapshot")) {
4070 return cmd_sb_occ_snapshot(dl
);
4071 } else if (dl_argv_match(dl
, "clearmax")) {
4073 return cmd_sb_occ_clearmax(dl
);
4075 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4079 static int cmd_sb(struct dl
*dl
)
4081 if (dl_argv_match(dl
, "help")) {
4084 } else if (dl_argv_match(dl
, "show") ||
4085 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
4087 return cmd_sb_show(dl
);
4088 } else if (dl_argv_match(dl
, "pool")) {
4090 return cmd_sb_pool(dl
);
4091 } else if (dl_argv_match(dl
, "port")) {
4093 return cmd_sb_port(dl
);
4094 } else if (dl_argv_match(dl
, "tc")) {
4096 return cmd_sb_tc(dl
);
4097 } else if (dl_argv_match(dl
, "occupancy")) {
4099 return cmd_sb_occ(dl
);
4101 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4105 static const char *cmd_name(uint8_t cmd
)
4108 case DEVLINK_CMD_UNSPEC
: return "unspec";
4109 case DEVLINK_CMD_GET
: return "get";
4110 case DEVLINK_CMD_SET
: return "set";
4111 case DEVLINK_CMD_NEW
: return "new";
4112 case DEVLINK_CMD_DEL
: return "del";
4113 case DEVLINK_CMD_PORT_GET
: return "get";
4114 case DEVLINK_CMD_PORT_SET
: return "set";
4115 case DEVLINK_CMD_PORT_NEW
: return "new";
4116 case DEVLINK_CMD_PORT_DEL
: return "del";
4117 case DEVLINK_CMD_PARAM_GET
: return "get";
4118 case DEVLINK_CMD_PARAM_SET
: return "set";
4119 case DEVLINK_CMD_PARAM_NEW
: return "new";
4120 case DEVLINK_CMD_PARAM_DEL
: return "del";
4121 case DEVLINK_CMD_REGION_GET
: return "get";
4122 case DEVLINK_CMD_REGION_SET
: return "set";
4123 case DEVLINK_CMD_REGION_NEW
: return "new";
4124 case DEVLINK_CMD_REGION_DEL
: return "del";
4125 case DEVLINK_CMD_FLASH_UPDATE
: return "begin";
4126 case DEVLINK_CMD_FLASH_UPDATE_END
: return "end";
4127 case DEVLINK_CMD_FLASH_UPDATE_STATUS
: return "status";
4128 case DEVLINK_CMD_TRAP_GET
: return "get";
4129 case DEVLINK_CMD_TRAP_SET
: return "set";
4130 case DEVLINK_CMD_TRAP_NEW
: return "new";
4131 case DEVLINK_CMD_TRAP_DEL
: return "del";
4132 case DEVLINK_CMD_TRAP_GROUP_GET
: return "get";
4133 case DEVLINK_CMD_TRAP_GROUP_SET
: return "set";
4134 case DEVLINK_CMD_TRAP_GROUP_NEW
: return "new";
4135 case DEVLINK_CMD_TRAP_GROUP_DEL
: return "del";
4136 default: return "<unknown cmd>";
4140 static const char *cmd_obj(uint8_t cmd
)
4143 case DEVLINK_CMD_UNSPEC
: return "unspec";
4144 case DEVLINK_CMD_GET
:
4145 case DEVLINK_CMD_SET
:
4146 case DEVLINK_CMD_NEW
:
4147 case DEVLINK_CMD_DEL
:
4149 case DEVLINK_CMD_PORT_GET
:
4150 case DEVLINK_CMD_PORT_SET
:
4151 case DEVLINK_CMD_PORT_NEW
:
4152 case DEVLINK_CMD_PORT_DEL
:
4154 case DEVLINK_CMD_PARAM_GET
:
4155 case DEVLINK_CMD_PARAM_SET
:
4156 case DEVLINK_CMD_PARAM_NEW
:
4157 case DEVLINK_CMD_PARAM_DEL
:
4159 case DEVLINK_CMD_REGION_GET
:
4160 case DEVLINK_CMD_REGION_SET
:
4161 case DEVLINK_CMD_REGION_NEW
:
4162 case DEVLINK_CMD_REGION_DEL
:
4164 case DEVLINK_CMD_FLASH_UPDATE
:
4165 case DEVLINK_CMD_FLASH_UPDATE_END
:
4166 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4168 case DEVLINK_CMD_TRAP_GET
:
4169 case DEVLINK_CMD_TRAP_SET
:
4170 case DEVLINK_CMD_TRAP_NEW
:
4171 case DEVLINK_CMD_TRAP_DEL
:
4173 case DEVLINK_CMD_TRAP_GROUP_GET
:
4174 case DEVLINK_CMD_TRAP_GROUP_SET
:
4175 case DEVLINK_CMD_TRAP_GROUP_NEW
:
4176 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4177 return "trap-group";
4178 default: return "<unknown obj>";
4182 static void pr_out_mon_header(uint8_t cmd
)
4184 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
4187 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
4189 const char *obj
= cmd_obj(cmd
);
4190 unsigned int index
= 0;
4191 const char *cur_obj
;
4195 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4196 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
4202 static void pr_out_flash_update(struct dl
*dl
, struct nlattr
**tb
)
4204 __pr_out_handle_start(dl
, tb
, true, false);
4206 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
])
4207 pr_out_str(dl
, "msg",
4208 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
]));
4210 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
])
4211 pr_out_str(dl
, "component",
4212 mnl_attr_get_str(tb
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
]));
4214 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
])
4215 pr_out_u64(dl
, "done",
4216 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
]));
4218 if (tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
])
4219 pr_out_u64(dl
, "total",
4220 mnl_attr_get_u64(tb
[DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
]));
4222 pr_out_handle_end(dl
);
4225 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
4226 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4227 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
);
4229 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4231 struct dl
*dl
= data
;
4232 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4233 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4234 uint8_t cmd
= genl
->cmd
;
4236 if (!cmd_filter_check(dl
, cmd
))
4240 case DEVLINK_CMD_GET
: /* fall through */
4241 case DEVLINK_CMD_SET
: /* fall through */
4242 case DEVLINK_CMD_NEW
: /* fall through */
4243 case DEVLINK_CMD_DEL
:
4244 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4245 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4246 return MNL_CB_ERROR
;
4247 pr_out_mon_header(genl
->cmd
);
4248 pr_out_handle(dl
, tb
);
4250 case DEVLINK_CMD_PORT_GET
: /* fall through */
4251 case DEVLINK_CMD_PORT_SET
: /* fall through */
4252 case DEVLINK_CMD_PORT_NEW
: /* fall through */
4253 case DEVLINK_CMD_PORT_DEL
:
4254 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4255 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4256 !tb
[DEVLINK_ATTR_PORT_INDEX
])
4257 return MNL_CB_ERROR
;
4258 pr_out_mon_header(genl
->cmd
);
4259 pr_out_port(dl
, tb
);
4261 case DEVLINK_CMD_PARAM_GET
: /* fall through */
4262 case DEVLINK_CMD_PARAM_SET
: /* fall through */
4263 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
4264 case DEVLINK_CMD_PARAM_DEL
:
4265 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4266 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4267 !tb
[DEVLINK_ATTR_PARAM
])
4268 return MNL_CB_ERROR
;
4269 pr_out_mon_header(genl
->cmd
);
4270 pr_out_param(dl
, tb
, false);
4272 case DEVLINK_CMD_REGION_GET
: /* fall through */
4273 case DEVLINK_CMD_REGION_SET
: /* fall through */
4274 case DEVLINK_CMD_REGION_NEW
: /* fall through */
4275 case DEVLINK_CMD_REGION_DEL
:
4276 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4277 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4278 !tb
[DEVLINK_ATTR_REGION_NAME
])
4279 return MNL_CB_ERROR
;
4280 pr_out_mon_header(genl
->cmd
);
4281 pr_out_region(dl
, tb
);
4283 case DEVLINK_CMD_FLASH_UPDATE
: /* fall through */
4284 case DEVLINK_CMD_FLASH_UPDATE_END
: /* fall through */
4285 case DEVLINK_CMD_FLASH_UPDATE_STATUS
:
4286 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4287 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
4288 return MNL_CB_ERROR
;
4289 pr_out_mon_header(genl
->cmd
);
4290 pr_out_flash_update(dl
, tb
);
4292 case DEVLINK_CMD_TRAP_GET
: /* fall through */
4293 case DEVLINK_CMD_TRAP_SET
: /* fall through */
4294 case DEVLINK_CMD_TRAP_NEW
: /* fall through */
4295 case DEVLINK_CMD_TRAP_DEL
:
4296 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4297 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4298 !tb
[DEVLINK_ATTR_TRAP_NAME
] ||
4299 !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
4300 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
4301 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4302 !tb
[DEVLINK_ATTR_TRAP_METADATA
] ||
4303 !tb
[DEVLINK_ATTR_STATS
])
4304 return MNL_CB_ERROR
;
4305 pr_out_mon_header(genl
->cmd
);
4306 pr_out_trap(dl
, tb
, false);
4308 case DEVLINK_CMD_TRAP_GROUP_GET
: /* fall through */
4309 case DEVLINK_CMD_TRAP_GROUP_SET
: /* fall through */
4310 case DEVLINK_CMD_TRAP_GROUP_NEW
: /* fall through */
4311 case DEVLINK_CMD_TRAP_GROUP_DEL
:
4312 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4313 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4314 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
4315 !tb
[DEVLINK_ATTR_STATS
])
4316 return MNL_CB_ERROR
;
4317 pr_out_mon_header(genl
->cmd
);
4318 pr_out_trap_group(dl
, tb
, false);
4324 static int cmd_mon_show(struct dl
*dl
)
4327 unsigned int index
= 0;
4328 const char *cur_obj
;
4330 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
4331 if (strcmp(cur_obj
, "all") != 0 &&
4332 strcmp(cur_obj
, "dev") != 0 &&
4333 strcmp(cur_obj
, "port") != 0 &&
4334 strcmp(cur_obj
, "trap") != 0 &&
4335 strcmp(cur_obj
, "trap-group") != 0) {
4336 pr_err("Unknown object \"%s\"\n", cur_obj
);
4340 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
4343 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
4349 static void cmd_mon_help(void)
4351 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
4352 "where OBJECT-LIST := { dev | port | trap | trap-group }\n");
4355 static int cmd_mon(struct dl
*dl
)
4357 if (dl_argv_match(dl
, "help")) {
4361 return cmd_mon_show(dl
);
4364 struct dpipe_field
{
4367 unsigned int bitwidth
;
4368 enum devlink_dpipe_field_mapping_type mapping_type
;
4371 struct dpipe_header
{
4372 struct list_head list
;
4375 struct dpipe_field
*fields
;
4376 unsigned int fields_count
;
4379 struct dpipe_table
{
4380 struct list_head list
;
4382 unsigned int resource_id
;
4383 bool resource_valid
;
4386 struct dpipe_tables
{
4387 struct list_head table_list
;
4397 enum devlink_resource_unit unit
;
4402 struct list_head list
;
4403 struct list_head resource_list
;
4404 struct resource
*parent
;
4408 struct list_head resource_list
;
4411 struct resource_ctx
{
4414 struct resources
*resources
;
4415 struct dpipe_tables
*tables
;
4416 bool print_resources
;
4417 bool pending_change
;
4420 static struct resource
*resource_alloc(void)
4422 struct resource
*resource
;
4424 resource
= calloc(1, sizeof(struct resource
));
4427 INIT_LIST_HEAD(&resource
->resource_list
);
4431 static void resource_free(struct resource
*resource
)
4433 struct resource
*child_resource
, *tmp
;
4435 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
4437 free(child_resource
->name
);
4438 resource_free(child_resource
);
4443 static struct resources
*resources_alloc(void)
4445 struct resources
*resources
;
4447 resources
= calloc(1, sizeof(struct resources
));
4450 INIT_LIST_HEAD(&resources
->resource_list
);
4454 static void resources_free(struct resources
*resources
)
4456 struct resource
*resource
, *tmp
;
4458 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
4459 resource_free(resource
);
4462 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
4464 ctx
->resources
= resources_alloc();
4465 if (!ctx
->resources
)
4471 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4473 resources_free(ctx
->resources
);
4479 struct list_head global_headers
;
4480 struct list_head local_headers
;
4481 struct dpipe_tables
*tables
;
4482 struct resources
*resources
;
4487 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4489 struct dpipe_header
*header
;
4491 header
= calloc(1, sizeof(struct dpipe_header
));
4494 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4495 if (!header
->fields
)
4496 goto err_fields_alloc
;
4497 header
->fields_count
= fields_count
;
4505 static void dpipe_header_free(struct dpipe_header
*header
)
4507 free(header
->fields
);
4511 static void dpipe_header_clear(struct dpipe_header
*header
)
4513 struct dpipe_field
*field
;
4516 for (i
= 0; i
< header
->fields_count
; i
++) {
4517 field
= &header
->fields
[i
];
4523 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4524 struct dpipe_header
*header
, bool global
)
4527 list_add(&header
->list
, &ctx
->global_headers
);
4529 list_add(&header
->list
, &ctx
->local_headers
);
4532 static void dpipe_header_del(struct dpipe_header
*header
)
4534 list_del(&header
->list
);
4537 static struct dpipe_table
*dpipe_table_alloc(void)
4539 return calloc(1, sizeof(struct dpipe_table
));
4542 static void dpipe_table_free(struct dpipe_table
*table
)
4547 static struct dpipe_tables
*dpipe_tables_alloc(void)
4549 struct dpipe_tables
*tables
;
4551 tables
= calloc(1, sizeof(struct dpipe_tables
));
4554 INIT_LIST_HEAD(&tables
->table_list
);
4558 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4560 struct dpipe_table
*table
, *tmp
;
4562 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4563 dpipe_table_free(table
);
4567 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4569 ctx
->tables
= dpipe_tables_alloc();
4574 INIT_LIST_HEAD(&ctx
->global_headers
);
4575 INIT_LIST_HEAD(&ctx
->local_headers
);
4579 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4581 struct dpipe_header
*header
, *tmp
;
4583 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4585 dpipe_header_del(header
);
4586 dpipe_header_clear(header
);
4587 dpipe_header_free(header
);
4589 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4591 dpipe_header_del(header
);
4592 dpipe_header_clear(header
);
4593 dpipe_header_free(header
);
4595 dpipe_tables_free(ctx
->tables
);
4598 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4599 uint32_t header_id
, bool global
)
4601 struct list_head
*header_list
;
4602 struct dpipe_header
*header
;
4605 header_list
= &ctx
->global_headers
;
4607 header_list
= &ctx
->local_headers
;
4608 list_for_each_entry(header
, header_list
, list
) {
4609 if (header
->id
!= header_id
)
4611 return header
->name
;
4616 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4618 uint32_t field_id
, bool global
)
4620 struct list_head
*header_list
;
4621 struct dpipe_header
*header
;
4624 header_list
= &ctx
->global_headers
;
4626 header_list
= &ctx
->local_headers
;
4627 list_for_each_entry(header
, header_list
, list
) {
4628 if (header
->id
!= header_id
)
4630 return header
->fields
[field_id
].name
;
4636 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4638 switch (mapping_type
) {
4639 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4641 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4649 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4650 uint32_t field_id
, bool global
)
4652 enum devlink_dpipe_field_mapping_type mapping_type
;
4653 struct list_head
*header_list
;
4654 struct dpipe_header
*header
;
4657 header_list
= &ctx
->global_headers
;
4659 header_list
= &ctx
->local_headers
;
4660 list_for_each_entry(header
, header_list
, list
) {
4661 if (header
->id
!= header_id
)
4663 mapping_type
= header
->fields
[field_id
].mapping_type
;
4664 return dpipe_field_mapping_e2s(mapping_type
);
4669 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4670 struct dpipe_field
*fields
,
4671 unsigned int field_count
)
4673 struct dpipe_field
*field
;
4676 for (i
= 0; i
< field_count
; i
++) {
4678 pr_out_entry_start(ctx
->dl
);
4679 pr_out_str(ctx
->dl
, "name", field
->name
);
4680 if (ctx
->dl
->verbose
)
4681 pr_out_uint(ctx
->dl
, "id", field
->id
);
4682 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4683 if (field
->mapping_type
)
4684 pr_out_str(ctx
->dl
, "mapping_type",
4685 dpipe_field_mapping_e2s(field
->mapping_type
));
4686 pr_out_entry_end(ctx
->dl
);
4691 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4692 struct dpipe_header
*header
, bool global
)
4694 pr_out_handle_start_arr(ctx
->dl
, tb
);
4695 pr_out_str(ctx
->dl
, "name", header
->name
);
4696 if (ctx
->dl
->verbose
) {
4697 pr_out_uint(ctx
->dl
, "id", header
->id
);
4698 pr_out_str(ctx
->dl
, "global",
4699 global
? "true" : "false");
4701 pr_out_array_start(ctx
->dl
, "field");
4702 pr_out_dpipe_fields(ctx
, header
->fields
,
4703 header
->fields_count
);
4704 pr_out_array_end(ctx
->dl
);
4705 pr_out_handle_end(ctx
->dl
);
4708 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4711 struct dpipe_header
*header
;
4713 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4714 pr_out_dpipe_header(ctx
, tb
, header
, false);
4716 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4717 pr_out_dpipe_header(ctx
, tb
, header
, true);
4720 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4722 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4726 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4727 if (err
!= MNL_CB_OK
)
4729 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4730 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4731 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4732 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4735 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4736 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4737 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4738 field
->name
= strdup(name
);
4741 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4745 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4746 struct dpipe_field
*fields
)
4748 struct nlattr
*nla_field
;
4752 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4753 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4761 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4763 struct nlattr
*nla_field
;
4764 unsigned int count
= 0;
4766 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4771 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4773 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4774 struct dpipe_header
*header
;
4775 unsigned int fields_count
;
4776 const char *header_name
;
4780 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4781 if (err
!= MNL_CB_OK
)
4784 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4785 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4786 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4789 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4790 header
= dpipe_header_alloc(fields_count
);
4794 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4795 header
->name
= strdup(header_name
);
4796 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4797 header
->fields_count
= fields_count
;
4798 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4800 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4804 dpipe_header_add(ctx
, header
, global
);
4808 dpipe_header_free(header
);
4812 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4814 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4815 struct nlattr
*nla_header
;
4818 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4819 err
= dpipe_header_get(ctx
, nla_header
);
4826 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4828 struct dpipe_ctx
*ctx
= data
;
4829 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4830 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4833 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4834 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4835 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4836 return MNL_CB_ERROR
;
4837 err
= dpipe_headers_get(ctx
, tb
);
4840 return MNL_CB_ERROR
;
4843 if (ctx
->print_headers
)
4844 pr_out_dpipe_headers(ctx
, tb
);
4848 static int cmd_dpipe_headers_show(struct dl
*dl
)
4850 struct nlmsghdr
*nlh
;
4851 struct dpipe_ctx ctx
= {};
4852 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4855 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4857 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4861 err
= dpipe_ctx_init(&ctx
, dl
);
4865 ctx
.print_headers
= true;
4867 pr_out_section_start(dl
, "header");
4868 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4870 pr_err("error get headers %s\n", strerror(ctx
.err
));
4871 pr_out_section_end(dl
);
4873 dpipe_ctx_fini(&ctx
);
4877 static void cmd_dpipe_header_help(void)
4879 pr_err("Usage: devlink dpipe headers show DEV\n");
4882 static int cmd_dpipe_header(struct dl
*dl
)
4884 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4885 cmd_dpipe_header_help();
4887 } else if (dl_argv_match(dl
, "show")) {
4889 return cmd_dpipe_headers_show(dl
);
4891 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4896 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4898 switch (action_type
) {
4899 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4900 return "field_modify";
4906 struct dpipe_op_info
{
4912 struct dpipe_action
{
4913 struct dpipe_op_info info
;
4917 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4918 struct dpipe_ctx
*ctx
)
4920 struct dpipe_op_info
*op_info
= &action
->info
;
4921 const char *mapping
;
4923 pr_out_str(ctx
->dl
, "type",
4924 dpipe_action_type_e2s(action
->type
));
4925 pr_out_str(ctx
->dl
, "header",
4926 dpipe_header_id2s(ctx
, op_info
->header_id
,
4927 op_info
->header_global
));
4928 pr_out_str(ctx
->dl
, "field",
4929 dpipe_field_id2s(ctx
, op_info
->header_id
,
4931 op_info
->header_global
));
4932 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4934 op_info
->header_global
);
4936 pr_out_str(ctx
->dl
, "mapping", mapping
);
4939 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4941 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4944 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4945 if (err
!= MNL_CB_OK
)
4948 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4949 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4950 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4951 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4955 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4956 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4957 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4958 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4963 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4964 struct nlattr
*nla_actions
)
4966 struct nlattr
*nla_action
;
4967 struct dpipe_action action
;
4969 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4970 pr_out_entry_start(ctx
->dl
);
4971 if (dpipe_action_parse(&action
, nla_action
))
4972 goto err_action_parse
;
4973 pr_out_dpipe_action(&action
, ctx
);
4974 pr_out_entry_end(ctx
->dl
);
4979 pr_out_entry_end(ctx
->dl
);
4984 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4986 switch (match_type
) {
4987 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4988 return "field_exact";
4994 struct dpipe_match
{
4995 struct dpipe_op_info info
;
4999 static void pr_out_dpipe_match(struct dpipe_match
*match
,
5000 struct dpipe_ctx
*ctx
)
5002 struct dpipe_op_info
*op_info
= &match
->info
;
5003 const char *mapping
;
5005 pr_out_str(ctx
->dl
, "type",
5006 dpipe_match_type_e2s(match
->type
));
5007 pr_out_str(ctx
->dl
, "header",
5008 dpipe_header_id2s(ctx
, op_info
->header_id
,
5009 op_info
->header_global
));
5010 pr_out_str(ctx
->dl
, "field",
5011 dpipe_field_id2s(ctx
, op_info
->header_id
,
5013 op_info
->header_global
));
5014 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
5016 op_info
->header_global
);
5018 pr_out_str(ctx
->dl
, "mapping", mapping
);
5021 static int dpipe_match_parse(struct dpipe_match
*match
,
5025 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
5028 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
5029 if (err
!= MNL_CB_OK
)
5032 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
5033 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
5034 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
5035 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
5039 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
5040 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
5041 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
5042 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
5047 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
5048 struct nlattr
*nla_matches
)
5050 struct nlattr
*nla_match
;
5051 struct dpipe_match match
;
5053 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
5054 pr_out_entry_start(ctx
->dl
);
5055 if (dpipe_match_parse(&match
, nla_match
))
5056 goto err_match_parse
;
5057 pr_out_dpipe_match(&match
, ctx
);
5058 pr_out_entry_end(ctx
->dl
);
5063 pr_out_entry_end(ctx
->dl
);
5067 static struct resource
*
5068 resource_find(struct resources
*resources
, struct resource
*resource
,
5069 uint64_t resource_id
)
5071 struct list_head
*list_head
;
5074 list_head
= &resources
->resource_list
;
5076 list_head
= &resource
->resource_list
;
5078 list_for_each_entry(resource
, list_head
, list
) {
5079 struct resource
*child_resource
;
5081 if (resource
->id
== resource_id
)
5084 child_resource
= resource_find(resources
, resource
,
5087 return child_resource
;
5093 resource_path_print(struct dl
*dl
, struct resources
*resources
,
5094 uint64_t resource_id
)
5096 struct resource
*resource
, *parent_resource
;
5097 const char del
[] = "/";
5101 resource
= resource_find(resources
, NULL
, resource_id
);
5105 for (parent_resource
= resource
; parent_resource
;
5106 parent_resource
= parent_resource
->parent
)
5107 path_len
+= strlen(parent_resource
->name
) + 1;
5110 path
= calloc(1, path_len
);
5114 path
+= path_len
- 1;
5115 for (parent_resource
= resource
; parent_resource
;
5116 parent_resource
= parent_resource
->parent
) {
5117 path
-= strlen(parent_resource
->name
);
5118 memcpy(path
, parent_resource
->name
,
5119 strlen(parent_resource
->name
));
5120 path
-= strlen(del
);
5121 memcpy(path
, del
, strlen(del
));
5123 pr_out_str(dl
, "resource_path", path
);
5127 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5129 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
5130 struct dpipe_table
*table
;
5131 uint32_t resource_units
;
5132 bool counters_enabled
;
5133 bool resource_valid
;
5137 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
5138 if (err
!= MNL_CB_OK
)
5141 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
5142 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
5143 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
5144 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
5145 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
5149 table
= dpipe_table_alloc();
5153 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
5154 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
5155 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
5157 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
5159 if (resource_valid
) {
5160 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
5161 table
->resource_valid
= true;
5164 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
5165 if (!ctx
->print_tables
)
5168 pr_out_str(ctx
->dl
, "name", table
->name
);
5169 pr_out_uint(ctx
->dl
, "size", size
);
5170 pr_out_str(ctx
->dl
, "counters_enabled",
5171 counters_enabled
? "true" : "false");
5173 if (resource_valid
) {
5174 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
5175 resource_path_print(ctx
->dl
, ctx
->resources
,
5176 table
->resource_id
);
5177 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
5180 pr_out_array_start(ctx
->dl
, "match");
5181 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
5182 goto err_matches_show
;
5183 pr_out_array_end(ctx
->dl
);
5185 pr_out_array_start(ctx
->dl
, "action");
5186 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
5187 goto err_actions_show
;
5188 pr_out_array_end(ctx
->dl
);
5194 pr_out_array_end(ctx
->dl
);
5198 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5200 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
5201 struct nlattr
*nla_table
;
5203 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
5204 if (ctx
->print_tables
)
5205 pr_out_handle_start_arr(ctx
->dl
, tb
);
5206 if (dpipe_table_show(ctx
, nla_table
))
5207 goto err_table_show
;
5208 if (ctx
->print_tables
)
5209 pr_out_handle_end(ctx
->dl
);
5214 if (ctx
->print_tables
)
5215 pr_out_handle_end(ctx
->dl
);
5219 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5221 struct dpipe_ctx
*ctx
= data
;
5222 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5223 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5225 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5226 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5227 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
5228 return MNL_CB_ERROR
;
5230 if (dpipe_tables_show(ctx
, tb
))
5231 return MNL_CB_ERROR
;
5235 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
5237 static int cmd_dpipe_table_show(struct dl
*dl
)
5239 struct nlmsghdr
*nlh
;
5240 struct dpipe_ctx dpipe_ctx
= {};
5241 struct resource_ctx resource_ctx
= {};
5242 uint16_t flags
= NLM_F_REQUEST
;
5245 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
5249 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5251 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5255 dpipe_ctx
.print_tables
= true;
5257 dl_opts_put(nlh
, dl
);
5258 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
5261 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
5262 goto err_headers_get
;
5265 err
= resource_ctx_init(&resource_ctx
, dl
);
5267 goto err_resource_ctx_init
;
5269 resource_ctx
.print_resources
= false;
5270 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
5271 dl_opts_put(nlh
, dl
);
5272 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5275 dpipe_ctx
.resources
= resource_ctx
.resources
;
5277 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5278 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
5279 dl_opts_put(nlh
, dl
);
5281 pr_out_section_start(dl
, "table");
5282 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
5283 pr_out_section_end(dl
);
5285 resource_ctx_fini(&resource_ctx
);
5286 dpipe_ctx_fini(&dpipe_ctx
);
5289 err_resource_ctx_init
:
5291 dpipe_ctx_fini(&dpipe_ctx
);
5295 static int cmd_dpipe_table_set(struct dl
*dl
)
5297 struct nlmsghdr
*nlh
;
5300 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
5301 NLM_F_REQUEST
| NLM_F_ACK
);
5303 err
= dl_argv_parse_put(nlh
, dl
,
5304 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
5305 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
5309 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5312 enum dpipe_value_type
{
5313 DPIPE_VALUE_TYPE_VALUE
,
5314 DPIPE_VALUE_TYPE_MASK
,
5318 dpipe_value_type_e2s(enum dpipe_value_type type
)
5321 case DPIPE_VALUE_TYPE_VALUE
:
5323 case DPIPE_VALUE_TYPE_MASK
:
5324 return "value_mask";
5330 struct dpipe_field_printer
{
5331 unsigned int field_id
;
5332 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
5335 struct dpipe_header_printer
{
5336 struct dpipe_field_printer
*printers
;
5337 unsigned int printers_count
;
5338 unsigned int header_id
;
5341 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
5342 enum dpipe_value_type type
,
5345 struct in_addr ip_addr
;
5347 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
5348 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
5352 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
5353 enum dpipe_value_type type
,
5356 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
5357 ether_ntoa((struct ether_addr
*)value
));
5360 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
5361 enum dpipe_value_type type
,
5364 char str
[INET6_ADDRSTRLEN
];
5366 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
5367 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
5370 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
5372 .printer
= dpipe_field_printer_ipv4_addr
,
5373 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
5377 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
5378 .printers
= dpipe_field_printers_ipv4
,
5379 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
5380 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
5383 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
5385 .printer
= dpipe_field_printer_ethernet_addr
,
5386 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
5390 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
5391 .printers
= dpipe_field_printers_ethernet
,
5392 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
5393 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
5396 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
5398 .printer
= dpipe_field_printer_ipv6_addr
,
5399 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
5403 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
5404 .printers
= dpipe_field_printers_ipv6
,
5405 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
5406 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
5409 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
5410 &dpipe_header_printer_ipv4
,
5411 &dpipe_header_printer_ethernet
,
5412 &dpipe_header_printer_ipv6
,
5415 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
5416 struct dpipe_op_info
*info
,
5417 enum dpipe_value_type type
,
5420 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
5421 struct dpipe_header_printer
*header_printer
;
5422 struct dpipe_field_printer
*field_printer
;
5423 unsigned int field_printers_count
;
5427 for (i
= 0; i
< header_printers_count
; i
++) {
5428 header_printer
= dpipe_header_printers
[i
];
5429 if (header_printer
->header_id
!= info
->header_id
)
5431 field_printers_count
= header_printer
->printers_count
;
5432 for (j
= 0; j
< field_printers_count
; j
++) {
5433 field_printer
= &header_printer
->printers
[j
];
5434 if (field_printer
->field_id
!= info
->field_id
)
5436 field_printer
->printer(ctx
, type
, value
);
5444 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
5446 unsigned int value_len
,
5447 struct dpipe_op_info
*info
,
5448 enum dpipe_value_type type
)
5450 if (info
->header_global
&&
5451 !dpipe_print_prot_header(ctx
, info
, type
, value
))
5454 if (value_len
== sizeof(uint32_t)) {
5455 uint32_t *value_32
= value
;
5457 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
5461 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
5462 struct nlattr
**nla_match_value
,
5463 struct dpipe_op_info
*info
)
5465 void *value
, *value_mask
;
5466 uint32_t value_mapping
;
5470 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5471 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5473 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5474 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5477 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5478 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
5482 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5483 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5484 DPIPE_VALUE_TYPE_MASK
);
5487 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5490 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5493 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5494 struct dpipe_match match
;
5497 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5498 if (err
!= MNL_CB_OK
)
5501 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5502 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5506 pr_out_entry_start(ctx
->dl
);
5507 if (dpipe_match_parse(&match
,
5508 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5509 goto err_match_parse
;
5510 pr_out_dpipe_match(&match
, ctx
);
5511 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5512 pr_out_entry_end(ctx
->dl
);
5517 pr_out_entry_end(ctx
->dl
);
5521 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5524 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5525 struct dpipe_action action
;
5528 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5529 if (err
!= MNL_CB_OK
)
5532 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5533 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5537 pr_out_entry_start(ctx
->dl
);
5538 if (dpipe_action_parse(&action
,
5539 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5540 goto err_action_parse
;
5541 pr_out_dpipe_action(&action
, ctx
);
5542 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5543 pr_out_entry_end(ctx
->dl
);
5548 pr_out_entry_end(ctx
->dl
);
5553 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5554 struct nlattr
*nla_action_values
)
5556 struct nlattr
*nla_action_value
;
5558 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5559 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5566 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5567 struct nlattr
*nla_match_values
)
5569 struct nlattr
*nla_match_value
;
5571 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5572 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5578 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5580 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5581 uint32_t entry_index
;
5585 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5586 if (err
!= MNL_CB_OK
)
5589 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5590 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5591 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5595 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5596 pr_out_uint(ctx
->dl
, "index", entry_index
);
5598 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5599 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5600 pr_out_uint(ctx
->dl
, "counter", counter
);
5603 pr_out_array_start(ctx
->dl
, "match_value");
5604 if (dpipe_tables_match_values_show(ctx
,
5605 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5606 goto err_match_values_show
;
5607 pr_out_array_end(ctx
->dl
);
5609 pr_out_array_start(ctx
->dl
, "action_value");
5610 if (dpipe_tables_action_values_show(ctx
,
5611 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5612 goto err_action_values_show
;
5613 pr_out_array_end(ctx
->dl
);
5616 err_action_values_show
:
5617 err_match_values_show
:
5618 pr_out_array_end(ctx
->dl
);
5622 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5624 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5625 struct nlattr
*nla_entry
;
5627 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5628 pr_out_handle_start_arr(ctx
->dl
, tb
);
5629 if (dpipe_entry_show(ctx
, nla_entry
))
5630 goto err_entry_show
;
5631 pr_out_handle_end(ctx
->dl
);
5636 pr_out_handle_end(ctx
->dl
);
5640 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5642 struct dpipe_ctx
*ctx
= data
;
5643 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5644 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5646 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5647 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5648 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5649 return MNL_CB_ERROR
;
5651 if (dpipe_table_entries_show(ctx
, tb
))
5652 return MNL_CB_ERROR
;
5656 static int cmd_dpipe_table_dump(struct dl
*dl
)
5658 struct nlmsghdr
*nlh
;
5659 struct dpipe_ctx ctx
= {};
5660 uint16_t flags
= NLM_F_REQUEST
;
5663 err
= dpipe_ctx_init(&ctx
, dl
);
5667 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5671 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5672 dl_opts_put(nlh
, dl
);
5673 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5675 pr_err("error get headers %s\n", strerror(ctx
.err
));
5679 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5680 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5681 dl_opts_put(nlh
, dl
);
5683 pr_out_section_start(dl
, "table_entry");
5684 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5685 pr_out_section_end(dl
);
5687 dpipe_ctx_fini(&ctx
);
5691 static void cmd_dpipe_table_help(void)
5693 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5694 "where OBJECT-LIST := { show | set | dump }\n");
5697 static int cmd_dpipe_table(struct dl
*dl
)
5699 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5700 cmd_dpipe_table_help();
5702 } else if (dl_argv_match(dl
, "show")) {
5704 return cmd_dpipe_table_show(dl
);
5705 } else if (dl_argv_match(dl
, "set")) {
5707 return cmd_dpipe_table_set(dl
);
5708 } else if (dl_argv_match(dl
, "dump")) {
5710 return cmd_dpipe_table_dump(dl
);
5712 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5716 static void cmd_dpipe_help(void)
5718 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5719 "where OBJECT-LIST := { header | table }\n");
5722 static int cmd_dpipe(struct dl
*dl
)
5724 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5727 } else if (dl_argv_match(dl
, "header")) {
5729 return cmd_dpipe_header(dl
);
5730 } else if (dl_argv_match(dl
, "table")) {
5732 return cmd_dpipe_table(dl
);
5734 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5739 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5740 struct nlattr
**nla_resource
)
5742 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5743 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5744 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5745 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5746 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5747 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5748 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5752 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5753 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5754 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5755 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5756 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5757 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5758 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5760 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5761 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5763 resource
->size_new
= resource
->size
;
5765 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5766 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5767 resource
->occ_valid
= true;
5770 if (resource
->size_new
!= resource
->size
)
5771 ctx
->pending_change
= true;
5777 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5778 struct resource
*parent_resource
, struct nlattr
*nl
)
5780 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5781 struct nlattr
*nla_child_resource
;
5782 struct nlattr
*nla_resources
;
5792 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5793 if (err
!= MNL_CB_OK
)
5796 err
= resource_parse(ctx
, resource
, nla_resource
);
5800 resource
->parent
= parent_resource
;
5801 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5804 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5805 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5807 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5808 struct resource
*child_resource
;
5809 struct list_head
*list
;
5811 child_resource
= resource_alloc();
5812 if (!child_resource
)
5816 list
= &ctx
->resources
->resource_list
;
5818 list
= &resource
->resource_list
;
5820 list_add_tail(&child_resource
->list
, list
);
5821 err
= resource_get(ctx
, child_resource
, resource
,
5822 nla_child_resource
);
5830 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5833 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5834 default: return "<unknown unit>";
5838 static void resource_show(struct resource
*resource
,
5839 struct resource_ctx
*ctx
)
5841 struct resource
*child_resource
;
5842 struct dpipe_table
*table
;
5843 struct dl
*dl
= ctx
->dl
;
5846 pr_out_str(dl
, "name", resource
->name
);
5848 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5849 pr_out_u64(dl
, "size", resource
->size
);
5850 if (resource
->size
!= resource
->size_new
)
5851 pr_out_u64(dl
, "size_new", resource
->size_new
);
5852 if (resource
->occ_valid
)
5853 pr_out_uint(dl
, "occ", resource
->size_occ
);
5854 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5856 if (resource
->size_min
!= resource
->size_max
) {
5857 pr_out_uint(dl
, "size_min", resource
->size_min
);
5858 pr_out_u64(dl
, "size_max", resource
->size_max
);
5859 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5862 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5863 if (table
->resource_id
== resource
->id
&&
5864 table
->resource_valid
)
5868 pr_out_array_start(dl
, "dpipe_tables");
5870 pr_out_str(dl
, "dpipe_tables", "none");
5872 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5873 if (table
->resource_id
!= resource
->id
||
5874 !table
->resource_valid
)
5876 pr_out_entry_start(dl
);
5877 pr_out_str(dl
, "table_name", table
->name
);
5878 pr_out_entry_end(dl
);
5881 pr_out_array_end(dl
);
5883 if (list_empty(&resource
->resource_list
))
5886 if (ctx
->pending_change
)
5887 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5889 pr_out_array_start(dl
, "resources");
5890 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5891 pr_out_entry_start(dl
);
5892 resource_show(child_resource
, ctx
);
5893 pr_out_entry_end(dl
);
5895 pr_out_array_end(dl
);
5899 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5901 struct resources
*resources
= ctx
->resources
;
5902 struct resource
*resource
;
5904 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5905 pr_out_handle_start_arr(ctx
->dl
, tb
);
5906 resource_show(resource
, ctx
);
5907 pr_out_handle_end(ctx
->dl
);
5911 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5913 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5916 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5918 struct resource_ctx
*ctx
= data
;
5919 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5920 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5923 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5924 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5925 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5926 return MNL_CB_ERROR
;
5928 err
= resources_get(ctx
, tb
);
5931 return MNL_CB_ERROR
;
5934 if (ctx
->print_resources
)
5935 resources_show(ctx
, tb
);
5940 static int cmd_resource_show(struct dl
*dl
)
5942 struct nlmsghdr
*nlh
;
5943 struct dpipe_ctx dpipe_ctx
= {};
5944 struct resource_ctx resource_ctx
= {};
5947 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5951 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5953 dl_opts_put(nlh
, dl
);
5955 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5959 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5962 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5966 err
= resource_ctx_init(&resource_ctx
, dl
);
5970 resource_ctx
.print_resources
= true;
5971 resource_ctx
.tables
= dpipe_ctx
.tables
;
5972 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5973 NLM_F_REQUEST
| NLM_F_ACK
);
5974 dl_opts_put(nlh
, dl
);
5975 pr_out_section_start(dl
, "resources");
5976 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5978 pr_out_section_end(dl
);
5979 resource_ctx_fini(&resource_ctx
);
5981 dpipe_ctx_fini(&dpipe_ctx
);
5985 static void cmd_resource_help(void)
5987 pr_err("Usage: devlink resource show DEV\n"
5988 " devlink resource set DEV path PATH size SIZE\n");
5991 static struct resource
*
5992 resource_find_by_name(struct list_head
*list
, char *name
)
5994 struct resource
*resource
;
5996 list_for_each_entry(resource
, list
, list
) {
5997 if (!strcmp(resource
->name
, name
))
6004 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
6005 uint32_t *p_resource_id
, bool *p_resource_valid
)
6007 struct resource
*resource
;
6008 uint32_t resource_id
= 0;
6009 char *resource_path_dup
;
6010 struct list_head
*list
;
6011 const char del
[] = "/";
6012 char *resource_name
;
6014 resource_path_dup
= strdup(resource_path
);
6015 list
= &ctx
->resources
->resource_list
;
6016 resource_name
= strtok(resource_path_dup
, del
);
6017 while (resource_name
!= NULL
) {
6018 resource
= resource_find_by_name(list
, resource_name
);
6020 goto err_resource_lookup
;
6022 list
= &resource
->resource_list
;
6023 resource_name
= strtok(NULL
, del
);
6024 resource_id
= resource
->id
;
6026 free(resource_path_dup
);
6027 *p_resource_valid
= true;
6028 *p_resource_id
= resource_id
;
6031 err_resource_lookup
:
6032 free(resource_path_dup
);
6036 static int cmd_resource_set(struct dl
*dl
)
6038 struct nlmsghdr
*nlh
;
6039 struct resource_ctx ctx
= {};
6042 err
= resource_ctx_init(&ctx
, dl
);
6046 ctx
.print_resources
= false;
6047 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
6048 DL_OPT_RESOURCE_SIZE
, 0);
6052 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
6054 dl_opts_put(nlh
, dl
);
6055 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
6057 pr_err("error getting resources %s\n", strerror(ctx
.err
));
6061 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
6062 &dl
->opts
.resource_id
,
6063 &dl
->opts
.resource_id_valid
);
6065 pr_err("error parsing resource path %s\n", strerror(-err
));
6069 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
6070 NLM_F_REQUEST
| NLM_F_ACK
);
6072 dl_opts_put(nlh
, dl
);
6073 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6075 resource_ctx_fini(&ctx
);
6079 static int cmd_resource(struct dl
*dl
)
6081 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6082 cmd_resource_help();
6084 } else if (dl_argv_match(dl
, "show")) {
6086 return cmd_resource_show(dl
);
6087 } else if (dl_argv_match(dl
, "set")) {
6089 return cmd_resource_set(dl
);
6091 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6095 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
6097 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
6098 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
6099 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
6102 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
6103 if (dl
->json_output
) {
6104 jsonw_name(dl
->jw
, buf
);
6105 jsonw_start_object(dl
->jw
);
6111 static void pr_out_region_handle_end(struct dl
*dl
)
6113 if (dl
->json_output
)
6114 jsonw_end_object(dl
->jw
);
6119 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
6121 if (dl
->json_output
) {
6122 jsonw_name(dl
->jw
, "snapshot");
6123 jsonw_start_array(dl
->jw
);
6125 if (g_indent_newline
)
6126 pr_out("snapshot %s", array
? "[" : "");
6128 pr_out(" snapshot %s", array
? "[" : "");
6132 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
6134 if (dl
->json_output
)
6135 jsonw_end_array(dl
->jw
);
6140 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
6142 uint32_t snapshot_id
;
6144 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6147 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
6149 if (dl
->json_output
)
6150 jsonw_uint(dl
->jw
, snapshot_id
);
6152 pr_out("%s%u", index
? " " : "", snapshot_id
);
6155 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
6157 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
6158 struct nlattr
*nla_sanpshot
;
6161 pr_out_region_snapshots_start(dl
, true);
6162 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
6163 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
6164 if (err
!= MNL_CB_OK
)
6166 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
6168 pr_out_region_snapshots_end(dl
, true);
6171 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
6173 pr_out_region_snapshots_start(dl
, false);
6174 pr_out_region_snapshots_id(dl
, tb
, 0);
6175 pr_out_region_snapshots_end(dl
, false);
6178 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
6180 pr_out_region_handle_start(dl
, tb
);
6182 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
6183 pr_out_u64(dl
, "size",
6184 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
6186 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
6187 pr_out_snapshots(dl
, tb
);
6189 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
6190 pr_out_snapshot(dl
, tb
);
6192 pr_out_region_handle_end(dl
);
6195 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6197 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6198 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6199 struct dl
*dl
= data
;
6201 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6202 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6203 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
6204 return MNL_CB_ERROR
;
6206 pr_out_region(dl
, tb
);
6211 static int cmd_region_show(struct dl
*dl
)
6213 struct nlmsghdr
*nlh
;
6214 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6217 if (dl_argc(dl
) == 0)
6218 flags
|= NLM_F_DUMP
;
6220 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
6222 if (dl_argc(dl
) > 0) {
6223 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
6228 pr_out_section_start(dl
, "regions");
6229 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
6230 pr_out_section_end(dl
);
6234 static int cmd_region_snapshot_del(struct dl
*dl
)
6236 struct nlmsghdr
*nlh
;
6239 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
6240 NLM_F_REQUEST
| NLM_F_ACK
);
6242 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6243 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6247 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6250 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
6252 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
6253 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6254 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
6255 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6256 struct dl
*dl
= data
;
6259 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6260 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6261 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
6262 return MNL_CB_ERROR
;
6264 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
6265 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
6266 if (err
!= MNL_CB_OK
)
6267 return MNL_CB_ERROR
;
6269 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
6270 if (!nla_chunk_data
)
6273 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
6274 if (!nla_chunk_addr
)
6277 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
6278 mnl_attr_get_payload_len(nla_chunk_data
),
6279 mnl_attr_get_u64(nla_chunk_addr
));
6284 static int cmd_region_dump(struct dl
*dl
)
6286 struct nlmsghdr
*nlh
;
6289 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6290 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6292 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6293 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6297 pr_out_section_start(dl
, "dump");
6298 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6299 pr_out_section_end(dl
);
6300 if (!dl
->json_output
)
6305 static int cmd_region_read(struct dl
*dl
)
6307 struct nlmsghdr
*nlh
;
6310 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
6311 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
6313 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
6314 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
6315 DL_OPT_REGION_SNAPSHOT_ID
, 0);
6319 pr_out_section_start(dl
, "read");
6320 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
6321 pr_out_section_end(dl
);
6322 if (!dl
->json_output
)
6327 static void cmd_region_help(void)
6329 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
6330 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
6331 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
6332 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
6335 static int cmd_region(struct dl
*dl
)
6337 if (dl_no_arg(dl
)) {
6338 return cmd_region_show(dl
);
6339 } else if (dl_argv_match(dl
, "help")) {
6342 } else if (dl_argv_match(dl
, "show")) {
6344 return cmd_region_show(dl
);
6345 } else if (dl_argv_match(dl
, "del")) {
6347 return cmd_region_snapshot_del(dl
);
6348 } else if (dl_argv_match(dl
, "dump")) {
6350 return cmd_region_dump(dl
);
6351 } else if (dl_argv_match(dl
, "read")) {
6353 return cmd_region_read(dl
);
6355 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6359 static int cmd_health_set_params(struct dl
*dl
)
6361 struct nlmsghdr
*nlh
;
6364 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
6365 NLM_F_REQUEST
| NLM_F_ACK
);
6366 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
6367 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
6368 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
6372 dl_opts_put(nlh
, dl
);
6373 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6376 static int cmd_health_dump_clear(struct dl
*dl
)
6378 struct nlmsghdr
*nlh
;
6381 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6382 NLM_F_REQUEST
| NLM_F_ACK
);
6384 err
= dl_argv_parse_put(nlh
, dl
,
6385 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6389 dl_opts_put(nlh
, dl
);
6390 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6393 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
6400 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
6403 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
6406 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
6409 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
6412 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
6414 case MNL_TYPE_NUL_STRING
:
6415 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
6417 case MNL_TYPE_BINARY
:
6418 len
= mnl_attr_get_payload_len(nl_data
);
6419 data
= mnl_attr_get_payload(nl_data
);
6420 pr_out_binary_value(dl
, data
, len
);
6430 struct list_head list
;
6433 struct fmsg_cb_data
{
6436 struct list_head entry_list
;
6439 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
6440 uint8_t *attr_value
, bool insert
)
6442 struct nest_entry
*entry
;
6445 entry
= malloc(sizeof(struct nest_entry
));
6449 entry
->attr_type
= *attr_value
;
6450 list_add(&entry
->list
, &fmsg_data
->entry_list
);
6452 if (list_empty(&fmsg_data
->entry_list
))
6453 return MNL_CB_ERROR
;
6454 entry
= list_first_entry(&fmsg_data
->entry_list
,
6455 struct nest_entry
, list
);
6456 *attr_value
= entry
->attr_type
;
6457 list_del(&entry
->list
);
6463 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
6466 struct dl
*dl
= fmsg_data
->dl
;
6467 uint8_t value
= nest_value
;
6470 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6471 if (err
!= MNL_CB_OK
)
6475 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6477 pr_out_entry_start(dl
);
6479 pr_out_entry_end(dl
);
6481 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6483 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6484 if (dl
->json_output
) {
6486 jsonw_start_array(dl
->jw
);
6488 jsonw_end_array(dl
->jw
);
6492 __pr_out_indent_inc();
6494 __pr_out_indent_dec();
6504 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6506 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6507 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6508 struct fmsg_cb_data
*fmsg_data
= data
;
6509 struct dl
*dl
= fmsg_data
->dl
;
6510 struct nlattr
*nla_object
;
6514 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6515 if (!tb
[DEVLINK_ATTR_FMSG
])
6516 return MNL_CB_ERROR
;
6518 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6519 attr_type
= mnl_attr_get_type(nla_object
);
6520 switch (attr_type
) {
6521 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6522 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6523 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6524 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6525 if (err
!= MNL_CB_OK
)
6528 case DEVLINK_ATTR_FMSG_NEST_END
:
6529 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6530 if (err
!= MNL_CB_OK
)
6533 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6534 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6536 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6537 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6539 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6540 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6542 if (err
!= MNL_CB_OK
)
6552 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
, uint16_t flags
)
6554 struct fmsg_cb_data data
;
6555 struct nlmsghdr
*nlh
;
6558 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, flags
| NLM_F_REQUEST
| NLM_F_ACK
);
6560 err
= dl_argv_parse_put(nlh
, dl
,
6561 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6566 INIT_LIST_HEAD(&data
.entry_list
);
6567 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6571 static int cmd_health_dump_show(struct dl
*dl
)
6573 return cmd_health_object_common(dl
,
6574 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6578 static int cmd_health_diagnose(struct dl
*dl
)
6580 return cmd_health_object_common(dl
,
6581 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6585 static int cmd_health_recover(struct dl
*dl
)
6587 struct nlmsghdr
*nlh
;
6590 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6591 NLM_F_REQUEST
| NLM_F_ACK
);
6593 err
= dl_argv_parse_put(nlh
, dl
,
6594 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6598 dl_opts_put(nlh
, dl
);
6599 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6602 enum devlink_health_reporter_state
{
6603 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6604 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6607 static const char *health_state_name(uint8_t state
)
6610 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6611 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6612 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6613 return HEALTH_REPORTER_STATE_ERROR_STR
;
6615 return "<unknown state>";
6619 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6621 struct sysinfo s_info
;
6627 info
= localtime(&now
);
6628 err
= sysinfo(&s_info
);
6631 /* Subtract uptime in sec from now yields the time of system
6632 * uptime. To this, add time_ms which is the amount of
6633 * milliseconds elapsed between uptime and the dump taken.
6635 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6636 info
= localtime(&sec
);
6638 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6639 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6642 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6644 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6645 enum devlink_health_reporter_state state
;
6646 const struct nlattr
*attr
;
6650 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6652 if (err
!= MNL_CB_OK
)
6655 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6656 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6657 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6658 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6661 pr_out_handle_start_arr(dl
, tb_health
);
6663 pr_out_str(dl
, "name",
6664 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6665 if (!dl
->json_output
) {
6667 __pr_out_indent_inc();
6669 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6670 pr_out_str(dl
, "state", health_state_name(state
));
6671 pr_out_u64(dl
, "error",
6672 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6673 pr_out_u64(dl
, "recover",
6674 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6675 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6676 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6677 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6679 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6680 time_ms
= mnl_attr_get_u64(attr
);
6681 format_logtime(time_ms
, dump_date
, dump_time
);
6683 pr_out_str(dl
, "last_dump_date", dump_date
);
6684 pr_out_str(dl
, "last_dump_time", dump_time
);
6686 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6687 pr_out_u64(dl
, "grace_period",
6688 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6689 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6690 pr_out_bool(dl
, "auto_recover",
6691 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6693 __pr_out_indent_dec();
6694 pr_out_handle_end(dl
);
6697 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6699 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6700 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6701 struct dl
*dl
= data
;
6703 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6704 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6705 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6706 return MNL_CB_ERROR
;
6708 pr_out_health(dl
, tb
);
6713 static int cmd_health_show(struct dl
*dl
)
6715 struct nlmsghdr
*nlh
;
6716 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6719 if (dl_argc(dl
) == 0)
6720 flags
|= NLM_F_DUMP
;
6721 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6724 if (dl_argc(dl
) > 0) {
6725 err
= dl_argv_parse_put(nlh
, dl
,
6727 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6731 pr_out_section_start(dl
, "health");
6733 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6734 pr_out_section_end(dl
);
6738 static void cmd_health_help(void)
6740 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6741 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6742 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6743 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6744 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6745 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6748 static int cmd_health(struct dl
*dl
)
6750 if (dl_argv_match(dl
, "help")) {
6753 } else if (dl_argv_match(dl
, "show") ||
6754 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6756 return cmd_health_show(dl
);
6757 } else if (dl_argv_match(dl
, "recover")) {
6759 return cmd_health_recover(dl
);
6760 } else if (dl_argv_match(dl
, "diagnose")) {
6762 return cmd_health_diagnose(dl
);
6763 } else if (dl_argv_match(dl
, "dump")) {
6765 if (dl_argv_match(dl
, "show")) {
6767 return cmd_health_dump_show(dl
);
6768 } else if (dl_argv_match(dl
, "clear")) {
6770 return cmd_health_dump_clear(dl
);
6772 } else if (dl_argv_match(dl
, "set")) {
6774 return cmd_health_set_params(dl
);
6776 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6780 static const char *trap_type_name(uint8_t type
)
6783 case DEVLINK_TRAP_TYPE_DROP
:
6785 case DEVLINK_TRAP_TYPE_EXCEPTION
:
6788 return "<unknown type>";
6792 static const char *trap_action_name(uint8_t action
)
6795 case DEVLINK_TRAP_ACTION_DROP
:
6797 case DEVLINK_TRAP_ACTION_TRAP
:
6800 return "<unknown action>";
6804 static const char *trap_metadata_name(const struct nlattr
*attr
)
6806 switch (attr
->nla_type
) {
6807 case DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
:
6808 return "input_port";
6810 return "<unknown metadata type>";
6813 static void pr_out_trap_metadata(struct dl
*dl
, struct nlattr
*attr
)
6815 struct nlattr
*attr_metadata
;
6817 pr_out_array_start(dl
, "metadata");
6818 mnl_attr_for_each_nested(attr_metadata
, attr
)
6819 pr_out_str_value(dl
, trap_metadata_name(attr_metadata
));
6820 pr_out_array_end(dl
);
6823 static void pr_out_trap(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6825 uint8_t action
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_ACTION
]);
6826 uint8_t type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_TRAP_TYPE
]);
6829 pr_out_handle_start_arr(dl
, tb
);
6831 __pr_out_handle_start(dl
, tb
, true, false);
6833 pr_out_str(dl
, "name", mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_NAME
]));
6834 pr_out_str(dl
, "type", trap_type_name(type
));
6835 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6836 pr_out_str(dl
, "action", trap_action_name(action
));
6837 pr_out_str(dl
, "group",
6838 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6840 pr_out_trap_metadata(dl
, tb
[DEVLINK_ATTR_TRAP_METADATA
]);
6841 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6842 pr_out_handle_end(dl
);
6845 static int cmd_trap_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6847 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6848 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6849 struct dl
*dl
= data
;
6851 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6852 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6853 !tb
[DEVLINK_ATTR_TRAP_NAME
] || !tb
[DEVLINK_ATTR_TRAP_TYPE
] ||
6854 !tb
[DEVLINK_ATTR_TRAP_ACTION
] ||
6855 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] ||
6856 !tb
[DEVLINK_ATTR_TRAP_METADATA
] || !tb
[DEVLINK_ATTR_STATS
])
6857 return MNL_CB_ERROR
;
6859 pr_out_trap(dl
, tb
, true);
6864 static void cmd_trap_help(void)
6866 pr_err("Usage: devlink trap set DEV trap TRAP [ action { trap | drop } ]\n");
6867 pr_err(" devlink trap show [ DEV trap TRAP ]\n");
6868 pr_err(" devlink trap group set DEV group GROUP [ action { trap | drop } ]\n");
6869 pr_err(" devlink trap group show [ DEV group GROUP ]\n");
6872 static int cmd_trap_show(struct dl
*dl
)
6874 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6875 struct nlmsghdr
*nlh
;
6878 if (dl_argc(dl
) == 0)
6879 flags
|= NLM_F_DUMP
;
6881 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GET
, flags
);
6883 if (dl_argc(dl
) > 0) {
6884 err
= dl_argv_parse_put(nlh
, dl
,
6885 DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
, 0);
6890 pr_out_section_start(dl
, "trap");
6891 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_show_cb
, dl
);
6892 pr_out_section_end(dl
);
6897 static int cmd_trap_set(struct dl
*dl
)
6899 struct nlmsghdr
*nlh
;
6902 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_SET
,
6903 NLM_F_REQUEST
| NLM_F_ACK
);
6905 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_TRAP_NAME
,
6906 DL_OPT_TRAP_ACTION
);
6910 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6913 static void pr_out_trap_group(struct dl
*dl
, struct nlattr
**tb
, bool array
)
6916 pr_out_handle_start_arr(dl
, tb
);
6918 __pr_out_handle_start(dl
, tb
, true, false);
6920 pr_out_str(dl
, "name",
6921 mnl_attr_get_str(tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
]));
6922 pr_out_bool(dl
, "generic", !!tb
[DEVLINK_ATTR_TRAP_GENERIC
]);
6923 pr_out_stats(dl
, tb
[DEVLINK_ATTR_STATS
]);
6924 pr_out_handle_end(dl
);
6927 static int cmd_trap_group_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6929 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6930 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6931 struct dl
*dl
= data
;
6933 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6934 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6935 !tb
[DEVLINK_ATTR_TRAP_GROUP_NAME
] || !tb
[DEVLINK_ATTR_STATS
])
6936 return MNL_CB_ERROR
;
6938 pr_out_trap_group(dl
, tb
, true);
6943 static int cmd_trap_group_show(struct dl
*dl
)
6945 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6946 struct nlmsghdr
*nlh
;
6949 if (dl_argc(dl
) == 0)
6950 flags
|= NLM_F_DUMP
;
6952 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_GET
, flags
);
6954 if (dl_argc(dl
) > 0) {
6955 err
= dl_argv_parse_put(nlh
, dl
,
6956 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
6962 pr_out_section_start(dl
, "trap_group");
6963 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_trap_group_show_cb
, dl
);
6964 pr_out_section_end(dl
);
6969 static int cmd_trap_group_set(struct dl
*dl
)
6971 struct nlmsghdr
*nlh
;
6974 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_TRAP_GROUP_SET
,
6975 NLM_F_REQUEST
| NLM_F_ACK
);
6977 err
= dl_argv_parse_put(nlh
, dl
,
6978 DL_OPT_HANDLE
| DL_OPT_TRAP_GROUP_NAME
,
6979 DL_OPT_TRAP_ACTION
);
6983 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6986 static int cmd_trap_group(struct dl
*dl
)
6988 if (dl_argv_match(dl
, "help")) {
6991 } else if (dl_argv_match(dl
, "show") ||
6992 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6994 return cmd_trap_group_show(dl
);
6995 } else if (dl_argv_match(dl
, "set")) {
6997 return cmd_trap_group_set(dl
);
6999 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7003 static int cmd_trap(struct dl
*dl
)
7005 if (dl_argv_match(dl
, "help")) {
7008 } else if (dl_argv_match(dl
, "show") ||
7009 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
7011 return cmd_trap_show(dl
);
7012 } else if (dl_argv_match(dl
, "set")) {
7014 return cmd_trap_set(dl
);
7015 } else if (dl_argv_match(dl
, "group")) {
7017 return cmd_trap_group(dl
);
7019 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
7023 static void help(void)
7025 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
7026 " devlink [ -f[orce] ] -b[atch] filename\n"
7027 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health | trap }\n"
7028 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] -s[tatistics] }\n");
7031 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
7036 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
7039 } else if (dl_argv_match(dl
, "dev")) {
7042 } else if (dl_argv_match(dl
, "port")) {
7044 return cmd_port(dl
);
7045 } else if (dl_argv_match(dl
, "sb")) {
7048 } else if (dl_argv_match(dl
, "monitor")) {
7051 } else if (dl_argv_match(dl
, "dpipe")) {
7053 return cmd_dpipe(dl
);
7054 } else if (dl_argv_match(dl
, "resource")) {
7056 return cmd_resource(dl
);
7057 } else if (dl_argv_match(dl
, "region")) {
7059 return cmd_region(dl
);
7060 } else if (dl_argv_match(dl
, "health")) {
7062 return cmd_health(dl
);
7063 } else if (dl_argv_match(dl
, "trap")) {
7065 return cmd_trap(dl
);
7067 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
7071 static int dl_init(struct dl
*dl
)
7075 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
7077 pr_err("Failed to connect to devlink Netlink\n");
7081 err
= ifname_map_init(dl
);
7083 pr_err("Failed to create index map\n");
7084 goto err_ifname_map_create
;
7086 if (dl
->json_output
) {
7087 dl
->jw
= jsonw_new(stdout
);
7089 pr_err("Failed to create JSON writer\n");
7092 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
7097 ifname_map_fini(dl
);
7098 err_ifname_map_create
:
7099 mnlg_socket_close(dl
->nlg
);
7103 static void dl_fini(struct dl
*dl
)
7105 if (dl
->json_output
)
7106 jsonw_destroy(&dl
->jw
);
7107 ifname_map_fini(dl
);
7108 mnlg_socket_close(dl
->nlg
);
7111 static struct dl
*dl_alloc(void)
7115 dl
= calloc(1, sizeof(*dl
));
7121 static void dl_free(struct dl
*dl
)
7126 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
7130 int ret
= EXIT_SUCCESS
;
7132 if (name
&& strcmp(name
, "-") != 0) {
7133 if (freopen(name
, "r", stdin
) == NULL
) {
7135 "Cannot open file \"%s\" for reading: %s\n",
7136 name
, strerror(errno
));
7137 return EXIT_FAILURE
;
7142 while (getcmdline(&line
, &len
, stdin
) != -1) {
7146 largc
= makeargs(line
, largv
, 100);
7148 continue; /* blank line */
7150 if (dl_cmd(dl
, largc
, largv
)) {
7151 fprintf(stderr
, "Command failed %s:%d\n",
7165 int main(int argc
, char **argv
)
7167 static const struct option long_options
[] = {
7168 { "Version", no_argument
, NULL
, 'V' },
7169 { "force", no_argument
, NULL
, 'f' },
7170 { "batch", required_argument
, NULL
, 'b' },
7171 { "no-nice-names", no_argument
, NULL
, 'n' },
7172 { "json", no_argument
, NULL
, 'j' },
7173 { "pretty", no_argument
, NULL
, 'p' },
7174 { "verbose", no_argument
, NULL
, 'v' },
7175 { "statistics", no_argument
, NULL
, 's' },
7176 { NULL
, 0, NULL
, 0 }
7178 const char *batch_file
= NULL
;
7187 pr_err("Failed to allocate memory for devlink\n");
7188 return EXIT_FAILURE
;
7191 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpvs",
7192 long_options
, NULL
)) >= 0) {
7196 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
7203 batch_file
= optarg
;
7206 dl
->no_nice_names
= true;
7209 dl
->json_output
= true;
7212 dl
->pretty_output
= true;
7221 pr_err("Unknown option.\n");
7238 err
= dl_batch(dl
, batch_file
, force
);
7240 err
= dl_cmd(dl
, argc
, argv
);