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>
27 #include "json_writer.h"
30 #define ESWITCH_MODE_LEGACY "legacy"
31 #define ESWITCH_MODE_SWITCHDEV "switchdev"
32 #define ESWITCH_INLINE_MODE_NONE "none"
33 #define ESWITCH_INLINE_MODE_LINK "link"
34 #define ESWITCH_INLINE_MODE_NETWORK "network"
35 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
37 #define pr_err(args...) fprintf(stderr, ##args)
38 #define pr_out(args...) \
40 if (g_indent_newline) { \
41 fprintf(stdout, "%s", g_indent_str); \
42 g_indent_newline = false; \
44 fprintf(stdout, ##args); \
47 #define pr_out_sp(num, args...) \
49 int ret = fprintf(stdout, ##args); \
51 fprintf(stdout, "%*s", num - ret, ""); \
54 static int g_indent_level
;
55 static bool g_indent_newline
;
56 #define INDENT_STR_STEP 2
57 #define INDENT_STR_MAXLEN 32
58 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
60 static void __pr_out_indent_inc(void)
62 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
64 g_indent_level
+= INDENT_STR_STEP
;
65 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
66 g_indent_str
[g_indent_level
] = '\0';
69 static void __pr_out_indent_dec(void)
71 if (g_indent_level
- INDENT_STR_STEP
< 0)
73 g_indent_level
-= INDENT_STR_STEP
;
74 g_indent_str
[g_indent_level
] = '\0';
77 static void __pr_out_newline(void)
80 g_indent_newline
= true;
83 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
84 mnl_cb_t data_cb
, void *data
)
88 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
90 pr_err("devlink answers: %s\n", strerror(errno
));
96 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
97 const struct nlmsghdr
*nlh
,
98 mnl_cb_t data_cb
, void *data
)
102 err
= mnlg_socket_send(nlg
, nlh
);
104 pr_err("Failed to call mnlg_socket_send\n");
107 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
110 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
111 const char *group_name
)
115 err
= mnlg_socket_group_add(nlg
, group_name
);
117 pr_err("Failed to call mnlg_socket_group_add\n");
124 struct list_head list
;
131 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
132 const char *dev_name
,
136 struct ifname_map
*ifname_map
;
138 ifname_map
= calloc(1, sizeof(*ifname_map
));
141 ifname_map
->bus_name
= strdup(bus_name
);
142 ifname_map
->dev_name
= strdup(dev_name
);
143 ifname_map
->port_index
= port_index
;
144 ifname_map
->ifname
= strdup(ifname
);
145 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
146 !ifname_map
->ifname
) {
147 free(ifname_map
->ifname
);
148 free(ifname_map
->dev_name
);
149 free(ifname_map
->bus_name
);
156 static void ifname_map_free(struct ifname_map
*ifname_map
)
158 free(ifname_map
->ifname
);
159 free(ifname_map
->dev_name
);
160 free(ifname_map
->bus_name
);
164 #define DL_OPT_HANDLE BIT(0)
165 #define DL_OPT_HANDLEP BIT(1)
166 #define DL_OPT_PORT_TYPE BIT(2)
167 #define DL_OPT_PORT_COUNT BIT(3)
168 #define DL_OPT_SB BIT(4)
169 #define DL_OPT_SB_POOL BIT(5)
170 #define DL_OPT_SB_SIZE BIT(6)
171 #define DL_OPT_SB_TYPE BIT(7)
172 #define DL_OPT_SB_THTYPE BIT(8)
173 #define DL_OPT_SB_TH BIT(9)
174 #define DL_OPT_SB_TC BIT(10)
175 #define DL_OPT_ESWITCH_MODE BIT(11)
176 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
177 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
178 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
179 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
182 uint32_t present
; /* flags of present items */
186 enum devlink_port_type port_type
;
189 uint16_t sb_pool_index
;
190 uint32_t sb_pool_size
;
191 enum devlink_sb_pool_type sb_pool_type
;
192 enum devlink_sb_threshold_type sb_pool_thtype
;
193 uint32_t sb_threshold
;
194 uint16_t sb_tc_index
;
195 enum devlink_eswitch_mode eswitch_mode
;
196 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
197 const char *dpipe_table_name
;
198 bool dpipe_counters_enable
;
199 bool eswitch_encap_mode
;
203 struct mnlg_socket
*nlg
;
204 struct list_head ifname_map_list
;
221 static int dl_argc(struct dl
*dl
)
226 static char *dl_argv(struct dl
*dl
)
228 if (dl_argc(dl
) == 0)
233 static void dl_arg_inc(struct dl
*dl
)
235 if (dl_argc(dl
) == 0)
241 static char *dl_argv_next(struct dl
*dl
)
245 if (dl_argc(dl
) == 0)
253 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
255 if (index
>= dl_argc(dl
))
257 return dl
->argv
[index
];
260 static int strcmpx(const char *str1
, const char *str2
)
262 if (strlen(str1
) > strlen(str2
))
264 return strncmp(str1
, str2
, strlen(str1
));
267 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
269 if (dl_argc(dl
) == 0)
271 return strcmpx(dl_argv(dl
), pattern
) == 0;
274 static bool dl_no_arg(struct dl
*dl
)
276 return dl_argc(dl
) == 0;
279 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
280 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
281 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
282 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
283 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
284 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
285 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
286 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
287 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
288 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
289 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
290 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
291 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
292 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
293 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
294 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
295 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
296 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
297 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
298 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
299 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
300 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
301 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
302 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
303 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
304 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
305 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
306 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
307 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
308 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
309 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
310 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
311 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
312 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
313 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
314 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
315 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
316 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
317 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
318 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
319 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
320 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
321 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
322 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
323 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
324 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
325 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
326 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
327 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
328 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
329 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
330 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
331 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
332 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
333 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
334 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
335 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
336 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
339 static int attr_cb(const struct nlattr
*attr
, void *data
)
341 const struct nlattr
**tb
= data
;
344 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
347 type
= mnl_attr_get_type(attr
);
348 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
355 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
357 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
358 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
359 struct dl
*dl
= data
;
360 struct ifname_map
*ifname_map
;
361 const char *bus_name
;
362 const char *dev_name
;
363 uint32_t port_ifindex
;
364 const char *port_ifname
;
366 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
367 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
368 !tb
[DEVLINK_ATTR_PORT_INDEX
])
371 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
374 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
375 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
376 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
377 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
378 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
379 port_ifindex
, port_ifname
);
382 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
387 static void ifname_map_fini(struct dl
*dl
)
389 struct ifname_map
*ifname_map
, *tmp
;
391 list_for_each_entry_safe(ifname_map
, tmp
,
392 &dl
->ifname_map_list
, list
) {
393 list_del(&ifname_map
->list
);
394 ifname_map_free(ifname_map
);
398 static int ifname_map_init(struct dl
*dl
)
400 struct nlmsghdr
*nlh
;
403 INIT_LIST_HEAD(&dl
->ifname_map_list
);
405 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
406 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
408 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
416 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
417 char **p_bus_name
, char **p_dev_name
,
418 uint32_t *p_port_index
)
420 struct ifname_map
*ifname_map
;
422 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
423 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
424 *p_bus_name
= ifname_map
->bus_name
;
425 *p_dev_name
= ifname_map
->dev_name
;
426 *p_port_index
= ifname_map
->port_index
;
433 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
434 const char *dev_name
, uint32_t port_index
,
437 struct ifname_map
*ifname_map
;
439 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
440 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
441 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
442 port_index
== ifname_map
->port_index
) {
443 *p_ifname
= ifname_map
->ifname
;
450 static unsigned int strslashcount(char *str
)
452 unsigned int count
= 0;
455 while ((pos
= strchr(pos
, '/'))) {
462 static int strslashrsplit(char *str
, char **before
, char **after
)
466 slash
= strrchr(str
, '/');
475 static int strtouint32_t(const char *str
, uint32_t *p_val
)
478 unsigned long int val
;
480 val
= strtoul(str
, &endptr
, 10);
481 if (endptr
== str
|| *endptr
!= '\0')
489 static int strtouint16_t(const char *str
, uint16_t *p_val
)
492 unsigned long int val
;
494 val
= strtoul(str
, &endptr
, 10);
495 if (endptr
== str
|| *endptr
!= '\0')
503 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
505 strslashrsplit(str
, p_bus_name
, p_dev_name
);
509 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
511 char *str
= dl_argv_next(dl
);
514 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
517 if (strslashcount(str
) != 1) {
518 pr_err("Wrong devlink identification string format.\n");
519 pr_err("Expected \"bus_name/dev_name\".\n");
522 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
525 static int __dl_argv_handle_port(char *str
,
526 char **p_bus_name
, char **p_dev_name
,
527 uint32_t *p_port_index
)
533 err
= strslashrsplit(str
, &handlestr
, &portstr
);
535 pr_err("Port identification \"%s\" is invalid\n", str
);
538 err
= strtouint32_t(portstr
, p_port_index
);
540 pr_err("Port index \"%s\" is not a number or not within range\n",
544 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
546 pr_err("Port identification \"%s\" is invalid\n", str
);
552 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
553 char **p_bus_name
, char **p_dev_name
,
554 uint32_t *p_port_index
)
558 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
561 pr_err("Netdevice \"%s\" not found\n", str
);
567 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
568 char **p_dev_name
, uint32_t *p_port_index
)
570 char *str
= dl_argv_next(dl
);
571 unsigned int slash_count
;
574 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
577 slash_count
= strslashcount(str
);
578 switch (slash_count
) {
580 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
581 p_dev_name
, p_port_index
);
583 return __dl_argv_handle_port(str
, p_bus_name
,
584 p_dev_name
, p_port_index
);
586 pr_err("Wrong port identification string format.\n");
587 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
592 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
593 char **p_dev_name
, uint32_t *p_port_index
,
594 uint32_t *p_handle_bit
)
596 char *str
= dl_argv_next(dl
);
597 unsigned int slash_count
;
601 pr_err("One of following identifications expected:\n"
602 "Devlink identification (\"bus_name/dev_name\")\n"
603 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
606 slash_count
= strslashcount(str
);
607 if (slash_count
== 1) {
608 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
611 *p_handle_bit
= DL_OPT_HANDLE
;
612 } else if (slash_count
== 2) {
613 err
= __dl_argv_handle_port(str
, p_bus_name
,
614 p_dev_name
, p_port_index
);
617 *p_handle_bit
= DL_OPT_HANDLEP
;
618 } else if (slash_count
== 0) {
619 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
620 p_dev_name
, p_port_index
);
623 *p_handle_bit
= DL_OPT_HANDLEP
;
625 pr_err("Wrong port identification string format.\n");
626 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
632 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
634 char *str
= dl_argv_next(dl
);
638 pr_err("Unsigned number argument expected\n");
642 err
= strtouint32_t(str
, p_val
);
644 pr_err("\"%s\" is not a number or not within range\n", str
);
650 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
652 char *str
= dl_argv_next(dl
);
656 pr_err("Unsigned number argument expected\n");
660 err
= strtouint16_t(str
, p_val
);
662 pr_err("\"%s\" is not a number or not within range\n", str
);
668 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
670 const char *str
= dl_argv_next(dl
);
673 pr_err("String parameter expected\n");
680 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
682 if (strcmp(typestr
, "auto") == 0) {
683 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
684 } else if (strcmp(typestr
, "eth") == 0) {
685 *p_type
= DEVLINK_PORT_TYPE_ETH
;
686 } else if (strcmp(typestr
, "ib") == 0) {
687 *p_type
= DEVLINK_PORT_TYPE_IB
;
689 pr_err("Unknown port type \"%s\"\n", typestr
);
695 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
697 if (strcmp(typestr
, "ingress") == 0) {
698 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
699 } else if (strcmp(typestr
, "egress") == 0) {
700 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
702 pr_err("Unknown pool type \"%s\"\n", typestr
);
708 static int threshold_type_get(const char *typestr
,
709 enum devlink_sb_threshold_type
*p_type
)
711 if (strcmp(typestr
, "static") == 0) {
712 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
713 } else if (strcmp(typestr
, "dynamic") == 0) {
714 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
716 pr_err("Unknown threshold type \"%s\"\n", typestr
);
722 static int eswitch_mode_get(const char *typestr
,
723 enum devlink_eswitch_mode
*p_mode
)
725 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
726 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
727 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
728 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
730 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
736 static int eswitch_inline_mode_get(const char *typestr
,
737 enum devlink_eswitch_inline_mode
*p_mode
)
739 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
740 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
741 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
742 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
743 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
744 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
745 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
746 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
748 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
754 static int dpipe_counters_enable_get(const char *typestr
,
755 bool *counters_enable
)
757 if (strcmp(typestr
, "enable") == 0) {
758 *counters_enable
= 1;
759 } else if (strcmp(typestr
, "disable") == 0) {
760 *counters_enable
= 0;
762 pr_err("Unknown counter_state \"%s\"\n", typestr
);
768 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
770 if (strcmp(typestr
, "enable") == 0) {
772 } else if (strcmp(typestr
, "disable") == 0) {
775 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
781 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
784 struct dl_opts
*opts
= &dl
->opts
;
785 uint32_t o_all
= o_required
| o_optional
;
786 uint32_t o_found
= 0;
789 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
792 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
793 &opts
->port_index
, &handle_bit
);
796 o_found
|= handle_bit
;
797 } else if (o_required
& DL_OPT_HANDLE
) {
798 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
801 o_found
|= DL_OPT_HANDLE
;
802 } else if (o_required
& DL_OPT_HANDLEP
) {
803 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
807 o_found
|= DL_OPT_HANDLEP
;
810 while (dl_argc(dl
)) {
811 if (dl_argv_match(dl
, "type") &&
812 (o_all
& DL_OPT_PORT_TYPE
)) {
816 err
= dl_argv_str(dl
, &typestr
);
819 err
= port_type_get(typestr
, &opts
->port_type
);
822 o_found
|= DL_OPT_PORT_TYPE
;
823 } else if (dl_argv_match(dl
, "count") &&
824 (o_all
& DL_OPT_PORT_COUNT
)) {
826 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
829 o_found
|= DL_OPT_PORT_COUNT
;
830 } else if (dl_argv_match(dl
, "sb") &&
831 (o_all
& DL_OPT_SB
)) {
833 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
836 o_found
|= DL_OPT_SB
;
837 } else if (dl_argv_match(dl
, "pool") &&
838 (o_all
& DL_OPT_SB_POOL
)) {
840 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
843 o_found
|= DL_OPT_SB_POOL
;
844 } else if (dl_argv_match(dl
, "size") &&
845 (o_all
& DL_OPT_SB_SIZE
)) {
847 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
850 o_found
|= DL_OPT_SB_SIZE
;
851 } else if (dl_argv_match(dl
, "type") &&
852 (o_all
& DL_OPT_SB_TYPE
)) {
856 err
= dl_argv_str(dl
, &typestr
);
859 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
862 o_found
|= DL_OPT_SB_TYPE
;
863 } else if (dl_argv_match(dl
, "thtype") &&
864 (o_all
& DL_OPT_SB_THTYPE
)) {
868 err
= dl_argv_str(dl
, &typestr
);
871 err
= threshold_type_get(typestr
,
872 &opts
->sb_pool_thtype
);
875 o_found
|= DL_OPT_SB_THTYPE
;
876 } else if (dl_argv_match(dl
, "th") &&
877 (o_all
& DL_OPT_SB_TH
)) {
879 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
882 o_found
|= DL_OPT_SB_TH
;
883 } else if (dl_argv_match(dl
, "tc") &&
884 (o_all
& DL_OPT_SB_TC
)) {
886 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
889 o_found
|= DL_OPT_SB_TC
;
890 } else if (dl_argv_match(dl
, "mode") &&
891 (o_all
& DL_OPT_ESWITCH_MODE
)) {
895 err
= dl_argv_str(dl
, &typestr
);
898 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
901 o_found
|= DL_OPT_ESWITCH_MODE
;
902 } else if (dl_argv_match(dl
, "inline-mode") &&
903 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
907 err
= dl_argv_str(dl
, &typestr
);
910 err
= eswitch_inline_mode_get(
911 typestr
, &opts
->eswitch_inline_mode
);
914 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
915 } else if (dl_argv_match(dl
, "name") &&
916 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
918 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
921 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
922 } else if (dl_argv_match(dl
, "counters") &&
923 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
927 err
= dl_argv_str(dl
, &typestr
);
930 err
= dpipe_counters_enable_get(typestr
,
931 &opts
->dpipe_counters_enable
);
934 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
935 } else if (dl_argv_match(dl
, "encap") &&
936 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
940 err
= dl_argv_str(dl
, &typestr
);
943 err
= eswitch_encap_mode_get(typestr
,
944 &opts
->eswitch_encap_mode
);
947 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
949 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
954 opts
->present
= o_found
;
956 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
958 opts
->present
|= DL_OPT_SB
;
961 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
962 pr_err("Port type option expected.\n");
966 if ((o_required
& DL_OPT_PORT_COUNT
) &&
967 !(o_found
& DL_OPT_PORT_COUNT
)) {
968 pr_err("Port split count option expected.\n");
972 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
973 pr_err("Pool index option expected.\n");
977 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
978 pr_err("Pool size option expected.\n");
982 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
983 pr_err("Pool type option expected.\n");
987 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
988 pr_err("Pool threshold type option expected.\n");
992 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
993 pr_err("Threshold option expected.\n");
997 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
998 pr_err("TC index option expected.\n");
1002 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
1003 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
1004 pr_err("E-Switch mode option expected.\n");
1008 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
1009 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1010 pr_err("E-Switch inline-mode option expected.\n");
1014 if ((o_required
& DL_OPT_DPIPE_TABLE_NAME
) &&
1015 !(o_found
& DL_OPT_DPIPE_TABLE_NAME
)) {
1016 pr_err("Dpipe table name expected\n");
1020 if ((o_required
& DL_OPT_DPIPE_TABLE_COUNTERS
) &&
1021 !(o_found
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1022 pr_err("Dpipe table counter state expected\n");
1026 if ((o_required
& DL_OPT_ESWITCH_ENCAP_MODE
) &&
1027 !(o_found
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1028 pr_err("E-Switch encapsulation option expected.\n");
1035 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1037 struct dl_opts
*opts
= &dl
->opts
;
1039 if (opts
->present
& DL_OPT_HANDLE
) {
1040 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1041 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1042 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1043 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1044 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1045 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1048 if (opts
->present
& DL_OPT_PORT_TYPE
)
1049 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1051 if (opts
->present
& DL_OPT_PORT_COUNT
)
1052 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1054 if (opts
->present
& DL_OPT_SB
)
1055 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1057 if (opts
->present
& DL_OPT_SB_POOL
)
1058 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1059 opts
->sb_pool_index
);
1060 if (opts
->present
& DL_OPT_SB_SIZE
)
1061 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1062 opts
->sb_pool_size
);
1063 if (opts
->present
& DL_OPT_SB_TYPE
)
1064 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1065 opts
->sb_pool_type
);
1066 if (opts
->present
& DL_OPT_SB_THTYPE
)
1067 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1068 opts
->sb_pool_thtype
);
1069 if (opts
->present
& DL_OPT_SB_TH
)
1070 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1071 opts
->sb_threshold
);
1072 if (opts
->present
& DL_OPT_SB_TC
)
1073 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1075 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1076 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1077 opts
->eswitch_mode
);
1078 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1079 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1080 opts
->eswitch_inline_mode
);
1081 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1082 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1083 opts
->dpipe_table_name
);
1084 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1085 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1086 opts
->dpipe_counters_enable
);
1087 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1088 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1089 opts
->eswitch_encap_mode
);
1092 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1093 uint32_t o_required
, uint32_t o_optional
)
1097 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1100 dl_opts_put(nlh
, dl
);
1104 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1106 struct dl_opts
*opts
= &dl
->opts
;
1107 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1108 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1109 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1110 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1112 if (opts
->present
& DL_OPT_HANDLE
&&
1113 attr_bus_name
&& attr_dev_name
) {
1114 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1115 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1117 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1118 strcmp(dev_name
, opts
->dev_name
) != 0)
1121 if (opts
->present
& DL_OPT_HANDLEP
&&
1122 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1123 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1124 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1125 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1127 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1128 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1129 port_index
!= opts
->port_index
)
1132 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1133 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1135 if (sb_index
!= opts
->sb_index
)
1141 static void cmd_dev_help(void)
1143 pr_err("Usage: devlink dev show [ DEV ]\n");
1144 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1145 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1146 pr_err(" [ encap { disable | enable } ]\n");
1147 pr_err(" devlink dev eswitch show DEV\n");
1150 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1151 const char *dev_name
)
1153 if (!dl
->arr_last
.present
)
1155 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1156 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1159 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1160 const char *dev_name
)
1162 dl
->arr_last
.present
= true;
1163 free(dl
->arr_last
.dev_name
);
1164 free(dl
->arr_last
.bus_name
);
1165 dl
->arr_last
.bus_name
= strdup(bus_name
);
1166 dl
->arr_last
.dev_name
= strdup(dev_name
);
1169 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1170 const char *dev_name
)
1172 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1175 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1176 const char *dev_name
)
1178 return dl
->arr_last
.present
&&
1179 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1182 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1183 bool content
, bool array
)
1185 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1186 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1189 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1191 if (dl
->json_output
) {
1193 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1194 jsonw_end_array(dl
->jw
);
1195 if (should_arr_last_handle_start(dl
, bus_name
,
1197 jsonw_name(dl
->jw
, buf
);
1198 jsonw_start_array(dl
->jw
);
1199 jsonw_start_object(dl
->jw
);
1200 arr_last_handle_set(dl
, bus_name
, dev_name
);
1202 jsonw_start_object(dl
->jw
);
1205 jsonw_name(dl
->jw
, buf
);
1206 jsonw_start_object(dl
->jw
);
1210 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1211 __pr_out_indent_dec();
1212 if (should_arr_last_handle_start(dl
, bus_name
,
1214 pr_out("%s%s", buf
, content
? ":" : "");
1216 __pr_out_indent_inc();
1217 arr_last_handle_set(dl
, bus_name
, dev_name
);
1220 pr_out("%s%s", buf
, content
? ":" : "");
1225 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1227 __pr_out_handle_start(dl
, tb
, true, true);
1230 static void pr_out_handle_end(struct dl
*dl
)
1232 if (dl
->json_output
)
1233 jsonw_end_object(dl
->jw
);
1238 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1240 __pr_out_handle_start(dl
, tb
, false, false);
1241 pr_out_handle_end(dl
);
1244 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1245 const char *dev_name
, uint32_t port_index
)
1247 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1248 dl
->arr_last
.port_index
== port_index
;
1251 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1252 const char *dev_name
, uint32_t port_index
)
1254 arr_last_handle_set(dl
, bus_name
, dev_name
);
1255 dl
->arr_last
.port_index
= port_index
;
1258 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1259 const char *bus_name
,
1260 const char *dev_name
,
1261 uint32_t port_index
)
1263 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1266 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1267 const char *bus_name
,
1268 const char *dev_name
,
1269 uint32_t port_index
)
1271 return dl
->arr_last
.present
&&
1272 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1275 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1276 const char *dev_name
,
1277 uint32_t port_index
, bool try_nice
,
1280 static char buf
[32];
1281 char *ifname
= NULL
;
1283 if (dl
->no_nice_names
|| !try_nice
||
1284 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1285 port_index
, &ifname
) != 0)
1286 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1288 sprintf(buf
, "%s", ifname
);
1290 if (dl
->json_output
) {
1292 if (should_arr_last_port_handle_end(dl
, bus_name
,
1295 jsonw_end_array(dl
->jw
);
1296 if (should_arr_last_port_handle_start(dl
, bus_name
,
1299 jsonw_name(dl
->jw
, buf
);
1300 jsonw_start_array(dl
->jw
);
1301 jsonw_start_object(dl
->jw
);
1302 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1305 jsonw_start_object(dl
->jw
);
1308 jsonw_name(dl
->jw
, buf
);
1309 jsonw_start_object(dl
->jw
);
1316 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1318 const char *bus_name
;
1319 const char *dev_name
;
1320 uint32_t port_index
;
1322 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1323 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1324 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1325 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1328 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1330 const char *bus_name
;
1331 const char *dev_name
;
1332 uint32_t port_index
;
1334 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1335 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1336 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1337 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1340 static void pr_out_port_handle_end(struct dl
*dl
)
1342 if (dl
->json_output
)
1343 jsonw_end_object(dl
->jw
);
1349 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1351 if (dl
->json_output
) {
1352 jsonw_string_field(dl
->jw
, name
, val
);
1354 if (g_indent_newline
)
1355 pr_out("%s %s", name
, val
);
1357 pr_out(" %s %s", name
, val
);
1361 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1363 if (dl
->json_output
) {
1364 jsonw_uint_field(dl
->jw
, name
, val
);
1366 if (g_indent_newline
)
1367 pr_out("%s %u", name
, val
);
1369 pr_out(" %s %u", name
, val
);
1373 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1375 pr_out_handle(dl
, tb
);
1378 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1380 if (dl
->json_output
) {
1381 jsonw_start_object(dl
->jw
);
1382 jsonw_name(dl
->jw
, name
);
1383 jsonw_start_object(dl
->jw
);
1387 static void pr_out_section_end(struct dl
*dl
)
1389 if (dl
->json_output
) {
1390 if (dl
->arr_last
.present
)
1391 jsonw_end_array(dl
->jw
);
1392 jsonw_end_object(dl
->jw
);
1393 jsonw_end_object(dl
->jw
);
1397 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1399 if (dl
->json_output
) {
1400 jsonw_name(dl
->jw
, name
);
1401 jsonw_start_array(dl
->jw
);
1403 if (!g_indent_newline
)
1405 pr_out("%s:", name
);
1407 __pr_out_indent_inc();
1411 static void pr_out_array_end(struct dl
*dl
)
1413 if (dl
->json_output
)
1414 jsonw_end_array(dl
->jw
);
1416 __pr_out_indent_dec();
1419 static void pr_out_entry_start(struct dl
*dl
)
1421 if (dl
->json_output
)
1422 jsonw_start_object(dl
->jw
);
1425 static void pr_out_entry_end(struct dl
*dl
)
1427 if (dl
->json_output
)
1428 jsonw_end_object(dl
->jw
);
1433 static const char *eswitch_mode_name(uint32_t mode
)
1436 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1437 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1438 default: return "<unknown mode>";
1442 static const char *eswitch_inline_mode_name(uint32_t mode
)
1445 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1446 return ESWITCH_INLINE_MODE_NONE
;
1447 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1448 return ESWITCH_INLINE_MODE_LINK
;
1449 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1450 return ESWITCH_INLINE_MODE_NETWORK
;
1451 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1452 return ESWITCH_INLINE_MODE_TRANSPORT
;
1454 return "<unknown mode>";
1458 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1460 __pr_out_handle_start(dl
, tb
, true, false);
1462 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1463 pr_out_str(dl
, "mode",
1464 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1466 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1467 pr_out_str(dl
, "inline-mode",
1468 eswitch_inline_mode_name(mnl_attr_get_u8(
1469 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1471 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1472 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1474 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1477 pr_out_handle_end(dl
);
1480 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1482 struct dl
*dl
= data
;
1483 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1484 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1486 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1487 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1488 return MNL_CB_ERROR
;
1489 pr_out_eswitch(dl
, tb
);
1493 static int cmd_dev_eswitch_show(struct dl
*dl
)
1495 struct nlmsghdr
*nlh
;
1498 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1499 NLM_F_REQUEST
| NLM_F_ACK
);
1501 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1505 pr_out_section_start(dl
, "dev");
1506 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1507 pr_out_section_end(dl
);
1511 static int cmd_dev_eswitch_set(struct dl
*dl
)
1513 struct nlmsghdr
*nlh
;
1516 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1517 NLM_F_REQUEST
| NLM_F_ACK
);
1519 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1520 DL_OPT_ESWITCH_MODE
|
1521 DL_OPT_ESWITCH_INLINE_MODE
|
1522 DL_OPT_ESWITCH_ENCAP_MODE
);
1527 if (dl
->opts
.present
== 1) {
1528 pr_err("Need to set at least one option\n");
1532 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1535 static int cmd_dev_eswitch(struct dl
*dl
)
1537 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1540 } else if (dl_argv_match(dl
, "set")) {
1542 return cmd_dev_eswitch_set(dl
);
1543 } else if (dl_argv_match(dl
, "show")) {
1545 return cmd_dev_eswitch_show(dl
);
1547 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1551 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1553 struct dl
*dl
= data
;
1554 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1555 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1557 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1558 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1559 return MNL_CB_ERROR
;
1564 static int cmd_dev_show(struct dl
*dl
)
1566 struct nlmsghdr
*nlh
;
1567 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1570 if (dl_argc(dl
) == 0)
1571 flags
|= NLM_F_DUMP
;
1573 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
1575 if (dl_argc(dl
) > 0) {
1576 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1581 pr_out_section_start(dl
, "dev");
1582 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
1583 pr_out_section_end(dl
);
1587 static int cmd_dev(struct dl
*dl
)
1589 if (dl_argv_match(dl
, "help")) {
1592 } else if (dl_argv_match(dl
, "show") ||
1593 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1595 return cmd_dev_show(dl
);
1596 } else if (dl_argv_match(dl
, "eswitch")) {
1598 return cmd_dev_eswitch(dl
);
1600 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1604 static void cmd_port_help(void)
1606 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
1607 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
1608 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
1609 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
1612 static const char *port_type_name(uint32_t type
)
1615 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
1616 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
1617 case DEVLINK_PORT_TYPE_ETH
: return "eth";
1618 case DEVLINK_PORT_TYPE_IB
: return "ib";
1619 default: return "<unknown type>";
1623 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
1625 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
1626 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
1628 pr_out_port_handle_start(dl
, tb
, false);
1630 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
1632 pr_out_str(dl
, "type", port_type_name(port_type
));
1634 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
1636 if (port_type
!= des_port_type
)
1637 pr_out_str(dl
, "des_type",
1638 port_type_name(des_port_type
));
1641 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
1642 pr_out_str(dl
, "netdev",
1643 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
1644 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
1645 pr_out_str(dl
, "ibdev",
1646 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
1647 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
1648 pr_out_uint(dl
, "split_group",
1649 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
1650 pr_out_port_handle_end(dl
);
1653 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1655 struct dl
*dl
= data
;
1656 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1657 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1659 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1660 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1661 !tb
[DEVLINK_ATTR_PORT_INDEX
])
1662 return MNL_CB_ERROR
;
1663 pr_out_port(dl
, tb
);
1667 static int cmd_port_show(struct dl
*dl
)
1669 struct nlmsghdr
*nlh
;
1670 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1673 if (dl_argc(dl
) == 0)
1674 flags
|= NLM_F_DUMP
;
1676 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
1678 if (dl_argc(dl
) > 0) {
1679 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1684 pr_out_section_start(dl
, "port");
1685 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
1686 pr_out_section_end(dl
);
1690 static int cmd_port_set(struct dl
*dl
)
1692 struct nlmsghdr
*nlh
;
1695 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
1696 NLM_F_REQUEST
| NLM_F_ACK
);
1698 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
1702 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1705 static int cmd_port_split(struct dl
*dl
)
1707 struct nlmsghdr
*nlh
;
1710 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
1711 NLM_F_REQUEST
| NLM_F_ACK
);
1713 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
1717 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1720 static int cmd_port_unsplit(struct dl
*dl
)
1722 struct nlmsghdr
*nlh
;
1725 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
1726 NLM_F_REQUEST
| NLM_F_ACK
);
1728 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1732 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1735 static int cmd_port(struct dl
*dl
)
1737 if (dl_argv_match(dl
, "help")) {
1740 } else if (dl_argv_match(dl
, "show") ||
1741 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1743 return cmd_port_show(dl
);
1744 } else if (dl_argv_match(dl
, "set")) {
1746 return cmd_port_set(dl
);
1747 } else if (dl_argv_match(dl
, "split")) {
1749 return cmd_port_split(dl
);
1750 } else if (dl_argv_match(dl
, "unsplit")) {
1752 return cmd_port_unsplit(dl
);
1754 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1758 static void cmd_sb_help(void)
1760 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
1761 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
1762 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
1763 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
1764 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1765 pr_err(" pool POOL_INDEX ]\n");
1766 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1767 pr_err(" pool POOL_INDEX th THRESHOLD\n");
1768 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1769 pr_err(" type { ingress | egress } ]\n");
1770 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1771 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
1772 pr_err(" th THRESHOLD\n");
1773 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
1774 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
1775 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
1778 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
1780 pr_out_handle_start_arr(dl
, tb
);
1781 pr_out_uint(dl
, "sb",
1782 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1783 pr_out_uint(dl
, "size",
1784 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
1785 pr_out_uint(dl
, "ing_pools",
1786 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
1787 pr_out_uint(dl
, "eg_pools",
1788 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
1789 pr_out_uint(dl
, "ing_tcs",
1790 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
1791 pr_out_uint(dl
, "eg_tcs",
1792 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
1793 pr_out_handle_end(dl
);
1796 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1798 struct dl
*dl
= data
;
1799 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1800 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1802 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1803 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1804 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
1805 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
1806 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
1807 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
1808 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
1809 return MNL_CB_ERROR
;
1814 static int cmd_sb_show(struct dl
*dl
)
1816 struct nlmsghdr
*nlh
;
1817 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1820 if (dl_argc(dl
) == 0)
1821 flags
|= NLM_F_DUMP
;
1823 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
1825 if (dl_argc(dl
) > 0) {
1826 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
1831 pr_out_section_start(dl
, "sb");
1832 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
1833 pr_out_section_end(dl
);
1837 static const char *pool_type_name(uint8_t type
)
1840 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
1841 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
1842 default: return "<unknown type>";
1846 static const char *threshold_type_name(uint8_t type
)
1849 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
1850 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
1851 default: return "<unknown type>";
1855 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
1857 pr_out_handle_start_arr(dl
, tb
);
1858 pr_out_uint(dl
, "sb",
1859 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1860 pr_out_uint(dl
, "pool",
1861 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1862 pr_out_str(dl
, "type",
1863 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
1864 pr_out_uint(dl
, "size",
1865 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
1866 pr_out_str(dl
, "thtype",
1867 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
1868 pr_out_handle_end(dl
);
1871 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1873 struct dl
*dl
= data
;
1874 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1875 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1877 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1878 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1879 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
1880 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
1881 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
1882 return MNL_CB_ERROR
;
1883 pr_out_sb_pool(dl
, tb
);
1887 static int cmd_sb_pool_show(struct dl
*dl
)
1889 struct nlmsghdr
*nlh
;
1890 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1893 if (dl_argc(dl
) == 0)
1894 flags
|= NLM_F_DUMP
;
1896 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
1898 if (dl_argc(dl
) > 0) {
1899 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
1905 pr_out_section_start(dl
, "pool");
1906 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
1907 pr_out_section_end(dl
);
1911 static int cmd_sb_pool_set(struct dl
*dl
)
1913 struct nlmsghdr
*nlh
;
1916 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
1917 NLM_F_REQUEST
| NLM_F_ACK
);
1919 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
1920 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
1924 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1927 static int cmd_sb_pool(struct dl
*dl
)
1929 if (dl_argv_match(dl
, "help")) {
1932 } else if (dl_argv_match(dl
, "show") ||
1933 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1935 return cmd_sb_pool_show(dl
);
1936 } else if (dl_argv_match(dl
, "set")) {
1938 return cmd_sb_pool_set(dl
);
1940 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1944 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
1946 pr_out_port_handle_start_arr(dl
, tb
, true);
1947 pr_out_uint(dl
, "sb",
1948 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1949 pr_out_uint(dl
, "pool",
1950 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1951 pr_out_uint(dl
, "threshold",
1952 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
1953 pr_out_port_handle_end(dl
);
1956 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1958 struct dl
*dl
= data
;
1959 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1960 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1962 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1963 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1964 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
1965 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
1966 return MNL_CB_ERROR
;
1967 pr_out_sb_port_pool(dl
, tb
);
1971 static int cmd_sb_port_pool_show(struct dl
*dl
)
1973 struct nlmsghdr
*nlh
;
1974 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1977 if (dl_argc(dl
) == 0)
1978 flags
|= NLM_F_DUMP
;
1980 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
1982 if (dl_argc(dl
) > 0) {
1983 err
= dl_argv_parse_put(nlh
, dl
,
1984 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
1990 pr_out_section_start(dl
, "port_pool");
1991 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
1992 pr_out_section_end(dl
);
1996 static int cmd_sb_port_pool_set(struct dl
*dl
)
1998 struct nlmsghdr
*nlh
;
2001 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
2002 NLM_F_REQUEST
| NLM_F_ACK
);
2004 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
2005 DL_OPT_SB_TH
, DL_OPT_SB
);
2009 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2012 static int cmd_sb_port_pool(struct dl
*dl
)
2014 if (dl_argv_match(dl
, "help")) {
2017 } else if (dl_argv_match(dl
, "show") ||
2018 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2020 return cmd_sb_port_pool_show(dl
);
2021 } else if (dl_argv_match(dl
, "set")) {
2023 return cmd_sb_port_pool_set(dl
);
2025 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2029 static int cmd_sb_port(struct dl
*dl
)
2031 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2034 } else if (dl_argv_match(dl
, "pool")) {
2036 return cmd_sb_port_pool(dl
);
2038 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2042 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
2044 pr_out_port_handle_start_arr(dl
, tb
, true);
2045 pr_out_uint(dl
, "sb",
2046 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2047 pr_out_uint(dl
, "tc",
2048 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
2049 pr_out_str(dl
, "type",
2050 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2051 pr_out_uint(dl
, "pool",
2052 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2053 pr_out_uint(dl
, "threshold",
2054 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2055 pr_out_port_handle_end(dl
);
2058 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2060 struct dl
*dl
= data
;
2061 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2062 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2064 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2065 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2066 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2067 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2068 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2069 return MNL_CB_ERROR
;
2070 pr_out_sb_tc_bind(dl
, tb
);
2074 static int cmd_sb_tc_bind_show(struct dl
*dl
)
2076 struct nlmsghdr
*nlh
;
2077 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2080 if (dl_argc(dl
) == 0)
2081 flags
|= NLM_F_DUMP
;
2083 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2085 if (dl_argc(dl
) > 0) {
2086 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2087 DL_OPT_SB_TYPE
, DL_OPT_SB
);
2092 pr_out_section_start(dl
, "tc_bind");
2093 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
2094 pr_out_section_end(dl
);
2098 static int cmd_sb_tc_bind_set(struct dl
*dl
)
2100 struct nlmsghdr
*nlh
;
2103 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2104 NLM_F_REQUEST
| NLM_F_ACK
);
2106 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2107 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
2112 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2115 static int cmd_sb_tc_bind(struct dl
*dl
)
2117 if (dl_argv_match(dl
, "help")) {
2120 } else if (dl_argv_match(dl
, "show") ||
2121 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2123 return cmd_sb_tc_bind_show(dl
);
2124 } else if (dl_argv_match(dl
, "set")) {
2126 return cmd_sb_tc_bind_set(dl
);
2128 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2132 static int cmd_sb_tc(struct dl
*dl
)
2134 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2137 } else if (dl_argv_match(dl
, "bind")) {
2139 return cmd_sb_tc_bind(dl
);
2141 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2146 struct list_head list
;
2150 uint32_t bound_pool_index
;
2154 struct list_head list
;
2157 uint32_t port_index
;
2159 struct list_head pool_list
;
2160 struct list_head ing_tc_list
;
2161 struct list_head eg_tc_list
;
2167 struct list_head port_list
;
2170 static struct occ_item
*occ_item_alloc(void)
2172 return calloc(1, sizeof(struct occ_item
));
2175 static void occ_item_free(struct occ_item
*occ_item
)
2180 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
2182 struct occ_port
*occ_port
;
2184 occ_port
= calloc(1, sizeof(*occ_port
));
2187 occ_port
->port_index
= port_index
;
2188 INIT_LIST_HEAD(&occ_port
->pool_list
);
2189 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
2190 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
2194 static void occ_port_free(struct occ_port
*occ_port
)
2196 struct occ_item
*occ_item
, *tmp
;
2198 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
2199 occ_item_free(occ_item
);
2200 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
2201 occ_item_free(occ_item
);
2202 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
2203 occ_item_free(occ_item
);
2206 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
2208 struct occ_show
*occ_show
;
2210 occ_show
= calloc(1, sizeof(*occ_show
));
2214 INIT_LIST_HEAD(&occ_show
->port_list
);
2218 static void occ_show_free(struct occ_show
*occ_show
)
2220 struct occ_port
*occ_port
, *tmp
;
2222 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
2223 occ_port_free(occ_port
);
2226 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
2229 struct occ_port
*occ_port
;
2230 uint32_t port_index
;
2232 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2234 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
2235 if (occ_port
->port_index
== port_index
)
2238 occ_port
= occ_port_alloc(port_index
);
2241 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
2245 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
2248 struct occ_item
*occ_item
;
2251 pr_out_sp(7, " %s:", label
);
2252 list_for_each_entry(occ_item
, list
, list
) {
2253 if ((i
- 1) % 4 == 0 && i
!= 1)
2256 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
2257 occ_item
->bound_pool_index
);
2259 pr_out_sp(7, "%2u:", occ_item
->index
);
2260 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
2264 if ((i
- 1) % 4 != 0)
2268 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
2269 struct list_head
*list
,
2272 struct occ_item
*occ_item
;
2275 jsonw_name(dl
->jw
, label
);
2276 jsonw_start_object(dl
->jw
);
2277 list_for_each_entry(occ_item
, list
, list
) {
2278 sprintf(buf
, "%u", occ_item
->index
);
2279 jsonw_name(dl
->jw
, buf
);
2280 jsonw_start_object(dl
->jw
);
2282 jsonw_uint_field(dl
->jw
, "bound_pool",
2283 occ_item
->bound_pool_index
);
2284 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
2285 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
2286 jsonw_end_object(dl
->jw
);
2288 jsonw_end_object(dl
->jw
);
2291 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
2293 if (dl
->json_output
) {
2294 pr_out_json_occ_show_item_list(dl
, "pool",
2295 &occ_port
->pool_list
, false);
2296 pr_out_json_occ_show_item_list(dl
, "itc",
2297 &occ_port
->ing_tc_list
, true);
2298 pr_out_json_occ_show_item_list(dl
, "etc",
2299 &occ_port
->eg_tc_list
, true);
2302 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
2303 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
2304 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
2308 static void pr_out_occ_show(struct occ_show
*occ_show
)
2310 struct dl
*dl
= occ_show
->dl
;
2311 struct dl_opts
*opts
= &dl
->opts
;
2312 struct occ_port
*occ_port
;
2314 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
2315 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
2316 occ_port
->port_index
, true, false);
2317 pr_out_occ_show_port(dl
, occ_port
);
2318 pr_out_port_handle_end(dl
);
2322 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
2325 struct occ_port
*occ_port
;
2326 struct occ_item
*occ_item
;
2328 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2331 occ_port
= occ_port_get(occ_show
, tb
);
2333 occ_show
->err
= -ENOMEM
;
2337 occ_item
= occ_item_alloc();
2339 occ_show
->err
= -ENOMEM
;
2342 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2343 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2344 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2345 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
2348 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2350 struct occ_show
*occ_show
= data
;
2351 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2352 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2354 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2355 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2356 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2357 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2358 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2359 return MNL_CB_ERROR
;
2360 cmd_sb_occ_port_pool_process(occ_show
, tb
);
2364 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
2367 struct occ_port
*occ_port
;
2368 struct occ_item
*occ_item
;
2371 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2374 occ_port
= occ_port_get(occ_show
, tb
);
2376 occ_show
->err
= -ENOMEM
;
2380 occ_item
= occ_item_alloc();
2382 occ_show
->err
= -ENOMEM
;
2385 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
2386 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2387 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2388 occ_item
->bound_pool_index
=
2389 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2390 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
2391 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
2392 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
2393 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
2394 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
2396 occ_item_free(occ_item
);
2399 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2401 struct occ_show
*occ_show
= data
;
2402 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2403 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2405 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2406 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2407 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2408 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2409 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2410 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2411 return MNL_CB_ERROR
;
2412 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
2416 static int cmd_sb_occ_show(struct dl
*dl
)
2418 struct nlmsghdr
*nlh
;
2419 struct occ_show
*occ_show
;
2420 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
2423 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
2427 occ_show
= occ_show_alloc(dl
);
2431 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2433 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2434 cmd_sb_occ_port_pool_process_cb
, occ_show
);
2438 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2440 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2441 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
2445 pr_out_section_start(dl
, "occupancy");
2446 pr_out_occ_show(occ_show
);
2447 pr_out_section_end(dl
);
2450 occ_show_free(occ_show
);
2454 static int cmd_sb_occ_snapshot(struct dl
*dl
)
2456 struct nlmsghdr
*nlh
;
2459 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2460 NLM_F_REQUEST
| NLM_F_ACK
);
2462 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2466 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2469 static int cmd_sb_occ_clearmax(struct dl
*dl
)
2471 struct nlmsghdr
*nlh
;
2474 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2475 NLM_F_REQUEST
| NLM_F_ACK
);
2477 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2481 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2484 static int cmd_sb_occ(struct dl
*dl
)
2486 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2489 } else if (dl_argv_match(dl
, "show") ||
2490 dl_argv_match(dl
, "list")) {
2492 return cmd_sb_occ_show(dl
);
2493 } else if (dl_argv_match(dl
, "snapshot")) {
2495 return cmd_sb_occ_snapshot(dl
);
2496 } else if (dl_argv_match(dl
, "clearmax")) {
2498 return cmd_sb_occ_clearmax(dl
);
2500 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2504 static int cmd_sb(struct dl
*dl
)
2506 if (dl_argv_match(dl
, "help")) {
2509 } else if (dl_argv_match(dl
, "show") ||
2510 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2512 return cmd_sb_show(dl
);
2513 } else if (dl_argv_match(dl
, "pool")) {
2515 return cmd_sb_pool(dl
);
2516 } else if (dl_argv_match(dl
, "port")) {
2518 return cmd_sb_port(dl
);
2519 } else if (dl_argv_match(dl
, "tc")) {
2521 return cmd_sb_tc(dl
);
2522 } else if (dl_argv_match(dl
, "occupancy")) {
2524 return cmd_sb_occ(dl
);
2526 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2530 static const char *cmd_name(uint8_t cmd
)
2533 case DEVLINK_CMD_UNSPEC
: return "unspec";
2534 case DEVLINK_CMD_GET
: return "get";
2535 case DEVLINK_CMD_SET
: return "set";
2536 case DEVLINK_CMD_NEW
: return "new";
2537 case DEVLINK_CMD_DEL
: return "del";
2538 case DEVLINK_CMD_PORT_GET
: return "get";
2539 case DEVLINK_CMD_PORT_SET
: return "set";
2540 case DEVLINK_CMD_PORT_NEW
: return "net";
2541 case DEVLINK_CMD_PORT_DEL
: return "del";
2542 default: return "<unknown cmd>";
2546 static const char *cmd_obj(uint8_t cmd
)
2549 case DEVLINK_CMD_UNSPEC
: return "unspec";
2550 case DEVLINK_CMD_GET
:
2551 case DEVLINK_CMD_SET
:
2552 case DEVLINK_CMD_NEW
:
2553 case DEVLINK_CMD_DEL
:
2555 case DEVLINK_CMD_PORT_GET
:
2556 case DEVLINK_CMD_PORT_SET
:
2557 case DEVLINK_CMD_PORT_NEW
:
2558 case DEVLINK_CMD_PORT_DEL
:
2560 default: return "<unknown obj>";
2564 static void pr_out_mon_header(uint8_t cmd
)
2566 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
2569 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
2571 const char *obj
= cmd_obj(cmd
);
2572 unsigned int index
= 0;
2573 const char *cur_obj
;
2577 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2578 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
2584 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2586 struct dl
*dl
= data
;
2587 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2588 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2589 uint8_t cmd
= genl
->cmd
;
2591 if (!cmd_filter_check(dl
, cmd
))
2595 case DEVLINK_CMD_GET
: /* fall through */
2596 case DEVLINK_CMD_SET
: /* fall through */
2597 case DEVLINK_CMD_NEW
: /* fall through */
2598 case DEVLINK_CMD_DEL
:
2599 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2600 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2601 return MNL_CB_ERROR
;
2602 pr_out_mon_header(genl
->cmd
);
2605 case DEVLINK_CMD_PORT_GET
: /* fall through */
2606 case DEVLINK_CMD_PORT_SET
: /* fall through */
2607 case DEVLINK_CMD_PORT_NEW
: /* fall through */
2608 case DEVLINK_CMD_PORT_DEL
:
2609 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2610 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2611 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2612 return MNL_CB_ERROR
;
2613 pr_out_mon_header(genl
->cmd
);
2614 pr_out_port(dl
, tb
);
2620 static int cmd_mon_show(struct dl
*dl
)
2623 unsigned int index
= 0;
2624 const char *cur_obj
;
2626 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2627 if (strcmp(cur_obj
, "all") != 0 &&
2628 strcmp(cur_obj
, "dev") != 0 &&
2629 strcmp(cur_obj
, "port") != 0) {
2630 pr_err("Unknown object \"%s\"\n", cur_obj
);
2634 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
2637 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
2643 static void cmd_mon_help(void)
2645 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
2646 "where OBJECT-LIST := { dev | port }\n");
2649 static int cmd_mon(struct dl
*dl
)
2651 if (dl_argv_match(dl
, "help")) {
2654 } else if (dl_no_arg(dl
)) {
2656 return cmd_mon_show(dl
);
2658 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2662 struct dpipe_field
{
2665 unsigned int bitwidth
;
2666 enum devlink_dpipe_field_mapping_type mapping_type
;
2669 struct dpipe_header
{
2670 struct list_head list
;
2673 struct dpipe_field
*fields
;
2674 unsigned int fields_count
;
2680 struct list_head global_headers
;
2681 struct list_head local_headers
;
2685 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
2687 struct dpipe_header
*header
;
2689 header
= calloc(1, sizeof(struct dpipe_header
));
2692 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
2693 if (!header
->fields
)
2694 goto err_fields_alloc
;
2695 header
->fields_count
= fields_count
;
2703 static void dpipe_header_free(struct dpipe_header
*header
)
2705 free(header
->fields
);
2709 static void dpipe_header_clear(struct dpipe_header
*header
)
2711 struct dpipe_field
*field
;
2714 for (i
= 0; i
< header
->fields_count
; i
++) {
2715 field
= &header
->fields
[i
];
2721 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
2722 struct dpipe_header
*header
, bool global
)
2725 list_add(&header
->list
, &ctx
->global_headers
);
2727 list_add(&header
->list
, &ctx
->local_headers
);
2730 static void dpipe_header_del(struct dpipe_header
*header
)
2732 list_del(&header
->list
);
2735 static struct dpipe_ctx
*dpipe_ctx_alloc(struct dl
*dl
)
2737 struct dpipe_ctx
*ctx
;
2739 ctx
= calloc(1, sizeof(struct dpipe_ctx
));
2743 INIT_LIST_HEAD(&ctx
->global_headers
);
2744 INIT_LIST_HEAD(&ctx
->local_headers
);
2748 static void dpipe_ctx_free(struct dpipe_ctx
*ctx
)
2753 static void dpipe_ctx_clear(struct dpipe_ctx
*ctx
)
2755 struct dpipe_header
*header
, *tmp
;
2757 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
2759 dpipe_header_del(header
);
2760 dpipe_header_clear(header
);
2761 dpipe_header_free(header
);
2763 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
2765 dpipe_header_del(header
);
2766 dpipe_header_clear(header
);
2767 dpipe_header_free(header
);
2771 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
2772 uint32_t header_id
, bool global
)
2774 struct list_head
*header_list
;
2775 struct dpipe_header
*header
;
2778 header_list
= &ctx
->global_headers
;
2780 header_list
= &ctx
->local_headers
;
2781 list_for_each_entry(header
, header_list
, list
) {
2782 if (header
->id
!= header_id
)
2784 return header
->name
;
2789 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
2791 uint32_t field_id
, bool global
)
2793 struct list_head
*header_list
;
2794 struct dpipe_header
*header
;
2797 header_list
= &ctx
->global_headers
;
2799 header_list
= &ctx
->local_headers
;
2800 list_for_each_entry(header
, header_list
, list
) {
2801 if (header
->id
!= header_id
)
2803 return header
->fields
[field_id
].name
;
2809 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
2811 switch (mapping_type
) {
2812 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
2814 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
2822 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
2823 uint32_t field_id
, bool global
)
2825 enum devlink_dpipe_field_mapping_type mapping_type
;
2826 struct list_head
*header_list
;
2827 struct dpipe_header
*header
;
2830 header_list
= &ctx
->global_headers
;
2832 header_list
= &ctx
->local_headers
;
2833 list_for_each_entry(header
, header_list
, list
) {
2834 if (header
->id
!= header_id
)
2836 mapping_type
= header
->fields
[field_id
].mapping_type
;
2837 return dpipe_field_mapping_e2s(mapping_type
);
2842 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
2843 struct dpipe_field
*fields
,
2844 unsigned int field_count
)
2846 struct dpipe_field
*field
;
2849 for (i
= 0; i
< field_count
; i
++) {
2851 pr_out_entry_start(ctx
->dl
);
2852 pr_out_str(ctx
->dl
, "name", field
->name
);
2853 if (ctx
->dl
->verbose
)
2854 pr_out_uint(ctx
->dl
, "id", field
->id
);
2855 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
2856 if (field
->mapping_type
)
2857 pr_out_str(ctx
->dl
, "mapping_type",
2858 dpipe_field_mapping_e2s(field
->mapping_type
));
2859 pr_out_entry_end(ctx
->dl
);
2864 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
2865 struct dpipe_header
*header
, bool global
)
2867 pr_out_handle_start_arr(ctx
->dl
, tb
);
2868 pr_out_str(ctx
->dl
, "name", header
->name
);
2869 if (ctx
->dl
->verbose
) {
2870 pr_out_uint(ctx
->dl
, "id", header
->id
);
2871 pr_out_str(ctx
->dl
, "global",
2872 global
? "true" : "false");
2874 pr_out_array_start(ctx
->dl
, "field");
2875 pr_out_dpipe_fields(ctx
, header
->fields
,
2876 header
->fields_count
);
2877 pr_out_array_end(ctx
->dl
);
2878 pr_out_handle_end(ctx
->dl
);
2881 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
2884 struct dpipe_header
*header
;
2886 list_for_each_entry(header
, &ctx
->local_headers
, list
)
2887 pr_out_dpipe_header(ctx
, tb
, header
, false);
2889 list_for_each_entry(header
, &ctx
->global_headers
, list
)
2890 pr_out_dpipe_header(ctx
, tb
, header
, true);
2893 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
2895 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
2899 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
2900 if (err
!= MNL_CB_OK
)
2902 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
2903 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
2904 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
2905 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
2908 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
2909 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
2910 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
2911 field
->name
= strdup(name
);
2914 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
2918 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
2919 struct dpipe_field
*fields
)
2921 struct nlattr
*nla_field
;
2925 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
2926 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
2934 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
2936 struct nlattr
*nla_field
;
2937 unsigned int count
= 0;
2939 mnl_attr_for_each_nested(nla_field
, nla_fields
)
2944 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
2946 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
2947 struct dpipe_header
*header
;
2948 unsigned int fields_count
;
2949 const char *header_name
;
2953 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
2954 if (err
!= MNL_CB_OK
)
2957 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
2958 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
2959 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
2962 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
2963 header
= dpipe_header_alloc(fields_count
);
2967 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
2968 header
->name
= strdup(header_name
);
2969 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
2970 header
->fields_count
= fields_count
;
2971 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
2973 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
2977 dpipe_header_add(ctx
, header
, global
);
2981 dpipe_header_free(header
);
2985 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
2987 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
2988 struct nlattr
*nla_header
;
2991 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
2992 err
= dpipe_header_get(ctx
, nla_header
);
2999 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
3001 struct dpipe_ctx
*ctx
= data
;
3002 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3003 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3006 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3007 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3008 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
3009 return MNL_CB_ERROR
;
3010 err
= dpipe_headers_get(ctx
, tb
);
3013 return MNL_CB_ERROR
;
3016 if (ctx
->print_headers
)
3017 pr_out_dpipe_headers(ctx
, tb
);
3021 static int cmd_dpipe_headers_show(struct dl
*dl
)
3023 struct nlmsghdr
*nlh
;
3024 struct dpipe_ctx
*ctx
;
3025 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3028 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3030 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3034 ctx
= dpipe_ctx_alloc(dl
);
3038 ctx
->print_headers
= true;
3040 pr_out_section_start(dl
, "header");
3041 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3043 pr_err("error get headers %s\n", strerror(ctx
->err
));
3044 pr_out_section_end(dl
);
3046 dpipe_ctx_clear(ctx
);
3047 dpipe_ctx_free(ctx
);
3051 static void cmd_dpipe_header_help(void)
3053 pr_err("Usage: devlink dpipe headers show DEV\n");
3056 static int cmd_dpipe_header(struct dl
*dl
)
3058 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3059 cmd_dpipe_header_help();
3061 } else if (dl_argv_match(dl
, "show")) {
3063 return cmd_dpipe_headers_show(dl
);
3065 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3070 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
3072 switch (action_type
) {
3073 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
3074 return "field_modify";
3080 struct dpipe_op_info
{
3086 struct dpipe_action
{
3087 struct dpipe_op_info info
;
3091 static void pr_out_dpipe_action(struct dpipe_action
*action
,
3092 struct dpipe_ctx
*ctx
)
3094 struct dpipe_op_info
*op_info
= &action
->info
;
3095 const char *mapping
;
3097 pr_out_str(ctx
->dl
, "type",
3098 dpipe_action_type_e2s(action
->type
));
3099 pr_out_str(ctx
->dl
, "header",
3100 dpipe_header_id2s(ctx
, op_info
->header_id
,
3101 op_info
->header_global
));
3102 pr_out_str(ctx
->dl
, "field",
3103 dpipe_field_id2s(ctx
, op_info
->header_id
,
3105 op_info
->header_global
));
3106 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3108 op_info
->header_global
);
3110 pr_out_str(ctx
->dl
, "mapping", mapping
);
3113 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
3115 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
3118 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
3119 if (err
!= MNL_CB_OK
)
3122 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
3123 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3124 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3125 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3129 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
3130 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3131 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3132 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3137 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
3138 struct nlattr
*nla_actions
)
3140 struct nlattr
*nla_action
;
3141 struct dpipe_action action
;
3143 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
3144 pr_out_entry_start(ctx
->dl
);
3145 if (dpipe_action_parse(&action
, nla_action
))
3146 goto err_action_parse
;
3147 pr_out_dpipe_action(&action
, ctx
);
3148 pr_out_entry_end(ctx
->dl
);
3153 pr_out_entry_end(ctx
->dl
);
3158 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
3160 switch (match_type
) {
3161 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
3162 return "field_exact";
3168 struct dpipe_match
{
3169 struct dpipe_op_info info
;
3173 static void pr_out_dpipe_match(struct dpipe_match
*match
,
3174 struct dpipe_ctx
*ctx
)
3176 struct dpipe_op_info
*op_info
= &match
->info
;
3177 const char *mapping
;
3179 pr_out_str(ctx
->dl
, "type",
3180 dpipe_match_type_e2s(match
->type
));
3181 pr_out_str(ctx
->dl
, "header",
3182 dpipe_header_id2s(ctx
, op_info
->header_id
,
3183 op_info
->header_global
));
3184 pr_out_str(ctx
->dl
, "field",
3185 dpipe_field_id2s(ctx
, op_info
->header_id
,
3187 op_info
->header_global
));
3188 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3190 op_info
->header_global
);
3192 pr_out_str(ctx
->dl
, "mapping", mapping
);
3195 static int dpipe_match_parse(struct dpipe_match
*match
,
3199 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
3202 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
3203 if (err
!= MNL_CB_OK
)
3206 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
3207 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3208 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3209 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3213 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
3214 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3215 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3216 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3221 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
3222 struct nlattr
*nla_matches
)
3224 struct nlattr
*nla_match
;
3225 struct dpipe_match match
;
3227 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
3228 pr_out_entry_start(ctx
->dl
);
3229 if (dpipe_match_parse(&match
, nla_match
))
3230 goto err_match_parse
;
3231 pr_out_dpipe_match(&match
, ctx
);
3232 pr_out_entry_end(ctx
->dl
);
3237 pr_out_entry_end(ctx
->dl
);
3241 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3243 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
3244 bool counters_enabled
;
3249 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
3250 if (err
!= MNL_CB_OK
)
3253 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
3254 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
3255 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
3256 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
3257 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
3261 name
= mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
3262 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
3263 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
3265 pr_out_str(ctx
->dl
, "name", name
);
3266 pr_out_uint(ctx
->dl
, "size", size
);
3267 pr_out_str(ctx
->dl
, "counters_enabled",
3268 counters_enabled
? "true" : "false");
3270 pr_out_array_start(ctx
->dl
, "match");
3271 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
3272 goto err_matches_show
;
3273 pr_out_array_end(ctx
->dl
);
3275 pr_out_array_start(ctx
->dl
, "action");
3276 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
3277 goto err_actions_show
;
3278 pr_out_array_end(ctx
->dl
);
3284 pr_out_array_end(ctx
->dl
);
3288 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3290 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
3291 struct nlattr
*nla_table
;
3293 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
3294 pr_out_handle_start_arr(ctx
->dl
, tb
);
3295 if (dpipe_table_show(ctx
, nla_table
))
3296 goto err_table_show
;
3297 pr_out_handle_end(ctx
->dl
);
3302 pr_out_handle_end(ctx
->dl
);
3306 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3308 struct dpipe_ctx
*ctx
= data
;
3309 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3310 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3312 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3313 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3314 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
3315 return MNL_CB_ERROR
;
3317 if (dpipe_tables_show(ctx
, tb
))
3318 return MNL_CB_ERROR
;
3322 static int cmd_dpipe_table_show(struct dl
*dl
)
3324 struct nlmsghdr
*nlh
;
3325 struct dpipe_ctx
*ctx
;
3326 uint16_t flags
= NLM_F_REQUEST
;
3329 ctx
= dpipe_ctx_alloc(dl
);
3333 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
3337 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3338 dl_opts_put(nlh
, dl
);
3339 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3341 pr_err("error get headers %s\n", strerror(ctx
->err
));
3345 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3346 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
3347 dl_opts_put(nlh
, dl
);
3349 pr_out_section_start(dl
, "table");
3350 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, ctx
);
3351 pr_out_section_end(dl
);
3353 dpipe_ctx_clear(ctx
);
3354 dpipe_ctx_free(ctx
);
3358 static int cmd_dpipe_table_set(struct dl
*dl
)
3360 struct nlmsghdr
*nlh
;
3363 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
3364 NLM_F_REQUEST
| NLM_F_ACK
);
3366 err
= dl_argv_parse_put(nlh
, dl
,
3367 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
3368 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
3372 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3375 enum dpipe_value_type
{
3376 DPIPE_VALUE_TYPE_VALUE
,
3377 DPIPE_VALUE_TYPE_MASK
,
3381 dpipe_value_type_e2s(enum dpipe_value_type type
)
3384 case DPIPE_VALUE_TYPE_VALUE
:
3386 case DPIPE_VALUE_TYPE_MASK
:
3387 return "value_mask";
3393 struct dpipe_field_printer
{
3394 unsigned int field_id
;
3395 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
3398 struct dpipe_header_printer
{
3399 struct dpipe_field_printer
*printers
;
3400 unsigned int printers_count
;
3401 unsigned int header_id
;
3404 static struct dpipe_header_printer
*dpipe_header_printers
[] = {};
3406 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
3407 struct dpipe_op_info
*info
,
3408 enum dpipe_value_type type
,
3411 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
3412 struct dpipe_header_printer
*header_printer
;
3413 struct dpipe_field_printer
*field_printer
;
3414 unsigned int field_printers_count
;
3418 for (i
= 0; i
< header_printers_count
; i
++) {
3419 header_printer
= dpipe_header_printers
[i
];
3420 if (header_printer
->header_id
!= info
->header_id
)
3422 field_printers_count
= header_printer
->printers_count
;
3423 for (j
= 0; j
< field_printers_count
; j
++) {
3424 field_printer
= &header_printer
->printers
[j
];
3425 if (field_printer
->field_id
!= info
->field_id
)
3427 field_printer
->printer(ctx
, type
, value
);
3435 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
3437 unsigned int value_len
,
3438 struct dpipe_op_info
*info
,
3439 enum dpipe_value_type type
)
3441 if (info
->header_global
&&
3442 !dpipe_print_prot_header(ctx
, info
, type
, value
))
3445 if (value_len
== sizeof(uint32_t)) {
3446 uint32_t *value_32
= value
;
3448 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
3452 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
3453 struct nlattr
**nla_match_value
,
3454 struct dpipe_op_info
*info
)
3456 void *value
, *value_mask
;
3457 uint32_t value_mapping
;
3461 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
3462 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
3464 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3465 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3468 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
3469 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
3473 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3474 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
3475 DPIPE_VALUE_TYPE_MASK
);
3478 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
3481 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
3484 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3485 struct dpipe_match match
;
3488 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
3489 if (err
!= MNL_CB_OK
)
3492 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
3493 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3497 pr_out_entry_start(ctx
->dl
);
3498 if (dpipe_match_parse(&match
,
3499 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
3500 goto err_match_parse
;
3501 pr_out_dpipe_match(&match
, ctx
);
3502 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
3503 pr_out_entry_end(ctx
->dl
);
3508 pr_out_entry_end(ctx
->dl
);
3512 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
3515 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3516 struct dpipe_action action
;
3519 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
3520 if (err
!= MNL_CB_OK
)
3523 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
3524 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3528 pr_out_entry_start(ctx
->dl
);
3529 if (dpipe_action_parse(&action
,
3530 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
3531 goto err_action_parse
;
3532 pr_out_dpipe_action(&action
, ctx
);
3533 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
3534 pr_out_entry_end(ctx
->dl
);
3539 pr_out_entry_end(ctx
->dl
);
3544 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
3545 struct nlattr
*nla_action_values
)
3547 struct nlattr
*nla_action_value
;
3549 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
3550 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
3557 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
3558 struct nlattr
*nla_match_values
)
3560 struct nlattr
*nla_match_value
;
3562 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
3563 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
3569 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3571 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
3572 uint32_t entry_index
;
3576 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
3577 if (err
!= MNL_CB_OK
)
3580 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
3581 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
3582 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
3586 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
3587 pr_out_uint(ctx
->dl
, "index", entry_index
);
3589 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
3590 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
3591 pr_out_uint(ctx
->dl
, "counter", counter
);
3594 pr_out_array_start(ctx
->dl
, "match_value");
3595 if (dpipe_tables_match_values_show(ctx
,
3596 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
3597 goto err_match_values_show
;
3598 pr_out_array_end(ctx
->dl
);
3600 pr_out_array_start(ctx
->dl
, "action_value");
3601 if (dpipe_tables_action_values_show(ctx
,
3602 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
3603 goto err_action_values_show
;
3604 pr_out_array_end(ctx
->dl
);
3607 err_action_values_show
:
3608 err_match_values_show
:
3609 pr_out_array_end(ctx
->dl
);
3613 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3615 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
3616 struct nlattr
*nla_entry
;
3618 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
3619 pr_out_handle_start_arr(ctx
->dl
, tb
);
3620 if (dpipe_entry_show(ctx
, nla_entry
))
3621 goto err_entry_show
;
3622 pr_out_handle_end(ctx
->dl
);
3627 pr_out_handle_end(ctx
->dl
);
3631 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
3633 struct dpipe_ctx
*ctx
= data
;
3634 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3635 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3637 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3638 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3639 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
3640 return MNL_CB_ERROR
;
3642 if (dpipe_table_entries_show(ctx
, tb
))
3643 return MNL_CB_ERROR
;
3647 static int cmd_dpipe_table_dump(struct dl
*dl
)
3649 struct nlmsghdr
*nlh
;
3650 struct dpipe_ctx
*ctx
;
3651 uint16_t flags
= NLM_F_REQUEST
;
3654 ctx
= dpipe_ctx_alloc(dl
);
3658 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
3662 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3663 dl_opts_put(nlh
, dl
);
3664 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3666 pr_err("error get headers %s\n", strerror(ctx
->err
));
3670 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3671 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
3672 dl_opts_put(nlh
, dl
);
3674 pr_out_section_start(dl
, "table_entry");
3675 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, ctx
);
3676 pr_out_section_end(dl
);
3678 dpipe_ctx_clear(ctx
);
3679 dpipe_ctx_free(ctx
);
3683 static void cmd_dpipe_table_help(void)
3685 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
3686 "where OBJECT-LIST := { show | set | dump }\n");
3689 static int cmd_dpipe_table(struct dl
*dl
)
3691 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3692 cmd_dpipe_table_help();
3694 } else if (dl_argv_match(dl
, "show")) {
3696 return cmd_dpipe_table_show(dl
);
3697 } else if (dl_argv_match(dl
, "set")) {
3699 return cmd_dpipe_table_set(dl
);
3700 } else if (dl_argv_match(dl
, "dump")) {
3702 return cmd_dpipe_table_dump(dl
);
3704 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3708 static void cmd_dpipe_help(void)
3710 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
3711 "where OBJECT-LIST := { header | table }\n");
3714 static int cmd_dpipe(struct dl
*dl
)
3716 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3719 } else if (dl_argv_match(dl
, "header")) {
3721 return cmd_dpipe_header(dl
);
3722 } else if (dl_argv_match(dl
, "table")) {
3724 return cmd_dpipe_table(dl
);
3726 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3730 static void help(void)
3732 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
3733 "where OBJECT := { dev | port | sb | monitor | dpipe }\n"
3734 " OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
3737 static int dl_cmd(struct dl
*dl
)
3739 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3742 } else if (dl_argv_match(dl
, "dev")) {
3745 } else if (dl_argv_match(dl
, "port")) {
3747 return cmd_port(dl
);
3748 } else if (dl_argv_match(dl
, "sb")) {
3751 } else if (dl_argv_match(dl
, "monitor")) {
3754 } else if (dl_argv_match(dl
, "dpipe")) {
3756 return cmd_dpipe(dl
);
3758 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
3762 static int dl_init(struct dl
*dl
, int argc
, char **argv
)
3769 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3771 pr_err("Failed to connect to devlink Netlink\n");
3775 err
= ifname_map_init(dl
);
3777 pr_err("Failed to create index map\n");
3778 goto err_ifname_map_create
;
3780 if (dl
->json_output
) {
3781 dl
->jw
= jsonw_new(stdout
);
3783 pr_err("Failed to create JSON writer\n");
3786 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
3791 ifname_map_fini(dl
);
3792 err_ifname_map_create
:
3793 mnlg_socket_close(dl
->nlg
);
3797 static void dl_fini(struct dl
*dl
)
3799 if (dl
->json_output
)
3800 jsonw_destroy(&dl
->jw
);
3801 ifname_map_fini(dl
);
3802 mnlg_socket_close(dl
->nlg
);
3805 static struct dl
*dl_alloc(void)
3809 dl
= calloc(1, sizeof(*dl
));
3815 static void dl_free(struct dl
*dl
)
3820 int main(int argc
, char **argv
)
3822 static const struct option long_options
[] = {
3823 { "Version", no_argument
, NULL
, 'V' },
3824 { "no-nice-names", no_argument
, NULL
, 'n' },
3825 { "json", no_argument
, NULL
, 'j' },
3826 { "pretty", no_argument
, NULL
, 'p' },
3827 { "verbose", no_argument
, NULL
, 'v' },
3828 { NULL
, 0, NULL
, 0 }
3837 pr_err("Failed to allocate memory for devlink\n");
3838 return EXIT_FAILURE
;
3841 while ((opt
= getopt_long(argc
, argv
, "Vnjpv",
3842 long_options
, NULL
)) >= 0) {
3846 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
3850 dl
->no_nice_names
= true;
3853 dl
->json_output
= true;
3856 dl
->pretty_output
= true;
3862 pr_err("Unknown option.\n");
3872 err
= dl_init(dl
, argc
, argv
);