1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/devlink.c - Network physical/parent device Netlink interface
5 * Heavily inspired by net/wireless/
6 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
7 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/gfp.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
17 #include <linux/netdevice.h>
18 #include <linux/spinlock.h>
19 #include <linux/refcount.h>
20 #include <linux/workqueue.h>
21 #include <linux/u64_stats_sync.h>
22 #include <linux/timekeeping.h>
23 #include <rdma/ib_verbs.h>
24 #include <net/netlink.h>
25 #include <net/genetlink.h>
26 #include <net/rtnetlink.h>
27 #include <net/net_namespace.h>
29 #include <net/devlink.h>
30 #include <net/drop_monitor.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devlink.h>
34 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet
[] = {
36 .name
= "destination mac",
37 .id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
42 struct devlink_dpipe_header devlink_dpipe_header_ethernet
= {
44 .id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
45 .fields
= devlink_dpipe_fields_ethernet
,
46 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ethernet
),
49 EXPORT_SYMBOL(devlink_dpipe_header_ethernet
);
51 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4
[] = {
53 .name
= "destination ip",
54 .id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
59 struct devlink_dpipe_header devlink_dpipe_header_ipv4
= {
61 .id
= DEVLINK_DPIPE_HEADER_IPV4
,
62 .fields
= devlink_dpipe_fields_ipv4
,
63 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ipv4
),
66 EXPORT_SYMBOL(devlink_dpipe_header_ipv4
);
68 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6
[] = {
70 .name
= "destination ip",
71 .id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
76 struct devlink_dpipe_header devlink_dpipe_header_ipv6
= {
78 .id
= DEVLINK_DPIPE_HEADER_IPV6
,
79 .fields
= devlink_dpipe_fields_ipv6
,
80 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ipv6
),
83 EXPORT_SYMBOL(devlink_dpipe_header_ipv6
);
85 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg
);
86 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr
);
88 static LIST_HEAD(devlink_list
);
92 * An overall lock guarding every operation coming from userspace.
93 * It also guards devlink devices list and it is taken when
94 * driver registers/unregisters it.
96 static DEFINE_MUTEX(devlink_mutex
);
98 struct net
*devlink_net(const struct devlink
*devlink
)
100 return read_pnet(&devlink
->_net
);
102 EXPORT_SYMBOL_GPL(devlink_net
);
104 static void __devlink_net_set(struct devlink
*devlink
, struct net
*net
)
106 write_pnet(&devlink
->_net
, net
);
109 void devlink_net_set(struct devlink
*devlink
, struct net
*net
)
111 if (WARN_ON(devlink
->registered
))
113 __devlink_net_set(devlink
, net
);
115 EXPORT_SYMBOL_GPL(devlink_net_set
);
117 static struct devlink
*devlink_get_from_attrs(struct net
*net
,
118 struct nlattr
**attrs
)
120 struct devlink
*devlink
;
124 if (!attrs
[DEVLINK_ATTR_BUS_NAME
] || !attrs
[DEVLINK_ATTR_DEV_NAME
])
125 return ERR_PTR(-EINVAL
);
127 busname
= nla_data(attrs
[DEVLINK_ATTR_BUS_NAME
]);
128 devname
= nla_data(attrs
[DEVLINK_ATTR_DEV_NAME
]);
130 lockdep_assert_held(&devlink_mutex
);
132 list_for_each_entry(devlink
, &devlink_list
, list
) {
133 if (strcmp(devlink
->dev
->bus
->name
, busname
) == 0 &&
134 strcmp(dev_name(devlink
->dev
), devname
) == 0 &&
135 net_eq(devlink_net(devlink
), net
))
139 return ERR_PTR(-ENODEV
);
142 static struct devlink
*devlink_get_from_info(struct genl_info
*info
)
144 return devlink_get_from_attrs(genl_info_net(info
), info
->attrs
);
147 static struct devlink_port
*devlink_port_get_by_index(struct devlink
*devlink
,
148 unsigned int port_index
)
150 struct devlink_port
*devlink_port
;
152 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
153 if (devlink_port
->index
== port_index
)
159 static bool devlink_port_index_exists(struct devlink
*devlink
,
160 unsigned int port_index
)
162 return devlink_port_get_by_index(devlink
, port_index
);
165 static struct devlink_port
*devlink_port_get_from_attrs(struct devlink
*devlink
,
166 struct nlattr
**attrs
)
168 if (attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
169 u32 port_index
= nla_get_u32(attrs
[DEVLINK_ATTR_PORT_INDEX
]);
170 struct devlink_port
*devlink_port
;
172 devlink_port
= devlink_port_get_by_index(devlink
, port_index
);
174 return ERR_PTR(-ENODEV
);
177 return ERR_PTR(-EINVAL
);
180 static struct devlink_port
*devlink_port_get_from_info(struct devlink
*devlink
,
181 struct genl_info
*info
)
183 return devlink_port_get_from_attrs(devlink
, info
->attrs
);
187 struct list_head list
;
190 u16 ingress_pools_count
;
191 u16 egress_pools_count
;
192 u16 ingress_tc_count
;
196 static u16
devlink_sb_pool_count(struct devlink_sb
*devlink_sb
)
198 return devlink_sb
->ingress_pools_count
+ devlink_sb
->egress_pools_count
;
201 static struct devlink_sb
*devlink_sb_get_by_index(struct devlink
*devlink
,
202 unsigned int sb_index
)
204 struct devlink_sb
*devlink_sb
;
206 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
207 if (devlink_sb
->index
== sb_index
)
213 static bool devlink_sb_index_exists(struct devlink
*devlink
,
214 unsigned int sb_index
)
216 return devlink_sb_get_by_index(devlink
, sb_index
);
219 static struct devlink_sb
*devlink_sb_get_from_attrs(struct devlink
*devlink
,
220 struct nlattr
**attrs
)
222 if (attrs
[DEVLINK_ATTR_SB_INDEX
]) {
223 u32 sb_index
= nla_get_u32(attrs
[DEVLINK_ATTR_SB_INDEX
]);
224 struct devlink_sb
*devlink_sb
;
226 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
228 return ERR_PTR(-ENODEV
);
231 return ERR_PTR(-EINVAL
);
234 static struct devlink_sb
*devlink_sb_get_from_info(struct devlink
*devlink
,
235 struct genl_info
*info
)
237 return devlink_sb_get_from_attrs(devlink
, info
->attrs
);
240 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
241 struct nlattr
**attrs
,
246 if (!attrs
[DEVLINK_ATTR_SB_POOL_INDEX
])
249 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_POOL_INDEX
]);
250 if (val
>= devlink_sb_pool_count(devlink_sb
))
256 static int devlink_sb_pool_index_get_from_info(struct devlink_sb
*devlink_sb
,
257 struct genl_info
*info
,
260 return devlink_sb_pool_index_get_from_attrs(devlink_sb
, info
->attrs
,
265 devlink_sb_pool_type_get_from_attrs(struct nlattr
**attrs
,
266 enum devlink_sb_pool_type
*p_pool_type
)
270 if (!attrs
[DEVLINK_ATTR_SB_POOL_TYPE
])
273 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_TYPE
]);
274 if (val
!= DEVLINK_SB_POOL_TYPE_INGRESS
&&
275 val
!= DEVLINK_SB_POOL_TYPE_EGRESS
)
282 devlink_sb_pool_type_get_from_info(struct genl_info
*info
,
283 enum devlink_sb_pool_type
*p_pool_type
)
285 return devlink_sb_pool_type_get_from_attrs(info
->attrs
, p_pool_type
);
289 devlink_sb_th_type_get_from_attrs(struct nlattr
**attrs
,
290 enum devlink_sb_threshold_type
*p_th_type
)
294 if (!attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
297 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
]);
298 if (val
!= DEVLINK_SB_THRESHOLD_TYPE_STATIC
&&
299 val
!= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
)
306 devlink_sb_th_type_get_from_info(struct genl_info
*info
,
307 enum devlink_sb_threshold_type
*p_th_type
)
309 return devlink_sb_th_type_get_from_attrs(info
->attrs
, p_th_type
);
313 devlink_sb_tc_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
314 struct nlattr
**attrs
,
315 enum devlink_sb_pool_type pool_type
,
320 if (!attrs
[DEVLINK_ATTR_SB_TC_INDEX
])
323 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_TC_INDEX
]);
324 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
&&
325 val
>= devlink_sb
->ingress_tc_count
)
327 if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
&&
328 val
>= devlink_sb
->egress_tc_count
)
335 devlink_sb_tc_index_get_from_info(struct devlink_sb
*devlink_sb
,
336 struct genl_info
*info
,
337 enum devlink_sb_pool_type pool_type
,
340 return devlink_sb_tc_index_get_from_attrs(devlink_sb
, info
->attrs
,
341 pool_type
, p_tc_index
);
344 struct devlink_region
{
345 struct devlink
*devlink
;
346 struct list_head list
;
347 const struct devlink_region_ops
*ops
;
348 struct list_head snapshot_list
;
354 struct devlink_snapshot
{
355 struct list_head list
;
356 struct devlink_region
*region
;
361 static struct devlink_region
*
362 devlink_region_get_by_name(struct devlink
*devlink
, const char *region_name
)
364 struct devlink_region
*region
;
366 list_for_each_entry(region
, &devlink
->region_list
, list
)
367 if (!strcmp(region
->ops
->name
, region_name
))
373 static struct devlink_snapshot
*
374 devlink_region_snapshot_get_by_id(struct devlink_region
*region
, u32 id
)
376 struct devlink_snapshot
*snapshot
;
378 list_for_each_entry(snapshot
, ®ion
->snapshot_list
, list
)
379 if (snapshot
->id
== id
)
385 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
386 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
387 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
389 /* The per devlink instance lock is taken by default in the pre-doit
390 * operation, yet several commands do not require this. The global
391 * devlink lock is taken and protects from disruption by user-calls.
393 #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
395 static int devlink_nl_pre_doit(const struct genl_ops
*ops
,
396 struct sk_buff
*skb
, struct genl_info
*info
)
398 struct devlink
*devlink
;
401 mutex_lock(&devlink_mutex
);
402 devlink
= devlink_get_from_info(info
);
403 if (IS_ERR(devlink
)) {
404 mutex_unlock(&devlink_mutex
);
405 return PTR_ERR(devlink
);
407 if (~ops
->internal_flags
& DEVLINK_NL_FLAG_NO_LOCK
)
408 mutex_lock(&devlink
->lock
);
409 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_DEVLINK
) {
410 info
->user_ptr
[0] = devlink
;
411 } else if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
) {
412 struct devlink_port
*devlink_port
;
414 devlink_port
= devlink_port_get_from_info(devlink
, info
);
415 if (IS_ERR(devlink_port
)) {
416 err
= PTR_ERR(devlink_port
);
419 info
->user_ptr
[0] = devlink_port
;
421 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_SB
) {
422 struct devlink_sb
*devlink_sb
;
424 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
425 if (IS_ERR(devlink_sb
)) {
426 err
= PTR_ERR(devlink_sb
);
429 info
->user_ptr
[1] = devlink_sb
;
434 if (~ops
->internal_flags
& DEVLINK_NL_FLAG_NO_LOCK
)
435 mutex_unlock(&devlink
->lock
);
436 mutex_unlock(&devlink_mutex
);
440 static void devlink_nl_post_doit(const struct genl_ops
*ops
,
441 struct sk_buff
*skb
, struct genl_info
*info
)
443 struct devlink
*devlink
;
445 /* When devlink changes netns, it would not be found
446 * by devlink_get_from_info(). So try if it is stored first.
448 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_DEVLINK
) {
449 devlink
= info
->user_ptr
[0];
451 devlink
= devlink_get_from_info(info
);
452 WARN_ON(IS_ERR(devlink
));
454 if (!IS_ERR(devlink
) && ~ops
->internal_flags
& DEVLINK_NL_FLAG_NO_LOCK
)
455 mutex_unlock(&devlink
->lock
);
456 mutex_unlock(&devlink_mutex
);
459 static struct genl_family devlink_nl_family
;
461 enum devlink_multicast_groups
{
462 DEVLINK_MCGRP_CONFIG
,
465 static const struct genl_multicast_group devlink_nl_mcgrps
[] = {
466 [DEVLINK_MCGRP_CONFIG
] = { .name
= DEVLINK_GENL_MCGRP_CONFIG_NAME
},
469 static int devlink_nl_put_handle(struct sk_buff
*msg
, struct devlink
*devlink
)
471 if (nla_put_string(msg
, DEVLINK_ATTR_BUS_NAME
, devlink
->dev
->bus
->name
))
473 if (nla_put_string(msg
, DEVLINK_ATTR_DEV_NAME
, dev_name(devlink
->dev
)))
478 static int devlink_nl_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
479 enum devlink_command cmd
, u32 portid
,
484 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
488 if (devlink_nl_put_handle(msg
, devlink
))
489 goto nla_put_failure
;
490 if (nla_put_u8(msg
, DEVLINK_ATTR_RELOAD_FAILED
, devlink
->reload_failed
))
491 goto nla_put_failure
;
493 genlmsg_end(msg
, hdr
);
497 genlmsg_cancel(msg
, hdr
);
501 static void devlink_notify(struct devlink
*devlink
, enum devlink_command cmd
)
506 WARN_ON(cmd
!= DEVLINK_CMD_NEW
&& cmd
!= DEVLINK_CMD_DEL
);
508 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
512 err
= devlink_nl_fill(msg
, devlink
, cmd
, 0, 0, 0);
518 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
519 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
522 static int devlink_nl_port_attrs_put(struct sk_buff
*msg
,
523 struct devlink_port
*devlink_port
)
525 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
529 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_FLAVOUR
, attrs
->flavour
))
531 switch (devlink_port
->attrs
.flavour
) {
532 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
533 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_PCI_PF_NUMBER
,
537 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
538 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_PCI_PF_NUMBER
,
540 nla_put_u16(msg
, DEVLINK_ATTR_PORT_PCI_VF_NUMBER
,
544 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
545 case DEVLINK_PORT_FLAVOUR_CPU
:
546 case DEVLINK_PORT_FLAVOUR_DSA
:
547 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
548 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NUMBER
,
549 attrs
->phys
.port_number
))
553 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_GROUP
,
554 attrs
->phys
.port_number
))
556 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER
,
557 attrs
->phys
.split_subport_number
))
566 static int devlink_nl_port_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
567 struct devlink_port
*devlink_port
,
568 enum devlink_command cmd
, u32 portid
,
573 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
577 if (devlink_nl_put_handle(msg
, devlink
))
578 goto nla_put_failure
;
579 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
580 goto nla_put_failure
;
582 spin_lock_bh(&devlink_port
->type_lock
);
583 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_TYPE
, devlink_port
->type
))
584 goto nla_put_failure_type_locked
;
585 if (devlink_port
->desired_type
!= DEVLINK_PORT_TYPE_NOTSET
&&
586 nla_put_u16(msg
, DEVLINK_ATTR_PORT_DESIRED_TYPE
,
587 devlink_port
->desired_type
))
588 goto nla_put_failure_type_locked
;
589 if (devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
) {
590 struct net_device
*netdev
= devlink_port
->type_dev
;
593 (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NETDEV_IFINDEX
,
595 nla_put_string(msg
, DEVLINK_ATTR_PORT_NETDEV_NAME
,
597 goto nla_put_failure_type_locked
;
599 if (devlink_port
->type
== DEVLINK_PORT_TYPE_IB
) {
600 struct ib_device
*ibdev
= devlink_port
->type_dev
;
603 nla_put_string(msg
, DEVLINK_ATTR_PORT_IBDEV_NAME
,
605 goto nla_put_failure_type_locked
;
607 spin_unlock_bh(&devlink_port
->type_lock
);
608 if (devlink_nl_port_attrs_put(msg
, devlink_port
))
609 goto nla_put_failure
;
611 genlmsg_end(msg
, hdr
);
614 nla_put_failure_type_locked
:
615 spin_unlock_bh(&devlink_port
->type_lock
);
617 genlmsg_cancel(msg
, hdr
);
621 static void devlink_port_notify(struct devlink_port
*devlink_port
,
622 enum devlink_command cmd
)
624 struct devlink
*devlink
= devlink_port
->devlink
;
628 if (!devlink_port
->registered
)
631 WARN_ON(cmd
!= DEVLINK_CMD_PORT_NEW
&& cmd
!= DEVLINK_CMD_PORT_DEL
);
633 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
637 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
, cmd
, 0, 0, 0);
643 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
644 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
647 static int devlink_nl_cmd_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
649 struct devlink
*devlink
= info
->user_ptr
[0];
653 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
657 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
658 info
->snd_portid
, info
->snd_seq
, 0);
664 return genlmsg_reply(msg
, info
);
667 static int devlink_nl_cmd_get_dumpit(struct sk_buff
*msg
,
668 struct netlink_callback
*cb
)
670 struct devlink
*devlink
;
671 int start
= cb
->args
[0];
675 mutex_lock(&devlink_mutex
);
676 list_for_each_entry(devlink
, &devlink_list
, list
) {
677 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
683 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
684 NETLINK_CB(cb
->skb
).portid
,
685 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
691 mutex_unlock(&devlink_mutex
);
697 static int devlink_nl_cmd_port_get_doit(struct sk_buff
*skb
,
698 struct genl_info
*info
)
700 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
701 struct devlink
*devlink
= devlink_port
->devlink
;
705 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
709 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
710 DEVLINK_CMD_PORT_NEW
,
711 info
->snd_portid
, info
->snd_seq
, 0);
717 return genlmsg_reply(msg
, info
);
720 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff
*msg
,
721 struct netlink_callback
*cb
)
723 struct devlink
*devlink
;
724 struct devlink_port
*devlink_port
;
725 int start
= cb
->args
[0];
729 mutex_lock(&devlink_mutex
);
730 list_for_each_entry(devlink
, &devlink_list
, list
) {
731 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
733 mutex_lock(&devlink
->lock
);
734 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
739 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
741 NETLINK_CB(cb
->skb
).portid
,
745 mutex_unlock(&devlink
->lock
);
750 mutex_unlock(&devlink
->lock
);
753 mutex_unlock(&devlink_mutex
);
759 static int devlink_port_type_set(struct devlink
*devlink
,
760 struct devlink_port
*devlink_port
,
761 enum devlink_port_type port_type
)
766 if (devlink
->ops
->port_type_set
) {
767 if (port_type
== DEVLINK_PORT_TYPE_NOTSET
)
769 if (port_type
== devlink_port
->type
)
771 err
= devlink
->ops
->port_type_set(devlink_port
, port_type
);
774 devlink_port
->desired_type
= port_type
;
775 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
781 static int devlink_nl_cmd_port_set_doit(struct sk_buff
*skb
,
782 struct genl_info
*info
)
784 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
785 struct devlink
*devlink
= devlink_port
->devlink
;
788 if (info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]) {
789 enum devlink_port_type port_type
;
791 port_type
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]);
792 err
= devlink_port_type_set(devlink
, devlink_port
, port_type
);
799 static int devlink_port_split(struct devlink
*devlink
, u32 port_index
,
800 u32 count
, struct netlink_ext_ack
*extack
)
803 if (devlink
->ops
->port_split
)
804 return devlink
->ops
->port_split(devlink
, port_index
, count
,
809 static int devlink_nl_cmd_port_split_doit(struct sk_buff
*skb
,
810 struct genl_info
*info
)
812 struct devlink
*devlink
= info
->user_ptr
[0];
816 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
] ||
817 !info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
])
820 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
821 count
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
]);
822 return devlink_port_split(devlink
, port_index
, count
, info
->extack
);
825 static int devlink_port_unsplit(struct devlink
*devlink
, u32 port_index
,
826 struct netlink_ext_ack
*extack
)
829 if (devlink
->ops
->port_unsplit
)
830 return devlink
->ops
->port_unsplit(devlink
, port_index
, extack
);
834 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff
*skb
,
835 struct genl_info
*info
)
837 struct devlink
*devlink
= info
->user_ptr
[0];
840 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
])
843 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
844 return devlink_port_unsplit(devlink
, port_index
, info
->extack
);
847 static int devlink_nl_sb_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
848 struct devlink_sb
*devlink_sb
,
849 enum devlink_command cmd
, u32 portid
,
854 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
858 if (devlink_nl_put_handle(msg
, devlink
))
859 goto nla_put_failure
;
860 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
861 goto nla_put_failure
;
862 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_SIZE
, devlink_sb
->size
))
863 goto nla_put_failure
;
864 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
,
865 devlink_sb
->ingress_pools_count
))
866 goto nla_put_failure
;
867 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
,
868 devlink_sb
->egress_pools_count
))
869 goto nla_put_failure
;
870 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_TC_COUNT
,
871 devlink_sb
->ingress_tc_count
))
872 goto nla_put_failure
;
873 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_TC_COUNT
,
874 devlink_sb
->egress_tc_count
))
875 goto nla_put_failure
;
877 genlmsg_end(msg
, hdr
);
881 genlmsg_cancel(msg
, hdr
);
885 static int devlink_nl_cmd_sb_get_doit(struct sk_buff
*skb
,
886 struct genl_info
*info
)
888 struct devlink
*devlink
= info
->user_ptr
[0];
889 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
893 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
897 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
899 info
->snd_portid
, info
->snd_seq
, 0);
905 return genlmsg_reply(msg
, info
);
908 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff
*msg
,
909 struct netlink_callback
*cb
)
911 struct devlink
*devlink
;
912 struct devlink_sb
*devlink_sb
;
913 int start
= cb
->args
[0];
917 mutex_lock(&devlink_mutex
);
918 list_for_each_entry(devlink
, &devlink_list
, list
) {
919 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
921 mutex_lock(&devlink
->lock
);
922 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
927 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
929 NETLINK_CB(cb
->skb
).portid
,
933 mutex_unlock(&devlink
->lock
);
938 mutex_unlock(&devlink
->lock
);
941 mutex_unlock(&devlink_mutex
);
947 static int devlink_nl_sb_pool_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
948 struct devlink_sb
*devlink_sb
,
949 u16 pool_index
, enum devlink_command cmd
,
950 u32 portid
, u32 seq
, int flags
)
952 struct devlink_sb_pool_info pool_info
;
956 err
= devlink
->ops
->sb_pool_get(devlink
, devlink_sb
->index
,
957 pool_index
, &pool_info
);
961 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
965 if (devlink_nl_put_handle(msg
, devlink
))
966 goto nla_put_failure
;
967 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
968 goto nla_put_failure
;
969 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
970 goto nla_put_failure
;
971 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_info
.pool_type
))
972 goto nla_put_failure
;
973 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_POOL_SIZE
, pool_info
.size
))
974 goto nla_put_failure
;
975 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
976 pool_info
.threshold_type
))
977 goto nla_put_failure
;
978 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_POOL_CELL_SIZE
,
979 pool_info
.cell_size
))
980 goto nla_put_failure
;
982 genlmsg_end(msg
, hdr
);
986 genlmsg_cancel(msg
, hdr
);
990 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff
*skb
,
991 struct genl_info
*info
)
993 struct devlink
*devlink
= info
->user_ptr
[0];
994 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
999 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1004 if (!devlink
->ops
->sb_pool_get
)
1007 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1011 err
= devlink_nl_sb_pool_fill(msg
, devlink
, devlink_sb
, pool_index
,
1012 DEVLINK_CMD_SB_POOL_NEW
,
1013 info
->snd_portid
, info
->snd_seq
, 0);
1019 return genlmsg_reply(msg
, info
);
1022 static int __sb_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
1023 struct devlink
*devlink
,
1024 struct devlink_sb
*devlink_sb
,
1025 u32 portid
, u32 seq
)
1027 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
1031 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
1032 if (*p_idx
< start
) {
1036 err
= devlink_nl_sb_pool_fill(msg
, devlink
,
1039 DEVLINK_CMD_SB_POOL_NEW
,
1040 portid
, seq
, NLM_F_MULTI
);
1048 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff
*msg
,
1049 struct netlink_callback
*cb
)
1051 struct devlink
*devlink
;
1052 struct devlink_sb
*devlink_sb
;
1053 int start
= cb
->args
[0];
1057 mutex_lock(&devlink_mutex
);
1058 list_for_each_entry(devlink
, &devlink_list
, list
) {
1059 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1060 !devlink
->ops
->sb_pool_get
)
1062 mutex_lock(&devlink
->lock
);
1063 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1064 err
= __sb_pool_get_dumpit(msg
, start
, &idx
, devlink
,
1066 NETLINK_CB(cb
->skb
).portid
,
1067 cb
->nlh
->nlmsg_seq
);
1068 if (err
&& err
!= -EOPNOTSUPP
) {
1069 mutex_unlock(&devlink
->lock
);
1073 mutex_unlock(&devlink
->lock
);
1076 mutex_unlock(&devlink_mutex
);
1078 if (err
!= -EMSGSIZE
)
1085 static int devlink_sb_pool_set(struct devlink
*devlink
, unsigned int sb_index
,
1086 u16 pool_index
, u32 size
,
1087 enum devlink_sb_threshold_type threshold_type
,
1088 struct netlink_ext_ack
*extack
)
1091 const struct devlink_ops
*ops
= devlink
->ops
;
1093 if (ops
->sb_pool_set
)
1094 return ops
->sb_pool_set(devlink
, sb_index
, pool_index
,
1095 size
, threshold_type
, extack
);
1099 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff
*skb
,
1100 struct genl_info
*info
)
1102 struct devlink
*devlink
= info
->user_ptr
[0];
1103 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1104 enum devlink_sb_threshold_type threshold_type
;
1109 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1114 err
= devlink_sb_th_type_get_from_info(info
, &threshold_type
);
1118 if (!info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
])
1121 size
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
]);
1122 return devlink_sb_pool_set(devlink
, devlink_sb
->index
,
1123 pool_index
, size
, threshold_type
,
1127 static int devlink_nl_sb_port_pool_fill(struct sk_buff
*msg
,
1128 struct devlink
*devlink
,
1129 struct devlink_port
*devlink_port
,
1130 struct devlink_sb
*devlink_sb
,
1132 enum devlink_command cmd
,
1133 u32 portid
, u32 seq
, int flags
)
1135 const struct devlink_ops
*ops
= devlink
->ops
;
1140 err
= ops
->sb_port_pool_get(devlink_port
, devlink_sb
->index
,
1141 pool_index
, &threshold
);
1145 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1149 if (devlink_nl_put_handle(msg
, devlink
))
1150 goto nla_put_failure
;
1151 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1152 goto nla_put_failure
;
1153 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1154 goto nla_put_failure
;
1155 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1156 goto nla_put_failure
;
1157 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1158 goto nla_put_failure
;
1160 if (ops
->sb_occ_port_pool_get
) {
1164 err
= ops
->sb_occ_port_pool_get(devlink_port
, devlink_sb
->index
,
1165 pool_index
, &cur
, &max
);
1166 if (err
&& err
!= -EOPNOTSUPP
)
1169 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1170 goto nla_put_failure
;
1171 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1172 goto nla_put_failure
;
1176 genlmsg_end(msg
, hdr
);
1180 genlmsg_cancel(msg
, hdr
);
1184 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff
*skb
,
1185 struct genl_info
*info
)
1187 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1188 struct devlink
*devlink
= devlink_port
->devlink
;
1189 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1190 struct sk_buff
*msg
;
1194 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1199 if (!devlink
->ops
->sb_port_pool_get
)
1202 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1206 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
, devlink_port
,
1207 devlink_sb
, pool_index
,
1208 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1209 info
->snd_portid
, info
->snd_seq
, 0);
1215 return genlmsg_reply(msg
, info
);
1218 static int __sb_port_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
1219 struct devlink
*devlink
,
1220 struct devlink_sb
*devlink_sb
,
1221 u32 portid
, u32 seq
)
1223 struct devlink_port
*devlink_port
;
1224 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
1228 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1229 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
1230 if (*p_idx
< start
) {
1234 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
,
1238 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1249 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff
*msg
,
1250 struct netlink_callback
*cb
)
1252 struct devlink
*devlink
;
1253 struct devlink_sb
*devlink_sb
;
1254 int start
= cb
->args
[0];
1258 mutex_lock(&devlink_mutex
);
1259 list_for_each_entry(devlink
, &devlink_list
, list
) {
1260 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1261 !devlink
->ops
->sb_port_pool_get
)
1263 mutex_lock(&devlink
->lock
);
1264 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1265 err
= __sb_port_pool_get_dumpit(msg
, start
, &idx
,
1266 devlink
, devlink_sb
,
1267 NETLINK_CB(cb
->skb
).portid
,
1268 cb
->nlh
->nlmsg_seq
);
1269 if (err
&& err
!= -EOPNOTSUPP
) {
1270 mutex_unlock(&devlink
->lock
);
1274 mutex_unlock(&devlink
->lock
);
1277 mutex_unlock(&devlink_mutex
);
1279 if (err
!= -EMSGSIZE
)
1286 static int devlink_sb_port_pool_set(struct devlink_port
*devlink_port
,
1287 unsigned int sb_index
, u16 pool_index
,
1289 struct netlink_ext_ack
*extack
)
1292 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1294 if (ops
->sb_port_pool_set
)
1295 return ops
->sb_port_pool_set(devlink_port
, sb_index
,
1296 pool_index
, threshold
, extack
);
1300 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff
*skb
,
1301 struct genl_info
*info
)
1303 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1304 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1309 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1314 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1317 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1318 return devlink_sb_port_pool_set(devlink_port
, devlink_sb
->index
,
1319 pool_index
, threshold
, info
->extack
);
1323 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1324 struct devlink_port
*devlink_port
,
1325 struct devlink_sb
*devlink_sb
, u16 tc_index
,
1326 enum devlink_sb_pool_type pool_type
,
1327 enum devlink_command cmd
,
1328 u32 portid
, u32 seq
, int flags
)
1330 const struct devlink_ops
*ops
= devlink
->ops
;
1336 err
= ops
->sb_tc_pool_bind_get(devlink_port
, devlink_sb
->index
,
1337 tc_index
, pool_type
,
1338 &pool_index
, &threshold
);
1342 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1346 if (devlink_nl_put_handle(msg
, devlink
))
1347 goto nla_put_failure
;
1348 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1349 goto nla_put_failure
;
1350 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1351 goto nla_put_failure
;
1352 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_TC_INDEX
, tc_index
))
1353 goto nla_put_failure
;
1354 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_type
))
1355 goto nla_put_failure
;
1356 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1357 goto nla_put_failure
;
1358 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1359 goto nla_put_failure
;
1361 if (ops
->sb_occ_tc_port_bind_get
) {
1365 err
= ops
->sb_occ_tc_port_bind_get(devlink_port
,
1367 tc_index
, pool_type
,
1369 if (err
&& err
!= -EOPNOTSUPP
)
1372 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1373 goto nla_put_failure
;
1374 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1375 goto nla_put_failure
;
1379 genlmsg_end(msg
, hdr
);
1383 genlmsg_cancel(msg
, hdr
);
1387 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff
*skb
,
1388 struct genl_info
*info
)
1390 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1391 struct devlink
*devlink
= devlink_port
->devlink
;
1392 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1393 struct sk_buff
*msg
;
1394 enum devlink_sb_pool_type pool_type
;
1398 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1402 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1403 pool_type
, &tc_index
);
1407 if (!devlink
->ops
->sb_tc_pool_bind_get
)
1410 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1414 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
, devlink_port
,
1415 devlink_sb
, tc_index
, pool_type
,
1416 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1424 return genlmsg_reply(msg
, info
);
1427 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1428 int start
, int *p_idx
,
1429 struct devlink
*devlink
,
1430 struct devlink_sb
*devlink_sb
,
1431 u32 portid
, u32 seq
)
1433 struct devlink_port
*devlink_port
;
1437 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1439 tc_index
< devlink_sb
->ingress_tc_count
; tc_index
++) {
1440 if (*p_idx
< start
) {
1444 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1448 DEVLINK_SB_POOL_TYPE_INGRESS
,
1449 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1457 tc_index
< devlink_sb
->egress_tc_count
; tc_index
++) {
1458 if (*p_idx
< start
) {
1462 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1466 DEVLINK_SB_POOL_TYPE_EGRESS
,
1467 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1479 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1480 struct netlink_callback
*cb
)
1482 struct devlink
*devlink
;
1483 struct devlink_sb
*devlink_sb
;
1484 int start
= cb
->args
[0];
1488 mutex_lock(&devlink_mutex
);
1489 list_for_each_entry(devlink
, &devlink_list
, list
) {
1490 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1491 !devlink
->ops
->sb_tc_pool_bind_get
)
1494 mutex_lock(&devlink
->lock
);
1495 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1496 err
= __sb_tc_pool_bind_get_dumpit(msg
, start
, &idx
,
1499 NETLINK_CB(cb
->skb
).portid
,
1500 cb
->nlh
->nlmsg_seq
);
1501 if (err
&& err
!= -EOPNOTSUPP
) {
1502 mutex_unlock(&devlink
->lock
);
1506 mutex_unlock(&devlink
->lock
);
1509 mutex_unlock(&devlink_mutex
);
1511 if (err
!= -EMSGSIZE
)
1518 static int devlink_sb_tc_pool_bind_set(struct devlink_port
*devlink_port
,
1519 unsigned int sb_index
, u16 tc_index
,
1520 enum devlink_sb_pool_type pool_type
,
1521 u16 pool_index
, u32 threshold
,
1522 struct netlink_ext_ack
*extack
)
1525 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1527 if (ops
->sb_tc_pool_bind_set
)
1528 return ops
->sb_tc_pool_bind_set(devlink_port
, sb_index
,
1529 tc_index
, pool_type
,
1530 pool_index
, threshold
, extack
);
1534 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff
*skb
,
1535 struct genl_info
*info
)
1537 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
1538 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1539 enum devlink_sb_pool_type pool_type
;
1545 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1549 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1550 pool_type
, &tc_index
);
1554 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1559 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1562 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1563 return devlink_sb_tc_pool_bind_set(devlink_port
, devlink_sb
->index
,
1564 tc_index
, pool_type
,
1565 pool_index
, threshold
, info
->extack
);
1568 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff
*skb
,
1569 struct genl_info
*info
)
1571 struct devlink
*devlink
= info
->user_ptr
[0];
1572 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1573 const struct devlink_ops
*ops
= devlink
->ops
;
1575 if (ops
->sb_occ_snapshot
)
1576 return ops
->sb_occ_snapshot(devlink
, devlink_sb
->index
);
1580 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff
*skb
,
1581 struct genl_info
*info
)
1583 struct devlink
*devlink
= info
->user_ptr
[0];
1584 struct devlink_sb
*devlink_sb
= info
->user_ptr
[1];
1585 const struct devlink_ops
*ops
= devlink
->ops
;
1587 if (ops
->sb_occ_max_clear
)
1588 return ops
->sb_occ_max_clear(devlink
, devlink_sb
->index
);
1592 static int devlink_nl_eswitch_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1593 enum devlink_command cmd
, u32 portid
,
1596 const struct devlink_ops
*ops
= devlink
->ops
;
1597 enum devlink_eswitch_encap_mode encap_mode
;
1603 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1607 err
= devlink_nl_put_handle(msg
, devlink
);
1609 goto nla_put_failure
;
1611 if (ops
->eswitch_mode_get
) {
1612 err
= ops
->eswitch_mode_get(devlink
, &mode
);
1614 goto nla_put_failure
;
1615 err
= nla_put_u16(msg
, DEVLINK_ATTR_ESWITCH_MODE
, mode
);
1617 goto nla_put_failure
;
1620 if (ops
->eswitch_inline_mode_get
) {
1621 err
= ops
->eswitch_inline_mode_get(devlink
, &inline_mode
);
1623 goto nla_put_failure
;
1624 err
= nla_put_u8(msg
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1627 goto nla_put_failure
;
1630 if (ops
->eswitch_encap_mode_get
) {
1631 err
= ops
->eswitch_encap_mode_get(devlink
, &encap_mode
);
1633 goto nla_put_failure
;
1634 err
= nla_put_u8(msg
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
, encap_mode
);
1636 goto nla_put_failure
;
1639 genlmsg_end(msg
, hdr
);
1643 genlmsg_cancel(msg
, hdr
);
1647 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff
*skb
,
1648 struct genl_info
*info
)
1650 struct devlink
*devlink
= info
->user_ptr
[0];
1651 struct sk_buff
*msg
;
1654 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1658 err
= devlink_nl_eswitch_fill(msg
, devlink
, DEVLINK_CMD_ESWITCH_GET
,
1659 info
->snd_portid
, info
->snd_seq
, 0);
1666 return genlmsg_reply(msg
, info
);
1669 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff
*skb
,
1670 struct genl_info
*info
)
1672 struct devlink
*devlink
= info
->user_ptr
[0];
1673 const struct devlink_ops
*ops
= devlink
->ops
;
1674 enum devlink_eswitch_encap_mode encap_mode
;
1679 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_MODE
]) {
1680 if (!ops
->eswitch_mode_set
)
1682 mode
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_ESWITCH_MODE
]);
1683 err
= ops
->eswitch_mode_set(devlink
, mode
, info
->extack
);
1688 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
1689 if (!ops
->eswitch_inline_mode_set
)
1691 inline_mode
= nla_get_u8(
1692 info
->attrs
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]);
1693 err
= ops
->eswitch_inline_mode_set(devlink
, inline_mode
,
1699 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
1700 if (!ops
->eswitch_encap_mode_set
)
1702 encap_mode
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
1703 err
= ops
->eswitch_encap_mode_set(devlink
, encap_mode
,
1712 int devlink_dpipe_match_put(struct sk_buff
*skb
,
1713 struct devlink_dpipe_match
*match
)
1715 struct devlink_dpipe_header
*header
= match
->header
;
1716 struct devlink_dpipe_field
*field
= &header
->fields
[match
->field_id
];
1717 struct nlattr
*match_attr
;
1719 match_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_MATCH
);
1723 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_MATCH_TYPE
, match
->type
) ||
1724 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_INDEX
, match
->header_index
) ||
1725 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
1726 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
1727 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
1728 goto nla_put_failure
;
1730 nla_nest_end(skb
, match_attr
);
1734 nla_nest_cancel(skb
, match_attr
);
1737 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put
);
1739 static int devlink_dpipe_matches_put(struct devlink_dpipe_table
*table
,
1740 struct sk_buff
*skb
)
1742 struct nlattr
*matches_attr
;
1744 matches_attr
= nla_nest_start_noflag(skb
,
1745 DEVLINK_ATTR_DPIPE_TABLE_MATCHES
);
1749 if (table
->table_ops
->matches_dump(table
->priv
, skb
))
1750 goto nla_put_failure
;
1752 nla_nest_end(skb
, matches_attr
);
1756 nla_nest_cancel(skb
, matches_attr
);
1760 int devlink_dpipe_action_put(struct sk_buff
*skb
,
1761 struct devlink_dpipe_action
*action
)
1763 struct devlink_dpipe_header
*header
= action
->header
;
1764 struct devlink_dpipe_field
*field
= &header
->fields
[action
->field_id
];
1765 struct nlattr
*action_attr
;
1767 action_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_ACTION
);
1771 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_ACTION_TYPE
, action
->type
) ||
1772 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_INDEX
, action
->header_index
) ||
1773 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
1774 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
1775 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
1776 goto nla_put_failure
;
1778 nla_nest_end(skb
, action_attr
);
1782 nla_nest_cancel(skb
, action_attr
);
1785 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put
);
1787 static int devlink_dpipe_actions_put(struct devlink_dpipe_table
*table
,
1788 struct sk_buff
*skb
)
1790 struct nlattr
*actions_attr
;
1792 actions_attr
= nla_nest_start_noflag(skb
,
1793 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
);
1797 if (table
->table_ops
->actions_dump(table
->priv
, skb
))
1798 goto nla_put_failure
;
1800 nla_nest_end(skb
, actions_attr
);
1804 nla_nest_cancel(skb
, actions_attr
);
1808 static int devlink_dpipe_table_put(struct sk_buff
*skb
,
1809 struct devlink_dpipe_table
*table
)
1811 struct nlattr
*table_attr
;
1814 table_size
= table
->table_ops
->size_get(table
->priv
);
1815 table_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_TABLE
);
1819 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_TABLE_NAME
, table
->name
) ||
1820 nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_SIZE
, table_size
,
1822 goto nla_put_failure
;
1823 if (nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
1824 table
->counters_enabled
))
1825 goto nla_put_failure
;
1827 if (table
->resource_valid
) {
1828 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
,
1829 table
->resource_id
, DEVLINK_ATTR_PAD
) ||
1830 nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
,
1831 table
->resource_units
, DEVLINK_ATTR_PAD
))
1832 goto nla_put_failure
;
1834 if (devlink_dpipe_matches_put(table
, skb
))
1835 goto nla_put_failure
;
1837 if (devlink_dpipe_actions_put(table
, skb
))
1838 goto nla_put_failure
;
1840 nla_nest_end(skb
, table_attr
);
1844 nla_nest_cancel(skb
, table_attr
);
1848 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff
**pskb
,
1849 struct genl_info
*info
)
1854 err
= genlmsg_reply(*pskb
, info
);
1858 *pskb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1864 static int devlink_dpipe_tables_fill(struct genl_info
*info
,
1865 enum devlink_command cmd
, int flags
,
1866 struct list_head
*dpipe_tables
,
1867 const char *table_name
)
1869 struct devlink
*devlink
= info
->user_ptr
[0];
1870 struct devlink_dpipe_table
*table
;
1871 struct nlattr
*tables_attr
;
1872 struct sk_buff
*skb
= NULL
;
1873 struct nlmsghdr
*nlh
;
1879 table
= list_first_entry(dpipe_tables
,
1880 struct devlink_dpipe_table
, list
);
1882 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
1886 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
1887 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
1893 if (devlink_nl_put_handle(skb
, devlink
))
1894 goto nla_put_failure
;
1895 tables_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_TABLES
);
1897 goto nla_put_failure
;
1901 list_for_each_entry_from(table
, dpipe_tables
, list
) {
1903 err
= devlink_dpipe_table_put(skb
, table
);
1911 if (!strcmp(table
->name
, table_name
)) {
1912 err
= devlink_dpipe_table_put(skb
, table
);
1920 nla_nest_end(skb
, tables_attr
);
1921 genlmsg_end(skb
, hdr
);
1926 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
1927 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
1929 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
1935 return genlmsg_reply(skb
, info
);
1944 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff
*skb
,
1945 struct genl_info
*info
)
1947 struct devlink
*devlink
= info
->user_ptr
[0];
1948 const char *table_name
= NULL
;
1950 if (info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
1951 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
1953 return devlink_dpipe_tables_fill(info
, DEVLINK_CMD_DPIPE_TABLE_GET
, 0,
1954 &devlink
->dpipe_table_list
,
1958 static int devlink_dpipe_value_put(struct sk_buff
*skb
,
1959 struct devlink_dpipe_value
*value
)
1961 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE
,
1962 value
->value_size
, value
->value
))
1965 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE_MASK
,
1966 value
->value_size
, value
->mask
))
1968 if (value
->mapping_valid
)
1969 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_VALUE_MAPPING
,
1970 value
->mapping_value
))
1975 static int devlink_dpipe_action_value_put(struct sk_buff
*skb
,
1976 struct devlink_dpipe_value
*value
)
1980 if (devlink_dpipe_action_put(skb
, value
->action
))
1982 if (devlink_dpipe_value_put(skb
, value
))
1987 static int devlink_dpipe_action_values_put(struct sk_buff
*skb
,
1988 struct devlink_dpipe_value
*values
,
1989 unsigned int values_count
)
1991 struct nlattr
*action_attr
;
1995 for (i
= 0; i
< values_count
; i
++) {
1996 action_attr
= nla_nest_start_noflag(skb
,
1997 DEVLINK_ATTR_DPIPE_ACTION_VALUE
);
2000 err
= devlink_dpipe_action_value_put(skb
, &values
[i
]);
2002 goto err_action_value_put
;
2003 nla_nest_end(skb
, action_attr
);
2007 err_action_value_put
:
2008 nla_nest_cancel(skb
, action_attr
);
2012 static int devlink_dpipe_match_value_put(struct sk_buff
*skb
,
2013 struct devlink_dpipe_value
*value
)
2017 if (devlink_dpipe_match_put(skb
, value
->match
))
2019 if (devlink_dpipe_value_put(skb
, value
))
2024 static int devlink_dpipe_match_values_put(struct sk_buff
*skb
,
2025 struct devlink_dpipe_value
*values
,
2026 unsigned int values_count
)
2028 struct nlattr
*match_attr
;
2032 for (i
= 0; i
< values_count
; i
++) {
2033 match_attr
= nla_nest_start_noflag(skb
,
2034 DEVLINK_ATTR_DPIPE_MATCH_VALUE
);
2037 err
= devlink_dpipe_match_value_put(skb
, &values
[i
]);
2039 goto err_match_value_put
;
2040 nla_nest_end(skb
, match_attr
);
2044 err_match_value_put
:
2045 nla_nest_cancel(skb
, match_attr
);
2049 static int devlink_dpipe_entry_put(struct sk_buff
*skb
,
2050 struct devlink_dpipe_entry
*entry
)
2052 struct nlattr
*entry_attr
, *matches_attr
, *actions_attr
;
2055 entry_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_ENTRY
);
2059 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_INDEX
, entry
->index
,
2061 goto nla_put_failure
;
2062 if (entry
->counter_valid
)
2063 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
,
2064 entry
->counter
, DEVLINK_ATTR_PAD
))
2065 goto nla_put_failure
;
2067 matches_attr
= nla_nest_start_noflag(skb
,
2068 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
);
2070 goto nla_put_failure
;
2072 err
= devlink_dpipe_match_values_put(skb
, entry
->match_values
,
2073 entry
->match_values_count
);
2075 nla_nest_cancel(skb
, matches_attr
);
2076 goto err_match_values_put
;
2078 nla_nest_end(skb
, matches_attr
);
2080 actions_attr
= nla_nest_start_noflag(skb
,
2081 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
);
2083 goto nla_put_failure
;
2085 err
= devlink_dpipe_action_values_put(skb
, entry
->action_values
,
2086 entry
->action_values_count
);
2088 nla_nest_cancel(skb
, actions_attr
);
2089 goto err_action_values_put
;
2091 nla_nest_end(skb
, actions_attr
);
2093 nla_nest_end(skb
, entry_attr
);
2098 err_match_values_put
:
2099 err_action_values_put
:
2100 nla_nest_cancel(skb
, entry_attr
);
2104 static struct devlink_dpipe_table
*
2105 devlink_dpipe_table_find(struct list_head
*dpipe_tables
,
2106 const char *table_name
, struct devlink
*devlink
)
2108 struct devlink_dpipe_table
*table
;
2109 list_for_each_entry_rcu(table
, dpipe_tables
, list
,
2110 lockdep_is_held(&devlink
->lock
)) {
2111 if (!strcmp(table
->name
, table_name
))
2117 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx
*dump_ctx
)
2119 struct devlink
*devlink
;
2122 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
->skb
,
2127 dump_ctx
->hdr
= genlmsg_put(dump_ctx
->skb
,
2128 dump_ctx
->info
->snd_portid
,
2129 dump_ctx
->info
->snd_seq
,
2130 &devlink_nl_family
, NLM_F_MULTI
,
2133 goto nla_put_failure
;
2135 devlink
= dump_ctx
->info
->user_ptr
[0];
2136 if (devlink_nl_put_handle(dump_ctx
->skb
, devlink
))
2137 goto nla_put_failure
;
2138 dump_ctx
->nest
= nla_nest_start_noflag(dump_ctx
->skb
,
2139 DEVLINK_ATTR_DPIPE_ENTRIES
);
2140 if (!dump_ctx
->nest
)
2141 goto nla_put_failure
;
2145 nlmsg_free(dump_ctx
->skb
);
2148 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare
);
2150 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx
*dump_ctx
,
2151 struct devlink_dpipe_entry
*entry
)
2153 return devlink_dpipe_entry_put(dump_ctx
->skb
, entry
);
2155 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append
);
2157 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx
*dump_ctx
)
2159 nla_nest_end(dump_ctx
->skb
, dump_ctx
->nest
);
2160 genlmsg_end(dump_ctx
->skb
, dump_ctx
->hdr
);
2163 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close
);
2165 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry
*entry
)
2168 unsigned int value_count
, value_index
;
2169 struct devlink_dpipe_value
*value
;
2171 value
= entry
->action_values
;
2172 value_count
= entry
->action_values_count
;
2173 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2174 kfree(value
[value_index
].value
);
2175 kfree(value
[value_index
].mask
);
2178 value
= entry
->match_values
;
2179 value_count
= entry
->match_values_count
;
2180 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2181 kfree(value
[value_index
].value
);
2182 kfree(value
[value_index
].mask
);
2185 EXPORT_SYMBOL(devlink_dpipe_entry_clear
);
2187 static int devlink_dpipe_entries_fill(struct genl_info
*info
,
2188 enum devlink_command cmd
, int flags
,
2189 struct devlink_dpipe_table
*table
)
2191 struct devlink_dpipe_dump_ctx dump_ctx
;
2192 struct nlmsghdr
*nlh
;
2195 dump_ctx
.skb
= NULL
;
2197 dump_ctx
.info
= info
;
2199 err
= table
->table_ops
->entries_dump(table
->priv
,
2200 table
->counters_enabled
,
2206 nlh
= nlmsg_put(dump_ctx
.skb
, info
->snd_portid
, info
->snd_seq
,
2207 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2209 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
.skb
, info
);
2214 return genlmsg_reply(dump_ctx
.skb
, info
);
2217 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff
*skb
,
2218 struct genl_info
*info
)
2220 struct devlink
*devlink
= info
->user_ptr
[0];
2221 struct devlink_dpipe_table
*table
;
2222 const char *table_name
;
2224 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
2227 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2228 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2229 table_name
, devlink
);
2233 if (!table
->table_ops
->entries_dump
)
2236 return devlink_dpipe_entries_fill(info
, DEVLINK_CMD_DPIPE_ENTRIES_GET
,
2240 static int devlink_dpipe_fields_put(struct sk_buff
*skb
,
2241 const struct devlink_dpipe_header
*header
)
2243 struct devlink_dpipe_field
*field
;
2244 struct nlattr
*field_attr
;
2247 for (i
= 0; i
< header
->fields_count
; i
++) {
2248 field
= &header
->fields
[i
];
2249 field_attr
= nla_nest_start_noflag(skb
,
2250 DEVLINK_ATTR_DPIPE_FIELD
);
2253 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_FIELD_NAME
, field
->name
) ||
2254 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
2255 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
, field
->bitwidth
) ||
2256 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
, field
->mapping_type
))
2257 goto nla_put_failure
;
2258 nla_nest_end(skb
, field_attr
);
2263 nla_nest_cancel(skb
, field_attr
);
2267 static int devlink_dpipe_header_put(struct sk_buff
*skb
,
2268 struct devlink_dpipe_header
*header
)
2270 struct nlattr
*fields_attr
, *header_attr
;
2273 header_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_HEADER
);
2277 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_HEADER_NAME
, header
->name
) ||
2278 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
2279 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
2280 goto nla_put_failure
;
2282 fields_attr
= nla_nest_start_noflag(skb
,
2283 DEVLINK_ATTR_DPIPE_HEADER_FIELDS
);
2285 goto nla_put_failure
;
2287 err
= devlink_dpipe_fields_put(skb
, header
);
2289 nla_nest_cancel(skb
, fields_attr
);
2290 goto nla_put_failure
;
2292 nla_nest_end(skb
, fields_attr
);
2293 nla_nest_end(skb
, header_attr
);
2298 nla_nest_cancel(skb
, header_attr
);
2302 static int devlink_dpipe_headers_fill(struct genl_info
*info
,
2303 enum devlink_command cmd
, int flags
,
2304 struct devlink_dpipe_headers
*
2307 struct devlink
*devlink
= info
->user_ptr
[0];
2308 struct nlattr
*headers_attr
;
2309 struct sk_buff
*skb
= NULL
;
2310 struct nlmsghdr
*nlh
;
2317 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2321 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2322 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2328 if (devlink_nl_put_handle(skb
, devlink
))
2329 goto nla_put_failure
;
2330 headers_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_HEADERS
);
2332 goto nla_put_failure
;
2335 for (; i
< dpipe_headers
->headers_count
; i
++) {
2336 err
= devlink_dpipe_header_put(skb
, dpipe_headers
->headers
[i
]);
2344 nla_nest_end(skb
, headers_attr
);
2345 genlmsg_end(skb
, hdr
);
2346 if (i
!= dpipe_headers
->headers_count
)
2350 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2351 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2353 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2358 return genlmsg_reply(skb
, info
);
2367 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff
*skb
,
2368 struct genl_info
*info
)
2370 struct devlink
*devlink
= info
->user_ptr
[0];
2372 if (!devlink
->dpipe_headers
)
2374 return devlink_dpipe_headers_fill(info
, DEVLINK_CMD_DPIPE_HEADERS_GET
,
2375 0, devlink
->dpipe_headers
);
2378 static int devlink_dpipe_table_counters_set(struct devlink
*devlink
,
2379 const char *table_name
,
2382 struct devlink_dpipe_table
*table
;
2384 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2385 table_name
, devlink
);
2389 if (table
->counter_control_extern
)
2392 if (!(table
->counters_enabled
^ enable
))
2395 table
->counters_enabled
= enable
;
2396 if (table
->table_ops
->counters_set_update
)
2397 table
->table_ops
->counters_set_update(table
->priv
, enable
);
2401 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff
*skb
,
2402 struct genl_info
*info
)
2404 struct devlink
*devlink
= info
->user_ptr
[0];
2405 const char *table_name
;
2406 bool counters_enable
;
2408 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
2409 !info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
])
2412 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2413 counters_enable
= !!nla_get_u8(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
2415 return devlink_dpipe_table_counters_set(devlink
, table_name
,
2419 static struct devlink_resource
*
2420 devlink_resource_find(struct devlink
*devlink
,
2421 struct devlink_resource
*resource
, u64 resource_id
)
2423 struct list_head
*resource_list
;
2426 resource_list
= &resource
->resource_list
;
2428 resource_list
= &devlink
->resource_list
;
2430 list_for_each_entry(resource
, resource_list
, list
) {
2431 struct devlink_resource
*child_resource
;
2433 if (resource
->id
== resource_id
)
2436 child_resource
= devlink_resource_find(devlink
, resource
,
2439 return child_resource
;
2445 devlink_resource_validate_children(struct devlink_resource
*resource
)
2447 struct devlink_resource
*child_resource
;
2448 bool size_valid
= true;
2451 if (list_empty(&resource
->resource_list
))
2454 list_for_each_entry(child_resource
, &resource
->resource_list
, list
)
2455 parts_size
+= child_resource
->size_new
;
2457 if (parts_size
> resource
->size_new
)
2460 resource
->size_valid
= size_valid
;
2464 devlink_resource_validate_size(struct devlink_resource
*resource
, u64 size
,
2465 struct netlink_ext_ack
*extack
)
2470 if (size
> resource
->size_params
.size_max
) {
2471 NL_SET_ERR_MSG_MOD(extack
, "Size larger than maximum");
2475 if (size
< resource
->size_params
.size_min
) {
2476 NL_SET_ERR_MSG_MOD(extack
, "Size smaller than minimum");
2480 div64_u64_rem(size
, resource
->size_params
.size_granularity
, &reminder
);
2482 NL_SET_ERR_MSG_MOD(extack
, "Wrong granularity");
2489 static int devlink_nl_cmd_resource_set(struct sk_buff
*skb
,
2490 struct genl_info
*info
)
2492 struct devlink
*devlink
= info
->user_ptr
[0];
2493 struct devlink_resource
*resource
;
2498 if (!info
->attrs
[DEVLINK_ATTR_RESOURCE_ID
] ||
2499 !info
->attrs
[DEVLINK_ATTR_RESOURCE_SIZE
])
2501 resource_id
= nla_get_u64(info
->attrs
[DEVLINK_ATTR_RESOURCE_ID
]);
2503 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
2507 size
= nla_get_u64(info
->attrs
[DEVLINK_ATTR_RESOURCE_SIZE
]);
2508 err
= devlink_resource_validate_size(resource
, size
, info
->extack
);
2512 resource
->size_new
= size
;
2513 devlink_resource_validate_children(resource
);
2514 if (resource
->parent
)
2515 devlink_resource_validate_children(resource
->parent
);
2520 devlink_resource_size_params_put(struct devlink_resource
*resource
,
2521 struct sk_buff
*skb
)
2523 struct devlink_resource_size_params
*size_params
;
2525 size_params
= &resource
->size_params
;
2526 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_GRAN
,
2527 size_params
->size_granularity
, DEVLINK_ATTR_PAD
) ||
2528 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_MAX
,
2529 size_params
->size_max
, DEVLINK_ATTR_PAD
) ||
2530 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_MIN
,
2531 size_params
->size_min
, DEVLINK_ATTR_PAD
) ||
2532 nla_put_u8(skb
, DEVLINK_ATTR_RESOURCE_UNIT
, size_params
->unit
))
2537 static int devlink_resource_occ_put(struct devlink_resource
*resource
,
2538 struct sk_buff
*skb
)
2540 if (!resource
->occ_get
)
2542 return nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_OCC
,
2543 resource
->occ_get(resource
->occ_get_priv
),
2547 static int devlink_resource_put(struct devlink
*devlink
, struct sk_buff
*skb
,
2548 struct devlink_resource
*resource
)
2550 struct devlink_resource
*child_resource
;
2551 struct nlattr
*child_resource_attr
;
2552 struct nlattr
*resource_attr
;
2554 resource_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_RESOURCE
);
2558 if (nla_put_string(skb
, DEVLINK_ATTR_RESOURCE_NAME
, resource
->name
) ||
2559 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE
, resource
->size
,
2560 DEVLINK_ATTR_PAD
) ||
2561 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_ID
, resource
->id
,
2563 goto nla_put_failure
;
2564 if (resource
->size
!= resource
->size_new
)
2565 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_NEW
,
2566 resource
->size_new
, DEVLINK_ATTR_PAD
);
2567 if (devlink_resource_occ_put(resource
, skb
))
2568 goto nla_put_failure
;
2569 if (devlink_resource_size_params_put(resource
, skb
))
2570 goto nla_put_failure
;
2571 if (list_empty(&resource
->resource_list
))
2574 if (nla_put_u8(skb
, DEVLINK_ATTR_RESOURCE_SIZE_VALID
,
2575 resource
->size_valid
))
2576 goto nla_put_failure
;
2578 child_resource_attr
= nla_nest_start_noflag(skb
,
2579 DEVLINK_ATTR_RESOURCE_LIST
);
2580 if (!child_resource_attr
)
2581 goto nla_put_failure
;
2583 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
2584 if (devlink_resource_put(devlink
, skb
, child_resource
))
2585 goto resource_put_failure
;
2588 nla_nest_end(skb
, child_resource_attr
);
2590 nla_nest_end(skb
, resource_attr
);
2593 resource_put_failure
:
2594 nla_nest_cancel(skb
, child_resource_attr
);
2596 nla_nest_cancel(skb
, resource_attr
);
2600 static int devlink_resource_fill(struct genl_info
*info
,
2601 enum devlink_command cmd
, int flags
)
2603 struct devlink
*devlink
= info
->user_ptr
[0];
2604 struct devlink_resource
*resource
;
2605 struct nlattr
*resources_attr
;
2606 struct sk_buff
*skb
= NULL
;
2607 struct nlmsghdr
*nlh
;
2613 resource
= list_first_entry(&devlink
->resource_list
,
2614 struct devlink_resource
, list
);
2616 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2620 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2621 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2627 if (devlink_nl_put_handle(skb
, devlink
))
2628 goto nla_put_failure
;
2630 resources_attr
= nla_nest_start_noflag(skb
,
2631 DEVLINK_ATTR_RESOURCE_LIST
);
2632 if (!resources_attr
)
2633 goto nla_put_failure
;
2637 list_for_each_entry_from(resource
, &devlink
->resource_list
, list
) {
2638 err
= devlink_resource_put(devlink
, skb
, resource
);
2641 goto err_resource_put
;
2647 nla_nest_end(skb
, resources_attr
);
2648 genlmsg_end(skb
, hdr
);
2652 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2653 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2655 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2660 return genlmsg_reply(skb
, info
);
2669 static int devlink_nl_cmd_resource_dump(struct sk_buff
*skb
,
2670 struct genl_info
*info
)
2672 struct devlink
*devlink
= info
->user_ptr
[0];
2674 if (list_empty(&devlink
->resource_list
))
2677 return devlink_resource_fill(info
, DEVLINK_CMD_RESOURCE_DUMP
, 0);
2681 devlink_resources_validate(struct devlink
*devlink
,
2682 struct devlink_resource
*resource
,
2683 struct genl_info
*info
)
2685 struct list_head
*resource_list
;
2689 resource_list
= &resource
->resource_list
;
2691 resource_list
= &devlink
->resource_list
;
2693 list_for_each_entry(resource
, resource_list
, list
) {
2694 if (!resource
->size_valid
)
2696 err
= devlink_resources_validate(devlink
, resource
, info
);
2703 static struct net
*devlink_netns_get(struct sk_buff
*skb
,
2704 struct genl_info
*info
)
2706 struct nlattr
*netns_pid_attr
= info
->attrs
[DEVLINK_ATTR_NETNS_PID
];
2707 struct nlattr
*netns_fd_attr
= info
->attrs
[DEVLINK_ATTR_NETNS_FD
];
2708 struct nlattr
*netns_id_attr
= info
->attrs
[DEVLINK_ATTR_NETNS_ID
];
2711 if (!!netns_pid_attr
+ !!netns_fd_attr
+ !!netns_id_attr
> 1) {
2712 NL_SET_ERR_MSG_MOD(info
->extack
, "multiple netns identifying attributes specified");
2713 return ERR_PTR(-EINVAL
);
2716 if (netns_pid_attr
) {
2717 net
= get_net_ns_by_pid(nla_get_u32(netns_pid_attr
));
2718 } else if (netns_fd_attr
) {
2719 net
= get_net_ns_by_fd(nla_get_u32(netns_fd_attr
));
2720 } else if (netns_id_attr
) {
2721 net
= get_net_ns_by_id(sock_net(skb
->sk
),
2722 nla_get_u32(netns_id_attr
));
2724 net
= ERR_PTR(-EINVAL
);
2727 net
= ERR_PTR(-EINVAL
);
2730 NL_SET_ERR_MSG_MOD(info
->extack
, "Unknown network namespace");
2731 return ERR_PTR(-EINVAL
);
2733 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
2735 return ERR_PTR(-EPERM
);
2740 static void devlink_param_notify(struct devlink
*devlink
,
2741 unsigned int port_index
,
2742 struct devlink_param_item
*param_item
,
2743 enum devlink_command cmd
);
2745 static void devlink_reload_netns_change(struct devlink
*devlink
,
2746 struct net
*dest_net
)
2748 struct devlink_param_item
*param_item
;
2750 /* Userspace needs to be notified about devlink objects
2751 * removed from original and entering new network namespace.
2752 * The rest of the devlink objects are re-created during
2753 * reload process so the notifications are generated separatelly.
2756 list_for_each_entry(param_item
, &devlink
->param_list
, list
)
2757 devlink_param_notify(devlink
, 0, param_item
,
2758 DEVLINK_CMD_PARAM_DEL
);
2759 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
2761 __devlink_net_set(devlink
, dest_net
);
2763 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
2764 list_for_each_entry(param_item
, &devlink
->param_list
, list
)
2765 devlink_param_notify(devlink
, 0, param_item
,
2766 DEVLINK_CMD_PARAM_NEW
);
2769 static bool devlink_reload_supported(struct devlink
*devlink
)
2771 return devlink
->ops
->reload_down
&& devlink
->ops
->reload_up
;
2774 static void devlink_reload_failed_set(struct devlink
*devlink
,
2777 if (devlink
->reload_failed
== reload_failed
)
2779 devlink
->reload_failed
= reload_failed
;
2780 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
2783 bool devlink_is_reload_failed(const struct devlink
*devlink
)
2785 return devlink
->reload_failed
;
2787 EXPORT_SYMBOL_GPL(devlink_is_reload_failed
);
2789 static int devlink_reload(struct devlink
*devlink
, struct net
*dest_net
,
2790 struct netlink_ext_ack
*extack
)
2794 if (!devlink
->reload_enabled
)
2797 err
= devlink
->ops
->reload_down(devlink
, !!dest_net
, extack
);
2801 if (dest_net
&& !net_eq(dest_net
, devlink_net(devlink
)))
2802 devlink_reload_netns_change(devlink
, dest_net
);
2804 err
= devlink
->ops
->reload_up(devlink
, extack
);
2805 devlink_reload_failed_set(devlink
, !!err
);
2809 static int devlink_nl_cmd_reload(struct sk_buff
*skb
, struct genl_info
*info
)
2811 struct devlink
*devlink
= info
->user_ptr
[0];
2812 struct net
*dest_net
= NULL
;
2815 if (!devlink_reload_supported(devlink
) || !devlink
->reload_enabled
)
2818 err
= devlink_resources_validate(devlink
, NULL
, info
);
2820 NL_SET_ERR_MSG_MOD(info
->extack
, "resources size validation failed");
2824 if (info
->attrs
[DEVLINK_ATTR_NETNS_PID
] ||
2825 info
->attrs
[DEVLINK_ATTR_NETNS_FD
] ||
2826 info
->attrs
[DEVLINK_ATTR_NETNS_ID
]) {
2827 dest_net
= devlink_netns_get(skb
, info
);
2828 if (IS_ERR(dest_net
))
2829 return PTR_ERR(dest_net
);
2832 err
= devlink_reload(devlink
, dest_net
, info
->extack
);
2840 static int devlink_nl_flash_update_fill(struct sk_buff
*msg
,
2841 struct devlink
*devlink
,
2842 enum devlink_command cmd
,
2843 const char *status_msg
,
2844 const char *component
,
2845 unsigned long done
, unsigned long total
)
2849 hdr
= genlmsg_put(msg
, 0, 0, &devlink_nl_family
, 0, cmd
);
2853 if (devlink_nl_put_handle(msg
, devlink
))
2854 goto nla_put_failure
;
2856 if (cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
)
2860 nla_put_string(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
,
2862 goto nla_put_failure
;
2864 nla_put_string(msg
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
2866 goto nla_put_failure
;
2867 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
,
2868 done
, DEVLINK_ATTR_PAD
))
2869 goto nla_put_failure
;
2870 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
,
2871 total
, DEVLINK_ATTR_PAD
))
2872 goto nla_put_failure
;
2875 genlmsg_end(msg
, hdr
);
2879 genlmsg_cancel(msg
, hdr
);
2883 static void __devlink_flash_update_notify(struct devlink
*devlink
,
2884 enum devlink_command cmd
,
2885 const char *status_msg
,
2886 const char *component
,
2888 unsigned long total
)
2890 struct sk_buff
*msg
;
2893 WARN_ON(cmd
!= DEVLINK_CMD_FLASH_UPDATE
&&
2894 cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
&&
2895 cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
);
2897 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2901 err
= devlink_nl_flash_update_fill(msg
, devlink
, cmd
, status_msg
,
2902 component
, done
, total
);
2906 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
2907 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
2914 void devlink_flash_update_begin_notify(struct devlink
*devlink
)
2916 __devlink_flash_update_notify(devlink
,
2917 DEVLINK_CMD_FLASH_UPDATE
,
2920 EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify
);
2922 void devlink_flash_update_end_notify(struct devlink
*devlink
)
2924 __devlink_flash_update_notify(devlink
,
2925 DEVLINK_CMD_FLASH_UPDATE_END
,
2928 EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify
);
2930 void devlink_flash_update_status_notify(struct devlink
*devlink
,
2931 const char *status_msg
,
2932 const char *component
,
2934 unsigned long total
)
2936 __devlink_flash_update_notify(devlink
,
2937 DEVLINK_CMD_FLASH_UPDATE_STATUS
,
2938 status_msg
, component
, done
, total
);
2940 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify
);
2942 static int devlink_nl_cmd_flash_update(struct sk_buff
*skb
,
2943 struct genl_info
*info
)
2945 struct devlink
*devlink
= info
->user_ptr
[0];
2946 const char *file_name
, *component
;
2947 struct nlattr
*nla_component
;
2949 if (!devlink
->ops
->flash_update
)
2952 if (!info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
])
2954 file_name
= nla_data(info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
]);
2956 nla_component
= info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
];
2957 component
= nla_component
? nla_data(nla_component
) : NULL
;
2959 return devlink
->ops
->flash_update(devlink
, file_name
, component
,
2963 static const struct devlink_param devlink_param_generic
[] = {
2965 .id
= DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET
,
2966 .name
= DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME
,
2967 .type
= DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE
,
2970 .id
= DEVLINK_PARAM_GENERIC_ID_MAX_MACS
,
2971 .name
= DEVLINK_PARAM_GENERIC_MAX_MACS_NAME
,
2972 .type
= DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE
,
2975 .id
= DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV
,
2976 .name
= DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME
,
2977 .type
= DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE
,
2980 .id
= DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT
,
2981 .name
= DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME
,
2982 .type
= DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE
,
2985 .id
= DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI
,
2986 .name
= DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME
,
2987 .type
= DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE
,
2990 .id
= DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX
,
2991 .name
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME
,
2992 .type
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE
,
2995 .id
= DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN
,
2996 .name
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME
,
2997 .type
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE
,
3000 .id
= DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY
,
3001 .name
= DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME
,
3002 .type
= DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE
,
3005 .id
= DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE
,
3006 .name
= DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME
,
3007 .type
= DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE
,
3010 .id
= DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE
,
3011 .name
= DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME
,
3012 .type
= DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE
,
3016 static int devlink_param_generic_verify(const struct devlink_param
*param
)
3018 /* verify it match generic parameter by id and name */
3019 if (param
->id
> DEVLINK_PARAM_GENERIC_ID_MAX
)
3021 if (strcmp(param
->name
, devlink_param_generic
[param
->id
].name
))
3024 WARN_ON(param
->type
!= devlink_param_generic
[param
->id
].type
);
3029 static int devlink_param_driver_verify(const struct devlink_param
*param
)
3033 if (param
->id
<= DEVLINK_PARAM_GENERIC_ID_MAX
)
3035 /* verify no such name in generic params */
3036 for (i
= 0; i
<= DEVLINK_PARAM_GENERIC_ID_MAX
; i
++)
3037 if (!strcmp(param
->name
, devlink_param_generic
[i
].name
))
3043 static struct devlink_param_item
*
3044 devlink_param_find_by_name(struct list_head
*param_list
,
3045 const char *param_name
)
3047 struct devlink_param_item
*param_item
;
3049 list_for_each_entry(param_item
, param_list
, list
)
3050 if (!strcmp(param_item
->param
->name
, param_name
))
3055 static struct devlink_param_item
*
3056 devlink_param_find_by_id(struct list_head
*param_list
, u32 param_id
)
3058 struct devlink_param_item
*param_item
;
3060 list_for_each_entry(param_item
, param_list
, list
)
3061 if (param_item
->param
->id
== param_id
)
3067 devlink_param_cmode_is_supported(const struct devlink_param
*param
,
3068 enum devlink_param_cmode cmode
)
3070 return test_bit(cmode
, ¶m
->supported_cmodes
);
3073 static int devlink_param_get(struct devlink
*devlink
,
3074 const struct devlink_param
*param
,
3075 struct devlink_param_gset_ctx
*ctx
)
3079 return param
->get(devlink
, param
->id
, ctx
);
3082 static int devlink_param_set(struct devlink
*devlink
,
3083 const struct devlink_param
*param
,
3084 struct devlink_param_gset_ctx
*ctx
)
3088 return param
->set(devlink
, param
->id
, ctx
);
3092 devlink_param_type_to_nla_type(enum devlink_param_type param_type
)
3094 switch (param_type
) {
3095 case DEVLINK_PARAM_TYPE_U8
:
3097 case DEVLINK_PARAM_TYPE_U16
:
3099 case DEVLINK_PARAM_TYPE_U32
:
3101 case DEVLINK_PARAM_TYPE_STRING
:
3103 case DEVLINK_PARAM_TYPE_BOOL
:
3111 devlink_nl_param_value_fill_one(struct sk_buff
*msg
,
3112 enum devlink_param_type type
,
3113 enum devlink_param_cmode cmode
,
3114 union devlink_param_value val
)
3116 struct nlattr
*param_value_attr
;
3118 param_value_attr
= nla_nest_start_noflag(msg
,
3119 DEVLINK_ATTR_PARAM_VALUE
);
3120 if (!param_value_attr
)
3121 goto nla_put_failure
;
3123 if (nla_put_u8(msg
, DEVLINK_ATTR_PARAM_VALUE_CMODE
, cmode
))
3124 goto value_nest_cancel
;
3127 case DEVLINK_PARAM_TYPE_U8
:
3128 if (nla_put_u8(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val
.vu8
))
3129 goto value_nest_cancel
;
3131 case DEVLINK_PARAM_TYPE_U16
:
3132 if (nla_put_u16(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val
.vu16
))
3133 goto value_nest_cancel
;
3135 case DEVLINK_PARAM_TYPE_U32
:
3136 if (nla_put_u32(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val
.vu32
))
3137 goto value_nest_cancel
;
3139 case DEVLINK_PARAM_TYPE_STRING
:
3140 if (nla_put_string(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
3142 goto value_nest_cancel
;
3144 case DEVLINK_PARAM_TYPE_BOOL
:
3146 nla_put_flag(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
))
3147 goto value_nest_cancel
;
3151 nla_nest_end(msg
, param_value_attr
);
3155 nla_nest_cancel(msg
, param_value_attr
);
3160 static int devlink_nl_param_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
3161 unsigned int port_index
,
3162 struct devlink_param_item
*param_item
,
3163 enum devlink_command cmd
,
3164 u32 portid
, u32 seq
, int flags
)
3166 union devlink_param_value param_value
[DEVLINK_PARAM_CMODE_MAX
+ 1];
3167 bool param_value_set
[DEVLINK_PARAM_CMODE_MAX
+ 1] = {};
3168 const struct devlink_param
*param
= param_item
->param
;
3169 struct devlink_param_gset_ctx ctx
;
3170 struct nlattr
*param_values_list
;
3171 struct nlattr
*param_attr
;
3177 /* Get value from driver part to driverinit configuration mode */
3178 for (i
= 0; i
<= DEVLINK_PARAM_CMODE_MAX
; i
++) {
3179 if (!devlink_param_cmode_is_supported(param
, i
))
3181 if (i
== DEVLINK_PARAM_CMODE_DRIVERINIT
) {
3182 if (!param_item
->driverinit_value_valid
)
3184 param_value
[i
] = param_item
->driverinit_value
;
3186 if (!param_item
->published
)
3189 err
= devlink_param_get(devlink
, param
, &ctx
);
3192 param_value
[i
] = ctx
.val
;
3194 param_value_set
[i
] = true;
3197 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
3201 if (devlink_nl_put_handle(msg
, devlink
))
3202 goto genlmsg_cancel
;
3204 if (cmd
== DEVLINK_CMD_PORT_PARAM_GET
||
3205 cmd
== DEVLINK_CMD_PORT_PARAM_NEW
||
3206 cmd
== DEVLINK_CMD_PORT_PARAM_DEL
)
3207 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, port_index
))
3208 goto genlmsg_cancel
;
3210 param_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_PARAM
);
3212 goto genlmsg_cancel
;
3213 if (nla_put_string(msg
, DEVLINK_ATTR_PARAM_NAME
, param
->name
))
3214 goto param_nest_cancel
;
3215 if (param
->generic
&& nla_put_flag(msg
, DEVLINK_ATTR_PARAM_GENERIC
))
3216 goto param_nest_cancel
;
3218 nla_type
= devlink_param_type_to_nla_type(param
->type
);
3220 goto param_nest_cancel
;
3221 if (nla_put_u8(msg
, DEVLINK_ATTR_PARAM_TYPE
, nla_type
))
3222 goto param_nest_cancel
;
3224 param_values_list
= nla_nest_start_noflag(msg
,
3225 DEVLINK_ATTR_PARAM_VALUES_LIST
);
3226 if (!param_values_list
)
3227 goto param_nest_cancel
;
3229 for (i
= 0; i
<= DEVLINK_PARAM_CMODE_MAX
; i
++) {
3230 if (!param_value_set
[i
])
3232 err
= devlink_nl_param_value_fill_one(msg
, param
->type
,
3235 goto values_list_nest_cancel
;
3238 nla_nest_end(msg
, param_values_list
);
3239 nla_nest_end(msg
, param_attr
);
3240 genlmsg_end(msg
, hdr
);
3243 values_list_nest_cancel
:
3244 nla_nest_end(msg
, param_values_list
);
3246 nla_nest_cancel(msg
, param_attr
);
3248 genlmsg_cancel(msg
, hdr
);
3252 static void devlink_param_notify(struct devlink
*devlink
,
3253 unsigned int port_index
,
3254 struct devlink_param_item
*param_item
,
3255 enum devlink_command cmd
)
3257 struct sk_buff
*msg
;
3260 WARN_ON(cmd
!= DEVLINK_CMD_PARAM_NEW
&& cmd
!= DEVLINK_CMD_PARAM_DEL
&&
3261 cmd
!= DEVLINK_CMD_PORT_PARAM_NEW
&&
3262 cmd
!= DEVLINK_CMD_PORT_PARAM_DEL
);
3264 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3267 err
= devlink_nl_param_fill(msg
, devlink
, port_index
, param_item
, cmd
,
3274 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
3275 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
3278 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff
*msg
,
3279 struct netlink_callback
*cb
)
3281 struct devlink_param_item
*param_item
;
3282 struct devlink
*devlink
;
3283 int start
= cb
->args
[0];
3287 mutex_lock(&devlink_mutex
);
3288 list_for_each_entry(devlink
, &devlink_list
, list
) {
3289 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
3291 mutex_lock(&devlink
->lock
);
3292 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
3297 err
= devlink_nl_param_fill(msg
, devlink
, 0, param_item
,
3298 DEVLINK_CMD_PARAM_GET
,
3299 NETLINK_CB(cb
->skb
).portid
,
3302 if (err
&& err
!= -EOPNOTSUPP
) {
3303 mutex_unlock(&devlink
->lock
);
3308 mutex_unlock(&devlink
->lock
);
3311 mutex_unlock(&devlink_mutex
);
3313 if (err
!= -EMSGSIZE
)
3321 devlink_param_type_get_from_info(struct genl_info
*info
,
3322 enum devlink_param_type
*param_type
)
3324 if (!info
->attrs
[DEVLINK_ATTR_PARAM_TYPE
])
3327 switch (nla_get_u8(info
->attrs
[DEVLINK_ATTR_PARAM_TYPE
])) {
3329 *param_type
= DEVLINK_PARAM_TYPE_U8
;
3332 *param_type
= DEVLINK_PARAM_TYPE_U16
;
3335 *param_type
= DEVLINK_PARAM_TYPE_U32
;
3338 *param_type
= DEVLINK_PARAM_TYPE_STRING
;
3341 *param_type
= DEVLINK_PARAM_TYPE_BOOL
;
3351 devlink_param_value_get_from_info(const struct devlink_param
*param
,
3352 struct genl_info
*info
,
3353 union devlink_param_value
*value
)
3355 struct nlattr
*param_data
;
3358 param_data
= info
->attrs
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
3360 if (param
->type
!= DEVLINK_PARAM_TYPE_BOOL
&& !param_data
)
3363 switch (param
->type
) {
3364 case DEVLINK_PARAM_TYPE_U8
:
3365 if (nla_len(param_data
) != sizeof(u8
))
3367 value
->vu8
= nla_get_u8(param_data
);
3369 case DEVLINK_PARAM_TYPE_U16
:
3370 if (nla_len(param_data
) != sizeof(u16
))
3372 value
->vu16
= nla_get_u16(param_data
);
3374 case DEVLINK_PARAM_TYPE_U32
:
3375 if (nla_len(param_data
) != sizeof(u32
))
3377 value
->vu32
= nla_get_u32(param_data
);
3379 case DEVLINK_PARAM_TYPE_STRING
:
3380 len
= strnlen(nla_data(param_data
), nla_len(param_data
));
3381 if (len
== nla_len(param_data
) ||
3382 len
>= __DEVLINK_PARAM_MAX_STRING_VALUE
)
3384 strcpy(value
->vstr
, nla_data(param_data
));
3386 case DEVLINK_PARAM_TYPE_BOOL
:
3387 if (param_data
&& nla_len(param_data
))
3389 value
->vbool
= nla_get_flag(param_data
);
3395 static struct devlink_param_item
*
3396 devlink_param_get_from_info(struct list_head
*param_list
,
3397 struct genl_info
*info
)
3401 if (!info
->attrs
[DEVLINK_ATTR_PARAM_NAME
])
3404 param_name
= nla_data(info
->attrs
[DEVLINK_ATTR_PARAM_NAME
]);
3405 return devlink_param_find_by_name(param_list
, param_name
);
3408 static int devlink_nl_cmd_param_get_doit(struct sk_buff
*skb
,
3409 struct genl_info
*info
)
3411 struct devlink
*devlink
= info
->user_ptr
[0];
3412 struct devlink_param_item
*param_item
;
3413 struct sk_buff
*msg
;
3416 param_item
= devlink_param_get_from_info(&devlink
->param_list
, info
);
3420 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3424 err
= devlink_nl_param_fill(msg
, devlink
, 0, param_item
,
3425 DEVLINK_CMD_PARAM_GET
,
3426 info
->snd_portid
, info
->snd_seq
, 0);
3432 return genlmsg_reply(msg
, info
);
3435 static int __devlink_nl_cmd_param_set_doit(struct devlink
*devlink
,
3436 unsigned int port_index
,
3437 struct list_head
*param_list
,
3438 struct genl_info
*info
,
3439 enum devlink_command cmd
)
3441 enum devlink_param_type param_type
;
3442 struct devlink_param_gset_ctx ctx
;
3443 enum devlink_param_cmode cmode
;
3444 struct devlink_param_item
*param_item
;
3445 const struct devlink_param
*param
;
3446 union devlink_param_value value
;
3449 param_item
= devlink_param_get_from_info(param_list
, info
);
3452 param
= param_item
->param
;
3453 err
= devlink_param_type_get_from_info(info
, ¶m_type
);
3456 if (param_type
!= param
->type
)
3458 err
= devlink_param_value_get_from_info(param
, info
, &value
);
3461 if (param
->validate
) {
3462 err
= param
->validate(devlink
, param
->id
, value
, info
->extack
);
3467 if (!info
->attrs
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])
3469 cmode
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
3470 if (!devlink_param_cmode_is_supported(param
, cmode
))
3473 if (cmode
== DEVLINK_PARAM_CMODE_DRIVERINIT
) {
3474 if (param
->type
== DEVLINK_PARAM_TYPE_STRING
)
3475 strcpy(param_item
->driverinit_value
.vstr
, value
.vstr
);
3477 param_item
->driverinit_value
= value
;
3478 param_item
->driverinit_value_valid
= true;
3484 err
= devlink_param_set(devlink
, param
, &ctx
);
3489 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
3493 static int devlink_nl_cmd_param_set_doit(struct sk_buff
*skb
,
3494 struct genl_info
*info
)
3496 struct devlink
*devlink
= info
->user_ptr
[0];
3498 return __devlink_nl_cmd_param_set_doit(devlink
, 0, &devlink
->param_list
,
3499 info
, DEVLINK_CMD_PARAM_NEW
);
3502 static int devlink_param_register_one(struct devlink
*devlink
,
3503 unsigned int port_index
,
3504 struct list_head
*param_list
,
3505 const struct devlink_param
*param
,
3506 enum devlink_command cmd
)
3508 struct devlink_param_item
*param_item
;
3510 if (devlink_param_find_by_name(param_list
, param
->name
))
3513 if (param
->supported_cmodes
== BIT(DEVLINK_PARAM_CMODE_DRIVERINIT
))
3514 WARN_ON(param
->get
|| param
->set
);
3516 WARN_ON(!param
->get
|| !param
->set
);
3518 param_item
= kzalloc(sizeof(*param_item
), GFP_KERNEL
);
3521 param_item
->param
= param
;
3523 list_add_tail(¶m_item
->list
, param_list
);
3524 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
3528 static void devlink_param_unregister_one(struct devlink
*devlink
,
3529 unsigned int port_index
,
3530 struct list_head
*param_list
,
3531 const struct devlink_param
*param
,
3532 enum devlink_command cmd
)
3534 struct devlink_param_item
*param_item
;
3536 param_item
= devlink_param_find_by_name(param_list
, param
->name
);
3537 WARN_ON(!param_item
);
3538 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
3539 list_del(¶m_item
->list
);
3543 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff
*msg
,
3544 struct netlink_callback
*cb
)
3546 struct devlink_param_item
*param_item
;
3547 struct devlink_port
*devlink_port
;
3548 struct devlink
*devlink
;
3549 int start
= cb
->args
[0];
3553 mutex_lock(&devlink_mutex
);
3554 list_for_each_entry(devlink
, &devlink_list
, list
) {
3555 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
3557 mutex_lock(&devlink
->lock
);
3558 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
3559 list_for_each_entry(param_item
,
3560 &devlink_port
->param_list
, list
) {
3565 err
= devlink_nl_param_fill(msg
,
3566 devlink_port
->devlink
,
3567 devlink_port
->index
, param_item
,
3568 DEVLINK_CMD_PORT_PARAM_GET
,
3569 NETLINK_CB(cb
->skb
).portid
,
3572 if (err
&& err
!= -EOPNOTSUPP
) {
3573 mutex_unlock(&devlink
->lock
);
3579 mutex_unlock(&devlink
->lock
);
3582 mutex_unlock(&devlink_mutex
);
3584 if (err
!= -EMSGSIZE
)
3591 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff
*skb
,
3592 struct genl_info
*info
)
3594 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
3595 struct devlink_param_item
*param_item
;
3596 struct sk_buff
*msg
;
3599 param_item
= devlink_param_get_from_info(&devlink_port
->param_list
,
3604 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3608 err
= devlink_nl_param_fill(msg
, devlink_port
->devlink
,
3609 devlink_port
->index
, param_item
,
3610 DEVLINK_CMD_PORT_PARAM_GET
,
3611 info
->snd_portid
, info
->snd_seq
, 0);
3617 return genlmsg_reply(msg
, info
);
3620 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff
*skb
,
3621 struct genl_info
*info
)
3623 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
3625 return __devlink_nl_cmd_param_set_doit(devlink_port
->devlink
,
3626 devlink_port
->index
,
3627 &devlink_port
->param_list
, info
,
3628 DEVLINK_CMD_PORT_PARAM_NEW
);
3631 static int devlink_nl_region_snapshot_id_put(struct sk_buff
*msg
,
3632 struct devlink
*devlink
,
3633 struct devlink_snapshot
*snapshot
)
3635 struct nlattr
*snap_attr
;
3638 snap_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_REGION_SNAPSHOT
);
3642 err
= nla_put_u32(msg
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
, snapshot
->id
);
3644 goto nla_put_failure
;
3646 nla_nest_end(msg
, snap_attr
);
3650 nla_nest_cancel(msg
, snap_attr
);
3654 static int devlink_nl_region_snapshots_id_put(struct sk_buff
*msg
,
3655 struct devlink
*devlink
,
3656 struct devlink_region
*region
)
3658 struct devlink_snapshot
*snapshot
;
3659 struct nlattr
*snapshots_attr
;
3662 snapshots_attr
= nla_nest_start_noflag(msg
,
3663 DEVLINK_ATTR_REGION_SNAPSHOTS
);
3664 if (!snapshots_attr
)
3667 list_for_each_entry(snapshot
, ®ion
->snapshot_list
, list
) {
3668 err
= devlink_nl_region_snapshot_id_put(msg
, devlink
, snapshot
);
3670 goto nla_put_failure
;
3673 nla_nest_end(msg
, snapshots_attr
);
3677 nla_nest_cancel(msg
, snapshots_attr
);
3681 static int devlink_nl_region_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
3682 enum devlink_command cmd
, u32 portid
,
3684 struct devlink_region
*region
)
3689 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
3693 err
= devlink_nl_put_handle(msg
, devlink
);
3695 goto nla_put_failure
;
3697 err
= nla_put_string(msg
, DEVLINK_ATTR_REGION_NAME
, region
->ops
->name
);
3699 goto nla_put_failure
;
3701 err
= nla_put_u64_64bit(msg
, DEVLINK_ATTR_REGION_SIZE
,
3705 goto nla_put_failure
;
3707 err
= devlink_nl_region_snapshots_id_put(msg
, devlink
, region
);
3709 goto nla_put_failure
;
3711 genlmsg_end(msg
, hdr
);
3715 genlmsg_cancel(msg
, hdr
);
3719 static void devlink_nl_region_notify(struct devlink_region
*region
,
3720 struct devlink_snapshot
*snapshot
,
3721 enum devlink_command cmd
)
3723 struct devlink
*devlink
= region
->devlink
;
3724 struct sk_buff
*msg
;
3728 WARN_ON(cmd
!= DEVLINK_CMD_REGION_NEW
&& cmd
!= DEVLINK_CMD_REGION_DEL
);
3730 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3734 hdr
= genlmsg_put(msg
, 0, 0, &devlink_nl_family
, 0, cmd
);
3738 err
= devlink_nl_put_handle(msg
, devlink
);
3740 goto out_cancel_msg
;
3742 err
= nla_put_string(msg
, DEVLINK_ATTR_REGION_NAME
,
3745 goto out_cancel_msg
;
3748 err
= nla_put_u32(msg
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
3751 goto out_cancel_msg
;
3753 err
= nla_put_u64_64bit(msg
, DEVLINK_ATTR_REGION_SIZE
,
3754 region
->size
, DEVLINK_ATTR_PAD
);
3756 goto out_cancel_msg
;
3758 genlmsg_end(msg
, hdr
);
3760 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
3761 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
3766 genlmsg_cancel(msg
, hdr
);
3772 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
3773 * @devlink: devlink instance
3774 * @id: the snapshot id
3776 * Track when a new snapshot begins using an id. Load the count for the
3777 * given id from the snapshot xarray, increment it, and store it back.
3779 * Called when a new snapshot is created with the given id.
3781 * The id *must* have been previously allocated by
3782 * devlink_region_snapshot_id_get().
3784 * Returns 0 on success, or an error on failure.
3786 static int __devlink_snapshot_id_increment(struct devlink
*devlink
, u32 id
)
3788 unsigned long count
;
3791 lockdep_assert_held(&devlink
->lock
);
3793 p
= xa_load(&devlink
->snapshot_ids
, id
);
3797 if (WARN_ON(!xa_is_value(p
)))
3800 count
= xa_to_value(p
);
3803 return xa_err(xa_store(&devlink
->snapshot_ids
, id
, xa_mk_value(count
),
3808 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
3809 * @devlink: devlink instance
3810 * @id: the snapshot id
3812 * Track when a snapshot is deleted and stops using an id. Load the count
3813 * for the given id from the snapshot xarray, decrement it, and store it
3816 * If the count reaches zero, erase this id from the xarray, freeing it
3817 * up for future re-use by devlink_region_snapshot_id_get().
3819 * Called when a snapshot using the given id is deleted, and when the
3820 * initial allocator of the id is finished using it.
3822 static void __devlink_snapshot_id_decrement(struct devlink
*devlink
, u32 id
)
3824 unsigned long count
;
3827 lockdep_assert_held(&devlink
->lock
);
3829 p
= xa_load(&devlink
->snapshot_ids
, id
);
3833 if (WARN_ON(!xa_is_value(p
)))
3836 count
= xa_to_value(p
);
3840 xa_store(&devlink
->snapshot_ids
, id
, xa_mk_value(count
),
3843 /* If this was the last user, we can erase this id */
3844 xa_erase(&devlink
->snapshot_ids
, id
);
3849 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
3850 * @devlink: devlink instance
3851 * @id: the snapshot id
3853 * Mark the given snapshot id as used by inserting a zero value into the
3856 * This must be called while holding the devlink instance lock. Unlike
3857 * devlink_snapshot_id_get, the initial reference count is zero, not one.
3858 * It is expected that the id will immediately be used before
3859 * releasing the devlink instance lock.
3861 * Returns zero on success, or an error code if the snapshot id could not
3864 static int __devlink_snapshot_id_insert(struct devlink
*devlink
, u32 id
)
3866 lockdep_assert_held(&devlink
->lock
);
3868 if (WARN_ON(xa_load(&devlink
->snapshot_ids
, id
)))
3871 return xa_err(xa_store(&devlink
->snapshot_ids
, id
, xa_mk_value(0),
3876 * __devlink_region_snapshot_id_get - get snapshot ID
3877 * @devlink: devlink instance
3878 * @id: storage to return snapshot id
3880 * Allocates a new snapshot id. Returns zero on success, or a negative
3881 * error on failure. Must be called while holding the devlink instance
3884 * Snapshot IDs are tracked using an xarray which stores the number of
3885 * users of the snapshot id.
3887 * Note that the caller of this function counts as a 'user', in order to
3888 * avoid race conditions. The caller must release its hold on the
3889 * snapshot by using devlink_region_snapshot_id_put.
3891 static int __devlink_region_snapshot_id_get(struct devlink
*devlink
, u32
*id
)
3893 lockdep_assert_held(&devlink
->lock
);
3895 return xa_alloc(&devlink
->snapshot_ids
, id
, xa_mk_value(1),
3896 xa_limit_32b
, GFP_KERNEL
);
3900 * __devlink_region_snapshot_create - create a new snapshot
3901 * This will add a new snapshot of a region. The snapshot
3902 * will be stored on the region struct and can be accessed
3903 * from devlink. This is useful for future analyses of snapshots.
3904 * Multiple snapshots can be created on a region.
3905 * The @snapshot_id should be obtained using the getter function.
3907 * Must be called only while holding the devlink instance lock.
3909 * @region: devlink region of the snapshot
3910 * @data: snapshot data
3911 * @snapshot_id: snapshot id to be created
3914 __devlink_region_snapshot_create(struct devlink_region
*region
,
3915 u8
*data
, u32 snapshot_id
)
3917 struct devlink
*devlink
= region
->devlink
;
3918 struct devlink_snapshot
*snapshot
;
3921 lockdep_assert_held(&devlink
->lock
);
3923 /* check if region can hold one more snapshot */
3924 if (region
->cur_snapshots
== region
->max_snapshots
)
3927 if (devlink_region_snapshot_get_by_id(region
, snapshot_id
))
3930 snapshot
= kzalloc(sizeof(*snapshot
), GFP_KERNEL
);
3934 err
= __devlink_snapshot_id_increment(devlink
, snapshot_id
);
3936 goto err_snapshot_id_increment
;
3938 snapshot
->id
= snapshot_id
;
3939 snapshot
->region
= region
;
3940 snapshot
->data
= data
;
3942 list_add_tail(&snapshot
->list
, ®ion
->snapshot_list
);
3944 region
->cur_snapshots
++;
3946 devlink_nl_region_notify(region
, snapshot
, DEVLINK_CMD_REGION_NEW
);
3949 err_snapshot_id_increment
:
3954 static void devlink_region_snapshot_del(struct devlink_region
*region
,
3955 struct devlink_snapshot
*snapshot
)
3957 struct devlink
*devlink
= region
->devlink
;
3959 lockdep_assert_held(&devlink
->lock
);
3961 devlink_nl_region_notify(region
, snapshot
, DEVLINK_CMD_REGION_DEL
);
3962 region
->cur_snapshots
--;
3963 list_del(&snapshot
->list
);
3964 region
->ops
->destructor(snapshot
->data
);
3965 __devlink_snapshot_id_decrement(devlink
, snapshot
->id
);
3969 static int devlink_nl_cmd_region_get_doit(struct sk_buff
*skb
,
3970 struct genl_info
*info
)
3972 struct devlink
*devlink
= info
->user_ptr
[0];
3973 struct devlink_region
*region
;
3974 const char *region_name
;
3975 struct sk_buff
*msg
;
3978 if (!info
->attrs
[DEVLINK_ATTR_REGION_NAME
])
3981 region_name
= nla_data(info
->attrs
[DEVLINK_ATTR_REGION_NAME
]);
3982 region
= devlink_region_get_by_name(devlink
, region_name
);
3986 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3990 err
= devlink_nl_region_fill(msg
, devlink
, DEVLINK_CMD_REGION_GET
,
3991 info
->snd_portid
, info
->snd_seq
, 0,
3998 return genlmsg_reply(msg
, info
);
4001 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff
*msg
,
4002 struct netlink_callback
*cb
)
4004 struct devlink_region
*region
;
4005 struct devlink
*devlink
;
4006 int start
= cb
->args
[0];
4010 mutex_lock(&devlink_mutex
);
4011 list_for_each_entry(devlink
, &devlink_list
, list
) {
4012 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
4015 mutex_lock(&devlink
->lock
);
4016 list_for_each_entry(region
, &devlink
->region_list
, list
) {
4021 err
= devlink_nl_region_fill(msg
, devlink
,
4022 DEVLINK_CMD_REGION_GET
,
4023 NETLINK_CB(cb
->skb
).portid
,
4025 NLM_F_MULTI
, region
);
4027 mutex_unlock(&devlink
->lock
);
4032 mutex_unlock(&devlink
->lock
);
4035 mutex_unlock(&devlink_mutex
);
4040 static int devlink_nl_cmd_region_del(struct sk_buff
*skb
,
4041 struct genl_info
*info
)
4043 struct devlink
*devlink
= info
->user_ptr
[0];
4044 struct devlink_snapshot
*snapshot
;
4045 struct devlink_region
*region
;
4046 const char *region_name
;
4049 if (!info
->attrs
[DEVLINK_ATTR_REGION_NAME
] ||
4050 !info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
4053 region_name
= nla_data(info
->attrs
[DEVLINK_ATTR_REGION_NAME
]);
4054 snapshot_id
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
4056 region
= devlink_region_get_by_name(devlink
, region_name
);
4060 snapshot
= devlink_region_snapshot_get_by_id(region
, snapshot_id
);
4064 devlink_region_snapshot_del(region
, snapshot
);
4069 devlink_nl_cmd_region_new(struct sk_buff
*skb
, struct genl_info
*info
)
4071 struct devlink
*devlink
= info
->user_ptr
[0];
4072 struct devlink_region
*region
;
4073 const char *region_name
;
4078 if (!info
->attrs
[DEVLINK_ATTR_REGION_NAME
]) {
4079 NL_SET_ERR_MSG_MOD(info
->extack
, "No region name provided");
4083 if (!info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]) {
4084 NL_SET_ERR_MSG_MOD(info
->extack
, "No snapshot id provided");
4088 region_name
= nla_data(info
->attrs
[DEVLINK_ATTR_REGION_NAME
]);
4089 region
= devlink_region_get_by_name(devlink
, region_name
);
4091 NL_SET_ERR_MSG_MOD(info
->extack
, "The requested region does not exist");
4095 if (!region
->ops
->snapshot
) {
4096 NL_SET_ERR_MSG_MOD(info
->extack
, "The requested region does not support taking an immediate snapshot");
4100 if (region
->cur_snapshots
== region
->max_snapshots
) {
4101 NL_SET_ERR_MSG_MOD(info
->extack
, "The region has reached the maximum number of stored snapshots");
4105 snapshot_id
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
4107 if (devlink_region_snapshot_get_by_id(region
, snapshot_id
)) {
4108 NL_SET_ERR_MSG_MOD(info
->extack
, "The requested snapshot id is already in use");
4112 err
= __devlink_snapshot_id_insert(devlink
, snapshot_id
);
4116 err
= region
->ops
->snapshot(devlink
, info
->extack
, &data
);
4118 goto err_snapshot_capture
;
4120 err
= __devlink_region_snapshot_create(region
, data
, snapshot_id
);
4122 goto err_snapshot_create
;
4126 err_snapshot_create
:
4127 region
->ops
->destructor(data
);
4128 err_snapshot_capture
:
4129 __devlink_snapshot_id_decrement(devlink
, snapshot_id
);
4133 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff
*msg
,
4134 struct devlink
*devlink
,
4135 u8
*chunk
, u32 chunk_size
,
4138 struct nlattr
*chunk_attr
;
4141 chunk_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_REGION_CHUNK
);
4145 err
= nla_put(msg
, DEVLINK_ATTR_REGION_CHUNK_DATA
, chunk_size
, chunk
);
4147 goto nla_put_failure
;
4149 err
= nla_put_u64_64bit(msg
, DEVLINK_ATTR_REGION_CHUNK_ADDR
, addr
,
4152 goto nla_put_failure
;
4154 nla_nest_end(msg
, chunk_attr
);
4158 nla_nest_cancel(msg
, chunk_attr
);
4162 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4164 static int devlink_nl_region_read_snapshot_fill(struct sk_buff
*skb
,
4165 struct devlink
*devlink
,
4166 struct devlink_region
*region
,
4167 struct nlattr
**attrs
,
4173 struct devlink_snapshot
*snapshot
;
4174 u64 curr_offset
= start_offset
;
4178 *new_offset
= start_offset
;
4180 snapshot_id
= nla_get_u32(attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
4181 snapshot
= devlink_region_snapshot_get_by_id(region
, snapshot_id
);
4185 if (end_offset
> region
->size
|| dump
)
4186 end_offset
= region
->size
;
4188 while (curr_offset
< end_offset
) {
4192 if (end_offset
- curr_offset
< DEVLINK_REGION_READ_CHUNK_SIZE
)
4193 data_size
= end_offset
- curr_offset
;
4195 data_size
= DEVLINK_REGION_READ_CHUNK_SIZE
;
4197 data
= &snapshot
->data
[curr_offset
];
4198 err
= devlink_nl_cmd_region_read_chunk_fill(skb
, devlink
,
4204 curr_offset
+= data_size
;
4206 *new_offset
= curr_offset
;
4211 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff
*skb
,
4212 struct netlink_callback
*cb
)
4214 const struct genl_dumpit_info
*info
= genl_dumpit_info(cb
);
4215 u64 ret_offset
, start_offset
, end_offset
= 0;
4216 struct nlattr
**attrs
= info
->attrs
;
4217 struct devlink_region
*region
;
4218 struct nlattr
*chunks_attr
;
4219 const char *region_name
;
4220 struct devlink
*devlink
;
4225 start_offset
= *((u64
*)&cb
->args
[0]);
4227 mutex_lock(&devlink_mutex
);
4228 devlink
= devlink_get_from_attrs(sock_net(cb
->skb
->sk
), attrs
);
4229 if (IS_ERR(devlink
)) {
4230 err
= PTR_ERR(devlink
);
4234 mutex_lock(&devlink
->lock
);
4236 if (!attrs
[DEVLINK_ATTR_REGION_NAME
] ||
4237 !attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]) {
4242 region_name
= nla_data(attrs
[DEVLINK_ATTR_REGION_NAME
]);
4243 region
= devlink_region_get_by_name(devlink
, region_name
);
4249 /* return 0 if there is no further data to read */
4250 if (start_offset
>= region
->size
) {
4255 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
4256 &devlink_nl_family
, NLM_F_ACK
| NLM_F_MULTI
,
4257 DEVLINK_CMD_REGION_READ
);
4263 err
= devlink_nl_put_handle(skb
, devlink
);
4265 goto nla_put_failure
;
4267 err
= nla_put_string(skb
, DEVLINK_ATTR_REGION_NAME
, region_name
);
4269 goto nla_put_failure
;
4271 chunks_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_REGION_CHUNKS
);
4274 goto nla_put_failure
;
4277 if (attrs
[DEVLINK_ATTR_REGION_CHUNK_ADDR
] &&
4278 attrs
[DEVLINK_ATTR_REGION_CHUNK_LEN
]) {
4281 nla_get_u64(attrs
[DEVLINK_ATTR_REGION_CHUNK_ADDR
]);
4283 end_offset
= nla_get_u64(attrs
[DEVLINK_ATTR_REGION_CHUNK_ADDR
]);
4284 end_offset
+= nla_get_u64(attrs
[DEVLINK_ATTR_REGION_CHUNK_LEN
]);
4287 if (start_offset
== end_offset
) {
4289 goto nla_put_failure
;
4293 err
= devlink_nl_region_read_snapshot_fill(skb
, devlink
,
4299 if (err
&& err
!= -EMSGSIZE
)
4300 goto nla_put_failure
;
4302 /* Check if there was any progress done to prevent infinite loop */
4303 if (ret_offset
== start_offset
) {
4305 goto nla_put_failure
;
4308 *((u64
*)&cb
->args
[0]) = ret_offset
;
4310 nla_nest_end(skb
, chunks_attr
);
4311 genlmsg_end(skb
, hdr
);
4312 mutex_unlock(&devlink
->lock
);
4313 mutex_unlock(&devlink_mutex
);
4318 genlmsg_cancel(skb
, hdr
);
4320 mutex_unlock(&devlink
->lock
);
4322 mutex_unlock(&devlink_mutex
);
4326 struct devlink_info_req
{
4327 struct sk_buff
*msg
;
4330 int devlink_info_driver_name_put(struct devlink_info_req
*req
, const char *name
)
4332 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_DRIVER_NAME
, name
);
4334 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put
);
4336 int devlink_info_serial_number_put(struct devlink_info_req
*req
, const char *sn
)
4338 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_SERIAL_NUMBER
, sn
);
4340 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put
);
4342 static int devlink_info_version_put(struct devlink_info_req
*req
, int attr
,
4343 const char *version_name
,
4344 const char *version_value
)
4346 struct nlattr
*nest
;
4349 nest
= nla_nest_start_noflag(req
->msg
, attr
);
4353 err
= nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_VERSION_NAME
,
4356 goto nla_put_failure
;
4358 err
= nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_VERSION_VALUE
,
4361 goto nla_put_failure
;
4363 nla_nest_end(req
->msg
, nest
);
4368 nla_nest_cancel(req
->msg
, nest
);
4372 int devlink_info_version_fixed_put(struct devlink_info_req
*req
,
4373 const char *version_name
,
4374 const char *version_value
)
4376 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_FIXED
,
4377 version_name
, version_value
);
4379 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put
);
4381 int devlink_info_version_stored_put(struct devlink_info_req
*req
,
4382 const char *version_name
,
4383 const char *version_value
)
4385 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_STORED
,
4386 version_name
, version_value
);
4388 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put
);
4390 int devlink_info_version_running_put(struct devlink_info_req
*req
,
4391 const char *version_name
,
4392 const char *version_value
)
4394 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_RUNNING
,
4395 version_name
, version_value
);
4397 EXPORT_SYMBOL_GPL(devlink_info_version_running_put
);
4400 devlink_nl_info_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
4401 enum devlink_command cmd
, u32 portid
,
4402 u32 seq
, int flags
, struct netlink_ext_ack
*extack
)
4404 struct devlink_info_req req
;
4408 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
4413 if (devlink_nl_put_handle(msg
, devlink
))
4414 goto err_cancel_msg
;
4417 err
= devlink
->ops
->info_get(devlink
, &req
, extack
);
4419 goto err_cancel_msg
;
4421 genlmsg_end(msg
, hdr
);
4425 genlmsg_cancel(msg
, hdr
);
4429 static int devlink_nl_cmd_info_get_doit(struct sk_buff
*skb
,
4430 struct genl_info
*info
)
4432 struct devlink
*devlink
= info
->user_ptr
[0];
4433 struct sk_buff
*msg
;
4436 if (!devlink
->ops
->info_get
)
4439 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
4443 err
= devlink_nl_info_fill(msg
, devlink
, DEVLINK_CMD_INFO_GET
,
4444 info
->snd_portid
, info
->snd_seq
, 0,
4451 return genlmsg_reply(msg
, info
);
4454 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff
*msg
,
4455 struct netlink_callback
*cb
)
4457 struct devlink
*devlink
;
4458 int start
= cb
->args
[0];
4462 mutex_lock(&devlink_mutex
);
4463 list_for_each_entry(devlink
, &devlink_list
, list
) {
4464 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
4471 if (!devlink
->ops
->info_get
) {
4476 mutex_lock(&devlink
->lock
);
4477 err
= devlink_nl_info_fill(msg
, devlink
, DEVLINK_CMD_INFO_GET
,
4478 NETLINK_CB(cb
->skb
).portid
,
4479 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
4481 mutex_unlock(&devlink
->lock
);
4482 if (err
&& err
!= -EOPNOTSUPP
)
4486 mutex_unlock(&devlink_mutex
);
4488 if (err
!= -EMSGSIZE
)
4495 struct devlink_fmsg_item
{
4496 struct list_head list
;
4503 struct devlink_fmsg
{
4504 struct list_head item_list
;
4505 bool putting_binary
; /* This flag forces enclosing of binary data
4506 * in an array brackets. It forces using
4507 * of designated API:
4508 * devlink_fmsg_binary_pair_nest_start()
4509 * devlink_fmsg_binary_pair_nest_end()
4513 static struct devlink_fmsg
*devlink_fmsg_alloc(void)
4515 struct devlink_fmsg
*fmsg
;
4517 fmsg
= kzalloc(sizeof(*fmsg
), GFP_KERNEL
);
4521 INIT_LIST_HEAD(&fmsg
->item_list
);
4526 static void devlink_fmsg_free(struct devlink_fmsg
*fmsg
)
4528 struct devlink_fmsg_item
*item
, *tmp
;
4530 list_for_each_entry_safe(item
, tmp
, &fmsg
->item_list
, list
) {
4531 list_del(&item
->list
);
4537 static int devlink_fmsg_nest_common(struct devlink_fmsg
*fmsg
,
4540 struct devlink_fmsg_item
*item
;
4542 item
= kzalloc(sizeof(*item
), GFP_KERNEL
);
4546 item
->attrtype
= attrtype
;
4547 list_add_tail(&item
->list
, &fmsg
->item_list
);
4552 int devlink_fmsg_obj_nest_start(struct devlink_fmsg
*fmsg
)
4554 if (fmsg
->putting_binary
)
4557 return devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_OBJ_NEST_START
);
4559 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start
);
4561 static int devlink_fmsg_nest_end(struct devlink_fmsg
*fmsg
)
4563 if (fmsg
->putting_binary
)
4566 return devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_NEST_END
);
4569 int devlink_fmsg_obj_nest_end(struct devlink_fmsg
*fmsg
)
4571 if (fmsg
->putting_binary
)
4574 return devlink_fmsg_nest_end(fmsg
);
4576 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end
);
4578 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4580 static int devlink_fmsg_put_name(struct devlink_fmsg
*fmsg
, const char *name
)
4582 struct devlink_fmsg_item
*item
;
4584 if (fmsg
->putting_binary
)
4587 if (strlen(name
) + 1 > DEVLINK_FMSG_MAX_SIZE
)
4590 item
= kzalloc(sizeof(*item
) + strlen(name
) + 1, GFP_KERNEL
);
4594 item
->nla_type
= NLA_NUL_STRING
;
4595 item
->len
= strlen(name
) + 1;
4596 item
->attrtype
= DEVLINK_ATTR_FMSG_OBJ_NAME
;
4597 memcpy(&item
->value
, name
, item
->len
);
4598 list_add_tail(&item
->list
, &fmsg
->item_list
);
4603 int devlink_fmsg_pair_nest_start(struct devlink_fmsg
*fmsg
, const char *name
)
4607 if (fmsg
->putting_binary
)
4610 err
= devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_PAIR_NEST_START
);
4614 err
= devlink_fmsg_put_name(fmsg
, name
);
4620 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start
);
4622 int devlink_fmsg_pair_nest_end(struct devlink_fmsg
*fmsg
)
4624 if (fmsg
->putting_binary
)
4627 return devlink_fmsg_nest_end(fmsg
);
4629 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end
);
4631 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg
*fmsg
,
4636 if (fmsg
->putting_binary
)
4639 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4643 err
= devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_ARR_NEST_START
);
4649 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start
);
4651 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg
*fmsg
)
4655 if (fmsg
->putting_binary
)
4658 err
= devlink_fmsg_nest_end(fmsg
);
4662 err
= devlink_fmsg_nest_end(fmsg
);
4668 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end
);
4670 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg
*fmsg
,
4675 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, name
);
4679 fmsg
->putting_binary
= true;
4682 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start
);
4684 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg
*fmsg
)
4686 if (!fmsg
->putting_binary
)
4689 fmsg
->putting_binary
= false;
4690 return devlink_fmsg_arr_pair_nest_end(fmsg
);
4692 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end
);
4694 static int devlink_fmsg_put_value(struct devlink_fmsg
*fmsg
,
4695 const void *value
, u16 value_len
,
4698 struct devlink_fmsg_item
*item
;
4700 if (value_len
> DEVLINK_FMSG_MAX_SIZE
)
4703 item
= kzalloc(sizeof(*item
) + value_len
, GFP_KERNEL
);
4707 item
->nla_type
= value_nla_type
;
4708 item
->len
= value_len
;
4709 item
->attrtype
= DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
;
4710 memcpy(&item
->value
, value
, item
->len
);
4711 list_add_tail(&item
->list
, &fmsg
->item_list
);
4716 int devlink_fmsg_bool_put(struct devlink_fmsg
*fmsg
, bool value
)
4718 if (fmsg
->putting_binary
)
4721 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_FLAG
);
4723 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put
);
4725 int devlink_fmsg_u8_put(struct devlink_fmsg
*fmsg
, u8 value
)
4727 if (fmsg
->putting_binary
)
4730 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U8
);
4732 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put
);
4734 int devlink_fmsg_u32_put(struct devlink_fmsg
*fmsg
, u32 value
)
4736 if (fmsg
->putting_binary
)
4739 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U32
);
4741 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put
);
4743 int devlink_fmsg_u64_put(struct devlink_fmsg
*fmsg
, u64 value
)
4745 if (fmsg
->putting_binary
)
4748 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U64
);
4750 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put
);
4752 int devlink_fmsg_string_put(struct devlink_fmsg
*fmsg
, const char *value
)
4754 if (fmsg
->putting_binary
)
4757 return devlink_fmsg_put_value(fmsg
, value
, strlen(value
) + 1,
4760 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put
);
4762 int devlink_fmsg_binary_put(struct devlink_fmsg
*fmsg
, const void *value
,
4765 if (!fmsg
->putting_binary
)
4768 return devlink_fmsg_put_value(fmsg
, value
, value_len
, NLA_BINARY
);
4770 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put
);
4772 int devlink_fmsg_bool_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4777 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4781 err
= devlink_fmsg_bool_put(fmsg
, value
);
4785 err
= devlink_fmsg_pair_nest_end(fmsg
);
4791 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put
);
4793 int devlink_fmsg_u8_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4798 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4802 err
= devlink_fmsg_u8_put(fmsg
, value
);
4806 err
= devlink_fmsg_pair_nest_end(fmsg
);
4812 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put
);
4814 int devlink_fmsg_u32_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4819 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4823 err
= devlink_fmsg_u32_put(fmsg
, value
);
4827 err
= devlink_fmsg_pair_nest_end(fmsg
);
4833 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put
);
4835 int devlink_fmsg_u64_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4840 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4844 err
= devlink_fmsg_u64_put(fmsg
, value
);
4848 err
= devlink_fmsg_pair_nest_end(fmsg
);
4854 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put
);
4856 int devlink_fmsg_string_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4861 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4865 err
= devlink_fmsg_string_put(fmsg
, value
);
4869 err
= devlink_fmsg_pair_nest_end(fmsg
);
4875 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put
);
4877 int devlink_fmsg_binary_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4878 const void *value
, u32 value_len
)
4885 err
= devlink_fmsg_binary_pair_nest_start(fmsg
, name
);
4889 for (offset
= 0; offset
< value_len
; offset
+= data_size
) {
4890 data_size
= value_len
- offset
;
4891 if (data_size
> DEVLINK_FMSG_MAX_SIZE
)
4892 data_size
= DEVLINK_FMSG_MAX_SIZE
;
4893 err
= devlink_fmsg_binary_put(fmsg
, value
+ offset
, data_size
);
4896 /* Exit from loop with a break (instead of
4897 * return) to make sure putting_binary is turned off in
4898 * devlink_fmsg_binary_pair_nest_end
4902 end_err
= devlink_fmsg_binary_pair_nest_end(fmsg
);
4908 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put
);
4911 devlink_fmsg_item_fill_type(struct devlink_fmsg_item
*msg
, struct sk_buff
*skb
)
4913 switch (msg
->nla_type
) {
4918 case NLA_NUL_STRING
:
4920 return nla_put_u8(skb
, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
,
4928 devlink_fmsg_item_fill_data(struct devlink_fmsg_item
*msg
, struct sk_buff
*skb
)
4930 int attrtype
= DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
;
4933 switch (msg
->nla_type
) {
4935 /* Always provide flag data, regardless of its value */
4936 tmp
= *(bool *) msg
->value
;
4938 return nla_put_u8(skb
, attrtype
, tmp
);
4940 return nla_put_u8(skb
, attrtype
, *(u8
*) msg
->value
);
4942 return nla_put_u32(skb
, attrtype
, *(u32
*) msg
->value
);
4944 return nla_put_u64_64bit(skb
, attrtype
, *(u64
*) msg
->value
,
4946 case NLA_NUL_STRING
:
4947 return nla_put_string(skb
, attrtype
, (char *) &msg
->value
);
4949 return nla_put(skb
, attrtype
, msg
->len
, (void *) &msg
->value
);
4956 devlink_fmsg_prepare_skb(struct devlink_fmsg
*fmsg
, struct sk_buff
*skb
,
4959 struct devlink_fmsg_item
*item
;
4960 struct nlattr
*fmsg_nlattr
;
4964 fmsg_nlattr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_FMSG
);
4968 list_for_each_entry(item
, &fmsg
->item_list
, list
) {
4974 switch (item
->attrtype
) {
4975 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
4976 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
4977 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
4978 case DEVLINK_ATTR_FMSG_NEST_END
:
4979 err
= nla_put_flag(skb
, item
->attrtype
);
4981 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
4982 err
= devlink_fmsg_item_fill_type(item
, skb
);
4985 err
= devlink_fmsg_item_fill_data(item
, skb
);
4987 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
4988 err
= nla_put_string(skb
, item
->attrtype
,
4989 (char *) &item
->value
);
5001 nla_nest_end(skb
, fmsg_nlattr
);
5005 static int devlink_fmsg_snd(struct devlink_fmsg
*fmsg
,
5006 struct genl_info
*info
,
5007 enum devlink_command cmd
, int flags
)
5009 struct nlmsghdr
*nlh
;
5010 struct sk_buff
*skb
;
5017 int tmp_index
= index
;
5019 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5023 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
5024 &devlink_nl_family
, flags
| NLM_F_MULTI
, cmd
);
5027 goto nla_put_failure
;
5030 err
= devlink_fmsg_prepare_skb(fmsg
, skb
, &index
);
5033 else if (err
!= -EMSGSIZE
|| tmp_index
== index
)
5034 goto nla_put_failure
;
5036 genlmsg_end(skb
, hdr
);
5037 err
= genlmsg_reply(skb
, info
);
5042 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5045 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
5046 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
5049 goto nla_put_failure
;
5052 return genlmsg_reply(skb
, info
);
5059 static int devlink_fmsg_dumpit(struct devlink_fmsg
*fmsg
, struct sk_buff
*skb
,
5060 struct netlink_callback
*cb
,
5061 enum devlink_command cmd
)
5063 int index
= cb
->args
[0];
5064 int tmp_index
= index
;
5068 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
5069 &devlink_nl_family
, NLM_F_ACK
| NLM_F_MULTI
, cmd
);
5072 goto nla_put_failure
;
5075 err
= devlink_fmsg_prepare_skb(fmsg
, skb
, &index
);
5076 if ((err
&& err
!= -EMSGSIZE
) || tmp_index
== index
)
5077 goto nla_put_failure
;
5079 cb
->args
[0] = index
;
5080 genlmsg_end(skb
, hdr
);
5084 genlmsg_cancel(skb
, hdr
);
5088 struct devlink_health_reporter
{
5089 struct list_head list
;
5091 const struct devlink_health_reporter_ops
*ops
;
5092 struct devlink
*devlink
;
5093 struct devlink_fmsg
*dump_fmsg
;
5094 struct mutex dump_lock
; /* lock parallel read/write from dump buffers */
5095 u64 graceful_period
;
5103 u64 last_recovery_ts
;
5104 refcount_t refcount
;
5108 devlink_health_reporter_priv(struct devlink_health_reporter
*reporter
)
5110 return reporter
->priv
;
5112 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv
);
5114 static struct devlink_health_reporter
*
5115 devlink_health_reporter_find_by_name(struct devlink
*devlink
,
5116 const char *reporter_name
)
5118 struct devlink_health_reporter
*reporter
;
5120 lockdep_assert_held(&devlink
->reporters_lock
);
5121 list_for_each_entry(reporter
, &devlink
->reporter_list
, list
)
5122 if (!strcmp(reporter
->ops
->name
, reporter_name
))
5128 * devlink_health_reporter_create - create devlink health reporter
5132 * @graceful_period: to avoid recovery loops, in msecs
5135 struct devlink_health_reporter
*
5136 devlink_health_reporter_create(struct devlink
*devlink
,
5137 const struct devlink_health_reporter_ops
*ops
,
5138 u64 graceful_period
, void *priv
)
5140 struct devlink_health_reporter
*reporter
;
5142 mutex_lock(&devlink
->reporters_lock
);
5143 if (devlink_health_reporter_find_by_name(devlink
, ops
->name
)) {
5144 reporter
= ERR_PTR(-EEXIST
);
5148 if (WARN_ON(graceful_period
&& !ops
->recover
)) {
5149 reporter
= ERR_PTR(-EINVAL
);
5153 reporter
= kzalloc(sizeof(*reporter
), GFP_KERNEL
);
5155 reporter
= ERR_PTR(-ENOMEM
);
5159 reporter
->priv
= priv
;
5160 reporter
->ops
= ops
;
5161 reporter
->devlink
= devlink
;
5162 reporter
->graceful_period
= graceful_period
;
5163 reporter
->auto_recover
= !!ops
->recover
;
5164 reporter
->auto_dump
= !!ops
->dump
;
5165 mutex_init(&reporter
->dump_lock
);
5166 refcount_set(&reporter
->refcount
, 1);
5167 list_add_tail(&reporter
->list
, &devlink
->reporter_list
);
5169 mutex_unlock(&devlink
->reporters_lock
);
5172 EXPORT_SYMBOL_GPL(devlink_health_reporter_create
);
5175 * devlink_health_reporter_destroy - destroy devlink health reporter
5177 * @reporter: devlink health reporter to destroy
5180 devlink_health_reporter_destroy(struct devlink_health_reporter
*reporter
)
5182 mutex_lock(&reporter
->devlink
->reporters_lock
);
5183 list_del(&reporter
->list
);
5184 mutex_unlock(&reporter
->devlink
->reporters_lock
);
5185 while (refcount_read(&reporter
->refcount
) > 1)
5187 mutex_destroy(&reporter
->dump_lock
);
5188 if (reporter
->dump_fmsg
)
5189 devlink_fmsg_free(reporter
->dump_fmsg
);
5192 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy
);
5195 devlink_nl_health_reporter_fill(struct sk_buff
*msg
,
5196 struct devlink
*devlink
,
5197 struct devlink_health_reporter
*reporter
,
5198 enum devlink_command cmd
, u32 portid
,
5201 struct nlattr
*reporter_attr
;
5204 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
5208 if (devlink_nl_put_handle(msg
, devlink
))
5209 goto genlmsg_cancel
;
5211 reporter_attr
= nla_nest_start_noflag(msg
,
5212 DEVLINK_ATTR_HEALTH_REPORTER
);
5214 goto genlmsg_cancel
;
5215 if (nla_put_string(msg
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
5216 reporter
->ops
->name
))
5217 goto reporter_nest_cancel
;
5218 if (nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_STATE
,
5219 reporter
->health_state
))
5220 goto reporter_nest_cancel
;
5221 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
,
5222 reporter
->error_count
, DEVLINK_ATTR_PAD
))
5223 goto reporter_nest_cancel
;
5224 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
,
5225 reporter
->recovery_count
, DEVLINK_ATTR_PAD
))
5226 goto reporter_nest_cancel
;
5227 if (reporter
->ops
->recover
&&
5228 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
5229 reporter
->graceful_period
,
5231 goto reporter_nest_cancel
;
5232 if (reporter
->ops
->recover
&&
5233 nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
5234 reporter
->auto_recover
))
5235 goto reporter_nest_cancel
;
5236 if (reporter
->dump_fmsg
&&
5237 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
,
5238 jiffies_to_msecs(reporter
->dump_ts
),
5240 goto reporter_nest_cancel
;
5241 if (reporter
->dump_fmsg
&&
5242 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
,
5243 reporter
->dump_real_ts
, DEVLINK_ATTR_PAD
))
5244 goto reporter_nest_cancel
;
5245 if (reporter
->ops
->dump
&&
5246 nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
5247 reporter
->auto_dump
))
5248 goto reporter_nest_cancel
;
5250 nla_nest_end(msg
, reporter_attr
);
5251 genlmsg_end(msg
, hdr
);
5254 reporter_nest_cancel
:
5255 nla_nest_end(msg
, reporter_attr
);
5257 genlmsg_cancel(msg
, hdr
);
5261 static void devlink_recover_notify(struct devlink_health_reporter
*reporter
,
5262 enum devlink_command cmd
)
5264 struct sk_buff
*msg
;
5267 WARN_ON(cmd
!= DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5269 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5273 err
= devlink_nl_health_reporter_fill(msg
, reporter
->devlink
,
5274 reporter
, cmd
, 0, 0, 0);
5280 genlmsg_multicast_netns(&devlink_nl_family
,
5281 devlink_net(reporter
->devlink
),
5282 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
5286 devlink_health_reporter_recovery_done(struct devlink_health_reporter
*reporter
)
5288 reporter
->recovery_count
++;
5289 reporter
->last_recovery_ts
= jiffies
;
5291 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done
);
5294 devlink_health_reporter_recover(struct devlink_health_reporter
*reporter
,
5295 void *priv_ctx
, struct netlink_ext_ack
*extack
)
5299 if (reporter
->health_state
== DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
)
5302 if (!reporter
->ops
->recover
)
5305 err
= reporter
->ops
->recover(reporter
, priv_ctx
, extack
);
5309 devlink_health_reporter_recovery_done(reporter
);
5310 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
;
5311 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5317 devlink_health_dump_clear(struct devlink_health_reporter
*reporter
)
5319 if (!reporter
->dump_fmsg
)
5321 devlink_fmsg_free(reporter
->dump_fmsg
);
5322 reporter
->dump_fmsg
= NULL
;
5325 static int devlink_health_do_dump(struct devlink_health_reporter
*reporter
,
5327 struct netlink_ext_ack
*extack
)
5331 if (!reporter
->ops
->dump
)
5334 if (reporter
->dump_fmsg
)
5337 reporter
->dump_fmsg
= devlink_fmsg_alloc();
5338 if (!reporter
->dump_fmsg
) {
5343 err
= devlink_fmsg_obj_nest_start(reporter
->dump_fmsg
);
5347 err
= reporter
->ops
->dump(reporter
, reporter
->dump_fmsg
,
5352 err
= devlink_fmsg_obj_nest_end(reporter
->dump_fmsg
);
5356 reporter
->dump_ts
= jiffies
;
5357 reporter
->dump_real_ts
= ktime_get_real_ns();
5362 devlink_health_dump_clear(reporter
);
5366 int devlink_health_report(struct devlink_health_reporter
*reporter
,
5367 const char *msg
, void *priv_ctx
)
5369 enum devlink_health_reporter_state prev_health_state
;
5370 struct devlink
*devlink
= reporter
->devlink
;
5371 unsigned long recover_ts_threshold
;
5373 /* write a log message of the current error */
5375 trace_devlink_health_report(devlink
, reporter
->ops
->name
, msg
);
5376 reporter
->error_count
++;
5377 prev_health_state
= reporter
->health_state
;
5378 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_ERROR
;
5379 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5381 /* abort if the previous error wasn't recovered */
5382 recover_ts_threshold
= reporter
->last_recovery_ts
+
5383 msecs_to_jiffies(reporter
->graceful_period
);
5384 if (reporter
->auto_recover
&&
5385 (prev_health_state
!= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
||
5386 (reporter
->last_recovery_ts
&& reporter
->recovery_count
&&
5387 time_is_after_jiffies(recover_ts_threshold
)))) {
5388 trace_devlink_health_recover_aborted(devlink
,
5389 reporter
->ops
->name
,
5390 reporter
->health_state
,
5392 reporter
->last_recovery_ts
);
5396 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_ERROR
;
5398 if (reporter
->auto_dump
) {
5399 mutex_lock(&reporter
->dump_lock
);
5400 /* store current dump of current error, for later analysis */
5401 devlink_health_do_dump(reporter
, priv_ctx
, NULL
);
5402 mutex_unlock(&reporter
->dump_lock
);
5405 if (reporter
->auto_recover
)
5406 return devlink_health_reporter_recover(reporter
,
5411 EXPORT_SYMBOL_GPL(devlink_health_report
);
5413 static struct devlink_health_reporter
*
5414 devlink_health_reporter_get_from_attrs(struct devlink
*devlink
,
5415 struct nlattr
**attrs
)
5417 struct devlink_health_reporter
*reporter
;
5418 char *reporter_name
;
5420 if (!attrs
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
])
5423 reporter_name
= nla_data(attrs
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]);
5424 mutex_lock(&devlink
->reporters_lock
);
5425 reporter
= devlink_health_reporter_find_by_name(devlink
, reporter_name
);
5427 refcount_inc(&reporter
->refcount
);
5428 mutex_unlock(&devlink
->reporters_lock
);
5432 static struct devlink_health_reporter
*
5433 devlink_health_reporter_get_from_info(struct devlink
*devlink
,
5434 struct genl_info
*info
)
5436 return devlink_health_reporter_get_from_attrs(devlink
, info
->attrs
);
5439 static struct devlink_health_reporter
*
5440 devlink_health_reporter_get_from_cb(struct netlink_callback
*cb
)
5442 const struct genl_dumpit_info
*info
= genl_dumpit_info(cb
);
5443 struct devlink_health_reporter
*reporter
;
5444 struct nlattr
**attrs
= info
->attrs
;
5445 struct devlink
*devlink
;
5447 mutex_lock(&devlink_mutex
);
5448 devlink
= devlink_get_from_attrs(sock_net(cb
->skb
->sk
), attrs
);
5449 if (IS_ERR(devlink
))
5452 reporter
= devlink_health_reporter_get_from_attrs(devlink
, attrs
);
5453 mutex_unlock(&devlink_mutex
);
5456 mutex_unlock(&devlink_mutex
);
5461 devlink_health_reporter_put(struct devlink_health_reporter
*reporter
)
5463 refcount_dec(&reporter
->refcount
);
5467 devlink_health_reporter_state_update(struct devlink_health_reporter
*reporter
,
5468 enum devlink_health_reporter_state state
)
5470 if (WARN_ON(state
!= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
&&
5471 state
!= DEVLINK_HEALTH_REPORTER_STATE_ERROR
))
5474 if (reporter
->health_state
== state
)
5477 reporter
->health_state
= state
;
5478 trace_devlink_health_reporter_state_update(reporter
->devlink
,
5479 reporter
->ops
->name
, state
);
5480 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5482 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update
);
5484 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff
*skb
,
5485 struct genl_info
*info
)
5487 struct devlink
*devlink
= info
->user_ptr
[0];
5488 struct devlink_health_reporter
*reporter
;
5489 struct sk_buff
*msg
;
5492 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5496 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5502 err
= devlink_nl_health_reporter_fill(msg
, devlink
, reporter
,
5503 DEVLINK_CMD_HEALTH_REPORTER_GET
,
5504 info
->snd_portid
, info
->snd_seq
,
5511 err
= genlmsg_reply(msg
, info
);
5513 devlink_health_reporter_put(reporter
);
5518 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff
*msg
,
5519 struct netlink_callback
*cb
)
5521 struct devlink_health_reporter
*reporter
;
5522 struct devlink
*devlink
;
5523 int start
= cb
->args
[0];
5527 mutex_lock(&devlink_mutex
);
5528 list_for_each_entry(devlink
, &devlink_list
, list
) {
5529 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
5531 mutex_lock(&devlink
->reporters_lock
);
5532 list_for_each_entry(reporter
, &devlink
->reporter_list
,
5538 err
= devlink_nl_health_reporter_fill(msg
, devlink
,
5540 DEVLINK_CMD_HEALTH_REPORTER_GET
,
5541 NETLINK_CB(cb
->skb
).portid
,
5545 mutex_unlock(&devlink
->reporters_lock
);
5550 mutex_unlock(&devlink
->reporters_lock
);
5553 mutex_unlock(&devlink_mutex
);
5560 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff
*skb
,
5561 struct genl_info
*info
)
5563 struct devlink
*devlink
= info
->user_ptr
[0];
5564 struct devlink_health_reporter
*reporter
;
5567 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5571 if (!reporter
->ops
->recover
&&
5572 (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] ||
5573 info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])) {
5577 if (!reporter
->ops
->dump
&&
5578 info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]) {
5583 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
5584 reporter
->graceful_period
=
5585 nla_get_u64(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]);
5587 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
5588 reporter
->auto_recover
=
5589 nla_get_u8(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]);
5591 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
5592 reporter
->auto_dump
=
5593 nla_get_u8(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]);
5595 devlink_health_reporter_put(reporter
);
5598 devlink_health_reporter_put(reporter
);
5602 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff
*skb
,
5603 struct genl_info
*info
)
5605 struct devlink
*devlink
= info
->user_ptr
[0];
5606 struct devlink_health_reporter
*reporter
;
5609 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5613 err
= devlink_health_reporter_recover(reporter
, NULL
, info
->extack
);
5615 devlink_health_reporter_put(reporter
);
5619 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff
*skb
,
5620 struct genl_info
*info
)
5622 struct devlink
*devlink
= info
->user_ptr
[0];
5623 struct devlink_health_reporter
*reporter
;
5624 struct devlink_fmsg
*fmsg
;
5627 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5631 if (!reporter
->ops
->diagnose
) {
5632 devlink_health_reporter_put(reporter
);
5636 fmsg
= devlink_fmsg_alloc();
5638 devlink_health_reporter_put(reporter
);
5642 err
= devlink_fmsg_obj_nest_start(fmsg
);
5646 err
= reporter
->ops
->diagnose(reporter
, fmsg
, info
->extack
);
5650 err
= devlink_fmsg_obj_nest_end(fmsg
);
5654 err
= devlink_fmsg_snd(fmsg
, info
,
5655 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
, 0);
5658 devlink_fmsg_free(fmsg
);
5659 devlink_health_reporter_put(reporter
);
5664 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff
*skb
,
5665 struct netlink_callback
*cb
)
5667 struct devlink_health_reporter
*reporter
;
5668 u64 start
= cb
->args
[0];
5671 reporter
= devlink_health_reporter_get_from_cb(cb
);
5675 if (!reporter
->ops
->dump
) {
5679 mutex_lock(&reporter
->dump_lock
);
5681 err
= devlink_health_do_dump(reporter
, NULL
, cb
->extack
);
5684 cb
->args
[1] = reporter
->dump_ts
;
5686 if (!reporter
->dump_fmsg
|| cb
->args
[1] != reporter
->dump_ts
) {
5687 NL_SET_ERR_MSG_MOD(cb
->extack
, "Dump trampled, please retry");
5692 err
= devlink_fmsg_dumpit(reporter
->dump_fmsg
, skb
, cb
,
5693 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
5695 mutex_unlock(&reporter
->dump_lock
);
5697 devlink_health_reporter_put(reporter
);
5702 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff
*skb
,
5703 struct genl_info
*info
)
5705 struct devlink
*devlink
= info
->user_ptr
[0];
5706 struct devlink_health_reporter
*reporter
;
5708 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5712 if (!reporter
->ops
->dump
) {
5713 devlink_health_reporter_put(reporter
);
5717 mutex_lock(&reporter
->dump_lock
);
5718 devlink_health_dump_clear(reporter
);
5719 mutex_unlock(&reporter
->dump_lock
);
5720 devlink_health_reporter_put(reporter
);
5724 struct devlink_stats
{
5727 struct u64_stats_sync syncp
;
5731 * struct devlink_trap_policer_item - Packet trap policer attributes.
5732 * @policer: Immutable packet trap policer attributes.
5733 * @rate: Rate in packets / sec.
5734 * @burst: Burst size in packets.
5735 * @list: trap_policer_list member.
5737 * Describes packet trap policer attributes. Created by devlink during trap
5738 * policer registration.
5740 struct devlink_trap_policer_item
{
5741 const struct devlink_trap_policer
*policer
;
5744 struct list_head list
;
5748 * struct devlink_trap_group_item - Packet trap group attributes.
5749 * @group: Immutable packet trap group attributes.
5750 * @policer_item: Associated policer item. Can be NULL.
5751 * @list: trap_group_list member.
5752 * @stats: Trap group statistics.
5754 * Describes packet trap group attributes. Created by devlink during trap
5755 * group registration.
5757 struct devlink_trap_group_item
{
5758 const struct devlink_trap_group
*group
;
5759 struct devlink_trap_policer_item
*policer_item
;
5760 struct list_head list
;
5761 struct devlink_stats __percpu
*stats
;
5765 * struct devlink_trap_item - Packet trap attributes.
5766 * @trap: Immutable packet trap attributes.
5767 * @group_item: Associated group item.
5768 * @list: trap_list member.
5769 * @action: Trap action.
5770 * @stats: Trap statistics.
5771 * @priv: Driver private information.
5773 * Describes both mutable and immutable packet trap attributes. Created by
5774 * devlink during trap registration and used for all trap related operations.
5776 struct devlink_trap_item
{
5777 const struct devlink_trap
*trap
;
5778 struct devlink_trap_group_item
*group_item
;
5779 struct list_head list
;
5780 enum devlink_trap_action action
;
5781 struct devlink_stats __percpu
*stats
;
5785 static struct devlink_trap_policer_item
*
5786 devlink_trap_policer_item_lookup(struct devlink
*devlink
, u32 id
)
5788 struct devlink_trap_policer_item
*policer_item
;
5790 list_for_each_entry(policer_item
, &devlink
->trap_policer_list
, list
) {
5791 if (policer_item
->policer
->id
== id
)
5792 return policer_item
;
5798 static struct devlink_trap_item
*
5799 devlink_trap_item_lookup(struct devlink
*devlink
, const char *name
)
5801 struct devlink_trap_item
*trap_item
;
5803 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
5804 if (!strcmp(trap_item
->trap
->name
, name
))
5811 static struct devlink_trap_item
*
5812 devlink_trap_item_get_from_info(struct devlink
*devlink
,
5813 struct genl_info
*info
)
5815 struct nlattr
*attr
;
5817 if (!info
->attrs
[DEVLINK_ATTR_TRAP_NAME
])
5819 attr
= info
->attrs
[DEVLINK_ATTR_TRAP_NAME
];
5821 return devlink_trap_item_lookup(devlink
, nla_data(attr
));
5825 devlink_trap_action_get_from_info(struct genl_info
*info
,
5826 enum devlink_trap_action
*p_trap_action
)
5830 val
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
]);
5832 case DEVLINK_TRAP_ACTION_DROP
: /* fall-through */
5833 case DEVLINK_TRAP_ACTION_TRAP
:
5834 *p_trap_action
= val
;
5843 static int devlink_trap_metadata_put(struct sk_buff
*msg
,
5844 const struct devlink_trap
*trap
)
5846 struct nlattr
*attr
;
5848 attr
= nla_nest_start(msg
, DEVLINK_ATTR_TRAP_METADATA
);
5852 if ((trap
->metadata_cap
& DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
) &&
5853 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
))
5854 goto nla_put_failure
;
5855 if ((trap
->metadata_cap
& DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE
) &&
5856 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
))
5857 goto nla_put_failure
;
5859 nla_nest_end(msg
, attr
);
5864 nla_nest_cancel(msg
, attr
);
5868 static void devlink_trap_stats_read(struct devlink_stats __percpu
*trap_stats
,
5869 struct devlink_stats
*stats
)
5873 memset(stats
, 0, sizeof(*stats
));
5874 for_each_possible_cpu(i
) {
5875 struct devlink_stats
*cpu_stats
;
5876 u64 rx_packets
, rx_bytes
;
5879 cpu_stats
= per_cpu_ptr(trap_stats
, i
);
5881 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
5882 rx_packets
= cpu_stats
->rx_packets
;
5883 rx_bytes
= cpu_stats
->rx_bytes
;
5884 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
5886 stats
->rx_packets
+= rx_packets
;
5887 stats
->rx_bytes
+= rx_bytes
;
5891 static int devlink_trap_stats_put(struct sk_buff
*msg
,
5892 struct devlink_stats __percpu
*trap_stats
)
5894 struct devlink_stats stats
;
5895 struct nlattr
*attr
;
5897 devlink_trap_stats_read(trap_stats
, &stats
);
5899 attr
= nla_nest_start(msg
, DEVLINK_ATTR_STATS
);
5903 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_PACKETS
,
5904 stats
.rx_packets
, DEVLINK_ATTR_PAD
))
5905 goto nla_put_failure
;
5907 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_BYTES
,
5908 stats
.rx_bytes
, DEVLINK_ATTR_PAD
))
5909 goto nla_put_failure
;
5911 nla_nest_end(msg
, attr
);
5916 nla_nest_cancel(msg
, attr
);
5920 static int devlink_nl_trap_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
5921 const struct devlink_trap_item
*trap_item
,
5922 enum devlink_command cmd
, u32 portid
, u32 seq
,
5925 struct devlink_trap_group_item
*group_item
= trap_item
->group_item
;
5929 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
5933 if (devlink_nl_put_handle(msg
, devlink
))
5934 goto nla_put_failure
;
5936 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
5937 group_item
->group
->name
))
5938 goto nla_put_failure
;
5940 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_NAME
, trap_item
->trap
->name
))
5941 goto nla_put_failure
;
5943 if (nla_put_u8(msg
, DEVLINK_ATTR_TRAP_TYPE
, trap_item
->trap
->type
))
5944 goto nla_put_failure
;
5946 if (trap_item
->trap
->generic
&&
5947 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_GENERIC
))
5948 goto nla_put_failure
;
5950 if (nla_put_u8(msg
, DEVLINK_ATTR_TRAP_ACTION
, trap_item
->action
))
5951 goto nla_put_failure
;
5953 err
= devlink_trap_metadata_put(msg
, trap_item
->trap
);
5955 goto nla_put_failure
;
5957 err
= devlink_trap_stats_put(msg
, trap_item
->stats
);
5959 goto nla_put_failure
;
5961 genlmsg_end(msg
, hdr
);
5966 genlmsg_cancel(msg
, hdr
);
5970 static int devlink_nl_cmd_trap_get_doit(struct sk_buff
*skb
,
5971 struct genl_info
*info
)
5973 struct netlink_ext_ack
*extack
= info
->extack
;
5974 struct devlink
*devlink
= info
->user_ptr
[0];
5975 struct devlink_trap_item
*trap_item
;
5976 struct sk_buff
*msg
;
5979 if (list_empty(&devlink
->trap_list
))
5982 trap_item
= devlink_trap_item_get_from_info(devlink
, info
);
5984 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap");
5988 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5992 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
,
5993 DEVLINK_CMD_TRAP_NEW
, info
->snd_portid
,
5998 return genlmsg_reply(msg
, info
);
6005 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff
*msg
,
6006 struct netlink_callback
*cb
)
6008 struct devlink_trap_item
*trap_item
;
6009 struct devlink
*devlink
;
6010 int start
= cb
->args
[0];
6014 mutex_lock(&devlink_mutex
);
6015 list_for_each_entry(devlink
, &devlink_list
, list
) {
6016 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6018 mutex_lock(&devlink
->lock
);
6019 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
6024 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
,
6025 DEVLINK_CMD_TRAP_NEW
,
6026 NETLINK_CB(cb
->skb
).portid
,
6030 mutex_unlock(&devlink
->lock
);
6035 mutex_unlock(&devlink
->lock
);
6038 mutex_unlock(&devlink_mutex
);
6044 static int __devlink_trap_action_set(struct devlink
*devlink
,
6045 struct devlink_trap_item
*trap_item
,
6046 enum devlink_trap_action trap_action
,
6047 struct netlink_ext_ack
*extack
)
6051 if (trap_item
->action
!= trap_action
&&
6052 trap_item
->trap
->type
!= DEVLINK_TRAP_TYPE_DROP
) {
6053 NL_SET_ERR_MSG_MOD(extack
, "Cannot change action of non-drop traps. Skipping");
6057 err
= devlink
->ops
->trap_action_set(devlink
, trap_item
->trap
,
6062 trap_item
->action
= trap_action
;
6067 static int devlink_trap_action_set(struct devlink
*devlink
,
6068 struct devlink_trap_item
*trap_item
,
6069 struct genl_info
*info
)
6071 enum devlink_trap_action trap_action
;
6074 if (!info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
])
6077 err
= devlink_trap_action_get_from_info(info
, &trap_action
);
6079 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid trap action");
6083 return __devlink_trap_action_set(devlink
, trap_item
, trap_action
,
6087 static int devlink_nl_cmd_trap_set_doit(struct sk_buff
*skb
,
6088 struct genl_info
*info
)
6090 struct netlink_ext_ack
*extack
= info
->extack
;
6091 struct devlink
*devlink
= info
->user_ptr
[0];
6092 struct devlink_trap_item
*trap_item
;
6095 if (list_empty(&devlink
->trap_list
))
6098 trap_item
= devlink_trap_item_get_from_info(devlink
, info
);
6100 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap");
6104 err
= devlink_trap_action_set(devlink
, trap_item
, info
);
6111 static struct devlink_trap_group_item
*
6112 devlink_trap_group_item_lookup(struct devlink
*devlink
, const char *name
)
6114 struct devlink_trap_group_item
*group_item
;
6116 list_for_each_entry(group_item
, &devlink
->trap_group_list
, list
) {
6117 if (!strcmp(group_item
->group
->name
, name
))
6124 static struct devlink_trap_group_item
*
6125 devlink_trap_group_item_lookup_by_id(struct devlink
*devlink
, u16 id
)
6127 struct devlink_trap_group_item
*group_item
;
6129 list_for_each_entry(group_item
, &devlink
->trap_group_list
, list
) {
6130 if (group_item
->group
->id
== id
)
6137 static struct devlink_trap_group_item
*
6138 devlink_trap_group_item_get_from_info(struct devlink
*devlink
,
6139 struct genl_info
*info
)
6143 if (!info
->attrs
[DEVLINK_ATTR_TRAP_GROUP_NAME
])
6145 name
= nla_data(info
->attrs
[DEVLINK_ATTR_TRAP_GROUP_NAME
]);
6147 return devlink_trap_group_item_lookup(devlink
, name
);
6151 devlink_nl_trap_group_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
6152 const struct devlink_trap_group_item
*group_item
,
6153 enum devlink_command cmd
, u32 portid
, u32 seq
,
6159 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
6163 if (devlink_nl_put_handle(msg
, devlink
))
6164 goto nla_put_failure
;
6166 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
6167 group_item
->group
->name
))
6168 goto nla_put_failure
;
6170 if (group_item
->group
->generic
&&
6171 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_GENERIC
))
6172 goto nla_put_failure
;
6174 if (group_item
->policer_item
&&
6175 nla_put_u32(msg
, DEVLINK_ATTR_TRAP_POLICER_ID
,
6176 group_item
->policer_item
->policer
->id
))
6177 goto nla_put_failure
;
6179 err
= devlink_trap_stats_put(msg
, group_item
->stats
);
6181 goto nla_put_failure
;
6183 genlmsg_end(msg
, hdr
);
6188 genlmsg_cancel(msg
, hdr
);
6192 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff
*skb
,
6193 struct genl_info
*info
)
6195 struct netlink_ext_ack
*extack
= info
->extack
;
6196 struct devlink
*devlink
= info
->user_ptr
[0];
6197 struct devlink_trap_group_item
*group_item
;
6198 struct sk_buff
*msg
;
6201 if (list_empty(&devlink
->trap_group_list
))
6204 group_item
= devlink_trap_group_item_get_from_info(devlink
, info
);
6206 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap group");
6210 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6214 err
= devlink_nl_trap_group_fill(msg
, devlink
, group_item
,
6215 DEVLINK_CMD_TRAP_GROUP_NEW
,
6216 info
->snd_portid
, info
->snd_seq
, 0);
6218 goto err_trap_group_fill
;
6220 return genlmsg_reply(msg
, info
);
6222 err_trap_group_fill
:
6227 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff
*msg
,
6228 struct netlink_callback
*cb
)
6230 enum devlink_command cmd
= DEVLINK_CMD_TRAP_GROUP_NEW
;
6231 struct devlink_trap_group_item
*group_item
;
6232 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
6233 struct devlink
*devlink
;
6234 int start
= cb
->args
[0];
6238 mutex_lock(&devlink_mutex
);
6239 list_for_each_entry(devlink
, &devlink_list
, list
) {
6240 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6242 mutex_lock(&devlink
->lock
);
6243 list_for_each_entry(group_item
, &devlink
->trap_group_list
,
6249 err
= devlink_nl_trap_group_fill(msg
, devlink
,
6255 mutex_unlock(&devlink
->lock
);
6260 mutex_unlock(&devlink
->lock
);
6263 mutex_unlock(&devlink_mutex
);
6270 __devlink_trap_group_action_set(struct devlink
*devlink
,
6271 struct devlink_trap_group_item
*group_item
,
6272 enum devlink_trap_action trap_action
,
6273 struct netlink_ext_ack
*extack
)
6275 const char *group_name
= group_item
->group
->name
;
6276 struct devlink_trap_item
*trap_item
;
6279 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
6280 if (strcmp(trap_item
->group_item
->group
->name
, group_name
))
6282 err
= __devlink_trap_action_set(devlink
, trap_item
,
6283 trap_action
, extack
);
6292 devlink_trap_group_action_set(struct devlink
*devlink
,
6293 struct devlink_trap_group_item
*group_item
,
6294 struct genl_info
*info
, bool *p_modified
)
6296 enum devlink_trap_action trap_action
;
6299 if (!info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
])
6302 err
= devlink_trap_action_get_from_info(info
, &trap_action
);
6304 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid trap action");
6308 err
= __devlink_trap_group_action_set(devlink
, group_item
, trap_action
,
6318 static int devlink_trap_group_set(struct devlink
*devlink
,
6319 struct devlink_trap_group_item
*group_item
,
6320 struct genl_info
*info
)
6322 struct devlink_trap_policer_item
*policer_item
;
6323 struct netlink_ext_ack
*extack
= info
->extack
;
6324 const struct devlink_trap_policer
*policer
;
6325 struct nlattr
**attrs
= info
->attrs
;
6328 if (!attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
])
6331 if (!devlink
->ops
->trap_group_set
)
6334 policer_item
= group_item
->policer_item
;
6335 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]) {
6338 policer_id
= nla_get_u32(attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]);
6339 policer_item
= devlink_trap_policer_item_lookup(devlink
,
6341 if (policer_id
&& !policer_item
) {
6342 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
6346 policer
= policer_item
? policer_item
->policer
: NULL
;
6348 err
= devlink
->ops
->trap_group_set(devlink
, group_item
->group
, policer
);
6352 group_item
->policer_item
= policer_item
;
6357 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff
*skb
,
6358 struct genl_info
*info
)
6360 struct netlink_ext_ack
*extack
= info
->extack
;
6361 struct devlink
*devlink
= info
->user_ptr
[0];
6362 struct devlink_trap_group_item
*group_item
;
6363 bool modified
= false;
6366 if (list_empty(&devlink
->trap_group_list
))
6369 group_item
= devlink_trap_group_item_get_from_info(devlink
, info
);
6371 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap group");
6375 err
= devlink_trap_group_action_set(devlink
, group_item
, info
,
6380 err
= devlink_trap_group_set(devlink
, group_item
, info
);
6382 goto err_trap_group_set
;
6388 NL_SET_ERR_MSG_MOD(extack
, "Trap group set failed, but some changes were committed already");
6392 static struct devlink_trap_policer_item
*
6393 devlink_trap_policer_item_get_from_info(struct devlink
*devlink
,
6394 struct genl_info
*info
)
6398 if (!info
->attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
])
6400 id
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]);
6402 return devlink_trap_policer_item_lookup(devlink
, id
);
6406 devlink_trap_policer_stats_put(struct sk_buff
*msg
, struct devlink
*devlink
,
6407 const struct devlink_trap_policer
*policer
)
6409 struct nlattr
*attr
;
6413 if (!devlink
->ops
->trap_policer_counter_get
)
6416 err
= devlink
->ops
->trap_policer_counter_get(devlink
, policer
, &drops
);
6420 attr
= nla_nest_start(msg
, DEVLINK_ATTR_STATS
);
6424 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_DROPPED
, drops
,
6426 goto nla_put_failure
;
6428 nla_nest_end(msg
, attr
);
6433 nla_nest_cancel(msg
, attr
);
6438 devlink_nl_trap_policer_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
6439 const struct devlink_trap_policer_item
*policer_item
,
6440 enum devlink_command cmd
, u32 portid
, u32 seq
,
6446 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
6450 if (devlink_nl_put_handle(msg
, devlink
))
6451 goto nla_put_failure
;
6453 if (nla_put_u32(msg
, DEVLINK_ATTR_TRAP_POLICER_ID
,
6454 policer_item
->policer
->id
))
6455 goto nla_put_failure
;
6457 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
6458 policer_item
->rate
, DEVLINK_ATTR_PAD
))
6459 goto nla_put_failure
;
6461 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
6462 policer_item
->burst
, DEVLINK_ATTR_PAD
))
6463 goto nla_put_failure
;
6465 err
= devlink_trap_policer_stats_put(msg
, devlink
,
6466 policer_item
->policer
);
6468 goto nla_put_failure
;
6470 genlmsg_end(msg
, hdr
);
6475 genlmsg_cancel(msg
, hdr
);
6479 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff
*skb
,
6480 struct genl_info
*info
)
6482 struct devlink_trap_policer_item
*policer_item
;
6483 struct netlink_ext_ack
*extack
= info
->extack
;
6484 struct devlink
*devlink
= info
->user_ptr
[0];
6485 struct sk_buff
*msg
;
6488 if (list_empty(&devlink
->trap_policer_list
))
6491 policer_item
= devlink_trap_policer_item_get_from_info(devlink
, info
);
6492 if (!policer_item
) {
6493 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
6497 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6501 err
= devlink_nl_trap_policer_fill(msg
, devlink
, policer_item
,
6502 DEVLINK_CMD_TRAP_POLICER_NEW
,
6503 info
->snd_portid
, info
->snd_seq
, 0);
6505 goto err_trap_policer_fill
;
6507 return genlmsg_reply(msg
, info
);
6509 err_trap_policer_fill
:
6514 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff
*msg
,
6515 struct netlink_callback
*cb
)
6517 enum devlink_command cmd
= DEVLINK_CMD_TRAP_POLICER_NEW
;
6518 struct devlink_trap_policer_item
*policer_item
;
6519 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
6520 struct devlink
*devlink
;
6521 int start
= cb
->args
[0];
6525 mutex_lock(&devlink_mutex
);
6526 list_for_each_entry(devlink
, &devlink_list
, list
) {
6527 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6529 mutex_lock(&devlink
->lock
);
6530 list_for_each_entry(policer_item
, &devlink
->trap_policer_list
,
6536 err
= devlink_nl_trap_policer_fill(msg
, devlink
,
6542 mutex_unlock(&devlink
->lock
);
6547 mutex_unlock(&devlink
->lock
);
6550 mutex_unlock(&devlink_mutex
);
6557 devlink_trap_policer_set(struct devlink
*devlink
,
6558 struct devlink_trap_policer_item
*policer_item
,
6559 struct genl_info
*info
)
6561 struct netlink_ext_ack
*extack
= info
->extack
;
6562 struct nlattr
**attrs
= info
->attrs
;
6566 rate
= policer_item
->rate
;
6567 burst
= policer_item
->burst
;
6569 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_RATE
])
6570 rate
= nla_get_u64(attrs
[DEVLINK_ATTR_TRAP_POLICER_RATE
]);
6572 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
6573 burst
= nla_get_u64(attrs
[DEVLINK_ATTR_TRAP_POLICER_BURST
]);
6575 if (rate
< policer_item
->policer
->min_rate
) {
6576 NL_SET_ERR_MSG_MOD(extack
, "Policer rate lower than limit");
6580 if (rate
> policer_item
->policer
->max_rate
) {
6581 NL_SET_ERR_MSG_MOD(extack
, "Policer rate higher than limit");
6585 if (burst
< policer_item
->policer
->min_burst
) {
6586 NL_SET_ERR_MSG_MOD(extack
, "Policer burst size lower than limit");
6590 if (burst
> policer_item
->policer
->max_burst
) {
6591 NL_SET_ERR_MSG_MOD(extack
, "Policer burst size higher than limit");
6595 err
= devlink
->ops
->trap_policer_set(devlink
, policer_item
->policer
,
6596 rate
, burst
, info
->extack
);
6600 policer_item
->rate
= rate
;
6601 policer_item
->burst
= burst
;
6606 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff
*skb
,
6607 struct genl_info
*info
)
6609 struct devlink_trap_policer_item
*policer_item
;
6610 struct netlink_ext_ack
*extack
= info
->extack
;
6611 struct devlink
*devlink
= info
->user_ptr
[0];
6613 if (list_empty(&devlink
->trap_policer_list
))
6616 if (!devlink
->ops
->trap_policer_set
)
6619 policer_item
= devlink_trap_policer_item_get_from_info(devlink
, info
);
6620 if (!policer_item
) {
6621 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
6625 return devlink_trap_policer_set(devlink
, policer_item
, info
);
6628 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
6629 [DEVLINK_ATTR_UNSPEC
] = { .strict_start_type
=
6630 DEVLINK_ATTR_TRAP_POLICER_ID
},
6631 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
6632 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
6633 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
6634 [DEVLINK_ATTR_PORT_TYPE
] = { .type
= NLA_U16
},
6635 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
6636 [DEVLINK_ATTR_SB_INDEX
] = { .type
= NLA_U32
},
6637 [DEVLINK_ATTR_SB_POOL_INDEX
] = { .type
= NLA_U16
},
6638 [DEVLINK_ATTR_SB_POOL_TYPE
] = { .type
= NLA_U8
},
6639 [DEVLINK_ATTR_SB_POOL_SIZE
] = { .type
= NLA_U32
},
6640 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = { .type
= NLA_U8
},
6641 [DEVLINK_ATTR_SB_THRESHOLD
] = { .type
= NLA_U32
},
6642 [DEVLINK_ATTR_SB_TC_INDEX
] = { .type
= NLA_U16
},
6643 [DEVLINK_ATTR_ESWITCH_MODE
] = { .type
= NLA_U16
},
6644 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = { .type
= NLA_U8
},
6645 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = { .type
= NLA_U8
},
6646 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = { .type
= NLA_NUL_STRING
},
6647 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = { .type
= NLA_U8
},
6648 [DEVLINK_ATTR_RESOURCE_ID
] = { .type
= NLA_U64
},
6649 [DEVLINK_ATTR_RESOURCE_SIZE
] = { .type
= NLA_U64
},
6650 [DEVLINK_ATTR_PARAM_NAME
] = { .type
= NLA_NUL_STRING
},
6651 [DEVLINK_ATTR_PARAM_TYPE
] = { .type
= NLA_U8
},
6652 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = { .type
= NLA_U8
},
6653 [DEVLINK_ATTR_REGION_NAME
] = { .type
= NLA_NUL_STRING
},
6654 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = { .type
= NLA_U32
},
6655 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = { .type
= NLA_U64
},
6656 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = { .type
= NLA_U64
},
6657 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = { .type
= NLA_NUL_STRING
},
6658 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = { .type
= NLA_U64
},
6659 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
] = { .type
= NLA_U8
},
6660 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
] = { .type
= NLA_NUL_STRING
},
6661 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = { .type
= NLA_NUL_STRING
},
6662 [DEVLINK_ATTR_TRAP_NAME
] = { .type
= NLA_NUL_STRING
},
6663 [DEVLINK_ATTR_TRAP_ACTION
] = { .type
= NLA_U8
},
6664 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = { .type
= NLA_NUL_STRING
},
6665 [DEVLINK_ATTR_NETNS_PID
] = { .type
= NLA_U32
},
6666 [DEVLINK_ATTR_NETNS_FD
] = { .type
= NLA_U32
},
6667 [DEVLINK_ATTR_NETNS_ID
] = { .type
= NLA_U32
},
6668 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
] = { .type
= NLA_U8
},
6669 [DEVLINK_ATTR_TRAP_POLICER_ID
] = { .type
= NLA_U32
},
6670 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = { .type
= NLA_U64
},
6671 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = { .type
= NLA_U64
},
6674 static const struct genl_ops devlink_nl_ops
[] = {
6676 .cmd
= DEVLINK_CMD_GET
,
6677 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6678 .doit
= devlink_nl_cmd_get_doit
,
6679 .dumpit
= devlink_nl_cmd_get_dumpit
,
6680 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6681 /* can be retrieved by unprivileged users */
6684 .cmd
= DEVLINK_CMD_PORT_GET
,
6685 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6686 .doit
= devlink_nl_cmd_port_get_doit
,
6687 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
6688 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6689 /* can be retrieved by unprivileged users */
6692 .cmd
= DEVLINK_CMD_PORT_SET
,
6693 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6694 .doit
= devlink_nl_cmd_port_set_doit
,
6695 .flags
= GENL_ADMIN_PERM
,
6696 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6699 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
6700 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6701 .doit
= devlink_nl_cmd_port_split_doit
,
6702 .flags
= GENL_ADMIN_PERM
,
6703 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6704 DEVLINK_NL_FLAG_NO_LOCK
,
6707 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
6708 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6709 .doit
= devlink_nl_cmd_port_unsplit_doit
,
6710 .flags
= GENL_ADMIN_PERM
,
6711 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6712 DEVLINK_NL_FLAG_NO_LOCK
,
6715 .cmd
= DEVLINK_CMD_SB_GET
,
6716 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6717 .doit
= devlink_nl_cmd_sb_get_doit
,
6718 .dumpit
= devlink_nl_cmd_sb_get_dumpit
,
6719 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6720 DEVLINK_NL_FLAG_NEED_SB
,
6721 /* can be retrieved by unprivileged users */
6724 .cmd
= DEVLINK_CMD_SB_POOL_GET
,
6725 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6726 .doit
= devlink_nl_cmd_sb_pool_get_doit
,
6727 .dumpit
= devlink_nl_cmd_sb_pool_get_dumpit
,
6728 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6729 DEVLINK_NL_FLAG_NEED_SB
,
6730 /* can be retrieved by unprivileged users */
6733 .cmd
= DEVLINK_CMD_SB_POOL_SET
,
6734 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6735 .doit
= devlink_nl_cmd_sb_pool_set_doit
,
6736 .flags
= GENL_ADMIN_PERM
,
6737 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6738 DEVLINK_NL_FLAG_NEED_SB
,
6741 .cmd
= DEVLINK_CMD_SB_PORT_POOL_GET
,
6742 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6743 .doit
= devlink_nl_cmd_sb_port_pool_get_doit
,
6744 .dumpit
= devlink_nl_cmd_sb_port_pool_get_dumpit
,
6745 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6746 DEVLINK_NL_FLAG_NEED_SB
,
6747 /* can be retrieved by unprivileged users */
6750 .cmd
= DEVLINK_CMD_SB_PORT_POOL_SET
,
6751 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6752 .doit
= devlink_nl_cmd_sb_port_pool_set_doit
,
6753 .flags
= GENL_ADMIN_PERM
,
6754 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6755 DEVLINK_NL_FLAG_NEED_SB
,
6758 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_GET
,
6759 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6760 .doit
= devlink_nl_cmd_sb_tc_pool_bind_get_doit
,
6761 .dumpit
= devlink_nl_cmd_sb_tc_pool_bind_get_dumpit
,
6762 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6763 DEVLINK_NL_FLAG_NEED_SB
,
6764 /* can be retrieved by unprivileged users */
6767 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
6768 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6769 .doit
= devlink_nl_cmd_sb_tc_pool_bind_set_doit
,
6770 .flags
= GENL_ADMIN_PERM
,
6771 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6772 DEVLINK_NL_FLAG_NEED_SB
,
6775 .cmd
= DEVLINK_CMD_SB_OCC_SNAPSHOT
,
6776 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6777 .doit
= devlink_nl_cmd_sb_occ_snapshot_doit
,
6778 .flags
= GENL_ADMIN_PERM
,
6779 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6780 DEVLINK_NL_FLAG_NEED_SB
,
6783 .cmd
= DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
6784 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6785 .doit
= devlink_nl_cmd_sb_occ_max_clear_doit
,
6786 .flags
= GENL_ADMIN_PERM
,
6787 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6788 DEVLINK_NL_FLAG_NEED_SB
,
6791 .cmd
= DEVLINK_CMD_ESWITCH_GET
,
6792 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6793 .doit
= devlink_nl_cmd_eswitch_get_doit
,
6794 .flags
= GENL_ADMIN_PERM
,
6795 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6796 DEVLINK_NL_FLAG_NO_LOCK
,
6799 .cmd
= DEVLINK_CMD_ESWITCH_SET
,
6800 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6801 .doit
= devlink_nl_cmd_eswitch_set_doit
,
6802 .flags
= GENL_ADMIN_PERM
,
6803 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6804 DEVLINK_NL_FLAG_NO_LOCK
,
6807 .cmd
= DEVLINK_CMD_DPIPE_TABLE_GET
,
6808 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6809 .doit
= devlink_nl_cmd_dpipe_table_get
,
6810 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6811 /* can be retrieved by unprivileged users */
6814 .cmd
= DEVLINK_CMD_DPIPE_ENTRIES_GET
,
6815 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6816 .doit
= devlink_nl_cmd_dpipe_entries_get
,
6817 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6818 /* can be retrieved by unprivileged users */
6821 .cmd
= DEVLINK_CMD_DPIPE_HEADERS_GET
,
6822 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6823 .doit
= devlink_nl_cmd_dpipe_headers_get
,
6824 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6825 /* can be retrieved by unprivileged users */
6828 .cmd
= DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
6829 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6830 .doit
= devlink_nl_cmd_dpipe_table_counters_set
,
6831 .flags
= GENL_ADMIN_PERM
,
6832 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6835 .cmd
= DEVLINK_CMD_RESOURCE_SET
,
6836 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6837 .doit
= devlink_nl_cmd_resource_set
,
6838 .flags
= GENL_ADMIN_PERM
,
6839 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6842 .cmd
= DEVLINK_CMD_RESOURCE_DUMP
,
6843 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6844 .doit
= devlink_nl_cmd_resource_dump
,
6845 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6846 /* can be retrieved by unprivileged users */
6849 .cmd
= DEVLINK_CMD_RELOAD
,
6850 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6851 .doit
= devlink_nl_cmd_reload
,
6852 .flags
= GENL_ADMIN_PERM
,
6853 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6854 DEVLINK_NL_FLAG_NO_LOCK
,
6857 .cmd
= DEVLINK_CMD_PARAM_GET
,
6858 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6859 .doit
= devlink_nl_cmd_param_get_doit
,
6860 .dumpit
= devlink_nl_cmd_param_get_dumpit
,
6861 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6862 /* can be retrieved by unprivileged users */
6865 .cmd
= DEVLINK_CMD_PARAM_SET
,
6866 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6867 .doit
= devlink_nl_cmd_param_set_doit
,
6868 .flags
= GENL_ADMIN_PERM
,
6869 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6872 .cmd
= DEVLINK_CMD_PORT_PARAM_GET
,
6873 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6874 .doit
= devlink_nl_cmd_port_param_get_doit
,
6875 .dumpit
= devlink_nl_cmd_port_param_get_dumpit
,
6876 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6877 /* can be retrieved by unprivileged users */
6880 .cmd
= DEVLINK_CMD_PORT_PARAM_SET
,
6881 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6882 .doit
= devlink_nl_cmd_port_param_set_doit
,
6883 .flags
= GENL_ADMIN_PERM
,
6884 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6887 .cmd
= DEVLINK_CMD_REGION_GET
,
6888 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6889 .doit
= devlink_nl_cmd_region_get_doit
,
6890 .dumpit
= devlink_nl_cmd_region_get_dumpit
,
6891 .flags
= GENL_ADMIN_PERM
,
6892 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6895 .cmd
= DEVLINK_CMD_REGION_NEW
,
6896 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6897 .doit
= devlink_nl_cmd_region_new
,
6898 .flags
= GENL_ADMIN_PERM
,
6899 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6902 .cmd
= DEVLINK_CMD_REGION_DEL
,
6903 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6904 .doit
= devlink_nl_cmd_region_del
,
6905 .flags
= GENL_ADMIN_PERM
,
6906 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6909 .cmd
= DEVLINK_CMD_REGION_READ
,
6910 .validate
= GENL_DONT_VALIDATE_STRICT
|
6911 GENL_DONT_VALIDATE_DUMP_STRICT
,
6912 .dumpit
= devlink_nl_cmd_region_read_dumpit
,
6913 .flags
= GENL_ADMIN_PERM
,
6914 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6917 .cmd
= DEVLINK_CMD_INFO_GET
,
6918 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6919 .doit
= devlink_nl_cmd_info_get_doit
,
6920 .dumpit
= devlink_nl_cmd_info_get_dumpit
,
6921 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6922 /* can be retrieved by unprivileged users */
6925 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_GET
,
6926 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6927 .doit
= devlink_nl_cmd_health_reporter_get_doit
,
6928 .dumpit
= devlink_nl_cmd_health_reporter_get_dumpit
,
6929 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6930 DEVLINK_NL_FLAG_NO_LOCK
,
6931 /* can be retrieved by unprivileged users */
6934 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_SET
,
6935 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6936 .doit
= devlink_nl_cmd_health_reporter_set_doit
,
6937 .flags
= GENL_ADMIN_PERM
,
6938 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6939 DEVLINK_NL_FLAG_NO_LOCK
,
6942 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6943 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6944 .doit
= devlink_nl_cmd_health_reporter_recover_doit
,
6945 .flags
= GENL_ADMIN_PERM
,
6946 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6947 DEVLINK_NL_FLAG_NO_LOCK
,
6950 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
6951 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6952 .doit
= devlink_nl_cmd_health_reporter_diagnose_doit
,
6953 .flags
= GENL_ADMIN_PERM
,
6954 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6955 DEVLINK_NL_FLAG_NO_LOCK
,
6958 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
6959 .validate
= GENL_DONT_VALIDATE_STRICT
|
6960 GENL_DONT_VALIDATE_DUMP_STRICT
,
6961 .dumpit
= devlink_nl_cmd_health_reporter_dump_get_dumpit
,
6962 .flags
= GENL_ADMIN_PERM
,
6963 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6964 DEVLINK_NL_FLAG_NO_LOCK
,
6967 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6968 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6969 .doit
= devlink_nl_cmd_health_reporter_dump_clear_doit
,
6970 .flags
= GENL_ADMIN_PERM
,
6971 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6972 DEVLINK_NL_FLAG_NO_LOCK
,
6975 .cmd
= DEVLINK_CMD_FLASH_UPDATE
,
6976 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6977 .doit
= devlink_nl_cmd_flash_update
,
6978 .flags
= GENL_ADMIN_PERM
,
6979 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6982 .cmd
= DEVLINK_CMD_TRAP_GET
,
6983 .doit
= devlink_nl_cmd_trap_get_doit
,
6984 .dumpit
= devlink_nl_cmd_trap_get_dumpit
,
6985 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6986 /* can be retrieved by unprivileged users */
6989 .cmd
= DEVLINK_CMD_TRAP_SET
,
6990 .doit
= devlink_nl_cmd_trap_set_doit
,
6991 .flags
= GENL_ADMIN_PERM
,
6992 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6995 .cmd
= DEVLINK_CMD_TRAP_GROUP_GET
,
6996 .doit
= devlink_nl_cmd_trap_group_get_doit
,
6997 .dumpit
= devlink_nl_cmd_trap_group_get_dumpit
,
6998 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6999 /* can be retrieved by unprivileged users */
7002 .cmd
= DEVLINK_CMD_TRAP_GROUP_SET
,
7003 .doit
= devlink_nl_cmd_trap_group_set_doit
,
7004 .flags
= GENL_ADMIN_PERM
,
7005 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
7008 .cmd
= DEVLINK_CMD_TRAP_POLICER_GET
,
7009 .doit
= devlink_nl_cmd_trap_policer_get_doit
,
7010 .dumpit
= devlink_nl_cmd_trap_policer_get_dumpit
,
7011 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
7012 /* can be retrieved by unprivileged users */
7015 .cmd
= DEVLINK_CMD_TRAP_POLICER_SET
,
7016 .doit
= devlink_nl_cmd_trap_policer_set_doit
,
7017 .flags
= GENL_ADMIN_PERM
,
7018 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
7022 static struct genl_family devlink_nl_family __ro_after_init
= {
7023 .name
= DEVLINK_GENL_NAME
,
7024 .version
= DEVLINK_GENL_VERSION
,
7025 .maxattr
= DEVLINK_ATTR_MAX
,
7026 .policy
= devlink_nl_policy
,
7028 .pre_doit
= devlink_nl_pre_doit
,
7029 .post_doit
= devlink_nl_post_doit
,
7030 .module
= THIS_MODULE
,
7031 .ops
= devlink_nl_ops
,
7032 .n_ops
= ARRAY_SIZE(devlink_nl_ops
),
7033 .mcgrps
= devlink_nl_mcgrps
,
7034 .n_mcgrps
= ARRAY_SIZE(devlink_nl_mcgrps
),
7038 * devlink_alloc - Allocate new devlink instance resources
7041 * @priv_size: size of user private data
7043 * Allocate new devlink instance resources, including devlink index
7046 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
7048 struct devlink
*devlink
;
7053 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
7057 xa_init_flags(&devlink
->snapshot_ids
, XA_FLAGS_ALLOC
);
7058 __devlink_net_set(devlink
, &init_net
);
7059 INIT_LIST_HEAD(&devlink
->port_list
);
7060 INIT_LIST_HEAD(&devlink
->sb_list
);
7061 INIT_LIST_HEAD_RCU(&devlink
->dpipe_table_list
);
7062 INIT_LIST_HEAD(&devlink
->resource_list
);
7063 INIT_LIST_HEAD(&devlink
->param_list
);
7064 INIT_LIST_HEAD(&devlink
->region_list
);
7065 INIT_LIST_HEAD(&devlink
->reporter_list
);
7066 INIT_LIST_HEAD(&devlink
->trap_list
);
7067 INIT_LIST_HEAD(&devlink
->trap_group_list
);
7068 INIT_LIST_HEAD(&devlink
->trap_policer_list
);
7069 mutex_init(&devlink
->lock
);
7070 mutex_init(&devlink
->reporters_lock
);
7073 EXPORT_SYMBOL_GPL(devlink_alloc
);
7076 * devlink_register - Register devlink instance
7079 * @dev: parent device
7081 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
7083 mutex_lock(&devlink_mutex
);
7085 devlink
->registered
= true;
7086 list_add_tail(&devlink
->list
, &devlink_list
);
7087 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
7088 mutex_unlock(&devlink_mutex
);
7091 EXPORT_SYMBOL_GPL(devlink_register
);
7094 * devlink_unregister - Unregister devlink instance
7098 void devlink_unregister(struct devlink
*devlink
)
7100 mutex_lock(&devlink_mutex
);
7101 WARN_ON(devlink_reload_supported(devlink
) &&
7102 devlink
->reload_enabled
);
7103 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
7104 list_del(&devlink
->list
);
7105 mutex_unlock(&devlink_mutex
);
7107 EXPORT_SYMBOL_GPL(devlink_unregister
);
7110 * devlink_reload_enable - Enable reload of devlink instance
7114 * Should be called at end of device initialization
7115 * process when reload operation is supported.
7117 void devlink_reload_enable(struct devlink
*devlink
)
7119 mutex_lock(&devlink_mutex
);
7120 devlink
->reload_enabled
= true;
7121 mutex_unlock(&devlink_mutex
);
7123 EXPORT_SYMBOL_GPL(devlink_reload_enable
);
7126 * devlink_reload_disable - Disable reload of devlink instance
7130 * Should be called at the beginning of device cleanup
7131 * process when reload operation is supported.
7133 void devlink_reload_disable(struct devlink
*devlink
)
7135 mutex_lock(&devlink_mutex
);
7136 /* Mutex is taken which ensures that no reload operation is in
7137 * progress while setting up forbidded flag.
7139 devlink
->reload_enabled
= false;
7140 mutex_unlock(&devlink_mutex
);
7142 EXPORT_SYMBOL_GPL(devlink_reload_disable
);
7145 * devlink_free - Free devlink instance resources
7149 void devlink_free(struct devlink
*devlink
)
7151 mutex_destroy(&devlink
->reporters_lock
);
7152 mutex_destroy(&devlink
->lock
);
7153 WARN_ON(!list_empty(&devlink
->trap_policer_list
));
7154 WARN_ON(!list_empty(&devlink
->trap_group_list
));
7155 WARN_ON(!list_empty(&devlink
->trap_list
));
7156 WARN_ON(!list_empty(&devlink
->reporter_list
));
7157 WARN_ON(!list_empty(&devlink
->region_list
));
7158 WARN_ON(!list_empty(&devlink
->param_list
));
7159 WARN_ON(!list_empty(&devlink
->resource_list
));
7160 WARN_ON(!list_empty(&devlink
->dpipe_table_list
));
7161 WARN_ON(!list_empty(&devlink
->sb_list
));
7162 WARN_ON(!list_empty(&devlink
->port_list
));
7164 xa_destroy(&devlink
->snapshot_ids
);
7168 EXPORT_SYMBOL_GPL(devlink_free
);
7170 static void devlink_port_type_warn(struct work_struct
*work
)
7172 WARN(true, "Type was not set for devlink port.");
7175 static bool devlink_port_type_should_warn(struct devlink_port
*devlink_port
)
7177 /* Ignore CPU and DSA flavours. */
7178 return devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_CPU
&&
7179 devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_DSA
;
7182 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
7184 static void devlink_port_type_warn_schedule(struct devlink_port
*devlink_port
)
7186 if (!devlink_port_type_should_warn(devlink_port
))
7188 /* Schedule a work to WARN in case driver does not set port
7189 * type within timeout.
7191 schedule_delayed_work(&devlink_port
->type_warn_dw
,
7192 DEVLINK_PORT_TYPE_WARN_TIMEOUT
);
7195 static void devlink_port_type_warn_cancel(struct devlink_port
*devlink_port
)
7197 if (!devlink_port_type_should_warn(devlink_port
))
7199 cancel_delayed_work_sync(&devlink_port
->type_warn_dw
);
7203 * devlink_port_register - Register devlink port
7206 * @devlink_port: devlink port
7207 * @port_index: driver-specific numerical identifier of the port
7209 * Register devlink port with provided port index. User can use
7210 * any indexing, even hw-related one. devlink_port structure
7211 * is convenient to be embedded inside user driver private structure.
7212 * Note that the caller should take care of zeroing the devlink_port
7215 int devlink_port_register(struct devlink
*devlink
,
7216 struct devlink_port
*devlink_port
,
7217 unsigned int port_index
)
7219 mutex_lock(&devlink
->lock
);
7220 if (devlink_port_index_exists(devlink
, port_index
)) {
7221 mutex_unlock(&devlink
->lock
);
7224 devlink_port
->devlink
= devlink
;
7225 devlink_port
->index
= port_index
;
7226 devlink_port
->registered
= true;
7227 spin_lock_init(&devlink_port
->type_lock
);
7228 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
7229 INIT_LIST_HEAD(&devlink_port
->param_list
);
7230 mutex_unlock(&devlink
->lock
);
7231 INIT_DELAYED_WORK(&devlink_port
->type_warn_dw
, &devlink_port_type_warn
);
7232 devlink_port_type_warn_schedule(devlink_port
);
7233 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
7236 EXPORT_SYMBOL_GPL(devlink_port_register
);
7239 * devlink_port_unregister - Unregister devlink port
7241 * @devlink_port: devlink port
7243 void devlink_port_unregister(struct devlink_port
*devlink_port
)
7245 struct devlink
*devlink
= devlink_port
->devlink
;
7247 devlink_port_type_warn_cancel(devlink_port
);
7248 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
7249 mutex_lock(&devlink
->lock
);
7250 list_del(&devlink_port
->list
);
7251 mutex_unlock(&devlink
->lock
);
7253 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
7255 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
7256 enum devlink_port_type type
,
7259 if (WARN_ON(!devlink_port
->registered
))
7261 devlink_port_type_warn_cancel(devlink_port
);
7262 spin_lock_bh(&devlink_port
->type_lock
);
7263 devlink_port
->type
= type
;
7264 devlink_port
->type_dev
= type_dev
;
7265 spin_unlock_bh(&devlink_port
->type_lock
);
7266 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
7270 * devlink_port_type_eth_set - Set port type to Ethernet
7272 * @devlink_port: devlink port
7273 * @netdev: related netdevice
7275 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
7276 struct net_device
*netdev
)
7278 const struct net_device_ops
*ops
= netdev
->netdev_ops
;
7280 /* If driver registers devlink port, it should set devlink port
7281 * attributes accordingly so the compat functions are called
7282 * and the original ops are not used.
7284 if (ops
->ndo_get_phys_port_name
) {
7285 /* Some drivers use the same set of ndos for netdevs
7286 * that have devlink_port registered and also for
7287 * those who don't. Make sure that ndo_get_phys_port_name
7288 * returns -EOPNOTSUPP here in case it is defined.
7291 char name
[IFNAMSIZ
];
7294 err
= ops
->ndo_get_phys_port_name(netdev
, name
, sizeof(name
));
7295 WARN_ON(err
!= -EOPNOTSUPP
);
7297 if (ops
->ndo_get_port_parent_id
) {
7298 /* Some drivers use the same set of ndos for netdevs
7299 * that have devlink_port registered and also for
7300 * those who don't. Make sure that ndo_get_port_parent_id
7301 * returns -EOPNOTSUPP here in case it is defined.
7304 struct netdev_phys_item_id ppid
;
7307 err
= ops
->ndo_get_port_parent_id(netdev
, &ppid
);
7308 WARN_ON(err
!= -EOPNOTSUPP
);
7310 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_ETH
, netdev
);
7312 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
7315 * devlink_port_type_ib_set - Set port type to InfiniBand
7317 * @devlink_port: devlink port
7318 * @ibdev: related IB device
7320 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
7321 struct ib_device
*ibdev
)
7323 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_IB
, ibdev
);
7325 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
7328 * devlink_port_type_clear - Clear port type
7330 * @devlink_port: devlink port
7332 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
7334 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_NOTSET
, NULL
);
7335 devlink_port_type_warn_schedule(devlink_port
);
7337 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
7339 static int __devlink_port_attrs_set(struct devlink_port
*devlink_port
,
7340 enum devlink_port_flavour flavour
,
7341 const unsigned char *switch_id
,
7342 unsigned char switch_id_len
)
7344 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7346 if (WARN_ON(devlink_port
->registered
))
7349 attrs
->flavour
= flavour
;
7351 attrs
->switch_port
= true;
7352 if (WARN_ON(switch_id_len
> MAX_PHYS_ITEM_ID_LEN
))
7353 switch_id_len
= MAX_PHYS_ITEM_ID_LEN
;
7354 memcpy(attrs
->switch_id
.id
, switch_id
, switch_id_len
);
7355 attrs
->switch_id
.id_len
= switch_id_len
;
7357 attrs
->switch_port
= false;
7363 * devlink_port_attrs_set - Set port attributes
7365 * @devlink_port: devlink port
7366 * @flavour: flavour of the port
7367 * @port_number: number of the port that is facing user, for example
7368 * the front panel port number
7369 * @split: indicates if this is split port
7370 * @split_subport_number: if the port is split, this is the number
7372 * @switch_id: if the port is part of switch, this is buffer with ID,
7373 * otwerwise this is NULL
7374 * @switch_id_len: length of the switch_id buffer
7376 void devlink_port_attrs_set(struct devlink_port
*devlink_port
,
7377 enum devlink_port_flavour flavour
,
7378 u32 port_number
, bool split
,
7379 u32 split_subport_number
,
7380 const unsigned char *switch_id
,
7381 unsigned char switch_id_len
)
7383 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7386 ret
= __devlink_port_attrs_set(devlink_port
, flavour
,
7387 switch_id
, switch_id_len
);
7390 attrs
->split
= split
;
7391 attrs
->phys
.port_number
= port_number
;
7392 attrs
->phys
.split_subport_number
= split_subport_number
;
7394 EXPORT_SYMBOL_GPL(devlink_port_attrs_set
);
7397 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
7399 * @devlink_port: devlink port
7400 * @pf: associated PF for the devlink port instance
7401 * @switch_id: if the port is part of switch, this is buffer with ID,
7402 * otherwise this is NULL
7403 * @switch_id_len: length of the switch_id buffer
7405 void devlink_port_attrs_pci_pf_set(struct devlink_port
*devlink_port
,
7406 const unsigned char *switch_id
,
7407 unsigned char switch_id_len
, u16 pf
)
7409 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7412 ret
= __devlink_port_attrs_set(devlink_port
,
7413 DEVLINK_PORT_FLAVOUR_PCI_PF
,
7414 switch_id
, switch_id_len
);
7418 attrs
->pci_pf
.pf
= pf
;
7420 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set
);
7423 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
7425 * @devlink_port: devlink port
7426 * @pf: associated PF for the devlink port instance
7427 * @vf: associated VF of a PF for the devlink port instance
7428 * @switch_id: if the port is part of switch, this is buffer with ID,
7429 * otherwise this is NULL
7430 * @switch_id_len: length of the switch_id buffer
7432 void devlink_port_attrs_pci_vf_set(struct devlink_port
*devlink_port
,
7433 const unsigned char *switch_id
,
7434 unsigned char switch_id_len
,
7437 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7440 ret
= __devlink_port_attrs_set(devlink_port
,
7441 DEVLINK_PORT_FLAVOUR_PCI_VF
,
7442 switch_id
, switch_id_len
);
7445 attrs
->pci_vf
.pf
= pf
;
7446 attrs
->pci_vf
.vf
= vf
;
7448 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set
);
7450 static int __devlink_port_phys_port_name_get(struct devlink_port
*devlink_port
,
7451 char *name
, size_t len
)
7453 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7459 switch (attrs
->flavour
) {
7460 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
7461 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
7463 n
= snprintf(name
, len
, "p%u", attrs
->phys
.port_number
);
7465 n
= snprintf(name
, len
, "p%us%u",
7466 attrs
->phys
.port_number
,
7467 attrs
->phys
.split_subport_number
);
7469 case DEVLINK_PORT_FLAVOUR_CPU
:
7470 case DEVLINK_PORT_FLAVOUR_DSA
:
7471 /* As CPU and DSA ports do not have a netdevice associated
7472 * case should not ever happen.
7476 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
7477 n
= snprintf(name
, len
, "pf%u", attrs
->pci_pf
.pf
);
7479 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
7480 n
= snprintf(name
, len
, "pf%uvf%u",
7481 attrs
->pci_vf
.pf
, attrs
->pci_vf
.vf
);
7491 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
7492 u32 size
, u16 ingress_pools_count
,
7493 u16 egress_pools_count
, u16 ingress_tc_count
,
7494 u16 egress_tc_count
)
7496 struct devlink_sb
*devlink_sb
;
7499 mutex_lock(&devlink
->lock
);
7500 if (devlink_sb_index_exists(devlink
, sb_index
)) {
7505 devlink_sb
= kzalloc(sizeof(*devlink_sb
), GFP_KERNEL
);
7510 devlink_sb
->index
= sb_index
;
7511 devlink_sb
->size
= size
;
7512 devlink_sb
->ingress_pools_count
= ingress_pools_count
;
7513 devlink_sb
->egress_pools_count
= egress_pools_count
;
7514 devlink_sb
->ingress_tc_count
= ingress_tc_count
;
7515 devlink_sb
->egress_tc_count
= egress_tc_count
;
7516 list_add_tail(&devlink_sb
->list
, &devlink
->sb_list
);
7518 mutex_unlock(&devlink
->lock
);
7521 EXPORT_SYMBOL_GPL(devlink_sb_register
);
7523 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
)
7525 struct devlink_sb
*devlink_sb
;
7527 mutex_lock(&devlink
->lock
);
7528 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
7529 WARN_ON(!devlink_sb
);
7530 list_del(&devlink_sb
->list
);
7531 mutex_unlock(&devlink
->lock
);
7534 EXPORT_SYMBOL_GPL(devlink_sb_unregister
);
7537 * devlink_dpipe_headers_register - register dpipe headers
7540 * @dpipe_headers: dpipe header array
7542 * Register the headers supported by hardware.
7544 int devlink_dpipe_headers_register(struct devlink
*devlink
,
7545 struct devlink_dpipe_headers
*dpipe_headers
)
7547 mutex_lock(&devlink
->lock
);
7548 devlink
->dpipe_headers
= dpipe_headers
;
7549 mutex_unlock(&devlink
->lock
);
7552 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register
);
7555 * devlink_dpipe_headers_unregister - unregister dpipe headers
7559 * Unregister the headers supported by hardware.
7561 void devlink_dpipe_headers_unregister(struct devlink
*devlink
)
7563 mutex_lock(&devlink
->lock
);
7564 devlink
->dpipe_headers
= NULL
;
7565 mutex_unlock(&devlink
->lock
);
7567 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister
);
7570 * devlink_dpipe_table_counter_enabled - check if counter allocation
7573 * @table_name: tables name
7575 * Used by driver to check if counter allocation is required.
7576 * After counter allocation is turned on the table entries
7577 * are updated to include counter statistics.
7579 * After that point on the driver must respect the counter
7580 * state so that each entry added to the table is added
7583 bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
7584 const char *table_name
)
7586 struct devlink_dpipe_table
*table
;
7590 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
7591 table_name
, devlink
);
7594 enabled
= table
->counters_enabled
;
7598 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled
);
7601 * devlink_dpipe_table_register - register dpipe table
7604 * @table_name: table name
7605 * @table_ops: table ops
7607 * @counter_control_extern: external control for counters
7609 int devlink_dpipe_table_register(struct devlink
*devlink
,
7610 const char *table_name
,
7611 struct devlink_dpipe_table_ops
*table_ops
,
7612 void *priv
, bool counter_control_extern
)
7614 struct devlink_dpipe_table
*table
;
7617 if (WARN_ON(!table_ops
->size_get
))
7620 mutex_lock(&devlink
->lock
);
7622 if (devlink_dpipe_table_find(&devlink
->dpipe_table_list
, table_name
,
7628 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
7634 table
->name
= table_name
;
7635 table
->table_ops
= table_ops
;
7637 table
->counter_control_extern
= counter_control_extern
;
7639 list_add_tail_rcu(&table
->list
, &devlink
->dpipe_table_list
);
7641 mutex_unlock(&devlink
->lock
);
7644 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register
);
7647 * devlink_dpipe_table_unregister - unregister dpipe table
7650 * @table_name: table name
7652 void devlink_dpipe_table_unregister(struct devlink
*devlink
,
7653 const char *table_name
)
7655 struct devlink_dpipe_table
*table
;
7657 mutex_lock(&devlink
->lock
);
7658 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
7659 table_name
, devlink
);
7662 list_del_rcu(&table
->list
);
7663 mutex_unlock(&devlink
->lock
);
7664 kfree_rcu(table
, rcu
);
7667 mutex_unlock(&devlink
->lock
);
7669 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister
);
7672 * devlink_resource_register - devlink resource register
7675 * @resource_name: resource's name
7676 * @resource_size: resource's size
7677 * @resource_id: resource's id
7678 * @parent_resource_id: resource's parent id
7679 * @size_params: size parameters
7681 int devlink_resource_register(struct devlink
*devlink
,
7682 const char *resource_name
,
7685 u64 parent_resource_id
,
7686 const struct devlink_resource_size_params
*size_params
)
7688 struct devlink_resource
*resource
;
7689 struct list_head
*resource_list
;
7693 top_hierarchy
= parent_resource_id
== DEVLINK_RESOURCE_ID_PARENT_TOP
;
7695 mutex_lock(&devlink
->lock
);
7696 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7702 resource
= kzalloc(sizeof(*resource
), GFP_KERNEL
);
7708 if (top_hierarchy
) {
7709 resource_list
= &devlink
->resource_list
;
7711 struct devlink_resource
*parent_resource
;
7713 parent_resource
= devlink_resource_find(devlink
, NULL
,
7714 parent_resource_id
);
7715 if (parent_resource
) {
7716 resource_list
= &parent_resource
->resource_list
;
7717 resource
->parent
= parent_resource
;
7725 resource
->name
= resource_name
;
7726 resource
->size
= resource_size
;
7727 resource
->size_new
= resource_size
;
7728 resource
->id
= resource_id
;
7729 resource
->size_valid
= true;
7730 memcpy(&resource
->size_params
, size_params
,
7731 sizeof(resource
->size_params
));
7732 INIT_LIST_HEAD(&resource
->resource_list
);
7733 list_add_tail(&resource
->list
, resource_list
);
7735 mutex_unlock(&devlink
->lock
);
7738 EXPORT_SYMBOL_GPL(devlink_resource_register
);
7741 * devlink_resources_unregister - free all resources
7744 * @resource: resource
7746 void devlink_resources_unregister(struct devlink
*devlink
,
7747 struct devlink_resource
*resource
)
7749 struct devlink_resource
*tmp
, *child_resource
;
7750 struct list_head
*resource_list
;
7753 resource_list
= &resource
->resource_list
;
7755 resource_list
= &devlink
->resource_list
;
7758 mutex_lock(&devlink
->lock
);
7760 list_for_each_entry_safe(child_resource
, tmp
, resource_list
, list
) {
7761 devlink_resources_unregister(devlink
, child_resource
);
7762 list_del(&child_resource
->list
);
7763 kfree(child_resource
);
7767 mutex_unlock(&devlink
->lock
);
7769 EXPORT_SYMBOL_GPL(devlink_resources_unregister
);
7772 * devlink_resource_size_get - get and update size
7775 * @resource_id: the requested resource id
7776 * @p_resource_size: ptr to update
7778 int devlink_resource_size_get(struct devlink
*devlink
,
7780 u64
*p_resource_size
)
7782 struct devlink_resource
*resource
;
7785 mutex_lock(&devlink
->lock
);
7786 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7791 *p_resource_size
= resource
->size_new
;
7792 resource
->size
= resource
->size_new
;
7794 mutex_unlock(&devlink
->lock
);
7797 EXPORT_SYMBOL_GPL(devlink_resource_size_get
);
7800 * devlink_dpipe_table_resource_set - set the resource id
7803 * @table_name: table name
7804 * @resource_id: resource id
7805 * @resource_units: number of resource's units consumed per table's entry
7807 int devlink_dpipe_table_resource_set(struct devlink
*devlink
,
7808 const char *table_name
, u64 resource_id
,
7811 struct devlink_dpipe_table
*table
;
7814 mutex_lock(&devlink
->lock
);
7815 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
7816 table_name
, devlink
);
7821 table
->resource_id
= resource_id
;
7822 table
->resource_units
= resource_units
;
7823 table
->resource_valid
= true;
7825 mutex_unlock(&devlink
->lock
);
7828 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set
);
7831 * devlink_resource_occ_get_register - register occupancy getter
7834 * @resource_id: resource id
7835 * @occ_get: occupancy getter callback
7836 * @occ_get_priv: occupancy getter callback priv
7838 void devlink_resource_occ_get_register(struct devlink
*devlink
,
7840 devlink_resource_occ_get_t
*occ_get
,
7843 struct devlink_resource
*resource
;
7845 mutex_lock(&devlink
->lock
);
7846 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7847 if (WARN_ON(!resource
))
7849 WARN_ON(resource
->occ_get
);
7851 resource
->occ_get
= occ_get
;
7852 resource
->occ_get_priv
= occ_get_priv
;
7854 mutex_unlock(&devlink
->lock
);
7856 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register
);
7859 * devlink_resource_occ_get_unregister - unregister occupancy getter
7862 * @resource_id: resource id
7864 void devlink_resource_occ_get_unregister(struct devlink
*devlink
,
7867 struct devlink_resource
*resource
;
7869 mutex_lock(&devlink
->lock
);
7870 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7871 if (WARN_ON(!resource
))
7873 WARN_ON(!resource
->occ_get
);
7875 resource
->occ_get
= NULL
;
7876 resource
->occ_get_priv
= NULL
;
7878 mutex_unlock(&devlink
->lock
);
7880 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister
);
7882 static int devlink_param_verify(const struct devlink_param
*param
)
7884 if (!param
|| !param
->name
|| !param
->supported_cmodes
)
7887 return devlink_param_generic_verify(param
);
7889 return devlink_param_driver_verify(param
);
7892 static int __devlink_params_register(struct devlink
*devlink
,
7893 unsigned int port_index
,
7894 struct list_head
*param_list
,
7895 const struct devlink_param
*params
,
7896 size_t params_count
,
7897 enum devlink_command reg_cmd
,
7898 enum devlink_command unreg_cmd
)
7900 const struct devlink_param
*param
= params
;
7904 mutex_lock(&devlink
->lock
);
7905 for (i
= 0; i
< params_count
; i
++, param
++) {
7906 err
= devlink_param_verify(param
);
7910 err
= devlink_param_register_one(devlink
, port_index
,
7911 param_list
, param
, reg_cmd
);
7916 mutex_unlock(&devlink
->lock
);
7922 for (param
--; i
> 0; i
--, param
--)
7923 devlink_param_unregister_one(devlink
, port_index
, param_list
,
7926 mutex_unlock(&devlink
->lock
);
7930 static void __devlink_params_unregister(struct devlink
*devlink
,
7931 unsigned int port_index
,
7932 struct list_head
*param_list
,
7933 const struct devlink_param
*params
,
7934 size_t params_count
,
7935 enum devlink_command cmd
)
7937 const struct devlink_param
*param
= params
;
7940 mutex_lock(&devlink
->lock
);
7941 for (i
= 0; i
< params_count
; i
++, param
++)
7942 devlink_param_unregister_one(devlink
, 0, param_list
, param
,
7944 mutex_unlock(&devlink
->lock
);
7948 * devlink_params_register - register configuration parameters
7951 * @params: configuration parameters array
7952 * @params_count: number of parameters provided
7954 * Register the configuration parameters supported by the driver.
7956 int devlink_params_register(struct devlink
*devlink
,
7957 const struct devlink_param
*params
,
7958 size_t params_count
)
7960 return __devlink_params_register(devlink
, 0, &devlink
->param_list
,
7961 params
, params_count
,
7962 DEVLINK_CMD_PARAM_NEW
,
7963 DEVLINK_CMD_PARAM_DEL
);
7965 EXPORT_SYMBOL_GPL(devlink_params_register
);
7968 * devlink_params_unregister - unregister configuration parameters
7970 * @params: configuration parameters to unregister
7971 * @params_count: number of parameters provided
7973 void devlink_params_unregister(struct devlink
*devlink
,
7974 const struct devlink_param
*params
,
7975 size_t params_count
)
7977 return __devlink_params_unregister(devlink
, 0, &devlink
->param_list
,
7978 params
, params_count
,
7979 DEVLINK_CMD_PARAM_DEL
);
7981 EXPORT_SYMBOL_GPL(devlink_params_unregister
);
7984 * devlink_params_publish - publish configuration parameters
7988 * Publish previously registered configuration parameters.
7990 void devlink_params_publish(struct devlink
*devlink
)
7992 struct devlink_param_item
*param_item
;
7994 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
7995 if (param_item
->published
)
7997 param_item
->published
= true;
7998 devlink_param_notify(devlink
, 0, param_item
,
7999 DEVLINK_CMD_PARAM_NEW
);
8002 EXPORT_SYMBOL_GPL(devlink_params_publish
);
8005 * devlink_params_unpublish - unpublish configuration parameters
8009 * Unpublish previously registered configuration parameters.
8011 void devlink_params_unpublish(struct devlink
*devlink
)
8013 struct devlink_param_item
*param_item
;
8015 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
8016 if (!param_item
->published
)
8018 param_item
->published
= false;
8019 devlink_param_notify(devlink
, 0, param_item
,
8020 DEVLINK_CMD_PARAM_DEL
);
8023 EXPORT_SYMBOL_GPL(devlink_params_unpublish
);
8026 * devlink_port_params_register - register port configuration parameters
8028 * @devlink_port: devlink port
8029 * @params: configuration parameters array
8030 * @params_count: number of parameters provided
8032 * Register the configuration parameters supported by the port.
8034 int devlink_port_params_register(struct devlink_port
*devlink_port
,
8035 const struct devlink_param
*params
,
8036 size_t params_count
)
8038 return __devlink_params_register(devlink_port
->devlink
,
8039 devlink_port
->index
,
8040 &devlink_port
->param_list
, params
,
8042 DEVLINK_CMD_PORT_PARAM_NEW
,
8043 DEVLINK_CMD_PORT_PARAM_DEL
);
8045 EXPORT_SYMBOL_GPL(devlink_port_params_register
);
8048 * devlink_port_params_unregister - unregister port configuration
8051 * @devlink_port: devlink port
8052 * @params: configuration parameters array
8053 * @params_count: number of parameters provided
8055 void devlink_port_params_unregister(struct devlink_port
*devlink_port
,
8056 const struct devlink_param
*params
,
8057 size_t params_count
)
8059 return __devlink_params_unregister(devlink_port
->devlink
,
8060 devlink_port
->index
,
8061 &devlink_port
->param_list
,
8062 params
, params_count
,
8063 DEVLINK_CMD_PORT_PARAM_DEL
);
8065 EXPORT_SYMBOL_GPL(devlink_port_params_unregister
);
8068 __devlink_param_driverinit_value_get(struct list_head
*param_list
, u32 param_id
,
8069 union devlink_param_value
*init_val
)
8071 struct devlink_param_item
*param_item
;
8073 param_item
= devlink_param_find_by_id(param_list
, param_id
);
8077 if (!param_item
->driverinit_value_valid
||
8078 !devlink_param_cmode_is_supported(param_item
->param
,
8079 DEVLINK_PARAM_CMODE_DRIVERINIT
))
8082 if (param_item
->param
->type
== DEVLINK_PARAM_TYPE_STRING
)
8083 strcpy(init_val
->vstr
, param_item
->driverinit_value
.vstr
);
8085 *init_val
= param_item
->driverinit_value
;
8091 __devlink_param_driverinit_value_set(struct devlink
*devlink
,
8092 unsigned int port_index
,
8093 struct list_head
*param_list
, u32 param_id
,
8094 union devlink_param_value init_val
,
8095 enum devlink_command cmd
)
8097 struct devlink_param_item
*param_item
;
8099 param_item
= devlink_param_find_by_id(param_list
, param_id
);
8103 if (!devlink_param_cmode_is_supported(param_item
->param
,
8104 DEVLINK_PARAM_CMODE_DRIVERINIT
))
8107 if (param_item
->param
->type
== DEVLINK_PARAM_TYPE_STRING
)
8108 strcpy(param_item
->driverinit_value
.vstr
, init_val
.vstr
);
8110 param_item
->driverinit_value
= init_val
;
8111 param_item
->driverinit_value_valid
= true;
8113 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
8118 * devlink_param_driverinit_value_get - get configuration parameter
8119 * value for driver initializing
8122 * @param_id: parameter ID
8123 * @init_val: value of parameter in driverinit configuration mode
8125 * This function should be used by the driver to get driverinit
8126 * configuration for initialization after reload command.
8128 int devlink_param_driverinit_value_get(struct devlink
*devlink
, u32 param_id
,
8129 union devlink_param_value
*init_val
)
8131 if (!devlink_reload_supported(devlink
))
8134 return __devlink_param_driverinit_value_get(&devlink
->param_list
,
8135 param_id
, init_val
);
8137 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get
);
8140 * devlink_param_driverinit_value_set - set value of configuration
8141 * parameter for driverinit
8142 * configuration mode
8145 * @param_id: parameter ID
8146 * @init_val: value of parameter to set for driverinit configuration mode
8148 * This function should be used by the driver to set driverinit
8149 * configuration mode default value.
8151 int devlink_param_driverinit_value_set(struct devlink
*devlink
, u32 param_id
,
8152 union devlink_param_value init_val
)
8154 return __devlink_param_driverinit_value_set(devlink
, 0,
8155 &devlink
->param_list
,
8157 DEVLINK_CMD_PARAM_NEW
);
8159 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set
);
8162 * devlink_port_param_driverinit_value_get - get configuration parameter
8163 * value for driver initializing
8165 * @devlink_port: devlink_port
8166 * @param_id: parameter ID
8167 * @init_val: value of parameter in driverinit configuration mode
8169 * This function should be used by the driver to get driverinit
8170 * configuration for initialization after reload command.
8172 int devlink_port_param_driverinit_value_get(struct devlink_port
*devlink_port
,
8174 union devlink_param_value
*init_val
)
8176 struct devlink
*devlink
= devlink_port
->devlink
;
8178 if (!devlink_reload_supported(devlink
))
8181 return __devlink_param_driverinit_value_get(&devlink_port
->param_list
,
8182 param_id
, init_val
);
8184 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get
);
8187 * devlink_port_param_driverinit_value_set - set value of configuration
8188 * parameter for driverinit
8189 * configuration mode
8191 * @devlink_port: devlink_port
8192 * @param_id: parameter ID
8193 * @init_val: value of parameter to set for driverinit configuration mode
8195 * This function should be used by the driver to set driverinit
8196 * configuration mode default value.
8198 int devlink_port_param_driverinit_value_set(struct devlink_port
*devlink_port
,
8200 union devlink_param_value init_val
)
8202 return __devlink_param_driverinit_value_set(devlink_port
->devlink
,
8203 devlink_port
->index
,
8204 &devlink_port
->param_list
,
8206 DEVLINK_CMD_PORT_PARAM_NEW
);
8208 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set
);
8211 * devlink_param_value_changed - notify devlink on a parameter's value
8212 * change. Should be called by the driver
8213 * right after the change.
8216 * @param_id: parameter ID
8218 * This function should be used by the driver to notify devlink on value
8219 * change, excluding driverinit configuration mode.
8220 * For driverinit configuration mode driver should use the function
8222 void devlink_param_value_changed(struct devlink
*devlink
, u32 param_id
)
8224 struct devlink_param_item
*param_item
;
8226 param_item
= devlink_param_find_by_id(&devlink
->param_list
, param_id
);
8227 WARN_ON(!param_item
);
8229 devlink_param_notify(devlink
, 0, param_item
, DEVLINK_CMD_PARAM_NEW
);
8231 EXPORT_SYMBOL_GPL(devlink_param_value_changed
);
8234 * devlink_port_param_value_changed - notify devlink on a parameter's value
8235 * change. Should be called by the driver
8236 * right after the change.
8238 * @devlink_port: devlink_port
8239 * @param_id: parameter ID
8241 * This function should be used by the driver to notify devlink on value
8242 * change, excluding driverinit configuration mode.
8243 * For driverinit configuration mode driver should use the function
8244 * devlink_port_param_driverinit_value_set() instead.
8246 void devlink_port_param_value_changed(struct devlink_port
*devlink_port
,
8249 struct devlink_param_item
*param_item
;
8251 param_item
= devlink_param_find_by_id(&devlink_port
->param_list
,
8253 WARN_ON(!param_item
);
8255 devlink_param_notify(devlink_port
->devlink
, devlink_port
->index
,
8256 param_item
, DEVLINK_CMD_PORT_PARAM_NEW
);
8258 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed
);
8261 * devlink_param_value_str_fill - Safely fill-up the string preventing
8262 * from overflow of the preallocated buffer
8264 * @dst_val: destination devlink_param_value
8265 * @src: source buffer
8267 void devlink_param_value_str_fill(union devlink_param_value
*dst_val
,
8272 len
= strlcpy(dst_val
->vstr
, src
, __DEVLINK_PARAM_MAX_STRING_VALUE
);
8273 WARN_ON(len
>= __DEVLINK_PARAM_MAX_STRING_VALUE
);
8275 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill
);
8278 * devlink_region_create - create a new address region
8281 * @ops: region operations and name
8282 * @region_max_snapshots: Maximum supported number of snapshots for region
8283 * @region_size: size of region
8285 struct devlink_region
*
8286 devlink_region_create(struct devlink
*devlink
,
8287 const struct devlink_region_ops
*ops
,
8288 u32 region_max_snapshots
, u64 region_size
)
8290 struct devlink_region
*region
;
8293 if (WARN_ON(!ops
) || WARN_ON(!ops
->destructor
))
8294 return ERR_PTR(-EINVAL
);
8296 mutex_lock(&devlink
->lock
);
8298 if (devlink_region_get_by_name(devlink
, ops
->name
)) {
8303 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
8309 region
->devlink
= devlink
;
8310 region
->max_snapshots
= region_max_snapshots
;
8312 region
->size
= region_size
;
8313 INIT_LIST_HEAD(®ion
->snapshot_list
);
8314 list_add_tail(®ion
->list
, &devlink
->region_list
);
8315 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_NEW
);
8317 mutex_unlock(&devlink
->lock
);
8321 mutex_unlock(&devlink
->lock
);
8322 return ERR_PTR(err
);
8324 EXPORT_SYMBOL_GPL(devlink_region_create
);
8327 * devlink_region_destroy - destroy address region
8329 * @region: devlink region to destroy
8331 void devlink_region_destroy(struct devlink_region
*region
)
8333 struct devlink
*devlink
= region
->devlink
;
8334 struct devlink_snapshot
*snapshot
, *ts
;
8336 mutex_lock(&devlink
->lock
);
8338 /* Free all snapshots of region */
8339 list_for_each_entry_safe(snapshot
, ts
, ®ion
->snapshot_list
, list
)
8340 devlink_region_snapshot_del(region
, snapshot
);
8342 list_del(®ion
->list
);
8344 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_DEL
);
8345 mutex_unlock(&devlink
->lock
);
8348 EXPORT_SYMBOL_GPL(devlink_region_destroy
);
8351 * devlink_region_snapshot_id_get - get snapshot ID
8353 * This callback should be called when adding a new snapshot,
8354 * Driver should use the same id for multiple snapshots taken
8355 * on multiple regions at the same time/by the same trigger.
8357 * The caller of this function must use devlink_region_snapshot_id_put
8358 * when finished creating regions using this id.
8360 * Returns zero on success, or a negative error code on failure.
8363 * @id: storage to return id
8365 int devlink_region_snapshot_id_get(struct devlink
*devlink
, u32
*id
)
8369 mutex_lock(&devlink
->lock
);
8370 err
= __devlink_region_snapshot_id_get(devlink
, id
);
8371 mutex_unlock(&devlink
->lock
);
8375 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get
);
8378 * devlink_region_snapshot_id_put - put snapshot ID reference
8380 * This should be called by a driver after finishing creating snapshots
8381 * with an id. Doing so ensures that the ID can later be released in the
8382 * event that all snapshots using it have been destroyed.
8385 * @id: id to release reference on
8387 void devlink_region_snapshot_id_put(struct devlink
*devlink
, u32 id
)
8389 mutex_lock(&devlink
->lock
);
8390 __devlink_snapshot_id_decrement(devlink
, id
);
8391 mutex_unlock(&devlink
->lock
);
8393 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put
);
8396 * devlink_region_snapshot_create - create a new snapshot
8397 * This will add a new snapshot of a region. The snapshot
8398 * will be stored on the region struct and can be accessed
8399 * from devlink. This is useful for future analyses of snapshots.
8400 * Multiple snapshots can be created on a region.
8401 * The @snapshot_id should be obtained using the getter function.
8403 * @region: devlink region of the snapshot
8404 * @data: snapshot data
8405 * @snapshot_id: snapshot id to be created
8407 int devlink_region_snapshot_create(struct devlink_region
*region
,
8408 u8
*data
, u32 snapshot_id
)
8410 struct devlink
*devlink
= region
->devlink
;
8413 mutex_lock(&devlink
->lock
);
8414 err
= __devlink_region_snapshot_create(region
, data
, snapshot_id
);
8415 mutex_unlock(&devlink
->lock
);
8419 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create
);
8421 #define DEVLINK_TRAP(_id, _type) \
8423 .type = DEVLINK_TRAP_TYPE_##_type, \
8424 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
8425 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
8428 static const struct devlink_trap devlink_trap_generic
[] = {
8429 DEVLINK_TRAP(SMAC_MC
, DROP
),
8430 DEVLINK_TRAP(VLAN_TAG_MISMATCH
, DROP
),
8431 DEVLINK_TRAP(INGRESS_VLAN_FILTER
, DROP
),
8432 DEVLINK_TRAP(INGRESS_STP_FILTER
, DROP
),
8433 DEVLINK_TRAP(EMPTY_TX_LIST
, DROP
),
8434 DEVLINK_TRAP(PORT_LOOPBACK_FILTER
, DROP
),
8435 DEVLINK_TRAP(BLACKHOLE_ROUTE
, DROP
),
8436 DEVLINK_TRAP(TTL_ERROR
, EXCEPTION
),
8437 DEVLINK_TRAP(TAIL_DROP
, DROP
),
8438 DEVLINK_TRAP(NON_IP_PACKET
, DROP
),
8439 DEVLINK_TRAP(UC_DIP_MC_DMAC
, DROP
),
8440 DEVLINK_TRAP(DIP_LB
, DROP
),
8441 DEVLINK_TRAP(SIP_MC
, DROP
),
8442 DEVLINK_TRAP(SIP_LB
, DROP
),
8443 DEVLINK_TRAP(CORRUPTED_IP_HDR
, DROP
),
8444 DEVLINK_TRAP(IPV4_SIP_BC
, DROP
),
8445 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE
, DROP
),
8446 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE
, DROP
),
8447 DEVLINK_TRAP(MTU_ERROR
, EXCEPTION
),
8448 DEVLINK_TRAP(UNRESOLVED_NEIGH
, EXCEPTION
),
8449 DEVLINK_TRAP(RPF
, EXCEPTION
),
8450 DEVLINK_TRAP(REJECT_ROUTE
, EXCEPTION
),
8451 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS
, EXCEPTION
),
8452 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS
, EXCEPTION
),
8453 DEVLINK_TRAP(NON_ROUTABLE
, DROP
),
8454 DEVLINK_TRAP(DECAP_ERROR
, EXCEPTION
),
8455 DEVLINK_TRAP(OVERLAY_SMAC_MC
, DROP
),
8456 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP
, DROP
),
8457 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP
, DROP
),
8460 #define DEVLINK_TRAP_GROUP(_id) \
8462 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
8463 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
8466 static const struct devlink_trap_group devlink_trap_group_generic
[] = {
8467 DEVLINK_TRAP_GROUP(L2_DROPS
),
8468 DEVLINK_TRAP_GROUP(L3_DROPS
),
8469 DEVLINK_TRAP_GROUP(BUFFER_DROPS
),
8470 DEVLINK_TRAP_GROUP(TUNNEL_DROPS
),
8471 DEVLINK_TRAP_GROUP(ACL_DROPS
),
8474 static int devlink_trap_generic_verify(const struct devlink_trap
*trap
)
8476 if (trap
->id
> DEVLINK_TRAP_GENERIC_ID_MAX
)
8479 if (strcmp(trap
->name
, devlink_trap_generic
[trap
->id
].name
))
8482 if (trap
->type
!= devlink_trap_generic
[trap
->id
].type
)
8488 static int devlink_trap_driver_verify(const struct devlink_trap
*trap
)
8492 if (trap
->id
<= DEVLINK_TRAP_GENERIC_ID_MAX
)
8495 for (i
= 0; i
< ARRAY_SIZE(devlink_trap_generic
); i
++) {
8496 if (!strcmp(trap
->name
, devlink_trap_generic
[i
].name
))
8503 static int devlink_trap_verify(const struct devlink_trap
*trap
)
8505 if (!trap
|| !trap
->name
)
8509 return devlink_trap_generic_verify(trap
);
8511 return devlink_trap_driver_verify(trap
);
8515 devlink_trap_group_generic_verify(const struct devlink_trap_group
*group
)
8517 if (group
->id
> DEVLINK_TRAP_GROUP_GENERIC_ID_MAX
)
8520 if (strcmp(group
->name
, devlink_trap_group_generic
[group
->id
].name
))
8527 devlink_trap_group_driver_verify(const struct devlink_trap_group
*group
)
8531 if (group
->id
<= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX
)
8534 for (i
= 0; i
< ARRAY_SIZE(devlink_trap_group_generic
); i
++) {
8535 if (!strcmp(group
->name
, devlink_trap_group_generic
[i
].name
))
8542 static int devlink_trap_group_verify(const struct devlink_trap_group
*group
)
8545 return devlink_trap_group_generic_verify(group
);
8547 return devlink_trap_group_driver_verify(group
);
8551 devlink_trap_group_notify(struct devlink
*devlink
,
8552 const struct devlink_trap_group_item
*group_item
,
8553 enum devlink_command cmd
)
8555 struct sk_buff
*msg
;
8558 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_GROUP_NEW
&&
8559 cmd
!= DEVLINK_CMD_TRAP_GROUP_DEL
);
8561 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
8565 err
= devlink_nl_trap_group_fill(msg
, devlink
, group_item
, cmd
, 0, 0,
8572 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
8573 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
8577 devlink_trap_item_group_link(struct devlink
*devlink
,
8578 struct devlink_trap_item
*trap_item
)
8580 u16 group_id
= trap_item
->trap
->init_group_id
;
8581 struct devlink_trap_group_item
*group_item
;
8583 group_item
= devlink_trap_group_item_lookup_by_id(devlink
, group_id
);
8584 if (WARN_ON_ONCE(!group_item
))
8587 trap_item
->group_item
= group_item
;
8592 static void devlink_trap_notify(struct devlink
*devlink
,
8593 const struct devlink_trap_item
*trap_item
,
8594 enum devlink_command cmd
)
8596 struct sk_buff
*msg
;
8599 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_NEW
&&
8600 cmd
!= DEVLINK_CMD_TRAP_DEL
);
8602 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
8606 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
, cmd
, 0, 0, 0);
8612 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
8613 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
8617 devlink_trap_register(struct devlink
*devlink
,
8618 const struct devlink_trap
*trap
, void *priv
)
8620 struct devlink_trap_item
*trap_item
;
8623 if (devlink_trap_item_lookup(devlink
, trap
->name
))
8626 trap_item
= kzalloc(sizeof(*trap_item
), GFP_KERNEL
);
8630 trap_item
->stats
= netdev_alloc_pcpu_stats(struct devlink_stats
);
8631 if (!trap_item
->stats
) {
8633 goto err_stats_alloc
;
8636 trap_item
->trap
= trap
;
8637 trap_item
->action
= trap
->init_action
;
8638 trap_item
->priv
= priv
;
8640 err
= devlink_trap_item_group_link(devlink
, trap_item
);
8642 goto err_group_link
;
8644 err
= devlink
->ops
->trap_init(devlink
, trap
, trap_item
);
8648 list_add_tail(&trap_item
->list
, &devlink
->trap_list
);
8649 devlink_trap_notify(devlink
, trap_item
, DEVLINK_CMD_TRAP_NEW
);
8655 free_percpu(trap_item
->stats
);
8661 static void devlink_trap_unregister(struct devlink
*devlink
,
8662 const struct devlink_trap
*trap
)
8664 struct devlink_trap_item
*trap_item
;
8666 trap_item
= devlink_trap_item_lookup(devlink
, trap
->name
);
8667 if (WARN_ON_ONCE(!trap_item
))
8670 devlink_trap_notify(devlink
, trap_item
, DEVLINK_CMD_TRAP_DEL
);
8671 list_del(&trap_item
->list
);
8672 if (devlink
->ops
->trap_fini
)
8673 devlink
->ops
->trap_fini(devlink
, trap
, trap_item
);
8674 free_percpu(trap_item
->stats
);
8678 static void devlink_trap_disable(struct devlink
*devlink
,
8679 const struct devlink_trap
*trap
)
8681 struct devlink_trap_item
*trap_item
;
8683 trap_item
= devlink_trap_item_lookup(devlink
, trap
->name
);
8684 if (WARN_ON_ONCE(!trap_item
))
8687 devlink
->ops
->trap_action_set(devlink
, trap
, DEVLINK_TRAP_ACTION_DROP
);
8688 trap_item
->action
= DEVLINK_TRAP_ACTION_DROP
;
8692 * devlink_traps_register - Register packet traps with devlink.
8693 * @devlink: devlink.
8694 * @traps: Packet traps.
8695 * @traps_count: Count of provided packet traps.
8696 * @priv: Driver private information.
8698 * Return: Non-zero value on failure.
8700 int devlink_traps_register(struct devlink
*devlink
,
8701 const struct devlink_trap
*traps
,
8702 size_t traps_count
, void *priv
)
8706 if (!devlink
->ops
->trap_init
|| !devlink
->ops
->trap_action_set
)
8709 mutex_lock(&devlink
->lock
);
8710 for (i
= 0; i
< traps_count
; i
++) {
8711 const struct devlink_trap
*trap
= &traps
[i
];
8713 err
= devlink_trap_verify(trap
);
8715 goto err_trap_verify
;
8717 err
= devlink_trap_register(devlink
, trap
, priv
);
8719 goto err_trap_register
;
8721 mutex_unlock(&devlink
->lock
);
8727 for (i
--; i
>= 0; i
--)
8728 devlink_trap_unregister(devlink
, &traps
[i
]);
8729 mutex_unlock(&devlink
->lock
);
8732 EXPORT_SYMBOL_GPL(devlink_traps_register
);
8735 * devlink_traps_unregister - Unregister packet traps from devlink.
8736 * @devlink: devlink.
8737 * @traps: Packet traps.
8738 * @traps_count: Count of provided packet traps.
8740 void devlink_traps_unregister(struct devlink
*devlink
,
8741 const struct devlink_trap
*traps
,
8746 mutex_lock(&devlink
->lock
);
8747 /* Make sure we do not have any packets in-flight while unregistering
8748 * traps by disabling all of them and waiting for a grace period.
8750 for (i
= traps_count
- 1; i
>= 0; i
--)
8751 devlink_trap_disable(devlink
, &traps
[i
]);
8753 for (i
= traps_count
- 1; i
>= 0; i
--)
8754 devlink_trap_unregister(devlink
, &traps
[i
]);
8755 mutex_unlock(&devlink
->lock
);
8757 EXPORT_SYMBOL_GPL(devlink_traps_unregister
);
8760 devlink_trap_stats_update(struct devlink_stats __percpu
*trap_stats
,
8763 struct devlink_stats
*stats
;
8765 stats
= this_cpu_ptr(trap_stats
);
8766 u64_stats_update_begin(&stats
->syncp
);
8767 stats
->rx_bytes
+= skb_len
;
8768 stats
->rx_packets
++;
8769 u64_stats_update_end(&stats
->syncp
);
8773 devlink_trap_report_metadata_fill(struct net_dm_hw_metadata
*hw_metadata
,
8774 const struct devlink_trap_item
*trap_item
,
8775 struct devlink_port
*in_devlink_port
,
8776 const struct flow_action_cookie
*fa_cookie
)
8778 struct devlink_trap_group_item
*group_item
= trap_item
->group_item
;
8780 hw_metadata
->trap_group_name
= group_item
->group
->name
;
8781 hw_metadata
->trap_name
= trap_item
->trap
->name
;
8782 hw_metadata
->fa_cookie
= fa_cookie
;
8784 spin_lock(&in_devlink_port
->type_lock
);
8785 if (in_devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
)
8786 hw_metadata
->input_dev
= in_devlink_port
->type_dev
;
8787 spin_unlock(&in_devlink_port
->type_lock
);
8791 * devlink_trap_report - Report trapped packet to drop monitor.
8792 * @devlink: devlink.
8793 * @skb: Trapped packet.
8794 * @trap_ctx: Trap context.
8795 * @in_devlink_port: Input devlink port.
8796 * @fa_cookie: Flow action cookie. Could be NULL.
8798 void devlink_trap_report(struct devlink
*devlink
, struct sk_buff
*skb
,
8799 void *trap_ctx
, struct devlink_port
*in_devlink_port
,
8800 const struct flow_action_cookie
*fa_cookie
)
8803 struct devlink_trap_item
*trap_item
= trap_ctx
;
8804 struct net_dm_hw_metadata hw_metadata
= {};
8806 devlink_trap_stats_update(trap_item
->stats
, skb
->len
);
8807 devlink_trap_stats_update(trap_item
->group_item
->stats
, skb
->len
);
8809 devlink_trap_report_metadata_fill(&hw_metadata
, trap_item
,
8810 in_devlink_port
, fa_cookie
);
8811 net_dm_hw_report(skb
, &hw_metadata
);
8813 EXPORT_SYMBOL_GPL(devlink_trap_report
);
8816 * devlink_trap_ctx_priv - Trap context to driver private information.
8817 * @trap_ctx: Trap context.
8819 * Return: Driver private information passed during registration.
8821 void *devlink_trap_ctx_priv(void *trap_ctx
)
8823 struct devlink_trap_item
*trap_item
= trap_ctx
;
8825 return trap_item
->priv
;
8827 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv
);
8830 devlink_trap_group_item_policer_link(struct devlink
*devlink
,
8831 struct devlink_trap_group_item
*group_item
)
8833 u32 policer_id
= group_item
->group
->init_policer_id
;
8834 struct devlink_trap_policer_item
*policer_item
;
8836 if (policer_id
== 0)
8839 policer_item
= devlink_trap_policer_item_lookup(devlink
, policer_id
);
8840 if (WARN_ON_ONCE(!policer_item
))
8843 group_item
->policer_item
= policer_item
;
8849 devlink_trap_group_register(struct devlink
*devlink
,
8850 const struct devlink_trap_group
*group
)
8852 struct devlink_trap_group_item
*group_item
;
8855 if (devlink_trap_group_item_lookup(devlink
, group
->name
))
8858 group_item
= kzalloc(sizeof(*group_item
), GFP_KERNEL
);
8862 group_item
->stats
= netdev_alloc_pcpu_stats(struct devlink_stats
);
8863 if (!group_item
->stats
) {
8865 goto err_stats_alloc
;
8868 group_item
->group
= group
;
8870 err
= devlink_trap_group_item_policer_link(devlink
, group_item
);
8872 goto err_policer_link
;
8874 if (devlink
->ops
->trap_group_init
) {
8875 err
= devlink
->ops
->trap_group_init(devlink
, group
);
8877 goto err_group_init
;
8880 list_add_tail(&group_item
->list
, &devlink
->trap_group_list
);
8881 devlink_trap_group_notify(devlink
, group_item
,
8882 DEVLINK_CMD_TRAP_GROUP_NEW
);
8888 free_percpu(group_item
->stats
);
8895 devlink_trap_group_unregister(struct devlink
*devlink
,
8896 const struct devlink_trap_group
*group
)
8898 struct devlink_trap_group_item
*group_item
;
8900 group_item
= devlink_trap_group_item_lookup(devlink
, group
->name
);
8901 if (WARN_ON_ONCE(!group_item
))
8904 devlink_trap_group_notify(devlink
, group_item
,
8905 DEVLINK_CMD_TRAP_GROUP_DEL
);
8906 list_del(&group_item
->list
);
8907 free_percpu(group_item
->stats
);
8912 * devlink_trap_groups_register - Register packet trap groups with devlink.
8913 * @devlink: devlink.
8914 * @groups: Packet trap groups.
8915 * @groups_count: Count of provided packet trap groups.
8917 * Return: Non-zero value on failure.
8919 int devlink_trap_groups_register(struct devlink
*devlink
,
8920 const struct devlink_trap_group
*groups
,
8921 size_t groups_count
)
8925 mutex_lock(&devlink
->lock
);
8926 for (i
= 0; i
< groups_count
; i
++) {
8927 const struct devlink_trap_group
*group
= &groups
[i
];
8929 err
= devlink_trap_group_verify(group
);
8931 goto err_trap_group_verify
;
8933 err
= devlink_trap_group_register(devlink
, group
);
8935 goto err_trap_group_register
;
8937 mutex_unlock(&devlink
->lock
);
8941 err_trap_group_register
:
8942 err_trap_group_verify
:
8943 for (i
--; i
>= 0; i
--)
8944 devlink_trap_group_unregister(devlink
, &groups
[i
]);
8945 mutex_unlock(&devlink
->lock
);
8948 EXPORT_SYMBOL_GPL(devlink_trap_groups_register
);
8951 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
8952 * @devlink: devlink.
8953 * @groups: Packet trap groups.
8954 * @groups_count: Count of provided packet trap groups.
8956 void devlink_trap_groups_unregister(struct devlink
*devlink
,
8957 const struct devlink_trap_group
*groups
,
8958 size_t groups_count
)
8962 mutex_lock(&devlink
->lock
);
8963 for (i
= groups_count
- 1; i
>= 0; i
--)
8964 devlink_trap_group_unregister(devlink
, &groups
[i
]);
8965 mutex_unlock(&devlink
->lock
);
8967 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister
);
8970 devlink_trap_policer_notify(struct devlink
*devlink
,
8971 const struct devlink_trap_policer_item
*policer_item
,
8972 enum devlink_command cmd
)
8974 struct sk_buff
*msg
;
8977 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_POLICER_NEW
&&
8978 cmd
!= DEVLINK_CMD_TRAP_POLICER_DEL
);
8980 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
8984 err
= devlink_nl_trap_policer_fill(msg
, devlink
, policer_item
, cmd
, 0,
8991 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
8992 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
8996 devlink_trap_policer_register(struct devlink
*devlink
,
8997 const struct devlink_trap_policer
*policer
)
8999 struct devlink_trap_policer_item
*policer_item
;
9002 if (devlink_trap_policer_item_lookup(devlink
, policer
->id
))
9005 policer_item
= kzalloc(sizeof(*policer_item
), GFP_KERNEL
);
9009 policer_item
->policer
= policer
;
9010 policer_item
->rate
= policer
->init_rate
;
9011 policer_item
->burst
= policer
->init_burst
;
9013 if (devlink
->ops
->trap_policer_init
) {
9014 err
= devlink
->ops
->trap_policer_init(devlink
, policer
);
9016 goto err_policer_init
;
9019 list_add_tail(&policer_item
->list
, &devlink
->trap_policer_list
);
9020 devlink_trap_policer_notify(devlink
, policer_item
,
9021 DEVLINK_CMD_TRAP_POLICER_NEW
);
9026 kfree(policer_item
);
9031 devlink_trap_policer_unregister(struct devlink
*devlink
,
9032 const struct devlink_trap_policer
*policer
)
9034 struct devlink_trap_policer_item
*policer_item
;
9036 policer_item
= devlink_trap_policer_item_lookup(devlink
, policer
->id
);
9037 if (WARN_ON_ONCE(!policer_item
))
9040 devlink_trap_policer_notify(devlink
, policer_item
,
9041 DEVLINK_CMD_TRAP_POLICER_DEL
);
9042 list_del(&policer_item
->list
);
9043 if (devlink
->ops
->trap_policer_fini
)
9044 devlink
->ops
->trap_policer_fini(devlink
, policer
);
9045 kfree(policer_item
);
9049 * devlink_trap_policers_register - Register packet trap policers with devlink.
9050 * @devlink: devlink.
9051 * @policers: Packet trap policers.
9052 * @policers_count: Count of provided packet trap policers.
9054 * Return: Non-zero value on failure.
9057 devlink_trap_policers_register(struct devlink
*devlink
,
9058 const struct devlink_trap_policer
*policers
,
9059 size_t policers_count
)
9063 mutex_lock(&devlink
->lock
);
9064 for (i
= 0; i
< policers_count
; i
++) {
9065 const struct devlink_trap_policer
*policer
= &policers
[i
];
9067 if (WARN_ON(policer
->id
== 0 ||
9068 policer
->max_rate
< policer
->min_rate
||
9069 policer
->max_burst
< policer
->min_burst
)) {
9071 goto err_trap_policer_verify
;
9074 err
= devlink_trap_policer_register(devlink
, policer
);
9076 goto err_trap_policer_register
;
9078 mutex_unlock(&devlink
->lock
);
9082 err_trap_policer_register
:
9083 err_trap_policer_verify
:
9084 for (i
--; i
>= 0; i
--)
9085 devlink_trap_policer_unregister(devlink
, &policers
[i
]);
9086 mutex_unlock(&devlink
->lock
);
9089 EXPORT_SYMBOL_GPL(devlink_trap_policers_register
);
9092 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
9093 * @devlink: devlink.
9094 * @policers: Packet trap policers.
9095 * @policers_count: Count of provided packet trap policers.
9098 devlink_trap_policers_unregister(struct devlink
*devlink
,
9099 const struct devlink_trap_policer
*policers
,
9100 size_t policers_count
)
9104 mutex_lock(&devlink
->lock
);
9105 for (i
= policers_count
- 1; i
>= 0; i
--)
9106 devlink_trap_policer_unregister(devlink
, &policers
[i
]);
9107 mutex_unlock(&devlink
->lock
);
9109 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister
);
9111 static void __devlink_compat_running_version(struct devlink
*devlink
,
9112 char *buf
, size_t len
)
9114 const struct nlattr
*nlattr
;
9115 struct devlink_info_req req
;
9116 struct sk_buff
*msg
;
9119 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
9124 err
= devlink
->ops
->info_get(devlink
, &req
, NULL
);
9128 nla_for_each_attr(nlattr
, (void *)msg
->data
, msg
->len
, rem
) {
9129 const struct nlattr
*kv
;
9132 if (nla_type(nlattr
) != DEVLINK_ATTR_INFO_VERSION_RUNNING
)
9135 nla_for_each_nested(kv
, nlattr
, rem_kv
) {
9136 if (nla_type(kv
) != DEVLINK_ATTR_INFO_VERSION_VALUE
)
9139 strlcat(buf
, nla_data(kv
), len
);
9140 strlcat(buf
, " ", len
);
9147 void devlink_compat_running_version(struct net_device
*dev
,
9148 char *buf
, size_t len
)
9150 struct devlink
*devlink
;
9155 devlink
= netdev_to_devlink(dev
);
9156 if (!devlink
|| !devlink
->ops
->info_get
)
9159 mutex_lock(&devlink
->lock
);
9160 __devlink_compat_running_version(devlink
, buf
, len
);
9161 mutex_unlock(&devlink
->lock
);
9168 int devlink_compat_flash_update(struct net_device
*dev
, const char *file_name
)
9170 struct devlink
*devlink
;
9176 devlink
= netdev_to_devlink(dev
);
9177 if (!devlink
|| !devlink
->ops
->flash_update
) {
9182 mutex_lock(&devlink
->lock
);
9183 ret
= devlink
->ops
->flash_update(devlink
, file_name
, NULL
, NULL
);
9184 mutex_unlock(&devlink
->lock
);
9193 int devlink_compat_phys_port_name_get(struct net_device
*dev
,
9194 char *name
, size_t len
)
9196 struct devlink_port
*devlink_port
;
9198 /* RTNL mutex is held here which ensures that devlink_port
9199 * instance cannot disappear in the middle. No need to take
9200 * any devlink lock as only permanent values are accessed.
9204 devlink_port
= netdev_to_devlink_port(dev
);
9208 return __devlink_port_phys_port_name_get(devlink_port
, name
, len
);
9211 int devlink_compat_switch_id_get(struct net_device
*dev
,
9212 struct netdev_phys_item_id
*ppid
)
9214 struct devlink_port
*devlink_port
;
9216 /* Caller must hold RTNL mutex or reference to dev, which ensures that
9217 * devlink_port instance cannot disappear in the middle. No need to take
9218 * any devlink lock as only permanent values are accessed.
9220 devlink_port
= netdev_to_devlink_port(dev
);
9221 if (!devlink_port
|| !devlink_port
->attrs
.switch_port
)
9224 memcpy(ppid
, &devlink_port
->attrs
.switch_id
, sizeof(*ppid
));
9229 static void __net_exit
devlink_pernet_pre_exit(struct net
*net
)
9231 struct devlink
*devlink
;
9234 /* In case network namespace is getting destroyed, reload
9235 * all devlink instances from this namespace into init_net.
9237 mutex_lock(&devlink_mutex
);
9238 list_for_each_entry(devlink
, &devlink_list
, list
) {
9239 if (net_eq(devlink_net(devlink
), net
)) {
9240 if (WARN_ON(!devlink_reload_supported(devlink
)))
9242 err
= devlink_reload(devlink
, &init_net
, NULL
);
9243 if (err
&& err
!= -EOPNOTSUPP
)
9244 pr_warn("Failed to reload devlink instance into init_net\n");
9247 mutex_unlock(&devlink_mutex
);
9250 static struct pernet_operations devlink_pernet_ops __net_initdata
= {
9251 .pre_exit
= devlink_pernet_pre_exit
,
9254 static int __init
devlink_init(void)
9258 err
= genl_register_family(&devlink_nl_family
);
9261 err
= register_pernet_subsys(&devlink_pernet_ops
);
9268 subsys_initcall(devlink_init
);