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>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/devlink.h>
32 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet
[] = {
34 .name
= "destination mac",
35 .id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
40 struct devlink_dpipe_header devlink_dpipe_header_ethernet
= {
42 .id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
43 .fields
= devlink_dpipe_fields_ethernet
,
44 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ethernet
),
47 EXPORT_SYMBOL(devlink_dpipe_header_ethernet
);
49 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4
[] = {
51 .name
= "destination ip",
52 .id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
57 struct devlink_dpipe_header devlink_dpipe_header_ipv4
= {
59 .id
= DEVLINK_DPIPE_HEADER_IPV4
,
60 .fields
= devlink_dpipe_fields_ipv4
,
61 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ipv4
),
64 EXPORT_SYMBOL(devlink_dpipe_header_ipv4
);
66 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6
[] = {
68 .name
= "destination ip",
69 .id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
74 struct devlink_dpipe_header devlink_dpipe_header_ipv6
= {
76 .id
= DEVLINK_DPIPE_HEADER_IPV6
,
77 .fields
= devlink_dpipe_fields_ipv6
,
78 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ipv6
),
81 EXPORT_SYMBOL(devlink_dpipe_header_ipv6
);
83 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg
);
85 static LIST_HEAD(devlink_list
);
89 * An overall lock guarding every operation coming from userspace.
90 * It also guards devlink devices list and it is taken when
91 * driver registers/unregisters it.
93 static DEFINE_MUTEX(devlink_mutex
);
97 * Shared lock to guard lists of ports in all devlink devices.
99 static DEFINE_MUTEX(devlink_port_mutex
);
101 static struct net
*devlink_net(const struct devlink
*devlink
)
103 return read_pnet(&devlink
->_net
);
106 static void devlink_net_set(struct devlink
*devlink
, struct net
*net
)
108 write_pnet(&devlink
->_net
, net
);
111 static struct devlink
*devlink_get_from_attrs(struct net
*net
,
112 struct nlattr
**attrs
)
114 struct devlink
*devlink
;
118 if (!attrs
[DEVLINK_ATTR_BUS_NAME
] || !attrs
[DEVLINK_ATTR_DEV_NAME
])
119 return ERR_PTR(-EINVAL
);
121 busname
= nla_data(attrs
[DEVLINK_ATTR_BUS_NAME
]);
122 devname
= nla_data(attrs
[DEVLINK_ATTR_DEV_NAME
]);
124 list_for_each_entry(devlink
, &devlink_list
, list
) {
125 if (strcmp(devlink
->dev
->bus
->name
, busname
) == 0 &&
126 strcmp(dev_name(devlink
->dev
), devname
) == 0 &&
127 net_eq(devlink_net(devlink
), net
))
131 return ERR_PTR(-ENODEV
);
134 static struct devlink
*devlink_get_from_info(struct genl_info
*info
)
136 return devlink_get_from_attrs(genl_info_net(info
), info
->attrs
);
139 static struct devlink_port
*devlink_port_get_by_index(struct devlink
*devlink
,
142 struct devlink_port
*devlink_port
;
144 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
145 if (devlink_port
->index
== port_index
)
151 static bool devlink_port_index_exists(struct devlink
*devlink
, int port_index
)
153 return devlink_port_get_by_index(devlink
, port_index
);
156 static struct devlink_port
*devlink_port_get_from_attrs(struct devlink
*devlink
,
157 struct nlattr
**attrs
)
159 if (attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
160 u32 port_index
= nla_get_u32(attrs
[DEVLINK_ATTR_PORT_INDEX
]);
161 struct devlink_port
*devlink_port
;
163 devlink_port
= devlink_port_get_by_index(devlink
, port_index
);
165 return ERR_PTR(-ENODEV
);
168 return ERR_PTR(-EINVAL
);
171 static struct devlink_port
*devlink_port_get_from_info(struct devlink
*devlink
,
172 struct genl_info
*info
)
174 return devlink_port_get_from_attrs(devlink
, info
->attrs
);
178 struct list_head list
;
181 u16 ingress_pools_count
;
182 u16 egress_pools_count
;
183 u16 ingress_tc_count
;
187 static u16
devlink_sb_pool_count(struct devlink_sb
*devlink_sb
)
189 return devlink_sb
->ingress_pools_count
+ devlink_sb
->egress_pools_count
;
192 static struct devlink_sb
*devlink_sb_get_by_index(struct devlink
*devlink
,
193 unsigned int sb_index
)
195 struct devlink_sb
*devlink_sb
;
197 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
198 if (devlink_sb
->index
== sb_index
)
204 static bool devlink_sb_index_exists(struct devlink
*devlink
,
205 unsigned int sb_index
)
207 return devlink_sb_get_by_index(devlink
, sb_index
);
210 static struct devlink_sb
*devlink_sb_get_from_attrs(struct devlink
*devlink
,
211 struct nlattr
**attrs
)
213 if (attrs
[DEVLINK_ATTR_SB_INDEX
]) {
214 u32 sb_index
= nla_get_u32(attrs
[DEVLINK_ATTR_SB_INDEX
]);
215 struct devlink_sb
*devlink_sb
;
217 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
219 return ERR_PTR(-ENODEV
);
222 return ERR_PTR(-EINVAL
);
225 static struct devlink_sb
*devlink_sb_get_from_info(struct devlink
*devlink
,
226 struct genl_info
*info
)
228 return devlink_sb_get_from_attrs(devlink
, info
->attrs
);
231 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
232 struct nlattr
**attrs
,
237 if (!attrs
[DEVLINK_ATTR_SB_POOL_INDEX
])
240 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_POOL_INDEX
]);
241 if (val
>= devlink_sb_pool_count(devlink_sb
))
247 static int devlink_sb_pool_index_get_from_info(struct devlink_sb
*devlink_sb
,
248 struct genl_info
*info
,
251 return devlink_sb_pool_index_get_from_attrs(devlink_sb
, info
->attrs
,
256 devlink_sb_pool_type_get_from_attrs(struct nlattr
**attrs
,
257 enum devlink_sb_pool_type
*p_pool_type
)
261 if (!attrs
[DEVLINK_ATTR_SB_POOL_TYPE
])
264 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_TYPE
]);
265 if (val
!= DEVLINK_SB_POOL_TYPE_INGRESS
&&
266 val
!= DEVLINK_SB_POOL_TYPE_EGRESS
)
273 devlink_sb_pool_type_get_from_info(struct genl_info
*info
,
274 enum devlink_sb_pool_type
*p_pool_type
)
276 return devlink_sb_pool_type_get_from_attrs(info
->attrs
, p_pool_type
);
280 devlink_sb_th_type_get_from_attrs(struct nlattr
**attrs
,
281 enum devlink_sb_threshold_type
*p_th_type
)
285 if (!attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
288 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
]);
289 if (val
!= DEVLINK_SB_THRESHOLD_TYPE_STATIC
&&
290 val
!= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
)
297 devlink_sb_th_type_get_from_info(struct genl_info
*info
,
298 enum devlink_sb_threshold_type
*p_th_type
)
300 return devlink_sb_th_type_get_from_attrs(info
->attrs
, p_th_type
);
304 devlink_sb_tc_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
305 struct nlattr
**attrs
,
306 enum devlink_sb_pool_type pool_type
,
311 if (!attrs
[DEVLINK_ATTR_SB_TC_INDEX
])
314 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_TC_INDEX
]);
315 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
&&
316 val
>= devlink_sb
->ingress_tc_count
)
318 if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
&&
319 val
>= devlink_sb
->egress_tc_count
)
326 devlink_sb_tc_index_get_from_info(struct devlink_sb
*devlink_sb
,
327 struct genl_info
*info
,
328 enum devlink_sb_pool_type pool_type
,
331 return devlink_sb_tc_index_get_from_attrs(devlink_sb
, info
->attrs
,
332 pool_type
, p_tc_index
);
335 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
336 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
337 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
338 #define DEVLINK_NL_FLAG_LOCK_PORTS BIT(3)
339 /* port is not needed but we need to ensure they don't
340 * change in the middle of command
343 static int devlink_nl_pre_doit(const struct genl_ops
*ops
,
344 struct sk_buff
*skb
, struct genl_info
*info
)
346 struct devlink
*devlink
;
348 mutex_lock(&devlink_mutex
);
349 devlink
= devlink_get_from_info(info
);
350 if (IS_ERR(devlink
)) {
351 mutex_unlock(&devlink_mutex
);
352 return PTR_ERR(devlink
);
354 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_DEVLINK
) {
355 info
->user_ptr
[0] = devlink
;
356 } else if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
) {
357 struct devlink_port
*devlink_port
;
359 mutex_lock(&devlink_port_mutex
);
360 devlink_port
= devlink_port_get_from_info(devlink
, info
);
361 if (IS_ERR(devlink_port
)) {
362 mutex_unlock(&devlink_port_mutex
);
363 mutex_unlock(&devlink_mutex
);
364 return PTR_ERR(devlink_port
);
366 info
->user_ptr
[0] = devlink_port
;
368 if (ops
->internal_flags
& DEVLINK_NL_FLAG_LOCK_PORTS
) {
369 mutex_lock(&devlink_port_mutex
);
371 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_SB
) {
372 struct devlink_sb
*devlink_sb
;
374 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
375 if (IS_ERR(devlink_sb
)) {
376 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
)
377 mutex_unlock(&devlink_port_mutex
);
378 mutex_unlock(&devlink_mutex
);
379 return PTR_ERR(devlink_sb
);
381 info
->user_ptr
[1] = devlink_sb
;
386 static void devlink_nl_post_doit(const struct genl_ops
*ops
,
387 struct sk_buff
*skb
, struct genl_info
*info
)
389 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
||
390 ops
->internal_flags
& DEVLINK_NL_FLAG_LOCK_PORTS
)
391 mutex_unlock(&devlink_port_mutex
);
392 mutex_unlock(&devlink_mutex
);
395 static struct genl_family devlink_nl_family
;
397 enum devlink_multicast_groups
{
398 DEVLINK_MCGRP_CONFIG
,
401 static const struct genl_multicast_group devlink_nl_mcgrps
[] = {
402 [DEVLINK_MCGRP_CONFIG
] = { .name
= DEVLINK_GENL_MCGRP_CONFIG_NAME
},
405 static int devlink_nl_put_handle(struct sk_buff
*msg
, struct devlink
*devlink
)
407 if (nla_put_string(msg
, DEVLINK_ATTR_BUS_NAME
, devlink
->dev
->bus
->name
))
409 if (nla_put_string(msg
, DEVLINK_ATTR_DEV_NAME
, dev_name(devlink
->dev
)))
414 static int devlink_nl_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
415 enum devlink_command cmd
, u32 portid
,
420 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
424 if (devlink_nl_put_handle(msg
, devlink
))
425 goto nla_put_failure
;
427 genlmsg_end(msg
, hdr
);
431 genlmsg_cancel(msg
, hdr
);
435 static void devlink_notify(struct devlink
*devlink
, enum devlink_command cmd
)
440 WARN_ON(cmd
!= DEVLINK_CMD_NEW
&& cmd
!= DEVLINK_CMD_DEL
);
442 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
446 err
= devlink_nl_fill(msg
, devlink
, cmd
, 0, 0, 0);
452 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
453 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
456 static int devlink_nl_port_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
457 struct devlink_port
*devlink_port
,
458 enum devlink_command cmd
, u32 portid
,
463 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
467 if (devlink_nl_put_handle(msg
, devlink
))
468 goto nla_put_failure
;
469 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
470 goto nla_put_failure
;
471 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_TYPE
, devlink_port
->type
))
472 goto nla_put_failure
;
473 if (devlink_port
->desired_type
!= DEVLINK_PORT_TYPE_NOTSET
&&
474 nla_put_u16(msg
, DEVLINK_ATTR_PORT_DESIRED_TYPE
,
475 devlink_port
->desired_type
))
476 goto nla_put_failure
;
477 if (devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
) {
478 struct net_device
*netdev
= devlink_port
->type_dev
;
481 (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NETDEV_IFINDEX
,
483 nla_put_string(msg
, DEVLINK_ATTR_PORT_NETDEV_NAME
,
485 goto nla_put_failure
;
487 if (devlink_port
->type
== DEVLINK_PORT_TYPE_IB
) {
488 struct ib_device
*ibdev
= devlink_port
->type_dev
;
491 nla_put_string(msg
, DEVLINK_ATTR_PORT_IBDEV_NAME
,
493 goto nla_put_failure
;
495 if (devlink_port
->split
&&
496 nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_GROUP
,
497 devlink_port
->split_group
))
498 goto nla_put_failure
;
500 genlmsg_end(msg
, hdr
);
504 genlmsg_cancel(msg
, hdr
);
508 static void devlink_port_notify(struct devlink_port
*devlink_port
,
509 enum devlink_command cmd
)
511 struct devlink
*devlink
= devlink_port
->devlink
;
515 if (!devlink_port
->registered
)
518 WARN_ON(cmd
!= DEVLINK_CMD_PORT_NEW
&& cmd
!= DEVLINK_CMD_PORT_DEL
);
520 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
524 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
, cmd
, 0, 0, 0);
530 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
531 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
534 static int devlink_nl_cmd_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
536 struct devlink
*devlink
= info
->user_ptr
[0];
540 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
544 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
545 info
->snd_portid
, info
->snd_seq
, 0);
551 return genlmsg_reply(msg
, info
);
554 static int devlink_nl_cmd_get_dumpit(struct sk_buff
*msg
,
555 struct netlink_callback
*cb
)
557 struct devlink
*devlink
;
558 int start
= cb
->args
[0];
562 mutex_lock(&devlink_mutex
);
563 list_for_each_entry(devlink
, &devlink_list
, list
) {
564 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
570 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
571 NETLINK_CB(cb
->skb
).portid
,
572 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
578 mutex_unlock(&devlink_mutex
);
584 static int devlink_nl_cmd_port_get_doit(struct sk_buff
*skb
,
585 struct genl_info
*info
)
587 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
588 struct devlink
*devlink
= devlink_port
->devlink
;
592 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
596 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
597 DEVLINK_CMD_PORT_NEW
,
598 info
->snd_portid
, info
->snd_seq
, 0);
604 return genlmsg_reply(msg
, info
);
607 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff
*msg
,
608 struct netlink_callback
*cb
)
610 struct devlink
*devlink
;
611 struct devlink_port
*devlink_port
;
612 int start
= cb
->args
[0];
616 mutex_lock(&devlink_mutex
);
617 mutex_lock(&devlink_port_mutex
);
618 list_for_each_entry(devlink
, &devlink_list
, list
) {
619 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
621 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
626 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
628 NETLINK_CB(cb
->skb
).portid
,
637 mutex_unlock(&devlink_port_mutex
);
638 mutex_unlock(&devlink_mutex
);
644 static int devlink_port_type_set(struct devlink
*devlink
,
645 struct devlink_port
*devlink_port
,
646 enum devlink_port_type port_type
)
651 if (devlink
->ops
&& devlink
->ops
->port_type_set
) {
652 if (port_type
== DEVLINK_PORT_TYPE_NOTSET
)
654 if (port_type
== devlink_port
->type
)
656 err
= devlink
->ops
->port_type_set(devlink_port
, port_type
);
659 devlink_port
->desired_type
= port_type
;
660 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
666 static int devlink_nl_cmd_port_set_doit(struct sk_buff
*skb
,
667 struct genl_info
*info
)
669 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
670 struct devlink
*devlink
= devlink_port
->devlink
;
673 if (info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]) {
674 enum devlink_port_type port_type
;
676 port_type
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]);
677 err
= devlink_port_type_set(devlink
, devlink_port
, port_type
);
684 static int devlink_port_split(struct devlink
*devlink
,
685 u32 port_index
, u32 count
)
688 if (devlink
->ops
&& devlink
->ops
->port_split
)
689 return devlink
->ops
->port_split(devlink
, port_index
, count
);
693 static int devlink_nl_cmd_port_split_doit(struct sk_buff
*skb
,
694 struct genl_info
*info
)
696 struct devlink
*devlink
= info
->user_ptr
[0];
700 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
] ||
701 !info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
])
704 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
705 count
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
]);
706 return devlink_port_split(devlink
, port_index
, count
);
709 static int devlink_port_unsplit(struct devlink
*devlink
, u32 port_index
)
712 if (devlink
->ops
&& devlink
->ops
->port_unsplit
)
713 return devlink
->ops
->port_unsplit(devlink
, port_index
);
717 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff
*skb
,
718 struct genl_info
*info
)
720 struct devlink
*devlink
= info
->user_ptr
[0];
723 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
])
726 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
727 return devlink_port_unsplit(devlink
, port_index
);
730 static int devlink_nl_sb_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
731 struct devlink_sb
*devlink_sb
,
732 enum devlink_command cmd
, u32 portid
,
737 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
741 if (devlink_nl_put_handle(msg
, devlink
))
742 goto nla_put_failure
;
743 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
744 goto nla_put_failure
;
745 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_SIZE
, devlink_sb
->size
))
746 goto nla_put_failure
;
747 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
,
748 devlink_sb
->ingress_pools_count
))
749 goto nla_put_failure
;
750 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
,
751 devlink_sb
->egress_pools_count
))
752 goto nla_put_failure
;
753 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_TC_COUNT
,
754 devlink_sb
->ingress_tc_count
))
755 goto nla_put_failure
;
756 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_TC_COUNT
,
757 devlink_sb
->egress_tc_count
))
758 goto nla_put_failure
;
760 genlmsg_end(msg
, hdr
);
764 genlmsg_cancel(msg
, hdr
);
768 static int devlink_nl_cmd_sb_get_doit(struct sk_buff
*skb
,
769 struct genl_info
*info
)
771 struct devlink
*devlink
= info
->user_ptr
[0];
772 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
776 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
780 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
782 info
->snd_portid
, info
->snd_seq
, 0);
788 return genlmsg_reply(msg
, info
);
791 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff
*msg
,
792 struct netlink_callback
*cb
)
794 struct devlink
*devlink
;
795 struct devlink_sb
*devlink_sb
;
796 int start
= cb
->args
[0];
800 mutex_lock(&devlink_mutex
);
801 list_for_each_entry(devlink
, &devlink_list
, list
) {
802 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
804 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
809 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
811 NETLINK_CB(cb
->skb
).portid
,
820 mutex_unlock(&devlink_mutex
);
826 static int devlink_nl_sb_pool_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
827 struct devlink_sb
*devlink_sb
,
828 u16 pool_index
, enum devlink_command cmd
,
829 u32 portid
, u32 seq
, int flags
)
831 struct devlink_sb_pool_info pool_info
;
835 err
= devlink
->ops
->sb_pool_get(devlink
, devlink_sb
->index
,
836 pool_index
, &pool_info
);
840 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
844 if (devlink_nl_put_handle(msg
, devlink
))
845 goto nla_put_failure
;
846 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
847 goto nla_put_failure
;
848 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
849 goto nla_put_failure
;
850 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_info
.pool_type
))
851 goto nla_put_failure
;
852 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_POOL_SIZE
, pool_info
.size
))
853 goto nla_put_failure
;
854 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
855 pool_info
.threshold_type
))
856 goto nla_put_failure
;
858 genlmsg_end(msg
, hdr
);
862 genlmsg_cancel(msg
, hdr
);
866 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff
*skb
,
867 struct genl_info
*info
)
869 struct devlink
*devlink
= info
->user_ptr
[0];
870 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
875 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
880 if (!devlink
->ops
|| !devlink
->ops
->sb_pool_get
)
883 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
887 err
= devlink_nl_sb_pool_fill(msg
, devlink
, devlink_sb
, pool_index
,
888 DEVLINK_CMD_SB_POOL_NEW
,
889 info
->snd_portid
, info
->snd_seq
, 0);
895 return genlmsg_reply(msg
, info
);
898 static int __sb_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
899 struct devlink
*devlink
,
900 struct devlink_sb
*devlink_sb
,
903 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
907 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
908 if (*p_idx
< start
) {
912 err
= devlink_nl_sb_pool_fill(msg
, devlink
,
915 DEVLINK_CMD_SB_POOL_NEW
,
916 portid
, seq
, NLM_F_MULTI
);
924 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff
*msg
,
925 struct netlink_callback
*cb
)
927 struct devlink
*devlink
;
928 struct devlink_sb
*devlink_sb
;
929 int start
= cb
->args
[0];
933 mutex_lock(&devlink_mutex
);
934 list_for_each_entry(devlink
, &devlink_list
, list
) {
935 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
936 !devlink
->ops
|| !devlink
->ops
->sb_pool_get
)
938 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
939 err
= __sb_pool_get_dumpit(msg
, start
, &idx
, devlink
,
941 NETLINK_CB(cb
->skb
).portid
,
943 if (err
&& err
!= -EOPNOTSUPP
)
948 mutex_unlock(&devlink_mutex
);
954 static int devlink_sb_pool_set(struct devlink
*devlink
, unsigned int sb_index
,
955 u16 pool_index
, u32 size
,
956 enum devlink_sb_threshold_type threshold_type
)
959 const struct devlink_ops
*ops
= devlink
->ops
;
961 if (ops
&& ops
->sb_pool_set
)
962 return ops
->sb_pool_set(devlink
, sb_index
, pool_index
,
963 size
, threshold_type
);
967 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff
*skb
,
968 struct genl_info
*info
)
970 struct devlink
*devlink
= info
->user_ptr
[0];
971 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
972 enum devlink_sb_threshold_type threshold_type
;
977 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
982 err
= devlink_sb_th_type_get_from_info(info
, &threshold_type
);
986 if (!info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
])
989 size
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
]);
990 return devlink_sb_pool_set(devlink
, devlink_sb
->index
,
991 pool_index
, size
, threshold_type
);
994 static int devlink_nl_sb_port_pool_fill(struct sk_buff
*msg
,
995 struct devlink
*devlink
,
996 struct devlink_port
*devlink_port
,
997 struct devlink_sb
*devlink_sb
,
999 enum devlink_command cmd
,
1000 u32 portid
, u32 seq
, int flags
)
1002 const struct devlink_ops
*ops
= devlink
->ops
;
1007 err
= ops
->sb_port_pool_get(devlink_port
, devlink_sb
->index
,
1008 pool_index
, &threshold
);
1012 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1016 if (devlink_nl_put_handle(msg
, devlink
))
1017 goto nla_put_failure
;
1018 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1019 goto nla_put_failure
;
1020 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1021 goto nla_put_failure
;
1022 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1023 goto nla_put_failure
;
1024 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1025 goto nla_put_failure
;
1027 if (ops
->sb_occ_port_pool_get
) {
1031 err
= ops
->sb_occ_port_pool_get(devlink_port
, devlink_sb
->index
,
1032 pool_index
, &cur
, &max
);
1033 if (err
&& err
!= -EOPNOTSUPP
)
1036 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1037 goto nla_put_failure
;
1038 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1039 goto nla_put_failure
;
1043 genlmsg_end(msg
, hdr
);
1047 genlmsg_cancel(msg
, hdr
);
1051 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff
*skb
,
1052 struct genl_info
*info
)
1054 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1055 struct devlink
*devlink
= devlink_port
->devlink
;
1056 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1057 struct sk_buff
*msg
;
1061 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1066 if (!devlink
->ops
|| !devlink
->ops
->sb_port_pool_get
)
1069 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1073 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
, devlink_port
,
1074 devlink_sb
, pool_index
,
1075 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1076 info
->snd_portid
, info
->snd_seq
, 0);
1082 return genlmsg_reply(msg
, info
);
1085 static int __sb_port_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
1086 struct devlink
*devlink
,
1087 struct devlink_sb
*devlink_sb
,
1088 u32 portid
, u32 seq
)
1090 struct devlink_port
*devlink_port
;
1091 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
1095 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1096 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
1097 if (*p_idx
< start
) {
1101 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
,
1105 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1116 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff
*msg
,
1117 struct netlink_callback
*cb
)
1119 struct devlink
*devlink
;
1120 struct devlink_sb
*devlink_sb
;
1121 int start
= cb
->args
[0];
1125 mutex_lock(&devlink_mutex
);
1126 mutex_lock(&devlink_port_mutex
);
1127 list_for_each_entry(devlink
, &devlink_list
, list
) {
1128 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1129 !devlink
->ops
|| !devlink
->ops
->sb_port_pool_get
)
1131 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1132 err
= __sb_port_pool_get_dumpit(msg
, start
, &idx
,
1133 devlink
, devlink_sb
,
1134 NETLINK_CB(cb
->skb
).portid
,
1135 cb
->nlh
->nlmsg_seq
);
1136 if (err
&& err
!= -EOPNOTSUPP
)
1141 mutex_unlock(&devlink_port_mutex
);
1142 mutex_unlock(&devlink_mutex
);
1148 static int devlink_sb_port_pool_set(struct devlink_port
*devlink_port
,
1149 unsigned int sb_index
, u16 pool_index
,
1153 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1155 if (ops
&& ops
->sb_port_pool_set
)
1156 return ops
->sb_port_pool_set(devlink_port
, sb_index
,
1157 pool_index
, threshold
);
1161 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff
*skb
,
1162 struct genl_info
*info
)
1164 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1165 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1170 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1175 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1178 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1179 return devlink_sb_port_pool_set(devlink_port
, devlink_sb
->index
,
1180 pool_index
, threshold
);
1184 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1185 struct devlink_port
*devlink_port
,
1186 struct devlink_sb
*devlink_sb
, u16 tc_index
,
1187 enum devlink_sb_pool_type pool_type
,
1188 enum devlink_command cmd
,
1189 u32 portid
, u32 seq
, int flags
)
1191 const struct devlink_ops
*ops
= devlink
->ops
;
1197 err
= ops
->sb_tc_pool_bind_get(devlink_port
, devlink_sb
->index
,
1198 tc_index
, pool_type
,
1199 &pool_index
, &threshold
);
1203 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1207 if (devlink_nl_put_handle(msg
, devlink
))
1208 goto nla_put_failure
;
1209 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1210 goto nla_put_failure
;
1211 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1212 goto nla_put_failure
;
1213 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_TC_INDEX
, tc_index
))
1214 goto nla_put_failure
;
1215 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_type
))
1216 goto nla_put_failure
;
1217 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1218 goto nla_put_failure
;
1219 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1220 goto nla_put_failure
;
1222 if (ops
->sb_occ_tc_port_bind_get
) {
1226 err
= ops
->sb_occ_tc_port_bind_get(devlink_port
,
1228 tc_index
, pool_type
,
1230 if (err
&& err
!= -EOPNOTSUPP
)
1233 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1234 goto nla_put_failure
;
1235 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1236 goto nla_put_failure
;
1240 genlmsg_end(msg
, hdr
);
1244 genlmsg_cancel(msg
, hdr
);
1248 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff
*skb
,
1249 struct genl_info
*info
)
1251 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1252 struct devlink
*devlink
= devlink_port
->devlink
;
1253 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1254 struct sk_buff
*msg
;
1255 enum devlink_sb_pool_type pool_type
;
1259 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1263 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1264 pool_type
, &tc_index
);
1268 if (!devlink
->ops
|| !devlink
->ops
->sb_tc_pool_bind_get
)
1271 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1275 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
, devlink_port
,
1276 devlink_sb
, tc_index
, pool_type
,
1277 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1285 return genlmsg_reply(msg
, info
);
1288 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1289 int start
, int *p_idx
,
1290 struct devlink
*devlink
,
1291 struct devlink_sb
*devlink_sb
,
1292 u32 portid
, u32 seq
)
1294 struct devlink_port
*devlink_port
;
1298 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1300 tc_index
< devlink_sb
->ingress_tc_count
; tc_index
++) {
1301 if (*p_idx
< start
) {
1305 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1309 DEVLINK_SB_POOL_TYPE_INGRESS
,
1310 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1318 tc_index
< devlink_sb
->egress_tc_count
; tc_index
++) {
1319 if (*p_idx
< start
) {
1323 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1327 DEVLINK_SB_POOL_TYPE_EGRESS
,
1328 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1340 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1341 struct netlink_callback
*cb
)
1343 struct devlink
*devlink
;
1344 struct devlink_sb
*devlink_sb
;
1345 int start
= cb
->args
[0];
1349 mutex_lock(&devlink_mutex
);
1350 mutex_lock(&devlink_port_mutex
);
1351 list_for_each_entry(devlink
, &devlink_list
, list
) {
1352 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1353 !devlink
->ops
|| !devlink
->ops
->sb_tc_pool_bind_get
)
1355 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1356 err
= __sb_tc_pool_bind_get_dumpit(msg
, start
, &idx
,
1359 NETLINK_CB(cb
->skb
).portid
,
1360 cb
->nlh
->nlmsg_seq
);
1361 if (err
&& err
!= -EOPNOTSUPP
)
1366 mutex_unlock(&devlink_port_mutex
);
1367 mutex_unlock(&devlink_mutex
);
1373 static int devlink_sb_tc_pool_bind_set(struct devlink_port
*devlink_port
,
1374 unsigned int sb_index
, u16 tc_index
,
1375 enum devlink_sb_pool_type pool_type
,
1376 u16 pool_index
, u32 threshold
)
1379 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1381 if (ops
&& ops
->sb_tc_pool_bind_set
)
1382 return ops
->sb_tc_pool_bind_set(devlink_port
, sb_index
,
1383 tc_index
, pool_type
,
1384 pool_index
, threshold
);
1388 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff
*skb
,
1389 struct genl_info
*info
)
1391 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1392 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1393 enum devlink_sb_pool_type pool_type
;
1399 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1403 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1404 pool_type
, &tc_index
);
1408 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1413 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1416 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1417 return devlink_sb_tc_pool_bind_set(devlink_port
, devlink_sb
->index
,
1418 tc_index
, pool_type
,
1419 pool_index
, threshold
);
1422 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff
*skb
,
1423 struct genl_info
*info
)
1425 struct devlink
*devlink
= info
->user_ptr
[0];
1426 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1427 const struct devlink_ops
*ops
= devlink
->ops
;
1429 if (ops
&& ops
->sb_occ_snapshot
)
1430 return ops
->sb_occ_snapshot(devlink
, devlink_sb
->index
);
1434 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff
*skb
,
1435 struct genl_info
*info
)
1437 struct devlink
*devlink
= info
->user_ptr
[0];
1438 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1439 const struct devlink_ops
*ops
= devlink
->ops
;
1441 if (ops
&& ops
->sb_occ_max_clear
)
1442 return ops
->sb_occ_max_clear(devlink
, devlink_sb
->index
);
1446 static int devlink_nl_eswitch_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1447 enum devlink_command cmd
, u32 portid
,
1450 const struct devlink_ops
*ops
= devlink
->ops
;
1451 u8 inline_mode
, encap_mode
;
1456 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1460 err
= devlink_nl_put_handle(msg
, devlink
);
1462 goto nla_put_failure
;
1464 if (ops
->eswitch_mode_get
) {
1465 err
= ops
->eswitch_mode_get(devlink
, &mode
);
1467 goto nla_put_failure
;
1468 err
= nla_put_u16(msg
, DEVLINK_ATTR_ESWITCH_MODE
, mode
);
1470 goto nla_put_failure
;
1473 if (ops
->eswitch_inline_mode_get
) {
1474 err
= ops
->eswitch_inline_mode_get(devlink
, &inline_mode
);
1476 goto nla_put_failure
;
1477 err
= nla_put_u8(msg
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1480 goto nla_put_failure
;
1483 if (ops
->eswitch_encap_mode_get
) {
1484 err
= ops
->eswitch_encap_mode_get(devlink
, &encap_mode
);
1486 goto nla_put_failure
;
1487 err
= nla_put_u8(msg
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
, encap_mode
);
1489 goto nla_put_failure
;
1492 genlmsg_end(msg
, hdr
);
1496 genlmsg_cancel(msg
, hdr
);
1500 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff
*skb
,
1501 struct genl_info
*info
)
1503 struct devlink
*devlink
= info
->user_ptr
[0];
1504 const struct devlink_ops
*ops
= devlink
->ops
;
1505 struct sk_buff
*msg
;
1511 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1515 err
= devlink_nl_eswitch_fill(msg
, devlink
, DEVLINK_CMD_ESWITCH_GET
,
1516 info
->snd_portid
, info
->snd_seq
, 0);
1523 return genlmsg_reply(msg
, info
);
1526 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff
*skb
,
1527 struct genl_info
*info
)
1529 struct devlink
*devlink
= info
->user_ptr
[0];
1530 const struct devlink_ops
*ops
= devlink
->ops
;
1531 u8 inline_mode
, encap_mode
;
1538 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_MODE
]) {
1539 if (!ops
->eswitch_mode_set
)
1541 mode
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_ESWITCH_MODE
]);
1542 err
= ops
->eswitch_mode_set(devlink
, mode
);
1547 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
1548 if (!ops
->eswitch_inline_mode_set
)
1550 inline_mode
= nla_get_u8(
1551 info
->attrs
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]);
1552 err
= ops
->eswitch_inline_mode_set(devlink
, inline_mode
);
1557 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1558 if (!ops
->eswitch_encap_mode_set
)
1560 encap_mode
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1561 err
= ops
->eswitch_encap_mode_set(devlink
, encap_mode
);
1569 int devlink_dpipe_match_put(struct sk_buff
*skb
,
1570 struct devlink_dpipe_match
*match
)
1572 struct devlink_dpipe_header
*header
= match
->header
;
1573 struct devlink_dpipe_field
*field
= &header
->fields
[match
->field_id
];
1574 struct nlattr
*match_attr
;
1576 match_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_MATCH
);
1580 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_MATCH_TYPE
, match
->type
) ||
1581 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_INDEX
, match
->header_index
) ||
1582 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
1583 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
1584 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
1585 goto nla_put_failure
;
1587 nla_nest_end(skb
, match_attr
);
1591 nla_nest_cancel(skb
, match_attr
);
1594 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put
);
1596 static int devlink_dpipe_matches_put(struct devlink_dpipe_table
*table
,
1597 struct sk_buff
*skb
)
1599 struct nlattr
*matches_attr
;
1601 matches_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_TABLE_MATCHES
);
1605 if (table
->table_ops
->matches_dump(table
->priv
, skb
))
1606 goto nla_put_failure
;
1608 nla_nest_end(skb
, matches_attr
);
1612 nla_nest_cancel(skb
, matches_attr
);
1616 int devlink_dpipe_action_put(struct sk_buff
*skb
,
1617 struct devlink_dpipe_action
*action
)
1619 struct devlink_dpipe_header
*header
= action
->header
;
1620 struct devlink_dpipe_field
*field
= &header
->fields
[action
->field_id
];
1621 struct nlattr
*action_attr
;
1623 action_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_ACTION
);
1627 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_ACTION_TYPE
, action
->type
) ||
1628 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_INDEX
, action
->header_index
) ||
1629 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
1630 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
1631 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
1632 goto nla_put_failure
;
1634 nla_nest_end(skb
, action_attr
);
1638 nla_nest_cancel(skb
, action_attr
);
1641 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put
);
1643 static int devlink_dpipe_actions_put(struct devlink_dpipe_table
*table
,
1644 struct sk_buff
*skb
)
1646 struct nlattr
*actions_attr
;
1648 actions_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
);
1652 if (table
->table_ops
->actions_dump(table
->priv
, skb
))
1653 goto nla_put_failure
;
1655 nla_nest_end(skb
, actions_attr
);
1659 nla_nest_cancel(skb
, actions_attr
);
1663 static int devlink_dpipe_table_put(struct sk_buff
*skb
,
1664 struct devlink_dpipe_table
*table
)
1666 struct nlattr
*table_attr
;
1669 table_size
= table
->table_ops
->size_get(table
->priv
);
1670 table_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_TABLE
);
1674 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_TABLE_NAME
, table
->name
) ||
1675 nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_SIZE
, table_size
,
1677 goto nla_put_failure
;
1678 if (nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1679 table
->counters_enabled
))
1680 goto nla_put_failure
;
1682 if (devlink_dpipe_matches_put(table
, skb
))
1683 goto nla_put_failure
;
1685 if (devlink_dpipe_actions_put(table
, skb
))
1686 goto nla_put_failure
;
1688 nla_nest_end(skb
, table_attr
);
1692 nla_nest_cancel(skb
, table_attr
);
1696 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff
**pskb
,
1697 struct genl_info
*info
)
1702 err
= genlmsg_reply(*pskb
, info
);
1706 *pskb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1712 static int devlink_dpipe_tables_fill(struct genl_info
*info
,
1713 enum devlink_command cmd
, int flags
,
1714 struct list_head
*dpipe_tables
,
1715 const char *table_name
)
1717 struct devlink
*devlink
= info
->user_ptr
[0];
1718 struct devlink_dpipe_table
*table
;
1719 struct nlattr
*tables_attr
;
1720 struct sk_buff
*skb
= NULL
;
1721 struct nlmsghdr
*nlh
;
1727 table
= list_first_entry(dpipe_tables
,
1728 struct devlink_dpipe_table
, list
);
1730 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
1734 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
1735 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
1741 if (devlink_nl_put_handle(skb
, devlink
))
1742 goto nla_put_failure
;
1743 tables_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_TABLES
);
1745 goto nla_put_failure
;
1749 list_for_each_entry_from(table
, dpipe_tables
, list
) {
1751 err
= devlink_dpipe_table_put(skb
, table
);
1759 if (!strcmp(table
->name
, table_name
)) {
1760 err
= devlink_dpipe_table_put(skb
, table
);
1768 nla_nest_end(skb
, tables_attr
);
1769 genlmsg_end(skb
, hdr
);
1774 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
1775 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
1777 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
1783 return genlmsg_reply(skb
, info
);
1788 genlmsg_cancel(skb
, hdr
);
1793 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff
*skb
,
1794 struct genl_info
*info
)
1796 struct devlink
*devlink
= info
->user_ptr
[0];
1797 const char *table_name
= NULL
;
1799 if (info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
1800 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
1802 return devlink_dpipe_tables_fill(info
, DEVLINK_CMD_DPIPE_TABLE_GET
, 0,
1803 &devlink
->dpipe_table_list
,
1807 static int devlink_dpipe_value_put(struct sk_buff
*skb
,
1808 struct devlink_dpipe_value
*value
)
1810 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE
,
1811 value
->value_size
, value
->value
))
1814 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE_MASK
,
1815 value
->value_size
, value
->mask
))
1817 if (value
->mapping_valid
)
1818 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_VALUE_MAPPING
,
1819 value
->mapping_value
))
1824 static int devlink_dpipe_action_value_put(struct sk_buff
*skb
,
1825 struct devlink_dpipe_value
*value
)
1829 if (devlink_dpipe_action_put(skb
, value
->action
))
1831 if (devlink_dpipe_value_put(skb
, value
))
1836 static int devlink_dpipe_action_values_put(struct sk_buff
*skb
,
1837 struct devlink_dpipe_value
*values
,
1838 unsigned int values_count
)
1840 struct nlattr
*action_attr
;
1844 for (i
= 0; i
< values_count
; i
++) {
1845 action_attr
= nla_nest_start(skb
,
1846 DEVLINK_ATTR_DPIPE_ACTION_VALUE
);
1849 err
= devlink_dpipe_action_value_put(skb
, &values
[i
]);
1851 goto err_action_value_put
;
1852 nla_nest_end(skb
, action_attr
);
1856 err_action_value_put
:
1857 nla_nest_cancel(skb
, action_attr
);
1861 static int devlink_dpipe_match_value_put(struct sk_buff
*skb
,
1862 struct devlink_dpipe_value
*value
)
1866 if (devlink_dpipe_match_put(skb
, value
->match
))
1868 if (devlink_dpipe_value_put(skb
, value
))
1873 static int devlink_dpipe_match_values_put(struct sk_buff
*skb
,
1874 struct devlink_dpipe_value
*values
,
1875 unsigned int values_count
)
1877 struct nlattr
*match_attr
;
1881 for (i
= 0; i
< values_count
; i
++) {
1882 match_attr
= nla_nest_start(skb
,
1883 DEVLINK_ATTR_DPIPE_MATCH_VALUE
);
1886 err
= devlink_dpipe_match_value_put(skb
, &values
[i
]);
1888 goto err_match_value_put
;
1889 nla_nest_end(skb
, match_attr
);
1893 err_match_value_put
:
1894 nla_nest_cancel(skb
, match_attr
);
1898 static int devlink_dpipe_entry_put(struct sk_buff
*skb
,
1899 struct devlink_dpipe_entry
*entry
)
1901 struct nlattr
*entry_attr
, *matches_attr
, *actions_attr
;
1904 entry_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_ENTRY
);
1908 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_INDEX
, entry
->index
,
1910 goto nla_put_failure
;
1911 if (entry
->counter_valid
)
1912 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
,
1913 entry
->counter
, DEVLINK_ATTR_PAD
))
1914 goto nla_put_failure
;
1916 matches_attr
= nla_nest_start(skb
,
1917 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
);
1919 goto nla_put_failure
;
1921 err
= devlink_dpipe_match_values_put(skb
, entry
->match_values
,
1922 entry
->match_values_count
);
1924 nla_nest_cancel(skb
, matches_attr
);
1925 goto err_match_values_put
;
1927 nla_nest_end(skb
, matches_attr
);
1929 actions_attr
= nla_nest_start(skb
,
1930 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
);
1932 goto nla_put_failure
;
1934 err
= devlink_dpipe_action_values_put(skb
, entry
->action_values
,
1935 entry
->action_values_count
);
1937 nla_nest_cancel(skb
, actions_attr
);
1938 goto err_action_values_put
;
1940 nla_nest_end(skb
, actions_attr
);
1942 nla_nest_end(skb
, entry_attr
);
1947 err_match_values_put
:
1948 err_action_values_put
:
1949 nla_nest_cancel(skb
, entry_attr
);
1953 static struct devlink_dpipe_table
*
1954 devlink_dpipe_table_find(struct list_head
*dpipe_tables
,
1955 const char *table_name
)
1957 struct devlink_dpipe_table
*table
;
1959 list_for_each_entry_rcu(table
, dpipe_tables
, list
) {
1960 if (!strcmp(table
->name
, table_name
))
1966 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx
*dump_ctx
)
1968 struct devlink
*devlink
;
1971 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
->skb
,
1976 dump_ctx
->hdr
= genlmsg_put(dump_ctx
->skb
,
1977 dump_ctx
->info
->snd_portid
,
1978 dump_ctx
->info
->snd_seq
,
1979 &devlink_nl_family
, NLM_F_MULTI
,
1982 goto nla_put_failure
;
1984 devlink
= dump_ctx
->info
->user_ptr
[0];
1985 if (devlink_nl_put_handle(dump_ctx
->skb
, devlink
))
1986 goto nla_put_failure
;
1987 dump_ctx
->nest
= nla_nest_start(dump_ctx
->skb
,
1988 DEVLINK_ATTR_DPIPE_ENTRIES
);
1989 if (!dump_ctx
->nest
)
1990 goto nla_put_failure
;
1994 genlmsg_cancel(dump_ctx
->skb
, dump_ctx
->hdr
);
1995 nlmsg_free(dump_ctx
->skb
);
1998 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare
);
2000 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx
*dump_ctx
,
2001 struct devlink_dpipe_entry
*entry
)
2003 return devlink_dpipe_entry_put(dump_ctx
->skb
, entry
);
2005 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append
);
2007 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx
*dump_ctx
)
2009 nla_nest_end(dump_ctx
->skb
, dump_ctx
->nest
);
2010 genlmsg_end(dump_ctx
->skb
, dump_ctx
->hdr
);
2013 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close
);
2015 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry
*entry
)
2018 unsigned int value_count
, value_index
;
2019 struct devlink_dpipe_value
*value
;
2021 value
= entry
->action_values
;
2022 value_count
= entry
->action_values_count
;
2023 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2024 kfree(value
[value_index
].value
);
2025 kfree(value
[value_index
].mask
);
2028 value
= entry
->match_values
;
2029 value_count
= entry
->match_values_count
;
2030 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2031 kfree(value
[value_index
].value
);
2032 kfree(value
[value_index
].mask
);
2035 EXPORT_SYMBOL(devlink_dpipe_entry_clear
);
2037 static int devlink_dpipe_entries_fill(struct genl_info
*info
,
2038 enum devlink_command cmd
, int flags
,
2039 struct devlink_dpipe_table
*table
)
2041 struct devlink_dpipe_dump_ctx dump_ctx
;
2042 struct nlmsghdr
*nlh
;
2045 dump_ctx
.skb
= NULL
;
2047 dump_ctx
.info
= info
;
2049 err
= table
->table_ops
->entries_dump(table
->priv
,
2050 table
->counters_enabled
,
2056 nlh
= nlmsg_put(dump_ctx
.skb
, info
->snd_portid
, info
->snd_seq
,
2057 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2059 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
.skb
, info
);
2064 return genlmsg_reply(dump_ctx
.skb
, info
);
2067 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff
*skb
,
2068 struct genl_info
*info
)
2070 struct devlink
*devlink
= info
->user_ptr
[0];
2071 struct devlink_dpipe_table
*table
;
2072 const char *table_name
;
2074 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
2077 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2078 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2083 if (!table
->table_ops
->entries_dump
)
2086 return devlink_dpipe_entries_fill(info
, DEVLINK_CMD_DPIPE_ENTRIES_GET
,
2090 static int devlink_dpipe_fields_put(struct sk_buff
*skb
,
2091 const struct devlink_dpipe_header
*header
)
2093 struct devlink_dpipe_field
*field
;
2094 struct nlattr
*field_attr
;
2097 for (i
= 0; i
< header
->fields_count
; i
++) {
2098 field
= &header
->fields
[i
];
2099 field_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_FIELD
);
2102 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_FIELD_NAME
, field
->name
) ||
2103 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
2104 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
, field
->bitwidth
) ||
2105 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
, field
->mapping_type
))
2106 goto nla_put_failure
;
2107 nla_nest_end(skb
, field_attr
);
2112 nla_nest_cancel(skb
, field_attr
);
2116 static int devlink_dpipe_header_put(struct sk_buff
*skb
,
2117 struct devlink_dpipe_header
*header
)
2119 struct nlattr
*fields_attr
, *header_attr
;
2122 header_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_HEADER
);
2126 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_HEADER_NAME
, header
->name
) ||
2127 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
2128 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
2129 goto nla_put_failure
;
2131 fields_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_HEADER_FIELDS
);
2133 goto nla_put_failure
;
2135 err
= devlink_dpipe_fields_put(skb
, header
);
2137 nla_nest_cancel(skb
, fields_attr
);
2138 goto nla_put_failure
;
2140 nla_nest_end(skb
, fields_attr
);
2141 nla_nest_end(skb
, header_attr
);
2146 nla_nest_cancel(skb
, header_attr
);
2150 static int devlink_dpipe_headers_fill(struct genl_info
*info
,
2151 enum devlink_command cmd
, int flags
,
2152 struct devlink_dpipe_headers
*
2155 struct devlink
*devlink
= info
->user_ptr
[0];
2156 struct nlattr
*headers_attr
;
2157 struct sk_buff
*skb
= NULL
;
2158 struct nlmsghdr
*nlh
;
2165 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2169 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2170 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2176 if (devlink_nl_put_handle(skb
, devlink
))
2177 goto nla_put_failure
;
2178 headers_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_HEADERS
);
2180 goto nla_put_failure
;
2183 for (; i
< dpipe_headers
->headers_count
; i
++) {
2184 err
= devlink_dpipe_header_put(skb
, dpipe_headers
->headers
[i
]);
2192 nla_nest_end(skb
, headers_attr
);
2193 genlmsg_end(skb
, hdr
);
2194 if (i
!= dpipe_headers
->headers_count
)
2198 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2199 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2201 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2206 return genlmsg_reply(skb
, info
);
2211 genlmsg_cancel(skb
, hdr
);
2216 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff
*skb
,
2217 struct genl_info
*info
)
2219 struct devlink
*devlink
= info
->user_ptr
[0];
2221 if (!devlink
->dpipe_headers
)
2223 return devlink_dpipe_headers_fill(info
, DEVLINK_CMD_DPIPE_HEADERS_GET
,
2224 0, devlink
->dpipe_headers
);
2227 static int devlink_dpipe_table_counters_set(struct devlink
*devlink
,
2228 const char *table_name
,
2231 struct devlink_dpipe_table
*table
;
2233 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2238 if (table
->counter_control_extern
)
2241 if (!(table
->counters_enabled
^ enable
))
2244 table
->counters_enabled
= enable
;
2245 if (table
->table_ops
->counters_set_update
)
2246 table
->table_ops
->counters_set_update(table
->priv
, enable
);
2250 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff
*skb
,
2251 struct genl_info
*info
)
2253 struct devlink
*devlink
= info
->user_ptr
[0];
2254 const char *table_name
;
2255 bool counters_enable
;
2257 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
2258 !info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
])
2261 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2262 counters_enable
= !!nla_get_u8(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
2264 return devlink_dpipe_table_counters_set(devlink
, table_name
,
2268 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
2269 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
2270 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
2271 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
2272 [DEVLINK_ATTR_PORT_TYPE
] = { .type
= NLA_U16
},
2273 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
2274 [DEVLINK_ATTR_SB_INDEX
] = { .type
= NLA_U32
},
2275 [DEVLINK_ATTR_SB_POOL_INDEX
] = { .type
= NLA_U16
},
2276 [DEVLINK_ATTR_SB_POOL_TYPE
] = { .type
= NLA_U8
},
2277 [DEVLINK_ATTR_SB_POOL_SIZE
] = { .type
= NLA_U32
},
2278 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = { .type
= NLA_U8
},
2279 [DEVLINK_ATTR_SB_THRESHOLD
] = { .type
= NLA_U32
},
2280 [DEVLINK_ATTR_SB_TC_INDEX
] = { .type
= NLA_U16
},
2281 [DEVLINK_ATTR_ESWITCH_MODE
] = { .type
= NLA_U16
},
2282 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = { .type
= NLA_U8
},
2283 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = { .type
= NLA_U8
},
2284 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = { .type
= NLA_NUL_STRING
},
2285 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = { .type
= NLA_U8
},
2288 static const struct genl_ops devlink_nl_ops
[] = {
2290 .cmd
= DEVLINK_CMD_GET
,
2291 .doit
= devlink_nl_cmd_get_doit
,
2292 .dumpit
= devlink_nl_cmd_get_dumpit
,
2293 .policy
= devlink_nl_policy
,
2294 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2295 /* can be retrieved by unprivileged users */
2298 .cmd
= DEVLINK_CMD_PORT_GET
,
2299 .doit
= devlink_nl_cmd_port_get_doit
,
2300 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
2301 .policy
= devlink_nl_policy
,
2302 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
2303 /* can be retrieved by unprivileged users */
2306 .cmd
= DEVLINK_CMD_PORT_SET
,
2307 .doit
= devlink_nl_cmd_port_set_doit
,
2308 .policy
= devlink_nl_policy
,
2309 .flags
= GENL_ADMIN_PERM
,
2310 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
2313 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
2314 .doit
= devlink_nl_cmd_port_split_doit
,
2315 .policy
= devlink_nl_policy
,
2316 .flags
= GENL_ADMIN_PERM
,
2317 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2320 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
2321 .doit
= devlink_nl_cmd_port_unsplit_doit
,
2322 .policy
= devlink_nl_policy
,
2323 .flags
= GENL_ADMIN_PERM
,
2324 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2327 .cmd
= DEVLINK_CMD_SB_GET
,
2328 .doit
= devlink_nl_cmd_sb_get_doit
,
2329 .dumpit
= devlink_nl_cmd_sb_get_dumpit
,
2330 .policy
= devlink_nl_policy
,
2331 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2332 DEVLINK_NL_FLAG_NEED_SB
,
2333 /* can be retrieved by unprivileged users */
2336 .cmd
= DEVLINK_CMD_SB_POOL_GET
,
2337 .doit
= devlink_nl_cmd_sb_pool_get_doit
,
2338 .dumpit
= devlink_nl_cmd_sb_pool_get_dumpit
,
2339 .policy
= devlink_nl_policy
,
2340 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2341 DEVLINK_NL_FLAG_NEED_SB
,
2342 /* can be retrieved by unprivileged users */
2345 .cmd
= DEVLINK_CMD_SB_POOL_SET
,
2346 .doit
= devlink_nl_cmd_sb_pool_set_doit
,
2347 .policy
= devlink_nl_policy
,
2348 .flags
= GENL_ADMIN_PERM
,
2349 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2350 DEVLINK_NL_FLAG_NEED_SB
,
2353 .cmd
= DEVLINK_CMD_SB_PORT_POOL_GET
,
2354 .doit
= devlink_nl_cmd_sb_port_pool_get_doit
,
2355 .dumpit
= devlink_nl_cmd_sb_port_pool_get_dumpit
,
2356 .policy
= devlink_nl_policy
,
2357 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2358 DEVLINK_NL_FLAG_NEED_SB
,
2359 /* can be retrieved by unprivileged users */
2362 .cmd
= DEVLINK_CMD_SB_PORT_POOL_SET
,
2363 .doit
= devlink_nl_cmd_sb_port_pool_set_doit
,
2364 .policy
= devlink_nl_policy
,
2365 .flags
= GENL_ADMIN_PERM
,
2366 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2367 DEVLINK_NL_FLAG_NEED_SB
,
2370 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_GET
,
2371 .doit
= devlink_nl_cmd_sb_tc_pool_bind_get_doit
,
2372 .dumpit
= devlink_nl_cmd_sb_tc_pool_bind_get_dumpit
,
2373 .policy
= devlink_nl_policy
,
2374 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2375 DEVLINK_NL_FLAG_NEED_SB
,
2376 /* can be retrieved by unprivileged users */
2379 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2380 .doit
= devlink_nl_cmd_sb_tc_pool_bind_set_doit
,
2381 .policy
= devlink_nl_policy
,
2382 .flags
= GENL_ADMIN_PERM
,
2383 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2384 DEVLINK_NL_FLAG_NEED_SB
,
2387 .cmd
= DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2388 .doit
= devlink_nl_cmd_sb_occ_snapshot_doit
,
2389 .policy
= devlink_nl_policy
,
2390 .flags
= GENL_ADMIN_PERM
,
2391 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2392 DEVLINK_NL_FLAG_NEED_SB
|
2393 DEVLINK_NL_FLAG_LOCK_PORTS
,
2396 .cmd
= DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2397 .doit
= devlink_nl_cmd_sb_occ_max_clear_doit
,
2398 .policy
= devlink_nl_policy
,
2399 .flags
= GENL_ADMIN_PERM
,
2400 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2401 DEVLINK_NL_FLAG_NEED_SB
|
2402 DEVLINK_NL_FLAG_LOCK_PORTS
,
2405 .cmd
= DEVLINK_CMD_ESWITCH_GET
,
2406 .doit
= devlink_nl_cmd_eswitch_get_doit
,
2407 .policy
= devlink_nl_policy
,
2408 .flags
= GENL_ADMIN_PERM
,
2409 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2412 .cmd
= DEVLINK_CMD_ESWITCH_SET
,
2413 .doit
= devlink_nl_cmd_eswitch_set_doit
,
2414 .policy
= devlink_nl_policy
,
2415 .flags
= GENL_ADMIN_PERM
,
2416 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2419 .cmd
= DEVLINK_CMD_DPIPE_TABLE_GET
,
2420 .doit
= devlink_nl_cmd_dpipe_table_get
,
2421 .policy
= devlink_nl_policy
,
2422 .flags
= GENL_ADMIN_PERM
,
2423 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2426 .cmd
= DEVLINK_CMD_DPIPE_ENTRIES_GET
,
2427 .doit
= devlink_nl_cmd_dpipe_entries_get
,
2428 .policy
= devlink_nl_policy
,
2429 .flags
= GENL_ADMIN_PERM
,
2430 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2433 .cmd
= DEVLINK_CMD_DPIPE_HEADERS_GET
,
2434 .doit
= devlink_nl_cmd_dpipe_headers_get
,
2435 .policy
= devlink_nl_policy
,
2436 .flags
= GENL_ADMIN_PERM
,
2437 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2440 .cmd
= DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
2441 .doit
= devlink_nl_cmd_dpipe_table_counters_set
,
2442 .policy
= devlink_nl_policy
,
2443 .flags
= GENL_ADMIN_PERM
,
2444 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2448 static struct genl_family devlink_nl_family __ro_after_init
= {
2449 .name
= DEVLINK_GENL_NAME
,
2450 .version
= DEVLINK_GENL_VERSION
,
2451 .maxattr
= DEVLINK_ATTR_MAX
,
2453 .pre_doit
= devlink_nl_pre_doit
,
2454 .post_doit
= devlink_nl_post_doit
,
2455 .module
= THIS_MODULE
,
2456 .ops
= devlink_nl_ops
,
2457 .n_ops
= ARRAY_SIZE(devlink_nl_ops
),
2458 .mcgrps
= devlink_nl_mcgrps
,
2459 .n_mcgrps
= ARRAY_SIZE(devlink_nl_mcgrps
),
2463 * devlink_alloc - Allocate new devlink instance resources
2466 * @priv_size: size of user private data
2468 * Allocate new devlink instance resources, including devlink index
2471 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
2473 struct devlink
*devlink
;
2475 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
2479 devlink_net_set(devlink
, &init_net
);
2480 INIT_LIST_HEAD(&devlink
->port_list
);
2481 INIT_LIST_HEAD(&devlink
->sb_list
);
2482 INIT_LIST_HEAD_RCU(&devlink
->dpipe_table_list
);
2485 EXPORT_SYMBOL_GPL(devlink_alloc
);
2488 * devlink_register - Register devlink instance
2492 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
2494 mutex_lock(&devlink_mutex
);
2496 list_add_tail(&devlink
->list
, &devlink_list
);
2497 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
2498 mutex_unlock(&devlink_mutex
);
2501 EXPORT_SYMBOL_GPL(devlink_register
);
2504 * devlink_unregister - Unregister devlink instance
2508 void devlink_unregister(struct devlink
*devlink
)
2510 mutex_lock(&devlink_mutex
);
2511 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
2512 list_del(&devlink
->list
);
2513 mutex_unlock(&devlink_mutex
);
2515 EXPORT_SYMBOL_GPL(devlink_unregister
);
2518 * devlink_free - Free devlink instance resources
2522 void devlink_free(struct devlink
*devlink
)
2526 EXPORT_SYMBOL_GPL(devlink_free
);
2529 * devlink_port_register - Register devlink port
2532 * @devlink_port: devlink port
2535 * Register devlink port with provided port index. User can use
2536 * any indexing, even hw-related one. devlink_port structure
2537 * is convenient to be embedded inside user driver private structure.
2538 * Note that the caller should take care of zeroing the devlink_port
2541 int devlink_port_register(struct devlink
*devlink
,
2542 struct devlink_port
*devlink_port
,
2543 unsigned int port_index
)
2545 mutex_lock(&devlink_port_mutex
);
2546 if (devlink_port_index_exists(devlink
, port_index
)) {
2547 mutex_unlock(&devlink_port_mutex
);
2550 devlink_port
->devlink
= devlink
;
2551 devlink_port
->index
= port_index
;
2552 devlink_port
->registered
= true;
2553 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
2554 mutex_unlock(&devlink_port_mutex
);
2555 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
2558 EXPORT_SYMBOL_GPL(devlink_port_register
);
2561 * devlink_port_unregister - Unregister devlink port
2563 * @devlink_port: devlink port
2565 void devlink_port_unregister(struct devlink_port
*devlink_port
)
2567 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
2568 mutex_lock(&devlink_port_mutex
);
2569 list_del(&devlink_port
->list
);
2570 mutex_unlock(&devlink_port_mutex
);
2572 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
2574 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
2575 enum devlink_port_type type
,
2578 devlink_port
->type
= type
;
2579 devlink_port
->type_dev
= type_dev
;
2580 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
2584 * devlink_port_type_eth_set - Set port type to Ethernet
2586 * @devlink_port: devlink port
2587 * @netdev: related netdevice
2589 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
2590 struct net_device
*netdev
)
2592 return __devlink_port_type_set(devlink_port
,
2593 DEVLINK_PORT_TYPE_ETH
, netdev
);
2595 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
2598 * devlink_port_type_ib_set - Set port type to InfiniBand
2600 * @devlink_port: devlink port
2601 * @ibdev: related IB device
2603 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
2604 struct ib_device
*ibdev
)
2606 return __devlink_port_type_set(devlink_port
,
2607 DEVLINK_PORT_TYPE_IB
, ibdev
);
2609 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
2612 * devlink_port_type_clear - Clear port type
2614 * @devlink_port: devlink port
2616 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
2618 return __devlink_port_type_set(devlink_port
,
2619 DEVLINK_PORT_TYPE_NOTSET
, NULL
);
2621 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
2624 * devlink_port_split_set - Set port is split
2626 * @devlink_port: devlink port
2627 * @split_group: split group - identifies group split port is part of
2629 void devlink_port_split_set(struct devlink_port
*devlink_port
,
2632 devlink_port
->split
= true;
2633 devlink_port
->split_group
= split_group
;
2634 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
2636 EXPORT_SYMBOL_GPL(devlink_port_split_set
);
2638 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
2639 u32 size
, u16 ingress_pools_count
,
2640 u16 egress_pools_count
, u16 ingress_tc_count
,
2641 u16 egress_tc_count
)
2643 struct devlink_sb
*devlink_sb
;
2646 mutex_lock(&devlink_mutex
);
2647 if (devlink_sb_index_exists(devlink
, sb_index
)) {
2652 devlink_sb
= kzalloc(sizeof(*devlink_sb
), GFP_KERNEL
);
2657 devlink_sb
->index
= sb_index
;
2658 devlink_sb
->size
= size
;
2659 devlink_sb
->ingress_pools_count
= ingress_pools_count
;
2660 devlink_sb
->egress_pools_count
= egress_pools_count
;
2661 devlink_sb
->ingress_tc_count
= ingress_tc_count
;
2662 devlink_sb
->egress_tc_count
= egress_tc_count
;
2663 list_add_tail(&devlink_sb
->list
, &devlink
->sb_list
);
2665 mutex_unlock(&devlink_mutex
);
2668 EXPORT_SYMBOL_GPL(devlink_sb_register
);
2670 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
)
2672 struct devlink_sb
*devlink_sb
;
2674 mutex_lock(&devlink_mutex
);
2675 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
2676 WARN_ON(!devlink_sb
);
2677 list_del(&devlink_sb
->list
);
2678 mutex_unlock(&devlink_mutex
);
2681 EXPORT_SYMBOL_GPL(devlink_sb_unregister
);
2684 * devlink_dpipe_headers_register - register dpipe headers
2687 * @dpipe_headers: dpipe header array
2689 * Register the headers supported by hardware.
2691 int devlink_dpipe_headers_register(struct devlink
*devlink
,
2692 struct devlink_dpipe_headers
*dpipe_headers
)
2694 mutex_lock(&devlink_mutex
);
2695 devlink
->dpipe_headers
= dpipe_headers
;
2696 mutex_unlock(&devlink_mutex
);
2699 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register
);
2702 * devlink_dpipe_headers_unregister - unregister dpipe headers
2706 * Unregister the headers supported by hardware.
2708 void devlink_dpipe_headers_unregister(struct devlink
*devlink
)
2710 mutex_lock(&devlink_mutex
);
2711 devlink
->dpipe_headers
= NULL
;
2712 mutex_unlock(&devlink_mutex
);
2714 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister
);
2717 * devlink_dpipe_table_counter_enabled - check if counter allocation
2720 * @table_name: tables name
2722 * Used by driver to check if counter allocation is required.
2723 * After counter allocation is turned on the table entries
2724 * are updated to include counter statistics.
2726 * After that point on the driver must respect the counter
2727 * state so that each entry added to the table is added
2730 bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
2731 const char *table_name
)
2733 struct devlink_dpipe_table
*table
;
2737 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2741 enabled
= table
->counters_enabled
;
2745 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled
);
2748 * devlink_dpipe_table_register - register dpipe table
2751 * @table_name: table name
2752 * @table_ops: table ops
2754 * @counter_control_extern: external control for counters
2756 int devlink_dpipe_table_register(struct devlink
*devlink
,
2757 const char *table_name
,
2758 struct devlink_dpipe_table_ops
*table_ops
,
2759 void *priv
, bool counter_control_extern
)
2761 struct devlink_dpipe_table
*table
;
2763 if (devlink_dpipe_table_find(&devlink
->dpipe_table_list
, table_name
))
2766 if (WARN_ON(!table_ops
->size_get
))
2769 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
2773 table
->name
= table_name
;
2774 table
->table_ops
= table_ops
;
2776 table
->counter_control_extern
= counter_control_extern
;
2778 mutex_lock(&devlink_mutex
);
2779 list_add_tail_rcu(&table
->list
, &devlink
->dpipe_table_list
);
2780 mutex_unlock(&devlink_mutex
);
2783 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register
);
2786 * devlink_dpipe_table_unregister - unregister dpipe table
2789 * @table_name: table name
2791 void devlink_dpipe_table_unregister(struct devlink
*devlink
,
2792 const char *table_name
)
2794 struct devlink_dpipe_table
*table
;
2796 mutex_lock(&devlink_mutex
);
2797 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2801 list_del_rcu(&table
->list
);
2802 mutex_unlock(&devlink_mutex
);
2803 kfree_rcu(table
, rcu
);
2806 mutex_unlock(&devlink_mutex
);
2808 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister
);
2810 static int __init
devlink_module_init(void)
2812 return genl_register_family(&devlink_nl_family
);
2815 static void __exit
devlink_module_exit(void)
2817 genl_unregister_family(&devlink_nl_family
);
2820 module_init(devlink_module_init
);
2821 module_exit(devlink_module_exit
);
2823 MODULE_LICENSE("GPL v2");
2824 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2825 MODULE_DESCRIPTION("Network physical device Netlink interface");
2826 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME
);