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>
20 #include <linux/genetlink.h>
21 #include <linux/devlink.h>
22 #include <libmnl/libmnl.h>
23 #include <netinet/ether.h>
28 #include "json_writer.h"
31 #define ESWITCH_MODE_LEGACY "legacy"
32 #define ESWITCH_MODE_SWITCHDEV "switchdev"
33 #define ESWITCH_INLINE_MODE_NONE "none"
34 #define ESWITCH_INLINE_MODE_LINK "link"
35 #define ESWITCH_INLINE_MODE_NETWORK "network"
36 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
38 static int g_new_line_count
;
40 #define pr_err(args...) fprintf(stderr, ##args)
41 #define pr_out(args...) \
43 if (g_indent_newline) { \
44 fprintf(stdout, "%s", g_indent_str); \
45 g_indent_newline = false; \
47 fprintf(stdout, ##args); \
48 g_new_line_count = 0; \
51 #define pr_out_sp(num, args...) \
53 int ret = fprintf(stdout, ##args); \
55 fprintf(stdout, "%*s", num - ret, ""); \
56 g_new_line_count = 0; \
59 static int g_indent_level
;
60 static bool g_indent_newline
;
61 #define INDENT_STR_STEP 2
62 #define INDENT_STR_MAXLEN 32
63 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
65 static void __pr_out_indent_inc(void)
67 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
69 g_indent_level
+= INDENT_STR_STEP
;
70 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
71 g_indent_str
[g_indent_level
] = '\0';
74 static void __pr_out_indent_dec(void)
76 if (g_indent_level
- INDENT_STR_STEP
< 0)
78 g_indent_level
-= INDENT_STR_STEP
;
79 g_indent_str
[g_indent_level
] = '\0';
82 static void __pr_out_newline(void)
84 if (g_new_line_count
< 1) {
86 g_indent_newline
= true;
91 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
92 mnl_cb_t data_cb
, void *data
)
96 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
98 pr_err("devlink answers: %s\n", strerror(errno
));
104 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
105 const struct nlmsghdr
*nlh
,
106 mnl_cb_t data_cb
, void *data
)
110 err
= mnlg_socket_send(nlg
, nlh
);
112 pr_err("Failed to call mnlg_socket_send\n");
115 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
118 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
119 const char *group_name
)
123 err
= mnlg_socket_group_add(nlg
, group_name
);
125 pr_err("Failed to call mnlg_socket_group_add\n");
132 struct list_head list
;
139 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
140 const char *dev_name
,
144 struct ifname_map
*ifname_map
;
146 ifname_map
= calloc(1, sizeof(*ifname_map
));
149 ifname_map
->bus_name
= strdup(bus_name
);
150 ifname_map
->dev_name
= strdup(dev_name
);
151 ifname_map
->port_index
= port_index
;
152 ifname_map
->ifname
= strdup(ifname
);
153 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
154 !ifname_map
->ifname
) {
155 free(ifname_map
->ifname
);
156 free(ifname_map
->dev_name
);
157 free(ifname_map
->bus_name
);
164 static void ifname_map_free(struct ifname_map
*ifname_map
)
166 free(ifname_map
->ifname
);
167 free(ifname_map
->dev_name
);
168 free(ifname_map
->bus_name
);
172 #define DL_OPT_HANDLE BIT(0)
173 #define DL_OPT_HANDLEP BIT(1)
174 #define DL_OPT_PORT_TYPE BIT(2)
175 #define DL_OPT_PORT_COUNT BIT(3)
176 #define DL_OPT_SB BIT(4)
177 #define DL_OPT_SB_POOL BIT(5)
178 #define DL_OPT_SB_SIZE BIT(6)
179 #define DL_OPT_SB_TYPE BIT(7)
180 #define DL_OPT_SB_THTYPE BIT(8)
181 #define DL_OPT_SB_TH BIT(9)
182 #define DL_OPT_SB_TC BIT(10)
183 #define DL_OPT_ESWITCH_MODE BIT(11)
184 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
185 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
186 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
187 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
188 #define DL_OPT_RESOURCE_PATH BIT(16)
189 #define DL_OPT_RESOURCE_SIZE BIT(17)
192 uint32_t present
; /* flags of present items */
196 enum devlink_port_type port_type
;
199 uint16_t sb_pool_index
;
200 uint32_t sb_pool_size
;
201 enum devlink_sb_pool_type sb_pool_type
;
202 enum devlink_sb_threshold_type sb_pool_thtype
;
203 uint32_t sb_threshold
;
204 uint16_t sb_tc_index
;
205 enum devlink_eswitch_mode eswitch_mode
;
206 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
207 const char *dpipe_table_name
;
208 bool dpipe_counters_enable
;
209 bool eswitch_encap_mode
;
210 const char *resource_path
;
211 uint32_t resource_size
;
212 uint32_t resource_id
;
213 bool resource_id_valid
;
217 struct mnlg_socket
*nlg
;
218 struct list_head ifname_map_list
;
235 static int dl_argc(struct dl
*dl
)
240 static char *dl_argv(struct dl
*dl
)
242 if (dl_argc(dl
) == 0)
247 static void dl_arg_inc(struct dl
*dl
)
249 if (dl_argc(dl
) == 0)
255 static char *dl_argv_next(struct dl
*dl
)
259 if (dl_argc(dl
) == 0)
267 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
269 if (index
>= dl_argc(dl
))
271 return dl
->argv
[index
];
274 static int strcmpx(const char *str1
, const char *str2
)
276 if (strlen(str1
) > strlen(str2
))
278 return strncmp(str1
, str2
, strlen(str1
));
281 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
283 if (dl_argc(dl
) == 0)
285 return strcmpx(dl_argv(dl
), pattern
) == 0;
288 static bool dl_no_arg(struct dl
*dl
)
290 return dl_argc(dl
) == 0;
293 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
294 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
295 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
296 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
297 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
298 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
299 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
300 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
301 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
302 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
303 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
304 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
305 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
306 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
307 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
308 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
309 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
310 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
311 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
312 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
313 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
314 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
315 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
316 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
317 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
318 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
319 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
320 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
321 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
322 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
323 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
324 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
325 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
326 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
327 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
328 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
329 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
330 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
331 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
332 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
333 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
334 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
335 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
336 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
337 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
338 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
339 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
340 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
341 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
342 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
343 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
344 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
345 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
346 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
347 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
348 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
349 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
350 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
353 static int attr_cb(const struct nlattr
*attr
, void *data
)
355 const struct nlattr
**tb
= data
;
358 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
361 type
= mnl_attr_get_type(attr
);
362 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
369 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
371 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
372 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
373 struct dl
*dl
= data
;
374 struct ifname_map
*ifname_map
;
375 const char *bus_name
;
376 const char *dev_name
;
377 uint32_t port_ifindex
;
378 const char *port_ifname
;
380 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
381 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
382 !tb
[DEVLINK_ATTR_PORT_INDEX
])
385 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
388 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
389 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
390 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
391 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
392 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
393 port_ifindex
, port_ifname
);
396 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
401 static void ifname_map_fini(struct dl
*dl
)
403 struct ifname_map
*ifname_map
, *tmp
;
405 list_for_each_entry_safe(ifname_map
, tmp
,
406 &dl
->ifname_map_list
, list
) {
407 list_del(&ifname_map
->list
);
408 ifname_map_free(ifname_map
);
412 static int ifname_map_init(struct dl
*dl
)
414 struct nlmsghdr
*nlh
;
417 INIT_LIST_HEAD(&dl
->ifname_map_list
);
419 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
420 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
422 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
430 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
431 char **p_bus_name
, char **p_dev_name
,
432 uint32_t *p_port_index
)
434 struct ifname_map
*ifname_map
;
436 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
437 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
438 *p_bus_name
= ifname_map
->bus_name
;
439 *p_dev_name
= ifname_map
->dev_name
;
440 *p_port_index
= ifname_map
->port_index
;
447 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
448 const char *dev_name
, uint32_t port_index
,
451 struct ifname_map
*ifname_map
;
453 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
454 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
455 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
456 port_index
== ifname_map
->port_index
) {
457 *p_ifname
= ifname_map
->ifname
;
464 static unsigned int strslashcount(char *str
)
466 unsigned int count
= 0;
469 while ((pos
= strchr(pos
, '/'))) {
476 static int strslashrsplit(char *str
, char **before
, char **after
)
480 slash
= strrchr(str
, '/');
489 static int strtouint32_t(const char *str
, uint32_t *p_val
)
492 unsigned long int val
;
494 val
= strtoul(str
, &endptr
, 10);
495 if (endptr
== str
|| *endptr
!= '\0')
503 static int strtouint16_t(const char *str
, uint16_t *p_val
)
506 unsigned long int val
;
508 val
= strtoul(str
, &endptr
, 10);
509 if (endptr
== str
|| *endptr
!= '\0')
517 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
519 strslashrsplit(str
, p_bus_name
, p_dev_name
);
523 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
525 char *str
= dl_argv_next(dl
);
528 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
531 if (strslashcount(str
) != 1) {
532 pr_err("Wrong devlink identification string format.\n");
533 pr_err("Expected \"bus_name/dev_name\".\n");
536 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
539 static int __dl_argv_handle_port(char *str
,
540 char **p_bus_name
, char **p_dev_name
,
541 uint32_t *p_port_index
)
547 err
= strslashrsplit(str
, &handlestr
, &portstr
);
549 pr_err("Port identification \"%s\" is invalid\n", str
);
552 err
= strtouint32_t(portstr
, p_port_index
);
554 pr_err("Port index \"%s\" is not a number or not within range\n",
558 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
560 pr_err("Port identification \"%s\" is invalid\n", str
);
566 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
567 char **p_bus_name
, char **p_dev_name
,
568 uint32_t *p_port_index
)
572 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
575 pr_err("Netdevice \"%s\" not found\n", str
);
581 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
582 char **p_dev_name
, uint32_t *p_port_index
)
584 char *str
= dl_argv_next(dl
);
585 unsigned int slash_count
;
588 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
591 slash_count
= strslashcount(str
);
592 switch (slash_count
) {
594 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
595 p_dev_name
, p_port_index
);
597 return __dl_argv_handle_port(str
, p_bus_name
,
598 p_dev_name
, p_port_index
);
600 pr_err("Wrong port identification string format.\n");
601 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
606 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
607 char **p_dev_name
, uint32_t *p_port_index
,
608 uint32_t *p_handle_bit
)
610 char *str
= dl_argv_next(dl
);
611 unsigned int slash_count
;
615 pr_err("One of following identifications expected:\n"
616 "Devlink identification (\"bus_name/dev_name\")\n"
617 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
620 slash_count
= strslashcount(str
);
621 if (slash_count
== 1) {
622 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
625 *p_handle_bit
= DL_OPT_HANDLE
;
626 } else if (slash_count
== 2) {
627 err
= __dl_argv_handle_port(str
, p_bus_name
,
628 p_dev_name
, p_port_index
);
631 *p_handle_bit
= DL_OPT_HANDLEP
;
632 } else if (slash_count
== 0) {
633 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
634 p_dev_name
, p_port_index
);
637 *p_handle_bit
= DL_OPT_HANDLEP
;
639 pr_err("Wrong port identification string format.\n");
640 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
646 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
648 char *str
= dl_argv_next(dl
);
652 pr_err("Unsigned number argument expected\n");
656 err
= strtouint32_t(str
, p_val
);
658 pr_err("\"%s\" is not a number or not within range\n", str
);
664 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
666 char *str
= dl_argv_next(dl
);
670 pr_err("Unsigned number argument expected\n");
674 err
= strtouint16_t(str
, p_val
);
676 pr_err("\"%s\" is not a number or not within range\n", str
);
682 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
684 const char *str
= dl_argv_next(dl
);
687 pr_err("String parameter expected\n");
694 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
696 if (strcmp(typestr
, "auto") == 0) {
697 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
698 } else if (strcmp(typestr
, "eth") == 0) {
699 *p_type
= DEVLINK_PORT_TYPE_ETH
;
700 } else if (strcmp(typestr
, "ib") == 0) {
701 *p_type
= DEVLINK_PORT_TYPE_IB
;
703 pr_err("Unknown port type \"%s\"\n", typestr
);
709 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
711 if (strcmp(typestr
, "ingress") == 0) {
712 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
713 } else if (strcmp(typestr
, "egress") == 0) {
714 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
716 pr_err("Unknown pool type \"%s\"\n", typestr
);
722 static int threshold_type_get(const char *typestr
,
723 enum devlink_sb_threshold_type
*p_type
)
725 if (strcmp(typestr
, "static") == 0) {
726 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
727 } else if (strcmp(typestr
, "dynamic") == 0) {
728 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
730 pr_err("Unknown threshold type \"%s\"\n", typestr
);
736 static int eswitch_mode_get(const char *typestr
,
737 enum devlink_eswitch_mode
*p_mode
)
739 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
740 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
741 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
742 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
744 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
750 static int eswitch_inline_mode_get(const char *typestr
,
751 enum devlink_eswitch_inline_mode
*p_mode
)
753 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
754 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
755 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
756 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
757 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
758 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
759 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
760 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
762 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
768 static int dpipe_counters_enable_get(const char *typestr
,
769 bool *counters_enable
)
771 if (strcmp(typestr
, "enable") == 0) {
772 *counters_enable
= 1;
773 } else if (strcmp(typestr
, "disable") == 0) {
774 *counters_enable
= 0;
776 pr_err("Unknown counter_state \"%s\"\n", typestr
);
782 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
784 if (strcmp(typestr
, "enable") == 0) {
786 } else if (strcmp(typestr
, "disable") == 0) {
789 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
795 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
798 struct dl_opts
*opts
= &dl
->opts
;
799 uint32_t o_all
= o_required
| o_optional
;
800 uint32_t o_found
= 0;
803 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
806 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
807 &opts
->port_index
, &handle_bit
);
810 o_found
|= handle_bit
;
811 } else if (o_required
& DL_OPT_HANDLE
) {
812 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
815 o_found
|= DL_OPT_HANDLE
;
816 } else if (o_required
& DL_OPT_HANDLEP
) {
817 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
821 o_found
|= DL_OPT_HANDLEP
;
824 while (dl_argc(dl
)) {
825 if (dl_argv_match(dl
, "type") &&
826 (o_all
& DL_OPT_PORT_TYPE
)) {
830 err
= dl_argv_str(dl
, &typestr
);
833 err
= port_type_get(typestr
, &opts
->port_type
);
836 o_found
|= DL_OPT_PORT_TYPE
;
837 } else if (dl_argv_match(dl
, "count") &&
838 (o_all
& DL_OPT_PORT_COUNT
)) {
840 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
843 o_found
|= DL_OPT_PORT_COUNT
;
844 } else if (dl_argv_match(dl
, "sb") &&
845 (o_all
& DL_OPT_SB
)) {
847 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
850 o_found
|= DL_OPT_SB
;
851 } else if (dl_argv_match(dl
, "pool") &&
852 (o_all
& DL_OPT_SB_POOL
)) {
854 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
857 o_found
|= DL_OPT_SB_POOL
;
858 } else if (dl_argv_match(dl
, "size") &&
859 (o_all
& DL_OPT_SB_SIZE
)) {
861 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
864 o_found
|= DL_OPT_SB_SIZE
;
865 } else if (dl_argv_match(dl
, "type") &&
866 (o_all
& DL_OPT_SB_TYPE
)) {
870 err
= dl_argv_str(dl
, &typestr
);
873 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
876 o_found
|= DL_OPT_SB_TYPE
;
877 } else if (dl_argv_match(dl
, "thtype") &&
878 (o_all
& DL_OPT_SB_THTYPE
)) {
882 err
= dl_argv_str(dl
, &typestr
);
885 err
= threshold_type_get(typestr
,
886 &opts
->sb_pool_thtype
);
889 o_found
|= DL_OPT_SB_THTYPE
;
890 } else if (dl_argv_match(dl
, "th") &&
891 (o_all
& DL_OPT_SB_TH
)) {
893 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
896 o_found
|= DL_OPT_SB_TH
;
897 } else if (dl_argv_match(dl
, "tc") &&
898 (o_all
& DL_OPT_SB_TC
)) {
900 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
903 o_found
|= DL_OPT_SB_TC
;
904 } else if (dl_argv_match(dl
, "mode") &&
905 (o_all
& DL_OPT_ESWITCH_MODE
)) {
909 err
= dl_argv_str(dl
, &typestr
);
912 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
915 o_found
|= DL_OPT_ESWITCH_MODE
;
916 } else if (dl_argv_match(dl
, "inline-mode") &&
917 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
921 err
= dl_argv_str(dl
, &typestr
);
924 err
= eswitch_inline_mode_get(
925 typestr
, &opts
->eswitch_inline_mode
);
928 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
929 } else if (dl_argv_match(dl
, "name") &&
930 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
932 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
935 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
936 } else if (dl_argv_match(dl
, "counters") &&
937 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
941 err
= dl_argv_str(dl
, &typestr
);
944 err
= dpipe_counters_enable_get(typestr
,
945 &opts
->dpipe_counters_enable
);
948 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
949 } else if (dl_argv_match(dl
, "encap") &&
950 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
954 err
= dl_argv_str(dl
, &typestr
);
957 err
= eswitch_encap_mode_get(typestr
,
958 &opts
->eswitch_encap_mode
);
961 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
962 } else if (dl_argv_match(dl
, "path") &&
963 (o_all
& DL_OPT_RESOURCE_PATH
)) {
965 err
= dl_argv_str(dl
, &opts
->resource_path
);
968 o_found
|= DL_OPT_RESOURCE_PATH
;
969 } else if (dl_argv_match(dl
, "size") &&
970 (o_all
& DL_OPT_RESOURCE_SIZE
)) {
972 err
= dl_argv_uint32_t(dl
, &opts
->resource_size
);
975 o_found
|= DL_OPT_RESOURCE_SIZE
;
977 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
982 opts
->present
= o_found
;
984 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
986 opts
->present
|= DL_OPT_SB
;
989 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
990 pr_err("Port type option expected.\n");
994 if ((o_required
& DL_OPT_PORT_COUNT
) &&
995 !(o_found
& DL_OPT_PORT_COUNT
)) {
996 pr_err("Port split count option expected.\n");
1000 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
1001 pr_err("Pool index option expected.\n");
1005 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
1006 pr_err("Pool size option expected.\n");
1010 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
1011 pr_err("Pool type option expected.\n");
1015 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
1016 pr_err("Pool threshold type option expected.\n");
1020 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
1021 pr_err("Threshold option expected.\n");
1025 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
1026 pr_err("TC index option expected.\n");
1030 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
1031 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
1032 pr_err("E-Switch mode option expected.\n");
1036 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
1037 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1038 pr_err("E-Switch inline-mode option expected.\n");
1042 if ((o_required
& DL_OPT_DPIPE_TABLE_NAME
) &&
1043 !(o_found
& DL_OPT_DPIPE_TABLE_NAME
)) {
1044 pr_err("Dpipe table name expected\n");
1048 if ((o_required
& DL_OPT_DPIPE_TABLE_COUNTERS
) &&
1049 !(o_found
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1050 pr_err("Dpipe table counter state expected\n");
1054 if ((o_required
& DL_OPT_ESWITCH_ENCAP_MODE
) &&
1055 !(o_found
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1056 pr_err("E-Switch encapsulation option expected.\n");
1063 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1065 struct dl_opts
*opts
= &dl
->opts
;
1067 if (opts
->present
& DL_OPT_HANDLE
) {
1068 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1069 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1070 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1071 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1072 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1073 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1076 if (opts
->present
& DL_OPT_PORT_TYPE
)
1077 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1079 if (opts
->present
& DL_OPT_PORT_COUNT
)
1080 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1082 if (opts
->present
& DL_OPT_SB
)
1083 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1085 if (opts
->present
& DL_OPT_SB_POOL
)
1086 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1087 opts
->sb_pool_index
);
1088 if (opts
->present
& DL_OPT_SB_SIZE
)
1089 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1090 opts
->sb_pool_size
);
1091 if (opts
->present
& DL_OPT_SB_TYPE
)
1092 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1093 opts
->sb_pool_type
);
1094 if (opts
->present
& DL_OPT_SB_THTYPE
)
1095 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1096 opts
->sb_pool_thtype
);
1097 if (opts
->present
& DL_OPT_SB_TH
)
1098 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1099 opts
->sb_threshold
);
1100 if (opts
->present
& DL_OPT_SB_TC
)
1101 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1103 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1104 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1105 opts
->eswitch_mode
);
1106 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1107 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1108 opts
->eswitch_inline_mode
);
1109 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1110 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1111 opts
->dpipe_table_name
);
1112 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1113 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1114 opts
->dpipe_counters_enable
);
1115 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1116 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1117 opts
->eswitch_encap_mode
);
1118 if ((opts
->present
& DL_OPT_RESOURCE_PATH
) && opts
->resource_id_valid
)
1119 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_ID
,
1121 if (opts
->present
& DL_OPT_RESOURCE_SIZE
)
1122 mnl_attr_put_u64(nlh
, DEVLINK_ATTR_RESOURCE_SIZE
,
1123 opts
->resource_size
);
1126 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1127 uint32_t o_required
, uint32_t o_optional
)
1131 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1134 dl_opts_put(nlh
, dl
);
1138 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1140 struct dl_opts
*opts
= &dl
->opts
;
1141 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1142 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1143 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1144 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1146 if (opts
->present
& DL_OPT_HANDLE
&&
1147 attr_bus_name
&& attr_dev_name
) {
1148 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1149 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1151 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1152 strcmp(dev_name
, opts
->dev_name
) != 0)
1155 if (opts
->present
& DL_OPT_HANDLEP
&&
1156 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1157 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1158 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1159 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1161 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1162 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1163 port_index
!= opts
->port_index
)
1166 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1167 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1169 if (sb_index
!= opts
->sb_index
)
1175 static void cmd_dev_help(void)
1177 pr_err("Usage: devlink dev show [ DEV ]\n");
1178 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1179 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1180 pr_err(" [ encap { disable | enable } ]\n");
1181 pr_err(" devlink dev eswitch show DEV\n");
1182 pr_err(" devlink dev reload DEV\n");
1185 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1186 const char *dev_name
)
1188 if (!dl
->arr_last
.present
)
1190 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1191 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1194 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1195 const char *dev_name
)
1197 dl
->arr_last
.present
= true;
1198 free(dl
->arr_last
.dev_name
);
1199 free(dl
->arr_last
.bus_name
);
1200 dl
->arr_last
.bus_name
= strdup(bus_name
);
1201 dl
->arr_last
.dev_name
= strdup(dev_name
);
1204 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1205 const char *dev_name
)
1207 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1210 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1211 const char *dev_name
)
1213 return dl
->arr_last
.present
&&
1214 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1217 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1218 bool content
, bool array
)
1220 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1221 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1224 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1226 if (dl
->json_output
) {
1228 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1229 jsonw_end_array(dl
->jw
);
1230 if (should_arr_last_handle_start(dl
, bus_name
,
1232 jsonw_name(dl
->jw
, buf
);
1233 jsonw_start_array(dl
->jw
);
1234 jsonw_start_object(dl
->jw
);
1235 arr_last_handle_set(dl
, bus_name
, dev_name
);
1237 jsonw_start_object(dl
->jw
);
1240 jsonw_name(dl
->jw
, buf
);
1241 jsonw_start_object(dl
->jw
);
1245 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1246 __pr_out_indent_dec();
1247 if (should_arr_last_handle_start(dl
, bus_name
,
1249 pr_out("%s%s", buf
, content
? ":" : "");
1251 __pr_out_indent_inc();
1252 arr_last_handle_set(dl
, bus_name
, dev_name
);
1255 pr_out("%s%s", buf
, content
? ":" : "");
1260 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1262 __pr_out_handle_start(dl
, tb
, true, true);
1265 static void pr_out_handle_end(struct dl
*dl
)
1267 if (dl
->json_output
)
1268 jsonw_end_object(dl
->jw
);
1273 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1275 __pr_out_handle_start(dl
, tb
, false, false);
1276 pr_out_handle_end(dl
);
1279 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1280 const char *dev_name
, uint32_t port_index
)
1282 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1283 dl
->arr_last
.port_index
== port_index
;
1286 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1287 const char *dev_name
, uint32_t port_index
)
1289 arr_last_handle_set(dl
, bus_name
, dev_name
);
1290 dl
->arr_last
.port_index
= port_index
;
1293 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1294 const char *bus_name
,
1295 const char *dev_name
,
1296 uint32_t port_index
)
1298 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1301 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1302 const char *bus_name
,
1303 const char *dev_name
,
1304 uint32_t port_index
)
1306 return dl
->arr_last
.present
&&
1307 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1310 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1311 const char *dev_name
,
1312 uint32_t port_index
, bool try_nice
,
1315 static char buf
[32];
1316 char *ifname
= NULL
;
1318 if (dl
->no_nice_names
|| !try_nice
||
1319 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1320 port_index
, &ifname
) != 0)
1321 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1323 sprintf(buf
, "%s", ifname
);
1325 if (dl
->json_output
) {
1327 if (should_arr_last_port_handle_end(dl
, bus_name
,
1330 jsonw_end_array(dl
->jw
);
1331 if (should_arr_last_port_handle_start(dl
, bus_name
,
1334 jsonw_name(dl
->jw
, buf
);
1335 jsonw_start_array(dl
->jw
);
1336 jsonw_start_object(dl
->jw
);
1337 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1340 jsonw_start_object(dl
->jw
);
1343 jsonw_name(dl
->jw
, buf
);
1344 jsonw_start_object(dl
->jw
);
1351 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1353 const char *bus_name
;
1354 const char *dev_name
;
1355 uint32_t port_index
;
1357 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1358 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1359 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1360 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1363 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1365 const char *bus_name
;
1366 const char *dev_name
;
1367 uint32_t port_index
;
1369 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1370 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1371 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1372 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1375 static void pr_out_port_handle_end(struct dl
*dl
)
1377 if (dl
->json_output
)
1378 jsonw_end_object(dl
->jw
);
1384 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1386 if (dl
->json_output
) {
1387 jsonw_string_field(dl
->jw
, name
, val
);
1389 if (g_indent_newline
)
1390 pr_out("%s %s", name
, val
);
1392 pr_out(" %s %s", name
, val
);
1396 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1398 if (dl
->json_output
) {
1399 jsonw_uint_field(dl
->jw
, name
, val
);
1401 if (g_indent_newline
)
1402 pr_out("%s %u", name
, val
);
1404 pr_out(" %s %u", name
, val
);
1408 static void pr_out_u64(struct dl
*dl
, const char *name
, uint64_t val
)
1410 if (val
== (uint64_t) -1)
1411 return pr_out_str(dl
, name
, "unlimited");
1413 return pr_out_uint(dl
, name
, val
);
1416 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1418 pr_out_handle(dl
, tb
);
1421 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1423 if (dl
->json_output
) {
1424 jsonw_start_object(dl
->jw
);
1425 jsonw_name(dl
->jw
, name
);
1426 jsonw_start_object(dl
->jw
);
1430 static void pr_out_section_end(struct dl
*dl
)
1432 if (dl
->json_output
) {
1433 if (dl
->arr_last
.present
)
1434 jsonw_end_array(dl
->jw
);
1435 jsonw_end_object(dl
->jw
);
1436 jsonw_end_object(dl
->jw
);
1440 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1442 if (dl
->json_output
) {
1443 jsonw_name(dl
->jw
, name
);
1444 jsonw_start_array(dl
->jw
);
1446 __pr_out_indent_inc();
1448 pr_out("%s:", name
);
1449 __pr_out_indent_inc();
1454 static void pr_out_array_end(struct dl
*dl
)
1456 if (dl
->json_output
) {
1457 jsonw_end_array(dl
->jw
);
1459 __pr_out_indent_dec();
1460 __pr_out_indent_dec();
1464 static void pr_out_entry_start(struct dl
*dl
)
1466 if (dl
->json_output
)
1467 jsonw_start_object(dl
->jw
);
1470 static void pr_out_entry_end(struct dl
*dl
)
1472 if (dl
->json_output
)
1473 jsonw_end_object(dl
->jw
);
1478 static const char *eswitch_mode_name(uint32_t mode
)
1481 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1482 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1483 default: return "<unknown mode>";
1487 static const char *eswitch_inline_mode_name(uint32_t mode
)
1490 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1491 return ESWITCH_INLINE_MODE_NONE
;
1492 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1493 return ESWITCH_INLINE_MODE_LINK
;
1494 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1495 return ESWITCH_INLINE_MODE_NETWORK
;
1496 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1497 return ESWITCH_INLINE_MODE_TRANSPORT
;
1499 return "<unknown mode>";
1503 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1505 __pr_out_handle_start(dl
, tb
, true, false);
1507 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1508 pr_out_str(dl
, "mode",
1509 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1511 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1512 pr_out_str(dl
, "inline-mode",
1513 eswitch_inline_mode_name(mnl_attr_get_u8(
1514 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1516 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1517 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1519 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1522 pr_out_handle_end(dl
);
1525 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1527 struct dl
*dl
= data
;
1528 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1529 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1531 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1532 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1533 return MNL_CB_ERROR
;
1534 pr_out_eswitch(dl
, tb
);
1538 static int cmd_dev_eswitch_show(struct dl
*dl
)
1540 struct nlmsghdr
*nlh
;
1543 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1544 NLM_F_REQUEST
| NLM_F_ACK
);
1546 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1550 pr_out_section_start(dl
, "dev");
1551 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1552 pr_out_section_end(dl
);
1556 static int cmd_dev_eswitch_set(struct dl
*dl
)
1558 struct nlmsghdr
*nlh
;
1561 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1562 NLM_F_REQUEST
| NLM_F_ACK
);
1564 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1565 DL_OPT_ESWITCH_MODE
|
1566 DL_OPT_ESWITCH_INLINE_MODE
|
1567 DL_OPT_ESWITCH_ENCAP_MODE
);
1572 if (dl
->opts
.present
== 1) {
1573 pr_err("Need to set at least one option\n");
1577 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1580 static int cmd_dev_eswitch(struct dl
*dl
)
1582 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1585 } else if (dl_argv_match(dl
, "set")) {
1587 return cmd_dev_eswitch_set(dl
);
1588 } else if (dl_argv_match(dl
, "show")) {
1590 return cmd_dev_eswitch_show(dl
);
1592 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1596 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1598 struct dl
*dl
= data
;
1599 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1600 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1602 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1603 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1604 return MNL_CB_ERROR
;
1609 static int cmd_dev_show(struct dl
*dl
)
1611 struct nlmsghdr
*nlh
;
1612 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1615 if (dl_argc(dl
) == 0)
1616 flags
|= NLM_F_DUMP
;
1618 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
1620 if (dl_argc(dl
) > 0) {
1621 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1626 pr_out_section_start(dl
, "dev");
1627 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
1628 pr_out_section_end(dl
);
1632 static void cmd_dev_reload_help(void)
1634 pr_err("Usage: devlink dev reload [ DEV ]\n");
1637 static int cmd_dev_reload(struct dl
*dl
)
1639 struct nlmsghdr
*nlh
;
1642 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1643 cmd_dev_reload_help();
1647 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RELOAD
,
1648 NLM_F_REQUEST
| NLM_F_ACK
);
1650 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1654 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1657 static int cmd_dev(struct dl
*dl
)
1659 if (dl_argv_match(dl
, "help")) {
1662 } else if (dl_argv_match(dl
, "show") ||
1663 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1665 return cmd_dev_show(dl
);
1666 } else if (dl_argv_match(dl
, "eswitch")) {
1668 return cmd_dev_eswitch(dl
);
1669 } else if (dl_argv_match(dl
, "reload")) {
1671 return cmd_dev_reload(dl
);
1673 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1677 static void cmd_port_help(void)
1679 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
1680 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
1681 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
1682 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
1685 static const char *port_type_name(uint32_t type
)
1688 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
1689 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
1690 case DEVLINK_PORT_TYPE_ETH
: return "eth";
1691 case DEVLINK_PORT_TYPE_IB
: return "ib";
1692 default: return "<unknown type>";
1696 static const char *port_flavour_name(uint16_t flavour
)
1699 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
1701 case DEVLINK_PORT_FLAVOUR_CPU
:
1703 case DEVLINK_PORT_FLAVOUR_DSA
:
1706 return "<unknown flavour>";
1710 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
1712 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
1713 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
1715 pr_out_port_handle_start(dl
, tb
, false);
1717 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
1719 pr_out_str(dl
, "type", port_type_name(port_type
));
1721 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
1723 if (port_type
!= des_port_type
)
1724 pr_out_str(dl
, "des_type",
1725 port_type_name(des_port_type
));
1728 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
1729 pr_out_str(dl
, "netdev",
1730 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
1731 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
1732 pr_out_str(dl
, "ibdev",
1733 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
1734 if (tb
[DEVLINK_ATTR_PORT_FLAVOUR
]) {
1735 uint16_t port_flavour
=
1736 mnl_attr_get_u16(tb
[DEVLINK_ATTR_PORT_FLAVOUR
]);
1738 pr_out_str(dl
, "flavour", port_flavour_name(port_flavour
));
1740 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
1741 pr_out_uint(dl
, "split_group",
1742 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
1743 pr_out_port_handle_end(dl
);
1746 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1748 struct dl
*dl
= data
;
1749 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1750 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1752 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1753 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1754 !tb
[DEVLINK_ATTR_PORT_INDEX
])
1755 return MNL_CB_ERROR
;
1756 pr_out_port(dl
, tb
);
1760 static int cmd_port_show(struct dl
*dl
)
1762 struct nlmsghdr
*nlh
;
1763 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1766 if (dl_argc(dl
) == 0)
1767 flags
|= NLM_F_DUMP
;
1769 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
1771 if (dl_argc(dl
) > 0) {
1772 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1777 pr_out_section_start(dl
, "port");
1778 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
1779 pr_out_section_end(dl
);
1783 static int cmd_port_set(struct dl
*dl
)
1785 struct nlmsghdr
*nlh
;
1788 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
1789 NLM_F_REQUEST
| NLM_F_ACK
);
1791 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
1795 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1798 static int cmd_port_split(struct dl
*dl
)
1800 struct nlmsghdr
*nlh
;
1803 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
1804 NLM_F_REQUEST
| NLM_F_ACK
);
1806 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
1810 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1813 static int cmd_port_unsplit(struct dl
*dl
)
1815 struct nlmsghdr
*nlh
;
1818 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
1819 NLM_F_REQUEST
| NLM_F_ACK
);
1821 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1825 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1828 static int cmd_port(struct dl
*dl
)
1830 if (dl_argv_match(dl
, "help")) {
1833 } else if (dl_argv_match(dl
, "show") ||
1834 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1836 return cmd_port_show(dl
);
1837 } else if (dl_argv_match(dl
, "set")) {
1839 return cmd_port_set(dl
);
1840 } else if (dl_argv_match(dl
, "split")) {
1842 return cmd_port_split(dl
);
1843 } else if (dl_argv_match(dl
, "unsplit")) {
1845 return cmd_port_unsplit(dl
);
1847 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1851 static void cmd_sb_help(void)
1853 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
1854 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
1855 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
1856 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
1857 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1858 pr_err(" pool POOL_INDEX ]\n");
1859 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1860 pr_err(" pool POOL_INDEX th THRESHOLD\n");
1861 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1862 pr_err(" type { ingress | egress } ]\n");
1863 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1864 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
1865 pr_err(" th THRESHOLD\n");
1866 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
1867 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
1868 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
1871 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
1873 pr_out_handle_start_arr(dl
, tb
);
1874 pr_out_uint(dl
, "sb",
1875 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1876 pr_out_uint(dl
, "size",
1877 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
1878 pr_out_uint(dl
, "ing_pools",
1879 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
1880 pr_out_uint(dl
, "eg_pools",
1881 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
1882 pr_out_uint(dl
, "ing_tcs",
1883 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
1884 pr_out_uint(dl
, "eg_tcs",
1885 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
1886 pr_out_handle_end(dl
);
1889 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1891 struct dl
*dl
= data
;
1892 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1893 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1895 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1896 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1897 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
1898 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
1899 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
1900 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
1901 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
1902 return MNL_CB_ERROR
;
1907 static int cmd_sb_show(struct dl
*dl
)
1909 struct nlmsghdr
*nlh
;
1910 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1913 if (dl_argc(dl
) == 0)
1914 flags
|= NLM_F_DUMP
;
1916 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
1918 if (dl_argc(dl
) > 0) {
1919 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
1924 pr_out_section_start(dl
, "sb");
1925 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
1926 pr_out_section_end(dl
);
1930 static const char *pool_type_name(uint8_t type
)
1933 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
1934 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
1935 default: return "<unknown type>";
1939 static const char *threshold_type_name(uint8_t type
)
1942 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
1943 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
1944 default: return "<unknown type>";
1948 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
1950 pr_out_handle_start_arr(dl
, tb
);
1951 pr_out_uint(dl
, "sb",
1952 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1953 pr_out_uint(dl
, "pool",
1954 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1955 pr_out_str(dl
, "type",
1956 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
1957 pr_out_uint(dl
, "size",
1958 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
1959 pr_out_str(dl
, "thtype",
1960 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
1961 pr_out_handle_end(dl
);
1964 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1966 struct dl
*dl
= data
;
1967 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1968 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1970 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1971 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1972 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
1973 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
1974 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
1975 return MNL_CB_ERROR
;
1976 pr_out_sb_pool(dl
, tb
);
1980 static int cmd_sb_pool_show(struct dl
*dl
)
1982 struct nlmsghdr
*nlh
;
1983 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1986 if (dl_argc(dl
) == 0)
1987 flags
|= NLM_F_DUMP
;
1989 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
1991 if (dl_argc(dl
) > 0) {
1992 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
1998 pr_out_section_start(dl
, "pool");
1999 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
2000 pr_out_section_end(dl
);
2004 static int cmd_sb_pool_set(struct dl
*dl
)
2006 struct nlmsghdr
*nlh
;
2009 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
2010 NLM_F_REQUEST
| NLM_F_ACK
);
2012 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
2013 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
2017 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2020 static int cmd_sb_pool(struct dl
*dl
)
2022 if (dl_argv_match(dl
, "help")) {
2025 } else if (dl_argv_match(dl
, "show") ||
2026 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2028 return cmd_sb_pool_show(dl
);
2029 } else if (dl_argv_match(dl
, "set")) {
2031 return cmd_sb_pool_set(dl
);
2033 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2037 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
2039 pr_out_port_handle_start_arr(dl
, tb
, true);
2040 pr_out_uint(dl
, "sb",
2041 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2042 pr_out_uint(dl
, "pool",
2043 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2044 pr_out_uint(dl
, "threshold",
2045 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2046 pr_out_port_handle_end(dl
);
2049 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2051 struct dl
*dl
= data
;
2052 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2053 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2055 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2056 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2057 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2058 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2059 return MNL_CB_ERROR
;
2060 pr_out_sb_port_pool(dl
, tb
);
2064 static int cmd_sb_port_pool_show(struct dl
*dl
)
2066 struct nlmsghdr
*nlh
;
2067 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2070 if (dl_argc(dl
) == 0)
2071 flags
|= NLM_F_DUMP
;
2073 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2075 if (dl_argc(dl
) > 0) {
2076 err
= dl_argv_parse_put(nlh
, dl
,
2077 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
2083 pr_out_section_start(dl
, "port_pool");
2084 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
2085 pr_out_section_end(dl
);
2089 static int cmd_sb_port_pool_set(struct dl
*dl
)
2091 struct nlmsghdr
*nlh
;
2094 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
2095 NLM_F_REQUEST
| NLM_F_ACK
);
2097 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
2098 DL_OPT_SB_TH
, DL_OPT_SB
);
2102 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2105 static int cmd_sb_port_pool(struct dl
*dl
)
2107 if (dl_argv_match(dl
, "help")) {
2110 } else if (dl_argv_match(dl
, "show") ||
2111 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2113 return cmd_sb_port_pool_show(dl
);
2114 } else if (dl_argv_match(dl
, "set")) {
2116 return cmd_sb_port_pool_set(dl
);
2118 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2122 static int cmd_sb_port(struct dl
*dl
)
2124 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2127 } else if (dl_argv_match(dl
, "pool")) {
2129 return cmd_sb_port_pool(dl
);
2131 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2135 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
2137 pr_out_port_handle_start_arr(dl
, tb
, true);
2138 pr_out_uint(dl
, "sb",
2139 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2140 pr_out_uint(dl
, "tc",
2141 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
2142 pr_out_str(dl
, "type",
2143 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2144 pr_out_uint(dl
, "pool",
2145 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2146 pr_out_uint(dl
, "threshold",
2147 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2148 pr_out_port_handle_end(dl
);
2151 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2153 struct dl
*dl
= data
;
2154 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2155 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2157 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2158 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2159 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2160 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2161 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2162 return MNL_CB_ERROR
;
2163 pr_out_sb_tc_bind(dl
, tb
);
2167 static int cmd_sb_tc_bind_show(struct dl
*dl
)
2169 struct nlmsghdr
*nlh
;
2170 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2173 if (dl_argc(dl
) == 0)
2174 flags
|= NLM_F_DUMP
;
2176 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2178 if (dl_argc(dl
) > 0) {
2179 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2180 DL_OPT_SB_TYPE
, DL_OPT_SB
);
2185 pr_out_section_start(dl
, "tc_bind");
2186 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
2187 pr_out_section_end(dl
);
2191 static int cmd_sb_tc_bind_set(struct dl
*dl
)
2193 struct nlmsghdr
*nlh
;
2196 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2197 NLM_F_REQUEST
| NLM_F_ACK
);
2199 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2200 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
2205 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2208 static int cmd_sb_tc_bind(struct dl
*dl
)
2210 if (dl_argv_match(dl
, "help")) {
2213 } else if (dl_argv_match(dl
, "show") ||
2214 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2216 return cmd_sb_tc_bind_show(dl
);
2217 } else if (dl_argv_match(dl
, "set")) {
2219 return cmd_sb_tc_bind_set(dl
);
2221 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2225 static int cmd_sb_tc(struct dl
*dl
)
2227 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2230 } else if (dl_argv_match(dl
, "bind")) {
2232 return cmd_sb_tc_bind(dl
);
2234 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2239 struct list_head list
;
2243 uint32_t bound_pool_index
;
2247 struct list_head list
;
2250 uint32_t port_index
;
2252 struct list_head pool_list
;
2253 struct list_head ing_tc_list
;
2254 struct list_head eg_tc_list
;
2260 struct list_head port_list
;
2263 static struct occ_item
*occ_item_alloc(void)
2265 return calloc(1, sizeof(struct occ_item
));
2268 static void occ_item_free(struct occ_item
*occ_item
)
2273 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
2275 struct occ_port
*occ_port
;
2277 occ_port
= calloc(1, sizeof(*occ_port
));
2280 occ_port
->port_index
= port_index
;
2281 INIT_LIST_HEAD(&occ_port
->pool_list
);
2282 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
2283 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
2287 static void occ_port_free(struct occ_port
*occ_port
)
2289 struct occ_item
*occ_item
, *tmp
;
2291 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
2292 occ_item_free(occ_item
);
2293 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
2294 occ_item_free(occ_item
);
2295 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
2296 occ_item_free(occ_item
);
2299 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
2301 struct occ_show
*occ_show
;
2303 occ_show
= calloc(1, sizeof(*occ_show
));
2307 INIT_LIST_HEAD(&occ_show
->port_list
);
2311 static void occ_show_free(struct occ_show
*occ_show
)
2313 struct occ_port
*occ_port
, *tmp
;
2315 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
2316 occ_port_free(occ_port
);
2319 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
2322 struct occ_port
*occ_port
;
2323 uint32_t port_index
;
2325 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2327 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
2328 if (occ_port
->port_index
== port_index
)
2331 occ_port
= occ_port_alloc(port_index
);
2334 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
2338 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
2341 struct occ_item
*occ_item
;
2344 pr_out_sp(7, " %s:", label
);
2345 list_for_each_entry(occ_item
, list
, list
) {
2346 if ((i
- 1) % 4 == 0 && i
!= 1)
2349 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
2350 occ_item
->bound_pool_index
);
2352 pr_out_sp(7, "%2u:", occ_item
->index
);
2353 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
2357 if ((i
- 1) % 4 != 0)
2361 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
2362 struct list_head
*list
,
2365 struct occ_item
*occ_item
;
2368 jsonw_name(dl
->jw
, label
);
2369 jsonw_start_object(dl
->jw
);
2370 list_for_each_entry(occ_item
, list
, list
) {
2371 sprintf(buf
, "%u", occ_item
->index
);
2372 jsonw_name(dl
->jw
, buf
);
2373 jsonw_start_object(dl
->jw
);
2375 jsonw_uint_field(dl
->jw
, "bound_pool",
2376 occ_item
->bound_pool_index
);
2377 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
2378 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
2379 jsonw_end_object(dl
->jw
);
2381 jsonw_end_object(dl
->jw
);
2384 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
2386 if (dl
->json_output
) {
2387 pr_out_json_occ_show_item_list(dl
, "pool",
2388 &occ_port
->pool_list
, false);
2389 pr_out_json_occ_show_item_list(dl
, "itc",
2390 &occ_port
->ing_tc_list
, true);
2391 pr_out_json_occ_show_item_list(dl
, "etc",
2392 &occ_port
->eg_tc_list
, true);
2395 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
2396 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
2397 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
2401 static void pr_out_occ_show(struct occ_show
*occ_show
)
2403 struct dl
*dl
= occ_show
->dl
;
2404 struct dl_opts
*opts
= &dl
->opts
;
2405 struct occ_port
*occ_port
;
2407 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
2408 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
2409 occ_port
->port_index
, true, false);
2410 pr_out_occ_show_port(dl
, occ_port
);
2411 pr_out_port_handle_end(dl
);
2415 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
2418 struct occ_port
*occ_port
;
2419 struct occ_item
*occ_item
;
2421 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2424 occ_port
= occ_port_get(occ_show
, tb
);
2426 occ_show
->err
= -ENOMEM
;
2430 occ_item
= occ_item_alloc();
2432 occ_show
->err
= -ENOMEM
;
2435 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2436 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2437 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2438 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
2441 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2443 struct occ_show
*occ_show
= data
;
2444 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2445 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2447 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2448 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2449 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2450 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2451 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2452 return MNL_CB_ERROR
;
2453 cmd_sb_occ_port_pool_process(occ_show
, tb
);
2457 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
2460 struct occ_port
*occ_port
;
2461 struct occ_item
*occ_item
;
2464 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2467 occ_port
= occ_port_get(occ_show
, tb
);
2469 occ_show
->err
= -ENOMEM
;
2473 occ_item
= occ_item_alloc();
2475 occ_show
->err
= -ENOMEM
;
2478 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
2479 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2480 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2481 occ_item
->bound_pool_index
=
2482 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2483 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
2484 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
2485 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
2486 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
2487 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
2489 occ_item_free(occ_item
);
2492 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2494 struct occ_show
*occ_show
= data
;
2495 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2496 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2498 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2499 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2500 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2501 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2502 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2503 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2504 return MNL_CB_ERROR
;
2505 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
2509 static int cmd_sb_occ_show(struct dl
*dl
)
2511 struct nlmsghdr
*nlh
;
2512 struct occ_show
*occ_show
;
2513 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
2516 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
2520 occ_show
= occ_show_alloc(dl
);
2524 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2526 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2527 cmd_sb_occ_port_pool_process_cb
, occ_show
);
2531 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2533 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2534 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
2538 pr_out_section_start(dl
, "occupancy");
2539 pr_out_occ_show(occ_show
);
2540 pr_out_section_end(dl
);
2543 occ_show_free(occ_show
);
2547 static int cmd_sb_occ_snapshot(struct dl
*dl
)
2549 struct nlmsghdr
*nlh
;
2552 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2553 NLM_F_REQUEST
| NLM_F_ACK
);
2555 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2559 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2562 static int cmd_sb_occ_clearmax(struct dl
*dl
)
2564 struct nlmsghdr
*nlh
;
2567 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2568 NLM_F_REQUEST
| NLM_F_ACK
);
2570 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2574 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2577 static int cmd_sb_occ(struct dl
*dl
)
2579 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2582 } else if (dl_argv_match(dl
, "show") ||
2583 dl_argv_match(dl
, "list")) {
2585 return cmd_sb_occ_show(dl
);
2586 } else if (dl_argv_match(dl
, "snapshot")) {
2588 return cmd_sb_occ_snapshot(dl
);
2589 } else if (dl_argv_match(dl
, "clearmax")) {
2591 return cmd_sb_occ_clearmax(dl
);
2593 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2597 static int cmd_sb(struct dl
*dl
)
2599 if (dl_argv_match(dl
, "help")) {
2602 } else if (dl_argv_match(dl
, "show") ||
2603 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2605 return cmd_sb_show(dl
);
2606 } else if (dl_argv_match(dl
, "pool")) {
2608 return cmd_sb_pool(dl
);
2609 } else if (dl_argv_match(dl
, "port")) {
2611 return cmd_sb_port(dl
);
2612 } else if (dl_argv_match(dl
, "tc")) {
2614 return cmd_sb_tc(dl
);
2615 } else if (dl_argv_match(dl
, "occupancy")) {
2617 return cmd_sb_occ(dl
);
2619 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2623 static const char *cmd_name(uint8_t cmd
)
2626 case DEVLINK_CMD_UNSPEC
: return "unspec";
2627 case DEVLINK_CMD_GET
: return "get";
2628 case DEVLINK_CMD_SET
: return "set";
2629 case DEVLINK_CMD_NEW
: return "new";
2630 case DEVLINK_CMD_DEL
: return "del";
2631 case DEVLINK_CMD_PORT_GET
: return "get";
2632 case DEVLINK_CMD_PORT_SET
: return "set";
2633 case DEVLINK_CMD_PORT_NEW
: return "new";
2634 case DEVLINK_CMD_PORT_DEL
: return "del";
2635 default: return "<unknown cmd>";
2639 static const char *cmd_obj(uint8_t cmd
)
2642 case DEVLINK_CMD_UNSPEC
: return "unspec";
2643 case DEVLINK_CMD_GET
:
2644 case DEVLINK_CMD_SET
:
2645 case DEVLINK_CMD_NEW
:
2646 case DEVLINK_CMD_DEL
:
2648 case DEVLINK_CMD_PORT_GET
:
2649 case DEVLINK_CMD_PORT_SET
:
2650 case DEVLINK_CMD_PORT_NEW
:
2651 case DEVLINK_CMD_PORT_DEL
:
2653 default: return "<unknown obj>";
2657 static void pr_out_mon_header(uint8_t cmd
)
2659 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
2662 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
2664 const char *obj
= cmd_obj(cmd
);
2665 unsigned int index
= 0;
2666 const char *cur_obj
;
2670 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2671 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
2677 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2679 struct dl
*dl
= data
;
2680 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2681 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2682 uint8_t cmd
= genl
->cmd
;
2684 if (!cmd_filter_check(dl
, cmd
))
2688 case DEVLINK_CMD_GET
: /* fall through */
2689 case DEVLINK_CMD_SET
: /* fall through */
2690 case DEVLINK_CMD_NEW
: /* fall through */
2691 case DEVLINK_CMD_DEL
:
2692 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2693 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2694 return MNL_CB_ERROR
;
2695 pr_out_mon_header(genl
->cmd
);
2698 case DEVLINK_CMD_PORT_GET
: /* fall through */
2699 case DEVLINK_CMD_PORT_SET
: /* fall through */
2700 case DEVLINK_CMD_PORT_NEW
: /* fall through */
2701 case DEVLINK_CMD_PORT_DEL
:
2702 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2703 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2704 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2705 return MNL_CB_ERROR
;
2706 pr_out_mon_header(genl
->cmd
);
2707 pr_out_port(dl
, tb
);
2713 static int cmd_mon_show(struct dl
*dl
)
2716 unsigned int index
= 0;
2717 const char *cur_obj
;
2719 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2720 if (strcmp(cur_obj
, "all") != 0 &&
2721 strcmp(cur_obj
, "dev") != 0 &&
2722 strcmp(cur_obj
, "port") != 0) {
2723 pr_err("Unknown object \"%s\"\n", cur_obj
);
2727 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
2730 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
2736 static void cmd_mon_help(void)
2738 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
2739 "where OBJECT-LIST := { dev | port }\n");
2742 static int cmd_mon(struct dl
*dl
)
2744 if (dl_argv_match(dl
, "help")) {
2747 } else if (dl_no_arg(dl
)) {
2749 return cmd_mon_show(dl
);
2751 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2755 struct dpipe_field
{
2758 unsigned int bitwidth
;
2759 enum devlink_dpipe_field_mapping_type mapping_type
;
2762 struct dpipe_header
{
2763 struct list_head list
;
2766 struct dpipe_field
*fields
;
2767 unsigned int fields_count
;
2770 struct dpipe_table
{
2771 struct list_head list
;
2773 unsigned int resource_id
;
2774 bool resource_valid
;
2777 struct dpipe_tables
{
2778 struct list_head table_list
;
2788 enum devlink_resource_unit unit
;
2793 struct list_head list
;
2794 struct list_head resource_list
;
2795 struct resource
*parent
;
2799 struct list_head resource_list
;
2802 struct resource_ctx
{
2805 struct resources
*resources
;
2806 struct dpipe_tables
*tables
;
2807 bool print_resources
;
2808 bool pending_change
;
2811 static struct resource
*resource_alloc(void)
2813 struct resource
*resource
;
2815 resource
= calloc(1, sizeof(struct resource
));
2818 INIT_LIST_HEAD(&resource
->resource_list
);
2822 static void resource_free(struct resource
*resource
)
2824 struct resource
*child_resource
, *tmp
;
2826 list_for_each_entry_safe(child_resource
, tmp
, &resource
->resource_list
,
2828 free(child_resource
->name
);
2829 resource_free(child_resource
);
2834 static struct resources
*resources_alloc(void)
2836 struct resources
*resources
;
2838 resources
= calloc(1, sizeof(struct resources
));
2841 INIT_LIST_HEAD(&resources
->resource_list
);
2845 static void resources_free(struct resources
*resources
)
2847 struct resource
*resource
, *tmp
;
2849 list_for_each_entry_safe(resource
, tmp
, &resources
->resource_list
, list
)
2850 resource_free(resource
);
2853 static int resource_ctx_init(struct resource_ctx
*ctx
, struct dl
*dl
)
2855 ctx
->resources
= resources_alloc();
2856 if (!ctx
->resources
)
2862 static void resource_ctx_fini(struct resource_ctx
*ctx
)
2864 resources_free(ctx
->resources
);
2870 struct list_head global_headers
;
2871 struct list_head local_headers
;
2872 struct dpipe_tables
*tables
;
2873 struct resources
*resources
;
2878 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
2880 struct dpipe_header
*header
;
2882 header
= calloc(1, sizeof(struct dpipe_header
));
2885 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
2886 if (!header
->fields
)
2887 goto err_fields_alloc
;
2888 header
->fields_count
= fields_count
;
2896 static void dpipe_header_free(struct dpipe_header
*header
)
2898 free(header
->fields
);
2902 static void dpipe_header_clear(struct dpipe_header
*header
)
2904 struct dpipe_field
*field
;
2907 for (i
= 0; i
< header
->fields_count
; i
++) {
2908 field
= &header
->fields
[i
];
2914 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
2915 struct dpipe_header
*header
, bool global
)
2918 list_add(&header
->list
, &ctx
->global_headers
);
2920 list_add(&header
->list
, &ctx
->local_headers
);
2923 static void dpipe_header_del(struct dpipe_header
*header
)
2925 list_del(&header
->list
);
2928 static struct dpipe_table
*dpipe_table_alloc(void)
2930 return calloc(1, sizeof(struct dpipe_table
));
2933 static void dpipe_table_free(struct dpipe_table
*table
)
2938 static struct dpipe_tables
*dpipe_tables_alloc(void)
2940 struct dpipe_tables
*tables
;
2942 tables
= calloc(1, sizeof(struct dpipe_tables
));
2945 INIT_LIST_HEAD(&tables
->table_list
);
2949 static void dpipe_tables_free(struct dpipe_tables
*tables
)
2951 struct dpipe_table
*table
, *tmp
;
2953 list_for_each_entry_safe(table
, tmp
, &tables
->table_list
, list
)
2954 dpipe_table_free(table
);
2958 static int dpipe_ctx_init(struct dpipe_ctx
*ctx
, struct dl
*dl
)
2960 ctx
->tables
= dpipe_tables_alloc();
2965 INIT_LIST_HEAD(&ctx
->global_headers
);
2966 INIT_LIST_HEAD(&ctx
->local_headers
);
2970 static void dpipe_ctx_fini(struct dpipe_ctx
*ctx
)
2972 struct dpipe_header
*header
, *tmp
;
2974 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
2976 dpipe_header_del(header
);
2977 dpipe_header_clear(header
);
2978 dpipe_header_free(header
);
2980 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
2982 dpipe_header_del(header
);
2983 dpipe_header_clear(header
);
2984 dpipe_header_free(header
);
2986 dpipe_tables_free(ctx
->tables
);
2989 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
2990 uint32_t header_id
, bool global
)
2992 struct list_head
*header_list
;
2993 struct dpipe_header
*header
;
2996 header_list
= &ctx
->global_headers
;
2998 header_list
= &ctx
->local_headers
;
2999 list_for_each_entry(header
, header_list
, list
) {
3000 if (header
->id
!= header_id
)
3002 return header
->name
;
3007 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
3009 uint32_t field_id
, bool global
)
3011 struct list_head
*header_list
;
3012 struct dpipe_header
*header
;
3015 header_list
= &ctx
->global_headers
;
3017 header_list
= &ctx
->local_headers
;
3018 list_for_each_entry(header
, header_list
, list
) {
3019 if (header
->id
!= header_id
)
3021 return header
->fields
[field_id
].name
;
3027 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
3029 switch (mapping_type
) {
3030 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
3032 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
3040 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
3041 uint32_t field_id
, bool global
)
3043 enum devlink_dpipe_field_mapping_type mapping_type
;
3044 struct list_head
*header_list
;
3045 struct dpipe_header
*header
;
3048 header_list
= &ctx
->global_headers
;
3050 header_list
= &ctx
->local_headers
;
3051 list_for_each_entry(header
, header_list
, list
) {
3052 if (header
->id
!= header_id
)
3054 mapping_type
= header
->fields
[field_id
].mapping_type
;
3055 return dpipe_field_mapping_e2s(mapping_type
);
3060 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
3061 struct dpipe_field
*fields
,
3062 unsigned int field_count
)
3064 struct dpipe_field
*field
;
3067 for (i
= 0; i
< field_count
; i
++) {
3069 pr_out_entry_start(ctx
->dl
);
3070 pr_out_str(ctx
->dl
, "name", field
->name
);
3071 if (ctx
->dl
->verbose
)
3072 pr_out_uint(ctx
->dl
, "id", field
->id
);
3073 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
3074 if (field
->mapping_type
)
3075 pr_out_str(ctx
->dl
, "mapping_type",
3076 dpipe_field_mapping_e2s(field
->mapping_type
));
3077 pr_out_entry_end(ctx
->dl
);
3082 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
3083 struct dpipe_header
*header
, bool global
)
3085 pr_out_handle_start_arr(ctx
->dl
, tb
);
3086 pr_out_str(ctx
->dl
, "name", header
->name
);
3087 if (ctx
->dl
->verbose
) {
3088 pr_out_uint(ctx
->dl
, "id", header
->id
);
3089 pr_out_str(ctx
->dl
, "global",
3090 global
? "true" : "false");
3092 pr_out_array_start(ctx
->dl
, "field");
3093 pr_out_dpipe_fields(ctx
, header
->fields
,
3094 header
->fields_count
);
3095 pr_out_array_end(ctx
->dl
);
3096 pr_out_handle_end(ctx
->dl
);
3099 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
3102 struct dpipe_header
*header
;
3104 list_for_each_entry(header
, &ctx
->local_headers
, list
)
3105 pr_out_dpipe_header(ctx
, tb
, header
, false);
3107 list_for_each_entry(header
, &ctx
->global_headers
, list
)
3108 pr_out_dpipe_header(ctx
, tb
, header
, true);
3111 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
3113 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
3117 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
3118 if (err
!= MNL_CB_OK
)
3120 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
3121 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
3122 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
3123 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
3126 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
3127 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3128 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
3129 field
->name
= strdup(name
);
3132 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
3136 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
3137 struct dpipe_field
*fields
)
3139 struct nlattr
*nla_field
;
3143 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
3144 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
3152 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
3154 struct nlattr
*nla_field
;
3155 unsigned int count
= 0;
3157 mnl_attr_for_each_nested(nla_field
, nla_fields
)
3162 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3164 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
3165 struct dpipe_header
*header
;
3166 unsigned int fields_count
;
3167 const char *header_name
;
3171 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
3172 if (err
!= MNL_CB_OK
)
3175 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
3176 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3177 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
3180 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
3181 header
= dpipe_header_alloc(fields_count
);
3185 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
3186 header
->name
= strdup(header_name
);
3187 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3188 header
->fields_count
= fields_count
;
3189 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3191 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
3195 dpipe_header_add(ctx
, header
, global
);
3199 dpipe_header_free(header
);
3203 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3205 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
3206 struct nlattr
*nla_header
;
3209 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
3210 err
= dpipe_header_get(ctx
, nla_header
);
3217 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
3219 struct dpipe_ctx
*ctx
= data
;
3220 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3221 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3224 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3225 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3226 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
3227 return MNL_CB_ERROR
;
3228 err
= dpipe_headers_get(ctx
, tb
);
3231 return MNL_CB_ERROR
;
3234 if (ctx
->print_headers
)
3235 pr_out_dpipe_headers(ctx
, tb
);
3239 static int cmd_dpipe_headers_show(struct dl
*dl
)
3241 struct nlmsghdr
*nlh
;
3242 struct dpipe_ctx ctx
= {};
3243 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3246 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3248 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3252 err
= dpipe_ctx_init(&ctx
, dl
);
3256 ctx
.print_headers
= true;
3258 pr_out_section_start(dl
, "header");
3259 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
3261 pr_err("error get headers %s\n", strerror(ctx
.err
));
3262 pr_out_section_end(dl
);
3264 dpipe_ctx_fini(&ctx
);
3268 static void cmd_dpipe_header_help(void)
3270 pr_err("Usage: devlink dpipe headers show DEV\n");
3273 static int cmd_dpipe_header(struct dl
*dl
)
3275 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3276 cmd_dpipe_header_help();
3278 } else if (dl_argv_match(dl
, "show")) {
3280 return cmd_dpipe_headers_show(dl
);
3282 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3287 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
3289 switch (action_type
) {
3290 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
3291 return "field_modify";
3297 struct dpipe_op_info
{
3303 struct dpipe_action
{
3304 struct dpipe_op_info info
;
3308 static void pr_out_dpipe_action(struct dpipe_action
*action
,
3309 struct dpipe_ctx
*ctx
)
3311 struct dpipe_op_info
*op_info
= &action
->info
;
3312 const char *mapping
;
3314 pr_out_str(ctx
->dl
, "type",
3315 dpipe_action_type_e2s(action
->type
));
3316 pr_out_str(ctx
->dl
, "header",
3317 dpipe_header_id2s(ctx
, op_info
->header_id
,
3318 op_info
->header_global
));
3319 pr_out_str(ctx
->dl
, "field",
3320 dpipe_field_id2s(ctx
, op_info
->header_id
,
3322 op_info
->header_global
));
3323 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3325 op_info
->header_global
);
3327 pr_out_str(ctx
->dl
, "mapping", mapping
);
3330 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
3332 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
3335 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
3336 if (err
!= MNL_CB_OK
)
3339 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
3340 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3341 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3342 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3346 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
3347 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3348 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3349 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3354 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
3355 struct nlattr
*nla_actions
)
3357 struct nlattr
*nla_action
;
3358 struct dpipe_action action
;
3360 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
3361 pr_out_entry_start(ctx
->dl
);
3362 if (dpipe_action_parse(&action
, nla_action
))
3363 goto err_action_parse
;
3364 pr_out_dpipe_action(&action
, ctx
);
3365 pr_out_entry_end(ctx
->dl
);
3370 pr_out_entry_end(ctx
->dl
);
3375 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
3377 switch (match_type
) {
3378 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
3379 return "field_exact";
3385 struct dpipe_match
{
3386 struct dpipe_op_info info
;
3390 static void pr_out_dpipe_match(struct dpipe_match
*match
,
3391 struct dpipe_ctx
*ctx
)
3393 struct dpipe_op_info
*op_info
= &match
->info
;
3394 const char *mapping
;
3396 pr_out_str(ctx
->dl
, "type",
3397 dpipe_match_type_e2s(match
->type
));
3398 pr_out_str(ctx
->dl
, "header",
3399 dpipe_header_id2s(ctx
, op_info
->header_id
,
3400 op_info
->header_global
));
3401 pr_out_str(ctx
->dl
, "field",
3402 dpipe_field_id2s(ctx
, op_info
->header_id
,
3404 op_info
->header_global
));
3405 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3407 op_info
->header_global
);
3409 pr_out_str(ctx
->dl
, "mapping", mapping
);
3412 static int dpipe_match_parse(struct dpipe_match
*match
,
3416 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
3419 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
3420 if (err
!= MNL_CB_OK
)
3423 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
3424 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3425 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3426 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3430 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
3431 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3432 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3433 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3438 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
3439 struct nlattr
*nla_matches
)
3441 struct nlattr
*nla_match
;
3442 struct dpipe_match match
;
3444 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
3445 pr_out_entry_start(ctx
->dl
);
3446 if (dpipe_match_parse(&match
, nla_match
))
3447 goto err_match_parse
;
3448 pr_out_dpipe_match(&match
, ctx
);
3449 pr_out_entry_end(ctx
->dl
);
3454 pr_out_entry_end(ctx
->dl
);
3458 static struct resource
*
3459 resource_find(struct resources
*resources
, struct resource
*resource
,
3460 uint64_t resource_id
)
3462 struct list_head
*list_head
;
3465 list_head
= &resources
->resource_list
;
3467 list_head
= &resource
->resource_list
;
3469 list_for_each_entry(resource
, list_head
, list
) {
3470 struct resource
*child_resource
;
3472 if (resource
->id
== resource_id
)
3475 child_resource
= resource_find(resources
, resource
,
3478 return child_resource
;
3484 resource_path_print(struct dl
*dl
, struct resources
*resources
,
3485 uint64_t resource_id
)
3487 struct resource
*resource
, *parent_resource
;
3488 const char del
[] = "/";
3492 resource
= resource_find(resources
, NULL
, resource_id
);
3496 for (parent_resource
= resource
; parent_resource
;
3497 parent_resource
= parent_resource
->parent
)
3498 path_len
+= strlen(parent_resource
->name
) + 1;
3501 path
= calloc(1, path_len
);
3505 path
+= path_len
- 1;
3506 for (parent_resource
= resource
; parent_resource
;
3507 parent_resource
= parent_resource
->parent
) {
3508 path
-= strlen(parent_resource
->name
);
3509 memcpy(path
, parent_resource
->name
,
3510 strlen(parent_resource
->name
));
3511 path
-= strlen(del
);
3512 memcpy(path
, del
, strlen(del
));
3514 pr_out_str(dl
, "resource_path", path
);
3518 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3520 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
3521 struct dpipe_table
*table
;
3522 uint32_t resource_units
;
3523 bool counters_enabled
;
3524 bool resource_valid
;
3528 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
3529 if (err
!= MNL_CB_OK
)
3532 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
3533 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
3534 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
3535 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
3536 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
3540 table
= dpipe_table_alloc();
3544 table
->name
= strdup(mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]));
3545 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
3546 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
3548 resource_valid
= !!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
];
3549 if (resource_valid
) {
3550 table
->resource_id
= mnl_attr_get_u64(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
]);
3551 table
->resource_valid
= true;
3554 list_add_tail(&table
->list
, &ctx
->tables
->table_list
);
3555 if (!ctx
->print_tables
)
3558 pr_out_str(ctx
->dl
, "name", table
->name
);
3559 pr_out_uint(ctx
->dl
, "size", size
);
3560 pr_out_str(ctx
->dl
, "counters_enabled",
3561 counters_enabled
? "true" : "false");
3563 if (resource_valid
) {
3564 resource_units
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
]);
3565 resource_path_print(ctx
->dl
, ctx
->resources
,
3566 table
->resource_id
);
3567 pr_out_uint(ctx
->dl
, "resource_units", resource_units
);
3570 pr_out_array_start(ctx
->dl
, "match");
3571 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
3572 goto err_matches_show
;
3573 pr_out_array_end(ctx
->dl
);
3575 pr_out_array_start(ctx
->dl
, "action");
3576 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
3577 goto err_actions_show
;
3578 pr_out_array_end(ctx
->dl
);
3584 pr_out_array_end(ctx
->dl
);
3588 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3590 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
3591 struct nlattr
*nla_table
;
3593 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
3594 if (ctx
->print_tables
)
3595 pr_out_handle_start_arr(ctx
->dl
, tb
);
3596 if (dpipe_table_show(ctx
, nla_table
))
3597 goto err_table_show
;
3598 if (ctx
->print_tables
)
3599 pr_out_handle_end(ctx
->dl
);
3604 if (ctx
->print_tables
)
3605 pr_out_handle_end(ctx
->dl
);
3609 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3611 struct dpipe_ctx
*ctx
= data
;
3612 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3613 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3615 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3616 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3617 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
3618 return MNL_CB_ERROR
;
3620 if (dpipe_tables_show(ctx
, tb
))
3621 return MNL_CB_ERROR
;
3625 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
);
3627 static int cmd_dpipe_table_show(struct dl
*dl
)
3629 struct nlmsghdr
*nlh
;
3630 struct dpipe_ctx dpipe_ctx
= {};
3631 struct resource_ctx resource_ctx
= {};
3632 uint16_t flags
= NLM_F_REQUEST
;
3635 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
3639 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3641 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
3645 dpipe_ctx
.print_tables
= true;
3647 dl_opts_put(nlh
, dl
);
3648 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
,
3651 pr_err("error get headers %s\n", strerror(dpipe_ctx
.err
));
3652 goto err_headers_get
;
3655 err
= resource_ctx_init(&resource_ctx
, dl
);
3657 goto err_resource_ctx_init
;
3659 resource_ctx
.print_resources
= false;
3660 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
, flags
);
3661 dl_opts_put(nlh
, dl
);
3662 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
3665 pr_err("error get resources %s\n", strerror(resource_ctx
.err
));
3666 goto err_resource_dump
;
3669 dpipe_ctx
.resources
= resource_ctx
.resources
;
3670 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3671 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
3672 dl_opts_put(nlh
, dl
);
3674 pr_out_section_start(dl
, "table");
3675 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, &dpipe_ctx
);
3676 pr_out_section_end(dl
);
3678 resource_ctx_fini(&resource_ctx
);
3679 dpipe_ctx_fini(&dpipe_ctx
);
3683 resource_ctx_fini(&resource_ctx
);
3684 err_resource_ctx_init
:
3686 dpipe_ctx_fini(&dpipe_ctx
);
3690 static int cmd_dpipe_table_set(struct dl
*dl
)
3692 struct nlmsghdr
*nlh
;
3695 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
3696 NLM_F_REQUEST
| NLM_F_ACK
);
3698 err
= dl_argv_parse_put(nlh
, dl
,
3699 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
3700 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
3704 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3707 enum dpipe_value_type
{
3708 DPIPE_VALUE_TYPE_VALUE
,
3709 DPIPE_VALUE_TYPE_MASK
,
3713 dpipe_value_type_e2s(enum dpipe_value_type type
)
3716 case DPIPE_VALUE_TYPE_VALUE
:
3718 case DPIPE_VALUE_TYPE_MASK
:
3719 return "value_mask";
3725 struct dpipe_field_printer
{
3726 unsigned int field_id
;
3727 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
3730 struct dpipe_header_printer
{
3731 struct dpipe_field_printer
*printers
;
3732 unsigned int printers_count
;
3733 unsigned int header_id
;
3736 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
3737 enum dpipe_value_type type
,
3740 struct in_addr ip_addr
;
3742 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
3743 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
3747 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
3748 enum dpipe_value_type type
,
3751 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
3752 ether_ntoa((struct ether_addr
*)value
));
3755 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
3756 enum dpipe_value_type type
,
3759 char str
[INET6_ADDRSTRLEN
];
3761 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
3762 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
3765 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
3767 .printer
= dpipe_field_printer_ipv4_addr
,
3768 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
3772 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
3773 .printers
= dpipe_field_printers_ipv4
,
3774 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
3775 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
3778 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
3780 .printer
= dpipe_field_printer_ethernet_addr
,
3781 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
3785 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
3786 .printers
= dpipe_field_printers_ethernet
,
3787 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
3788 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
3791 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
3793 .printer
= dpipe_field_printer_ipv6_addr
,
3794 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
3798 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
3799 .printers
= dpipe_field_printers_ipv6
,
3800 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
3801 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
3804 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
3805 &dpipe_header_printer_ipv4
,
3806 &dpipe_header_printer_ethernet
,
3807 &dpipe_header_printer_ipv6
,
3810 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
3811 struct dpipe_op_info
*info
,
3812 enum dpipe_value_type type
,
3815 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
3816 struct dpipe_header_printer
*header_printer
;
3817 struct dpipe_field_printer
*field_printer
;
3818 unsigned int field_printers_count
;
3822 for (i
= 0; i
< header_printers_count
; i
++) {
3823 header_printer
= dpipe_header_printers
[i
];
3824 if (header_printer
->header_id
!= info
->header_id
)
3826 field_printers_count
= header_printer
->printers_count
;
3827 for (j
= 0; j
< field_printers_count
; j
++) {
3828 field_printer
= &header_printer
->printers
[j
];
3829 if (field_printer
->field_id
!= info
->field_id
)
3831 field_printer
->printer(ctx
, type
, value
);
3839 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
3841 unsigned int value_len
,
3842 struct dpipe_op_info
*info
,
3843 enum dpipe_value_type type
)
3845 if (info
->header_global
&&
3846 !dpipe_print_prot_header(ctx
, info
, type
, value
))
3849 if (value_len
== sizeof(uint32_t)) {
3850 uint32_t *value_32
= value
;
3852 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
3856 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
3857 struct nlattr
**nla_match_value
,
3858 struct dpipe_op_info
*info
)
3860 void *value
, *value_mask
;
3861 uint32_t value_mapping
;
3865 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
3866 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
3868 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3869 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3872 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
3873 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
3877 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3878 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
3879 DPIPE_VALUE_TYPE_MASK
);
3882 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
3885 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
3888 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3889 struct dpipe_match match
;
3892 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
3893 if (err
!= MNL_CB_OK
)
3896 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
3897 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3901 pr_out_entry_start(ctx
->dl
);
3902 if (dpipe_match_parse(&match
,
3903 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
3904 goto err_match_parse
;
3905 pr_out_dpipe_match(&match
, ctx
);
3906 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
3907 pr_out_entry_end(ctx
->dl
);
3912 pr_out_entry_end(ctx
->dl
);
3916 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
3919 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3920 struct dpipe_action action
;
3923 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
3924 if (err
!= MNL_CB_OK
)
3927 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
3928 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3932 pr_out_entry_start(ctx
->dl
);
3933 if (dpipe_action_parse(&action
,
3934 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
3935 goto err_action_parse
;
3936 pr_out_dpipe_action(&action
, ctx
);
3937 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
3938 pr_out_entry_end(ctx
->dl
);
3943 pr_out_entry_end(ctx
->dl
);
3948 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
3949 struct nlattr
*nla_action_values
)
3951 struct nlattr
*nla_action_value
;
3953 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
3954 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
3961 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
3962 struct nlattr
*nla_match_values
)
3964 struct nlattr
*nla_match_value
;
3966 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
3967 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
3973 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3975 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
3976 uint32_t entry_index
;
3980 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
3981 if (err
!= MNL_CB_OK
)
3984 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
3985 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
3986 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
3990 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
3991 pr_out_uint(ctx
->dl
, "index", entry_index
);
3993 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
3994 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
3995 pr_out_uint(ctx
->dl
, "counter", counter
);
3998 pr_out_array_start(ctx
->dl
, "match_value");
3999 if (dpipe_tables_match_values_show(ctx
,
4000 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
4001 goto err_match_values_show
;
4002 pr_out_array_end(ctx
->dl
);
4004 pr_out_array_start(ctx
->dl
, "action_value");
4005 if (dpipe_tables_action_values_show(ctx
,
4006 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
4007 goto err_action_values_show
;
4008 pr_out_array_end(ctx
->dl
);
4011 err_action_values_show
:
4012 err_match_values_show
:
4013 pr_out_array_end(ctx
->dl
);
4017 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
4019 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
4020 struct nlattr
*nla_entry
;
4022 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
4023 pr_out_handle_start_arr(ctx
->dl
, tb
);
4024 if (dpipe_entry_show(ctx
, nla_entry
))
4025 goto err_entry_show
;
4026 pr_out_handle_end(ctx
->dl
);
4031 pr_out_handle_end(ctx
->dl
);
4035 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
4037 struct dpipe_ctx
*ctx
= data
;
4038 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4039 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4041 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4042 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4043 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
4044 return MNL_CB_ERROR
;
4046 if (dpipe_table_entries_show(ctx
, tb
))
4047 return MNL_CB_ERROR
;
4051 static int cmd_dpipe_table_dump(struct dl
*dl
)
4053 struct nlmsghdr
*nlh
;
4054 struct dpipe_ctx ctx
= {};
4055 uint16_t flags
= NLM_F_REQUEST
;
4058 err
= dpipe_ctx_init(&ctx
, dl
);
4062 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
4066 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
4067 dl_opts_put(nlh
, dl
);
4068 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, &ctx
);
4070 pr_err("error get headers %s\n", strerror(ctx
.err
));
4074 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4075 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
4076 dl_opts_put(nlh
, dl
);
4078 pr_out_section_start(dl
, "table_entry");
4079 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, &ctx
);
4080 pr_out_section_end(dl
);
4082 dpipe_ctx_fini(&ctx
);
4086 static void cmd_dpipe_table_help(void)
4088 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
4089 "where OBJECT-LIST := { show | set | dump }\n");
4092 static int cmd_dpipe_table(struct dl
*dl
)
4094 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4095 cmd_dpipe_table_help();
4097 } else if (dl_argv_match(dl
, "show")) {
4099 return cmd_dpipe_table_show(dl
);
4100 } else if (dl_argv_match(dl
, "set")) {
4102 return cmd_dpipe_table_set(dl
);
4103 } else if (dl_argv_match(dl
, "dump")) {
4105 return cmd_dpipe_table_dump(dl
);
4107 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4111 static void cmd_dpipe_help(void)
4113 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
4114 "where OBJECT-LIST := { header | table }\n");
4117 static int cmd_dpipe(struct dl
*dl
)
4119 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4122 } else if (dl_argv_match(dl
, "header")) {
4124 return cmd_dpipe_header(dl
);
4125 } else if (dl_argv_match(dl
, "table")) {
4127 return cmd_dpipe_table(dl
);
4129 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4134 resource_parse(struct resource_ctx
*ctx
, struct resource
*resource
,
4135 struct nlattr
**nla_resource
)
4137 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
] ||
4138 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
] ||
4139 !nla_resource
[DEVLINK_ATTR_RESOURCE_ID
] ||
4140 !nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
] ||
4141 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
] ||
4142 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
] ||
4143 !nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]) {
4147 resource
->name
= strdup(mnl_attr_get_str(nla_resource
[DEVLINK_ATTR_RESOURCE_NAME
]));
4148 resource
->size
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE
]);
4149 resource
->id
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_ID
]);
4150 resource
->unit
= mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_UNIT
]);
4151 resource
->size_min
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MIN
]);
4152 resource
->size_max
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_MAX
]);
4153 resource
->size_gran
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_GRAN
]);
4155 if (nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
])
4156 resource
->size_new
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_NEW
]);
4158 resource
->size_new
= resource
->size
;
4160 if (nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]) {
4161 resource
->size_occ
= mnl_attr_get_u64(nla_resource
[DEVLINK_ATTR_RESOURCE_OCC
]);
4162 resource
->occ_valid
= true;
4165 if (resource
->size_new
!= resource
->size
)
4166 ctx
->pending_change
= true;
4172 resource_get(struct resource_ctx
*ctx
, struct resource
*resource
,
4173 struct resource
*parent_resource
, struct nlattr
*nl
)
4175 struct nlattr
*nla_resource
[DEVLINK_ATTR_MAX
+ 1] = {};
4176 struct nlattr
*nla_child_resource
;
4177 struct nlattr
*nla_resources
;
4187 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_resource
);
4188 if (err
!= MNL_CB_OK
)
4191 err
= resource_parse(ctx
, resource
, nla_resource
);
4195 resource
->parent
= parent_resource
;
4196 if (!nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
])
4199 resource
->size_valid
= !!mnl_attr_get_u8(nla_resource
[DEVLINK_ATTR_RESOURCE_SIZE_VALID
]);
4200 nla_resources
= nla_resource
[DEVLINK_ATTR_RESOURCE_LIST
];
4202 mnl_attr_for_each_nested(nla_child_resource
, nla_resources
) {
4203 struct resource
*child_resource
;
4204 struct list_head
*list
;
4206 child_resource
= resource_alloc();
4207 if (!child_resource
)
4211 list
= &ctx
->resources
->resource_list
;
4213 list
= &resource
->resource_list
;
4215 list_add_tail(&child_resource
->list
, list
);
4216 err
= resource_get(ctx
, child_resource
, resource
,
4217 nla_child_resource
);
4225 static const char *resource_unit_str_get(enum devlink_resource_unit unit
)
4228 case DEVLINK_RESOURCE_UNIT_ENTRY
: return "entry";
4229 default: return "<unknown unit>";
4233 static void resource_show(struct resource
*resource
,
4234 struct resource_ctx
*ctx
)
4236 struct resource
*child_resource
;
4237 struct dpipe_table
*table
;
4238 struct dl
*dl
= ctx
->dl
;
4241 pr_out_str(dl
, "name", resource
->name
);
4243 resource_path_print(dl
, ctx
->resources
, resource
->id
);
4244 pr_out_u64(dl
, "size", resource
->size
);
4245 if (resource
->size
!= resource
->size_new
)
4246 pr_out_u64(dl
, "size_new", resource
->size_new
);
4247 if (resource
->occ_valid
)
4248 pr_out_uint(dl
, "occ", resource
->size_occ
);
4249 pr_out_str(dl
, "unit", resource_unit_str_get(resource
->unit
));
4251 if (resource
->size_min
!= resource
->size_max
) {
4252 pr_out_uint(dl
, "size_min", resource
->size_min
);
4253 pr_out_u64(dl
, "size_max", resource
->size_max
);
4254 pr_out_uint(dl
, "size_gran", resource
->size_gran
);
4257 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
)
4258 if (table
->resource_id
== resource
->id
&&
4259 table
->resource_valid
)
4263 pr_out_array_start(dl
, "dpipe_tables");
4265 pr_out_str(dl
, "dpipe_tables", "none");
4267 list_for_each_entry(table
, &ctx
->tables
->table_list
, list
) {
4268 if (table
->resource_id
!= resource
->id
||
4269 !table
->resource_valid
)
4271 pr_out_entry_start(dl
);
4272 pr_out_str(dl
, "table_name", table
->name
);
4273 pr_out_entry_end(dl
);
4276 pr_out_array_end(dl
);
4278 if (list_empty(&resource
->resource_list
))
4281 if (ctx
->pending_change
)
4282 pr_out_str(dl
, "size_valid", resource
->size_valid
?
4284 pr_out_array_start(dl
, "resources");
4285 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
4286 pr_out_entry_start(dl
);
4287 resource_show(child_resource
, ctx
);
4288 pr_out_entry_end(dl
);
4290 pr_out_array_end(dl
);
4294 resources_show(struct resource_ctx
*ctx
, struct nlattr
**tb
)
4296 struct resources
*resources
= ctx
->resources
;
4297 struct resource
*resource
;
4299 list_for_each_entry(resource
, &resources
->resource_list
, list
) {
4300 pr_out_handle_start_arr(ctx
->dl
, tb
);
4301 resource_show(resource
, ctx
);
4302 pr_out_handle_end(ctx
->dl
);
4306 static int resources_get(struct resource_ctx
*ctx
, struct nlattr
**tb
)
4308 return resource_get(ctx
, NULL
, NULL
, tb
[DEVLINK_ATTR_RESOURCE_LIST
]);
4311 static int cmd_resource_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
4313 struct resource_ctx
*ctx
= data
;
4314 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
4315 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
4318 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
4319 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
4320 !tb
[DEVLINK_ATTR_RESOURCE_LIST
])
4321 return MNL_CB_ERROR
;
4323 err
= resources_get(ctx
, tb
);
4326 return MNL_CB_ERROR
;
4329 if (ctx
->print_resources
)
4330 resources_show(ctx
, tb
);
4335 static int cmd_resource_show(struct dl
*dl
)
4337 struct nlmsghdr
*nlh
;
4338 struct dpipe_ctx dpipe_ctx
= {};
4339 struct resource_ctx resource_ctx
= {};
4342 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, 0);
4346 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
,
4348 dl_opts_put(nlh
, dl
);
4350 err
= dpipe_ctx_init(&dpipe_ctx
, dl
);
4354 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
,
4357 pr_err("error get tables %s\n", strerror(dpipe_ctx
.err
));
4361 err
= resource_ctx_init(&resource_ctx
, dl
);
4365 resource_ctx
.print_resources
= true;
4366 resource_ctx
.tables
= dpipe_ctx
.tables
;
4367 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
4368 NLM_F_REQUEST
| NLM_F_ACK
);
4369 dl_opts_put(nlh
, dl
);
4370 pr_out_section_start(dl
, "resources");
4371 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
,
4373 pr_out_section_end(dl
);
4374 resource_ctx_fini(&resource_ctx
);
4376 dpipe_ctx_fini(&dpipe_ctx
);
4380 static void cmd_resource_help(void)
4382 pr_err("Usage: devlink resource show DEV\n"
4383 " devlink resource set DEV path PATH size SIZE\n");
4386 static struct resource
*
4387 resource_find_by_name(struct list_head
*list
, char *name
)
4389 struct resource
*resource
;
4391 list_for_each_entry(resource
, list
, list
) {
4392 if (!strcmp(resource
->name
, name
))
4399 resource_path_parse(struct resource_ctx
*ctx
, const char *resource_path
,
4400 uint32_t *p_resource_id
, bool *p_resource_valid
)
4402 struct resource
*resource
;
4403 uint32_t resource_id
= 0;
4404 char *resource_path_dup
;
4405 struct list_head
*list
;
4406 const char del
[] = "/";
4407 char *resource_name
;
4409 resource_path_dup
= strdup(resource_path
);
4410 list
= &ctx
->resources
->resource_list
;
4411 resource_name
= strtok(resource_path_dup
, del
);
4412 while (resource_name
!= NULL
) {
4413 resource
= resource_find_by_name(list
, resource_name
);
4415 goto err_resource_lookup
;
4417 list
= &resource
->resource_list
;
4418 resource_name
= strtok(NULL
, del
);
4419 resource_id
= resource
->id
;
4421 free(resource_path_dup
);
4422 *p_resource_valid
= true;
4423 *p_resource_id
= resource_id
;
4426 err_resource_lookup
:
4427 free(resource_path_dup
);
4431 static int cmd_resource_set(struct dl
*dl
)
4433 struct nlmsghdr
*nlh
;
4434 struct resource_ctx ctx
= {};
4437 err
= resource_ctx_init(&ctx
, dl
);
4441 ctx
.print_resources
= false;
4442 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_RESOURCE_PATH
|
4443 DL_OPT_RESOURCE_SIZE
, 0);
4447 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_DUMP
,
4449 dl_opts_put(nlh
, dl
);
4450 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_resource_dump_cb
, &ctx
);
4452 pr_err("error getting resources %s\n", strerror(ctx
.err
));
4456 err
= resource_path_parse(&ctx
, dl
->opts
.resource_path
,
4457 &dl
->opts
.resource_id
,
4458 &dl
->opts
.resource_id_valid
);
4460 pr_err("error parsing resource path %s\n", strerror(err
));
4464 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_RESOURCE_SET
,
4465 NLM_F_REQUEST
| NLM_F_ACK
);
4467 dl_opts_put(nlh
, dl
);
4468 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
4470 resource_ctx_fini(&ctx
);
4474 static int cmd_resource(struct dl
*dl
)
4476 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4477 cmd_resource_help();
4479 } else if (dl_argv_match(dl
, "show")) {
4481 return cmd_resource_show(dl
);
4482 } else if (dl_argv_match(dl
, "set")) {
4484 return cmd_resource_set(dl
);
4486 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
4490 static void help(void)
4492 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
4493 " devlink [ -f[orce] ] -b[atch] filename\n"
4494 "where OBJECT := { dev | port | sb | monitor | dpipe | resource }\n"
4495 " OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
4498 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
4503 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
4506 } else if (dl_argv_match(dl
, "dev")) {
4509 } else if (dl_argv_match(dl
, "port")) {
4511 return cmd_port(dl
);
4512 } else if (dl_argv_match(dl
, "sb")) {
4515 } else if (dl_argv_match(dl
, "monitor")) {
4518 } else if (dl_argv_match(dl
, "dpipe")) {
4520 return cmd_dpipe(dl
);
4521 } else if (dl_argv_match(dl
, "resource")) {
4523 return cmd_resource(dl
);
4525 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
4529 static int dl_init(struct dl
*dl
)
4533 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
4535 pr_err("Failed to connect to devlink Netlink\n");
4539 err
= ifname_map_init(dl
);
4541 pr_err("Failed to create index map\n");
4542 goto err_ifname_map_create
;
4544 if (dl
->json_output
) {
4545 dl
->jw
= jsonw_new(stdout
);
4547 pr_err("Failed to create JSON writer\n");
4550 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
4555 ifname_map_fini(dl
);
4556 err_ifname_map_create
:
4557 mnlg_socket_close(dl
->nlg
);
4561 static void dl_fini(struct dl
*dl
)
4563 if (dl
->json_output
)
4564 jsonw_destroy(&dl
->jw
);
4565 ifname_map_fini(dl
);
4566 mnlg_socket_close(dl
->nlg
);
4569 static struct dl
*dl_alloc(void)
4573 dl
= calloc(1, sizeof(*dl
));
4579 static void dl_free(struct dl
*dl
)
4584 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
4588 int ret
= EXIT_SUCCESS
;
4590 if (name
&& strcmp(name
, "-") != 0) {
4591 if (freopen(name
, "r", stdin
) == NULL
) {
4593 "Cannot open file \"%s\" for reading: %s\n",
4594 name
, strerror(errno
));
4595 return EXIT_FAILURE
;
4600 while (getcmdline(&line
, &len
, stdin
) != -1) {
4604 largc
= makeargs(line
, largv
, 100);
4606 continue; /* blank line */
4608 if (dl_cmd(dl
, largc
, largv
)) {
4609 fprintf(stderr
, "Command failed %s:%d\n",
4623 int main(int argc
, char **argv
)
4625 static const struct option long_options
[] = {
4626 { "Version", no_argument
, NULL
, 'V' },
4627 { "force", no_argument
, NULL
, 'f' },
4628 { "batch", required_argument
, NULL
, 'b' },
4629 { "no-nice-names", no_argument
, NULL
, 'n' },
4630 { "json", no_argument
, NULL
, 'j' },
4631 { "pretty", no_argument
, NULL
, 'p' },
4632 { "verbose", no_argument
, NULL
, 'v' },
4633 { NULL
, 0, NULL
, 0 }
4635 const char *batch_file
= NULL
;
4644 pr_err("Failed to allocate memory for devlink\n");
4645 return EXIT_FAILURE
;
4648 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
4649 long_options
, NULL
)) >= 0) {
4653 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
4660 batch_file
= optarg
;
4663 dl
->no_nice_names
= true;
4666 dl
->json_output
= true;
4669 dl
->pretty_output
= true;
4675 pr_err("Unknown option.\n");
4692 err
= dl_batch(dl
, batch_file
, force
);
4694 err
= dl_cmd(dl
, argc
, argv
);