2 * net/core/devlink.c - Network physical/parent device Netlink interface
4 * Heavily inspired by net/wireless/
5 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/gfp.h>
19 #include <linux/device.h>
20 #include <linux/list.h>
21 #include <linux/netdevice.h>
22 #include <rdma/ib_verbs.h>
23 #include <net/netlink.h>
24 #include <net/genetlink.h>
25 #include <net/rtnetlink.h>
26 #include <net/net_namespace.h>
28 #include <net/devlink.h>
30 static LIST_HEAD(devlink_list
);
34 * An overall lock guarding every operation coming from userspace.
35 * It also guards devlink devices list and it is taken when
36 * driver registers/unregisters it.
38 static DEFINE_MUTEX(devlink_mutex
);
42 * Shared lock to guard lists of ports in all devlink devices.
44 static DEFINE_MUTEX(devlink_port_mutex
);
46 static struct net
*devlink_net(const struct devlink
*devlink
)
48 return read_pnet(&devlink
->_net
);
51 static void devlink_net_set(struct devlink
*devlink
, struct net
*net
)
53 write_pnet(&devlink
->_net
, net
);
56 static struct devlink
*devlink_get_from_attrs(struct net
*net
,
57 struct nlattr
**attrs
)
59 struct devlink
*devlink
;
63 if (!attrs
[DEVLINK_ATTR_BUS_NAME
] || !attrs
[DEVLINK_ATTR_DEV_NAME
])
64 return ERR_PTR(-EINVAL
);
66 busname
= nla_data(attrs
[DEVLINK_ATTR_BUS_NAME
]);
67 devname
= nla_data(attrs
[DEVLINK_ATTR_DEV_NAME
]);
69 list_for_each_entry(devlink
, &devlink_list
, list
) {
70 if (strcmp(devlink
->dev
->bus
->name
, busname
) == 0 &&
71 strcmp(dev_name(devlink
->dev
), devname
) == 0 &&
72 net_eq(devlink_net(devlink
), net
))
76 return ERR_PTR(-ENODEV
);
79 static struct devlink
*devlink_get_from_info(struct genl_info
*info
)
81 return devlink_get_from_attrs(genl_info_net(info
), info
->attrs
);
84 static struct devlink_port
*devlink_port_get_by_index(struct devlink
*devlink
,
87 struct devlink_port
*devlink_port
;
89 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
90 if (devlink_port
->index
== port_index
)
96 static bool devlink_port_index_exists(struct devlink
*devlink
, int port_index
)
98 return devlink_port_get_by_index(devlink
, port_index
);
101 static struct devlink_port
*devlink_port_get_from_attrs(struct devlink
*devlink
,
102 struct nlattr
**attrs
)
104 if (attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
105 u32 port_index
= nla_get_u32(attrs
[DEVLINK_ATTR_PORT_INDEX
]);
106 struct devlink_port
*devlink_port
;
108 devlink_port
= devlink_port_get_by_index(devlink
, port_index
);
110 return ERR_PTR(-ENODEV
);
113 return ERR_PTR(-EINVAL
);
116 static struct devlink_port
*devlink_port_get_from_info(struct devlink
*devlink
,
117 struct genl_info
*info
)
119 return devlink_port_get_from_attrs(devlink
, info
->attrs
);
123 struct list_head list
;
126 u16 ingress_pools_count
;
127 u16 egress_pools_count
;
128 u16 ingress_tc_count
;
132 static u16
devlink_sb_pool_count(struct devlink_sb
*devlink_sb
)
134 return devlink_sb
->ingress_pools_count
+ devlink_sb
->egress_pools_count
;
137 static struct devlink_sb
*devlink_sb_get_by_index(struct devlink
*devlink
,
138 unsigned int sb_index
)
140 struct devlink_sb
*devlink_sb
;
142 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
143 if (devlink_sb
->index
== sb_index
)
149 static bool devlink_sb_index_exists(struct devlink
*devlink
,
150 unsigned int sb_index
)
152 return devlink_sb_get_by_index(devlink
, sb_index
);
155 static struct devlink_sb
*devlink_sb_get_from_attrs(struct devlink
*devlink
,
156 struct nlattr
**attrs
)
158 if (attrs
[DEVLINK_ATTR_SB_INDEX
]) {
159 u32 sb_index
= nla_get_u32(attrs
[DEVLINK_ATTR_SB_INDEX
]);
160 struct devlink_sb
*devlink_sb
;
162 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
164 return ERR_PTR(-ENODEV
);
167 return ERR_PTR(-EINVAL
);
170 static struct devlink_sb
*devlink_sb_get_from_info(struct devlink
*devlink
,
171 struct genl_info
*info
)
173 return devlink_sb_get_from_attrs(devlink
, info
->attrs
);
176 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
177 struct nlattr
**attrs
,
182 if (!attrs
[DEVLINK_ATTR_SB_POOL_INDEX
])
185 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_POOL_INDEX
]);
186 if (val
>= devlink_sb_pool_count(devlink_sb
))
192 static int devlink_sb_pool_index_get_from_info(struct devlink_sb
*devlink_sb
,
193 struct genl_info
*info
,
196 return devlink_sb_pool_index_get_from_attrs(devlink_sb
, info
->attrs
,
201 devlink_sb_pool_type_get_from_attrs(struct nlattr
**attrs
,
202 enum devlink_sb_pool_type
*p_pool_type
)
206 if (!attrs
[DEVLINK_ATTR_SB_POOL_TYPE
])
209 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_TYPE
]);
210 if (val
!= DEVLINK_SB_POOL_TYPE_INGRESS
&&
211 val
!= DEVLINK_SB_POOL_TYPE_EGRESS
)
218 devlink_sb_pool_type_get_from_info(struct genl_info
*info
,
219 enum devlink_sb_pool_type
*p_pool_type
)
221 return devlink_sb_pool_type_get_from_attrs(info
->attrs
, p_pool_type
);
225 devlink_sb_th_type_get_from_attrs(struct nlattr
**attrs
,
226 enum devlink_sb_threshold_type
*p_th_type
)
230 if (!attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
233 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
]);
234 if (val
!= DEVLINK_SB_THRESHOLD_TYPE_STATIC
&&
235 val
!= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
)
242 devlink_sb_th_type_get_from_info(struct genl_info
*info
,
243 enum devlink_sb_threshold_type
*p_th_type
)
245 return devlink_sb_th_type_get_from_attrs(info
->attrs
, p_th_type
);
249 devlink_sb_tc_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
250 struct nlattr
**attrs
,
251 enum devlink_sb_pool_type pool_type
,
256 if (!attrs
[DEVLINK_ATTR_SB_TC_INDEX
])
259 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_TC_INDEX
]);
260 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
&&
261 val
>= devlink_sb
->ingress_tc_count
)
263 if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
&&
264 val
>= devlink_sb
->egress_tc_count
)
271 devlink_sb_tc_index_get_from_info(struct devlink_sb
*devlink_sb
,
272 struct genl_info
*info
,
273 enum devlink_sb_pool_type pool_type
,
276 return devlink_sb_tc_index_get_from_attrs(devlink_sb
, info
->attrs
,
277 pool_type
, p_tc_index
);
280 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
281 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
282 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
283 #define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3)
284 /* port is not needed but we need to ensure they don't
285 * change in the middle of command
288 static int devlink_nl_pre_doit(const struct genl_ops
*ops
,
289 struct sk_buff
*skb
, struct genl_info
*info
)
291 struct devlink
*devlink
;
293 mutex_lock(&devlink_mutex
);
294 devlink
= devlink_get_from_info(info
);
295 if (IS_ERR(devlink
)) {
296 mutex_unlock(&devlink_mutex
);
297 return PTR_ERR(devlink
);
299 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_DEVLINK
) {
300 info
->user_ptr
[0] = devlink
;
301 } else if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
) {
302 struct devlink_port
*devlink_port
;
304 mutex_lock(&devlink_port_mutex
);
305 devlink_port
= devlink_port_get_from_info(devlink
, info
);
306 if (IS_ERR(devlink_port
)) {
307 mutex_unlock(&devlink_port_mutex
);
308 mutex_unlock(&devlink_mutex
);
309 return PTR_ERR(devlink_port
);
311 info
->user_ptr
[0] = devlink_port
;
313 if (ops
->internal_flags
& DEVLINK_NL_FLAG_LOCK_PORTS
) {
314 mutex_lock(&devlink_port_mutex
);
316 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_SB
) {
317 struct devlink_sb
*devlink_sb
;
319 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
320 if (IS_ERR(devlink_sb
)) {
321 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
)
322 mutex_unlock(&devlink_port_mutex
);
323 mutex_unlock(&devlink_mutex
);
324 return PTR_ERR(devlink_sb
);
326 info
->user_ptr
[1] = devlink_sb
;
331 static void devlink_nl_post_doit(const struct genl_ops
*ops
,
332 struct sk_buff
*skb
, struct genl_info
*info
)
334 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
||
335 ops
->internal_flags
& DEVLINK_NL_FLAG_LOCK_PORTS
)
336 mutex_unlock(&devlink_port_mutex
);
337 mutex_unlock(&devlink_mutex
);
340 static struct genl_family devlink_nl_family
= {
341 .id
= GENL_ID_GENERATE
,
342 .name
= DEVLINK_GENL_NAME
,
343 .version
= DEVLINK_GENL_VERSION
,
344 .maxattr
= DEVLINK_ATTR_MAX
,
346 .pre_doit
= devlink_nl_pre_doit
,
347 .post_doit
= devlink_nl_post_doit
,
350 enum devlink_multicast_groups
{
351 DEVLINK_MCGRP_CONFIG
,
354 static const struct genl_multicast_group devlink_nl_mcgrps
[] = {
355 [DEVLINK_MCGRP_CONFIG
] = { .name
= DEVLINK_GENL_MCGRP_CONFIG_NAME
},
358 static int devlink_nl_put_handle(struct sk_buff
*msg
, struct devlink
*devlink
)
360 if (nla_put_string(msg
, DEVLINK_ATTR_BUS_NAME
, devlink
->dev
->bus
->name
))
362 if (nla_put_string(msg
, DEVLINK_ATTR_DEV_NAME
, dev_name(devlink
->dev
)))
367 static int devlink_nl_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
368 enum devlink_command cmd
, u32 portid
,
373 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
377 if (devlink_nl_put_handle(msg
, devlink
))
378 goto nla_put_failure
;
380 genlmsg_end(msg
, hdr
);
384 genlmsg_cancel(msg
, hdr
);
388 static void devlink_notify(struct devlink
*devlink
, enum devlink_command cmd
)
393 WARN_ON(cmd
!= DEVLINK_CMD_NEW
&& cmd
!= DEVLINK_CMD_DEL
);
395 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
399 err
= devlink_nl_fill(msg
, devlink
, cmd
, 0, 0, 0);
405 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
406 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
409 static int devlink_nl_port_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
410 struct devlink_port
*devlink_port
,
411 enum devlink_command cmd
, u32 portid
,
416 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
420 if (devlink_nl_put_handle(msg
, devlink
))
421 goto nla_put_failure
;
422 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
423 goto nla_put_failure
;
424 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_TYPE
, devlink_port
->type
))
425 goto nla_put_failure
;
426 if (devlink_port
->desired_type
!= DEVLINK_PORT_TYPE_NOTSET
&&
427 nla_put_u16(msg
, DEVLINK_ATTR_PORT_DESIRED_TYPE
,
428 devlink_port
->desired_type
))
429 goto nla_put_failure
;
430 if (devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
) {
431 struct net_device
*netdev
= devlink_port
->type_dev
;
434 (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NETDEV_IFINDEX
,
436 nla_put_string(msg
, DEVLINK_ATTR_PORT_NETDEV_NAME
,
438 goto nla_put_failure
;
440 if (devlink_port
->type
== DEVLINK_PORT_TYPE_IB
) {
441 struct ib_device
*ibdev
= devlink_port
->type_dev
;
444 nla_put_string(msg
, DEVLINK_ATTR_PORT_IBDEV_NAME
,
446 goto nla_put_failure
;
448 if (devlink_port
->split
&&
449 nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_GROUP
,
450 devlink_port
->split_group
))
451 goto nla_put_failure
;
453 genlmsg_end(msg
, hdr
);
457 genlmsg_cancel(msg
, hdr
);
461 static void devlink_port_notify(struct devlink_port
*devlink_port
,
462 enum devlink_command cmd
)
464 struct devlink
*devlink
= devlink_port
->devlink
;
468 if (!devlink_port
->registered
)
471 WARN_ON(cmd
!= DEVLINK_CMD_PORT_NEW
&& cmd
!= DEVLINK_CMD_PORT_DEL
);
473 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
477 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
, cmd
, 0, 0, 0);
483 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
484 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
487 static int devlink_nl_cmd_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
489 struct devlink
*devlink
= info
->user_ptr
[0];
493 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
497 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
498 info
->snd_portid
, info
->snd_seq
, 0);
504 return genlmsg_reply(msg
, info
);
507 static int devlink_nl_cmd_get_dumpit(struct sk_buff
*msg
,
508 struct netlink_callback
*cb
)
510 struct devlink
*devlink
;
511 int start
= cb
->args
[0];
515 mutex_lock(&devlink_mutex
);
516 list_for_each_entry(devlink
, &devlink_list
, list
) {
517 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
523 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
524 NETLINK_CB(cb
->skb
).portid
,
525 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
531 mutex_unlock(&devlink_mutex
);
537 static int devlink_nl_cmd_port_get_doit(struct sk_buff
*skb
,
538 struct genl_info
*info
)
540 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
541 struct devlink
*devlink
= devlink_port
->devlink
;
545 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
549 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
550 DEVLINK_CMD_PORT_NEW
,
551 info
->snd_portid
, info
->snd_seq
, 0);
557 return genlmsg_reply(msg
, info
);
560 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff
*msg
,
561 struct netlink_callback
*cb
)
563 struct devlink
*devlink
;
564 struct devlink_port
*devlink_port
;
565 int start
= cb
->args
[0];
569 mutex_lock(&devlink_mutex
);
570 mutex_lock(&devlink_port_mutex
);
571 list_for_each_entry(devlink
, &devlink_list
, list
) {
572 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
574 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
579 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
581 NETLINK_CB(cb
->skb
).portid
,
590 mutex_unlock(&devlink_port_mutex
);
591 mutex_unlock(&devlink_mutex
);
597 static int devlink_port_type_set(struct devlink
*devlink
,
598 struct devlink_port
*devlink_port
,
599 enum devlink_port_type port_type
)
604 if (devlink
->ops
&& devlink
->ops
->port_type_set
) {
605 if (port_type
== DEVLINK_PORT_TYPE_NOTSET
)
607 err
= devlink
->ops
->port_type_set(devlink_port
, port_type
);
610 devlink_port
->desired_type
= port_type
;
611 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
617 static int devlink_nl_cmd_port_set_doit(struct sk_buff
*skb
,
618 struct genl_info
*info
)
620 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
621 struct devlink
*devlink
= devlink_port
->devlink
;
624 if (info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]) {
625 enum devlink_port_type port_type
;
627 port_type
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]);
628 err
= devlink_port_type_set(devlink
, devlink_port
, port_type
);
635 static int devlink_port_split(struct devlink
*devlink
,
636 u32 port_index
, u32 count
)
639 if (devlink
->ops
&& devlink
->ops
->port_split
)
640 return devlink
->ops
->port_split(devlink
, port_index
, count
);
644 static int devlink_nl_cmd_port_split_doit(struct sk_buff
*skb
,
645 struct genl_info
*info
)
647 struct devlink
*devlink
= info
->user_ptr
[0];
651 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
] ||
652 !info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
])
655 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
656 count
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
]);
657 return devlink_port_split(devlink
, port_index
, count
);
660 static int devlink_port_unsplit(struct devlink
*devlink
, u32 port_index
)
663 if (devlink
->ops
&& devlink
->ops
->port_unsplit
)
664 return devlink
->ops
->port_unsplit(devlink
, port_index
);
668 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff
*skb
,
669 struct genl_info
*info
)
671 struct devlink
*devlink
= info
->user_ptr
[0];
674 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
])
677 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
678 return devlink_port_unsplit(devlink
, port_index
);
681 static int devlink_nl_sb_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
682 struct devlink_sb
*devlink_sb
,
683 enum devlink_command cmd
, u32 portid
,
688 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
692 if (devlink_nl_put_handle(msg
, devlink
))
693 goto nla_put_failure
;
694 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
695 goto nla_put_failure
;
696 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_SIZE
, devlink_sb
->size
))
697 goto nla_put_failure
;
698 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
,
699 devlink_sb
->ingress_pools_count
))
700 goto nla_put_failure
;
701 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
,
702 devlink_sb
->egress_pools_count
))
703 goto nla_put_failure
;
704 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_TC_COUNT
,
705 devlink_sb
->ingress_tc_count
))
706 goto nla_put_failure
;
707 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_TC_COUNT
,
708 devlink_sb
->egress_tc_count
))
709 goto nla_put_failure
;
711 genlmsg_end(msg
, hdr
);
715 genlmsg_cancel(msg
, hdr
);
719 static int devlink_nl_cmd_sb_get_doit(struct sk_buff
*skb
,
720 struct genl_info
*info
)
722 struct devlink
*devlink
= info
->user_ptr
[0];
723 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
727 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
731 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
733 info
->snd_portid
, info
->snd_seq
, 0);
739 return genlmsg_reply(msg
, info
);
742 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff
*msg
,
743 struct netlink_callback
*cb
)
745 struct devlink
*devlink
;
746 struct devlink_sb
*devlink_sb
;
747 int start
= cb
->args
[0];
751 mutex_lock(&devlink_mutex
);
752 list_for_each_entry(devlink
, &devlink_list
, list
) {
753 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
755 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
760 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
762 NETLINK_CB(cb
->skb
).portid
,
771 mutex_unlock(&devlink_mutex
);
777 static int devlink_nl_sb_pool_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
778 struct devlink_sb
*devlink_sb
,
779 u16 pool_index
, enum devlink_command cmd
,
780 u32 portid
, u32 seq
, int flags
)
782 struct devlink_sb_pool_info pool_info
;
786 err
= devlink
->ops
->sb_pool_get(devlink
, devlink_sb
->index
,
787 pool_index
, &pool_info
);
791 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
795 if (devlink_nl_put_handle(msg
, devlink
))
796 goto nla_put_failure
;
797 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
798 goto nla_put_failure
;
799 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
800 goto nla_put_failure
;
801 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_info
.pool_type
))
802 goto nla_put_failure
;
803 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_POOL_SIZE
, pool_info
.size
))
804 goto nla_put_failure
;
805 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
806 pool_info
.threshold_type
))
807 goto nla_put_failure
;
809 genlmsg_end(msg
, hdr
);
813 genlmsg_cancel(msg
, hdr
);
817 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff
*skb
,
818 struct genl_info
*info
)
820 struct devlink
*devlink
= info
->user_ptr
[0];
821 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
826 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
831 if (!devlink
->ops
|| !devlink
->ops
->sb_pool_get
)
834 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
838 err
= devlink_nl_sb_pool_fill(msg
, devlink
, devlink_sb
, pool_index
,
839 DEVLINK_CMD_SB_POOL_NEW
,
840 info
->snd_portid
, info
->snd_seq
, 0);
846 return genlmsg_reply(msg
, info
);
849 static int __sb_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
850 struct devlink
*devlink
,
851 struct devlink_sb
*devlink_sb
,
854 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
858 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
859 if (*p_idx
< start
) {
863 err
= devlink_nl_sb_pool_fill(msg
, devlink
,
866 DEVLINK_CMD_SB_POOL_NEW
,
867 portid
, seq
, NLM_F_MULTI
);
875 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff
*msg
,
876 struct netlink_callback
*cb
)
878 struct devlink
*devlink
;
879 struct devlink_sb
*devlink_sb
;
880 int start
= cb
->args
[0];
884 mutex_lock(&devlink_mutex
);
885 list_for_each_entry(devlink
, &devlink_list
, list
) {
886 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
887 !devlink
->ops
|| !devlink
->ops
->sb_pool_get
)
889 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
890 err
= __sb_pool_get_dumpit(msg
, start
, &idx
, devlink
,
892 NETLINK_CB(cb
->skb
).portid
,
894 if (err
&& err
!= -EOPNOTSUPP
)
899 mutex_unlock(&devlink_mutex
);
905 static int devlink_sb_pool_set(struct devlink
*devlink
, unsigned int sb_index
,
906 u16 pool_index
, u32 size
,
907 enum devlink_sb_threshold_type threshold_type
)
910 const struct devlink_ops
*ops
= devlink
->ops
;
912 if (ops
&& ops
->sb_pool_set
)
913 return ops
->sb_pool_set(devlink
, sb_index
, pool_index
,
914 size
, threshold_type
);
918 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff
*skb
,
919 struct genl_info
*info
)
921 struct devlink
*devlink
= info
->user_ptr
[0];
922 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
923 enum devlink_sb_threshold_type threshold_type
;
928 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
933 err
= devlink_sb_th_type_get_from_info(info
, &threshold_type
);
937 if (!info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
])
940 size
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
]);
941 return devlink_sb_pool_set(devlink
, devlink_sb
->index
,
942 pool_index
, size
, threshold_type
);
945 static int devlink_nl_sb_port_pool_fill(struct sk_buff
*msg
,
946 struct devlink
*devlink
,
947 struct devlink_port
*devlink_port
,
948 struct devlink_sb
*devlink_sb
,
950 enum devlink_command cmd
,
951 u32 portid
, u32 seq
, int flags
)
953 const struct devlink_ops
*ops
= devlink
->ops
;
958 err
= ops
->sb_port_pool_get(devlink_port
, devlink_sb
->index
,
959 pool_index
, &threshold
);
963 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
967 if (devlink_nl_put_handle(msg
, devlink
))
968 goto nla_put_failure
;
969 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
970 goto nla_put_failure
;
971 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
972 goto nla_put_failure
;
973 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
974 goto nla_put_failure
;
975 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
976 goto nla_put_failure
;
978 if (ops
->sb_occ_port_pool_get
) {
982 err
= ops
->sb_occ_port_pool_get(devlink_port
, devlink_sb
->index
,
983 pool_index
, &cur
, &max
);
984 if (err
&& err
!= -EOPNOTSUPP
)
987 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
988 goto nla_put_failure
;
989 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
990 goto nla_put_failure
;
994 genlmsg_end(msg
, hdr
);
998 genlmsg_cancel(msg
, hdr
);
1002 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff
*skb
,
1003 struct genl_info
*info
)
1005 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1006 struct devlink
*devlink
= devlink_port
->devlink
;
1007 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1008 struct sk_buff
*msg
;
1012 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1017 if (!devlink
->ops
|| !devlink
->ops
->sb_port_pool_get
)
1020 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1024 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
, devlink_port
,
1025 devlink_sb
, pool_index
,
1026 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1027 info
->snd_portid
, info
->snd_seq
, 0);
1033 return genlmsg_reply(msg
, info
);
1036 static int __sb_port_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
1037 struct devlink
*devlink
,
1038 struct devlink_sb
*devlink_sb
,
1039 u32 portid
, u32 seq
)
1041 struct devlink_port
*devlink_port
;
1042 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
1046 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1047 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
1048 if (*p_idx
< start
) {
1052 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
,
1056 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1067 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff
*msg
,
1068 struct netlink_callback
*cb
)
1070 struct devlink
*devlink
;
1071 struct devlink_sb
*devlink_sb
;
1072 int start
= cb
->args
[0];
1076 mutex_lock(&devlink_mutex
);
1077 mutex_lock(&devlink_port_mutex
);
1078 list_for_each_entry(devlink
, &devlink_list
, list
) {
1079 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1080 !devlink
->ops
|| !devlink
->ops
->sb_port_pool_get
)
1082 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1083 err
= __sb_port_pool_get_dumpit(msg
, start
, &idx
,
1084 devlink
, devlink_sb
,
1085 NETLINK_CB(cb
->skb
).portid
,
1086 cb
->nlh
->nlmsg_seq
);
1087 if (err
&& err
!= -EOPNOTSUPP
)
1092 mutex_unlock(&devlink_port_mutex
);
1093 mutex_unlock(&devlink_mutex
);
1099 static int devlink_sb_port_pool_set(struct devlink_port
*devlink_port
,
1100 unsigned int sb_index
, u16 pool_index
,
1104 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1106 if (ops
&& ops
->sb_port_pool_set
)
1107 return ops
->sb_port_pool_set(devlink_port
, sb_index
,
1108 pool_index
, threshold
);
1112 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff
*skb
,
1113 struct genl_info
*info
)
1115 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1116 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1121 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1126 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1129 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1130 return devlink_sb_port_pool_set(devlink_port
, devlink_sb
->index
,
1131 pool_index
, threshold
);
1135 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1136 struct devlink_port
*devlink_port
,
1137 struct devlink_sb
*devlink_sb
, u16 tc_index
,
1138 enum devlink_sb_pool_type pool_type
,
1139 enum devlink_command cmd
,
1140 u32 portid
, u32 seq
, int flags
)
1142 const struct devlink_ops
*ops
= devlink
->ops
;
1148 err
= ops
->sb_tc_pool_bind_get(devlink_port
, devlink_sb
->index
,
1149 tc_index
, pool_type
,
1150 &pool_index
, &threshold
);
1154 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1158 if (devlink_nl_put_handle(msg
, devlink
))
1159 goto nla_put_failure
;
1160 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1161 goto nla_put_failure
;
1162 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1163 goto nla_put_failure
;
1164 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_TC_INDEX
, tc_index
))
1165 goto nla_put_failure
;
1166 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_type
))
1167 goto nla_put_failure
;
1168 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1169 goto nla_put_failure
;
1170 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1171 goto nla_put_failure
;
1173 if (ops
->sb_occ_tc_port_bind_get
) {
1177 err
= ops
->sb_occ_tc_port_bind_get(devlink_port
,
1179 tc_index
, pool_type
,
1181 if (err
&& err
!= -EOPNOTSUPP
)
1184 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1185 goto nla_put_failure
;
1186 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1187 goto nla_put_failure
;
1191 genlmsg_end(msg
, hdr
);
1195 genlmsg_cancel(msg
, hdr
);
1199 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff
*skb
,
1200 struct genl_info
*info
)
1202 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1203 struct devlink
*devlink
= devlink_port
->devlink
;
1204 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1205 struct sk_buff
*msg
;
1206 enum devlink_sb_pool_type pool_type
;
1210 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1214 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1215 pool_type
, &tc_index
);
1219 if (!devlink
->ops
|| !devlink
->ops
->sb_tc_pool_bind_get
)
1222 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1226 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
, devlink_port
,
1227 devlink_sb
, tc_index
, pool_type
,
1228 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1236 return genlmsg_reply(msg
, info
);
1239 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1240 int start
, int *p_idx
,
1241 struct devlink
*devlink
,
1242 struct devlink_sb
*devlink_sb
,
1243 u32 portid
, u32 seq
)
1245 struct devlink_port
*devlink_port
;
1249 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1251 tc_index
< devlink_sb
->ingress_tc_count
; tc_index
++) {
1252 if (*p_idx
< start
) {
1256 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1260 DEVLINK_SB_POOL_TYPE_INGRESS
,
1261 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1269 tc_index
< devlink_sb
->egress_tc_count
; tc_index
++) {
1270 if (*p_idx
< start
) {
1274 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1278 DEVLINK_SB_POOL_TYPE_EGRESS
,
1279 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1291 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1292 struct netlink_callback
*cb
)
1294 struct devlink
*devlink
;
1295 struct devlink_sb
*devlink_sb
;
1296 int start
= cb
->args
[0];
1300 mutex_lock(&devlink_mutex
);
1301 mutex_lock(&devlink_port_mutex
);
1302 list_for_each_entry(devlink
, &devlink_list
, list
) {
1303 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1304 !devlink
->ops
|| !devlink
->ops
->sb_tc_pool_bind_get
)
1306 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1307 err
= __sb_tc_pool_bind_get_dumpit(msg
, start
, &idx
,
1310 NETLINK_CB(cb
->skb
).portid
,
1311 cb
->nlh
->nlmsg_seq
);
1312 if (err
&& err
!= -EOPNOTSUPP
)
1317 mutex_unlock(&devlink_port_mutex
);
1318 mutex_unlock(&devlink_mutex
);
1324 static int devlink_sb_tc_pool_bind_set(struct devlink_port
*devlink_port
,
1325 unsigned int sb_index
, u16 tc_index
,
1326 enum devlink_sb_pool_type pool_type
,
1327 u16 pool_index
, u32 threshold
)
1330 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1332 if (ops
&& ops
->sb_tc_pool_bind_set
)
1333 return ops
->sb_tc_pool_bind_set(devlink_port
, sb_index
,
1334 tc_index
, pool_type
,
1335 pool_index
, threshold
);
1339 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff
*skb
,
1340 struct genl_info
*info
)
1342 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1343 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1344 enum devlink_sb_pool_type pool_type
;
1350 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1354 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1355 pool_type
, &tc_index
);
1359 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1364 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1367 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1368 return devlink_sb_tc_pool_bind_set(devlink_port
, devlink_sb
->index
,
1369 tc_index
, pool_type
,
1370 pool_index
, threshold
);
1373 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff
*skb
,
1374 struct genl_info
*info
)
1376 struct devlink
*devlink
= info
->user_ptr
[0];
1377 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1378 const struct devlink_ops
*ops
= devlink
->ops
;
1380 if (ops
&& ops
->sb_occ_snapshot
)
1381 return ops
->sb_occ_snapshot(devlink
, devlink_sb
->index
);
1385 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff
*skb
,
1386 struct genl_info
*info
)
1388 struct devlink
*devlink
= info
->user_ptr
[0];
1389 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1390 const struct devlink_ops
*ops
= devlink
->ops
;
1392 if (ops
&& ops
->sb_occ_max_clear
)
1393 return ops
->sb_occ_max_clear(devlink
, devlink_sb
->index
);
1397 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
1398 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
1399 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
1400 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
1401 [DEVLINK_ATTR_PORT_TYPE
] = { .type
= NLA_U16
},
1402 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
1403 [DEVLINK_ATTR_SB_INDEX
] = { .type
= NLA_U32
},
1404 [DEVLINK_ATTR_SB_POOL_INDEX
] = { .type
= NLA_U16
},
1405 [DEVLINK_ATTR_SB_POOL_TYPE
] = { .type
= NLA_U8
},
1406 [DEVLINK_ATTR_SB_POOL_SIZE
] = { .type
= NLA_U32
},
1407 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = { .type
= NLA_U8
},
1408 [DEVLINK_ATTR_SB_THRESHOLD
] = { .type
= NLA_U32
},
1409 [DEVLINK_ATTR_SB_TC_INDEX
] = { .type
= NLA_U16
},
1412 static const struct genl_ops devlink_nl_ops
[] = {
1414 .cmd
= DEVLINK_CMD_GET
,
1415 .doit
= devlink_nl_cmd_get_doit
,
1416 .dumpit
= devlink_nl_cmd_get_dumpit
,
1417 .policy
= devlink_nl_policy
,
1418 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
1419 /* can be retrieved by unprivileged users */
1422 .cmd
= DEVLINK_CMD_PORT_GET
,
1423 .doit
= devlink_nl_cmd_port_get_doit
,
1424 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
1425 .policy
= devlink_nl_policy
,
1426 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
1427 /* can be retrieved by unprivileged users */
1430 .cmd
= DEVLINK_CMD_PORT_SET
,
1431 .doit
= devlink_nl_cmd_port_set_doit
,
1432 .policy
= devlink_nl_policy
,
1433 .flags
= GENL_ADMIN_PERM
,
1434 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
1437 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
1438 .doit
= devlink_nl_cmd_port_split_doit
,
1439 .policy
= devlink_nl_policy
,
1440 .flags
= GENL_ADMIN_PERM
,
1441 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
1444 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
1445 .doit
= devlink_nl_cmd_port_unsplit_doit
,
1446 .policy
= devlink_nl_policy
,
1447 .flags
= GENL_ADMIN_PERM
,
1448 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
1451 .cmd
= DEVLINK_CMD_SB_GET
,
1452 .doit
= devlink_nl_cmd_sb_get_doit
,
1453 .dumpit
= devlink_nl_cmd_sb_get_dumpit
,
1454 .policy
= devlink_nl_policy
,
1455 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
1456 DEVLINK_NL_FLAG_NEED_SB
,
1457 /* can be retrieved by unprivileged users */
1460 .cmd
= DEVLINK_CMD_SB_POOL_GET
,
1461 .doit
= devlink_nl_cmd_sb_pool_get_doit
,
1462 .dumpit
= devlink_nl_cmd_sb_pool_get_dumpit
,
1463 .policy
= devlink_nl_policy
,
1464 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
1465 DEVLINK_NL_FLAG_NEED_SB
,
1466 /* can be retrieved by unprivileged users */
1469 .cmd
= DEVLINK_CMD_SB_POOL_SET
,
1470 .doit
= devlink_nl_cmd_sb_pool_set_doit
,
1471 .policy
= devlink_nl_policy
,
1472 .flags
= GENL_ADMIN_PERM
,
1473 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
1474 DEVLINK_NL_FLAG_NEED_SB
,
1477 .cmd
= DEVLINK_CMD_SB_PORT_POOL_GET
,
1478 .doit
= devlink_nl_cmd_sb_port_pool_get_doit
,
1479 .dumpit
= devlink_nl_cmd_sb_port_pool_get_dumpit
,
1480 .policy
= devlink_nl_policy
,
1481 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
1482 DEVLINK_NL_FLAG_NEED_SB
,
1483 /* can be retrieved by unprivileged users */
1486 .cmd
= DEVLINK_CMD_SB_PORT_POOL_SET
,
1487 .doit
= devlink_nl_cmd_sb_port_pool_set_doit
,
1488 .policy
= devlink_nl_policy
,
1489 .flags
= GENL_ADMIN_PERM
,
1490 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
1491 DEVLINK_NL_FLAG_NEED_SB
,
1494 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_GET
,
1495 .doit
= devlink_nl_cmd_sb_tc_pool_bind_get_doit
,
1496 .dumpit
= devlink_nl_cmd_sb_tc_pool_bind_get_dumpit
,
1497 .policy
= devlink_nl_policy
,
1498 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
1499 DEVLINK_NL_FLAG_NEED_SB
,
1500 /* can be retrieved by unprivileged users */
1503 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
1504 .doit
= devlink_nl_cmd_sb_tc_pool_bind_set_doit
,
1505 .policy
= devlink_nl_policy
,
1506 .flags
= GENL_ADMIN_PERM
,
1507 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
1508 DEVLINK_NL_FLAG_NEED_SB
,
1511 .cmd
= DEVLINK_CMD_SB_OCC_SNAPSHOT
,
1512 .doit
= devlink_nl_cmd_sb_occ_snapshot_doit
,
1513 .policy
= devlink_nl_policy
,
1514 .flags
= GENL_ADMIN_PERM
,
1515 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
1516 DEVLINK_NL_FLAG_NEED_SB
|
1517 DEVLINK_NL_FLAG_LOCK_PORTS
,
1520 .cmd
= DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
1521 .doit
= devlink_nl_cmd_sb_occ_max_clear_doit
,
1522 .policy
= devlink_nl_policy
,
1523 .flags
= GENL_ADMIN_PERM
,
1524 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
1525 DEVLINK_NL_FLAG_NEED_SB
|
1526 DEVLINK_NL_FLAG_LOCK_PORTS
,
1531 * devlink_alloc - Allocate new devlink instance resources
1534 * @priv_size: size of user private data
1536 * Allocate new devlink instance resources, including devlink index
1539 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
1541 struct devlink
*devlink
;
1543 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
1547 devlink_net_set(devlink
, &init_net
);
1548 INIT_LIST_HEAD(&devlink
->port_list
);
1549 INIT_LIST_HEAD(&devlink
->sb_list
);
1552 EXPORT_SYMBOL_GPL(devlink_alloc
);
1555 * devlink_register - Register devlink instance
1559 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
1561 mutex_lock(&devlink_mutex
);
1563 list_add_tail(&devlink
->list
, &devlink_list
);
1564 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
1565 mutex_unlock(&devlink_mutex
);
1568 EXPORT_SYMBOL_GPL(devlink_register
);
1571 * devlink_unregister - Unregister devlink instance
1575 void devlink_unregister(struct devlink
*devlink
)
1577 mutex_lock(&devlink_mutex
);
1578 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
1579 list_del(&devlink
->list
);
1580 mutex_unlock(&devlink_mutex
);
1582 EXPORT_SYMBOL_GPL(devlink_unregister
);
1585 * devlink_free - Free devlink instance resources
1589 void devlink_free(struct devlink
*devlink
)
1593 EXPORT_SYMBOL_GPL(devlink_free
);
1596 * devlink_port_register - Register devlink port
1599 * @devlink_port: devlink port
1602 * Register devlink port with provided port index. User can use
1603 * any indexing, even hw-related one. devlink_port structure
1604 * is convenient to be embedded inside user driver private structure.
1605 * Note that the caller should take care of zeroing the devlink_port
1608 int devlink_port_register(struct devlink
*devlink
,
1609 struct devlink_port
*devlink_port
,
1610 unsigned int port_index
)
1612 mutex_lock(&devlink_port_mutex
);
1613 if (devlink_port_index_exists(devlink
, port_index
)) {
1614 mutex_unlock(&devlink_port_mutex
);
1617 devlink_port
->devlink
= devlink
;
1618 devlink_port
->index
= port_index
;
1619 devlink_port
->registered
= true;
1620 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
1621 mutex_unlock(&devlink_port_mutex
);
1622 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
1625 EXPORT_SYMBOL_GPL(devlink_port_register
);
1628 * devlink_port_unregister - Unregister devlink port
1630 * @devlink_port: devlink port
1632 void devlink_port_unregister(struct devlink_port
*devlink_port
)
1634 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
1635 mutex_lock(&devlink_port_mutex
);
1636 list_del(&devlink_port
->list
);
1637 mutex_unlock(&devlink_port_mutex
);
1639 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
1641 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
1642 enum devlink_port_type type
,
1645 devlink_port
->type
= type
;
1646 devlink_port
->type_dev
= type_dev
;
1647 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
1651 * devlink_port_type_eth_set - Set port type to Ethernet
1653 * @devlink_port: devlink port
1654 * @netdev: related netdevice
1656 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
1657 struct net_device
*netdev
)
1659 return __devlink_port_type_set(devlink_port
,
1660 DEVLINK_PORT_TYPE_ETH
, netdev
);
1662 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
1665 * devlink_port_type_ib_set - Set port type to InfiniBand
1667 * @devlink_port: devlink port
1668 * @ibdev: related IB device
1670 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
1671 struct ib_device
*ibdev
)
1673 return __devlink_port_type_set(devlink_port
,
1674 DEVLINK_PORT_TYPE_IB
, ibdev
);
1676 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
1679 * devlink_port_type_clear - Clear port type
1681 * @devlink_port: devlink port
1683 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
1685 return __devlink_port_type_set(devlink_port
,
1686 DEVLINK_PORT_TYPE_NOTSET
, NULL
);
1688 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
1691 * devlink_port_split_set - Set port is split
1693 * @devlink_port: devlink port
1694 * @split_group: split group - identifies group split port is part of
1696 void devlink_port_split_set(struct devlink_port
*devlink_port
,
1699 devlink_port
->split
= true;
1700 devlink_port
->split_group
= split_group
;
1701 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
1703 EXPORT_SYMBOL_GPL(devlink_port_split_set
);
1705 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
1706 u32 size
, u16 ingress_pools_count
,
1707 u16 egress_pools_count
, u16 ingress_tc_count
,
1708 u16 egress_tc_count
)
1710 struct devlink_sb
*devlink_sb
;
1713 mutex_lock(&devlink_mutex
);
1714 if (devlink_sb_index_exists(devlink
, sb_index
)) {
1719 devlink_sb
= kzalloc(sizeof(*devlink_sb
), GFP_KERNEL
);
1724 devlink_sb
->index
= sb_index
;
1725 devlink_sb
->size
= size
;
1726 devlink_sb
->ingress_pools_count
= ingress_pools_count
;
1727 devlink_sb
->egress_pools_count
= egress_pools_count
;
1728 devlink_sb
->ingress_tc_count
= ingress_tc_count
;
1729 devlink_sb
->egress_tc_count
= egress_tc_count
;
1730 list_add_tail(&devlink_sb
->list
, &devlink
->sb_list
);
1732 mutex_unlock(&devlink_mutex
);
1735 EXPORT_SYMBOL_GPL(devlink_sb_register
);
1737 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
)
1739 struct devlink_sb
*devlink_sb
;
1741 mutex_lock(&devlink_mutex
);
1742 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
1743 WARN_ON(!devlink_sb
);
1744 list_del(&devlink_sb
->list
);
1745 mutex_unlock(&devlink_mutex
);
1748 EXPORT_SYMBOL_GPL(devlink_sb_unregister
);
1750 static int __init
devlink_module_init(void)
1752 return genl_register_family_with_ops_groups(&devlink_nl_family
,
1757 static void __exit
devlink_module_exit(void)
1759 genl_unregister_family(&devlink_nl_family
);
1762 module_init(devlink_module_init
);
1763 module_exit(devlink_module_exit
);
1765 MODULE_LICENSE("GPL v2");
1766 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1767 MODULE_DESCRIPTION("Network physical device Netlink interface");
1768 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME
);