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 #define pr_err(args...) fprintf(stderr, ##args)
39 #define pr_out(args...) \
41 if (g_indent_newline) { \
42 fprintf(stdout, "%s", g_indent_str); \
43 g_indent_newline = false; \
45 fprintf(stdout, ##args); \
48 #define pr_out_sp(num, args...) \
50 int ret = fprintf(stdout, ##args); \
52 fprintf(stdout, "%*s", num - ret, ""); \
55 static int g_indent_level
;
56 static bool g_indent_newline
;
57 #define INDENT_STR_STEP 2
58 #define INDENT_STR_MAXLEN 32
59 static char g_indent_str
[INDENT_STR_MAXLEN
+ 1] = "";
61 static void __pr_out_indent_inc(void)
63 if (g_indent_level
+ INDENT_STR_STEP
> INDENT_STR_MAXLEN
)
65 g_indent_level
+= INDENT_STR_STEP
;
66 memset(g_indent_str
, ' ', sizeof(g_indent_str
));
67 g_indent_str
[g_indent_level
] = '\0';
70 static void __pr_out_indent_dec(void)
72 if (g_indent_level
- INDENT_STR_STEP
< 0)
74 g_indent_level
-= INDENT_STR_STEP
;
75 g_indent_str
[g_indent_level
] = '\0';
78 static void __pr_out_newline(void)
81 g_indent_newline
= true;
84 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
85 mnl_cb_t data_cb
, void *data
)
89 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
91 pr_err("devlink answers: %s\n", strerror(errno
));
97 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
98 const struct nlmsghdr
*nlh
,
99 mnl_cb_t data_cb
, void *data
)
103 err
= mnlg_socket_send(nlg
, nlh
);
105 pr_err("Failed to call mnlg_socket_send\n");
108 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
111 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
112 const char *group_name
)
116 err
= mnlg_socket_group_add(nlg
, group_name
);
118 pr_err("Failed to call mnlg_socket_group_add\n");
125 struct list_head list
;
132 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
133 const char *dev_name
,
137 struct ifname_map
*ifname_map
;
139 ifname_map
= calloc(1, sizeof(*ifname_map
));
142 ifname_map
->bus_name
= strdup(bus_name
);
143 ifname_map
->dev_name
= strdup(dev_name
);
144 ifname_map
->port_index
= port_index
;
145 ifname_map
->ifname
= strdup(ifname
);
146 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
147 !ifname_map
->ifname
) {
148 free(ifname_map
->ifname
);
149 free(ifname_map
->dev_name
);
150 free(ifname_map
->bus_name
);
157 static void ifname_map_free(struct ifname_map
*ifname_map
)
159 free(ifname_map
->ifname
);
160 free(ifname_map
->dev_name
);
161 free(ifname_map
->bus_name
);
165 #define DL_OPT_HANDLE BIT(0)
166 #define DL_OPT_HANDLEP BIT(1)
167 #define DL_OPT_PORT_TYPE BIT(2)
168 #define DL_OPT_PORT_COUNT BIT(3)
169 #define DL_OPT_SB BIT(4)
170 #define DL_OPT_SB_POOL BIT(5)
171 #define DL_OPT_SB_SIZE BIT(6)
172 #define DL_OPT_SB_TYPE BIT(7)
173 #define DL_OPT_SB_THTYPE BIT(8)
174 #define DL_OPT_SB_TH BIT(9)
175 #define DL_OPT_SB_TC BIT(10)
176 #define DL_OPT_ESWITCH_MODE BIT(11)
177 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
178 #define DL_OPT_DPIPE_TABLE_NAME BIT(13)
179 #define DL_OPT_DPIPE_TABLE_COUNTERS BIT(14)
180 #define DL_OPT_ESWITCH_ENCAP_MODE BIT(15)
183 uint32_t present
; /* flags of present items */
187 enum devlink_port_type port_type
;
190 uint16_t sb_pool_index
;
191 uint32_t sb_pool_size
;
192 enum devlink_sb_pool_type sb_pool_type
;
193 enum devlink_sb_threshold_type sb_pool_thtype
;
194 uint32_t sb_threshold
;
195 uint16_t sb_tc_index
;
196 enum devlink_eswitch_mode eswitch_mode
;
197 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
198 const char *dpipe_table_name
;
199 bool dpipe_counters_enable
;
200 bool eswitch_encap_mode
;
204 struct mnlg_socket
*nlg
;
205 struct list_head ifname_map_list
;
222 static int dl_argc(struct dl
*dl
)
227 static char *dl_argv(struct dl
*dl
)
229 if (dl_argc(dl
) == 0)
234 static void dl_arg_inc(struct dl
*dl
)
236 if (dl_argc(dl
) == 0)
242 static char *dl_argv_next(struct dl
*dl
)
246 if (dl_argc(dl
) == 0)
254 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
256 if (index
>= dl_argc(dl
))
258 return dl
->argv
[index
];
261 static int strcmpx(const char *str1
, const char *str2
)
263 if (strlen(str1
) > strlen(str2
))
265 return strncmp(str1
, str2
, strlen(str1
));
268 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
270 if (dl_argc(dl
) == 0)
272 return strcmpx(dl_argv(dl
), pattern
) == 0;
275 static bool dl_no_arg(struct dl
*dl
)
277 return dl_argc(dl
) == 0;
280 static const enum mnl_attr_data_type devlink_policy
[DEVLINK_ATTR_MAX
+ 1] = {
281 [DEVLINK_ATTR_BUS_NAME
] = MNL_TYPE_NUL_STRING
,
282 [DEVLINK_ATTR_DEV_NAME
] = MNL_TYPE_NUL_STRING
,
283 [DEVLINK_ATTR_PORT_INDEX
] = MNL_TYPE_U32
,
284 [DEVLINK_ATTR_PORT_TYPE
] = MNL_TYPE_U16
,
285 [DEVLINK_ATTR_PORT_DESIRED_TYPE
] = MNL_TYPE_U16
,
286 [DEVLINK_ATTR_PORT_NETDEV_IFINDEX
] = MNL_TYPE_U32
,
287 [DEVLINK_ATTR_PORT_NETDEV_NAME
] = MNL_TYPE_NUL_STRING
,
288 [DEVLINK_ATTR_PORT_IBDEV_NAME
] = MNL_TYPE_NUL_STRING
,
289 [DEVLINK_ATTR_SB_INDEX
] = MNL_TYPE_U32
,
290 [DEVLINK_ATTR_SB_SIZE
] = MNL_TYPE_U32
,
291 [DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
292 [DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] = MNL_TYPE_U16
,
293 [DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] = MNL_TYPE_U16
,
294 [DEVLINK_ATTR_SB_EGRESS_TC_COUNT
] = MNL_TYPE_U16
,
295 [DEVLINK_ATTR_SB_POOL_INDEX
] = MNL_TYPE_U16
,
296 [DEVLINK_ATTR_SB_POOL_TYPE
] = MNL_TYPE_U8
,
297 [DEVLINK_ATTR_SB_POOL_SIZE
] = MNL_TYPE_U32
,
298 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = MNL_TYPE_U8
,
299 [DEVLINK_ATTR_SB_THRESHOLD
] = MNL_TYPE_U32
,
300 [DEVLINK_ATTR_SB_TC_INDEX
] = MNL_TYPE_U16
,
301 [DEVLINK_ATTR_SB_OCC_CUR
] = MNL_TYPE_U32
,
302 [DEVLINK_ATTR_SB_OCC_MAX
] = MNL_TYPE_U32
,
303 [DEVLINK_ATTR_ESWITCH_MODE
] = MNL_TYPE_U16
,
304 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = MNL_TYPE_U8
,
305 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = MNL_TYPE_U8
,
306 [DEVLINK_ATTR_DPIPE_TABLES
] = MNL_TYPE_NESTED
,
307 [DEVLINK_ATTR_DPIPE_TABLE
] = MNL_TYPE_NESTED
,
308 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = MNL_TYPE_STRING
,
309 [DEVLINK_ATTR_DPIPE_TABLE_SIZE
] = MNL_TYPE_U64
,
310 [DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] = MNL_TYPE_NESTED
,
311 [DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] = MNL_TYPE_NESTED
,
312 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = MNL_TYPE_U8
,
313 [DEVLINK_ATTR_DPIPE_ENTRIES
] = MNL_TYPE_NESTED
,
314 [DEVLINK_ATTR_DPIPE_ENTRY
] = MNL_TYPE_NESTED
,
315 [DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] = MNL_TYPE_U64
,
316 [DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] = MNL_TYPE_NESTED
,
317 [DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
] = MNL_TYPE_NESTED
,
318 [DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
] = MNL_TYPE_U64
,
319 [DEVLINK_ATTR_DPIPE_MATCH
] = MNL_TYPE_NESTED
,
320 [DEVLINK_ATTR_DPIPE_MATCH_VALUE
] = MNL_TYPE_NESTED
,
321 [DEVLINK_ATTR_DPIPE_MATCH_TYPE
] = MNL_TYPE_U32
,
322 [DEVLINK_ATTR_DPIPE_ACTION
] = MNL_TYPE_NESTED
,
323 [DEVLINK_ATTR_DPIPE_ACTION_VALUE
] = MNL_TYPE_NESTED
,
324 [DEVLINK_ATTR_DPIPE_ACTION_TYPE
] = MNL_TYPE_U32
,
325 [DEVLINK_ATTR_DPIPE_VALUE_MAPPING
] = MNL_TYPE_U32
,
326 [DEVLINK_ATTR_DPIPE_HEADERS
] = MNL_TYPE_NESTED
,
327 [DEVLINK_ATTR_DPIPE_HEADER
] = MNL_TYPE_NESTED
,
328 [DEVLINK_ATTR_DPIPE_HEADER_NAME
] = MNL_TYPE_STRING
,
329 [DEVLINK_ATTR_DPIPE_HEADER_ID
] = MNL_TYPE_U32
,
330 [DEVLINK_ATTR_DPIPE_HEADER_FIELDS
] = MNL_TYPE_NESTED
,
331 [DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
] = MNL_TYPE_U8
,
332 [DEVLINK_ATTR_DPIPE_HEADER_INDEX
] = MNL_TYPE_U32
,
333 [DEVLINK_ATTR_DPIPE_FIELD
] = MNL_TYPE_NESTED
,
334 [DEVLINK_ATTR_DPIPE_FIELD_NAME
] = MNL_TYPE_STRING
,
335 [DEVLINK_ATTR_DPIPE_FIELD_ID
] = MNL_TYPE_U32
,
336 [DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] = MNL_TYPE_U32
,
337 [DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
] = MNL_TYPE_U32
,
340 static int attr_cb(const struct nlattr
*attr
, void *data
)
342 const struct nlattr
**tb
= data
;
345 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
348 type
= mnl_attr_get_type(attr
);
349 if (mnl_attr_validate(attr
, devlink_policy
[type
]) < 0)
356 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
358 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
359 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
360 struct dl
*dl
= data
;
361 struct ifname_map
*ifname_map
;
362 const char *bus_name
;
363 const char *dev_name
;
364 uint32_t port_ifindex
;
365 const char *port_ifname
;
367 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
368 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
369 !tb
[DEVLINK_ATTR_PORT_INDEX
])
372 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
375 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
376 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
377 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
378 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
379 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
380 port_ifindex
, port_ifname
);
383 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
388 static void ifname_map_fini(struct dl
*dl
)
390 struct ifname_map
*ifname_map
, *tmp
;
392 list_for_each_entry_safe(ifname_map
, tmp
,
393 &dl
->ifname_map_list
, list
) {
394 list_del(&ifname_map
->list
);
395 ifname_map_free(ifname_map
);
399 static int ifname_map_init(struct dl
*dl
)
401 struct nlmsghdr
*nlh
;
404 INIT_LIST_HEAD(&dl
->ifname_map_list
);
406 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
407 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
409 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
417 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
418 char **p_bus_name
, char **p_dev_name
,
419 uint32_t *p_port_index
)
421 struct ifname_map
*ifname_map
;
423 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
424 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
425 *p_bus_name
= ifname_map
->bus_name
;
426 *p_dev_name
= ifname_map
->dev_name
;
427 *p_port_index
= ifname_map
->port_index
;
434 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
435 const char *dev_name
, uint32_t port_index
,
438 struct ifname_map
*ifname_map
;
440 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
441 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
442 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
443 port_index
== ifname_map
->port_index
) {
444 *p_ifname
= ifname_map
->ifname
;
451 static unsigned int strslashcount(char *str
)
453 unsigned int count
= 0;
456 while ((pos
= strchr(pos
, '/'))) {
463 static int strslashrsplit(char *str
, char **before
, char **after
)
467 slash
= strrchr(str
, '/');
476 static int strtouint32_t(const char *str
, uint32_t *p_val
)
479 unsigned long int val
;
481 val
= strtoul(str
, &endptr
, 10);
482 if (endptr
== str
|| *endptr
!= '\0')
490 static int strtouint16_t(const char *str
, uint16_t *p_val
)
493 unsigned long int val
;
495 val
= strtoul(str
, &endptr
, 10);
496 if (endptr
== str
|| *endptr
!= '\0')
504 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
506 strslashrsplit(str
, p_bus_name
, p_dev_name
);
510 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
512 char *str
= dl_argv_next(dl
);
515 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
518 if (strslashcount(str
) != 1) {
519 pr_err("Wrong devlink identification string format.\n");
520 pr_err("Expected \"bus_name/dev_name\".\n");
523 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
526 static int __dl_argv_handle_port(char *str
,
527 char **p_bus_name
, char **p_dev_name
,
528 uint32_t *p_port_index
)
534 err
= strslashrsplit(str
, &handlestr
, &portstr
);
536 pr_err("Port identification \"%s\" is invalid\n", str
);
539 err
= strtouint32_t(portstr
, p_port_index
);
541 pr_err("Port index \"%s\" is not a number or not within range\n",
545 err
= strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
547 pr_err("Port identification \"%s\" is invalid\n", str
);
553 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
554 char **p_bus_name
, char **p_dev_name
,
555 uint32_t *p_port_index
)
559 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
562 pr_err("Netdevice \"%s\" not found\n", str
);
568 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
569 char **p_dev_name
, uint32_t *p_port_index
)
571 char *str
= dl_argv_next(dl
);
572 unsigned int slash_count
;
575 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
578 slash_count
= strslashcount(str
);
579 switch (slash_count
) {
581 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
582 p_dev_name
, p_port_index
);
584 return __dl_argv_handle_port(str
, p_bus_name
,
585 p_dev_name
, p_port_index
);
587 pr_err("Wrong port identification string format.\n");
588 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
593 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
594 char **p_dev_name
, uint32_t *p_port_index
,
595 uint32_t *p_handle_bit
)
597 char *str
= dl_argv_next(dl
);
598 unsigned int slash_count
;
602 pr_err("One of following identifications expected:\n"
603 "Devlink identification (\"bus_name/dev_name\")\n"
604 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
607 slash_count
= strslashcount(str
);
608 if (slash_count
== 1) {
609 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
612 *p_handle_bit
= DL_OPT_HANDLE
;
613 } else if (slash_count
== 2) {
614 err
= __dl_argv_handle_port(str
, p_bus_name
,
615 p_dev_name
, p_port_index
);
618 *p_handle_bit
= DL_OPT_HANDLEP
;
619 } else if (slash_count
== 0) {
620 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
621 p_dev_name
, p_port_index
);
624 *p_handle_bit
= DL_OPT_HANDLEP
;
626 pr_err("Wrong port identification string format.\n");
627 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
633 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
635 char *str
= dl_argv_next(dl
);
639 pr_err("Unsigned number argument expected\n");
643 err
= strtouint32_t(str
, p_val
);
645 pr_err("\"%s\" is not a number or not within range\n", str
);
651 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
653 char *str
= dl_argv_next(dl
);
657 pr_err("Unsigned number argument expected\n");
661 err
= strtouint16_t(str
, p_val
);
663 pr_err("\"%s\" is not a number or not within range\n", str
);
669 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
671 const char *str
= dl_argv_next(dl
);
674 pr_err("String parameter expected\n");
681 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
683 if (strcmp(typestr
, "auto") == 0) {
684 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
685 } else if (strcmp(typestr
, "eth") == 0) {
686 *p_type
= DEVLINK_PORT_TYPE_ETH
;
687 } else if (strcmp(typestr
, "ib") == 0) {
688 *p_type
= DEVLINK_PORT_TYPE_IB
;
690 pr_err("Unknown port type \"%s\"\n", typestr
);
696 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
698 if (strcmp(typestr
, "ingress") == 0) {
699 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
700 } else if (strcmp(typestr
, "egress") == 0) {
701 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
703 pr_err("Unknown pool type \"%s\"\n", typestr
);
709 static int threshold_type_get(const char *typestr
,
710 enum devlink_sb_threshold_type
*p_type
)
712 if (strcmp(typestr
, "static") == 0) {
713 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
714 } else if (strcmp(typestr
, "dynamic") == 0) {
715 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
717 pr_err("Unknown threshold type \"%s\"\n", typestr
);
723 static int eswitch_mode_get(const char *typestr
,
724 enum devlink_eswitch_mode
*p_mode
)
726 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
727 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
728 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
729 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
731 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
737 static int eswitch_inline_mode_get(const char *typestr
,
738 enum devlink_eswitch_inline_mode
*p_mode
)
740 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
741 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
742 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
743 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
744 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
745 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
746 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
747 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
749 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
755 static int dpipe_counters_enable_get(const char *typestr
,
756 bool *counters_enable
)
758 if (strcmp(typestr
, "enable") == 0) {
759 *counters_enable
= 1;
760 } else if (strcmp(typestr
, "disable") == 0) {
761 *counters_enable
= 0;
763 pr_err("Unknown counter_state \"%s\"\n", typestr
);
769 static int eswitch_encap_mode_get(const char *typestr
, bool *p_mode
)
771 if (strcmp(typestr
, "enable") == 0) {
773 } else if (strcmp(typestr
, "disable") == 0) {
776 pr_err("Unknown eswitch encap mode \"%s\"\n", typestr
);
782 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
785 struct dl_opts
*opts
= &dl
->opts
;
786 uint32_t o_all
= o_required
| o_optional
;
787 uint32_t o_found
= 0;
790 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
793 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
794 &opts
->port_index
, &handle_bit
);
797 o_found
|= handle_bit
;
798 } else if (o_required
& DL_OPT_HANDLE
) {
799 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
802 o_found
|= DL_OPT_HANDLE
;
803 } else if (o_required
& DL_OPT_HANDLEP
) {
804 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
808 o_found
|= DL_OPT_HANDLEP
;
811 while (dl_argc(dl
)) {
812 if (dl_argv_match(dl
, "type") &&
813 (o_all
& DL_OPT_PORT_TYPE
)) {
817 err
= dl_argv_str(dl
, &typestr
);
820 err
= port_type_get(typestr
, &opts
->port_type
);
823 o_found
|= DL_OPT_PORT_TYPE
;
824 } else if (dl_argv_match(dl
, "count") &&
825 (o_all
& DL_OPT_PORT_COUNT
)) {
827 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
830 o_found
|= DL_OPT_PORT_COUNT
;
831 } else if (dl_argv_match(dl
, "sb") &&
832 (o_all
& DL_OPT_SB
)) {
834 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
837 o_found
|= DL_OPT_SB
;
838 } else if (dl_argv_match(dl
, "pool") &&
839 (o_all
& DL_OPT_SB_POOL
)) {
841 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
844 o_found
|= DL_OPT_SB_POOL
;
845 } else if (dl_argv_match(dl
, "size") &&
846 (o_all
& DL_OPT_SB_SIZE
)) {
848 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
851 o_found
|= DL_OPT_SB_SIZE
;
852 } else if (dl_argv_match(dl
, "type") &&
853 (o_all
& DL_OPT_SB_TYPE
)) {
857 err
= dl_argv_str(dl
, &typestr
);
860 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
863 o_found
|= DL_OPT_SB_TYPE
;
864 } else if (dl_argv_match(dl
, "thtype") &&
865 (o_all
& DL_OPT_SB_THTYPE
)) {
869 err
= dl_argv_str(dl
, &typestr
);
872 err
= threshold_type_get(typestr
,
873 &opts
->sb_pool_thtype
);
876 o_found
|= DL_OPT_SB_THTYPE
;
877 } else if (dl_argv_match(dl
, "th") &&
878 (o_all
& DL_OPT_SB_TH
)) {
880 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
883 o_found
|= DL_OPT_SB_TH
;
884 } else if (dl_argv_match(dl
, "tc") &&
885 (o_all
& DL_OPT_SB_TC
)) {
887 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
890 o_found
|= DL_OPT_SB_TC
;
891 } else if (dl_argv_match(dl
, "mode") &&
892 (o_all
& DL_OPT_ESWITCH_MODE
)) {
896 err
= dl_argv_str(dl
, &typestr
);
899 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
902 o_found
|= DL_OPT_ESWITCH_MODE
;
903 } else if (dl_argv_match(dl
, "inline-mode") &&
904 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
908 err
= dl_argv_str(dl
, &typestr
);
911 err
= eswitch_inline_mode_get(
912 typestr
, &opts
->eswitch_inline_mode
);
915 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
916 } else if (dl_argv_match(dl
, "name") &&
917 (o_all
& DL_OPT_DPIPE_TABLE_NAME
)) {
919 err
= dl_argv_str(dl
, &opts
->dpipe_table_name
);
922 o_found
|= DL_OPT_DPIPE_TABLE_NAME
;
923 } else if (dl_argv_match(dl
, "counters") &&
924 (o_all
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
928 err
= dl_argv_str(dl
, &typestr
);
931 err
= dpipe_counters_enable_get(typestr
,
932 &opts
->dpipe_counters_enable
);
935 o_found
|= DL_OPT_DPIPE_TABLE_COUNTERS
;
936 } else if (dl_argv_match(dl
, "encap") &&
937 (o_all
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
941 err
= dl_argv_str(dl
, &typestr
);
944 err
= eswitch_encap_mode_get(typestr
,
945 &opts
->eswitch_encap_mode
);
948 o_found
|= DL_OPT_ESWITCH_ENCAP_MODE
;
950 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
955 opts
->present
= o_found
;
957 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
959 opts
->present
|= DL_OPT_SB
;
962 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
963 pr_err("Port type option expected.\n");
967 if ((o_required
& DL_OPT_PORT_COUNT
) &&
968 !(o_found
& DL_OPT_PORT_COUNT
)) {
969 pr_err("Port split count option expected.\n");
973 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
974 pr_err("Pool index option expected.\n");
978 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
979 pr_err("Pool size option expected.\n");
983 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
984 pr_err("Pool type option expected.\n");
988 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
989 pr_err("Pool threshold type option expected.\n");
993 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
994 pr_err("Threshold option expected.\n");
998 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
999 pr_err("TC index option expected.\n");
1003 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
1004 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
1005 pr_err("E-Switch mode option expected.\n");
1009 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
1010 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
1011 pr_err("E-Switch inline-mode option expected.\n");
1015 if ((o_required
& DL_OPT_DPIPE_TABLE_NAME
) &&
1016 !(o_found
& DL_OPT_DPIPE_TABLE_NAME
)) {
1017 pr_err("Dpipe table name expected\n");
1021 if ((o_required
& DL_OPT_DPIPE_TABLE_COUNTERS
) &&
1022 !(o_found
& DL_OPT_DPIPE_TABLE_COUNTERS
)) {
1023 pr_err("Dpipe table counter state expected\n");
1027 if ((o_required
& DL_OPT_ESWITCH_ENCAP_MODE
) &&
1028 !(o_found
& DL_OPT_ESWITCH_ENCAP_MODE
)) {
1029 pr_err("E-Switch encapsulation option expected.\n");
1036 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
1038 struct dl_opts
*opts
= &dl
->opts
;
1040 if (opts
->present
& DL_OPT_HANDLE
) {
1041 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1042 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1043 } else if (opts
->present
& DL_OPT_HANDLEP
) {
1044 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
1045 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
1046 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
1049 if (opts
->present
& DL_OPT_PORT_TYPE
)
1050 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
1052 if (opts
->present
& DL_OPT_PORT_COUNT
)
1053 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
1055 if (opts
->present
& DL_OPT_SB
)
1056 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
1058 if (opts
->present
& DL_OPT_SB_POOL
)
1059 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
1060 opts
->sb_pool_index
);
1061 if (opts
->present
& DL_OPT_SB_SIZE
)
1062 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
1063 opts
->sb_pool_size
);
1064 if (opts
->present
& DL_OPT_SB_TYPE
)
1065 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
1066 opts
->sb_pool_type
);
1067 if (opts
->present
& DL_OPT_SB_THTYPE
)
1068 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1069 opts
->sb_pool_thtype
);
1070 if (opts
->present
& DL_OPT_SB_TH
)
1071 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
1072 opts
->sb_threshold
);
1073 if (opts
->present
& DL_OPT_SB_TC
)
1074 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
1076 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
1077 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
1078 opts
->eswitch_mode
);
1079 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
1080 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1081 opts
->eswitch_inline_mode
);
1082 if (opts
->present
& DL_OPT_DPIPE_TABLE_NAME
)
1083 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DPIPE_TABLE_NAME
,
1084 opts
->dpipe_table_name
);
1085 if (opts
->present
& DL_OPT_DPIPE_TABLE_COUNTERS
)
1086 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1087 opts
->dpipe_counters_enable
);
1088 if (opts
->present
& DL_OPT_ESWITCH_ENCAP_MODE
)
1089 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
,
1090 opts
->eswitch_encap_mode
);
1093 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
1094 uint32_t o_required
, uint32_t o_optional
)
1098 err
= dl_argv_parse(dl
, o_required
, o_optional
);
1101 dl_opts_put(nlh
, dl
);
1105 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
1107 struct dl_opts
*opts
= &dl
->opts
;
1108 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
1109 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
1110 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
1111 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
1113 if (opts
->present
& DL_OPT_HANDLE
&&
1114 attr_bus_name
&& attr_dev_name
) {
1115 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1116 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1118 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1119 strcmp(dev_name
, opts
->dev_name
) != 0)
1122 if (opts
->present
& DL_OPT_HANDLEP
&&
1123 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
1124 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
1125 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
1126 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
1128 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
1129 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1130 port_index
!= opts
->port_index
)
1133 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1134 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1136 if (sb_index
!= opts
->sb_index
)
1142 static void cmd_dev_help(void)
1144 pr_err("Usage: devlink dev show [ DEV ]\n");
1145 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1146 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1147 pr_err(" [ encap { disable | enable } ]\n");
1148 pr_err(" devlink dev eswitch show DEV\n");
1151 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1152 const char *dev_name
)
1154 if (!dl
->arr_last
.present
)
1156 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1157 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1160 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1161 const char *dev_name
)
1163 dl
->arr_last
.present
= true;
1164 free(dl
->arr_last
.dev_name
);
1165 free(dl
->arr_last
.bus_name
);
1166 dl
->arr_last
.bus_name
= strdup(bus_name
);
1167 dl
->arr_last
.dev_name
= strdup(dev_name
);
1170 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1171 const char *dev_name
)
1173 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1176 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1177 const char *dev_name
)
1179 return dl
->arr_last
.present
&&
1180 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1183 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1184 bool content
, bool array
)
1186 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1187 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1190 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1192 if (dl
->json_output
) {
1194 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1195 jsonw_end_array(dl
->jw
);
1196 if (should_arr_last_handle_start(dl
, bus_name
,
1198 jsonw_name(dl
->jw
, buf
);
1199 jsonw_start_array(dl
->jw
);
1200 jsonw_start_object(dl
->jw
);
1201 arr_last_handle_set(dl
, bus_name
, dev_name
);
1203 jsonw_start_object(dl
->jw
);
1206 jsonw_name(dl
->jw
, buf
);
1207 jsonw_start_object(dl
->jw
);
1211 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1212 __pr_out_indent_dec();
1213 if (should_arr_last_handle_start(dl
, bus_name
,
1215 pr_out("%s%s", buf
, content
? ":" : "");
1217 __pr_out_indent_inc();
1218 arr_last_handle_set(dl
, bus_name
, dev_name
);
1221 pr_out("%s%s", buf
, content
? ":" : "");
1226 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1228 __pr_out_handle_start(dl
, tb
, true, true);
1231 static void pr_out_handle_end(struct dl
*dl
)
1233 if (dl
->json_output
)
1234 jsonw_end_object(dl
->jw
);
1239 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1241 __pr_out_handle_start(dl
, tb
, false, false);
1242 pr_out_handle_end(dl
);
1245 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1246 const char *dev_name
, uint32_t port_index
)
1248 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1249 dl
->arr_last
.port_index
== port_index
;
1252 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1253 const char *dev_name
, uint32_t port_index
)
1255 arr_last_handle_set(dl
, bus_name
, dev_name
);
1256 dl
->arr_last
.port_index
= port_index
;
1259 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1260 const char *bus_name
,
1261 const char *dev_name
,
1262 uint32_t port_index
)
1264 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1267 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1268 const char *bus_name
,
1269 const char *dev_name
,
1270 uint32_t port_index
)
1272 return dl
->arr_last
.present
&&
1273 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1276 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1277 const char *dev_name
,
1278 uint32_t port_index
, bool try_nice
,
1281 static char buf
[32];
1282 char *ifname
= NULL
;
1284 if (dl
->no_nice_names
|| !try_nice
||
1285 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1286 port_index
, &ifname
) != 0)
1287 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1289 sprintf(buf
, "%s", ifname
);
1291 if (dl
->json_output
) {
1293 if (should_arr_last_port_handle_end(dl
, bus_name
,
1296 jsonw_end_array(dl
->jw
);
1297 if (should_arr_last_port_handle_start(dl
, bus_name
,
1300 jsonw_name(dl
->jw
, buf
);
1301 jsonw_start_array(dl
->jw
);
1302 jsonw_start_object(dl
->jw
);
1303 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1306 jsonw_start_object(dl
->jw
);
1309 jsonw_name(dl
->jw
, buf
);
1310 jsonw_start_object(dl
->jw
);
1317 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1319 const char *bus_name
;
1320 const char *dev_name
;
1321 uint32_t port_index
;
1323 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1324 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1325 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1326 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1329 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1331 const char *bus_name
;
1332 const char *dev_name
;
1333 uint32_t port_index
;
1335 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1336 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1337 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1338 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1341 static void pr_out_port_handle_end(struct dl
*dl
)
1343 if (dl
->json_output
)
1344 jsonw_end_object(dl
->jw
);
1350 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1352 if (dl
->json_output
) {
1353 jsonw_string_field(dl
->jw
, name
, val
);
1355 if (g_indent_newline
)
1356 pr_out("%s %s", name
, val
);
1358 pr_out(" %s %s", name
, val
);
1362 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1364 if (dl
->json_output
) {
1365 jsonw_uint_field(dl
->jw
, name
, val
);
1367 if (g_indent_newline
)
1368 pr_out("%s %u", name
, val
);
1370 pr_out(" %s %u", name
, val
);
1374 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1376 pr_out_handle(dl
, tb
);
1379 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1381 if (dl
->json_output
) {
1382 jsonw_start_object(dl
->jw
);
1383 jsonw_name(dl
->jw
, name
);
1384 jsonw_start_object(dl
->jw
);
1388 static void pr_out_section_end(struct dl
*dl
)
1390 if (dl
->json_output
) {
1391 if (dl
->arr_last
.present
)
1392 jsonw_end_array(dl
->jw
);
1393 jsonw_end_object(dl
->jw
);
1394 jsonw_end_object(dl
->jw
);
1398 static void pr_out_array_start(struct dl
*dl
, const char *name
)
1400 if (dl
->json_output
) {
1401 jsonw_name(dl
->jw
, name
);
1402 jsonw_start_array(dl
->jw
);
1404 if (!g_indent_newline
)
1406 pr_out("%s:", name
);
1408 __pr_out_indent_inc();
1412 static void pr_out_array_end(struct dl
*dl
)
1414 if (dl
->json_output
)
1415 jsonw_end_array(dl
->jw
);
1417 __pr_out_indent_dec();
1420 static void pr_out_entry_start(struct dl
*dl
)
1422 if (dl
->json_output
)
1423 jsonw_start_object(dl
->jw
);
1426 static void pr_out_entry_end(struct dl
*dl
)
1428 if (dl
->json_output
)
1429 jsonw_end_object(dl
->jw
);
1434 static const char *eswitch_mode_name(uint32_t mode
)
1437 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1438 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1439 default: return "<unknown mode>";
1443 static const char *eswitch_inline_mode_name(uint32_t mode
)
1446 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1447 return ESWITCH_INLINE_MODE_NONE
;
1448 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1449 return ESWITCH_INLINE_MODE_LINK
;
1450 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1451 return ESWITCH_INLINE_MODE_NETWORK
;
1452 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1453 return ESWITCH_INLINE_MODE_TRANSPORT
;
1455 return "<unknown mode>";
1459 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1461 __pr_out_handle_start(dl
, tb
, true, false);
1463 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1464 pr_out_str(dl
, "mode",
1465 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1467 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1468 pr_out_str(dl
, "inline-mode",
1469 eswitch_inline_mode_name(mnl_attr_get_u8(
1470 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1472 if (tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1473 bool encap_mode
= !!mnl_attr_get_u8(tb
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1475 pr_out_str(dl
, "encap", encap_mode
? "enable" : "disable");
1478 pr_out_handle_end(dl
);
1481 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1483 struct dl
*dl
= data
;
1484 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1485 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1487 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1488 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1489 return MNL_CB_ERROR
;
1490 pr_out_eswitch(dl
, tb
);
1494 static int cmd_dev_eswitch_show(struct dl
*dl
)
1496 struct nlmsghdr
*nlh
;
1499 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_GET
,
1500 NLM_F_REQUEST
| NLM_F_ACK
);
1502 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1506 pr_out_section_start(dl
, "dev");
1507 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1508 pr_out_section_end(dl
);
1512 static int cmd_dev_eswitch_set(struct dl
*dl
)
1514 struct nlmsghdr
*nlh
;
1517 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_SET
,
1518 NLM_F_REQUEST
| NLM_F_ACK
);
1520 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1521 DL_OPT_ESWITCH_MODE
|
1522 DL_OPT_ESWITCH_INLINE_MODE
|
1523 DL_OPT_ESWITCH_ENCAP_MODE
);
1528 if (dl
->opts
.present
== 1) {
1529 pr_err("Need to set at least one option\n");
1533 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1536 static int cmd_dev_eswitch(struct dl
*dl
)
1538 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1541 } else if (dl_argv_match(dl
, "set")) {
1543 return cmd_dev_eswitch_set(dl
);
1544 } else if (dl_argv_match(dl
, "show")) {
1546 return cmd_dev_eswitch_show(dl
);
1548 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1552 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1554 struct dl
*dl
= data
;
1555 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1556 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1558 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1559 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1560 return MNL_CB_ERROR
;
1565 static int cmd_dev_show(struct dl
*dl
)
1567 struct nlmsghdr
*nlh
;
1568 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1571 if (dl_argc(dl
) == 0)
1572 flags
|= NLM_F_DUMP
;
1574 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
1576 if (dl_argc(dl
) > 0) {
1577 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1582 pr_out_section_start(dl
, "dev");
1583 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
1584 pr_out_section_end(dl
);
1588 static int cmd_dev(struct dl
*dl
)
1590 if (dl_argv_match(dl
, "help")) {
1593 } else if (dl_argv_match(dl
, "show") ||
1594 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1596 return cmd_dev_show(dl
);
1597 } else if (dl_argv_match(dl
, "eswitch")) {
1599 return cmd_dev_eswitch(dl
);
1601 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1605 static void cmd_port_help(void)
1607 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
1608 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
1609 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
1610 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
1613 static const char *port_type_name(uint32_t type
)
1616 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
1617 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
1618 case DEVLINK_PORT_TYPE_ETH
: return "eth";
1619 case DEVLINK_PORT_TYPE_IB
: return "ib";
1620 default: return "<unknown type>";
1624 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
1626 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
1627 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
1629 pr_out_port_handle_start(dl
, tb
, false);
1631 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
1633 pr_out_str(dl
, "type", port_type_name(port_type
));
1635 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
1637 if (port_type
!= des_port_type
)
1638 pr_out_str(dl
, "des_type",
1639 port_type_name(des_port_type
));
1642 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
1643 pr_out_str(dl
, "netdev",
1644 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
1645 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
1646 pr_out_str(dl
, "ibdev",
1647 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
1648 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
1649 pr_out_uint(dl
, "split_group",
1650 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
1651 pr_out_port_handle_end(dl
);
1654 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1656 struct dl
*dl
= data
;
1657 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1658 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1660 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1661 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1662 !tb
[DEVLINK_ATTR_PORT_INDEX
])
1663 return MNL_CB_ERROR
;
1664 pr_out_port(dl
, tb
);
1668 static int cmd_port_show(struct dl
*dl
)
1670 struct nlmsghdr
*nlh
;
1671 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1674 if (dl_argc(dl
) == 0)
1675 flags
|= NLM_F_DUMP
;
1677 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
1679 if (dl_argc(dl
) > 0) {
1680 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1685 pr_out_section_start(dl
, "port");
1686 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
1687 pr_out_section_end(dl
);
1691 static int cmd_port_set(struct dl
*dl
)
1693 struct nlmsghdr
*nlh
;
1696 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
1697 NLM_F_REQUEST
| NLM_F_ACK
);
1699 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
1703 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1706 static int cmd_port_split(struct dl
*dl
)
1708 struct nlmsghdr
*nlh
;
1711 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
1712 NLM_F_REQUEST
| NLM_F_ACK
);
1714 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
1718 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1721 static int cmd_port_unsplit(struct dl
*dl
)
1723 struct nlmsghdr
*nlh
;
1726 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
1727 NLM_F_REQUEST
| NLM_F_ACK
);
1729 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1733 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1736 static int cmd_port(struct dl
*dl
)
1738 if (dl_argv_match(dl
, "help")) {
1741 } else if (dl_argv_match(dl
, "show") ||
1742 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1744 return cmd_port_show(dl
);
1745 } else if (dl_argv_match(dl
, "set")) {
1747 return cmd_port_set(dl
);
1748 } else if (dl_argv_match(dl
, "split")) {
1750 return cmd_port_split(dl
);
1751 } else if (dl_argv_match(dl
, "unsplit")) {
1753 return cmd_port_unsplit(dl
);
1755 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1759 static void cmd_sb_help(void)
1761 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
1762 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
1763 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
1764 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
1765 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1766 pr_err(" pool POOL_INDEX ]\n");
1767 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1768 pr_err(" pool POOL_INDEX th THRESHOLD\n");
1769 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1770 pr_err(" type { ingress | egress } ]\n");
1771 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1772 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
1773 pr_err(" th THRESHOLD\n");
1774 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
1775 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
1776 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
1779 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
1781 pr_out_handle_start_arr(dl
, tb
);
1782 pr_out_uint(dl
, "sb",
1783 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1784 pr_out_uint(dl
, "size",
1785 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
1786 pr_out_uint(dl
, "ing_pools",
1787 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
1788 pr_out_uint(dl
, "eg_pools",
1789 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
1790 pr_out_uint(dl
, "ing_tcs",
1791 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
1792 pr_out_uint(dl
, "eg_tcs",
1793 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
1794 pr_out_handle_end(dl
);
1797 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1799 struct dl
*dl
= data
;
1800 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1801 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1803 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1804 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1805 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
1806 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
1807 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
1808 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
1809 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
1810 return MNL_CB_ERROR
;
1815 static int cmd_sb_show(struct dl
*dl
)
1817 struct nlmsghdr
*nlh
;
1818 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1821 if (dl_argc(dl
) == 0)
1822 flags
|= NLM_F_DUMP
;
1824 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
1826 if (dl_argc(dl
) > 0) {
1827 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
1832 pr_out_section_start(dl
, "sb");
1833 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
1834 pr_out_section_end(dl
);
1838 static const char *pool_type_name(uint8_t type
)
1841 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
1842 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
1843 default: return "<unknown type>";
1847 static const char *threshold_type_name(uint8_t type
)
1850 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
1851 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
1852 default: return "<unknown type>";
1856 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
1858 pr_out_handle_start_arr(dl
, tb
);
1859 pr_out_uint(dl
, "sb",
1860 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1861 pr_out_uint(dl
, "pool",
1862 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1863 pr_out_str(dl
, "type",
1864 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
1865 pr_out_uint(dl
, "size",
1866 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
1867 pr_out_str(dl
, "thtype",
1868 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
1869 pr_out_handle_end(dl
);
1872 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1874 struct dl
*dl
= data
;
1875 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1876 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1878 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1879 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1880 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
1881 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
1882 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
1883 return MNL_CB_ERROR
;
1884 pr_out_sb_pool(dl
, tb
);
1888 static int cmd_sb_pool_show(struct dl
*dl
)
1890 struct nlmsghdr
*nlh
;
1891 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1894 if (dl_argc(dl
) == 0)
1895 flags
|= NLM_F_DUMP
;
1897 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
1899 if (dl_argc(dl
) > 0) {
1900 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
1906 pr_out_section_start(dl
, "pool");
1907 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
1908 pr_out_section_end(dl
);
1912 static int cmd_sb_pool_set(struct dl
*dl
)
1914 struct nlmsghdr
*nlh
;
1917 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
1918 NLM_F_REQUEST
| NLM_F_ACK
);
1920 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
1921 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
1925 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1928 static int cmd_sb_pool(struct dl
*dl
)
1930 if (dl_argv_match(dl
, "help")) {
1933 } else if (dl_argv_match(dl
, "show") ||
1934 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1936 return cmd_sb_pool_show(dl
);
1937 } else if (dl_argv_match(dl
, "set")) {
1939 return cmd_sb_pool_set(dl
);
1941 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1945 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
1947 pr_out_port_handle_start_arr(dl
, tb
, true);
1948 pr_out_uint(dl
, "sb",
1949 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1950 pr_out_uint(dl
, "pool",
1951 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1952 pr_out_uint(dl
, "threshold",
1953 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
1954 pr_out_port_handle_end(dl
);
1957 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1959 struct dl
*dl
= data
;
1960 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1961 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1963 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1964 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1965 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
1966 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
1967 return MNL_CB_ERROR
;
1968 pr_out_sb_port_pool(dl
, tb
);
1972 static int cmd_sb_port_pool_show(struct dl
*dl
)
1974 struct nlmsghdr
*nlh
;
1975 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1978 if (dl_argc(dl
) == 0)
1979 flags
|= NLM_F_DUMP
;
1981 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
1983 if (dl_argc(dl
) > 0) {
1984 err
= dl_argv_parse_put(nlh
, dl
,
1985 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
1991 pr_out_section_start(dl
, "port_pool");
1992 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
1993 pr_out_section_end(dl
);
1997 static int cmd_sb_port_pool_set(struct dl
*dl
)
1999 struct nlmsghdr
*nlh
;
2002 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
2003 NLM_F_REQUEST
| NLM_F_ACK
);
2005 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
2006 DL_OPT_SB_TH
, DL_OPT_SB
);
2010 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2013 static int cmd_sb_port_pool(struct dl
*dl
)
2015 if (dl_argv_match(dl
, "help")) {
2018 } else if (dl_argv_match(dl
, "show") ||
2019 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2021 return cmd_sb_port_pool_show(dl
);
2022 } else if (dl_argv_match(dl
, "set")) {
2024 return cmd_sb_port_pool_set(dl
);
2026 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2030 static int cmd_sb_port(struct dl
*dl
)
2032 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2035 } else if (dl_argv_match(dl
, "pool")) {
2037 return cmd_sb_port_pool(dl
);
2039 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2043 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
2045 pr_out_port_handle_start_arr(dl
, tb
, true);
2046 pr_out_uint(dl
, "sb",
2047 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
2048 pr_out_uint(dl
, "tc",
2049 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
2050 pr_out_str(dl
, "type",
2051 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
2052 pr_out_uint(dl
, "pool",
2053 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
2054 pr_out_uint(dl
, "threshold",
2055 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
2056 pr_out_port_handle_end(dl
);
2059 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2061 struct dl
*dl
= data
;
2062 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2063 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2065 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2066 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2067 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2068 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2069 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
2070 return MNL_CB_ERROR
;
2071 pr_out_sb_tc_bind(dl
, tb
);
2075 static int cmd_sb_tc_bind_show(struct dl
*dl
)
2077 struct nlmsghdr
*nlh
;
2078 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
2081 if (dl_argc(dl
) == 0)
2082 flags
|= NLM_F_DUMP
;
2084 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2086 if (dl_argc(dl
) > 0) {
2087 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2088 DL_OPT_SB_TYPE
, DL_OPT_SB
);
2093 pr_out_section_start(dl
, "tc_bind");
2094 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
2095 pr_out_section_end(dl
);
2099 static int cmd_sb_tc_bind_set(struct dl
*dl
)
2101 struct nlmsghdr
*nlh
;
2104 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2105 NLM_F_REQUEST
| NLM_F_ACK
);
2107 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
2108 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
2113 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2116 static int cmd_sb_tc_bind(struct dl
*dl
)
2118 if (dl_argv_match(dl
, "help")) {
2121 } else if (dl_argv_match(dl
, "show") ||
2122 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2124 return cmd_sb_tc_bind_show(dl
);
2125 } else if (dl_argv_match(dl
, "set")) {
2127 return cmd_sb_tc_bind_set(dl
);
2129 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2133 static int cmd_sb_tc(struct dl
*dl
)
2135 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2138 } else if (dl_argv_match(dl
, "bind")) {
2140 return cmd_sb_tc_bind(dl
);
2142 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2147 struct list_head list
;
2151 uint32_t bound_pool_index
;
2155 struct list_head list
;
2158 uint32_t port_index
;
2160 struct list_head pool_list
;
2161 struct list_head ing_tc_list
;
2162 struct list_head eg_tc_list
;
2168 struct list_head port_list
;
2171 static struct occ_item
*occ_item_alloc(void)
2173 return calloc(1, sizeof(struct occ_item
));
2176 static void occ_item_free(struct occ_item
*occ_item
)
2181 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
2183 struct occ_port
*occ_port
;
2185 occ_port
= calloc(1, sizeof(*occ_port
));
2188 occ_port
->port_index
= port_index
;
2189 INIT_LIST_HEAD(&occ_port
->pool_list
);
2190 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
2191 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
2195 static void occ_port_free(struct occ_port
*occ_port
)
2197 struct occ_item
*occ_item
, *tmp
;
2199 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
2200 occ_item_free(occ_item
);
2201 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
2202 occ_item_free(occ_item
);
2203 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
2204 occ_item_free(occ_item
);
2207 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
2209 struct occ_show
*occ_show
;
2211 occ_show
= calloc(1, sizeof(*occ_show
));
2215 INIT_LIST_HEAD(&occ_show
->port_list
);
2219 static void occ_show_free(struct occ_show
*occ_show
)
2221 struct occ_port
*occ_port
, *tmp
;
2223 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
2224 occ_port_free(occ_port
);
2227 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
2230 struct occ_port
*occ_port
;
2231 uint32_t port_index
;
2233 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2235 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
2236 if (occ_port
->port_index
== port_index
)
2239 occ_port
= occ_port_alloc(port_index
);
2242 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
2246 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
2249 struct occ_item
*occ_item
;
2252 pr_out_sp(7, " %s:", label
);
2253 list_for_each_entry(occ_item
, list
, list
) {
2254 if ((i
- 1) % 4 == 0 && i
!= 1)
2257 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
2258 occ_item
->bound_pool_index
);
2260 pr_out_sp(7, "%2u:", occ_item
->index
);
2261 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
2265 if ((i
- 1) % 4 != 0)
2269 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
2270 struct list_head
*list
,
2273 struct occ_item
*occ_item
;
2276 jsonw_name(dl
->jw
, label
);
2277 jsonw_start_object(dl
->jw
);
2278 list_for_each_entry(occ_item
, list
, list
) {
2279 sprintf(buf
, "%u", occ_item
->index
);
2280 jsonw_name(dl
->jw
, buf
);
2281 jsonw_start_object(dl
->jw
);
2283 jsonw_uint_field(dl
->jw
, "bound_pool",
2284 occ_item
->bound_pool_index
);
2285 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
2286 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
2287 jsonw_end_object(dl
->jw
);
2289 jsonw_end_object(dl
->jw
);
2292 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
2294 if (dl
->json_output
) {
2295 pr_out_json_occ_show_item_list(dl
, "pool",
2296 &occ_port
->pool_list
, false);
2297 pr_out_json_occ_show_item_list(dl
, "itc",
2298 &occ_port
->ing_tc_list
, true);
2299 pr_out_json_occ_show_item_list(dl
, "etc",
2300 &occ_port
->eg_tc_list
, true);
2303 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
2304 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
2305 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
2309 static void pr_out_occ_show(struct occ_show
*occ_show
)
2311 struct dl
*dl
= occ_show
->dl
;
2312 struct dl_opts
*opts
= &dl
->opts
;
2313 struct occ_port
*occ_port
;
2315 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
2316 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
2317 occ_port
->port_index
, true, false);
2318 pr_out_occ_show_port(dl
, occ_port
);
2319 pr_out_port_handle_end(dl
);
2323 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
2326 struct occ_port
*occ_port
;
2327 struct occ_item
*occ_item
;
2329 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2332 occ_port
= occ_port_get(occ_show
, tb
);
2334 occ_show
->err
= -ENOMEM
;
2338 occ_item
= occ_item_alloc();
2340 occ_show
->err
= -ENOMEM
;
2343 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2344 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2345 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2346 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
2349 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2351 struct occ_show
*occ_show
= data
;
2352 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2353 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2355 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2356 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2357 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2358 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2359 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2360 return MNL_CB_ERROR
;
2361 cmd_sb_occ_port_pool_process(occ_show
, tb
);
2365 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
2368 struct occ_port
*occ_port
;
2369 struct occ_item
*occ_item
;
2372 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2375 occ_port
= occ_port_get(occ_show
, tb
);
2377 occ_show
->err
= -ENOMEM
;
2381 occ_item
= occ_item_alloc();
2383 occ_show
->err
= -ENOMEM
;
2386 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
2387 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2388 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2389 occ_item
->bound_pool_index
=
2390 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2391 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
2392 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
2393 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
2394 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
2395 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
2397 occ_item_free(occ_item
);
2400 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2402 struct occ_show
*occ_show
= data
;
2403 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2404 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2406 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2407 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2408 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2409 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2410 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2411 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2412 return MNL_CB_ERROR
;
2413 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
2417 static int cmd_sb_occ_show(struct dl
*dl
)
2419 struct nlmsghdr
*nlh
;
2420 struct occ_show
*occ_show
;
2421 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
2424 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
2428 occ_show
= occ_show_alloc(dl
);
2432 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2434 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2435 cmd_sb_occ_port_pool_process_cb
, occ_show
);
2439 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2441 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2442 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
2446 pr_out_section_start(dl
, "occupancy");
2447 pr_out_occ_show(occ_show
);
2448 pr_out_section_end(dl
);
2451 occ_show_free(occ_show
);
2455 static int cmd_sb_occ_snapshot(struct dl
*dl
)
2457 struct nlmsghdr
*nlh
;
2460 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2461 NLM_F_REQUEST
| NLM_F_ACK
);
2463 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2467 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2470 static int cmd_sb_occ_clearmax(struct dl
*dl
)
2472 struct nlmsghdr
*nlh
;
2475 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2476 NLM_F_REQUEST
| NLM_F_ACK
);
2478 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2482 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2485 static int cmd_sb_occ(struct dl
*dl
)
2487 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2490 } else if (dl_argv_match(dl
, "show") ||
2491 dl_argv_match(dl
, "list")) {
2493 return cmd_sb_occ_show(dl
);
2494 } else if (dl_argv_match(dl
, "snapshot")) {
2496 return cmd_sb_occ_snapshot(dl
);
2497 } else if (dl_argv_match(dl
, "clearmax")) {
2499 return cmd_sb_occ_clearmax(dl
);
2501 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2505 static int cmd_sb(struct dl
*dl
)
2507 if (dl_argv_match(dl
, "help")) {
2510 } else if (dl_argv_match(dl
, "show") ||
2511 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2513 return cmd_sb_show(dl
);
2514 } else if (dl_argv_match(dl
, "pool")) {
2516 return cmd_sb_pool(dl
);
2517 } else if (dl_argv_match(dl
, "port")) {
2519 return cmd_sb_port(dl
);
2520 } else if (dl_argv_match(dl
, "tc")) {
2522 return cmd_sb_tc(dl
);
2523 } else if (dl_argv_match(dl
, "occupancy")) {
2525 return cmd_sb_occ(dl
);
2527 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2531 static const char *cmd_name(uint8_t cmd
)
2534 case DEVLINK_CMD_UNSPEC
: return "unspec";
2535 case DEVLINK_CMD_GET
: return "get";
2536 case DEVLINK_CMD_SET
: return "set";
2537 case DEVLINK_CMD_NEW
: return "new";
2538 case DEVLINK_CMD_DEL
: return "del";
2539 case DEVLINK_CMD_PORT_GET
: return "get";
2540 case DEVLINK_CMD_PORT_SET
: return "set";
2541 case DEVLINK_CMD_PORT_NEW
: return "net";
2542 case DEVLINK_CMD_PORT_DEL
: return "del";
2543 default: return "<unknown cmd>";
2547 static const char *cmd_obj(uint8_t cmd
)
2550 case DEVLINK_CMD_UNSPEC
: return "unspec";
2551 case DEVLINK_CMD_GET
:
2552 case DEVLINK_CMD_SET
:
2553 case DEVLINK_CMD_NEW
:
2554 case DEVLINK_CMD_DEL
:
2556 case DEVLINK_CMD_PORT_GET
:
2557 case DEVLINK_CMD_PORT_SET
:
2558 case DEVLINK_CMD_PORT_NEW
:
2559 case DEVLINK_CMD_PORT_DEL
:
2561 default: return "<unknown obj>";
2565 static void pr_out_mon_header(uint8_t cmd
)
2567 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
2570 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
2572 const char *obj
= cmd_obj(cmd
);
2573 unsigned int index
= 0;
2574 const char *cur_obj
;
2578 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2579 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
2585 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2587 struct dl
*dl
= data
;
2588 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2589 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2590 uint8_t cmd
= genl
->cmd
;
2592 if (!cmd_filter_check(dl
, cmd
))
2596 case DEVLINK_CMD_GET
: /* fall through */
2597 case DEVLINK_CMD_SET
: /* fall through */
2598 case DEVLINK_CMD_NEW
: /* fall through */
2599 case DEVLINK_CMD_DEL
:
2600 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2601 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2602 return MNL_CB_ERROR
;
2603 pr_out_mon_header(genl
->cmd
);
2606 case DEVLINK_CMD_PORT_GET
: /* fall through */
2607 case DEVLINK_CMD_PORT_SET
: /* fall through */
2608 case DEVLINK_CMD_PORT_NEW
: /* fall through */
2609 case DEVLINK_CMD_PORT_DEL
:
2610 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2611 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2612 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2613 return MNL_CB_ERROR
;
2614 pr_out_mon_header(genl
->cmd
);
2615 pr_out_port(dl
, tb
);
2621 static int cmd_mon_show(struct dl
*dl
)
2624 unsigned int index
= 0;
2625 const char *cur_obj
;
2627 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2628 if (strcmp(cur_obj
, "all") != 0 &&
2629 strcmp(cur_obj
, "dev") != 0 &&
2630 strcmp(cur_obj
, "port") != 0) {
2631 pr_err("Unknown object \"%s\"\n", cur_obj
);
2635 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
2638 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
2644 static void cmd_mon_help(void)
2646 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
2647 "where OBJECT-LIST := { dev | port }\n");
2650 static int cmd_mon(struct dl
*dl
)
2652 if (dl_argv_match(dl
, "help")) {
2655 } else if (dl_no_arg(dl
)) {
2657 return cmd_mon_show(dl
);
2659 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2663 struct dpipe_field
{
2666 unsigned int bitwidth
;
2667 enum devlink_dpipe_field_mapping_type mapping_type
;
2670 struct dpipe_header
{
2671 struct list_head list
;
2674 struct dpipe_field
*fields
;
2675 unsigned int fields_count
;
2681 struct list_head global_headers
;
2682 struct list_head local_headers
;
2686 static struct dpipe_header
*dpipe_header_alloc(unsigned int fields_count
)
2688 struct dpipe_header
*header
;
2690 header
= calloc(1, sizeof(struct dpipe_header
));
2693 header
->fields
= calloc(fields_count
, sizeof(struct dpipe_field
));
2694 if (!header
->fields
)
2695 goto err_fields_alloc
;
2696 header
->fields_count
= fields_count
;
2704 static void dpipe_header_free(struct dpipe_header
*header
)
2706 free(header
->fields
);
2710 static void dpipe_header_clear(struct dpipe_header
*header
)
2712 struct dpipe_field
*field
;
2715 for (i
= 0; i
< header
->fields_count
; i
++) {
2716 field
= &header
->fields
[i
];
2722 static void dpipe_header_add(struct dpipe_ctx
*ctx
,
2723 struct dpipe_header
*header
, bool global
)
2726 list_add(&header
->list
, &ctx
->global_headers
);
2728 list_add(&header
->list
, &ctx
->local_headers
);
2731 static void dpipe_header_del(struct dpipe_header
*header
)
2733 list_del(&header
->list
);
2736 static struct dpipe_ctx
*dpipe_ctx_alloc(struct dl
*dl
)
2738 struct dpipe_ctx
*ctx
;
2740 ctx
= calloc(1, sizeof(struct dpipe_ctx
));
2744 INIT_LIST_HEAD(&ctx
->global_headers
);
2745 INIT_LIST_HEAD(&ctx
->local_headers
);
2749 static void dpipe_ctx_free(struct dpipe_ctx
*ctx
)
2754 static void dpipe_ctx_clear(struct dpipe_ctx
*ctx
)
2756 struct dpipe_header
*header
, *tmp
;
2758 list_for_each_entry_safe(header
, tmp
, &ctx
->global_headers
,
2760 dpipe_header_del(header
);
2761 dpipe_header_clear(header
);
2762 dpipe_header_free(header
);
2764 list_for_each_entry_safe(header
, tmp
, &ctx
->local_headers
,
2766 dpipe_header_del(header
);
2767 dpipe_header_clear(header
);
2768 dpipe_header_free(header
);
2772 static const char *dpipe_header_id2s(struct dpipe_ctx
*ctx
,
2773 uint32_t header_id
, bool global
)
2775 struct list_head
*header_list
;
2776 struct dpipe_header
*header
;
2779 header_list
= &ctx
->global_headers
;
2781 header_list
= &ctx
->local_headers
;
2782 list_for_each_entry(header
, header_list
, list
) {
2783 if (header
->id
!= header_id
)
2785 return header
->name
;
2790 static const char *dpipe_field_id2s(struct dpipe_ctx
*ctx
,
2792 uint32_t field_id
, bool global
)
2794 struct list_head
*header_list
;
2795 struct dpipe_header
*header
;
2798 header_list
= &ctx
->global_headers
;
2800 header_list
= &ctx
->local_headers
;
2801 list_for_each_entry(header
, header_list
, list
) {
2802 if (header
->id
!= header_id
)
2804 return header
->fields
[field_id
].name
;
2810 dpipe_field_mapping_e2s(enum devlink_dpipe_field_mapping_type mapping_type
)
2812 switch (mapping_type
) {
2813 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_NONE
:
2815 case DEVLINK_DPIPE_FIELD_MAPPING_TYPE_IFINDEX
:
2823 dpipe_mapping_get(struct dpipe_ctx
*ctx
, uint32_t header_id
,
2824 uint32_t field_id
, bool global
)
2826 enum devlink_dpipe_field_mapping_type mapping_type
;
2827 struct list_head
*header_list
;
2828 struct dpipe_header
*header
;
2831 header_list
= &ctx
->global_headers
;
2833 header_list
= &ctx
->local_headers
;
2834 list_for_each_entry(header
, header_list
, list
) {
2835 if (header
->id
!= header_id
)
2837 mapping_type
= header
->fields
[field_id
].mapping_type
;
2838 return dpipe_field_mapping_e2s(mapping_type
);
2843 static void pr_out_dpipe_fields(struct dpipe_ctx
*ctx
,
2844 struct dpipe_field
*fields
,
2845 unsigned int field_count
)
2847 struct dpipe_field
*field
;
2850 for (i
= 0; i
< field_count
; i
++) {
2852 pr_out_entry_start(ctx
->dl
);
2853 pr_out_str(ctx
->dl
, "name", field
->name
);
2854 if (ctx
->dl
->verbose
)
2855 pr_out_uint(ctx
->dl
, "id", field
->id
);
2856 pr_out_uint(ctx
->dl
, "bitwidth", field
->bitwidth
);
2857 if (field
->mapping_type
)
2858 pr_out_str(ctx
->dl
, "mapping_type",
2859 dpipe_field_mapping_e2s(field
->mapping_type
));
2860 pr_out_entry_end(ctx
->dl
);
2865 pr_out_dpipe_header(struct dpipe_ctx
*ctx
, struct nlattr
**tb
,
2866 struct dpipe_header
*header
, bool global
)
2868 pr_out_handle_start_arr(ctx
->dl
, tb
);
2869 pr_out_str(ctx
->dl
, "name", header
->name
);
2870 if (ctx
->dl
->verbose
) {
2871 pr_out_uint(ctx
->dl
, "id", header
->id
);
2872 pr_out_str(ctx
->dl
, "global",
2873 global
? "true" : "false");
2875 pr_out_array_start(ctx
->dl
, "field");
2876 pr_out_dpipe_fields(ctx
, header
->fields
,
2877 header
->fields_count
);
2878 pr_out_array_end(ctx
->dl
);
2879 pr_out_handle_end(ctx
->dl
);
2882 static void pr_out_dpipe_headers(struct dpipe_ctx
*ctx
,
2885 struct dpipe_header
*header
;
2887 list_for_each_entry(header
, &ctx
->local_headers
, list
)
2888 pr_out_dpipe_header(ctx
, tb
, header
, false);
2890 list_for_each_entry(header
, &ctx
->global_headers
, list
)
2891 pr_out_dpipe_header(ctx
, tb
, header
, true);
2894 static int dpipe_header_field_get(struct nlattr
*nl
, struct dpipe_field
*field
)
2896 struct nlattr
*nla_field
[DEVLINK_ATTR_MAX
+ 1] = {};
2900 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_field
);
2901 if (err
!= MNL_CB_OK
)
2903 if (!nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
] ||
2904 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
] ||
2905 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
] ||
2906 !nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
])
2909 name
= mnl_attr_get_str(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_NAME
]);
2910 field
->id
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
2911 field
->bitwidth
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
]);
2912 field
->name
= strdup(name
);
2915 field
->mapping_type
= mnl_attr_get_u32(nla_field
[DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
]);
2919 static int dpipe_header_fields_get(struct nlattr
*nla_fields
,
2920 struct dpipe_field
*fields
)
2922 struct nlattr
*nla_field
;
2926 mnl_attr_for_each_nested(nla_field
, nla_fields
) {
2927 err
= dpipe_header_field_get(nla_field
, &fields
[count
]);
2935 static unsigned int dpipe_header_field_count_get(struct nlattr
*nla_fields
)
2937 struct nlattr
*nla_field
;
2938 unsigned int count
= 0;
2940 mnl_attr_for_each_nested(nla_field
, nla_fields
)
2945 static int dpipe_header_get(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
2947 struct nlattr
*nla_header
[DEVLINK_ATTR_MAX
+ 1] = {};
2948 struct dpipe_header
*header
;
2949 unsigned int fields_count
;
2950 const char *header_name
;
2954 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_header
);
2955 if (err
!= MNL_CB_OK
)
2958 if (!nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
] ||
2959 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
2960 !nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
])
2963 fields_count
= dpipe_header_field_count_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
]);
2964 header
= dpipe_header_alloc(fields_count
);
2968 header_name
= mnl_attr_get_str(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_NAME
]);
2969 header
->name
= strdup(header_name
);
2970 header
->id
= mnl_attr_get_u32(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
2971 header
->fields_count
= fields_count
;
2972 global
= !!mnl_attr_get_u8(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
2974 err
= dpipe_header_fields_get(nla_header
[DEVLINK_ATTR_DPIPE_HEADER_FIELDS
],
2978 dpipe_header_add(ctx
, header
, global
);
2982 dpipe_header_free(header
);
2986 static int dpipe_headers_get(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
2988 struct nlattr
*nla_headers
= tb
[DEVLINK_ATTR_DPIPE_HEADERS
];
2989 struct nlattr
*nla_header
;
2992 mnl_attr_for_each_nested(nla_header
, nla_headers
) {
2993 err
= dpipe_header_get(ctx
, nla_header
);
3000 static int cmd_dpipe_header_cb(const struct nlmsghdr
*nlh
, void *data
)
3002 struct dpipe_ctx
*ctx
= data
;
3003 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3004 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3007 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3008 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3009 !tb
[DEVLINK_ATTR_DPIPE_HEADERS
])
3010 return MNL_CB_ERROR
;
3011 err
= dpipe_headers_get(ctx
, tb
);
3014 return MNL_CB_ERROR
;
3017 if (ctx
->print_headers
)
3018 pr_out_dpipe_headers(ctx
, tb
);
3022 static int cmd_dpipe_headers_show(struct dl
*dl
)
3024 struct nlmsghdr
*nlh
;
3025 struct dpipe_ctx
*ctx
;
3026 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3029 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3031 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
3035 ctx
= dpipe_ctx_alloc(dl
);
3039 ctx
->print_headers
= true;
3041 pr_out_section_start(dl
, "header");
3042 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3044 pr_err("error get headers %s\n", strerror(ctx
->err
));
3045 pr_out_section_end(dl
);
3047 dpipe_ctx_clear(ctx
);
3048 dpipe_ctx_free(ctx
);
3052 static void cmd_dpipe_header_help(void)
3054 pr_err("Usage: devlink dpipe headers show DEV\n");
3057 static int cmd_dpipe_header(struct dl
*dl
)
3059 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3060 cmd_dpipe_header_help();
3062 } else if (dl_argv_match(dl
, "show")) {
3064 return cmd_dpipe_headers_show(dl
);
3066 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3071 *dpipe_action_type_e2s(enum devlink_dpipe_action_type action_type
)
3073 switch (action_type
) {
3074 case DEVLINK_DPIPE_ACTION_TYPE_FIELD_MODIFY
:
3075 return "field_modify";
3081 struct dpipe_op_info
{
3087 struct dpipe_action
{
3088 struct dpipe_op_info info
;
3092 static void pr_out_dpipe_action(struct dpipe_action
*action
,
3093 struct dpipe_ctx
*ctx
)
3095 struct dpipe_op_info
*op_info
= &action
->info
;
3096 const char *mapping
;
3098 pr_out_str(ctx
->dl
, "type",
3099 dpipe_action_type_e2s(action
->type
));
3100 pr_out_str(ctx
->dl
, "header",
3101 dpipe_header_id2s(ctx
, op_info
->header_id
,
3102 op_info
->header_global
));
3103 pr_out_str(ctx
->dl
, "field",
3104 dpipe_field_id2s(ctx
, op_info
->header_id
,
3106 op_info
->header_global
));
3107 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3109 op_info
->header_global
);
3111 pr_out_str(ctx
->dl
, "mapping", mapping
);
3114 static int dpipe_action_parse(struct dpipe_action
*action
, struct nlattr
*nl
)
3116 struct nlattr
*nla_action
[DEVLINK_ATTR_MAX
+ 1] = {};
3119 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action
);
3120 if (err
!= MNL_CB_OK
)
3123 if (!nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
] ||
3124 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3125 !nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3126 !nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3130 action
->type
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_ACTION_TYPE
]);
3131 action
->info
.header_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3132 action
->info
.field_id
= mnl_attr_get_u32(nla_action
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3133 action
->info
.header_global
= !!mnl_attr_get_u8(nla_action
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3138 static int dpipe_table_actions_show(struct dpipe_ctx
*ctx
,
3139 struct nlattr
*nla_actions
)
3141 struct nlattr
*nla_action
;
3142 struct dpipe_action action
;
3144 mnl_attr_for_each_nested(nla_action
, nla_actions
) {
3145 pr_out_entry_start(ctx
->dl
);
3146 if (dpipe_action_parse(&action
, nla_action
))
3147 goto err_action_parse
;
3148 pr_out_dpipe_action(&action
, ctx
);
3149 pr_out_entry_end(ctx
->dl
);
3154 pr_out_entry_end(ctx
->dl
);
3159 dpipe_match_type_e2s(enum devlink_dpipe_match_type match_type
)
3161 switch (match_type
) {
3162 case DEVLINK_DPIPE_MATCH_TYPE_FIELD_EXACT
:
3163 return "field_exact";
3169 struct dpipe_match
{
3170 struct dpipe_op_info info
;
3174 static void pr_out_dpipe_match(struct dpipe_match
*match
,
3175 struct dpipe_ctx
*ctx
)
3177 struct dpipe_op_info
*op_info
= &match
->info
;
3178 const char *mapping
;
3180 pr_out_str(ctx
->dl
, "type",
3181 dpipe_match_type_e2s(match
->type
));
3182 pr_out_str(ctx
->dl
, "header",
3183 dpipe_header_id2s(ctx
, op_info
->header_id
,
3184 op_info
->header_global
));
3185 pr_out_str(ctx
->dl
, "field",
3186 dpipe_field_id2s(ctx
, op_info
->header_id
,
3188 op_info
->header_global
));
3189 mapping
= dpipe_mapping_get(ctx
, op_info
->header_id
,
3191 op_info
->header_global
);
3193 pr_out_str(ctx
->dl
, "mapping", mapping
);
3196 static int dpipe_match_parse(struct dpipe_match
*match
,
3200 struct nlattr
*nla_match
[DEVLINK_ATTR_MAX
+ 1] = {};
3203 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match
);
3204 if (err
!= MNL_CB_OK
)
3207 if (!nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
] ||
3208 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_INDEX
] ||
3209 !nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
] ||
3210 !nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]) {
3214 match
->type
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_MATCH_TYPE
]);
3215 match
->info
.header_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_ID
]);
3216 match
->info
.field_id
= mnl_attr_get_u32(nla_match
[DEVLINK_ATTR_DPIPE_FIELD_ID
]);
3217 match
->info
.header_global
= !!mnl_attr_get_u8(nla_match
[DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
]);
3222 static int dpipe_table_matches_show(struct dpipe_ctx
*ctx
,
3223 struct nlattr
*nla_matches
)
3225 struct nlattr
*nla_match
;
3226 struct dpipe_match match
;
3228 mnl_attr_for_each_nested(nla_match
, nla_matches
) {
3229 pr_out_entry_start(ctx
->dl
);
3230 if (dpipe_match_parse(&match
, nla_match
))
3231 goto err_match_parse
;
3232 pr_out_dpipe_match(&match
, ctx
);
3233 pr_out_entry_end(ctx
->dl
);
3238 pr_out_entry_end(ctx
->dl
);
3242 static int dpipe_table_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3244 struct nlattr
*nla_table
[DEVLINK_ATTR_MAX
+ 1] = {};
3245 bool counters_enabled
;
3250 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_table
);
3251 if (err
!= MNL_CB_OK
)
3254 if (!nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
3255 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
] ||
3256 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
] ||
3257 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
] ||
3258 !nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]) {
3262 name
= mnl_attr_get_str(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
3263 size
= mnl_attr_get_u32(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_SIZE
]);
3264 counters_enabled
= !!mnl_attr_get_u8(nla_table
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
3266 pr_out_str(ctx
->dl
, "name", name
);
3267 pr_out_uint(ctx
->dl
, "size", size
);
3268 pr_out_str(ctx
->dl
, "counters_enabled",
3269 counters_enabled
? "true" : "false");
3271 pr_out_array_start(ctx
->dl
, "match");
3272 if (dpipe_table_matches_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_MATCHES
]))
3273 goto err_matches_show
;
3274 pr_out_array_end(ctx
->dl
);
3276 pr_out_array_start(ctx
->dl
, "action");
3277 if (dpipe_table_actions_show(ctx
, nla_table
[DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
]))
3278 goto err_actions_show
;
3279 pr_out_array_end(ctx
->dl
);
3285 pr_out_array_end(ctx
->dl
);
3289 static int dpipe_tables_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3291 struct nlattr
*nla_tables
= tb
[DEVLINK_ATTR_DPIPE_TABLES
];
3292 struct nlattr
*nla_table
;
3294 mnl_attr_for_each_nested(nla_table
, nla_tables
) {
3295 pr_out_handle_start_arr(ctx
->dl
, tb
);
3296 if (dpipe_table_show(ctx
, nla_table
))
3297 goto err_table_show
;
3298 pr_out_handle_end(ctx
->dl
);
3303 pr_out_handle_end(ctx
->dl
);
3307 static int cmd_dpipe_table_show_cb(const struct nlmsghdr
*nlh
, void *data
)
3309 struct dpipe_ctx
*ctx
= data
;
3310 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3311 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3313 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3314 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3315 !tb
[DEVLINK_ATTR_DPIPE_TABLES
])
3316 return MNL_CB_ERROR
;
3318 if (dpipe_tables_show(ctx
, tb
))
3319 return MNL_CB_ERROR
;
3323 static int cmd_dpipe_table_show(struct dl
*dl
)
3325 struct nlmsghdr
*nlh
;
3326 struct dpipe_ctx
*ctx
;
3327 uint16_t flags
= NLM_F_REQUEST
;
3330 ctx
= dpipe_ctx_alloc(dl
);
3334 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
, DL_OPT_DPIPE_TABLE_NAME
);
3338 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3339 dl_opts_put(nlh
, dl
);
3340 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3342 pr_err("error get headers %s\n", strerror(ctx
->err
));
3346 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3347 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_GET
, flags
);
3348 dl_opts_put(nlh
, dl
);
3350 pr_out_section_start(dl
, "table");
3351 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_show_cb
, ctx
);
3352 pr_out_section_end(dl
);
3354 dpipe_ctx_clear(ctx
);
3355 dpipe_ctx_free(ctx
);
3359 static int cmd_dpipe_table_set(struct dl
*dl
)
3361 struct nlmsghdr
*nlh
;
3364 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
3365 NLM_F_REQUEST
| NLM_F_ACK
);
3367 err
= dl_argv_parse_put(nlh
, dl
,
3368 DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
|
3369 DL_OPT_DPIPE_TABLE_COUNTERS
, 0);
3373 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
3376 enum dpipe_value_type
{
3377 DPIPE_VALUE_TYPE_VALUE
,
3378 DPIPE_VALUE_TYPE_MASK
,
3382 dpipe_value_type_e2s(enum dpipe_value_type type
)
3385 case DPIPE_VALUE_TYPE_VALUE
:
3387 case DPIPE_VALUE_TYPE_MASK
:
3388 return "value_mask";
3394 struct dpipe_field_printer
{
3395 unsigned int field_id
;
3396 void (*printer
)(struct dpipe_ctx
*, enum dpipe_value_type
, void *);
3399 struct dpipe_header_printer
{
3400 struct dpipe_field_printer
*printers
;
3401 unsigned int printers_count
;
3402 unsigned int header_id
;
3405 static void dpipe_field_printer_ipv4_addr(struct dpipe_ctx
*ctx
,
3406 enum dpipe_value_type type
,
3409 struct in_addr ip_addr
;
3411 ip_addr
.s_addr
= htonl(*(uint32_t *)value
);
3412 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), inet_ntoa(ip_addr
));
3416 dpipe_field_printer_ethernet_addr(struct dpipe_ctx
*ctx
,
3417 enum dpipe_value_type type
,
3420 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
),
3421 ether_ntoa((struct ether_addr
*)value
));
3424 static void dpipe_field_printer_ipv6_addr(struct dpipe_ctx
*ctx
,
3425 enum dpipe_value_type type
,
3428 char str
[INET6_ADDRSTRLEN
];
3430 inet_ntop(AF_INET6
, value
, str
, INET6_ADDRSTRLEN
);
3431 pr_out_str(ctx
->dl
, dpipe_value_type_e2s(type
), str
);
3434 static struct dpipe_field_printer dpipe_field_printers_ipv4
[] = {
3436 .printer
= dpipe_field_printer_ipv4_addr
,
3437 .field_id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
3441 static struct dpipe_header_printer dpipe_header_printer_ipv4
= {
3442 .printers
= dpipe_field_printers_ipv4
,
3443 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv4
),
3444 .header_id
= DEVLINK_DPIPE_HEADER_IPV4
,
3447 static struct dpipe_field_printer dpipe_field_printers_ethernet
[] = {
3449 .printer
= dpipe_field_printer_ethernet_addr
,
3450 .field_id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
3454 static struct dpipe_header_printer dpipe_header_printer_ethernet
= {
3455 .printers
= dpipe_field_printers_ethernet
,
3456 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ethernet
),
3457 .header_id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
3460 static struct dpipe_field_printer dpipe_field_printers_ipv6
[] = {
3462 .printer
= dpipe_field_printer_ipv6_addr
,
3463 .field_id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
3467 static struct dpipe_header_printer dpipe_header_printer_ipv6
= {
3468 .printers
= dpipe_field_printers_ipv6
,
3469 .printers_count
= ARRAY_SIZE(dpipe_field_printers_ipv6
),
3470 .header_id
= DEVLINK_DPIPE_HEADER_IPV6
,
3473 static struct dpipe_header_printer
*dpipe_header_printers
[] = {
3474 &dpipe_header_printer_ipv4
,
3475 &dpipe_header_printer_ethernet
,
3476 &dpipe_header_printer_ipv6
,
3479 static int dpipe_print_prot_header(struct dpipe_ctx
*ctx
,
3480 struct dpipe_op_info
*info
,
3481 enum dpipe_value_type type
,
3484 unsigned int header_printers_count
= ARRAY_SIZE(dpipe_header_printers
);
3485 struct dpipe_header_printer
*header_printer
;
3486 struct dpipe_field_printer
*field_printer
;
3487 unsigned int field_printers_count
;
3491 for (i
= 0; i
< header_printers_count
; i
++) {
3492 header_printer
= dpipe_header_printers
[i
];
3493 if (header_printer
->header_id
!= info
->header_id
)
3495 field_printers_count
= header_printer
->printers_count
;
3496 for (j
= 0; j
< field_printers_count
; j
++) {
3497 field_printer
= &header_printer
->printers
[j
];
3498 if (field_printer
->field_id
!= info
->field_id
)
3500 field_printer
->printer(ctx
, type
, value
);
3508 static void __pr_out_entry_value(struct dpipe_ctx
*ctx
,
3510 unsigned int value_len
,
3511 struct dpipe_op_info
*info
,
3512 enum dpipe_value_type type
)
3514 if (info
->header_global
&&
3515 !dpipe_print_prot_header(ctx
, info
, type
, value
))
3518 if (value_len
== sizeof(uint32_t)) {
3519 uint32_t *value_32
= value
;
3521 pr_out_uint(ctx
->dl
, dpipe_value_type_e2s(type
), *value_32
);
3525 static void pr_out_dpipe_entry_value(struct dpipe_ctx
*ctx
,
3526 struct nlattr
**nla_match_value
,
3527 struct dpipe_op_info
*info
)
3529 void *value
, *value_mask
;
3530 uint32_t value_mapping
;
3534 mask
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MASK
];
3535 mapping
= !!nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
];
3537 value_len
= mnl_attr_get_payload_len(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3538 value
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3541 value_mapping
= mnl_attr_get_u32(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE_MAPPING
]);
3542 pr_out_uint(ctx
->dl
, "mapping_value", value_mapping
);
3546 value_mask
= mnl_attr_get_payload(nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]);
3547 __pr_out_entry_value(ctx
, value_mask
, value_len
, info
,
3548 DPIPE_VALUE_TYPE_MASK
);
3551 __pr_out_entry_value(ctx
, value
, value_len
, info
, DPIPE_VALUE_TYPE_VALUE
);
3554 static int dpipe_entry_match_value_show(struct dpipe_ctx
*ctx
,
3557 struct nlattr
*nla_match_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3558 struct dpipe_match match
;
3561 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_match_value
);
3562 if (err
!= MNL_CB_OK
)
3565 if (!nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
] ||
3566 !nla_match_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3570 pr_out_entry_start(ctx
->dl
);
3571 if (dpipe_match_parse(&match
,
3572 nla_match_value
[DEVLINK_ATTR_DPIPE_MATCH
]))
3573 goto err_match_parse
;
3574 pr_out_dpipe_match(&match
, ctx
);
3575 pr_out_dpipe_entry_value(ctx
, nla_match_value
, &match
.info
);
3576 pr_out_entry_end(ctx
->dl
);
3581 pr_out_entry_end(ctx
->dl
);
3585 static int dpipe_entry_action_value_show(struct dpipe_ctx
*ctx
,
3588 struct nlattr
*nla_action_value
[DEVLINK_ATTR_MAX
+ 1] = {};
3589 struct dpipe_action action
;
3592 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_action_value
);
3593 if (err
!= MNL_CB_OK
)
3596 if (!nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
] ||
3597 !nla_action_value
[DEVLINK_ATTR_DPIPE_VALUE
]) {
3601 pr_out_entry_start(ctx
->dl
);
3602 if (dpipe_action_parse(&action
,
3603 nla_action_value
[DEVLINK_ATTR_DPIPE_ACTION
]))
3604 goto err_action_parse
;
3605 pr_out_dpipe_action(&action
, ctx
);
3606 pr_out_dpipe_entry_value(ctx
, nla_action_value
, &action
.info
);
3607 pr_out_entry_end(ctx
->dl
);
3612 pr_out_entry_end(ctx
->dl
);
3617 dpipe_tables_action_values_show(struct dpipe_ctx
*ctx
,
3618 struct nlattr
*nla_action_values
)
3620 struct nlattr
*nla_action_value
;
3622 mnl_attr_for_each_nested(nla_action_value
, nla_action_values
) {
3623 if (dpipe_entry_action_value_show(ctx
, nla_action_value
))
3630 dpipe_tables_match_values_show(struct dpipe_ctx
*ctx
,
3631 struct nlattr
*nla_match_values
)
3633 struct nlattr
*nla_match_value
;
3635 mnl_attr_for_each_nested(nla_match_value
, nla_match_values
) {
3636 if (dpipe_entry_match_value_show(ctx
, nla_match_value
))
3642 static int dpipe_entry_show(struct dpipe_ctx
*ctx
, struct nlattr
*nl
)
3644 struct nlattr
*nla_entry
[DEVLINK_ATTR_MAX
+ 1] = {};
3645 uint32_t entry_index
;
3649 err
= mnl_attr_parse_nested(nl
, attr_cb
, nla_entry
);
3650 if (err
!= MNL_CB_OK
)
3653 if (!nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
] ||
3654 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
] ||
3655 !nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]) {
3659 entry_index
= mnl_attr_get_u32(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_INDEX
]);
3660 pr_out_uint(ctx
->dl
, "index", entry_index
);
3662 if (nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]) {
3663 counter
= mnl_attr_get_u64(nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
]);
3664 pr_out_uint(ctx
->dl
, "counter", counter
);
3667 pr_out_array_start(ctx
->dl
, "match_value");
3668 if (dpipe_tables_match_values_show(ctx
,
3669 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
]))
3670 goto err_match_values_show
;
3671 pr_out_array_end(ctx
->dl
);
3673 pr_out_array_start(ctx
->dl
, "action_value");
3674 if (dpipe_tables_action_values_show(ctx
,
3675 nla_entry
[DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
]))
3676 goto err_action_values_show
;
3677 pr_out_array_end(ctx
->dl
);
3680 err_action_values_show
:
3681 err_match_values_show
:
3682 pr_out_array_end(ctx
->dl
);
3686 static int dpipe_table_entries_show(struct dpipe_ctx
*ctx
, struct nlattr
**tb
)
3688 struct nlattr
*nla_entries
= tb
[DEVLINK_ATTR_DPIPE_ENTRIES
];
3689 struct nlattr
*nla_entry
;
3691 mnl_attr_for_each_nested(nla_entry
, nla_entries
) {
3692 pr_out_handle_start_arr(ctx
->dl
, tb
);
3693 if (dpipe_entry_show(ctx
, nla_entry
))
3694 goto err_entry_show
;
3695 pr_out_handle_end(ctx
->dl
);
3700 pr_out_handle_end(ctx
->dl
);
3704 static int cmd_dpipe_table_entry_dump_cb(const struct nlmsghdr
*nlh
, void *data
)
3706 struct dpipe_ctx
*ctx
= data
;
3707 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
3708 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
3710 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
3711 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
3712 !tb
[DEVLINK_ATTR_DPIPE_ENTRIES
])
3713 return MNL_CB_ERROR
;
3715 if (dpipe_table_entries_show(ctx
, tb
))
3716 return MNL_CB_ERROR
;
3720 static int cmd_dpipe_table_dump(struct dl
*dl
)
3722 struct nlmsghdr
*nlh
;
3723 struct dpipe_ctx
*ctx
;
3724 uint16_t flags
= NLM_F_REQUEST
;
3727 ctx
= dpipe_ctx_alloc(dl
);
3731 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_DPIPE_TABLE_NAME
, 0);
3735 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_HEADERS_GET
, flags
);
3736 dl_opts_put(nlh
, dl
);
3737 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_header_cb
, ctx
);
3739 pr_err("error get headers %s\n", strerror(ctx
->err
));
3743 flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3744 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_DPIPE_ENTRIES_GET
, flags
);
3745 dl_opts_put(nlh
, dl
);
3747 pr_out_section_start(dl
, "table_entry");
3748 _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dpipe_table_entry_dump_cb
, ctx
);
3749 pr_out_section_end(dl
);
3751 dpipe_ctx_clear(ctx
);
3752 dpipe_ctx_free(ctx
);
3756 static void cmd_dpipe_table_help(void)
3758 pr_err("Usage: devlink dpipe table [ OBJECT-LIST ]\n"
3759 "where OBJECT-LIST := { show | set | dump }\n");
3762 static int cmd_dpipe_table(struct dl
*dl
)
3764 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3765 cmd_dpipe_table_help();
3767 } else if (dl_argv_match(dl
, "show")) {
3769 return cmd_dpipe_table_show(dl
);
3770 } else if (dl_argv_match(dl
, "set")) {
3772 return cmd_dpipe_table_set(dl
);
3773 } else if (dl_argv_match(dl
, "dump")) {
3775 return cmd_dpipe_table_dump(dl
);
3777 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3781 static void cmd_dpipe_help(void)
3783 pr_err("Usage: devlink dpipe [ OBJECT-LIST ]\n"
3784 "where OBJECT-LIST := { header | table }\n");
3787 static int cmd_dpipe(struct dl
*dl
)
3789 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3792 } else if (dl_argv_match(dl
, "header")) {
3794 return cmd_dpipe_header(dl
);
3795 } else if (dl_argv_match(dl
, "table")) {
3797 return cmd_dpipe_table(dl
);
3799 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
3803 static void help(void)
3805 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
3806 " devlink [ -f[orce] ] -b[atch] filename\n"
3807 "where OBJECT := { dev | port | sb | monitor | dpipe }\n"
3808 " OPTIONS := { -V[ersion] | -n[no-nice-names] | -j[json] | -p[pretty] | -v[verbose] }\n");
3811 static int dl_cmd(struct dl
*dl
, int argc
, char **argv
)
3816 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
3819 } else if (dl_argv_match(dl
, "dev")) {
3822 } else if (dl_argv_match(dl
, "port")) {
3824 return cmd_port(dl
);
3825 } else if (dl_argv_match(dl
, "sb")) {
3828 } else if (dl_argv_match(dl
, "monitor")) {
3831 } else if (dl_argv_match(dl
, "dpipe")) {
3833 return cmd_dpipe(dl
);
3835 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
3839 static int dl_init(struct dl
*dl
)
3843 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
3845 pr_err("Failed to connect to devlink Netlink\n");
3849 err
= ifname_map_init(dl
);
3851 pr_err("Failed to create index map\n");
3852 goto err_ifname_map_create
;
3854 if (dl
->json_output
) {
3855 dl
->jw
= jsonw_new(stdout
);
3857 pr_err("Failed to create JSON writer\n");
3860 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
3865 ifname_map_fini(dl
);
3866 err_ifname_map_create
:
3867 mnlg_socket_close(dl
->nlg
);
3871 static void dl_fini(struct dl
*dl
)
3873 if (dl
->json_output
)
3874 jsonw_destroy(&dl
->jw
);
3875 ifname_map_fini(dl
);
3876 mnlg_socket_close(dl
->nlg
);
3879 static struct dl
*dl_alloc(void)
3883 dl
= calloc(1, sizeof(*dl
));
3889 static void dl_free(struct dl
*dl
)
3894 static int dl_batch(struct dl
*dl
, const char *name
, bool force
)
3898 int ret
= EXIT_SUCCESS
;
3900 if (name
&& strcmp(name
, "-") != 0) {
3901 if (freopen(name
, "r", stdin
) == NULL
) {
3903 "Cannot open file \"%s\" for reading: %s\n",
3904 name
, strerror(errno
));
3905 return EXIT_FAILURE
;
3910 while (getcmdline(&line
, &len
, stdin
) != -1) {
3914 largc
= makeargs(line
, largv
, 100);
3916 continue; /* blank line */
3918 if (dl_cmd(dl
, largc
, largv
)) {
3919 fprintf(stderr
, "Command failed %s:%d\n",
3933 int main(int argc
, char **argv
)
3935 static const struct option long_options
[] = {
3936 { "Version", no_argument
, NULL
, 'V' },
3937 { "force", no_argument
, NULL
, 'f' },
3938 { "batch", required_argument
, NULL
, 'b' },
3939 { "no-nice-names", no_argument
, NULL
, 'n' },
3940 { "json", no_argument
, NULL
, 'j' },
3941 { "pretty", no_argument
, NULL
, 'p' },
3942 { "verbose", no_argument
, NULL
, 'v' },
3943 { NULL
, 0, NULL
, 0 }
3945 const char *batch_file
= NULL
;
3954 pr_err("Failed to allocate memory for devlink\n");
3955 return EXIT_FAILURE
;
3958 while ((opt
= getopt_long(argc
, argv
, "Vfb:njpv",
3959 long_options
, NULL
)) >= 0) {
3963 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
3970 batch_file
= optarg
;
3973 dl
->no_nice_names
= true;
3976 dl
->json_output
= true;
3979 dl
->pretty_output
= true;
3985 pr_err("Unknown option.\n");
4002 err
= dl_batch(dl
, batch_file
, force
);
4004 err
= dl_cmd(dl
, argc
, argv
);