2 * devlink.c Devlink tool
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
22 #include <sys/sysinfo.h>
23 #define _LINUX_SYSINFO_H /* avoid collision with musl header */
24 #include <linux/genetlink.h>
25 #include <linux/devlink.h>
26 #include <libmnl/libmnl.h>
27 #include <netinet/ether.h>
28 #include <sys/queue.h>
33 #include "json_writer.h"
36 #define ESWITCH_MODE_LEGACY "legacy"
37 #define ESWITCH_MODE_SWITCHDEV "switchdev"
38 #define ESWITCH_INLINE_MODE_NONE "none"
39 #define ESWITCH_INLINE_MODE_LINK "link"
40 #define ESWITCH_INLINE_MODE_NETWORK "network"
41 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
43 #define PARAM_CMODE_RUNTIME_STR "runtime"
44 #define PARAM_CMODE_DRIVERINIT_STR "driverinit"
45 #define PARAM_CMODE_PERMANENT_STR "permanent"
46 #define DL_ARGS_REQUIRED_MAX_ERR_LEN 80
48 #define HEALTH_REPORTER_STATE_HEALTHY_STR "healthy"
49 #define HEALTH_REPORTER_STATE_ERROR_STR "error"
50 #define HEALTH_REPORTER_TIMESTAMP_FMT_LEN 80
52 static int g_new_line_count
;
53 static int g_indent_level
;
54 static bool g_indent_newline
;
56 #define INDENT_STR_STEP 2
57 #define INDENT_STR_MAXLEN 32
58 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
60 static void __attribute__((format(printf
, 1, 2)))
61 pr_err(const char *fmt
, ...)
66 vfprintf(stderr
, fmt
, ap
);
70 static void __attribute__((format(printf
, 1, 2)))
71 pr_out(const char *fmt
, ...)
75 if (g_indent_newline
) {
76 printf("%s", g_indent_str
);
77 g_indent_newline
= false;
85 static void __attribute__((format(printf
, 2, 3)))
86 pr_out_sp(unsigned int num
, const char *fmt
, ...)
92 ret
= vprintf(fmt
, ap
);
96 printf("%*s", num
- ret
, "");
97 g_new_line_count
= 0; \
100 static void __pr_out_indent_inc(void)
102 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
104 g_indent_level
+= INDENT_STR_STEP
;
105 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
106 g_indent_str
[g_indent_level
] = '\0';
109 static void __pr_out_indent_dec(void)
111 if (g_indent_level
- INDENT_STR_STEP
< 0)
113 g_indent_level
-= INDENT_STR_STEP
;
114 g_indent_str
[g_indent_level
] = '\0';
117 static void __pr_out_newline(void)
119 if (g_new_line_count
< 1) {
121 g_indent_newline
= true;
126 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
127 mnl_cb_t data_cb
, void *data
)
131 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
133 pr_err("devlink answers: %s\n", strerror(errno
));
139 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
140 const struct nlmsghdr
*nlh
,
141 mnl_cb_t data_cb
, void *data
)
145 err
= mnlg_socket_send(nlg
, nlh
);
147 pr_err("Failed to call mnlg_socket_send\n");
150 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
153 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
154 const char *group_name
)
158 err
= mnlg_socket_group_add(nlg
, group_name
);
160 pr_err("Failed to call mnlg_socket_group_add\n");
167 struct list_head list
;
174 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
175 const char *dev_name
,
179 struct ifname_map
*ifname_map
;
181 ifname_map
= calloc(1, sizeof(*ifname_map
));
184 ifname_map
->bus_name
= strdup(bus_name
);
185 ifname_map
->dev_name
= strdup(dev_name
);
186 ifname_map
->port_index
= port_index
;
187 ifname_map
->ifname
= strdup(ifname
);
188 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
189 !ifname_map
->ifname
) {
190 free(ifname_map
->ifname
);
191 free(ifname_map
->dev_name
);
192 free(ifname_map
->bus_name
);
199 static void ifname_map_free(struct ifname_map
*ifname_map
)
201 free(ifname_map
->ifname
);
202 free(ifname_map
->dev_name
);
203 free(ifname_map
->bus_name
);
207 #define DL_OPT_HANDLE BIT(0)
208 #define DL_OPT_HANDLEP BIT(1)
209 #define DL_OPT_PORT_TYPE BIT(2)
210 #define DL_OPT_PORT_COUNT BIT(3)
211 #define DL_OPT_SB BIT(4)
212 #define DL_OPT_SB_POOL BIT(5)
213 #define DL_OPT_SB_SIZE BIT(6)
214 #define DL_OPT_SB_TYPE BIT(7)
215 #define DL_OPT_SB_THTYPE BIT(8)
216 #define DL_OPT_SB_TH BIT(9)
217 #define DL_OPT_SB_TC BIT(10)
218 #define DL_OPT_ESWITCH_MODE BIT(11)
219 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
220 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
221 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
222 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
223 #define DL_OPT_RESOURCE_PATH BIT(16)
224 #define DL_OPT_RESOURCE_SIZE BIT(17)
225 #define DL_OPT_PARAM_NAME BIT(18)
226 #define DL_OPT_PARAM_VALUE BIT(19)
227 #define DL_OPT_PARAM_CMODE BIT(20)
228 #define DL_OPT_HANDLE_REGION BIT(21)
229 #define DL_OPT_REGION_SNAPSHOT_ID BIT(22)
230 #define DL_OPT_REGION_ADDRESS BIT(23)
231 #define DL_OPT_REGION_LENGTH BIT(24)
232 #define DL_OPT_FLASH_FILE_NAME BIT(25)
233 #define DL_OPT_FLASH_COMPONENT BIT(26)
234 #define DL_OPT_HEALTH_REPORTER_NAME BIT(27)
235 #define DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD BIT(27)
236 #define DL_OPT_HEALTH_REPORTER_AUTO_RECOVER BIT(28)
239 uint32_t present
; /* flags of present items */
243 enum devlink_port_type port_type
;
246 uint16_t sb_pool_index
;
247 uint32_t sb_pool_size
;
248 enum devlink_sb_pool_type sb_pool_type
;
249 enum devlink_sb_threshold_type sb_pool_thtype
;
250 uint32_t sb_threshold
;
251 uint16_t sb_tc_index
;
252 enum devlink_eswitch_mode eswitch_mode
;
253 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
254 const char *dpipe_table_name
;
255 bool dpipe_counters_enable
;
256 bool eswitch_encap_mode
;
257 const char *resource_path
;
258 uint32_t resource_size
;
259 uint32_t resource_id
;
260 bool resource_id_valid
;
261 const char *param_name
;
262 const char *param_value
;
263 enum devlink_param_cmode cmode
;
265 uint32_t region_snapshot_id
;
266 uint64_t region_address
;
267 uint64_t region_length
;
268 const char *flash_file_name
;
269 const char *flash_component
;
270 const char *reporter_name
;
271 uint64_t reporter_graceful_period
;
272 bool reporter_auto_recover
;
276 struct mnlg_socket
*nlg
;
277 struct list_head ifname_map_list
;
294 static int dl_argc(struct dl
*dl
)
299 static char *dl_argv(struct dl
*dl
)
301 if (dl_argc(dl
) == 0)
306 static void dl_arg_inc(struct dl
*dl
)
308 if (dl_argc(dl
) == 0)
314 static char *dl_argv_next(struct dl
*dl
)
318 if (dl_argc(dl
) == 0)
326 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
328 if (index
>= dl_argc(dl
))
330 return dl
->argv
[index
];
333 static int strcmpx(const char *str1
, const char *str2
)
335 if (strlen(str1
) > strlen(str2
))
337 return strncmp(str1
, str2
, strlen(str1
));
340 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
342 if (dl_argc(dl
) == 0)
344 return strcmpx(dl_argv(dl
), pattern
) == 0;
347 static bool dl_no_arg(struct dl
*dl
)
349 return dl_argc(dl
) == 0;
352 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
353 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
354 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
355 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
356 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
357 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
358 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
359 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
360 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
361 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
362 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
363 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
364 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
365 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
366 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
367 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
368 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
369 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
370 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
371 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
372 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
373 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
374 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
375 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
376 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
377 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
378 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
379 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
380 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
381 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
382 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
383 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
384 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
385 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
386 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
387 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
388 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
389 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
390 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
391 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
392 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
393 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
394 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
395 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
396 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
397 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
398 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
399 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
400 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
401 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
402 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
403 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
404 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
405 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
406 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
407 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
408 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
409 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
410 [DEVLINK_ATTR_PARAM
] = MNL_TYPE_NESTED
,
411 [DEVLINK_ATTR_PARAM_NAME
] = MNL_TYPE_STRING
,
412 [DEVLINK_ATTR_PARAM_TYPE
] = MNL_TYPE_U8
,
413 [DEVLINK_ATTR_PARAM_VALUES_LIST
] = MNL_TYPE_NESTED
,
414 [DEVLINK_ATTR_PARAM_VALUE
] = MNL_TYPE_NESTED
,
415 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = MNL_TYPE_U8
,
416 [DEVLINK_ATTR_REGION_NAME
] = MNL_TYPE_STRING
,
417 [DEVLINK_ATTR_REGION_SIZE
] = MNL_TYPE_U64
,
418 [DEVLINK_ATTR_REGION_SNAPSHOTS
] = MNL_TYPE_NESTED
,
419 [DEVLINK_ATTR_REGION_SNAPSHOT
] = MNL_TYPE_NESTED
,
420 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = MNL_TYPE_U32
,
421 [DEVLINK_ATTR_REGION_CHUNKS
] = MNL_TYPE_NESTED
,
422 [DEVLINK_ATTR_REGION_CHUNK
] = MNL_TYPE_NESTED
,
423 [DEVLINK_ATTR_REGION_CHUNK_DATA
] = MNL_TYPE_BINARY
,
424 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = MNL_TYPE_U64
,
425 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = MNL_TYPE_U64
,
426 [DEVLINK_ATTR_INFO_DRIVER_NAME
] = MNL_TYPE_STRING
,
427 [DEVLINK_ATTR_INFO_SERIAL_NUMBER
] = MNL_TYPE_STRING
,
428 [DEVLINK_ATTR_INFO_VERSION_FIXED
] = MNL_TYPE_NESTED
,
429 [DEVLINK_ATTR_INFO_VERSION_RUNNING
] = MNL_TYPE_NESTED
,
430 [DEVLINK_ATTR_INFO_VERSION_STORED
] = MNL_TYPE_NESTED
,
431 [DEVLINK_ATTR_INFO_VERSION_NAME
] = MNL_TYPE_STRING
,
432 [DEVLINK_ATTR_INFO_VERSION_VALUE
] = MNL_TYPE_STRING
,
433 [DEVLINK_ATTR_HEALTH_REPORTER
] = MNL_TYPE_NESTED
,
434 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = MNL_TYPE_STRING
,
435 [DEVLINK_ATTR_HEALTH_REPORTER_STATE
] = MNL_TYPE_U8
,
436 [DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] = MNL_TYPE_U64
,
437 [DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] = MNL_TYPE_U64
,
438 [DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
] = MNL_TYPE_U64
,
439 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = MNL_TYPE_U64
,
442 static int attr_cb(const struct nlattr
*attr
, void *data
)
444 const struct nlattr
**tb
= data
;
447 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
450 type
= mnl_attr_get_type(attr
);
451 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
458 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
460 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
461 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
462 struct dl
*dl
= data
;
463 struct ifname_map
*ifname_map
;
464 const char *bus_name
;
465 const char *dev_name
;
466 uint32_t port_ifindex
;
467 const char *port_ifname
;
469 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
470 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
471 !tb
[DEVLINK_ATTR_PORT_INDEX
])
474 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
477 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
478 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
479 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
480 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
481 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
482 port_ifindex
, port_ifname
);
485 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
490 static void ifname_map_fini(struct dl
*dl
)
492 struct ifname_map
*ifname_map
, *tmp
;
494 list_for_each_entry_safe(ifname_map
, tmp
,
495 &dl
->ifname_map_list
, list
) {
496 list_del(&ifname_map
->list
);
497 ifname_map_free(ifname_map
);
501 static int ifname_map_init(struct dl
*dl
)
503 struct nlmsghdr
*nlh
;
506 INIT_LIST_HEAD(&dl
->ifname_map_list
);
508 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
509 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
511 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
519 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
520 char **p_bus_name
, char **p_dev_name
,
521 uint32_t *p_port_index
)
523 struct ifname_map
*ifname_map
;
525 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
526 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
527 *p_bus_name
= ifname_map
->bus_name
;
528 *p_dev_name
= ifname_map
->dev_name
;
529 *p_port_index
= ifname_map
->port_index
;
536 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
537 const char *dev_name
, uint32_t port_index
,
540 struct ifname_map
*ifname_map
;
542 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
543 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
544 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
545 port_index
== ifname_map
->port_index
) {
546 *p_ifname
= ifname_map
->ifname
;
553 static unsigned int strslashcount(char *str
)
555 unsigned int count
= 0;
558 while ((pos
= strchr(pos
, '/'))) {
565 static int strslashrsplit(char *str
, char **before
, char **after
)
569 slash
= strrchr(str
, '/');
578 static int strtouint64_t(const char *str
, uint64_t *p_val
)
581 unsigned long long int val
;
583 val
= strtoull(str
, &endptr
, 10);
584 if (endptr
== str
|| *endptr
!= '\0')
592 static int strtouint32_t(const char *str
, uint32_t *p_val
)
595 unsigned long int val
;
597 val
= strtoul(str
, &endptr
, 10);
598 if (endptr
== str
|| *endptr
!= '\0')
606 static int strtouint16_t(const char *str
, uint16_t *p_val
)
609 unsigned long int val
;
611 val
= strtoul(str
, &endptr
, 10);
612 if (endptr
== str
|| *endptr
!= '\0')
620 static int strtouint8_t(const char *str
, uint8_t *p_val
)
623 unsigned long int val
;
625 val
= strtoul(str
, &endptr
, 10);
626 if (endptr
== str
|| *endptr
!= '\0')
634 static int strtobool(const char *str
, bool *p_val
)
638 if (!strcmp(str
, "true") || !strcmp(str
, "1"))
640 else if (!strcmp(str
, "false") || !strcmp(str
, "0"))
648 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
650 strslashrsplit(str
, p_bus_name
, p_dev_name
);
654 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
656 char *str
= dl_argv_next(dl
);
659 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
662 if (strslashcount(str
) != 1) {
663 pr_err("Wrong devlink identification string format.\n");
664 pr_err("Expected \"bus_name/dev_name\".\n");
667 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
670 static int __dl_argv_handle_port(char *str
,
671 char **p_bus_name
, char **p_dev_name
,
672 uint32_t *p_port_index
)
678 err
= strslashrsplit(str
, &handlestr
, &portstr
);
680 pr_err("Port identification \"%s\" is invalid\n", str
);
683 err
= strtouint32_t(portstr
, p_port_index
);
685 pr_err("Port index \"%s\" is not a number or not within range\n",
689 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
691 pr_err("Port identification \"%s\" is invalid\n", str
);
697 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
698 char **p_bus_name
, char **p_dev_name
,
699 uint32_t *p_port_index
)
703 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
706 pr_err("Netdevice \"%s\" not found\n", str
);
712 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
713 char **p_dev_name
, uint32_t *p_port_index
)
715 char *str
= dl_argv_next(dl
);
716 unsigned int slash_count
;
719 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
722 slash_count
= strslashcount(str
);
723 switch (slash_count
) {
725 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
726 p_dev_name
, p_port_index
);
728 return __dl_argv_handle_port(str
, p_bus_name
,
729 p_dev_name
, p_port_index
);
731 pr_err("Wrong port identification string format.\n");
732 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
737 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
738 char **p_dev_name
, uint32_t *p_port_index
,
739 uint32_t *p_handle_bit
)
741 char *str
= dl_argv_next(dl
);
742 unsigned int slash_count
;
746 pr_err("One of following identifications expected:\n"
747 "Devlink identification (\"bus_name/dev_name\")\n"
748 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
751 slash_count
= strslashcount(str
);
752 if (slash_count
== 1) {
753 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
756 *p_handle_bit
= DL_OPT_HANDLE
;
757 } else if (slash_count
== 2) {
758 err
= __dl_argv_handle_port(str
, p_bus_name
,
759 p_dev_name
, p_port_index
);
762 *p_handle_bit
= DL_OPT_HANDLEP
;
763 } else if (slash_count
== 0) {
764 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
765 p_dev_name
, p_port_index
);
768 *p_handle_bit
= DL_OPT_HANDLEP
;
770 pr_err("Wrong port identification string format.\n");
771 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
777 static int __dl_argv_handle_region(char *str
, char **p_bus_name
,
778 char **p_dev_name
, char **p_region
)
783 err
= strslashrsplit(str
, &handlestr
, p_region
);
785 pr_err("Region identification \"%s\" is invalid\n", str
);
788 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
790 pr_err("Region identification \"%s\" is invalid\n", str
);
796 static int dl_argv_handle_region(struct dl
*dl
, char **p_bus_name
,
797 char **p_dev_name
, char **p_region
)
799 char *str
= dl_argv_next(dl
);
800 unsigned int slash_count
;
803 pr_err("Expected \"bus_name/dev_name/region\" identification.\n");
807 slash_count
= strslashcount(str
);
808 if (slash_count
!= 2) {
809 pr_err("Wrong region identification string format.\n");
810 pr_err("Expected \"bus_name/dev_name/region\" identification.\n"".\n");
814 return __dl_argv_handle_region(str
, p_bus_name
, p_dev_name
, p_region
);
817 static int dl_argv_uint64_t(struct dl
*dl
, uint64_t *p_val
)
819 char *str
= dl_argv_next(dl
);
823 pr_err("Unsigned number argument expected\n");
827 err
= strtouint64_t(str
, p_val
);
829 pr_err("\"%s\" is not a number or not within range\n", str
);
835 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
837 char *str
= dl_argv_next(dl
);
841 pr_err("Unsigned number argument expected\n");
845 err
= strtouint32_t(str
, p_val
);
847 pr_err("\"%s\" is not a number or not within range\n", str
);
853 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
855 char *str
= dl_argv_next(dl
);
859 pr_err("Unsigned number argument expected\n");
863 err
= strtouint16_t(str
, p_val
);
865 pr_err("\"%s\" is not a number or not within range\n", str
);
871 static int dl_argv_bool(struct dl
*dl
, bool *p_val
)
873 char *str
= dl_argv_next(dl
);
877 pr_err("Boolean argument expected\n");
881 err
= strtobool(str
, p_val
);
883 pr_err("\"%s\" is not a valid boolean value\n", str
);
889 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
891 const char *str
= dl_argv_next(dl
);
894 pr_err("String parameter expected\n");
901 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
903 if (strcmp(typestr
, "auto") == 0) {
904 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
905 } else if (strcmp(typestr
, "eth") == 0) {
906 *p_type
= DEVLINK_PORT_TYPE_ETH
;
907 } else if (strcmp(typestr
, "ib") == 0) {
908 *p_type
= DEVLINK_PORT_TYPE_IB
;
910 pr_err("Unknown port type \"%s\"\n", typestr
);
916 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
918 if (strcmp(typestr
, "ingress") == 0) {
919 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
920 } else if (strcmp(typestr
, "egress") == 0) {
921 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
923 pr_err("Unknown pool type \"%s\"\n", typestr
);
929 static int threshold_type_get(const char *typestr
,
930 enum devlink_sb_threshold_type
*p_type
)
932 if (strcmp(typestr
, "static") == 0) {
933 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
934 } else if (strcmp(typestr
, "dynamic") == 0) {
935 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
937 pr_err("Unknown threshold type \"%s\"\n", typestr
);
943 static int eswitch_mode_get(const char *typestr
,
944 enum devlink_eswitch_mode
*p_mode
)
946 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
947 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
948 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
949 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
951 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
957 static int eswitch_inline_mode_get(const char *typestr
,
958 enum devlink_eswitch_inline_mode
*p_mode
)
960 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
961 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
962 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
963 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
964 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
965 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
966 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
967 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
969 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
975 static int dpipe_counters_enable_get(const char *typestr
,
976 bool *counters_enable
)
978 if (strcmp(typestr
, "enable") == 0) {
979 *counters_enable
= 1;
980 } else if (strcmp(typestr
, "disable") == 0) {
981 *counters_enable
= 0;
983 pr_err("Unknown counter_state \"%s\"\n", typestr
);
989 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
991 if (strcmp(typestr
, "enable") == 0) {
993 } else if (strcmp(typestr
, "disable") == 0) {
996 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
1002 static int param_cmode_get(const char *cmodestr
,
1003 enum devlink_param_cmode
*cmode
)
1005 if (strcmp(cmodestr
, PARAM_CMODE_RUNTIME_STR
) == 0) {
1006 *cmode
= DEVLINK_PARAM_CMODE_RUNTIME
;
1007 } else if (strcmp(cmodestr
, PARAM_CMODE_DRIVERINIT_STR
) == 0) {
1008 *cmode
= DEVLINK_PARAM_CMODE_DRIVERINIT
;
1009 } else if (strcmp(cmodestr
, PARAM_CMODE_PERMANENT_STR
) == 0) {
1010 *cmode
= DEVLINK_PARAM_CMODE_PERMANENT
;
1012 pr_err("Unknown configuration mode \"%s\"\n", cmodestr
);
1018 struct dl_args_metadata
{
1020 char err_msg
[DL_ARGS_REQUIRED_MAX_ERR_LEN
];
1023 static const struct dl_args_metadata dl_args_required
[] = {
1024 {DL_OPT_PORT_TYPE
, "Port type not set."},
1025 {DL_OPT_PORT_COUNT
, "Port split count option expected."},
1026 {DL_OPT_SB_POOL
, "Pool index option expected."},
1027 {DL_OPT_SB_SIZE
, "Pool size option expected."},
1028 {DL_OPT_SB_TYPE
, "Pool type option expected."},
1029 {DL_OPT_SB_THTYPE
, "Pool threshold type option expected."},
1030 {DL_OPT_SB_TH
, "Threshold option expected."},
1031 {DL_OPT_SB_TC
, "TC index option expected."},
1032 {DL_OPT_ESWITCH_MODE
, "E-Switch mode option expected."},
1033 {DL_OPT_ESWITCH_INLINE_MODE
, "E-Switch inline-mode option expected."},
1034 {DL_OPT_DPIPE_TABLE_NAME
, "Dpipe table name expected."},
1035 {DL_OPT_DPIPE_TABLE_COUNTERS
, "Dpipe table counter state expected."},
1036 {DL_OPT_ESWITCH_ENCAP_MODE
, "E-Switch encapsulation option expected."},
1037 {DL_OPT_PARAM_NAME
, "Parameter name expected."},
1038 {DL_OPT_PARAM_VALUE
, "Value to set expected."},
1039 {DL_OPT_PARAM_CMODE
, "Configuration mode expected."},
1040 {DL_OPT_REGION_SNAPSHOT_ID
, "Region snapshot id expected."},
1041 {DL_OPT_REGION_ADDRESS
, "Region address value expected."},
1042 {DL_OPT_REGION_LENGTH
, "Region length value expected."},
1043 {DL_OPT_HEALTH_REPORTER_NAME
, "Reporter's name is expected."},
1046 static int dl_args_finding_required_validate(uint32_t o_required
,
1052 for (i
= 0; i
< ARRAY_SIZE(dl_args_required
); i
++) {
1053 o_flag
= dl_args_required
[i
].o_flag
;
1054 if ((o_required
& o_flag
) && !(o_found
& o_flag
)) {
1055 pr_err("%s\n", dl_args_required
[i
].err_msg
);
1062 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
1063 uint32_t o_optional
)
1065 struct dl_opts
*opts
= &dl
->opts
;
1066 uint32_t o_all
= o_required
| o_optional
;
1067 uint32_t o_found
= 0;
1070 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
1071 uint32_t handle_bit
;
1073 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
1074 &opts
->port_index
, &handle_bit
);
1077 o_found
|= handle_bit
;
1078 } else if (o_required
& DL_OPT_HANDLE
) {
1079 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
1082 o_found
|= DL_OPT_HANDLE
;
1083 } else if (o_required
& DL_OPT_HANDLEP
) {
1084 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
1088 o_found
|= DL_OPT_HANDLEP
;
1089 } else if (o_required
& DL_OPT_HANDLE_REGION
) {
1090 err
= dl_argv_handle_region(dl
, &opts
->bus_name
,
1092 &opts
->region_name
);
1095 o_found
|= DL_OPT_HANDLE_REGION
;
1098 while (dl_argc(dl
)) {
1099 if (dl_argv_match(dl
, "type") &&
1100 (o_all
& DL_OPT_PORT_TYPE
)) {
1101 const char *typestr
;
1104 err
= dl_argv_str(dl
, &typestr
);
1107 err
= port_type_get(typestr
, &opts
->port_type
);
1110 o_found
|= DL_OPT_PORT_TYPE
;
1111 } else if (dl_argv_match(dl
, "count") &&
1112 (o_all
& DL_OPT_PORT_COUNT
)) {
1114 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
1117 o_found
|= DL_OPT_PORT_COUNT
;
1118 } else if (dl_argv_match(dl
, "sb") &&
1119 (o_all
& DL_OPT_SB
)) {
1121 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
1124 o_found
|= DL_OPT_SB
;
1125 } else if (dl_argv_match(dl
, "pool") &&
1126 (o_all
& DL_OPT_SB_POOL
)) {
1128 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
1131 o_found
|= DL_OPT_SB_POOL
;
1132 } else if (dl_argv_match(dl
, "size") &&
1133 (o_all
& DL_OPT_SB_SIZE
)) {
1135 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
1138 o_found
|= DL_OPT_SB_SIZE
;
1139 } else if (dl_argv_match(dl
, "type") &&
1140 (o_all
& DL_OPT_SB_TYPE
)) {
1141 const char *typestr
;
1144 err
= dl_argv_str(dl
, &typestr
);
1147 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
1150 o_found
|= DL_OPT_SB_TYPE
;
1151 } else if (dl_argv_match(dl
, "thtype") &&
1152 (o_all
& DL_OPT_SB_THTYPE
)) {
1153 const char *typestr
;
1156 err
= dl_argv_str(dl
, &typestr
);
1159 err
= threshold_type_get(typestr
,
1160 &opts
->sb_pool_thtype
);
1163 o_found
|= DL_OPT_SB_THTYPE
;
1164 } else if (dl_argv_match(dl
, "th") &&
1165 (o_all
& DL_OPT_SB_TH
)) {
1167 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
1170 o_found
|= DL_OPT_SB_TH
;
1171 } else if (dl_argv_match(dl
, "tc") &&
1172 (o_all
& DL_OPT_SB_TC
)) {
1174 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
1177 o_found
|= DL_OPT_SB_TC
;
1178 } else if (dl_argv_match(dl
, "mode") &&
1179 (o_all
& DL_OPT_ESWITCH_MODE
)) {
1180 const char *typestr
;
1183 err
= dl_argv_str(dl
, &typestr
);
1186 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
1189 o_found
|= DL_OPT_ESWITCH_MODE
;
1190 } else if (dl_argv_match(dl
, "inline-mode") &&
1191 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1192 const char *typestr
;
1195 err
= dl_argv_str(dl
, &typestr
);
1198 err
= eswitch_inline_mode_get(
1199 typestr
, &opts
->eswitch_inline_mode
);
1202 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
1203 } else if (dl_argv_match(dl
, "name") &&
1204 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
1206 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
1209 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
1210 } else if (dl_argv_match(dl
, "counters") &&
1211 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1212 const char *typestr
;
1215 err
= dl_argv_str(dl
, &typestr
);
1218 err
= dpipe_counters_enable_get(typestr
,
1219 &opts
->dpipe_counters_enable
);
1222 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
1223 } else if (dl_argv_match(dl
, "encap") &&
1224 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1225 const char *typestr
;
1228 err
= dl_argv_str(dl
, &typestr
);
1231 err
= eswitch_encap_mode_get(typestr
,
1232 &opts
->eswitch_encap_mode
);
1235 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
1236 } else if (dl_argv_match(dl
, "path") &&
1237 (o_all
& DL_OPT_RESOURCE_PATH
)) {
1239 err
= dl_argv_str(dl
, &opts
->resource_path
);
1242 o_found
|= DL_OPT_RESOURCE_PATH
;
1243 } else if (dl_argv_match(dl
, "size") &&
1244 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
1246 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
1249 o_found
|= DL_OPT_RESOURCE_SIZE
;
1250 } else if (dl_argv_match(dl
, "name") &&
1251 (o_all
& DL_OPT_PARAM_NAME
)) {
1253 err
= dl_argv_str(dl
, &opts
->param_name
);
1256 o_found
|= DL_OPT_PARAM_NAME
;
1257 } else if (dl_argv_match(dl
, "value") &&
1258 (o_all
& DL_OPT_PARAM_VALUE
)) {
1260 err
= dl_argv_str(dl
, &opts
->param_value
);
1263 o_found
|= DL_OPT_PARAM_VALUE
;
1264 } else if (dl_argv_match(dl
, "cmode") &&
1265 (o_all
& DL_OPT_PARAM_CMODE
)) {
1266 const char *cmodestr
;
1269 err
= dl_argv_str(dl
, &cmodestr
);
1272 err
= param_cmode_get(cmodestr
, &opts
->cmode
);
1275 o_found
|= DL_OPT_PARAM_CMODE
;
1276 } else if (dl_argv_match(dl
, "snapshot") &&
1277 (o_all
& DL_OPT_REGION_SNAPSHOT_ID
)) {
1279 err
= dl_argv_uint32_t(dl
, &opts
->region_snapshot_id
);
1282 o_found
|= DL_OPT_REGION_SNAPSHOT_ID
;
1283 } else if (dl_argv_match(dl
, "address") &&
1284 (o_all
& DL_OPT_REGION_ADDRESS
)) {
1286 err
= dl_argv_uint64_t(dl
, &opts
->region_address
);
1289 o_found
|= DL_OPT_REGION_ADDRESS
;
1290 } else if (dl_argv_match(dl
, "length") &&
1291 (o_all
& DL_OPT_REGION_LENGTH
)) {
1293 err
= dl_argv_uint64_t(dl
, &opts
->region_length
);
1296 o_found
|= DL_OPT_REGION_LENGTH
;
1297 } else if (dl_argv_match(dl
, "file") &&
1298 (o_all
& DL_OPT_FLASH_FILE_NAME
)) {
1300 err
= dl_argv_str(dl
, &opts
->flash_file_name
);
1303 o_found
|= DL_OPT_FLASH_FILE_NAME
;
1304 } else if (dl_argv_match(dl
, "component") &&
1305 (o_all
& DL_OPT_FLASH_COMPONENT
)) {
1307 err
= dl_argv_str(dl
, &opts
->flash_component
);
1310 o_found
|= DL_OPT_FLASH_COMPONENT
;
1311 } else if (dl_argv_match(dl
, "reporter") &&
1312 (o_all
& DL_OPT_HEALTH_REPORTER_NAME
)) {
1314 err
= dl_argv_str(dl
, &opts
->reporter_name
);
1317 o_found
|= DL_OPT_HEALTH_REPORTER_NAME
;
1318 } else if (dl_argv_match(dl
, "grace_period") &&
1319 (o_all
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)) {
1321 err
= dl_argv_uint64_t(dl
,
1322 &opts
->reporter_graceful_period
);
1325 o_found
|= DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
;
1326 } else if (dl_argv_match(dl
, "auto_recover") &&
1327 (o_all
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)) {
1329 err
= dl_argv_bool(dl
, &opts
->reporter_auto_recover
);
1332 o_found
|= DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
;
1334 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
1339 opts
->present
= o_found
;
1341 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
1343 opts
->present
|= DL_OPT_SB
;
1346 return dl_args_finding_required_validate(o_required
, o_found
);
1349 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1351 struct dl_opts
*opts
= &dl
->opts
;
1353 if (opts
->present
& DL_OPT_HANDLE
) {
1354 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1355 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1356 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1357 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1358 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1359 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1361 } else if (opts
->present
& DL_OPT_HANDLE_REGION
) {
1362 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1363 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1364 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_REGION_NAME
,
1367 if (opts
->present
& DL_OPT_PORT_TYPE
)
1368 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1370 if (opts
->present
& DL_OPT_PORT_COUNT
)
1371 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1373 if (opts
->present
& DL_OPT_SB
)
1374 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1376 if (opts
->present
& DL_OPT_SB_POOL
)
1377 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1378 opts
->sb_pool_index
);
1379 if (opts
->present
& DL_OPT_SB_SIZE
)
1380 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1381 opts
->sb_pool_size
);
1382 if (opts
->present
& DL_OPT_SB_TYPE
)
1383 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1384 opts
->sb_pool_type
);
1385 if (opts
->present
& DL_OPT_SB_THTYPE
)
1386 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1387 opts
->sb_pool_thtype
);
1388 if (opts
->present
& DL_OPT_SB_TH
)
1389 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1390 opts
->sb_threshold
);
1391 if (opts
->present
& DL_OPT_SB_TC
)
1392 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1394 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1395 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1396 opts
->eswitch_mode
);
1397 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1398 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1399 opts
->eswitch_inline_mode
);
1400 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1401 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1402 opts
->dpipe_table_name
);
1403 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1404 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1405 opts
->dpipe_counters_enable
);
1406 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1407 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1408 opts
->eswitch_encap_mode
);
1409 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1410 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1412 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1413 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1414 opts
->resource_size
);
1415 if (opts
->present
& DL_OPT_PARAM_NAME
)
1416 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_NAME
,
1418 if (opts
->present
& DL_OPT_PARAM_CMODE
)
1419 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_CMODE
,
1421 if (opts
->present
& DL_OPT_REGION_SNAPSHOT_ID
)
1422 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
1423 opts
->region_snapshot_id
);
1424 if (opts
->present
& DL_OPT_REGION_ADDRESS
)
1425 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_ADDR
,
1426 opts
->region_address
);
1427 if (opts
->present
& DL_OPT_REGION_LENGTH
)
1428 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_REGION_CHUNK_LEN
,
1429 opts
->region_length
);
1430 if (opts
->present
& DL_OPT_FLASH_FILE_NAME
)
1431 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
,
1432 opts
->flash_file_name
);
1433 if (opts
->present
& DL_OPT_FLASH_COMPONENT
)
1434 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
1435 opts
->flash_component
);
1436 if (opts
->present
& DL_OPT_HEALTH_REPORTER_NAME
)
1437 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
1438 opts
->reporter_name
);
1439 if (opts
->present
& DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
)
1440 mnl_attr_put_u64(nlh
,
1441 DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
1442 opts
->reporter_graceful_period
);
1443 if (opts
->present
& DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
)
1444 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
1445 opts
->reporter_auto_recover
);
1449 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1450 uint32_t o_required
, uint32_t o_optional
)
1454 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1457 dl_opts_put(nlh
, dl
);
1461 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1463 struct dl_opts
*opts
= &dl
->opts
;
1464 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1465 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1466 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1467 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1469 if (opts
->present
& DL_OPT_HANDLE
&&
1470 attr_bus_name
&& attr_dev_name
) {
1471 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1472 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1474 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1475 strcmp(dev_name
, opts
->dev_name
) != 0)
1478 if (opts
->present
& DL_OPT_HANDLEP
&&
1479 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1480 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1481 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1482 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1484 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1485 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1486 port_index
!= opts
->port_index
)
1489 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1490 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1492 if (sb_index
!= opts
->sb_index
)
1498 static void cmd_dev_help(void)
1500 pr_err("Usage: devlink dev show [ DEV ]\n");
1501 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1502 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1503 pr_err(" [ encap { disable | enable } ]\n");
1504 pr_err(" devlink dev eswitch show DEV\n");
1505 pr_err(" devlink dev param set DEV name PARAMETER value VALUE cmode { permanent | driverinit | runtime }\n");
1506 pr_err(" devlink dev param show [DEV name PARAMETER]\n");
1507 pr_err(" devlink dev reload DEV\n");
1508 pr_err(" devlink dev info [ DEV ]\n");
1509 pr_err(" devlink dev flash DEV file PATH [ component NAME ]\n");
1512 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1513 const char *dev_name
)
1515 if (!dl
->arr_last
.present
)
1517 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1518 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1521 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1522 const char *dev_name
)
1524 dl
->arr_last
.present
= true;
1525 free(dl
->arr_last
.dev_name
);
1526 free(dl
->arr_last
.bus_name
);
1527 dl
->arr_last
.bus_name
= strdup(bus_name
);
1528 dl
->arr_last
.dev_name
= strdup(dev_name
);
1531 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1532 const char *dev_name
)
1534 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1537 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1538 const char *dev_name
)
1540 return dl
->arr_last
.present
&&
1541 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1544 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1545 bool content
, bool array
)
1547 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1548 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1551 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1553 if (dl
->json_output
) {
1555 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1556 jsonw_end_array(dl
->jw
);
1557 if (should_arr_last_handle_start(dl
, bus_name
,
1559 jsonw_name(dl
->jw
, buf
);
1560 jsonw_start_array(dl
->jw
);
1561 jsonw_start_object(dl
->jw
);
1562 arr_last_handle_set(dl
, bus_name
, dev_name
);
1564 jsonw_start_object(dl
->jw
);
1567 jsonw_name(dl
->jw
, buf
);
1568 jsonw_start_object(dl
->jw
);
1572 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1573 __pr_out_indent_dec();
1574 if (should_arr_last_handle_start(dl
, bus_name
,
1576 pr_out("%s%s", buf
, content
? ":" : "");
1578 __pr_out_indent_inc();
1579 arr_last_handle_set(dl
, bus_name
, dev_name
);
1582 pr_out("%s%s", buf
, content
? ":" : "");
1587 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1589 __pr_out_handle_start(dl
, tb
, true, true);
1592 static void pr_out_handle_end(struct dl
*dl
)
1594 if (dl
->json_output
)
1595 jsonw_end_object(dl
->jw
);
1600 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1602 __pr_out_handle_start(dl
, tb
, false, false);
1603 pr_out_handle_end(dl
);
1606 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1607 const char *dev_name
, uint32_t port_index
)
1609 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1610 dl
->arr_last
.port_index
== port_index
;
1613 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1614 const char *dev_name
, uint32_t port_index
)
1616 arr_last_handle_set(dl
, bus_name
, dev_name
);
1617 dl
->arr_last
.port_index
= port_index
;
1620 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1621 const char *bus_name
,
1622 const char *dev_name
,
1623 uint32_t port_index
)
1625 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1628 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1629 const char *bus_name
,
1630 const char *dev_name
,
1631 uint32_t port_index
)
1633 return dl
->arr_last
.present
&&
1634 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1637 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1638 const char *dev_name
,
1639 uint32_t port_index
, bool try_nice
,
1642 static char buf
[64];
1643 char *ifname
= NULL
;
1645 if (dl
->no_nice_names
|| !try_nice
||
1646 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1647 port_index
, &ifname
) != 0)
1648 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1650 sprintf(buf
, "%s", ifname
);
1652 if (dl
->json_output
) {
1654 if (should_arr_last_port_handle_end(dl
, bus_name
,
1657 jsonw_end_array(dl
->jw
);
1658 if (should_arr_last_port_handle_start(dl
, bus_name
,
1661 jsonw_name(dl
->jw
, buf
);
1662 jsonw_start_array(dl
->jw
);
1663 jsonw_start_object(dl
->jw
);
1664 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1667 jsonw_start_object(dl
->jw
);
1670 jsonw_name(dl
->jw
, buf
);
1671 jsonw_start_object(dl
->jw
);
1678 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1680 const char *bus_name
;
1681 const char *dev_name
;
1682 uint32_t port_index
;
1684 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1685 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1686 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1687 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1690 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1692 const char *bus_name
;
1693 const char *dev_name
;
1694 uint32_t port_index
;
1696 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1697 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1698 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1699 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1702 static void pr_out_port_handle_end(struct dl
*dl
)
1704 if (dl
->json_output
)
1705 jsonw_end_object(dl
->jw
);
1711 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1713 if (dl
->json_output
) {
1714 jsonw_string_field(dl
->jw
, name
, val
);
1716 if (g_indent_newline
)
1717 pr_out("%s %s", name
, val
);
1719 pr_out(" %s %s", name
, val
);
1723 static void pr_out_bool(struct dl
*dl
, const char *name
, bool val
)
1725 if (dl
->json_output
)
1726 jsonw_bool_field(dl
->jw
, name
, val
);
1728 pr_out_str(dl
, name
, val
? "true" : "false");
1731 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1733 if (dl
->json_output
) {
1734 jsonw_uint_field(dl
->jw
, name
, val
);
1736 if (g_indent_newline
)
1737 pr_out("%s %u", name
, val
);
1739 pr_out(" %s %u", name
, val
);
1743 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1745 if (val
== (uint64_t) -1)
1746 return pr_out_str(dl
, name
, "unlimited");
1748 if (dl
->json_output
) {
1749 jsonw_u64_field(dl
->jw
, name
, val
);
1751 if (g_indent_newline
)
1752 pr_out("%s %"PRIu64
, name
, val
);
1754 pr_out(" %s %"PRIu64
, name
, val
);
1758 static void pr_out_bool_value(struct dl
*dl
, bool value
)
1760 if (dl
->json_output
)
1761 jsonw_bool(dl
->jw
, value
);
1763 pr_out(" %s", value
? "true" : "false");
1766 static void pr_out_uint_value(struct dl
*dl
, unsigned int value
)
1768 if (dl
->json_output
)
1769 jsonw_uint(dl
->jw
, value
);
1771 pr_out(" %u", value
);
1774 static void pr_out_uint64_value(struct dl
*dl
, uint64_t value
)
1776 if (dl
->json_output
)
1777 jsonw_u64(dl
->jw
, value
);
1779 pr_out(" %"PRIu64
, value
);
1782 static void pr_out_binary_value(struct dl
*dl
, uint8_t *data
, uint32_t len
)
1786 if (dl
->json_output
)
1787 jsonw_start_array(dl
->jw
);
1792 if (dl
->json_output
) {
1793 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1795 pr_out(" %02x", data
[i
]);
1801 if (dl
->json_output
)
1802 jsonw_end_array(dl
->jw
);
1803 else if ((i
- 1) % 16)
1807 static void pr_out_str_value(struct dl
*dl
, const char *value
)
1809 if (dl
->json_output
)
1810 jsonw_string(dl
->jw
, value
);
1812 pr_out(" %s", value
);
1815 static void pr_out_name(struct dl
*dl
, const char *name
)
1817 if (dl
->json_output
)
1818 jsonw_name(dl
->jw
, name
);
1820 pr_out(" %s:", name
);
1823 static void pr_out_region_chunk_start(struct dl
*dl
, uint64_t addr
)
1825 if (dl
->json_output
) {
1826 jsonw_name(dl
->jw
, "address");
1827 jsonw_uint(dl
->jw
, addr
);
1828 jsonw_name(dl
->jw
, "data");
1829 jsonw_start_array(dl
->jw
);
1833 static void pr_out_region_chunk_end(struct dl
*dl
)
1835 if (dl
->json_output
)
1836 jsonw_end_array(dl
->jw
);
1839 static void pr_out_region_chunk(struct dl
*dl
, uint8_t *data
, uint32_t len
,
1842 static uint64_t align_val
;
1845 pr_out_region_chunk_start(dl
, addr
);
1847 if (!dl
->json_output
)
1848 if (!(align_val
% 16))
1849 pr_out("%s%016"PRIx64
" ",
1850 align_val
? "\n" : "",
1855 if (dl
->json_output
)
1856 jsonw_printf(dl
->jw
, "%d", data
[i
]);
1858 pr_out("%02x ", data
[i
]);
1863 pr_out_region_chunk_end(dl
);
1866 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1868 pr_out_handle(dl
, tb
);
1871 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1873 if (dl
->json_output
) {
1874 jsonw_start_object(dl
->jw
);
1875 jsonw_name(dl
->jw
, name
);
1876 jsonw_start_object(dl
->jw
);
1880 static void pr_out_section_end(struct dl
*dl
)
1882 if (dl
->json_output
) {
1883 if (dl
->arr_last
.present
)
1884 jsonw_end_array(dl
->jw
);
1885 jsonw_end_object(dl
->jw
);
1886 jsonw_end_object(dl
->jw
);
1890 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1892 if (dl
->json_output
) {
1893 jsonw_name(dl
->jw
, name
);
1894 jsonw_start_array(dl
->jw
);
1896 __pr_out_indent_inc();
1898 pr_out("%s:", name
);
1899 __pr_out_indent_inc();
1904 static void pr_out_array_end(struct dl
*dl
)
1906 if (dl
->json_output
) {
1907 jsonw_end_array(dl
->jw
);
1909 __pr_out_indent_dec();
1910 __pr_out_indent_dec();
1914 static void pr_out_object_start(struct dl
*dl
, const char *name
)
1916 if (dl
->json_output
) {
1917 jsonw_name(dl
->jw
, name
);
1918 jsonw_start_object(dl
->jw
);
1920 __pr_out_indent_inc();
1922 pr_out("%s:", name
);
1923 __pr_out_indent_inc();
1928 static void pr_out_object_end(struct dl
*dl
)
1930 if (dl
->json_output
) {
1931 jsonw_end_object(dl
->jw
);
1933 __pr_out_indent_dec();
1934 __pr_out_indent_dec();
1938 static void pr_out_entry_start(struct dl
*dl
)
1940 if (dl
->json_output
)
1941 jsonw_start_object(dl
->jw
);
1944 static void pr_out_entry_end(struct dl
*dl
)
1946 if (dl
->json_output
)
1947 jsonw_end_object(dl
->jw
);
1952 static const char *param_cmode_name(uint8_t cmode
)
1955 case DEVLINK_PARAM_CMODE_RUNTIME
:
1956 return PARAM_CMODE_RUNTIME_STR
;
1957 case DEVLINK_PARAM_CMODE_DRIVERINIT
:
1958 return PARAM_CMODE_DRIVERINIT_STR
;
1959 case DEVLINK_PARAM_CMODE_PERMANENT
:
1960 return PARAM_CMODE_PERMANENT_STR
;
1961 default: return "<unknown type>";
1965 static const char *eswitch_mode_name(uint32_t mode
)
1968 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1969 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1970 default: return "<unknown mode>";
1974 static const char *eswitch_inline_mode_name(uint32_t mode
)
1977 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1978 return ESWITCH_INLINE_MODE_NONE
;
1979 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1980 return ESWITCH_INLINE_MODE_LINK
;
1981 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1982 return ESWITCH_INLINE_MODE_NETWORK
;
1983 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1984 return ESWITCH_INLINE_MODE_TRANSPORT
;
1986 return "<unknown mode>";
1990 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1992 __pr_out_handle_start(dl
, tb
, true, false);
1994 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1995 pr_out_str(dl
, "mode",
1996 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1998 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1999 pr_out_str(dl
, "inline-mode",
2000 eswitch_inline_mode_name(mnl_attr_get_u8(
2001 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
2003 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2004 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2006 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
2009 pr_out_handle_end(dl
);
2012 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2014 struct dl
*dl
= data
;
2015 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2016 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2018 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2019 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2020 return MNL_CB_ERROR
;
2021 pr_out_eswitch(dl
, tb
);
2025 static int cmd_dev_eswitch_show(struct dl
*dl
)
2027 struct nlmsghdr
*nlh
;
2030 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
2031 NLM_F_REQUEST
| NLM_F_ACK
);
2033 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2037 pr_out_section_start(dl
, "dev");
2038 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
2039 pr_out_section_end(dl
);
2043 static int cmd_dev_eswitch_set(struct dl
*dl
)
2045 struct nlmsghdr
*nlh
;
2048 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
2049 NLM_F_REQUEST
| NLM_F_ACK
);
2051 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
2052 DL_OPT_ESWITCH_MODE
|
2053 DL_OPT_ESWITCH_INLINE_MODE
|
2054 DL_OPT_ESWITCH_ENCAP_MODE
);
2059 if (dl
->opts
.present
== 1) {
2060 pr_err("Need to set at least one option\n");
2064 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2067 static int cmd_dev_eswitch(struct dl
*dl
)
2069 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2072 } else if (dl_argv_match(dl
, "set")) {
2074 return cmd_dev_eswitch_set(dl
);
2075 } else if (dl_argv_match(dl
, "show")) {
2077 return cmd_dev_eswitch_show(dl
);
2079 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2083 struct param_val_conv
{
2089 static bool param_val_conv_exists(const struct param_val_conv
*param_val_conv
,
2090 uint32_t len
, const char *name
)
2094 for (i
= 0; i
< len
; i
++)
2095 if (!strcmp(param_val_conv
[i
].name
, name
))
2102 param_val_conv_uint_get(const struct param_val_conv
*param_val_conv
,
2103 uint32_t len
, const char *name
, const char *vstr
,
2108 for (i
= 0; i
< len
; i
++)
2109 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2110 !strcmp(param_val_conv
[i
].vstr
, vstr
)) {
2111 *vuint
= param_val_conv
[i
].vuint
;
2119 param_val_conv_str_get(const struct param_val_conv
*param_val_conv
,
2120 uint32_t len
, const char *name
, uint32_t vuint
,
2125 for (i
= 0; i
< len
; i
++)
2126 if (!strcmp(param_val_conv
[i
].name
, name
) &&
2127 param_val_conv
[i
].vuint
== vuint
) {
2128 *vstr
= param_val_conv
[i
].vstr
;
2135 static const struct param_val_conv param_val_conv
[] = {
2137 .name
= "fw_load_policy",
2139 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_DRIVER
,
2142 .name
= "fw_load_policy",
2144 .vuint
= DEVLINK_PARAM_FW_LOAD_POLICY_VALUE_FLASH
,
2148 #define PARAM_VAL_CONV_LEN ARRAY_SIZE(param_val_conv)
2150 static void pr_out_param_value(struct dl
*dl
, const char *nla_name
,
2151 int nla_type
, struct nlattr
*nl
)
2153 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2154 struct nlattr
*val_attr
;
2159 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_value
);
2160 if (err
!= MNL_CB_OK
)
2163 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2164 (nla_type
!= MNL_TYPE_FLAG
&&
2165 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2168 pr_out_str(dl
, "cmode",
2169 param_cmode_name(mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])));
2170 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2172 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2178 err
= param_val_conv_str_get(param_val_conv
,
2181 mnl_attr_get_u8(val_attr
),
2185 pr_out_str(dl
, "value", vstr
);
2187 pr_out_uint(dl
, "value", mnl_attr_get_u8(val_attr
));
2192 err
= param_val_conv_str_get(param_val_conv
,
2195 mnl_attr_get_u16(val_attr
),
2199 pr_out_str(dl
, "value", vstr
);
2201 pr_out_uint(dl
, "value", mnl_attr_get_u16(val_attr
));
2206 err
= param_val_conv_str_get(param_val_conv
,
2209 mnl_attr_get_u32(val_attr
),
2213 pr_out_str(dl
, "value", vstr
);
2215 pr_out_uint(dl
, "value", mnl_attr_get_u32(val_attr
));
2218 case MNL_TYPE_STRING
:
2219 pr_out_str(dl
, "value", mnl_attr_get_str(val_attr
));
2222 pr_out_bool(dl
, "value", val_attr
? true : false);
2227 static void pr_out_param(struct dl
*dl
, struct nlattr
**tb
, bool array
)
2229 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2230 struct nlattr
*param_value_attr
;
2231 const char *nla_name
;
2235 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2236 if (err
!= MNL_CB_OK
)
2238 if (!nla_param
[DEVLINK_ATTR_PARAM_NAME
] ||
2239 !nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2240 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2244 pr_out_handle_start_arr(dl
, tb
);
2246 __pr_out_handle_start(dl
, tb
, true, false);
2248 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2250 nla_name
= mnl_attr_get_str(nla_param
[DEVLINK_ATTR_PARAM_NAME
]);
2251 pr_out_str(dl
, "name", nla_name
);
2253 if (!nla_param
[DEVLINK_ATTR_PARAM_GENERIC
])
2254 pr_out_str(dl
, "type", "driver-specific");
2256 pr_out_str(dl
, "type", "generic");
2258 pr_out_array_start(dl
, "values");
2259 mnl_attr_for_each_nested(param_value_attr
,
2260 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2261 pr_out_entry_start(dl
);
2262 pr_out_param_value(dl
, nla_name
, nla_type
, param_value_attr
);
2263 pr_out_entry_end(dl
);
2265 pr_out_array_end(dl
);
2266 pr_out_handle_end(dl
);
2269 static int cmd_dev_param_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2271 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2272 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2273 struct dl
*dl
= data
;
2275 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2276 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2277 !tb
[DEVLINK_ATTR_PARAM
])
2278 return MNL_CB_ERROR
;
2279 pr_out_param(dl
, tb
, true);
2295 static int cmd_dev_param_set_cb(const struct nlmsghdr
*nlh
, void *data
)
2297 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2298 struct nlattr
*nla_param
[DEVLINK_ATTR_MAX
+ 1] = {};
2299 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2300 struct nlattr
*param_value_attr
;
2301 enum devlink_param_cmode cmode
;
2302 struct param_ctx
*ctx
= data
;
2303 struct dl
*dl
= ctx
->dl
;
2307 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2308 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2309 !tb
[DEVLINK_ATTR_PARAM
])
2310 return MNL_CB_ERROR
;
2312 err
= mnl_attr_parse_nested(tb
[DEVLINK_ATTR_PARAM
], attr_cb
, nla_param
);
2313 if (err
!= MNL_CB_OK
)
2314 return MNL_CB_ERROR
;
2316 if (!nla_param
[DEVLINK_ATTR_PARAM_TYPE
] ||
2317 !nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
])
2318 return MNL_CB_ERROR
;
2320 nla_type
= mnl_attr_get_u8(nla_param
[DEVLINK_ATTR_PARAM_TYPE
]);
2321 mnl_attr_for_each_nested(param_value_attr
,
2322 nla_param
[DEVLINK_ATTR_PARAM_VALUES_LIST
]) {
2323 struct nlattr
*nla_value
[DEVLINK_ATTR_MAX
+ 1] = {};
2324 struct nlattr
*val_attr
;
2326 err
= mnl_attr_parse_nested(param_value_attr
,
2327 attr_cb
, nla_value
);
2328 if (err
!= MNL_CB_OK
)
2329 return MNL_CB_ERROR
;
2331 if (!nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
] ||
2332 (nla_type
!= MNL_TYPE_FLAG
&&
2333 !nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
]))
2334 return MNL_CB_ERROR
;
2336 cmode
= mnl_attr_get_u8(nla_value
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
2337 if (cmode
== dl
->opts
.cmode
) {
2338 val_attr
= nla_value
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
2341 ctx
->value
.vu8
= mnl_attr_get_u8(val_attr
);
2344 ctx
->value
.vu16
= mnl_attr_get_u16(val_attr
);
2347 ctx
->value
.vu32
= mnl_attr_get_u32(val_attr
);
2349 case MNL_TYPE_STRING
:
2350 ctx
->value
.vstr
= mnl_attr_get_str(val_attr
);
2353 ctx
->value
.vbool
= val_attr
? true : false;
2359 ctx
->nla_type
= nla_type
;
2363 static int cmd_dev_param_set(struct dl
*dl
)
2365 struct param_ctx ctx
= {};
2366 struct nlmsghdr
*nlh
;
2374 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
|
2376 DL_OPT_PARAM_VALUE
|
2377 DL_OPT_PARAM_CMODE
, 0);
2381 /* Get value type */
2382 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
,
2383 NLM_F_REQUEST
| NLM_F_ACK
);
2384 dl_opts_put(nlh
, dl
);
2387 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_set_cb
, &ctx
);
2391 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_SET
,
2392 NLM_F_REQUEST
| NLM_F_ACK
);
2393 dl_opts_put(nlh
, dl
);
2395 conv_exists
= param_val_conv_exists(param_val_conv
, PARAM_VAL_CONV_LEN
,
2396 dl
->opts
.param_name
);
2398 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_TYPE
, ctx
.nla_type
);
2399 switch (ctx
.nla_type
) {
2402 err
= param_val_conv_uint_get(param_val_conv
,
2404 dl
->opts
.param_name
,
2405 dl
->opts
.param_value
,
2409 err
= strtouint8_t(dl
->opts
.param_value
, &val_u8
);
2412 goto err_param_value_parse
;
2413 if (val_u8
== ctx
.value
.vu8
)
2415 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u8
);
2419 err
= param_val_conv_uint_get(param_val_conv
,
2421 dl
->opts
.param_name
,
2422 dl
->opts
.param_value
,
2426 err
= strtouint16_t(dl
->opts
.param_value
, &val_u16
);
2429 goto err_param_value_parse
;
2430 if (val_u16
== ctx
.value
.vu16
)
2432 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u16
);
2436 err
= param_val_conv_uint_get(param_val_conv
,
2438 dl
->opts
.param_name
,
2439 dl
->opts
.param_value
,
2442 err
= strtouint32_t(dl
->opts
.param_value
, &val_u32
);
2444 goto err_param_value_parse
;
2445 if (val_u32
== ctx
.value
.vu32
)
2447 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val_u32
);
2450 err
= strtobool(dl
->opts
.param_value
, &val_bool
);
2452 goto err_param_value_parse
;
2453 if (val_bool
== ctx
.value
.vbool
)
2456 mnl_attr_put(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2459 case MNL_TYPE_STRING
:
2460 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
2461 dl
->opts
.param_value
);
2462 if (!strcmp(dl
->opts
.param_value
, ctx
.value
.vstr
))
2466 printf("Value type not supported\n");
2469 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2471 err_param_value_parse
:
2472 pr_err("Value \"%s\" is not a number or not within range\n",
2473 dl
->opts
.param_value
);
2477 static int cmd_dev_param_show(struct dl
*dl
)
2479 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2480 struct nlmsghdr
*nlh
;
2483 if (dl_argc(dl
) == 0)
2484 flags
|= NLM_F_DUMP
;
2486 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PARAM_GET
, flags
);
2488 if (dl_argc(dl
) > 0) {
2489 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
|
2490 DL_OPT_PARAM_NAME
, 0);
2495 pr_out_section_start(dl
, "param");
2496 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_param_show_cb
, dl
);
2497 pr_out_section_end(dl
);
2501 static int cmd_dev_param(struct dl
*dl
)
2503 if (dl_argv_match(dl
, "help")) {
2506 } else if (dl_argv_match(dl
, "show") ||
2507 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2509 return cmd_dev_param_show(dl
);
2510 } else if (dl_argv_match(dl
, "set")) {
2512 return cmd_dev_param_set(dl
);
2514 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2517 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2519 struct dl
*dl
= data
;
2520 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2521 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2523 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2524 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2525 return MNL_CB_ERROR
;
2530 static int cmd_dev_show(struct dl
*dl
)
2532 struct nlmsghdr
*nlh
;
2533 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2536 if (dl_argc(dl
) == 0)
2537 flags
|= NLM_F_DUMP
;
2539 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
2541 if (dl_argc(dl
) > 0) {
2542 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2547 pr_out_section_start(dl
, "dev");
2548 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
2549 pr_out_section_end(dl
);
2553 static void cmd_dev_reload_help(void)
2555 pr_err("Usage: devlink dev reload [ DEV ]\n");
2558 static int cmd_dev_reload(struct dl
*dl
)
2560 struct nlmsghdr
*nlh
;
2563 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2564 cmd_dev_reload_help();
2568 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
2569 NLM_F_REQUEST
| NLM_F_ACK
);
2571 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2575 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2578 static void pr_out_versions_single(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2579 const char *name
, int type
)
2581 struct nlattr
*version
;
2583 mnl_attr_for_each(version
, nlh
, sizeof(struct genlmsghdr
)) {
2584 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2585 const char *ver_value
;
2586 const char *ver_name
;
2589 if (mnl_attr_get_type(version
) != type
)
2592 err
= mnl_attr_parse_nested(version
, attr_cb
, tb
);
2593 if (err
!= MNL_CB_OK
)
2596 if (!tb
[DEVLINK_ATTR_INFO_VERSION_NAME
] ||
2597 !tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
])
2601 pr_out_object_start(dl
, name
);
2605 ver_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_NAME
]);
2606 ver_value
= mnl_attr_get_str(tb
[DEVLINK_ATTR_INFO_VERSION_VALUE
]);
2608 pr_out_str(dl
, ver_name
, ver_value
);
2609 if (!dl
->json_output
)
2614 pr_out_object_end(dl
);
2617 static void pr_out_info(struct dl
*dl
, const struct nlmsghdr
*nlh
,
2618 struct nlattr
**tb
, bool has_versions
)
2620 __pr_out_handle_start(dl
, tb
, true, false);
2622 __pr_out_indent_inc();
2623 if (tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
]) {
2624 struct nlattr
*nla_drv
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
];
2626 if (!dl
->json_output
)
2628 pr_out_str(dl
, "driver", mnl_attr_get_str(nla_drv
));
2631 if (tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
]) {
2632 struct nlattr
*nla_sn
= tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
];
2634 if (!dl
->json_output
)
2636 pr_out_str(dl
, "serial_number", mnl_attr_get_str(nla_sn
));
2638 __pr_out_indent_dec();
2641 pr_out_object_start(dl
, "versions");
2643 pr_out_versions_single(dl
, nlh
, "fixed",
2644 DEVLINK_ATTR_INFO_VERSION_FIXED
);
2645 pr_out_versions_single(dl
, nlh
, "running",
2646 DEVLINK_ATTR_INFO_VERSION_RUNNING
);
2647 pr_out_versions_single(dl
, nlh
, "stored",
2648 DEVLINK_ATTR_INFO_VERSION_STORED
);
2650 pr_out_object_end(dl
);
2653 pr_out_handle_end(dl
);
2656 static int cmd_versions_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2658 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2659 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2660 bool has_versions
, has_info
;
2661 struct dl
*dl
= data
;
2663 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2665 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2666 return MNL_CB_ERROR
;
2668 has_versions
= tb
[DEVLINK_ATTR_INFO_VERSION_FIXED
] ||
2669 tb
[DEVLINK_ATTR_INFO_VERSION_RUNNING
] ||
2670 tb
[DEVLINK_ATTR_INFO_VERSION_STORED
];
2671 has_info
= tb
[DEVLINK_ATTR_INFO_DRIVER_NAME
] ||
2672 tb
[DEVLINK_ATTR_INFO_SERIAL_NUMBER
] ||
2676 pr_out_info(dl
, nlh
, tb
, has_versions
);
2681 static void cmd_dev_info_help(void)
2683 pr_err("Usage: devlink dev info [ DEV ]\n");
2686 static int cmd_dev_info(struct dl
*dl
)
2688 struct nlmsghdr
*nlh
;
2689 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2692 if (dl_argv_match(dl
, "help")) {
2693 cmd_dev_info_help();
2697 if (dl_argc(dl
) == 0)
2698 flags
|= NLM_F_DUMP
;
2700 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_INFO_GET
, flags
);
2702 if (dl_argc(dl
) > 0) {
2703 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
2708 pr_out_section_start(dl
, "info");
2709 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_versions_show_cb
, dl
);
2710 pr_out_section_end(dl
);
2714 static void cmd_dev_flash_help(void)
2716 pr_err("Usage: devlink dev flash DEV file PATH [ component NAME ]\n");
2719 static int cmd_dev_flash(struct dl
*dl
)
2721 struct nlmsghdr
*nlh
;
2724 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2725 cmd_dev_flash_help();
2729 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_FLASH_UPDATE
,
2730 NLM_F_REQUEST
| NLM_F_ACK
);
2732 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_FLASH_FILE_NAME
,
2733 DL_OPT_FLASH_COMPONENT
);
2737 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2740 static int cmd_dev(struct dl
*dl
)
2742 if (dl_argv_match(dl
, "help")) {
2745 } else if (dl_argv_match(dl
, "show") ||
2746 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2748 return cmd_dev_show(dl
);
2749 } else if (dl_argv_match(dl
, "eswitch")) {
2751 return cmd_dev_eswitch(dl
);
2752 } else if (dl_argv_match(dl
, "reload")) {
2754 return cmd_dev_reload(dl
);
2755 } else if (dl_argv_match(dl
, "param")) {
2757 return cmd_dev_param(dl
);
2758 } else if (dl_argv_match(dl
, "info")) {
2760 return cmd_dev_info(dl
);
2761 } else if (dl_argv_match(dl
, "flash")) {
2763 return cmd_dev_flash(dl
);
2765 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2769 static void cmd_port_help(void)
2771 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
2772 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
2773 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
2774 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
2777 static const char *port_type_name(uint32_t type
)
2780 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
2781 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
2782 case DEVLINK_PORT_TYPE_ETH
: return "eth";
2783 case DEVLINK_PORT_TYPE_IB
: return "ib";
2784 default: return "<unknown type>";
2788 static const char *port_flavour_name(uint16_t flavour
)
2791 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
2793 case DEVLINK_PORT_FLAVOUR_CPU
:
2795 case DEVLINK_PORT_FLAVOUR_DSA
:
2798 return "<unknown flavour>";
2802 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
2804 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
2805 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
2807 pr_out_port_handle_start(dl
, tb
, false);
2809 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
2811 pr_out_str(dl
, "type", port_type_name(port_type
));
2813 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
2815 if (port_type
!= des_port_type
)
2816 pr_out_str(dl
, "des_type",
2817 port_type_name(des_port_type
));
2820 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
2821 pr_out_str(dl
, "netdev",
2822 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
2823 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
2824 pr_out_str(dl
, "ibdev",
2825 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
2826 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
2827 uint16_t port_flavour
=
2828 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
2830 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
2832 if (tb
[DEVLINK_ATTR_PORT_NUMBER
]) {
2833 uint32_t port_number
;
2835 port_number
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_NUMBER
]);
2836 pr_out_uint(dl
, "port", port_number
);
2838 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
2839 pr_out_uint(dl
, "split_group",
2840 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
2841 pr_out_port_handle_end(dl
);
2844 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2846 struct dl
*dl
= data
;
2847 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2848 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2850 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2851 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2852 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2853 return MNL_CB_ERROR
;
2854 pr_out_port(dl
, tb
);
2858 static int cmd_port_show(struct dl
*dl
)
2860 struct nlmsghdr
*nlh
;
2861 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2864 if (dl_argc(dl
) == 0)
2865 flags
|= NLM_F_DUMP
;
2867 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
2869 if (dl_argc(dl
) > 0) {
2870 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2875 pr_out_section_start(dl
, "port");
2876 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
2877 pr_out_section_end(dl
);
2881 static int cmd_port_set(struct dl
*dl
)
2883 struct nlmsghdr
*nlh
;
2886 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
2887 NLM_F_REQUEST
| NLM_F_ACK
);
2889 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
2893 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2896 static int cmd_port_split(struct dl
*dl
)
2898 struct nlmsghdr
*nlh
;
2901 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
2902 NLM_F_REQUEST
| NLM_F_ACK
);
2904 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
2908 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2911 static int cmd_port_unsplit(struct dl
*dl
)
2913 struct nlmsghdr
*nlh
;
2916 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
2917 NLM_F_REQUEST
| NLM_F_ACK
);
2919 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
2923 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2926 static int cmd_port(struct dl
*dl
)
2928 if (dl_argv_match(dl
, "help")) {
2931 } else if (dl_argv_match(dl
, "show") ||
2932 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2934 return cmd_port_show(dl
);
2935 } else if (dl_argv_match(dl
, "set")) {
2937 return cmd_port_set(dl
);
2938 } else if (dl_argv_match(dl
, "split")) {
2940 return cmd_port_split(dl
);
2941 } else if (dl_argv_match(dl
, "unsplit")) {
2943 return cmd_port_unsplit(dl
);
2945 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2949 static void cmd_sb_help(void)
2951 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
2952 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
2953 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
2954 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
2955 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2956 pr_err(" pool POOL_INDEX ]\n");
2957 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
2958 pr_err(" pool POOL_INDEX th THRESHOLD\n");
2959 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2960 pr_err(" type { ingress | egress } ]\n");
2961 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
2962 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
2963 pr_err(" th THRESHOLD\n");
2964 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
2965 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
2966 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
2969 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
2971 pr_out_handle_start_arr(dl
, tb
);
2972 pr_out_uint(dl
, "sb",
2973 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2974 pr_out_uint(dl
, "size",
2975 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
2976 pr_out_uint(dl
, "ing_pools",
2977 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
2978 pr_out_uint(dl
, "eg_pools",
2979 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
2980 pr_out_uint(dl
, "ing_tcs",
2981 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
2982 pr_out_uint(dl
, "eg_tcs",
2983 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
2984 pr_out_handle_end(dl
);
2987 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2989 struct dl
*dl
= data
;
2990 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2991 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2993 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2994 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2995 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
2996 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
2997 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
2998 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
2999 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
3000 return MNL_CB_ERROR
;
3005 static int cmd_sb_show(struct dl
*dl
)
3007 struct nlmsghdr
*nlh
;
3008 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3011 if (dl_argc(dl
) == 0)
3012 flags
|= NLM_F_DUMP
;
3014 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
3016 if (dl_argc(dl
) > 0) {
3017 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3022 pr_out_section_start(dl
, "sb");
3023 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
3024 pr_out_section_end(dl
);
3028 static const char *pool_type_name(uint8_t type
)
3031 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
3032 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
3033 default: return "<unknown type>";
3037 static const char *threshold_type_name(uint8_t type
)
3040 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
3041 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
3042 default: return "<unknown type>";
3046 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
3048 pr_out_handle_start_arr(dl
, tb
);
3049 pr_out_uint(dl
, "sb",
3050 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3051 pr_out_uint(dl
, "pool",
3052 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3053 pr_out_str(dl
, "type",
3054 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3055 pr_out_uint(dl
, "size",
3056 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
3057 pr_out_str(dl
, "thtype",
3058 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
3059 if (tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
])
3060 pr_out_uint(dl
, "cell_size",
3061 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_CELL_SIZE
]));
3062 pr_out_handle_end(dl
);
3065 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3067 struct dl
*dl
= data
;
3068 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3069 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3071 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3072 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3073 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3074 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
3075 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
3076 return MNL_CB_ERROR
;
3077 pr_out_sb_pool(dl
, tb
);
3081 static int cmd_sb_pool_show(struct dl
*dl
)
3083 struct nlmsghdr
*nlh
;
3084 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3087 if (dl_argc(dl
) == 0)
3088 flags
|= NLM_F_DUMP
;
3090 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
3092 if (dl_argc(dl
) > 0) {
3093 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
3099 pr_out_section_start(dl
, "pool");
3100 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
3101 pr_out_section_end(dl
);
3105 static int cmd_sb_pool_set(struct dl
*dl
)
3107 struct nlmsghdr
*nlh
;
3110 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
3111 NLM_F_REQUEST
| NLM_F_ACK
);
3113 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
3114 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
3118 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3121 static int cmd_sb_pool(struct dl
*dl
)
3123 if (dl_argv_match(dl
, "help")) {
3126 } else if (dl_argv_match(dl
, "show") ||
3127 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3129 return cmd_sb_pool_show(dl
);
3130 } else if (dl_argv_match(dl
, "set")) {
3132 return cmd_sb_pool_set(dl
);
3134 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3138 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
3140 pr_out_port_handle_start_arr(dl
, tb
, true);
3141 pr_out_uint(dl
, "sb",
3142 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3143 pr_out_uint(dl
, "pool",
3144 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3145 pr_out_uint(dl
, "threshold",
3146 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3147 pr_out_port_handle_end(dl
);
3150 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3152 struct dl
*dl
= data
;
3153 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3154 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3156 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3157 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3158 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3159 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3160 return MNL_CB_ERROR
;
3161 pr_out_sb_port_pool(dl
, tb
);
3165 static int cmd_sb_port_pool_show(struct dl
*dl
)
3167 struct nlmsghdr
*nlh
;
3168 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3171 if (dl_argc(dl
) == 0)
3172 flags
|= NLM_F_DUMP
;
3174 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3176 if (dl_argc(dl
) > 0) {
3177 err
= dl_argv_parse_put(nlh
, dl
,
3178 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
3184 pr_out_section_start(dl
, "port_pool");
3185 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
3186 pr_out_section_end(dl
);
3190 static int cmd_sb_port_pool_set(struct dl
*dl
)
3192 struct nlmsghdr
*nlh
;
3195 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
3196 NLM_F_REQUEST
| NLM_F_ACK
);
3198 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
3199 DL_OPT_SB_TH
, DL_OPT_SB
);
3203 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3206 static int cmd_sb_port_pool(struct dl
*dl
)
3208 if (dl_argv_match(dl
, "help")) {
3211 } else if (dl_argv_match(dl
, "show") ||
3212 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3214 return cmd_sb_port_pool_show(dl
);
3215 } else if (dl_argv_match(dl
, "set")) {
3217 return cmd_sb_port_pool_set(dl
);
3219 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3223 static int cmd_sb_port(struct dl
*dl
)
3225 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3228 } else if (dl_argv_match(dl
, "pool")) {
3230 return cmd_sb_port_pool(dl
);
3232 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3236 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
3238 pr_out_port_handle_start_arr(dl
, tb
, true);
3239 pr_out_uint(dl
, "sb",
3240 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
3241 pr_out_uint(dl
, "tc",
3242 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
3243 pr_out_str(dl
, "type",
3244 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
3245 pr_out_uint(dl
, "pool",
3246 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
3247 pr_out_uint(dl
, "threshold",
3248 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
3249 pr_out_port_handle_end(dl
);
3252 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3254 struct dl
*dl
= data
;
3255 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3256 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3258 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3259 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3260 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3261 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3262 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
3263 return MNL_CB_ERROR
;
3264 pr_out_sb_tc_bind(dl
, tb
);
3268 static int cmd_sb_tc_bind_show(struct dl
*dl
)
3270 struct nlmsghdr
*nlh
;
3271 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3274 if (dl_argc(dl
) == 0)
3275 flags
|= NLM_F_DUMP
;
3277 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3279 if (dl_argc(dl
) > 0) {
3280 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3281 DL_OPT_SB_TYPE
, DL_OPT_SB
);
3286 pr_out_section_start(dl
, "tc_bind");
3287 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
3288 pr_out_section_end(dl
);
3292 static int cmd_sb_tc_bind_set(struct dl
*dl
)
3294 struct nlmsghdr
*nlh
;
3297 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
3298 NLM_F_REQUEST
| NLM_F_ACK
);
3300 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
3301 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
3306 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3309 static int cmd_sb_tc_bind(struct dl
*dl
)
3311 if (dl_argv_match(dl
, "help")) {
3314 } else if (dl_argv_match(dl
, "show") ||
3315 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3317 return cmd_sb_tc_bind_show(dl
);
3318 } else if (dl_argv_match(dl
, "set")) {
3320 return cmd_sb_tc_bind_set(dl
);
3322 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3326 static int cmd_sb_tc(struct dl
*dl
)
3328 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3331 } else if (dl_argv_match(dl
, "bind")) {
3333 return cmd_sb_tc_bind(dl
);
3335 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3340 struct list_head list
;
3344 uint32_t bound_pool_index
;
3348 struct list_head list
;
3351 uint32_t port_index
;
3353 struct list_head pool_list
;
3354 struct list_head ing_tc_list
;
3355 struct list_head eg_tc_list
;
3361 struct list_head port_list
;
3364 static struct occ_item
*occ_item_alloc(void)
3366 return calloc(1, sizeof(struct occ_item
));
3369 static void occ_item_free(struct occ_item
*occ_item
)
3374 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
3376 struct occ_port
*occ_port
;
3378 occ_port
= calloc(1, sizeof(*occ_port
));
3381 occ_port
->port_index
= port_index
;
3382 INIT_LIST_HEAD(&occ_port
->pool_list
);
3383 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
3384 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
3388 static void occ_port_free(struct occ_port
*occ_port
)
3390 struct occ_item
*occ_item
, *tmp
;
3392 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
3393 occ_item_free(occ_item
);
3394 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
3395 occ_item_free(occ_item
);
3396 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
3397 occ_item_free(occ_item
);
3400 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
3402 struct occ_show
*occ_show
;
3404 occ_show
= calloc(1, sizeof(*occ_show
));
3408 INIT_LIST_HEAD(&occ_show
->port_list
);
3412 static void occ_show_free(struct occ_show
*occ_show
)
3414 struct occ_port
*occ_port
, *tmp
;
3416 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
3417 occ_port_free(occ_port
);
3420 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
3423 struct occ_port
*occ_port
;
3424 uint32_t port_index
;
3426 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
3428 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
3429 if (occ_port
->port_index
== port_index
)
3432 occ_port
= occ_port_alloc(port_index
);
3435 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
3439 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
3442 struct occ_item
*occ_item
;
3445 pr_out_sp(7, " %s:", label
);
3446 list_for_each_entry(occ_item
, list
, list
) {
3447 if ((i
- 1) % 4 == 0 && i
!= 1)
3450 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
3451 occ_item
->bound_pool_index
);
3453 pr_out_sp(7, "%2u:", occ_item
->index
);
3454 pr_out_sp(21, "%10u/%u", occ_item
->cur
, occ_item
->max
);
3458 if ((i
- 1) % 4 != 0)
3462 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
3463 struct list_head
*list
,
3466 struct occ_item
*occ_item
;
3469 jsonw_name(dl
->jw
, label
);
3470 jsonw_start_object(dl
->jw
);
3471 list_for_each_entry(occ_item
, list
, list
) {
3472 sprintf(buf
, "%u", occ_item
->index
);
3473 jsonw_name(dl
->jw
, buf
);
3474 jsonw_start_object(dl
->jw
);
3476 jsonw_uint_field(dl
->jw
, "bound_pool",
3477 occ_item
->bound_pool_index
);
3478 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
3479 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
3480 jsonw_end_object(dl
->jw
);
3482 jsonw_end_object(dl
->jw
);
3485 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
3487 if (dl
->json_output
) {
3488 pr_out_json_occ_show_item_list(dl
, "pool",
3489 &occ_port
->pool_list
, false);
3490 pr_out_json_occ_show_item_list(dl
, "itc",
3491 &occ_port
->ing_tc_list
, true);
3492 pr_out_json_occ_show_item_list(dl
, "etc",
3493 &occ_port
->eg_tc_list
, true);
3496 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
3497 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
3498 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
3502 static void pr_out_occ_show(struct occ_show
*occ_show
)
3504 struct dl
*dl
= occ_show
->dl
;
3505 struct dl_opts
*opts
= &dl
->opts
;
3506 struct occ_port
*occ_port
;
3508 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
3509 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
3510 occ_port
->port_index
, true, false);
3511 pr_out_occ_show_port(dl
, occ_port
);
3512 pr_out_port_handle_end(dl
);
3516 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
3519 struct occ_port
*occ_port
;
3520 struct occ_item
*occ_item
;
3522 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3525 occ_port
= occ_port_get(occ_show
, tb
);
3527 occ_show
->err
= -ENOMEM
;
3531 occ_item
= occ_item_alloc();
3533 occ_show
->err
= -ENOMEM
;
3536 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3537 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3538 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3539 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
3542 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3544 struct occ_show
*occ_show
= data
;
3545 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3546 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3548 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3549 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3550 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3551 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3552 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3553 return MNL_CB_ERROR
;
3554 cmd_sb_occ_port_pool_process(occ_show
, tb
);
3558 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
3561 struct occ_port
*occ_port
;
3562 struct occ_item
*occ_item
;
3565 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
3568 occ_port
= occ_port_get(occ_show
, tb
);
3570 occ_show
->err
= -ENOMEM
;
3574 occ_item
= occ_item_alloc();
3576 occ_show
->err
= -ENOMEM
;
3579 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
3580 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
3581 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
3582 occ_item
->bound_pool_index
=
3583 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
3584 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
3585 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
3586 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
3587 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
3588 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
3590 occ_item_free(occ_item
);
3593 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
3595 struct occ_show
*occ_show
= data
;
3596 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3597 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3599 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3600 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3601 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
3602 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
3603 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
3604 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
3605 return MNL_CB_ERROR
;
3606 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
3610 static int cmd_sb_occ_show(struct dl
*dl
)
3612 struct nlmsghdr
*nlh
;
3613 struct occ_show
*occ_show
;
3614 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
3617 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
3621 occ_show
= occ_show_alloc(dl
);
3625 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
3627 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3628 cmd_sb_occ_port_pool_process_cb
, occ_show
);
3632 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
3634 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
3635 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
3639 pr_out_section_start(dl
, "occupancy");
3640 pr_out_occ_show(occ_show
);
3641 pr_out_section_end(dl
);
3644 occ_show_free(occ_show
);
3648 static int cmd_sb_occ_snapshot(struct dl
*dl
)
3650 struct nlmsghdr
*nlh
;
3653 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
3654 NLM_F_REQUEST
| NLM_F_ACK
);
3656 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3660 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3663 static int cmd_sb_occ_clearmax(struct dl
*dl
)
3665 struct nlmsghdr
*nlh
;
3668 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
3669 NLM_F_REQUEST
| NLM_F_ACK
);
3671 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
3675 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3678 static int cmd_sb_occ(struct dl
*dl
)
3680 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3683 } else if (dl_argv_match(dl
, "show") ||
3684 dl_argv_match(dl
, "list")) {
3686 return cmd_sb_occ_show(dl
);
3687 } else if (dl_argv_match(dl
, "snapshot")) {
3689 return cmd_sb_occ_snapshot(dl
);
3690 } else if (dl_argv_match(dl
, "clearmax")) {
3692 return cmd_sb_occ_clearmax(dl
);
3694 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3698 static int cmd_sb(struct dl
*dl
)
3700 if (dl_argv_match(dl
, "help")) {
3703 } else if (dl_argv_match(dl
, "show") ||
3704 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
3706 return cmd_sb_show(dl
);
3707 } else if (dl_argv_match(dl
, "pool")) {
3709 return cmd_sb_pool(dl
);
3710 } else if (dl_argv_match(dl
, "port")) {
3712 return cmd_sb_port(dl
);
3713 } else if (dl_argv_match(dl
, "tc")) {
3715 return cmd_sb_tc(dl
);
3716 } else if (dl_argv_match(dl
, "occupancy")) {
3718 return cmd_sb_occ(dl
);
3720 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3724 static const char *cmd_name(uint8_t cmd
)
3727 case DEVLINK_CMD_UNSPEC
: return "unspec";
3728 case DEVLINK_CMD_GET
: return "get";
3729 case DEVLINK_CMD_SET
: return "set";
3730 case DEVLINK_CMD_NEW
: return "new";
3731 case DEVLINK_CMD_DEL
: return "del";
3732 case DEVLINK_CMD_PORT_GET
: return "get";
3733 case DEVLINK_CMD_PORT_SET
: return "set";
3734 case DEVLINK_CMD_PORT_NEW
: return "new";
3735 case DEVLINK_CMD_PORT_DEL
: return "del";
3736 case DEVLINK_CMD_PARAM_GET
: return "get";
3737 case DEVLINK_CMD_PARAM_SET
: return "set";
3738 case DEVLINK_CMD_PARAM_NEW
: return "new";
3739 case DEVLINK_CMD_PARAM_DEL
: return "del";
3740 case DEVLINK_CMD_REGION_GET
: return "get";
3741 case DEVLINK_CMD_REGION_SET
: return "set";
3742 case DEVLINK_CMD_REGION_NEW
: return "new";
3743 case DEVLINK_CMD_REGION_DEL
: return "del";
3744 default: return "<unknown cmd>";
3748 static const char *cmd_obj(uint8_t cmd
)
3751 case DEVLINK_CMD_UNSPEC
: return "unspec";
3752 case DEVLINK_CMD_GET
:
3753 case DEVLINK_CMD_SET
:
3754 case DEVLINK_CMD_NEW
:
3755 case DEVLINK_CMD_DEL
:
3757 case DEVLINK_CMD_PORT_GET
:
3758 case DEVLINK_CMD_PORT_SET
:
3759 case DEVLINK_CMD_PORT_NEW
:
3760 case DEVLINK_CMD_PORT_DEL
:
3762 case DEVLINK_CMD_PARAM_GET
:
3763 case DEVLINK_CMD_PARAM_SET
:
3764 case DEVLINK_CMD_PARAM_NEW
:
3765 case DEVLINK_CMD_PARAM_DEL
:
3767 case DEVLINK_CMD_REGION_GET
:
3768 case DEVLINK_CMD_REGION_SET
:
3769 case DEVLINK_CMD_REGION_NEW
:
3770 case DEVLINK_CMD_REGION_DEL
:
3772 default: return "<unknown obj>";
3776 static void pr_out_mon_header(uint8_t cmd
)
3778 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
3781 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
3783 const char *obj
= cmd_obj(cmd
);
3784 unsigned int index
= 0;
3785 const char *cur_obj
;
3789 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3790 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
3796 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
);
3798 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3800 struct dl
*dl
= data
;
3801 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3802 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3803 uint8_t cmd
= genl
->cmd
;
3805 if (!cmd_filter_check(dl
, cmd
))
3809 case DEVLINK_CMD_GET
: /* fall through */
3810 case DEVLINK_CMD_SET
: /* fall through */
3811 case DEVLINK_CMD_NEW
: /* fall through */
3812 case DEVLINK_CMD_DEL
:
3813 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3814 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
3815 return MNL_CB_ERROR
;
3816 pr_out_mon_header(genl
->cmd
);
3819 case DEVLINK_CMD_PORT_GET
: /* fall through */
3820 case DEVLINK_CMD_PORT_SET
: /* fall through */
3821 case DEVLINK_CMD_PORT_NEW
: /* fall through */
3822 case DEVLINK_CMD_PORT_DEL
:
3823 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3824 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3825 !tb
[DEVLINK_ATTR_PORT_INDEX
])
3826 return MNL_CB_ERROR
;
3827 pr_out_mon_header(genl
->cmd
);
3828 pr_out_port(dl
, tb
);
3830 case DEVLINK_CMD_PARAM_GET
: /* fall through */
3831 case DEVLINK_CMD_PARAM_SET
: /* fall through */
3832 case DEVLINK_CMD_PARAM_NEW
: /* fall through */
3833 case DEVLINK_CMD_PARAM_DEL
:
3834 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3835 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3836 !tb
[DEVLINK_ATTR_PARAM
])
3837 return MNL_CB_ERROR
;
3838 pr_out_mon_header(genl
->cmd
);
3839 pr_out_param(dl
, tb
, false);
3841 case DEVLINK_CMD_REGION_GET
: /* fall through */
3842 case DEVLINK_CMD_REGION_SET
: /* fall through */
3843 case DEVLINK_CMD_REGION_NEW
: /* fall through */
3844 case DEVLINK_CMD_REGION_DEL
:
3845 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3846 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3847 !tb
[DEVLINK_ATTR_REGION_NAME
])
3848 return MNL_CB_ERROR
;
3849 pr_out_mon_header(genl
->cmd
);
3850 pr_out_region(dl
, tb
);
3856 static int cmd_mon_show(struct dl
*dl
)
3859 unsigned int index
= 0;
3860 const char *cur_obj
;
3862 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
3863 if (strcmp(cur_obj
, "all") != 0 &&
3864 strcmp(cur_obj
, "dev") != 0 &&
3865 strcmp(cur_obj
, "port") != 0) {
3866 pr_err("Unknown object \"%s\"\n", cur_obj
);
3870 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
3873 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
3879 static void cmd_mon_help(void)
3881 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
3882 "where OBJECT-LIST := { dev | port }\n");
3885 static int cmd_mon(struct dl
*dl
)
3887 if (dl_argv_match(dl
, "help")) {
3891 return cmd_mon_show(dl
);
3894 struct dpipe_field
{
3897 unsigned int bitwidth
;
3898 enum devlink_dpipe_field_mapping_type mapping_type
;
3901 struct dpipe_header
{
3902 struct list_head list
;
3905 struct dpipe_field
*fields
;
3906 unsigned int fields_count
;
3909 struct dpipe_table
{
3910 struct list_head list
;
3912 unsigned int resource_id
;
3913 bool resource_valid
;
3916 struct dpipe_tables
{
3917 struct list_head table_list
;
3927 enum devlink_resource_unit unit
;
3932 struct list_head list
;
3933 struct list_head resource_list
;
3934 struct resource
*parent
;
3938 struct list_head resource_list
;
3941 struct resource_ctx
{
3944 struct resources
*resources
;
3945 struct dpipe_tables
*tables
;
3946 bool print_resources
;
3947 bool pending_change
;
3950 static struct resource
*resource_alloc(void)
3952 struct resource
*resource
;
3954 resource
= calloc(1, sizeof(struct resource
));
3957 INIT_LIST_HEAD(&resource
->resource_list
);
3961 static void resource_free(struct resource
*resource
)
3963 struct resource
*child_resource
, *tmp
;
3965 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
3967 free(child_resource
->name
);
3968 resource_free(child_resource
);
3973 static struct resources
*resources_alloc(void)
3975 struct resources
*resources
;
3977 resources
= calloc(1, sizeof(struct resources
));
3980 INIT_LIST_HEAD(&resources
->resource_list
);
3984 static void resources_free(struct resources
*resources
)
3986 struct resource
*resource
, *tmp
;
3988 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
3989 resource_free(resource
);
3992 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
3994 ctx
->resources
= resources_alloc();
3995 if (!ctx
->resources
)
4001 static void resource_ctx_fini(struct resource_ctx
*ctx
)
4003 resources_free(ctx
->resources
);
4009 struct list_head global_headers
;
4010 struct list_head local_headers
;
4011 struct dpipe_tables
*tables
;
4012 struct resources
*resources
;
4017 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
4019 struct dpipe_header
*header
;
4021 header
= calloc(1, sizeof(struct dpipe_header
));
4024 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
4025 if (!header
->fields
)
4026 goto err_fields_alloc
;
4027 header
->fields_count
= fields_count
;
4035 static void dpipe_header_free(struct dpipe_header
*header
)
4037 free(header
->fields
);
4041 static void dpipe_header_clear(struct dpipe_header
*header
)
4043 struct dpipe_field
*field
;
4046 for (i
= 0; i
< header
->fields_count
; i
++) {
4047 field
= &header
->fields
[i
];
4053 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
4054 struct dpipe_header
*header
, bool global
)
4057 list_add(&header
->list
, &ctx
->global_headers
);
4059 list_add(&header
->list
, &ctx
->local_headers
);
4062 static void dpipe_header_del(struct dpipe_header
*header
)
4064 list_del(&header
->list
);
4067 static struct dpipe_table
*dpipe_table_alloc(void)
4069 return calloc(1, sizeof(struct dpipe_table
));
4072 static void dpipe_table_free(struct dpipe_table
*table
)
4077 static struct dpipe_tables
*dpipe_tables_alloc(void)
4079 struct dpipe_tables
*tables
;
4081 tables
= calloc(1, sizeof(struct dpipe_tables
));
4084 INIT_LIST_HEAD(&tables
->table_list
);
4088 static void dpipe_tables_free(struct dpipe_tables
*tables
)
4090 struct dpipe_table
*table
, *tmp
;
4092 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
4093 dpipe_table_free(table
);
4097 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
4099 ctx
->tables
= dpipe_tables_alloc();
4104 INIT_LIST_HEAD(&ctx
->global_headers
);
4105 INIT_LIST_HEAD(&ctx
->local_headers
);
4109 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
4111 struct dpipe_header
*header
, *tmp
;
4113 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
4115 dpipe_header_del(header
);
4116 dpipe_header_clear(header
);
4117 dpipe_header_free(header
);
4119 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
4121 dpipe_header_del(header
);
4122 dpipe_header_clear(header
);
4123 dpipe_header_free(header
);
4125 dpipe_tables_free(ctx
->tables
);
4128 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
4129 uint32_t header_id
, bool global
)
4131 struct list_head
*header_list
;
4132 struct dpipe_header
*header
;
4135 header_list
= &ctx
->global_headers
;
4137 header_list
= &ctx
->local_headers
;
4138 list_for_each_entry(header
, header_list
, list
) {
4139 if (header
->id
!= header_id
)
4141 return header
->name
;
4146 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
4148 uint32_t field_id
, bool global
)
4150 struct list_head
*header_list
;
4151 struct dpipe_header
*header
;
4154 header_list
= &ctx
->global_headers
;
4156 header_list
= &ctx
->local_headers
;
4157 list_for_each_entry(header
, header_list
, list
) {
4158 if (header
->id
!= header_id
)
4160 return header
->fields
[field_id
].name
;
4166 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
4168 switch (mapping_type
) {
4169 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
4171 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
4179 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
4180 uint32_t field_id
, bool global
)
4182 enum devlink_dpipe_field_mapping_type mapping_type
;
4183 struct list_head
*header_list
;
4184 struct dpipe_header
*header
;
4187 header_list
= &ctx
->global_headers
;
4189 header_list
= &ctx
->local_headers
;
4190 list_for_each_entry(header
, header_list
, list
) {
4191 if (header
->id
!= header_id
)
4193 mapping_type
= header
->fields
[field_id
].mapping_type
;
4194 return dpipe_field_mapping_e2s(mapping_type
);
4199 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
4200 struct dpipe_field
*fields
,
4201 unsigned int field_count
)
4203 struct dpipe_field
*field
;
4206 for (i
= 0; i
< field_count
; i
++) {
4208 pr_out_entry_start(ctx
->dl
);
4209 pr_out_str(ctx
->dl
, "name", field
->name
);
4210 if (ctx
->dl
->verbose
)
4211 pr_out_uint(ctx
->dl
, "id", field
->id
);
4212 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
4213 if (field
->mapping_type
)
4214 pr_out_str(ctx
->dl
, "mapping_type",
4215 dpipe_field_mapping_e2s(field
->mapping_type
));
4216 pr_out_entry_end(ctx
->dl
);
4221 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
4222 struct dpipe_header
*header
, bool global
)
4224 pr_out_handle_start_arr(ctx
->dl
, tb
);
4225 pr_out_str(ctx
->dl
, "name", header
->name
);
4226 if (ctx
->dl
->verbose
) {
4227 pr_out_uint(ctx
->dl
, "id", header
->id
);
4228 pr_out_str(ctx
->dl
, "global",
4229 global
? "true" : "false");
4231 pr_out_array_start(ctx
->dl
, "field");
4232 pr_out_dpipe_fields(ctx
, header
->fields
,
4233 header
->fields_count
);
4234 pr_out_array_end(ctx
->dl
);
4235 pr_out_handle_end(ctx
->dl
);
4238 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
4241 struct dpipe_header
*header
;
4243 list_for_each_entry(header
, &ctx
->local_headers
, list
)
4244 pr_out_dpipe_header(ctx
, tb
, header
, false);
4246 list_for_each_entry(header
, &ctx
->global_headers
, list
)
4247 pr_out_dpipe_header(ctx
, tb
, header
, true);
4250 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
4252 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
4256 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
4257 if (err
!= MNL_CB_OK
)
4259 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
4260 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
4261 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
4262 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
4265 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
4266 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4267 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
4268 field
->name
= strdup(name
);
4271 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
4275 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
4276 struct dpipe_field
*fields
)
4278 struct nlattr
*nla_field
;
4282 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
4283 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
4291 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
4293 struct nlattr
*nla_field
;
4294 unsigned int count
= 0;
4296 mnl_attr_for_each_nested(nla_field
, nla_fields
)
4301 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4303 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
4304 struct dpipe_header
*header
;
4305 unsigned int fields_count
;
4306 const char *header_name
;
4310 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
4311 if (err
!= MNL_CB_OK
)
4314 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
4315 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4316 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
4319 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
4320 header
= dpipe_header_alloc(fields_count
);
4324 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
4325 header
->name
= strdup(header_name
);
4326 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4327 header
->fields_count
= fields_count
;
4328 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4330 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
4334 dpipe_header_add(ctx
, header
, global
);
4338 dpipe_header_free(header
);
4342 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4344 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
4345 struct nlattr
*nla_header
;
4348 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
4349 err
= dpipe_header_get(ctx
, nla_header
);
4356 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
4358 struct dpipe_ctx
*ctx
= data
;
4359 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4360 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4363 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4364 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4365 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
4366 return MNL_CB_ERROR
;
4367 err
= dpipe_headers_get(ctx
, tb
);
4370 return MNL_CB_ERROR
;
4373 if (ctx
->print_headers
)
4374 pr_out_dpipe_headers(ctx
, tb
);
4378 static int cmd_dpipe_headers_show(struct dl
*dl
)
4380 struct nlmsghdr
*nlh
;
4381 struct dpipe_ctx ctx
= {};
4382 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4385 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4387 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
4391 err
= dpipe_ctx_init(&ctx
, dl
);
4395 ctx
.print_headers
= true;
4397 pr_out_section_start(dl
, "header");
4398 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4400 pr_err("error get headers %s\n", strerror(ctx
.err
));
4401 pr_out_section_end(dl
);
4403 dpipe_ctx_fini(&ctx
);
4407 static void cmd_dpipe_header_help(void)
4409 pr_err("Usage: devlink dpipe headers show DEV\n");
4412 static int cmd_dpipe_header(struct dl
*dl
)
4414 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4415 cmd_dpipe_header_help();
4417 } else if (dl_argv_match(dl
, "show")) {
4419 return cmd_dpipe_headers_show(dl
);
4421 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4426 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
4428 switch (action_type
) {
4429 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
4430 return "field_modify";
4436 struct dpipe_op_info
{
4442 struct dpipe_action
{
4443 struct dpipe_op_info info
;
4447 static void pr_out_dpipe_action(struct dpipe_action
*action
,
4448 struct dpipe_ctx
*ctx
)
4450 struct dpipe_op_info
*op_info
= &action
->info
;
4451 const char *mapping
;
4453 pr_out_str(ctx
->dl
, "type",
4454 dpipe_action_type_e2s(action
->type
));
4455 pr_out_str(ctx
->dl
, "header",
4456 dpipe_header_id2s(ctx
, op_info
->header_id
,
4457 op_info
->header_global
));
4458 pr_out_str(ctx
->dl
, "field",
4459 dpipe_field_id2s(ctx
, op_info
->header_id
,
4461 op_info
->header_global
));
4462 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4464 op_info
->header_global
);
4466 pr_out_str(ctx
->dl
, "mapping", mapping
);
4469 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
4471 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
4474 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
4475 if (err
!= MNL_CB_OK
)
4478 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
4479 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4480 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4481 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4485 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
4486 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4487 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4488 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4493 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
4494 struct nlattr
*nla_actions
)
4496 struct nlattr
*nla_action
;
4497 struct dpipe_action action
;
4499 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
4500 pr_out_entry_start(ctx
->dl
);
4501 if (dpipe_action_parse(&action
, nla_action
))
4502 goto err_action_parse
;
4503 pr_out_dpipe_action(&action
, ctx
);
4504 pr_out_entry_end(ctx
->dl
);
4509 pr_out_entry_end(ctx
->dl
);
4514 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
4516 switch (match_type
) {
4517 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
4518 return "field_exact";
4524 struct dpipe_match
{
4525 struct dpipe_op_info info
;
4529 static void pr_out_dpipe_match(struct dpipe_match
*match
,
4530 struct dpipe_ctx
*ctx
)
4532 struct dpipe_op_info
*op_info
= &match
->info
;
4533 const char *mapping
;
4535 pr_out_str(ctx
->dl
, "type",
4536 dpipe_match_type_e2s(match
->type
));
4537 pr_out_str(ctx
->dl
, "header",
4538 dpipe_header_id2s(ctx
, op_info
->header_id
,
4539 op_info
->header_global
));
4540 pr_out_str(ctx
->dl
, "field",
4541 dpipe_field_id2s(ctx
, op_info
->header_id
,
4543 op_info
->header_global
));
4544 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
4546 op_info
->header_global
);
4548 pr_out_str(ctx
->dl
, "mapping", mapping
);
4551 static int dpipe_match_parse(struct dpipe_match
*match
,
4555 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
4558 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
4559 if (err
!= MNL_CB_OK
)
4562 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
4563 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
4564 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
4565 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
4569 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
4570 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
4571 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
4572 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
4577 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
4578 struct nlattr
*nla_matches
)
4580 struct nlattr
*nla_match
;
4581 struct dpipe_match match
;
4583 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
4584 pr_out_entry_start(ctx
->dl
);
4585 if (dpipe_match_parse(&match
, nla_match
))
4586 goto err_match_parse
;
4587 pr_out_dpipe_match(&match
, ctx
);
4588 pr_out_entry_end(ctx
->dl
);
4593 pr_out_entry_end(ctx
->dl
);
4597 static struct resource
*
4598 resource_find(struct resources
*resources
, struct resource
*resource
,
4599 uint64_t resource_id
)
4601 struct list_head
*list_head
;
4604 list_head
= &resources
->resource_list
;
4606 list_head
= &resource
->resource_list
;
4608 list_for_each_entry(resource
, list_head
, list
) {
4609 struct resource
*child_resource
;
4611 if (resource
->id
== resource_id
)
4614 child_resource
= resource_find(resources
, resource
,
4617 return child_resource
;
4623 resource_path_print(struct dl
*dl
, struct resources
*resources
,
4624 uint64_t resource_id
)
4626 struct resource
*resource
, *parent_resource
;
4627 const char del
[] = "/";
4631 resource
= resource_find(resources
, NULL
, resource_id
);
4635 for (parent_resource
= resource
; parent_resource
;
4636 parent_resource
= parent_resource
->parent
)
4637 path_len
+= strlen(parent_resource
->name
) + 1;
4640 path
= calloc(1, path_len
);
4644 path
+= path_len
- 1;
4645 for (parent_resource
= resource
; parent_resource
;
4646 parent_resource
= parent_resource
->parent
) {
4647 path
-= strlen(parent_resource
->name
);
4648 memcpy(path
, parent_resource
->name
,
4649 strlen(parent_resource
->name
));
4650 path
-= strlen(del
);
4651 memcpy(path
, del
, strlen(del
));
4653 pr_out_str(dl
, "resource_path", path
);
4657 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
4659 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
4660 struct dpipe_table
*table
;
4661 uint32_t resource_units
;
4662 bool counters_enabled
;
4663 bool resource_valid
;
4667 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
4668 if (err
!= MNL_CB_OK
)
4671 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
4672 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
4673 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
4674 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
4675 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
4679 table
= dpipe_table_alloc();
4683 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
4684 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
4685 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
4687 resource_valid
= nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
] &&
4689 if (resource_valid
) {
4690 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
4691 table
->resource_valid
= true;
4694 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
4695 if (!ctx
->print_tables
)
4698 pr_out_str(ctx
->dl
, "name", table
->name
);
4699 pr_out_uint(ctx
->dl
, "size", size
);
4700 pr_out_str(ctx
->dl
, "counters_enabled",
4701 counters_enabled
? "true" : "false");
4703 if (resource_valid
) {
4704 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
4705 resource_path_print(ctx
->dl
, ctx
->resources
,
4706 table
->resource_id
);
4707 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
4710 pr_out_array_start(ctx
->dl
, "match");
4711 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
4712 goto err_matches_show
;
4713 pr_out_array_end(ctx
->dl
);
4715 pr_out_array_start(ctx
->dl
, "action");
4716 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
4717 goto err_actions_show
;
4718 pr_out_array_end(ctx
->dl
);
4724 pr_out_array_end(ctx
->dl
);
4728 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4730 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
4731 struct nlattr
*nla_table
;
4733 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
4734 if (ctx
->print_tables
)
4735 pr_out_handle_start_arr(ctx
->dl
, tb
);
4736 if (dpipe_table_show(ctx
, nla_table
))
4737 goto err_table_show
;
4738 if (ctx
->print_tables
)
4739 pr_out_handle_end(ctx
->dl
);
4744 if (ctx
->print_tables
)
4745 pr_out_handle_end(ctx
->dl
);
4749 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
4751 struct dpipe_ctx
*ctx
= data
;
4752 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4753 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4755 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4756 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4757 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
4758 return MNL_CB_ERROR
;
4760 if (dpipe_tables_show(ctx
, tb
))
4761 return MNL_CB_ERROR
;
4765 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
4767 static int cmd_dpipe_table_show(struct dl
*dl
)
4769 struct nlmsghdr
*nlh
;
4770 struct dpipe_ctx dpipe_ctx
= {};
4771 struct resource_ctx resource_ctx
= {};
4772 uint16_t flags
= NLM_F_REQUEST
;
4775 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
4779 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4781 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4785 dpipe_ctx
.print_tables
= true;
4787 dl_opts_put(nlh
, dl
);
4788 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
4791 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
4792 goto err_headers_get
;
4795 err
= resource_ctx_init(&resource_ctx
, dl
);
4797 goto err_resource_ctx_init
;
4799 resource_ctx
.print_resources
= false;
4800 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
4801 dl_opts_put(nlh
, dl
);
4802 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4805 dpipe_ctx
.resources
= resource_ctx
.resources
;
4807 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4808 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
4809 dl_opts_put(nlh
, dl
);
4811 pr_out_section_start(dl
, "table");
4812 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
4813 pr_out_section_end(dl
);
4815 resource_ctx_fini(&resource_ctx
);
4816 dpipe_ctx_fini(&dpipe_ctx
);
4819 err_resource_ctx_init
:
4821 dpipe_ctx_fini(&dpipe_ctx
);
4825 static int cmd_dpipe_table_set(struct dl
*dl
)
4827 struct nlmsghdr
*nlh
;
4830 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
4831 NLM_F_REQUEST
| NLM_F_ACK
);
4833 err
= dl_argv_parse_put(nlh
, dl
,
4834 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
4835 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
4839 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4842 enum dpipe_value_type
{
4843 DPIPE_VALUE_TYPE_VALUE
,
4844 DPIPE_VALUE_TYPE_MASK
,
4848 dpipe_value_type_e2s(enum dpipe_value_type type
)
4851 case DPIPE_VALUE_TYPE_VALUE
:
4853 case DPIPE_VALUE_TYPE_MASK
:
4854 return "value_mask";
4860 struct dpipe_field_printer
{
4861 unsigned int field_id
;
4862 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
4865 struct dpipe_header_printer
{
4866 struct dpipe_field_printer
*printers
;
4867 unsigned int printers_count
;
4868 unsigned int header_id
;
4871 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
4872 enum dpipe_value_type type
,
4875 struct in_addr ip_addr
;
4877 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
4878 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
4882 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
4883 enum dpipe_value_type type
,
4886 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
4887 ether_ntoa((struct ether_addr
*)value
));
4890 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
4891 enum dpipe_value_type type
,
4894 char str
[INET6_ADDRSTRLEN
];
4896 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
4897 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
4900 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
4902 .printer
= dpipe_field_printer_ipv4_addr
,
4903 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
4907 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
4908 .printers
= dpipe_field_printers_ipv4
,
4909 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
4910 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
4913 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
4915 .printer
= dpipe_field_printer_ethernet_addr
,
4916 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
4920 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
4921 .printers
= dpipe_field_printers_ethernet
,
4922 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
4923 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
4926 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
4928 .printer
= dpipe_field_printer_ipv6_addr
,
4929 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
4933 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
4934 .printers
= dpipe_field_printers_ipv6
,
4935 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
4936 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
4939 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
4940 &dpipe_header_printer_ipv4
,
4941 &dpipe_header_printer_ethernet
,
4942 &dpipe_header_printer_ipv6
,
4945 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
4946 struct dpipe_op_info
*info
,
4947 enum dpipe_value_type type
,
4950 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
4951 struct dpipe_header_printer
*header_printer
;
4952 struct dpipe_field_printer
*field_printer
;
4953 unsigned int field_printers_count
;
4957 for (i
= 0; i
< header_printers_count
; i
++) {
4958 header_printer
= dpipe_header_printers
[i
];
4959 if (header_printer
->header_id
!= info
->header_id
)
4961 field_printers_count
= header_printer
->printers_count
;
4962 for (j
= 0; j
< field_printers_count
; j
++) {
4963 field_printer
= &header_printer
->printers
[j
];
4964 if (field_printer
->field_id
!= info
->field_id
)
4966 field_printer
->printer(ctx
, type
, value
);
4974 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
4976 unsigned int value_len
,
4977 struct dpipe_op_info
*info
,
4978 enum dpipe_value_type type
)
4980 if (info
->header_global
&&
4981 !dpipe_print_prot_header(ctx
, info
, type
, value
))
4984 if (value_len
== sizeof(uint32_t)) {
4985 uint32_t *value_32
= value
;
4987 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
4991 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
4992 struct nlattr
**nla_match_value
,
4993 struct dpipe_op_info
*info
)
4995 void *value
, *value_mask
;
4996 uint32_t value_mapping
;
5000 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
5001 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
5003 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5004 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5007 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
5008 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
5012 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
5013 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
5014 DPIPE_VALUE_TYPE_MASK
);
5017 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
5020 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
5023 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5024 struct dpipe_match match
;
5027 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
5028 if (err
!= MNL_CB_OK
)
5031 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
5032 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5036 pr_out_entry_start(ctx
->dl
);
5037 if (dpipe_match_parse(&match
,
5038 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
5039 goto err_match_parse
;
5040 pr_out_dpipe_match(&match
, ctx
);
5041 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
5042 pr_out_entry_end(ctx
->dl
);
5047 pr_out_entry_end(ctx
->dl
);
5051 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
5054 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
5055 struct dpipe_action action
;
5058 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
5059 if (err
!= MNL_CB_OK
)
5062 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
5063 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
5067 pr_out_entry_start(ctx
->dl
);
5068 if (dpipe_action_parse(&action
,
5069 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
5070 goto err_action_parse
;
5071 pr_out_dpipe_action(&action
, ctx
);
5072 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
5073 pr_out_entry_end(ctx
->dl
);
5078 pr_out_entry_end(ctx
->dl
);
5083 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
5084 struct nlattr
*nla_action_values
)
5086 struct nlattr
*nla_action_value
;
5088 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
5089 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
5096 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
5097 struct nlattr
*nla_match_values
)
5099 struct nlattr
*nla_match_value
;
5101 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
5102 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
5108 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
5110 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
5111 uint32_t entry_index
;
5115 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
5116 if (err
!= MNL_CB_OK
)
5119 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
5120 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
5121 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
5125 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
5126 pr_out_uint(ctx
->dl
, "index", entry_index
);
5128 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
5129 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
5130 pr_out_uint(ctx
->dl
, "counter", counter
);
5133 pr_out_array_start(ctx
->dl
, "match_value");
5134 if (dpipe_tables_match_values_show(ctx
,
5135 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
5136 goto err_match_values_show
;
5137 pr_out_array_end(ctx
->dl
);
5139 pr_out_array_start(ctx
->dl
, "action_value");
5140 if (dpipe_tables_action_values_show(ctx
,
5141 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
5142 goto err_action_values_show
;
5143 pr_out_array_end(ctx
->dl
);
5146 err_action_values_show
:
5147 err_match_values_show
:
5148 pr_out_array_end(ctx
->dl
);
5152 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
5154 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
5155 struct nlattr
*nla_entry
;
5157 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
5158 pr_out_handle_start_arr(ctx
->dl
, tb
);
5159 if (dpipe_entry_show(ctx
, nla_entry
))
5160 goto err_entry_show
;
5161 pr_out_handle_end(ctx
->dl
);
5166 pr_out_handle_end(ctx
->dl
);
5170 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5172 struct dpipe_ctx
*ctx
= data
;
5173 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5174 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5176 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5177 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5178 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
5179 return MNL_CB_ERROR
;
5181 if (dpipe_table_entries_show(ctx
, tb
))
5182 return MNL_CB_ERROR
;
5186 static int cmd_dpipe_table_dump(struct dl
*dl
)
5188 struct nlmsghdr
*nlh
;
5189 struct dpipe_ctx ctx
= {};
5190 uint16_t flags
= NLM_F_REQUEST
;
5193 err
= dpipe_ctx_init(&ctx
, dl
);
5197 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
5201 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
5202 dl_opts_put(nlh
, dl
);
5203 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
5205 pr_err("error get headers %s\n", strerror(ctx
.err
));
5209 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5210 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
5211 dl_opts_put(nlh
, dl
);
5213 pr_out_section_start(dl
, "table_entry");
5214 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
5215 pr_out_section_end(dl
);
5217 dpipe_ctx_fini(&ctx
);
5221 static void cmd_dpipe_table_help(void)
5223 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
5224 "where OBJECT-LIST := { show | set | dump }\n");
5227 static int cmd_dpipe_table(struct dl
*dl
)
5229 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5230 cmd_dpipe_table_help();
5232 } else if (dl_argv_match(dl
, "show")) {
5234 return cmd_dpipe_table_show(dl
);
5235 } else if (dl_argv_match(dl
, "set")) {
5237 return cmd_dpipe_table_set(dl
);
5238 } else if (dl_argv_match(dl
, "dump")) {
5240 return cmd_dpipe_table_dump(dl
);
5242 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5246 static void cmd_dpipe_help(void)
5248 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
5249 "where OBJECT-LIST := { header | table }\n");
5252 static int cmd_dpipe(struct dl
*dl
)
5254 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5257 } else if (dl_argv_match(dl
, "header")) {
5259 return cmd_dpipe_header(dl
);
5260 } else if (dl_argv_match(dl
, "table")) {
5262 return cmd_dpipe_table(dl
);
5264 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5269 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
5270 struct nlattr
**nla_resource
)
5272 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
5273 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
5274 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
5275 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
5276 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
5277 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
5278 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
5282 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
5283 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
5284 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
5285 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
5286 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
5287 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
5288 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
5290 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
5291 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
5293 resource
->size_new
= resource
->size
;
5295 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
5296 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
5297 resource
->occ_valid
= true;
5300 if (resource
->size_new
!= resource
->size
)
5301 ctx
->pending_change
= true;
5307 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
5308 struct resource
*parent_resource
, struct nlattr
*nl
)
5310 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
5311 struct nlattr
*nla_child_resource
;
5312 struct nlattr
*nla_resources
;
5322 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
5323 if (err
!= MNL_CB_OK
)
5326 err
= resource_parse(ctx
, resource
, nla_resource
);
5330 resource
->parent
= parent_resource
;
5331 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
5334 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
5335 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
5337 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
5338 struct resource
*child_resource
;
5339 struct list_head
*list
;
5341 child_resource
= resource_alloc();
5342 if (!child_resource
)
5346 list
= &ctx
->resources
->resource_list
;
5348 list
= &resource
->resource_list
;
5350 list_add_tail(&child_resource
->list
, list
);
5351 err
= resource_get(ctx
, child_resource
, resource
,
5352 nla_child_resource
);
5360 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
5363 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
5364 default: return "<unknown unit>";
5368 static void resource_show(struct resource
*resource
,
5369 struct resource_ctx
*ctx
)
5371 struct resource
*child_resource
;
5372 struct dpipe_table
*table
;
5373 struct dl
*dl
= ctx
->dl
;
5376 pr_out_str(dl
, "name", resource
->name
);
5378 resource_path_print(dl
, ctx
->resources
, resource
->id
);
5379 pr_out_u64(dl
, "size", resource
->size
);
5380 if (resource
->size
!= resource
->size_new
)
5381 pr_out_u64(dl
, "size_new", resource
->size_new
);
5382 if (resource
->occ_valid
)
5383 pr_out_uint(dl
, "occ", resource
->size_occ
);
5384 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
5386 if (resource
->size_min
!= resource
->size_max
) {
5387 pr_out_uint(dl
, "size_min", resource
->size_min
);
5388 pr_out_u64(dl
, "size_max", resource
->size_max
);
5389 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
5392 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
5393 if (table
->resource_id
== resource
->id
&&
5394 table
->resource_valid
)
5398 pr_out_array_start(dl
, "dpipe_tables");
5400 pr_out_str(dl
, "dpipe_tables", "none");
5402 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
5403 if (table
->resource_id
!= resource
->id
||
5404 !table
->resource_valid
)
5406 pr_out_entry_start(dl
);
5407 pr_out_str(dl
, "table_name", table
->name
);
5408 pr_out_entry_end(dl
);
5411 pr_out_array_end(dl
);
5413 if (list_empty(&resource
->resource_list
))
5416 if (ctx
->pending_change
)
5417 pr_out_str(dl
, "size_valid", resource
->size_valid
?
5419 pr_out_array_start(dl
, "resources");
5420 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
5421 pr_out_entry_start(dl
);
5422 resource_show(child_resource
, ctx
);
5423 pr_out_entry_end(dl
);
5425 pr_out_array_end(dl
);
5429 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5431 struct resources
*resources
= ctx
->resources
;
5432 struct resource
*resource
;
5434 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
5435 pr_out_handle_start_arr(ctx
->dl
, tb
);
5436 resource_show(resource
, ctx
);
5437 pr_out_handle_end(ctx
->dl
);
5441 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
5443 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
5446 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
5448 struct resource_ctx
*ctx
= data
;
5449 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5450 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5453 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5454 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5455 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
5456 return MNL_CB_ERROR
;
5458 err
= resources_get(ctx
, tb
);
5461 return MNL_CB_ERROR
;
5464 if (ctx
->print_resources
)
5465 resources_show(ctx
, tb
);
5470 static int cmd_resource_show(struct dl
*dl
)
5472 struct nlmsghdr
*nlh
;
5473 struct dpipe_ctx dpipe_ctx
= {};
5474 struct resource_ctx resource_ctx
= {};
5477 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
5481 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
5483 dl_opts_put(nlh
, dl
);
5485 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
5489 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
5492 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
5496 err
= resource_ctx_init(&resource_ctx
, dl
);
5500 resource_ctx
.print_resources
= true;
5501 resource_ctx
.tables
= dpipe_ctx
.tables
;
5502 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5503 NLM_F_REQUEST
| NLM_F_ACK
);
5504 dl_opts_put(nlh
, dl
);
5505 pr_out_section_start(dl
, "resources");
5506 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
5508 pr_out_section_end(dl
);
5509 resource_ctx_fini(&resource_ctx
);
5511 dpipe_ctx_fini(&dpipe_ctx
);
5515 static void cmd_resource_help(void)
5517 pr_err("Usage: devlink resource show DEV\n"
5518 " devlink resource set DEV path PATH size SIZE\n");
5521 static struct resource
*
5522 resource_find_by_name(struct list_head
*list
, char *name
)
5524 struct resource
*resource
;
5526 list_for_each_entry(resource
, list
, list
) {
5527 if (!strcmp(resource
->name
, name
))
5534 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
5535 uint32_t *p_resource_id
, bool *p_resource_valid
)
5537 struct resource
*resource
;
5538 uint32_t resource_id
= 0;
5539 char *resource_path_dup
;
5540 struct list_head
*list
;
5541 const char del
[] = "/";
5542 char *resource_name
;
5544 resource_path_dup
= strdup(resource_path
);
5545 list
= &ctx
->resources
->resource_list
;
5546 resource_name
= strtok(resource_path_dup
, del
);
5547 while (resource_name
!= NULL
) {
5548 resource
= resource_find_by_name(list
, resource_name
);
5550 goto err_resource_lookup
;
5552 list
= &resource
->resource_list
;
5553 resource_name
= strtok(NULL
, del
);
5554 resource_id
= resource
->id
;
5556 free(resource_path_dup
);
5557 *p_resource_valid
= true;
5558 *p_resource_id
= resource_id
;
5561 err_resource_lookup
:
5562 free(resource_path_dup
);
5566 static int cmd_resource_set(struct dl
*dl
)
5568 struct nlmsghdr
*nlh
;
5569 struct resource_ctx ctx
= {};
5572 err
= resource_ctx_init(&ctx
, dl
);
5576 ctx
.print_resources
= false;
5577 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
5578 DL_OPT_RESOURCE_SIZE
, 0);
5582 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
5584 dl_opts_put(nlh
, dl
);
5585 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
5587 pr_err("error getting resources %s\n", strerror(ctx
.err
));
5591 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
5592 &dl
->opts
.resource_id
,
5593 &dl
->opts
.resource_id_valid
);
5595 pr_err("error parsing resource path %s\n", strerror(-err
));
5599 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
5600 NLM_F_REQUEST
| NLM_F_ACK
);
5602 dl_opts_put(nlh
, dl
);
5603 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5605 resource_ctx_fini(&ctx
);
5609 static int cmd_resource(struct dl
*dl
)
5611 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
5612 cmd_resource_help();
5614 } else if (dl_argv_match(dl
, "show")) {
5616 return cmd_resource_show(dl
);
5617 } else if (dl_argv_match(dl
, "set")) {
5619 return cmd_resource_set(dl
);
5621 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5625 static void pr_out_region_handle_start(struct dl
*dl
, struct nlattr
**tb
)
5627 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
5628 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
5629 const char *region_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_REGION_NAME
]);
5632 sprintf(buf
, "%s/%s/%s", bus_name
, dev_name
, region_name
);
5633 if (dl
->json_output
) {
5634 jsonw_name(dl
->jw
, buf
);
5635 jsonw_start_object(dl
->jw
);
5641 static void pr_out_region_handle_end(struct dl
*dl
)
5643 if (dl
->json_output
)
5644 jsonw_end_object(dl
->jw
);
5649 static void pr_out_region_snapshots_start(struct dl
*dl
, bool array
)
5651 if (dl
->json_output
) {
5652 jsonw_name(dl
->jw
, "snapshot");
5653 jsonw_start_array(dl
->jw
);
5655 if (g_indent_newline
)
5656 pr_out("snapshot %s", array
? "[" : "");
5658 pr_out(" snapshot %s", array
? "[" : "");
5662 static void pr_out_region_snapshots_end(struct dl
*dl
, bool array
)
5664 if (dl
->json_output
)
5665 jsonw_end_array(dl
->jw
);
5670 static void pr_out_region_snapshots_id(struct dl
*dl
, struct nlattr
**tb
, int index
)
5672 uint32_t snapshot_id
;
5674 if (!tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5677 snapshot_id
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
5679 if (dl
->json_output
)
5680 jsonw_uint(dl
->jw
, snapshot_id
);
5682 pr_out("%s%u", index
? " " : "", snapshot_id
);
5685 static void pr_out_snapshots(struct dl
*dl
, struct nlattr
**tb
)
5687 struct nlattr
*tb_snapshot
[DEVLINK_ATTR_MAX
+ 1] = {};
5688 struct nlattr
*nla_sanpshot
;
5691 pr_out_region_snapshots_start(dl
, true);
5692 mnl_attr_for_each_nested(nla_sanpshot
, tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
]) {
5693 err
= mnl_attr_parse_nested(nla_sanpshot
, attr_cb
, tb_snapshot
);
5694 if (err
!= MNL_CB_OK
)
5696 pr_out_region_snapshots_id(dl
, tb_snapshot
, index
++);
5698 pr_out_region_snapshots_end(dl
, true);
5701 static void pr_out_snapshot(struct dl
*dl
, struct nlattr
**tb
)
5703 pr_out_region_snapshots_start(dl
, false);
5704 pr_out_region_snapshots_id(dl
, tb
, 0);
5705 pr_out_region_snapshots_end(dl
, false);
5708 static void pr_out_region(struct dl
*dl
, struct nlattr
**tb
)
5710 pr_out_region_handle_start(dl
, tb
);
5712 if (tb
[DEVLINK_ATTR_REGION_SIZE
])
5713 pr_out_u64(dl
, "size",
5714 mnl_attr_get_u64(tb
[DEVLINK_ATTR_REGION_SIZE
]));
5716 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOTS
])
5717 pr_out_snapshots(dl
, tb
);
5719 if (tb
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
5720 pr_out_snapshot(dl
, tb
);
5722 pr_out_region_handle_end(dl
);
5725 static int cmd_region_show_cb(const struct nlmsghdr
*nlh
, void *data
)
5727 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5728 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5729 struct dl
*dl
= data
;
5731 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5732 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5733 !tb
[DEVLINK_ATTR_REGION_NAME
] || !tb
[DEVLINK_ATTR_REGION_SIZE
])
5734 return MNL_CB_ERROR
;
5736 pr_out_region(dl
, tb
);
5741 static int cmd_region_show(struct dl
*dl
)
5743 struct nlmsghdr
*nlh
;
5744 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
5747 if (dl_argc(dl
) == 0)
5748 flags
|= NLM_F_DUMP
;
5750 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_GET
, flags
);
5752 if (dl_argc(dl
) > 0) {
5753 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
, 0);
5758 pr_out_section_start(dl
, "regions");
5759 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_show_cb
, dl
);
5760 pr_out_section_end(dl
);
5764 static int cmd_region_snapshot_del(struct dl
*dl
)
5766 struct nlmsghdr
*nlh
;
5769 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_DEL
,
5770 NLM_F_REQUEST
| NLM_F_ACK
);
5772 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5773 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5777 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5780 static int cmd_region_read_cb(const struct nlmsghdr
*nlh
, void *data
)
5782 struct nlattr
*nla_entry
, *nla_chunk_data
, *nla_chunk_addr
;
5783 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
5784 struct nlattr
*tb_field
[DEVLINK_ATTR_MAX
+ 1] = {};
5785 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
5786 struct dl
*dl
= data
;
5789 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
5790 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
5791 !tb
[DEVLINK_ATTR_REGION_CHUNKS
])
5792 return MNL_CB_ERROR
;
5794 mnl_attr_for_each_nested(nla_entry
, tb
[DEVLINK_ATTR_REGION_CHUNKS
]) {
5795 err
= mnl_attr_parse_nested(nla_entry
, attr_cb
, tb_field
);
5796 if (err
!= MNL_CB_OK
)
5797 return MNL_CB_ERROR
;
5799 nla_chunk_data
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_DATA
];
5800 if (!nla_chunk_data
)
5803 nla_chunk_addr
= tb_field
[DEVLINK_ATTR_REGION_CHUNK_ADDR
];
5804 if (!nla_chunk_addr
)
5807 pr_out_region_chunk(dl
, mnl_attr_get_payload(nla_chunk_data
),
5808 mnl_attr_get_payload_len(nla_chunk_data
),
5809 mnl_attr_get_u64(nla_chunk_addr
));
5814 static int cmd_region_dump(struct dl
*dl
)
5816 struct nlmsghdr
*nlh
;
5819 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5820 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5822 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5823 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5827 pr_out_section_start(dl
, "dump");
5828 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5829 pr_out_section_end(dl
);
5830 if (!dl
->json_output
)
5835 static int cmd_region_read(struct dl
*dl
)
5837 struct nlmsghdr
*nlh
;
5840 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_REGION_READ
,
5841 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
5843 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE_REGION
|
5844 DL_OPT_REGION_ADDRESS
| DL_OPT_REGION_LENGTH
|
5845 DL_OPT_REGION_SNAPSHOT_ID
, 0);
5849 pr_out_section_start(dl
, "read");
5850 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_region_read_cb
, dl
);
5851 pr_out_section_end(dl
);
5852 if (!dl
->json_output
)
5857 static void cmd_region_help(void)
5859 pr_err("Usage: devlink region show [ DEV/REGION ]\n");
5860 pr_err(" devlink region del DEV/REGION snapshot SNAPSHOT_ID\n");
5861 pr_err(" devlink region dump DEV/REGION [ snapshot SNAPSHOT_ID ]\n");
5862 pr_err(" devlink region read DEV/REGION [ snapshot SNAPSHOT_ID ] address ADDRESS length LENGTH\n");
5865 static int cmd_region(struct dl
*dl
)
5867 if (dl_no_arg(dl
)) {
5868 return cmd_region_show(dl
);
5869 } else if (dl_argv_match(dl
, "help")) {
5872 } else if (dl_argv_match(dl
, "show")) {
5874 return cmd_region_show(dl
);
5875 } else if (dl_argv_match(dl
, "del")) {
5877 return cmd_region_snapshot_del(dl
);
5878 } else if (dl_argv_match(dl
, "dump")) {
5880 return cmd_region_dump(dl
);
5881 } else if (dl_argv_match(dl
, "read")) {
5883 return cmd_region_read(dl
);
5885 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
5889 static int cmd_health_set_params(struct dl
*dl
)
5891 struct nlmsghdr
*nlh
;
5894 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_SET
,
5895 NLM_F_REQUEST
| NLM_F_ACK
);
5896 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
,
5897 DL_OPT_HEALTH_REPORTER_GRACEFUL_PERIOD
|
5898 DL_OPT_HEALTH_REPORTER_AUTO_RECOVER
);
5902 dl_opts_put(nlh
, dl
);
5903 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5906 static int cmd_health_dump_clear(struct dl
*dl
)
5908 struct nlmsghdr
*nlh
;
5911 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
5912 NLM_F_REQUEST
| NLM_F_ACK
);
5914 err
= dl_argv_parse_put(nlh
, dl
,
5915 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
5919 dl_opts_put(nlh
, dl
);
5920 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
5923 static int fmsg_value_show(struct dl
*dl
, int type
, struct nlattr
*nl_data
)
5930 pr_out_bool_value(dl
, mnl_attr_get_u8(nl_data
));
5933 pr_out_uint_value(dl
, mnl_attr_get_u8(nl_data
));
5936 pr_out_uint_value(dl
, mnl_attr_get_u16(nl_data
));
5939 pr_out_uint_value(dl
, mnl_attr_get_u32(nl_data
));
5942 pr_out_uint64_value(dl
, mnl_attr_get_u64(nl_data
));
5944 case MNL_TYPE_NUL_STRING
:
5945 pr_out_str_value(dl
, mnl_attr_get_str(nl_data
));
5947 case MNL_TYPE_BINARY
:
5948 len
= mnl_attr_get_payload_len(nl_data
);
5949 data
= mnl_attr_get_payload(nl_data
);
5950 pr_out_binary_value(dl
, data
, len
);
5958 struct nest_qentry
{
5960 TAILQ_ENTRY(nest_qentry
) nest_entries
;
5963 struct fmsg_cb_data
{
5966 TAILQ_HEAD(, nest_qentry
) qhead
;
5969 static int cmd_fmsg_nest_queue(struct fmsg_cb_data
*fmsg_data
,
5970 uint8_t *attr_value
, bool insert
)
5972 struct nest_qentry
*entry
= NULL
;
5975 entry
= malloc(sizeof(struct nest_qentry
));
5979 entry
->attr_type
= *attr_value
;
5980 TAILQ_INSERT_HEAD(&fmsg_data
->qhead
, entry
, nest_entries
);
5982 if (TAILQ_EMPTY(&fmsg_data
->qhead
))
5983 return MNL_CB_ERROR
;
5984 entry
= TAILQ_FIRST(&fmsg_data
->qhead
);
5985 *attr_value
= entry
->attr_type
;
5986 TAILQ_REMOVE(&fmsg_data
->qhead
, entry
, nest_entries
);
5992 static int cmd_fmsg_nest(struct fmsg_cb_data
*fmsg_data
, uint8_t nest_value
,
5995 struct dl
*dl
= fmsg_data
->dl
;
5996 uint8_t value
= nest_value
;
5999 err
= cmd_fmsg_nest_queue(fmsg_data
, &value
, start
);
6000 if (err
!= MNL_CB_OK
)
6004 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6006 pr_out_entry_start(dl
);
6008 pr_out_entry_end(dl
);
6010 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6012 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6013 if (dl
->json_output
) {
6015 jsonw_start_array(dl
->jw
);
6017 jsonw_end_array(dl
->jw
);
6021 __pr_out_indent_inc();
6023 __pr_out_indent_dec();
6033 static int cmd_fmsg_object_cb(const struct nlmsghdr
*nlh
, void *data
)
6035 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6036 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6037 struct fmsg_cb_data
*fmsg_data
= data
;
6038 struct dl
*dl
= fmsg_data
->dl
;
6039 struct nlattr
*nla_object
;
6043 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6044 if (!tb
[DEVLINK_ATTR_FMSG
])
6045 return MNL_CB_ERROR
;
6047 mnl_attr_for_each_nested(nla_object
, tb
[DEVLINK_ATTR_FMSG
]) {
6048 attr_type
= mnl_attr_get_type(nla_object
);
6049 switch (attr_type
) {
6050 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
6051 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
6052 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
6053 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, true);
6054 if (err
!= MNL_CB_OK
)
6057 case DEVLINK_ATTR_FMSG_NEST_END
:
6058 err
= cmd_fmsg_nest(fmsg_data
, attr_type
, false);
6059 if (err
!= MNL_CB_OK
)
6062 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
6063 pr_out_name(dl
, mnl_attr_get_str(nla_object
));
6065 case DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
:
6066 fmsg_data
->value_type
= mnl_attr_get_u8(nla_object
);
6068 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
6069 err
= fmsg_value_show(dl
, fmsg_data
->value_type
,
6071 if (err
!= MNL_CB_OK
)
6081 static int cmd_health_object_common(struct dl
*dl
, uint8_t cmd
)
6083 struct fmsg_cb_data data
;
6084 struct nlmsghdr
*nlh
;
6087 nlh
= mnlg_msg_prepare(dl
->nlg
, cmd
, NLM_F_REQUEST
| NLM_F_ACK
);
6089 err
= dl_argv_parse_put(nlh
, dl
,
6090 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6095 TAILQ_INIT(&data
.qhead
);
6096 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_fmsg_object_cb
, &data
);
6100 static int cmd_health_dump_show(struct dl
*dl
)
6102 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
6105 static int cmd_health_diagnose(struct dl
*dl
)
6107 return cmd_health_object_common(dl
, DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
);
6110 static int cmd_health_recover(struct dl
*dl
)
6112 struct nlmsghdr
*nlh
;
6115 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6116 NLM_F_REQUEST
| NLM_F_ACK
);
6118 err
= dl_argv_parse_put(nlh
, dl
,
6119 DL_OPT_HANDLE
| DL_OPT_HEALTH_REPORTER_NAME
, 0);
6123 dl_opts_put(nlh
, dl
);
6124 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
6127 enum devlink_health_reporter_state
{
6128 DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
,
6129 DEVLINK_HEALTH_REPORTER_STATE_ERROR
,
6132 static const char *health_state_name(uint8_t state
)
6135 case DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
:
6136 return HEALTH_REPORTER_STATE_HEALTHY_STR
;
6137 case DEVLINK_HEALTH_REPORTER_STATE_ERROR
:
6138 return HEALTH_REPORTER_STATE_ERROR_STR
;
6140 return "<unknown state>";
6144 static void format_logtime(uint64_t time_ms
, char *ts_date
, char *ts_time
)
6146 struct sysinfo s_info
;
6152 info
= localtime(&now
);
6153 err
= sysinfo(&s_info
);
6156 /* Subtract uptime in sec from now yields the time of system
6157 * uptime. To this, add time_ms which is the amount of
6158 * milliseconds elapsed between uptime and the dump taken.
6160 sec
= now
- s_info
.uptime
+ time_ms
/ 1000;
6161 info
= localtime(&sec
);
6163 strftime(ts_date
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%Y-%m-%d", info
);
6164 strftime(ts_time
, HEALTH_REPORTER_TIMESTAMP_FMT_LEN
, "%H:%M:%S", info
);
6167 static void pr_out_health(struct dl
*dl
, struct nlattr
**tb_health
)
6169 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6170 enum devlink_health_reporter_state state
;
6171 const struct nlattr
*attr
;
6175 err
= mnl_attr_parse_nested(tb_health
[DEVLINK_ATTR_HEALTH_REPORTER
],
6177 if (err
!= MNL_CB_OK
)
6180 if (!tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
] ||
6181 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
] ||
6182 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
] ||
6183 !tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
])
6186 pr_out_handle_start_arr(dl
, tb_health
);
6188 pr_out_str(dl
, "name",
6189 mnl_attr_get_str(tb
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]));
6190 if (!dl
->json_output
) {
6192 __pr_out_indent_inc();
6194 state
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_STATE
]);
6195 pr_out_str(dl
, "state", health_state_name(state
));
6196 pr_out_u64(dl
, "error",
6197 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
]));
6198 pr_out_u64(dl
, "recover",
6199 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
]));
6200 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
]) {
6201 char dump_date
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6202 char dump_time
[HEALTH_REPORTER_TIMESTAMP_FMT_LEN
];
6204 attr
= tb
[DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
];
6205 time_ms
= mnl_attr_get_u64(attr
);
6206 format_logtime(time_ms
, dump_date
, dump_time
);
6208 pr_out_str(dl
, "last_dump_date", dump_date
);
6209 pr_out_str(dl
, "last_dump_time", dump_time
);
6211 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6212 pr_out_u64(dl
, "grace_period",
6213 mnl_attr_get_u64(tb
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]));
6214 if (tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6215 pr_out_bool(dl
, "auto_recover",
6216 mnl_attr_get_u8(tb
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]));
6218 __pr_out_indent_dec();
6219 pr_out_handle_end(dl
);
6222 static int cmd_health_show_cb(const struct nlmsghdr
*nlh
, void *data
)
6224 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
6225 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
6226 struct dl
*dl
= data
;
6228 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
6229 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
6230 !tb
[DEVLINK_ATTR_HEALTH_REPORTER
])
6231 return MNL_CB_ERROR
;
6233 pr_out_health(dl
, tb
);
6238 static int cmd_health_show(struct dl
*dl
)
6240 struct nlmsghdr
*nlh
;
6241 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
6244 if (dl_argc(dl
) == 0)
6245 flags
|= NLM_F_DUMP
;
6246 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_HEALTH_REPORTER_GET
,
6249 if (dl_argc(dl
) > 0) {
6250 err
= dl_argv_parse_put(nlh
, dl
,
6252 DL_OPT_HEALTH_REPORTER_NAME
, 0);
6256 pr_out_section_start(dl
, "health");
6258 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_health_show_cb
, dl
);
6259 pr_out_section_end(dl
);
6263 static void cmd_health_help(void)
6265 pr_err("Usage: devlink health show [ dev DEV reporter REPORTER_NAME ]\n");
6266 pr_err(" devlink health recover DEV reporter REPORTER_NAME\n");
6267 pr_err(" devlink health diagnose DEV reporter REPORTER_NAME\n");
6268 pr_err(" devlink health dump show DEV reporter REPORTER_NAME\n");
6269 pr_err(" devlink health dump clear DEV reporter REPORTER_NAME\n");
6270 pr_err(" devlink health set DEV reporter REPORTER_NAME { grace_period | auto_recover } { msec | boolean }\n");
6273 static int cmd_health(struct dl
*dl
)
6275 if (dl_argv_match(dl
, "help")) {
6278 } else if (dl_argv_match(dl
, "show") ||
6279 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
6281 return cmd_health_show(dl
);
6282 } else if (dl_argv_match(dl
, "recover")) {
6284 return cmd_health_recover(dl
);
6285 } else if (dl_argv_match(dl
, "diagnose")) {
6287 return cmd_health_diagnose(dl
);
6288 } else if (dl_argv_match(dl
, "dump")) {
6290 if (dl_argv_match(dl
, "show")) {
6292 return cmd_health_dump_show(dl
);
6293 } else if (dl_argv_match(dl
, "clear")) {
6295 return cmd_health_dump_clear(dl
);
6297 } else if (dl_argv_match(dl
, "set")) {
6299 return cmd_health_set_params(dl
);
6301 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
6305 static void help(void)
6307 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
6308 " devlink [ -f[orce] ] -b[atch] filename\n"
6309 "where OBJECT := { dev | port | sb | monitor | dpipe | resource | region | health }\n"
6310 " OPTIONS := { -V[ersion] | -n[o-nice-names] | -j[son] | -p[retty] | -v[erbose] }\n");
6313 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
6318 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
6321 } else if (dl_argv_match(dl
, "dev")) {
6324 } else if (dl_argv_match(dl
, "port")) {
6326 return cmd_port(dl
);
6327 } else if (dl_argv_match(dl
, "sb")) {
6330 } else if (dl_argv_match(dl
, "monitor")) {
6333 } else if (dl_argv_match(dl
, "dpipe")) {
6335 return cmd_dpipe(dl
);
6336 } else if (dl_argv_match(dl
, "resource")) {
6338 return cmd_resource(dl
);
6339 } else if (dl_argv_match(dl
, "region")) {
6341 return cmd_region(dl
);
6342 } else if (dl_argv_match(dl
, "health")) {
6344 return cmd_health(dl
);
6346 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
6350 static int dl_init(struct dl
*dl
)
6354 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
6356 pr_err("Failed to connect to devlink Netlink\n");
6360 err
= ifname_map_init(dl
);
6362 pr_err("Failed to create index map\n");
6363 goto err_ifname_map_create
;
6365 if (dl
->json_output
) {
6366 dl
->jw
= jsonw_new(stdout
);
6368 pr_err("Failed to create JSON writer\n");
6371 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
6376 ifname_map_fini(dl
);
6377 err_ifname_map_create
:
6378 mnlg_socket_close(dl
->nlg
);
6382 static void dl_fini(struct dl
*dl
)
6384 if (dl
->json_output
)
6385 jsonw_destroy(&dl
->jw
);
6386 ifname_map_fini(dl
);
6387 mnlg_socket_close(dl
->nlg
);
6390 static struct dl
*dl_alloc(void)
6394 dl
= calloc(1, sizeof(*dl
));
6400 static void dl_free(struct dl
*dl
)
6405 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
6409 int ret
= EXIT_SUCCESS
;
6411 if (name
&& strcmp(name
, "-") != 0) {
6412 if (freopen(name
, "r", stdin
) == NULL
) {
6414 "Cannot open file \"%s\" for reading: %s\n",
6415 name
, strerror(errno
));
6416 return EXIT_FAILURE
;
6421 while (getcmdline(&line
, &len
, stdin
) != -1) {
6425 largc
= makeargs(line
, largv
, 100);
6427 continue; /* blank line */
6429 if (dl_cmd(dl
, largc
, largv
)) {
6430 fprintf(stderr
, "Command failed %s:%d\n",
6444 int main(int argc
, char **argv
)
6446 static const struct option long_options
[] = {
6447 { "Version", no_argument
, NULL
, 'V' },
6448 { "force", no_argument
, NULL
, 'f' },
6449 { "batch", required_argument
, NULL
, 'b' },
6450 { "no-nice-names", no_argument
, NULL
, 'n' },
6451 { "json", no_argument
, NULL
, 'j' },
6452 { "pretty", no_argument
, NULL
, 'p' },
6453 { "verbose", no_argument
, NULL
, 'v' },
6454 { NULL
, 0, NULL
, 0 }
6456 const char *batch_file
= NULL
;
6465 pr_err("Failed to allocate memory for devlink\n");
6466 return EXIT_FAILURE
;
6469 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
6470 long_options
, NULL
)) >= 0) {
6474 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
6481 batch_file
= optarg
;
6484 dl
->no_nice_names
= true;
6487 dl
->json_output
= true;
6490 dl
->pretty_output
= true;
6496 pr_err("Unknown option.\n");
6513 err
= dl_batch(dl
, batch_file
, force
);
6515 err
= dl_cmd(dl
, argc
, argv
);