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 #define CREATE_TRACE_POINTS
31 #include <trace/events/devlink.h>
33 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet
[] = {
35 .name
= "destination mac",
36 .id
= DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC
,
41 struct devlink_dpipe_header devlink_dpipe_header_ethernet
= {
43 .id
= DEVLINK_DPIPE_HEADER_ETHERNET
,
44 .fields
= devlink_dpipe_fields_ethernet
,
45 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ethernet
),
48 EXPORT_SYMBOL(devlink_dpipe_header_ethernet
);
50 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4
[] = {
52 .name
= "destination ip",
53 .id
= DEVLINK_DPIPE_FIELD_IPV4_DST_IP
,
58 struct devlink_dpipe_header devlink_dpipe_header_ipv4
= {
60 .id
= DEVLINK_DPIPE_HEADER_IPV4
,
61 .fields
= devlink_dpipe_fields_ipv4
,
62 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ipv4
),
65 EXPORT_SYMBOL(devlink_dpipe_header_ipv4
);
67 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6
[] = {
69 .name
= "destination ip",
70 .id
= DEVLINK_DPIPE_FIELD_IPV6_DST_IP
,
75 struct devlink_dpipe_header devlink_dpipe_header_ipv6
= {
77 .id
= DEVLINK_DPIPE_HEADER_IPV6
,
78 .fields
= devlink_dpipe_fields_ipv6
,
79 .fields_count
= ARRAY_SIZE(devlink_dpipe_fields_ipv6
),
82 EXPORT_SYMBOL(devlink_dpipe_header_ipv6
);
84 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg
);
85 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr
);
86 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report
);
88 static const struct nla_policy devlink_function_nl_policy
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1] = {
89 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
] = { .type
= NLA_BINARY
},
92 static LIST_HEAD(devlink_list
);
96 * An overall lock guarding every operation coming from userspace.
97 * It also guards devlink devices list and it is taken when
98 * driver registers/unregisters it.
100 static DEFINE_MUTEX(devlink_mutex
);
102 struct net
*devlink_net(const struct devlink
*devlink
)
104 return read_pnet(&devlink
->_net
);
106 EXPORT_SYMBOL_GPL(devlink_net
);
108 static void __devlink_net_set(struct devlink
*devlink
, struct net
*net
)
110 write_pnet(&devlink
->_net
, net
);
113 void devlink_net_set(struct devlink
*devlink
, struct net
*net
)
115 if (WARN_ON(devlink
->registered
))
117 __devlink_net_set(devlink
, net
);
119 EXPORT_SYMBOL_GPL(devlink_net_set
);
121 static struct devlink
*devlink_get_from_attrs(struct net
*net
,
122 struct nlattr
**attrs
)
124 struct devlink
*devlink
;
128 if (!attrs
[DEVLINK_ATTR_BUS_NAME
] || !attrs
[DEVLINK_ATTR_DEV_NAME
])
129 return ERR_PTR(-EINVAL
);
131 busname
= nla_data(attrs
[DEVLINK_ATTR_BUS_NAME
]);
132 devname
= nla_data(attrs
[DEVLINK_ATTR_DEV_NAME
]);
134 lockdep_assert_held(&devlink_mutex
);
136 list_for_each_entry(devlink
, &devlink_list
, list
) {
137 if (strcmp(devlink
->dev
->bus
->name
, busname
) == 0 &&
138 strcmp(dev_name(devlink
->dev
), devname
) == 0 &&
139 net_eq(devlink_net(devlink
), net
))
143 return ERR_PTR(-ENODEV
);
146 static struct devlink
*devlink_get_from_info(struct genl_info
*info
)
148 return devlink_get_from_attrs(genl_info_net(info
), info
->attrs
);
151 static struct devlink_port
*devlink_port_get_by_index(struct devlink
*devlink
,
152 unsigned int port_index
)
154 struct devlink_port
*devlink_port
;
156 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
157 if (devlink_port
->index
== port_index
)
163 static bool devlink_port_index_exists(struct devlink
*devlink
,
164 unsigned int port_index
)
166 return devlink_port_get_by_index(devlink
, port_index
);
169 static struct devlink_port
*devlink_port_get_from_attrs(struct devlink
*devlink
,
170 struct nlattr
**attrs
)
172 if (attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
173 u32 port_index
= nla_get_u32(attrs
[DEVLINK_ATTR_PORT_INDEX
]);
174 struct devlink_port
*devlink_port
;
176 devlink_port
= devlink_port_get_by_index(devlink
, port_index
);
178 return ERR_PTR(-ENODEV
);
181 return ERR_PTR(-EINVAL
);
184 static struct devlink_port
*devlink_port_get_from_info(struct devlink
*devlink
,
185 struct genl_info
*info
)
187 return devlink_port_get_from_attrs(devlink
, info
->attrs
);
191 struct list_head list
;
194 u16 ingress_pools_count
;
195 u16 egress_pools_count
;
196 u16 ingress_tc_count
;
200 static u16
devlink_sb_pool_count(struct devlink_sb
*devlink_sb
)
202 return devlink_sb
->ingress_pools_count
+ devlink_sb
->egress_pools_count
;
205 static struct devlink_sb
*devlink_sb_get_by_index(struct devlink
*devlink
,
206 unsigned int sb_index
)
208 struct devlink_sb
*devlink_sb
;
210 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
211 if (devlink_sb
->index
== sb_index
)
217 static bool devlink_sb_index_exists(struct devlink
*devlink
,
218 unsigned int sb_index
)
220 return devlink_sb_get_by_index(devlink
, sb_index
);
223 static struct devlink_sb
*devlink_sb_get_from_attrs(struct devlink
*devlink
,
224 struct nlattr
**attrs
)
226 if (attrs
[DEVLINK_ATTR_SB_INDEX
]) {
227 u32 sb_index
= nla_get_u32(attrs
[DEVLINK_ATTR_SB_INDEX
]);
228 struct devlink_sb
*devlink_sb
;
230 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
232 return ERR_PTR(-ENODEV
);
235 return ERR_PTR(-EINVAL
);
238 static struct devlink_sb
*devlink_sb_get_from_info(struct devlink
*devlink
,
239 struct genl_info
*info
)
241 return devlink_sb_get_from_attrs(devlink
, info
->attrs
);
244 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
245 struct nlattr
**attrs
,
250 if (!attrs
[DEVLINK_ATTR_SB_POOL_INDEX
])
253 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_POOL_INDEX
]);
254 if (val
>= devlink_sb_pool_count(devlink_sb
))
260 static int devlink_sb_pool_index_get_from_info(struct devlink_sb
*devlink_sb
,
261 struct genl_info
*info
,
264 return devlink_sb_pool_index_get_from_attrs(devlink_sb
, info
->attrs
,
269 devlink_sb_pool_type_get_from_attrs(struct nlattr
**attrs
,
270 enum devlink_sb_pool_type
*p_pool_type
)
274 if (!attrs
[DEVLINK_ATTR_SB_POOL_TYPE
])
277 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_TYPE
]);
278 if (val
!= DEVLINK_SB_POOL_TYPE_INGRESS
&&
279 val
!= DEVLINK_SB_POOL_TYPE_EGRESS
)
286 devlink_sb_pool_type_get_from_info(struct genl_info
*info
,
287 enum devlink_sb_pool_type
*p_pool_type
)
289 return devlink_sb_pool_type_get_from_attrs(info
->attrs
, p_pool_type
);
293 devlink_sb_th_type_get_from_attrs(struct nlattr
**attrs
,
294 enum devlink_sb_threshold_type
*p_th_type
)
298 if (!attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
])
301 val
= nla_get_u8(attrs
[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
]);
302 if (val
!= DEVLINK_SB_THRESHOLD_TYPE_STATIC
&&
303 val
!= DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC
)
310 devlink_sb_th_type_get_from_info(struct genl_info
*info
,
311 enum devlink_sb_threshold_type
*p_th_type
)
313 return devlink_sb_th_type_get_from_attrs(info
->attrs
, p_th_type
);
317 devlink_sb_tc_index_get_from_attrs(struct devlink_sb
*devlink_sb
,
318 struct nlattr
**attrs
,
319 enum devlink_sb_pool_type pool_type
,
324 if (!attrs
[DEVLINK_ATTR_SB_TC_INDEX
])
327 val
= nla_get_u16(attrs
[DEVLINK_ATTR_SB_TC_INDEX
]);
328 if (pool_type
== DEVLINK_SB_POOL_TYPE_INGRESS
&&
329 val
>= devlink_sb
->ingress_tc_count
)
331 if (pool_type
== DEVLINK_SB_POOL_TYPE_EGRESS
&&
332 val
>= devlink_sb
->egress_tc_count
)
339 devlink_sb_tc_index_get_from_info(struct devlink_sb
*devlink_sb
,
340 struct genl_info
*info
,
341 enum devlink_sb_pool_type pool_type
,
344 return devlink_sb_tc_index_get_from_attrs(devlink_sb
, info
->attrs
,
345 pool_type
, p_tc_index
);
348 struct devlink_region
{
349 struct devlink
*devlink
;
350 struct devlink_port
*port
;
351 struct list_head list
;
353 const struct devlink_region_ops
*ops
;
354 const struct devlink_port_region_ops
*port_ops
;
356 struct list_head snapshot_list
;
362 struct devlink_snapshot
{
363 struct list_head list
;
364 struct devlink_region
*region
;
369 static struct devlink_region
*
370 devlink_region_get_by_name(struct devlink
*devlink
, const char *region_name
)
372 struct devlink_region
*region
;
374 list_for_each_entry(region
, &devlink
->region_list
, list
)
375 if (!strcmp(region
->ops
->name
, region_name
))
381 static struct devlink_region
*
382 devlink_port_region_get_by_name(struct devlink_port
*port
,
383 const char *region_name
)
385 struct devlink_region
*region
;
387 list_for_each_entry(region
, &port
->region_list
, list
)
388 if (!strcmp(region
->ops
->name
, region_name
))
394 static struct devlink_snapshot
*
395 devlink_region_snapshot_get_by_id(struct devlink_region
*region
, u32 id
)
397 struct devlink_snapshot
*snapshot
;
399 list_for_each_entry(snapshot
, ®ion
->snapshot_list
, list
)
400 if (snapshot
->id
== id
)
406 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
407 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
409 /* The per devlink instance lock is taken by default in the pre-doit
410 * operation, yet several commands do not require this. The global
411 * devlink lock is taken and protects from disruption by user-calls.
413 #define DEVLINK_NL_FLAG_NO_LOCK BIT(2)
415 static int devlink_nl_pre_doit(const struct genl_ops
*ops
,
416 struct sk_buff
*skb
, struct genl_info
*info
)
418 struct devlink_port
*devlink_port
;
419 struct devlink
*devlink
;
422 mutex_lock(&devlink_mutex
);
423 devlink
= devlink_get_from_info(info
);
424 if (IS_ERR(devlink
)) {
425 mutex_unlock(&devlink_mutex
);
426 return PTR_ERR(devlink
);
428 if (~ops
->internal_flags
& DEVLINK_NL_FLAG_NO_LOCK
)
429 mutex_lock(&devlink
->lock
);
430 info
->user_ptr
[0] = devlink
;
431 if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_PORT
) {
432 devlink_port
= devlink_port_get_from_info(devlink
, info
);
433 if (IS_ERR(devlink_port
)) {
434 err
= PTR_ERR(devlink_port
);
437 info
->user_ptr
[1] = devlink_port
;
438 } else if (ops
->internal_flags
& DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
) {
439 devlink_port
= devlink_port_get_from_info(devlink
, info
);
440 if (!IS_ERR(devlink_port
))
441 info
->user_ptr
[1] = devlink_port
;
446 if (~ops
->internal_flags
& DEVLINK_NL_FLAG_NO_LOCK
)
447 mutex_unlock(&devlink
->lock
);
448 mutex_unlock(&devlink_mutex
);
452 static void devlink_nl_post_doit(const struct genl_ops
*ops
,
453 struct sk_buff
*skb
, struct genl_info
*info
)
455 struct devlink
*devlink
;
457 devlink
= info
->user_ptr
[0];
458 if (~ops
->internal_flags
& DEVLINK_NL_FLAG_NO_LOCK
)
459 mutex_unlock(&devlink
->lock
);
460 mutex_unlock(&devlink_mutex
);
463 static struct genl_family devlink_nl_family
;
465 enum devlink_multicast_groups
{
466 DEVLINK_MCGRP_CONFIG
,
469 static const struct genl_multicast_group devlink_nl_mcgrps
[] = {
470 [DEVLINK_MCGRP_CONFIG
] = { .name
= DEVLINK_GENL_MCGRP_CONFIG_NAME
},
473 static int devlink_nl_put_handle(struct sk_buff
*msg
, struct devlink
*devlink
)
475 if (nla_put_string(msg
, DEVLINK_ATTR_BUS_NAME
, devlink
->dev
->bus
->name
))
477 if (nla_put_string(msg
, DEVLINK_ATTR_DEV_NAME
, dev_name(devlink
->dev
)))
482 struct devlink_reload_combination
{
483 enum devlink_reload_action action
;
484 enum devlink_reload_limit limit
;
487 static const struct devlink_reload_combination devlink_reload_invalid_combinations
[] = {
489 /* can't reinitialize driver with no down time */
490 .action
= DEVLINK_RELOAD_ACTION_DRIVER_REINIT
,
491 .limit
= DEVLINK_RELOAD_LIMIT_NO_RESET
,
496 devlink_reload_combination_is_invalid(enum devlink_reload_action action
,
497 enum devlink_reload_limit limit
)
501 for (i
= 0; i
< ARRAY_SIZE(devlink_reload_invalid_combinations
); i
++)
502 if (devlink_reload_invalid_combinations
[i
].action
== action
&&
503 devlink_reload_invalid_combinations
[i
].limit
== limit
)
509 devlink_reload_action_is_supported(struct devlink
*devlink
, enum devlink_reload_action action
)
511 return test_bit(action
, &devlink
->ops
->reload_actions
);
515 devlink_reload_limit_is_supported(struct devlink
*devlink
, enum devlink_reload_limit limit
)
517 return test_bit(limit
, &devlink
->ops
->reload_limits
);
520 static int devlink_reload_stat_put(struct sk_buff
*msg
,
521 enum devlink_reload_limit limit
, u32 value
)
523 struct nlattr
*reload_stats_entry
;
525 reload_stats_entry
= nla_nest_start(msg
, DEVLINK_ATTR_RELOAD_STATS_ENTRY
);
526 if (!reload_stats_entry
)
529 if (nla_put_u8(msg
, DEVLINK_ATTR_RELOAD_STATS_LIMIT
, limit
) ||
530 nla_put_u32(msg
, DEVLINK_ATTR_RELOAD_STATS_VALUE
, value
))
531 goto nla_put_failure
;
532 nla_nest_end(msg
, reload_stats_entry
);
536 nla_nest_cancel(msg
, reload_stats_entry
);
540 static int devlink_reload_stats_put(struct sk_buff
*msg
, struct devlink
*devlink
, bool is_remote
)
542 struct nlattr
*reload_stats_attr
, *act_info
, *act_stats
;
547 reload_stats_attr
= nla_nest_start(msg
, DEVLINK_ATTR_RELOAD_STATS
);
549 reload_stats_attr
= nla_nest_start(msg
, DEVLINK_ATTR_REMOTE_RELOAD_STATS
);
551 if (!reload_stats_attr
)
554 for (i
= 0; i
<= DEVLINK_RELOAD_ACTION_MAX
; i
++) {
556 !devlink_reload_action_is_supported(devlink
, i
)) ||
557 i
== DEVLINK_RELOAD_ACTION_UNSPEC
)
559 act_info
= nla_nest_start(msg
, DEVLINK_ATTR_RELOAD_ACTION_INFO
);
561 goto nla_put_failure
;
563 if (nla_put_u8(msg
, DEVLINK_ATTR_RELOAD_ACTION
, i
))
564 goto action_info_nest_cancel
;
565 act_stats
= nla_nest_start(msg
, DEVLINK_ATTR_RELOAD_ACTION_STATS
);
567 goto action_info_nest_cancel
;
569 for (j
= 0; j
<= DEVLINK_RELOAD_LIMIT_MAX
; j
++) {
570 /* Remote stats are shown even if not locally supported.
571 * Stats of actions with unspecified limit are shown
572 * though drivers don't need to register unspecified
575 if ((!is_remote
&& j
!= DEVLINK_RELOAD_LIMIT_UNSPEC
&&
576 !devlink_reload_limit_is_supported(devlink
, j
)) ||
577 devlink_reload_combination_is_invalid(i
, j
))
580 stat_idx
= j
* __DEVLINK_RELOAD_ACTION_MAX
+ i
;
582 value
= devlink
->stats
.reload_stats
[stat_idx
];
584 value
= devlink
->stats
.remote_reload_stats
[stat_idx
];
585 if (devlink_reload_stat_put(msg
, j
, value
))
586 goto action_stats_nest_cancel
;
588 nla_nest_end(msg
, act_stats
);
589 nla_nest_end(msg
, act_info
);
591 nla_nest_end(msg
, reload_stats_attr
);
594 action_stats_nest_cancel
:
595 nla_nest_cancel(msg
, act_stats
);
596 action_info_nest_cancel
:
597 nla_nest_cancel(msg
, act_info
);
599 nla_nest_cancel(msg
, reload_stats_attr
);
603 static int devlink_nl_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
604 enum devlink_command cmd
, u32 portid
,
607 struct nlattr
*dev_stats
;
610 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
614 if (devlink_nl_put_handle(msg
, devlink
))
615 goto nla_put_failure
;
616 if (nla_put_u8(msg
, DEVLINK_ATTR_RELOAD_FAILED
, devlink
->reload_failed
))
617 goto nla_put_failure
;
619 dev_stats
= nla_nest_start(msg
, DEVLINK_ATTR_DEV_STATS
);
621 goto nla_put_failure
;
623 if (devlink_reload_stats_put(msg
, devlink
, false))
624 goto dev_stats_nest_cancel
;
625 if (devlink_reload_stats_put(msg
, devlink
, true))
626 goto dev_stats_nest_cancel
;
628 nla_nest_end(msg
, dev_stats
);
629 genlmsg_end(msg
, hdr
);
632 dev_stats_nest_cancel
:
633 nla_nest_cancel(msg
, dev_stats
);
635 genlmsg_cancel(msg
, hdr
);
639 static void devlink_notify(struct devlink
*devlink
, enum devlink_command cmd
)
644 WARN_ON(cmd
!= DEVLINK_CMD_NEW
&& cmd
!= DEVLINK_CMD_DEL
);
646 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
650 err
= devlink_nl_fill(msg
, devlink
, cmd
, 0, 0, 0);
656 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
657 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
660 static int devlink_nl_port_attrs_put(struct sk_buff
*msg
,
661 struct devlink_port
*devlink_port
)
663 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
665 if (!devlink_port
->attrs_set
)
668 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_LANES
, attrs
->lanes
))
671 if (nla_put_u8(msg
, DEVLINK_ATTR_PORT_SPLITTABLE
, attrs
->splittable
))
673 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_FLAVOUR
, attrs
->flavour
))
675 switch (devlink_port
->attrs
.flavour
) {
676 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
677 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
,
678 attrs
->pci_pf
.controller
) ||
679 nla_put_u16(msg
, DEVLINK_ATTR_PORT_PCI_PF_NUMBER
, attrs
->pci_pf
.pf
))
681 if (nla_put_u8(msg
, DEVLINK_ATTR_PORT_EXTERNAL
, attrs
->pci_pf
.external
))
684 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
685 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER
,
686 attrs
->pci_vf
.controller
) ||
687 nla_put_u16(msg
, DEVLINK_ATTR_PORT_PCI_PF_NUMBER
, attrs
->pci_vf
.pf
) ||
688 nla_put_u16(msg
, DEVLINK_ATTR_PORT_PCI_VF_NUMBER
, attrs
->pci_vf
.vf
))
690 if (nla_put_u8(msg
, DEVLINK_ATTR_PORT_EXTERNAL
, attrs
->pci_vf
.external
))
693 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
694 case DEVLINK_PORT_FLAVOUR_CPU
:
695 case DEVLINK_PORT_FLAVOUR_DSA
:
696 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
697 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NUMBER
,
698 attrs
->phys
.port_number
))
702 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_GROUP
,
703 attrs
->phys
.port_number
))
705 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER
,
706 attrs
->phys
.split_subport_number
))
716 devlink_nl_port_function_attrs_put(struct sk_buff
*msg
, struct devlink_port
*port
,
717 struct netlink_ext_ack
*extack
)
719 struct devlink
*devlink
= port
->devlink
;
720 const struct devlink_ops
*ops
;
721 struct nlattr
*function_attr
;
722 bool empty_nest
= true;
725 function_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_PORT_FUNCTION
);
730 if (ops
->port_function_hw_addr_get
) {
732 u8 hw_addr
[MAX_ADDR_LEN
];
734 err
= ops
->port_function_hw_addr_get(devlink
, port
, hw_addr
, &hw_addr_len
, extack
);
735 if (err
== -EOPNOTSUPP
) {
736 /* Port function attributes are optional for a port. If port doesn't
737 * support function attribute, returning -EOPNOTSUPP is not an error.
744 err
= nla_put(msg
, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
, hw_addr_len
, hw_addr
);
751 if (err
|| empty_nest
)
752 nla_nest_cancel(msg
, function_attr
);
754 nla_nest_end(msg
, function_attr
);
758 static int devlink_nl_port_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
759 struct devlink_port
*devlink_port
,
760 enum devlink_command cmd
, u32 portid
,
762 struct netlink_ext_ack
*extack
)
766 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
770 if (devlink_nl_put_handle(msg
, devlink
))
771 goto nla_put_failure
;
772 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
773 goto nla_put_failure
;
775 /* Hold rtnl lock while accessing port's netdev attributes. */
777 spin_lock_bh(&devlink_port
->type_lock
);
778 if (nla_put_u16(msg
, DEVLINK_ATTR_PORT_TYPE
, devlink_port
->type
))
779 goto nla_put_failure_type_locked
;
780 if (devlink_port
->desired_type
!= DEVLINK_PORT_TYPE_NOTSET
&&
781 nla_put_u16(msg
, DEVLINK_ATTR_PORT_DESIRED_TYPE
,
782 devlink_port
->desired_type
))
783 goto nla_put_failure_type_locked
;
784 if (devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
) {
785 struct net
*net
= devlink_net(devlink_port
->devlink
);
786 struct net_device
*netdev
= devlink_port
->type_dev
;
788 if (netdev
&& net_eq(net
, dev_net(netdev
)) &&
789 (nla_put_u32(msg
, DEVLINK_ATTR_PORT_NETDEV_IFINDEX
,
791 nla_put_string(msg
, DEVLINK_ATTR_PORT_NETDEV_NAME
,
793 goto nla_put_failure_type_locked
;
795 if (devlink_port
->type
== DEVLINK_PORT_TYPE_IB
) {
796 struct ib_device
*ibdev
= devlink_port
->type_dev
;
799 nla_put_string(msg
, DEVLINK_ATTR_PORT_IBDEV_NAME
,
801 goto nla_put_failure_type_locked
;
803 spin_unlock_bh(&devlink_port
->type_lock
);
805 if (devlink_nl_port_attrs_put(msg
, devlink_port
))
806 goto nla_put_failure
;
807 if (devlink_nl_port_function_attrs_put(msg
, devlink_port
, extack
))
808 goto nla_put_failure
;
810 genlmsg_end(msg
, hdr
);
813 nla_put_failure_type_locked
:
814 spin_unlock_bh(&devlink_port
->type_lock
);
817 genlmsg_cancel(msg
, hdr
);
821 static void devlink_port_notify(struct devlink_port
*devlink_port
,
822 enum devlink_command cmd
)
824 struct devlink
*devlink
= devlink_port
->devlink
;
828 if (!devlink_port
->registered
)
831 WARN_ON(cmd
!= DEVLINK_CMD_PORT_NEW
&& cmd
!= DEVLINK_CMD_PORT_DEL
);
833 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
837 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
, cmd
, 0, 0, 0,
844 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
845 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
848 static int devlink_nl_cmd_get_doit(struct sk_buff
*skb
, struct genl_info
*info
)
850 struct devlink
*devlink
= info
->user_ptr
[0];
854 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
858 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
859 info
->snd_portid
, info
->snd_seq
, 0);
865 return genlmsg_reply(msg
, info
);
868 static int devlink_nl_cmd_get_dumpit(struct sk_buff
*msg
,
869 struct netlink_callback
*cb
)
871 struct devlink
*devlink
;
872 int start
= cb
->args
[0];
876 mutex_lock(&devlink_mutex
);
877 list_for_each_entry(devlink
, &devlink_list
, list
) {
878 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
884 err
= devlink_nl_fill(msg
, devlink
, DEVLINK_CMD_NEW
,
885 NETLINK_CB(cb
->skb
).portid
,
886 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
);
892 mutex_unlock(&devlink_mutex
);
898 static int devlink_nl_cmd_port_get_doit(struct sk_buff
*skb
,
899 struct genl_info
*info
)
901 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
902 struct devlink
*devlink
= devlink_port
->devlink
;
906 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
910 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
911 DEVLINK_CMD_PORT_NEW
,
912 info
->snd_portid
, info
->snd_seq
, 0,
919 return genlmsg_reply(msg
, info
);
922 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff
*msg
,
923 struct netlink_callback
*cb
)
925 struct devlink
*devlink
;
926 struct devlink_port
*devlink_port
;
927 int start
= cb
->args
[0];
931 mutex_lock(&devlink_mutex
);
932 list_for_each_entry(devlink
, &devlink_list
, list
) {
933 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
935 mutex_lock(&devlink
->lock
);
936 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
941 err
= devlink_nl_port_fill(msg
, devlink
, devlink_port
,
943 NETLINK_CB(cb
->skb
).portid
,
948 mutex_unlock(&devlink
->lock
);
953 mutex_unlock(&devlink
->lock
);
956 mutex_unlock(&devlink_mutex
);
962 static int devlink_port_type_set(struct devlink
*devlink
,
963 struct devlink_port
*devlink_port
,
964 enum devlink_port_type port_type
)
969 if (devlink
->ops
->port_type_set
) {
970 if (port_type
== devlink_port
->type
)
972 err
= devlink
->ops
->port_type_set(devlink_port
, port_type
);
975 devlink_port
->desired_type
= port_type
;
976 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
983 devlink_port_function_hw_addr_set(struct devlink
*devlink
, struct devlink_port
*port
,
984 const struct nlattr
*attr
, struct netlink_ext_ack
*extack
)
986 const struct devlink_ops
*ops
;
991 hw_addr
= nla_data(attr
);
992 hw_addr_len
= nla_len(attr
);
993 if (hw_addr_len
> MAX_ADDR_LEN
) {
994 NL_SET_ERR_MSG_MOD(extack
, "Port function hardware address too long");
997 if (port
->type
== DEVLINK_PORT_TYPE_ETH
) {
998 if (hw_addr_len
!= ETH_ALEN
) {
999 NL_SET_ERR_MSG_MOD(extack
, "Address must be 6 bytes for Ethernet device");
1002 if (!is_unicast_ether_addr(hw_addr
)) {
1003 NL_SET_ERR_MSG_MOD(extack
, "Non-unicast hardware address unsupported");
1009 if (!ops
->port_function_hw_addr_set
) {
1010 NL_SET_ERR_MSG_MOD(extack
, "Port doesn't support function attributes");
1014 err
= ops
->port_function_hw_addr_set(devlink
, port
, hw_addr
, hw_addr_len
, extack
);
1018 devlink_port_notify(port
, DEVLINK_CMD_PORT_NEW
);
1023 devlink_port_function_set(struct devlink
*devlink
, struct devlink_port
*port
,
1024 const struct nlattr
*attr
, struct netlink_ext_ack
*extack
)
1026 struct nlattr
*tb
[DEVLINK_PORT_FUNCTION_ATTR_MAX
+ 1];
1029 err
= nla_parse_nested(tb
, DEVLINK_PORT_FUNCTION_ATTR_MAX
, attr
,
1030 devlink_function_nl_policy
, extack
);
1032 NL_SET_ERR_MSG_MOD(extack
, "Fail to parse port function attributes");
1036 attr
= tb
[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR
];
1038 err
= devlink_port_function_hw_addr_set(devlink
, port
, attr
, extack
);
1043 static int devlink_nl_cmd_port_set_doit(struct sk_buff
*skb
,
1044 struct genl_info
*info
)
1046 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
1047 struct devlink
*devlink
= devlink_port
->devlink
;
1050 if (info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]) {
1051 enum devlink_port_type port_type
;
1053 port_type
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_PORT_TYPE
]);
1054 err
= devlink_port_type_set(devlink
, devlink_port
, port_type
);
1059 if (info
->attrs
[DEVLINK_ATTR_PORT_FUNCTION
]) {
1060 struct nlattr
*attr
= info
->attrs
[DEVLINK_ATTR_PORT_FUNCTION
];
1061 struct netlink_ext_ack
*extack
= info
->extack
;
1063 err
= devlink_port_function_set(devlink
, devlink_port
, attr
, extack
);
1071 static int devlink_port_split(struct devlink
*devlink
, u32 port_index
,
1072 u32 count
, struct netlink_ext_ack
*extack
)
1075 if (devlink
->ops
->port_split
)
1076 return devlink
->ops
->port_split(devlink
, port_index
, count
,
1081 static int devlink_nl_cmd_port_split_doit(struct sk_buff
*skb
,
1082 struct genl_info
*info
)
1084 struct devlink
*devlink
= info
->user_ptr
[0];
1085 struct devlink_port
*devlink_port
;
1089 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
] ||
1090 !info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
])
1093 devlink_port
= devlink_port_get_from_info(devlink
, info
);
1094 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
1095 count
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_SPLIT_COUNT
]);
1097 if (IS_ERR(devlink_port
))
1100 if (!devlink_port
->attrs
.splittable
) {
1101 /* Split ports cannot be split. */
1102 if (devlink_port
->attrs
.split
)
1103 NL_SET_ERR_MSG_MOD(info
->extack
, "Port cannot be split further");
1105 NL_SET_ERR_MSG_MOD(info
->extack
, "Port cannot be split");
1109 if (count
< 2 || !is_power_of_2(count
) || count
> devlink_port
->attrs
.lanes
) {
1110 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid split count");
1114 return devlink_port_split(devlink
, port_index
, count
, info
->extack
);
1117 static int devlink_port_unsplit(struct devlink
*devlink
, u32 port_index
,
1118 struct netlink_ext_ack
*extack
)
1121 if (devlink
->ops
->port_unsplit
)
1122 return devlink
->ops
->port_unsplit(devlink
, port_index
, extack
);
1126 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff
*skb
,
1127 struct genl_info
*info
)
1129 struct devlink
*devlink
= info
->user_ptr
[0];
1132 if (!info
->attrs
[DEVLINK_ATTR_PORT_INDEX
])
1135 port_index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
1136 return devlink_port_unsplit(devlink
, port_index
, info
->extack
);
1139 static int devlink_nl_sb_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1140 struct devlink_sb
*devlink_sb
,
1141 enum devlink_command cmd
, u32 portid
,
1146 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1150 if (devlink_nl_put_handle(msg
, devlink
))
1151 goto nla_put_failure
;
1152 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1153 goto nla_put_failure
;
1154 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_SIZE
, devlink_sb
->size
))
1155 goto nla_put_failure
;
1156 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT
,
1157 devlink_sb
->ingress_pools_count
))
1158 goto nla_put_failure
;
1159 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT
,
1160 devlink_sb
->egress_pools_count
))
1161 goto nla_put_failure
;
1162 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_INGRESS_TC_COUNT
,
1163 devlink_sb
->ingress_tc_count
))
1164 goto nla_put_failure
;
1165 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_EGRESS_TC_COUNT
,
1166 devlink_sb
->egress_tc_count
))
1167 goto nla_put_failure
;
1169 genlmsg_end(msg
, hdr
);
1173 genlmsg_cancel(msg
, hdr
);
1177 static int devlink_nl_cmd_sb_get_doit(struct sk_buff
*skb
,
1178 struct genl_info
*info
)
1180 struct devlink
*devlink
= info
->user_ptr
[0];
1181 struct devlink_sb
*devlink_sb
;
1182 struct sk_buff
*msg
;
1185 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1186 if (IS_ERR(devlink_sb
))
1187 return PTR_ERR(devlink_sb
);
1189 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1193 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
1195 info
->snd_portid
, info
->snd_seq
, 0);
1201 return genlmsg_reply(msg
, info
);
1204 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff
*msg
,
1205 struct netlink_callback
*cb
)
1207 struct devlink
*devlink
;
1208 struct devlink_sb
*devlink_sb
;
1209 int start
= cb
->args
[0];
1213 mutex_lock(&devlink_mutex
);
1214 list_for_each_entry(devlink
, &devlink_list
, list
) {
1215 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
1217 mutex_lock(&devlink
->lock
);
1218 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1223 err
= devlink_nl_sb_fill(msg
, devlink
, devlink_sb
,
1225 NETLINK_CB(cb
->skb
).portid
,
1229 mutex_unlock(&devlink
->lock
);
1234 mutex_unlock(&devlink
->lock
);
1237 mutex_unlock(&devlink_mutex
);
1243 static int devlink_nl_sb_pool_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1244 struct devlink_sb
*devlink_sb
,
1245 u16 pool_index
, enum devlink_command cmd
,
1246 u32 portid
, u32 seq
, int flags
)
1248 struct devlink_sb_pool_info pool_info
;
1252 err
= devlink
->ops
->sb_pool_get(devlink
, devlink_sb
->index
,
1253 pool_index
, &pool_info
);
1257 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1261 if (devlink_nl_put_handle(msg
, devlink
))
1262 goto nla_put_failure
;
1263 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1264 goto nla_put_failure
;
1265 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1266 goto nla_put_failure
;
1267 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_info
.pool_type
))
1268 goto nla_put_failure
;
1269 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_POOL_SIZE
, pool_info
.size
))
1270 goto nla_put_failure
;
1271 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
,
1272 pool_info
.threshold_type
))
1273 goto nla_put_failure
;
1274 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_POOL_CELL_SIZE
,
1275 pool_info
.cell_size
))
1276 goto nla_put_failure
;
1278 genlmsg_end(msg
, hdr
);
1282 genlmsg_cancel(msg
, hdr
);
1286 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff
*skb
,
1287 struct genl_info
*info
)
1289 struct devlink
*devlink
= info
->user_ptr
[0];
1290 struct devlink_sb
*devlink_sb
;
1291 struct sk_buff
*msg
;
1295 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1296 if (IS_ERR(devlink_sb
))
1297 return PTR_ERR(devlink_sb
);
1299 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1304 if (!devlink
->ops
->sb_pool_get
)
1307 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1311 err
= devlink_nl_sb_pool_fill(msg
, devlink
, devlink_sb
, pool_index
,
1312 DEVLINK_CMD_SB_POOL_NEW
,
1313 info
->snd_portid
, info
->snd_seq
, 0);
1319 return genlmsg_reply(msg
, info
);
1322 static int __sb_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
1323 struct devlink
*devlink
,
1324 struct devlink_sb
*devlink_sb
,
1325 u32 portid
, u32 seq
)
1327 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
1331 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
1332 if (*p_idx
< start
) {
1336 err
= devlink_nl_sb_pool_fill(msg
, devlink
,
1339 DEVLINK_CMD_SB_POOL_NEW
,
1340 portid
, seq
, NLM_F_MULTI
);
1348 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff
*msg
,
1349 struct netlink_callback
*cb
)
1351 struct devlink
*devlink
;
1352 struct devlink_sb
*devlink_sb
;
1353 int start
= cb
->args
[0];
1357 mutex_lock(&devlink_mutex
);
1358 list_for_each_entry(devlink
, &devlink_list
, list
) {
1359 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1360 !devlink
->ops
->sb_pool_get
)
1362 mutex_lock(&devlink
->lock
);
1363 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1364 err
= __sb_pool_get_dumpit(msg
, start
, &idx
, devlink
,
1366 NETLINK_CB(cb
->skb
).portid
,
1367 cb
->nlh
->nlmsg_seq
);
1368 if (err
== -EOPNOTSUPP
) {
1371 mutex_unlock(&devlink
->lock
);
1375 mutex_unlock(&devlink
->lock
);
1378 mutex_unlock(&devlink_mutex
);
1380 if (err
!= -EMSGSIZE
)
1387 static int devlink_sb_pool_set(struct devlink
*devlink
, unsigned int sb_index
,
1388 u16 pool_index
, u32 size
,
1389 enum devlink_sb_threshold_type threshold_type
,
1390 struct netlink_ext_ack
*extack
)
1393 const struct devlink_ops
*ops
= devlink
->ops
;
1395 if (ops
->sb_pool_set
)
1396 return ops
->sb_pool_set(devlink
, sb_index
, pool_index
,
1397 size
, threshold_type
, extack
);
1401 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff
*skb
,
1402 struct genl_info
*info
)
1404 struct devlink
*devlink
= info
->user_ptr
[0];
1405 enum devlink_sb_threshold_type threshold_type
;
1406 struct devlink_sb
*devlink_sb
;
1411 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1412 if (IS_ERR(devlink_sb
))
1413 return PTR_ERR(devlink_sb
);
1415 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1420 err
= devlink_sb_th_type_get_from_info(info
, &threshold_type
);
1424 if (!info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
])
1427 size
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_POOL_SIZE
]);
1428 return devlink_sb_pool_set(devlink
, devlink_sb
->index
,
1429 pool_index
, size
, threshold_type
,
1433 static int devlink_nl_sb_port_pool_fill(struct sk_buff
*msg
,
1434 struct devlink
*devlink
,
1435 struct devlink_port
*devlink_port
,
1436 struct devlink_sb
*devlink_sb
,
1438 enum devlink_command cmd
,
1439 u32 portid
, u32 seq
, int flags
)
1441 const struct devlink_ops
*ops
= devlink
->ops
;
1446 err
= ops
->sb_port_pool_get(devlink_port
, devlink_sb
->index
,
1447 pool_index
, &threshold
);
1451 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1455 if (devlink_nl_put_handle(msg
, devlink
))
1456 goto nla_put_failure
;
1457 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1458 goto nla_put_failure
;
1459 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1460 goto nla_put_failure
;
1461 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1462 goto nla_put_failure
;
1463 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1464 goto nla_put_failure
;
1466 if (ops
->sb_occ_port_pool_get
) {
1470 err
= ops
->sb_occ_port_pool_get(devlink_port
, devlink_sb
->index
,
1471 pool_index
, &cur
, &max
);
1472 if (err
&& err
!= -EOPNOTSUPP
)
1473 goto sb_occ_get_failure
;
1475 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1476 goto nla_put_failure
;
1477 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1478 goto nla_put_failure
;
1482 genlmsg_end(msg
, hdr
);
1488 genlmsg_cancel(msg
, hdr
);
1492 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff
*skb
,
1493 struct genl_info
*info
)
1495 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
1496 struct devlink
*devlink
= devlink_port
->devlink
;
1497 struct devlink_sb
*devlink_sb
;
1498 struct sk_buff
*msg
;
1502 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1503 if (IS_ERR(devlink_sb
))
1504 return PTR_ERR(devlink_sb
);
1506 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1511 if (!devlink
->ops
->sb_port_pool_get
)
1514 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1518 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
, devlink_port
,
1519 devlink_sb
, pool_index
,
1520 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1521 info
->snd_portid
, info
->snd_seq
, 0);
1527 return genlmsg_reply(msg
, info
);
1530 static int __sb_port_pool_get_dumpit(struct sk_buff
*msg
, int start
, int *p_idx
,
1531 struct devlink
*devlink
,
1532 struct devlink_sb
*devlink_sb
,
1533 u32 portid
, u32 seq
)
1535 struct devlink_port
*devlink_port
;
1536 u16 pool_count
= devlink_sb_pool_count(devlink_sb
);
1540 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1541 for (pool_index
= 0; pool_index
< pool_count
; pool_index
++) {
1542 if (*p_idx
< start
) {
1546 err
= devlink_nl_sb_port_pool_fill(msg
, devlink
,
1550 DEVLINK_CMD_SB_PORT_POOL_NEW
,
1561 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff
*msg
,
1562 struct netlink_callback
*cb
)
1564 struct devlink
*devlink
;
1565 struct devlink_sb
*devlink_sb
;
1566 int start
= cb
->args
[0];
1570 mutex_lock(&devlink_mutex
);
1571 list_for_each_entry(devlink
, &devlink_list
, list
) {
1572 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1573 !devlink
->ops
->sb_port_pool_get
)
1575 mutex_lock(&devlink
->lock
);
1576 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1577 err
= __sb_port_pool_get_dumpit(msg
, start
, &idx
,
1578 devlink
, devlink_sb
,
1579 NETLINK_CB(cb
->skb
).portid
,
1580 cb
->nlh
->nlmsg_seq
);
1581 if (err
== -EOPNOTSUPP
) {
1584 mutex_unlock(&devlink
->lock
);
1588 mutex_unlock(&devlink
->lock
);
1591 mutex_unlock(&devlink_mutex
);
1593 if (err
!= -EMSGSIZE
)
1600 static int devlink_sb_port_pool_set(struct devlink_port
*devlink_port
,
1601 unsigned int sb_index
, u16 pool_index
,
1603 struct netlink_ext_ack
*extack
)
1606 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1608 if (ops
->sb_port_pool_set
)
1609 return ops
->sb_port_pool_set(devlink_port
, sb_index
,
1610 pool_index
, threshold
, extack
);
1614 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff
*skb
,
1615 struct genl_info
*info
)
1617 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
1618 struct devlink
*devlink
= info
->user_ptr
[0];
1619 struct devlink_sb
*devlink_sb
;
1624 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1625 if (IS_ERR(devlink_sb
))
1626 return PTR_ERR(devlink_sb
);
1628 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1633 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1636 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1637 return devlink_sb_port_pool_set(devlink_port
, devlink_sb
->index
,
1638 pool_index
, threshold
, info
->extack
);
1642 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1643 struct devlink_port
*devlink_port
,
1644 struct devlink_sb
*devlink_sb
, u16 tc_index
,
1645 enum devlink_sb_pool_type pool_type
,
1646 enum devlink_command cmd
,
1647 u32 portid
, u32 seq
, int flags
)
1649 const struct devlink_ops
*ops
= devlink
->ops
;
1655 err
= ops
->sb_tc_pool_bind_get(devlink_port
, devlink_sb
->index
,
1656 tc_index
, pool_type
,
1657 &pool_index
, &threshold
);
1661 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1665 if (devlink_nl_put_handle(msg
, devlink
))
1666 goto nla_put_failure
;
1667 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, devlink_port
->index
))
1668 goto nla_put_failure
;
1669 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_INDEX
, devlink_sb
->index
))
1670 goto nla_put_failure
;
1671 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_TC_INDEX
, tc_index
))
1672 goto nla_put_failure
;
1673 if (nla_put_u8(msg
, DEVLINK_ATTR_SB_POOL_TYPE
, pool_type
))
1674 goto nla_put_failure
;
1675 if (nla_put_u16(msg
, DEVLINK_ATTR_SB_POOL_INDEX
, pool_index
))
1676 goto nla_put_failure
;
1677 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_THRESHOLD
, threshold
))
1678 goto nla_put_failure
;
1680 if (ops
->sb_occ_tc_port_bind_get
) {
1684 err
= ops
->sb_occ_tc_port_bind_get(devlink_port
,
1686 tc_index
, pool_type
,
1688 if (err
&& err
!= -EOPNOTSUPP
)
1691 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_CUR
, cur
))
1692 goto nla_put_failure
;
1693 if (nla_put_u32(msg
, DEVLINK_ATTR_SB_OCC_MAX
, max
))
1694 goto nla_put_failure
;
1698 genlmsg_end(msg
, hdr
);
1702 genlmsg_cancel(msg
, hdr
);
1706 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff
*skb
,
1707 struct genl_info
*info
)
1709 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
1710 struct devlink
*devlink
= devlink_port
->devlink
;
1711 struct devlink_sb
*devlink_sb
;
1712 struct sk_buff
*msg
;
1713 enum devlink_sb_pool_type pool_type
;
1717 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1718 if (IS_ERR(devlink_sb
))
1719 return PTR_ERR(devlink_sb
);
1721 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1725 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1726 pool_type
, &tc_index
);
1730 if (!devlink
->ops
->sb_tc_pool_bind_get
)
1733 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1737 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
, devlink_port
,
1738 devlink_sb
, tc_index
, pool_type
,
1739 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1747 return genlmsg_reply(msg
, info
);
1750 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1751 int start
, int *p_idx
,
1752 struct devlink
*devlink
,
1753 struct devlink_sb
*devlink_sb
,
1754 u32 portid
, u32 seq
)
1756 struct devlink_port
*devlink_port
;
1760 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
1762 tc_index
< devlink_sb
->ingress_tc_count
; tc_index
++) {
1763 if (*p_idx
< start
) {
1767 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1771 DEVLINK_SB_POOL_TYPE_INGRESS
,
1772 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1780 tc_index
< devlink_sb
->egress_tc_count
; tc_index
++) {
1781 if (*p_idx
< start
) {
1785 err
= devlink_nl_sb_tc_pool_bind_fill(msg
, devlink
,
1789 DEVLINK_SB_POOL_TYPE_EGRESS
,
1790 DEVLINK_CMD_SB_TC_POOL_BIND_NEW
,
1802 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff
*msg
,
1803 struct netlink_callback
*cb
)
1805 struct devlink
*devlink
;
1806 struct devlink_sb
*devlink_sb
;
1807 int start
= cb
->args
[0];
1811 mutex_lock(&devlink_mutex
);
1812 list_for_each_entry(devlink
, &devlink_list
, list
) {
1813 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)) ||
1814 !devlink
->ops
->sb_tc_pool_bind_get
)
1817 mutex_lock(&devlink
->lock
);
1818 list_for_each_entry(devlink_sb
, &devlink
->sb_list
, list
) {
1819 err
= __sb_tc_pool_bind_get_dumpit(msg
, start
, &idx
,
1822 NETLINK_CB(cb
->skb
).portid
,
1823 cb
->nlh
->nlmsg_seq
);
1824 if (err
== -EOPNOTSUPP
) {
1827 mutex_unlock(&devlink
->lock
);
1831 mutex_unlock(&devlink
->lock
);
1834 mutex_unlock(&devlink_mutex
);
1836 if (err
!= -EMSGSIZE
)
1843 static int devlink_sb_tc_pool_bind_set(struct devlink_port
*devlink_port
,
1844 unsigned int sb_index
, u16 tc_index
,
1845 enum devlink_sb_pool_type pool_type
,
1846 u16 pool_index
, u32 threshold
,
1847 struct netlink_ext_ack
*extack
)
1850 const struct devlink_ops
*ops
= devlink_port
->devlink
->ops
;
1852 if (ops
->sb_tc_pool_bind_set
)
1853 return ops
->sb_tc_pool_bind_set(devlink_port
, sb_index
,
1854 tc_index
, pool_type
,
1855 pool_index
, threshold
, extack
);
1859 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff
*skb
,
1860 struct genl_info
*info
)
1862 struct devlink_port
*devlink_port
= info
->user_ptr
[1];
1863 struct devlink
*devlink
= info
->user_ptr
[0];
1864 enum devlink_sb_pool_type pool_type
;
1865 struct devlink_sb
*devlink_sb
;
1871 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1872 if (IS_ERR(devlink_sb
))
1873 return PTR_ERR(devlink_sb
);
1875 err
= devlink_sb_pool_type_get_from_info(info
, &pool_type
);
1879 err
= devlink_sb_tc_index_get_from_info(devlink_sb
, info
,
1880 pool_type
, &tc_index
);
1884 err
= devlink_sb_pool_index_get_from_info(devlink_sb
, info
,
1889 if (!info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
])
1892 threshold
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_SB_THRESHOLD
]);
1893 return devlink_sb_tc_pool_bind_set(devlink_port
, devlink_sb
->index
,
1894 tc_index
, pool_type
,
1895 pool_index
, threshold
, info
->extack
);
1898 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff
*skb
,
1899 struct genl_info
*info
)
1901 struct devlink
*devlink
= info
->user_ptr
[0];
1902 const struct devlink_ops
*ops
= devlink
->ops
;
1903 struct devlink_sb
*devlink_sb
;
1905 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1906 if (IS_ERR(devlink_sb
))
1907 return PTR_ERR(devlink_sb
);
1909 if (ops
->sb_occ_snapshot
)
1910 return ops
->sb_occ_snapshot(devlink
, devlink_sb
->index
);
1914 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff
*skb
,
1915 struct genl_info
*info
)
1917 struct devlink
*devlink
= info
->user_ptr
[0];
1918 const struct devlink_ops
*ops
= devlink
->ops
;
1919 struct devlink_sb
*devlink_sb
;
1921 devlink_sb
= devlink_sb_get_from_info(devlink
, info
);
1922 if (IS_ERR(devlink_sb
))
1923 return PTR_ERR(devlink_sb
);
1925 if (ops
->sb_occ_max_clear
)
1926 return ops
->sb_occ_max_clear(devlink
, devlink_sb
->index
);
1930 static int devlink_nl_eswitch_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
1931 enum devlink_command cmd
, u32 portid
,
1934 const struct devlink_ops
*ops
= devlink
->ops
;
1935 enum devlink_eswitch_encap_mode encap_mode
;
1941 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
1945 err
= devlink_nl_put_handle(msg
, devlink
);
1947 goto nla_put_failure
;
1949 if (ops
->eswitch_mode_get
) {
1950 err
= ops
->eswitch_mode_get(devlink
, &mode
);
1952 goto nla_put_failure
;
1953 err
= nla_put_u16(msg
, DEVLINK_ATTR_ESWITCH_MODE
, mode
);
1955 goto nla_put_failure
;
1958 if (ops
->eswitch_inline_mode_get
) {
1959 err
= ops
->eswitch_inline_mode_get(devlink
, &inline_mode
);
1961 goto nla_put_failure
;
1962 err
= nla_put_u8(msg
, DEVLINK_ATTR_ESWITCH_INLINE_MODE
,
1965 goto nla_put_failure
;
1968 if (ops
->eswitch_encap_mode_get
) {
1969 err
= ops
->eswitch_encap_mode_get(devlink
, &encap_mode
);
1971 goto nla_put_failure
;
1972 err
= nla_put_u8(msg
, DEVLINK_ATTR_ESWITCH_ENCAP_MODE
, encap_mode
);
1974 goto nla_put_failure
;
1977 genlmsg_end(msg
, hdr
);
1981 genlmsg_cancel(msg
, hdr
);
1985 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff
*skb
,
1986 struct genl_info
*info
)
1988 struct devlink
*devlink
= info
->user_ptr
[0];
1989 struct sk_buff
*msg
;
1992 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1996 err
= devlink_nl_eswitch_fill(msg
, devlink
, DEVLINK_CMD_ESWITCH_GET
,
1997 info
->snd_portid
, info
->snd_seq
, 0);
2004 return genlmsg_reply(msg
, info
);
2007 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff
*skb
,
2008 struct genl_info
*info
)
2010 struct devlink
*devlink
= info
->user_ptr
[0];
2011 const struct devlink_ops
*ops
= devlink
->ops
;
2012 enum devlink_eswitch_encap_mode encap_mode
;
2017 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_MODE
]) {
2018 if (!ops
->eswitch_mode_set
)
2020 mode
= nla_get_u16(info
->attrs
[DEVLINK_ATTR_ESWITCH_MODE
]);
2021 err
= ops
->eswitch_mode_set(devlink
, mode
, info
->extack
);
2026 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]) {
2027 if (!ops
->eswitch_inline_mode_set
)
2029 inline_mode
= nla_get_u8(
2030 info
->attrs
[DEVLINK_ATTR_ESWITCH_INLINE_MODE
]);
2031 err
= ops
->eswitch_inline_mode_set(devlink
, inline_mode
,
2037 if (info
->attrs
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]) {
2038 if (!ops
->eswitch_encap_mode_set
)
2040 encap_mode
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_ESWITCH_ENCAP_MODE
]);
2041 err
= ops
->eswitch_encap_mode_set(devlink
, encap_mode
,
2050 int devlink_dpipe_match_put(struct sk_buff
*skb
,
2051 struct devlink_dpipe_match
*match
)
2053 struct devlink_dpipe_header
*header
= match
->header
;
2054 struct devlink_dpipe_field
*field
= &header
->fields
[match
->field_id
];
2055 struct nlattr
*match_attr
;
2057 match_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_MATCH
);
2061 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_MATCH_TYPE
, match
->type
) ||
2062 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_INDEX
, match
->header_index
) ||
2063 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
2064 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
2065 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
2066 goto nla_put_failure
;
2068 nla_nest_end(skb
, match_attr
);
2072 nla_nest_cancel(skb
, match_attr
);
2075 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put
);
2077 static int devlink_dpipe_matches_put(struct devlink_dpipe_table
*table
,
2078 struct sk_buff
*skb
)
2080 struct nlattr
*matches_attr
;
2082 matches_attr
= nla_nest_start_noflag(skb
,
2083 DEVLINK_ATTR_DPIPE_TABLE_MATCHES
);
2087 if (table
->table_ops
->matches_dump(table
->priv
, skb
))
2088 goto nla_put_failure
;
2090 nla_nest_end(skb
, matches_attr
);
2094 nla_nest_cancel(skb
, matches_attr
);
2098 int devlink_dpipe_action_put(struct sk_buff
*skb
,
2099 struct devlink_dpipe_action
*action
)
2101 struct devlink_dpipe_header
*header
= action
->header
;
2102 struct devlink_dpipe_field
*field
= &header
->fields
[action
->field_id
];
2103 struct nlattr
*action_attr
;
2105 action_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_ACTION
);
2109 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_ACTION_TYPE
, action
->type
) ||
2110 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_INDEX
, action
->header_index
) ||
2111 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
2112 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
2113 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
2114 goto nla_put_failure
;
2116 nla_nest_end(skb
, action_attr
);
2120 nla_nest_cancel(skb
, action_attr
);
2123 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put
);
2125 static int devlink_dpipe_actions_put(struct devlink_dpipe_table
*table
,
2126 struct sk_buff
*skb
)
2128 struct nlattr
*actions_attr
;
2130 actions_attr
= nla_nest_start_noflag(skb
,
2131 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS
);
2135 if (table
->table_ops
->actions_dump(table
->priv
, skb
))
2136 goto nla_put_failure
;
2138 nla_nest_end(skb
, actions_attr
);
2142 nla_nest_cancel(skb
, actions_attr
);
2146 static int devlink_dpipe_table_put(struct sk_buff
*skb
,
2147 struct devlink_dpipe_table
*table
)
2149 struct nlattr
*table_attr
;
2152 table_size
= table
->table_ops
->size_get(table
->priv
);
2153 table_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_TABLE
);
2157 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_TABLE_NAME
, table
->name
) ||
2158 nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_SIZE
, table_size
,
2160 goto nla_put_failure
;
2161 if (nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
,
2162 table
->counters_enabled
))
2163 goto nla_put_failure
;
2165 if (table
->resource_valid
) {
2166 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID
,
2167 table
->resource_id
, DEVLINK_ATTR_PAD
) ||
2168 nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS
,
2169 table
->resource_units
, DEVLINK_ATTR_PAD
))
2170 goto nla_put_failure
;
2172 if (devlink_dpipe_matches_put(table
, skb
))
2173 goto nla_put_failure
;
2175 if (devlink_dpipe_actions_put(table
, skb
))
2176 goto nla_put_failure
;
2178 nla_nest_end(skb
, table_attr
);
2182 nla_nest_cancel(skb
, table_attr
);
2186 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff
**pskb
,
2187 struct genl_info
*info
)
2192 err
= genlmsg_reply(*pskb
, info
);
2196 *pskb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2202 static int devlink_dpipe_tables_fill(struct genl_info
*info
,
2203 enum devlink_command cmd
, int flags
,
2204 struct list_head
*dpipe_tables
,
2205 const char *table_name
)
2207 struct devlink
*devlink
= info
->user_ptr
[0];
2208 struct devlink_dpipe_table
*table
;
2209 struct nlattr
*tables_attr
;
2210 struct sk_buff
*skb
= NULL
;
2211 struct nlmsghdr
*nlh
;
2217 table
= list_first_entry(dpipe_tables
,
2218 struct devlink_dpipe_table
, list
);
2220 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2224 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2225 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2231 if (devlink_nl_put_handle(skb
, devlink
))
2232 goto nla_put_failure
;
2233 tables_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_TABLES
);
2235 goto nla_put_failure
;
2239 list_for_each_entry_from(table
, dpipe_tables
, list
) {
2241 err
= devlink_dpipe_table_put(skb
, table
);
2249 if (!strcmp(table
->name
, table_name
)) {
2250 err
= devlink_dpipe_table_put(skb
, table
);
2258 nla_nest_end(skb
, tables_attr
);
2259 genlmsg_end(skb
, hdr
);
2264 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2265 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2267 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2273 return genlmsg_reply(skb
, info
);
2282 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff
*skb
,
2283 struct genl_info
*info
)
2285 struct devlink
*devlink
= info
->user_ptr
[0];
2286 const char *table_name
= NULL
;
2288 if (info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
2289 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2291 return devlink_dpipe_tables_fill(info
, DEVLINK_CMD_DPIPE_TABLE_GET
, 0,
2292 &devlink
->dpipe_table_list
,
2296 static int devlink_dpipe_value_put(struct sk_buff
*skb
,
2297 struct devlink_dpipe_value
*value
)
2299 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE
,
2300 value
->value_size
, value
->value
))
2303 if (nla_put(skb
, DEVLINK_ATTR_DPIPE_VALUE_MASK
,
2304 value
->value_size
, value
->mask
))
2306 if (value
->mapping_valid
)
2307 if (nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_VALUE_MAPPING
,
2308 value
->mapping_value
))
2313 static int devlink_dpipe_action_value_put(struct sk_buff
*skb
,
2314 struct devlink_dpipe_value
*value
)
2318 if (devlink_dpipe_action_put(skb
, value
->action
))
2320 if (devlink_dpipe_value_put(skb
, value
))
2325 static int devlink_dpipe_action_values_put(struct sk_buff
*skb
,
2326 struct devlink_dpipe_value
*values
,
2327 unsigned int values_count
)
2329 struct nlattr
*action_attr
;
2333 for (i
= 0; i
< values_count
; i
++) {
2334 action_attr
= nla_nest_start_noflag(skb
,
2335 DEVLINK_ATTR_DPIPE_ACTION_VALUE
);
2338 err
= devlink_dpipe_action_value_put(skb
, &values
[i
]);
2340 goto err_action_value_put
;
2341 nla_nest_end(skb
, action_attr
);
2345 err_action_value_put
:
2346 nla_nest_cancel(skb
, action_attr
);
2350 static int devlink_dpipe_match_value_put(struct sk_buff
*skb
,
2351 struct devlink_dpipe_value
*value
)
2355 if (devlink_dpipe_match_put(skb
, value
->match
))
2357 if (devlink_dpipe_value_put(skb
, value
))
2362 static int devlink_dpipe_match_values_put(struct sk_buff
*skb
,
2363 struct devlink_dpipe_value
*values
,
2364 unsigned int values_count
)
2366 struct nlattr
*match_attr
;
2370 for (i
= 0; i
< values_count
; i
++) {
2371 match_attr
= nla_nest_start_noflag(skb
,
2372 DEVLINK_ATTR_DPIPE_MATCH_VALUE
);
2375 err
= devlink_dpipe_match_value_put(skb
, &values
[i
]);
2377 goto err_match_value_put
;
2378 nla_nest_end(skb
, match_attr
);
2382 err_match_value_put
:
2383 nla_nest_cancel(skb
, match_attr
);
2387 static int devlink_dpipe_entry_put(struct sk_buff
*skb
,
2388 struct devlink_dpipe_entry
*entry
)
2390 struct nlattr
*entry_attr
, *matches_attr
, *actions_attr
;
2393 entry_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_ENTRY
);
2397 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_INDEX
, entry
->index
,
2399 goto nla_put_failure
;
2400 if (entry
->counter_valid
)
2401 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER
,
2402 entry
->counter
, DEVLINK_ATTR_PAD
))
2403 goto nla_put_failure
;
2405 matches_attr
= nla_nest_start_noflag(skb
,
2406 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES
);
2408 goto nla_put_failure
;
2410 err
= devlink_dpipe_match_values_put(skb
, entry
->match_values
,
2411 entry
->match_values_count
);
2413 nla_nest_cancel(skb
, matches_attr
);
2414 goto err_match_values_put
;
2416 nla_nest_end(skb
, matches_attr
);
2418 actions_attr
= nla_nest_start_noflag(skb
,
2419 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES
);
2421 goto nla_put_failure
;
2423 err
= devlink_dpipe_action_values_put(skb
, entry
->action_values
,
2424 entry
->action_values_count
);
2426 nla_nest_cancel(skb
, actions_attr
);
2427 goto err_action_values_put
;
2429 nla_nest_end(skb
, actions_attr
);
2431 nla_nest_end(skb
, entry_attr
);
2436 err_match_values_put
:
2437 err_action_values_put
:
2438 nla_nest_cancel(skb
, entry_attr
);
2442 static struct devlink_dpipe_table
*
2443 devlink_dpipe_table_find(struct list_head
*dpipe_tables
,
2444 const char *table_name
, struct devlink
*devlink
)
2446 struct devlink_dpipe_table
*table
;
2447 list_for_each_entry_rcu(table
, dpipe_tables
, list
,
2448 lockdep_is_held(&devlink
->lock
)) {
2449 if (!strcmp(table
->name
, table_name
))
2455 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx
*dump_ctx
)
2457 struct devlink
*devlink
;
2460 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
->skb
,
2465 dump_ctx
->hdr
= genlmsg_put(dump_ctx
->skb
,
2466 dump_ctx
->info
->snd_portid
,
2467 dump_ctx
->info
->snd_seq
,
2468 &devlink_nl_family
, NLM_F_MULTI
,
2471 goto nla_put_failure
;
2473 devlink
= dump_ctx
->info
->user_ptr
[0];
2474 if (devlink_nl_put_handle(dump_ctx
->skb
, devlink
))
2475 goto nla_put_failure
;
2476 dump_ctx
->nest
= nla_nest_start_noflag(dump_ctx
->skb
,
2477 DEVLINK_ATTR_DPIPE_ENTRIES
);
2478 if (!dump_ctx
->nest
)
2479 goto nla_put_failure
;
2483 nlmsg_free(dump_ctx
->skb
);
2486 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare
);
2488 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx
*dump_ctx
,
2489 struct devlink_dpipe_entry
*entry
)
2491 return devlink_dpipe_entry_put(dump_ctx
->skb
, entry
);
2493 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append
);
2495 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx
*dump_ctx
)
2497 nla_nest_end(dump_ctx
->skb
, dump_ctx
->nest
);
2498 genlmsg_end(dump_ctx
->skb
, dump_ctx
->hdr
);
2501 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close
);
2503 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry
*entry
)
2506 unsigned int value_count
, value_index
;
2507 struct devlink_dpipe_value
*value
;
2509 value
= entry
->action_values
;
2510 value_count
= entry
->action_values_count
;
2511 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2512 kfree(value
[value_index
].value
);
2513 kfree(value
[value_index
].mask
);
2516 value
= entry
->match_values
;
2517 value_count
= entry
->match_values_count
;
2518 for (value_index
= 0; value_index
< value_count
; value_index
++) {
2519 kfree(value
[value_index
].value
);
2520 kfree(value
[value_index
].mask
);
2523 EXPORT_SYMBOL(devlink_dpipe_entry_clear
);
2525 static int devlink_dpipe_entries_fill(struct genl_info
*info
,
2526 enum devlink_command cmd
, int flags
,
2527 struct devlink_dpipe_table
*table
)
2529 struct devlink_dpipe_dump_ctx dump_ctx
;
2530 struct nlmsghdr
*nlh
;
2533 dump_ctx
.skb
= NULL
;
2535 dump_ctx
.info
= info
;
2537 err
= table
->table_ops
->entries_dump(table
->priv
,
2538 table
->counters_enabled
,
2544 nlh
= nlmsg_put(dump_ctx
.skb
, info
->snd_portid
, info
->snd_seq
,
2545 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2547 err
= devlink_dpipe_send_and_alloc_skb(&dump_ctx
.skb
, info
);
2552 return genlmsg_reply(dump_ctx
.skb
, info
);
2555 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff
*skb
,
2556 struct genl_info
*info
)
2558 struct devlink
*devlink
= info
->user_ptr
[0];
2559 struct devlink_dpipe_table
*table
;
2560 const char *table_name
;
2562 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
])
2565 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2566 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2567 table_name
, devlink
);
2571 if (!table
->table_ops
->entries_dump
)
2574 return devlink_dpipe_entries_fill(info
, DEVLINK_CMD_DPIPE_ENTRIES_GET
,
2578 static int devlink_dpipe_fields_put(struct sk_buff
*skb
,
2579 const struct devlink_dpipe_header
*header
)
2581 struct devlink_dpipe_field
*field
;
2582 struct nlattr
*field_attr
;
2585 for (i
= 0; i
< header
->fields_count
; i
++) {
2586 field
= &header
->fields
[i
];
2587 field_attr
= nla_nest_start_noflag(skb
,
2588 DEVLINK_ATTR_DPIPE_FIELD
);
2591 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_FIELD_NAME
, field
->name
) ||
2592 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_ID
, field
->id
) ||
2593 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH
, field
->bitwidth
) ||
2594 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE
, field
->mapping_type
))
2595 goto nla_put_failure
;
2596 nla_nest_end(skb
, field_attr
);
2601 nla_nest_cancel(skb
, field_attr
);
2605 static int devlink_dpipe_header_put(struct sk_buff
*skb
,
2606 struct devlink_dpipe_header
*header
)
2608 struct nlattr
*fields_attr
, *header_attr
;
2611 header_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_HEADER
);
2615 if (nla_put_string(skb
, DEVLINK_ATTR_DPIPE_HEADER_NAME
, header
->name
) ||
2616 nla_put_u32(skb
, DEVLINK_ATTR_DPIPE_HEADER_ID
, header
->id
) ||
2617 nla_put_u8(skb
, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL
, header
->global
))
2618 goto nla_put_failure
;
2620 fields_attr
= nla_nest_start_noflag(skb
,
2621 DEVLINK_ATTR_DPIPE_HEADER_FIELDS
);
2623 goto nla_put_failure
;
2625 err
= devlink_dpipe_fields_put(skb
, header
);
2627 nla_nest_cancel(skb
, fields_attr
);
2628 goto nla_put_failure
;
2630 nla_nest_end(skb
, fields_attr
);
2631 nla_nest_end(skb
, header_attr
);
2636 nla_nest_cancel(skb
, header_attr
);
2640 static int devlink_dpipe_headers_fill(struct genl_info
*info
,
2641 enum devlink_command cmd
, int flags
,
2642 struct devlink_dpipe_headers
*
2645 struct devlink
*devlink
= info
->user_ptr
[0];
2646 struct nlattr
*headers_attr
;
2647 struct sk_buff
*skb
= NULL
;
2648 struct nlmsghdr
*nlh
;
2655 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2659 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2660 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2666 if (devlink_nl_put_handle(skb
, devlink
))
2667 goto nla_put_failure
;
2668 headers_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_DPIPE_HEADERS
);
2670 goto nla_put_failure
;
2673 for (; i
< dpipe_headers
->headers_count
; i
++) {
2674 err
= devlink_dpipe_header_put(skb
, dpipe_headers
->headers
[i
]);
2682 nla_nest_end(skb
, headers_attr
);
2683 genlmsg_end(skb
, hdr
);
2684 if (i
!= dpipe_headers
->headers_count
)
2688 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2689 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2691 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2696 return genlmsg_reply(skb
, info
);
2705 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff
*skb
,
2706 struct genl_info
*info
)
2708 struct devlink
*devlink
= info
->user_ptr
[0];
2710 if (!devlink
->dpipe_headers
)
2712 return devlink_dpipe_headers_fill(info
, DEVLINK_CMD_DPIPE_HEADERS_GET
,
2713 0, devlink
->dpipe_headers
);
2716 static int devlink_dpipe_table_counters_set(struct devlink
*devlink
,
2717 const char *table_name
,
2720 struct devlink_dpipe_table
*table
;
2722 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
2723 table_name
, devlink
);
2727 if (table
->counter_control_extern
)
2730 if (!(table
->counters_enabled
^ enable
))
2733 table
->counters_enabled
= enable
;
2734 if (table
->table_ops
->counters_set_update
)
2735 table
->table_ops
->counters_set_update(table
->priv
, enable
);
2739 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff
*skb
,
2740 struct genl_info
*info
)
2742 struct devlink
*devlink
= info
->user_ptr
[0];
2743 const char *table_name
;
2744 bool counters_enable
;
2746 if (!info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
] ||
2747 !info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
])
2750 table_name
= nla_data(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_NAME
]);
2751 counters_enable
= !!nla_get_u8(info
->attrs
[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
]);
2753 return devlink_dpipe_table_counters_set(devlink
, table_name
,
2757 static struct devlink_resource
*
2758 devlink_resource_find(struct devlink
*devlink
,
2759 struct devlink_resource
*resource
, u64 resource_id
)
2761 struct list_head
*resource_list
;
2764 resource_list
= &resource
->resource_list
;
2766 resource_list
= &devlink
->resource_list
;
2768 list_for_each_entry(resource
, resource_list
, list
) {
2769 struct devlink_resource
*child_resource
;
2771 if (resource
->id
== resource_id
)
2774 child_resource
= devlink_resource_find(devlink
, resource
,
2777 return child_resource
;
2783 devlink_resource_validate_children(struct devlink_resource
*resource
)
2785 struct devlink_resource
*child_resource
;
2786 bool size_valid
= true;
2789 if (list_empty(&resource
->resource_list
))
2792 list_for_each_entry(child_resource
, &resource
->resource_list
, list
)
2793 parts_size
+= child_resource
->size_new
;
2795 if (parts_size
> resource
->size_new
)
2798 resource
->size_valid
= size_valid
;
2802 devlink_resource_validate_size(struct devlink_resource
*resource
, u64 size
,
2803 struct netlink_ext_ack
*extack
)
2808 if (size
> resource
->size_params
.size_max
) {
2809 NL_SET_ERR_MSG_MOD(extack
, "Size larger than maximum");
2813 if (size
< resource
->size_params
.size_min
) {
2814 NL_SET_ERR_MSG_MOD(extack
, "Size smaller than minimum");
2818 div64_u64_rem(size
, resource
->size_params
.size_granularity
, &reminder
);
2820 NL_SET_ERR_MSG_MOD(extack
, "Wrong granularity");
2827 static int devlink_nl_cmd_resource_set(struct sk_buff
*skb
,
2828 struct genl_info
*info
)
2830 struct devlink
*devlink
= info
->user_ptr
[0];
2831 struct devlink_resource
*resource
;
2836 if (!info
->attrs
[DEVLINK_ATTR_RESOURCE_ID
] ||
2837 !info
->attrs
[DEVLINK_ATTR_RESOURCE_SIZE
])
2839 resource_id
= nla_get_u64(info
->attrs
[DEVLINK_ATTR_RESOURCE_ID
]);
2841 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
2845 size
= nla_get_u64(info
->attrs
[DEVLINK_ATTR_RESOURCE_SIZE
]);
2846 err
= devlink_resource_validate_size(resource
, size
, info
->extack
);
2850 resource
->size_new
= size
;
2851 devlink_resource_validate_children(resource
);
2852 if (resource
->parent
)
2853 devlink_resource_validate_children(resource
->parent
);
2858 devlink_resource_size_params_put(struct devlink_resource
*resource
,
2859 struct sk_buff
*skb
)
2861 struct devlink_resource_size_params
*size_params
;
2863 size_params
= &resource
->size_params
;
2864 if (nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_GRAN
,
2865 size_params
->size_granularity
, DEVLINK_ATTR_PAD
) ||
2866 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_MAX
,
2867 size_params
->size_max
, DEVLINK_ATTR_PAD
) ||
2868 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_MIN
,
2869 size_params
->size_min
, DEVLINK_ATTR_PAD
) ||
2870 nla_put_u8(skb
, DEVLINK_ATTR_RESOURCE_UNIT
, size_params
->unit
))
2875 static int devlink_resource_occ_put(struct devlink_resource
*resource
,
2876 struct sk_buff
*skb
)
2878 if (!resource
->occ_get
)
2880 return nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_OCC
,
2881 resource
->occ_get(resource
->occ_get_priv
),
2885 static int devlink_resource_put(struct devlink
*devlink
, struct sk_buff
*skb
,
2886 struct devlink_resource
*resource
)
2888 struct devlink_resource
*child_resource
;
2889 struct nlattr
*child_resource_attr
;
2890 struct nlattr
*resource_attr
;
2892 resource_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_RESOURCE
);
2896 if (nla_put_string(skb
, DEVLINK_ATTR_RESOURCE_NAME
, resource
->name
) ||
2897 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE
, resource
->size
,
2898 DEVLINK_ATTR_PAD
) ||
2899 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_ID
, resource
->id
,
2901 goto nla_put_failure
;
2902 if (resource
->size
!= resource
->size_new
)
2903 nla_put_u64_64bit(skb
, DEVLINK_ATTR_RESOURCE_SIZE_NEW
,
2904 resource
->size_new
, DEVLINK_ATTR_PAD
);
2905 if (devlink_resource_occ_put(resource
, skb
))
2906 goto nla_put_failure
;
2907 if (devlink_resource_size_params_put(resource
, skb
))
2908 goto nla_put_failure
;
2909 if (list_empty(&resource
->resource_list
))
2912 if (nla_put_u8(skb
, DEVLINK_ATTR_RESOURCE_SIZE_VALID
,
2913 resource
->size_valid
))
2914 goto nla_put_failure
;
2916 child_resource_attr
= nla_nest_start_noflag(skb
,
2917 DEVLINK_ATTR_RESOURCE_LIST
);
2918 if (!child_resource_attr
)
2919 goto nla_put_failure
;
2921 list_for_each_entry(child_resource
, &resource
->resource_list
, list
) {
2922 if (devlink_resource_put(devlink
, skb
, child_resource
))
2923 goto resource_put_failure
;
2926 nla_nest_end(skb
, child_resource_attr
);
2928 nla_nest_end(skb
, resource_attr
);
2931 resource_put_failure
:
2932 nla_nest_cancel(skb
, child_resource_attr
);
2934 nla_nest_cancel(skb
, resource_attr
);
2938 static int devlink_resource_fill(struct genl_info
*info
,
2939 enum devlink_command cmd
, int flags
)
2941 struct devlink
*devlink
= info
->user_ptr
[0];
2942 struct devlink_resource
*resource
;
2943 struct nlattr
*resources_attr
;
2944 struct sk_buff
*skb
= NULL
;
2945 struct nlmsghdr
*nlh
;
2951 resource
= list_first_entry(&devlink
->resource_list
,
2952 struct devlink_resource
, list
);
2954 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2958 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2959 &devlink_nl_family
, NLM_F_MULTI
, cmd
);
2965 if (devlink_nl_put_handle(skb
, devlink
))
2966 goto nla_put_failure
;
2968 resources_attr
= nla_nest_start_noflag(skb
,
2969 DEVLINK_ATTR_RESOURCE_LIST
);
2970 if (!resources_attr
)
2971 goto nla_put_failure
;
2975 list_for_each_entry_from(resource
, &devlink
->resource_list
, list
) {
2976 err
= devlink_resource_put(devlink
, skb
, resource
);
2979 goto err_resource_put
;
2985 nla_nest_end(skb
, resources_attr
);
2986 genlmsg_end(skb
, hdr
);
2990 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
2991 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
2993 err
= devlink_dpipe_send_and_alloc_skb(&skb
, info
);
2998 return genlmsg_reply(skb
, info
);
3007 static int devlink_nl_cmd_resource_dump(struct sk_buff
*skb
,
3008 struct genl_info
*info
)
3010 struct devlink
*devlink
= info
->user_ptr
[0];
3012 if (list_empty(&devlink
->resource_list
))
3015 return devlink_resource_fill(info
, DEVLINK_CMD_RESOURCE_DUMP
, 0);
3019 devlink_resources_validate(struct devlink
*devlink
,
3020 struct devlink_resource
*resource
,
3021 struct genl_info
*info
)
3023 struct list_head
*resource_list
;
3027 resource_list
= &resource
->resource_list
;
3029 resource_list
= &devlink
->resource_list
;
3031 list_for_each_entry(resource
, resource_list
, list
) {
3032 if (!resource
->size_valid
)
3034 err
= devlink_resources_validate(devlink
, resource
, info
);
3041 static struct net
*devlink_netns_get(struct sk_buff
*skb
,
3042 struct genl_info
*info
)
3044 struct nlattr
*netns_pid_attr
= info
->attrs
[DEVLINK_ATTR_NETNS_PID
];
3045 struct nlattr
*netns_fd_attr
= info
->attrs
[DEVLINK_ATTR_NETNS_FD
];
3046 struct nlattr
*netns_id_attr
= info
->attrs
[DEVLINK_ATTR_NETNS_ID
];
3049 if (!!netns_pid_attr
+ !!netns_fd_attr
+ !!netns_id_attr
> 1) {
3050 NL_SET_ERR_MSG_MOD(info
->extack
, "multiple netns identifying attributes specified");
3051 return ERR_PTR(-EINVAL
);
3054 if (netns_pid_attr
) {
3055 net
= get_net_ns_by_pid(nla_get_u32(netns_pid_attr
));
3056 } else if (netns_fd_attr
) {
3057 net
= get_net_ns_by_fd(nla_get_u32(netns_fd_attr
));
3058 } else if (netns_id_attr
) {
3059 net
= get_net_ns_by_id(sock_net(skb
->sk
),
3060 nla_get_u32(netns_id_attr
));
3062 net
= ERR_PTR(-EINVAL
);
3065 net
= ERR_PTR(-EINVAL
);
3068 NL_SET_ERR_MSG_MOD(info
->extack
, "Unknown network namespace");
3069 return ERR_PTR(-EINVAL
);
3071 if (!netlink_ns_capable(skb
, net
->user_ns
, CAP_NET_ADMIN
)) {
3073 return ERR_PTR(-EPERM
);
3078 static void devlink_param_notify(struct devlink
*devlink
,
3079 unsigned int port_index
,
3080 struct devlink_param_item
*param_item
,
3081 enum devlink_command cmd
);
3083 static void devlink_reload_netns_change(struct devlink
*devlink
,
3084 struct net
*dest_net
)
3086 struct devlink_param_item
*param_item
;
3088 /* Userspace needs to be notified about devlink objects
3089 * removed from original and entering new network namespace.
3090 * The rest of the devlink objects are re-created during
3091 * reload process so the notifications are generated separatelly.
3094 list_for_each_entry(param_item
, &devlink
->param_list
, list
)
3095 devlink_param_notify(devlink
, 0, param_item
,
3096 DEVLINK_CMD_PARAM_DEL
);
3097 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
3099 __devlink_net_set(devlink
, dest_net
);
3101 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
3102 list_for_each_entry(param_item
, &devlink
->param_list
, list
)
3103 devlink_param_notify(devlink
, 0, param_item
,
3104 DEVLINK_CMD_PARAM_NEW
);
3107 static bool devlink_reload_supported(const struct devlink_ops
*ops
)
3109 return ops
->reload_down
&& ops
->reload_up
;
3112 static void devlink_reload_failed_set(struct devlink
*devlink
,
3115 if (devlink
->reload_failed
== reload_failed
)
3117 devlink
->reload_failed
= reload_failed
;
3118 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
3121 bool devlink_is_reload_failed(const struct devlink
*devlink
)
3123 return devlink
->reload_failed
;
3125 EXPORT_SYMBOL_GPL(devlink_is_reload_failed
);
3128 __devlink_reload_stats_update(struct devlink
*devlink
, u32
*reload_stats
,
3129 enum devlink_reload_limit limit
, u32 actions_performed
)
3131 unsigned long actions
= actions_performed
;
3135 for_each_set_bit(action
, &actions
, __DEVLINK_RELOAD_ACTION_MAX
) {
3136 stat_idx
= limit
* __DEVLINK_RELOAD_ACTION_MAX
+ action
;
3137 reload_stats
[stat_idx
]++;
3139 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
3143 devlink_reload_stats_update(struct devlink
*devlink
, enum devlink_reload_limit limit
,
3144 u32 actions_performed
)
3146 __devlink_reload_stats_update(devlink
, devlink
->stats
.reload_stats
, limit
,
3151 * devlink_remote_reload_actions_performed - Update devlink on reload actions
3152 * performed which are not a direct result of devlink reload call.
3154 * This should be called by a driver after performing reload actions in case it was not
3155 * a result of devlink reload call. For example fw_activate was performed as a result
3156 * of devlink reload triggered fw_activate on another host.
3157 * The motivation for this function is to keep data on reload actions performed on this
3158 * function whether it was done due to direct devlink reload call or not.
3161 * @limit: reload limit
3162 * @actions_performed: bitmask of actions performed
3164 void devlink_remote_reload_actions_performed(struct devlink
*devlink
,
3165 enum devlink_reload_limit limit
,
3166 u32 actions_performed
)
3168 if (WARN_ON(!actions_performed
||
3169 actions_performed
& BIT(DEVLINK_RELOAD_ACTION_UNSPEC
) ||
3170 actions_performed
>= BIT(__DEVLINK_RELOAD_ACTION_MAX
) ||
3171 limit
> DEVLINK_RELOAD_LIMIT_MAX
))
3174 __devlink_reload_stats_update(devlink
, devlink
->stats
.remote_reload_stats
, limit
,
3177 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed
);
3179 static int devlink_reload(struct devlink
*devlink
, struct net
*dest_net
,
3180 enum devlink_reload_action action
, enum devlink_reload_limit limit
,
3181 u32
*actions_performed
, struct netlink_ext_ack
*extack
)
3183 u32 remote_reload_stats
[DEVLINK_RELOAD_STATS_ARRAY_SIZE
];
3186 if (!devlink
->reload_enabled
)
3189 memcpy(remote_reload_stats
, devlink
->stats
.remote_reload_stats
,
3190 sizeof(remote_reload_stats
));
3191 err
= devlink
->ops
->reload_down(devlink
, !!dest_net
, action
, limit
, extack
);
3195 if (dest_net
&& !net_eq(dest_net
, devlink_net(devlink
)))
3196 devlink_reload_netns_change(devlink
, dest_net
);
3198 err
= devlink
->ops
->reload_up(devlink
, action
, limit
, actions_performed
, extack
);
3199 devlink_reload_failed_set(devlink
, !!err
);
3203 WARN_ON(!(*actions_performed
& BIT(action
)));
3204 /* Catch driver on updating the remote action within devlink reload */
3205 WARN_ON(memcmp(remote_reload_stats
, devlink
->stats
.remote_reload_stats
,
3206 sizeof(remote_reload_stats
)));
3207 devlink_reload_stats_update(devlink
, limit
, *actions_performed
);
3212 devlink_nl_reload_actions_performed_snd(struct devlink
*devlink
, u32 actions_performed
,
3213 enum devlink_command cmd
, struct genl_info
*info
)
3215 struct sk_buff
*msg
;
3218 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3222 hdr
= genlmsg_put(msg
, info
->snd_portid
, info
->snd_seq
, &devlink_nl_family
, 0, cmd
);
3226 if (devlink_nl_put_handle(msg
, devlink
))
3227 goto nla_put_failure
;
3229 if (nla_put_bitfield32(msg
, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED
, actions_performed
,
3231 goto nla_put_failure
;
3232 genlmsg_end(msg
, hdr
);
3234 return genlmsg_reply(msg
, info
);
3237 genlmsg_cancel(msg
, hdr
);
3243 static int devlink_nl_cmd_reload(struct sk_buff
*skb
, struct genl_info
*info
)
3245 struct devlink
*devlink
= info
->user_ptr
[0];
3246 enum devlink_reload_action action
;
3247 enum devlink_reload_limit limit
;
3248 struct net
*dest_net
= NULL
;
3249 u32 actions_performed
;
3252 if (!devlink_reload_supported(devlink
->ops
))
3255 err
= devlink_resources_validate(devlink
, NULL
, info
);
3257 NL_SET_ERR_MSG_MOD(info
->extack
, "resources size validation failed");
3261 if (info
->attrs
[DEVLINK_ATTR_NETNS_PID
] ||
3262 info
->attrs
[DEVLINK_ATTR_NETNS_FD
] ||
3263 info
->attrs
[DEVLINK_ATTR_NETNS_ID
]) {
3264 dest_net
= devlink_netns_get(skb
, info
);
3265 if (IS_ERR(dest_net
))
3266 return PTR_ERR(dest_net
);
3269 if (info
->attrs
[DEVLINK_ATTR_RELOAD_ACTION
])
3270 action
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_RELOAD_ACTION
]);
3272 action
= DEVLINK_RELOAD_ACTION_DRIVER_REINIT
;
3274 if (!devlink_reload_action_is_supported(devlink
, action
)) {
3275 NL_SET_ERR_MSG_MOD(info
->extack
,
3276 "Requested reload action is not supported by the driver");
3280 limit
= DEVLINK_RELOAD_LIMIT_UNSPEC
;
3281 if (info
->attrs
[DEVLINK_ATTR_RELOAD_LIMITS
]) {
3282 struct nla_bitfield32 limits
;
3283 u32 limits_selected
;
3285 limits
= nla_get_bitfield32(info
->attrs
[DEVLINK_ATTR_RELOAD_LIMITS
]);
3286 limits_selected
= limits
.value
& limits
.selector
;
3287 if (!limits_selected
) {
3288 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid limit selected");
3291 for (limit
= 0 ; limit
<= DEVLINK_RELOAD_LIMIT_MAX
; limit
++)
3292 if (limits_selected
& BIT(limit
))
3294 /* UAPI enables multiselection, but currently it is not used */
3295 if (limits_selected
!= BIT(limit
)) {
3296 NL_SET_ERR_MSG_MOD(info
->extack
,
3297 "Multiselection of limit is not supported");
3300 if (!devlink_reload_limit_is_supported(devlink
, limit
)) {
3301 NL_SET_ERR_MSG_MOD(info
->extack
,
3302 "Requested limit is not supported by the driver");
3305 if (devlink_reload_combination_is_invalid(action
, limit
)) {
3306 NL_SET_ERR_MSG_MOD(info
->extack
,
3307 "Requested limit is invalid for this action");
3311 err
= devlink_reload(devlink
, dest_net
, action
, limit
, &actions_performed
, info
->extack
);
3318 /* For backward compatibility generate reply only if attributes used by user */
3319 if (!info
->attrs
[DEVLINK_ATTR_RELOAD_ACTION
] && !info
->attrs
[DEVLINK_ATTR_RELOAD_LIMITS
])
3322 return devlink_nl_reload_actions_performed_snd(devlink
, actions_performed
,
3323 DEVLINK_CMD_RELOAD
, info
);
3326 static int devlink_nl_flash_update_fill(struct sk_buff
*msg
,
3327 struct devlink
*devlink
,
3328 enum devlink_command cmd
,
3329 struct devlink_flash_notify
*params
)
3333 hdr
= genlmsg_put(msg
, 0, 0, &devlink_nl_family
, 0, cmd
);
3337 if (devlink_nl_put_handle(msg
, devlink
))
3338 goto nla_put_failure
;
3340 if (cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
)
3343 if (params
->status_msg
&&
3344 nla_put_string(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG
,
3345 params
->status_msg
))
3346 goto nla_put_failure
;
3347 if (params
->component
&&
3348 nla_put_string(msg
, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
,
3350 goto nla_put_failure
;
3351 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE
,
3352 params
->done
, DEVLINK_ATTR_PAD
))
3353 goto nla_put_failure
;
3354 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL
,
3355 params
->total
, DEVLINK_ATTR_PAD
))
3356 goto nla_put_failure
;
3357 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT
,
3358 params
->timeout
, DEVLINK_ATTR_PAD
))
3359 goto nla_put_failure
;
3362 genlmsg_end(msg
, hdr
);
3366 genlmsg_cancel(msg
, hdr
);
3370 static void __devlink_flash_update_notify(struct devlink
*devlink
,
3371 enum devlink_command cmd
,
3372 struct devlink_flash_notify
*params
)
3374 struct sk_buff
*msg
;
3377 WARN_ON(cmd
!= DEVLINK_CMD_FLASH_UPDATE
&&
3378 cmd
!= DEVLINK_CMD_FLASH_UPDATE_END
&&
3379 cmd
!= DEVLINK_CMD_FLASH_UPDATE_STATUS
);
3381 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3385 err
= devlink_nl_flash_update_fill(msg
, devlink
, cmd
, params
);
3389 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
3390 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
3397 static void devlink_flash_update_begin_notify(struct devlink
*devlink
)
3399 struct devlink_flash_notify params
= { 0 };
3401 __devlink_flash_update_notify(devlink
,
3402 DEVLINK_CMD_FLASH_UPDATE
,
3406 static void devlink_flash_update_end_notify(struct devlink
*devlink
)
3408 struct devlink_flash_notify params
= { 0 };
3410 __devlink_flash_update_notify(devlink
,
3411 DEVLINK_CMD_FLASH_UPDATE_END
,
3415 void devlink_flash_update_status_notify(struct devlink
*devlink
,
3416 const char *status_msg
,
3417 const char *component
,
3419 unsigned long total
)
3421 struct devlink_flash_notify params
= {
3422 .status_msg
= status_msg
,
3423 .component
= component
,
3428 __devlink_flash_update_notify(devlink
,
3429 DEVLINK_CMD_FLASH_UPDATE_STATUS
,
3432 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify
);
3434 void devlink_flash_update_timeout_notify(struct devlink
*devlink
,
3435 const char *status_msg
,
3436 const char *component
,
3437 unsigned long timeout
)
3439 struct devlink_flash_notify params
= {
3440 .status_msg
= status_msg
,
3441 .component
= component
,
3445 __devlink_flash_update_notify(devlink
,
3446 DEVLINK_CMD_FLASH_UPDATE_STATUS
,
3449 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify
);
3451 static int devlink_nl_cmd_flash_update(struct sk_buff
*skb
,
3452 struct genl_info
*info
)
3454 struct nlattr
*nla_component
, *nla_overwrite_mask
, *nla_file_name
;
3455 struct devlink_flash_update_params params
= {};
3456 struct devlink
*devlink
= info
->user_ptr
[0];
3457 const char *file_name
;
3458 u32 supported_params
;
3461 if (!devlink
->ops
->flash_update
)
3464 if (!info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
])
3467 supported_params
= devlink
->ops
->supported_flash_update_params
;
3469 nla_component
= info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
];
3470 if (nla_component
) {
3471 if (!(supported_params
& DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT
)) {
3472 NL_SET_ERR_MSG_ATTR(info
->extack
, nla_component
,
3473 "component update is not supported by this device");
3476 params
.component
= nla_data(nla_component
);
3479 nla_overwrite_mask
= info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK
];
3480 if (nla_overwrite_mask
) {
3481 struct nla_bitfield32 sections
;
3483 if (!(supported_params
& DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK
)) {
3484 NL_SET_ERR_MSG_ATTR(info
->extack
, nla_overwrite_mask
,
3485 "overwrite settings are not supported by this device");
3488 sections
= nla_get_bitfield32(nla_overwrite_mask
);
3489 params
.overwrite_mask
= sections
.value
& sections
.selector
;
3492 nla_file_name
= info
->attrs
[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
];
3493 file_name
= nla_data(nla_file_name
);
3494 ret
= request_firmware(¶ms
.fw
, file_name
, devlink
->dev
);
3496 NL_SET_ERR_MSG_ATTR(info
->extack
, nla_file_name
, "failed to locate the requested firmware file");
3500 devlink_flash_update_begin_notify(devlink
);
3501 ret
= devlink
->ops
->flash_update(devlink
, ¶ms
, info
->extack
);
3502 devlink_flash_update_end_notify(devlink
);
3504 release_firmware(params
.fw
);
3509 static const struct devlink_param devlink_param_generic
[] = {
3511 .id
= DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET
,
3512 .name
= DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME
,
3513 .type
= DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE
,
3516 .id
= DEVLINK_PARAM_GENERIC_ID_MAX_MACS
,
3517 .name
= DEVLINK_PARAM_GENERIC_MAX_MACS_NAME
,
3518 .type
= DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE
,
3521 .id
= DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV
,
3522 .name
= DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME
,
3523 .type
= DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE
,
3526 .id
= DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT
,
3527 .name
= DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME
,
3528 .type
= DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE
,
3531 .id
= DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI
,
3532 .name
= DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME
,
3533 .type
= DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE
,
3536 .id
= DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX
,
3537 .name
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME
,
3538 .type
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE
,
3541 .id
= DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN
,
3542 .name
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME
,
3543 .type
= DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE
,
3546 .id
= DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY
,
3547 .name
= DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME
,
3548 .type
= DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE
,
3551 .id
= DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE
,
3552 .name
= DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME
,
3553 .type
= DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE
,
3556 .id
= DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE
,
3557 .name
= DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME
,
3558 .type
= DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE
,
3561 .id
= DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET
,
3562 .name
= DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME
,
3563 .type
= DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE
,
3567 static int devlink_param_generic_verify(const struct devlink_param
*param
)
3569 /* verify it match generic parameter by id and name */
3570 if (param
->id
> DEVLINK_PARAM_GENERIC_ID_MAX
)
3572 if (strcmp(param
->name
, devlink_param_generic
[param
->id
].name
))
3575 WARN_ON(param
->type
!= devlink_param_generic
[param
->id
].type
);
3580 static int devlink_param_driver_verify(const struct devlink_param
*param
)
3584 if (param
->id
<= DEVLINK_PARAM_GENERIC_ID_MAX
)
3586 /* verify no such name in generic params */
3587 for (i
= 0; i
<= DEVLINK_PARAM_GENERIC_ID_MAX
; i
++)
3588 if (!strcmp(param
->name
, devlink_param_generic
[i
].name
))
3594 static struct devlink_param_item
*
3595 devlink_param_find_by_name(struct list_head
*param_list
,
3596 const char *param_name
)
3598 struct devlink_param_item
*param_item
;
3600 list_for_each_entry(param_item
, param_list
, list
)
3601 if (!strcmp(param_item
->param
->name
, param_name
))
3606 static struct devlink_param_item
*
3607 devlink_param_find_by_id(struct list_head
*param_list
, u32 param_id
)
3609 struct devlink_param_item
*param_item
;
3611 list_for_each_entry(param_item
, param_list
, list
)
3612 if (param_item
->param
->id
== param_id
)
3618 devlink_param_cmode_is_supported(const struct devlink_param
*param
,
3619 enum devlink_param_cmode cmode
)
3621 return test_bit(cmode
, ¶m
->supported_cmodes
);
3624 static int devlink_param_get(struct devlink
*devlink
,
3625 const struct devlink_param
*param
,
3626 struct devlink_param_gset_ctx
*ctx
)
3630 return param
->get(devlink
, param
->id
, ctx
);
3633 static int devlink_param_set(struct devlink
*devlink
,
3634 const struct devlink_param
*param
,
3635 struct devlink_param_gset_ctx
*ctx
)
3639 return param
->set(devlink
, param
->id
, ctx
);
3643 devlink_param_type_to_nla_type(enum devlink_param_type param_type
)
3645 switch (param_type
) {
3646 case DEVLINK_PARAM_TYPE_U8
:
3648 case DEVLINK_PARAM_TYPE_U16
:
3650 case DEVLINK_PARAM_TYPE_U32
:
3652 case DEVLINK_PARAM_TYPE_STRING
:
3654 case DEVLINK_PARAM_TYPE_BOOL
:
3662 devlink_nl_param_value_fill_one(struct sk_buff
*msg
,
3663 enum devlink_param_type type
,
3664 enum devlink_param_cmode cmode
,
3665 union devlink_param_value val
)
3667 struct nlattr
*param_value_attr
;
3669 param_value_attr
= nla_nest_start_noflag(msg
,
3670 DEVLINK_ATTR_PARAM_VALUE
);
3671 if (!param_value_attr
)
3672 goto nla_put_failure
;
3674 if (nla_put_u8(msg
, DEVLINK_ATTR_PARAM_VALUE_CMODE
, cmode
))
3675 goto value_nest_cancel
;
3678 case DEVLINK_PARAM_TYPE_U8
:
3679 if (nla_put_u8(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val
.vu8
))
3680 goto value_nest_cancel
;
3682 case DEVLINK_PARAM_TYPE_U16
:
3683 if (nla_put_u16(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val
.vu16
))
3684 goto value_nest_cancel
;
3686 case DEVLINK_PARAM_TYPE_U32
:
3687 if (nla_put_u32(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
, val
.vu32
))
3688 goto value_nest_cancel
;
3690 case DEVLINK_PARAM_TYPE_STRING
:
3691 if (nla_put_string(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
,
3693 goto value_nest_cancel
;
3695 case DEVLINK_PARAM_TYPE_BOOL
:
3697 nla_put_flag(msg
, DEVLINK_ATTR_PARAM_VALUE_DATA
))
3698 goto value_nest_cancel
;
3702 nla_nest_end(msg
, param_value_attr
);
3706 nla_nest_cancel(msg
, param_value_attr
);
3711 static int devlink_nl_param_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
3712 unsigned int port_index
,
3713 struct devlink_param_item
*param_item
,
3714 enum devlink_command cmd
,
3715 u32 portid
, u32 seq
, int flags
)
3717 union devlink_param_value param_value
[DEVLINK_PARAM_CMODE_MAX
+ 1];
3718 bool param_value_set
[DEVLINK_PARAM_CMODE_MAX
+ 1] = {};
3719 const struct devlink_param
*param
= param_item
->param
;
3720 struct devlink_param_gset_ctx ctx
;
3721 struct nlattr
*param_values_list
;
3722 struct nlattr
*param_attr
;
3728 /* Get value from driver part to driverinit configuration mode */
3729 for (i
= 0; i
<= DEVLINK_PARAM_CMODE_MAX
; i
++) {
3730 if (!devlink_param_cmode_is_supported(param
, i
))
3732 if (i
== DEVLINK_PARAM_CMODE_DRIVERINIT
) {
3733 if (!param_item
->driverinit_value_valid
)
3735 param_value
[i
] = param_item
->driverinit_value
;
3737 if (!param_item
->published
)
3740 err
= devlink_param_get(devlink
, param
, &ctx
);
3743 param_value
[i
] = ctx
.val
;
3745 param_value_set
[i
] = true;
3748 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
3752 if (devlink_nl_put_handle(msg
, devlink
))
3753 goto genlmsg_cancel
;
3755 if (cmd
== DEVLINK_CMD_PORT_PARAM_GET
||
3756 cmd
== DEVLINK_CMD_PORT_PARAM_NEW
||
3757 cmd
== DEVLINK_CMD_PORT_PARAM_DEL
)
3758 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, port_index
))
3759 goto genlmsg_cancel
;
3761 param_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_PARAM
);
3763 goto genlmsg_cancel
;
3764 if (nla_put_string(msg
, DEVLINK_ATTR_PARAM_NAME
, param
->name
))
3765 goto param_nest_cancel
;
3766 if (param
->generic
&& nla_put_flag(msg
, DEVLINK_ATTR_PARAM_GENERIC
))
3767 goto param_nest_cancel
;
3769 nla_type
= devlink_param_type_to_nla_type(param
->type
);
3771 goto param_nest_cancel
;
3772 if (nla_put_u8(msg
, DEVLINK_ATTR_PARAM_TYPE
, nla_type
))
3773 goto param_nest_cancel
;
3775 param_values_list
= nla_nest_start_noflag(msg
,
3776 DEVLINK_ATTR_PARAM_VALUES_LIST
);
3777 if (!param_values_list
)
3778 goto param_nest_cancel
;
3780 for (i
= 0; i
<= DEVLINK_PARAM_CMODE_MAX
; i
++) {
3781 if (!param_value_set
[i
])
3783 err
= devlink_nl_param_value_fill_one(msg
, param
->type
,
3786 goto values_list_nest_cancel
;
3789 nla_nest_end(msg
, param_values_list
);
3790 nla_nest_end(msg
, param_attr
);
3791 genlmsg_end(msg
, hdr
);
3794 values_list_nest_cancel
:
3795 nla_nest_end(msg
, param_values_list
);
3797 nla_nest_cancel(msg
, param_attr
);
3799 genlmsg_cancel(msg
, hdr
);
3803 static void devlink_param_notify(struct devlink
*devlink
,
3804 unsigned int port_index
,
3805 struct devlink_param_item
*param_item
,
3806 enum devlink_command cmd
)
3808 struct sk_buff
*msg
;
3811 WARN_ON(cmd
!= DEVLINK_CMD_PARAM_NEW
&& cmd
!= DEVLINK_CMD_PARAM_DEL
&&
3812 cmd
!= DEVLINK_CMD_PORT_PARAM_NEW
&&
3813 cmd
!= DEVLINK_CMD_PORT_PARAM_DEL
);
3815 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3818 err
= devlink_nl_param_fill(msg
, devlink
, port_index
, param_item
, cmd
,
3825 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
3826 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
3829 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff
*msg
,
3830 struct netlink_callback
*cb
)
3832 struct devlink_param_item
*param_item
;
3833 struct devlink
*devlink
;
3834 int start
= cb
->args
[0];
3838 mutex_lock(&devlink_mutex
);
3839 list_for_each_entry(devlink
, &devlink_list
, list
) {
3840 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
3842 mutex_lock(&devlink
->lock
);
3843 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
3848 err
= devlink_nl_param_fill(msg
, devlink
, 0, param_item
,
3849 DEVLINK_CMD_PARAM_GET
,
3850 NETLINK_CB(cb
->skb
).portid
,
3853 if (err
== -EOPNOTSUPP
) {
3856 mutex_unlock(&devlink
->lock
);
3861 mutex_unlock(&devlink
->lock
);
3864 mutex_unlock(&devlink_mutex
);
3866 if (err
!= -EMSGSIZE
)
3874 devlink_param_type_get_from_info(struct genl_info
*info
,
3875 enum devlink_param_type
*param_type
)
3877 if (!info
->attrs
[DEVLINK_ATTR_PARAM_TYPE
])
3880 switch (nla_get_u8(info
->attrs
[DEVLINK_ATTR_PARAM_TYPE
])) {
3882 *param_type
= DEVLINK_PARAM_TYPE_U8
;
3885 *param_type
= DEVLINK_PARAM_TYPE_U16
;
3888 *param_type
= DEVLINK_PARAM_TYPE_U32
;
3891 *param_type
= DEVLINK_PARAM_TYPE_STRING
;
3894 *param_type
= DEVLINK_PARAM_TYPE_BOOL
;
3904 devlink_param_value_get_from_info(const struct devlink_param
*param
,
3905 struct genl_info
*info
,
3906 union devlink_param_value
*value
)
3908 struct nlattr
*param_data
;
3911 param_data
= info
->attrs
[DEVLINK_ATTR_PARAM_VALUE_DATA
];
3913 if (param
->type
!= DEVLINK_PARAM_TYPE_BOOL
&& !param_data
)
3916 switch (param
->type
) {
3917 case DEVLINK_PARAM_TYPE_U8
:
3918 if (nla_len(param_data
) != sizeof(u8
))
3920 value
->vu8
= nla_get_u8(param_data
);
3922 case DEVLINK_PARAM_TYPE_U16
:
3923 if (nla_len(param_data
) != sizeof(u16
))
3925 value
->vu16
= nla_get_u16(param_data
);
3927 case DEVLINK_PARAM_TYPE_U32
:
3928 if (nla_len(param_data
) != sizeof(u32
))
3930 value
->vu32
= nla_get_u32(param_data
);
3932 case DEVLINK_PARAM_TYPE_STRING
:
3933 len
= strnlen(nla_data(param_data
), nla_len(param_data
));
3934 if (len
== nla_len(param_data
) ||
3935 len
>= __DEVLINK_PARAM_MAX_STRING_VALUE
)
3937 strcpy(value
->vstr
, nla_data(param_data
));
3939 case DEVLINK_PARAM_TYPE_BOOL
:
3940 if (param_data
&& nla_len(param_data
))
3942 value
->vbool
= nla_get_flag(param_data
);
3948 static struct devlink_param_item
*
3949 devlink_param_get_from_info(struct list_head
*param_list
,
3950 struct genl_info
*info
)
3954 if (!info
->attrs
[DEVLINK_ATTR_PARAM_NAME
])
3957 param_name
= nla_data(info
->attrs
[DEVLINK_ATTR_PARAM_NAME
]);
3958 return devlink_param_find_by_name(param_list
, param_name
);
3961 static int devlink_nl_cmd_param_get_doit(struct sk_buff
*skb
,
3962 struct genl_info
*info
)
3964 struct devlink
*devlink
= info
->user_ptr
[0];
3965 struct devlink_param_item
*param_item
;
3966 struct sk_buff
*msg
;
3969 param_item
= devlink_param_get_from_info(&devlink
->param_list
, info
);
3973 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3977 err
= devlink_nl_param_fill(msg
, devlink
, 0, param_item
,
3978 DEVLINK_CMD_PARAM_GET
,
3979 info
->snd_portid
, info
->snd_seq
, 0);
3985 return genlmsg_reply(msg
, info
);
3988 static int __devlink_nl_cmd_param_set_doit(struct devlink
*devlink
,
3989 unsigned int port_index
,
3990 struct list_head
*param_list
,
3991 struct genl_info
*info
,
3992 enum devlink_command cmd
)
3994 enum devlink_param_type param_type
;
3995 struct devlink_param_gset_ctx ctx
;
3996 enum devlink_param_cmode cmode
;
3997 struct devlink_param_item
*param_item
;
3998 const struct devlink_param
*param
;
3999 union devlink_param_value value
;
4002 param_item
= devlink_param_get_from_info(param_list
, info
);
4005 param
= param_item
->param
;
4006 err
= devlink_param_type_get_from_info(info
, ¶m_type
);
4009 if (param_type
!= param
->type
)
4011 err
= devlink_param_value_get_from_info(param
, info
, &value
);
4014 if (param
->validate
) {
4015 err
= param
->validate(devlink
, param
->id
, value
, info
->extack
);
4020 if (!info
->attrs
[DEVLINK_ATTR_PARAM_VALUE_CMODE
])
4022 cmode
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_PARAM_VALUE_CMODE
]);
4023 if (!devlink_param_cmode_is_supported(param
, cmode
))
4026 if (cmode
== DEVLINK_PARAM_CMODE_DRIVERINIT
) {
4027 if (param
->type
== DEVLINK_PARAM_TYPE_STRING
)
4028 strcpy(param_item
->driverinit_value
.vstr
, value
.vstr
);
4030 param_item
->driverinit_value
= value
;
4031 param_item
->driverinit_value_valid
= true;
4037 err
= devlink_param_set(devlink
, param
, &ctx
);
4042 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
4046 static int devlink_nl_cmd_param_set_doit(struct sk_buff
*skb
,
4047 struct genl_info
*info
)
4049 struct devlink
*devlink
= info
->user_ptr
[0];
4051 return __devlink_nl_cmd_param_set_doit(devlink
, 0, &devlink
->param_list
,
4052 info
, DEVLINK_CMD_PARAM_NEW
);
4055 static int devlink_param_register_one(struct devlink
*devlink
,
4056 unsigned int port_index
,
4057 struct list_head
*param_list
,
4058 const struct devlink_param
*param
,
4059 enum devlink_command cmd
)
4061 struct devlink_param_item
*param_item
;
4063 if (devlink_param_find_by_name(param_list
, param
->name
))
4066 if (param
->supported_cmodes
== BIT(DEVLINK_PARAM_CMODE_DRIVERINIT
))
4067 WARN_ON(param
->get
|| param
->set
);
4069 WARN_ON(!param
->get
|| !param
->set
);
4071 param_item
= kzalloc(sizeof(*param_item
), GFP_KERNEL
);
4074 param_item
->param
= param
;
4076 list_add_tail(¶m_item
->list
, param_list
);
4077 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
4081 static void devlink_param_unregister_one(struct devlink
*devlink
,
4082 unsigned int port_index
,
4083 struct list_head
*param_list
,
4084 const struct devlink_param
*param
,
4085 enum devlink_command cmd
)
4087 struct devlink_param_item
*param_item
;
4089 param_item
= devlink_param_find_by_name(param_list
, param
->name
);
4090 WARN_ON(!param_item
);
4091 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
4092 list_del(¶m_item
->list
);
4096 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff
*msg
,
4097 struct netlink_callback
*cb
)
4099 struct devlink_param_item
*param_item
;
4100 struct devlink_port
*devlink_port
;
4101 struct devlink
*devlink
;
4102 int start
= cb
->args
[0];
4106 mutex_lock(&devlink_mutex
);
4107 list_for_each_entry(devlink
, &devlink_list
, list
) {
4108 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
4110 mutex_lock(&devlink
->lock
);
4111 list_for_each_entry(devlink_port
, &devlink
->port_list
, list
) {
4112 list_for_each_entry(param_item
,
4113 &devlink_port
->param_list
, list
) {
4118 err
= devlink_nl_param_fill(msg
,
4119 devlink_port
->devlink
,
4120 devlink_port
->index
, param_item
,
4121 DEVLINK_CMD_PORT_PARAM_GET
,
4122 NETLINK_CB(cb
->skb
).portid
,
4125 if (err
== -EOPNOTSUPP
) {
4128 mutex_unlock(&devlink
->lock
);
4134 mutex_unlock(&devlink
->lock
);
4137 mutex_unlock(&devlink_mutex
);
4139 if (err
!= -EMSGSIZE
)
4146 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff
*skb
,
4147 struct genl_info
*info
)
4149 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
4150 struct devlink_param_item
*param_item
;
4151 struct sk_buff
*msg
;
4154 param_item
= devlink_param_get_from_info(&devlink_port
->param_list
,
4159 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
4163 err
= devlink_nl_param_fill(msg
, devlink_port
->devlink
,
4164 devlink_port
->index
, param_item
,
4165 DEVLINK_CMD_PORT_PARAM_GET
,
4166 info
->snd_portid
, info
->snd_seq
, 0);
4172 return genlmsg_reply(msg
, info
);
4175 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff
*skb
,
4176 struct genl_info
*info
)
4178 struct devlink_port
*devlink_port
= info
->user_ptr
[0];
4180 return __devlink_nl_cmd_param_set_doit(devlink_port
->devlink
,
4181 devlink_port
->index
,
4182 &devlink_port
->param_list
, info
,
4183 DEVLINK_CMD_PORT_PARAM_NEW
);
4186 static int devlink_nl_region_snapshot_id_put(struct sk_buff
*msg
,
4187 struct devlink
*devlink
,
4188 struct devlink_snapshot
*snapshot
)
4190 struct nlattr
*snap_attr
;
4193 snap_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_REGION_SNAPSHOT
);
4197 err
= nla_put_u32(msg
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
, snapshot
->id
);
4199 goto nla_put_failure
;
4201 nla_nest_end(msg
, snap_attr
);
4205 nla_nest_cancel(msg
, snap_attr
);
4209 static int devlink_nl_region_snapshots_id_put(struct sk_buff
*msg
,
4210 struct devlink
*devlink
,
4211 struct devlink_region
*region
)
4213 struct devlink_snapshot
*snapshot
;
4214 struct nlattr
*snapshots_attr
;
4217 snapshots_attr
= nla_nest_start_noflag(msg
,
4218 DEVLINK_ATTR_REGION_SNAPSHOTS
);
4219 if (!snapshots_attr
)
4222 list_for_each_entry(snapshot
, ®ion
->snapshot_list
, list
) {
4223 err
= devlink_nl_region_snapshot_id_put(msg
, devlink
, snapshot
);
4225 goto nla_put_failure
;
4228 nla_nest_end(msg
, snapshots_attr
);
4232 nla_nest_cancel(msg
, snapshots_attr
);
4236 static int devlink_nl_region_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
4237 enum devlink_command cmd
, u32 portid
,
4239 struct devlink_region
*region
)
4244 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
4248 err
= devlink_nl_put_handle(msg
, devlink
);
4250 goto nla_put_failure
;
4253 err
= nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
,
4254 region
->port
->index
);
4256 goto nla_put_failure
;
4259 err
= nla_put_string(msg
, DEVLINK_ATTR_REGION_NAME
, region
->ops
->name
);
4261 goto nla_put_failure
;
4263 err
= nla_put_u64_64bit(msg
, DEVLINK_ATTR_REGION_SIZE
,
4267 goto nla_put_failure
;
4269 err
= devlink_nl_region_snapshots_id_put(msg
, devlink
, region
);
4271 goto nla_put_failure
;
4273 genlmsg_end(msg
, hdr
);
4277 genlmsg_cancel(msg
, hdr
);
4281 static struct sk_buff
*
4282 devlink_nl_region_notify_build(struct devlink_region
*region
,
4283 struct devlink_snapshot
*snapshot
,
4284 enum devlink_command cmd
, u32 portid
, u32 seq
)
4286 struct devlink
*devlink
= region
->devlink
;
4287 struct sk_buff
*msg
;
4292 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
4294 return ERR_PTR(-ENOMEM
);
4296 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, 0, cmd
);
4302 err
= devlink_nl_put_handle(msg
, devlink
);
4304 goto out_cancel_msg
;
4307 err
= nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
,
4308 region
->port
->index
);
4310 goto out_cancel_msg
;
4313 err
= nla_put_string(msg
, DEVLINK_ATTR_REGION_NAME
,
4316 goto out_cancel_msg
;
4319 err
= nla_put_u32(msg
, DEVLINK_ATTR_REGION_SNAPSHOT_ID
,
4322 goto out_cancel_msg
;
4324 err
= nla_put_u64_64bit(msg
, DEVLINK_ATTR_REGION_SIZE
,
4325 region
->size
, DEVLINK_ATTR_PAD
);
4327 goto out_cancel_msg
;
4329 genlmsg_end(msg
, hdr
);
4334 genlmsg_cancel(msg
, hdr
);
4337 return ERR_PTR(err
);
4340 static void devlink_nl_region_notify(struct devlink_region
*region
,
4341 struct devlink_snapshot
*snapshot
,
4342 enum devlink_command cmd
)
4344 struct devlink
*devlink
= region
->devlink
;
4345 struct sk_buff
*msg
;
4347 WARN_ON(cmd
!= DEVLINK_CMD_REGION_NEW
&& cmd
!= DEVLINK_CMD_REGION_DEL
);
4349 msg
= devlink_nl_region_notify_build(region
, snapshot
, cmd
, 0, 0);
4353 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
4354 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
4358 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4359 * @devlink: devlink instance
4360 * @id: the snapshot id
4362 * Track when a new snapshot begins using an id. Load the count for the
4363 * given id from the snapshot xarray, increment it, and store it back.
4365 * Called when a new snapshot is created with the given id.
4367 * The id *must* have been previously allocated by
4368 * devlink_region_snapshot_id_get().
4370 * Returns 0 on success, or an error on failure.
4372 static int __devlink_snapshot_id_increment(struct devlink
*devlink
, u32 id
)
4374 unsigned long count
;
4377 lockdep_assert_held(&devlink
->lock
);
4379 p
= xa_load(&devlink
->snapshot_ids
, id
);
4383 if (WARN_ON(!xa_is_value(p
)))
4386 count
= xa_to_value(p
);
4389 return xa_err(xa_store(&devlink
->snapshot_ids
, id
, xa_mk_value(count
),
4394 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4395 * @devlink: devlink instance
4396 * @id: the snapshot id
4398 * Track when a snapshot is deleted and stops using an id. Load the count
4399 * for the given id from the snapshot xarray, decrement it, and store it
4402 * If the count reaches zero, erase this id from the xarray, freeing it
4403 * up for future re-use by devlink_region_snapshot_id_get().
4405 * Called when a snapshot using the given id is deleted, and when the
4406 * initial allocator of the id is finished using it.
4408 static void __devlink_snapshot_id_decrement(struct devlink
*devlink
, u32 id
)
4410 unsigned long count
;
4413 lockdep_assert_held(&devlink
->lock
);
4415 p
= xa_load(&devlink
->snapshot_ids
, id
);
4419 if (WARN_ON(!xa_is_value(p
)))
4422 count
= xa_to_value(p
);
4426 xa_store(&devlink
->snapshot_ids
, id
, xa_mk_value(count
),
4429 /* If this was the last user, we can erase this id */
4430 xa_erase(&devlink
->snapshot_ids
, id
);
4435 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4436 * @devlink: devlink instance
4437 * @id: the snapshot id
4439 * Mark the given snapshot id as used by inserting a zero value into the
4442 * This must be called while holding the devlink instance lock. Unlike
4443 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4444 * It is expected that the id will immediately be used before
4445 * releasing the devlink instance lock.
4447 * Returns zero on success, or an error code if the snapshot id could not
4450 static int __devlink_snapshot_id_insert(struct devlink
*devlink
, u32 id
)
4452 lockdep_assert_held(&devlink
->lock
);
4454 if (xa_load(&devlink
->snapshot_ids
, id
))
4457 return xa_err(xa_store(&devlink
->snapshot_ids
, id
, xa_mk_value(0),
4462 * __devlink_region_snapshot_id_get - get snapshot ID
4463 * @devlink: devlink instance
4464 * @id: storage to return snapshot id
4466 * Allocates a new snapshot id. Returns zero on success, or a negative
4467 * error on failure. Must be called while holding the devlink instance
4470 * Snapshot IDs are tracked using an xarray which stores the number of
4471 * users of the snapshot id.
4473 * Note that the caller of this function counts as a 'user', in order to
4474 * avoid race conditions. The caller must release its hold on the
4475 * snapshot by using devlink_region_snapshot_id_put.
4477 static int __devlink_region_snapshot_id_get(struct devlink
*devlink
, u32
*id
)
4479 lockdep_assert_held(&devlink
->lock
);
4481 return xa_alloc(&devlink
->snapshot_ids
, id
, xa_mk_value(1),
4482 xa_limit_32b
, GFP_KERNEL
);
4486 * __devlink_region_snapshot_create - create a new snapshot
4487 * This will add a new snapshot of a region. The snapshot
4488 * will be stored on the region struct and can be accessed
4489 * from devlink. This is useful for future analyses of snapshots.
4490 * Multiple snapshots can be created on a region.
4491 * The @snapshot_id should be obtained using the getter function.
4493 * Must be called only while holding the devlink instance lock.
4495 * @region: devlink region of the snapshot
4496 * @data: snapshot data
4497 * @snapshot_id: snapshot id to be created
4500 __devlink_region_snapshot_create(struct devlink_region
*region
,
4501 u8
*data
, u32 snapshot_id
)
4503 struct devlink
*devlink
= region
->devlink
;
4504 struct devlink_snapshot
*snapshot
;
4507 lockdep_assert_held(&devlink
->lock
);
4509 /* check if region can hold one more snapshot */
4510 if (region
->cur_snapshots
== region
->max_snapshots
)
4513 if (devlink_region_snapshot_get_by_id(region
, snapshot_id
))
4516 snapshot
= kzalloc(sizeof(*snapshot
), GFP_KERNEL
);
4520 err
= __devlink_snapshot_id_increment(devlink
, snapshot_id
);
4522 goto err_snapshot_id_increment
;
4524 snapshot
->id
= snapshot_id
;
4525 snapshot
->region
= region
;
4526 snapshot
->data
= data
;
4528 list_add_tail(&snapshot
->list
, ®ion
->snapshot_list
);
4530 region
->cur_snapshots
++;
4532 devlink_nl_region_notify(region
, snapshot
, DEVLINK_CMD_REGION_NEW
);
4535 err_snapshot_id_increment
:
4540 static void devlink_region_snapshot_del(struct devlink_region
*region
,
4541 struct devlink_snapshot
*snapshot
)
4543 struct devlink
*devlink
= region
->devlink
;
4545 lockdep_assert_held(&devlink
->lock
);
4547 devlink_nl_region_notify(region
, snapshot
, DEVLINK_CMD_REGION_DEL
);
4548 region
->cur_snapshots
--;
4549 list_del(&snapshot
->list
);
4550 region
->ops
->destructor(snapshot
->data
);
4551 __devlink_snapshot_id_decrement(devlink
, snapshot
->id
);
4555 static int devlink_nl_cmd_region_get_doit(struct sk_buff
*skb
,
4556 struct genl_info
*info
)
4558 struct devlink
*devlink
= info
->user_ptr
[0];
4559 struct devlink_port
*port
= NULL
;
4560 struct devlink_region
*region
;
4561 const char *region_name
;
4562 struct sk_buff
*msg
;
4566 if (!info
->attrs
[DEVLINK_ATTR_REGION_NAME
])
4569 if (info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
4570 index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
4572 port
= devlink_port_get_by_index(devlink
, index
);
4577 region_name
= nla_data(info
->attrs
[DEVLINK_ATTR_REGION_NAME
]);
4579 region
= devlink_port_region_get_by_name(port
, region_name
);
4581 region
= devlink_region_get_by_name(devlink
, region_name
);
4586 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
4590 err
= devlink_nl_region_fill(msg
, devlink
, DEVLINK_CMD_REGION_GET
,
4591 info
->snd_portid
, info
->snd_seq
, 0,
4598 return genlmsg_reply(msg
, info
);
4601 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff
*msg
,
4602 struct netlink_callback
*cb
,
4603 struct devlink_port
*port
,
4607 struct devlink_region
*region
;
4610 list_for_each_entry(region
, &port
->region_list
, list
) {
4615 err
= devlink_nl_region_fill(msg
, port
->devlink
,
4616 DEVLINK_CMD_REGION_GET
,
4617 NETLINK_CB(cb
->skb
).portid
,
4619 NLM_F_MULTI
, region
);
4629 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff
*msg
,
4630 struct netlink_callback
*cb
,
4631 struct devlink
*devlink
,
4635 struct devlink_region
*region
;
4636 struct devlink_port
*port
;
4639 mutex_lock(&devlink
->lock
);
4640 list_for_each_entry(region
, &devlink
->region_list
, list
) {
4645 err
= devlink_nl_region_fill(msg
, devlink
,
4646 DEVLINK_CMD_REGION_GET
,
4647 NETLINK_CB(cb
->skb
).portid
,
4649 NLM_F_MULTI
, region
);
4655 list_for_each_entry(port
, &devlink
->port_list
, list
) {
4656 err
= devlink_nl_cmd_region_get_port_dumpit(msg
, cb
, port
, idx
,
4663 mutex_unlock(&devlink
->lock
);
4667 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff
*msg
,
4668 struct netlink_callback
*cb
)
4670 struct devlink
*devlink
;
4671 int start
= cb
->args
[0];
4675 mutex_lock(&devlink_mutex
);
4676 list_for_each_entry(devlink
, &devlink_list
, list
) {
4677 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
4679 err
= devlink_nl_cmd_region_get_devlink_dumpit(msg
, cb
, devlink
,
4685 mutex_unlock(&devlink_mutex
);
4690 static int devlink_nl_cmd_region_del(struct sk_buff
*skb
,
4691 struct genl_info
*info
)
4693 struct devlink
*devlink
= info
->user_ptr
[0];
4694 struct devlink_snapshot
*snapshot
;
4695 struct devlink_port
*port
= NULL
;
4696 struct devlink_region
*region
;
4697 const char *region_name
;
4701 if (!info
->attrs
[DEVLINK_ATTR_REGION_NAME
] ||
4702 !info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
])
4705 region_name
= nla_data(info
->attrs
[DEVLINK_ATTR_REGION_NAME
]);
4706 snapshot_id
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
4708 if (info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
4709 index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
4711 port
= devlink_port_get_by_index(devlink
, index
);
4717 region
= devlink_port_region_get_by_name(port
, region_name
);
4719 region
= devlink_region_get_by_name(devlink
, region_name
);
4724 snapshot
= devlink_region_snapshot_get_by_id(region
, snapshot_id
);
4728 devlink_region_snapshot_del(region
, snapshot
);
4733 devlink_nl_cmd_region_new(struct sk_buff
*skb
, struct genl_info
*info
)
4735 struct devlink
*devlink
= info
->user_ptr
[0];
4736 struct devlink_snapshot
*snapshot
;
4737 struct devlink_port
*port
= NULL
;
4738 struct nlattr
*snapshot_id_attr
;
4739 struct devlink_region
*region
;
4740 const char *region_name
;
4746 if (!info
->attrs
[DEVLINK_ATTR_REGION_NAME
]) {
4747 NL_SET_ERR_MSG_MOD(info
->extack
, "No region name provided");
4751 region_name
= nla_data(info
->attrs
[DEVLINK_ATTR_REGION_NAME
]);
4753 if (info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
4754 index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
4756 port
= devlink_port_get_by_index(devlink
, index
);
4762 region
= devlink_port_region_get_by_name(port
, region_name
);
4764 region
= devlink_region_get_by_name(devlink
, region_name
);
4767 NL_SET_ERR_MSG_MOD(info
->extack
, "The requested region does not exist");
4771 if (!region
->ops
->snapshot
) {
4772 NL_SET_ERR_MSG_MOD(info
->extack
, "The requested region does not support taking an immediate snapshot");
4776 if (region
->cur_snapshots
== region
->max_snapshots
) {
4777 NL_SET_ERR_MSG_MOD(info
->extack
, "The region has reached the maximum number of stored snapshots");
4781 snapshot_id_attr
= info
->attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
];
4782 if (snapshot_id_attr
) {
4783 snapshot_id
= nla_get_u32(snapshot_id_attr
);
4785 if (devlink_region_snapshot_get_by_id(region
, snapshot_id
)) {
4786 NL_SET_ERR_MSG_MOD(info
->extack
, "The requested snapshot id is already in use");
4790 err
= __devlink_snapshot_id_insert(devlink
, snapshot_id
);
4794 err
= __devlink_region_snapshot_id_get(devlink
, &snapshot_id
);
4796 NL_SET_ERR_MSG_MOD(info
->extack
, "Failed to allocate a new snapshot id");
4802 err
= region
->port_ops
->snapshot(port
, region
->port_ops
,
4803 info
->extack
, &data
);
4805 err
= region
->ops
->snapshot(devlink
, region
->ops
,
4806 info
->extack
, &data
);
4808 goto err_snapshot_capture
;
4810 err
= __devlink_region_snapshot_create(region
, data
, snapshot_id
);
4812 goto err_snapshot_create
;
4814 if (!snapshot_id_attr
) {
4815 struct sk_buff
*msg
;
4817 snapshot
= devlink_region_snapshot_get_by_id(region
,
4819 if (WARN_ON(!snapshot
))
4822 msg
= devlink_nl_region_notify_build(region
, snapshot
,
4823 DEVLINK_CMD_REGION_NEW
,
4826 err
= PTR_ERR_OR_ZERO(msg
);
4830 err
= genlmsg_reply(msg
, info
);
4837 err_snapshot_create
:
4838 region
->ops
->destructor(data
);
4839 err_snapshot_capture
:
4840 __devlink_snapshot_id_decrement(devlink
, snapshot_id
);
4844 devlink_region_snapshot_del(region
, snapshot
);
4848 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff
*msg
,
4849 struct devlink
*devlink
,
4850 u8
*chunk
, u32 chunk_size
,
4853 struct nlattr
*chunk_attr
;
4856 chunk_attr
= nla_nest_start_noflag(msg
, DEVLINK_ATTR_REGION_CHUNK
);
4860 err
= nla_put(msg
, DEVLINK_ATTR_REGION_CHUNK_DATA
, chunk_size
, chunk
);
4862 goto nla_put_failure
;
4864 err
= nla_put_u64_64bit(msg
, DEVLINK_ATTR_REGION_CHUNK_ADDR
, addr
,
4867 goto nla_put_failure
;
4869 nla_nest_end(msg
, chunk_attr
);
4873 nla_nest_cancel(msg
, chunk_attr
);
4877 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4879 static int devlink_nl_region_read_snapshot_fill(struct sk_buff
*skb
,
4880 struct devlink
*devlink
,
4881 struct devlink_region
*region
,
4882 struct nlattr
**attrs
,
4887 struct devlink_snapshot
*snapshot
;
4888 u64 curr_offset
= start_offset
;
4892 *new_offset
= start_offset
;
4894 snapshot_id
= nla_get_u32(attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]);
4895 snapshot
= devlink_region_snapshot_get_by_id(region
, snapshot_id
);
4899 while (curr_offset
< end_offset
) {
4903 if (end_offset
- curr_offset
< DEVLINK_REGION_READ_CHUNK_SIZE
)
4904 data_size
= end_offset
- curr_offset
;
4906 data_size
= DEVLINK_REGION_READ_CHUNK_SIZE
;
4908 data
= &snapshot
->data
[curr_offset
];
4909 err
= devlink_nl_cmd_region_read_chunk_fill(skb
, devlink
,
4915 curr_offset
+= data_size
;
4917 *new_offset
= curr_offset
;
4922 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff
*skb
,
4923 struct netlink_callback
*cb
)
4925 const struct genl_dumpit_info
*info
= genl_dumpit_info(cb
);
4926 u64 ret_offset
, start_offset
, end_offset
= U64_MAX
;
4927 struct nlattr
**attrs
= info
->attrs
;
4928 struct devlink_port
*port
= NULL
;
4929 struct devlink_region
*region
;
4930 struct nlattr
*chunks_attr
;
4931 const char *region_name
;
4932 struct devlink
*devlink
;
4937 start_offset
= *((u64
*)&cb
->args
[0]);
4939 mutex_lock(&devlink_mutex
);
4940 devlink
= devlink_get_from_attrs(sock_net(cb
->skb
->sk
), attrs
);
4941 if (IS_ERR(devlink
)) {
4942 err
= PTR_ERR(devlink
);
4946 mutex_lock(&devlink
->lock
);
4948 if (!attrs
[DEVLINK_ATTR_REGION_NAME
] ||
4949 !attrs
[DEVLINK_ATTR_REGION_SNAPSHOT_ID
]) {
4954 if (info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]) {
4955 index
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_PORT_INDEX
]);
4957 port
= devlink_port_get_by_index(devlink
, index
);
4964 region_name
= nla_data(attrs
[DEVLINK_ATTR_REGION_NAME
]);
4967 region
= devlink_port_region_get_by_name(port
, region_name
);
4969 region
= devlink_region_get_by_name(devlink
, region_name
);
4976 if (attrs
[DEVLINK_ATTR_REGION_CHUNK_ADDR
] &&
4977 attrs
[DEVLINK_ATTR_REGION_CHUNK_LEN
]) {
4980 nla_get_u64(attrs
[DEVLINK_ATTR_REGION_CHUNK_ADDR
]);
4982 end_offset
= nla_get_u64(attrs
[DEVLINK_ATTR_REGION_CHUNK_ADDR
]);
4983 end_offset
+= nla_get_u64(attrs
[DEVLINK_ATTR_REGION_CHUNK_LEN
]);
4986 if (end_offset
> region
->size
)
4987 end_offset
= region
->size
;
4989 /* return 0 if there is no further data to read */
4990 if (start_offset
== end_offset
) {
4995 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
4996 &devlink_nl_family
, NLM_F_ACK
| NLM_F_MULTI
,
4997 DEVLINK_CMD_REGION_READ
);
5003 err
= devlink_nl_put_handle(skb
, devlink
);
5005 goto nla_put_failure
;
5008 err
= nla_put_u32(skb
, DEVLINK_ATTR_PORT_INDEX
,
5009 region
->port
->index
);
5011 goto nla_put_failure
;
5014 err
= nla_put_string(skb
, DEVLINK_ATTR_REGION_NAME
, region_name
);
5016 goto nla_put_failure
;
5018 chunks_attr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_REGION_CHUNKS
);
5021 goto nla_put_failure
;
5024 err
= devlink_nl_region_read_snapshot_fill(skb
, devlink
,
5027 end_offset
, &ret_offset
);
5029 if (err
&& err
!= -EMSGSIZE
)
5030 goto nla_put_failure
;
5032 /* Check if there was any progress done to prevent infinite loop */
5033 if (ret_offset
== start_offset
) {
5035 goto nla_put_failure
;
5038 *((u64
*)&cb
->args
[0]) = ret_offset
;
5040 nla_nest_end(skb
, chunks_attr
);
5041 genlmsg_end(skb
, hdr
);
5042 mutex_unlock(&devlink
->lock
);
5043 mutex_unlock(&devlink_mutex
);
5048 genlmsg_cancel(skb
, hdr
);
5050 mutex_unlock(&devlink
->lock
);
5052 mutex_unlock(&devlink_mutex
);
5056 struct devlink_info_req
{
5057 struct sk_buff
*msg
;
5060 int devlink_info_driver_name_put(struct devlink_info_req
*req
, const char *name
)
5062 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_DRIVER_NAME
, name
);
5064 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put
);
5066 int devlink_info_serial_number_put(struct devlink_info_req
*req
, const char *sn
)
5068 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_SERIAL_NUMBER
, sn
);
5070 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put
);
5072 int devlink_info_board_serial_number_put(struct devlink_info_req
*req
,
5075 return nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER
,
5078 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put
);
5080 static int devlink_info_version_put(struct devlink_info_req
*req
, int attr
,
5081 const char *version_name
,
5082 const char *version_value
)
5084 struct nlattr
*nest
;
5087 nest
= nla_nest_start_noflag(req
->msg
, attr
);
5091 err
= nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_VERSION_NAME
,
5094 goto nla_put_failure
;
5096 err
= nla_put_string(req
->msg
, DEVLINK_ATTR_INFO_VERSION_VALUE
,
5099 goto nla_put_failure
;
5101 nla_nest_end(req
->msg
, nest
);
5106 nla_nest_cancel(req
->msg
, nest
);
5110 int devlink_info_version_fixed_put(struct devlink_info_req
*req
,
5111 const char *version_name
,
5112 const char *version_value
)
5114 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_FIXED
,
5115 version_name
, version_value
);
5117 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put
);
5119 int devlink_info_version_stored_put(struct devlink_info_req
*req
,
5120 const char *version_name
,
5121 const char *version_value
)
5123 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_STORED
,
5124 version_name
, version_value
);
5126 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put
);
5128 int devlink_info_version_running_put(struct devlink_info_req
*req
,
5129 const char *version_name
,
5130 const char *version_value
)
5132 return devlink_info_version_put(req
, DEVLINK_ATTR_INFO_VERSION_RUNNING
,
5133 version_name
, version_value
);
5135 EXPORT_SYMBOL_GPL(devlink_info_version_running_put
);
5138 devlink_nl_info_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
5139 enum devlink_command cmd
, u32 portid
,
5140 u32 seq
, int flags
, struct netlink_ext_ack
*extack
)
5142 struct devlink_info_req req
;
5146 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
5151 if (devlink_nl_put_handle(msg
, devlink
))
5152 goto err_cancel_msg
;
5155 err
= devlink
->ops
->info_get(devlink
, &req
, extack
);
5157 goto err_cancel_msg
;
5159 genlmsg_end(msg
, hdr
);
5163 genlmsg_cancel(msg
, hdr
);
5167 static int devlink_nl_cmd_info_get_doit(struct sk_buff
*skb
,
5168 struct genl_info
*info
)
5170 struct devlink
*devlink
= info
->user_ptr
[0];
5171 struct sk_buff
*msg
;
5174 if (!devlink
->ops
->info_get
)
5177 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5181 err
= devlink_nl_info_fill(msg
, devlink
, DEVLINK_CMD_INFO_GET
,
5182 info
->snd_portid
, info
->snd_seq
, 0,
5189 return genlmsg_reply(msg
, info
);
5192 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff
*msg
,
5193 struct netlink_callback
*cb
)
5195 struct devlink
*devlink
;
5196 int start
= cb
->args
[0];
5200 mutex_lock(&devlink_mutex
);
5201 list_for_each_entry(devlink
, &devlink_list
, list
) {
5202 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
5209 if (!devlink
->ops
->info_get
) {
5214 mutex_lock(&devlink
->lock
);
5215 err
= devlink_nl_info_fill(msg
, devlink
, DEVLINK_CMD_INFO_GET
,
5216 NETLINK_CB(cb
->skb
).portid
,
5217 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
5219 mutex_unlock(&devlink
->lock
);
5220 if (err
== -EOPNOTSUPP
)
5226 mutex_unlock(&devlink_mutex
);
5228 if (err
!= -EMSGSIZE
)
5235 struct devlink_fmsg_item
{
5236 struct list_head list
;
5243 struct devlink_fmsg
{
5244 struct list_head item_list
;
5245 bool putting_binary
; /* This flag forces enclosing of binary data
5246 * in an array brackets. It forces using
5247 * of designated API:
5248 * devlink_fmsg_binary_pair_nest_start()
5249 * devlink_fmsg_binary_pair_nest_end()
5253 static struct devlink_fmsg
*devlink_fmsg_alloc(void)
5255 struct devlink_fmsg
*fmsg
;
5257 fmsg
= kzalloc(sizeof(*fmsg
), GFP_KERNEL
);
5261 INIT_LIST_HEAD(&fmsg
->item_list
);
5266 static void devlink_fmsg_free(struct devlink_fmsg
*fmsg
)
5268 struct devlink_fmsg_item
*item
, *tmp
;
5270 list_for_each_entry_safe(item
, tmp
, &fmsg
->item_list
, list
) {
5271 list_del(&item
->list
);
5277 static int devlink_fmsg_nest_common(struct devlink_fmsg
*fmsg
,
5280 struct devlink_fmsg_item
*item
;
5282 item
= kzalloc(sizeof(*item
), GFP_KERNEL
);
5286 item
->attrtype
= attrtype
;
5287 list_add_tail(&item
->list
, &fmsg
->item_list
);
5292 int devlink_fmsg_obj_nest_start(struct devlink_fmsg
*fmsg
)
5294 if (fmsg
->putting_binary
)
5297 return devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_OBJ_NEST_START
);
5299 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start
);
5301 static int devlink_fmsg_nest_end(struct devlink_fmsg
*fmsg
)
5303 if (fmsg
->putting_binary
)
5306 return devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_NEST_END
);
5309 int devlink_fmsg_obj_nest_end(struct devlink_fmsg
*fmsg
)
5311 if (fmsg
->putting_binary
)
5314 return devlink_fmsg_nest_end(fmsg
);
5316 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end
);
5318 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
5320 static int devlink_fmsg_put_name(struct devlink_fmsg
*fmsg
, const char *name
)
5322 struct devlink_fmsg_item
*item
;
5324 if (fmsg
->putting_binary
)
5327 if (strlen(name
) + 1 > DEVLINK_FMSG_MAX_SIZE
)
5330 item
= kzalloc(sizeof(*item
) + strlen(name
) + 1, GFP_KERNEL
);
5334 item
->nla_type
= NLA_NUL_STRING
;
5335 item
->len
= strlen(name
) + 1;
5336 item
->attrtype
= DEVLINK_ATTR_FMSG_OBJ_NAME
;
5337 memcpy(&item
->value
, name
, item
->len
);
5338 list_add_tail(&item
->list
, &fmsg
->item_list
);
5343 int devlink_fmsg_pair_nest_start(struct devlink_fmsg
*fmsg
, const char *name
)
5347 if (fmsg
->putting_binary
)
5350 err
= devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_PAIR_NEST_START
);
5354 err
= devlink_fmsg_put_name(fmsg
, name
);
5360 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start
);
5362 int devlink_fmsg_pair_nest_end(struct devlink_fmsg
*fmsg
)
5364 if (fmsg
->putting_binary
)
5367 return devlink_fmsg_nest_end(fmsg
);
5369 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end
);
5371 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg
*fmsg
,
5376 if (fmsg
->putting_binary
)
5379 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
5383 err
= devlink_fmsg_nest_common(fmsg
, DEVLINK_ATTR_FMSG_ARR_NEST_START
);
5389 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start
);
5391 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg
*fmsg
)
5395 if (fmsg
->putting_binary
)
5398 err
= devlink_fmsg_nest_end(fmsg
);
5402 err
= devlink_fmsg_nest_end(fmsg
);
5408 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end
);
5410 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg
*fmsg
,
5415 err
= devlink_fmsg_arr_pair_nest_start(fmsg
, name
);
5419 fmsg
->putting_binary
= true;
5422 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start
);
5424 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg
*fmsg
)
5426 if (!fmsg
->putting_binary
)
5429 fmsg
->putting_binary
= false;
5430 return devlink_fmsg_arr_pair_nest_end(fmsg
);
5432 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end
);
5434 static int devlink_fmsg_put_value(struct devlink_fmsg
*fmsg
,
5435 const void *value
, u16 value_len
,
5438 struct devlink_fmsg_item
*item
;
5440 if (value_len
> DEVLINK_FMSG_MAX_SIZE
)
5443 item
= kzalloc(sizeof(*item
) + value_len
, GFP_KERNEL
);
5447 item
->nla_type
= value_nla_type
;
5448 item
->len
= value_len
;
5449 item
->attrtype
= DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
;
5450 memcpy(&item
->value
, value
, item
->len
);
5451 list_add_tail(&item
->list
, &fmsg
->item_list
);
5456 int devlink_fmsg_bool_put(struct devlink_fmsg
*fmsg
, bool value
)
5458 if (fmsg
->putting_binary
)
5461 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_FLAG
);
5463 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put
);
5465 int devlink_fmsg_u8_put(struct devlink_fmsg
*fmsg
, u8 value
)
5467 if (fmsg
->putting_binary
)
5470 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U8
);
5472 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put
);
5474 int devlink_fmsg_u32_put(struct devlink_fmsg
*fmsg
, u32 value
)
5476 if (fmsg
->putting_binary
)
5479 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U32
);
5481 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put
);
5483 int devlink_fmsg_u64_put(struct devlink_fmsg
*fmsg
, u64 value
)
5485 if (fmsg
->putting_binary
)
5488 return devlink_fmsg_put_value(fmsg
, &value
, sizeof(value
), NLA_U64
);
5490 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put
);
5492 int devlink_fmsg_string_put(struct devlink_fmsg
*fmsg
, const char *value
)
5494 if (fmsg
->putting_binary
)
5497 return devlink_fmsg_put_value(fmsg
, value
, strlen(value
) + 1,
5500 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put
);
5502 int devlink_fmsg_binary_put(struct devlink_fmsg
*fmsg
, const void *value
,
5505 if (!fmsg
->putting_binary
)
5508 return devlink_fmsg_put_value(fmsg
, value
, value_len
, NLA_BINARY
);
5510 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put
);
5512 int devlink_fmsg_bool_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
5517 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
5521 err
= devlink_fmsg_bool_put(fmsg
, value
);
5525 err
= devlink_fmsg_pair_nest_end(fmsg
);
5531 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put
);
5533 int devlink_fmsg_u8_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
5538 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
5542 err
= devlink_fmsg_u8_put(fmsg
, value
);
5546 err
= devlink_fmsg_pair_nest_end(fmsg
);
5552 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put
);
5554 int devlink_fmsg_u32_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
5559 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
5563 err
= devlink_fmsg_u32_put(fmsg
, value
);
5567 err
= devlink_fmsg_pair_nest_end(fmsg
);
5573 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put
);
5575 int devlink_fmsg_u64_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
5580 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
5584 err
= devlink_fmsg_u64_put(fmsg
, value
);
5588 err
= devlink_fmsg_pair_nest_end(fmsg
);
5594 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put
);
5596 int devlink_fmsg_string_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
5601 err
= devlink_fmsg_pair_nest_start(fmsg
, name
);
5605 err
= devlink_fmsg_string_put(fmsg
, value
);
5609 err
= devlink_fmsg_pair_nest_end(fmsg
);
5615 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put
);
5617 int devlink_fmsg_binary_pair_put(struct devlink_fmsg
*fmsg
, const char *name
,
5618 const void *value
, u32 value_len
)
5625 err
= devlink_fmsg_binary_pair_nest_start(fmsg
, name
);
5629 for (offset
= 0; offset
< value_len
; offset
+= data_size
) {
5630 data_size
= value_len
- offset
;
5631 if (data_size
> DEVLINK_FMSG_MAX_SIZE
)
5632 data_size
= DEVLINK_FMSG_MAX_SIZE
;
5633 err
= devlink_fmsg_binary_put(fmsg
, value
+ offset
, data_size
);
5636 /* Exit from loop with a break (instead of
5637 * return) to make sure putting_binary is turned off in
5638 * devlink_fmsg_binary_pair_nest_end
5642 end_err
= devlink_fmsg_binary_pair_nest_end(fmsg
);
5648 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put
);
5651 devlink_fmsg_item_fill_type(struct devlink_fmsg_item
*msg
, struct sk_buff
*skb
)
5653 switch (msg
->nla_type
) {
5658 case NLA_NUL_STRING
:
5660 return nla_put_u8(skb
, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE
,
5668 devlink_fmsg_item_fill_data(struct devlink_fmsg_item
*msg
, struct sk_buff
*skb
)
5670 int attrtype
= DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
;
5673 switch (msg
->nla_type
) {
5675 /* Always provide flag data, regardless of its value */
5676 tmp
= *(bool *) msg
->value
;
5678 return nla_put_u8(skb
, attrtype
, tmp
);
5680 return nla_put_u8(skb
, attrtype
, *(u8
*) msg
->value
);
5682 return nla_put_u32(skb
, attrtype
, *(u32
*) msg
->value
);
5684 return nla_put_u64_64bit(skb
, attrtype
, *(u64
*) msg
->value
,
5686 case NLA_NUL_STRING
:
5687 return nla_put_string(skb
, attrtype
, (char *) &msg
->value
);
5689 return nla_put(skb
, attrtype
, msg
->len
, (void *) &msg
->value
);
5696 devlink_fmsg_prepare_skb(struct devlink_fmsg
*fmsg
, struct sk_buff
*skb
,
5699 struct devlink_fmsg_item
*item
;
5700 struct nlattr
*fmsg_nlattr
;
5704 fmsg_nlattr
= nla_nest_start_noflag(skb
, DEVLINK_ATTR_FMSG
);
5708 list_for_each_entry(item
, &fmsg
->item_list
, list
) {
5714 switch (item
->attrtype
) {
5715 case DEVLINK_ATTR_FMSG_OBJ_NEST_START
:
5716 case DEVLINK_ATTR_FMSG_PAIR_NEST_START
:
5717 case DEVLINK_ATTR_FMSG_ARR_NEST_START
:
5718 case DEVLINK_ATTR_FMSG_NEST_END
:
5719 err
= nla_put_flag(skb
, item
->attrtype
);
5721 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA
:
5722 err
= devlink_fmsg_item_fill_type(item
, skb
);
5725 err
= devlink_fmsg_item_fill_data(item
, skb
);
5727 case DEVLINK_ATTR_FMSG_OBJ_NAME
:
5728 err
= nla_put_string(skb
, item
->attrtype
,
5729 (char *) &item
->value
);
5741 nla_nest_end(skb
, fmsg_nlattr
);
5745 static int devlink_fmsg_snd(struct devlink_fmsg
*fmsg
,
5746 struct genl_info
*info
,
5747 enum devlink_command cmd
, int flags
)
5749 struct nlmsghdr
*nlh
;
5750 struct sk_buff
*skb
;
5757 int tmp_index
= index
;
5759 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5763 hdr
= genlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
5764 &devlink_nl_family
, flags
| NLM_F_MULTI
, cmd
);
5767 goto nla_put_failure
;
5770 err
= devlink_fmsg_prepare_skb(fmsg
, skb
, &index
);
5773 else if (err
!= -EMSGSIZE
|| tmp_index
== index
)
5774 goto nla_put_failure
;
5776 genlmsg_end(skb
, hdr
);
5777 err
= genlmsg_reply(skb
, info
);
5782 skb
= genlmsg_new(GENLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
5785 nlh
= nlmsg_put(skb
, info
->snd_portid
, info
->snd_seq
,
5786 NLMSG_DONE
, 0, flags
| NLM_F_MULTI
);
5789 goto nla_put_failure
;
5792 return genlmsg_reply(skb
, info
);
5799 static int devlink_fmsg_dumpit(struct devlink_fmsg
*fmsg
, struct sk_buff
*skb
,
5800 struct netlink_callback
*cb
,
5801 enum devlink_command cmd
)
5803 int index
= cb
->args
[0];
5804 int tmp_index
= index
;
5808 hdr
= genlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
, cb
->nlh
->nlmsg_seq
,
5809 &devlink_nl_family
, NLM_F_ACK
| NLM_F_MULTI
, cmd
);
5812 goto nla_put_failure
;
5815 err
= devlink_fmsg_prepare_skb(fmsg
, skb
, &index
);
5816 if ((err
&& err
!= -EMSGSIZE
) || tmp_index
== index
)
5817 goto nla_put_failure
;
5819 cb
->args
[0] = index
;
5820 genlmsg_end(skb
, hdr
);
5824 genlmsg_cancel(skb
, hdr
);
5828 struct devlink_health_reporter
{
5829 struct list_head list
;
5831 const struct devlink_health_reporter_ops
*ops
;
5832 struct devlink
*devlink
;
5833 struct devlink_port
*devlink_port
;
5834 struct devlink_fmsg
*dump_fmsg
;
5835 struct mutex dump_lock
; /* lock parallel read/write from dump buffers */
5836 u64 graceful_period
;
5844 u64 last_recovery_ts
;
5845 refcount_t refcount
;
5849 devlink_health_reporter_priv(struct devlink_health_reporter
*reporter
)
5851 return reporter
->priv
;
5853 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv
);
5855 static struct devlink_health_reporter
*
5856 __devlink_health_reporter_find_by_name(struct list_head
*reporter_list
,
5857 struct mutex
*list_lock
,
5858 const char *reporter_name
)
5860 struct devlink_health_reporter
*reporter
;
5862 lockdep_assert_held(list_lock
);
5863 list_for_each_entry(reporter
, reporter_list
, list
)
5864 if (!strcmp(reporter
->ops
->name
, reporter_name
))
5869 static struct devlink_health_reporter
*
5870 devlink_health_reporter_find_by_name(struct devlink
*devlink
,
5871 const char *reporter_name
)
5873 return __devlink_health_reporter_find_by_name(&devlink
->reporter_list
,
5874 &devlink
->reporters_lock
,
5878 static struct devlink_health_reporter
*
5879 devlink_port_health_reporter_find_by_name(struct devlink_port
*devlink_port
,
5880 const char *reporter_name
)
5882 return __devlink_health_reporter_find_by_name(&devlink_port
->reporter_list
,
5883 &devlink_port
->reporters_lock
,
5887 static struct devlink_health_reporter
*
5888 __devlink_health_reporter_create(struct devlink
*devlink
,
5889 const struct devlink_health_reporter_ops
*ops
,
5890 u64 graceful_period
, void *priv
)
5892 struct devlink_health_reporter
*reporter
;
5894 if (WARN_ON(graceful_period
&& !ops
->recover
))
5895 return ERR_PTR(-EINVAL
);
5897 reporter
= kzalloc(sizeof(*reporter
), GFP_KERNEL
);
5899 return ERR_PTR(-ENOMEM
);
5901 reporter
->priv
= priv
;
5902 reporter
->ops
= ops
;
5903 reporter
->devlink
= devlink
;
5904 reporter
->graceful_period
= graceful_period
;
5905 reporter
->auto_recover
= !!ops
->recover
;
5906 reporter
->auto_dump
= !!ops
->dump
;
5907 mutex_init(&reporter
->dump_lock
);
5908 refcount_set(&reporter
->refcount
, 1);
5913 * devlink_port_health_reporter_create - create devlink health reporter for
5914 * specified port instance
5916 * @port: devlink_port which should contain the new reporter
5918 * @graceful_period: to avoid recovery loops, in msecs
5921 struct devlink_health_reporter
*
5922 devlink_port_health_reporter_create(struct devlink_port
*port
,
5923 const struct devlink_health_reporter_ops
*ops
,
5924 u64 graceful_period
, void *priv
)
5926 struct devlink_health_reporter
*reporter
;
5928 mutex_lock(&port
->reporters_lock
);
5929 if (__devlink_health_reporter_find_by_name(&port
->reporter_list
,
5930 &port
->reporters_lock
, ops
->name
)) {
5931 reporter
= ERR_PTR(-EEXIST
);
5935 reporter
= __devlink_health_reporter_create(port
->devlink
, ops
,
5936 graceful_period
, priv
);
5937 if (IS_ERR(reporter
))
5940 reporter
->devlink_port
= port
;
5941 list_add_tail(&reporter
->list
, &port
->reporter_list
);
5943 mutex_unlock(&port
->reporters_lock
);
5946 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create
);
5949 * devlink_health_reporter_create - create devlink health reporter
5953 * @graceful_period: to avoid recovery loops, in msecs
5956 struct devlink_health_reporter
*
5957 devlink_health_reporter_create(struct devlink
*devlink
,
5958 const struct devlink_health_reporter_ops
*ops
,
5959 u64 graceful_period
, void *priv
)
5961 struct devlink_health_reporter
*reporter
;
5963 mutex_lock(&devlink
->reporters_lock
);
5964 if (devlink_health_reporter_find_by_name(devlink
, ops
->name
)) {
5965 reporter
= ERR_PTR(-EEXIST
);
5969 reporter
= __devlink_health_reporter_create(devlink
, ops
,
5970 graceful_period
, priv
);
5971 if (IS_ERR(reporter
))
5974 list_add_tail(&reporter
->list
, &devlink
->reporter_list
);
5976 mutex_unlock(&devlink
->reporters_lock
);
5979 EXPORT_SYMBOL_GPL(devlink_health_reporter_create
);
5982 devlink_health_reporter_free(struct devlink_health_reporter
*reporter
)
5984 mutex_destroy(&reporter
->dump_lock
);
5985 if (reporter
->dump_fmsg
)
5986 devlink_fmsg_free(reporter
->dump_fmsg
);
5991 devlink_health_reporter_put(struct devlink_health_reporter
*reporter
)
5993 if (refcount_dec_and_test(&reporter
->refcount
))
5994 devlink_health_reporter_free(reporter
);
5998 __devlink_health_reporter_destroy(struct devlink_health_reporter
*reporter
)
6000 list_del(&reporter
->list
);
6001 devlink_health_reporter_put(reporter
);
6005 * devlink_health_reporter_destroy - destroy devlink health reporter
6007 * @reporter: devlink health reporter to destroy
6010 devlink_health_reporter_destroy(struct devlink_health_reporter
*reporter
)
6012 struct mutex
*lock
= &reporter
->devlink
->reporters_lock
;
6015 __devlink_health_reporter_destroy(reporter
);
6018 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy
);
6021 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
6023 * @reporter: devlink health reporter to destroy
6026 devlink_port_health_reporter_destroy(struct devlink_health_reporter
*reporter
)
6028 struct mutex
*lock
= &reporter
->devlink_port
->reporters_lock
;
6031 __devlink_health_reporter_destroy(reporter
);
6034 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy
);
6037 devlink_nl_health_reporter_fill(struct sk_buff
*msg
,
6038 struct devlink
*devlink
,
6039 struct devlink_health_reporter
*reporter
,
6040 enum devlink_command cmd
, u32 portid
,
6043 struct nlattr
*reporter_attr
;
6046 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
6050 if (devlink_nl_put_handle(msg
, devlink
))
6051 goto genlmsg_cancel
;
6053 if (reporter
->devlink_port
) {
6054 if (nla_put_u32(msg
, DEVLINK_ATTR_PORT_INDEX
, reporter
->devlink_port
->index
))
6055 goto genlmsg_cancel
;
6057 reporter_attr
= nla_nest_start_noflag(msg
,
6058 DEVLINK_ATTR_HEALTH_REPORTER
);
6060 goto genlmsg_cancel
;
6061 if (nla_put_string(msg
, DEVLINK_ATTR_HEALTH_REPORTER_NAME
,
6062 reporter
->ops
->name
))
6063 goto reporter_nest_cancel
;
6064 if (nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_STATE
,
6065 reporter
->health_state
))
6066 goto reporter_nest_cancel
;
6067 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT
,
6068 reporter
->error_count
, DEVLINK_ATTR_PAD
))
6069 goto reporter_nest_cancel
;
6070 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT
,
6071 reporter
->recovery_count
, DEVLINK_ATTR_PAD
))
6072 goto reporter_nest_cancel
;
6073 if (reporter
->ops
->recover
&&
6074 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
,
6075 reporter
->graceful_period
,
6077 goto reporter_nest_cancel
;
6078 if (reporter
->ops
->recover
&&
6079 nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
,
6080 reporter
->auto_recover
))
6081 goto reporter_nest_cancel
;
6082 if (reporter
->dump_fmsg
&&
6083 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS
,
6084 jiffies_to_msecs(reporter
->dump_ts
),
6086 goto reporter_nest_cancel
;
6087 if (reporter
->dump_fmsg
&&
6088 nla_put_u64_64bit(msg
, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS
,
6089 reporter
->dump_real_ts
, DEVLINK_ATTR_PAD
))
6090 goto reporter_nest_cancel
;
6091 if (reporter
->ops
->dump
&&
6092 nla_put_u8(msg
, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
,
6093 reporter
->auto_dump
))
6094 goto reporter_nest_cancel
;
6096 nla_nest_end(msg
, reporter_attr
);
6097 genlmsg_end(msg
, hdr
);
6100 reporter_nest_cancel
:
6101 nla_nest_end(msg
, reporter_attr
);
6103 genlmsg_cancel(msg
, hdr
);
6107 static void devlink_recover_notify(struct devlink_health_reporter
*reporter
,
6108 enum devlink_command cmd
)
6110 struct sk_buff
*msg
;
6113 WARN_ON(cmd
!= DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
6115 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6119 err
= devlink_nl_health_reporter_fill(msg
, reporter
->devlink
,
6120 reporter
, cmd
, 0, 0, 0);
6126 genlmsg_multicast_netns(&devlink_nl_family
,
6127 devlink_net(reporter
->devlink
),
6128 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
6132 devlink_health_reporter_recovery_done(struct devlink_health_reporter
*reporter
)
6134 reporter
->recovery_count
++;
6135 reporter
->last_recovery_ts
= jiffies
;
6137 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done
);
6140 devlink_health_reporter_recover(struct devlink_health_reporter
*reporter
,
6141 void *priv_ctx
, struct netlink_ext_ack
*extack
)
6145 if (reporter
->health_state
== DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
)
6148 if (!reporter
->ops
->recover
)
6151 err
= reporter
->ops
->recover(reporter
, priv_ctx
, extack
);
6155 devlink_health_reporter_recovery_done(reporter
);
6156 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
;
6157 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
6163 devlink_health_dump_clear(struct devlink_health_reporter
*reporter
)
6165 if (!reporter
->dump_fmsg
)
6167 devlink_fmsg_free(reporter
->dump_fmsg
);
6168 reporter
->dump_fmsg
= NULL
;
6171 static int devlink_health_do_dump(struct devlink_health_reporter
*reporter
,
6173 struct netlink_ext_ack
*extack
)
6177 if (!reporter
->ops
->dump
)
6180 if (reporter
->dump_fmsg
)
6183 reporter
->dump_fmsg
= devlink_fmsg_alloc();
6184 if (!reporter
->dump_fmsg
) {
6189 err
= devlink_fmsg_obj_nest_start(reporter
->dump_fmsg
);
6193 err
= reporter
->ops
->dump(reporter
, reporter
->dump_fmsg
,
6198 err
= devlink_fmsg_obj_nest_end(reporter
->dump_fmsg
);
6202 reporter
->dump_ts
= jiffies
;
6203 reporter
->dump_real_ts
= ktime_get_real_ns();
6208 devlink_health_dump_clear(reporter
);
6212 int devlink_health_report(struct devlink_health_reporter
*reporter
,
6213 const char *msg
, void *priv_ctx
)
6215 enum devlink_health_reporter_state prev_health_state
;
6216 struct devlink
*devlink
= reporter
->devlink
;
6217 unsigned long recover_ts_threshold
;
6219 /* write a log message of the current error */
6221 trace_devlink_health_report(devlink
, reporter
->ops
->name
, msg
);
6222 reporter
->error_count
++;
6223 prev_health_state
= reporter
->health_state
;
6224 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_ERROR
;
6225 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
6227 /* abort if the previous error wasn't recovered */
6228 recover_ts_threshold
= reporter
->last_recovery_ts
+
6229 msecs_to_jiffies(reporter
->graceful_period
);
6230 if (reporter
->auto_recover
&&
6231 (prev_health_state
!= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
||
6232 (reporter
->last_recovery_ts
&& reporter
->recovery_count
&&
6233 time_is_after_jiffies(recover_ts_threshold
)))) {
6234 trace_devlink_health_recover_aborted(devlink
,
6235 reporter
->ops
->name
,
6236 reporter
->health_state
,
6238 reporter
->last_recovery_ts
);
6242 reporter
->health_state
= DEVLINK_HEALTH_REPORTER_STATE_ERROR
;
6244 if (reporter
->auto_dump
) {
6245 mutex_lock(&reporter
->dump_lock
);
6246 /* store current dump of current error, for later analysis */
6247 devlink_health_do_dump(reporter
, priv_ctx
, NULL
);
6248 mutex_unlock(&reporter
->dump_lock
);
6251 if (reporter
->auto_recover
)
6252 return devlink_health_reporter_recover(reporter
,
6257 EXPORT_SYMBOL_GPL(devlink_health_report
);
6259 static struct devlink_health_reporter
*
6260 devlink_health_reporter_get_from_attrs(struct devlink
*devlink
,
6261 struct nlattr
**attrs
)
6263 struct devlink_health_reporter
*reporter
;
6264 struct devlink_port
*devlink_port
;
6265 char *reporter_name
;
6267 if (!attrs
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
])
6270 reporter_name
= nla_data(attrs
[DEVLINK_ATTR_HEALTH_REPORTER_NAME
]);
6271 devlink_port
= devlink_port_get_from_attrs(devlink
, attrs
);
6272 if (IS_ERR(devlink_port
)) {
6273 mutex_lock(&devlink
->reporters_lock
);
6274 reporter
= devlink_health_reporter_find_by_name(devlink
, reporter_name
);
6276 refcount_inc(&reporter
->refcount
);
6277 mutex_unlock(&devlink
->reporters_lock
);
6279 mutex_lock(&devlink_port
->reporters_lock
);
6280 reporter
= devlink_port_health_reporter_find_by_name(devlink_port
, reporter_name
);
6282 refcount_inc(&reporter
->refcount
);
6283 mutex_unlock(&devlink_port
->reporters_lock
);
6289 static struct devlink_health_reporter
*
6290 devlink_health_reporter_get_from_info(struct devlink
*devlink
,
6291 struct genl_info
*info
)
6293 return devlink_health_reporter_get_from_attrs(devlink
, info
->attrs
);
6296 static struct devlink_health_reporter
*
6297 devlink_health_reporter_get_from_cb(struct netlink_callback
*cb
)
6299 const struct genl_dumpit_info
*info
= genl_dumpit_info(cb
);
6300 struct devlink_health_reporter
*reporter
;
6301 struct nlattr
**attrs
= info
->attrs
;
6302 struct devlink
*devlink
;
6304 mutex_lock(&devlink_mutex
);
6305 devlink
= devlink_get_from_attrs(sock_net(cb
->skb
->sk
), attrs
);
6306 if (IS_ERR(devlink
))
6309 reporter
= devlink_health_reporter_get_from_attrs(devlink
, attrs
);
6310 mutex_unlock(&devlink_mutex
);
6313 mutex_unlock(&devlink_mutex
);
6318 devlink_health_reporter_state_update(struct devlink_health_reporter
*reporter
,
6319 enum devlink_health_reporter_state state
)
6321 if (WARN_ON(state
!= DEVLINK_HEALTH_REPORTER_STATE_HEALTHY
&&
6322 state
!= DEVLINK_HEALTH_REPORTER_STATE_ERROR
))
6325 if (reporter
->health_state
== state
)
6328 reporter
->health_state
= state
;
6329 trace_devlink_health_reporter_state_update(reporter
->devlink
,
6330 reporter
->ops
->name
, state
);
6331 devlink_recover_notify(reporter
, DEVLINK_CMD_HEALTH_REPORTER_RECOVER
);
6333 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update
);
6335 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff
*skb
,
6336 struct genl_info
*info
)
6338 struct devlink
*devlink
= info
->user_ptr
[0];
6339 struct devlink_health_reporter
*reporter
;
6340 struct sk_buff
*msg
;
6343 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
6347 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6353 err
= devlink_nl_health_reporter_fill(msg
, devlink
, reporter
,
6354 DEVLINK_CMD_HEALTH_REPORTER_GET
,
6355 info
->snd_portid
, info
->snd_seq
,
6362 err
= genlmsg_reply(msg
, info
);
6364 devlink_health_reporter_put(reporter
);
6369 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff
*msg
,
6370 struct netlink_callback
*cb
)
6372 struct devlink_health_reporter
*reporter
;
6373 struct devlink_port
*port
;
6374 struct devlink
*devlink
;
6375 int start
= cb
->args
[0];
6379 mutex_lock(&devlink_mutex
);
6380 list_for_each_entry(devlink
, &devlink_list
, list
) {
6381 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6383 mutex_lock(&devlink
->reporters_lock
);
6384 list_for_each_entry(reporter
, &devlink
->reporter_list
,
6390 err
= devlink_nl_health_reporter_fill(msg
, devlink
,
6392 DEVLINK_CMD_HEALTH_REPORTER_GET
,
6393 NETLINK_CB(cb
->skb
).portid
,
6397 mutex_unlock(&devlink
->reporters_lock
);
6402 mutex_unlock(&devlink
->reporters_lock
);
6405 list_for_each_entry(devlink
, &devlink_list
, list
) {
6406 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6408 mutex_lock(&devlink
->lock
);
6409 list_for_each_entry(port
, &devlink
->port_list
, list
) {
6410 mutex_lock(&port
->reporters_lock
);
6411 list_for_each_entry(reporter
, &port
->reporter_list
, list
) {
6416 err
= devlink_nl_health_reporter_fill(msg
, devlink
, reporter
,
6417 DEVLINK_CMD_HEALTH_REPORTER_GET
,
6418 NETLINK_CB(cb
->skb
).portid
,
6422 mutex_unlock(&port
->reporters_lock
);
6423 mutex_unlock(&devlink
->lock
);
6428 mutex_unlock(&port
->reporters_lock
);
6430 mutex_unlock(&devlink
->lock
);
6433 mutex_unlock(&devlink_mutex
);
6440 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff
*skb
,
6441 struct genl_info
*info
)
6443 struct devlink
*devlink
= info
->user_ptr
[0];
6444 struct devlink_health_reporter
*reporter
;
6447 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
6451 if (!reporter
->ops
->recover
&&
6452 (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] ||
6453 info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])) {
6457 if (!reporter
->ops
->dump
&&
6458 info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]) {
6463 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
])
6464 reporter
->graceful_period
=
6465 nla_get_u64(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
]);
6467 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
])
6468 reporter
->auto_recover
=
6469 nla_get_u8(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
]);
6471 if (info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
])
6472 reporter
->auto_dump
=
6473 nla_get_u8(info
->attrs
[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
]);
6475 devlink_health_reporter_put(reporter
);
6478 devlink_health_reporter_put(reporter
);
6482 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff
*skb
,
6483 struct genl_info
*info
)
6485 struct devlink
*devlink
= info
->user_ptr
[0];
6486 struct devlink_health_reporter
*reporter
;
6489 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
6493 err
= devlink_health_reporter_recover(reporter
, NULL
, info
->extack
);
6495 devlink_health_reporter_put(reporter
);
6499 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff
*skb
,
6500 struct genl_info
*info
)
6502 struct devlink
*devlink
= info
->user_ptr
[0];
6503 struct devlink_health_reporter
*reporter
;
6504 struct devlink_fmsg
*fmsg
;
6507 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
6511 if (!reporter
->ops
->diagnose
) {
6512 devlink_health_reporter_put(reporter
);
6516 fmsg
= devlink_fmsg_alloc();
6518 devlink_health_reporter_put(reporter
);
6522 err
= devlink_fmsg_obj_nest_start(fmsg
);
6526 err
= reporter
->ops
->diagnose(reporter
, fmsg
, info
->extack
);
6530 err
= devlink_fmsg_obj_nest_end(fmsg
);
6534 err
= devlink_fmsg_snd(fmsg
, info
,
6535 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
, 0);
6538 devlink_fmsg_free(fmsg
);
6539 devlink_health_reporter_put(reporter
);
6544 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff
*skb
,
6545 struct netlink_callback
*cb
)
6547 struct devlink_health_reporter
*reporter
;
6548 u64 start
= cb
->args
[0];
6551 reporter
= devlink_health_reporter_get_from_cb(cb
);
6555 if (!reporter
->ops
->dump
) {
6559 mutex_lock(&reporter
->dump_lock
);
6561 err
= devlink_health_do_dump(reporter
, NULL
, cb
->extack
);
6564 cb
->args
[1] = reporter
->dump_ts
;
6566 if (!reporter
->dump_fmsg
|| cb
->args
[1] != reporter
->dump_ts
) {
6567 NL_SET_ERR_MSG_MOD(cb
->extack
, "Dump trampled, please retry");
6572 err
= devlink_fmsg_dumpit(reporter
->dump_fmsg
, skb
, cb
,
6573 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
);
6575 mutex_unlock(&reporter
->dump_lock
);
6577 devlink_health_reporter_put(reporter
);
6582 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff
*skb
,
6583 struct genl_info
*info
)
6585 struct devlink
*devlink
= info
->user_ptr
[0];
6586 struct devlink_health_reporter
*reporter
;
6588 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
6592 if (!reporter
->ops
->dump
) {
6593 devlink_health_reporter_put(reporter
);
6597 mutex_lock(&reporter
->dump_lock
);
6598 devlink_health_dump_clear(reporter
);
6599 mutex_unlock(&reporter
->dump_lock
);
6600 devlink_health_reporter_put(reporter
);
6604 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff
*skb
,
6605 struct genl_info
*info
)
6607 struct devlink
*devlink
= info
->user_ptr
[0];
6608 struct devlink_health_reporter
*reporter
;
6611 reporter
= devlink_health_reporter_get_from_info(devlink
, info
);
6615 if (!reporter
->ops
->test
) {
6616 devlink_health_reporter_put(reporter
);
6620 err
= reporter
->ops
->test(reporter
, info
->extack
);
6622 devlink_health_reporter_put(reporter
);
6626 struct devlink_stats
{
6629 struct u64_stats_sync syncp
;
6633 * struct devlink_trap_policer_item - Packet trap policer attributes.
6634 * @policer: Immutable packet trap policer attributes.
6635 * @rate: Rate in packets / sec.
6636 * @burst: Burst size in packets.
6637 * @list: trap_policer_list member.
6639 * Describes packet trap policer attributes. Created by devlink during trap
6640 * policer registration.
6642 struct devlink_trap_policer_item
{
6643 const struct devlink_trap_policer
*policer
;
6646 struct list_head list
;
6650 * struct devlink_trap_group_item - Packet trap group attributes.
6651 * @group: Immutable packet trap group attributes.
6652 * @policer_item: Associated policer item. Can be NULL.
6653 * @list: trap_group_list member.
6654 * @stats: Trap group statistics.
6656 * Describes packet trap group attributes. Created by devlink during trap
6657 * group registration.
6659 struct devlink_trap_group_item
{
6660 const struct devlink_trap_group
*group
;
6661 struct devlink_trap_policer_item
*policer_item
;
6662 struct list_head list
;
6663 struct devlink_stats __percpu
*stats
;
6667 * struct devlink_trap_item - Packet trap attributes.
6668 * @trap: Immutable packet trap attributes.
6669 * @group_item: Associated group item.
6670 * @list: trap_list member.
6671 * @action: Trap action.
6672 * @stats: Trap statistics.
6673 * @priv: Driver private information.
6675 * Describes both mutable and immutable packet trap attributes. Created by
6676 * devlink during trap registration and used for all trap related operations.
6678 struct devlink_trap_item
{
6679 const struct devlink_trap
*trap
;
6680 struct devlink_trap_group_item
*group_item
;
6681 struct list_head list
;
6682 enum devlink_trap_action action
;
6683 struct devlink_stats __percpu
*stats
;
6687 static struct devlink_trap_policer_item
*
6688 devlink_trap_policer_item_lookup(struct devlink
*devlink
, u32 id
)
6690 struct devlink_trap_policer_item
*policer_item
;
6692 list_for_each_entry(policer_item
, &devlink
->trap_policer_list
, list
) {
6693 if (policer_item
->policer
->id
== id
)
6694 return policer_item
;
6700 static struct devlink_trap_item
*
6701 devlink_trap_item_lookup(struct devlink
*devlink
, const char *name
)
6703 struct devlink_trap_item
*trap_item
;
6705 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
6706 if (!strcmp(trap_item
->trap
->name
, name
))
6713 static struct devlink_trap_item
*
6714 devlink_trap_item_get_from_info(struct devlink
*devlink
,
6715 struct genl_info
*info
)
6717 struct nlattr
*attr
;
6719 if (!info
->attrs
[DEVLINK_ATTR_TRAP_NAME
])
6721 attr
= info
->attrs
[DEVLINK_ATTR_TRAP_NAME
];
6723 return devlink_trap_item_lookup(devlink
, nla_data(attr
));
6727 devlink_trap_action_get_from_info(struct genl_info
*info
,
6728 enum devlink_trap_action
*p_trap_action
)
6732 val
= nla_get_u8(info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
]);
6734 case DEVLINK_TRAP_ACTION_DROP
:
6735 case DEVLINK_TRAP_ACTION_TRAP
:
6736 case DEVLINK_TRAP_ACTION_MIRROR
:
6737 *p_trap_action
= val
;
6746 static int devlink_trap_metadata_put(struct sk_buff
*msg
,
6747 const struct devlink_trap
*trap
)
6749 struct nlattr
*attr
;
6751 attr
= nla_nest_start(msg
, DEVLINK_ATTR_TRAP_METADATA
);
6755 if ((trap
->metadata_cap
& DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT
) &&
6756 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT
))
6757 goto nla_put_failure
;
6758 if ((trap
->metadata_cap
& DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE
) &&
6759 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE
))
6760 goto nla_put_failure
;
6762 nla_nest_end(msg
, attr
);
6767 nla_nest_cancel(msg
, attr
);
6771 static void devlink_trap_stats_read(struct devlink_stats __percpu
*trap_stats
,
6772 struct devlink_stats
*stats
)
6776 memset(stats
, 0, sizeof(*stats
));
6777 for_each_possible_cpu(i
) {
6778 struct devlink_stats
*cpu_stats
;
6779 u64 rx_packets
, rx_bytes
;
6782 cpu_stats
= per_cpu_ptr(trap_stats
, i
);
6784 start
= u64_stats_fetch_begin_irq(&cpu_stats
->syncp
);
6785 rx_packets
= cpu_stats
->rx_packets
;
6786 rx_bytes
= cpu_stats
->rx_bytes
;
6787 } while (u64_stats_fetch_retry_irq(&cpu_stats
->syncp
, start
));
6789 stats
->rx_packets
+= rx_packets
;
6790 stats
->rx_bytes
+= rx_bytes
;
6794 static int devlink_trap_stats_put(struct sk_buff
*msg
,
6795 struct devlink_stats __percpu
*trap_stats
)
6797 struct devlink_stats stats
;
6798 struct nlattr
*attr
;
6800 devlink_trap_stats_read(trap_stats
, &stats
);
6802 attr
= nla_nest_start(msg
, DEVLINK_ATTR_STATS
);
6806 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_PACKETS
,
6807 stats
.rx_packets
, DEVLINK_ATTR_PAD
))
6808 goto nla_put_failure
;
6810 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_BYTES
,
6811 stats
.rx_bytes
, DEVLINK_ATTR_PAD
))
6812 goto nla_put_failure
;
6814 nla_nest_end(msg
, attr
);
6819 nla_nest_cancel(msg
, attr
);
6823 static int devlink_nl_trap_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
6824 const struct devlink_trap_item
*trap_item
,
6825 enum devlink_command cmd
, u32 portid
, u32 seq
,
6828 struct devlink_trap_group_item
*group_item
= trap_item
->group_item
;
6832 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
6836 if (devlink_nl_put_handle(msg
, devlink
))
6837 goto nla_put_failure
;
6839 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
6840 group_item
->group
->name
))
6841 goto nla_put_failure
;
6843 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_NAME
, trap_item
->trap
->name
))
6844 goto nla_put_failure
;
6846 if (nla_put_u8(msg
, DEVLINK_ATTR_TRAP_TYPE
, trap_item
->trap
->type
))
6847 goto nla_put_failure
;
6849 if (trap_item
->trap
->generic
&&
6850 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_GENERIC
))
6851 goto nla_put_failure
;
6853 if (nla_put_u8(msg
, DEVLINK_ATTR_TRAP_ACTION
, trap_item
->action
))
6854 goto nla_put_failure
;
6856 err
= devlink_trap_metadata_put(msg
, trap_item
->trap
);
6858 goto nla_put_failure
;
6860 err
= devlink_trap_stats_put(msg
, trap_item
->stats
);
6862 goto nla_put_failure
;
6864 genlmsg_end(msg
, hdr
);
6869 genlmsg_cancel(msg
, hdr
);
6873 static int devlink_nl_cmd_trap_get_doit(struct sk_buff
*skb
,
6874 struct genl_info
*info
)
6876 struct netlink_ext_ack
*extack
= info
->extack
;
6877 struct devlink
*devlink
= info
->user_ptr
[0];
6878 struct devlink_trap_item
*trap_item
;
6879 struct sk_buff
*msg
;
6882 if (list_empty(&devlink
->trap_list
))
6885 trap_item
= devlink_trap_item_get_from_info(devlink
, info
);
6887 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap");
6891 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
6895 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
,
6896 DEVLINK_CMD_TRAP_NEW
, info
->snd_portid
,
6901 return genlmsg_reply(msg
, info
);
6908 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff
*msg
,
6909 struct netlink_callback
*cb
)
6911 struct devlink_trap_item
*trap_item
;
6912 struct devlink
*devlink
;
6913 int start
= cb
->args
[0];
6917 mutex_lock(&devlink_mutex
);
6918 list_for_each_entry(devlink
, &devlink_list
, list
) {
6919 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
6921 mutex_lock(&devlink
->lock
);
6922 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
6927 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
,
6928 DEVLINK_CMD_TRAP_NEW
,
6929 NETLINK_CB(cb
->skb
).portid
,
6933 mutex_unlock(&devlink
->lock
);
6938 mutex_unlock(&devlink
->lock
);
6941 mutex_unlock(&devlink_mutex
);
6947 static int __devlink_trap_action_set(struct devlink
*devlink
,
6948 struct devlink_trap_item
*trap_item
,
6949 enum devlink_trap_action trap_action
,
6950 struct netlink_ext_ack
*extack
)
6954 if (trap_item
->action
!= trap_action
&&
6955 trap_item
->trap
->type
!= DEVLINK_TRAP_TYPE_DROP
) {
6956 NL_SET_ERR_MSG_MOD(extack
, "Cannot change action of non-drop traps. Skipping");
6960 err
= devlink
->ops
->trap_action_set(devlink
, trap_item
->trap
,
6961 trap_action
, extack
);
6965 trap_item
->action
= trap_action
;
6970 static int devlink_trap_action_set(struct devlink
*devlink
,
6971 struct devlink_trap_item
*trap_item
,
6972 struct genl_info
*info
)
6974 enum devlink_trap_action trap_action
;
6977 if (!info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
])
6980 err
= devlink_trap_action_get_from_info(info
, &trap_action
);
6982 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid trap action");
6986 return __devlink_trap_action_set(devlink
, trap_item
, trap_action
,
6990 static int devlink_nl_cmd_trap_set_doit(struct sk_buff
*skb
,
6991 struct genl_info
*info
)
6993 struct netlink_ext_ack
*extack
= info
->extack
;
6994 struct devlink
*devlink
= info
->user_ptr
[0];
6995 struct devlink_trap_item
*trap_item
;
6997 if (list_empty(&devlink
->trap_list
))
7000 trap_item
= devlink_trap_item_get_from_info(devlink
, info
);
7002 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap");
7006 return devlink_trap_action_set(devlink
, trap_item
, info
);
7009 static struct devlink_trap_group_item
*
7010 devlink_trap_group_item_lookup(struct devlink
*devlink
, const char *name
)
7012 struct devlink_trap_group_item
*group_item
;
7014 list_for_each_entry(group_item
, &devlink
->trap_group_list
, list
) {
7015 if (!strcmp(group_item
->group
->name
, name
))
7022 static struct devlink_trap_group_item
*
7023 devlink_trap_group_item_lookup_by_id(struct devlink
*devlink
, u16 id
)
7025 struct devlink_trap_group_item
*group_item
;
7027 list_for_each_entry(group_item
, &devlink
->trap_group_list
, list
) {
7028 if (group_item
->group
->id
== id
)
7035 static struct devlink_trap_group_item
*
7036 devlink_trap_group_item_get_from_info(struct devlink
*devlink
,
7037 struct genl_info
*info
)
7041 if (!info
->attrs
[DEVLINK_ATTR_TRAP_GROUP_NAME
])
7043 name
= nla_data(info
->attrs
[DEVLINK_ATTR_TRAP_GROUP_NAME
]);
7045 return devlink_trap_group_item_lookup(devlink
, name
);
7049 devlink_nl_trap_group_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
7050 const struct devlink_trap_group_item
*group_item
,
7051 enum devlink_command cmd
, u32 portid
, u32 seq
,
7057 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
7061 if (devlink_nl_put_handle(msg
, devlink
))
7062 goto nla_put_failure
;
7064 if (nla_put_string(msg
, DEVLINK_ATTR_TRAP_GROUP_NAME
,
7065 group_item
->group
->name
))
7066 goto nla_put_failure
;
7068 if (group_item
->group
->generic
&&
7069 nla_put_flag(msg
, DEVLINK_ATTR_TRAP_GENERIC
))
7070 goto nla_put_failure
;
7072 if (group_item
->policer_item
&&
7073 nla_put_u32(msg
, DEVLINK_ATTR_TRAP_POLICER_ID
,
7074 group_item
->policer_item
->policer
->id
))
7075 goto nla_put_failure
;
7077 err
= devlink_trap_stats_put(msg
, group_item
->stats
);
7079 goto nla_put_failure
;
7081 genlmsg_end(msg
, hdr
);
7086 genlmsg_cancel(msg
, hdr
);
7090 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff
*skb
,
7091 struct genl_info
*info
)
7093 struct netlink_ext_ack
*extack
= info
->extack
;
7094 struct devlink
*devlink
= info
->user_ptr
[0];
7095 struct devlink_trap_group_item
*group_item
;
7096 struct sk_buff
*msg
;
7099 if (list_empty(&devlink
->trap_group_list
))
7102 group_item
= devlink_trap_group_item_get_from_info(devlink
, info
);
7104 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap group");
7108 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
7112 err
= devlink_nl_trap_group_fill(msg
, devlink
, group_item
,
7113 DEVLINK_CMD_TRAP_GROUP_NEW
,
7114 info
->snd_portid
, info
->snd_seq
, 0);
7116 goto err_trap_group_fill
;
7118 return genlmsg_reply(msg
, info
);
7120 err_trap_group_fill
:
7125 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff
*msg
,
7126 struct netlink_callback
*cb
)
7128 enum devlink_command cmd
= DEVLINK_CMD_TRAP_GROUP_NEW
;
7129 struct devlink_trap_group_item
*group_item
;
7130 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
7131 struct devlink
*devlink
;
7132 int start
= cb
->args
[0];
7136 mutex_lock(&devlink_mutex
);
7137 list_for_each_entry(devlink
, &devlink_list
, list
) {
7138 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
7140 mutex_lock(&devlink
->lock
);
7141 list_for_each_entry(group_item
, &devlink
->trap_group_list
,
7147 err
= devlink_nl_trap_group_fill(msg
, devlink
,
7153 mutex_unlock(&devlink
->lock
);
7158 mutex_unlock(&devlink
->lock
);
7161 mutex_unlock(&devlink_mutex
);
7168 __devlink_trap_group_action_set(struct devlink
*devlink
,
7169 struct devlink_trap_group_item
*group_item
,
7170 enum devlink_trap_action trap_action
,
7171 struct netlink_ext_ack
*extack
)
7173 const char *group_name
= group_item
->group
->name
;
7174 struct devlink_trap_item
*trap_item
;
7177 if (devlink
->ops
->trap_group_action_set
) {
7178 err
= devlink
->ops
->trap_group_action_set(devlink
, group_item
->group
,
7179 trap_action
, extack
);
7183 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
7184 if (strcmp(trap_item
->group_item
->group
->name
, group_name
))
7186 if (trap_item
->action
!= trap_action
&&
7187 trap_item
->trap
->type
!= DEVLINK_TRAP_TYPE_DROP
)
7189 trap_item
->action
= trap_action
;
7195 list_for_each_entry(trap_item
, &devlink
->trap_list
, list
) {
7196 if (strcmp(trap_item
->group_item
->group
->name
, group_name
))
7198 err
= __devlink_trap_action_set(devlink
, trap_item
,
7199 trap_action
, extack
);
7208 devlink_trap_group_action_set(struct devlink
*devlink
,
7209 struct devlink_trap_group_item
*group_item
,
7210 struct genl_info
*info
, bool *p_modified
)
7212 enum devlink_trap_action trap_action
;
7215 if (!info
->attrs
[DEVLINK_ATTR_TRAP_ACTION
])
7218 err
= devlink_trap_action_get_from_info(info
, &trap_action
);
7220 NL_SET_ERR_MSG_MOD(info
->extack
, "Invalid trap action");
7224 err
= __devlink_trap_group_action_set(devlink
, group_item
, trap_action
,
7234 static int devlink_trap_group_set(struct devlink
*devlink
,
7235 struct devlink_trap_group_item
*group_item
,
7236 struct genl_info
*info
)
7238 struct devlink_trap_policer_item
*policer_item
;
7239 struct netlink_ext_ack
*extack
= info
->extack
;
7240 const struct devlink_trap_policer
*policer
;
7241 struct nlattr
**attrs
= info
->attrs
;
7244 if (!attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7247 if (!devlink
->ops
->trap_group_set
)
7250 policer_item
= group_item
->policer_item
;
7251 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]) {
7254 policer_id
= nla_get_u32(attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]);
7255 policer_item
= devlink_trap_policer_item_lookup(devlink
,
7257 if (policer_id
&& !policer_item
) {
7258 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
7262 policer
= policer_item
? policer_item
->policer
: NULL
;
7264 err
= devlink
->ops
->trap_group_set(devlink
, group_item
->group
, policer
,
7269 group_item
->policer_item
= policer_item
;
7274 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff
*skb
,
7275 struct genl_info
*info
)
7277 struct netlink_ext_ack
*extack
= info
->extack
;
7278 struct devlink
*devlink
= info
->user_ptr
[0];
7279 struct devlink_trap_group_item
*group_item
;
7280 bool modified
= false;
7283 if (list_empty(&devlink
->trap_group_list
))
7286 group_item
= devlink_trap_group_item_get_from_info(devlink
, info
);
7288 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap group");
7292 err
= devlink_trap_group_action_set(devlink
, group_item
, info
,
7297 err
= devlink_trap_group_set(devlink
, group_item
, info
);
7299 goto err_trap_group_set
;
7305 NL_SET_ERR_MSG_MOD(extack
, "Trap group set failed, but some changes were committed already");
7309 static struct devlink_trap_policer_item
*
7310 devlink_trap_policer_item_get_from_info(struct devlink
*devlink
,
7311 struct genl_info
*info
)
7315 if (!info
->attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
])
7317 id
= nla_get_u32(info
->attrs
[DEVLINK_ATTR_TRAP_POLICER_ID
]);
7319 return devlink_trap_policer_item_lookup(devlink
, id
);
7323 devlink_trap_policer_stats_put(struct sk_buff
*msg
, struct devlink
*devlink
,
7324 const struct devlink_trap_policer
*policer
)
7326 struct nlattr
*attr
;
7330 if (!devlink
->ops
->trap_policer_counter_get
)
7333 err
= devlink
->ops
->trap_policer_counter_get(devlink
, policer
, &drops
);
7337 attr
= nla_nest_start(msg
, DEVLINK_ATTR_STATS
);
7341 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_STATS_RX_DROPPED
, drops
,
7343 goto nla_put_failure
;
7345 nla_nest_end(msg
, attr
);
7350 nla_nest_cancel(msg
, attr
);
7355 devlink_nl_trap_policer_fill(struct sk_buff
*msg
, struct devlink
*devlink
,
7356 const struct devlink_trap_policer_item
*policer_item
,
7357 enum devlink_command cmd
, u32 portid
, u32 seq
,
7363 hdr
= genlmsg_put(msg
, portid
, seq
, &devlink_nl_family
, flags
, cmd
);
7367 if (devlink_nl_put_handle(msg
, devlink
))
7368 goto nla_put_failure
;
7370 if (nla_put_u32(msg
, DEVLINK_ATTR_TRAP_POLICER_ID
,
7371 policer_item
->policer
->id
))
7372 goto nla_put_failure
;
7374 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_TRAP_POLICER_RATE
,
7375 policer_item
->rate
, DEVLINK_ATTR_PAD
))
7376 goto nla_put_failure
;
7378 if (nla_put_u64_64bit(msg
, DEVLINK_ATTR_TRAP_POLICER_BURST
,
7379 policer_item
->burst
, DEVLINK_ATTR_PAD
))
7380 goto nla_put_failure
;
7382 err
= devlink_trap_policer_stats_put(msg
, devlink
,
7383 policer_item
->policer
);
7385 goto nla_put_failure
;
7387 genlmsg_end(msg
, hdr
);
7392 genlmsg_cancel(msg
, hdr
);
7396 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff
*skb
,
7397 struct genl_info
*info
)
7399 struct devlink_trap_policer_item
*policer_item
;
7400 struct netlink_ext_ack
*extack
= info
->extack
;
7401 struct devlink
*devlink
= info
->user_ptr
[0];
7402 struct sk_buff
*msg
;
7405 if (list_empty(&devlink
->trap_policer_list
))
7408 policer_item
= devlink_trap_policer_item_get_from_info(devlink
, info
);
7409 if (!policer_item
) {
7410 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
7414 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
7418 err
= devlink_nl_trap_policer_fill(msg
, devlink
, policer_item
,
7419 DEVLINK_CMD_TRAP_POLICER_NEW
,
7420 info
->snd_portid
, info
->snd_seq
, 0);
7422 goto err_trap_policer_fill
;
7424 return genlmsg_reply(msg
, info
);
7426 err_trap_policer_fill
:
7431 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff
*msg
,
7432 struct netlink_callback
*cb
)
7434 enum devlink_command cmd
= DEVLINK_CMD_TRAP_POLICER_NEW
;
7435 struct devlink_trap_policer_item
*policer_item
;
7436 u32 portid
= NETLINK_CB(cb
->skb
).portid
;
7437 struct devlink
*devlink
;
7438 int start
= cb
->args
[0];
7442 mutex_lock(&devlink_mutex
);
7443 list_for_each_entry(devlink
, &devlink_list
, list
) {
7444 if (!net_eq(devlink_net(devlink
), sock_net(msg
->sk
)))
7446 mutex_lock(&devlink
->lock
);
7447 list_for_each_entry(policer_item
, &devlink
->trap_policer_list
,
7453 err
= devlink_nl_trap_policer_fill(msg
, devlink
,
7459 mutex_unlock(&devlink
->lock
);
7464 mutex_unlock(&devlink
->lock
);
7467 mutex_unlock(&devlink_mutex
);
7474 devlink_trap_policer_set(struct devlink
*devlink
,
7475 struct devlink_trap_policer_item
*policer_item
,
7476 struct genl_info
*info
)
7478 struct netlink_ext_ack
*extack
= info
->extack
;
7479 struct nlattr
**attrs
= info
->attrs
;
7483 rate
= policer_item
->rate
;
7484 burst
= policer_item
->burst
;
7486 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_RATE
])
7487 rate
= nla_get_u64(attrs
[DEVLINK_ATTR_TRAP_POLICER_RATE
]);
7489 if (attrs
[DEVLINK_ATTR_TRAP_POLICER_BURST
])
7490 burst
= nla_get_u64(attrs
[DEVLINK_ATTR_TRAP_POLICER_BURST
]);
7492 if (rate
< policer_item
->policer
->min_rate
) {
7493 NL_SET_ERR_MSG_MOD(extack
, "Policer rate lower than limit");
7497 if (rate
> policer_item
->policer
->max_rate
) {
7498 NL_SET_ERR_MSG_MOD(extack
, "Policer rate higher than limit");
7502 if (burst
< policer_item
->policer
->min_burst
) {
7503 NL_SET_ERR_MSG_MOD(extack
, "Policer burst size lower than limit");
7507 if (burst
> policer_item
->policer
->max_burst
) {
7508 NL_SET_ERR_MSG_MOD(extack
, "Policer burst size higher than limit");
7512 err
= devlink
->ops
->trap_policer_set(devlink
, policer_item
->policer
,
7513 rate
, burst
, info
->extack
);
7517 policer_item
->rate
= rate
;
7518 policer_item
->burst
= burst
;
7523 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff
*skb
,
7524 struct genl_info
*info
)
7526 struct devlink_trap_policer_item
*policer_item
;
7527 struct netlink_ext_ack
*extack
= info
->extack
;
7528 struct devlink
*devlink
= info
->user_ptr
[0];
7530 if (list_empty(&devlink
->trap_policer_list
))
7533 if (!devlink
->ops
->trap_policer_set
)
7536 policer_item
= devlink_trap_policer_item_get_from_info(devlink
, info
);
7537 if (!policer_item
) {
7538 NL_SET_ERR_MSG_MOD(extack
, "Device did not register this trap policer");
7542 return devlink_trap_policer_set(devlink
, policer_item
, info
);
7545 static const struct nla_policy devlink_nl_policy
[DEVLINK_ATTR_MAX
+ 1] = {
7546 [DEVLINK_ATTR_UNSPEC
] = { .strict_start_type
=
7547 DEVLINK_ATTR_TRAP_POLICER_ID
},
7548 [DEVLINK_ATTR_BUS_NAME
] = { .type
= NLA_NUL_STRING
},
7549 [DEVLINK_ATTR_DEV_NAME
] = { .type
= NLA_NUL_STRING
},
7550 [DEVLINK_ATTR_PORT_INDEX
] = { .type
= NLA_U32
},
7551 [DEVLINK_ATTR_PORT_TYPE
] = NLA_POLICY_RANGE(NLA_U16
, DEVLINK_PORT_TYPE_AUTO
,
7552 DEVLINK_PORT_TYPE_IB
),
7553 [DEVLINK_ATTR_PORT_SPLIT_COUNT
] = { .type
= NLA_U32
},
7554 [DEVLINK_ATTR_SB_INDEX
] = { .type
= NLA_U32
},
7555 [DEVLINK_ATTR_SB_POOL_INDEX
] = { .type
= NLA_U16
},
7556 [DEVLINK_ATTR_SB_POOL_TYPE
] = { .type
= NLA_U8
},
7557 [DEVLINK_ATTR_SB_POOL_SIZE
] = { .type
= NLA_U32
},
7558 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE
] = { .type
= NLA_U8
},
7559 [DEVLINK_ATTR_SB_THRESHOLD
] = { .type
= NLA_U32
},
7560 [DEVLINK_ATTR_SB_TC_INDEX
] = { .type
= NLA_U16
},
7561 [DEVLINK_ATTR_ESWITCH_MODE
] = NLA_POLICY_RANGE(NLA_U16
, DEVLINK_ESWITCH_MODE_LEGACY
,
7562 DEVLINK_ESWITCH_MODE_SWITCHDEV
),
7563 [DEVLINK_ATTR_ESWITCH_INLINE_MODE
] = { .type
= NLA_U8
},
7564 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE
] = { .type
= NLA_U8
},
7565 [DEVLINK_ATTR_DPIPE_TABLE_NAME
] = { .type
= NLA_NUL_STRING
},
7566 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED
] = { .type
= NLA_U8
},
7567 [DEVLINK_ATTR_RESOURCE_ID
] = { .type
= NLA_U64
},
7568 [DEVLINK_ATTR_RESOURCE_SIZE
] = { .type
= NLA_U64
},
7569 [DEVLINK_ATTR_PARAM_NAME
] = { .type
= NLA_NUL_STRING
},
7570 [DEVLINK_ATTR_PARAM_TYPE
] = { .type
= NLA_U8
},
7571 [DEVLINK_ATTR_PARAM_VALUE_CMODE
] = { .type
= NLA_U8
},
7572 [DEVLINK_ATTR_REGION_NAME
] = { .type
= NLA_NUL_STRING
},
7573 [DEVLINK_ATTR_REGION_SNAPSHOT_ID
] = { .type
= NLA_U32
},
7574 [DEVLINK_ATTR_REGION_CHUNK_ADDR
] = { .type
= NLA_U64
},
7575 [DEVLINK_ATTR_REGION_CHUNK_LEN
] = { .type
= NLA_U64
},
7576 [DEVLINK_ATTR_HEALTH_REPORTER_NAME
] = { .type
= NLA_NUL_STRING
},
7577 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD
] = { .type
= NLA_U64
},
7578 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER
] = { .type
= NLA_U8
},
7579 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME
] = { .type
= NLA_NUL_STRING
},
7580 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT
] = { .type
= NLA_NUL_STRING
},
7581 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK
] =
7582 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS
),
7583 [DEVLINK_ATTR_TRAP_NAME
] = { .type
= NLA_NUL_STRING
},
7584 [DEVLINK_ATTR_TRAP_ACTION
] = { .type
= NLA_U8
},
7585 [DEVLINK_ATTR_TRAP_GROUP_NAME
] = { .type
= NLA_NUL_STRING
},
7586 [DEVLINK_ATTR_NETNS_PID
] = { .type
= NLA_U32
},
7587 [DEVLINK_ATTR_NETNS_FD
] = { .type
= NLA_U32
},
7588 [DEVLINK_ATTR_NETNS_ID
] = { .type
= NLA_U32
},
7589 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP
] = { .type
= NLA_U8
},
7590 [DEVLINK_ATTR_TRAP_POLICER_ID
] = { .type
= NLA_U32
},
7591 [DEVLINK_ATTR_TRAP_POLICER_RATE
] = { .type
= NLA_U64
},
7592 [DEVLINK_ATTR_TRAP_POLICER_BURST
] = { .type
= NLA_U64
},
7593 [DEVLINK_ATTR_PORT_FUNCTION
] = { .type
= NLA_NESTED
},
7594 [DEVLINK_ATTR_RELOAD_ACTION
] = NLA_POLICY_RANGE(NLA_U8
, DEVLINK_RELOAD_ACTION_DRIVER_REINIT
,
7595 DEVLINK_RELOAD_ACTION_MAX
),
7596 [DEVLINK_ATTR_RELOAD_LIMITS
] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK
),
7599 static const struct genl_small_ops devlink_nl_ops
[] = {
7601 .cmd
= DEVLINK_CMD_GET
,
7602 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7603 .doit
= devlink_nl_cmd_get_doit
,
7604 .dumpit
= devlink_nl_cmd_get_dumpit
,
7605 /* can be retrieved by unprivileged users */
7608 .cmd
= DEVLINK_CMD_PORT_GET
,
7609 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7610 .doit
= devlink_nl_cmd_port_get_doit
,
7611 .dumpit
= devlink_nl_cmd_port_get_dumpit
,
7612 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7613 /* can be retrieved by unprivileged users */
7616 .cmd
= DEVLINK_CMD_PORT_SET
,
7617 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7618 .doit
= devlink_nl_cmd_port_set_doit
,
7619 .flags
= GENL_ADMIN_PERM
,
7620 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7623 .cmd
= DEVLINK_CMD_PORT_SPLIT
,
7624 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7625 .doit
= devlink_nl_cmd_port_split_doit
,
7626 .flags
= GENL_ADMIN_PERM
,
7627 .internal_flags
= DEVLINK_NL_FLAG_NO_LOCK
,
7630 .cmd
= DEVLINK_CMD_PORT_UNSPLIT
,
7631 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7632 .doit
= devlink_nl_cmd_port_unsplit_doit
,
7633 .flags
= GENL_ADMIN_PERM
,
7634 .internal_flags
= DEVLINK_NL_FLAG_NO_LOCK
,
7637 .cmd
= DEVLINK_CMD_SB_GET
,
7638 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7639 .doit
= devlink_nl_cmd_sb_get_doit
,
7640 .dumpit
= devlink_nl_cmd_sb_get_dumpit
,
7641 /* can be retrieved by unprivileged users */
7644 .cmd
= DEVLINK_CMD_SB_POOL_GET
,
7645 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7646 .doit
= devlink_nl_cmd_sb_pool_get_doit
,
7647 .dumpit
= devlink_nl_cmd_sb_pool_get_dumpit
,
7648 /* can be retrieved by unprivileged users */
7651 .cmd
= DEVLINK_CMD_SB_POOL_SET
,
7652 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7653 .doit
= devlink_nl_cmd_sb_pool_set_doit
,
7654 .flags
= GENL_ADMIN_PERM
,
7657 .cmd
= DEVLINK_CMD_SB_PORT_POOL_GET
,
7658 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7659 .doit
= devlink_nl_cmd_sb_port_pool_get_doit
,
7660 .dumpit
= devlink_nl_cmd_sb_port_pool_get_dumpit
,
7661 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7662 /* can be retrieved by unprivileged users */
7665 .cmd
= DEVLINK_CMD_SB_PORT_POOL_SET
,
7666 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7667 .doit
= devlink_nl_cmd_sb_port_pool_set_doit
,
7668 .flags
= GENL_ADMIN_PERM
,
7669 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7672 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_GET
,
7673 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7674 .doit
= devlink_nl_cmd_sb_tc_pool_bind_get_doit
,
7675 .dumpit
= devlink_nl_cmd_sb_tc_pool_bind_get_dumpit
,
7676 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7677 /* can be retrieved by unprivileged users */
7680 .cmd
= DEVLINK_CMD_SB_TC_POOL_BIND_SET
,
7681 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7682 .doit
= devlink_nl_cmd_sb_tc_pool_bind_set_doit
,
7683 .flags
= GENL_ADMIN_PERM
,
7684 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7687 .cmd
= DEVLINK_CMD_SB_OCC_SNAPSHOT
,
7688 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7689 .doit
= devlink_nl_cmd_sb_occ_snapshot_doit
,
7690 .flags
= GENL_ADMIN_PERM
,
7693 .cmd
= DEVLINK_CMD_SB_OCC_MAX_CLEAR
,
7694 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7695 .doit
= devlink_nl_cmd_sb_occ_max_clear_doit
,
7696 .flags
= GENL_ADMIN_PERM
,
7699 .cmd
= DEVLINK_CMD_ESWITCH_GET
,
7700 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7701 .doit
= devlink_nl_cmd_eswitch_get_doit
,
7702 .flags
= GENL_ADMIN_PERM
,
7703 .internal_flags
= DEVLINK_NL_FLAG_NO_LOCK
,
7706 .cmd
= DEVLINK_CMD_ESWITCH_SET
,
7707 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7708 .doit
= devlink_nl_cmd_eswitch_set_doit
,
7709 .flags
= GENL_ADMIN_PERM
,
7710 .internal_flags
= DEVLINK_NL_FLAG_NO_LOCK
,
7713 .cmd
= DEVLINK_CMD_DPIPE_TABLE_GET
,
7714 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7715 .doit
= devlink_nl_cmd_dpipe_table_get
,
7716 /* can be retrieved by unprivileged users */
7719 .cmd
= DEVLINK_CMD_DPIPE_ENTRIES_GET
,
7720 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7721 .doit
= devlink_nl_cmd_dpipe_entries_get
,
7722 /* can be retrieved by unprivileged users */
7725 .cmd
= DEVLINK_CMD_DPIPE_HEADERS_GET
,
7726 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7727 .doit
= devlink_nl_cmd_dpipe_headers_get
,
7728 /* can be retrieved by unprivileged users */
7731 .cmd
= DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET
,
7732 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7733 .doit
= devlink_nl_cmd_dpipe_table_counters_set
,
7734 .flags
= GENL_ADMIN_PERM
,
7737 .cmd
= DEVLINK_CMD_RESOURCE_SET
,
7738 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7739 .doit
= devlink_nl_cmd_resource_set
,
7740 .flags
= GENL_ADMIN_PERM
,
7743 .cmd
= DEVLINK_CMD_RESOURCE_DUMP
,
7744 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7745 .doit
= devlink_nl_cmd_resource_dump
,
7746 /* can be retrieved by unprivileged users */
7749 .cmd
= DEVLINK_CMD_RELOAD
,
7750 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7751 .doit
= devlink_nl_cmd_reload
,
7752 .flags
= GENL_ADMIN_PERM
,
7753 .internal_flags
= DEVLINK_NL_FLAG_NO_LOCK
,
7756 .cmd
= DEVLINK_CMD_PARAM_GET
,
7757 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7758 .doit
= devlink_nl_cmd_param_get_doit
,
7759 .dumpit
= devlink_nl_cmd_param_get_dumpit
,
7760 /* can be retrieved by unprivileged users */
7763 .cmd
= DEVLINK_CMD_PARAM_SET
,
7764 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7765 .doit
= devlink_nl_cmd_param_set_doit
,
7766 .flags
= GENL_ADMIN_PERM
,
7769 .cmd
= DEVLINK_CMD_PORT_PARAM_GET
,
7770 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7771 .doit
= devlink_nl_cmd_port_param_get_doit
,
7772 .dumpit
= devlink_nl_cmd_port_param_get_dumpit
,
7773 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7774 /* can be retrieved by unprivileged users */
7777 .cmd
= DEVLINK_CMD_PORT_PARAM_SET
,
7778 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7779 .doit
= devlink_nl_cmd_port_param_set_doit
,
7780 .flags
= GENL_ADMIN_PERM
,
7781 .internal_flags
= DEVLINK_NL_FLAG_NEED_PORT
,
7784 .cmd
= DEVLINK_CMD_REGION_GET
,
7785 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7786 .doit
= devlink_nl_cmd_region_get_doit
,
7787 .dumpit
= devlink_nl_cmd_region_get_dumpit
,
7788 .flags
= GENL_ADMIN_PERM
,
7791 .cmd
= DEVLINK_CMD_REGION_NEW
,
7792 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7793 .doit
= devlink_nl_cmd_region_new
,
7794 .flags
= GENL_ADMIN_PERM
,
7797 .cmd
= DEVLINK_CMD_REGION_DEL
,
7798 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7799 .doit
= devlink_nl_cmd_region_del
,
7800 .flags
= GENL_ADMIN_PERM
,
7803 .cmd
= DEVLINK_CMD_REGION_READ
,
7804 .validate
= GENL_DONT_VALIDATE_STRICT
|
7805 GENL_DONT_VALIDATE_DUMP_STRICT
,
7806 .dumpit
= devlink_nl_cmd_region_read_dumpit
,
7807 .flags
= GENL_ADMIN_PERM
,
7810 .cmd
= DEVLINK_CMD_INFO_GET
,
7811 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7812 .doit
= devlink_nl_cmd_info_get_doit
,
7813 .dumpit
= devlink_nl_cmd_info_get_dumpit
,
7814 /* can be retrieved by unprivileged users */
7817 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_GET
,
7818 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7819 .doit
= devlink_nl_cmd_health_reporter_get_doit
,
7820 .dumpit
= devlink_nl_cmd_health_reporter_get_dumpit
,
7821 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7822 DEVLINK_NL_FLAG_NO_LOCK
,
7823 /* can be retrieved by unprivileged users */
7826 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_SET
,
7827 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7828 .doit
= devlink_nl_cmd_health_reporter_set_doit
,
7829 .flags
= GENL_ADMIN_PERM
,
7830 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7831 DEVLINK_NL_FLAG_NO_LOCK
,
7834 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_RECOVER
,
7835 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7836 .doit
= devlink_nl_cmd_health_reporter_recover_doit
,
7837 .flags
= GENL_ADMIN_PERM
,
7838 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7839 DEVLINK_NL_FLAG_NO_LOCK
,
7842 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE
,
7843 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7844 .doit
= devlink_nl_cmd_health_reporter_diagnose_doit
,
7845 .flags
= GENL_ADMIN_PERM
,
7846 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7847 DEVLINK_NL_FLAG_NO_LOCK
,
7850 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET
,
7851 .validate
= GENL_DONT_VALIDATE_STRICT
|
7852 GENL_DONT_VALIDATE_DUMP_STRICT
,
7853 .dumpit
= devlink_nl_cmd_health_reporter_dump_get_dumpit
,
7854 .flags
= GENL_ADMIN_PERM
,
7855 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7856 DEVLINK_NL_FLAG_NO_LOCK
,
7859 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR
,
7860 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7861 .doit
= devlink_nl_cmd_health_reporter_dump_clear_doit
,
7862 .flags
= GENL_ADMIN_PERM
,
7863 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7864 DEVLINK_NL_FLAG_NO_LOCK
,
7867 .cmd
= DEVLINK_CMD_HEALTH_REPORTER_TEST
,
7868 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7869 .doit
= devlink_nl_cmd_health_reporter_test_doit
,
7870 .flags
= GENL_ADMIN_PERM
,
7871 .internal_flags
= DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT
|
7872 DEVLINK_NL_FLAG_NO_LOCK
,
7875 .cmd
= DEVLINK_CMD_FLASH_UPDATE
,
7876 .validate
= GENL_DONT_VALIDATE_STRICT
| GENL_DONT_VALIDATE_DUMP
,
7877 .doit
= devlink_nl_cmd_flash_update
,
7878 .flags
= GENL_ADMIN_PERM
,
7881 .cmd
= DEVLINK_CMD_TRAP_GET
,
7882 .doit
= devlink_nl_cmd_trap_get_doit
,
7883 .dumpit
= devlink_nl_cmd_trap_get_dumpit
,
7884 /* can be retrieved by unprivileged users */
7887 .cmd
= DEVLINK_CMD_TRAP_SET
,
7888 .doit
= devlink_nl_cmd_trap_set_doit
,
7889 .flags
= GENL_ADMIN_PERM
,
7892 .cmd
= DEVLINK_CMD_TRAP_GROUP_GET
,
7893 .doit
= devlink_nl_cmd_trap_group_get_doit
,
7894 .dumpit
= devlink_nl_cmd_trap_group_get_dumpit
,
7895 /* can be retrieved by unprivileged users */
7898 .cmd
= DEVLINK_CMD_TRAP_GROUP_SET
,
7899 .doit
= devlink_nl_cmd_trap_group_set_doit
,
7900 .flags
= GENL_ADMIN_PERM
,
7903 .cmd
= DEVLINK_CMD_TRAP_POLICER_GET
,
7904 .doit
= devlink_nl_cmd_trap_policer_get_doit
,
7905 .dumpit
= devlink_nl_cmd_trap_policer_get_dumpit
,
7906 /* can be retrieved by unprivileged users */
7909 .cmd
= DEVLINK_CMD_TRAP_POLICER_SET
,
7910 .doit
= devlink_nl_cmd_trap_policer_set_doit
,
7911 .flags
= GENL_ADMIN_PERM
,
7915 static struct genl_family devlink_nl_family __ro_after_init
= {
7916 .name
= DEVLINK_GENL_NAME
,
7917 .version
= DEVLINK_GENL_VERSION
,
7918 .maxattr
= DEVLINK_ATTR_MAX
,
7919 .policy
= devlink_nl_policy
,
7921 .pre_doit
= devlink_nl_pre_doit
,
7922 .post_doit
= devlink_nl_post_doit
,
7923 .module
= THIS_MODULE
,
7924 .small_ops
= devlink_nl_ops
,
7925 .n_small_ops
= ARRAY_SIZE(devlink_nl_ops
),
7926 .mcgrps
= devlink_nl_mcgrps
,
7927 .n_mcgrps
= ARRAY_SIZE(devlink_nl_mcgrps
),
7930 static bool devlink_reload_actions_valid(const struct devlink_ops
*ops
)
7932 const struct devlink_reload_combination
*comb
;
7935 if (!devlink_reload_supported(ops
)) {
7936 if (WARN_ON(ops
->reload_actions
))
7941 if (WARN_ON(!ops
->reload_actions
||
7942 ops
->reload_actions
& BIT(DEVLINK_RELOAD_ACTION_UNSPEC
) ||
7943 ops
->reload_actions
>= BIT(__DEVLINK_RELOAD_ACTION_MAX
)))
7946 if (WARN_ON(ops
->reload_limits
& BIT(DEVLINK_RELOAD_LIMIT_UNSPEC
) ||
7947 ops
->reload_limits
>= BIT(__DEVLINK_RELOAD_LIMIT_MAX
)))
7950 for (i
= 0; i
< ARRAY_SIZE(devlink_reload_invalid_combinations
); i
++) {
7951 comb
= &devlink_reload_invalid_combinations
[i
];
7952 if (ops
->reload_actions
== BIT(comb
->action
) &&
7953 ops
->reload_limits
== BIT(comb
->limit
))
7960 * devlink_alloc - Allocate new devlink instance resources
7963 * @priv_size: size of user private data
7965 * Allocate new devlink instance resources, including devlink index
7968 struct devlink
*devlink_alloc(const struct devlink_ops
*ops
, size_t priv_size
)
7970 struct devlink
*devlink
;
7975 if (!devlink_reload_actions_valid(ops
))
7978 devlink
= kzalloc(sizeof(*devlink
) + priv_size
, GFP_KERNEL
);
7982 xa_init_flags(&devlink
->snapshot_ids
, XA_FLAGS_ALLOC
);
7983 __devlink_net_set(devlink
, &init_net
);
7984 INIT_LIST_HEAD(&devlink
->port_list
);
7985 INIT_LIST_HEAD(&devlink
->sb_list
);
7986 INIT_LIST_HEAD_RCU(&devlink
->dpipe_table_list
);
7987 INIT_LIST_HEAD(&devlink
->resource_list
);
7988 INIT_LIST_HEAD(&devlink
->param_list
);
7989 INIT_LIST_HEAD(&devlink
->region_list
);
7990 INIT_LIST_HEAD(&devlink
->reporter_list
);
7991 INIT_LIST_HEAD(&devlink
->trap_list
);
7992 INIT_LIST_HEAD(&devlink
->trap_group_list
);
7993 INIT_LIST_HEAD(&devlink
->trap_policer_list
);
7994 mutex_init(&devlink
->lock
);
7995 mutex_init(&devlink
->reporters_lock
);
7998 EXPORT_SYMBOL_GPL(devlink_alloc
);
8001 * devlink_register - Register devlink instance
8004 * @dev: parent device
8006 int devlink_register(struct devlink
*devlink
, struct device
*dev
)
8009 devlink
->registered
= true;
8010 mutex_lock(&devlink_mutex
);
8011 list_add_tail(&devlink
->list
, &devlink_list
);
8012 devlink_notify(devlink
, DEVLINK_CMD_NEW
);
8013 mutex_unlock(&devlink_mutex
);
8016 EXPORT_SYMBOL_GPL(devlink_register
);
8019 * devlink_unregister - Unregister devlink instance
8023 void devlink_unregister(struct devlink
*devlink
)
8025 mutex_lock(&devlink_mutex
);
8026 WARN_ON(devlink_reload_supported(devlink
->ops
) &&
8027 devlink
->reload_enabled
);
8028 devlink_notify(devlink
, DEVLINK_CMD_DEL
);
8029 list_del(&devlink
->list
);
8030 mutex_unlock(&devlink_mutex
);
8032 EXPORT_SYMBOL_GPL(devlink_unregister
);
8035 * devlink_reload_enable - Enable reload of devlink instance
8039 * Should be called at end of device initialization
8040 * process when reload operation is supported.
8042 void devlink_reload_enable(struct devlink
*devlink
)
8044 mutex_lock(&devlink_mutex
);
8045 devlink
->reload_enabled
= true;
8046 mutex_unlock(&devlink_mutex
);
8048 EXPORT_SYMBOL_GPL(devlink_reload_enable
);
8051 * devlink_reload_disable - Disable reload of devlink instance
8055 * Should be called at the beginning of device cleanup
8056 * process when reload operation is supported.
8058 void devlink_reload_disable(struct devlink
*devlink
)
8060 mutex_lock(&devlink_mutex
);
8061 /* Mutex is taken which ensures that no reload operation is in
8062 * progress while setting up forbidded flag.
8064 devlink
->reload_enabled
= false;
8065 mutex_unlock(&devlink_mutex
);
8067 EXPORT_SYMBOL_GPL(devlink_reload_disable
);
8070 * devlink_free - Free devlink instance resources
8074 void devlink_free(struct devlink
*devlink
)
8076 mutex_destroy(&devlink
->reporters_lock
);
8077 mutex_destroy(&devlink
->lock
);
8078 WARN_ON(!list_empty(&devlink
->trap_policer_list
));
8079 WARN_ON(!list_empty(&devlink
->trap_group_list
));
8080 WARN_ON(!list_empty(&devlink
->trap_list
));
8081 WARN_ON(!list_empty(&devlink
->reporter_list
));
8082 WARN_ON(!list_empty(&devlink
->region_list
));
8083 WARN_ON(!list_empty(&devlink
->param_list
));
8084 WARN_ON(!list_empty(&devlink
->resource_list
));
8085 WARN_ON(!list_empty(&devlink
->dpipe_table_list
));
8086 WARN_ON(!list_empty(&devlink
->sb_list
));
8087 WARN_ON(!list_empty(&devlink
->port_list
));
8089 xa_destroy(&devlink
->snapshot_ids
);
8093 EXPORT_SYMBOL_GPL(devlink_free
);
8095 static void devlink_port_type_warn(struct work_struct
*work
)
8097 WARN(true, "Type was not set for devlink port.");
8100 static bool devlink_port_type_should_warn(struct devlink_port
*devlink_port
)
8102 /* Ignore CPU and DSA flavours. */
8103 return devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_CPU
&&
8104 devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_DSA
&&
8105 devlink_port
->attrs
.flavour
!= DEVLINK_PORT_FLAVOUR_UNUSED
;
8108 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
8110 static void devlink_port_type_warn_schedule(struct devlink_port
*devlink_port
)
8112 if (!devlink_port_type_should_warn(devlink_port
))
8114 /* Schedule a work to WARN in case driver does not set port
8115 * type within timeout.
8117 schedule_delayed_work(&devlink_port
->type_warn_dw
,
8118 DEVLINK_PORT_TYPE_WARN_TIMEOUT
);
8121 static void devlink_port_type_warn_cancel(struct devlink_port
*devlink_port
)
8123 if (!devlink_port_type_should_warn(devlink_port
))
8125 cancel_delayed_work_sync(&devlink_port
->type_warn_dw
);
8129 * devlink_port_register - Register devlink port
8132 * @devlink_port: devlink port
8133 * @port_index: driver-specific numerical identifier of the port
8135 * Register devlink port with provided port index. User can use
8136 * any indexing, even hw-related one. devlink_port structure
8137 * is convenient to be embedded inside user driver private structure.
8138 * Note that the caller should take care of zeroing the devlink_port
8141 int devlink_port_register(struct devlink
*devlink
,
8142 struct devlink_port
*devlink_port
,
8143 unsigned int port_index
)
8145 mutex_lock(&devlink
->lock
);
8146 if (devlink_port_index_exists(devlink
, port_index
)) {
8147 mutex_unlock(&devlink
->lock
);
8150 devlink_port
->devlink
= devlink
;
8151 devlink_port
->index
= port_index
;
8152 devlink_port
->registered
= true;
8153 spin_lock_init(&devlink_port
->type_lock
);
8154 INIT_LIST_HEAD(&devlink_port
->reporter_list
);
8155 mutex_init(&devlink_port
->reporters_lock
);
8156 list_add_tail(&devlink_port
->list
, &devlink
->port_list
);
8157 INIT_LIST_HEAD(&devlink_port
->param_list
);
8158 INIT_LIST_HEAD(&devlink_port
->region_list
);
8159 mutex_unlock(&devlink
->lock
);
8160 INIT_DELAYED_WORK(&devlink_port
->type_warn_dw
, &devlink_port_type_warn
);
8161 devlink_port_type_warn_schedule(devlink_port
);
8162 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
8165 EXPORT_SYMBOL_GPL(devlink_port_register
);
8168 * devlink_port_unregister - Unregister devlink port
8170 * @devlink_port: devlink port
8172 void devlink_port_unregister(struct devlink_port
*devlink_port
)
8174 struct devlink
*devlink
= devlink_port
->devlink
;
8176 devlink_port_type_warn_cancel(devlink_port
);
8177 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_DEL
);
8178 mutex_lock(&devlink
->lock
);
8179 list_del(&devlink_port
->list
);
8180 mutex_unlock(&devlink
->lock
);
8181 WARN_ON(!list_empty(&devlink_port
->reporter_list
));
8182 WARN_ON(!list_empty(&devlink_port
->region_list
));
8183 mutex_destroy(&devlink_port
->reporters_lock
);
8185 EXPORT_SYMBOL_GPL(devlink_port_unregister
);
8187 static void __devlink_port_type_set(struct devlink_port
*devlink_port
,
8188 enum devlink_port_type type
,
8191 if (WARN_ON(!devlink_port
->registered
))
8193 devlink_port_type_warn_cancel(devlink_port
);
8194 spin_lock_bh(&devlink_port
->type_lock
);
8195 devlink_port
->type
= type
;
8196 devlink_port
->type_dev
= type_dev
;
8197 spin_unlock_bh(&devlink_port
->type_lock
);
8198 devlink_port_notify(devlink_port
, DEVLINK_CMD_PORT_NEW
);
8201 static void devlink_port_type_netdev_checks(struct devlink_port
*devlink_port
,
8202 struct net_device
*netdev
)
8204 const struct net_device_ops
*ops
= netdev
->netdev_ops
;
8206 /* If driver registers devlink port, it should set devlink port
8207 * attributes accordingly so the compat functions are called
8208 * and the original ops are not used.
8210 if (ops
->ndo_get_phys_port_name
) {
8211 /* Some drivers use the same set of ndos for netdevs
8212 * that have devlink_port registered and also for
8213 * those who don't. Make sure that ndo_get_phys_port_name
8214 * returns -EOPNOTSUPP here in case it is defined.
8217 char name
[IFNAMSIZ
];
8220 err
= ops
->ndo_get_phys_port_name(netdev
, name
, sizeof(name
));
8221 WARN_ON(err
!= -EOPNOTSUPP
);
8223 if (ops
->ndo_get_port_parent_id
) {
8224 /* Some drivers use the same set of ndos for netdevs
8225 * that have devlink_port registered and also for
8226 * those who don't. Make sure that ndo_get_port_parent_id
8227 * returns -EOPNOTSUPP here in case it is defined.
8230 struct netdev_phys_item_id ppid
;
8233 err
= ops
->ndo_get_port_parent_id(netdev
, &ppid
);
8234 WARN_ON(err
!= -EOPNOTSUPP
);
8239 * devlink_port_type_eth_set - Set port type to Ethernet
8241 * @devlink_port: devlink port
8242 * @netdev: related netdevice
8244 void devlink_port_type_eth_set(struct devlink_port
*devlink_port
,
8245 struct net_device
*netdev
)
8248 devlink_port_type_netdev_checks(devlink_port
, netdev
);
8250 dev_warn(devlink_port
->devlink
->dev
,
8251 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
8252 devlink_port
->index
);
8254 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_ETH
, netdev
);
8256 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set
);
8259 * devlink_port_type_ib_set - Set port type to InfiniBand
8261 * @devlink_port: devlink port
8262 * @ibdev: related IB device
8264 void devlink_port_type_ib_set(struct devlink_port
*devlink_port
,
8265 struct ib_device
*ibdev
)
8267 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_IB
, ibdev
);
8269 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set
);
8272 * devlink_port_type_clear - Clear port type
8274 * @devlink_port: devlink port
8276 void devlink_port_type_clear(struct devlink_port
*devlink_port
)
8278 __devlink_port_type_set(devlink_port
, DEVLINK_PORT_TYPE_NOTSET
, NULL
);
8279 devlink_port_type_warn_schedule(devlink_port
);
8281 EXPORT_SYMBOL_GPL(devlink_port_type_clear
);
8283 static int __devlink_port_attrs_set(struct devlink_port
*devlink_port
,
8284 enum devlink_port_flavour flavour
)
8286 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
8288 devlink_port
->attrs_set
= true;
8289 attrs
->flavour
= flavour
;
8290 if (attrs
->switch_id
.id_len
) {
8291 devlink_port
->switch_port
= true;
8292 if (WARN_ON(attrs
->switch_id
.id_len
> MAX_PHYS_ITEM_ID_LEN
))
8293 attrs
->switch_id
.id_len
= MAX_PHYS_ITEM_ID_LEN
;
8295 devlink_port
->switch_port
= false;
8301 * devlink_port_attrs_set - Set port attributes
8303 * @devlink_port: devlink port
8304 * @attrs: devlink port attrs
8306 void devlink_port_attrs_set(struct devlink_port
*devlink_port
,
8307 struct devlink_port_attrs
*attrs
)
8311 if (WARN_ON(devlink_port
->registered
))
8313 devlink_port
->attrs
= *attrs
;
8314 ret
= __devlink_port_attrs_set(devlink_port
, attrs
->flavour
);
8317 WARN_ON(attrs
->splittable
&& attrs
->split
);
8319 EXPORT_SYMBOL_GPL(devlink_port_attrs_set
);
8322 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
8324 * @devlink_port: devlink port
8325 * @controller: associated controller number for the devlink port instance
8326 * @pf: associated PF for the devlink port instance
8327 * @external: indicates if the port is for an external controller
8329 void devlink_port_attrs_pci_pf_set(struct devlink_port
*devlink_port
, u32 controller
,
8330 u16 pf
, bool external
)
8332 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
8335 if (WARN_ON(devlink_port
->registered
))
8337 ret
= __devlink_port_attrs_set(devlink_port
,
8338 DEVLINK_PORT_FLAVOUR_PCI_PF
);
8341 attrs
->pci_pf
.controller
= controller
;
8342 attrs
->pci_pf
.pf
= pf
;
8343 attrs
->pci_pf
.external
= external
;
8345 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set
);
8348 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
8350 * @devlink_port: devlink port
8351 * @controller: associated controller number for the devlink port instance
8352 * @pf: associated PF for the devlink port instance
8353 * @vf: associated VF of a PF for the devlink port instance
8354 * @external: indicates if the port is for an external controller
8356 void devlink_port_attrs_pci_vf_set(struct devlink_port
*devlink_port
, u32 controller
,
8357 u16 pf
, u16 vf
, bool external
)
8359 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
8362 if (WARN_ON(devlink_port
->registered
))
8364 ret
= __devlink_port_attrs_set(devlink_port
,
8365 DEVLINK_PORT_FLAVOUR_PCI_VF
);
8368 attrs
->pci_vf
.controller
= controller
;
8369 attrs
->pci_vf
.pf
= pf
;
8370 attrs
->pci_vf
.vf
= vf
;
8371 attrs
->pci_vf
.external
= external
;
8373 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set
);
8375 static int __devlink_port_phys_port_name_get(struct devlink_port
*devlink_port
,
8376 char *name
, size_t len
)
8378 struct devlink_port_attrs
*attrs
= &devlink_port
->attrs
;
8381 if (!devlink_port
->attrs_set
)
8384 switch (attrs
->flavour
) {
8385 case DEVLINK_PORT_FLAVOUR_PHYSICAL
:
8386 case DEVLINK_PORT_FLAVOUR_VIRTUAL
:
8388 n
= snprintf(name
, len
, "p%u", attrs
->phys
.port_number
);
8390 n
= snprintf(name
, len
, "p%us%u",
8391 attrs
->phys
.port_number
,
8392 attrs
->phys
.split_subport_number
);
8394 case DEVLINK_PORT_FLAVOUR_CPU
:
8395 case DEVLINK_PORT_FLAVOUR_DSA
:
8396 case DEVLINK_PORT_FLAVOUR_UNUSED
:
8397 /* As CPU and DSA ports do not have a netdevice associated
8398 * case should not ever happen.
8402 case DEVLINK_PORT_FLAVOUR_PCI_PF
:
8403 if (attrs
->pci_pf
.external
) {
8404 n
= snprintf(name
, len
, "c%u", attrs
->pci_pf
.controller
);
8410 n
= snprintf(name
, len
, "pf%u", attrs
->pci_pf
.pf
);
8412 case DEVLINK_PORT_FLAVOUR_PCI_VF
:
8413 if (attrs
->pci_vf
.external
) {
8414 n
= snprintf(name
, len
, "c%u", attrs
->pci_vf
.controller
);
8420 n
= snprintf(name
, len
, "pf%uvf%u",
8421 attrs
->pci_vf
.pf
, attrs
->pci_vf
.vf
);
8431 int devlink_sb_register(struct devlink
*devlink
, unsigned int sb_index
,
8432 u32 size
, u16 ingress_pools_count
,
8433 u16 egress_pools_count
, u16 ingress_tc_count
,
8434 u16 egress_tc_count
)
8436 struct devlink_sb
*devlink_sb
;
8439 mutex_lock(&devlink
->lock
);
8440 if (devlink_sb_index_exists(devlink
, sb_index
)) {
8445 devlink_sb
= kzalloc(sizeof(*devlink_sb
), GFP_KERNEL
);
8450 devlink_sb
->index
= sb_index
;
8451 devlink_sb
->size
= size
;
8452 devlink_sb
->ingress_pools_count
= ingress_pools_count
;
8453 devlink_sb
->egress_pools_count
= egress_pools_count
;
8454 devlink_sb
->ingress_tc_count
= ingress_tc_count
;
8455 devlink_sb
->egress_tc_count
= egress_tc_count
;
8456 list_add_tail(&devlink_sb
->list
, &devlink
->sb_list
);
8458 mutex_unlock(&devlink
->lock
);
8461 EXPORT_SYMBOL_GPL(devlink_sb_register
);
8463 void devlink_sb_unregister(struct devlink
*devlink
, unsigned int sb_index
)
8465 struct devlink_sb
*devlink_sb
;
8467 mutex_lock(&devlink
->lock
);
8468 devlink_sb
= devlink_sb_get_by_index(devlink
, sb_index
);
8469 WARN_ON(!devlink_sb
);
8470 list_del(&devlink_sb
->list
);
8471 mutex_unlock(&devlink
->lock
);
8474 EXPORT_SYMBOL_GPL(devlink_sb_unregister
);
8477 * devlink_dpipe_headers_register - register dpipe headers
8480 * @dpipe_headers: dpipe header array
8482 * Register the headers supported by hardware.
8484 int devlink_dpipe_headers_register(struct devlink
*devlink
,
8485 struct devlink_dpipe_headers
*dpipe_headers
)
8487 mutex_lock(&devlink
->lock
);
8488 devlink
->dpipe_headers
= dpipe_headers
;
8489 mutex_unlock(&devlink
->lock
);
8492 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register
);
8495 * devlink_dpipe_headers_unregister - unregister dpipe headers
8499 * Unregister the headers supported by hardware.
8501 void devlink_dpipe_headers_unregister(struct devlink
*devlink
)
8503 mutex_lock(&devlink
->lock
);
8504 devlink
->dpipe_headers
= NULL
;
8505 mutex_unlock(&devlink
->lock
);
8507 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister
);
8510 * devlink_dpipe_table_counter_enabled - check if counter allocation
8513 * @table_name: tables name
8515 * Used by driver to check if counter allocation is required.
8516 * After counter allocation is turned on the table entries
8517 * are updated to include counter statistics.
8519 * After that point on the driver must respect the counter
8520 * state so that each entry added to the table is added
8523 bool devlink_dpipe_table_counter_enabled(struct devlink
*devlink
,
8524 const char *table_name
)
8526 struct devlink_dpipe_table
*table
;
8530 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
8531 table_name
, devlink
);
8534 enabled
= table
->counters_enabled
;
8538 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled
);
8541 * devlink_dpipe_table_register - register dpipe table
8544 * @table_name: table name
8545 * @table_ops: table ops
8547 * @counter_control_extern: external control for counters
8549 int devlink_dpipe_table_register(struct devlink
*devlink
,
8550 const char *table_name
,
8551 struct devlink_dpipe_table_ops
*table_ops
,
8552 void *priv
, bool counter_control_extern
)
8554 struct devlink_dpipe_table
*table
;
8557 if (WARN_ON(!table_ops
->size_get
))
8560 mutex_lock(&devlink
->lock
);
8562 if (devlink_dpipe_table_find(&devlink
->dpipe_table_list
, table_name
,
8568 table
= kzalloc(sizeof(*table
), GFP_KERNEL
);
8574 table
->name
= table_name
;
8575 table
->table_ops
= table_ops
;
8577 table
->counter_control_extern
= counter_control_extern
;
8579 list_add_tail_rcu(&table
->list
, &devlink
->dpipe_table_list
);
8581 mutex_unlock(&devlink
->lock
);
8584 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register
);
8587 * devlink_dpipe_table_unregister - unregister dpipe table
8590 * @table_name: table name
8592 void devlink_dpipe_table_unregister(struct devlink
*devlink
,
8593 const char *table_name
)
8595 struct devlink_dpipe_table
*table
;
8597 mutex_lock(&devlink
->lock
);
8598 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
8599 table_name
, devlink
);
8602 list_del_rcu(&table
->list
);
8603 mutex_unlock(&devlink
->lock
);
8604 kfree_rcu(table
, rcu
);
8607 mutex_unlock(&devlink
->lock
);
8609 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister
);
8612 * devlink_resource_register - devlink resource register
8615 * @resource_name: resource's name
8616 * @resource_size: resource's size
8617 * @resource_id: resource's id
8618 * @parent_resource_id: resource's parent id
8619 * @size_params: size parameters
8621 int devlink_resource_register(struct devlink
*devlink
,
8622 const char *resource_name
,
8625 u64 parent_resource_id
,
8626 const struct devlink_resource_size_params
*size_params
)
8628 struct devlink_resource
*resource
;
8629 struct list_head
*resource_list
;
8633 top_hierarchy
= parent_resource_id
== DEVLINK_RESOURCE_ID_PARENT_TOP
;
8635 mutex_lock(&devlink
->lock
);
8636 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
8642 resource
= kzalloc(sizeof(*resource
), GFP_KERNEL
);
8648 if (top_hierarchy
) {
8649 resource_list
= &devlink
->resource_list
;
8651 struct devlink_resource
*parent_resource
;
8653 parent_resource
= devlink_resource_find(devlink
, NULL
,
8654 parent_resource_id
);
8655 if (parent_resource
) {
8656 resource_list
= &parent_resource
->resource_list
;
8657 resource
->parent
= parent_resource
;
8665 resource
->name
= resource_name
;
8666 resource
->size
= resource_size
;
8667 resource
->size_new
= resource_size
;
8668 resource
->id
= resource_id
;
8669 resource
->size_valid
= true;
8670 memcpy(&resource
->size_params
, size_params
,
8671 sizeof(resource
->size_params
));
8672 INIT_LIST_HEAD(&resource
->resource_list
);
8673 list_add_tail(&resource
->list
, resource_list
);
8675 mutex_unlock(&devlink
->lock
);
8678 EXPORT_SYMBOL_GPL(devlink_resource_register
);
8681 * devlink_resources_unregister - free all resources
8684 * @resource: resource
8686 void devlink_resources_unregister(struct devlink
*devlink
,
8687 struct devlink_resource
*resource
)
8689 struct devlink_resource
*tmp
, *child_resource
;
8690 struct list_head
*resource_list
;
8693 resource_list
= &resource
->resource_list
;
8695 resource_list
= &devlink
->resource_list
;
8698 mutex_lock(&devlink
->lock
);
8700 list_for_each_entry_safe(child_resource
, tmp
, resource_list
, list
) {
8701 devlink_resources_unregister(devlink
, child_resource
);
8702 list_del(&child_resource
->list
);
8703 kfree(child_resource
);
8707 mutex_unlock(&devlink
->lock
);
8709 EXPORT_SYMBOL_GPL(devlink_resources_unregister
);
8712 * devlink_resource_size_get - get and update size
8715 * @resource_id: the requested resource id
8716 * @p_resource_size: ptr to update
8718 int devlink_resource_size_get(struct devlink
*devlink
,
8720 u64
*p_resource_size
)
8722 struct devlink_resource
*resource
;
8725 mutex_lock(&devlink
->lock
);
8726 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
8731 *p_resource_size
= resource
->size_new
;
8732 resource
->size
= resource
->size_new
;
8734 mutex_unlock(&devlink
->lock
);
8737 EXPORT_SYMBOL_GPL(devlink_resource_size_get
);
8740 * devlink_dpipe_table_resource_set - set the resource id
8743 * @table_name: table name
8744 * @resource_id: resource id
8745 * @resource_units: number of resource's units consumed per table's entry
8747 int devlink_dpipe_table_resource_set(struct devlink
*devlink
,
8748 const char *table_name
, u64 resource_id
,
8751 struct devlink_dpipe_table
*table
;
8754 mutex_lock(&devlink
->lock
);
8755 table
= devlink_dpipe_table_find(&devlink
->dpipe_table_list
,
8756 table_name
, devlink
);
8761 table
->resource_id
= resource_id
;
8762 table
->resource_units
= resource_units
;
8763 table
->resource_valid
= true;
8765 mutex_unlock(&devlink
->lock
);
8768 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set
);
8771 * devlink_resource_occ_get_register - register occupancy getter
8774 * @resource_id: resource id
8775 * @occ_get: occupancy getter callback
8776 * @occ_get_priv: occupancy getter callback priv
8778 void devlink_resource_occ_get_register(struct devlink
*devlink
,
8780 devlink_resource_occ_get_t
*occ_get
,
8783 struct devlink_resource
*resource
;
8785 mutex_lock(&devlink
->lock
);
8786 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
8787 if (WARN_ON(!resource
))
8789 WARN_ON(resource
->occ_get
);
8791 resource
->occ_get
= occ_get
;
8792 resource
->occ_get_priv
= occ_get_priv
;
8794 mutex_unlock(&devlink
->lock
);
8796 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register
);
8799 * devlink_resource_occ_get_unregister - unregister occupancy getter
8802 * @resource_id: resource id
8804 void devlink_resource_occ_get_unregister(struct devlink
*devlink
,
8807 struct devlink_resource
*resource
;
8809 mutex_lock(&devlink
->lock
);
8810 resource
= devlink_resource_find(devlink
, NULL
, resource_id
);
8811 if (WARN_ON(!resource
))
8813 WARN_ON(!resource
->occ_get
);
8815 resource
->occ_get
= NULL
;
8816 resource
->occ_get_priv
= NULL
;
8818 mutex_unlock(&devlink
->lock
);
8820 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister
);
8822 static int devlink_param_verify(const struct devlink_param
*param
)
8824 if (!param
|| !param
->name
|| !param
->supported_cmodes
)
8827 return devlink_param_generic_verify(param
);
8829 return devlink_param_driver_verify(param
);
8832 static int __devlink_params_register(struct devlink
*devlink
,
8833 unsigned int port_index
,
8834 struct list_head
*param_list
,
8835 const struct devlink_param
*params
,
8836 size_t params_count
,
8837 enum devlink_command reg_cmd
,
8838 enum devlink_command unreg_cmd
)
8840 const struct devlink_param
*param
= params
;
8844 mutex_lock(&devlink
->lock
);
8845 for (i
= 0; i
< params_count
; i
++, param
++) {
8846 err
= devlink_param_verify(param
);
8850 err
= devlink_param_register_one(devlink
, port_index
,
8851 param_list
, param
, reg_cmd
);
8856 mutex_unlock(&devlink
->lock
);
8862 for (param
--; i
> 0; i
--, param
--)
8863 devlink_param_unregister_one(devlink
, port_index
, param_list
,
8866 mutex_unlock(&devlink
->lock
);
8870 static void __devlink_params_unregister(struct devlink
*devlink
,
8871 unsigned int port_index
,
8872 struct list_head
*param_list
,
8873 const struct devlink_param
*params
,
8874 size_t params_count
,
8875 enum devlink_command cmd
)
8877 const struct devlink_param
*param
= params
;
8880 mutex_lock(&devlink
->lock
);
8881 for (i
= 0; i
< params_count
; i
++, param
++)
8882 devlink_param_unregister_one(devlink
, 0, param_list
, param
,
8884 mutex_unlock(&devlink
->lock
);
8888 * devlink_params_register - register configuration parameters
8891 * @params: configuration parameters array
8892 * @params_count: number of parameters provided
8894 * Register the configuration parameters supported by the driver.
8896 int devlink_params_register(struct devlink
*devlink
,
8897 const struct devlink_param
*params
,
8898 size_t params_count
)
8900 return __devlink_params_register(devlink
, 0, &devlink
->param_list
,
8901 params
, params_count
,
8902 DEVLINK_CMD_PARAM_NEW
,
8903 DEVLINK_CMD_PARAM_DEL
);
8905 EXPORT_SYMBOL_GPL(devlink_params_register
);
8908 * devlink_params_unregister - unregister configuration parameters
8910 * @params: configuration parameters to unregister
8911 * @params_count: number of parameters provided
8913 void devlink_params_unregister(struct devlink
*devlink
,
8914 const struct devlink_param
*params
,
8915 size_t params_count
)
8917 return __devlink_params_unregister(devlink
, 0, &devlink
->param_list
,
8918 params
, params_count
,
8919 DEVLINK_CMD_PARAM_DEL
);
8921 EXPORT_SYMBOL_GPL(devlink_params_unregister
);
8924 * devlink_params_publish - publish configuration parameters
8928 * Publish previously registered configuration parameters.
8930 void devlink_params_publish(struct devlink
*devlink
)
8932 struct devlink_param_item
*param_item
;
8934 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
8935 if (param_item
->published
)
8937 param_item
->published
= true;
8938 devlink_param_notify(devlink
, 0, param_item
,
8939 DEVLINK_CMD_PARAM_NEW
);
8942 EXPORT_SYMBOL_GPL(devlink_params_publish
);
8945 * devlink_params_unpublish - unpublish configuration parameters
8949 * Unpublish previously registered configuration parameters.
8951 void devlink_params_unpublish(struct devlink
*devlink
)
8953 struct devlink_param_item
*param_item
;
8955 list_for_each_entry(param_item
, &devlink
->param_list
, list
) {
8956 if (!param_item
->published
)
8958 param_item
->published
= false;
8959 devlink_param_notify(devlink
, 0, param_item
,
8960 DEVLINK_CMD_PARAM_DEL
);
8963 EXPORT_SYMBOL_GPL(devlink_params_unpublish
);
8966 * devlink_port_params_register - register port configuration parameters
8968 * @devlink_port: devlink port
8969 * @params: configuration parameters array
8970 * @params_count: number of parameters provided
8972 * Register the configuration parameters supported by the port.
8974 int devlink_port_params_register(struct devlink_port
*devlink_port
,
8975 const struct devlink_param
*params
,
8976 size_t params_count
)
8978 return __devlink_params_register(devlink_port
->devlink
,
8979 devlink_port
->index
,
8980 &devlink_port
->param_list
, params
,
8982 DEVLINK_CMD_PORT_PARAM_NEW
,
8983 DEVLINK_CMD_PORT_PARAM_DEL
);
8985 EXPORT_SYMBOL_GPL(devlink_port_params_register
);
8988 * devlink_port_params_unregister - unregister port configuration
8991 * @devlink_port: devlink port
8992 * @params: configuration parameters array
8993 * @params_count: number of parameters provided
8995 void devlink_port_params_unregister(struct devlink_port
*devlink_port
,
8996 const struct devlink_param
*params
,
8997 size_t params_count
)
8999 return __devlink_params_unregister(devlink_port
->devlink
,
9000 devlink_port
->index
,
9001 &devlink_port
->param_list
,
9002 params
, params_count
,
9003 DEVLINK_CMD_PORT_PARAM_DEL
);
9005 EXPORT_SYMBOL_GPL(devlink_port_params_unregister
);
9008 __devlink_param_driverinit_value_get(struct list_head
*param_list
, u32 param_id
,
9009 union devlink_param_value
*init_val
)
9011 struct devlink_param_item
*param_item
;
9013 param_item
= devlink_param_find_by_id(param_list
, param_id
);
9017 if (!param_item
->driverinit_value_valid
||
9018 !devlink_param_cmode_is_supported(param_item
->param
,
9019 DEVLINK_PARAM_CMODE_DRIVERINIT
))
9022 if (param_item
->param
->type
== DEVLINK_PARAM_TYPE_STRING
)
9023 strcpy(init_val
->vstr
, param_item
->driverinit_value
.vstr
);
9025 *init_val
= param_item
->driverinit_value
;
9031 __devlink_param_driverinit_value_set(struct devlink
*devlink
,
9032 unsigned int port_index
,
9033 struct list_head
*param_list
, u32 param_id
,
9034 union devlink_param_value init_val
,
9035 enum devlink_command cmd
)
9037 struct devlink_param_item
*param_item
;
9039 param_item
= devlink_param_find_by_id(param_list
, param_id
);
9043 if (!devlink_param_cmode_is_supported(param_item
->param
,
9044 DEVLINK_PARAM_CMODE_DRIVERINIT
))
9047 if (param_item
->param
->type
== DEVLINK_PARAM_TYPE_STRING
)
9048 strcpy(param_item
->driverinit_value
.vstr
, init_val
.vstr
);
9050 param_item
->driverinit_value
= init_val
;
9051 param_item
->driverinit_value_valid
= true;
9053 devlink_param_notify(devlink
, port_index
, param_item
, cmd
);
9058 * devlink_param_driverinit_value_get - get configuration parameter
9059 * value for driver initializing
9062 * @param_id: parameter ID
9063 * @init_val: value of parameter in driverinit configuration mode
9065 * This function should be used by the driver to get driverinit
9066 * configuration for initialization after reload command.
9068 int devlink_param_driverinit_value_get(struct devlink
*devlink
, u32 param_id
,
9069 union devlink_param_value
*init_val
)
9071 if (!devlink_reload_supported(devlink
->ops
))
9074 return __devlink_param_driverinit_value_get(&devlink
->param_list
,
9075 param_id
, init_val
);
9077 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get
);
9080 * devlink_param_driverinit_value_set - set value of configuration
9081 * parameter for driverinit
9082 * configuration mode
9085 * @param_id: parameter ID
9086 * @init_val: value of parameter to set for driverinit configuration mode
9088 * This function should be used by the driver to set driverinit
9089 * configuration mode default value.
9091 int devlink_param_driverinit_value_set(struct devlink
*devlink
, u32 param_id
,
9092 union devlink_param_value init_val
)
9094 return __devlink_param_driverinit_value_set(devlink
, 0,
9095 &devlink
->param_list
,
9097 DEVLINK_CMD_PARAM_NEW
);
9099 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set
);
9102 * devlink_port_param_driverinit_value_get - get configuration parameter
9103 * value for driver initializing
9105 * @devlink_port: devlink_port
9106 * @param_id: parameter ID
9107 * @init_val: value of parameter in driverinit configuration mode
9109 * This function should be used by the driver to get driverinit
9110 * configuration for initialization after reload command.
9112 int devlink_port_param_driverinit_value_get(struct devlink_port
*devlink_port
,
9114 union devlink_param_value
*init_val
)
9116 struct devlink
*devlink
= devlink_port
->devlink
;
9118 if (!devlink_reload_supported(devlink
->ops
))
9121 return __devlink_param_driverinit_value_get(&devlink_port
->param_list
,
9122 param_id
, init_val
);
9124 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get
);
9127 * devlink_port_param_driverinit_value_set - set value of configuration
9128 * parameter for driverinit
9129 * configuration mode
9131 * @devlink_port: devlink_port
9132 * @param_id: parameter ID
9133 * @init_val: value of parameter to set for driverinit configuration mode
9135 * This function should be used by the driver to set driverinit
9136 * configuration mode default value.
9138 int devlink_port_param_driverinit_value_set(struct devlink_port
*devlink_port
,
9140 union devlink_param_value init_val
)
9142 return __devlink_param_driverinit_value_set(devlink_port
->devlink
,
9143 devlink_port
->index
,
9144 &devlink_port
->param_list
,
9146 DEVLINK_CMD_PORT_PARAM_NEW
);
9148 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set
);
9151 * devlink_param_value_changed - notify devlink on a parameter's value
9152 * change. Should be called by the driver
9153 * right after the change.
9156 * @param_id: parameter ID
9158 * This function should be used by the driver to notify devlink on value
9159 * change, excluding driverinit configuration mode.
9160 * For driverinit configuration mode driver should use the function
9162 void devlink_param_value_changed(struct devlink
*devlink
, u32 param_id
)
9164 struct devlink_param_item
*param_item
;
9166 param_item
= devlink_param_find_by_id(&devlink
->param_list
, param_id
);
9167 WARN_ON(!param_item
);
9169 devlink_param_notify(devlink
, 0, param_item
, DEVLINK_CMD_PARAM_NEW
);
9171 EXPORT_SYMBOL_GPL(devlink_param_value_changed
);
9174 * devlink_port_param_value_changed - notify devlink on a parameter's value
9175 * change. Should be called by the driver
9176 * right after the change.
9178 * @devlink_port: devlink_port
9179 * @param_id: parameter ID
9181 * This function should be used by the driver to notify devlink on value
9182 * change, excluding driverinit configuration mode.
9183 * For driverinit configuration mode driver should use the function
9184 * devlink_port_param_driverinit_value_set() instead.
9186 void devlink_port_param_value_changed(struct devlink_port
*devlink_port
,
9189 struct devlink_param_item
*param_item
;
9191 param_item
= devlink_param_find_by_id(&devlink_port
->param_list
,
9193 WARN_ON(!param_item
);
9195 devlink_param_notify(devlink_port
->devlink
, devlink_port
->index
,
9196 param_item
, DEVLINK_CMD_PORT_PARAM_NEW
);
9198 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed
);
9201 * devlink_param_value_str_fill - Safely fill-up the string preventing
9202 * from overflow of the preallocated buffer
9204 * @dst_val: destination devlink_param_value
9205 * @src: source buffer
9207 void devlink_param_value_str_fill(union devlink_param_value
*dst_val
,
9212 len
= strlcpy(dst_val
->vstr
, src
, __DEVLINK_PARAM_MAX_STRING_VALUE
);
9213 WARN_ON(len
>= __DEVLINK_PARAM_MAX_STRING_VALUE
);
9215 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill
);
9218 * devlink_region_create - create a new address region
9221 * @ops: region operations and name
9222 * @region_max_snapshots: Maximum supported number of snapshots for region
9223 * @region_size: size of region
9225 struct devlink_region
*
9226 devlink_region_create(struct devlink
*devlink
,
9227 const struct devlink_region_ops
*ops
,
9228 u32 region_max_snapshots
, u64 region_size
)
9230 struct devlink_region
*region
;
9233 if (WARN_ON(!ops
) || WARN_ON(!ops
->destructor
))
9234 return ERR_PTR(-EINVAL
);
9236 mutex_lock(&devlink
->lock
);
9238 if (devlink_region_get_by_name(devlink
, ops
->name
)) {
9243 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
9249 region
->devlink
= devlink
;
9250 region
->max_snapshots
= region_max_snapshots
;
9252 region
->size
= region_size
;
9253 INIT_LIST_HEAD(®ion
->snapshot_list
);
9254 list_add_tail(®ion
->list
, &devlink
->region_list
);
9255 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_NEW
);
9257 mutex_unlock(&devlink
->lock
);
9261 mutex_unlock(&devlink
->lock
);
9262 return ERR_PTR(err
);
9264 EXPORT_SYMBOL_GPL(devlink_region_create
);
9267 * devlink_port_region_create - create a new address region for a port
9269 * @port: devlink port
9270 * @ops: region operations and name
9271 * @region_max_snapshots: Maximum supported number of snapshots for region
9272 * @region_size: size of region
9274 struct devlink_region
*
9275 devlink_port_region_create(struct devlink_port
*port
,
9276 const struct devlink_port_region_ops
*ops
,
9277 u32 region_max_snapshots
, u64 region_size
)
9279 struct devlink
*devlink
= port
->devlink
;
9280 struct devlink_region
*region
;
9283 if (WARN_ON(!ops
) || WARN_ON(!ops
->destructor
))
9284 return ERR_PTR(-EINVAL
);
9286 mutex_lock(&devlink
->lock
);
9288 if (devlink_port_region_get_by_name(port
, ops
->name
)) {
9293 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
9299 region
->devlink
= devlink
;
9300 region
->port
= port
;
9301 region
->max_snapshots
= region_max_snapshots
;
9302 region
->port_ops
= ops
;
9303 region
->size
= region_size
;
9304 INIT_LIST_HEAD(®ion
->snapshot_list
);
9305 list_add_tail(®ion
->list
, &port
->region_list
);
9306 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_NEW
);
9308 mutex_unlock(&devlink
->lock
);
9312 mutex_unlock(&devlink
->lock
);
9313 return ERR_PTR(err
);
9315 EXPORT_SYMBOL_GPL(devlink_port_region_create
);
9318 * devlink_region_destroy - destroy address region
9320 * @region: devlink region to destroy
9322 void devlink_region_destroy(struct devlink_region
*region
)
9324 struct devlink
*devlink
= region
->devlink
;
9325 struct devlink_snapshot
*snapshot
, *ts
;
9327 mutex_lock(&devlink
->lock
);
9329 /* Free all snapshots of region */
9330 list_for_each_entry_safe(snapshot
, ts
, ®ion
->snapshot_list
, list
)
9331 devlink_region_snapshot_del(region
, snapshot
);
9333 list_del(®ion
->list
);
9335 devlink_nl_region_notify(region
, NULL
, DEVLINK_CMD_REGION_DEL
);
9336 mutex_unlock(&devlink
->lock
);
9339 EXPORT_SYMBOL_GPL(devlink_region_destroy
);
9342 * devlink_region_snapshot_id_get - get snapshot ID
9344 * This callback should be called when adding a new snapshot,
9345 * Driver should use the same id for multiple snapshots taken
9346 * on multiple regions at the same time/by the same trigger.
9348 * The caller of this function must use devlink_region_snapshot_id_put
9349 * when finished creating regions using this id.
9351 * Returns zero on success, or a negative error code on failure.
9354 * @id: storage to return id
9356 int devlink_region_snapshot_id_get(struct devlink
*devlink
, u32
*id
)
9360 mutex_lock(&devlink
->lock
);
9361 err
= __devlink_region_snapshot_id_get(devlink
, id
);
9362 mutex_unlock(&devlink
->lock
);
9366 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get
);
9369 * devlink_region_snapshot_id_put - put snapshot ID reference
9371 * This should be called by a driver after finishing creating snapshots
9372 * with an id. Doing so ensures that the ID can later be released in the
9373 * event that all snapshots using it have been destroyed.
9376 * @id: id to release reference on
9378 void devlink_region_snapshot_id_put(struct devlink
*devlink
, u32 id
)
9380 mutex_lock(&devlink
->lock
);
9381 __devlink_snapshot_id_decrement(devlink
, id
);
9382 mutex_unlock(&devlink
->lock
);
9384 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put
);
9387 * devlink_region_snapshot_create - create a new snapshot
9388 * This will add a new snapshot of a region. The snapshot
9389 * will be stored on the region struct and can be accessed
9390 * from devlink. This is useful for future analyses of snapshots.
9391 * Multiple snapshots can be created on a region.
9392 * The @snapshot_id should be obtained using the getter function.
9394 * @region: devlink region of the snapshot
9395 * @data: snapshot data
9396 * @snapshot_id: snapshot id to be created
9398 int devlink_region_snapshot_create(struct devlink_region
*region
,
9399 u8
*data
, u32 snapshot_id
)
9401 struct devlink
*devlink
= region
->devlink
;
9404 mutex_lock(&devlink
->lock
);
9405 err
= __devlink_region_snapshot_create(region
, data
, snapshot_id
);
9406 mutex_unlock(&devlink
->lock
);
9410 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create
);
9412 #define DEVLINK_TRAP(_id, _type) \
9414 .type = DEVLINK_TRAP_TYPE_##_type, \
9415 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
9416 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
9419 static const struct devlink_trap devlink_trap_generic
[] = {
9420 DEVLINK_TRAP(SMAC_MC
, DROP
),
9421 DEVLINK_TRAP(VLAN_TAG_MISMATCH
, DROP
),
9422 DEVLINK_TRAP(INGRESS_VLAN_FILTER
, DROP
),
9423 DEVLINK_TRAP(INGRESS_STP_FILTER
, DROP
),
9424 DEVLINK_TRAP(EMPTY_TX_LIST
, DROP
),
9425 DEVLINK_TRAP(PORT_LOOPBACK_FILTER
, DROP
),
9426 DEVLINK_TRAP(BLACKHOLE_ROUTE
, DROP
),
9427 DEVLINK_TRAP(TTL_ERROR
, EXCEPTION
),
9428 DEVLINK_TRAP(TAIL_DROP
, DROP
),
9429 DEVLINK_TRAP(NON_IP_PACKET
, DROP
),
9430 DEVLINK_TRAP(UC_DIP_MC_DMAC
, DROP
),
9431 DEVLINK_TRAP(DIP_LB
, DROP
),
9432 DEVLINK_TRAP(SIP_MC
, DROP
),
9433 DEVLINK_TRAP(SIP_LB
, DROP
),
9434 DEVLINK_TRAP(CORRUPTED_IP_HDR
, DROP
),
9435 DEVLINK_TRAP(IPV4_SIP_BC
, DROP
),
9436 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE
, DROP
),
9437 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE
, DROP
),
9438 DEVLINK_TRAP(MTU_ERROR
, EXCEPTION
),
9439 DEVLINK_TRAP(UNRESOLVED_NEIGH
, EXCEPTION
),
9440 DEVLINK_TRAP(RPF
, EXCEPTION
),
9441 DEVLINK_TRAP(REJECT_ROUTE
, EXCEPTION
),
9442 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS
, EXCEPTION
),
9443 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS
, EXCEPTION
),
9444 DEVLINK_TRAP(NON_ROUTABLE
, DROP
),
9445 DEVLINK_TRAP(DECAP_ERROR
, EXCEPTION
),
9446 DEVLINK_TRAP(OVERLAY_SMAC_MC
, DROP
),
9447 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP
, DROP
),
9448 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP
, DROP
),
9449 DEVLINK_TRAP(STP
, CONTROL
),
9450 DEVLINK_TRAP(LACP
, CONTROL
),
9451 DEVLINK_TRAP(LLDP
, CONTROL
),
9452 DEVLINK_TRAP(IGMP_QUERY
, CONTROL
),
9453 DEVLINK_TRAP(IGMP_V1_REPORT
, CONTROL
),
9454 DEVLINK_TRAP(IGMP_V2_REPORT
, CONTROL
),
9455 DEVLINK_TRAP(IGMP_V3_REPORT
, CONTROL
),
9456 DEVLINK_TRAP(IGMP_V2_LEAVE
, CONTROL
),
9457 DEVLINK_TRAP(MLD_QUERY
, CONTROL
),
9458 DEVLINK_TRAP(MLD_V1_REPORT
, CONTROL
),
9459 DEVLINK_TRAP(MLD_V2_REPORT
, CONTROL
),
9460 DEVLINK_TRAP(MLD_V1_DONE
, CONTROL
),
9461 DEVLINK_TRAP(IPV4_DHCP
, CONTROL
),
9462 DEVLINK_TRAP(IPV6_DHCP
, CONTROL
),
9463 DEVLINK_TRAP(ARP_REQUEST
, CONTROL
),
9464 DEVLINK_TRAP(ARP_RESPONSE
, CONTROL
),
9465 DEVLINK_TRAP(ARP_OVERLAY
, CONTROL
),
9466 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT
, CONTROL
),
9467 DEVLINK_TRAP(IPV6_NEIGH_ADVERT
, CONTROL
),
9468 DEVLINK_TRAP(IPV4_BFD
, CONTROL
),
9469 DEVLINK_TRAP(IPV6_BFD
, CONTROL
),
9470 DEVLINK_TRAP(IPV4_OSPF
, CONTROL
),
9471 DEVLINK_TRAP(IPV6_OSPF
, CONTROL
),
9472 DEVLINK_TRAP(IPV4_BGP
, CONTROL
),
9473 DEVLINK_TRAP(IPV6_BGP
, CONTROL
),
9474 DEVLINK_TRAP(IPV4_VRRP
, CONTROL
),
9475 DEVLINK_TRAP(IPV6_VRRP
, CONTROL
),
9476 DEVLINK_TRAP(IPV4_PIM
, CONTROL
),
9477 DEVLINK_TRAP(IPV6_PIM
, CONTROL
),
9478 DEVLINK_TRAP(UC_LB
, CONTROL
),
9479 DEVLINK_TRAP(LOCAL_ROUTE
, CONTROL
),
9480 DEVLINK_TRAP(EXTERNAL_ROUTE
, CONTROL
),
9481 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE
, CONTROL
),
9482 DEVLINK_TRAP(IPV6_DIP_ALL_NODES
, CONTROL
),
9483 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS
, CONTROL
),
9484 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT
, CONTROL
),
9485 DEVLINK_TRAP(IPV6_ROUTER_ADVERT
, CONTROL
),
9486 DEVLINK_TRAP(IPV6_REDIRECT
, CONTROL
),
9487 DEVLINK_TRAP(IPV4_ROUTER_ALERT
, CONTROL
),
9488 DEVLINK_TRAP(IPV6_ROUTER_ALERT
, CONTROL
),
9489 DEVLINK_TRAP(PTP_EVENT
, CONTROL
),
9490 DEVLINK_TRAP(PTP_GENERAL
, CONTROL
),
9491 DEVLINK_TRAP(FLOW_ACTION_SAMPLE
, CONTROL
),
9492 DEVLINK_TRAP(FLOW_ACTION_TRAP
, CONTROL
),
9493 DEVLINK_TRAP(EARLY_DROP
, DROP
),
9494 DEVLINK_TRAP(VXLAN_PARSING
, DROP
),
9495 DEVLINK_TRAP(LLC_SNAP_PARSING
, DROP
),
9496 DEVLINK_TRAP(VLAN_PARSING
, DROP
),
9497 DEVLINK_TRAP(PPPOE_PPP_PARSING
, DROP
),
9498 DEVLINK_TRAP(MPLS_PARSING
, DROP
),
9499 DEVLINK_TRAP(ARP_PARSING
, DROP
),
9500 DEVLINK_TRAP(IP_1_PARSING
, DROP
),
9501 DEVLINK_TRAP(IP_N_PARSING
, DROP
),
9502 DEVLINK_TRAP(GRE_PARSING
, DROP
),
9503 DEVLINK_TRAP(UDP_PARSING
, DROP
),
9504 DEVLINK_TRAP(TCP_PARSING
, DROP
),
9505 DEVLINK_TRAP(IPSEC_PARSING
, DROP
),
9506 DEVLINK_TRAP(SCTP_PARSING
, DROP
),
9507 DEVLINK_TRAP(DCCP_PARSING
, DROP
),
9508 DEVLINK_TRAP(GTP_PARSING
, DROP
),
9509 DEVLINK_TRAP(ESP_PARSING
, DROP
),
9510 DEVLINK_TRAP(BLACKHOLE_NEXTHOP
, DROP
),
9513 #define DEVLINK_TRAP_GROUP(_id) \
9515 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
9516 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
9519 static const struct devlink_trap_group devlink_trap_group_generic
[] = {
9520 DEVLINK_TRAP_GROUP(L2_DROPS
),
9521 DEVLINK_TRAP_GROUP(L3_DROPS
),
9522 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS
),
9523 DEVLINK_TRAP_GROUP(BUFFER_DROPS
),
9524 DEVLINK_TRAP_GROUP(TUNNEL_DROPS
),
9525 DEVLINK_TRAP_GROUP(ACL_DROPS
),
9526 DEVLINK_TRAP_GROUP(STP
),
9527 DEVLINK_TRAP_GROUP(LACP
),
9528 DEVLINK_TRAP_GROUP(LLDP
),
9529 DEVLINK_TRAP_GROUP(MC_SNOOPING
),
9530 DEVLINK_TRAP_GROUP(DHCP
),
9531 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY
),
9532 DEVLINK_TRAP_GROUP(BFD
),
9533 DEVLINK_TRAP_GROUP(OSPF
),
9534 DEVLINK_TRAP_GROUP(BGP
),
9535 DEVLINK_TRAP_GROUP(VRRP
),
9536 DEVLINK_TRAP_GROUP(PIM
),
9537 DEVLINK_TRAP_GROUP(UC_LB
),
9538 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY
),
9539 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY
),
9540 DEVLINK_TRAP_GROUP(IPV6
),
9541 DEVLINK_TRAP_GROUP(PTP_EVENT
),
9542 DEVLINK_TRAP_GROUP(PTP_GENERAL
),
9543 DEVLINK_TRAP_GROUP(ACL_SAMPLE
),
9544 DEVLINK_TRAP_GROUP(ACL_TRAP
),
9545 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS
),
9548 static int devlink_trap_generic_verify(const struct devlink_trap
*trap
)
9550 if (trap
->id
> DEVLINK_TRAP_GENERIC_ID_MAX
)
9553 if (strcmp(trap
->name
, devlink_trap_generic
[trap
->id
].name
))
9556 if (trap
->type
!= devlink_trap_generic
[trap
->id
].type
)
9562 static int devlink_trap_driver_verify(const struct devlink_trap
*trap
)
9566 if (trap
->id
<= DEVLINK_TRAP_GENERIC_ID_MAX
)
9569 for (i
= 0; i
< ARRAY_SIZE(devlink_trap_generic
); i
++) {
9570 if (!strcmp(trap
->name
, devlink_trap_generic
[i
].name
))
9577 static int devlink_trap_verify(const struct devlink_trap
*trap
)
9579 if (!trap
|| !trap
->name
)
9583 return devlink_trap_generic_verify(trap
);
9585 return devlink_trap_driver_verify(trap
);
9589 devlink_trap_group_generic_verify(const struct devlink_trap_group
*group
)
9591 if (group
->id
> DEVLINK_TRAP_GROUP_GENERIC_ID_MAX
)
9594 if (strcmp(group
->name
, devlink_trap_group_generic
[group
->id
].name
))
9601 devlink_trap_group_driver_verify(const struct devlink_trap_group
*group
)
9605 if (group
->id
<= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX
)
9608 for (i
= 0; i
< ARRAY_SIZE(devlink_trap_group_generic
); i
++) {
9609 if (!strcmp(group
->name
, devlink_trap_group_generic
[i
].name
))
9616 static int devlink_trap_group_verify(const struct devlink_trap_group
*group
)
9619 return devlink_trap_group_generic_verify(group
);
9621 return devlink_trap_group_driver_verify(group
);
9625 devlink_trap_group_notify(struct devlink
*devlink
,
9626 const struct devlink_trap_group_item
*group_item
,
9627 enum devlink_command cmd
)
9629 struct sk_buff
*msg
;
9632 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_GROUP_NEW
&&
9633 cmd
!= DEVLINK_CMD_TRAP_GROUP_DEL
);
9635 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
9639 err
= devlink_nl_trap_group_fill(msg
, devlink
, group_item
, cmd
, 0, 0,
9646 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
9647 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
9651 devlink_trap_item_group_link(struct devlink
*devlink
,
9652 struct devlink_trap_item
*trap_item
)
9654 u16 group_id
= trap_item
->trap
->init_group_id
;
9655 struct devlink_trap_group_item
*group_item
;
9657 group_item
= devlink_trap_group_item_lookup_by_id(devlink
, group_id
);
9658 if (WARN_ON_ONCE(!group_item
))
9661 trap_item
->group_item
= group_item
;
9666 static void devlink_trap_notify(struct devlink
*devlink
,
9667 const struct devlink_trap_item
*trap_item
,
9668 enum devlink_command cmd
)
9670 struct sk_buff
*msg
;
9673 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_NEW
&&
9674 cmd
!= DEVLINK_CMD_TRAP_DEL
);
9676 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
9680 err
= devlink_nl_trap_fill(msg
, devlink
, trap_item
, cmd
, 0, 0, 0);
9686 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
9687 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
9691 devlink_trap_register(struct devlink
*devlink
,
9692 const struct devlink_trap
*trap
, void *priv
)
9694 struct devlink_trap_item
*trap_item
;
9697 if (devlink_trap_item_lookup(devlink
, trap
->name
))
9700 trap_item
= kzalloc(sizeof(*trap_item
), GFP_KERNEL
);
9704 trap_item
->stats
= netdev_alloc_pcpu_stats(struct devlink_stats
);
9705 if (!trap_item
->stats
) {
9707 goto err_stats_alloc
;
9710 trap_item
->trap
= trap
;
9711 trap_item
->action
= trap
->init_action
;
9712 trap_item
->priv
= priv
;
9714 err
= devlink_trap_item_group_link(devlink
, trap_item
);
9716 goto err_group_link
;
9718 err
= devlink
->ops
->trap_init(devlink
, trap
, trap_item
);
9722 list_add_tail(&trap_item
->list
, &devlink
->trap_list
);
9723 devlink_trap_notify(devlink
, trap_item
, DEVLINK_CMD_TRAP_NEW
);
9729 free_percpu(trap_item
->stats
);
9735 static void devlink_trap_unregister(struct devlink
*devlink
,
9736 const struct devlink_trap
*trap
)
9738 struct devlink_trap_item
*trap_item
;
9740 trap_item
= devlink_trap_item_lookup(devlink
, trap
->name
);
9741 if (WARN_ON_ONCE(!trap_item
))
9744 devlink_trap_notify(devlink
, trap_item
, DEVLINK_CMD_TRAP_DEL
);
9745 list_del(&trap_item
->list
);
9746 if (devlink
->ops
->trap_fini
)
9747 devlink
->ops
->trap_fini(devlink
, trap
, trap_item
);
9748 free_percpu(trap_item
->stats
);
9752 static void devlink_trap_disable(struct devlink
*devlink
,
9753 const struct devlink_trap
*trap
)
9755 struct devlink_trap_item
*trap_item
;
9757 trap_item
= devlink_trap_item_lookup(devlink
, trap
->name
);
9758 if (WARN_ON_ONCE(!trap_item
))
9761 devlink
->ops
->trap_action_set(devlink
, trap
, DEVLINK_TRAP_ACTION_DROP
,
9763 trap_item
->action
= DEVLINK_TRAP_ACTION_DROP
;
9767 * devlink_traps_register - Register packet traps with devlink.
9768 * @devlink: devlink.
9769 * @traps: Packet traps.
9770 * @traps_count: Count of provided packet traps.
9771 * @priv: Driver private information.
9773 * Return: Non-zero value on failure.
9775 int devlink_traps_register(struct devlink
*devlink
,
9776 const struct devlink_trap
*traps
,
9777 size_t traps_count
, void *priv
)
9781 if (!devlink
->ops
->trap_init
|| !devlink
->ops
->trap_action_set
)
9784 mutex_lock(&devlink
->lock
);
9785 for (i
= 0; i
< traps_count
; i
++) {
9786 const struct devlink_trap
*trap
= &traps
[i
];
9788 err
= devlink_trap_verify(trap
);
9790 goto err_trap_verify
;
9792 err
= devlink_trap_register(devlink
, trap
, priv
);
9794 goto err_trap_register
;
9796 mutex_unlock(&devlink
->lock
);
9802 for (i
--; i
>= 0; i
--)
9803 devlink_trap_unregister(devlink
, &traps
[i
]);
9804 mutex_unlock(&devlink
->lock
);
9807 EXPORT_SYMBOL_GPL(devlink_traps_register
);
9810 * devlink_traps_unregister - Unregister packet traps from devlink.
9811 * @devlink: devlink.
9812 * @traps: Packet traps.
9813 * @traps_count: Count of provided packet traps.
9815 void devlink_traps_unregister(struct devlink
*devlink
,
9816 const struct devlink_trap
*traps
,
9821 mutex_lock(&devlink
->lock
);
9822 /* Make sure we do not have any packets in-flight while unregistering
9823 * traps by disabling all of them and waiting for a grace period.
9825 for (i
= traps_count
- 1; i
>= 0; i
--)
9826 devlink_trap_disable(devlink
, &traps
[i
]);
9828 for (i
= traps_count
- 1; i
>= 0; i
--)
9829 devlink_trap_unregister(devlink
, &traps
[i
]);
9830 mutex_unlock(&devlink
->lock
);
9832 EXPORT_SYMBOL_GPL(devlink_traps_unregister
);
9835 devlink_trap_stats_update(struct devlink_stats __percpu
*trap_stats
,
9838 struct devlink_stats
*stats
;
9840 stats
= this_cpu_ptr(trap_stats
);
9841 u64_stats_update_begin(&stats
->syncp
);
9842 stats
->rx_bytes
+= skb_len
;
9843 stats
->rx_packets
++;
9844 u64_stats_update_end(&stats
->syncp
);
9848 devlink_trap_report_metadata_set(struct devlink_trap_metadata
*metadata
,
9849 const struct devlink_trap_item
*trap_item
,
9850 struct devlink_port
*in_devlink_port
,
9851 const struct flow_action_cookie
*fa_cookie
)
9853 metadata
->trap_name
= trap_item
->trap
->name
;
9854 metadata
->trap_group_name
= trap_item
->group_item
->group
->name
;
9855 metadata
->fa_cookie
= fa_cookie
;
9856 metadata
->trap_type
= trap_item
->trap
->type
;
9858 spin_lock(&in_devlink_port
->type_lock
);
9859 if (in_devlink_port
->type
== DEVLINK_PORT_TYPE_ETH
)
9860 metadata
->input_dev
= in_devlink_port
->type_dev
;
9861 spin_unlock(&in_devlink_port
->type_lock
);
9865 * devlink_trap_report - Report trapped packet to drop monitor.
9866 * @devlink: devlink.
9867 * @skb: Trapped packet.
9868 * @trap_ctx: Trap context.
9869 * @in_devlink_port: Input devlink port.
9870 * @fa_cookie: Flow action cookie. Could be NULL.
9872 void devlink_trap_report(struct devlink
*devlink
, struct sk_buff
*skb
,
9873 void *trap_ctx
, struct devlink_port
*in_devlink_port
,
9874 const struct flow_action_cookie
*fa_cookie
)
9877 struct devlink_trap_item
*trap_item
= trap_ctx
;
9879 devlink_trap_stats_update(trap_item
->stats
, skb
->len
);
9880 devlink_trap_stats_update(trap_item
->group_item
->stats
, skb
->len
);
9882 if (trace_devlink_trap_report_enabled()) {
9883 struct devlink_trap_metadata metadata
= {};
9885 devlink_trap_report_metadata_set(&metadata
, trap_item
,
9886 in_devlink_port
, fa_cookie
);
9887 trace_devlink_trap_report(devlink
, skb
, &metadata
);
9890 EXPORT_SYMBOL_GPL(devlink_trap_report
);
9893 * devlink_trap_ctx_priv - Trap context to driver private information.
9894 * @trap_ctx: Trap context.
9896 * Return: Driver private information passed during registration.
9898 void *devlink_trap_ctx_priv(void *trap_ctx
)
9900 struct devlink_trap_item
*trap_item
= trap_ctx
;
9902 return trap_item
->priv
;
9904 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv
);
9907 devlink_trap_group_item_policer_link(struct devlink
*devlink
,
9908 struct devlink_trap_group_item
*group_item
)
9910 u32 policer_id
= group_item
->group
->init_policer_id
;
9911 struct devlink_trap_policer_item
*policer_item
;
9913 if (policer_id
== 0)
9916 policer_item
= devlink_trap_policer_item_lookup(devlink
, policer_id
);
9917 if (WARN_ON_ONCE(!policer_item
))
9920 group_item
->policer_item
= policer_item
;
9926 devlink_trap_group_register(struct devlink
*devlink
,
9927 const struct devlink_trap_group
*group
)
9929 struct devlink_trap_group_item
*group_item
;
9932 if (devlink_trap_group_item_lookup(devlink
, group
->name
))
9935 group_item
= kzalloc(sizeof(*group_item
), GFP_KERNEL
);
9939 group_item
->stats
= netdev_alloc_pcpu_stats(struct devlink_stats
);
9940 if (!group_item
->stats
) {
9942 goto err_stats_alloc
;
9945 group_item
->group
= group
;
9947 err
= devlink_trap_group_item_policer_link(devlink
, group_item
);
9949 goto err_policer_link
;
9951 if (devlink
->ops
->trap_group_init
) {
9952 err
= devlink
->ops
->trap_group_init(devlink
, group
);
9954 goto err_group_init
;
9957 list_add_tail(&group_item
->list
, &devlink
->trap_group_list
);
9958 devlink_trap_group_notify(devlink
, group_item
,
9959 DEVLINK_CMD_TRAP_GROUP_NEW
);
9965 free_percpu(group_item
->stats
);
9972 devlink_trap_group_unregister(struct devlink
*devlink
,
9973 const struct devlink_trap_group
*group
)
9975 struct devlink_trap_group_item
*group_item
;
9977 group_item
= devlink_trap_group_item_lookup(devlink
, group
->name
);
9978 if (WARN_ON_ONCE(!group_item
))
9981 devlink_trap_group_notify(devlink
, group_item
,
9982 DEVLINK_CMD_TRAP_GROUP_DEL
);
9983 list_del(&group_item
->list
);
9984 free_percpu(group_item
->stats
);
9989 * devlink_trap_groups_register - Register packet trap groups with devlink.
9990 * @devlink: devlink.
9991 * @groups: Packet trap groups.
9992 * @groups_count: Count of provided packet trap groups.
9994 * Return: Non-zero value on failure.
9996 int devlink_trap_groups_register(struct devlink
*devlink
,
9997 const struct devlink_trap_group
*groups
,
9998 size_t groups_count
)
10002 mutex_lock(&devlink
->lock
);
10003 for (i
= 0; i
< groups_count
; i
++) {
10004 const struct devlink_trap_group
*group
= &groups
[i
];
10006 err
= devlink_trap_group_verify(group
);
10008 goto err_trap_group_verify
;
10010 err
= devlink_trap_group_register(devlink
, group
);
10012 goto err_trap_group_register
;
10014 mutex_unlock(&devlink
->lock
);
10018 err_trap_group_register
:
10019 err_trap_group_verify
:
10020 for (i
--; i
>= 0; i
--)
10021 devlink_trap_group_unregister(devlink
, &groups
[i
]);
10022 mutex_unlock(&devlink
->lock
);
10025 EXPORT_SYMBOL_GPL(devlink_trap_groups_register
);
10028 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
10029 * @devlink: devlink.
10030 * @groups: Packet trap groups.
10031 * @groups_count: Count of provided packet trap groups.
10033 void devlink_trap_groups_unregister(struct devlink
*devlink
,
10034 const struct devlink_trap_group
*groups
,
10035 size_t groups_count
)
10039 mutex_lock(&devlink
->lock
);
10040 for (i
= groups_count
- 1; i
>= 0; i
--)
10041 devlink_trap_group_unregister(devlink
, &groups
[i
]);
10042 mutex_unlock(&devlink
->lock
);
10044 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister
);
10047 devlink_trap_policer_notify(struct devlink
*devlink
,
10048 const struct devlink_trap_policer_item
*policer_item
,
10049 enum devlink_command cmd
)
10051 struct sk_buff
*msg
;
10054 WARN_ON_ONCE(cmd
!= DEVLINK_CMD_TRAP_POLICER_NEW
&&
10055 cmd
!= DEVLINK_CMD_TRAP_POLICER_DEL
);
10057 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
10061 err
= devlink_nl_trap_policer_fill(msg
, devlink
, policer_item
, cmd
, 0,
10068 genlmsg_multicast_netns(&devlink_nl_family
, devlink_net(devlink
),
10069 msg
, 0, DEVLINK_MCGRP_CONFIG
, GFP_KERNEL
);
10073 devlink_trap_policer_register(struct devlink
*devlink
,
10074 const struct devlink_trap_policer
*policer
)
10076 struct devlink_trap_policer_item
*policer_item
;
10079 if (devlink_trap_policer_item_lookup(devlink
, policer
->id
))
10082 policer_item
= kzalloc(sizeof(*policer_item
), GFP_KERNEL
);
10086 policer_item
->policer
= policer
;
10087 policer_item
->rate
= policer
->init_rate
;
10088 policer_item
->burst
= policer
->init_burst
;
10090 if (devlink
->ops
->trap_policer_init
) {
10091 err
= devlink
->ops
->trap_policer_init(devlink
, policer
);
10093 goto err_policer_init
;
10096 list_add_tail(&policer_item
->list
, &devlink
->trap_policer_list
);
10097 devlink_trap_policer_notify(devlink
, policer_item
,
10098 DEVLINK_CMD_TRAP_POLICER_NEW
);
10103 kfree(policer_item
);
10108 devlink_trap_policer_unregister(struct devlink
*devlink
,
10109 const struct devlink_trap_policer
*policer
)
10111 struct devlink_trap_policer_item
*policer_item
;
10113 policer_item
= devlink_trap_policer_item_lookup(devlink
, policer
->id
);
10114 if (WARN_ON_ONCE(!policer_item
))
10117 devlink_trap_policer_notify(devlink
, policer_item
,
10118 DEVLINK_CMD_TRAP_POLICER_DEL
);
10119 list_del(&policer_item
->list
);
10120 if (devlink
->ops
->trap_policer_fini
)
10121 devlink
->ops
->trap_policer_fini(devlink
, policer
);
10122 kfree(policer_item
);
10126 * devlink_trap_policers_register - Register packet trap policers with devlink.
10127 * @devlink: devlink.
10128 * @policers: Packet trap policers.
10129 * @policers_count: Count of provided packet trap policers.
10131 * Return: Non-zero value on failure.
10134 devlink_trap_policers_register(struct devlink
*devlink
,
10135 const struct devlink_trap_policer
*policers
,
10136 size_t policers_count
)
10140 mutex_lock(&devlink
->lock
);
10141 for (i
= 0; i
< policers_count
; i
++) {
10142 const struct devlink_trap_policer
*policer
= &policers
[i
];
10144 if (WARN_ON(policer
->id
== 0 ||
10145 policer
->max_rate
< policer
->min_rate
||
10146 policer
->max_burst
< policer
->min_burst
)) {
10148 goto err_trap_policer_verify
;
10151 err
= devlink_trap_policer_register(devlink
, policer
);
10153 goto err_trap_policer_register
;
10155 mutex_unlock(&devlink
->lock
);
10159 err_trap_policer_register
:
10160 err_trap_policer_verify
:
10161 for (i
--; i
>= 0; i
--)
10162 devlink_trap_policer_unregister(devlink
, &policers
[i
]);
10163 mutex_unlock(&devlink
->lock
);
10166 EXPORT_SYMBOL_GPL(devlink_trap_policers_register
);
10169 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
10170 * @devlink: devlink.
10171 * @policers: Packet trap policers.
10172 * @policers_count: Count of provided packet trap policers.
10175 devlink_trap_policers_unregister(struct devlink
*devlink
,
10176 const struct devlink_trap_policer
*policers
,
10177 size_t policers_count
)
10181 mutex_lock(&devlink
->lock
);
10182 for (i
= policers_count
- 1; i
>= 0; i
--)
10183 devlink_trap_policer_unregister(devlink
, &policers
[i
]);
10184 mutex_unlock(&devlink
->lock
);
10186 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister
);
10188 static void __devlink_compat_running_version(struct devlink
*devlink
,
10189 char *buf
, size_t len
)
10191 const struct nlattr
*nlattr
;
10192 struct devlink_info_req req
;
10193 struct sk_buff
*msg
;
10196 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
10201 err
= devlink
->ops
->info_get(devlink
, &req
, NULL
);
10205 nla_for_each_attr(nlattr
, (void *)msg
->data
, msg
->len
, rem
) {
10206 const struct nlattr
*kv
;
10209 if (nla_type(nlattr
) != DEVLINK_ATTR_INFO_VERSION_RUNNING
)
10212 nla_for_each_nested(kv
, nlattr
, rem_kv
) {
10213 if (nla_type(kv
) != DEVLINK_ATTR_INFO_VERSION_VALUE
)
10216 strlcat(buf
, nla_data(kv
), len
);
10217 strlcat(buf
, " ", len
);
10224 void devlink_compat_running_version(struct net_device
*dev
,
10225 char *buf
, size_t len
)
10227 struct devlink
*devlink
;
10232 devlink
= netdev_to_devlink(dev
);
10233 if (!devlink
|| !devlink
->ops
->info_get
)
10236 mutex_lock(&devlink
->lock
);
10237 __devlink_compat_running_version(devlink
, buf
, len
);
10238 mutex_unlock(&devlink
->lock
);
10245 int devlink_compat_flash_update(struct net_device
*dev
, const char *file_name
)
10247 struct devlink_flash_update_params params
= {};
10248 struct devlink
*devlink
;
10254 devlink
= netdev_to_devlink(dev
);
10255 if (!devlink
|| !devlink
->ops
->flash_update
) {
10260 ret
= request_firmware(¶ms
.fw
, file_name
, devlink
->dev
);
10264 mutex_lock(&devlink
->lock
);
10265 devlink_flash_update_begin_notify(devlink
);
10266 ret
= devlink
->ops
->flash_update(devlink
, ¶ms
, NULL
);
10267 devlink_flash_update_end_notify(devlink
);
10268 mutex_unlock(&devlink
->lock
);
10270 release_firmware(params
.fw
);
10279 int devlink_compat_phys_port_name_get(struct net_device
*dev
,
10280 char *name
, size_t len
)
10282 struct devlink_port
*devlink_port
;
10284 /* RTNL mutex is held here which ensures that devlink_port
10285 * instance cannot disappear in the middle. No need to take
10286 * any devlink lock as only permanent values are accessed.
10290 devlink_port
= netdev_to_devlink_port(dev
);
10292 return -EOPNOTSUPP
;
10294 return __devlink_port_phys_port_name_get(devlink_port
, name
, len
);
10297 int devlink_compat_switch_id_get(struct net_device
*dev
,
10298 struct netdev_phys_item_id
*ppid
)
10300 struct devlink_port
*devlink_port
;
10302 /* Caller must hold RTNL mutex or reference to dev, which ensures that
10303 * devlink_port instance cannot disappear in the middle. No need to take
10304 * any devlink lock as only permanent values are accessed.
10306 devlink_port
= netdev_to_devlink_port(dev
);
10307 if (!devlink_port
|| !devlink_port
->switch_port
)
10308 return -EOPNOTSUPP
;
10310 memcpy(ppid
, &devlink_port
->attrs
.switch_id
, sizeof(*ppid
));
10315 static void __net_exit
devlink_pernet_pre_exit(struct net
*net
)
10317 struct devlink
*devlink
;
10318 u32 actions_performed
;
10321 /* In case network namespace is getting destroyed, reload
10322 * all devlink instances from this namespace into init_net.
10324 mutex_lock(&devlink_mutex
);
10325 list_for_each_entry(devlink
, &devlink_list
, list
) {
10326 if (net_eq(devlink_net(devlink
), net
)) {
10327 if (WARN_ON(!devlink_reload_supported(devlink
->ops
)))
10329 err
= devlink_reload(devlink
, &init_net
,
10330 DEVLINK_RELOAD_ACTION_DRIVER_REINIT
,
10331 DEVLINK_RELOAD_LIMIT_UNSPEC
,
10332 &actions_performed
, NULL
);
10333 if (err
&& err
!= -EOPNOTSUPP
)
10334 pr_warn("Failed to reload devlink instance into init_net\n");
10337 mutex_unlock(&devlink_mutex
);
10340 static struct pernet_operations devlink_pernet_ops __net_initdata
= {
10341 .pre_exit
= devlink_pernet_pre_exit
,
10344 static int __init
devlink_init(void)
10348 err
= genl_register_family(&devlink_nl_family
);
10351 err
= register_pernet_subsys(&devlink_pernet_ops
);
10358 subsys_initcall(devlink_init
);