2 * devlink.c Devlink tool
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
20 #include <linux/genetlink.h>
21 #include <linux/devlink.h>
22 #include <libmnl/libmnl.h>
23 #include <netinet/ether.h>
28 #include "json_writer.h"
31 #define ESWITCH_MODE_LEGACY "legacy"
32 #define ESWITCH_MODE_SWITCHDEV "switchdev"
33 #define ESWITCH_INLINE_MODE_NONE "none"
34 #define ESWITCH_INLINE_MODE_LINK "link"
35 #define ESWITCH_INLINE_MODE_NETWORK "network"
36 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
38 static int g_new_line_count
;
40 #define pr_err(args...) fprintf(stderr, ##args)
41 #define pr_out(args...) \
43 if (g_indent_newline) { \
44 fprintf(stdout, "%s", g_indent_str); \
45 g_indent_newline = false; \
47 fprintf(stdout, ##args); \
48 g_new_line_count = 0; \
51 #define pr_out_sp(num, args...) \
53 int ret = fprintf(stdout, ##args); \
55 fprintf(stdout, "%*s", num - ret, ""); \
56 g_new_line_count = 0; \
59 static int g_indent_level
;
60 static bool g_indent_newline
;
61 #define INDENT_STR_STEP 2
62 #define INDENT_STR_MAXLEN 32
63 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
65 static void __pr_out_indent_inc(void)
67 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
69 g_indent_level
+= INDENT_STR_STEP
;
70 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
71 g_indent_str
[g_indent_level
] = '\0';
74 static void __pr_out_indent_dec(void)
76 if (g_indent_level
- INDENT_STR_STEP
< 0)
78 g_indent_level
-= INDENT_STR_STEP
;
79 g_indent_str
[g_indent_level
] = '\0';
82 static void __pr_out_newline(void)
84 if (g_new_line_count
< 1) {
86 g_indent_newline
= true;
91 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
92 mnl_cb_t data_cb
, void *data
)
96 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
98 pr_err("devlink answers: %s\n", strerror(errno
));
104 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
105 const struct nlmsghdr
*nlh
,
106 mnl_cb_t data_cb
, void *data
)
110 err
= mnlg_socket_send(nlg
, nlh
);
112 pr_err("Failed to call mnlg_socket_send\n");
115 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
118 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
119 const char *group_name
)
123 err
= mnlg_socket_group_add(nlg
, group_name
);
125 pr_err("Failed to call mnlg_socket_group_add\n");
132 struct list_head list
;
139 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
140 const char *dev_name
,
144 struct ifname_map
*ifname_map
;
146 ifname_map
= calloc(1, sizeof(*ifname_map
));
149 ifname_map
->bus_name
= strdup(bus_name
);
150 ifname_map
->dev_name
= strdup(dev_name
);
151 ifname_map
->port_index
= port_index
;
152 ifname_map
->ifname
= strdup(ifname
);
153 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
154 !ifname_map
->ifname
) {
155 free(ifname_map
->ifname
);
156 free(ifname_map
->dev_name
);
157 free(ifname_map
->bus_name
);
164 static void ifname_map_free(struct ifname_map
*ifname_map
)
166 free(ifname_map
->ifname
);
167 free(ifname_map
->dev_name
);
168 free(ifname_map
->bus_name
);
172 #define DL_OPT_HANDLE BIT(0)
173 #define DL_OPT_HANDLEP BIT(1)
174 #define DL_OPT_PORT_TYPE BIT(2)
175 #define DL_OPT_PORT_COUNT BIT(3)
176 #define DL_OPT_SB BIT(4)
177 #define DL_OPT_SB_POOL BIT(5)
178 #define DL_OPT_SB_SIZE BIT(6)
179 #define DL_OPT_SB_TYPE BIT(7)
180 #define DL_OPT_SB_THTYPE BIT(8)
181 #define DL_OPT_SB_TH BIT(9)
182 #define DL_OPT_SB_TC BIT(10)
183 #define DL_OPT_ESWITCH_MODE BIT(11)
184 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
185 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
186 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
187 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
190 uint32_t present
; /* flags of present items */
194 enum devlink_port_type port_type
;
197 uint16_t sb_pool_index
;
198 uint32_t sb_pool_size
;
199 enum devlink_sb_pool_type sb_pool_type
;
200 enum devlink_sb_threshold_type sb_pool_thtype
;
201 uint32_t sb_threshold
;
202 uint16_t sb_tc_index
;
203 enum devlink_eswitch_mode eswitch_mode
;
204 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
205 const char *dpipe_table_name
;
206 bool dpipe_counters_enable
;
207 bool eswitch_encap_mode
;
211 struct mnlg_socket
*nlg
;
212 struct list_head ifname_map_list
;
229 static int dl_argc(struct dl
*dl
)
234 static char *dl_argv(struct dl
*dl
)
236 if (dl_argc(dl
) == 0)
241 static void dl_arg_inc(struct dl
*dl
)
243 if (dl_argc(dl
) == 0)
249 static char *dl_argv_next(struct dl
*dl
)
253 if (dl_argc(dl
) == 0)
261 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
263 if (index
>= dl_argc(dl
))
265 return dl
->argv
[index
];
268 static int strcmpx(const char *str1
, const char *str2
)
270 if (strlen(str1
) > strlen(str2
))
272 return strncmp(str1
, str2
, strlen(str1
));
275 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
277 if (dl_argc(dl
) == 0)
279 return strcmpx(dl_argv(dl
), pattern
) == 0;
282 static bool dl_no_arg(struct dl
*dl
)
284 return dl_argc(dl
) == 0;
287 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
288 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
289 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
290 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
291 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
292 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
293 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
294 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
295 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
296 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
297 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
298 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
299 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
300 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
301 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
302 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
303 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
304 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
305 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
306 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
307 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
308 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
309 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
310 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
311 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
312 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
313 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
314 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
315 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
316 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
317 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
318 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
319 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
320 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
321 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
322 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
323 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
324 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
325 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
326 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
327 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
328 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
329 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
330 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
331 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
332 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
333 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
334 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
335 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
336 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
337 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
338 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
339 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
340 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
341 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
342 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
343 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
344 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
347 static int attr_cb(const struct nlattr
*attr
, void *data
)
349 const struct nlattr
**tb
= data
;
352 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
355 type
= mnl_attr_get_type(attr
);
356 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
363 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
365 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
366 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
367 struct dl
*dl
= data
;
368 struct ifname_map
*ifname_map
;
369 const char *bus_name
;
370 const char *dev_name
;
371 uint32_t port_ifindex
;
372 const char *port_ifname
;
374 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
375 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
376 !tb
[DEVLINK_ATTR_PORT_INDEX
])
379 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
382 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
383 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
384 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
385 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
386 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
387 port_ifindex
, port_ifname
);
390 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
395 static void ifname_map_fini(struct dl
*dl
)
397 struct ifname_map
*ifname_map
, *tmp
;
399 list_for_each_entry_safe(ifname_map
, tmp
,
400 &dl
->ifname_map_list
, list
) {
401 list_del(&ifname_map
->list
);
402 ifname_map_free(ifname_map
);
406 static int ifname_map_init(struct dl
*dl
)
408 struct nlmsghdr
*nlh
;
411 INIT_LIST_HEAD(&dl
->ifname_map_list
);
413 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
414 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
416 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
424 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
425 char **p_bus_name
, char **p_dev_name
,
426 uint32_t *p_port_index
)
428 struct ifname_map
*ifname_map
;
430 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
431 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
432 *p_bus_name
= ifname_map
->bus_name
;
433 *p_dev_name
= ifname_map
->dev_name
;
434 *p_port_index
= ifname_map
->port_index
;
441 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
442 const char *dev_name
, uint32_t port_index
,
445 struct ifname_map
*ifname_map
;
447 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
448 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
449 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
450 port_index
== ifname_map
->port_index
) {
451 *p_ifname
= ifname_map
->ifname
;
458 static unsigned int strslashcount(char *str
)
460 unsigned int count
= 0;
463 while ((pos
= strchr(pos
, '/'))) {
470 static int strslashrsplit(char *str
, char **before
, char **after
)
474 slash
= strrchr(str
, '/');
483 static int strtouint32_t(const char *str
, uint32_t *p_val
)
486 unsigned long int val
;
488 val
= strtoul(str
, &endptr
, 10);
489 if (endptr
== str
|| *endptr
!= '\0')
497 static int strtouint16_t(const char *str
, uint16_t *p_val
)
500 unsigned long int val
;
502 val
= strtoul(str
, &endptr
, 10);
503 if (endptr
== str
|| *endptr
!= '\0')
511 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
513 strslashrsplit(str
, p_bus_name
, p_dev_name
);
517 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
519 char *str
= dl_argv_next(dl
);
522 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
525 if (strslashcount(str
) != 1) {
526 pr_err("Wrong devlink identification string format.\n");
527 pr_err("Expected \"bus_name/dev_name\".\n");
530 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
533 static int __dl_argv_handle_port(char *str
,
534 char **p_bus_name
, char **p_dev_name
,
535 uint32_t *p_port_index
)
541 err
= strslashrsplit(str
, &handlestr
, &portstr
);
543 pr_err("Port identification \"%s\" is invalid\n", str
);
546 err
= strtouint32_t(portstr
, p_port_index
);
548 pr_err("Port index \"%s\" is not a number or not within range\n",
552 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
554 pr_err("Port identification \"%s\" is invalid\n", str
);
560 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
561 char **p_bus_name
, char **p_dev_name
,
562 uint32_t *p_port_index
)
566 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
569 pr_err("Netdevice \"%s\" not found\n", str
);
575 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
576 char **p_dev_name
, uint32_t *p_port_index
)
578 char *str
= dl_argv_next(dl
);
579 unsigned int slash_count
;
582 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
585 slash_count
= strslashcount(str
);
586 switch (slash_count
) {
588 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
589 p_dev_name
, p_port_index
);
591 return __dl_argv_handle_port(str
, p_bus_name
,
592 p_dev_name
, p_port_index
);
594 pr_err("Wrong port identification string format.\n");
595 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
600 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
601 char **p_dev_name
, uint32_t *p_port_index
,
602 uint32_t *p_handle_bit
)
604 char *str
= dl_argv_next(dl
);
605 unsigned int slash_count
;
609 pr_err("One of following identifications expected:\n"
610 "Devlink identification (\"bus_name/dev_name\")\n"
611 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
614 slash_count
= strslashcount(str
);
615 if (slash_count
== 1) {
616 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
619 *p_handle_bit
= DL_OPT_HANDLE
;
620 } else if (slash_count
== 2) {
621 err
= __dl_argv_handle_port(str
, p_bus_name
,
622 p_dev_name
, p_port_index
);
625 *p_handle_bit
= DL_OPT_HANDLEP
;
626 } else if (slash_count
== 0) {
627 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
628 p_dev_name
, p_port_index
);
631 *p_handle_bit
= DL_OPT_HANDLEP
;
633 pr_err("Wrong port identification string format.\n");
634 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
640 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
642 char *str
= dl_argv_next(dl
);
646 pr_err("Unsigned number argument expected\n");
650 err
= strtouint32_t(str
, p_val
);
652 pr_err("\"%s\" is not a number or not within range\n", str
);
658 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
660 char *str
= dl_argv_next(dl
);
664 pr_err("Unsigned number argument expected\n");
668 err
= strtouint16_t(str
, p_val
);
670 pr_err("\"%s\" is not a number or not within range\n", str
);
676 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
678 const char *str
= dl_argv_next(dl
);
681 pr_err("String parameter expected\n");
688 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
690 if (strcmp(typestr
, "auto") == 0) {
691 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
692 } else if (strcmp(typestr
, "eth") == 0) {
693 *p_type
= DEVLINK_PORT_TYPE_ETH
;
694 } else if (strcmp(typestr
, "ib") == 0) {
695 *p_type
= DEVLINK_PORT_TYPE_IB
;
697 pr_err("Unknown port type \"%s\"\n", typestr
);
703 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
705 if (strcmp(typestr
, "ingress") == 0) {
706 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
707 } else if (strcmp(typestr
, "egress") == 0) {
708 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
710 pr_err("Unknown pool type \"%s\"\n", typestr
);
716 static int threshold_type_get(const char *typestr
,
717 enum devlink_sb_threshold_type
*p_type
)
719 if (strcmp(typestr
, "static") == 0) {
720 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
721 } else if (strcmp(typestr
, "dynamic") == 0) {
722 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
724 pr_err("Unknown threshold type \"%s\"\n", typestr
);
730 static int eswitch_mode_get(const char *typestr
,
731 enum devlink_eswitch_mode
*p_mode
)
733 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
734 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
735 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
736 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
738 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
744 static int eswitch_inline_mode_get(const char *typestr
,
745 enum devlink_eswitch_inline_mode
*p_mode
)
747 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
748 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
749 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
750 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
751 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
752 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
753 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
754 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
756 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
762 static int dpipe_counters_enable_get(const char *typestr
,
763 bool *counters_enable
)
765 if (strcmp(typestr
, "enable") == 0) {
766 *counters_enable
= 1;
767 } else if (strcmp(typestr
, "disable") == 0) {
768 *counters_enable
= 0;
770 pr_err("Unknown counter_state \"%s\"\n", typestr
);
776 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
778 if (strcmp(typestr
, "enable") == 0) {
780 } else if (strcmp(typestr
, "disable") == 0) {
783 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
789 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
792 struct dl_opts
*opts
= &dl
->opts
;
793 uint32_t o_all
= o_required
| o_optional
;
794 uint32_t o_found
= 0;
797 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
800 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
801 &opts
->port_index
, &handle_bit
);
804 o_found
|= handle_bit
;
805 } else if (o_required
& DL_OPT_HANDLE
) {
806 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
809 o_found
|= DL_OPT_HANDLE
;
810 } else if (o_required
& DL_OPT_HANDLEP
) {
811 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
815 o_found
|= DL_OPT_HANDLEP
;
818 while (dl_argc(dl
)) {
819 if (dl_argv_match(dl
, "type") &&
820 (o_all
& DL_OPT_PORT_TYPE
)) {
824 err
= dl_argv_str(dl
, &typestr
);
827 err
= port_type_get(typestr
, &opts
->port_type
);
830 o_found
|= DL_OPT_PORT_TYPE
;
831 } else if (dl_argv_match(dl
, "count") &&
832 (o_all
& DL_OPT_PORT_COUNT
)) {
834 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
837 o_found
|= DL_OPT_PORT_COUNT
;
838 } else if (dl_argv_match(dl
, "sb") &&
839 (o_all
& DL_OPT_SB
)) {
841 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
844 o_found
|= DL_OPT_SB
;
845 } else if (dl_argv_match(dl
, "pool") &&
846 (o_all
& DL_OPT_SB_POOL
)) {
848 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
851 o_found
|= DL_OPT_SB_POOL
;
852 } else if (dl_argv_match(dl
, "size") &&
853 (o_all
& DL_OPT_SB_SIZE
)) {
855 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
858 o_found
|= DL_OPT_SB_SIZE
;
859 } else if (dl_argv_match(dl
, "type") &&
860 (o_all
& DL_OPT_SB_TYPE
)) {
864 err
= dl_argv_str(dl
, &typestr
);
867 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
870 o_found
|= DL_OPT_SB_TYPE
;
871 } else if (dl_argv_match(dl
, "thtype") &&
872 (o_all
& DL_OPT_SB_THTYPE
)) {
876 err
= dl_argv_str(dl
, &typestr
);
879 err
= threshold_type_get(typestr
,
880 &opts
->sb_pool_thtype
);
883 o_found
|= DL_OPT_SB_THTYPE
;
884 } else if (dl_argv_match(dl
, "th") &&
885 (o_all
& DL_OPT_SB_TH
)) {
887 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
890 o_found
|= DL_OPT_SB_TH
;
891 } else if (dl_argv_match(dl
, "tc") &&
892 (o_all
& DL_OPT_SB_TC
)) {
894 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
897 o_found
|= DL_OPT_SB_TC
;
898 } else if (dl_argv_match(dl
, "mode") &&
899 (o_all
& DL_OPT_ESWITCH_MODE
)) {
903 err
= dl_argv_str(dl
, &typestr
);
906 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
909 o_found
|= DL_OPT_ESWITCH_MODE
;
910 } else if (dl_argv_match(dl
, "inline-mode") &&
911 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
915 err
= dl_argv_str(dl
, &typestr
);
918 err
= eswitch_inline_mode_get(
919 typestr
, &opts
->eswitch_inline_mode
);
922 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
923 } else if (dl_argv_match(dl
, "name") &&
924 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
926 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
929 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
930 } else if (dl_argv_match(dl
, "counters") &&
931 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
935 err
= dl_argv_str(dl
, &typestr
);
938 err
= dpipe_counters_enable_get(typestr
,
939 &opts
->dpipe_counters_enable
);
942 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
943 } else if (dl_argv_match(dl
, "encap") &&
944 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
948 err
= dl_argv_str(dl
, &typestr
);
951 err
= eswitch_encap_mode_get(typestr
,
952 &opts
->eswitch_encap_mode
);
955 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
957 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
962 opts
->present
= o_found
;
964 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
966 opts
->present
|= DL_OPT_SB
;
969 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
970 pr_err("Port type option expected.\n");
974 if ((o_required
& DL_OPT_PORT_COUNT
) &&
975 !(o_found
& DL_OPT_PORT_COUNT
)) {
976 pr_err("Port split count option expected.\n");
980 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
981 pr_err("Pool index option expected.\n");
985 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
986 pr_err("Pool size option expected.\n");
990 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
991 pr_err("Pool type option expected.\n");
995 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
996 pr_err("Pool threshold type option expected.\n");
1000 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
1001 pr_err("Threshold option expected.\n");
1005 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
1006 pr_err("TC index option expected.\n");
1010 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
1011 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
1012 pr_err("E-Switch mode option expected.\n");
1016 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
1017 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1018 pr_err("E-Switch inline-mode option expected.\n");
1022 if ((o_required
& DL_OPT_DPIPE_TABLE_NAME
) &&
1023 !(o_found
& DL_OPT_DPIPE_TABLE_NAME
)) {
1024 pr_err("Dpipe table name expected\n");
1028 if ((o_required
& DL_OPT_DPIPE_TABLE_COUNTERS
) &&
1029 !(o_found
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1030 pr_err("Dpipe table counter state expected\n");
1034 if ((o_required
& DL_OPT_ESWITCH_ENCAP_MODE
) &&
1035 !(o_found
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1036 pr_err("E-Switch encapsulation option expected.\n");
1043 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1045 struct dl_opts
*opts
= &dl
->opts
;
1047 if (opts
->present
& DL_OPT_HANDLE
) {
1048 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1049 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1050 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1051 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1052 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1053 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1056 if (opts
->present
& DL_OPT_PORT_TYPE
)
1057 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1059 if (opts
->present
& DL_OPT_PORT_COUNT
)
1060 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1062 if (opts
->present
& DL_OPT_SB
)
1063 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1065 if (opts
->present
& DL_OPT_SB_POOL
)
1066 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1067 opts
->sb_pool_index
);
1068 if (opts
->present
& DL_OPT_SB_SIZE
)
1069 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1070 opts
->sb_pool_size
);
1071 if (opts
->present
& DL_OPT_SB_TYPE
)
1072 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1073 opts
->sb_pool_type
);
1074 if (opts
->present
& DL_OPT_SB_THTYPE
)
1075 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1076 opts
->sb_pool_thtype
);
1077 if (opts
->present
& DL_OPT_SB_TH
)
1078 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1079 opts
->sb_threshold
);
1080 if (opts
->present
& DL_OPT_SB_TC
)
1081 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1083 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1084 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1085 opts
->eswitch_mode
);
1086 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1087 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1088 opts
->eswitch_inline_mode
);
1089 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1090 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1091 opts
->dpipe_table_name
);
1092 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1093 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1094 opts
->dpipe_counters_enable
);
1095 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1096 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1097 opts
->eswitch_encap_mode
);
1100 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1101 uint32_t o_required
, uint32_t o_optional
)
1105 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1108 dl_opts_put(nlh
, dl
);
1112 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1114 struct dl_opts
*opts
= &dl
->opts
;
1115 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1116 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1117 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1118 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1120 if (opts
->present
& DL_OPT_HANDLE
&&
1121 attr_bus_name
&& attr_dev_name
) {
1122 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1123 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1125 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1126 strcmp(dev_name
, opts
->dev_name
) != 0)
1129 if (opts
->present
& DL_OPT_HANDLEP
&&
1130 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1131 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1132 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1133 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1135 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1136 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1137 port_index
!= opts
->port_index
)
1140 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1141 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1143 if (sb_index
!= opts
->sb_index
)
1149 static void cmd_dev_help(void)
1151 pr_err("Usage: devlink dev show [ DEV ]\n");
1152 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1153 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1154 pr_err(" [ encap { disable | enable } ]\n");
1155 pr_err(" devlink dev eswitch show DEV\n");
1158 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1159 const char *dev_name
)
1161 if (!dl
->arr_last
.present
)
1163 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1164 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1167 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1168 const char *dev_name
)
1170 dl
->arr_last
.present
= true;
1171 free(dl
->arr_last
.dev_name
);
1172 free(dl
->arr_last
.bus_name
);
1173 dl
->arr_last
.bus_name
= strdup(bus_name
);
1174 dl
->arr_last
.dev_name
= strdup(dev_name
);
1177 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1178 const char *dev_name
)
1180 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1183 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1184 const char *dev_name
)
1186 return dl
->arr_last
.present
&&
1187 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1190 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1191 bool content
, bool array
)
1193 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1194 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1197 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1199 if (dl
->json_output
) {
1201 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1202 jsonw_end_array(dl
->jw
);
1203 if (should_arr_last_handle_start(dl
, bus_name
,
1205 jsonw_name(dl
->jw
, buf
);
1206 jsonw_start_array(dl
->jw
);
1207 jsonw_start_object(dl
->jw
);
1208 arr_last_handle_set(dl
, bus_name
, dev_name
);
1210 jsonw_start_object(dl
->jw
);
1213 jsonw_name(dl
->jw
, buf
);
1214 jsonw_start_object(dl
->jw
);
1218 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1219 __pr_out_indent_dec();
1220 if (should_arr_last_handle_start(dl
, bus_name
,
1222 pr_out("%s%s", buf
, content
? ":" : "");
1224 __pr_out_indent_inc();
1225 arr_last_handle_set(dl
, bus_name
, dev_name
);
1228 pr_out("%s%s", buf
, content
? ":" : "");
1233 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1235 __pr_out_handle_start(dl
, tb
, true, true);
1238 static void pr_out_handle_end(struct dl
*dl
)
1240 if (dl
->json_output
)
1241 jsonw_end_object(dl
->jw
);
1246 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1248 __pr_out_handle_start(dl
, tb
, false, false);
1249 pr_out_handle_end(dl
);
1252 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1253 const char *dev_name
, uint32_t port_index
)
1255 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1256 dl
->arr_last
.port_index
== port_index
;
1259 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1260 const char *dev_name
, uint32_t port_index
)
1262 arr_last_handle_set(dl
, bus_name
, dev_name
);
1263 dl
->arr_last
.port_index
= port_index
;
1266 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1267 const char *bus_name
,
1268 const char *dev_name
,
1269 uint32_t port_index
)
1271 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1274 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1275 const char *bus_name
,
1276 const char *dev_name
,
1277 uint32_t port_index
)
1279 return dl
->arr_last
.present
&&
1280 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1283 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1284 const char *dev_name
,
1285 uint32_t port_index
, bool try_nice
,
1288 static char buf
[32];
1289 char *ifname
= NULL
;
1291 if (dl
->no_nice_names
|| !try_nice
||
1292 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1293 port_index
, &ifname
) != 0)
1294 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1296 sprintf(buf
, "%s", ifname
);
1298 if (dl
->json_output
) {
1300 if (should_arr_last_port_handle_end(dl
, bus_name
,
1303 jsonw_end_array(dl
->jw
);
1304 if (should_arr_last_port_handle_start(dl
, bus_name
,
1307 jsonw_name(dl
->jw
, buf
);
1308 jsonw_start_array(dl
->jw
);
1309 jsonw_start_object(dl
->jw
);
1310 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1313 jsonw_start_object(dl
->jw
);
1316 jsonw_name(dl
->jw
, buf
);
1317 jsonw_start_object(dl
->jw
);
1324 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1326 const char *bus_name
;
1327 const char *dev_name
;
1328 uint32_t port_index
;
1330 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1331 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1332 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1333 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1336 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1338 const char *bus_name
;
1339 const char *dev_name
;
1340 uint32_t port_index
;
1342 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1343 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1344 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1345 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1348 static void pr_out_port_handle_end(struct dl
*dl
)
1350 if (dl
->json_output
)
1351 jsonw_end_object(dl
->jw
);
1357 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1359 if (dl
->json_output
) {
1360 jsonw_string_field(dl
->jw
, name
, val
);
1362 if (g_indent_newline
)
1363 pr_out("%s %s", name
, val
);
1365 pr_out(" %s %s", name
, val
);
1369 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1371 if (dl
->json_output
) {
1372 jsonw_uint_field(dl
->jw
, name
, val
);
1374 if (g_indent_newline
)
1375 pr_out("%s %u", name
, val
);
1377 pr_out(" %s %u", name
, val
);
1381 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1383 pr_out_handle(dl
, tb
);
1386 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1388 if (dl
->json_output
) {
1389 jsonw_start_object(dl
->jw
);
1390 jsonw_name(dl
->jw
, name
);
1391 jsonw_start_object(dl
->jw
);
1395 static void pr_out_section_end(struct dl
*dl
)
1397 if (dl
->json_output
) {
1398 if (dl
->arr_last
.present
)
1399 jsonw_end_array(dl
->jw
);
1400 jsonw_end_object(dl
->jw
);
1401 jsonw_end_object(dl
->jw
);
1405 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1407 if (dl
->json_output
) {
1408 jsonw_name(dl
->jw
, name
);
1409 jsonw_start_array(dl
->jw
);
1411 __pr_out_indent_inc();
1413 pr_out("%s:", name
);
1414 __pr_out_indent_inc();
1419 static void pr_out_array_end(struct dl
*dl
)
1421 if (dl
->json_output
) {
1422 jsonw_end_array(dl
->jw
);
1424 __pr_out_indent_dec();
1425 __pr_out_indent_dec();
1429 static void pr_out_entry_start(struct dl
*dl
)
1431 if (dl
->json_output
)
1432 jsonw_start_object(dl
->jw
);
1435 static void pr_out_entry_end(struct dl
*dl
)
1437 if (dl
->json_output
)
1438 jsonw_end_object(dl
->jw
);
1443 static const char *eswitch_mode_name(uint32_t mode
)
1446 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1447 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1448 default: return "<unknown mode>";
1452 static const char *eswitch_inline_mode_name(uint32_t mode
)
1455 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1456 return ESWITCH_INLINE_MODE_NONE
;
1457 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1458 return ESWITCH_INLINE_MODE_LINK
;
1459 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1460 return ESWITCH_INLINE_MODE_NETWORK
;
1461 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1462 return ESWITCH_INLINE_MODE_TRANSPORT
;
1464 return "<unknown mode>";
1468 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1470 __pr_out_handle_start(dl
, tb
, true, false);
1472 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1473 pr_out_str(dl
, "mode",
1474 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1476 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1477 pr_out_str(dl
, "inline-mode",
1478 eswitch_inline_mode_name(mnl_attr_get_u8(
1479 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1481 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1482 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1484 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1487 pr_out_handle_end(dl
);
1490 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1492 struct dl
*dl
= data
;
1493 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1494 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1496 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1497 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1498 return MNL_CB_ERROR
;
1499 pr_out_eswitch(dl
, tb
);
1503 static int cmd_dev_eswitch_show(struct dl
*dl
)
1505 struct nlmsghdr
*nlh
;
1508 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1509 NLM_F_REQUEST
| NLM_F_ACK
);
1511 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1515 pr_out_section_start(dl
, "dev");
1516 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1517 pr_out_section_end(dl
);
1521 static int cmd_dev_eswitch_set(struct dl
*dl
)
1523 struct nlmsghdr
*nlh
;
1526 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1527 NLM_F_REQUEST
| NLM_F_ACK
);
1529 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1530 DL_OPT_ESWITCH_MODE
|
1531 DL_OPT_ESWITCH_INLINE_MODE
|
1532 DL_OPT_ESWITCH_ENCAP_MODE
);
1537 if (dl
->opts
.present
== 1) {
1538 pr_err("Need to set at least one option\n");
1542 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1545 static int cmd_dev_eswitch(struct dl
*dl
)
1547 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1550 } else if (dl_argv_match(dl
, "set")) {
1552 return cmd_dev_eswitch_set(dl
);
1553 } else if (dl_argv_match(dl
, "show")) {
1555 return cmd_dev_eswitch_show(dl
);
1557 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1561 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1563 struct dl
*dl
= data
;
1564 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1565 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1567 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1568 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1569 return MNL_CB_ERROR
;
1574 static int cmd_dev_show(struct dl
*dl
)
1576 struct nlmsghdr
*nlh
;
1577 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1580 if (dl_argc(dl
) == 0)
1581 flags
|= NLM_F_DUMP
;
1583 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
1585 if (dl_argc(dl
) > 0) {
1586 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1591 pr_out_section_start(dl
, "dev");
1592 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
1593 pr_out_section_end(dl
);
1597 static int cmd_dev(struct dl
*dl
)
1599 if (dl_argv_match(dl
, "help")) {
1602 } else if (dl_argv_match(dl
, "show") ||
1603 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1605 return cmd_dev_show(dl
);
1606 } else if (dl_argv_match(dl
, "eswitch")) {
1608 return cmd_dev_eswitch(dl
);
1610 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1614 static void cmd_port_help(void)
1616 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
1617 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
1618 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
1619 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
1622 static const char *port_type_name(uint32_t type
)
1625 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
1626 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
1627 case DEVLINK_PORT_TYPE_ETH
: return "eth";
1628 case DEVLINK_PORT_TYPE_IB
: return "ib";
1629 default: return "<unknown type>";
1633 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
1635 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
1636 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
1638 pr_out_port_handle_start(dl
, tb
, false);
1640 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
1642 pr_out_str(dl
, "type", port_type_name(port_type
));
1644 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
1646 if (port_type
!= des_port_type
)
1647 pr_out_str(dl
, "des_type",
1648 port_type_name(des_port_type
));
1651 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
1652 pr_out_str(dl
, "netdev",
1653 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
1654 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
1655 pr_out_str(dl
, "ibdev",
1656 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
1657 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
1658 pr_out_uint(dl
, "split_group",
1659 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
1660 pr_out_port_handle_end(dl
);
1663 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1665 struct dl
*dl
= data
;
1666 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1667 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1669 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1670 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1671 !tb
[DEVLINK_ATTR_PORT_INDEX
])
1672 return MNL_CB_ERROR
;
1673 pr_out_port(dl
, tb
);
1677 static int cmd_port_show(struct dl
*dl
)
1679 struct nlmsghdr
*nlh
;
1680 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1683 if (dl_argc(dl
) == 0)
1684 flags
|= NLM_F_DUMP
;
1686 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
1688 if (dl_argc(dl
) > 0) {
1689 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1694 pr_out_section_start(dl
, "port");
1695 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
1696 pr_out_section_end(dl
);
1700 static int cmd_port_set(struct dl
*dl
)
1702 struct nlmsghdr
*nlh
;
1705 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
1706 NLM_F_REQUEST
| NLM_F_ACK
);
1708 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
1712 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1715 static int cmd_port_split(struct dl
*dl
)
1717 struct nlmsghdr
*nlh
;
1720 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
1721 NLM_F_REQUEST
| NLM_F_ACK
);
1723 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
1727 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1730 static int cmd_port_unsplit(struct dl
*dl
)
1732 struct nlmsghdr
*nlh
;
1735 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
1736 NLM_F_REQUEST
| NLM_F_ACK
);
1738 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1742 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1745 static int cmd_port(struct dl
*dl
)
1747 if (dl_argv_match(dl
, "help")) {
1750 } else if (dl_argv_match(dl
, "show") ||
1751 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1753 return cmd_port_show(dl
);
1754 } else if (dl_argv_match(dl
, "set")) {
1756 return cmd_port_set(dl
);
1757 } else if (dl_argv_match(dl
, "split")) {
1759 return cmd_port_split(dl
);
1760 } else if (dl_argv_match(dl
, "unsplit")) {
1762 return cmd_port_unsplit(dl
);
1764 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1768 static void cmd_sb_help(void)
1770 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
1771 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
1772 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
1773 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
1774 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1775 pr_err(" pool POOL_INDEX ]\n");
1776 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1777 pr_err(" pool POOL_INDEX th THRESHOLD\n");
1778 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1779 pr_err(" type { ingress | egress } ]\n");
1780 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1781 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
1782 pr_err(" th THRESHOLD\n");
1783 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
1784 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
1785 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
1788 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
1790 pr_out_handle_start_arr(dl
, tb
);
1791 pr_out_uint(dl
, "sb",
1792 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1793 pr_out_uint(dl
, "size",
1794 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
1795 pr_out_uint(dl
, "ing_pools",
1796 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
1797 pr_out_uint(dl
, "eg_pools",
1798 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
1799 pr_out_uint(dl
, "ing_tcs",
1800 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
1801 pr_out_uint(dl
, "eg_tcs",
1802 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
1803 pr_out_handle_end(dl
);
1806 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1808 struct dl
*dl
= data
;
1809 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1810 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1812 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1813 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1814 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
1815 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
1816 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
1817 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
1818 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
1819 return MNL_CB_ERROR
;
1824 static int cmd_sb_show(struct dl
*dl
)
1826 struct nlmsghdr
*nlh
;
1827 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1830 if (dl_argc(dl
) == 0)
1831 flags
|= NLM_F_DUMP
;
1833 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
1835 if (dl_argc(dl
) > 0) {
1836 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
1841 pr_out_section_start(dl
, "sb");
1842 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
1843 pr_out_section_end(dl
);
1847 static const char *pool_type_name(uint8_t type
)
1850 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
1851 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
1852 default: return "<unknown type>";
1856 static const char *threshold_type_name(uint8_t type
)
1859 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
1860 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
1861 default: return "<unknown type>";
1865 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
1867 pr_out_handle_start_arr(dl
, tb
);
1868 pr_out_uint(dl
, "sb",
1869 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1870 pr_out_uint(dl
, "pool",
1871 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1872 pr_out_str(dl
, "type",
1873 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
1874 pr_out_uint(dl
, "size",
1875 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
1876 pr_out_str(dl
, "thtype",
1877 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
1878 pr_out_handle_end(dl
);
1881 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1883 struct dl
*dl
= data
;
1884 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1885 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1887 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1888 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1889 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
1890 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
1891 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
1892 return MNL_CB_ERROR
;
1893 pr_out_sb_pool(dl
, tb
);
1897 static int cmd_sb_pool_show(struct dl
*dl
)
1899 struct nlmsghdr
*nlh
;
1900 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1903 if (dl_argc(dl
) == 0)
1904 flags
|= NLM_F_DUMP
;
1906 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
1908 if (dl_argc(dl
) > 0) {
1909 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
1915 pr_out_section_start(dl
, "pool");
1916 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
1917 pr_out_section_end(dl
);
1921 static int cmd_sb_pool_set(struct dl
*dl
)
1923 struct nlmsghdr
*nlh
;
1926 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
1927 NLM_F_REQUEST
| NLM_F_ACK
);
1929 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
1930 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
1934 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1937 static int cmd_sb_pool(struct dl
*dl
)
1939 if (dl_argv_match(dl
, "help")) {
1942 } else if (dl_argv_match(dl
, "show") ||
1943 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1945 return cmd_sb_pool_show(dl
);
1946 } else if (dl_argv_match(dl
, "set")) {
1948 return cmd_sb_pool_set(dl
);
1950 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1954 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
1956 pr_out_port_handle_start_arr(dl
, tb
, true);
1957 pr_out_uint(dl
, "sb",
1958 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1959 pr_out_uint(dl
, "pool",
1960 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1961 pr_out_uint(dl
, "threshold",
1962 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
1963 pr_out_port_handle_end(dl
);
1966 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1968 struct dl
*dl
= data
;
1969 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1970 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1972 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1973 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1974 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
1975 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
1976 return MNL_CB_ERROR
;
1977 pr_out_sb_port_pool(dl
, tb
);
1981 static int cmd_sb_port_pool_show(struct dl
*dl
)
1983 struct nlmsghdr
*nlh
;
1984 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1987 if (dl_argc(dl
) == 0)
1988 flags
|= NLM_F_DUMP
;
1990 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
1992 if (dl_argc(dl
) > 0) {
1993 err
= dl_argv_parse_put(nlh
, dl
,
1994 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
2000 pr_out_section_start(dl
, "port_pool");
2001 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
2002 pr_out_section_end(dl
);
2006 static int cmd_sb_port_pool_set(struct dl
*dl
)
2008 struct nlmsghdr
*nlh
;
2011 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
2012 NLM_F_REQUEST
| NLM_F_ACK
);
2014 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
2015 DL_OPT_SB_TH
, DL_OPT_SB
);
2019 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2022 static int cmd_sb_port_pool(struct dl
*dl
)
2024 if (dl_argv_match(dl
, "help")) {
2027 } else if (dl_argv_match(dl
, "show") ||
2028 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2030 return cmd_sb_port_pool_show(dl
);
2031 } else if (dl_argv_match(dl
, "set")) {
2033 return cmd_sb_port_pool_set(dl
);
2035 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2039 static int cmd_sb_port(struct dl
*dl
)
2041 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2044 } else if (dl_argv_match(dl
, "pool")) {
2046 return cmd_sb_port_pool(dl
);
2048 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2052 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
2054 pr_out_port_handle_start_arr(dl
, tb
, true);
2055 pr_out_uint(dl
, "sb",
2056 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2057 pr_out_uint(dl
, "tc",
2058 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
2059 pr_out_str(dl
, "type",
2060 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2061 pr_out_uint(dl
, "pool",
2062 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2063 pr_out_uint(dl
, "threshold",
2064 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2065 pr_out_port_handle_end(dl
);
2068 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2070 struct dl
*dl
= data
;
2071 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2072 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2074 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2075 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2076 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2077 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2078 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2079 return MNL_CB_ERROR
;
2080 pr_out_sb_tc_bind(dl
, tb
);
2084 static int cmd_sb_tc_bind_show(struct dl
*dl
)
2086 struct nlmsghdr
*nlh
;
2087 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2090 if (dl_argc(dl
) == 0)
2091 flags
|= NLM_F_DUMP
;
2093 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2095 if (dl_argc(dl
) > 0) {
2096 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2097 DL_OPT_SB_TYPE
, DL_OPT_SB
);
2102 pr_out_section_start(dl
, "tc_bind");
2103 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
2104 pr_out_section_end(dl
);
2108 static int cmd_sb_tc_bind_set(struct dl
*dl
)
2110 struct nlmsghdr
*nlh
;
2113 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2114 NLM_F_REQUEST
| NLM_F_ACK
);
2116 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2117 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
2122 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2125 static int cmd_sb_tc_bind(struct dl
*dl
)
2127 if (dl_argv_match(dl
, "help")) {
2130 } else if (dl_argv_match(dl
, "show") ||
2131 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2133 return cmd_sb_tc_bind_show(dl
);
2134 } else if (dl_argv_match(dl
, "set")) {
2136 return cmd_sb_tc_bind_set(dl
);
2138 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2142 static int cmd_sb_tc(struct dl
*dl
)
2144 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2147 } else if (dl_argv_match(dl
, "bind")) {
2149 return cmd_sb_tc_bind(dl
);
2151 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2156 struct list_head list
;
2160 uint32_t bound_pool_index
;
2164 struct list_head list
;
2167 uint32_t port_index
;
2169 struct list_head pool_list
;
2170 struct list_head ing_tc_list
;
2171 struct list_head eg_tc_list
;
2177 struct list_head port_list
;
2180 static struct occ_item
*occ_item_alloc(void)
2182 return calloc(1, sizeof(struct occ_item
));
2185 static void occ_item_free(struct occ_item
*occ_item
)
2190 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
2192 struct occ_port
*occ_port
;
2194 occ_port
= calloc(1, sizeof(*occ_port
));
2197 occ_port
->port_index
= port_index
;
2198 INIT_LIST_HEAD(&occ_port
->pool_list
);
2199 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
2200 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
2204 static void occ_port_free(struct occ_port
*occ_port
)
2206 struct occ_item
*occ_item
, *tmp
;
2208 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
2209 occ_item_free(occ_item
);
2210 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
2211 occ_item_free(occ_item
);
2212 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
2213 occ_item_free(occ_item
);
2216 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
2218 struct occ_show
*occ_show
;
2220 occ_show
= calloc(1, sizeof(*occ_show
));
2224 INIT_LIST_HEAD(&occ_show
->port_list
);
2228 static void occ_show_free(struct occ_show
*occ_show
)
2230 struct occ_port
*occ_port
, *tmp
;
2232 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
2233 occ_port_free(occ_port
);
2236 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
2239 struct occ_port
*occ_port
;
2240 uint32_t port_index
;
2242 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2244 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
2245 if (occ_port
->port_index
== port_index
)
2248 occ_port
= occ_port_alloc(port_index
);
2251 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
2255 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
2258 struct occ_item
*occ_item
;
2261 pr_out_sp(7, " %s:", label
);
2262 list_for_each_entry(occ_item
, list
, list
) {
2263 if ((i
- 1) % 4 == 0 && i
!= 1)
2266 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
2267 occ_item
->bound_pool_index
);
2269 pr_out_sp(7, "%2u:", occ_item
->index
);
2270 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
2274 if ((i
- 1) % 4 != 0)
2278 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
2279 struct list_head
*list
,
2282 struct occ_item
*occ_item
;
2285 jsonw_name(dl
->jw
, label
);
2286 jsonw_start_object(dl
->jw
);
2287 list_for_each_entry(occ_item
, list
, list
) {
2288 sprintf(buf
, "%u", occ_item
->index
);
2289 jsonw_name(dl
->jw
, buf
);
2290 jsonw_start_object(dl
->jw
);
2292 jsonw_uint_field(dl
->jw
, "bound_pool",
2293 occ_item
->bound_pool_index
);
2294 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
2295 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
2296 jsonw_end_object(dl
->jw
);
2298 jsonw_end_object(dl
->jw
);
2301 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
2303 if (dl
->json_output
) {
2304 pr_out_json_occ_show_item_list(dl
, "pool",
2305 &occ_port
->pool_list
, false);
2306 pr_out_json_occ_show_item_list(dl
, "itc",
2307 &occ_port
->ing_tc_list
, true);
2308 pr_out_json_occ_show_item_list(dl
, "etc",
2309 &occ_port
->eg_tc_list
, true);
2312 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
2313 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
2314 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
2318 static void pr_out_occ_show(struct occ_show
*occ_show
)
2320 struct dl
*dl
= occ_show
->dl
;
2321 struct dl_opts
*opts
= &dl
->opts
;
2322 struct occ_port
*occ_port
;
2324 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
2325 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
2326 occ_port
->port_index
, true, false);
2327 pr_out_occ_show_port(dl
, occ_port
);
2328 pr_out_port_handle_end(dl
);
2332 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
2335 struct occ_port
*occ_port
;
2336 struct occ_item
*occ_item
;
2338 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2341 occ_port
= occ_port_get(occ_show
, tb
);
2343 occ_show
->err
= -ENOMEM
;
2347 occ_item
= occ_item_alloc();
2349 occ_show
->err
= -ENOMEM
;
2352 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2353 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2354 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2355 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
2358 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2360 struct occ_show
*occ_show
= data
;
2361 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2362 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2364 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2365 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2366 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2367 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2368 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2369 return MNL_CB_ERROR
;
2370 cmd_sb_occ_port_pool_process(occ_show
, tb
);
2374 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
2377 struct occ_port
*occ_port
;
2378 struct occ_item
*occ_item
;
2381 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2384 occ_port
= occ_port_get(occ_show
, tb
);
2386 occ_show
->err
= -ENOMEM
;
2390 occ_item
= occ_item_alloc();
2392 occ_show
->err
= -ENOMEM
;
2395 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
2396 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2397 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2398 occ_item
->bound_pool_index
=
2399 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2400 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
2401 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
2402 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
2403 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
2404 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
2406 occ_item_free(occ_item
);
2409 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2411 struct occ_show
*occ_show
= data
;
2412 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2413 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2415 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2416 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2417 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2418 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2419 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2420 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2421 return MNL_CB_ERROR
;
2422 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
2426 static int cmd_sb_occ_show(struct dl
*dl
)
2428 struct nlmsghdr
*nlh
;
2429 struct occ_show
*occ_show
;
2430 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
2433 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
2437 occ_show
= occ_show_alloc(dl
);
2441 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2443 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2444 cmd_sb_occ_port_pool_process_cb
, occ_show
);
2448 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2450 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2451 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
2455 pr_out_section_start(dl
, "occupancy");
2456 pr_out_occ_show(occ_show
);
2457 pr_out_section_end(dl
);
2460 occ_show_free(occ_show
);
2464 static int cmd_sb_occ_snapshot(struct dl
*dl
)
2466 struct nlmsghdr
*nlh
;
2469 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2470 NLM_F_REQUEST
| NLM_F_ACK
);
2472 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2476 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2479 static int cmd_sb_occ_clearmax(struct dl
*dl
)
2481 struct nlmsghdr
*nlh
;
2484 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2485 NLM_F_REQUEST
| NLM_F_ACK
);
2487 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2491 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2494 static int cmd_sb_occ(struct dl
*dl
)
2496 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2499 } else if (dl_argv_match(dl
, "show") ||
2500 dl_argv_match(dl
, "list")) {
2502 return cmd_sb_occ_show(dl
);
2503 } else if (dl_argv_match(dl
, "snapshot")) {
2505 return cmd_sb_occ_snapshot(dl
);
2506 } else if (dl_argv_match(dl
, "clearmax")) {
2508 return cmd_sb_occ_clearmax(dl
);
2510 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2514 static int cmd_sb(struct dl
*dl
)
2516 if (dl_argv_match(dl
, "help")) {
2519 } else if (dl_argv_match(dl
, "show") ||
2520 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2522 return cmd_sb_show(dl
);
2523 } else if (dl_argv_match(dl
, "pool")) {
2525 return cmd_sb_pool(dl
);
2526 } else if (dl_argv_match(dl
, "port")) {
2528 return cmd_sb_port(dl
);
2529 } else if (dl_argv_match(dl
, "tc")) {
2531 return cmd_sb_tc(dl
);
2532 } else if (dl_argv_match(dl
, "occupancy")) {
2534 return cmd_sb_occ(dl
);
2536 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2540 static const char *cmd_name(uint8_t cmd
)
2543 case DEVLINK_CMD_UNSPEC
: return "unspec";
2544 case DEVLINK_CMD_GET
: return "get";
2545 case DEVLINK_CMD_SET
: return "set";
2546 case DEVLINK_CMD_NEW
: return "new";
2547 case DEVLINK_CMD_DEL
: return "del";
2548 case DEVLINK_CMD_PORT_GET
: return "get";
2549 case DEVLINK_CMD_PORT_SET
: return "set";
2550 case DEVLINK_CMD_PORT_NEW
: return "net";
2551 case DEVLINK_CMD_PORT_DEL
: return "del";
2552 default: return "<unknown cmd>";
2556 static const char *cmd_obj(uint8_t cmd
)
2559 case DEVLINK_CMD_UNSPEC
: return "unspec";
2560 case DEVLINK_CMD_GET
:
2561 case DEVLINK_CMD_SET
:
2562 case DEVLINK_CMD_NEW
:
2563 case DEVLINK_CMD_DEL
:
2565 case DEVLINK_CMD_PORT_GET
:
2566 case DEVLINK_CMD_PORT_SET
:
2567 case DEVLINK_CMD_PORT_NEW
:
2568 case DEVLINK_CMD_PORT_DEL
:
2570 default: return "<unknown obj>";
2574 static void pr_out_mon_header(uint8_t cmd
)
2576 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
2579 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
2581 const char *obj
= cmd_obj(cmd
);
2582 unsigned int index
= 0;
2583 const char *cur_obj
;
2587 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2588 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
2594 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2596 struct dl
*dl
= data
;
2597 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2598 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2599 uint8_t cmd
= genl
->cmd
;
2601 if (!cmd_filter_check(dl
, cmd
))
2605 case DEVLINK_CMD_GET
: /* fall through */
2606 case DEVLINK_CMD_SET
: /* fall through */
2607 case DEVLINK_CMD_NEW
: /* fall through */
2608 case DEVLINK_CMD_DEL
:
2609 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2610 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2611 return MNL_CB_ERROR
;
2612 pr_out_mon_header(genl
->cmd
);
2615 case DEVLINK_CMD_PORT_GET
: /* fall through */
2616 case DEVLINK_CMD_PORT_SET
: /* fall through */
2617 case DEVLINK_CMD_PORT_NEW
: /* fall through */
2618 case DEVLINK_CMD_PORT_DEL
:
2619 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2620 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2621 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2622 return MNL_CB_ERROR
;
2623 pr_out_mon_header(genl
->cmd
);
2624 pr_out_port(dl
, tb
);
2630 static int cmd_mon_show(struct dl
*dl
)
2633 unsigned int index
= 0;
2634 const char *cur_obj
;
2636 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2637 if (strcmp(cur_obj
, "all") != 0 &&
2638 strcmp(cur_obj
, "dev") != 0 &&
2639 strcmp(cur_obj
, "port") != 0) {
2640 pr_err("Unknown object \"%s\"\n", cur_obj
);
2644 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
2647 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
2653 static void cmd_mon_help(void)
2655 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
2656 "where OBJECT-LIST := { dev | port }\n");
2659 static int cmd_mon(struct dl
*dl
)
2661 if (dl_argv_match(dl
, "help")) {
2664 } else if (dl_no_arg(dl
)) {
2666 return cmd_mon_show(dl
);
2668 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2672 struct dpipe_field
{
2675 unsigned int bitwidth
;
2676 enum devlink_dpipe_field_mapping_type mapping_type
;
2679 struct dpipe_header
{
2680 struct list_head list
;
2683 struct dpipe_field
*fields
;
2684 unsigned int fields_count
;
2690 struct list_head global_headers
;
2691 struct list_head local_headers
;
2695 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
2697 struct dpipe_header
*header
;
2699 header
= calloc(1, sizeof(struct dpipe_header
));
2702 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
2703 if (!header
->fields
)
2704 goto err_fields_alloc
;
2705 header
->fields_count
= fields_count
;
2713 static void dpipe_header_free(struct dpipe_header
*header
)
2715 free(header
->fields
);
2719 static void dpipe_header_clear(struct dpipe_header
*header
)
2721 struct dpipe_field
*field
;
2724 for (i
= 0; i
< header
->fields_count
; i
++) {
2725 field
= &header
->fields
[i
];
2731 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
2732 struct dpipe_header
*header
, bool global
)
2735 list_add(&header
->list
, &ctx
->global_headers
);
2737 list_add(&header
->list
, &ctx
->local_headers
);
2740 static void dpipe_header_del(struct dpipe_header
*header
)
2742 list_del(&header
->list
);
2745 static struct dpipe_ctx
*dpipe_ctx_alloc(struct dl
*dl
)
2747 struct dpipe_ctx
*ctx
;
2749 ctx
= calloc(1, sizeof(struct dpipe_ctx
));
2753 INIT_LIST_HEAD(&ctx
->global_headers
);
2754 INIT_LIST_HEAD(&ctx
->local_headers
);
2758 static void dpipe_ctx_free(struct dpipe_ctx
*ctx
)
2763 static void dpipe_ctx_clear(struct dpipe_ctx
*ctx
)
2765 struct dpipe_header
*header
, *tmp
;
2767 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
2769 dpipe_header_del(header
);
2770 dpipe_header_clear(header
);
2771 dpipe_header_free(header
);
2773 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
2775 dpipe_header_del(header
);
2776 dpipe_header_clear(header
);
2777 dpipe_header_free(header
);
2781 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
2782 uint32_t header_id
, bool global
)
2784 struct list_head
*header_list
;
2785 struct dpipe_header
*header
;
2788 header_list
= &ctx
->global_headers
;
2790 header_list
= &ctx
->local_headers
;
2791 list_for_each_entry(header
, header_list
, list
) {
2792 if (header
->id
!= header_id
)
2794 return header
->name
;
2799 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
2801 uint32_t field_id
, bool global
)
2803 struct list_head
*header_list
;
2804 struct dpipe_header
*header
;
2807 header_list
= &ctx
->global_headers
;
2809 header_list
= &ctx
->local_headers
;
2810 list_for_each_entry(header
, header_list
, list
) {
2811 if (header
->id
!= header_id
)
2813 return header
->fields
[field_id
].name
;
2819 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
2821 switch (mapping_type
) {
2822 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
2824 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
2832 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
2833 uint32_t field_id
, bool global
)
2835 enum devlink_dpipe_field_mapping_type mapping_type
;
2836 struct list_head
*header_list
;
2837 struct dpipe_header
*header
;
2840 header_list
= &ctx
->global_headers
;
2842 header_list
= &ctx
->local_headers
;
2843 list_for_each_entry(header
, header_list
, list
) {
2844 if (header
->id
!= header_id
)
2846 mapping_type
= header
->fields
[field_id
].mapping_type
;
2847 return dpipe_field_mapping_e2s(mapping_type
);
2852 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
2853 struct dpipe_field
*fields
,
2854 unsigned int field_count
)
2856 struct dpipe_field
*field
;
2859 for (i
= 0; i
< field_count
; i
++) {
2861 pr_out_entry_start(ctx
->dl
);
2862 pr_out_str(ctx
->dl
, "name", field
->name
);
2863 if (ctx
->dl
->verbose
)
2864 pr_out_uint(ctx
->dl
, "id", field
->id
);
2865 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
2866 if (field
->mapping_type
)
2867 pr_out_str(ctx
->dl
, "mapping_type",
2868 dpipe_field_mapping_e2s(field
->mapping_type
));
2869 pr_out_entry_end(ctx
->dl
);
2874 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
2875 struct dpipe_header
*header
, bool global
)
2877 pr_out_handle_start_arr(ctx
->dl
, tb
);
2878 pr_out_str(ctx
->dl
, "name", header
->name
);
2879 if (ctx
->dl
->verbose
) {
2880 pr_out_uint(ctx
->dl
, "id", header
->id
);
2881 pr_out_str(ctx
->dl
, "global",
2882 global
? "true" : "false");
2884 pr_out_array_start(ctx
->dl
, "field");
2885 pr_out_dpipe_fields(ctx
, header
->fields
,
2886 header
->fields_count
);
2887 pr_out_array_end(ctx
->dl
);
2888 pr_out_handle_end(ctx
->dl
);
2891 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
2894 struct dpipe_header
*header
;
2896 list_for_each_entry(header
, &ctx
->local_headers
, list
)
2897 pr_out_dpipe_header(ctx
, tb
, header
, false);
2899 list_for_each_entry(header
, &ctx
->global_headers
, list
)
2900 pr_out_dpipe_header(ctx
, tb
, header
, true);
2903 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
2905 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
2909 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
2910 if (err
!= MNL_CB_OK
)
2912 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
2913 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
2914 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
2915 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
2918 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
2919 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
2920 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
2921 field
->name
= strdup(name
);
2924 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
2928 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
2929 struct dpipe_field
*fields
)
2931 struct nlattr
*nla_field
;
2935 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
2936 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
2944 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
2946 struct nlattr
*nla_field
;
2947 unsigned int count
= 0;
2949 mnl_attr_for_each_nested(nla_field
, nla_fields
)
2954 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
2956 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
2957 struct dpipe_header
*header
;
2958 unsigned int fields_count
;
2959 const char *header_name
;
2963 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
2964 if (err
!= MNL_CB_OK
)
2967 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
2968 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
2969 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
2972 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
2973 header
= dpipe_header_alloc(fields_count
);
2977 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
2978 header
->name
= strdup(header_name
);
2979 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
2980 header
->fields_count
= fields_count
;
2981 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
2983 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
2987 dpipe_header_add(ctx
, header
, global
);
2991 dpipe_header_free(header
);
2995 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
2997 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
2998 struct nlattr
*nla_header
;
3001 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
3002 err
= dpipe_header_get(ctx
, nla_header
);
3009 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
3011 struct dpipe_ctx
*ctx
= data
;
3012 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3013 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3016 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3017 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3018 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
3019 return MNL_CB_ERROR
;
3020 err
= dpipe_headers_get(ctx
, tb
);
3023 return MNL_CB_ERROR
;
3026 if (ctx
->print_headers
)
3027 pr_out_dpipe_headers(ctx
, tb
);
3031 static int cmd_dpipe_headers_show(struct dl
*dl
)
3033 struct nlmsghdr
*nlh
;
3034 struct dpipe_ctx
*ctx
;
3035 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3038 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3040 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3044 ctx
= dpipe_ctx_alloc(dl
);
3048 ctx
->print_headers
= true;
3050 pr_out_section_start(dl
, "header");
3051 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3053 pr_err("error get headers %s\n", strerror(ctx
->err
));
3054 pr_out_section_end(dl
);
3056 dpipe_ctx_clear(ctx
);
3057 dpipe_ctx_free(ctx
);
3061 static void cmd_dpipe_header_help(void)
3063 pr_err("Usage: devlink dpipe headers show DEV\n");
3066 static int cmd_dpipe_header(struct dl
*dl
)
3068 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3069 cmd_dpipe_header_help();
3071 } else if (dl_argv_match(dl
, "show")) {
3073 return cmd_dpipe_headers_show(dl
);
3075 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3080 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
3082 switch (action_type
) {
3083 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
3084 return "field_modify";
3090 struct dpipe_op_info
{
3096 struct dpipe_action
{
3097 struct dpipe_op_info info
;
3101 static void pr_out_dpipe_action(struct dpipe_action
*action
,
3102 struct dpipe_ctx
*ctx
)
3104 struct dpipe_op_info
*op_info
= &action
->info
;
3105 const char *mapping
;
3107 pr_out_str(ctx
->dl
, "type",
3108 dpipe_action_type_e2s(action
->type
));
3109 pr_out_str(ctx
->dl
, "header",
3110 dpipe_header_id2s(ctx
, op_info
->header_id
,
3111 op_info
->header_global
));
3112 pr_out_str(ctx
->dl
, "field",
3113 dpipe_field_id2s(ctx
, op_info
->header_id
,
3115 op_info
->header_global
));
3116 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3118 op_info
->header_global
);
3120 pr_out_str(ctx
->dl
, "mapping", mapping
);
3123 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
3125 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
3128 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
3129 if (err
!= MNL_CB_OK
)
3132 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
3133 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3134 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3135 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3139 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
3140 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3141 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3142 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3147 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
3148 struct nlattr
*nla_actions
)
3150 struct nlattr
*nla_action
;
3151 struct dpipe_action action
;
3153 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
3154 pr_out_entry_start(ctx
->dl
);
3155 if (dpipe_action_parse(&action
, nla_action
))
3156 goto err_action_parse
;
3157 pr_out_dpipe_action(&action
, ctx
);
3158 pr_out_entry_end(ctx
->dl
);
3163 pr_out_entry_end(ctx
->dl
);
3168 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
3170 switch (match_type
) {
3171 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
3172 return "field_exact";
3178 struct dpipe_match
{
3179 struct dpipe_op_info info
;
3183 static void pr_out_dpipe_match(struct dpipe_match
*match
,
3184 struct dpipe_ctx
*ctx
)
3186 struct dpipe_op_info
*op_info
= &match
->info
;
3187 const char *mapping
;
3189 pr_out_str(ctx
->dl
, "type",
3190 dpipe_match_type_e2s(match
->type
));
3191 pr_out_str(ctx
->dl
, "header",
3192 dpipe_header_id2s(ctx
, op_info
->header_id
,
3193 op_info
->header_global
));
3194 pr_out_str(ctx
->dl
, "field",
3195 dpipe_field_id2s(ctx
, op_info
->header_id
,
3197 op_info
->header_global
));
3198 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3200 op_info
->header_global
);
3202 pr_out_str(ctx
->dl
, "mapping", mapping
);
3205 static int dpipe_match_parse(struct dpipe_match
*match
,
3209 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
3212 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
3213 if (err
!= MNL_CB_OK
)
3216 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
3217 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3218 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3219 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3223 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
3224 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3225 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3226 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3231 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
3232 struct nlattr
*nla_matches
)
3234 struct nlattr
*nla_match
;
3235 struct dpipe_match match
;
3237 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
3238 pr_out_entry_start(ctx
->dl
);
3239 if (dpipe_match_parse(&match
, nla_match
))
3240 goto err_match_parse
;
3241 pr_out_dpipe_match(&match
, ctx
);
3242 pr_out_entry_end(ctx
->dl
);
3247 pr_out_entry_end(ctx
->dl
);
3251 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3253 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
3254 bool counters_enabled
;
3259 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
3260 if (err
!= MNL_CB_OK
)
3263 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
3264 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
3265 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
3266 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
3267 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
3271 name
= mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
3272 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
3273 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
3275 pr_out_str(ctx
->dl
, "name", name
);
3276 pr_out_uint(ctx
->dl
, "size", size
);
3277 pr_out_str(ctx
->dl
, "counters_enabled",
3278 counters_enabled
? "true" : "false");
3280 pr_out_array_start(ctx
->dl
, "match");
3281 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
3282 goto err_matches_show
;
3283 pr_out_array_end(ctx
->dl
);
3285 pr_out_array_start(ctx
->dl
, "action");
3286 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
3287 goto err_actions_show
;
3288 pr_out_array_end(ctx
->dl
);
3294 pr_out_array_end(ctx
->dl
);
3298 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3300 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
3301 struct nlattr
*nla_table
;
3303 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
3304 pr_out_handle_start_arr(ctx
->dl
, tb
);
3305 if (dpipe_table_show(ctx
, nla_table
))
3306 goto err_table_show
;
3307 pr_out_handle_end(ctx
->dl
);
3312 pr_out_handle_end(ctx
->dl
);
3316 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3318 struct dpipe_ctx
*ctx
= data
;
3319 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3320 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3322 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3323 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3324 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
3325 return MNL_CB_ERROR
;
3327 if (dpipe_tables_show(ctx
, tb
))
3328 return MNL_CB_ERROR
;
3332 static int cmd_dpipe_table_show(struct dl
*dl
)
3334 struct nlmsghdr
*nlh
;
3335 struct dpipe_ctx
*ctx
;
3336 uint16_t flags
= NLM_F_REQUEST
;
3339 ctx
= dpipe_ctx_alloc(dl
);
3343 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
3347 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3348 dl_opts_put(nlh
, dl
);
3349 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3351 pr_err("error get headers %s\n", strerror(ctx
->err
));
3355 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3356 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
3357 dl_opts_put(nlh
, dl
);
3359 pr_out_section_start(dl
, "table");
3360 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, ctx
);
3361 pr_out_section_end(dl
);
3363 dpipe_ctx_clear(ctx
);
3364 dpipe_ctx_free(ctx
);
3368 static int cmd_dpipe_table_set(struct dl
*dl
)
3370 struct nlmsghdr
*nlh
;
3373 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
3374 NLM_F_REQUEST
| NLM_F_ACK
);
3376 err
= dl_argv_parse_put(nlh
, dl
,
3377 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
3378 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
3382 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3385 enum dpipe_value_type
{
3386 DPIPE_VALUE_TYPE_VALUE
,
3387 DPIPE_VALUE_TYPE_MASK
,
3391 dpipe_value_type_e2s(enum dpipe_value_type type
)
3394 case DPIPE_VALUE_TYPE_VALUE
:
3396 case DPIPE_VALUE_TYPE_MASK
:
3397 return "value_mask";
3403 struct dpipe_field_printer
{
3404 unsigned int field_id
;
3405 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
3408 struct dpipe_header_printer
{
3409 struct dpipe_field_printer
*printers
;
3410 unsigned int printers_count
;
3411 unsigned int header_id
;
3414 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
3415 enum dpipe_value_type type
,
3418 struct in_addr ip_addr
;
3420 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
3421 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
3425 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
3426 enum dpipe_value_type type
,
3429 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
3430 ether_ntoa((struct ether_addr
*)value
));
3433 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
3434 enum dpipe_value_type type
,
3437 char str
[INET6_ADDRSTRLEN
];
3439 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
3440 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
3443 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
3445 .printer
= dpipe_field_printer_ipv4_addr
,
3446 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
3450 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
3451 .printers
= dpipe_field_printers_ipv4
,
3452 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
3453 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
3456 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
3458 .printer
= dpipe_field_printer_ethernet_addr
,
3459 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
3463 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
3464 .printers
= dpipe_field_printers_ethernet
,
3465 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
3466 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
3469 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
3471 .printer
= dpipe_field_printer_ipv6_addr
,
3472 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
3476 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
3477 .printers
= dpipe_field_printers_ipv6
,
3478 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
3479 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
3482 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
3483 &dpipe_header_printer_ipv4
,
3484 &dpipe_header_printer_ethernet
,
3485 &dpipe_header_printer_ipv6
,
3488 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
3489 struct dpipe_op_info
*info
,
3490 enum dpipe_value_type type
,
3493 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
3494 struct dpipe_header_printer
*header_printer
;
3495 struct dpipe_field_printer
*field_printer
;
3496 unsigned int field_printers_count
;
3500 for (i
= 0; i
< header_printers_count
; i
++) {
3501 header_printer
= dpipe_header_printers
[i
];
3502 if (header_printer
->header_id
!= info
->header_id
)
3504 field_printers_count
= header_printer
->printers_count
;
3505 for (j
= 0; j
< field_printers_count
; j
++) {
3506 field_printer
= &header_printer
->printers
[j
];
3507 if (field_printer
->field_id
!= info
->field_id
)
3509 field_printer
->printer(ctx
, type
, value
);
3517 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
3519 unsigned int value_len
,
3520 struct dpipe_op_info
*info
,
3521 enum dpipe_value_type type
)
3523 if (info
->header_global
&&
3524 !dpipe_print_prot_header(ctx
, info
, type
, value
))
3527 if (value_len
== sizeof(uint32_t)) {
3528 uint32_t *value_32
= value
;
3530 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
3534 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
3535 struct nlattr
**nla_match_value
,
3536 struct dpipe_op_info
*info
)
3538 void *value
, *value_mask
;
3539 uint32_t value_mapping
;
3543 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
3544 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
3546 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3547 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3550 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
3551 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
3555 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3556 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
3557 DPIPE_VALUE_TYPE_MASK
);
3560 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
3563 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
3566 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3567 struct dpipe_match match
;
3570 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
3571 if (err
!= MNL_CB_OK
)
3574 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
3575 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3579 pr_out_entry_start(ctx
->dl
);
3580 if (dpipe_match_parse(&match
,
3581 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
3582 goto err_match_parse
;
3583 pr_out_dpipe_match(&match
, ctx
);
3584 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
3585 pr_out_entry_end(ctx
->dl
);
3590 pr_out_entry_end(ctx
->dl
);
3594 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
3597 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3598 struct dpipe_action action
;
3601 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
3602 if (err
!= MNL_CB_OK
)
3605 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
3606 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3610 pr_out_entry_start(ctx
->dl
);
3611 if (dpipe_action_parse(&action
,
3612 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
3613 goto err_action_parse
;
3614 pr_out_dpipe_action(&action
, ctx
);
3615 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
3616 pr_out_entry_end(ctx
->dl
);
3621 pr_out_entry_end(ctx
->dl
);
3626 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
3627 struct nlattr
*nla_action_values
)
3629 struct nlattr
*nla_action_value
;
3631 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
3632 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
3639 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
3640 struct nlattr
*nla_match_values
)
3642 struct nlattr
*nla_match_value
;
3644 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
3645 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
3651 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3653 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
3654 uint32_t entry_index
;
3658 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
3659 if (err
!= MNL_CB_OK
)
3662 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
3663 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
3664 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
3668 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
3669 pr_out_uint(ctx
->dl
, "index", entry_index
);
3671 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
3672 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
3673 pr_out_uint(ctx
->dl
, "counter", counter
);
3676 pr_out_array_start(ctx
->dl
, "match_value");
3677 if (dpipe_tables_match_values_show(ctx
,
3678 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
3679 goto err_match_values_show
;
3680 pr_out_array_end(ctx
->dl
);
3682 pr_out_array_start(ctx
->dl
, "action_value");
3683 if (dpipe_tables_action_values_show(ctx
,
3684 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
3685 goto err_action_values_show
;
3686 pr_out_array_end(ctx
->dl
);
3689 err_action_values_show
:
3690 err_match_values_show
:
3691 pr_out_array_end(ctx
->dl
);
3695 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3697 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
3698 struct nlattr
*nla_entry
;
3700 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
3701 pr_out_handle_start_arr(ctx
->dl
, tb
);
3702 if (dpipe_entry_show(ctx
, nla_entry
))
3703 goto err_entry_show
;
3704 pr_out_handle_end(ctx
->dl
);
3709 pr_out_handle_end(ctx
->dl
);
3713 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
3715 struct dpipe_ctx
*ctx
= data
;
3716 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3717 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3719 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3720 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3721 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
3722 return MNL_CB_ERROR
;
3724 if (dpipe_table_entries_show(ctx
, tb
))
3725 return MNL_CB_ERROR
;
3729 static int cmd_dpipe_table_dump(struct dl
*dl
)
3731 struct nlmsghdr
*nlh
;
3732 struct dpipe_ctx
*ctx
;
3733 uint16_t flags
= NLM_F_REQUEST
;
3736 ctx
= dpipe_ctx_alloc(dl
);
3740 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
3744 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3745 dl_opts_put(nlh
, dl
);
3746 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3748 pr_err("error get headers %s\n", strerror(ctx
->err
));
3752 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3753 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
3754 dl_opts_put(nlh
, dl
);
3756 pr_out_section_start(dl
, "table_entry");
3757 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, ctx
);
3758 pr_out_section_end(dl
);
3760 dpipe_ctx_clear(ctx
);
3761 dpipe_ctx_free(ctx
);
3765 static void cmd_dpipe_table_help(void)
3767 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
3768 "where OBJECT-LIST := { show | set | dump }\n");
3771 static int cmd_dpipe_table(struct dl
*dl
)
3773 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3774 cmd_dpipe_table_help();
3776 } else if (dl_argv_match(dl
, "show")) {
3778 return cmd_dpipe_table_show(dl
);
3779 } else if (dl_argv_match(dl
, "set")) {
3781 return cmd_dpipe_table_set(dl
);
3782 } else if (dl_argv_match(dl
, "dump")) {
3784 return cmd_dpipe_table_dump(dl
);
3786 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3790 static void cmd_dpipe_help(void)
3792 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
3793 "where OBJECT-LIST := { header | table }\n");
3796 static int cmd_dpipe(struct dl
*dl
)
3798 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3801 } else if (dl_argv_match(dl
, "header")) {
3803 return cmd_dpipe_header(dl
);
3804 } else if (dl_argv_match(dl
, "table")) {
3806 return cmd_dpipe_table(dl
);
3808 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3812 static void help(void)
3814 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
3815 " devlink [ -f[orce] ] -b[atch] filename\n"
3816 "where OBJECT := { dev | port | sb | monitor | dpipe }\n"
3817 " OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
3820 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
3825 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3828 } else if (dl_argv_match(dl
, "dev")) {
3831 } else if (dl_argv_match(dl
, "port")) {
3833 return cmd_port(dl
);
3834 } else if (dl_argv_match(dl
, "sb")) {
3837 } else if (dl_argv_match(dl
, "monitor")) {
3840 } else if (dl_argv_match(dl
, "dpipe")) {
3842 return cmd_dpipe(dl
);
3844 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
3848 static int dl_init(struct dl
*dl
)
3852 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3854 pr_err("Failed to connect to devlink Netlink\n");
3858 err
= ifname_map_init(dl
);
3860 pr_err("Failed to create index map\n");
3861 goto err_ifname_map_create
;
3863 if (dl
->json_output
) {
3864 dl
->jw
= jsonw_new(stdout
);
3866 pr_err("Failed to create JSON writer\n");
3869 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
3874 ifname_map_fini(dl
);
3875 err_ifname_map_create
:
3876 mnlg_socket_close(dl
->nlg
);
3880 static void dl_fini(struct dl
*dl
)
3882 if (dl
->json_output
)
3883 jsonw_destroy(&dl
->jw
);
3884 ifname_map_fini(dl
);
3885 mnlg_socket_close(dl
->nlg
);
3888 static struct dl
*dl_alloc(void)
3892 dl
= calloc(1, sizeof(*dl
));
3898 static void dl_free(struct dl
*dl
)
3903 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
3907 int ret
= EXIT_SUCCESS
;
3909 if (name
&& strcmp(name
, "-") != 0) {
3910 if (freopen(name
, "r", stdin
) == NULL
) {
3912 "Cannot open file \"%s\" for reading: %s\n",
3913 name
, strerror(errno
));
3914 return EXIT_FAILURE
;
3919 while (getcmdline(&line
, &len
, stdin
) != -1) {
3923 largc
= makeargs(line
, largv
, 100);
3925 continue; /* blank line */
3927 if (dl_cmd(dl
, largc
, largv
)) {
3928 fprintf(stderr
, "Command failed %s:%d\n",
3942 int main(int argc
, char **argv
)
3944 static const struct option long_options
[] = {
3945 { "Version", no_argument
, NULL
, 'V' },
3946 { "force", no_argument
, NULL
, 'f' },
3947 { "batch", required_argument
, NULL
, 'b' },
3948 { "no-nice-names", no_argument
, NULL
, 'n' },
3949 { "json", no_argument
, NULL
, 'j' },
3950 { "pretty", no_argument
, NULL
, 'p' },
3951 { "verbose", no_argument
, NULL
, 'v' },
3952 { NULL
, 0, NULL
, 0 }
3954 const char *batch_file
= NULL
;
3963 pr_err("Failed to allocate memory for devlink\n");
3964 return EXIT_FAILURE
;
3967 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
3968 long_options
, NULL
)) >= 0) {
3972 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
3979 batch_file
= optarg
;
3982 dl
->no_nice_names
= true;
3985 dl
->json_output
= true;
3988 dl
->pretty_output
= true;
3994 pr_err("Unknown option.\n");
4011 err
= dl_batch(dl
, batch_file
, force
);
4013 err
= dl_cmd(dl
, argc
, argv
);