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
);
1779 goto err_skb_send_alloc
;
1783 return genlmsg_reply(skb
, info
);
1789 genlmsg_cancel(skb
, hdr
);
1794 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff
*skb
,
1795 struct genl_info
*info
)
1797 struct devlink
*devlink
= info
->user_ptr
[0];
1798 const char *table_name
= NULL
;
1800 if (info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
1801 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
1803 return devlink_dpipe_tables_fill(info
, DEVLINK_CMD_DPIPE_TABLE_GET
, 0,
1804 &devlink
->dpipe_table_list
,
1808 static int devlink_dpipe_value_put(struct sk_buff
*skb
,
1809 struct devlink_dpipe_value
*value
)
1811 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE
,
1812 value
->value_size
, value
->value
))
1815 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE_MASK
,
1816 value
->value_size
, value
->mask
))
1818 if (value
->mapping_valid
)
1819 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_VALUE_MAPPING
,
1820 value
->mapping_value
))
1825 static int devlink_dpipe_action_value_put(struct sk_buff
*skb
,
1826 struct devlink_dpipe_value
*value
)
1830 if (devlink_dpipe_action_put(skb
, value
->action
))
1832 if (devlink_dpipe_value_put(skb
, value
))
1837 static int devlink_dpipe_action_values_put(struct sk_buff
*skb
,
1838 struct devlink_dpipe_value
*values
,
1839 unsigned int values_count
)
1841 struct nlattr
*action_attr
;
1845 for (i
= 0; i
< values_count
; i
++) {
1846 action_attr
= nla_nest_start(skb
,
1847 DEVLINK_ATTR_DPIPE_ACTION_VALUE
);
1850 err
= devlink_dpipe_action_value_put(skb
, &values
[i
]);
1852 goto err_action_value_put
;
1853 nla_nest_end(skb
, action_attr
);
1857 err_action_value_put
:
1858 nla_nest_cancel(skb
, action_attr
);
1862 static int devlink_dpipe_match_value_put(struct sk_buff
*skb
,
1863 struct devlink_dpipe_value
*value
)
1867 if (devlink_dpipe_match_put(skb
, value
->match
))
1869 if (devlink_dpipe_value_put(skb
, value
))
1874 static int devlink_dpipe_match_values_put(struct sk_buff
*skb
,
1875 struct devlink_dpipe_value
*values
,
1876 unsigned int values_count
)
1878 struct nlattr
*match_attr
;
1882 for (i
= 0; i
< values_count
; i
++) {
1883 match_attr
= nla_nest_start(skb
,
1884 DEVLINK_ATTR_DPIPE_MATCH_VALUE
);
1887 err
= devlink_dpipe_match_value_put(skb
, &values
[i
]);
1889 goto err_match_value_put
;
1890 nla_nest_end(skb
, match_attr
);
1894 err_match_value_put
:
1895 nla_nest_cancel(skb
, match_attr
);
1899 static int devlink_dpipe_entry_put(struct sk_buff
*skb
,
1900 struct devlink_dpipe_entry
*entry
)
1902 struct nlattr
*entry_attr
, *matches_attr
, *actions_attr
;
1905 entry_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_ENTRY
);
1909 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_INDEX
, entry
->index
,
1911 goto nla_put_failure
;
1912 if (entry
->counter_valid
)
1913 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
,
1914 entry
->counter
, DEVLINK_ATTR_PAD
))
1915 goto nla_put_failure
;
1917 matches_attr
= nla_nest_start(skb
,
1918 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
);
1920 goto nla_put_failure
;
1922 err
= devlink_dpipe_match_values_put(skb
, entry
->match_values
,
1923 entry
->match_values_count
);
1925 nla_nest_cancel(skb
, matches_attr
);
1926 goto err_match_values_put
;
1928 nla_nest_end(skb
, matches_attr
);
1930 actions_attr
= nla_nest_start(skb
,
1931 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
);
1933 goto nla_put_failure
;
1935 err
= devlink_dpipe_action_values_put(skb
, entry
->action_values
,
1936 entry
->action_values_count
);
1938 nla_nest_cancel(skb
, actions_attr
);
1939 goto err_action_values_put
;
1941 nla_nest_end(skb
, actions_attr
);
1943 nla_nest_end(skb
, entry_attr
);
1948 err_match_values_put
:
1949 err_action_values_put
:
1950 nla_nest_cancel(skb
, entry_attr
);
1954 static struct devlink_dpipe_table
*
1955 devlink_dpipe_table_find(struct list_head
*dpipe_tables
,
1956 const char *table_name
)
1958 struct devlink_dpipe_table
*table
;
1960 list_for_each_entry_rcu(table
, dpipe_tables
, list
) {
1961 if (!strcmp(table
->name
, table_name
))
1967 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx
*dump_ctx
)
1969 struct devlink
*devlink
;
1972 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
->skb
,
1977 dump_ctx
->hdr
= genlmsg_put(dump_ctx
->skb
,
1978 dump_ctx
->info
->snd_portid
,
1979 dump_ctx
->info
->snd_seq
,
1980 &devlink_nl_family
, NLM_F_MULTI
,
1983 goto nla_put_failure
;
1985 devlink
= dump_ctx
->info
->user_ptr
[0];
1986 if (devlink_nl_put_handle(dump_ctx
->skb
, devlink
))
1987 goto nla_put_failure
;
1988 dump_ctx
->nest
= nla_nest_start(dump_ctx
->skb
,
1989 DEVLINK_ATTR_DPIPE_ENTRIES
);
1990 if (!dump_ctx
->nest
)
1991 goto nla_put_failure
;
1995 genlmsg_cancel(dump_ctx
->skb
, dump_ctx
->hdr
);
1996 nlmsg_free(dump_ctx
->skb
);
1999 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare
);
2001 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx
*dump_ctx
,
2002 struct devlink_dpipe_entry
*entry
)
2004 return devlink_dpipe_entry_put(dump_ctx
->skb
, entry
);
2006 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append
);
2008 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx
*dump_ctx
)
2010 nla_nest_end(dump_ctx
->skb
, dump_ctx
->nest
);
2011 genlmsg_end(dump_ctx
->skb
, dump_ctx
->hdr
);
2014 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close
);
2016 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry
*entry
)
2019 unsigned int value_count
, value_index
;
2020 struct devlink_dpipe_value
*value
;
2022 value
= entry
->action_values
;
2023 value_count
= entry
->action_values_count
;
2024 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2025 kfree(value
[value_index
].value
);
2026 kfree(value
[value_index
].mask
);
2029 value
= entry
->match_values
;
2030 value_count
= entry
->match_values_count
;
2031 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2032 kfree(value
[value_index
].value
);
2033 kfree(value
[value_index
].mask
);
2036 EXPORT_SYMBOL(devlink_dpipe_entry_clear
);
2038 static int devlink_dpipe_entries_fill(struct genl_info
*info
,
2039 enum devlink_command cmd
, int flags
,
2040 struct devlink_dpipe_table
*table
)
2042 struct devlink_dpipe_dump_ctx dump_ctx
;
2043 struct nlmsghdr
*nlh
;
2046 dump_ctx
.skb
= NULL
;
2048 dump_ctx
.info
= info
;
2050 err
= table
->table_ops
->entries_dump(table
->priv
,
2051 table
->counters_enabled
,
2054 goto err_entries_dump
;
2057 nlh
= nlmsg_put(dump_ctx
.skb
, info
->snd_portid
, info
->snd_seq
,
2058 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2060 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
.skb
, info
);
2062 goto err_skb_send_alloc
;
2065 return genlmsg_reply(dump_ctx
.skb
, info
);
2069 genlmsg_cancel(dump_ctx
.skb
, dump_ctx
.hdr
);
2070 nlmsg_free(dump_ctx
.skb
);
2074 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff
*skb
,
2075 struct genl_info
*info
)
2077 struct devlink
*devlink
= info
->user_ptr
[0];
2078 struct devlink_dpipe_table
*table
;
2079 const char *table_name
;
2081 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
2084 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2085 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2090 if (!table
->table_ops
->entries_dump
)
2093 return devlink_dpipe_entries_fill(info
, DEVLINK_CMD_DPIPE_ENTRIES_GET
,
2097 static int devlink_dpipe_fields_put(struct sk_buff
*skb
,
2098 const struct devlink_dpipe_header
*header
)
2100 struct devlink_dpipe_field
*field
;
2101 struct nlattr
*field_attr
;
2104 for (i
= 0; i
< header
->fields_count
; i
++) {
2105 field
= &header
->fields
[i
];
2106 field_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_FIELD
);
2109 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_FIELD_NAME
, field
->name
) ||
2110 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
2111 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
, field
->bitwidth
) ||
2112 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
, field
->mapping_type
))
2113 goto nla_put_failure
;
2114 nla_nest_end(skb
, field_attr
);
2119 nla_nest_cancel(skb
, field_attr
);
2123 static int devlink_dpipe_header_put(struct sk_buff
*skb
,
2124 struct devlink_dpipe_header
*header
)
2126 struct nlattr
*fields_attr
, *header_attr
;
2129 header_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_HEADER
);
2133 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_HEADER_NAME
, header
->name
) ||
2134 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
2135 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
2136 goto nla_put_failure
;
2138 fields_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_HEADER_FIELDS
);
2140 goto nla_put_failure
;
2142 err
= devlink_dpipe_fields_put(skb
, header
);
2144 nla_nest_cancel(skb
, fields_attr
);
2145 goto nla_put_failure
;
2147 nla_nest_end(skb
, fields_attr
);
2148 nla_nest_end(skb
, header_attr
);
2153 nla_nest_cancel(skb
, header_attr
);
2157 static int devlink_dpipe_headers_fill(struct genl_info
*info
,
2158 enum devlink_command cmd
, int flags
,
2159 struct devlink_dpipe_headers
*
2162 struct devlink
*devlink
= info
->user_ptr
[0];
2163 struct nlattr
*headers_attr
;
2164 struct sk_buff
*skb
= NULL
;
2165 struct nlmsghdr
*nlh
;
2172 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2176 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2177 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2183 if (devlink_nl_put_handle(skb
, devlink
))
2184 goto nla_put_failure
;
2185 headers_attr
= nla_nest_start(skb
, DEVLINK_ATTR_DPIPE_HEADERS
);
2187 goto nla_put_failure
;
2190 for (; i
< dpipe_headers
->headers_count
; i
++) {
2191 err
= devlink_dpipe_header_put(skb
, dpipe_headers
->headers
[i
]);
2199 nla_nest_end(skb
, headers_attr
);
2200 genlmsg_end(skb
, hdr
);
2201 if (i
!= dpipe_headers
->headers_count
)
2205 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2206 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2208 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2210 goto err_skb_send_alloc
;
2213 return genlmsg_reply(skb
, info
);
2219 genlmsg_cancel(skb
, hdr
);
2224 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff
*skb
,
2225 struct genl_info
*info
)
2227 struct devlink
*devlink
= info
->user_ptr
[0];
2229 if (!devlink
->dpipe_headers
)
2231 return devlink_dpipe_headers_fill(info
, DEVLINK_CMD_DPIPE_HEADERS_GET
,
2232 0, devlink
->dpipe_headers
);
2235 static int devlink_dpipe_table_counters_set(struct devlink
*devlink
,
2236 const char *table_name
,
2239 struct devlink_dpipe_table
*table
;
2241 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2246 if (table
->counter_control_extern
)
2249 if (!(table
->counters_enabled
^ enable
))
2252 table
->counters_enabled
= enable
;
2253 if (table
->table_ops
->counters_set_update
)
2254 table
->table_ops
->counters_set_update(table
->priv
, enable
);
2258 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff
*skb
,
2259 struct genl_info
*info
)
2261 struct devlink
*devlink
= info
->user_ptr
[0];
2262 const char *table_name
;
2263 bool counters_enable
;
2265 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
2266 !info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
])
2269 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2270 counters_enable
= !!nla_get_u8(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
2272 return devlink_dpipe_table_counters_set(devlink
, table_name
,
2276 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
2277 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
2278 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
2279 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
2280 [DEVLINK_ATTR_PORT_TYPE
] = { .type
= NLA_U16
},
2281 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
2282 [DEVLINK_ATTR_SB_INDEX
] = { .type
= NLA_U32
},
2283 [DEVLINK_ATTR_SB_POOL_INDEX
] = { .type
= NLA_U16
},
2284 [DEVLINK_ATTR_SB_POOL_TYPE
] = { .type
= NLA_U8
},
2285 [DEVLINK_ATTR_SB_POOL_SIZE
] = { .type
= NLA_U32
},
2286 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = { .type
= NLA_U8
},
2287 [DEVLINK_ATTR_SB_THRESHOLD
] = { .type
= NLA_U32
},
2288 [DEVLINK_ATTR_SB_TC_INDEX
] = { .type
= NLA_U16
},
2289 [DEVLINK_ATTR_ESWITCH_MODE
] = { .type
= NLA_U16
},
2290 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = { .type
= NLA_U8
},
2291 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = { .type
= NLA_U8
},
2292 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = { .type
= NLA_NUL_STRING
},
2293 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = { .type
= NLA_U8
},
2296 static const struct genl_ops devlink_nl_ops
[] = {
2298 .cmd
= DEVLINK_CMD_GET
,
2299 .doit
= devlink_nl_cmd_get_doit
,
2300 .dumpit
= devlink_nl_cmd_get_dumpit
,
2301 .policy
= devlink_nl_policy
,
2302 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2303 /* can be retrieved by unprivileged users */
2306 .cmd
= DEVLINK_CMD_PORT_GET
,
2307 .doit
= devlink_nl_cmd_port_get_doit
,
2308 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
2309 .policy
= devlink_nl_policy
,
2310 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
2311 /* can be retrieved by unprivileged users */
2314 .cmd
= DEVLINK_CMD_PORT_SET
,
2315 .doit
= devlink_nl_cmd_port_set_doit
,
2316 .policy
= devlink_nl_policy
,
2317 .flags
= GENL_ADMIN_PERM
,
2318 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
2321 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
2322 .doit
= devlink_nl_cmd_port_split_doit
,
2323 .policy
= devlink_nl_policy
,
2324 .flags
= GENL_ADMIN_PERM
,
2325 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2328 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
2329 .doit
= devlink_nl_cmd_port_unsplit_doit
,
2330 .policy
= devlink_nl_policy
,
2331 .flags
= GENL_ADMIN_PERM
,
2332 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2335 .cmd
= DEVLINK_CMD_SB_GET
,
2336 .doit
= devlink_nl_cmd_sb_get_doit
,
2337 .dumpit
= devlink_nl_cmd_sb_get_dumpit
,
2338 .policy
= devlink_nl_policy
,
2339 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2340 DEVLINK_NL_FLAG_NEED_SB
,
2341 /* can be retrieved by unprivileged users */
2344 .cmd
= DEVLINK_CMD_SB_POOL_GET
,
2345 .doit
= devlink_nl_cmd_sb_pool_get_doit
,
2346 .dumpit
= devlink_nl_cmd_sb_pool_get_dumpit
,
2347 .policy
= devlink_nl_policy
,
2348 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2349 DEVLINK_NL_FLAG_NEED_SB
,
2350 /* can be retrieved by unprivileged users */
2353 .cmd
= DEVLINK_CMD_SB_POOL_SET
,
2354 .doit
= devlink_nl_cmd_sb_pool_set_doit
,
2355 .policy
= devlink_nl_policy
,
2356 .flags
= GENL_ADMIN_PERM
,
2357 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2358 DEVLINK_NL_FLAG_NEED_SB
,
2361 .cmd
= DEVLINK_CMD_SB_PORT_POOL_GET
,
2362 .doit
= devlink_nl_cmd_sb_port_pool_get_doit
,
2363 .dumpit
= devlink_nl_cmd_sb_port_pool_get_dumpit
,
2364 .policy
= devlink_nl_policy
,
2365 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2366 DEVLINK_NL_FLAG_NEED_SB
,
2367 /* can be retrieved by unprivileged users */
2370 .cmd
= DEVLINK_CMD_SB_PORT_POOL_SET
,
2371 .doit
= devlink_nl_cmd_sb_port_pool_set_doit
,
2372 .policy
= devlink_nl_policy
,
2373 .flags
= GENL_ADMIN_PERM
,
2374 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2375 DEVLINK_NL_FLAG_NEED_SB
,
2378 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_GET
,
2379 .doit
= devlink_nl_cmd_sb_tc_pool_bind_get_doit
,
2380 .dumpit
= devlink_nl_cmd_sb_tc_pool_bind_get_dumpit
,
2381 .policy
= devlink_nl_policy
,
2382 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2383 DEVLINK_NL_FLAG_NEED_SB
,
2384 /* can be retrieved by unprivileged users */
2387 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
2388 .doit
= devlink_nl_cmd_sb_tc_pool_bind_set_doit
,
2389 .policy
= devlink_nl_policy
,
2390 .flags
= GENL_ADMIN_PERM
,
2391 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
2392 DEVLINK_NL_FLAG_NEED_SB
,
2395 .cmd
= DEVLINK_CMD_SB_OCC_SNAPSHOT
,
2396 .doit
= devlink_nl_cmd_sb_occ_snapshot_doit
,
2397 .policy
= devlink_nl_policy
,
2398 .flags
= GENL_ADMIN_PERM
,
2399 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2400 DEVLINK_NL_FLAG_NEED_SB
|
2401 DEVLINK_NL_FLAG_LOCK_PORTS
,
2404 .cmd
= DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
2405 .doit
= devlink_nl_cmd_sb_occ_max_clear_doit
,
2406 .policy
= devlink_nl_policy
,
2407 .flags
= GENL_ADMIN_PERM
,
2408 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
2409 DEVLINK_NL_FLAG_NEED_SB
|
2410 DEVLINK_NL_FLAG_LOCK_PORTS
,
2413 .cmd
= DEVLINK_CMD_ESWITCH_GET
,
2414 .doit
= devlink_nl_cmd_eswitch_get_doit
,
2415 .policy
= devlink_nl_policy
,
2416 .flags
= GENL_ADMIN_PERM
,
2417 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2420 .cmd
= DEVLINK_CMD_ESWITCH_SET
,
2421 .doit
= devlink_nl_cmd_eswitch_set_doit
,
2422 .policy
= devlink_nl_policy
,
2423 .flags
= GENL_ADMIN_PERM
,
2424 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2427 .cmd
= DEVLINK_CMD_DPIPE_TABLE_GET
,
2428 .doit
= devlink_nl_cmd_dpipe_table_get
,
2429 .policy
= devlink_nl_policy
,
2430 .flags
= GENL_ADMIN_PERM
,
2431 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2434 .cmd
= DEVLINK_CMD_DPIPE_ENTRIES_GET
,
2435 .doit
= devlink_nl_cmd_dpipe_entries_get
,
2436 .policy
= devlink_nl_policy
,
2437 .flags
= GENL_ADMIN_PERM
,
2438 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2441 .cmd
= DEVLINK_CMD_DPIPE_HEADERS_GET
,
2442 .doit
= devlink_nl_cmd_dpipe_headers_get
,
2443 .policy
= devlink_nl_policy
,
2444 .flags
= GENL_ADMIN_PERM
,
2445 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2448 .cmd
= DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
2449 .doit
= devlink_nl_cmd_dpipe_table_counters_set
,
2450 .policy
= devlink_nl_policy
,
2451 .flags
= GENL_ADMIN_PERM
,
2452 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
2456 static struct genl_family devlink_nl_family __ro_after_init
= {
2457 .name
= DEVLINK_GENL_NAME
,
2458 .version
= DEVLINK_GENL_VERSION
,
2459 .maxattr
= DEVLINK_ATTR_MAX
,
2461 .pre_doit
= devlink_nl_pre_doit
,
2462 .post_doit
= devlink_nl_post_doit
,
2463 .module
= THIS_MODULE
,
2464 .ops
= devlink_nl_ops
,
2465 .n_ops
= ARRAY_SIZE(devlink_nl_ops
),
2466 .mcgrps
= devlink_nl_mcgrps
,
2467 .n_mcgrps
= ARRAY_SIZE(devlink_nl_mcgrps
),
2471 * devlink_alloc - Allocate new devlink instance resources
2474 * @priv_size: size of user private data
2476 * Allocate new devlink instance resources, including devlink index
2479 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
2481 struct devlink
*devlink
;
2483 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
2487 devlink_net_set(devlink
, &init_net
);
2488 INIT_LIST_HEAD(&devlink
->port_list
);
2489 INIT_LIST_HEAD(&devlink
->sb_list
);
2490 INIT_LIST_HEAD_RCU(&devlink
->dpipe_table_list
);
2493 EXPORT_SYMBOL_GPL(devlink_alloc
);
2496 * devlink_register - Register devlink instance
2500 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
2502 mutex_lock(&devlink_mutex
);
2504 list_add_tail(&devlink
->list
, &devlink_list
);
2505 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
2506 mutex_unlock(&devlink_mutex
);
2509 EXPORT_SYMBOL_GPL(devlink_register
);
2512 * devlink_unregister - Unregister devlink instance
2516 void devlink_unregister(struct devlink
*devlink
)
2518 mutex_lock(&devlink_mutex
);
2519 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
2520 list_del(&devlink
->list
);
2521 mutex_unlock(&devlink_mutex
);
2523 EXPORT_SYMBOL_GPL(devlink_unregister
);
2526 * devlink_free - Free devlink instance resources
2530 void devlink_free(struct devlink
*devlink
)
2534 EXPORT_SYMBOL_GPL(devlink_free
);
2537 * devlink_port_register - Register devlink port
2540 * @devlink_port: devlink port
2543 * Register devlink port with provided port index. User can use
2544 * any indexing, even hw-related one. devlink_port structure
2545 * is convenient to be embedded inside user driver private structure.
2546 * Note that the caller should take care of zeroing the devlink_port
2549 int devlink_port_register(struct devlink
*devlink
,
2550 struct devlink_port
*devlink_port
,
2551 unsigned int port_index
)
2553 mutex_lock(&devlink_port_mutex
);
2554 if (devlink_port_index_exists(devlink
, port_index
)) {
2555 mutex_unlock(&devlink_port_mutex
);
2558 devlink_port
->devlink
= devlink
;
2559 devlink_port
->index
= port_index
;
2560 devlink_port
->registered
= true;
2561 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
2562 mutex_unlock(&devlink_port_mutex
);
2563 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
2566 EXPORT_SYMBOL_GPL(devlink_port_register
);
2569 * devlink_port_unregister - Unregister devlink port
2571 * @devlink_port: devlink port
2573 void devlink_port_unregister(struct devlink_port
*devlink_port
)
2575 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
2576 mutex_lock(&devlink_port_mutex
);
2577 list_del(&devlink_port
->list
);
2578 mutex_unlock(&devlink_port_mutex
);
2580 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
2582 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
2583 enum devlink_port_type type
,
2586 devlink_port
->type
= type
;
2587 devlink_port
->type_dev
= type_dev
;
2588 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
2592 * devlink_port_type_eth_set - Set port type to Ethernet
2594 * @devlink_port: devlink port
2595 * @netdev: related netdevice
2597 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
2598 struct net_device
*netdev
)
2600 return __devlink_port_type_set(devlink_port
,
2601 DEVLINK_PORT_TYPE_ETH
, netdev
);
2603 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
2606 * devlink_port_type_ib_set - Set port type to InfiniBand
2608 * @devlink_port: devlink port
2609 * @ibdev: related IB device
2611 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
2612 struct ib_device
*ibdev
)
2614 return __devlink_port_type_set(devlink_port
,
2615 DEVLINK_PORT_TYPE_IB
, ibdev
);
2617 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
2620 * devlink_port_type_clear - Clear port type
2622 * @devlink_port: devlink port
2624 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
2626 return __devlink_port_type_set(devlink_port
,
2627 DEVLINK_PORT_TYPE_NOTSET
, NULL
);
2629 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
2632 * devlink_port_split_set - Set port is split
2634 * @devlink_port: devlink port
2635 * @split_group: split group - identifies group split port is part of
2637 void devlink_port_split_set(struct devlink_port
*devlink_port
,
2640 devlink_port
->split
= true;
2641 devlink_port
->split_group
= split_group
;
2642 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
2644 EXPORT_SYMBOL_GPL(devlink_port_split_set
);
2646 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
2647 u32 size
, u16 ingress_pools_count
,
2648 u16 egress_pools_count
, u16 ingress_tc_count
,
2649 u16 egress_tc_count
)
2651 struct devlink_sb
*devlink_sb
;
2654 mutex_lock(&devlink_mutex
);
2655 if (devlink_sb_index_exists(devlink
, sb_index
)) {
2660 devlink_sb
= kzalloc(sizeof(*devlink_sb
), GFP_KERNEL
);
2665 devlink_sb
->index
= sb_index
;
2666 devlink_sb
->size
= size
;
2667 devlink_sb
->ingress_pools_count
= ingress_pools_count
;
2668 devlink_sb
->egress_pools_count
= egress_pools_count
;
2669 devlink_sb
->ingress_tc_count
= ingress_tc_count
;
2670 devlink_sb
->egress_tc_count
= egress_tc_count
;
2671 list_add_tail(&devlink_sb
->list
, &devlink
->sb_list
);
2673 mutex_unlock(&devlink_mutex
);
2676 EXPORT_SYMBOL_GPL(devlink_sb_register
);
2678 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
)
2680 struct devlink_sb
*devlink_sb
;
2682 mutex_lock(&devlink_mutex
);
2683 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
2684 WARN_ON(!devlink_sb
);
2685 list_del(&devlink_sb
->list
);
2686 mutex_unlock(&devlink_mutex
);
2689 EXPORT_SYMBOL_GPL(devlink_sb_unregister
);
2692 * devlink_dpipe_headers_register - register dpipe headers
2695 * @dpipe_headers: dpipe header array
2697 * Register the headers supported by hardware.
2699 int devlink_dpipe_headers_register(struct devlink
*devlink
,
2700 struct devlink_dpipe_headers
*dpipe_headers
)
2702 mutex_lock(&devlink_mutex
);
2703 devlink
->dpipe_headers
= dpipe_headers
;
2704 mutex_unlock(&devlink_mutex
);
2707 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register
);
2710 * devlink_dpipe_headers_unregister - unregister dpipe headers
2714 * Unregister the headers supported by hardware.
2716 void devlink_dpipe_headers_unregister(struct devlink
*devlink
)
2718 mutex_lock(&devlink_mutex
);
2719 devlink
->dpipe_headers
= NULL
;
2720 mutex_unlock(&devlink_mutex
);
2722 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister
);
2725 * devlink_dpipe_table_counter_enabled - check if counter allocation
2728 * @table_name: tables name
2730 * Used by driver to check if counter allocation is required.
2731 * After counter allocation is turned on the table entries
2732 * are updated to include counter statistics.
2734 * After that point on the driver must respect the counter
2735 * state so that each entry added to the table is added
2738 bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
2739 const char *table_name
)
2741 struct devlink_dpipe_table
*table
;
2745 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2749 enabled
= table
->counters_enabled
;
2753 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled
);
2756 * devlink_dpipe_table_register - register dpipe table
2759 * @table_name: table name
2760 * @table_ops: table ops
2762 * @counter_control_extern: external control for counters
2764 int devlink_dpipe_table_register(struct devlink
*devlink
,
2765 const char *table_name
,
2766 struct devlink_dpipe_table_ops
*table_ops
,
2767 void *priv
, bool counter_control_extern
)
2769 struct devlink_dpipe_table
*table
;
2771 if (devlink_dpipe_table_find(&devlink
->dpipe_table_list
, table_name
))
2774 if (WARN_ON(!table_ops
->size_get
))
2777 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
2781 table
->name
= table_name
;
2782 table
->table_ops
= table_ops
;
2784 table
->counter_control_extern
= counter_control_extern
;
2786 mutex_lock(&devlink_mutex
);
2787 list_add_tail_rcu(&table
->list
, &devlink
->dpipe_table_list
);
2788 mutex_unlock(&devlink_mutex
);
2791 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register
);
2794 * devlink_dpipe_table_unregister - unregister dpipe table
2797 * @table_name: table name
2799 void devlink_dpipe_table_unregister(struct devlink
*devlink
,
2800 const char *table_name
)
2802 struct devlink_dpipe_table
*table
;
2804 mutex_lock(&devlink_mutex
);
2805 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2809 list_del_rcu(&table
->list
);
2810 mutex_unlock(&devlink_mutex
);
2811 kfree_rcu(table
, rcu
);
2814 mutex_unlock(&devlink_mutex
);
2816 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister
);
2818 static int __init
devlink_module_init(void)
2820 return genl_register_family(&devlink_nl_family
);
2823 static void __exit
devlink_module_exit(void)
2825 genl_unregister_family(&devlink_nl_family
);
2828 module_init(devlink_module_init
);
2829 module_exit(devlink_module_exit
);
2831 MODULE_LICENSE("GPL v2");
2832 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2833 MODULE_DESCRIPTION("Network physical device Netlink interface");
2834 MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME
);