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 <linux/genetlink.h>
23 #include <linux/devlink.h>
24 #include <libmnl/libmnl.h>
25 #include <netinet/ether.h>
26 #include <sys/sysinfo.h>
27 #include <sys/queue.h>
32 #include "json_writer.h"
35 #define ESWITCH_MODE_LEGACY "legacy"
36 #define ESWITCH_MODE_SWITCHDEV "switchdev"
37 #define ESWITCH_INLINE_MODE_NONE "none"
38 #define ESWITCH_INLINE_MODE_LINK "link"
39 #define ESWITCH_INLINE_MODE_NETWORK "network"
40 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
42 #define PARAM_CMODE_RUNTIME_STR "runtime"
43 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
44 #define PARAM_CMODE_PERMANENT_STR "permanent"
45 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
47 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
48 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
49 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
51 static int g_new_line_count
;
52 static int g_indent_level
;
53 static bool g_indent_newline
;
55 #define INDENT_STR_STEP 2
56 #define INDENT_STR_MAXLEN 32
57 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
59 static void __attribute__((format(printf
, 1, 2)))
60 pr_err(const char *fmt
, ...)
65 vfprintf(stderr
, fmt
, ap
);
69 static void __attribute__((format(printf
, 1, 2)))
70 pr_out(const char *fmt
, ...)
74 if (g_indent_newline
) {
75 printf("%s", g_indent_str
);
76 g_indent_newline
= false;
84 static void __attribute__((format(printf
, 2, 3)))
85 pr_out_sp(unsigned int num
, const char *fmt
, ...)
91 ret
= vprintf(fmt
, ap
);
95 printf("%*s", num
- ret
, "");
96 g_new_line_count
= 0; \
99 static void __pr_out_indent_inc(void)
101 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
103 g_indent_level
+= INDENT_STR_STEP
;
104 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
105 g_indent_str
[g_indent_level
] = '\0';
108 static void __pr_out_indent_dec(void)
110 if (g_indent_level
- INDENT_STR_STEP
< 0)
112 g_indent_level
-= INDENT_STR_STEP
;
113 g_indent_str
[g_indent_level
] = '\0';
116 static void __pr_out_newline(void)
118 if (g_new_line_count
< 1) {
120 g_indent_newline
= true;
125 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
126 mnl_cb_t data_cb
, void *data
)
130 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
132 pr_err("devlink answers: %s\n", strerror(errno
));
138 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
139 const struct nlmsghdr
*nlh
,
140 mnl_cb_t data_cb
, void *data
)
144 err
= mnlg_socket_send(nlg
, nlh
);
146 pr_err("Failed to call mnlg_socket_send\n");
149 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
152 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
153 const char *group_name
)
157 err
= mnlg_socket_group_add(nlg
, group_name
);
159 pr_err("Failed to call mnlg_socket_group_add\n");
166 struct list_head list
;
173 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
174 const char *dev_name
,
178 struct ifname_map
*ifname_map
;
180 ifname_map
= calloc(1, sizeof(*ifname_map
));
183 ifname_map
->bus_name
= strdup(bus_name
);
184 ifname_map
->dev_name
= strdup(dev_name
);
185 ifname_map
->port_index
= port_index
;
186 ifname_map
->ifname
= strdup(ifname
);
187 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
188 !ifname_map
->ifname
) {
189 free(ifname_map
->ifname
);
190 free(ifname_map
->dev_name
);
191 free(ifname_map
->bus_name
);
198 static void ifname_map_free(struct ifname_map
*ifname_map
)
200 free(ifname_map
->ifname
);
201 free(ifname_map
->dev_name
);
202 free(ifname_map
->bus_name
);
206 #define DL_OPT_HANDLE BIT(0)
207 #define DL_OPT_HANDLEP BIT(1)
208 #define DL_OPT_PORT_TYPE BIT(2)
209 #define DL_OPT_PORT_COUNT BIT(3)
210 #define DL_OPT_SB BIT(4)
211 #define DL_OPT_SB_POOL BIT(5)
212 #define DL_OPT_SB_SIZE BIT(6)
213 #define DL_OPT_SB_TYPE BIT(7)
214 #define DL_OPT_SB_THTYPE BIT(8)
215 #define DL_OPT_SB_TH BIT(9)
216 #define DL_OPT_SB_TC BIT(10)
217 #define DL_OPT_ESWITCH_MODE BIT(11)
218 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
219 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
220 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
221 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
222 #define DL_OPT_RESOURCE_PATH BIT(16)
223 #define DL_OPT_RESOURCE_SIZE BIT(17)
224 #define DL_OPT_PARAM_NAME BIT(18)
225 #define DL_OPT_PARAM_VALUE BIT(19)
226 #define DL_OPT_PARAM_CMODE BIT(20)
227 #define DL_OPT_HANDLE_REGION BIT(21)
228 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
229 #define DL_OPT_REGION_ADDRESS BIT(23)
230 #define DL_OPT_REGION_LENGTH BIT(24)
231 #define DL_OPT_FLASH_FILE_NAME BIT(25)
232 #define DL_OPT_FLASH_COMPONENT BIT(26)
233 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
234 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(27)
235 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(28)
238 uint32_t present
; /* flags of present items */
242 enum devlink_port_type port_type
;
245 uint16_t sb_pool_index
;
246 uint32_t sb_pool_size
;
247 enum devlink_sb_pool_type sb_pool_type
;
248 enum devlink_sb_threshold_type sb_pool_thtype
;
249 uint32_t sb_threshold
;
250 uint16_t sb_tc_index
;
251 enum devlink_eswitch_mode eswitch_mode
;
252 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
253 const char *dpipe_table_name
;
254 bool dpipe_counters_enable
;
255 bool eswitch_encap_mode
;
256 const char *resource_path
;
257 uint32_t resource_size
;
258 uint32_t resource_id
;
259 bool resource_id_valid
;
260 const char *param_name
;
261 const char *param_value
;
262 enum devlink_param_cmode cmode
;
264 uint32_t region_snapshot_id
;
265 uint64_t region_address
;
266 uint64_t region_length
;
267 const char *flash_file_name
;
268 const char *flash_component
;
269 const char *reporter_name
;
270 uint64_t reporter_graceful_period
;
271 bool reporter_auto_recover
;
275 struct mnlg_socket
*nlg
;
276 struct list_head ifname_map_list
;
293 static int dl_argc(struct dl
*dl
)
298 static char *dl_argv(struct dl
*dl
)
300 if (dl_argc(dl
) == 0)
305 static void dl_arg_inc(struct dl
*dl
)
307 if (dl_argc(dl
) == 0)
313 static char *dl_argv_next(struct dl
*dl
)
317 if (dl_argc(dl
) == 0)
325 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
327 if (index
>= dl_argc(dl
))
329 return dl
->argv
[index
];
332 static int strcmpx(const char *str1
, const char *str2
)
334 if (strlen(str1
) > strlen(str2
))
336 return strncmp(str1
, str2
, strlen(str1
));
339 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
341 if (dl_argc(dl
) == 0)
343 return strcmpx(dl_argv(dl
), pattern
) == 0;
346 static bool dl_no_arg(struct dl
*dl
)
348 return dl_argc(dl
) == 0;
351 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
352 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
353 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
354 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
355 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
356 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
357 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
358 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
359 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
360 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
361 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
362 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
363 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
364 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
365 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
366 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
367 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
368 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
369 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
370 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
371 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
372 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
373 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
374 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
375 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
376 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
377 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
378 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
379 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
380 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
381 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
382 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
383 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
384 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
385 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
386 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
387 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
388 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
389 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
390 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
391 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
392 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
393 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
394 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
395 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
396 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
397 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
398 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
399 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
400 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
401 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
402 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
403 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
404 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
405 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
406 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
407 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
408 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
409 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
410 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
411 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
412 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
413 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
414 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
415 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
416 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
417 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
418 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
419 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
420 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
421 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
422 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
423 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
424 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
425 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
426 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
427 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
428 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
429 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
430 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
431 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
432 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
433 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
434 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
435 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
436 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
437 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
438 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
441 static int attr_cb(const struct nlattr
*attr
, void *data
)
443 const struct nlattr
**tb
= data
;
446 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
449 type
= mnl_attr_get_type(attr
);
450 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
457 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
459 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
460 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
461 struct dl
*dl
= data
;
462 struct ifname_map
*ifname_map
;
463 const char *bus_name
;
464 const char *dev_name
;
465 uint32_t port_ifindex
;
466 const char *port_ifname
;
468 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
469 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
470 !tb
[DEVLINK_ATTR_PORT_INDEX
])
473 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
476 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
477 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
478 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
479 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
480 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
481 port_ifindex
, port_ifname
);
484 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
489 static void ifname_map_fini(struct dl
*dl
)
491 struct ifname_map
*ifname_map
, *tmp
;
493 list_for_each_entry_safe(ifname_map
, tmp
,
494 &dl
->ifname_map_list
, list
) {
495 list_del(&ifname_map
->list
);
496 ifname_map_free(ifname_map
);
500 static int ifname_map_init(struct dl
*dl
)
502 struct nlmsghdr
*nlh
;
505 INIT_LIST_HEAD(&dl
->ifname_map_list
);
507 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
508 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
510 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
518 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
519 char **p_bus_name
, char **p_dev_name
,
520 uint32_t *p_port_index
)
522 struct ifname_map
*ifname_map
;
524 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
525 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
526 *p_bus_name
= ifname_map
->bus_name
;
527 *p_dev_name
= ifname_map
->dev_name
;
528 *p_port_index
= ifname_map
->port_index
;
535 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
536 const char *dev_name
, uint32_t port_index
,
539 struct ifname_map
*ifname_map
;
541 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
542 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
543 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
544 port_index
== ifname_map
->port_index
) {
545 *p_ifname
= ifname_map
->ifname
;
552 static unsigned int strslashcount(char *str
)
554 unsigned int count
= 0;
557 while ((pos
= strchr(pos
, '/'))) {
564 static int strslashrsplit(char *str
, char **before
, char **after
)
568 slash
= strrchr(str
, '/');
577 static int strtouint64_t(const char *str
, uint64_t *p_val
)
580 unsigned long long int val
;
582 val
= strtoull(str
, &endptr
, 10);
583 if (endptr
== str
|| *endptr
!= '\0')
591 static int strtouint32_t(const char *str
, uint32_t *p_val
)
594 unsigned long int val
;
596 val
= strtoul(str
, &endptr
, 10);
597 if (endptr
== str
|| *endptr
!= '\0')
605 static int strtouint16_t(const char *str
, uint16_t *p_val
)
608 unsigned long int val
;
610 val
= strtoul(str
, &endptr
, 10);
611 if (endptr
== str
|| *endptr
!= '\0')
619 static int strtouint8_t(const char *str
, uint8_t *p_val
)
622 unsigned long int val
;
624 val
= strtoul(str
, &endptr
, 10);
625 if (endptr
== str
|| *endptr
!= '\0')
633 static int strtobool(const char *str
, bool *p_val
)
637 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
639 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
647 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
649 strslashrsplit(str
, p_bus_name
, p_dev_name
);
653 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
655 char *str
= dl_argv_next(dl
);
658 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
661 if (strslashcount(str
) != 1) {
662 pr_err("Wrong devlink identification string format.\n");
663 pr_err("Expected \"bus_name/dev_name\".\n");
666 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
669 static int __dl_argv_handle_port(char *str
,
670 char **p_bus_name
, char **p_dev_name
,
671 uint32_t *p_port_index
)
677 err
= strslashrsplit(str
, &handlestr
, &portstr
);
679 pr_err("Port identification \"%s\" is invalid\n", str
);
682 err
= strtouint32_t(portstr
, p_port_index
);
684 pr_err("Port index \"%s\" is not a number or not within range\n",
688 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
690 pr_err("Port identification \"%s\" is invalid\n", str
);
696 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
697 char **p_bus_name
, char **p_dev_name
,
698 uint32_t *p_port_index
)
702 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
705 pr_err("Netdevice \"%s\" not found\n", str
);
711 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
712 char **p_dev_name
, uint32_t *p_port_index
)
714 char *str
= dl_argv_next(dl
);
715 unsigned int slash_count
;
718 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
721 slash_count
= strslashcount(str
);
722 switch (slash_count
) {
724 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
725 p_dev_name
, p_port_index
);
727 return __dl_argv_handle_port(str
, p_bus_name
,
728 p_dev_name
, p_port_index
);
730 pr_err("Wrong port identification string format.\n");
731 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
736 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
737 char **p_dev_name
, uint32_t *p_port_index
,
738 uint32_t *p_handle_bit
)
740 char *str
= dl_argv_next(dl
);
741 unsigned int slash_count
;
745 pr_err("One of following identifications expected:\n"
746 "Devlink identification (\"bus_name/dev_name\")\n"
747 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
750 slash_count
= strslashcount(str
);
751 if (slash_count
== 1) {
752 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
755 *p_handle_bit
= DL_OPT_HANDLE
;
756 } else if (slash_count
== 2) {
757 err
= __dl_argv_handle_port(str
, p_bus_name
,
758 p_dev_name
, p_port_index
);
761 *p_handle_bit
= DL_OPT_HANDLEP
;
762 } else if (slash_count
== 0) {
763 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
764 p_dev_name
, p_port_index
);
767 *p_handle_bit
= DL_OPT_HANDLEP
;
769 pr_err("Wrong port identification string format.\n");
770 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
776 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
777 char **p_dev_name
, char **p_region
)
782 err
= strslashrsplit(str
, &handlestr
, p_region
);
784 pr_err("Region identification \"%s\" is invalid\n", str
);
787 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
789 pr_err("Region identification \"%s\" is invalid\n", str
);
795 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
796 char **p_dev_name
, char **p_region
)
798 char *str
= dl_argv_next(dl
);
799 unsigned int slash_count
;
802 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
806 slash_count
= strslashcount(str
);
807 if (slash_count
!= 2) {
808 pr_err("Wrong region identification string format.\n");
809 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
813 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
816 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
818 char *str
= dl_argv_next(dl
);
822 pr_err("Unsigned number argument expected\n");
826 err
= strtouint64_t(str
, p_val
);
828 pr_err("\"%s\" is not a number or not within range\n", str
);
834 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
836 char *str
= dl_argv_next(dl
);
840 pr_err("Unsigned number argument expected\n");
844 err
= strtouint32_t(str
, p_val
);
846 pr_err("\"%s\" is not a number or not within range\n", str
);
852 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
854 char *str
= dl_argv_next(dl
);
858 pr_err("Unsigned number argument expected\n");
862 err
= strtouint16_t(str
, p_val
);
864 pr_err("\"%s\" is not a number or not within range\n", str
);
870 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
872 char *str
= dl_argv_next(dl
);
876 pr_err("Boolean argument expected\n");
880 err
= strtobool(str
, p_val
);
882 pr_err("\"%s\" is not a valid boolean value\n", str
);
888 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
890 const char *str
= dl_argv_next(dl
);
893 pr_err("String parameter expected\n");
900 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
902 if (strcmp(typestr
, "auto") == 0) {
903 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
904 } else if (strcmp(typestr
, "eth") == 0) {
905 *p_type
= DEVLINK_PORT_TYPE_ETH
;
906 } else if (strcmp(typestr
, "ib") == 0) {
907 *p_type
= DEVLINK_PORT_TYPE_IB
;
909 pr_err("Unknown port type \"%s\"\n", typestr
);
915 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
917 if (strcmp(typestr
, "ingress") == 0) {
918 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
919 } else if (strcmp(typestr
, "egress") == 0) {
920 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
922 pr_err("Unknown pool type \"%s\"\n", typestr
);
928 static int threshold_type_get(const char *typestr
,
929 enum devlink_sb_threshold_type
*p_type
)
931 if (strcmp(typestr
, "static") == 0) {
932 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
933 } else if (strcmp(typestr
, "dynamic") == 0) {
934 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
936 pr_err("Unknown threshold type \"%s\"\n", typestr
);
942 static int eswitch_mode_get(const char *typestr
,
943 enum devlink_eswitch_mode
*p_mode
)
945 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
946 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
947 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
948 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
950 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
956 static int eswitch_inline_mode_get(const char *typestr
,
957 enum devlink_eswitch_inline_mode
*p_mode
)
959 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
960 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
961 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
962 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
963 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
964 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
965 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
966 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
968 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
974 static int dpipe_counters_enable_get(const char *typestr
,
975 bool *counters_enable
)
977 if (strcmp(typestr
, "enable") == 0) {
978 *counters_enable
= 1;
979 } else if (strcmp(typestr
, "disable") == 0) {
980 *counters_enable
= 0;
982 pr_err("Unknown counter_state \"%s\"\n", typestr
);
988 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
990 if (strcmp(typestr
, "enable") == 0) {
992 } else if (strcmp(typestr
, "disable") == 0) {
995 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1001 static int param_cmode_get(const char *cmodestr
,
1002 enum devlink_param_cmode
*cmode
)
1004 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1005 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1006 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1007 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1008 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1009 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1011 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1017 struct dl_args_metadata
{
1019 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1022 static const struct dl_args_metadata dl_args_required
[] = {
1023 {DL_OPT_PORT_TYPE
, "Port type not set."},
1024 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1025 {DL_OPT_SB_POOL
, "Pool index option expected."},
1026 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1027 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1028 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1029 {DL_OPT_SB_TH
, "Threshold option expected."},
1030 {DL_OPT_SB_TC
, "TC index option expected."},
1031 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1032 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1033 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1034 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1035 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1036 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1037 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1038 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1039 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1040 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1041 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1042 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1045 static int dl_args_finding_required_validate(uint32_t o_required
,
1051 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1052 o_flag
= dl_args_required
[i
].o_flag
;
1053 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1054 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1061 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
1062 uint32_t o_optional
)
1064 struct dl_opts
*opts
= &dl
->opts
;
1065 uint32_t o_all
= o_required
| o_optional
;
1066 uint32_t o_found
= 0;
1069 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1070 uint32_t handle_bit
;
1072 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1073 &opts
->port_index
, &handle_bit
);
1076 o_found
|= handle_bit
;
1077 } else if (o_required
& DL_OPT_HANDLE
) {
1078 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1081 o_found
|= DL_OPT_HANDLE
;
1082 } else if (o_required
& DL_OPT_HANDLEP
) {
1083 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1087 o_found
|= DL_OPT_HANDLEP
;
1088 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1089 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1091 &opts
->region_name
);
1094 o_found
|= DL_OPT_HANDLE_REGION
;
1097 while (dl_argc(dl
)) {
1098 if (dl_argv_match(dl
, "type") &&
1099 (o_all
& DL_OPT_PORT_TYPE
)) {
1100 const char *typestr
;
1103 err
= dl_argv_str(dl
, &typestr
);
1106 err
= port_type_get(typestr
, &opts
->port_type
);
1109 o_found
|= DL_OPT_PORT_TYPE
;
1110 } else if (dl_argv_match(dl
, "count") &&
1111 (o_all
& DL_OPT_PORT_COUNT
)) {
1113 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1116 o_found
|= DL_OPT_PORT_COUNT
;
1117 } else if (dl_argv_match(dl
, "sb") &&
1118 (o_all
& DL_OPT_SB
)) {
1120 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1123 o_found
|= DL_OPT_SB
;
1124 } else if (dl_argv_match(dl
, "pool") &&
1125 (o_all
& DL_OPT_SB_POOL
)) {
1127 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1130 o_found
|= DL_OPT_SB_POOL
;
1131 } else if (dl_argv_match(dl
, "size") &&
1132 (o_all
& DL_OPT_SB_SIZE
)) {
1134 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1137 o_found
|= DL_OPT_SB_SIZE
;
1138 } else if (dl_argv_match(dl
, "type") &&
1139 (o_all
& DL_OPT_SB_TYPE
)) {
1140 const char *typestr
;
1143 err
= dl_argv_str(dl
, &typestr
);
1146 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1149 o_found
|= DL_OPT_SB_TYPE
;
1150 } else if (dl_argv_match(dl
, "thtype") &&
1151 (o_all
& DL_OPT_SB_THTYPE
)) {
1152 const char *typestr
;
1155 err
= dl_argv_str(dl
, &typestr
);
1158 err
= threshold_type_get(typestr
,
1159 &opts
->sb_pool_thtype
);
1162 o_found
|= DL_OPT_SB_THTYPE
;
1163 } else if (dl_argv_match(dl
, "th") &&
1164 (o_all
& DL_OPT_SB_TH
)) {
1166 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1169 o_found
|= DL_OPT_SB_TH
;
1170 } else if (dl_argv_match(dl
, "tc") &&
1171 (o_all
& DL_OPT_SB_TC
)) {
1173 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1176 o_found
|= DL_OPT_SB_TC
;
1177 } else if (dl_argv_match(dl
, "mode") &&
1178 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1179 const char *typestr
;
1182 err
= dl_argv_str(dl
, &typestr
);
1185 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1188 o_found
|= DL_OPT_ESWITCH_MODE
;
1189 } else if (dl_argv_match(dl
, "inline-mode") &&
1190 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1191 const char *typestr
;
1194 err
= dl_argv_str(dl
, &typestr
);
1197 err
= eswitch_inline_mode_get(
1198 typestr
, &opts
->eswitch_inline_mode
);
1201 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1202 } else if (dl_argv_match(dl
, "name") &&
1203 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1205 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1208 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1209 } else if (dl_argv_match(dl
, "counters") &&
1210 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1211 const char *typestr
;
1214 err
= dl_argv_str(dl
, &typestr
);
1217 err
= dpipe_counters_enable_get(typestr
,
1218 &opts
->dpipe_counters_enable
);
1221 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1222 } else if (dl_argv_match(dl
, "encap") &&
1223 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1224 const char *typestr
;
1227 err
= dl_argv_str(dl
, &typestr
);
1230 err
= eswitch_encap_mode_get(typestr
,
1231 &opts
->eswitch_encap_mode
);
1234 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1235 } else if (dl_argv_match(dl
, "path") &&
1236 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1238 err
= dl_argv_str(dl
, &opts
->resource_path
);
1241 o_found
|= DL_OPT_RESOURCE_PATH
;
1242 } else if (dl_argv_match(dl
, "size") &&
1243 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1245 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1248 o_found
|= DL_OPT_RESOURCE_SIZE
;
1249 } else if (dl_argv_match(dl
, "name") &&
1250 (o_all
& DL_OPT_PARAM_NAME
)) {
1252 err
= dl_argv_str(dl
, &opts
->param_name
);
1255 o_found
|= DL_OPT_PARAM_NAME
;
1256 } else if (dl_argv_match(dl
, "value") &&
1257 (o_all
& DL_OPT_PARAM_VALUE
)) {
1259 err
= dl_argv_str(dl
, &opts
->param_value
);
1262 o_found
|= DL_OPT_PARAM_VALUE
;
1263 } else if (dl_argv_match(dl
, "cmode") &&
1264 (o_all
& DL_OPT_PARAM_CMODE
)) {
1265 const char *cmodestr
;
1268 err
= dl_argv_str(dl
, &cmodestr
);
1271 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1274 o_found
|= DL_OPT_PARAM_CMODE
;
1275 } else if (dl_argv_match(dl
, "snapshot") &&
1276 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1278 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1281 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1282 } else if (dl_argv_match(dl
, "address") &&
1283 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1285 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1288 o_found
|= DL_OPT_REGION_ADDRESS
;
1289 } else if (dl_argv_match(dl
, "length") &&
1290 (o_all
& DL_OPT_REGION_LENGTH
)) {
1292 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1295 o_found
|= DL_OPT_REGION_LENGTH
;
1296 } else if (dl_argv_match(dl
, "file") &&
1297 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1299 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1302 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1303 } else if (dl_argv_match(dl
, "component") &&
1304 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1306 err
= dl_argv_str(dl
, &opts
->flash_component
);
1309 o_found
|= DL_OPT_FLASH_COMPONENT
;
1310 } else if (dl_argv_match(dl
, "reporter") &&
1311 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1313 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1316 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1317 } else if (dl_argv_match(dl
, "grace_period") &&
1318 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1320 err
= dl_argv_uint64_t(dl
,
1321 &opts
->reporter_graceful_period
);
1324 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1325 } else if (dl_argv_match(dl
, "auto_recover") &&
1326 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1328 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1331 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1333 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1338 opts
->present
= o_found
;
1340 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1342 opts
->present
|= DL_OPT_SB
;
1345 return dl_args_finding_required_validate(o_required
, o_found
);
1348 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1350 struct dl_opts
*opts
= &dl
->opts
;
1352 if (opts
->present
& DL_OPT_HANDLE
) {
1353 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1354 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1355 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1356 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1357 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1358 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1360 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1361 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1362 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1363 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1366 if (opts
->present
& DL_OPT_PORT_TYPE
)
1367 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1369 if (opts
->present
& DL_OPT_PORT_COUNT
)
1370 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1372 if (opts
->present
& DL_OPT_SB
)
1373 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1375 if (opts
->present
& DL_OPT_SB_POOL
)
1376 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1377 opts
->sb_pool_index
);
1378 if (opts
->present
& DL_OPT_SB_SIZE
)
1379 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1380 opts
->sb_pool_size
);
1381 if (opts
->present
& DL_OPT_SB_TYPE
)
1382 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1383 opts
->sb_pool_type
);
1384 if (opts
->present
& DL_OPT_SB_THTYPE
)
1385 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1386 opts
->sb_pool_thtype
);
1387 if (opts
->present
& DL_OPT_SB_TH
)
1388 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1389 opts
->sb_threshold
);
1390 if (opts
->present
& DL_OPT_SB_TC
)
1391 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1393 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1394 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1395 opts
->eswitch_mode
);
1396 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1397 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1398 opts
->eswitch_inline_mode
);
1399 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1400 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1401 opts
->dpipe_table_name
);
1402 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1403 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1404 opts
->dpipe_counters_enable
);
1405 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1406 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1407 opts
->eswitch_encap_mode
);
1408 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1409 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1411 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1412 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1413 opts
->resource_size
);
1414 if (opts
->present
& DL_OPT_PARAM_NAME
)
1415 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1417 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1418 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1420 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1421 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1422 opts
->region_snapshot_id
);
1423 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1424 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1425 opts
->region_address
);
1426 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1427 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1428 opts
->region_length
);
1429 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1430 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1431 opts
->flash_file_name
);
1432 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1433 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1434 opts
->flash_component
);
1435 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1436 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1437 opts
->reporter_name
);
1438 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1439 mnl_attr_put_u64(nlh
,
1440 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1441 opts
->reporter_graceful_period
);
1442 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1443 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1444 opts
->reporter_auto_recover
);
1448 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1449 uint32_t o_required
, uint32_t o_optional
)
1453 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1456 dl_opts_put(nlh
, dl
);
1460 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1462 struct dl_opts
*opts
= &dl
->opts
;
1463 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1464 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1465 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1466 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1468 if (opts
->present
& DL_OPT_HANDLE
&&
1469 attr_bus_name
&& attr_dev_name
) {
1470 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1471 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1473 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1474 strcmp(dev_name
, opts
->dev_name
) != 0)
1477 if (opts
->present
& DL_OPT_HANDLEP
&&
1478 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1479 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1480 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1481 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1483 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1484 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1485 port_index
!= opts
->port_index
)
1488 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1489 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1491 if (sb_index
!= opts
->sb_index
)
1497 static void cmd_dev_help(void)
1499 pr_err("Usage: devlink dev show [ DEV ]\n");
1500 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1501 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1502 pr_err(" [ encap { disable | enable } ]\n");
1503 pr_err(" devlink dev eswitch show DEV\n");
1504 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1505 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1506 pr_err(" devlink dev reload DEV\n");
1507 pr_err(" devlink dev info [ DEV ]\n");
1508 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1511 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1512 const char *dev_name
)
1514 if (!dl
->arr_last
.present
)
1516 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1517 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1520 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1521 const char *dev_name
)
1523 dl
->arr_last
.present
= true;
1524 free(dl
->arr_last
.dev_name
);
1525 free(dl
->arr_last
.bus_name
);
1526 dl
->arr_last
.bus_name
= strdup(bus_name
);
1527 dl
->arr_last
.dev_name
= strdup(dev_name
);
1530 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1531 const char *dev_name
)
1533 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1536 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1537 const char *dev_name
)
1539 return dl
->arr_last
.present
&&
1540 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1543 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1544 bool content
, bool array
)
1546 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1547 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1550 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1552 if (dl
->json_output
) {
1554 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1555 jsonw_end_array(dl
->jw
);
1556 if (should_arr_last_handle_start(dl
, bus_name
,
1558 jsonw_name(dl
->jw
, buf
);
1559 jsonw_start_array(dl
->jw
);
1560 jsonw_start_object(dl
->jw
);
1561 arr_last_handle_set(dl
, bus_name
, dev_name
);
1563 jsonw_start_object(dl
->jw
);
1566 jsonw_name(dl
->jw
, buf
);
1567 jsonw_start_object(dl
->jw
);
1571 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1572 __pr_out_indent_dec();
1573 if (should_arr_last_handle_start(dl
, bus_name
,
1575 pr_out("%s%s", buf
, content
? ":" : "");
1577 __pr_out_indent_inc();
1578 arr_last_handle_set(dl
, bus_name
, dev_name
);
1581 pr_out("%s%s", buf
, content
? ":" : "");
1586 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1588 __pr_out_handle_start(dl
, tb
, true, true);
1591 static void pr_out_handle_end(struct dl
*dl
)
1593 if (dl
->json_output
)
1594 jsonw_end_object(dl
->jw
);
1599 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1601 __pr_out_handle_start(dl
, tb
, false, false);
1602 pr_out_handle_end(dl
);
1605 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1606 const char *dev_name
, uint32_t port_index
)
1608 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1609 dl
->arr_last
.port_index
== port_index
;
1612 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1613 const char *dev_name
, uint32_t port_index
)
1615 arr_last_handle_set(dl
, bus_name
, dev_name
);
1616 dl
->arr_last
.port_index
= port_index
;
1619 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1620 const char *bus_name
,
1621 const char *dev_name
,
1622 uint32_t port_index
)
1624 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1627 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1628 const char *bus_name
,
1629 const char *dev_name
,
1630 uint32_t port_index
)
1632 return dl
->arr_last
.present
&&
1633 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1636 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1637 const char *dev_name
,
1638 uint32_t port_index
, bool try_nice
,
1641 static char buf
[64];
1642 char *ifname
= NULL
;
1644 if (dl
->no_nice_names
|| !try_nice
||
1645 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1646 port_index
, &ifname
) != 0)
1647 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1649 sprintf(buf
, "%s", ifname
);
1651 if (dl
->json_output
) {
1653 if (should_arr_last_port_handle_end(dl
, bus_name
,
1656 jsonw_end_array(dl
->jw
);
1657 if (should_arr_last_port_handle_start(dl
, bus_name
,
1660 jsonw_name(dl
->jw
, buf
);
1661 jsonw_start_array(dl
->jw
);
1662 jsonw_start_object(dl
->jw
);
1663 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1666 jsonw_start_object(dl
->jw
);
1669 jsonw_name(dl
->jw
, buf
);
1670 jsonw_start_object(dl
->jw
);
1677 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1679 const char *bus_name
;
1680 const char *dev_name
;
1681 uint32_t port_index
;
1683 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1684 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1685 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1686 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1689 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1691 const char *bus_name
;
1692 const char *dev_name
;
1693 uint32_t port_index
;
1695 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1696 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1697 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1698 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1701 static void pr_out_port_handle_end(struct dl
*dl
)
1703 if (dl
->json_output
)
1704 jsonw_end_object(dl
->jw
);
1710 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1712 if (dl
->json_output
) {
1713 jsonw_string_field(dl
->jw
, name
, val
);
1715 if (g_indent_newline
)
1716 pr_out("%s %s", name
, val
);
1718 pr_out(" %s %s", name
, val
);
1722 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1724 if (dl
->json_output
)
1725 jsonw_bool_field(dl
->jw
, name
, val
);
1727 pr_out_str(dl
, name
, val
? "true" : "false");
1730 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1732 if (dl
->json_output
) {
1733 jsonw_uint_field(dl
->jw
, name
, val
);
1735 if (g_indent_newline
)
1736 pr_out("%s %u", name
, val
);
1738 pr_out(" %s %u", name
, val
);
1742 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1744 if (val
== (uint64_t) -1)
1745 return pr_out_str(dl
, name
, "unlimited");
1747 if (dl
->json_output
) {
1748 jsonw_u64_field(dl
->jw
, name
, val
);
1750 if (g_indent_newline
)
1751 pr_out("%s %lu", name
, val
);
1753 pr_out(" %s %lu", name
, val
);
1757 static void pr_out_bool_value(struct dl
*dl
, bool value
)
1759 if (dl
->json_output
)
1760 jsonw_bool(dl
->jw
, value
);
1762 pr_out(" %s", value
? "true" : "false");
1765 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
1767 if (dl
->json_output
)
1768 jsonw_uint(dl
->jw
, value
);
1770 pr_out(" %u", value
);
1773 static void pr_out_uint64_value(struct dl
*dl
, uint64_t value
)
1775 if (dl
->json_output
)
1776 jsonw_u64(dl
->jw
, value
);
1778 pr_out(" %lu", value
);
1781 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1785 if (dl
->json_output
)
1786 jsonw_start_array(dl
->jw
);
1791 if (dl
->json_output
) {
1792 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1794 pr_out(" %02x", data
[i
]);
1800 if (dl
->json_output
)
1801 jsonw_end_array(dl
->jw
);
1802 else if ((i
- 1) % 16)
1806 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1808 if (dl
->json_output
)
1809 jsonw_string(dl
->jw
, value
);
1811 pr_out(" %s", value
);
1814 static void pr_out_name(struct dl
*dl
, const char *name
)
1816 if (dl
->json_output
)
1817 jsonw_name(dl
->jw
, name
);
1819 pr_out(" %s:", name
);
1822 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1824 if (dl
->json_output
) {
1825 jsonw_name(dl
->jw
, "address");
1826 jsonw_uint(dl
->jw
, addr
);
1827 jsonw_name(dl
->jw
, "data");
1828 jsonw_start_array(dl
->jw
);
1832 static void pr_out_region_chunk_end(struct dl
*dl
)
1834 if (dl
->json_output
)
1835 jsonw_end_array(dl
->jw
);
1838 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1841 static uint64_t align_val
;
1844 pr_out_region_chunk_start(dl
, addr
);
1846 if (!dl
->json_output
)
1847 if (!(align_val
% 16))
1848 pr_out("%s%016"PRIx64
" ",
1849 align_val
? "\n" : "",
1854 if (dl
->json_output
)
1855 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1857 pr_out("%02x ", data
[i
]);
1862 pr_out_region_chunk_end(dl
);
1865 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1867 pr_out_handle(dl
, tb
);
1870 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1872 if (dl
->json_output
) {
1873 jsonw_start_object(dl
->jw
);
1874 jsonw_name(dl
->jw
, name
);
1875 jsonw_start_object(dl
->jw
);
1879 static void pr_out_section_end(struct dl
*dl
)
1881 if (dl
->json_output
) {
1882 if (dl
->arr_last
.present
)
1883 jsonw_end_array(dl
->jw
);
1884 jsonw_end_object(dl
->jw
);
1885 jsonw_end_object(dl
->jw
);
1889 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1891 if (dl
->json_output
) {
1892 jsonw_name(dl
->jw
, name
);
1893 jsonw_start_array(dl
->jw
);
1895 __pr_out_indent_inc();
1897 pr_out("%s:", name
);
1898 __pr_out_indent_inc();
1903 static void pr_out_array_end(struct dl
*dl
)
1905 if (dl
->json_output
) {
1906 jsonw_end_array(dl
->jw
);
1908 __pr_out_indent_dec();
1909 __pr_out_indent_dec();
1913 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1915 if (dl
->json_output
) {
1916 jsonw_name(dl
->jw
, name
);
1917 jsonw_start_object(dl
->jw
);
1919 __pr_out_indent_inc();
1921 pr_out("%s:", name
);
1922 __pr_out_indent_inc();
1927 static void pr_out_object_end(struct dl
*dl
)
1929 if (dl
->json_output
) {
1930 jsonw_end_object(dl
->jw
);
1932 __pr_out_indent_dec();
1933 __pr_out_indent_dec();
1937 static void pr_out_entry_start(struct dl
*dl
)
1939 if (dl
->json_output
)
1940 jsonw_start_object(dl
->jw
);
1943 static void pr_out_entry_end(struct dl
*dl
)
1945 if (dl
->json_output
)
1946 jsonw_end_object(dl
->jw
);
1951 static const char *param_cmode_name(uint8_t cmode
)
1954 case DEVLINK_PARAM_CMODE_RUNTIME
:
1955 return PARAM_CMODE_RUNTIME_STR
;
1956 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1957 return PARAM_CMODE_DRIVERINIT_STR
;
1958 case DEVLINK_PARAM_CMODE_PERMANENT
:
1959 return PARAM_CMODE_PERMANENT_STR
;
1960 default: return "<unknown type>";
1964 static const char *eswitch_mode_name(uint32_t mode
)
1967 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1968 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1969 default: return "<unknown mode>";
1973 static const char *eswitch_inline_mode_name(uint32_t mode
)
1976 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1977 return ESWITCH_INLINE_MODE_NONE
;
1978 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1979 return ESWITCH_INLINE_MODE_LINK
;
1980 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1981 return ESWITCH_INLINE_MODE_NETWORK
;
1982 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1983 return ESWITCH_INLINE_MODE_TRANSPORT
;
1985 return "<unknown mode>";
1989 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1991 __pr_out_handle_start(dl
, tb
, true, false);
1993 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1994 pr_out_str(dl
, "mode",
1995 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1997 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1998 pr_out_str(dl
, "inline-mode",
1999 eswitch_inline_mode_name(mnl_attr_get_u8(
2000 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2002 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2003 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2005 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
2008 pr_out_handle_end(dl
);
2011 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2013 struct dl
*dl
= data
;
2014 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2015 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2017 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2018 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2019 return MNL_CB_ERROR
;
2020 pr_out_eswitch(dl
, tb
);
2024 static int cmd_dev_eswitch_show(struct dl
*dl
)
2026 struct nlmsghdr
*nlh
;
2029 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2030 NLM_F_REQUEST
| NLM_F_ACK
);
2032 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2036 pr_out_section_start(dl
, "dev");
2037 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2038 pr_out_section_end(dl
);
2042 static int cmd_dev_eswitch_set(struct dl
*dl
)
2044 struct nlmsghdr
*nlh
;
2047 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2048 NLM_F_REQUEST
| NLM_F_ACK
);
2050 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2051 DL_OPT_ESWITCH_MODE
|
2052 DL_OPT_ESWITCH_INLINE_MODE
|
2053 DL_OPT_ESWITCH_ENCAP_MODE
);
2058 if (dl
->opts
.present
== 1) {
2059 pr_err("Need to set at least one option\n");
2063 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2066 static int cmd_dev_eswitch(struct dl
*dl
)
2068 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2071 } else if (dl_argv_match(dl
, "set")) {
2073 return cmd_dev_eswitch_set(dl
);
2074 } else if (dl_argv_match(dl
, "show")) {
2076 return cmd_dev_eswitch_show(dl
);
2078 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2082 struct param_val_conv
{
2088 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2089 uint32_t len
, const char *name
)
2093 for (i
= 0; i
< len
; i
++)
2094 if (!strcmp(param_val_conv
[i
].name
, name
))
2101 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2102 uint32_t len
, const char *name
, const char *vstr
,
2107 for (i
= 0; i
< len
; i
++)
2108 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2109 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2110 *vuint
= param_val_conv
[i
].vuint
;
2118 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2119 uint32_t len
, const char *name
, uint32_t vuint
,
2124 for (i
= 0; i
< len
; i
++)
2125 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2126 param_val_conv
[i
].vuint
== vuint
) {
2127 *vstr
= param_val_conv
[i
].vstr
;
2134 static const struct param_val_conv param_val_conv
[] = {
2136 .name
= "fw_load_policy",
2138 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2141 .name
= "fw_load_policy",
2143 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2147 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2149 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2150 int nla_type
, struct nlattr
*nl
)
2152 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2153 struct nlattr
*val_attr
;
2158 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2159 if (err
!= MNL_CB_OK
)
2162 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2163 (nla_type
!= MNL_TYPE_FLAG
&&
2164 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2167 pr_out_str(dl
, "cmode",
2168 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2169 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2171 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2177 err
= param_val_conv_str_get(param_val_conv
,
2180 mnl_attr_get_u8(val_attr
),
2184 pr_out_str(dl
, "value", vstr
);
2186 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2191 err
= param_val_conv_str_get(param_val_conv
,
2194 mnl_attr_get_u16(val_attr
),
2198 pr_out_str(dl
, "value", vstr
);
2200 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2205 err
= param_val_conv_str_get(param_val_conv
,
2208 mnl_attr_get_u32(val_attr
),
2212 pr_out_str(dl
, "value", vstr
);
2214 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2217 case MNL_TYPE_STRING
:
2218 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2221 pr_out_bool(dl
, "value", val_attr
? true : false);
2226 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2228 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2229 struct nlattr
*param_value_attr
;
2230 const char *nla_name
;
2234 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2235 if (err
!= MNL_CB_OK
)
2237 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2238 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2239 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2243 pr_out_handle_start_arr(dl
, tb
);
2245 __pr_out_handle_start(dl
, tb
, true, false);
2247 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2249 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2250 pr_out_str(dl
, "name", nla_name
);
2252 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2253 pr_out_str(dl
, "type", "driver-specific");
2255 pr_out_str(dl
, "type", "generic");
2257 pr_out_array_start(dl
, "values");
2258 mnl_attr_for_each_nested(param_value_attr
,
2259 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2260 pr_out_entry_start(dl
);
2261 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2262 pr_out_entry_end(dl
);
2264 pr_out_array_end(dl
);
2265 pr_out_handle_end(dl
);
2268 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2270 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2271 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2272 struct dl
*dl
= data
;
2274 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2275 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2276 !tb
[DEVLINK_ATTR_PARAM
])
2277 return MNL_CB_ERROR
;
2278 pr_out_param(dl
, tb
, true);
2294 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2296 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2297 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2298 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2299 struct nlattr
*param_value_attr
;
2300 enum devlink_param_cmode cmode
;
2301 struct param_ctx
*ctx
= data
;
2302 struct dl
*dl
= ctx
->dl
;
2306 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2307 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2308 !tb
[DEVLINK_ATTR_PARAM
])
2309 return MNL_CB_ERROR
;
2311 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2312 if (err
!= MNL_CB_OK
)
2313 return MNL_CB_ERROR
;
2315 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2316 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2317 return MNL_CB_ERROR
;
2319 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2320 mnl_attr_for_each_nested(param_value_attr
,
2321 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2322 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2323 struct nlattr
*val_attr
;
2325 err
= mnl_attr_parse_nested(param_value_attr
,
2326 attr_cb
, nla_value
);
2327 if (err
!= MNL_CB_OK
)
2328 return MNL_CB_ERROR
;
2330 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2331 (nla_type
!= MNL_TYPE_FLAG
&&
2332 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2333 return MNL_CB_ERROR
;
2335 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2336 if (cmode
== dl
->opts
.cmode
) {
2337 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2340 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2343 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2346 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2348 case MNL_TYPE_STRING
:
2349 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2352 ctx
->value
.vbool
= val_attr
? true : false;
2358 ctx
->nla_type
= nla_type
;
2362 static int cmd_dev_param_set(struct dl
*dl
)
2364 struct param_ctx ctx
= {};
2365 struct nlmsghdr
*nlh
;
2373 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2375 DL_OPT_PARAM_VALUE
|
2376 DL_OPT_PARAM_CMODE
, 0);
2380 /* Get value type */
2381 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2382 NLM_F_REQUEST
| NLM_F_ACK
);
2383 dl_opts_put(nlh
, dl
);
2386 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2390 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2391 NLM_F_REQUEST
| NLM_F_ACK
);
2392 dl_opts_put(nlh
, dl
);
2394 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2395 dl
->opts
.param_name
);
2397 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2398 switch (ctx
.nla_type
) {
2401 err
= param_val_conv_uint_get(param_val_conv
,
2403 dl
->opts
.param_name
,
2404 dl
->opts
.param_value
,
2408 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2411 goto err_param_value_parse
;
2412 if (val_u8
== ctx
.value
.vu8
)
2414 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2418 err
= param_val_conv_uint_get(param_val_conv
,
2420 dl
->opts
.param_name
,
2421 dl
->opts
.param_value
,
2425 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2428 goto err_param_value_parse
;
2429 if (val_u16
== ctx
.value
.vu16
)
2431 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2435 err
= param_val_conv_uint_get(param_val_conv
,
2437 dl
->opts
.param_name
,
2438 dl
->opts
.param_value
,
2441 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2443 goto err_param_value_parse
;
2444 if (val_u32
== ctx
.value
.vu32
)
2446 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2449 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2451 goto err_param_value_parse
;
2452 if (val_bool
== ctx
.value
.vbool
)
2455 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2458 case MNL_TYPE_STRING
:
2459 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2460 dl
->opts
.param_value
);
2461 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2465 printf("Value type not supported\n");
2468 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2470 err_param_value_parse
:
2471 pr_err("Value \"%s\" is not a number or not within range\n",
2472 dl
->opts
.param_value
);
2476 static int cmd_dev_param_show(struct dl
*dl
)
2478 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2479 struct nlmsghdr
*nlh
;
2482 if (dl_argc(dl
) == 0)
2483 flags
|= NLM_F_DUMP
;
2485 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2487 if (dl_argc(dl
) > 0) {
2488 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2489 DL_OPT_PARAM_NAME
, 0);
2494 pr_out_section_start(dl
, "param");
2495 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2496 pr_out_section_end(dl
);
2500 static int cmd_dev_param(struct dl
*dl
)
2502 if (dl_argv_match(dl
, "help")) {
2505 } else if (dl_argv_match(dl
, "show") ||
2506 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2508 return cmd_dev_param_show(dl
);
2509 } else if (dl_argv_match(dl
, "set")) {
2511 return cmd_dev_param_set(dl
);
2513 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2516 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2518 struct dl
*dl
= data
;
2519 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2520 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2522 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2523 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2524 return MNL_CB_ERROR
;
2529 static int cmd_dev_show(struct dl
*dl
)
2531 struct nlmsghdr
*nlh
;
2532 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2535 if (dl_argc(dl
) == 0)
2536 flags
|= NLM_F_DUMP
;
2538 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2540 if (dl_argc(dl
) > 0) {
2541 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2546 pr_out_section_start(dl
, "dev");
2547 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2548 pr_out_section_end(dl
);
2552 static void cmd_dev_reload_help(void)
2554 pr_err("Usage: devlink dev reload [ DEV ]\n");
2557 static int cmd_dev_reload(struct dl
*dl
)
2559 struct nlmsghdr
*nlh
;
2562 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2563 cmd_dev_reload_help();
2567 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2568 NLM_F_REQUEST
| NLM_F_ACK
);
2570 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2574 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2577 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2578 const char *name
, int type
)
2580 struct nlattr
*version
;
2582 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2583 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2584 const char *ver_value
;
2585 const char *ver_name
;
2588 if (mnl_attr_get_type(version
) != type
)
2591 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2592 if (err
!= MNL_CB_OK
)
2595 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2596 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2600 pr_out_object_start(dl
, name
);
2604 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2605 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2607 pr_out_str(dl
, ver_name
, ver_value
);
2608 if (!dl
->json_output
)
2613 pr_out_object_end(dl
);
2616 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2617 struct nlattr
**tb
, bool has_versions
)
2619 __pr_out_handle_start(dl
, tb
, true, false);
2621 __pr_out_indent_inc();
2622 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2623 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2625 if (!dl
->json_output
)
2627 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2630 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2631 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2633 if (!dl
->json_output
)
2635 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2637 __pr_out_indent_dec();
2640 pr_out_object_start(dl
, "versions");
2642 pr_out_versions_single(dl
, nlh
, "fixed",
2643 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2644 pr_out_versions_single(dl
, nlh
, "running",
2645 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2646 pr_out_versions_single(dl
, nlh
, "stored",
2647 DEVLINK_ATTR_INFO_VERSION_STORED
);
2649 pr_out_object_end(dl
);
2652 pr_out_handle_end(dl
);
2655 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2657 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2658 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2659 bool has_versions
, has_info
;
2660 struct dl
*dl
= data
;
2662 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2664 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2665 return MNL_CB_ERROR
;
2667 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2668 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2669 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2670 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2671 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2675 pr_out_info(dl
, nlh
, tb
, has_versions
);
2680 static void cmd_dev_info_help(void)
2682 pr_err("Usage: devlink dev info [ DEV ]\n");
2685 static int cmd_dev_info(struct dl
*dl
)
2687 struct nlmsghdr
*nlh
;
2688 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2691 if (dl_argv_match(dl
, "help")) {
2692 cmd_dev_info_help();
2696 if (dl_argc(dl
) == 0)
2697 flags
|= NLM_F_DUMP
;
2699 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2701 if (dl_argc(dl
) > 0) {
2702 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2707 pr_out_section_start(dl
, "info");
2708 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2709 pr_out_section_end(dl
);
2713 static void cmd_dev_flash_help(void)
2715 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2718 static int cmd_dev_flash(struct dl
*dl
)
2720 struct nlmsghdr
*nlh
;
2723 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2724 cmd_dev_flash_help();
2728 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2729 NLM_F_REQUEST
| NLM_F_ACK
);
2731 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2732 DL_OPT_FLASH_COMPONENT
);
2736 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2739 static int cmd_dev(struct dl
*dl
)
2741 if (dl_argv_match(dl
, "help")) {
2744 } else if (dl_argv_match(dl
, "show") ||
2745 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2747 return cmd_dev_show(dl
);
2748 } else if (dl_argv_match(dl
, "eswitch")) {
2750 return cmd_dev_eswitch(dl
);
2751 } else if (dl_argv_match(dl
, "reload")) {
2753 return cmd_dev_reload(dl
);
2754 } else if (dl_argv_match(dl
, "param")) {
2756 return cmd_dev_param(dl
);
2757 } else if (dl_argv_match(dl
, "info")) {
2759 return cmd_dev_info(dl
);
2760 } else if (dl_argv_match(dl
, "flash")) {
2762 return cmd_dev_flash(dl
);
2764 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2768 static void cmd_port_help(void)
2770 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2771 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2772 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2773 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2776 static const char *port_type_name(uint32_t type
)
2779 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2780 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2781 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2782 case DEVLINK_PORT_TYPE_IB
: return "ib";
2783 default: return "<unknown type>";
2787 static const char *port_flavour_name(uint16_t flavour
)
2790 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2792 case DEVLINK_PORT_FLAVOUR_CPU
:
2794 case DEVLINK_PORT_FLAVOUR_DSA
:
2797 return "<unknown flavour>";
2801 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2803 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2804 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2806 pr_out_port_handle_start(dl
, tb
, false);
2808 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2810 pr_out_str(dl
, "type", port_type_name(port_type
));
2812 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2814 if (port_type
!= des_port_type
)
2815 pr_out_str(dl
, "des_type",
2816 port_type_name(des_port_type
));
2819 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2820 pr_out_str(dl
, "netdev",
2821 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2822 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2823 pr_out_str(dl
, "ibdev",
2824 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2825 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2826 uint16_t port_flavour
=
2827 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2829 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2831 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2832 pr_out_uint(dl
, "split_group",
2833 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2834 pr_out_port_handle_end(dl
);
2837 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2839 struct dl
*dl
= data
;
2840 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2841 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2843 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2844 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2845 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2846 return MNL_CB_ERROR
;
2847 pr_out_port(dl
, tb
);
2851 static int cmd_port_show(struct dl
*dl
)
2853 struct nlmsghdr
*nlh
;
2854 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2857 if (dl_argc(dl
) == 0)
2858 flags
|= NLM_F_DUMP
;
2860 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2862 if (dl_argc(dl
) > 0) {
2863 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2868 pr_out_section_start(dl
, "port");
2869 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2870 pr_out_section_end(dl
);
2874 static int cmd_port_set(struct dl
*dl
)
2876 struct nlmsghdr
*nlh
;
2879 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2880 NLM_F_REQUEST
| NLM_F_ACK
);
2882 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2886 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2889 static int cmd_port_split(struct dl
*dl
)
2891 struct nlmsghdr
*nlh
;
2894 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2895 NLM_F_REQUEST
| NLM_F_ACK
);
2897 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2901 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2904 static int cmd_port_unsplit(struct dl
*dl
)
2906 struct nlmsghdr
*nlh
;
2909 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2910 NLM_F_REQUEST
| NLM_F_ACK
);
2912 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2916 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2919 static int cmd_port(struct dl
*dl
)
2921 if (dl_argv_match(dl
, "help")) {
2924 } else if (dl_argv_match(dl
, "show") ||
2925 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2927 return cmd_port_show(dl
);
2928 } else if (dl_argv_match(dl
, "set")) {
2930 return cmd_port_set(dl
);
2931 } else if (dl_argv_match(dl
, "split")) {
2933 return cmd_port_split(dl
);
2934 } else if (dl_argv_match(dl
, "unsplit")) {
2936 return cmd_port_unsplit(dl
);
2938 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2942 static void cmd_sb_help(void)
2944 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2945 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2946 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2947 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2948 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2949 pr_err(" pool POOL_INDEX ]\n");
2950 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2951 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2952 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2953 pr_err(" type { ingress | egress } ]\n");
2954 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2955 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2956 pr_err(" th THRESHOLD\n");
2957 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2958 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2959 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2962 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2964 pr_out_handle_start_arr(dl
, tb
);
2965 pr_out_uint(dl
, "sb",
2966 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2967 pr_out_uint(dl
, "size",
2968 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2969 pr_out_uint(dl
, "ing_pools",
2970 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2971 pr_out_uint(dl
, "eg_pools",
2972 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2973 pr_out_uint(dl
, "ing_tcs",
2974 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2975 pr_out_uint(dl
, "eg_tcs",
2976 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2977 pr_out_handle_end(dl
);
2980 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2982 struct dl
*dl
= data
;
2983 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2984 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2986 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2987 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2988 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2989 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2990 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2991 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2992 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
2993 return MNL_CB_ERROR
;
2998 static int cmd_sb_show(struct dl
*dl
)
3000 struct nlmsghdr
*nlh
;
3001 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3004 if (dl_argc(dl
) == 0)
3005 flags
|= NLM_F_DUMP
;
3007 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3009 if (dl_argc(dl
) > 0) {
3010 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3015 pr_out_section_start(dl
, "sb");
3016 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3017 pr_out_section_end(dl
);
3021 static const char *pool_type_name(uint8_t type
)
3024 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3025 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3026 default: return "<unknown type>";
3030 static const char *threshold_type_name(uint8_t type
)
3033 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3034 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3035 default: return "<unknown type>";
3039 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3041 pr_out_handle_start_arr(dl
, tb
);
3042 pr_out_uint(dl
, "sb",
3043 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3044 pr_out_uint(dl
, "pool",
3045 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3046 pr_out_str(dl
, "type",
3047 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3048 pr_out_uint(dl
, "size",
3049 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3050 pr_out_str(dl
, "thtype",
3051 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3052 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3053 pr_out_uint(dl
, "cell_size",
3054 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3055 pr_out_handle_end(dl
);
3058 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3060 struct dl
*dl
= data
;
3061 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3062 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3064 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3065 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3066 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3067 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3068 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3069 return MNL_CB_ERROR
;
3070 pr_out_sb_pool(dl
, tb
);
3074 static int cmd_sb_pool_show(struct dl
*dl
)
3076 struct nlmsghdr
*nlh
;
3077 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3080 if (dl_argc(dl
) == 0)
3081 flags
|= NLM_F_DUMP
;
3083 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3085 if (dl_argc(dl
) > 0) {
3086 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3092 pr_out_section_start(dl
, "pool");
3093 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3094 pr_out_section_end(dl
);
3098 static int cmd_sb_pool_set(struct dl
*dl
)
3100 struct nlmsghdr
*nlh
;
3103 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3104 NLM_F_REQUEST
| NLM_F_ACK
);
3106 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3107 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3111 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3114 static int cmd_sb_pool(struct dl
*dl
)
3116 if (dl_argv_match(dl
, "help")) {
3119 } else if (dl_argv_match(dl
, "show") ||
3120 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3122 return cmd_sb_pool_show(dl
);
3123 } else if (dl_argv_match(dl
, "set")) {
3125 return cmd_sb_pool_set(dl
);
3127 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3131 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3133 pr_out_port_handle_start_arr(dl
, tb
, true);
3134 pr_out_uint(dl
, "sb",
3135 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3136 pr_out_uint(dl
, "pool",
3137 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3138 pr_out_uint(dl
, "threshold",
3139 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3140 pr_out_port_handle_end(dl
);
3143 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3145 struct dl
*dl
= data
;
3146 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3147 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3149 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3150 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3151 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3152 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3153 return MNL_CB_ERROR
;
3154 pr_out_sb_port_pool(dl
, tb
);
3158 static int cmd_sb_port_pool_show(struct dl
*dl
)
3160 struct nlmsghdr
*nlh
;
3161 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3164 if (dl_argc(dl
) == 0)
3165 flags
|= NLM_F_DUMP
;
3167 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3169 if (dl_argc(dl
) > 0) {
3170 err
= dl_argv_parse_put(nlh
, dl
,
3171 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3177 pr_out_section_start(dl
, "port_pool");
3178 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3179 pr_out_section_end(dl
);
3183 static int cmd_sb_port_pool_set(struct dl
*dl
)
3185 struct nlmsghdr
*nlh
;
3188 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3189 NLM_F_REQUEST
| NLM_F_ACK
);
3191 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3192 DL_OPT_SB_TH
, DL_OPT_SB
);
3196 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3199 static int cmd_sb_port_pool(struct dl
*dl
)
3201 if (dl_argv_match(dl
, "help")) {
3204 } else if (dl_argv_match(dl
, "show") ||
3205 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3207 return cmd_sb_port_pool_show(dl
);
3208 } else if (dl_argv_match(dl
, "set")) {
3210 return cmd_sb_port_pool_set(dl
);
3212 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3216 static int cmd_sb_port(struct dl
*dl
)
3218 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3221 } else if (dl_argv_match(dl
, "pool")) {
3223 return cmd_sb_port_pool(dl
);
3225 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3229 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3231 pr_out_port_handle_start_arr(dl
, tb
, true);
3232 pr_out_uint(dl
, "sb",
3233 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3234 pr_out_uint(dl
, "tc",
3235 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3236 pr_out_str(dl
, "type",
3237 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3238 pr_out_uint(dl
, "pool",
3239 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3240 pr_out_uint(dl
, "threshold",
3241 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3242 pr_out_port_handle_end(dl
);
3245 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3247 struct dl
*dl
= data
;
3248 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3249 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3251 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3252 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3253 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3254 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3255 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3256 return MNL_CB_ERROR
;
3257 pr_out_sb_tc_bind(dl
, tb
);
3261 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3263 struct nlmsghdr
*nlh
;
3264 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3267 if (dl_argc(dl
) == 0)
3268 flags
|= NLM_F_DUMP
;
3270 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3272 if (dl_argc(dl
) > 0) {
3273 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3274 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3279 pr_out_section_start(dl
, "tc_bind");
3280 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3281 pr_out_section_end(dl
);
3285 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3287 struct nlmsghdr
*nlh
;
3290 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3291 NLM_F_REQUEST
| NLM_F_ACK
);
3293 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3294 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3299 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3302 static int cmd_sb_tc_bind(struct dl
*dl
)
3304 if (dl_argv_match(dl
, "help")) {
3307 } else if (dl_argv_match(dl
, "show") ||
3308 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3310 return cmd_sb_tc_bind_show(dl
);
3311 } else if (dl_argv_match(dl
, "set")) {
3313 return cmd_sb_tc_bind_set(dl
);
3315 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3319 static int cmd_sb_tc(struct dl
*dl
)
3321 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3324 } else if (dl_argv_match(dl
, "bind")) {
3326 return cmd_sb_tc_bind(dl
);
3328 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3333 struct list_head list
;
3337 uint32_t bound_pool_index
;
3341 struct list_head list
;
3344 uint32_t port_index
;
3346 struct list_head pool_list
;
3347 struct list_head ing_tc_list
;
3348 struct list_head eg_tc_list
;
3354 struct list_head port_list
;
3357 static struct occ_item
*occ_item_alloc(void)
3359 return calloc(1, sizeof(struct occ_item
));
3362 static void occ_item_free(struct occ_item
*occ_item
)
3367 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3369 struct occ_port
*occ_port
;
3371 occ_port
= calloc(1, sizeof(*occ_port
));
3374 occ_port
->port_index
= port_index
;
3375 INIT_LIST_HEAD(&occ_port
->pool_list
);
3376 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3377 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3381 static void occ_port_free(struct occ_port
*occ_port
)
3383 struct occ_item
*occ_item
, *tmp
;
3385 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3386 occ_item_free(occ_item
);
3387 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3388 occ_item_free(occ_item
);
3389 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3390 occ_item_free(occ_item
);
3393 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3395 struct occ_show
*occ_show
;
3397 occ_show
= calloc(1, sizeof(*occ_show
));
3401 INIT_LIST_HEAD(&occ_show
->port_list
);
3405 static void occ_show_free(struct occ_show
*occ_show
)
3407 struct occ_port
*occ_port
, *tmp
;
3409 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3410 occ_port_free(occ_port
);
3413 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3416 struct occ_port
*occ_port
;
3417 uint32_t port_index
;
3419 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3421 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3422 if (occ_port
->port_index
== port_index
)
3425 occ_port
= occ_port_alloc(port_index
);
3428 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3432 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3435 struct occ_item
*occ_item
;
3438 pr_out_sp(7, " %s:", label
);
3439 list_for_each_entry(occ_item
, list
, list
) {
3440 if ((i
- 1) % 4 == 0 && i
!= 1)
3443 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3444 occ_item
->bound_pool_index
);
3446 pr_out_sp(7, "%2u:", occ_item
->index
);
3447 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3451 if ((i
- 1) % 4 != 0)
3455 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3456 struct list_head
*list
,
3459 struct occ_item
*occ_item
;
3462 jsonw_name(dl
->jw
, label
);
3463 jsonw_start_object(dl
->jw
);
3464 list_for_each_entry(occ_item
, list
, list
) {
3465 sprintf(buf
, "%u", occ_item
->index
);
3466 jsonw_name(dl
->jw
, buf
);
3467 jsonw_start_object(dl
->jw
);
3469 jsonw_uint_field(dl
->jw
, "bound_pool",
3470 occ_item
->bound_pool_index
);
3471 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3472 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3473 jsonw_end_object(dl
->jw
);
3475 jsonw_end_object(dl
->jw
);
3478 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3480 if (dl
->json_output
) {
3481 pr_out_json_occ_show_item_list(dl
, "pool",
3482 &occ_port
->pool_list
, false);
3483 pr_out_json_occ_show_item_list(dl
, "itc",
3484 &occ_port
->ing_tc_list
, true);
3485 pr_out_json_occ_show_item_list(dl
, "etc",
3486 &occ_port
->eg_tc_list
, true);
3489 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3490 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3491 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3495 static void pr_out_occ_show(struct occ_show
*occ_show
)
3497 struct dl
*dl
= occ_show
->dl
;
3498 struct dl_opts
*opts
= &dl
->opts
;
3499 struct occ_port
*occ_port
;
3501 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3502 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3503 occ_port
->port_index
, true, false);
3504 pr_out_occ_show_port(dl
, occ_port
);
3505 pr_out_port_handle_end(dl
);
3509 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3512 struct occ_port
*occ_port
;
3513 struct occ_item
*occ_item
;
3515 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3518 occ_port
= occ_port_get(occ_show
, tb
);
3520 occ_show
->err
= -ENOMEM
;
3524 occ_item
= occ_item_alloc();
3526 occ_show
->err
= -ENOMEM
;
3529 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3530 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3531 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3532 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3535 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3537 struct occ_show
*occ_show
= data
;
3538 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3539 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3541 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3542 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3543 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3544 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3545 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3546 return MNL_CB_ERROR
;
3547 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3551 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3554 struct occ_port
*occ_port
;
3555 struct occ_item
*occ_item
;
3558 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3561 occ_port
= occ_port_get(occ_show
, tb
);
3563 occ_show
->err
= -ENOMEM
;
3567 occ_item
= occ_item_alloc();
3569 occ_show
->err
= -ENOMEM
;
3572 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3573 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3574 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3575 occ_item
->bound_pool_index
=
3576 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3577 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3578 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3579 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3580 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3581 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3583 occ_item_free(occ_item
);
3586 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3588 struct occ_show
*occ_show
= data
;
3589 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3590 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3592 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3593 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3594 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3595 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3596 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3597 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3598 return MNL_CB_ERROR
;
3599 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3603 static int cmd_sb_occ_show(struct dl
*dl
)
3605 struct nlmsghdr
*nlh
;
3606 struct occ_show
*occ_show
;
3607 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3610 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3614 occ_show
= occ_show_alloc(dl
);
3618 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3620 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3621 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3625 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3627 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3628 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3632 pr_out_section_start(dl
, "occupancy");
3633 pr_out_occ_show(occ_show
);
3634 pr_out_section_end(dl
);
3637 occ_show_free(occ_show
);
3641 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3643 struct nlmsghdr
*nlh
;
3646 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3647 NLM_F_REQUEST
| NLM_F_ACK
);
3649 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3653 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3656 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3658 struct nlmsghdr
*nlh
;
3661 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3662 NLM_F_REQUEST
| NLM_F_ACK
);
3664 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3668 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3671 static int cmd_sb_occ(struct dl
*dl
)
3673 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3676 } else if (dl_argv_match(dl
, "show") ||
3677 dl_argv_match(dl
, "list")) {
3679 return cmd_sb_occ_show(dl
);
3680 } else if (dl_argv_match(dl
, "snapshot")) {
3682 return cmd_sb_occ_snapshot(dl
);
3683 } else if (dl_argv_match(dl
, "clearmax")) {
3685 return cmd_sb_occ_clearmax(dl
);
3687 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3691 static int cmd_sb(struct dl
*dl
)
3693 if (dl_argv_match(dl
, "help")) {
3696 } else if (dl_argv_match(dl
, "show") ||
3697 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3699 return cmd_sb_show(dl
);
3700 } else if (dl_argv_match(dl
, "pool")) {
3702 return cmd_sb_pool(dl
);
3703 } else if (dl_argv_match(dl
, "port")) {
3705 return cmd_sb_port(dl
);
3706 } else if (dl_argv_match(dl
, "tc")) {
3708 return cmd_sb_tc(dl
);
3709 } else if (dl_argv_match(dl
, "occupancy")) {
3711 return cmd_sb_occ(dl
);
3713 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3717 static const char *cmd_name(uint8_t cmd
)
3720 case DEVLINK_CMD_UNSPEC
: return "unspec";
3721 case DEVLINK_CMD_GET
: return "get";
3722 case DEVLINK_CMD_SET
: return "set";
3723 case DEVLINK_CMD_NEW
: return "new";
3724 case DEVLINK_CMD_DEL
: return "del";
3725 case DEVLINK_CMD_PORT_GET
: return "get";
3726 case DEVLINK_CMD_PORT_SET
: return "set";
3727 case DEVLINK_CMD_PORT_NEW
: return "new";
3728 case DEVLINK_CMD_PORT_DEL
: return "del";
3729 case DEVLINK_CMD_PARAM_GET
: return "get";
3730 case DEVLINK_CMD_PARAM_SET
: return "set";
3731 case DEVLINK_CMD_PARAM_NEW
: return "new";
3732 case DEVLINK_CMD_PARAM_DEL
: return "del";
3733 case DEVLINK_CMD_REGION_GET
: return "get";
3734 case DEVLINK_CMD_REGION_SET
: return "set";
3735 case DEVLINK_CMD_REGION_NEW
: return "new";
3736 case DEVLINK_CMD_REGION_DEL
: return "del";
3737 default: return "<unknown cmd>";
3741 static const char *cmd_obj(uint8_t cmd
)
3744 case DEVLINK_CMD_UNSPEC
: return "unspec";
3745 case DEVLINK_CMD_GET
:
3746 case DEVLINK_CMD_SET
:
3747 case DEVLINK_CMD_NEW
:
3748 case DEVLINK_CMD_DEL
:
3750 case DEVLINK_CMD_PORT_GET
:
3751 case DEVLINK_CMD_PORT_SET
:
3752 case DEVLINK_CMD_PORT_NEW
:
3753 case DEVLINK_CMD_PORT_DEL
:
3755 case DEVLINK_CMD_PARAM_GET
:
3756 case DEVLINK_CMD_PARAM_SET
:
3757 case DEVLINK_CMD_PARAM_NEW
:
3758 case DEVLINK_CMD_PARAM_DEL
:
3760 case DEVLINK_CMD_REGION_GET
:
3761 case DEVLINK_CMD_REGION_SET
:
3762 case DEVLINK_CMD_REGION_NEW
:
3763 case DEVLINK_CMD_REGION_DEL
:
3765 default: return "<unknown obj>";
3769 static void pr_out_mon_header(uint8_t cmd
)
3771 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3774 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3776 const char *obj
= cmd_obj(cmd
);
3777 unsigned int index
= 0;
3778 const char *cur_obj
;
3782 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3783 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3789 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3791 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3793 struct dl
*dl
= data
;
3794 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3795 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3796 uint8_t cmd
= genl
->cmd
;
3798 if (!cmd_filter_check(dl
, cmd
))
3802 case DEVLINK_CMD_GET
: /* fall through */
3803 case DEVLINK_CMD_SET
: /* fall through */
3804 case DEVLINK_CMD_NEW
: /* fall through */
3805 case DEVLINK_CMD_DEL
:
3806 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3807 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3808 return MNL_CB_ERROR
;
3809 pr_out_mon_header(genl
->cmd
);
3812 case DEVLINK_CMD_PORT_GET
: /* fall through */
3813 case DEVLINK_CMD_PORT_SET
: /* fall through */
3814 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3815 case DEVLINK_CMD_PORT_DEL
:
3816 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3817 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3818 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3819 return MNL_CB_ERROR
;
3820 pr_out_mon_header(genl
->cmd
);
3821 pr_out_port(dl
, tb
);
3823 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3824 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3825 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3826 case DEVLINK_CMD_PARAM_DEL
:
3827 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3828 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3829 !tb
[DEVLINK_ATTR_PARAM
])
3830 return MNL_CB_ERROR
;
3831 pr_out_mon_header(genl
->cmd
);
3832 pr_out_param(dl
, tb
, false);
3834 case DEVLINK_CMD_REGION_GET
: /* fall through */
3835 case DEVLINK_CMD_REGION_SET
: /* fall through */
3836 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3837 case DEVLINK_CMD_REGION_DEL
:
3838 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3839 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3840 !tb
[DEVLINK_ATTR_REGION_NAME
])
3841 return MNL_CB_ERROR
;
3842 pr_out_mon_header(genl
->cmd
);
3843 pr_out_region(dl
, tb
);
3849 static int cmd_mon_show(struct dl
*dl
)
3852 unsigned int index
= 0;
3853 const char *cur_obj
;
3855 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3856 if (strcmp(cur_obj
, "all") != 0 &&
3857 strcmp(cur_obj
, "dev") != 0 &&
3858 strcmp(cur_obj
, "port") != 0) {
3859 pr_err("Unknown object \"%s\"\n", cur_obj
);
3863 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3866 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3872 static void cmd_mon_help(void)
3874 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3875 "where OBJECT-LIST := { dev | port }\n");
3878 static int cmd_mon(struct dl
*dl
)
3880 if (dl_argv_match(dl
, "help")) {
3884 return cmd_mon_show(dl
);
3887 struct dpipe_field
{
3890 unsigned int bitwidth
;
3891 enum devlink_dpipe_field_mapping_type mapping_type
;
3894 struct dpipe_header
{
3895 struct list_head list
;
3898 struct dpipe_field
*fields
;
3899 unsigned int fields_count
;
3902 struct dpipe_table
{
3903 struct list_head list
;
3905 unsigned int resource_id
;
3906 bool resource_valid
;
3909 struct dpipe_tables
{
3910 struct list_head table_list
;
3920 enum devlink_resource_unit unit
;
3925 struct list_head list
;
3926 struct list_head resource_list
;
3927 struct resource
*parent
;
3931 struct list_head resource_list
;
3934 struct resource_ctx
{
3937 struct resources
*resources
;
3938 struct dpipe_tables
*tables
;
3939 bool print_resources
;
3940 bool pending_change
;
3943 static struct resource
*resource_alloc(void)
3945 struct resource
*resource
;
3947 resource
= calloc(1, sizeof(struct resource
));
3950 INIT_LIST_HEAD(&resource
->resource_list
);
3954 static void resource_free(struct resource
*resource
)
3956 struct resource
*child_resource
, *tmp
;
3958 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3960 free(child_resource
->name
);
3961 resource_free(child_resource
);
3966 static struct resources
*resources_alloc(void)
3968 struct resources
*resources
;
3970 resources
= calloc(1, sizeof(struct resources
));
3973 INIT_LIST_HEAD(&resources
->resource_list
);
3977 static void resources_free(struct resources
*resources
)
3979 struct resource
*resource
, *tmp
;
3981 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3982 resource_free(resource
);
3985 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3987 ctx
->resources
= resources_alloc();
3988 if (!ctx
->resources
)
3994 static void resource_ctx_fini(struct resource_ctx
*ctx
)
3996 resources_free(ctx
->resources
);
4002 struct list_head global_headers
;
4003 struct list_head local_headers
;
4004 struct dpipe_tables
*tables
;
4005 struct resources
*resources
;
4010 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4012 struct dpipe_header
*header
;
4014 header
= calloc(1, sizeof(struct dpipe_header
));
4017 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4018 if (!header
->fields
)
4019 goto err_fields_alloc
;
4020 header
->fields_count
= fields_count
;
4028 static void dpipe_header_free(struct dpipe_header
*header
)
4030 free(header
->fields
);
4034 static void dpipe_header_clear(struct dpipe_header
*header
)
4036 struct dpipe_field
*field
;
4039 for (i
= 0; i
< header
->fields_count
; i
++) {
4040 field
= &header
->fields
[i
];
4046 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4047 struct dpipe_header
*header
, bool global
)
4050 list_add(&header
->list
, &ctx
->global_headers
);
4052 list_add(&header
->list
, &ctx
->local_headers
);
4055 static void dpipe_header_del(struct dpipe_header
*header
)
4057 list_del(&header
->list
);
4060 static struct dpipe_table
*dpipe_table_alloc(void)
4062 return calloc(1, sizeof(struct dpipe_table
));
4065 static void dpipe_table_free(struct dpipe_table
*table
)
4070 static struct dpipe_tables
*dpipe_tables_alloc(void)
4072 struct dpipe_tables
*tables
;
4074 tables
= calloc(1, sizeof(struct dpipe_tables
));
4077 INIT_LIST_HEAD(&tables
->table_list
);
4081 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4083 struct dpipe_table
*table
, *tmp
;
4085 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4086 dpipe_table_free(table
);
4090 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4092 ctx
->tables
= dpipe_tables_alloc();
4097 INIT_LIST_HEAD(&ctx
->global_headers
);
4098 INIT_LIST_HEAD(&ctx
->local_headers
);
4102 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4104 struct dpipe_header
*header
, *tmp
;
4106 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4108 dpipe_header_del(header
);
4109 dpipe_header_clear(header
);
4110 dpipe_header_free(header
);
4112 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4114 dpipe_header_del(header
);
4115 dpipe_header_clear(header
);
4116 dpipe_header_free(header
);
4118 dpipe_tables_free(ctx
->tables
);
4121 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4122 uint32_t header_id
, bool global
)
4124 struct list_head
*header_list
;
4125 struct dpipe_header
*header
;
4128 header_list
= &ctx
->global_headers
;
4130 header_list
= &ctx
->local_headers
;
4131 list_for_each_entry(header
, header_list
, list
) {
4132 if (header
->id
!= header_id
)
4134 return header
->name
;
4139 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4141 uint32_t field_id
, bool global
)
4143 struct list_head
*header_list
;
4144 struct dpipe_header
*header
;
4147 header_list
= &ctx
->global_headers
;
4149 header_list
= &ctx
->local_headers
;
4150 list_for_each_entry(header
, header_list
, list
) {
4151 if (header
->id
!= header_id
)
4153 return header
->fields
[field_id
].name
;
4159 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4161 switch (mapping_type
) {
4162 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4164 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4172 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4173 uint32_t field_id
, bool global
)
4175 enum devlink_dpipe_field_mapping_type mapping_type
;
4176 struct list_head
*header_list
;
4177 struct dpipe_header
*header
;
4180 header_list
= &ctx
->global_headers
;
4182 header_list
= &ctx
->local_headers
;
4183 list_for_each_entry(header
, header_list
, list
) {
4184 if (header
->id
!= header_id
)
4186 mapping_type
= header
->fields
[field_id
].mapping_type
;
4187 return dpipe_field_mapping_e2s(mapping_type
);
4192 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4193 struct dpipe_field
*fields
,
4194 unsigned int field_count
)
4196 struct dpipe_field
*field
;
4199 for (i
= 0; i
< field_count
; i
++) {
4201 pr_out_entry_start(ctx
->dl
);
4202 pr_out_str(ctx
->dl
, "name", field
->name
);
4203 if (ctx
->dl
->verbose
)
4204 pr_out_uint(ctx
->dl
, "id", field
->id
);
4205 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4206 if (field
->mapping_type
)
4207 pr_out_str(ctx
->dl
, "mapping_type",
4208 dpipe_field_mapping_e2s(field
->mapping_type
));
4209 pr_out_entry_end(ctx
->dl
);
4214 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4215 struct dpipe_header
*header
, bool global
)
4217 pr_out_handle_start_arr(ctx
->dl
, tb
);
4218 pr_out_str(ctx
->dl
, "name", header
->name
);
4219 if (ctx
->dl
->verbose
) {
4220 pr_out_uint(ctx
->dl
, "id", header
->id
);
4221 pr_out_str(ctx
->dl
, "global",
4222 global
? "true" : "false");
4224 pr_out_array_start(ctx
->dl
, "field");
4225 pr_out_dpipe_fields(ctx
, header
->fields
,
4226 header
->fields_count
);
4227 pr_out_array_end(ctx
->dl
);
4228 pr_out_handle_end(ctx
->dl
);
4231 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4234 struct dpipe_header
*header
;
4236 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4237 pr_out_dpipe_header(ctx
, tb
, header
, false);
4239 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4240 pr_out_dpipe_header(ctx
, tb
, header
, true);
4243 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4245 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4249 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4250 if (err
!= MNL_CB_OK
)
4252 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4253 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4254 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4255 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4258 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4259 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4260 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4261 field
->name
= strdup(name
);
4264 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4268 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4269 struct dpipe_field
*fields
)
4271 struct nlattr
*nla_field
;
4275 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4276 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4284 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4286 struct nlattr
*nla_field
;
4287 unsigned int count
= 0;
4289 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4294 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4296 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4297 struct dpipe_header
*header
;
4298 unsigned int fields_count
;
4299 const char *header_name
;
4303 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4304 if (err
!= MNL_CB_OK
)
4307 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4308 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4309 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4312 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4313 header
= dpipe_header_alloc(fields_count
);
4317 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4318 header
->name
= strdup(header_name
);
4319 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4320 header
->fields_count
= fields_count
;
4321 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4323 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4327 dpipe_header_add(ctx
, header
, global
);
4331 dpipe_header_free(header
);
4335 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4337 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4338 struct nlattr
*nla_header
;
4341 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4342 err
= dpipe_header_get(ctx
, nla_header
);
4349 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4351 struct dpipe_ctx
*ctx
= data
;
4352 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4353 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4356 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4357 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4358 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4359 return MNL_CB_ERROR
;
4360 err
= dpipe_headers_get(ctx
, tb
);
4363 return MNL_CB_ERROR
;
4366 if (ctx
->print_headers
)
4367 pr_out_dpipe_headers(ctx
, tb
);
4371 static int cmd_dpipe_headers_show(struct dl
*dl
)
4373 struct nlmsghdr
*nlh
;
4374 struct dpipe_ctx ctx
= {};
4375 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4378 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4380 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4384 err
= dpipe_ctx_init(&ctx
, dl
);
4388 ctx
.print_headers
= true;
4390 pr_out_section_start(dl
, "header");
4391 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4393 pr_err("error get headers %s\n", strerror(ctx
.err
));
4394 pr_out_section_end(dl
);
4396 dpipe_ctx_fini(&ctx
);
4400 static void cmd_dpipe_header_help(void)
4402 pr_err("Usage: devlink dpipe headers show DEV\n");
4405 static int cmd_dpipe_header(struct dl
*dl
)
4407 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4408 cmd_dpipe_header_help();
4410 } else if (dl_argv_match(dl
, "show")) {
4412 return cmd_dpipe_headers_show(dl
);
4414 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4419 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4421 switch (action_type
) {
4422 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4423 return "field_modify";
4429 struct dpipe_op_info
{
4435 struct dpipe_action
{
4436 struct dpipe_op_info info
;
4440 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4441 struct dpipe_ctx
*ctx
)
4443 struct dpipe_op_info
*op_info
= &action
->info
;
4444 const char *mapping
;
4446 pr_out_str(ctx
->dl
, "type",
4447 dpipe_action_type_e2s(action
->type
));
4448 pr_out_str(ctx
->dl
, "header",
4449 dpipe_header_id2s(ctx
, op_info
->header_id
,
4450 op_info
->header_global
));
4451 pr_out_str(ctx
->dl
, "field",
4452 dpipe_field_id2s(ctx
, op_info
->header_id
,
4454 op_info
->header_global
));
4455 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4457 op_info
->header_global
);
4459 pr_out_str(ctx
->dl
, "mapping", mapping
);
4462 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4464 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4467 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4468 if (err
!= MNL_CB_OK
)
4471 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4472 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4473 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4474 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4478 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4479 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4480 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4481 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4486 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4487 struct nlattr
*nla_actions
)
4489 struct nlattr
*nla_action
;
4490 struct dpipe_action action
;
4492 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4493 pr_out_entry_start(ctx
->dl
);
4494 if (dpipe_action_parse(&action
, nla_action
))
4495 goto err_action_parse
;
4496 pr_out_dpipe_action(&action
, ctx
);
4497 pr_out_entry_end(ctx
->dl
);
4502 pr_out_entry_end(ctx
->dl
);
4507 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4509 switch (match_type
) {
4510 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4511 return "field_exact";
4517 struct dpipe_match
{
4518 struct dpipe_op_info info
;
4522 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4523 struct dpipe_ctx
*ctx
)
4525 struct dpipe_op_info
*op_info
= &match
->info
;
4526 const char *mapping
;
4528 pr_out_str(ctx
->dl
, "type",
4529 dpipe_match_type_e2s(match
->type
));
4530 pr_out_str(ctx
->dl
, "header",
4531 dpipe_header_id2s(ctx
, op_info
->header_id
,
4532 op_info
->header_global
));
4533 pr_out_str(ctx
->dl
, "field",
4534 dpipe_field_id2s(ctx
, op_info
->header_id
,
4536 op_info
->header_global
));
4537 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4539 op_info
->header_global
);
4541 pr_out_str(ctx
->dl
, "mapping", mapping
);
4544 static int dpipe_match_parse(struct dpipe_match
*match
,
4548 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4551 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4552 if (err
!= MNL_CB_OK
)
4555 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4556 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4557 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4558 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4562 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4563 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4564 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4565 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4570 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4571 struct nlattr
*nla_matches
)
4573 struct nlattr
*nla_match
;
4574 struct dpipe_match match
;
4576 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4577 pr_out_entry_start(ctx
->dl
);
4578 if (dpipe_match_parse(&match
, nla_match
))
4579 goto err_match_parse
;
4580 pr_out_dpipe_match(&match
, ctx
);
4581 pr_out_entry_end(ctx
->dl
);
4586 pr_out_entry_end(ctx
->dl
);
4590 static struct resource
*
4591 resource_find(struct resources
*resources
, struct resource
*resource
,
4592 uint64_t resource_id
)
4594 struct list_head
*list_head
;
4597 list_head
= &resources
->resource_list
;
4599 list_head
= &resource
->resource_list
;
4601 list_for_each_entry(resource
, list_head
, list
) {
4602 struct resource
*child_resource
;
4604 if (resource
->id
== resource_id
)
4607 child_resource
= resource_find(resources
, resource
,
4610 return child_resource
;
4616 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4617 uint64_t resource_id
)
4619 struct resource
*resource
, *parent_resource
;
4620 const char del
[] = "/";
4624 resource
= resource_find(resources
, NULL
, resource_id
);
4628 for (parent_resource
= resource
; parent_resource
;
4629 parent_resource
= parent_resource
->parent
)
4630 path_len
+= strlen(parent_resource
->name
) + 1;
4633 path
= calloc(1, path_len
);
4637 path
+= path_len
- 1;
4638 for (parent_resource
= resource
; parent_resource
;
4639 parent_resource
= parent_resource
->parent
) {
4640 path
-= strlen(parent_resource
->name
);
4641 memcpy(path
, parent_resource
->name
,
4642 strlen(parent_resource
->name
));
4643 path
-= strlen(del
);
4644 memcpy(path
, del
, strlen(del
));
4646 pr_out_str(dl
, "resource_path", path
);
4650 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4652 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4653 struct dpipe_table
*table
;
4654 uint32_t resource_units
;
4655 bool counters_enabled
;
4656 bool resource_valid
;
4660 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4661 if (err
!= MNL_CB_OK
)
4664 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4665 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4666 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4667 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4668 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4672 table
= dpipe_table_alloc();
4676 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4677 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4678 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4680 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4682 if (resource_valid
) {
4683 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4684 table
->resource_valid
= true;
4687 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4688 if (!ctx
->print_tables
)
4691 pr_out_str(ctx
->dl
, "name", table
->name
);
4692 pr_out_uint(ctx
->dl
, "size", size
);
4693 pr_out_str(ctx
->dl
, "counters_enabled",
4694 counters_enabled
? "true" : "false");
4696 if (resource_valid
) {
4697 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4698 resource_path_print(ctx
->dl
, ctx
->resources
,
4699 table
->resource_id
);
4700 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4703 pr_out_array_start(ctx
->dl
, "match");
4704 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4705 goto err_matches_show
;
4706 pr_out_array_end(ctx
->dl
);
4708 pr_out_array_start(ctx
->dl
, "action");
4709 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4710 goto err_actions_show
;
4711 pr_out_array_end(ctx
->dl
);
4717 pr_out_array_end(ctx
->dl
);
4721 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4723 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4724 struct nlattr
*nla_table
;
4726 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4727 if (ctx
->print_tables
)
4728 pr_out_handle_start_arr(ctx
->dl
, tb
);
4729 if (dpipe_table_show(ctx
, nla_table
))
4730 goto err_table_show
;
4731 if (ctx
->print_tables
)
4732 pr_out_handle_end(ctx
->dl
);
4737 if (ctx
->print_tables
)
4738 pr_out_handle_end(ctx
->dl
);
4742 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4744 struct dpipe_ctx
*ctx
= data
;
4745 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4746 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4748 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4749 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4750 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4751 return MNL_CB_ERROR
;
4753 if (dpipe_tables_show(ctx
, tb
))
4754 return MNL_CB_ERROR
;
4758 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4760 static int cmd_dpipe_table_show(struct dl
*dl
)
4762 struct nlmsghdr
*nlh
;
4763 struct dpipe_ctx dpipe_ctx
= {};
4764 struct resource_ctx resource_ctx
= {};
4765 uint16_t flags
= NLM_F_REQUEST
;
4768 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4772 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4774 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4778 dpipe_ctx
.print_tables
= true;
4780 dl_opts_put(nlh
, dl
);
4781 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4784 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4785 goto err_headers_get
;
4788 err
= resource_ctx_init(&resource_ctx
, dl
);
4790 goto err_resource_ctx_init
;
4792 resource_ctx
.print_resources
= false;
4793 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4794 dl_opts_put(nlh
, dl
);
4795 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4798 dpipe_ctx
.resources
= resource_ctx
.resources
;
4800 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4801 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4802 dl_opts_put(nlh
, dl
);
4804 pr_out_section_start(dl
, "table");
4805 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4806 pr_out_section_end(dl
);
4808 resource_ctx_fini(&resource_ctx
);
4809 dpipe_ctx_fini(&dpipe_ctx
);
4812 err_resource_ctx_init
:
4814 dpipe_ctx_fini(&dpipe_ctx
);
4818 static int cmd_dpipe_table_set(struct dl
*dl
)
4820 struct nlmsghdr
*nlh
;
4823 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4824 NLM_F_REQUEST
| NLM_F_ACK
);
4826 err
= dl_argv_parse_put(nlh
, dl
,
4827 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4828 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4832 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4835 enum dpipe_value_type
{
4836 DPIPE_VALUE_TYPE_VALUE
,
4837 DPIPE_VALUE_TYPE_MASK
,
4841 dpipe_value_type_e2s(enum dpipe_value_type type
)
4844 case DPIPE_VALUE_TYPE_VALUE
:
4846 case DPIPE_VALUE_TYPE_MASK
:
4847 return "value_mask";
4853 struct dpipe_field_printer
{
4854 unsigned int field_id
;
4855 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4858 struct dpipe_header_printer
{
4859 struct dpipe_field_printer
*printers
;
4860 unsigned int printers_count
;
4861 unsigned int header_id
;
4864 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4865 enum dpipe_value_type type
,
4868 struct in_addr ip_addr
;
4870 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4871 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4875 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4876 enum dpipe_value_type type
,
4879 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4880 ether_ntoa((struct ether_addr
*)value
));
4883 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4884 enum dpipe_value_type type
,
4887 char str
[INET6_ADDRSTRLEN
];
4889 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4890 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4893 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4895 .printer
= dpipe_field_printer_ipv4_addr
,
4896 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4900 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4901 .printers
= dpipe_field_printers_ipv4
,
4902 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4903 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4906 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4908 .printer
= dpipe_field_printer_ethernet_addr
,
4909 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4913 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4914 .printers
= dpipe_field_printers_ethernet
,
4915 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4916 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4919 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4921 .printer
= dpipe_field_printer_ipv6_addr
,
4922 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4926 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4927 .printers
= dpipe_field_printers_ipv6
,
4928 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4929 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4932 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4933 &dpipe_header_printer_ipv4
,
4934 &dpipe_header_printer_ethernet
,
4935 &dpipe_header_printer_ipv6
,
4938 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4939 struct dpipe_op_info
*info
,
4940 enum dpipe_value_type type
,
4943 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4944 struct dpipe_header_printer
*header_printer
;
4945 struct dpipe_field_printer
*field_printer
;
4946 unsigned int field_printers_count
;
4950 for (i
= 0; i
< header_printers_count
; i
++) {
4951 header_printer
= dpipe_header_printers
[i
];
4952 if (header_printer
->header_id
!= info
->header_id
)
4954 field_printers_count
= header_printer
->printers_count
;
4955 for (j
= 0; j
< field_printers_count
; j
++) {
4956 field_printer
= &header_printer
->printers
[j
];
4957 if (field_printer
->field_id
!= info
->field_id
)
4959 field_printer
->printer(ctx
, type
, value
);
4967 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4969 unsigned int value_len
,
4970 struct dpipe_op_info
*info
,
4971 enum dpipe_value_type type
)
4973 if (info
->header_global
&&
4974 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4977 if (value_len
== sizeof(uint32_t)) {
4978 uint32_t *value_32
= value
;
4980 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4984 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4985 struct nlattr
**nla_match_value
,
4986 struct dpipe_op_info
*info
)
4988 void *value
, *value_mask
;
4989 uint32_t value_mapping
;
4993 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
4994 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
4996 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
4997 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5000 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5001 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
5005 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5006 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5007 DPIPE_VALUE_TYPE_MASK
);
5010 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5013 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5016 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5017 struct dpipe_match match
;
5020 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5021 if (err
!= MNL_CB_OK
)
5024 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5025 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5029 pr_out_entry_start(ctx
->dl
);
5030 if (dpipe_match_parse(&match
,
5031 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5032 goto err_match_parse
;
5033 pr_out_dpipe_match(&match
, ctx
);
5034 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5035 pr_out_entry_end(ctx
->dl
);
5040 pr_out_entry_end(ctx
->dl
);
5044 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5047 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5048 struct dpipe_action action
;
5051 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5052 if (err
!= MNL_CB_OK
)
5055 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5056 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5060 pr_out_entry_start(ctx
->dl
);
5061 if (dpipe_action_parse(&action
,
5062 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5063 goto err_action_parse
;
5064 pr_out_dpipe_action(&action
, ctx
);
5065 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5066 pr_out_entry_end(ctx
->dl
);
5071 pr_out_entry_end(ctx
->dl
);
5076 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5077 struct nlattr
*nla_action_values
)
5079 struct nlattr
*nla_action_value
;
5081 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5082 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5089 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5090 struct nlattr
*nla_match_values
)
5092 struct nlattr
*nla_match_value
;
5094 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5095 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5101 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5103 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5104 uint32_t entry_index
;
5108 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5109 if (err
!= MNL_CB_OK
)
5112 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5113 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5114 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5118 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5119 pr_out_uint(ctx
->dl
, "index", entry_index
);
5121 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5122 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5123 pr_out_uint(ctx
->dl
, "counter", counter
);
5126 pr_out_array_start(ctx
->dl
, "match_value");
5127 if (dpipe_tables_match_values_show(ctx
,
5128 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5129 goto err_match_values_show
;
5130 pr_out_array_end(ctx
->dl
);
5132 pr_out_array_start(ctx
->dl
, "action_value");
5133 if (dpipe_tables_action_values_show(ctx
,
5134 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5135 goto err_action_values_show
;
5136 pr_out_array_end(ctx
->dl
);
5139 err_action_values_show
:
5140 err_match_values_show
:
5141 pr_out_array_end(ctx
->dl
);
5145 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5147 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5148 struct nlattr
*nla_entry
;
5150 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5151 pr_out_handle_start_arr(ctx
->dl
, tb
);
5152 if (dpipe_entry_show(ctx
, nla_entry
))
5153 goto err_entry_show
;
5154 pr_out_handle_end(ctx
->dl
);
5159 pr_out_handle_end(ctx
->dl
);
5163 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5165 struct dpipe_ctx
*ctx
= data
;
5166 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5167 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5169 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5170 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5171 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5172 return MNL_CB_ERROR
;
5174 if (dpipe_table_entries_show(ctx
, tb
))
5175 return MNL_CB_ERROR
;
5179 static int cmd_dpipe_table_dump(struct dl
*dl
)
5181 struct nlmsghdr
*nlh
;
5182 struct dpipe_ctx ctx
= {};
5183 uint16_t flags
= NLM_F_REQUEST
;
5186 err
= dpipe_ctx_init(&ctx
, dl
);
5190 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5194 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5195 dl_opts_put(nlh
, dl
);
5196 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5198 pr_err("error get headers %s\n", strerror(ctx
.err
));
5202 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5203 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5204 dl_opts_put(nlh
, dl
);
5206 pr_out_section_start(dl
, "table_entry");
5207 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5208 pr_out_section_end(dl
);
5210 dpipe_ctx_fini(&ctx
);
5214 static void cmd_dpipe_table_help(void)
5216 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5217 "where OBJECT-LIST := { show | set | dump }\n");
5220 static int cmd_dpipe_table(struct dl
*dl
)
5222 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5223 cmd_dpipe_table_help();
5225 } else if (dl_argv_match(dl
, "show")) {
5227 return cmd_dpipe_table_show(dl
);
5228 } else if (dl_argv_match(dl
, "set")) {
5230 return cmd_dpipe_table_set(dl
);
5231 } else if (dl_argv_match(dl
, "dump")) {
5233 return cmd_dpipe_table_dump(dl
);
5235 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5239 static void cmd_dpipe_help(void)
5241 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5242 "where OBJECT-LIST := { header | table }\n");
5245 static int cmd_dpipe(struct dl
*dl
)
5247 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5250 } else if (dl_argv_match(dl
, "header")) {
5252 return cmd_dpipe_header(dl
);
5253 } else if (dl_argv_match(dl
, "table")) {
5255 return cmd_dpipe_table(dl
);
5257 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5262 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5263 struct nlattr
**nla_resource
)
5265 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5266 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5267 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5268 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5269 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5270 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5271 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5275 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5276 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5277 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5278 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5279 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5280 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5281 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5283 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5284 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5286 resource
->size_new
= resource
->size
;
5288 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5289 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5290 resource
->occ_valid
= true;
5293 if (resource
->size_new
!= resource
->size
)
5294 ctx
->pending_change
= true;
5300 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5301 struct resource
*parent_resource
, struct nlattr
*nl
)
5303 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5304 struct nlattr
*nla_child_resource
;
5305 struct nlattr
*nla_resources
;
5315 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5316 if (err
!= MNL_CB_OK
)
5319 err
= resource_parse(ctx
, resource
, nla_resource
);
5323 resource
->parent
= parent_resource
;
5324 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5327 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5328 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5330 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5331 struct resource
*child_resource
;
5332 struct list_head
*list
;
5334 child_resource
= resource_alloc();
5335 if (!child_resource
)
5339 list
= &ctx
->resources
->resource_list
;
5341 list
= &resource
->resource_list
;
5343 list_add_tail(&child_resource
->list
, list
);
5344 err
= resource_get(ctx
, child_resource
, resource
,
5345 nla_child_resource
);
5353 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5356 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5357 default: return "<unknown unit>";
5361 static void resource_show(struct resource
*resource
,
5362 struct resource_ctx
*ctx
)
5364 struct resource
*child_resource
;
5365 struct dpipe_table
*table
;
5366 struct dl
*dl
= ctx
->dl
;
5369 pr_out_str(dl
, "name", resource
->name
);
5371 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5372 pr_out_u64(dl
, "size", resource
->size
);
5373 if (resource
->size
!= resource
->size_new
)
5374 pr_out_u64(dl
, "size_new", resource
->size_new
);
5375 if (resource
->occ_valid
)
5376 pr_out_uint(dl
, "occ", resource
->size_occ
);
5377 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5379 if (resource
->size_min
!= resource
->size_max
) {
5380 pr_out_uint(dl
, "size_min", resource
->size_min
);
5381 pr_out_u64(dl
, "size_max", resource
->size_max
);
5382 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5385 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5386 if (table
->resource_id
== resource
->id
&&
5387 table
->resource_valid
)
5391 pr_out_array_start(dl
, "dpipe_tables");
5393 pr_out_str(dl
, "dpipe_tables", "none");
5395 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5396 if (table
->resource_id
!= resource
->id
||
5397 !table
->resource_valid
)
5399 pr_out_entry_start(dl
);
5400 pr_out_str(dl
, "table_name", table
->name
);
5401 pr_out_entry_end(dl
);
5404 pr_out_array_end(dl
);
5406 if (list_empty(&resource
->resource_list
))
5409 if (ctx
->pending_change
)
5410 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5412 pr_out_array_start(dl
, "resources");
5413 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5414 pr_out_entry_start(dl
);
5415 resource_show(child_resource
, ctx
);
5416 pr_out_entry_end(dl
);
5418 pr_out_array_end(dl
);
5422 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5424 struct resources
*resources
= ctx
->resources
;
5425 struct resource
*resource
;
5427 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5428 pr_out_handle_start_arr(ctx
->dl
, tb
);
5429 resource_show(resource
, ctx
);
5430 pr_out_handle_end(ctx
->dl
);
5434 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5436 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5439 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5441 struct resource_ctx
*ctx
= data
;
5442 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5443 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5446 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5447 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5448 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5449 return MNL_CB_ERROR
;
5451 err
= resources_get(ctx
, tb
);
5454 return MNL_CB_ERROR
;
5457 if (ctx
->print_resources
)
5458 resources_show(ctx
, tb
);
5463 static int cmd_resource_show(struct dl
*dl
)
5465 struct nlmsghdr
*nlh
;
5466 struct dpipe_ctx dpipe_ctx
= {};
5467 struct resource_ctx resource_ctx
= {};
5470 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5474 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5476 dl_opts_put(nlh
, dl
);
5478 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5482 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5485 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5489 err
= resource_ctx_init(&resource_ctx
, dl
);
5493 resource_ctx
.print_resources
= true;
5494 resource_ctx
.tables
= dpipe_ctx
.tables
;
5495 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5496 NLM_F_REQUEST
| NLM_F_ACK
);
5497 dl_opts_put(nlh
, dl
);
5498 pr_out_section_start(dl
, "resources");
5499 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5501 pr_out_section_end(dl
);
5502 resource_ctx_fini(&resource_ctx
);
5504 dpipe_ctx_fini(&dpipe_ctx
);
5508 static void cmd_resource_help(void)
5510 pr_err("Usage: devlink resource show DEV\n"
5511 " devlink resource set DEV path PATH size SIZE\n");
5514 static struct resource
*
5515 resource_find_by_name(struct list_head
*list
, char *name
)
5517 struct resource
*resource
;
5519 list_for_each_entry(resource
, list
, list
) {
5520 if (!strcmp(resource
->name
, name
))
5527 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5528 uint32_t *p_resource_id
, bool *p_resource_valid
)
5530 struct resource
*resource
;
5531 uint32_t resource_id
= 0;
5532 char *resource_path_dup
;
5533 struct list_head
*list
;
5534 const char del
[] = "/";
5535 char *resource_name
;
5537 resource_path_dup
= strdup(resource_path
);
5538 list
= &ctx
->resources
->resource_list
;
5539 resource_name
= strtok(resource_path_dup
, del
);
5540 while (resource_name
!= NULL
) {
5541 resource
= resource_find_by_name(list
, resource_name
);
5543 goto err_resource_lookup
;
5545 list
= &resource
->resource_list
;
5546 resource_name
= strtok(NULL
, del
);
5547 resource_id
= resource
->id
;
5549 free(resource_path_dup
);
5550 *p_resource_valid
= true;
5551 *p_resource_id
= resource_id
;
5554 err_resource_lookup
:
5555 free(resource_path_dup
);
5559 static int cmd_resource_set(struct dl
*dl
)
5561 struct nlmsghdr
*nlh
;
5562 struct resource_ctx ctx
= {};
5565 err
= resource_ctx_init(&ctx
, dl
);
5569 ctx
.print_resources
= false;
5570 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5571 DL_OPT_RESOURCE_SIZE
, 0);
5575 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5577 dl_opts_put(nlh
, dl
);
5578 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5580 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5584 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5585 &dl
->opts
.resource_id
,
5586 &dl
->opts
.resource_id_valid
);
5588 pr_err("error parsing resource path %s\n", strerror(-err
));
5592 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5593 NLM_F_REQUEST
| NLM_F_ACK
);
5595 dl_opts_put(nlh
, dl
);
5596 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5598 resource_ctx_fini(&ctx
);
5602 static int cmd_resource(struct dl
*dl
)
5604 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5605 cmd_resource_help();
5607 } else if (dl_argv_match(dl
, "show")) {
5609 return cmd_resource_show(dl
);
5610 } else if (dl_argv_match(dl
, "set")) {
5612 return cmd_resource_set(dl
);
5614 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5618 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5620 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5621 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5622 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5625 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5626 if (dl
->json_output
) {
5627 jsonw_name(dl
->jw
, buf
);
5628 jsonw_start_object(dl
->jw
);
5634 static void pr_out_region_handle_end(struct dl
*dl
)
5636 if (dl
->json_output
)
5637 jsonw_end_object(dl
->jw
);
5642 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5644 if (dl
->json_output
) {
5645 jsonw_name(dl
->jw
, "snapshot");
5646 jsonw_start_array(dl
->jw
);
5648 if (g_indent_newline
)
5649 pr_out("snapshot %s", array
? "[" : "");
5651 pr_out(" snapshot %s", array
? "[" : "");
5655 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5657 if (dl
->json_output
)
5658 jsonw_end_array(dl
->jw
);
5663 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5665 uint32_t snapshot_id
;
5667 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5670 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5672 if (dl
->json_output
)
5673 jsonw_uint(dl
->jw
, snapshot_id
);
5675 pr_out("%s%u", index
? " " : "", snapshot_id
);
5678 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5680 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5681 struct nlattr
*nla_sanpshot
;
5684 pr_out_region_snapshots_start(dl
, true);
5685 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5686 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5687 if (err
!= MNL_CB_OK
)
5689 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5691 pr_out_region_snapshots_end(dl
, true);
5694 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5696 pr_out_region_snapshots_start(dl
, false);
5697 pr_out_region_snapshots_id(dl
, tb
, 0);
5698 pr_out_region_snapshots_end(dl
, false);
5701 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5703 pr_out_region_handle_start(dl
, tb
);
5705 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5706 pr_out_u64(dl
, "size",
5707 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5709 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5710 pr_out_snapshots(dl
, tb
);
5712 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5713 pr_out_snapshot(dl
, tb
);
5715 pr_out_region_handle_end(dl
);
5718 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5720 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5721 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5722 struct dl
*dl
= data
;
5724 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5725 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5726 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5727 return MNL_CB_ERROR
;
5729 pr_out_region(dl
, tb
);
5734 static int cmd_region_show(struct dl
*dl
)
5736 struct nlmsghdr
*nlh
;
5737 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5740 if (dl_argc(dl
) == 0)
5741 flags
|= NLM_F_DUMP
;
5743 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5745 if (dl_argc(dl
) > 0) {
5746 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5751 pr_out_section_start(dl
, "regions");
5752 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5753 pr_out_section_end(dl
);
5757 static int cmd_region_snapshot_del(struct dl
*dl
)
5759 struct nlmsghdr
*nlh
;
5762 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5763 NLM_F_REQUEST
| NLM_F_ACK
);
5765 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5766 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5770 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5773 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5775 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5776 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5777 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5778 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5779 struct dl
*dl
= data
;
5782 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5783 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5784 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5785 return MNL_CB_ERROR
;
5787 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5788 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5789 if (err
!= MNL_CB_OK
)
5790 return MNL_CB_ERROR
;
5792 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5793 if (!nla_chunk_data
)
5796 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5797 if (!nla_chunk_addr
)
5800 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5801 mnl_attr_get_payload_len(nla_chunk_data
),
5802 mnl_attr_get_u64(nla_chunk_addr
));
5807 static int cmd_region_dump(struct dl
*dl
)
5809 struct nlmsghdr
*nlh
;
5812 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5813 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5815 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5816 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5820 pr_out_section_start(dl
, "dump");
5821 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5822 pr_out_section_end(dl
);
5823 if (!dl
->json_output
)
5828 static int cmd_region_read(struct dl
*dl
)
5830 struct nlmsghdr
*nlh
;
5833 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5834 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5836 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5837 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5838 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5842 pr_out_section_start(dl
, "read");
5843 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5844 pr_out_section_end(dl
);
5845 if (!dl
->json_output
)
5850 static void cmd_region_help(void)
5852 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5853 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5854 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5855 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5858 static int cmd_region(struct dl
*dl
)
5860 if (dl_no_arg(dl
)) {
5861 return cmd_region_show(dl
);
5862 } else if (dl_argv_match(dl
, "help")) {
5865 } else if (dl_argv_match(dl
, "show")) {
5867 return cmd_region_show(dl
);
5868 } else if (dl_argv_match(dl
, "del")) {
5870 return cmd_region_snapshot_del(dl
);
5871 } else if (dl_argv_match(dl
, "dump")) {
5873 return cmd_region_dump(dl
);
5874 } else if (dl_argv_match(dl
, "read")) {
5876 return cmd_region_read(dl
);
5878 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5882 static int cmd_health_set_params(struct dl
*dl
)
5884 struct nlmsghdr
*nlh
;
5887 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
5888 NLM_F_REQUEST
| NLM_F_ACK
);
5889 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
5890 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
5891 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
5895 dl_opts_put(nlh
, dl
);
5896 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5899 static int cmd_health_dump_clear(struct dl
*dl
)
5901 struct nlmsghdr
*nlh
;
5904 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
5905 NLM_F_REQUEST
| NLM_F_ACK
);
5907 err
= dl_argv_parse_put(nlh
, dl
,
5908 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
5912 dl_opts_put(nlh
, dl
);
5913 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5916 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
5923 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
5926 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
5929 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
5932 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
5935 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
5937 case MNL_TYPE_NUL_STRING
:
5938 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
5940 case MNL_TYPE_BINARY
:
5941 len
= mnl_attr_get_payload_len(nl_data
);
5942 data
= mnl_attr_get_payload(nl_data
);
5943 pr_out_binary_value(dl
, data
, len
);
5951 struct nest_qentry
{
5953 TAILQ_ENTRY(nest_qentry
) nest_entries
;
5956 struct fmsg_cb_data
{
5959 TAILQ_HEAD(, nest_qentry
) qhead
;
5962 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
5963 uint8_t *attr_value
, bool insert
)
5965 struct nest_qentry
*entry
= NULL
;
5968 entry
= malloc(sizeof(struct nest_qentry
));
5972 entry
->attr_type
= *attr_value
;
5973 TAILQ_INSERT_HEAD(&fmsg_data
->qhead
, entry
, nest_entries
);
5975 if (TAILQ_EMPTY(&fmsg_data
->qhead
))
5976 return MNL_CB_ERROR
;
5977 entry
= TAILQ_FIRST(&fmsg_data
->qhead
);
5978 *attr_value
= entry
->attr_type
;
5979 TAILQ_REMOVE(&fmsg_data
->qhead
, entry
, nest_entries
);
5985 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
5988 struct dl
*dl
= fmsg_data
->dl
;
5989 uint8_t value
= nest_value
;
5992 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
5993 if (err
!= MNL_CB_OK
)
5997 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
5999 pr_out_entry_start(dl
);
6001 pr_out_entry_end(dl
);
6003 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6005 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6006 if (dl
->json_output
) {
6008 jsonw_start_array(dl
->jw
);
6010 jsonw_end_array(dl
->jw
);
6014 __pr_out_indent_inc();
6016 __pr_out_indent_dec();
6026 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6028 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6029 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6030 struct fmsg_cb_data
*fmsg_data
= data
;
6031 struct dl
*dl
= fmsg_data
->dl
;
6032 struct nlattr
*nla_object
;
6036 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6037 if (!tb
[DEVLINK_ATTR_FMSG
])
6038 return MNL_CB_ERROR
;
6040 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6041 attr_type
= mnl_attr_get_type(nla_object
);
6042 switch (attr_type
) {
6043 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6044 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6045 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6046 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6047 if (err
!= MNL_CB_OK
)
6050 case DEVLINK_ATTR_FMSG_NEST_END
:
6051 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6052 if (err
!= MNL_CB_OK
)
6055 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6056 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6058 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6059 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6061 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6062 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6064 if (err
!= MNL_CB_OK
)
6074 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
)
6076 struct fmsg_cb_data data
;
6077 struct nlmsghdr
*nlh
;
6080 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, NLM_F_REQUEST
| NLM_F_ACK
);
6082 err
= dl_argv_parse_put(nlh
, dl
,
6083 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6088 TAILQ_INIT(&data
.qhead
);
6089 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6093 static int cmd_health_dump_show(struct dl
*dl
)
6095 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
6098 static int cmd_health_diagnose(struct dl
*dl
)
6100 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
);
6103 static int cmd_health_recover(struct dl
*dl
)
6105 struct nlmsghdr
*nlh
;
6108 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6109 NLM_F_REQUEST
| NLM_F_ACK
);
6111 err
= dl_argv_parse_put(nlh
, dl
,
6112 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6116 dl_opts_put(nlh
, dl
);
6117 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6120 enum devlink_health_reporter_state
{
6121 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6122 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6125 static const char *health_state_name(uint8_t state
)
6128 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6129 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6130 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6131 return HEALTH_REPORTER_STATE_ERROR_STR
;
6133 return "<unknown state>";
6137 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6139 struct sysinfo s_info
;
6145 info
= localtime(&now
);
6146 err
= sysinfo(&s_info
);
6149 /* Subtract uptime in sec from now yields the time of system
6150 * uptime. To this, add time_ms which is the amount of
6151 * milliseconds elapsed between uptime and the dump taken.
6153 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6154 info
= localtime(&sec
);
6156 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6157 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6160 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6162 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6163 enum devlink_health_reporter_state state
;
6164 const struct nlattr
*attr
;
6168 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6170 if (err
!= MNL_CB_OK
)
6173 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6174 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6175 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6176 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6179 pr_out_handle_start_arr(dl
, tb_health
);
6181 pr_out_str(dl
, "name",
6182 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6183 if (!dl
->json_output
) {
6185 __pr_out_indent_inc();
6187 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6188 pr_out_str(dl
, "state", health_state_name(state
));
6189 pr_out_u64(dl
, "error",
6190 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6191 pr_out_u64(dl
, "recover",
6192 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6193 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6194 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6195 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6197 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6198 time_ms
= mnl_attr_get_u64(attr
);
6199 format_logtime(time_ms
, dump_date
, dump_time
);
6201 pr_out_str(dl
, "last_dump_date", dump_date
);
6202 pr_out_str(dl
, "last_dump_time", dump_time
);
6204 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6205 pr_out_u64(dl
, "grace_period",
6206 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6207 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6208 pr_out_bool(dl
, "auto_recover",
6209 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6211 __pr_out_indent_dec();
6212 pr_out_handle_end(dl
);
6215 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6217 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6218 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6219 struct dl
*dl
= data
;
6221 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6222 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6223 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6224 return MNL_CB_ERROR
;
6226 pr_out_health(dl
, tb
);
6231 static int cmd_health_show(struct dl
*dl
)
6233 struct nlmsghdr
*nlh
;
6234 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6237 if (dl_argc(dl
) == 0)
6238 flags
|= NLM_F_DUMP
;
6239 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6242 if (dl_argc(dl
) > 0) {
6243 err
= dl_argv_parse_put(nlh
, dl
,
6245 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6249 pr_out_section_start(dl
, "health");
6251 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6252 pr_out_section_end(dl
);
6256 static void cmd_health_help(void)
6258 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6259 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6260 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6261 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6262 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6263 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6266 static int cmd_health(struct dl
*dl
)
6268 if (dl_argv_match(dl
, "help")) {
6271 } else if (dl_argv_match(dl
, "show") ||
6272 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6274 return cmd_health_show(dl
);
6275 } else if (dl_argv_match(dl
, "recover")) {
6277 return cmd_health_recover(dl
);
6278 } else if (dl_argv_match(dl
, "diagnose")) {
6280 return cmd_health_diagnose(dl
);
6281 } else if (dl_argv_match(dl
, "dump")) {
6283 if (dl_argv_match(dl
, "show")) {
6285 return cmd_health_dump_show(dl
);
6286 } else if (dl_argv_match(dl
, "clear")) {
6288 return cmd_health_dump_clear(dl
);
6290 } else if (dl_argv_match(dl
, "set")) {
6292 return cmd_health_set_params(dl
);
6294 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6298 static void help(void)
6300 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
6301 " devlink [ -f[orce] ] -b[atch] filename\n"
6302 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health }\n"
6303 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
6306 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
6311 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6314 } else if (dl_argv_match(dl
, "dev")) {
6317 } else if (dl_argv_match(dl
, "port")) {
6319 return cmd_port(dl
);
6320 } else if (dl_argv_match(dl
, "sb")) {
6323 } else if (dl_argv_match(dl
, "monitor")) {
6326 } else if (dl_argv_match(dl
, "dpipe")) {
6328 return cmd_dpipe(dl
);
6329 } else if (dl_argv_match(dl
, "resource")) {
6331 return cmd_resource(dl
);
6332 } else if (dl_argv_match(dl
, "region")) {
6334 return cmd_region(dl
);
6335 } else if (dl_argv_match(dl
, "health")) {
6337 return cmd_health(dl
);
6339 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
6343 static int dl_init(struct dl
*dl
)
6347 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
6349 pr_err("Failed to connect to devlink Netlink\n");
6353 err
= ifname_map_init(dl
);
6355 pr_err("Failed to create index map\n");
6356 goto err_ifname_map_create
;
6358 if (dl
->json_output
) {
6359 dl
->jw
= jsonw_new(stdout
);
6361 pr_err("Failed to create JSON writer\n");
6364 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
6369 ifname_map_fini(dl
);
6370 err_ifname_map_create
:
6371 mnlg_socket_close(dl
->nlg
);
6375 static void dl_fini(struct dl
*dl
)
6377 if (dl
->json_output
)
6378 jsonw_destroy(&dl
->jw
);
6379 ifname_map_fini(dl
);
6380 mnlg_socket_close(dl
->nlg
);
6383 static struct dl
*dl_alloc(void)
6387 dl
= calloc(1, sizeof(*dl
));
6393 static void dl_free(struct dl
*dl
)
6398 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
6402 int ret
= EXIT_SUCCESS
;
6404 if (name
&& strcmp(name
, "-") != 0) {
6405 if (freopen(name
, "r", stdin
) == NULL
) {
6407 "Cannot open file \"%s\" for reading: %s\n",
6408 name
, strerror(errno
));
6409 return EXIT_FAILURE
;
6414 while (getcmdline(&line
, &len
, stdin
) != -1) {
6418 largc
= makeargs(line
, largv
, 100);
6420 continue; /* blank line */
6422 if (dl_cmd(dl
, largc
, largv
)) {
6423 fprintf(stderr
, "Command failed %s:%d\n",
6437 int main(int argc
, char **argv
)
6439 static const struct option long_options
[] = {
6440 { "Version", no_argument
, NULL
, 'V' },
6441 { "force", no_argument
, NULL
, 'f' },
6442 { "batch", required_argument
, NULL
, 'b' },
6443 { "no-nice-names", no_argument
, NULL
, 'n' },
6444 { "json", no_argument
, NULL
, 'j' },
6445 { "pretty", no_argument
, NULL
, 'p' },
6446 { "verbose", no_argument
, NULL
, 'v' },
6447 { NULL
, 0, NULL
, 0 }
6449 const char *batch_file
= NULL
;
6458 pr_err("Failed to allocate memory for devlink\n");
6459 return EXIT_FAILURE
;
6462 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
6463 long_options
, NULL
)) >= 0) {
6467 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
6474 batch_file
= optarg
;
6477 dl
->no_nice_names
= true;
6480 dl
->json_output
= true;
6483 dl
->pretty_output
= true;
6489 pr_err("Unknown option.\n");
6506 err
= dl_batch(dl
, batch_file
, force
);
6508 err
= dl_cmd(dl
, argc
, argv
);