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
]);
4288 err
= devlink_nl_region_read_snapshot_fill(skb
, devlink
,
4294 if (err
&& err
!= -EMSGSIZE
)
4295 goto nla_put_failure
;
4297 /* Check if there was any progress done to prevent infinite loop */
4298 if (ret_offset
== start_offset
) {
4300 goto nla_put_failure
;
4303 *((u64
*)&cb
->args
[0]) = ret_offset
;
4305 nla_nest_end(skb
, chunks_attr
);
4306 genlmsg_end(skb
, hdr
);
4307 mutex_unlock(&devlink
->lock
);
4308 mutex_unlock(&devlink_mutex
);
4313 genlmsg_cancel(skb
, hdr
);
4315 mutex_unlock(&devlink
->lock
);
4317 mutex_unlock(&devlink_mutex
);
4321 struct devlink_info_req
{
4322 struct sk_buff
*msg
;
4325 int devlink_info_driver_name_put(struct devlink_info_req
*req
, const char *name
)
4327 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_DRIVER_NAME
, name
);
4329 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put
);
4331 int devlink_info_serial_number_put(struct devlink_info_req
*req
, const char *sn
)
4333 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_SERIAL_NUMBER
, sn
);
4335 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put
);
4337 static int devlink_info_version_put(struct devlink_info_req
*req
, int attr
,
4338 const char *version_name
,
4339 const char *version_value
)
4341 struct nlattr
*nest
;
4344 nest
= nla_nest_start_noflag(req
->msg
, attr
);
4348 err
= nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_VERSION_NAME
,
4351 goto nla_put_failure
;
4353 err
= nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_VERSION_VALUE
,
4356 goto nla_put_failure
;
4358 nla_nest_end(req
->msg
, nest
);
4363 nla_nest_cancel(req
->msg
, nest
);
4367 int devlink_info_version_fixed_put(struct devlink_info_req
*req
,
4368 const char *version_name
,
4369 const char *version_value
)
4371 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_FIXED
,
4372 version_name
, version_value
);
4374 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put
);
4376 int devlink_info_version_stored_put(struct devlink_info_req
*req
,
4377 const char *version_name
,
4378 const char *version_value
)
4380 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_STORED
,
4381 version_name
, version_value
);
4383 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put
);
4385 int devlink_info_version_running_put(struct devlink_info_req
*req
,
4386 const char *version_name
,
4387 const char *version_value
)
4389 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_RUNNING
,
4390 version_name
, version_value
);
4392 EXPORT_SYMBOL_GPL(devlink_info_version_running_put
);
4395 devlink_nl_info_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
4396 enum devlink_command cmd
, u32 portid
,
4397 u32 seq
, int flags
, struct netlink_ext_ack
*extack
)
4399 struct devlink_info_req req
;
4403 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
4408 if (devlink_nl_put_handle(msg
, devlink
))
4409 goto err_cancel_msg
;
4412 err
= devlink
->ops
->info_get(devlink
, &req
, extack
);
4414 goto err_cancel_msg
;
4416 genlmsg_end(msg
, hdr
);
4420 genlmsg_cancel(msg
, hdr
);
4424 static int devlink_nl_cmd_info_get_doit(struct sk_buff
*skb
,
4425 struct genl_info
*info
)
4427 struct devlink
*devlink
= info
->user_ptr
[0];
4428 struct sk_buff
*msg
;
4431 if (!devlink
->ops
->info_get
)
4434 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
4438 err
= devlink_nl_info_fill(msg
, devlink
, DEVLINK_CMD_INFO_GET
,
4439 info
->snd_portid
, info
->snd_seq
, 0,
4446 return genlmsg_reply(msg
, info
);
4449 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff
*msg
,
4450 struct netlink_callback
*cb
)
4452 struct devlink
*devlink
;
4453 int start
= cb
->args
[0];
4457 mutex_lock(&devlink_mutex
);
4458 list_for_each_entry(devlink
, &devlink_list
, list
) {
4459 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
4466 if (!devlink
->ops
->info_get
) {
4471 mutex_lock(&devlink
->lock
);
4472 err
= devlink_nl_info_fill(msg
, devlink
, DEVLINK_CMD_INFO_GET
,
4473 NETLINK_CB(cb
->skb
).portid
,
4474 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
4476 mutex_unlock(&devlink
->lock
);
4477 if (err
&& err
!= -EOPNOTSUPP
)
4481 mutex_unlock(&devlink_mutex
);
4483 if (err
!= -EMSGSIZE
)
4490 struct devlink_fmsg_item
{
4491 struct list_head list
;
4498 struct devlink_fmsg
{
4499 struct list_head item_list
;
4500 bool putting_binary
; /* This flag forces enclosing of binary data
4501 * in an array brackets. It forces using
4502 * of designated API:
4503 * devlink_fmsg_binary_pair_nest_start()
4504 * devlink_fmsg_binary_pair_nest_end()
4508 static struct devlink_fmsg
*devlink_fmsg_alloc(void)
4510 struct devlink_fmsg
*fmsg
;
4512 fmsg
= kzalloc(sizeof(*fmsg
), GFP_KERNEL
);
4516 INIT_LIST_HEAD(&fmsg
->item_list
);
4521 static void devlink_fmsg_free(struct devlink_fmsg
*fmsg
)
4523 struct devlink_fmsg_item
*item
, *tmp
;
4525 list_for_each_entry_safe(item
, tmp
, &fmsg
->item_list
, list
) {
4526 list_del(&item
->list
);
4532 static int devlink_fmsg_nest_common(struct devlink_fmsg
*fmsg
,
4535 struct devlink_fmsg_item
*item
;
4537 item
= kzalloc(sizeof(*item
), GFP_KERNEL
);
4541 item
->attrtype
= attrtype
;
4542 list_add_tail(&item
->list
, &fmsg
->item_list
);
4547 int devlink_fmsg_obj_nest_start(struct devlink_fmsg
*fmsg
)
4549 if (fmsg
->putting_binary
)
4552 return devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_OBJ_NEST_START
);
4554 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start
);
4556 static int devlink_fmsg_nest_end(struct devlink_fmsg
*fmsg
)
4558 if (fmsg
->putting_binary
)
4561 return devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_NEST_END
);
4564 int devlink_fmsg_obj_nest_end(struct devlink_fmsg
*fmsg
)
4566 if (fmsg
->putting_binary
)
4569 return devlink_fmsg_nest_end(fmsg
);
4571 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end
);
4573 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4575 static int devlink_fmsg_put_name(struct devlink_fmsg
*fmsg
, const char *name
)
4577 struct devlink_fmsg_item
*item
;
4579 if (fmsg
->putting_binary
)
4582 if (strlen(name
) + 1 > DEVLINK_FMSG_MAX_SIZE
)
4585 item
= kzalloc(sizeof(*item
) + strlen(name
) + 1, GFP_KERNEL
);
4589 item
->nla_type
= NLA_NUL_STRING
;
4590 item
->len
= strlen(name
) + 1;
4591 item
->attrtype
= DEVLINK_ATTR_FMSG_OBJ_NAME
;
4592 memcpy(&item
->value
, name
, item
->len
);
4593 list_add_tail(&item
->list
, &fmsg
->item_list
);
4598 int devlink_fmsg_pair_nest_start(struct devlink_fmsg
*fmsg
, const char *name
)
4602 if (fmsg
->putting_binary
)
4605 err
= devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_PAIR_NEST_START
);
4609 err
= devlink_fmsg_put_name(fmsg
, name
);
4615 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start
);
4617 int devlink_fmsg_pair_nest_end(struct devlink_fmsg
*fmsg
)
4619 if (fmsg
->putting_binary
)
4622 return devlink_fmsg_nest_end(fmsg
);
4624 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end
);
4626 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg
*fmsg
,
4631 if (fmsg
->putting_binary
)
4634 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4638 err
= devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_ARR_NEST_START
);
4644 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start
);
4646 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg
*fmsg
)
4650 if (fmsg
->putting_binary
)
4653 err
= devlink_fmsg_nest_end(fmsg
);
4657 err
= devlink_fmsg_nest_end(fmsg
);
4663 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end
);
4665 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg
*fmsg
,
4670 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, name
);
4674 fmsg
->putting_binary
= true;
4677 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start
);
4679 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg
*fmsg
)
4681 if (!fmsg
->putting_binary
)
4684 fmsg
->putting_binary
= false;
4685 return devlink_fmsg_arr_pair_nest_end(fmsg
);
4687 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end
);
4689 static int devlink_fmsg_put_value(struct devlink_fmsg
*fmsg
,
4690 const void *value
, u16 value_len
,
4693 struct devlink_fmsg_item
*item
;
4695 if (value_len
> DEVLINK_FMSG_MAX_SIZE
)
4698 item
= kzalloc(sizeof(*item
) + value_len
, GFP_KERNEL
);
4702 item
->nla_type
= value_nla_type
;
4703 item
->len
= value_len
;
4704 item
->attrtype
= DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
;
4705 memcpy(&item
->value
, value
, item
->len
);
4706 list_add_tail(&item
->list
, &fmsg
->item_list
);
4711 int devlink_fmsg_bool_put(struct devlink_fmsg
*fmsg
, bool value
)
4713 if (fmsg
->putting_binary
)
4716 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_FLAG
);
4718 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put
);
4720 int devlink_fmsg_u8_put(struct devlink_fmsg
*fmsg
, u8 value
)
4722 if (fmsg
->putting_binary
)
4725 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U8
);
4727 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put
);
4729 int devlink_fmsg_u32_put(struct devlink_fmsg
*fmsg
, u32 value
)
4731 if (fmsg
->putting_binary
)
4734 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U32
);
4736 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put
);
4738 int devlink_fmsg_u64_put(struct devlink_fmsg
*fmsg
, u64 value
)
4740 if (fmsg
->putting_binary
)
4743 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U64
);
4745 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put
);
4747 int devlink_fmsg_string_put(struct devlink_fmsg
*fmsg
, const char *value
)
4749 if (fmsg
->putting_binary
)
4752 return devlink_fmsg_put_value(fmsg
, value
, strlen(value
) + 1,
4755 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put
);
4757 int devlink_fmsg_binary_put(struct devlink_fmsg
*fmsg
, const void *value
,
4760 if (!fmsg
->putting_binary
)
4763 return devlink_fmsg_put_value(fmsg
, value
, value_len
, NLA_BINARY
);
4765 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put
);
4767 int devlink_fmsg_bool_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4772 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4776 err
= devlink_fmsg_bool_put(fmsg
, value
);
4780 err
= devlink_fmsg_pair_nest_end(fmsg
);
4786 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put
);
4788 int devlink_fmsg_u8_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4793 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4797 err
= devlink_fmsg_u8_put(fmsg
, value
);
4801 err
= devlink_fmsg_pair_nest_end(fmsg
);
4807 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put
);
4809 int devlink_fmsg_u32_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4814 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4818 err
= devlink_fmsg_u32_put(fmsg
, value
);
4822 err
= devlink_fmsg_pair_nest_end(fmsg
);
4828 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put
);
4830 int devlink_fmsg_u64_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4835 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4839 err
= devlink_fmsg_u64_put(fmsg
, value
);
4843 err
= devlink_fmsg_pair_nest_end(fmsg
);
4849 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put
);
4851 int devlink_fmsg_string_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4856 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
4860 err
= devlink_fmsg_string_put(fmsg
, value
);
4864 err
= devlink_fmsg_pair_nest_end(fmsg
);
4870 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put
);
4872 int devlink_fmsg_binary_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
4873 const void *value
, u32 value_len
)
4880 err
= devlink_fmsg_binary_pair_nest_start(fmsg
, name
);
4884 for (offset
= 0; offset
< value_len
; offset
+= data_size
) {
4885 data_size
= value_len
- offset
;
4886 if (data_size
> DEVLINK_FMSG_MAX_SIZE
)
4887 data_size
= DEVLINK_FMSG_MAX_SIZE
;
4888 err
= devlink_fmsg_binary_put(fmsg
, value
+ offset
, data_size
);
4891 /* Exit from loop with a break (instead of
4892 * return) to make sure putting_binary is turned off in
4893 * devlink_fmsg_binary_pair_nest_end
4897 end_err
= devlink_fmsg_binary_pair_nest_end(fmsg
);
4903 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put
);
4906 devlink_fmsg_item_fill_type(struct devlink_fmsg_item
*msg
, struct sk_buff
*skb
)
4908 switch (msg
->nla_type
) {
4913 case NLA_NUL_STRING
:
4915 return nla_put_u8(skb
, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
,
4923 devlink_fmsg_item_fill_data(struct devlink_fmsg_item
*msg
, struct sk_buff
*skb
)
4925 int attrtype
= DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
;
4928 switch (msg
->nla_type
) {
4930 /* Always provide flag data, regardless of its value */
4931 tmp
= *(bool *) msg
->value
;
4933 return nla_put_u8(skb
, attrtype
, tmp
);
4935 return nla_put_u8(skb
, attrtype
, *(u8
*) msg
->value
);
4937 return nla_put_u32(skb
, attrtype
, *(u32
*) msg
->value
);
4939 return nla_put_u64_64bit(skb
, attrtype
, *(u64
*) msg
->value
,
4941 case NLA_NUL_STRING
:
4942 return nla_put_string(skb
, attrtype
, (char *) &msg
->value
);
4944 return nla_put(skb
, attrtype
, msg
->len
, (void *) &msg
->value
);
4951 devlink_fmsg_prepare_skb(struct devlink_fmsg
*fmsg
, struct sk_buff
*skb
,
4954 struct devlink_fmsg_item
*item
;
4955 struct nlattr
*fmsg_nlattr
;
4959 fmsg_nlattr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_FMSG
);
4963 list_for_each_entry(item
, &fmsg
->item_list
, list
) {
4969 switch (item
->attrtype
) {
4970 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
4971 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
4972 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
4973 case DEVLINK_ATTR_FMSG_NEST_END
:
4974 err
= nla_put_flag(skb
, item
->attrtype
);
4976 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
4977 err
= devlink_fmsg_item_fill_type(item
, skb
);
4980 err
= devlink_fmsg_item_fill_data(item
, skb
);
4982 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
4983 err
= nla_put_string(skb
, item
->attrtype
,
4984 (char *) &item
->value
);
4996 nla_nest_end(skb
, fmsg_nlattr
);
5000 static int devlink_fmsg_snd(struct devlink_fmsg
*fmsg
,
5001 struct genl_info
*info
,
5002 enum devlink_command cmd
, int flags
)
5004 struct nlmsghdr
*nlh
;
5005 struct sk_buff
*skb
;
5012 int tmp_index
= index
;
5014 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5018 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
5019 &devlink_nl_family
, flags
| NLM_F_MULTI
, cmd
);
5022 goto nla_put_failure
;
5025 err
= devlink_fmsg_prepare_skb(fmsg
, skb
, &index
);
5028 else if (err
!= -EMSGSIZE
|| tmp_index
== index
)
5029 goto nla_put_failure
;
5031 genlmsg_end(skb
, hdr
);
5032 err
= genlmsg_reply(skb
, info
);
5037 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5040 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
5041 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
5044 goto nla_put_failure
;
5047 return genlmsg_reply(skb
, info
);
5054 static int devlink_fmsg_dumpit(struct devlink_fmsg
*fmsg
, struct sk_buff
*skb
,
5055 struct netlink_callback
*cb
,
5056 enum devlink_command cmd
)
5058 int index
= cb
->args
[0];
5059 int tmp_index
= index
;
5063 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
5064 &devlink_nl_family
, NLM_F_ACK
| NLM_F_MULTI
, cmd
);
5067 goto nla_put_failure
;
5070 err
= devlink_fmsg_prepare_skb(fmsg
, skb
, &index
);
5071 if ((err
&& err
!= -EMSGSIZE
) || tmp_index
== index
)
5072 goto nla_put_failure
;
5074 cb
->args
[0] = index
;
5075 genlmsg_end(skb
, hdr
);
5079 genlmsg_cancel(skb
, hdr
);
5083 struct devlink_health_reporter
{
5084 struct list_head list
;
5086 const struct devlink_health_reporter_ops
*ops
;
5087 struct devlink
*devlink
;
5088 struct devlink_fmsg
*dump_fmsg
;
5089 struct mutex dump_lock
; /* lock parallel read/write from dump buffers */
5090 u64 graceful_period
;
5098 u64 last_recovery_ts
;
5099 refcount_t refcount
;
5103 devlink_health_reporter_priv(struct devlink_health_reporter
*reporter
)
5105 return reporter
->priv
;
5107 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv
);
5109 static struct devlink_health_reporter
*
5110 devlink_health_reporter_find_by_name(struct devlink
*devlink
,
5111 const char *reporter_name
)
5113 struct devlink_health_reporter
*reporter
;
5115 lockdep_assert_held(&devlink
->reporters_lock
);
5116 list_for_each_entry(reporter
, &devlink
->reporter_list
, list
)
5117 if (!strcmp(reporter
->ops
->name
, reporter_name
))
5123 * devlink_health_reporter_create - create devlink health reporter
5127 * @graceful_period: to avoid recovery loops, in msecs
5130 struct devlink_health_reporter
*
5131 devlink_health_reporter_create(struct devlink
*devlink
,
5132 const struct devlink_health_reporter_ops
*ops
,
5133 u64 graceful_period
, void *priv
)
5135 struct devlink_health_reporter
*reporter
;
5137 mutex_lock(&devlink
->reporters_lock
);
5138 if (devlink_health_reporter_find_by_name(devlink
, ops
->name
)) {
5139 reporter
= ERR_PTR(-EEXIST
);
5143 if (WARN_ON(graceful_period
&& !ops
->recover
)) {
5144 reporter
= ERR_PTR(-EINVAL
);
5148 reporter
= kzalloc(sizeof(*reporter
), GFP_KERNEL
);
5150 reporter
= ERR_PTR(-ENOMEM
);
5154 reporter
->priv
= priv
;
5155 reporter
->ops
= ops
;
5156 reporter
->devlink
= devlink
;
5157 reporter
->graceful_period
= graceful_period
;
5158 reporter
->auto_recover
= !!ops
->recover
;
5159 reporter
->auto_dump
= !!ops
->dump
;
5160 mutex_init(&reporter
->dump_lock
);
5161 refcount_set(&reporter
->refcount
, 1);
5162 list_add_tail(&reporter
->list
, &devlink
->reporter_list
);
5164 mutex_unlock(&devlink
->reporters_lock
);
5167 EXPORT_SYMBOL_GPL(devlink_health_reporter_create
);
5170 * devlink_health_reporter_destroy - destroy devlink health reporter
5172 * @reporter: devlink health reporter to destroy
5175 devlink_health_reporter_destroy(struct devlink_health_reporter
*reporter
)
5177 mutex_lock(&reporter
->devlink
->reporters_lock
);
5178 list_del(&reporter
->list
);
5179 mutex_unlock(&reporter
->devlink
->reporters_lock
);
5180 while (refcount_read(&reporter
->refcount
) > 1)
5182 mutex_destroy(&reporter
->dump_lock
);
5183 if (reporter
->dump_fmsg
)
5184 devlink_fmsg_free(reporter
->dump_fmsg
);
5187 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy
);
5190 devlink_nl_health_reporter_fill(struct sk_buff
*msg
,
5191 struct devlink
*devlink
,
5192 struct devlink_health_reporter
*reporter
,
5193 enum devlink_command cmd
, u32 portid
,
5196 struct nlattr
*reporter_attr
;
5199 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
5203 if (devlink_nl_put_handle(msg
, devlink
))
5204 goto genlmsg_cancel
;
5206 reporter_attr
= nla_nest_start_noflag(msg
,
5207 DEVLINK_ATTR_HEALTH_REPORTER
);
5209 goto genlmsg_cancel
;
5210 if (nla_put_string(msg
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
5211 reporter
->ops
->name
))
5212 goto reporter_nest_cancel
;
5213 if (nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_STATE
,
5214 reporter
->health_state
))
5215 goto reporter_nest_cancel
;
5216 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
,
5217 reporter
->error_count
, DEVLINK_ATTR_PAD
))
5218 goto reporter_nest_cancel
;
5219 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
,
5220 reporter
->recovery_count
, DEVLINK_ATTR_PAD
))
5221 goto reporter_nest_cancel
;
5222 if (reporter
->ops
->recover
&&
5223 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
5224 reporter
->graceful_period
,
5226 goto reporter_nest_cancel
;
5227 if (reporter
->ops
->recover
&&
5228 nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
5229 reporter
->auto_recover
))
5230 goto reporter_nest_cancel
;
5231 if (reporter
->dump_fmsg
&&
5232 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
,
5233 jiffies_to_msecs(reporter
->dump_ts
),
5235 goto reporter_nest_cancel
;
5236 if (reporter
->dump_fmsg
&&
5237 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
,
5238 reporter
->dump_real_ts
, DEVLINK_ATTR_PAD
))
5239 goto reporter_nest_cancel
;
5240 if (reporter
->ops
->dump
&&
5241 nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
5242 reporter
->auto_dump
))
5243 goto reporter_nest_cancel
;
5245 nla_nest_end(msg
, reporter_attr
);
5246 genlmsg_end(msg
, hdr
);
5249 reporter_nest_cancel
:
5250 nla_nest_end(msg
, reporter_attr
);
5252 genlmsg_cancel(msg
, hdr
);
5256 static void devlink_recover_notify(struct devlink_health_reporter
*reporter
,
5257 enum devlink_command cmd
)
5259 struct sk_buff
*msg
;
5262 WARN_ON(cmd
!= DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5264 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5268 err
= devlink_nl_health_reporter_fill(msg
, reporter
->devlink
,
5269 reporter
, cmd
, 0, 0, 0);
5275 genlmsg_multicast_netns(&devlink_nl_family
,
5276 devlink_net(reporter
->devlink
),
5277 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
5281 devlink_health_reporter_recovery_done(struct devlink_health_reporter
*reporter
)
5283 reporter
->recovery_count
++;
5284 reporter
->last_recovery_ts
= jiffies
;
5286 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done
);
5289 devlink_health_reporter_recover(struct devlink_health_reporter
*reporter
,
5290 void *priv_ctx
, struct netlink_ext_ack
*extack
)
5294 if (reporter
->health_state
== DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
)
5297 if (!reporter
->ops
->recover
)
5300 err
= reporter
->ops
->recover(reporter
, priv_ctx
, extack
);
5304 devlink_health_reporter_recovery_done(reporter
);
5305 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
;
5306 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5312 devlink_health_dump_clear(struct devlink_health_reporter
*reporter
)
5314 if (!reporter
->dump_fmsg
)
5316 devlink_fmsg_free(reporter
->dump_fmsg
);
5317 reporter
->dump_fmsg
= NULL
;
5320 static int devlink_health_do_dump(struct devlink_health_reporter
*reporter
,
5322 struct netlink_ext_ack
*extack
)
5326 if (!reporter
->ops
->dump
)
5329 if (reporter
->dump_fmsg
)
5332 reporter
->dump_fmsg
= devlink_fmsg_alloc();
5333 if (!reporter
->dump_fmsg
) {
5338 err
= devlink_fmsg_obj_nest_start(reporter
->dump_fmsg
);
5342 err
= reporter
->ops
->dump(reporter
, reporter
->dump_fmsg
,
5347 err
= devlink_fmsg_obj_nest_end(reporter
->dump_fmsg
);
5351 reporter
->dump_ts
= jiffies
;
5352 reporter
->dump_real_ts
= ktime_get_real_ns();
5357 devlink_health_dump_clear(reporter
);
5361 int devlink_health_report(struct devlink_health_reporter
*reporter
,
5362 const char *msg
, void *priv_ctx
)
5364 enum devlink_health_reporter_state prev_health_state
;
5365 struct devlink
*devlink
= reporter
->devlink
;
5367 /* write a log message of the current error */
5369 trace_devlink_health_report(devlink
, reporter
->ops
->name
, msg
);
5370 reporter
->error_count
++;
5371 prev_health_state
= reporter
->health_state
;
5372 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_ERROR
;
5373 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5375 /* abort if the previous error wasn't recovered */
5376 if (reporter
->auto_recover
&&
5377 (prev_health_state
!= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
||
5378 jiffies
- reporter
->last_recovery_ts
<
5379 msecs_to_jiffies(reporter
->graceful_period
))) {
5380 trace_devlink_health_recover_aborted(devlink
,
5381 reporter
->ops
->name
,
5382 reporter
->health_state
,
5384 reporter
->last_recovery_ts
);
5388 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_ERROR
;
5390 if (reporter
->auto_dump
) {
5391 mutex_lock(&reporter
->dump_lock
);
5392 /* store current dump of current error, for later analysis */
5393 devlink_health_do_dump(reporter
, priv_ctx
, NULL
);
5394 mutex_unlock(&reporter
->dump_lock
);
5397 if (reporter
->auto_recover
)
5398 return devlink_health_reporter_recover(reporter
,
5403 EXPORT_SYMBOL_GPL(devlink_health_report
);
5405 static struct devlink_health_reporter
*
5406 devlink_health_reporter_get_from_attrs(struct devlink
*devlink
,
5407 struct nlattr
**attrs
)
5409 struct devlink_health_reporter
*reporter
;
5410 char *reporter_name
;
5412 if (!attrs
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
])
5415 reporter_name
= nla_data(attrs
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]);
5416 mutex_lock(&devlink
->reporters_lock
);
5417 reporter
= devlink_health_reporter_find_by_name(devlink
, reporter_name
);
5419 refcount_inc(&reporter
->refcount
);
5420 mutex_unlock(&devlink
->reporters_lock
);
5424 static struct devlink_health_reporter
*
5425 devlink_health_reporter_get_from_info(struct devlink
*devlink
,
5426 struct genl_info
*info
)
5428 return devlink_health_reporter_get_from_attrs(devlink
, info
->attrs
);
5431 static struct devlink_health_reporter
*
5432 devlink_health_reporter_get_from_cb(struct netlink_callback
*cb
)
5434 const struct genl_dumpit_info
*info
= genl_dumpit_info(cb
);
5435 struct devlink_health_reporter
*reporter
;
5436 struct nlattr
**attrs
= info
->attrs
;
5437 struct devlink
*devlink
;
5439 mutex_lock(&devlink_mutex
);
5440 devlink
= devlink_get_from_attrs(sock_net(cb
->skb
->sk
), attrs
);
5441 if (IS_ERR(devlink
))
5444 reporter
= devlink_health_reporter_get_from_attrs(devlink
, attrs
);
5445 mutex_unlock(&devlink_mutex
);
5448 mutex_unlock(&devlink_mutex
);
5453 devlink_health_reporter_put(struct devlink_health_reporter
*reporter
)
5455 refcount_dec(&reporter
->refcount
);
5459 devlink_health_reporter_state_update(struct devlink_health_reporter
*reporter
,
5460 enum devlink_health_reporter_state state
)
5462 if (WARN_ON(state
!= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
&&
5463 state
!= DEVLINK_HEALTH_REPORTER_STATE_ERROR
))
5466 if (reporter
->health_state
== state
)
5469 reporter
->health_state
= state
;
5470 trace_devlink_health_reporter_state_update(reporter
->devlink
,
5471 reporter
->ops
->name
, state
);
5472 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
5474 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update
);
5476 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff
*skb
,
5477 struct genl_info
*info
)
5479 struct devlink
*devlink
= info
->user_ptr
[0];
5480 struct devlink_health_reporter
*reporter
;
5481 struct sk_buff
*msg
;
5484 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5488 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5494 err
= devlink_nl_health_reporter_fill(msg
, devlink
, reporter
,
5495 DEVLINK_CMD_HEALTH_REPORTER_GET
,
5496 info
->snd_portid
, info
->snd_seq
,
5503 err
= genlmsg_reply(msg
, info
);
5505 devlink_health_reporter_put(reporter
);
5510 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff
*msg
,
5511 struct netlink_callback
*cb
)
5513 struct devlink_health_reporter
*reporter
;
5514 struct devlink
*devlink
;
5515 int start
= cb
->args
[0];
5519 mutex_lock(&devlink_mutex
);
5520 list_for_each_entry(devlink
, &devlink_list
, list
) {
5521 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
5523 mutex_lock(&devlink
->reporters_lock
);
5524 list_for_each_entry(reporter
, &devlink
->reporter_list
,
5530 err
= devlink_nl_health_reporter_fill(msg
, devlink
,
5532 DEVLINK_CMD_HEALTH_REPORTER_GET
,
5533 NETLINK_CB(cb
->skb
).portid
,
5537 mutex_unlock(&devlink
->reporters_lock
);
5542 mutex_unlock(&devlink
->reporters_lock
);
5545 mutex_unlock(&devlink_mutex
);
5552 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff
*skb
,
5553 struct genl_info
*info
)
5555 struct devlink
*devlink
= info
->user_ptr
[0];
5556 struct devlink_health_reporter
*reporter
;
5559 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5563 if (!reporter
->ops
->recover
&&
5564 (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] ||
5565 info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])) {
5569 if (!reporter
->ops
->dump
&&
5570 info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]) {
5575 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
5576 reporter
->graceful_period
=
5577 nla_get_u64(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]);
5579 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
5580 reporter
->auto_recover
=
5581 nla_get_u8(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]);
5583 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
5584 reporter
->auto_dump
=
5585 nla_get_u8(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]);
5587 devlink_health_reporter_put(reporter
);
5590 devlink_health_reporter_put(reporter
);
5594 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff
*skb
,
5595 struct genl_info
*info
)
5597 struct devlink
*devlink
= info
->user_ptr
[0];
5598 struct devlink_health_reporter
*reporter
;
5601 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5605 err
= devlink_health_reporter_recover(reporter
, NULL
, info
->extack
);
5607 devlink_health_reporter_put(reporter
);
5611 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff
*skb
,
5612 struct genl_info
*info
)
5614 struct devlink
*devlink
= info
->user_ptr
[0];
5615 struct devlink_health_reporter
*reporter
;
5616 struct devlink_fmsg
*fmsg
;
5619 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5623 if (!reporter
->ops
->diagnose
) {
5624 devlink_health_reporter_put(reporter
);
5628 fmsg
= devlink_fmsg_alloc();
5630 devlink_health_reporter_put(reporter
);
5634 err
= devlink_fmsg_obj_nest_start(fmsg
);
5638 err
= reporter
->ops
->diagnose(reporter
, fmsg
, info
->extack
);
5642 err
= devlink_fmsg_obj_nest_end(fmsg
);
5646 err
= devlink_fmsg_snd(fmsg
, info
,
5647 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
, 0);
5650 devlink_fmsg_free(fmsg
);
5651 devlink_health_reporter_put(reporter
);
5656 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff
*skb
,
5657 struct netlink_callback
*cb
)
5659 struct devlink_health_reporter
*reporter
;
5660 u64 start
= cb
->args
[0];
5663 reporter
= devlink_health_reporter_get_from_cb(cb
);
5667 if (!reporter
->ops
->dump
) {
5671 mutex_lock(&reporter
->dump_lock
);
5673 err
= devlink_health_do_dump(reporter
, NULL
, cb
->extack
);
5676 cb
->args
[1] = reporter
->dump_ts
;
5678 if (!reporter
->dump_fmsg
|| cb
->args
[1] != reporter
->dump_ts
) {
5679 NL_SET_ERR_MSG_MOD(cb
->extack
, "Dump trampled, please retry");
5684 err
= devlink_fmsg_dumpit(reporter
->dump_fmsg
, skb
, cb
,
5685 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
5687 mutex_unlock(&reporter
->dump_lock
);
5689 devlink_health_reporter_put(reporter
);
5694 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff
*skb
,
5695 struct genl_info
*info
)
5697 struct devlink
*devlink
= info
->user_ptr
[0];
5698 struct devlink_health_reporter
*reporter
;
5700 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
5704 if (!reporter
->ops
->dump
) {
5705 devlink_health_reporter_put(reporter
);
5709 mutex_lock(&reporter
->dump_lock
);
5710 devlink_health_dump_clear(reporter
);
5711 mutex_unlock(&reporter
->dump_lock
);
5712 devlink_health_reporter_put(reporter
);
5716 struct devlink_stats
{
5719 struct u64_stats_sync syncp
;
5723 * struct devlink_trap_policer_item - Packet trap policer attributes.
5724 * @policer: Immutable packet trap policer attributes.
5725 * @rate: Rate in packets / sec.
5726 * @burst: Burst size in packets.
5727 * @list: trap_policer_list member.
5729 * Describes packet trap policer attributes. Created by devlink during trap
5730 * policer registration.
5732 struct devlink_trap_policer_item
{
5733 const struct devlink_trap_policer
*policer
;
5736 struct list_head list
;
5740 * struct devlink_trap_group_item - Packet trap group attributes.
5741 * @group: Immutable packet trap group attributes.
5742 * @policer_item: Associated policer item. Can be NULL.
5743 * @list: trap_group_list member.
5744 * @stats: Trap group statistics.
5746 * Describes packet trap group attributes. Created by devlink during trap
5747 * group registration.
5749 struct devlink_trap_group_item
{
5750 const struct devlink_trap_group
*group
;
5751 struct devlink_trap_policer_item
*policer_item
;
5752 struct list_head list
;
5753 struct devlink_stats __percpu
*stats
;
5757 * struct devlink_trap_item - Packet trap attributes.
5758 * @trap: Immutable packet trap attributes.
5759 * @group_item: Associated group item.
5760 * @list: trap_list member.
5761 * @action: Trap action.
5762 * @stats: Trap statistics.
5763 * @priv: Driver private information.
5765 * Describes both mutable and immutable packet trap attributes. Created by
5766 * devlink during trap registration and used for all trap related operations.
5768 struct devlink_trap_item
{
5769 const struct devlink_trap
*trap
;
5770 struct devlink_trap_group_item
*group_item
;
5771 struct list_head list
;
5772 enum devlink_trap_action action
;
5773 struct devlink_stats __percpu
*stats
;
5777 static struct devlink_trap_policer_item
*
5778 devlink_trap_policer_item_lookup(struct devlink
*devlink
, u32 id
)
5780 struct devlink_trap_policer_item
*policer_item
;
5782 list_for_each_entry(policer_item
, &devlink
->trap_policer_list
, list
) {
5783 if (policer_item
->policer
->id
== id
)
5784 return policer_item
;
5790 static struct devlink_trap_item
*
5791 devlink_trap_item_lookup(struct devlink
*devlink
, const char *name
)
5793 struct devlink_trap_item
*trap_item
;
5795 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
5796 if (!strcmp(trap_item
->trap
->name
, name
))
5803 static struct devlink_trap_item
*
5804 devlink_trap_item_get_from_info(struct devlink
*devlink
,
5805 struct genl_info
*info
)
5807 struct nlattr
*attr
;
5809 if (!info
->attrs
[DEVLINK_ATTR_TRAP_NAME
])
5811 attr
= info
->attrs
[DEVLINK_ATTR_TRAP_NAME
];
5813 return devlink_trap_item_lookup(devlink
, nla_data(attr
));
5817 devlink_trap_action_get_from_info(struct genl_info
*info
,
5818 enum devlink_trap_action
*p_trap_action
)
5822 val
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
]);
5824 case DEVLINK_TRAP_ACTION_DROP
: /* fall-through */
5825 case DEVLINK_TRAP_ACTION_TRAP
:
5826 *p_trap_action
= val
;
5835 static int devlink_trap_metadata_put(struct sk_buff
*msg
,
5836 const struct devlink_trap
*trap
)
5838 struct nlattr
*attr
;
5840 attr
= nla_nest_start(msg
, DEVLINK_ATTR_TRAP_METADATA
);
5844 if ((trap
->metadata_cap
& DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
) &&
5845 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
))
5846 goto nla_put_failure
;
5847 if ((trap
->metadata_cap
& DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE
) &&
5848 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
))
5849 goto nla_put_failure
;
5851 nla_nest_end(msg
, attr
);
5856 nla_nest_cancel(msg
, attr
);
5860 static void devlink_trap_stats_read(struct devlink_stats __percpu
*trap_stats
,
5861 struct devlink_stats
*stats
)
5865 memset(stats
, 0, sizeof(*stats
));
5866 for_each_possible_cpu(i
) {
5867 struct devlink_stats
*cpu_stats
;
5868 u64 rx_packets
, rx_bytes
;
5871 cpu_stats
= per_cpu_ptr(trap_stats
, i
);
5873 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
5874 rx_packets
= cpu_stats
->rx_packets
;
5875 rx_bytes
= cpu_stats
->rx_bytes
;
5876 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
5878 stats
->rx_packets
+= rx_packets
;
5879 stats
->rx_bytes
+= rx_bytes
;
5883 static int devlink_trap_stats_put(struct sk_buff
*msg
,
5884 struct devlink_stats __percpu
*trap_stats
)
5886 struct devlink_stats stats
;
5887 struct nlattr
*attr
;
5889 devlink_trap_stats_read(trap_stats
, &stats
);
5891 attr
= nla_nest_start(msg
, DEVLINK_ATTR_STATS
);
5895 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_PACKETS
,
5896 stats
.rx_packets
, DEVLINK_ATTR_PAD
))
5897 goto nla_put_failure
;
5899 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_BYTES
,
5900 stats
.rx_bytes
, DEVLINK_ATTR_PAD
))
5901 goto nla_put_failure
;
5903 nla_nest_end(msg
, attr
);
5908 nla_nest_cancel(msg
, attr
);
5912 static int devlink_nl_trap_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
5913 const struct devlink_trap_item
*trap_item
,
5914 enum devlink_command cmd
, u32 portid
, u32 seq
,
5917 struct devlink_trap_group_item
*group_item
= trap_item
->group_item
;
5921 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
5925 if (devlink_nl_put_handle(msg
, devlink
))
5926 goto nla_put_failure
;
5928 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
5929 group_item
->group
->name
))
5930 goto nla_put_failure
;
5932 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_NAME
, trap_item
->trap
->name
))
5933 goto nla_put_failure
;
5935 if (nla_put_u8(msg
, DEVLINK_ATTR_TRAP_TYPE
, trap_item
->trap
->type
))
5936 goto nla_put_failure
;
5938 if (trap_item
->trap
->generic
&&
5939 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_GENERIC
))
5940 goto nla_put_failure
;
5942 if (nla_put_u8(msg
, DEVLINK_ATTR_TRAP_ACTION
, trap_item
->action
))
5943 goto nla_put_failure
;
5945 err
= devlink_trap_metadata_put(msg
, trap_item
->trap
);
5947 goto nla_put_failure
;
5949 err
= devlink_trap_stats_put(msg
, trap_item
->stats
);
5951 goto nla_put_failure
;
5953 genlmsg_end(msg
, hdr
);
5958 genlmsg_cancel(msg
, hdr
);
5962 static int devlink_nl_cmd_trap_get_doit(struct sk_buff
*skb
,
5963 struct genl_info
*info
)
5965 struct netlink_ext_ack
*extack
= info
->extack
;
5966 struct devlink
*devlink
= info
->user_ptr
[0];
5967 struct devlink_trap_item
*trap_item
;
5968 struct sk_buff
*msg
;
5971 if (list_empty(&devlink
->trap_list
))
5974 trap_item
= devlink_trap_item_get_from_info(devlink
, info
);
5976 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap");
5980 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5984 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
,
5985 DEVLINK_CMD_TRAP_NEW
, info
->snd_portid
,
5990 return genlmsg_reply(msg
, info
);
5997 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff
*msg
,
5998 struct netlink_callback
*cb
)
6000 struct devlink_trap_item
*trap_item
;
6001 struct devlink
*devlink
;
6002 int start
= cb
->args
[0];
6006 mutex_lock(&devlink_mutex
);
6007 list_for_each_entry(devlink
, &devlink_list
, list
) {
6008 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6010 mutex_lock(&devlink
->lock
);
6011 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
6016 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
,
6017 DEVLINK_CMD_TRAP_NEW
,
6018 NETLINK_CB(cb
->skb
).portid
,
6022 mutex_unlock(&devlink
->lock
);
6027 mutex_unlock(&devlink
->lock
);
6030 mutex_unlock(&devlink_mutex
);
6036 static int __devlink_trap_action_set(struct devlink
*devlink
,
6037 struct devlink_trap_item
*trap_item
,
6038 enum devlink_trap_action trap_action
,
6039 struct netlink_ext_ack
*extack
)
6043 if (trap_item
->action
!= trap_action
&&
6044 trap_item
->trap
->type
!= DEVLINK_TRAP_TYPE_DROP
) {
6045 NL_SET_ERR_MSG_MOD(extack
, "Cannot change action of non-drop traps. Skipping");
6049 err
= devlink
->ops
->trap_action_set(devlink
, trap_item
->trap
,
6054 trap_item
->action
= trap_action
;
6059 static int devlink_trap_action_set(struct devlink
*devlink
,
6060 struct devlink_trap_item
*trap_item
,
6061 struct genl_info
*info
)
6063 enum devlink_trap_action trap_action
;
6066 if (!info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
])
6069 err
= devlink_trap_action_get_from_info(info
, &trap_action
);
6071 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid trap action");
6075 return __devlink_trap_action_set(devlink
, trap_item
, trap_action
,
6079 static int devlink_nl_cmd_trap_set_doit(struct sk_buff
*skb
,
6080 struct genl_info
*info
)
6082 struct netlink_ext_ack
*extack
= info
->extack
;
6083 struct devlink
*devlink
= info
->user_ptr
[0];
6084 struct devlink_trap_item
*trap_item
;
6087 if (list_empty(&devlink
->trap_list
))
6090 trap_item
= devlink_trap_item_get_from_info(devlink
, info
);
6092 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap");
6096 err
= devlink_trap_action_set(devlink
, trap_item
, info
);
6103 static struct devlink_trap_group_item
*
6104 devlink_trap_group_item_lookup(struct devlink
*devlink
, const char *name
)
6106 struct devlink_trap_group_item
*group_item
;
6108 list_for_each_entry(group_item
, &devlink
->trap_group_list
, list
) {
6109 if (!strcmp(group_item
->group
->name
, name
))
6116 static struct devlink_trap_group_item
*
6117 devlink_trap_group_item_lookup_by_id(struct devlink
*devlink
, u16 id
)
6119 struct devlink_trap_group_item
*group_item
;
6121 list_for_each_entry(group_item
, &devlink
->trap_group_list
, list
) {
6122 if (group_item
->group
->id
== id
)
6129 static struct devlink_trap_group_item
*
6130 devlink_trap_group_item_get_from_info(struct devlink
*devlink
,
6131 struct genl_info
*info
)
6135 if (!info
->attrs
[DEVLINK_ATTR_TRAP_GROUP_NAME
])
6137 name
= nla_data(info
->attrs
[DEVLINK_ATTR_TRAP_GROUP_NAME
]);
6139 return devlink_trap_group_item_lookup(devlink
, name
);
6143 devlink_nl_trap_group_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
6144 const struct devlink_trap_group_item
*group_item
,
6145 enum devlink_command cmd
, u32 portid
, u32 seq
,
6151 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
6155 if (devlink_nl_put_handle(msg
, devlink
))
6156 goto nla_put_failure
;
6158 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
6159 group_item
->group
->name
))
6160 goto nla_put_failure
;
6162 if (group_item
->group
->generic
&&
6163 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_GENERIC
))
6164 goto nla_put_failure
;
6166 if (group_item
->policer_item
&&
6167 nla_put_u32(msg
, DEVLINK_ATTR_TRAP_POLICER_ID
,
6168 group_item
->policer_item
->policer
->id
))
6169 goto nla_put_failure
;
6171 err
= devlink_trap_stats_put(msg
, group_item
->stats
);
6173 goto nla_put_failure
;
6175 genlmsg_end(msg
, hdr
);
6180 genlmsg_cancel(msg
, hdr
);
6184 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff
*skb
,
6185 struct genl_info
*info
)
6187 struct netlink_ext_ack
*extack
= info
->extack
;
6188 struct devlink
*devlink
= info
->user_ptr
[0];
6189 struct devlink_trap_group_item
*group_item
;
6190 struct sk_buff
*msg
;
6193 if (list_empty(&devlink
->trap_group_list
))
6196 group_item
= devlink_trap_group_item_get_from_info(devlink
, info
);
6198 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap group");
6202 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6206 err
= devlink_nl_trap_group_fill(msg
, devlink
, group_item
,
6207 DEVLINK_CMD_TRAP_GROUP_NEW
,
6208 info
->snd_portid
, info
->snd_seq
, 0);
6210 goto err_trap_group_fill
;
6212 return genlmsg_reply(msg
, info
);
6214 err_trap_group_fill
:
6219 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff
*msg
,
6220 struct netlink_callback
*cb
)
6222 enum devlink_command cmd
= DEVLINK_CMD_TRAP_GROUP_NEW
;
6223 struct devlink_trap_group_item
*group_item
;
6224 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
6225 struct devlink
*devlink
;
6226 int start
= cb
->args
[0];
6230 mutex_lock(&devlink_mutex
);
6231 list_for_each_entry(devlink
, &devlink_list
, list
) {
6232 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6234 mutex_lock(&devlink
->lock
);
6235 list_for_each_entry(group_item
, &devlink
->trap_group_list
,
6241 err
= devlink_nl_trap_group_fill(msg
, devlink
,
6247 mutex_unlock(&devlink
->lock
);
6252 mutex_unlock(&devlink
->lock
);
6255 mutex_unlock(&devlink_mutex
);
6262 __devlink_trap_group_action_set(struct devlink
*devlink
,
6263 struct devlink_trap_group_item
*group_item
,
6264 enum devlink_trap_action trap_action
,
6265 struct netlink_ext_ack
*extack
)
6267 const char *group_name
= group_item
->group
->name
;
6268 struct devlink_trap_item
*trap_item
;
6271 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
6272 if (strcmp(trap_item
->group_item
->group
->name
, group_name
))
6274 err
= __devlink_trap_action_set(devlink
, trap_item
,
6275 trap_action
, extack
);
6284 devlink_trap_group_action_set(struct devlink
*devlink
,
6285 struct devlink_trap_group_item
*group_item
,
6286 struct genl_info
*info
, bool *p_modified
)
6288 enum devlink_trap_action trap_action
;
6291 if (!info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
])
6294 err
= devlink_trap_action_get_from_info(info
, &trap_action
);
6296 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid trap action");
6300 err
= __devlink_trap_group_action_set(devlink
, group_item
, trap_action
,
6310 static int devlink_trap_group_set(struct devlink
*devlink
,
6311 struct devlink_trap_group_item
*group_item
,
6312 struct genl_info
*info
)
6314 struct devlink_trap_policer_item
*policer_item
;
6315 struct netlink_ext_ack
*extack
= info
->extack
;
6316 const struct devlink_trap_policer
*policer
;
6317 struct nlattr
**attrs
= info
->attrs
;
6320 if (!attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
])
6323 if (!devlink
->ops
->trap_group_set
)
6326 policer_item
= group_item
->policer_item
;
6327 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]) {
6330 policer_id
= nla_get_u32(attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]);
6331 policer_item
= devlink_trap_policer_item_lookup(devlink
,
6333 if (policer_id
&& !policer_item
) {
6334 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
6338 policer
= policer_item
? policer_item
->policer
: NULL
;
6340 err
= devlink
->ops
->trap_group_set(devlink
, group_item
->group
, policer
);
6344 group_item
->policer_item
= policer_item
;
6349 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff
*skb
,
6350 struct genl_info
*info
)
6352 struct netlink_ext_ack
*extack
= info
->extack
;
6353 struct devlink
*devlink
= info
->user_ptr
[0];
6354 struct devlink_trap_group_item
*group_item
;
6355 bool modified
= false;
6358 if (list_empty(&devlink
->trap_group_list
))
6361 group_item
= devlink_trap_group_item_get_from_info(devlink
, info
);
6363 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap group");
6367 err
= devlink_trap_group_action_set(devlink
, group_item
, info
,
6372 err
= devlink_trap_group_set(devlink
, group_item
, info
);
6374 goto err_trap_group_set
;
6380 NL_SET_ERR_MSG_MOD(extack
, "Trap group set failed, but some changes were committed already");
6384 static struct devlink_trap_policer_item
*
6385 devlink_trap_policer_item_get_from_info(struct devlink
*devlink
,
6386 struct genl_info
*info
)
6390 if (!info
->attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
])
6392 id
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]);
6394 return devlink_trap_policer_item_lookup(devlink
, id
);
6398 devlink_trap_policer_stats_put(struct sk_buff
*msg
, struct devlink
*devlink
,
6399 const struct devlink_trap_policer
*policer
)
6401 struct nlattr
*attr
;
6405 if (!devlink
->ops
->trap_policer_counter_get
)
6408 err
= devlink
->ops
->trap_policer_counter_get(devlink
, policer
, &drops
);
6412 attr
= nla_nest_start(msg
, DEVLINK_ATTR_STATS
);
6416 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_DROPPED
, drops
,
6418 goto nla_put_failure
;
6420 nla_nest_end(msg
, attr
);
6425 nla_nest_cancel(msg
, attr
);
6430 devlink_nl_trap_policer_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
6431 const struct devlink_trap_policer_item
*policer_item
,
6432 enum devlink_command cmd
, u32 portid
, u32 seq
,
6438 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
6442 if (devlink_nl_put_handle(msg
, devlink
))
6443 goto nla_put_failure
;
6445 if (nla_put_u32(msg
, DEVLINK_ATTR_TRAP_POLICER_ID
,
6446 policer_item
->policer
->id
))
6447 goto nla_put_failure
;
6449 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
6450 policer_item
->rate
, DEVLINK_ATTR_PAD
))
6451 goto nla_put_failure
;
6453 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
6454 policer_item
->burst
, DEVLINK_ATTR_PAD
))
6455 goto nla_put_failure
;
6457 err
= devlink_trap_policer_stats_put(msg
, devlink
,
6458 policer_item
->policer
);
6460 goto nla_put_failure
;
6462 genlmsg_end(msg
, hdr
);
6467 genlmsg_cancel(msg
, hdr
);
6471 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff
*skb
,
6472 struct genl_info
*info
)
6474 struct devlink_trap_policer_item
*policer_item
;
6475 struct netlink_ext_ack
*extack
= info
->extack
;
6476 struct devlink
*devlink
= info
->user_ptr
[0];
6477 struct sk_buff
*msg
;
6480 if (list_empty(&devlink
->trap_policer_list
))
6483 policer_item
= devlink_trap_policer_item_get_from_info(devlink
, info
);
6484 if (!policer_item
) {
6485 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
6489 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6493 err
= devlink_nl_trap_policer_fill(msg
, devlink
, policer_item
,
6494 DEVLINK_CMD_TRAP_POLICER_NEW
,
6495 info
->snd_portid
, info
->snd_seq
, 0);
6497 goto err_trap_policer_fill
;
6499 return genlmsg_reply(msg
, info
);
6501 err_trap_policer_fill
:
6506 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff
*msg
,
6507 struct netlink_callback
*cb
)
6509 enum devlink_command cmd
= DEVLINK_CMD_TRAP_POLICER_NEW
;
6510 struct devlink_trap_policer_item
*policer_item
;
6511 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
6512 struct devlink
*devlink
;
6513 int start
= cb
->args
[0];
6517 mutex_lock(&devlink_mutex
);
6518 list_for_each_entry(devlink
, &devlink_list
, list
) {
6519 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6521 mutex_lock(&devlink
->lock
);
6522 list_for_each_entry(policer_item
, &devlink
->trap_policer_list
,
6528 err
= devlink_nl_trap_policer_fill(msg
, devlink
,
6534 mutex_unlock(&devlink
->lock
);
6539 mutex_unlock(&devlink
->lock
);
6542 mutex_unlock(&devlink_mutex
);
6549 devlink_trap_policer_set(struct devlink
*devlink
,
6550 struct devlink_trap_policer_item
*policer_item
,
6551 struct genl_info
*info
)
6553 struct netlink_ext_ack
*extack
= info
->extack
;
6554 struct nlattr
**attrs
= info
->attrs
;
6558 rate
= policer_item
->rate
;
6559 burst
= policer_item
->burst
;
6561 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_RATE
])
6562 rate
= nla_get_u64(attrs
[DEVLINK_ATTR_TRAP_POLICER_RATE
]);
6564 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
6565 burst
= nla_get_u64(attrs
[DEVLINK_ATTR_TRAP_POLICER_BURST
]);
6567 if (rate
< policer_item
->policer
->min_rate
) {
6568 NL_SET_ERR_MSG_MOD(extack
, "Policer rate lower than limit");
6572 if (rate
> policer_item
->policer
->max_rate
) {
6573 NL_SET_ERR_MSG_MOD(extack
, "Policer rate higher than limit");
6577 if (burst
< policer_item
->policer
->min_burst
) {
6578 NL_SET_ERR_MSG_MOD(extack
, "Policer burst size lower than limit");
6582 if (burst
> policer_item
->policer
->max_burst
) {
6583 NL_SET_ERR_MSG_MOD(extack
, "Policer burst size higher than limit");
6587 err
= devlink
->ops
->trap_policer_set(devlink
, policer_item
->policer
,
6588 rate
, burst
, info
->extack
);
6592 policer_item
->rate
= rate
;
6593 policer_item
->burst
= burst
;
6598 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff
*skb
,
6599 struct genl_info
*info
)
6601 struct devlink_trap_policer_item
*policer_item
;
6602 struct netlink_ext_ack
*extack
= info
->extack
;
6603 struct devlink
*devlink
= info
->user_ptr
[0];
6605 if (list_empty(&devlink
->trap_policer_list
))
6608 if (!devlink
->ops
->trap_policer_set
)
6611 policer_item
= devlink_trap_policer_item_get_from_info(devlink
, info
);
6612 if (!policer_item
) {
6613 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
6617 return devlink_trap_policer_set(devlink
, policer_item
, info
);
6620 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
6621 [DEVLINK_ATTR_UNSPEC
] = { .strict_start_type
=
6622 DEVLINK_ATTR_TRAP_POLICER_ID
},
6623 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
6624 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
6625 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
6626 [DEVLINK_ATTR_PORT_TYPE
] = { .type
= NLA_U16
},
6627 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
6628 [DEVLINK_ATTR_SB_INDEX
] = { .type
= NLA_U32
},
6629 [DEVLINK_ATTR_SB_POOL_INDEX
] = { .type
= NLA_U16
},
6630 [DEVLINK_ATTR_SB_POOL_TYPE
] = { .type
= NLA_U8
},
6631 [DEVLINK_ATTR_SB_POOL_SIZE
] = { .type
= NLA_U32
},
6632 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = { .type
= NLA_U8
},
6633 [DEVLINK_ATTR_SB_THRESHOLD
] = { .type
= NLA_U32
},
6634 [DEVLINK_ATTR_SB_TC_INDEX
] = { .type
= NLA_U16
},
6635 [DEVLINK_ATTR_ESWITCH_MODE
] = { .type
= NLA_U16
},
6636 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = { .type
= NLA_U8
},
6637 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = { .type
= NLA_U8
},
6638 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = { .type
= NLA_NUL_STRING
},
6639 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = { .type
= NLA_U8
},
6640 [DEVLINK_ATTR_RESOURCE_ID
] = { .type
= NLA_U64
},
6641 [DEVLINK_ATTR_RESOURCE_SIZE
] = { .type
= NLA_U64
},
6642 [DEVLINK_ATTR_PARAM_NAME
] = { .type
= NLA_NUL_STRING
},
6643 [DEVLINK_ATTR_PARAM_TYPE
] = { .type
= NLA_U8
},
6644 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = { .type
= NLA_U8
},
6645 [DEVLINK_ATTR_REGION_NAME
] = { .type
= NLA_NUL_STRING
},
6646 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = { .type
= NLA_U32
},
6647 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = { .type
= NLA_U64
},
6648 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = { .type
= NLA_U64
},
6649 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = { .type
= NLA_NUL_STRING
},
6650 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = { .type
= NLA_U64
},
6651 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
] = { .type
= NLA_U8
},
6652 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
] = { .type
= NLA_NUL_STRING
},
6653 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = { .type
= NLA_NUL_STRING
},
6654 [DEVLINK_ATTR_TRAP_NAME
] = { .type
= NLA_NUL_STRING
},
6655 [DEVLINK_ATTR_TRAP_ACTION
] = { .type
= NLA_U8
},
6656 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = { .type
= NLA_NUL_STRING
},
6657 [DEVLINK_ATTR_NETNS_PID
] = { .type
= NLA_U32
},
6658 [DEVLINK_ATTR_NETNS_FD
] = { .type
= NLA_U32
},
6659 [DEVLINK_ATTR_NETNS_ID
] = { .type
= NLA_U32
},
6660 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
] = { .type
= NLA_U8
},
6661 [DEVLINK_ATTR_TRAP_POLICER_ID
] = { .type
= NLA_U32
},
6662 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = { .type
= NLA_U64
},
6663 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = { .type
= NLA_U64
},
6666 static const struct genl_ops devlink_nl_ops
[] = {
6668 .cmd
= DEVLINK_CMD_GET
,
6669 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6670 .doit
= devlink_nl_cmd_get_doit
,
6671 .dumpit
= devlink_nl_cmd_get_dumpit
,
6672 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6673 /* can be retrieved by unprivileged users */
6676 .cmd
= DEVLINK_CMD_PORT_GET
,
6677 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6678 .doit
= devlink_nl_cmd_port_get_doit
,
6679 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
6680 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6681 /* can be retrieved by unprivileged users */
6684 .cmd
= DEVLINK_CMD_PORT_SET
,
6685 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6686 .doit
= devlink_nl_cmd_port_set_doit
,
6687 .flags
= GENL_ADMIN_PERM
,
6688 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6691 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
6692 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6693 .doit
= devlink_nl_cmd_port_split_doit
,
6694 .flags
= GENL_ADMIN_PERM
,
6695 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6696 DEVLINK_NL_FLAG_NO_LOCK
,
6699 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
6700 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6701 .doit
= devlink_nl_cmd_port_unsplit_doit
,
6702 .flags
= GENL_ADMIN_PERM
,
6703 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6704 DEVLINK_NL_FLAG_NO_LOCK
,
6707 .cmd
= DEVLINK_CMD_SB_GET
,
6708 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6709 .doit
= devlink_nl_cmd_sb_get_doit
,
6710 .dumpit
= devlink_nl_cmd_sb_get_dumpit
,
6711 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6712 DEVLINK_NL_FLAG_NEED_SB
,
6713 /* can be retrieved by unprivileged users */
6716 .cmd
= DEVLINK_CMD_SB_POOL_GET
,
6717 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6718 .doit
= devlink_nl_cmd_sb_pool_get_doit
,
6719 .dumpit
= devlink_nl_cmd_sb_pool_get_dumpit
,
6720 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6721 DEVLINK_NL_FLAG_NEED_SB
,
6722 /* can be retrieved by unprivileged users */
6725 .cmd
= DEVLINK_CMD_SB_POOL_SET
,
6726 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6727 .doit
= devlink_nl_cmd_sb_pool_set_doit
,
6728 .flags
= GENL_ADMIN_PERM
,
6729 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6730 DEVLINK_NL_FLAG_NEED_SB
,
6733 .cmd
= DEVLINK_CMD_SB_PORT_POOL_GET
,
6734 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6735 .doit
= devlink_nl_cmd_sb_port_pool_get_doit
,
6736 .dumpit
= devlink_nl_cmd_sb_port_pool_get_dumpit
,
6737 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6738 DEVLINK_NL_FLAG_NEED_SB
,
6739 /* can be retrieved by unprivileged users */
6742 .cmd
= DEVLINK_CMD_SB_PORT_POOL_SET
,
6743 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6744 .doit
= devlink_nl_cmd_sb_port_pool_set_doit
,
6745 .flags
= GENL_ADMIN_PERM
,
6746 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6747 DEVLINK_NL_FLAG_NEED_SB
,
6750 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_GET
,
6751 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6752 .doit
= devlink_nl_cmd_sb_tc_pool_bind_get_doit
,
6753 .dumpit
= devlink_nl_cmd_sb_tc_pool_bind_get_dumpit
,
6754 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6755 DEVLINK_NL_FLAG_NEED_SB
,
6756 /* can be retrieved by unprivileged users */
6759 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
6760 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6761 .doit
= devlink_nl_cmd_sb_tc_pool_bind_set_doit
,
6762 .flags
= GENL_ADMIN_PERM
,
6763 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
|
6764 DEVLINK_NL_FLAG_NEED_SB
,
6767 .cmd
= DEVLINK_CMD_SB_OCC_SNAPSHOT
,
6768 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6769 .doit
= devlink_nl_cmd_sb_occ_snapshot_doit
,
6770 .flags
= GENL_ADMIN_PERM
,
6771 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6772 DEVLINK_NL_FLAG_NEED_SB
,
6775 .cmd
= DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
6776 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6777 .doit
= devlink_nl_cmd_sb_occ_max_clear_doit
,
6778 .flags
= GENL_ADMIN_PERM
,
6779 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6780 DEVLINK_NL_FLAG_NEED_SB
,
6783 .cmd
= DEVLINK_CMD_ESWITCH_GET
,
6784 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6785 .doit
= devlink_nl_cmd_eswitch_get_doit
,
6786 .flags
= GENL_ADMIN_PERM
,
6787 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6788 DEVLINK_NL_FLAG_NO_LOCK
,
6791 .cmd
= DEVLINK_CMD_ESWITCH_SET
,
6792 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6793 .doit
= devlink_nl_cmd_eswitch_set_doit
,
6794 .flags
= GENL_ADMIN_PERM
,
6795 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6796 DEVLINK_NL_FLAG_NO_LOCK
,
6799 .cmd
= DEVLINK_CMD_DPIPE_TABLE_GET
,
6800 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6801 .doit
= devlink_nl_cmd_dpipe_table_get
,
6802 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6803 /* can be retrieved by unprivileged users */
6806 .cmd
= DEVLINK_CMD_DPIPE_ENTRIES_GET
,
6807 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6808 .doit
= devlink_nl_cmd_dpipe_entries_get
,
6809 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6810 /* can be retrieved by unprivileged users */
6813 .cmd
= DEVLINK_CMD_DPIPE_HEADERS_GET
,
6814 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6815 .doit
= devlink_nl_cmd_dpipe_headers_get
,
6816 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6817 /* can be retrieved by unprivileged users */
6820 .cmd
= DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
6821 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6822 .doit
= devlink_nl_cmd_dpipe_table_counters_set
,
6823 .flags
= GENL_ADMIN_PERM
,
6824 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6827 .cmd
= DEVLINK_CMD_RESOURCE_SET
,
6828 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6829 .doit
= devlink_nl_cmd_resource_set
,
6830 .flags
= GENL_ADMIN_PERM
,
6831 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6834 .cmd
= DEVLINK_CMD_RESOURCE_DUMP
,
6835 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6836 .doit
= devlink_nl_cmd_resource_dump
,
6837 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6838 /* can be retrieved by unprivileged users */
6841 .cmd
= DEVLINK_CMD_RELOAD
,
6842 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6843 .doit
= devlink_nl_cmd_reload
,
6844 .flags
= GENL_ADMIN_PERM
,
6845 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6846 DEVLINK_NL_FLAG_NO_LOCK
,
6849 .cmd
= DEVLINK_CMD_PARAM_GET
,
6850 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6851 .doit
= devlink_nl_cmd_param_get_doit
,
6852 .dumpit
= devlink_nl_cmd_param_get_dumpit
,
6853 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6854 /* can be retrieved by unprivileged users */
6857 .cmd
= DEVLINK_CMD_PARAM_SET
,
6858 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6859 .doit
= devlink_nl_cmd_param_set_doit
,
6860 .flags
= GENL_ADMIN_PERM
,
6861 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6864 .cmd
= DEVLINK_CMD_PORT_PARAM_GET
,
6865 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6866 .doit
= devlink_nl_cmd_port_param_get_doit
,
6867 .dumpit
= devlink_nl_cmd_port_param_get_dumpit
,
6868 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6869 /* can be retrieved by unprivileged users */
6872 .cmd
= DEVLINK_CMD_PORT_PARAM_SET
,
6873 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6874 .doit
= devlink_nl_cmd_port_param_set_doit
,
6875 .flags
= GENL_ADMIN_PERM
,
6876 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
6879 .cmd
= DEVLINK_CMD_REGION_GET
,
6880 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6881 .doit
= devlink_nl_cmd_region_get_doit
,
6882 .dumpit
= devlink_nl_cmd_region_get_dumpit
,
6883 .flags
= GENL_ADMIN_PERM
,
6884 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6887 .cmd
= DEVLINK_CMD_REGION_NEW
,
6888 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6889 .doit
= devlink_nl_cmd_region_new
,
6890 .flags
= GENL_ADMIN_PERM
,
6891 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6894 .cmd
= DEVLINK_CMD_REGION_DEL
,
6895 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6896 .doit
= devlink_nl_cmd_region_del
,
6897 .flags
= GENL_ADMIN_PERM
,
6898 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6901 .cmd
= DEVLINK_CMD_REGION_READ
,
6902 .validate
= GENL_DONT_VALIDATE_STRICT
|
6903 GENL_DONT_VALIDATE_DUMP_STRICT
,
6904 .dumpit
= devlink_nl_cmd_region_read_dumpit
,
6905 .flags
= GENL_ADMIN_PERM
,
6906 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6909 .cmd
= DEVLINK_CMD_INFO_GET
,
6910 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6911 .doit
= devlink_nl_cmd_info_get_doit
,
6912 .dumpit
= devlink_nl_cmd_info_get_dumpit
,
6913 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6914 /* can be retrieved by unprivileged users */
6917 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_GET
,
6918 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6919 .doit
= devlink_nl_cmd_health_reporter_get_doit
,
6920 .dumpit
= devlink_nl_cmd_health_reporter_get_dumpit
,
6921 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6922 DEVLINK_NL_FLAG_NO_LOCK
,
6923 /* can be retrieved by unprivileged users */
6926 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_SET
,
6927 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6928 .doit
= devlink_nl_cmd_health_reporter_set_doit
,
6929 .flags
= GENL_ADMIN_PERM
,
6930 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6931 DEVLINK_NL_FLAG_NO_LOCK
,
6934 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
6935 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6936 .doit
= devlink_nl_cmd_health_reporter_recover_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_DIAGNOSE
,
6943 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6944 .doit
= devlink_nl_cmd_health_reporter_diagnose_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_DUMP_GET
,
6951 .validate
= GENL_DONT_VALIDATE_STRICT
|
6952 GENL_DONT_VALIDATE_DUMP_STRICT
,
6953 .dumpit
= devlink_nl_cmd_health_reporter_dump_get_dumpit
,
6954 .flags
= GENL_ADMIN_PERM
,
6955 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6956 DEVLINK_NL_FLAG_NO_LOCK
,
6959 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
6960 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6961 .doit
= devlink_nl_cmd_health_reporter_dump_clear_doit
,
6962 .flags
= GENL_ADMIN_PERM
,
6963 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
|
6964 DEVLINK_NL_FLAG_NO_LOCK
,
6967 .cmd
= DEVLINK_CMD_FLASH_UPDATE
,
6968 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
6969 .doit
= devlink_nl_cmd_flash_update
,
6970 .flags
= GENL_ADMIN_PERM
,
6971 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6974 .cmd
= DEVLINK_CMD_TRAP_GET
,
6975 .doit
= devlink_nl_cmd_trap_get_doit
,
6976 .dumpit
= devlink_nl_cmd_trap_get_dumpit
,
6977 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6978 /* can be retrieved by unprivileged users */
6981 .cmd
= DEVLINK_CMD_TRAP_SET
,
6982 .doit
= devlink_nl_cmd_trap_set_doit
,
6983 .flags
= GENL_ADMIN_PERM
,
6984 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6987 .cmd
= DEVLINK_CMD_TRAP_GROUP_GET
,
6988 .doit
= devlink_nl_cmd_trap_group_get_doit
,
6989 .dumpit
= devlink_nl_cmd_trap_group_get_dumpit
,
6990 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
6991 /* can be retrieved by unprivileged users */
6994 .cmd
= DEVLINK_CMD_TRAP_GROUP_SET
,
6995 .doit
= devlink_nl_cmd_trap_group_set_doit
,
6996 .flags
= GENL_ADMIN_PERM
,
6997 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
7000 .cmd
= DEVLINK_CMD_TRAP_POLICER_GET
,
7001 .doit
= devlink_nl_cmd_trap_policer_get_doit
,
7002 .dumpit
= devlink_nl_cmd_trap_policer_get_dumpit
,
7003 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
7004 /* can be retrieved by unprivileged users */
7007 .cmd
= DEVLINK_CMD_TRAP_POLICER_SET
,
7008 .doit
= devlink_nl_cmd_trap_policer_set_doit
,
7009 .flags
= GENL_ADMIN_PERM
,
7010 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK
,
7014 static struct genl_family devlink_nl_family __ro_after_init
= {
7015 .name
= DEVLINK_GENL_NAME
,
7016 .version
= DEVLINK_GENL_VERSION
,
7017 .maxattr
= DEVLINK_ATTR_MAX
,
7018 .policy
= devlink_nl_policy
,
7020 .pre_doit
= devlink_nl_pre_doit
,
7021 .post_doit
= devlink_nl_post_doit
,
7022 .module
= THIS_MODULE
,
7023 .ops
= devlink_nl_ops
,
7024 .n_ops
= ARRAY_SIZE(devlink_nl_ops
),
7025 .mcgrps
= devlink_nl_mcgrps
,
7026 .n_mcgrps
= ARRAY_SIZE(devlink_nl_mcgrps
),
7030 * devlink_alloc - Allocate new devlink instance resources
7033 * @priv_size: size of user private data
7035 * Allocate new devlink instance resources, including devlink index
7038 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
7040 struct devlink
*devlink
;
7045 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
7049 xa_init_flags(&devlink
->snapshot_ids
, XA_FLAGS_ALLOC
);
7050 __devlink_net_set(devlink
, &init_net
);
7051 INIT_LIST_HEAD(&devlink
->port_list
);
7052 INIT_LIST_HEAD(&devlink
->sb_list
);
7053 INIT_LIST_HEAD_RCU(&devlink
->dpipe_table_list
);
7054 INIT_LIST_HEAD(&devlink
->resource_list
);
7055 INIT_LIST_HEAD(&devlink
->param_list
);
7056 INIT_LIST_HEAD(&devlink
->region_list
);
7057 INIT_LIST_HEAD(&devlink
->reporter_list
);
7058 INIT_LIST_HEAD(&devlink
->trap_list
);
7059 INIT_LIST_HEAD(&devlink
->trap_group_list
);
7060 INIT_LIST_HEAD(&devlink
->trap_policer_list
);
7061 mutex_init(&devlink
->lock
);
7062 mutex_init(&devlink
->reporters_lock
);
7065 EXPORT_SYMBOL_GPL(devlink_alloc
);
7068 * devlink_register - Register devlink instance
7071 * @dev: parent device
7073 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
7075 mutex_lock(&devlink_mutex
);
7077 devlink
->registered
= true;
7078 list_add_tail(&devlink
->list
, &devlink_list
);
7079 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
7080 mutex_unlock(&devlink_mutex
);
7083 EXPORT_SYMBOL_GPL(devlink_register
);
7086 * devlink_unregister - Unregister devlink instance
7090 void devlink_unregister(struct devlink
*devlink
)
7092 mutex_lock(&devlink_mutex
);
7093 WARN_ON(devlink_reload_supported(devlink
) &&
7094 devlink
->reload_enabled
);
7095 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
7096 list_del(&devlink
->list
);
7097 mutex_unlock(&devlink_mutex
);
7099 EXPORT_SYMBOL_GPL(devlink_unregister
);
7102 * devlink_reload_enable - Enable reload of devlink instance
7106 * Should be called at end of device initialization
7107 * process when reload operation is supported.
7109 void devlink_reload_enable(struct devlink
*devlink
)
7111 mutex_lock(&devlink_mutex
);
7112 devlink
->reload_enabled
= true;
7113 mutex_unlock(&devlink_mutex
);
7115 EXPORT_SYMBOL_GPL(devlink_reload_enable
);
7118 * devlink_reload_disable - Disable reload of devlink instance
7122 * Should be called at the beginning of device cleanup
7123 * process when reload operation is supported.
7125 void devlink_reload_disable(struct devlink
*devlink
)
7127 mutex_lock(&devlink_mutex
);
7128 /* Mutex is taken which ensures that no reload operation is in
7129 * progress while setting up forbidded flag.
7131 devlink
->reload_enabled
= false;
7132 mutex_unlock(&devlink_mutex
);
7134 EXPORT_SYMBOL_GPL(devlink_reload_disable
);
7137 * devlink_free - Free devlink instance resources
7141 void devlink_free(struct devlink
*devlink
)
7143 mutex_destroy(&devlink
->reporters_lock
);
7144 mutex_destroy(&devlink
->lock
);
7145 WARN_ON(!list_empty(&devlink
->trap_policer_list
));
7146 WARN_ON(!list_empty(&devlink
->trap_group_list
));
7147 WARN_ON(!list_empty(&devlink
->trap_list
));
7148 WARN_ON(!list_empty(&devlink
->reporter_list
));
7149 WARN_ON(!list_empty(&devlink
->region_list
));
7150 WARN_ON(!list_empty(&devlink
->param_list
));
7151 WARN_ON(!list_empty(&devlink
->resource_list
));
7152 WARN_ON(!list_empty(&devlink
->dpipe_table_list
));
7153 WARN_ON(!list_empty(&devlink
->sb_list
));
7154 WARN_ON(!list_empty(&devlink
->port_list
));
7156 xa_destroy(&devlink
->snapshot_ids
);
7160 EXPORT_SYMBOL_GPL(devlink_free
);
7162 static void devlink_port_type_warn(struct work_struct
*work
)
7164 WARN(true, "Type was not set for devlink port.");
7167 static bool devlink_port_type_should_warn(struct devlink_port
*devlink_port
)
7169 /* Ignore CPU and DSA flavours. */
7170 return devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_CPU
&&
7171 devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_DSA
;
7174 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
7176 static void devlink_port_type_warn_schedule(struct devlink_port
*devlink_port
)
7178 if (!devlink_port_type_should_warn(devlink_port
))
7180 /* Schedule a work to WARN in case driver does not set port
7181 * type within timeout.
7183 schedule_delayed_work(&devlink_port
->type_warn_dw
,
7184 DEVLINK_PORT_TYPE_WARN_TIMEOUT
);
7187 static void devlink_port_type_warn_cancel(struct devlink_port
*devlink_port
)
7189 if (!devlink_port_type_should_warn(devlink_port
))
7191 cancel_delayed_work_sync(&devlink_port
->type_warn_dw
);
7195 * devlink_port_register - Register devlink port
7198 * @devlink_port: devlink port
7199 * @port_index: driver-specific numerical identifier of the port
7201 * Register devlink port with provided port index. User can use
7202 * any indexing, even hw-related one. devlink_port structure
7203 * is convenient to be embedded inside user driver private structure.
7204 * Note that the caller should take care of zeroing the devlink_port
7207 int devlink_port_register(struct devlink
*devlink
,
7208 struct devlink_port
*devlink_port
,
7209 unsigned int port_index
)
7211 mutex_lock(&devlink
->lock
);
7212 if (devlink_port_index_exists(devlink
, port_index
)) {
7213 mutex_unlock(&devlink
->lock
);
7216 devlink_port
->devlink
= devlink
;
7217 devlink_port
->index
= port_index
;
7218 devlink_port
->registered
= true;
7219 spin_lock_init(&devlink_port
->type_lock
);
7220 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
7221 INIT_LIST_HEAD(&devlink_port
->param_list
);
7222 mutex_unlock(&devlink
->lock
);
7223 INIT_DELAYED_WORK(&devlink_port
->type_warn_dw
, &devlink_port_type_warn
);
7224 devlink_port_type_warn_schedule(devlink_port
);
7225 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
7228 EXPORT_SYMBOL_GPL(devlink_port_register
);
7231 * devlink_port_unregister - Unregister devlink port
7233 * @devlink_port: devlink port
7235 void devlink_port_unregister(struct devlink_port
*devlink_port
)
7237 struct devlink
*devlink
= devlink_port
->devlink
;
7239 devlink_port_type_warn_cancel(devlink_port
);
7240 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
7241 mutex_lock(&devlink
->lock
);
7242 list_del(&devlink_port
->list
);
7243 mutex_unlock(&devlink
->lock
);
7245 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
7247 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
7248 enum devlink_port_type type
,
7251 if (WARN_ON(!devlink_port
->registered
))
7253 devlink_port_type_warn_cancel(devlink_port
);
7254 spin_lock_bh(&devlink_port
->type_lock
);
7255 devlink_port
->type
= type
;
7256 devlink_port
->type_dev
= type_dev
;
7257 spin_unlock_bh(&devlink_port
->type_lock
);
7258 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
7262 * devlink_port_type_eth_set - Set port type to Ethernet
7264 * @devlink_port: devlink port
7265 * @netdev: related netdevice
7267 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
7268 struct net_device
*netdev
)
7270 const struct net_device_ops
*ops
= netdev
->netdev_ops
;
7272 /* If driver registers devlink port, it should set devlink port
7273 * attributes accordingly so the compat functions are called
7274 * and the original ops are not used.
7276 if (ops
->ndo_get_phys_port_name
) {
7277 /* Some drivers use the same set of ndos for netdevs
7278 * that have devlink_port registered and also for
7279 * those who don't. Make sure that ndo_get_phys_port_name
7280 * returns -EOPNOTSUPP here in case it is defined.
7283 char name
[IFNAMSIZ
];
7286 err
= ops
->ndo_get_phys_port_name(netdev
, name
, sizeof(name
));
7287 WARN_ON(err
!= -EOPNOTSUPP
);
7289 if (ops
->ndo_get_port_parent_id
) {
7290 /* Some drivers use the same set of ndos for netdevs
7291 * that have devlink_port registered and also for
7292 * those who don't. Make sure that ndo_get_port_parent_id
7293 * returns -EOPNOTSUPP here in case it is defined.
7296 struct netdev_phys_item_id ppid
;
7299 err
= ops
->ndo_get_port_parent_id(netdev
, &ppid
);
7300 WARN_ON(err
!= -EOPNOTSUPP
);
7302 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_ETH
, netdev
);
7304 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
7307 * devlink_port_type_ib_set - Set port type to InfiniBand
7309 * @devlink_port: devlink port
7310 * @ibdev: related IB device
7312 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
7313 struct ib_device
*ibdev
)
7315 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_IB
, ibdev
);
7317 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
7320 * devlink_port_type_clear - Clear port type
7322 * @devlink_port: devlink port
7324 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
7326 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_NOTSET
, NULL
);
7327 devlink_port_type_warn_schedule(devlink_port
);
7329 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
7331 static int __devlink_port_attrs_set(struct devlink_port
*devlink_port
,
7332 enum devlink_port_flavour flavour
,
7333 const unsigned char *switch_id
,
7334 unsigned char switch_id_len
)
7336 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7338 if (WARN_ON(devlink_port
->registered
))
7341 attrs
->flavour
= flavour
;
7343 attrs
->switch_port
= true;
7344 if (WARN_ON(switch_id_len
> MAX_PHYS_ITEM_ID_LEN
))
7345 switch_id_len
= MAX_PHYS_ITEM_ID_LEN
;
7346 memcpy(attrs
->switch_id
.id
, switch_id
, switch_id_len
);
7347 attrs
->switch_id
.id_len
= switch_id_len
;
7349 attrs
->switch_port
= false;
7355 * devlink_port_attrs_set - Set port attributes
7357 * @devlink_port: devlink port
7358 * @flavour: flavour of the port
7359 * @port_number: number of the port that is facing user, for example
7360 * the front panel port number
7361 * @split: indicates if this is split port
7362 * @split_subport_number: if the port is split, this is the number
7364 * @switch_id: if the port is part of switch, this is buffer with ID,
7365 * otwerwise this is NULL
7366 * @switch_id_len: length of the switch_id buffer
7368 void devlink_port_attrs_set(struct devlink_port
*devlink_port
,
7369 enum devlink_port_flavour flavour
,
7370 u32 port_number
, bool split
,
7371 u32 split_subport_number
,
7372 const unsigned char *switch_id
,
7373 unsigned char switch_id_len
)
7375 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7378 ret
= __devlink_port_attrs_set(devlink_port
, flavour
,
7379 switch_id
, switch_id_len
);
7382 attrs
->split
= split
;
7383 attrs
->phys
.port_number
= port_number
;
7384 attrs
->phys
.split_subport_number
= split_subport_number
;
7386 EXPORT_SYMBOL_GPL(devlink_port_attrs_set
);
7389 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
7391 * @devlink_port: devlink port
7392 * @pf: associated PF for the devlink port instance
7393 * @switch_id: if the port is part of switch, this is buffer with ID,
7394 * otherwise this is NULL
7395 * @switch_id_len: length of the switch_id buffer
7397 void devlink_port_attrs_pci_pf_set(struct devlink_port
*devlink_port
,
7398 const unsigned char *switch_id
,
7399 unsigned char switch_id_len
, u16 pf
)
7401 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7404 ret
= __devlink_port_attrs_set(devlink_port
,
7405 DEVLINK_PORT_FLAVOUR_PCI_PF
,
7406 switch_id
, switch_id_len
);
7410 attrs
->pci_pf
.pf
= pf
;
7412 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set
);
7415 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
7417 * @devlink_port: devlink port
7418 * @pf: associated PF for the devlink port instance
7419 * @vf: associated VF of a PF for the devlink port instance
7420 * @switch_id: if the port is part of switch, this is buffer with ID,
7421 * otherwise this is NULL
7422 * @switch_id_len: length of the switch_id buffer
7424 void devlink_port_attrs_pci_vf_set(struct devlink_port
*devlink_port
,
7425 const unsigned char *switch_id
,
7426 unsigned char switch_id_len
,
7429 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7432 ret
= __devlink_port_attrs_set(devlink_port
,
7433 DEVLINK_PORT_FLAVOUR_PCI_VF
,
7434 switch_id
, switch_id_len
);
7437 attrs
->pci_vf
.pf
= pf
;
7438 attrs
->pci_vf
.vf
= vf
;
7440 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set
);
7442 static int __devlink_port_phys_port_name_get(struct devlink_port
*devlink_port
,
7443 char *name
, size_t len
)
7445 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
7451 switch (attrs
->flavour
) {
7452 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
7453 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
7455 n
= snprintf(name
, len
, "p%u", attrs
->phys
.port_number
);
7457 n
= snprintf(name
, len
, "p%us%u",
7458 attrs
->phys
.port_number
,
7459 attrs
->phys
.split_subport_number
);
7461 case DEVLINK_PORT_FLAVOUR_CPU
:
7462 case DEVLINK_PORT_FLAVOUR_DSA
:
7463 /* As CPU and DSA ports do not have a netdevice associated
7464 * case should not ever happen.
7468 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
7469 n
= snprintf(name
, len
, "pf%u", attrs
->pci_pf
.pf
);
7471 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
7472 n
= snprintf(name
, len
, "pf%uvf%u",
7473 attrs
->pci_vf
.pf
, attrs
->pci_vf
.vf
);
7483 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
7484 u32 size
, u16 ingress_pools_count
,
7485 u16 egress_pools_count
, u16 ingress_tc_count
,
7486 u16 egress_tc_count
)
7488 struct devlink_sb
*devlink_sb
;
7491 mutex_lock(&devlink
->lock
);
7492 if (devlink_sb_index_exists(devlink
, sb_index
)) {
7497 devlink_sb
= kzalloc(sizeof(*devlink_sb
), GFP_KERNEL
);
7502 devlink_sb
->index
= sb_index
;
7503 devlink_sb
->size
= size
;
7504 devlink_sb
->ingress_pools_count
= ingress_pools_count
;
7505 devlink_sb
->egress_pools_count
= egress_pools_count
;
7506 devlink_sb
->ingress_tc_count
= ingress_tc_count
;
7507 devlink_sb
->egress_tc_count
= egress_tc_count
;
7508 list_add_tail(&devlink_sb
->list
, &devlink
->sb_list
);
7510 mutex_unlock(&devlink
->lock
);
7513 EXPORT_SYMBOL_GPL(devlink_sb_register
);
7515 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
)
7517 struct devlink_sb
*devlink_sb
;
7519 mutex_lock(&devlink
->lock
);
7520 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
7521 WARN_ON(!devlink_sb
);
7522 list_del(&devlink_sb
->list
);
7523 mutex_unlock(&devlink
->lock
);
7526 EXPORT_SYMBOL_GPL(devlink_sb_unregister
);
7529 * devlink_dpipe_headers_register - register dpipe headers
7532 * @dpipe_headers: dpipe header array
7534 * Register the headers supported by hardware.
7536 int devlink_dpipe_headers_register(struct devlink
*devlink
,
7537 struct devlink_dpipe_headers
*dpipe_headers
)
7539 mutex_lock(&devlink
->lock
);
7540 devlink
->dpipe_headers
= dpipe_headers
;
7541 mutex_unlock(&devlink
->lock
);
7544 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register
);
7547 * devlink_dpipe_headers_unregister - unregister dpipe headers
7551 * Unregister the headers supported by hardware.
7553 void devlink_dpipe_headers_unregister(struct devlink
*devlink
)
7555 mutex_lock(&devlink
->lock
);
7556 devlink
->dpipe_headers
= NULL
;
7557 mutex_unlock(&devlink
->lock
);
7559 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister
);
7562 * devlink_dpipe_table_counter_enabled - check if counter allocation
7565 * @table_name: tables name
7567 * Used by driver to check if counter allocation is required.
7568 * After counter allocation is turned on the table entries
7569 * are updated to include counter statistics.
7571 * After that point on the driver must respect the counter
7572 * state so that each entry added to the table is added
7575 bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
7576 const char *table_name
)
7578 struct devlink_dpipe_table
*table
;
7582 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
7583 table_name
, devlink
);
7586 enabled
= table
->counters_enabled
;
7590 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled
);
7593 * devlink_dpipe_table_register - register dpipe table
7596 * @table_name: table name
7597 * @table_ops: table ops
7599 * @counter_control_extern: external control for counters
7601 int devlink_dpipe_table_register(struct devlink
*devlink
,
7602 const char *table_name
,
7603 struct devlink_dpipe_table_ops
*table_ops
,
7604 void *priv
, bool counter_control_extern
)
7606 struct devlink_dpipe_table
*table
;
7609 if (WARN_ON(!table_ops
->size_get
))
7612 mutex_lock(&devlink
->lock
);
7614 if (devlink_dpipe_table_find(&devlink
->dpipe_table_list
, table_name
,
7620 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
7626 table
->name
= table_name
;
7627 table
->table_ops
= table_ops
;
7629 table
->counter_control_extern
= counter_control_extern
;
7631 list_add_tail_rcu(&table
->list
, &devlink
->dpipe_table_list
);
7633 mutex_unlock(&devlink
->lock
);
7636 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register
);
7639 * devlink_dpipe_table_unregister - unregister dpipe table
7642 * @table_name: table name
7644 void devlink_dpipe_table_unregister(struct devlink
*devlink
,
7645 const char *table_name
)
7647 struct devlink_dpipe_table
*table
;
7649 mutex_lock(&devlink
->lock
);
7650 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
7651 table_name
, devlink
);
7654 list_del_rcu(&table
->list
);
7655 mutex_unlock(&devlink
->lock
);
7656 kfree_rcu(table
, rcu
);
7659 mutex_unlock(&devlink
->lock
);
7661 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister
);
7664 * devlink_resource_register - devlink resource register
7667 * @resource_name: resource's name
7668 * @resource_size: resource's size
7669 * @resource_id: resource's id
7670 * @parent_resource_id: resource's parent id
7671 * @size_params: size parameters
7673 int devlink_resource_register(struct devlink
*devlink
,
7674 const char *resource_name
,
7677 u64 parent_resource_id
,
7678 const struct devlink_resource_size_params
*size_params
)
7680 struct devlink_resource
*resource
;
7681 struct list_head
*resource_list
;
7685 top_hierarchy
= parent_resource_id
== DEVLINK_RESOURCE_ID_PARENT_TOP
;
7687 mutex_lock(&devlink
->lock
);
7688 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7694 resource
= kzalloc(sizeof(*resource
), GFP_KERNEL
);
7700 if (top_hierarchy
) {
7701 resource_list
= &devlink
->resource_list
;
7703 struct devlink_resource
*parent_resource
;
7705 parent_resource
= devlink_resource_find(devlink
, NULL
,
7706 parent_resource_id
);
7707 if (parent_resource
) {
7708 resource_list
= &parent_resource
->resource_list
;
7709 resource
->parent
= parent_resource
;
7717 resource
->name
= resource_name
;
7718 resource
->size
= resource_size
;
7719 resource
->size_new
= resource_size
;
7720 resource
->id
= resource_id
;
7721 resource
->size_valid
= true;
7722 memcpy(&resource
->size_params
, size_params
,
7723 sizeof(resource
->size_params
));
7724 INIT_LIST_HEAD(&resource
->resource_list
);
7725 list_add_tail(&resource
->list
, resource_list
);
7727 mutex_unlock(&devlink
->lock
);
7730 EXPORT_SYMBOL_GPL(devlink_resource_register
);
7733 * devlink_resources_unregister - free all resources
7736 * @resource: resource
7738 void devlink_resources_unregister(struct devlink
*devlink
,
7739 struct devlink_resource
*resource
)
7741 struct devlink_resource
*tmp
, *child_resource
;
7742 struct list_head
*resource_list
;
7745 resource_list
= &resource
->resource_list
;
7747 resource_list
= &devlink
->resource_list
;
7750 mutex_lock(&devlink
->lock
);
7752 list_for_each_entry_safe(child_resource
, tmp
, resource_list
, list
) {
7753 devlink_resources_unregister(devlink
, child_resource
);
7754 list_del(&child_resource
->list
);
7755 kfree(child_resource
);
7759 mutex_unlock(&devlink
->lock
);
7761 EXPORT_SYMBOL_GPL(devlink_resources_unregister
);
7764 * devlink_resource_size_get - get and update size
7767 * @resource_id: the requested resource id
7768 * @p_resource_size: ptr to update
7770 int devlink_resource_size_get(struct devlink
*devlink
,
7772 u64
*p_resource_size
)
7774 struct devlink_resource
*resource
;
7777 mutex_lock(&devlink
->lock
);
7778 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7783 *p_resource_size
= resource
->size_new
;
7784 resource
->size
= resource
->size_new
;
7786 mutex_unlock(&devlink
->lock
);
7789 EXPORT_SYMBOL_GPL(devlink_resource_size_get
);
7792 * devlink_dpipe_table_resource_set - set the resource id
7795 * @table_name: table name
7796 * @resource_id: resource id
7797 * @resource_units: number of resource's units consumed per table's entry
7799 int devlink_dpipe_table_resource_set(struct devlink
*devlink
,
7800 const char *table_name
, u64 resource_id
,
7803 struct devlink_dpipe_table
*table
;
7806 mutex_lock(&devlink
->lock
);
7807 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
7808 table_name
, devlink
);
7813 table
->resource_id
= resource_id
;
7814 table
->resource_units
= resource_units
;
7815 table
->resource_valid
= true;
7817 mutex_unlock(&devlink
->lock
);
7820 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set
);
7823 * devlink_resource_occ_get_register - register occupancy getter
7826 * @resource_id: resource id
7827 * @occ_get: occupancy getter callback
7828 * @occ_get_priv: occupancy getter callback priv
7830 void devlink_resource_occ_get_register(struct devlink
*devlink
,
7832 devlink_resource_occ_get_t
*occ_get
,
7835 struct devlink_resource
*resource
;
7837 mutex_lock(&devlink
->lock
);
7838 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7839 if (WARN_ON(!resource
))
7841 WARN_ON(resource
->occ_get
);
7843 resource
->occ_get
= occ_get
;
7844 resource
->occ_get_priv
= occ_get_priv
;
7846 mutex_unlock(&devlink
->lock
);
7848 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register
);
7851 * devlink_resource_occ_get_unregister - unregister occupancy getter
7854 * @resource_id: resource id
7856 void devlink_resource_occ_get_unregister(struct devlink
*devlink
,
7859 struct devlink_resource
*resource
;
7861 mutex_lock(&devlink
->lock
);
7862 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
7863 if (WARN_ON(!resource
))
7865 WARN_ON(!resource
->occ_get
);
7867 resource
->occ_get
= NULL
;
7868 resource
->occ_get_priv
= NULL
;
7870 mutex_unlock(&devlink
->lock
);
7872 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister
);
7874 static int devlink_param_verify(const struct devlink_param
*param
)
7876 if (!param
|| !param
->name
|| !param
->supported_cmodes
)
7879 return devlink_param_generic_verify(param
);
7881 return devlink_param_driver_verify(param
);
7884 static int __devlink_params_register(struct devlink
*devlink
,
7885 unsigned int port_index
,
7886 struct list_head
*param_list
,
7887 const struct devlink_param
*params
,
7888 size_t params_count
,
7889 enum devlink_command reg_cmd
,
7890 enum devlink_command unreg_cmd
)
7892 const struct devlink_param
*param
= params
;
7896 mutex_lock(&devlink
->lock
);
7897 for (i
= 0; i
< params_count
; i
++, param
++) {
7898 err
= devlink_param_verify(param
);
7902 err
= devlink_param_register_one(devlink
, port_index
,
7903 param_list
, param
, reg_cmd
);
7908 mutex_unlock(&devlink
->lock
);
7914 for (param
--; i
> 0; i
--, param
--)
7915 devlink_param_unregister_one(devlink
, port_index
, param_list
,
7918 mutex_unlock(&devlink
->lock
);
7922 static void __devlink_params_unregister(struct devlink
*devlink
,
7923 unsigned int port_index
,
7924 struct list_head
*param_list
,
7925 const struct devlink_param
*params
,
7926 size_t params_count
,
7927 enum devlink_command cmd
)
7929 const struct devlink_param
*param
= params
;
7932 mutex_lock(&devlink
->lock
);
7933 for (i
= 0; i
< params_count
; i
++, param
++)
7934 devlink_param_unregister_one(devlink
, 0, param_list
, param
,
7936 mutex_unlock(&devlink
->lock
);
7940 * devlink_params_register - register configuration parameters
7943 * @params: configuration parameters array
7944 * @params_count: number of parameters provided
7946 * Register the configuration parameters supported by the driver.
7948 int devlink_params_register(struct devlink
*devlink
,
7949 const struct devlink_param
*params
,
7950 size_t params_count
)
7952 return __devlink_params_register(devlink
, 0, &devlink
->param_list
,
7953 params
, params_count
,
7954 DEVLINK_CMD_PARAM_NEW
,
7955 DEVLINK_CMD_PARAM_DEL
);
7957 EXPORT_SYMBOL_GPL(devlink_params_register
);
7960 * devlink_params_unregister - unregister configuration parameters
7962 * @params: configuration parameters to unregister
7963 * @params_count: number of parameters provided
7965 void devlink_params_unregister(struct devlink
*devlink
,
7966 const struct devlink_param
*params
,
7967 size_t params_count
)
7969 return __devlink_params_unregister(devlink
, 0, &devlink
->param_list
,
7970 params
, params_count
,
7971 DEVLINK_CMD_PARAM_DEL
);
7973 EXPORT_SYMBOL_GPL(devlink_params_unregister
);
7976 * devlink_params_publish - publish configuration parameters
7980 * Publish previously registered configuration parameters.
7982 void devlink_params_publish(struct devlink
*devlink
)
7984 struct devlink_param_item
*param_item
;
7986 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
7987 if (param_item
->published
)
7989 param_item
->published
= true;
7990 devlink_param_notify(devlink
, 0, param_item
,
7991 DEVLINK_CMD_PARAM_NEW
);
7994 EXPORT_SYMBOL_GPL(devlink_params_publish
);
7997 * devlink_params_unpublish - unpublish configuration parameters
8001 * Unpublish previously registered configuration parameters.
8003 void devlink_params_unpublish(struct devlink
*devlink
)
8005 struct devlink_param_item
*param_item
;
8007 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
8008 if (!param_item
->published
)
8010 param_item
->published
= false;
8011 devlink_param_notify(devlink
, 0, param_item
,
8012 DEVLINK_CMD_PARAM_DEL
);
8015 EXPORT_SYMBOL_GPL(devlink_params_unpublish
);
8018 * devlink_port_params_register - register port configuration parameters
8020 * @devlink_port: devlink port
8021 * @params: configuration parameters array
8022 * @params_count: number of parameters provided
8024 * Register the configuration parameters supported by the port.
8026 int devlink_port_params_register(struct devlink_port
*devlink_port
,
8027 const struct devlink_param
*params
,
8028 size_t params_count
)
8030 return __devlink_params_register(devlink_port
->devlink
,
8031 devlink_port
->index
,
8032 &devlink_port
->param_list
, params
,
8034 DEVLINK_CMD_PORT_PARAM_NEW
,
8035 DEVLINK_CMD_PORT_PARAM_DEL
);
8037 EXPORT_SYMBOL_GPL(devlink_port_params_register
);
8040 * devlink_port_params_unregister - unregister port configuration
8043 * @devlink_port: devlink port
8044 * @params: configuration parameters array
8045 * @params_count: number of parameters provided
8047 void devlink_port_params_unregister(struct devlink_port
*devlink_port
,
8048 const struct devlink_param
*params
,
8049 size_t params_count
)
8051 return __devlink_params_unregister(devlink_port
->devlink
,
8052 devlink_port
->index
,
8053 &devlink_port
->param_list
,
8054 params
, params_count
,
8055 DEVLINK_CMD_PORT_PARAM_DEL
);
8057 EXPORT_SYMBOL_GPL(devlink_port_params_unregister
);
8060 __devlink_param_driverinit_value_get(struct list_head
*param_list
, u32 param_id
,
8061 union devlink_param_value
*init_val
)
8063 struct devlink_param_item
*param_item
;
8065 param_item
= devlink_param_find_by_id(param_list
, param_id
);
8069 if (!param_item
->driverinit_value_valid
||
8070 !devlink_param_cmode_is_supported(param_item
->param
,
8071 DEVLINK_PARAM_CMODE_DRIVERINIT
))
8074 if (param_item
->param
->type
== DEVLINK_PARAM_TYPE_STRING
)
8075 strcpy(init_val
->vstr
, param_item
->driverinit_value
.vstr
);
8077 *init_val
= param_item
->driverinit_value
;
8083 __devlink_param_driverinit_value_set(struct devlink
*devlink
,
8084 unsigned int port_index
,
8085 struct list_head
*param_list
, u32 param_id
,
8086 union devlink_param_value init_val
,
8087 enum devlink_command cmd
)
8089 struct devlink_param_item
*param_item
;
8091 param_item
= devlink_param_find_by_id(param_list
, param_id
);
8095 if (!devlink_param_cmode_is_supported(param_item
->param
,
8096 DEVLINK_PARAM_CMODE_DRIVERINIT
))
8099 if (param_item
->param
->type
== DEVLINK_PARAM_TYPE_STRING
)
8100 strcpy(param_item
->driverinit_value
.vstr
, init_val
.vstr
);
8102 param_item
->driverinit_value
= init_val
;
8103 param_item
->driverinit_value_valid
= true;
8105 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
8110 * devlink_param_driverinit_value_get - get configuration parameter
8111 * value for driver initializing
8114 * @param_id: parameter ID
8115 * @init_val: value of parameter in driverinit configuration mode
8117 * This function should be used by the driver to get driverinit
8118 * configuration for initialization after reload command.
8120 int devlink_param_driverinit_value_get(struct devlink
*devlink
, u32 param_id
,
8121 union devlink_param_value
*init_val
)
8123 if (!devlink_reload_supported(devlink
))
8126 return __devlink_param_driverinit_value_get(&devlink
->param_list
,
8127 param_id
, init_val
);
8129 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get
);
8132 * devlink_param_driverinit_value_set - set value of configuration
8133 * parameter for driverinit
8134 * configuration mode
8137 * @param_id: parameter ID
8138 * @init_val: value of parameter to set for driverinit configuration mode
8140 * This function should be used by the driver to set driverinit
8141 * configuration mode default value.
8143 int devlink_param_driverinit_value_set(struct devlink
*devlink
, u32 param_id
,
8144 union devlink_param_value init_val
)
8146 return __devlink_param_driverinit_value_set(devlink
, 0,
8147 &devlink
->param_list
,
8149 DEVLINK_CMD_PARAM_NEW
);
8151 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set
);
8154 * devlink_port_param_driverinit_value_get - get configuration parameter
8155 * value for driver initializing
8157 * @devlink_port: devlink_port
8158 * @param_id: parameter ID
8159 * @init_val: value of parameter in driverinit configuration mode
8161 * This function should be used by the driver to get driverinit
8162 * configuration for initialization after reload command.
8164 int devlink_port_param_driverinit_value_get(struct devlink_port
*devlink_port
,
8166 union devlink_param_value
*init_val
)
8168 struct devlink
*devlink
= devlink_port
->devlink
;
8170 if (!devlink_reload_supported(devlink
))
8173 return __devlink_param_driverinit_value_get(&devlink_port
->param_list
,
8174 param_id
, init_val
);
8176 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get
);
8179 * devlink_port_param_driverinit_value_set - set value of configuration
8180 * parameter for driverinit
8181 * configuration mode
8183 * @devlink_port: devlink_port
8184 * @param_id: parameter ID
8185 * @init_val: value of parameter to set for driverinit configuration mode
8187 * This function should be used by the driver to set driverinit
8188 * configuration mode default value.
8190 int devlink_port_param_driverinit_value_set(struct devlink_port
*devlink_port
,
8192 union devlink_param_value init_val
)
8194 return __devlink_param_driverinit_value_set(devlink_port
->devlink
,
8195 devlink_port
->index
,
8196 &devlink_port
->param_list
,
8198 DEVLINK_CMD_PORT_PARAM_NEW
);
8200 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set
);
8203 * devlink_param_value_changed - notify devlink on a parameter's value
8204 * change. Should be called by the driver
8205 * right after the change.
8208 * @param_id: parameter ID
8210 * This function should be used by the driver to notify devlink on value
8211 * change, excluding driverinit configuration mode.
8212 * For driverinit configuration mode driver should use the function
8214 void devlink_param_value_changed(struct devlink
*devlink
, u32 param_id
)
8216 struct devlink_param_item
*param_item
;
8218 param_item
= devlink_param_find_by_id(&devlink
->param_list
, param_id
);
8219 WARN_ON(!param_item
);
8221 devlink_param_notify(devlink
, 0, param_item
, DEVLINK_CMD_PARAM_NEW
);
8223 EXPORT_SYMBOL_GPL(devlink_param_value_changed
);
8226 * devlink_port_param_value_changed - notify devlink on a parameter's value
8227 * change. Should be called by the driver
8228 * right after the change.
8230 * @devlink_port: devlink_port
8231 * @param_id: parameter ID
8233 * This function should be used by the driver to notify devlink on value
8234 * change, excluding driverinit configuration mode.
8235 * For driverinit configuration mode driver should use the function
8236 * devlink_port_param_driverinit_value_set() instead.
8238 void devlink_port_param_value_changed(struct devlink_port
*devlink_port
,
8241 struct devlink_param_item
*param_item
;
8243 param_item
= devlink_param_find_by_id(&devlink_port
->param_list
,
8245 WARN_ON(!param_item
);
8247 devlink_param_notify(devlink_port
->devlink
, devlink_port
->index
,
8248 param_item
, DEVLINK_CMD_PORT_PARAM_NEW
);
8250 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed
);
8253 * devlink_param_value_str_fill - Safely fill-up the string preventing
8254 * from overflow of the preallocated buffer
8256 * @dst_val: destination devlink_param_value
8257 * @src: source buffer
8259 void devlink_param_value_str_fill(union devlink_param_value
*dst_val
,
8264 len
= strlcpy(dst_val
->vstr
, src
, __DEVLINK_PARAM_MAX_STRING_VALUE
);
8265 WARN_ON(len
>= __DEVLINK_PARAM_MAX_STRING_VALUE
);
8267 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill
);
8270 * devlink_region_create - create a new address region
8273 * @ops: region operations and name
8274 * @region_max_snapshots: Maximum supported number of snapshots for region
8275 * @region_size: size of region
8277 struct devlink_region
*
8278 devlink_region_create(struct devlink
*devlink
,
8279 const struct devlink_region_ops
*ops
,
8280 u32 region_max_snapshots
, u64 region_size
)
8282 struct devlink_region
*region
;
8285 if (WARN_ON(!ops
) || WARN_ON(!ops
->destructor
))
8286 return ERR_PTR(-EINVAL
);
8288 mutex_lock(&devlink
->lock
);
8290 if (devlink_region_get_by_name(devlink
, ops
->name
)) {
8295 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
8301 region
->devlink
= devlink
;
8302 region
->max_snapshots
= region_max_snapshots
;
8304 region
->size
= region_size
;
8305 INIT_LIST_HEAD(®ion
->snapshot_list
);
8306 list_add_tail(®ion
->list
, &devlink
->region_list
);
8307 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_NEW
);
8309 mutex_unlock(&devlink
->lock
);
8313 mutex_unlock(&devlink
->lock
);
8314 return ERR_PTR(err
);
8316 EXPORT_SYMBOL_GPL(devlink_region_create
);
8319 * devlink_region_destroy - destroy address region
8321 * @region: devlink region to destroy
8323 void devlink_region_destroy(struct devlink_region
*region
)
8325 struct devlink
*devlink
= region
->devlink
;
8326 struct devlink_snapshot
*snapshot
, *ts
;
8328 mutex_lock(&devlink
->lock
);
8330 /* Free all snapshots of region */
8331 list_for_each_entry_safe(snapshot
, ts
, ®ion
->snapshot_list
, list
)
8332 devlink_region_snapshot_del(region
, snapshot
);
8334 list_del(®ion
->list
);
8336 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_DEL
);
8337 mutex_unlock(&devlink
->lock
);
8340 EXPORT_SYMBOL_GPL(devlink_region_destroy
);
8343 * devlink_region_snapshot_id_get - get snapshot ID
8345 * This callback should be called when adding a new snapshot,
8346 * Driver should use the same id for multiple snapshots taken
8347 * on multiple regions at the same time/by the same trigger.
8349 * The caller of this function must use devlink_region_snapshot_id_put
8350 * when finished creating regions using this id.
8352 * Returns zero on success, or a negative error code on failure.
8355 * @id: storage to return id
8357 int devlink_region_snapshot_id_get(struct devlink
*devlink
, u32
*id
)
8361 mutex_lock(&devlink
->lock
);
8362 err
= __devlink_region_snapshot_id_get(devlink
, id
);
8363 mutex_unlock(&devlink
->lock
);
8367 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get
);
8370 * devlink_region_snapshot_id_put - put snapshot ID reference
8372 * This should be called by a driver after finishing creating snapshots
8373 * with an id. Doing so ensures that the ID can later be released in the
8374 * event that all snapshots using it have been destroyed.
8377 * @id: id to release reference on
8379 void devlink_region_snapshot_id_put(struct devlink
*devlink
, u32 id
)
8381 mutex_lock(&devlink
->lock
);
8382 __devlink_snapshot_id_decrement(devlink
, id
);
8383 mutex_unlock(&devlink
->lock
);
8385 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put
);
8388 * devlink_region_snapshot_create - create a new snapshot
8389 * This will add a new snapshot of a region. The snapshot
8390 * will be stored on the region struct and can be accessed
8391 * from devlink. This is useful for future analyses of snapshots.
8392 * Multiple snapshots can be created on a region.
8393 * The @snapshot_id should be obtained using the getter function.
8395 * @region: devlink region of the snapshot
8396 * @data: snapshot data
8397 * @snapshot_id: snapshot id to be created
8399 int devlink_region_snapshot_create(struct devlink_region
*region
,
8400 u8
*data
, u32 snapshot_id
)
8402 struct devlink
*devlink
= region
->devlink
;
8405 mutex_lock(&devlink
->lock
);
8406 err
= __devlink_region_snapshot_create(region
, data
, snapshot_id
);
8407 mutex_unlock(&devlink
->lock
);
8411 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create
);
8413 #define DEVLINK_TRAP(_id, _type) \
8415 .type = DEVLINK_TRAP_TYPE_##_type, \
8416 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
8417 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
8420 static const struct devlink_trap devlink_trap_generic
[] = {
8421 DEVLINK_TRAP(SMAC_MC
, DROP
),
8422 DEVLINK_TRAP(VLAN_TAG_MISMATCH
, DROP
),
8423 DEVLINK_TRAP(INGRESS_VLAN_FILTER
, DROP
),
8424 DEVLINK_TRAP(INGRESS_STP_FILTER
, DROP
),
8425 DEVLINK_TRAP(EMPTY_TX_LIST
, DROP
),
8426 DEVLINK_TRAP(PORT_LOOPBACK_FILTER
, DROP
),
8427 DEVLINK_TRAP(BLACKHOLE_ROUTE
, DROP
),
8428 DEVLINK_TRAP(TTL_ERROR
, EXCEPTION
),
8429 DEVLINK_TRAP(TAIL_DROP
, DROP
),
8430 DEVLINK_TRAP(NON_IP_PACKET
, DROP
),
8431 DEVLINK_TRAP(UC_DIP_MC_DMAC
, DROP
),
8432 DEVLINK_TRAP(DIP_LB
, DROP
),
8433 DEVLINK_TRAP(SIP_MC
, DROP
),
8434 DEVLINK_TRAP(SIP_LB
, DROP
),
8435 DEVLINK_TRAP(CORRUPTED_IP_HDR
, DROP
),
8436 DEVLINK_TRAP(IPV4_SIP_BC
, DROP
),
8437 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE
, DROP
),
8438 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE
, DROP
),
8439 DEVLINK_TRAP(MTU_ERROR
, EXCEPTION
),
8440 DEVLINK_TRAP(UNRESOLVED_NEIGH
, EXCEPTION
),
8441 DEVLINK_TRAP(RPF
, EXCEPTION
),
8442 DEVLINK_TRAP(REJECT_ROUTE
, EXCEPTION
),
8443 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS
, EXCEPTION
),
8444 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS
, EXCEPTION
),
8445 DEVLINK_TRAP(NON_ROUTABLE
, DROP
),
8446 DEVLINK_TRAP(DECAP_ERROR
, EXCEPTION
),
8447 DEVLINK_TRAP(OVERLAY_SMAC_MC
, DROP
),
8448 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP
, DROP
),
8449 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP
, DROP
),
8452 #define DEVLINK_TRAP_GROUP(_id) \
8454 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
8455 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
8458 static const struct devlink_trap_group devlink_trap_group_generic
[] = {
8459 DEVLINK_TRAP_GROUP(L2_DROPS
),
8460 DEVLINK_TRAP_GROUP(L3_DROPS
),
8461 DEVLINK_TRAP_GROUP(BUFFER_DROPS
),
8462 DEVLINK_TRAP_GROUP(TUNNEL_DROPS
),
8463 DEVLINK_TRAP_GROUP(ACL_DROPS
),
8466 static int devlink_trap_generic_verify(const struct devlink_trap
*trap
)
8468 if (trap
->id
> DEVLINK_TRAP_GENERIC_ID_MAX
)
8471 if (strcmp(trap
->name
, devlink_trap_generic
[trap
->id
].name
))
8474 if (trap
->type
!= devlink_trap_generic
[trap
->id
].type
)
8480 static int devlink_trap_driver_verify(const struct devlink_trap
*trap
)
8484 if (trap
->id
<= DEVLINK_TRAP_GENERIC_ID_MAX
)
8487 for (i
= 0; i
< ARRAY_SIZE(devlink_trap_generic
); i
++) {
8488 if (!strcmp(trap
->name
, devlink_trap_generic
[i
].name
))
8495 static int devlink_trap_verify(const struct devlink_trap
*trap
)
8497 if (!trap
|| !trap
->name
)
8501 return devlink_trap_generic_verify(trap
);
8503 return devlink_trap_driver_verify(trap
);
8507 devlink_trap_group_generic_verify(const struct devlink_trap_group
*group
)
8509 if (group
->id
> DEVLINK_TRAP_GROUP_GENERIC_ID_MAX
)
8512 if (strcmp(group
->name
, devlink_trap_group_generic
[group
->id
].name
))
8519 devlink_trap_group_driver_verify(const struct devlink_trap_group
*group
)
8523 if (group
->id
<= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX
)
8526 for (i
= 0; i
< ARRAY_SIZE(devlink_trap_group_generic
); i
++) {
8527 if (!strcmp(group
->name
, devlink_trap_group_generic
[i
].name
))
8534 static int devlink_trap_group_verify(const struct devlink_trap_group
*group
)
8537 return devlink_trap_group_generic_verify(group
);
8539 return devlink_trap_group_driver_verify(group
);
8543 devlink_trap_group_notify(struct devlink
*devlink
,
8544 const struct devlink_trap_group_item
*group_item
,
8545 enum devlink_command cmd
)
8547 struct sk_buff
*msg
;
8550 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_GROUP_NEW
&&
8551 cmd
!= DEVLINK_CMD_TRAP_GROUP_DEL
);
8553 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
8557 err
= devlink_nl_trap_group_fill(msg
, devlink
, group_item
, cmd
, 0, 0,
8564 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
8565 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
8569 devlink_trap_item_group_link(struct devlink
*devlink
,
8570 struct devlink_trap_item
*trap_item
)
8572 u16 group_id
= trap_item
->trap
->init_group_id
;
8573 struct devlink_trap_group_item
*group_item
;
8575 group_item
= devlink_trap_group_item_lookup_by_id(devlink
, group_id
);
8576 if (WARN_ON_ONCE(!group_item
))
8579 trap_item
->group_item
= group_item
;
8584 static void devlink_trap_notify(struct devlink
*devlink
,
8585 const struct devlink_trap_item
*trap_item
,
8586 enum devlink_command cmd
)
8588 struct sk_buff
*msg
;
8591 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_NEW
&&
8592 cmd
!= DEVLINK_CMD_TRAP_DEL
);
8594 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
8598 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
, cmd
, 0, 0, 0);
8604 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
8605 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
8609 devlink_trap_register(struct devlink
*devlink
,
8610 const struct devlink_trap
*trap
, void *priv
)
8612 struct devlink_trap_item
*trap_item
;
8615 if (devlink_trap_item_lookup(devlink
, trap
->name
))
8618 trap_item
= kzalloc(sizeof(*trap_item
), GFP_KERNEL
);
8622 trap_item
->stats
= netdev_alloc_pcpu_stats(struct devlink_stats
);
8623 if (!trap_item
->stats
) {
8625 goto err_stats_alloc
;
8628 trap_item
->trap
= trap
;
8629 trap_item
->action
= trap
->init_action
;
8630 trap_item
->priv
= priv
;
8632 err
= devlink_trap_item_group_link(devlink
, trap_item
);
8634 goto err_group_link
;
8636 err
= devlink
->ops
->trap_init(devlink
, trap
, trap_item
);
8640 list_add_tail(&trap_item
->list
, &devlink
->trap_list
);
8641 devlink_trap_notify(devlink
, trap_item
, DEVLINK_CMD_TRAP_NEW
);
8647 free_percpu(trap_item
->stats
);
8653 static void devlink_trap_unregister(struct devlink
*devlink
,
8654 const struct devlink_trap
*trap
)
8656 struct devlink_trap_item
*trap_item
;
8658 trap_item
= devlink_trap_item_lookup(devlink
, trap
->name
);
8659 if (WARN_ON_ONCE(!trap_item
))
8662 devlink_trap_notify(devlink
, trap_item
, DEVLINK_CMD_TRAP_DEL
);
8663 list_del(&trap_item
->list
);
8664 if (devlink
->ops
->trap_fini
)
8665 devlink
->ops
->trap_fini(devlink
, trap
, trap_item
);
8666 free_percpu(trap_item
->stats
);
8670 static void devlink_trap_disable(struct devlink
*devlink
,
8671 const struct devlink_trap
*trap
)
8673 struct devlink_trap_item
*trap_item
;
8675 trap_item
= devlink_trap_item_lookup(devlink
, trap
->name
);
8676 if (WARN_ON_ONCE(!trap_item
))
8679 devlink
->ops
->trap_action_set(devlink
, trap
, DEVLINK_TRAP_ACTION_DROP
);
8680 trap_item
->action
= DEVLINK_TRAP_ACTION_DROP
;
8684 * devlink_traps_register - Register packet traps with devlink.
8685 * @devlink: devlink.
8686 * @traps: Packet traps.
8687 * @traps_count: Count of provided packet traps.
8688 * @priv: Driver private information.
8690 * Return: Non-zero value on failure.
8692 int devlink_traps_register(struct devlink
*devlink
,
8693 const struct devlink_trap
*traps
,
8694 size_t traps_count
, void *priv
)
8698 if (!devlink
->ops
->trap_init
|| !devlink
->ops
->trap_action_set
)
8701 mutex_lock(&devlink
->lock
);
8702 for (i
= 0; i
< traps_count
; i
++) {
8703 const struct devlink_trap
*trap
= &traps
[i
];
8705 err
= devlink_trap_verify(trap
);
8707 goto err_trap_verify
;
8709 err
= devlink_trap_register(devlink
, trap
, priv
);
8711 goto err_trap_register
;
8713 mutex_unlock(&devlink
->lock
);
8719 for (i
--; i
>= 0; i
--)
8720 devlink_trap_unregister(devlink
, &traps
[i
]);
8721 mutex_unlock(&devlink
->lock
);
8724 EXPORT_SYMBOL_GPL(devlink_traps_register
);
8727 * devlink_traps_unregister - Unregister packet traps from devlink.
8728 * @devlink: devlink.
8729 * @traps: Packet traps.
8730 * @traps_count: Count of provided packet traps.
8732 void devlink_traps_unregister(struct devlink
*devlink
,
8733 const struct devlink_trap
*traps
,
8738 mutex_lock(&devlink
->lock
);
8739 /* Make sure we do not have any packets in-flight while unregistering
8740 * traps by disabling all of them and waiting for a grace period.
8742 for (i
= traps_count
- 1; i
>= 0; i
--)
8743 devlink_trap_disable(devlink
, &traps
[i
]);
8745 for (i
= traps_count
- 1; i
>= 0; i
--)
8746 devlink_trap_unregister(devlink
, &traps
[i
]);
8747 mutex_unlock(&devlink
->lock
);
8749 EXPORT_SYMBOL_GPL(devlink_traps_unregister
);
8752 devlink_trap_stats_update(struct devlink_stats __percpu
*trap_stats
,
8755 struct devlink_stats
*stats
;
8757 stats
= this_cpu_ptr(trap_stats
);
8758 u64_stats_update_begin(&stats
->syncp
);
8759 stats
->rx_bytes
+= skb_len
;
8760 stats
->rx_packets
++;
8761 u64_stats_update_end(&stats
->syncp
);
8765 devlink_trap_report_metadata_fill(struct net_dm_hw_metadata
*hw_metadata
,
8766 const struct devlink_trap_item
*trap_item
,
8767 struct devlink_port
*in_devlink_port
,
8768 const struct flow_action_cookie
*fa_cookie
)
8770 struct devlink_trap_group_item
*group_item
= trap_item
->group_item
;
8772 hw_metadata
->trap_group_name
= group_item
->group
->name
;
8773 hw_metadata
->trap_name
= trap_item
->trap
->name
;
8774 hw_metadata
->fa_cookie
= fa_cookie
;
8776 spin_lock(&in_devlink_port
->type_lock
);
8777 if (in_devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
)
8778 hw_metadata
->input_dev
= in_devlink_port
->type_dev
;
8779 spin_unlock(&in_devlink_port
->type_lock
);
8783 * devlink_trap_report - Report trapped packet to drop monitor.
8784 * @devlink: devlink.
8785 * @skb: Trapped packet.
8786 * @trap_ctx: Trap context.
8787 * @in_devlink_port: Input devlink port.
8788 * @fa_cookie: Flow action cookie. Could be NULL.
8790 void devlink_trap_report(struct devlink
*devlink
, struct sk_buff
*skb
,
8791 void *trap_ctx
, struct devlink_port
*in_devlink_port
,
8792 const struct flow_action_cookie
*fa_cookie
)
8795 struct devlink_trap_item
*trap_item
= trap_ctx
;
8796 struct net_dm_hw_metadata hw_metadata
= {};
8798 devlink_trap_stats_update(trap_item
->stats
, skb
->len
);
8799 devlink_trap_stats_update(trap_item
->group_item
->stats
, skb
->len
);
8801 devlink_trap_report_metadata_fill(&hw_metadata
, trap_item
,
8802 in_devlink_port
, fa_cookie
);
8803 net_dm_hw_report(skb
, &hw_metadata
);
8805 EXPORT_SYMBOL_GPL(devlink_trap_report
);
8808 * devlink_trap_ctx_priv - Trap context to driver private information.
8809 * @trap_ctx: Trap context.
8811 * Return: Driver private information passed during registration.
8813 void *devlink_trap_ctx_priv(void *trap_ctx
)
8815 struct devlink_trap_item
*trap_item
= trap_ctx
;
8817 return trap_item
->priv
;
8819 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv
);
8822 devlink_trap_group_item_policer_link(struct devlink
*devlink
,
8823 struct devlink_trap_group_item
*group_item
)
8825 u32 policer_id
= group_item
->group
->init_policer_id
;
8826 struct devlink_trap_policer_item
*policer_item
;
8828 if (policer_id
== 0)
8831 policer_item
= devlink_trap_policer_item_lookup(devlink
, policer_id
);
8832 if (WARN_ON_ONCE(!policer_item
))
8835 group_item
->policer_item
= policer_item
;
8841 devlink_trap_group_register(struct devlink
*devlink
,
8842 const struct devlink_trap_group
*group
)
8844 struct devlink_trap_group_item
*group_item
;
8847 if (devlink_trap_group_item_lookup(devlink
, group
->name
))
8850 group_item
= kzalloc(sizeof(*group_item
), GFP_KERNEL
);
8854 group_item
->stats
= netdev_alloc_pcpu_stats(struct devlink_stats
);
8855 if (!group_item
->stats
) {
8857 goto err_stats_alloc
;
8860 group_item
->group
= group
;
8862 err
= devlink_trap_group_item_policer_link(devlink
, group_item
);
8864 goto err_policer_link
;
8866 if (devlink
->ops
->trap_group_init
) {
8867 err
= devlink
->ops
->trap_group_init(devlink
, group
);
8869 goto err_group_init
;
8872 list_add_tail(&group_item
->list
, &devlink
->trap_group_list
);
8873 devlink_trap_group_notify(devlink
, group_item
,
8874 DEVLINK_CMD_TRAP_GROUP_NEW
);
8880 free_percpu(group_item
->stats
);
8887 devlink_trap_group_unregister(struct devlink
*devlink
,
8888 const struct devlink_trap_group
*group
)
8890 struct devlink_trap_group_item
*group_item
;
8892 group_item
= devlink_trap_group_item_lookup(devlink
, group
->name
);
8893 if (WARN_ON_ONCE(!group_item
))
8896 devlink_trap_group_notify(devlink
, group_item
,
8897 DEVLINK_CMD_TRAP_GROUP_DEL
);
8898 list_del(&group_item
->list
);
8899 free_percpu(group_item
->stats
);
8904 * devlink_trap_groups_register - Register packet trap groups with devlink.
8905 * @devlink: devlink.
8906 * @groups: Packet trap groups.
8907 * @groups_count: Count of provided packet trap groups.
8909 * Return: Non-zero value on failure.
8911 int devlink_trap_groups_register(struct devlink
*devlink
,
8912 const struct devlink_trap_group
*groups
,
8913 size_t groups_count
)
8917 mutex_lock(&devlink
->lock
);
8918 for (i
= 0; i
< groups_count
; i
++) {
8919 const struct devlink_trap_group
*group
= &groups
[i
];
8921 err
= devlink_trap_group_verify(group
);
8923 goto err_trap_group_verify
;
8925 err
= devlink_trap_group_register(devlink
, group
);
8927 goto err_trap_group_register
;
8929 mutex_unlock(&devlink
->lock
);
8933 err_trap_group_register
:
8934 err_trap_group_verify
:
8935 for (i
--; i
>= 0; i
--)
8936 devlink_trap_group_unregister(devlink
, &groups
[i
]);
8937 mutex_unlock(&devlink
->lock
);
8940 EXPORT_SYMBOL_GPL(devlink_trap_groups_register
);
8943 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
8944 * @devlink: devlink.
8945 * @groups: Packet trap groups.
8946 * @groups_count: Count of provided packet trap groups.
8948 void devlink_trap_groups_unregister(struct devlink
*devlink
,
8949 const struct devlink_trap_group
*groups
,
8950 size_t groups_count
)
8954 mutex_lock(&devlink
->lock
);
8955 for (i
= groups_count
- 1; i
>= 0; i
--)
8956 devlink_trap_group_unregister(devlink
, &groups
[i
]);
8957 mutex_unlock(&devlink
->lock
);
8959 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister
);
8962 devlink_trap_policer_notify(struct devlink
*devlink
,
8963 const struct devlink_trap_policer_item
*policer_item
,
8964 enum devlink_command cmd
)
8966 struct sk_buff
*msg
;
8969 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_POLICER_NEW
&&
8970 cmd
!= DEVLINK_CMD_TRAP_POLICER_DEL
);
8972 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
8976 err
= devlink_nl_trap_policer_fill(msg
, devlink
, policer_item
, cmd
, 0,
8983 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
8984 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
8988 devlink_trap_policer_register(struct devlink
*devlink
,
8989 const struct devlink_trap_policer
*policer
)
8991 struct devlink_trap_policer_item
*policer_item
;
8994 if (devlink_trap_policer_item_lookup(devlink
, policer
->id
))
8997 policer_item
= kzalloc(sizeof(*policer_item
), GFP_KERNEL
);
9001 policer_item
->policer
= policer
;
9002 policer_item
->rate
= policer
->init_rate
;
9003 policer_item
->burst
= policer
->init_burst
;
9005 if (devlink
->ops
->trap_policer_init
) {
9006 err
= devlink
->ops
->trap_policer_init(devlink
, policer
);
9008 goto err_policer_init
;
9011 list_add_tail(&policer_item
->list
, &devlink
->trap_policer_list
);
9012 devlink_trap_policer_notify(devlink
, policer_item
,
9013 DEVLINK_CMD_TRAP_POLICER_NEW
);
9018 kfree(policer_item
);
9023 devlink_trap_policer_unregister(struct devlink
*devlink
,
9024 const struct devlink_trap_policer
*policer
)
9026 struct devlink_trap_policer_item
*policer_item
;
9028 policer_item
= devlink_trap_policer_item_lookup(devlink
, policer
->id
);
9029 if (WARN_ON_ONCE(!policer_item
))
9032 devlink_trap_policer_notify(devlink
, policer_item
,
9033 DEVLINK_CMD_TRAP_POLICER_DEL
);
9034 list_del(&policer_item
->list
);
9035 if (devlink
->ops
->trap_policer_fini
)
9036 devlink
->ops
->trap_policer_fini(devlink
, policer
);
9037 kfree(policer_item
);
9041 * devlink_trap_policers_register - Register packet trap policers with devlink.
9042 * @devlink: devlink.
9043 * @policers: Packet trap policers.
9044 * @policers_count: Count of provided packet trap policers.
9046 * Return: Non-zero value on failure.
9049 devlink_trap_policers_register(struct devlink
*devlink
,
9050 const struct devlink_trap_policer
*policers
,
9051 size_t policers_count
)
9055 mutex_lock(&devlink
->lock
);
9056 for (i
= 0; i
< policers_count
; i
++) {
9057 const struct devlink_trap_policer
*policer
= &policers
[i
];
9059 if (WARN_ON(policer
->id
== 0 ||
9060 policer
->max_rate
< policer
->min_rate
||
9061 policer
->max_burst
< policer
->min_burst
)) {
9063 goto err_trap_policer_verify
;
9066 err
= devlink_trap_policer_register(devlink
, policer
);
9068 goto err_trap_policer_register
;
9070 mutex_unlock(&devlink
->lock
);
9074 err_trap_policer_register
:
9075 err_trap_policer_verify
:
9076 for (i
--; i
>= 0; i
--)
9077 devlink_trap_policer_unregister(devlink
, &policers
[i
]);
9078 mutex_unlock(&devlink
->lock
);
9081 EXPORT_SYMBOL_GPL(devlink_trap_policers_register
);
9084 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
9085 * @devlink: devlink.
9086 * @policers: Packet trap policers.
9087 * @policers_count: Count of provided packet trap policers.
9090 devlink_trap_policers_unregister(struct devlink
*devlink
,
9091 const struct devlink_trap_policer
*policers
,
9092 size_t policers_count
)
9096 mutex_lock(&devlink
->lock
);
9097 for (i
= policers_count
- 1; i
>= 0; i
--)
9098 devlink_trap_policer_unregister(devlink
, &policers
[i
]);
9099 mutex_unlock(&devlink
->lock
);
9101 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister
);
9103 static void __devlink_compat_running_version(struct devlink
*devlink
,
9104 char *buf
, size_t len
)
9106 const struct nlattr
*nlattr
;
9107 struct devlink_info_req req
;
9108 struct sk_buff
*msg
;
9111 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
9116 err
= devlink
->ops
->info_get(devlink
, &req
, NULL
);
9120 nla_for_each_attr(nlattr
, (void *)msg
->data
, msg
->len
, rem
) {
9121 const struct nlattr
*kv
;
9124 if (nla_type(nlattr
) != DEVLINK_ATTR_INFO_VERSION_RUNNING
)
9127 nla_for_each_nested(kv
, nlattr
, rem_kv
) {
9128 if (nla_type(kv
) != DEVLINK_ATTR_INFO_VERSION_VALUE
)
9131 strlcat(buf
, nla_data(kv
), len
);
9132 strlcat(buf
, " ", len
);
9139 void devlink_compat_running_version(struct net_device
*dev
,
9140 char *buf
, size_t len
)
9142 struct devlink
*devlink
;
9147 devlink
= netdev_to_devlink(dev
);
9148 if (!devlink
|| !devlink
->ops
->info_get
)
9151 mutex_lock(&devlink
->lock
);
9152 __devlink_compat_running_version(devlink
, buf
, len
);
9153 mutex_unlock(&devlink
->lock
);
9160 int devlink_compat_flash_update(struct net_device
*dev
, const char *file_name
)
9162 struct devlink
*devlink
;
9168 devlink
= netdev_to_devlink(dev
);
9169 if (!devlink
|| !devlink
->ops
->flash_update
) {
9174 mutex_lock(&devlink
->lock
);
9175 ret
= devlink
->ops
->flash_update(devlink
, file_name
, NULL
, NULL
);
9176 mutex_unlock(&devlink
->lock
);
9185 int devlink_compat_phys_port_name_get(struct net_device
*dev
,
9186 char *name
, size_t len
)
9188 struct devlink_port
*devlink_port
;
9190 /* RTNL mutex is held here which ensures that devlink_port
9191 * instance cannot disappear in the middle. No need to take
9192 * any devlink lock as only permanent values are accessed.
9196 devlink_port
= netdev_to_devlink_port(dev
);
9200 return __devlink_port_phys_port_name_get(devlink_port
, name
, len
);
9203 int devlink_compat_switch_id_get(struct net_device
*dev
,
9204 struct netdev_phys_item_id
*ppid
)
9206 struct devlink_port
*devlink_port
;
9208 /* Caller must hold RTNL mutex or reference to dev, which ensures that
9209 * devlink_port instance cannot disappear in the middle. No need to take
9210 * any devlink lock as only permanent values are accessed.
9212 devlink_port
= netdev_to_devlink_port(dev
);
9213 if (!devlink_port
|| !devlink_port
->attrs
.switch_port
)
9216 memcpy(ppid
, &devlink_port
->attrs
.switch_id
, sizeof(*ppid
));
9221 static void __net_exit
devlink_pernet_pre_exit(struct net
*net
)
9223 struct devlink
*devlink
;
9226 /* In case network namespace is getting destroyed, reload
9227 * all devlink instances from this namespace into init_net.
9229 mutex_lock(&devlink_mutex
);
9230 list_for_each_entry(devlink
, &devlink_list
, list
) {
9231 if (net_eq(devlink_net(devlink
), net
)) {
9232 if (WARN_ON(!devlink_reload_supported(devlink
)))
9234 err
= devlink_reload(devlink
, &init_net
, NULL
);
9235 if (err
&& err
!= -EOPNOTSUPP
)
9236 pr_warn("Failed to reload devlink instance into init_net\n");
9239 mutex_unlock(&devlink_mutex
);
9242 static struct pernet_operations devlink_pernet_ops __net_initdata
= {
9243 .pre_exit
= devlink_pernet_pre_exit
,
9246 static int __init
devlink_init(void)
9250 err
= genl_register_family(&devlink_nl_family
);
9253 err
= register_pernet_subsys(&devlink_pernet_ops
);
9260 subsys_initcall(devlink_init
);