2 * devlink.c Devlink tool
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Jiri Pirko <jiri@mellanox.com>
20 #include <linux/genetlink.h>
21 #include <linux/devlink.h>
22 #include <libmnl/libmnl.h>
27 #include "json_writer.h"
29 #define ESWITCH_MODE_LEGACY "legacy"
30 #define ESWITCH_MODE_SWITCHDEV "switchdev"
31 #define ESWITCH_INLINE_MODE_NONE "none"
32 #define ESWITCH_INLINE_MODE_LINK "link"
33 #define ESWITCH_INLINE_MODE_NETWORK "network"
34 #define ESWITCH_INLINE_MODE_TRANSPORT "transport"
36 #define pr_err(args...) fprintf(stderr, ##args)
37 #define pr_out(args...) fprintf(stdout, ##args)
38 #define pr_out_sp(num, args...) \
40 int ret = fprintf(stdout, ##args); \
42 fprintf(stdout, "%*s", num - ret, ""); \
45 static int _mnlg_socket_recv_run(struct mnlg_socket
*nlg
,
46 mnl_cb_t data_cb
, void *data
)
50 err
= mnlg_socket_recv_run(nlg
, data_cb
, data
);
52 pr_err("devlink answers: %s\n", strerror(errno
));
58 static int _mnlg_socket_sndrcv(struct mnlg_socket
*nlg
,
59 const struct nlmsghdr
*nlh
,
60 mnl_cb_t data_cb
, void *data
)
64 err
= mnlg_socket_send(nlg
, nlh
);
66 pr_err("Failed to call mnlg_socket_send\n");
69 return _mnlg_socket_recv_run(nlg
, data_cb
, data
);
72 static int _mnlg_socket_group_add(struct mnlg_socket
*nlg
,
73 const char *group_name
)
77 err
= mnlg_socket_group_add(nlg
, group_name
);
79 pr_err("Failed to call mnlg_socket_group_add\n");
86 struct list_head list
;
93 static struct ifname_map
*ifname_map_alloc(const char *bus_name
,
98 struct ifname_map
*ifname_map
;
100 ifname_map
= calloc(1, sizeof(*ifname_map
));
103 ifname_map
->bus_name
= strdup(bus_name
);
104 ifname_map
->dev_name
= strdup(dev_name
);
105 ifname_map
->port_index
= port_index
;
106 ifname_map
->ifname
= strdup(ifname
);
107 if (!ifname_map
->bus_name
|| !ifname_map
->dev_name
||
108 !ifname_map
->ifname
) {
109 free(ifname_map
->ifname
);
110 free(ifname_map
->dev_name
);
111 free(ifname_map
->bus_name
);
118 static void ifname_map_free(struct ifname_map
*ifname_map
)
120 free(ifname_map
->ifname
);
121 free(ifname_map
->dev_name
);
122 free(ifname_map
->bus_name
);
126 #define BIT(nr) (1UL << (nr))
127 #define DL_OPT_HANDLE BIT(0)
128 #define DL_OPT_HANDLEP BIT(1)
129 #define DL_OPT_PORT_TYPE BIT(2)
130 #define DL_OPT_PORT_COUNT BIT(3)
131 #define DL_OPT_SB BIT(4)
132 #define DL_OPT_SB_POOL BIT(5)
133 #define DL_OPT_SB_SIZE BIT(6)
134 #define DL_OPT_SB_TYPE BIT(7)
135 #define DL_OPT_SB_THTYPE BIT(8)
136 #define DL_OPT_SB_TH BIT(9)
137 #define DL_OPT_SB_TC BIT(10)
138 #define DL_OPT_ESWITCH_MODE BIT(11)
139 #define DL_OPT_ESWITCH_INLINE_MODE BIT(12)
142 uint32_t present
; /* flags of present items */
146 enum devlink_port_type port_type
;
149 uint16_t sb_pool_index
;
150 uint32_t sb_pool_size
;
151 enum devlink_sb_pool_type sb_pool_type
;
152 enum devlink_sb_threshold_type sb_pool_thtype
;
153 uint32_t sb_threshold
;
154 uint16_t sb_tc_index
;
155 enum devlink_eswitch_mode eswitch_mode
;
156 enum devlink_eswitch_inline_mode eswitch_inline_mode
;
160 struct mnlg_socket
*nlg
;
161 struct list_head ifname_map_list
;
177 static int dl_argc(struct dl
*dl
)
182 static char *dl_argv(struct dl
*dl
)
184 if (dl_argc(dl
) == 0)
189 static void dl_arg_inc(struct dl
*dl
)
191 if (dl_argc(dl
) == 0)
197 static char *dl_argv_next(struct dl
*dl
)
201 if (dl_argc(dl
) == 0)
209 static char *dl_argv_index(struct dl
*dl
, unsigned int index
)
211 if (index
>= dl_argc(dl
))
213 return dl
->argv
[index
];
216 static int strcmpx(const char *str1
, const char *str2
)
218 if (strlen(str1
) > strlen(str2
))
220 return strncmp(str1
, str2
, strlen(str1
));
223 static bool dl_argv_match(struct dl
*dl
, const char *pattern
)
225 if (dl_argc(dl
) == 0)
227 return strcmpx(dl_argv(dl
), pattern
) == 0;
230 static bool dl_no_arg(struct dl
*dl
)
232 return dl_argc(dl
) == 0;
235 static int attr_cb(const struct nlattr
*attr
, void *data
)
237 const struct nlattr
**tb
= data
;
240 type
= mnl_attr_get_type(attr
);
242 if (mnl_attr_type_valid(attr
, DEVLINK_ATTR_MAX
) < 0)
245 if (type
== DEVLINK_ATTR_BUS_NAME
&&
246 mnl_attr_validate(attr
, MNL_TYPE_NUL_STRING
) < 0)
248 if (type
== DEVLINK_ATTR_DEV_NAME
&&
249 mnl_attr_validate(attr
, MNL_TYPE_NUL_STRING
) < 0)
251 if (type
== DEVLINK_ATTR_PORT_INDEX
&&
252 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
254 if (type
== DEVLINK_ATTR_PORT_TYPE
&&
255 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
257 if (type
== DEVLINK_ATTR_PORT_DESIRED_TYPE
&&
258 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
260 if (type
== DEVLINK_ATTR_PORT_NETDEV_IFINDEX
&&
261 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
263 if (type
== DEVLINK_ATTR_PORT_NETDEV_NAME
&&
264 mnl_attr_validate(attr
, MNL_TYPE_NUL_STRING
) < 0)
266 if (type
== DEVLINK_ATTR_PORT_IBDEV_NAME
&&
267 mnl_attr_validate(attr
, MNL_TYPE_NUL_STRING
) < 0)
269 if (type
== DEVLINK_ATTR_SB_INDEX
&&
270 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
272 if (type
== DEVLINK_ATTR_SB_SIZE
&&
273 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
275 if (type
== DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
&&
276 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
278 if (type
== DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
&&
279 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
281 if (type
== DEVLINK_ATTR_SB_INGRESS_TC_COUNT
&&
282 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
284 if (type
== DEVLINK_ATTR_SB_EGRESS_TC_COUNT
&&
285 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
287 if (type
== DEVLINK_ATTR_SB_POOL_INDEX
&&
288 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
290 if (type
== DEVLINK_ATTR_SB_POOL_TYPE
&&
291 mnl_attr_validate(attr
, MNL_TYPE_U8
) < 0)
293 if (type
== DEVLINK_ATTR_SB_POOL_SIZE
&&
294 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
296 if (type
== DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
&&
297 mnl_attr_validate(attr
, MNL_TYPE_U8
) < 0)
299 if (type
== DEVLINK_ATTR_SB_THRESHOLD
&&
300 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
302 if (type
== DEVLINK_ATTR_SB_TC_INDEX
&&
303 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
305 if (type
== DEVLINK_ATTR_SB_OCC_CUR
&&
306 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
308 if (type
== DEVLINK_ATTR_SB_OCC_MAX
&&
309 mnl_attr_validate(attr
, MNL_TYPE_U32
) < 0)
311 if (type
== DEVLINK_ATTR_ESWITCH_MODE
&&
312 mnl_attr_validate(attr
, MNL_TYPE_U16
) < 0)
314 if (type
== DEVLINK_ATTR_ESWITCH_INLINE_MODE
&&
315 mnl_attr_validate(attr
, MNL_TYPE_U8
) < 0)
321 static int ifname_map_cb(const struct nlmsghdr
*nlh
, void *data
)
323 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
324 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
325 struct dl
*dl
= data
;
326 struct ifname_map
*ifname_map
;
327 const char *bus_name
;
328 const char *dev_name
;
329 uint32_t port_ifindex
;
330 const char *port_ifname
;
332 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
333 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
334 !tb
[DEVLINK_ATTR_PORT_INDEX
])
337 if (!tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
340 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
341 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
342 port_ifindex
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
343 port_ifname
= mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]);
344 ifname_map
= ifname_map_alloc(bus_name
, dev_name
,
345 port_ifindex
, port_ifname
);
348 list_add(&ifname_map
->list
, &dl
->ifname_map_list
);
353 static void ifname_map_fini(struct dl
*dl
)
355 struct ifname_map
*ifname_map
, *tmp
;
357 list_for_each_entry_safe(ifname_map
, tmp
,
358 &dl
->ifname_map_list
, list
) {
359 list_del(&ifname_map
->list
);
360 ifname_map_free(ifname_map
);
364 static int ifname_map_init(struct dl
*dl
)
366 struct nlmsghdr
*nlh
;
369 INIT_LIST_HEAD(&dl
->ifname_map_list
);
371 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
,
372 NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
);
374 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, ifname_map_cb
, dl
);
382 static int ifname_map_lookup(struct dl
*dl
, const char *ifname
,
383 char **p_bus_name
, char **p_dev_name
,
384 uint32_t *p_port_index
)
386 struct ifname_map
*ifname_map
;
388 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
389 if (strcmp(ifname
, ifname_map
->ifname
) == 0) {
390 *p_bus_name
= ifname_map
->bus_name
;
391 *p_dev_name
= ifname_map
->dev_name
;
392 *p_port_index
= ifname_map
->port_index
;
399 static int ifname_map_rev_lookup(struct dl
*dl
, const char *bus_name
,
400 const char *dev_name
, uint32_t port_index
,
403 struct ifname_map
*ifname_map
;
405 list_for_each_entry(ifname_map
, &dl
->ifname_map_list
, list
) {
406 if (strcmp(bus_name
, ifname_map
->bus_name
) == 0 &&
407 strcmp(dev_name
, ifname_map
->dev_name
) == 0 &&
408 port_index
== ifname_map
->port_index
) {
409 *p_ifname
= ifname_map
->ifname
;
416 static unsigned int strslashcount(char *str
)
418 unsigned int count
= 0;
421 while ((pos
= strchr(pos
, '/'))) {
428 static int strslashrsplit(char *str
, char **before
, char **after
)
432 slash
= strrchr(str
, '/');
441 static int strtouint32_t(const char *str
, uint32_t *p_val
)
444 unsigned long int val
;
446 val
= strtoul(str
, &endptr
, 10);
447 if (endptr
== str
|| *endptr
!= '\0')
455 static int strtouint16_t(const char *str
, uint16_t *p_val
)
458 unsigned long int val
;
460 val
= strtoul(str
, &endptr
, 10);
461 if (endptr
== str
|| *endptr
!= '\0')
469 static int __dl_argv_handle(char *str
, char **p_bus_name
, char **p_dev_name
)
471 strslashrsplit(str
, p_bus_name
, p_dev_name
);
475 static int dl_argv_handle(struct dl
*dl
, char **p_bus_name
, char **p_dev_name
)
477 char *str
= dl_argv_next(dl
);
480 pr_err("Devlink identification (\"bus_name/dev_name\") expected\n");
483 if (strslashcount(str
) != 1) {
484 pr_err("Wrong devlink identification string format.\n");
485 pr_err("Expected \"bus_name/dev_name\".\n");
488 return __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
491 static int __dl_argv_handle_port(char *str
,
492 char **p_bus_name
, char **p_dev_name
,
493 uint32_t *p_port_index
)
495 char *handlestr
= handlestr
;
496 char *portstr
= portstr
;
499 strslashrsplit(str
, &handlestr
, &portstr
);
500 err
= strtouint32_t(portstr
, p_port_index
);
502 pr_err("Port index \"%s\" is not a number or not within range\n",
506 strslashrsplit(handlestr
, p_bus_name
, p_dev_name
);
510 static int __dl_argv_handle_port_ifname(struct dl
*dl
, char *str
,
511 char **p_bus_name
, char **p_dev_name
,
512 uint32_t *p_port_index
)
516 err
= ifname_map_lookup(dl
, str
, p_bus_name
, p_dev_name
,
519 pr_err("Netdevice \"%s\" not found\n", str
);
525 static int dl_argv_handle_port(struct dl
*dl
, char **p_bus_name
,
526 char **p_dev_name
, uint32_t *p_port_index
)
528 char *str
= dl_argv_next(dl
);
529 unsigned int slash_count
;
532 pr_err("Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\") expected.\n");
535 slash_count
= strslashcount(str
);
536 switch (slash_count
) {
538 return __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
539 p_dev_name
, p_port_index
);
541 return __dl_argv_handle_port(str
, p_bus_name
,
542 p_dev_name
, p_port_index
);
544 pr_err("Wrong port identification string format.\n");
545 pr_err("Expected \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
550 static int dl_argv_handle_both(struct dl
*dl
, char **p_bus_name
,
551 char **p_dev_name
, uint32_t *p_port_index
,
552 uint32_t *p_handle_bit
)
554 char *str
= dl_argv_next(dl
);
555 unsigned int slash_count
;
559 pr_err("One of following identifications expected:\n"
560 "Devlink identification (\"bus_name/dev_name\")\n"
561 "Port identification (\"bus_name/dev_name/port_index\" or \"netdev ifname\")\n");
564 slash_count
= strslashcount(str
);
565 if (slash_count
== 1) {
566 err
= __dl_argv_handle(str
, p_bus_name
, p_dev_name
);
569 *p_handle_bit
= DL_OPT_HANDLE
;
570 } else if (slash_count
== 2) {
571 err
= __dl_argv_handle_port(str
, p_bus_name
,
572 p_dev_name
, p_port_index
);
575 *p_handle_bit
= DL_OPT_HANDLEP
;
576 } else if (slash_count
== 0) {
577 err
= __dl_argv_handle_port_ifname(dl
, str
, p_bus_name
,
578 p_dev_name
, p_port_index
);
581 *p_handle_bit
= DL_OPT_HANDLEP
;
583 pr_err("Wrong port identification string format.\n");
584 pr_err("Expected \"bus_name/dev_name\" or \"bus_name/dev_name/port_index\" or \"netdev_ifname\".\n");
590 static int dl_argv_uint32_t(struct dl
*dl
, uint32_t *p_val
)
592 char *str
= dl_argv_next(dl
);
596 pr_err("Unsigned number argument expected\n");
600 err
= strtouint32_t(str
, p_val
);
602 pr_err("\"%s\" is not a number or not within range\n", str
);
608 static int dl_argv_uint16_t(struct dl
*dl
, uint16_t *p_val
)
610 char *str
= dl_argv_next(dl
);
614 pr_err("Unsigned number argument expected\n");
618 err
= strtouint16_t(str
, p_val
);
620 pr_err("\"%s\" is not a number or not within range\n", str
);
626 static int dl_argv_str(struct dl
*dl
, const char **p_str
)
628 const char *str
= dl_argv_next(dl
);
631 pr_err("String parameter expected\n");
638 static int port_type_get(const char *typestr
, enum devlink_port_type
*p_type
)
640 if (strcmp(typestr
, "auto") == 0) {
641 *p_type
= DEVLINK_PORT_TYPE_AUTO
;
642 } else if (strcmp(typestr
, "eth") == 0) {
643 *p_type
= DEVLINK_PORT_TYPE_ETH
;
644 } else if (strcmp(typestr
, "ib") == 0) {
645 *p_type
= DEVLINK_PORT_TYPE_IB
;
647 pr_err("Unknown port type \"%s\"\n", typestr
);
653 static int pool_type_get(const char *typestr
, enum devlink_sb_pool_type
*p_type
)
655 if (strcmp(typestr
, "ingress") == 0) {
656 *p_type
= DEVLINK_SB_POOL_TYPE_INGRESS
;
657 } else if (strcmp(typestr
, "egress") == 0) {
658 *p_type
= DEVLINK_SB_POOL_TYPE_EGRESS
;
660 pr_err("Unknown pool type \"%s\"\n", typestr
);
666 static int threshold_type_get(const char *typestr
,
667 enum devlink_sb_threshold_type
*p_type
)
669 if (strcmp(typestr
, "static") == 0) {
670 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_STATIC
;
671 } else if (strcmp(typestr
, "dynamic") == 0) {
672 *p_type
= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
;
674 pr_err("Unknown threshold type \"%s\"\n", typestr
);
680 static int eswitch_mode_get(const char *typestr
,
681 enum devlink_eswitch_mode
*p_mode
)
683 if (strcmp(typestr
, ESWITCH_MODE_LEGACY
) == 0) {
684 *p_mode
= DEVLINK_ESWITCH_MODE_LEGACY
;
685 } else if (strcmp(typestr
, ESWITCH_MODE_SWITCHDEV
) == 0) {
686 *p_mode
= DEVLINK_ESWITCH_MODE_SWITCHDEV
;
688 pr_err("Unknown eswitch mode \"%s\"\n", typestr
);
694 static int eswitch_inline_mode_get(const char *typestr
,
695 enum devlink_eswitch_inline_mode
*p_mode
)
697 if (strcmp(typestr
, ESWITCH_INLINE_MODE_NONE
) == 0) {
698 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NONE
;
699 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_LINK
) == 0) {
700 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_LINK
;
701 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_NETWORK
) == 0) {
702 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_NETWORK
;
703 } else if (strcmp(typestr
, ESWITCH_INLINE_MODE_TRANSPORT
) == 0) {
704 *p_mode
= DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
;
706 pr_err("Unknown eswitch inline mode \"%s\"\n", typestr
);
712 static int dl_argv_parse(struct dl
*dl
, uint32_t o_required
,
715 struct dl_opts
*opts
= &dl
->opts
;
716 uint32_t o_all
= o_required
| o_optional
;
717 uint32_t o_found
= 0;
720 if (o_required
& DL_OPT_HANDLE
&& o_required
& DL_OPT_HANDLEP
) {
721 uint32_t handle_bit
= handle_bit
;
723 err
= dl_argv_handle_both(dl
, &opts
->bus_name
, &opts
->dev_name
,
724 &opts
->port_index
, &handle_bit
);
727 o_found
|= handle_bit
;
728 } else if (o_required
& DL_OPT_HANDLE
) {
729 err
= dl_argv_handle(dl
, &opts
->bus_name
, &opts
->dev_name
);
732 o_found
|= DL_OPT_HANDLE
;
733 } else if (o_required
& DL_OPT_HANDLEP
) {
734 err
= dl_argv_handle_port(dl
, &opts
->bus_name
, &opts
->dev_name
,
738 o_found
|= DL_OPT_HANDLEP
;
741 while (dl_argc(dl
)) {
742 if (dl_argv_match(dl
, "type") &&
743 (o_all
& DL_OPT_PORT_TYPE
)) {
747 err
= dl_argv_str(dl
, &typestr
);
750 err
= port_type_get(typestr
, &opts
->port_type
);
753 o_found
|= DL_OPT_PORT_TYPE
;
754 } else if (dl_argv_match(dl
, "count") &&
755 (o_all
& DL_OPT_PORT_COUNT
)) {
757 err
= dl_argv_uint32_t(dl
, &opts
->port_count
);
760 o_found
|= DL_OPT_PORT_COUNT
;
761 } else if (dl_argv_match(dl
, "sb") &&
762 (o_all
& DL_OPT_SB
)) {
764 err
= dl_argv_uint32_t(dl
, &opts
->sb_index
);
767 o_found
|= DL_OPT_SB
;
768 } else if (dl_argv_match(dl
, "pool") &&
769 (o_all
& DL_OPT_SB_POOL
)) {
771 err
= dl_argv_uint16_t(dl
, &opts
->sb_pool_index
);
774 o_found
|= DL_OPT_SB_POOL
;
775 } else if (dl_argv_match(dl
, "size") &&
776 (o_all
& DL_OPT_SB_SIZE
)) {
778 err
= dl_argv_uint32_t(dl
, &opts
->sb_pool_size
);
781 o_found
|= DL_OPT_SB_SIZE
;
782 } else if (dl_argv_match(dl
, "type") &&
783 (o_all
& DL_OPT_SB_TYPE
)) {
787 err
= dl_argv_str(dl
, &typestr
);
790 err
= pool_type_get(typestr
, &opts
->sb_pool_type
);
793 o_found
|= DL_OPT_SB_TYPE
;
794 } else if (dl_argv_match(dl
, "thtype") &&
795 (o_all
& DL_OPT_SB_THTYPE
)) {
799 err
= dl_argv_str(dl
, &typestr
);
802 err
= threshold_type_get(typestr
,
803 &opts
->sb_pool_thtype
);
806 o_found
|= DL_OPT_SB_THTYPE
;
807 } else if (dl_argv_match(dl
, "th") &&
808 (o_all
& DL_OPT_SB_TH
)) {
810 err
= dl_argv_uint32_t(dl
, &opts
->sb_threshold
);
813 o_found
|= DL_OPT_SB_TH
;
814 } else if (dl_argv_match(dl
, "tc") &&
815 (o_all
& DL_OPT_SB_TC
)) {
817 err
= dl_argv_uint16_t(dl
, &opts
->sb_tc_index
);
820 o_found
|= DL_OPT_SB_TC
;
821 } else if (dl_argv_match(dl
, "mode") &&
822 (o_all
& DL_OPT_ESWITCH_MODE
)) {
826 err
= dl_argv_str(dl
, &typestr
);
829 err
= eswitch_mode_get(typestr
, &opts
->eswitch_mode
);
832 o_found
|= DL_OPT_ESWITCH_MODE
;
833 } else if (dl_argv_match(dl
, "inline-mode") &&
834 (o_all
& DL_OPT_ESWITCH_INLINE_MODE
)) {
838 err
= dl_argv_str(dl
, &typestr
);
841 err
= eswitch_inline_mode_get(
842 typestr
, &opts
->eswitch_inline_mode
);
845 o_found
|= DL_OPT_ESWITCH_INLINE_MODE
;
847 pr_err("Unknown option \"%s\"\n", dl_argv(dl
));
852 opts
->present
= o_found
;
854 if ((o_optional
& DL_OPT_SB
) && !(o_found
& DL_OPT_SB
)) {
856 opts
->present
|= DL_OPT_SB
;
859 if ((o_required
& DL_OPT_PORT_TYPE
) && !(o_found
& DL_OPT_PORT_TYPE
)) {
860 pr_err("Port type option expected.\n");
864 if ((o_required
& DL_OPT_PORT_COUNT
) &&
865 !(o_found
& DL_OPT_PORT_COUNT
)) {
866 pr_err("Port split count option expected.\n");
870 if ((o_required
& DL_OPT_SB_POOL
) && !(o_found
& DL_OPT_SB_POOL
)) {
871 pr_err("Pool index option expected.\n");
875 if ((o_required
& DL_OPT_SB_SIZE
) && !(o_found
& DL_OPT_SB_SIZE
)) {
876 pr_err("Pool size option expected.\n");
880 if ((o_required
& DL_OPT_SB_TYPE
) && !(o_found
& DL_OPT_SB_TYPE
)) {
881 pr_err("Pool type option expected.\n");
885 if ((o_required
& DL_OPT_SB_THTYPE
) && !(o_found
& DL_OPT_SB_THTYPE
)) {
886 pr_err("Pool threshold type option expected.\n");
890 if ((o_required
& DL_OPT_SB_TH
) && !(o_found
& DL_OPT_SB_TH
)) {
891 pr_err("Threshold option expected.\n");
895 if ((o_required
& DL_OPT_SB_TC
) && !(o_found
& DL_OPT_SB_TC
)) {
896 pr_err("TC index option expected.\n");
900 if ((o_required
& DL_OPT_ESWITCH_MODE
) &&
901 !(o_found
& DL_OPT_ESWITCH_MODE
)) {
902 pr_err("E-Switch mode option expected.\n");
906 if ((o_required
& DL_OPT_ESWITCH_INLINE_MODE
) &&
907 !(o_found
& DL_OPT_ESWITCH_INLINE_MODE
)) {
908 pr_err("E-Switch inline-mode option expected.\n");
915 static void dl_opts_put(struct nlmsghdr
*nlh
, struct dl
*dl
)
917 struct dl_opts
*opts
= &dl
->opts
;
919 if (opts
->present
& DL_OPT_HANDLE
) {
920 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
921 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
922 } else if (opts
->present
& DL_OPT_HANDLEP
) {
923 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_BUS_NAME
, opts
->bus_name
);
924 mnl_attr_put_strz(nlh
, DEVLINK_ATTR_DEV_NAME
, opts
->dev_name
);
925 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_INDEX
,
928 if (opts
->present
& DL_OPT_PORT_TYPE
)
929 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_PORT_TYPE
,
931 if (opts
->present
& DL_OPT_PORT_COUNT
)
932 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_PORT_SPLIT_COUNT
,
934 if (opts
->present
& DL_OPT_SB
)
935 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_INDEX
,
937 if (opts
->present
& DL_OPT_SB_POOL
)
938 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_POOL_INDEX
,
939 opts
->sb_pool_index
);
940 if (opts
->present
& DL_OPT_SB_SIZE
)
941 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_POOL_SIZE
,
943 if (opts
->present
& DL_OPT_SB_TYPE
)
944 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_TYPE
,
946 if (opts
->present
& DL_OPT_SB_THTYPE
)
947 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
948 opts
->sb_pool_thtype
);
949 if (opts
->present
& DL_OPT_SB_TH
)
950 mnl_attr_put_u32(nlh
, DEVLINK_ATTR_SB_THRESHOLD
,
952 if (opts
->present
& DL_OPT_SB_TC
)
953 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_SB_TC_INDEX
,
955 if (opts
->present
& DL_OPT_ESWITCH_MODE
)
956 mnl_attr_put_u16(nlh
, DEVLINK_ATTR_ESWITCH_MODE
,
958 if (opts
->present
& DL_OPT_ESWITCH_INLINE_MODE
)
959 mnl_attr_put_u8(nlh
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
960 opts
->eswitch_inline_mode
);
963 static int dl_argv_parse_put(struct nlmsghdr
*nlh
, struct dl
*dl
,
964 uint32_t o_required
, uint32_t o_optional
)
968 err
= dl_argv_parse(dl
, o_required
, o_optional
);
971 dl_opts_put(nlh
, dl
);
975 static bool dl_dump_filter(struct dl
*dl
, struct nlattr
**tb
)
977 struct dl_opts
*opts
= &dl
->opts
;
978 struct nlattr
*attr_bus_name
= tb
[DEVLINK_ATTR_BUS_NAME
];
979 struct nlattr
*attr_dev_name
= tb
[DEVLINK_ATTR_DEV_NAME
];
980 struct nlattr
*attr_port_index
= tb
[DEVLINK_ATTR_PORT_INDEX
];
981 struct nlattr
*attr_sb_index
= tb
[DEVLINK_ATTR_SB_INDEX
];
983 if (opts
->present
& DL_OPT_HANDLE
&&
984 attr_bus_name
&& attr_dev_name
) {
985 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
986 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
988 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
989 strcmp(dev_name
, opts
->dev_name
) != 0)
992 if (opts
->present
& DL_OPT_HANDLEP
&&
993 attr_bus_name
&& attr_dev_name
&& attr_port_index
) {
994 const char *bus_name
= mnl_attr_get_str(attr_bus_name
);
995 const char *dev_name
= mnl_attr_get_str(attr_dev_name
);
996 uint32_t port_index
= mnl_attr_get_u32(attr_port_index
);
998 if (strcmp(bus_name
, opts
->bus_name
) != 0 ||
999 strcmp(dev_name
, opts
->dev_name
) != 0 ||
1000 port_index
!= opts
->port_index
)
1003 if (opts
->present
& DL_OPT_SB
&& attr_sb_index
) {
1004 uint32_t sb_index
= mnl_attr_get_u32(attr_sb_index
);
1006 if (sb_index
!= opts
->sb_index
)
1012 static void cmd_dev_help(void)
1014 pr_err("Usage: devlink dev show [ DEV ]\n");
1015 pr_err(" devlink dev eswitch set DEV [ mode { legacy | switchdev } ]\n");
1016 pr_err(" [ inline-mode { none | link | network | transport } ]\n");
1017 pr_err(" devlink dev eswitch show DEV\n");
1020 static bool cmp_arr_last_handle(struct dl
*dl
, const char *bus_name
,
1021 const char *dev_name
)
1023 if (!dl
->arr_last
.present
)
1025 return strcmp(dl
->arr_last
.bus_name
, bus_name
) == 0 &&
1026 strcmp(dl
->arr_last
.dev_name
, dev_name
) == 0;
1029 static void arr_last_handle_set(struct dl
*dl
, const char *bus_name
,
1030 const char *dev_name
)
1032 dl
->arr_last
.present
= true;
1033 free(dl
->arr_last
.dev_name
);
1034 free(dl
->arr_last
.bus_name
);
1035 dl
->arr_last
.bus_name
= strdup(bus_name
);
1036 dl
->arr_last
.dev_name
= strdup(dev_name
);
1039 static bool should_arr_last_handle_start(struct dl
*dl
, const char *bus_name
,
1040 const char *dev_name
)
1042 return !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1045 static bool should_arr_last_handle_end(struct dl
*dl
, const char *bus_name
,
1046 const char *dev_name
)
1048 return dl
->arr_last
.present
&&
1049 !cmp_arr_last_handle(dl
, bus_name
, dev_name
);
1052 static void __pr_out_handle_start(struct dl
*dl
, struct nlattr
**tb
,
1053 bool content
, bool array
)
1055 const char *bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1056 const char *dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1059 sprintf(buf
, "%s/%s", bus_name
, dev_name
);
1061 if (dl
->json_output
) {
1063 if (should_arr_last_handle_end(dl
, bus_name
, dev_name
))
1064 jsonw_end_array(dl
->jw
);
1065 if (should_arr_last_handle_start(dl
, bus_name
,
1067 jsonw_name(dl
->jw
, buf
);
1068 jsonw_start_array(dl
->jw
);
1069 jsonw_start_object(dl
->jw
);
1070 arr_last_handle_set(dl
, bus_name
, dev_name
);
1072 jsonw_start_object(dl
->jw
);
1075 jsonw_name(dl
->jw
, buf
);
1076 jsonw_start_object(dl
->jw
);
1079 pr_out("%s%s", buf
, content
? ":" : "");
1083 static void pr_out_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
)
1085 __pr_out_handle_start(dl
, tb
, true, true);
1088 static void pr_out_handle_end(struct dl
*dl
)
1090 if (dl
->json_output
)
1091 jsonw_end_object(dl
->jw
);
1096 static void pr_out_handle(struct dl
*dl
, struct nlattr
**tb
)
1098 __pr_out_handle_start(dl
, tb
, false, false);
1099 pr_out_handle_end(dl
);
1102 static bool cmp_arr_last_port_handle(struct dl
*dl
, const char *bus_name
,
1103 const char *dev_name
, uint32_t port_index
)
1105 return cmp_arr_last_handle(dl
, bus_name
, dev_name
) &&
1106 dl
->arr_last
.port_index
== port_index
;
1109 static void arr_last_port_handle_set(struct dl
*dl
, const char *bus_name
,
1110 const char *dev_name
, uint32_t port_index
)
1112 arr_last_handle_set(dl
, bus_name
, dev_name
);
1113 dl
->arr_last
.port_index
= port_index
;
1116 static bool should_arr_last_port_handle_start(struct dl
*dl
,
1117 const char *bus_name
,
1118 const char *dev_name
,
1119 uint32_t port_index
)
1121 return !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1124 static bool should_arr_last_port_handle_end(struct dl
*dl
,
1125 const char *bus_name
,
1126 const char *dev_name
,
1127 uint32_t port_index
)
1129 return dl
->arr_last
.present
&&
1130 !cmp_arr_last_port_handle(dl
, bus_name
, dev_name
, port_index
);
1133 static void __pr_out_port_handle_start(struct dl
*dl
, const char *bus_name
,
1134 const char *dev_name
,
1135 uint32_t port_index
, bool try_nice
,
1138 static char buf
[32];
1139 char *ifname
= NULL
;
1141 if (dl
->no_nice_names
|| !try_nice
||
1142 ifname_map_rev_lookup(dl
, bus_name
, dev_name
,
1143 port_index
, &ifname
) != 0)
1144 sprintf(buf
, "%s/%s/%d", bus_name
, dev_name
, port_index
);
1146 sprintf(buf
, "%s", ifname
);
1148 if (dl
->json_output
) {
1150 if (should_arr_last_port_handle_end(dl
, bus_name
,
1153 jsonw_end_array(dl
->jw
);
1154 if (should_arr_last_port_handle_start(dl
, bus_name
,
1157 jsonw_name(dl
->jw
, buf
);
1158 jsonw_start_array(dl
->jw
);
1159 jsonw_start_object(dl
->jw
);
1160 arr_last_port_handle_set(dl
, bus_name
, dev_name
,
1163 jsonw_start_object(dl
->jw
);
1166 jsonw_name(dl
->jw
, buf
);
1167 jsonw_start_object(dl
->jw
);
1174 static void pr_out_port_handle_start(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1176 const char *bus_name
;
1177 const char *dev_name
;
1178 uint32_t port_index
;
1180 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1181 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1182 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1183 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, false);
1186 static void pr_out_port_handle_start_arr(struct dl
*dl
, struct nlattr
**tb
, bool try_nice
)
1188 const char *bus_name
;
1189 const char *dev_name
;
1190 uint32_t port_index
;
1192 bus_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_BUS_NAME
]);
1193 dev_name
= mnl_attr_get_str(tb
[DEVLINK_ATTR_DEV_NAME
]);
1194 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
1195 __pr_out_port_handle_start(dl
, bus_name
, dev_name
, port_index
, try_nice
, true);
1198 static void pr_out_port_handle_end(struct dl
*dl
)
1200 if (dl
->json_output
)
1201 jsonw_end_object(dl
->jw
);
1207 static void pr_out_str(struct dl
*dl
, const char *name
, const char *val
)
1209 if (dl
->json_output
)
1210 jsonw_string_field(dl
->jw
, name
, val
);
1212 pr_out(" %s %s", name
, val
);
1215 static void pr_out_uint(struct dl
*dl
, const char *name
, unsigned int val
)
1217 if (dl
->json_output
)
1218 jsonw_uint_field(dl
->jw
, name
, val
);
1220 pr_out(" %s %u", name
, val
);
1223 static void pr_out_dev(struct dl
*dl
, struct nlattr
**tb
)
1225 pr_out_handle(dl
, tb
);
1228 static void pr_out_section_start(struct dl
*dl
, const char *name
)
1230 if (dl
->json_output
) {
1231 jsonw_start_object(dl
->jw
);
1232 jsonw_name(dl
->jw
, name
);
1233 jsonw_start_object(dl
->jw
);
1237 static void pr_out_section_end(struct dl
*dl
)
1239 if (dl
->json_output
) {
1240 if (dl
->arr_last
.present
)
1241 jsonw_end_array(dl
->jw
);
1242 jsonw_end_object(dl
->jw
);
1243 jsonw_end_object(dl
->jw
);
1247 static const char *eswitch_mode_name(uint32_t mode
)
1250 case DEVLINK_ESWITCH_MODE_LEGACY
: return ESWITCH_MODE_LEGACY
;
1251 case DEVLINK_ESWITCH_MODE_SWITCHDEV
: return ESWITCH_MODE_SWITCHDEV
;
1252 default: return "<unknown mode>";
1256 static const char *eswitch_inline_mode_name(uint32_t mode
)
1259 case DEVLINK_ESWITCH_INLINE_MODE_NONE
:
1260 return ESWITCH_INLINE_MODE_NONE
;
1261 case DEVLINK_ESWITCH_INLINE_MODE_LINK
:
1262 return ESWITCH_INLINE_MODE_LINK
;
1263 case DEVLINK_ESWITCH_INLINE_MODE_NETWORK
:
1264 return ESWITCH_INLINE_MODE_NETWORK
;
1265 case DEVLINK_ESWITCH_INLINE_MODE_TRANSPORT
:
1266 return ESWITCH_INLINE_MODE_TRANSPORT
;
1268 return "<unknown mode>";
1272 static void pr_out_eswitch(struct dl
*dl
, struct nlattr
**tb
)
1274 __pr_out_handle_start(dl
, tb
, true, false);
1276 if (tb
[DEVLINK_ATTR_ESWITCH_MODE
])
1277 pr_out_str(dl
, "mode",
1278 eswitch_mode_name(mnl_attr_get_u16(tb
[DEVLINK_ATTR_ESWITCH_MODE
])));
1280 if (tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])
1281 pr_out_str(dl
, "inline-mode",
1282 eswitch_inline_mode_name(mnl_attr_get_u8(
1283 tb
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
])));
1285 pr_out_handle_end(dl
);
1288 static int cmd_dev_eswitch_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1290 struct dl
*dl
= data
;
1291 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1292 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1294 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1295 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1296 return MNL_CB_ERROR
;
1297 pr_out_eswitch(dl
, tb
);
1301 static int cmd_dev_eswitch_show(struct dl
*dl
)
1303 struct nlmsghdr
*nlh
;
1306 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_MODE_GET
,
1307 NLM_F_REQUEST
| NLM_F_ACK
);
1309 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1313 pr_out_section_start(dl
, "dev");
1314 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_eswitch_show_cb
, dl
);
1315 pr_out_section_end(dl
);
1319 static int cmd_dev_eswitch_set(struct dl
*dl
)
1321 struct nlmsghdr
*nlh
;
1324 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_ESWITCH_MODE_SET
,
1325 NLM_F_REQUEST
| NLM_F_ACK
);
1327 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
,
1328 DL_OPT_ESWITCH_MODE
|
1329 DL_OPT_ESWITCH_INLINE_MODE
);
1334 if (dl
->opts
.present
== 1) {
1335 pr_err("Need to set at least one option\n");
1339 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1342 static int cmd_dev_eswitch(struct dl
*dl
)
1344 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1347 } else if (dl_argv_match(dl
, "set")) {
1349 return cmd_dev_eswitch_set(dl
);
1350 } else if (dl_argv_match(dl
, "show")) {
1352 return cmd_dev_eswitch_show(dl
);
1354 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1358 static int cmd_dev_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1360 struct dl
*dl
= data
;
1361 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1362 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1364 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1365 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
1366 return MNL_CB_ERROR
;
1371 static int cmd_dev_show(struct dl
*dl
)
1373 struct nlmsghdr
*nlh
;
1374 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1377 if (dl_argc(dl
) == 0)
1378 flags
|= NLM_F_DUMP
;
1380 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_GET
, flags
);
1382 if (dl_argc(dl
) > 0) {
1383 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, 0);
1388 pr_out_section_start(dl
, "dev");
1389 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_dev_show_cb
, dl
);
1390 pr_out_section_end(dl
);
1394 static int cmd_dev(struct dl
*dl
)
1396 if (dl_argv_match(dl
, "help")) {
1399 } else if (dl_argv_match(dl
, "show") ||
1400 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1402 return cmd_dev_show(dl
);
1403 } else if (dl_argv_match(dl
, "eswitch")) {
1405 return cmd_dev_eswitch(dl
);
1407 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1411 static void cmd_port_help(void)
1413 pr_err("Usage: devlink port show [ DEV/PORT_INDEX ]\n");
1414 pr_err(" devlink port set DEV/PORT_INDEX [ type { eth | ib | auto} ]\n");
1415 pr_err(" devlink port split DEV/PORT_INDEX count COUNT\n");
1416 pr_err(" devlink port unsplit DEV/PORT_INDEX\n");
1419 static const char *port_type_name(uint32_t type
)
1422 case DEVLINK_PORT_TYPE_NOTSET
: return "notset";
1423 case DEVLINK_PORT_TYPE_AUTO
: return "auto";
1424 case DEVLINK_PORT_TYPE_ETH
: return "eth";
1425 case DEVLINK_PORT_TYPE_IB
: return "ib";
1426 default: return "<unknown type>";
1430 static void pr_out_port(struct dl
*dl
, struct nlattr
**tb
)
1432 struct nlattr
*pt_attr
= tb
[DEVLINK_ATTR_PORT_TYPE
];
1433 struct nlattr
*dpt_attr
= tb
[DEVLINK_ATTR_PORT_DESIRED_TYPE
];
1435 pr_out_port_handle_start(dl
, tb
, false);
1437 uint16_t port_type
= mnl_attr_get_u16(pt_attr
);
1439 pr_out_str(dl
, "type", port_type_name(port_type
));
1441 uint16_t des_port_type
= mnl_attr_get_u16(dpt_attr
);
1443 if (port_type
!= des_port_type
)
1444 pr_out_str(dl
, "des_type",
1445 port_type_name(des_port_type
));
1448 if (tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
])
1449 pr_out_str(dl
, "netdev",
1450 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_NETDEV_NAME
]));
1451 if (tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
])
1452 pr_out_str(dl
, "ibdev",
1453 mnl_attr_get_str(tb
[DEVLINK_ATTR_PORT_IBDEV_NAME
]));
1454 if (tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
])
1455 pr_out_uint(dl
, "split_group",
1456 mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_SPLIT_GROUP
]));
1457 pr_out_port_handle_end(dl
);
1460 static int cmd_port_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1462 struct dl
*dl
= data
;
1463 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1464 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1466 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1467 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1468 !tb
[DEVLINK_ATTR_PORT_INDEX
])
1469 return MNL_CB_ERROR
;
1470 pr_out_port(dl
, tb
);
1474 static int cmd_port_show(struct dl
*dl
)
1476 struct nlmsghdr
*nlh
;
1477 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1480 if (dl_argc(dl
) == 0)
1481 flags
|= NLM_F_DUMP
;
1483 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_GET
, flags
);
1485 if (dl_argc(dl
) > 0) {
1486 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1491 pr_out_section_start(dl
, "port");
1492 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_port_show_cb
, dl
);
1493 pr_out_section_end(dl
);
1497 static int cmd_port_set(struct dl
*dl
)
1499 struct nlmsghdr
*nlh
;
1502 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SET
,
1503 NLM_F_REQUEST
| NLM_F_ACK
);
1505 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_TYPE
, 0);
1509 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1512 static int cmd_port_split(struct dl
*dl
)
1514 struct nlmsghdr
*nlh
;
1517 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_SPLIT
,
1518 NLM_F_REQUEST
| NLM_F_ACK
);
1520 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_PORT_COUNT
, 0);
1524 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1527 static int cmd_port_unsplit(struct dl
*dl
)
1529 struct nlmsghdr
*nlh
;
1532 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_PORT_UNSPLIT
,
1533 NLM_F_REQUEST
| NLM_F_ACK
);
1535 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
, 0);
1539 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1542 static int cmd_port(struct dl
*dl
)
1544 if (dl_argv_match(dl
, "help")) {
1547 } else if (dl_argv_match(dl
, "show") ||
1548 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1550 return cmd_port_show(dl
);
1551 } else if (dl_argv_match(dl
, "set")) {
1553 return cmd_port_set(dl
);
1554 } else if (dl_argv_match(dl
, "split")) {
1556 return cmd_port_split(dl
);
1557 } else if (dl_argv_match(dl
, "unsplit")) {
1559 return cmd_port_unsplit(dl
);
1561 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1565 static void cmd_sb_help(void)
1567 pr_err("Usage: devlink sb show [ DEV [ sb SB_INDEX ] ]\n");
1568 pr_err(" devlink sb pool show [ DEV [ sb SB_INDEX ] pool POOL_INDEX ]\n");
1569 pr_err(" devlink sb pool set DEV [ sb SB_INDEX ] pool POOL_INDEX\n");
1570 pr_err(" size POOL_SIZE thtype { static | dynamic }\n");
1571 pr_err(" devlink sb port pool show [ DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1572 pr_err(" pool POOL_INDEX ]\n");
1573 pr_err(" devlink sb port pool set DEV/PORT_INDEX [ sb SB_INDEX ]\n");
1574 pr_err(" pool POOL_INDEX th THRESHOLD\n");
1575 pr_err(" devlink sb tc bind show [ DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1576 pr_err(" type { ingress | egress } ]\n");
1577 pr_err(" devlink sb tc bind set DEV/PORT_INDEX [ sb SB_INDEX ] tc TC_INDEX\n");
1578 pr_err(" type { ingress | egress } pool POOL_INDEX\n");
1579 pr_err(" th THRESHOLD\n");
1580 pr_err(" devlink sb occupancy show { DEV | DEV/PORT_INDEX } [ sb SB_INDEX ]\n");
1581 pr_err(" devlink sb occupancy snapshot DEV [ sb SB_INDEX ]\n");
1582 pr_err(" devlink sb occupancy clearmax DEV [ sb SB_INDEX ]\n");
1585 static void pr_out_sb(struct dl
*dl
, struct nlattr
**tb
)
1587 pr_out_handle_start_arr(dl
, tb
);
1588 pr_out_uint(dl
, "sb",
1589 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1590 pr_out_uint(dl
, "size",
1591 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_SIZE
]));
1592 pr_out_uint(dl
, "ing_pools",
1593 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
]));
1594 pr_out_uint(dl
, "eg_pools",
1595 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
]));
1596 pr_out_uint(dl
, "ing_tcs",
1597 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
]));
1598 pr_out_uint(dl
, "eg_tcs",
1599 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
]));
1600 pr_out_handle_end(dl
);
1603 static int cmd_sb_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1605 struct dl
*dl
= data
;
1606 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1607 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1609 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1610 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1611 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_SIZE
] ||
1612 !tb
[DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
] ||
1613 !tb
[DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
] ||
1614 !tb
[DEVLINK_ATTR_SB_INGRESS_TC_COUNT
] ||
1615 !tb
[DEVLINK_ATTR_SB_EGRESS_TC_COUNT
])
1616 return MNL_CB_ERROR
;
1621 static int cmd_sb_show(struct dl
*dl
)
1623 struct nlmsghdr
*nlh
;
1624 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1627 if (dl_argc(dl
) == 0)
1628 flags
|= NLM_F_DUMP
;
1630 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_GET
, flags
);
1632 if (dl_argc(dl
) > 0) {
1633 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
1638 pr_out_section_start(dl
, "sb");
1639 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_show_cb
, dl
);
1640 pr_out_section_end(dl
);
1644 static const char *pool_type_name(uint8_t type
)
1647 case DEVLINK_SB_POOL_TYPE_INGRESS
: return "ingress";
1648 case DEVLINK_SB_POOL_TYPE_EGRESS
: return "egress";
1649 default: return "<unknown type>";
1653 static const char *threshold_type_name(uint8_t type
)
1656 case DEVLINK_SB_THRESHOLD_TYPE_STATIC
: return "static";
1657 case DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
: return "dynamic";
1658 default: return "<unknown type>";
1662 static void pr_out_sb_pool(struct dl
*dl
, struct nlattr
**tb
)
1664 pr_out_handle_start_arr(dl
, tb
);
1665 pr_out_uint(dl
, "sb",
1666 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1667 pr_out_uint(dl
, "pool",
1668 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1669 pr_out_str(dl
, "type",
1670 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
1671 pr_out_uint(dl
, "size",
1672 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_POOL_SIZE
]));
1673 pr_out_str(dl
, "thtype",
1674 threshold_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])));
1675 pr_out_handle_end(dl
);
1678 static int cmd_sb_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1680 struct dl
*dl
= data
;
1681 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1682 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1684 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1685 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1686 !tb
[DEVLINK_ATTR_SB_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
1687 !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] || !tb
[DEVLINK_ATTR_SB_POOL_SIZE
] ||
1688 !tb
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
1689 return MNL_CB_ERROR
;
1690 pr_out_sb_pool(dl
, tb
);
1694 static int cmd_sb_pool_show(struct dl
*dl
)
1696 struct nlmsghdr
*nlh
;
1697 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1700 if (dl_argc(dl
) == 0)
1701 flags
|= NLM_F_DUMP
;
1703 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_GET
, flags
);
1705 if (dl_argc(dl
) > 0) {
1706 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
,
1712 pr_out_section_start(dl
, "pool");
1713 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_pool_show_cb
, dl
);
1714 pr_out_section_end(dl
);
1718 static int cmd_sb_pool_set(struct dl
*dl
)
1720 struct nlmsghdr
*nlh
;
1723 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_POOL_SET
,
1724 NLM_F_REQUEST
| NLM_F_ACK
);
1726 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
| DL_OPT_SB_POOL
|
1727 DL_OPT_SB_SIZE
| DL_OPT_SB_THTYPE
, DL_OPT_SB
);
1731 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1734 static int cmd_sb_pool(struct dl
*dl
)
1736 if (dl_argv_match(dl
, "help")) {
1739 } else if (dl_argv_match(dl
, "show") ||
1740 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1742 return cmd_sb_pool_show(dl
);
1743 } else if (dl_argv_match(dl
, "set")) {
1745 return cmd_sb_pool_set(dl
);
1747 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1751 static void pr_out_sb_port_pool(struct dl
*dl
, struct nlattr
**tb
)
1753 pr_out_port_handle_start_arr(dl
, tb
, true);
1754 pr_out_uint(dl
, "sb",
1755 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1756 pr_out_uint(dl
, "pool",
1757 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1758 pr_out_uint(dl
, "threshold",
1759 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
1760 pr_out_port_handle_end(dl
);
1763 static int cmd_sb_port_pool_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1765 struct dl
*dl
= data
;
1766 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1767 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1769 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1770 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1771 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
1772 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
1773 return MNL_CB_ERROR
;
1774 pr_out_sb_port_pool(dl
, tb
);
1778 static int cmd_sb_port_pool_show(struct dl
*dl
)
1780 struct nlmsghdr
*nlh
;
1781 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1784 if (dl_argc(dl
) == 0)
1785 flags
|= NLM_F_DUMP
;
1787 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
1789 if (dl_argc(dl
) > 0) {
1790 err
= dl_argv_parse_put(nlh
, dl
,
1791 DL_OPT_HANDLEP
| DL_OPT_SB_POOL
,
1797 pr_out_section_start(dl
, "port_pool");
1798 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_port_pool_show_cb
, dl
);
1799 pr_out_section_end(dl
);
1803 static int cmd_sb_port_pool_set(struct dl
*dl
)
1805 struct nlmsghdr
*nlh
;
1808 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_SET
,
1809 NLM_F_REQUEST
| NLM_F_ACK
);
1811 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_POOL
|
1812 DL_OPT_SB_TH
, DL_OPT_SB
);
1816 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1819 static int cmd_sb_port_pool(struct dl
*dl
)
1821 if (dl_argv_match(dl
, "help")) {
1824 } else if (dl_argv_match(dl
, "show") ||
1825 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1827 return cmd_sb_port_pool_show(dl
);
1828 } else if (dl_argv_match(dl
, "set")) {
1830 return cmd_sb_port_pool_set(dl
);
1832 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1836 static int cmd_sb_port(struct dl
*dl
)
1838 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1841 } else if (dl_argv_match(dl
, "pool")) {
1843 return cmd_sb_port_pool(dl
);
1845 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1849 static void pr_out_sb_tc_bind(struct dl
*dl
, struct nlattr
**tb
)
1851 pr_out_port_handle_start_arr(dl
, tb
, true);
1852 pr_out_uint(dl
, "sb",
1853 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_INDEX
]));
1854 pr_out_uint(dl
, "tc",
1855 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]));
1856 pr_out_str(dl
, "type",
1857 pool_type_name(mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
])));
1858 pr_out_uint(dl
, "pool",
1859 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]));
1860 pr_out_uint(dl
, "threshold",
1861 mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_THRESHOLD
]));
1862 pr_out_port_handle_end(dl
);
1865 static int cmd_sb_tc_bind_show_cb(const struct nlmsghdr
*nlh
, void *data
)
1867 struct dl
*dl
= data
;
1868 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
1869 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
1871 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
1872 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
1873 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
1874 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
1875 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] || !tb
[DEVLINK_ATTR_SB_THRESHOLD
])
1876 return MNL_CB_ERROR
;
1877 pr_out_sb_tc_bind(dl
, tb
);
1881 static int cmd_sb_tc_bind_show(struct dl
*dl
)
1883 struct nlmsghdr
*nlh
;
1884 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1887 if (dl_argc(dl
) == 0)
1888 flags
|= NLM_F_DUMP
;
1890 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
1892 if (dl_argc(dl
) > 0) {
1893 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
1894 DL_OPT_SB_TYPE
, DL_OPT_SB
);
1899 pr_out_section_start(dl
, "tc_bind");
1900 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
, cmd_sb_tc_bind_show_cb
, dl
);
1901 pr_out_section_end(dl
);
1905 static int cmd_sb_tc_bind_set(struct dl
*dl
)
1907 struct nlmsghdr
*nlh
;
1910 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
1911 NLM_F_REQUEST
| NLM_F_ACK
);
1913 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLEP
| DL_OPT_SB_TC
|
1914 DL_OPT_SB_TYPE
| DL_OPT_SB_POOL
| DL_OPT_SB_TH
,
1919 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
1922 static int cmd_sb_tc_bind(struct dl
*dl
)
1924 if (dl_argv_match(dl
, "help")) {
1927 } else if (dl_argv_match(dl
, "show") ||
1928 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
1930 return cmd_sb_tc_bind_show(dl
);
1931 } else if (dl_argv_match(dl
, "set")) {
1933 return cmd_sb_tc_bind_set(dl
);
1935 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1939 static int cmd_sb_tc(struct dl
*dl
)
1941 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
1944 } else if (dl_argv_match(dl
, "bind")) {
1946 return cmd_sb_tc_bind(dl
);
1948 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
1953 struct list_head list
;
1957 uint32_t bound_pool_index
;
1961 struct list_head list
;
1964 uint32_t port_index
;
1966 struct list_head pool_list
;
1967 struct list_head ing_tc_list
;
1968 struct list_head eg_tc_list
;
1974 struct list_head port_list
;
1977 static struct occ_item
*occ_item_alloc(void)
1979 return calloc(1, sizeof(struct occ_item
));
1982 static void occ_item_free(struct occ_item
*occ_item
)
1987 static struct occ_port
*occ_port_alloc(uint32_t port_index
)
1989 struct occ_port
*occ_port
;
1991 occ_port
= calloc(1, sizeof(*occ_port
));
1994 occ_port
->port_index
= port_index
;
1995 INIT_LIST_HEAD(&occ_port
->pool_list
);
1996 INIT_LIST_HEAD(&occ_port
->ing_tc_list
);
1997 INIT_LIST_HEAD(&occ_port
->eg_tc_list
);
2001 static void occ_port_free(struct occ_port
*occ_port
)
2003 struct occ_item
*occ_item
, *tmp
;
2005 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->pool_list
, list
)
2006 occ_item_free(occ_item
);
2007 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->ing_tc_list
, list
)
2008 occ_item_free(occ_item
);
2009 list_for_each_entry_safe(occ_item
, tmp
, &occ_port
->eg_tc_list
, list
)
2010 occ_item_free(occ_item
);
2013 static struct occ_show
*occ_show_alloc(struct dl
*dl
)
2015 struct occ_show
*occ_show
;
2017 occ_show
= calloc(1, sizeof(*occ_show
));
2021 INIT_LIST_HEAD(&occ_show
->port_list
);
2025 static void occ_show_free(struct occ_show
*occ_show
)
2027 struct occ_port
*occ_port
, *tmp
;
2029 list_for_each_entry_safe(occ_port
, tmp
, &occ_show
->port_list
, list
)
2030 occ_port_free(occ_port
);
2033 static struct occ_port
*occ_port_get(struct occ_show
*occ_show
,
2036 struct occ_port
*occ_port
;
2037 uint32_t port_index
;
2039 port_index
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_PORT_INDEX
]);
2041 list_for_each_entry_reverse(occ_port
, &occ_show
->port_list
, list
) {
2042 if (occ_port
->port_index
== port_index
)
2045 occ_port
= occ_port_alloc(port_index
);
2048 list_add_tail(&occ_port
->list
, &occ_show
->port_list
);
2052 static void pr_out_occ_show_item_list(const char *label
, struct list_head
*list
,
2055 struct occ_item
*occ_item
;
2058 pr_out_sp(7, " %s:", label
);
2059 list_for_each_entry(occ_item
, list
, list
) {
2060 if ((i
- 1) % 4 == 0 && i
!= 1)
2063 pr_out_sp(7, "%2u(%u):", occ_item
->index
,
2064 occ_item
->bound_pool_index
);
2066 pr_out_sp(7, "%2u:", occ_item
->index
);
2067 pr_out_sp(15, "%7u/%u", occ_item
->cur
, occ_item
->max
);
2071 if ((i
- 1) % 4 != 0)
2075 static void pr_out_json_occ_show_item_list(struct dl
*dl
, const char *label
,
2076 struct list_head
*list
,
2079 struct occ_item
*occ_item
;
2082 jsonw_name(dl
->jw
, label
);
2083 jsonw_start_object(dl
->jw
);
2084 list_for_each_entry(occ_item
, list
, list
) {
2085 sprintf(buf
, "%u", occ_item
->index
);
2086 jsonw_name(dl
->jw
, buf
);
2087 jsonw_start_object(dl
->jw
);
2089 jsonw_uint_field(dl
->jw
, "bound_pool",
2090 occ_item
->bound_pool_index
);
2091 jsonw_uint_field(dl
->jw
, "current", occ_item
->cur
);
2092 jsonw_uint_field(dl
->jw
, "max", occ_item
->max
);
2093 jsonw_end_object(dl
->jw
);
2095 jsonw_end_object(dl
->jw
);
2098 static void pr_out_occ_show_port(struct dl
*dl
, struct occ_port
*occ_port
)
2100 if (dl
->json_output
) {
2101 pr_out_json_occ_show_item_list(dl
, "pool",
2102 &occ_port
->pool_list
, false);
2103 pr_out_json_occ_show_item_list(dl
, "itc",
2104 &occ_port
->ing_tc_list
, true);
2105 pr_out_json_occ_show_item_list(dl
, "etc",
2106 &occ_port
->eg_tc_list
, true);
2109 pr_out_occ_show_item_list("pool", &occ_port
->pool_list
, false);
2110 pr_out_occ_show_item_list("itc", &occ_port
->ing_tc_list
, true);
2111 pr_out_occ_show_item_list("etc", &occ_port
->eg_tc_list
, true);
2115 static void pr_out_occ_show(struct occ_show
*occ_show
)
2117 struct dl
*dl
= occ_show
->dl
;
2118 struct dl_opts
*opts
= &dl
->opts
;
2119 struct occ_port
*occ_port
;
2121 list_for_each_entry(occ_port
, &occ_show
->port_list
, list
) {
2122 __pr_out_port_handle_start(dl
, opts
->bus_name
, opts
->dev_name
,
2123 occ_port
->port_index
, true, false);
2124 pr_out_occ_show_port(dl
, occ_port
);
2125 pr_out_port_handle_end(dl
);
2129 static void cmd_sb_occ_port_pool_process(struct occ_show
*occ_show
,
2132 struct occ_port
*occ_port
;
2133 struct occ_item
*occ_item
;
2135 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2138 occ_port
= occ_port_get(occ_show
, tb
);
2140 occ_show
->err
= -ENOMEM
;
2144 occ_item
= occ_item_alloc();
2146 occ_show
->err
= -ENOMEM
;
2149 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2150 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2151 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2152 list_add_tail(&occ_item
->list
, &occ_port
->pool_list
);
2155 static int cmd_sb_occ_port_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2157 struct occ_show
*occ_show
= data
;
2158 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2159 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2161 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2162 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2163 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2164 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2165 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2166 return MNL_CB_ERROR
;
2167 cmd_sb_occ_port_pool_process(occ_show
, tb
);
2171 static void cmd_sb_occ_tc_pool_process(struct occ_show
*occ_show
,
2174 struct occ_port
*occ_port
;
2175 struct occ_item
*occ_item
;
2178 if (occ_show
->err
|| !dl_dump_filter(occ_show
->dl
, tb
))
2181 occ_port
= occ_port_get(occ_show
, tb
);
2183 occ_show
->err
= -ENOMEM
;
2187 occ_item
= occ_item_alloc();
2189 occ_show
->err
= -ENOMEM
;
2192 occ_item
->index
= mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_TC_INDEX
]);
2193 occ_item
->cur
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_CUR
]);
2194 occ_item
->max
= mnl_attr_get_u32(tb
[DEVLINK_ATTR_SB_OCC_MAX
]);
2195 occ_item
->bound_pool_index
=
2196 mnl_attr_get_u16(tb
[DEVLINK_ATTR_SB_POOL_INDEX
]);
2197 pool_type
= mnl_attr_get_u8(tb
[DEVLINK_ATTR_SB_POOL_TYPE
]);
2198 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
)
2199 list_add_tail(&occ_item
->list
, &occ_port
->ing_tc_list
);
2200 else if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
)
2201 list_add_tail(&occ_item
->list
, &occ_port
->eg_tc_list
);
2203 occ_item_free(occ_item
);
2206 static int cmd_sb_occ_tc_pool_process_cb(const struct nlmsghdr
*nlh
, void *data
)
2208 struct occ_show
*occ_show
= data
;
2209 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2210 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2212 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2213 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2214 !tb
[DEVLINK_ATTR_PORT_INDEX
] || !tb
[DEVLINK_ATTR_SB_INDEX
] ||
2215 !tb
[DEVLINK_ATTR_SB_TC_INDEX
] || !tb
[DEVLINK_ATTR_SB_POOL_TYPE
] ||
2216 !tb
[DEVLINK_ATTR_SB_POOL_INDEX
] ||
2217 !tb
[DEVLINK_ATTR_SB_OCC_CUR
] || !tb
[DEVLINK_ATTR_SB_OCC_MAX
])
2218 return MNL_CB_ERROR
;
2219 cmd_sb_occ_tc_pool_process(occ_show
, tb
);
2223 static int cmd_sb_occ_show(struct dl
*dl
)
2225 struct nlmsghdr
*nlh
;
2226 struct occ_show
*occ_show
;
2227 uint16_t flags
= NLM_F_REQUEST
| NLM_F_ACK
| NLM_F_DUMP
;
2230 err
= dl_argv_parse(dl
, DL_OPT_HANDLE
| DL_OPT_HANDLEP
, DL_OPT_SB
);
2234 occ_show
= occ_show_alloc(dl
);
2238 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_PORT_POOL_GET
, flags
);
2240 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2241 cmd_sb_occ_port_pool_process_cb
, occ_show
);
2245 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_TC_POOL_BIND_GET
, flags
);
2247 err
= _mnlg_socket_sndrcv(dl
->nlg
, nlh
,
2248 cmd_sb_occ_tc_pool_process_cb
, occ_show
);
2252 pr_out_section_start(dl
, "occupancy");
2253 pr_out_occ_show(occ_show
);
2254 pr_out_section_end(dl
);
2257 occ_show_free(occ_show
);
2261 static int cmd_sb_occ_snapshot(struct dl
*dl
)
2263 struct nlmsghdr
*nlh
;
2266 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2267 NLM_F_REQUEST
| NLM_F_ACK
);
2269 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2273 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2276 static int cmd_sb_occ_clearmax(struct dl
*dl
)
2278 struct nlmsghdr
*nlh
;
2281 nlh
= mnlg_msg_prepare(dl
->nlg
, DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2282 NLM_F_REQUEST
| NLM_F_ACK
);
2284 err
= dl_argv_parse_put(nlh
, dl
, DL_OPT_HANDLE
, DL_OPT_SB
);
2288 return _mnlg_socket_sndrcv(dl
->nlg
, nlh
, NULL
, NULL
);
2291 static int cmd_sb_occ(struct dl
*dl
)
2293 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2296 } else if (dl_argv_match(dl
, "show") ||
2297 dl_argv_match(dl
, "list")) {
2299 return cmd_sb_occ_show(dl
);
2300 } else if (dl_argv_match(dl
, "snapshot")) {
2302 return cmd_sb_occ_snapshot(dl
);
2303 } else if (dl_argv_match(dl
, "clearmax")) {
2305 return cmd_sb_occ_clearmax(dl
);
2307 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2311 static int cmd_sb(struct dl
*dl
)
2313 if (dl_argv_match(dl
, "help")) {
2316 } else if (dl_argv_match(dl
, "show") ||
2317 dl_argv_match(dl
, "list") || dl_no_arg(dl
)) {
2319 return cmd_sb_show(dl
);
2320 } else if (dl_argv_match(dl
, "pool")) {
2322 return cmd_sb_pool(dl
);
2323 } else if (dl_argv_match(dl
, "port")) {
2325 return cmd_sb_port(dl
);
2326 } else if (dl_argv_match(dl
, "tc")) {
2328 return cmd_sb_tc(dl
);
2329 } else if (dl_argv_match(dl
, "occupancy")) {
2331 return cmd_sb_occ(dl
);
2333 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2337 static const char *cmd_name(uint8_t cmd
)
2340 case DEVLINK_CMD_UNSPEC
: return "unspec";
2341 case DEVLINK_CMD_GET
: return "get";
2342 case DEVLINK_CMD_SET
: return "set";
2343 case DEVLINK_CMD_NEW
: return "new";
2344 case DEVLINK_CMD_DEL
: return "del";
2345 case DEVLINK_CMD_PORT_GET
: return "get";
2346 case DEVLINK_CMD_PORT_SET
: return "set";
2347 case DEVLINK_CMD_PORT_NEW
: return "net";
2348 case DEVLINK_CMD_PORT_DEL
: return "del";
2349 default: return "<unknown cmd>";
2353 static const char *cmd_obj(uint8_t cmd
)
2356 case DEVLINK_CMD_UNSPEC
: return "unspec";
2357 case DEVLINK_CMD_GET
:
2358 case DEVLINK_CMD_SET
:
2359 case DEVLINK_CMD_NEW
:
2360 case DEVLINK_CMD_DEL
:
2362 case DEVLINK_CMD_PORT_GET
:
2363 case DEVLINK_CMD_PORT_SET
:
2364 case DEVLINK_CMD_PORT_NEW
:
2365 case DEVLINK_CMD_PORT_DEL
:
2367 default: return "<unknown obj>";
2371 static void pr_out_mon_header(uint8_t cmd
)
2373 pr_out("[%s,%s] ", cmd_obj(cmd
), cmd_name(cmd
));
2376 static bool cmd_filter_check(struct dl
*dl
, uint8_t cmd
)
2378 const char *obj
= cmd_obj(cmd
);
2379 unsigned int index
= 0;
2380 const char *cur_obj
;
2384 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2385 if (strcmp(cur_obj
, obj
) == 0 || strcmp(cur_obj
, "all") == 0)
2391 static int cmd_mon_show_cb(const struct nlmsghdr
*nlh
, void *data
)
2393 struct dl
*dl
= data
;
2394 struct nlattr
*tb
[DEVLINK_ATTR_MAX
+ 1] = {};
2395 struct genlmsghdr
*genl
= mnl_nlmsg_get_payload(nlh
);
2396 uint8_t cmd
= genl
->cmd
;
2398 if (!cmd_filter_check(dl
, cmd
))
2402 case DEVLINK_CMD_GET
: /* fall through */
2403 case DEVLINK_CMD_SET
: /* fall through */
2404 case DEVLINK_CMD_NEW
: /* fall through */
2405 case DEVLINK_CMD_DEL
:
2406 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2407 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
])
2408 return MNL_CB_ERROR
;
2409 pr_out_mon_header(genl
->cmd
);
2412 case DEVLINK_CMD_PORT_GET
: /* fall through */
2413 case DEVLINK_CMD_PORT_SET
: /* fall through */
2414 case DEVLINK_CMD_PORT_NEW
: /* fall through */
2415 case DEVLINK_CMD_PORT_DEL
:
2416 mnl_attr_parse(nlh
, sizeof(*genl
), attr_cb
, tb
);
2417 if (!tb
[DEVLINK_ATTR_BUS_NAME
] || !tb
[DEVLINK_ATTR_DEV_NAME
] ||
2418 !tb
[DEVLINK_ATTR_PORT_INDEX
])
2419 return MNL_CB_ERROR
;
2420 pr_out_mon_header(genl
->cmd
);
2421 pr_out_port(dl
, tb
);
2427 static int cmd_mon_show(struct dl
*dl
)
2430 unsigned int index
= 0;
2431 const char *cur_obj
;
2433 while ((cur_obj
= dl_argv_index(dl
, index
++))) {
2434 if (strcmp(cur_obj
, "all") != 0 &&
2435 strcmp(cur_obj
, "dev") != 0 &&
2436 strcmp(cur_obj
, "port") != 0) {
2437 pr_err("Unknown object \"%s\"\n", cur_obj
);
2441 err
= _mnlg_socket_group_add(dl
->nlg
, DEVLINK_GENL_MCGRP_CONFIG_NAME
);
2444 err
= _mnlg_socket_recv_run(dl
->nlg
, cmd_mon_show_cb
, dl
);
2450 static void cmd_mon_help(void)
2452 pr_err("Usage: devlink monitor [ all | OBJECT-LIST ]\n"
2453 "where OBJECT-LIST := { dev | port }\n");
2456 static int cmd_mon(struct dl
*dl
)
2458 if (dl_argv_match(dl
, "help")) {
2461 } else if (dl_no_arg(dl
)) {
2463 return cmd_mon_show(dl
);
2465 pr_err("Command \"%s\" not found\n", dl_argv(dl
));
2469 static void help(void)
2471 pr_err("Usage: devlink [ OPTIONS ] OBJECT { COMMAND | help }\n"
2472 "where OBJECT := { dev | port | sb | monitor }\n"
2473 " OPTIONS := { -V[ersion] | -n[no-nice-names] }\n");
2476 static int dl_cmd(struct dl
*dl
)
2478 if (dl_argv_match(dl
, "help") || dl_no_arg(dl
)) {
2481 } else if (dl_argv_match(dl
, "dev")) {
2484 } else if (dl_argv_match(dl
, "port")) {
2486 return cmd_port(dl
);
2487 } else if (dl_argv_match(dl
, "sb")) {
2490 } else if (dl_argv_match(dl
, "monitor")) {
2494 pr_err("Object \"%s\" not found\n", dl_argv(dl
));
2498 static int dl_init(struct dl
*dl
, int argc
, char **argv
)
2505 dl
->nlg
= mnlg_socket_open(DEVLINK_GENL_NAME
, DEVLINK_GENL_VERSION
);
2507 pr_err("Failed to connect to devlink Netlink\n");
2511 err
= ifname_map_init(dl
);
2513 pr_err("Failed to create index map\n");
2514 goto err_ifname_map_create
;
2516 if (dl
->json_output
) {
2517 dl
->jw
= jsonw_new(stdout
);
2519 pr_err("Failed to create JSON writer\n");
2522 jsonw_pretty(dl
->jw
, dl
->pretty_output
);
2527 ifname_map_fini(dl
);
2528 err_ifname_map_create
:
2529 mnlg_socket_close(dl
->nlg
);
2533 static void dl_fini(struct dl
*dl
)
2535 if (dl
->json_output
)
2536 jsonw_destroy(&dl
->jw
);
2537 ifname_map_fini(dl
);
2538 mnlg_socket_close(dl
->nlg
);
2541 static struct dl
*dl_alloc(void)
2545 dl
= calloc(1, sizeof(*dl
));
2551 static void dl_free(struct dl
*dl
)
2556 int main(int argc
, char **argv
)
2558 static const struct option long_options
[] = {
2559 { "Version", no_argument
, NULL
, 'V' },
2560 { "no-nice-names", no_argument
, NULL
, 'n' },
2561 { "json", no_argument
, NULL
, 'j' },
2562 { "pretty", no_argument
, NULL
, 'p' },
2563 { NULL
, 0, NULL
, 0 }
2572 pr_err("Failed to allocate memory for devlink\n");
2573 return EXIT_FAILURE
;
2576 while ((opt
= getopt_long(argc
, argv
, "Vnjp",
2577 long_options
, NULL
)) >= 0) {
2581 printf("devlink utility, iproute2-ss%s\n", SNAPSHOT
);
2582 return EXIT_SUCCESS
;
2584 dl
->no_nice_names
= true;
2587 dl
->json_output
= true;
2590 dl
->pretty_output
= true;
2593 pr_err("Unknown option.\n");
2595 return EXIT_FAILURE
;
2602 err
= dl_init(dl
, argc
, argv
);