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.
10 #include <linux/etherdevice.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/gfp.h>
16 #include <linux/device.h>
17 #include <linux/list.h>
18 #include <linux/netdevice.h>
19 #include <linux/spinlock.h>
20 #include <linux/refcount.h>
21 #include <linux/workqueue.h>
22 #include <linux/u64_stats_sync.h>
23 #include <linux/timekeeping.h>
24 #include <rdma/ib_verbs.h>
25 #include <net/netlink.h>
26 #include <net/genetlink.h>
27 #include <net/rtnetlink.h>
28 #include <net/net_namespace.h>
30 #include <net/devlink.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devlink.h>
34 #define DEVLINK_RELOAD_STATS_ARRAY_SIZE \
35 (__DEVLINK_RELOAD_LIMIT_MAX * __DEVLINK_RELOAD_ACTION_MAX)
37 struct devlink_dev_stats {
38 u32 reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
39 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
44 struct list_head port_list;
45 struct list_head rate_list;
46 struct list_head sb_list;
47 struct list_head dpipe_table_list;
48 struct list_head resource_list;
49 struct list_head param_list;
50 struct list_head region_list;
51 struct list_head reporter_list;
52 struct mutex reporters_lock; /* protects reporter_list */
53 struct devlink_dpipe_headers *dpipe_headers;
54 struct list_head trap_list;
55 struct list_head trap_group_list;
56 struct list_head trap_policer_list;
57 struct list_head linecard_list;
58 struct mutex linecards_lock; /* protects linecard_list */
59 const struct devlink_ops *ops;
61 struct xarray snapshot_ids;
62 struct devlink_dev_stats stats;
65 /* Serializes access to devlink instance specific objects such as
66 * port, sb, dpipe, resource, params, region, traps and more.
69 struct lock_class_key lock_key;
72 struct completion comp;
74 char priv[] __aligned(NETDEV_ALIGN);
77 struct devlink_linecard_ops;
78 struct devlink_linecard_type;
80 struct devlink_linecard {
81 struct list_head list;
82 struct devlink *devlink;
85 const struct devlink_linecard_ops *ops;
87 enum devlink_linecard_state state;
88 struct mutex state_lock; /* Protects state */
90 struct devlink_linecard_type *types;
91 unsigned int types_count;
92 struct devlink *nested_devlink;
96 * struct devlink_resource - devlink resource
97 * @name: name of the resource
98 * @id: id, per devlink instance
99 * @size: size of the resource
100 * @size_new: updated size of the resource, reload is needed
101 * @size_valid: valid in case the total size of the resource is valid
102 * including its children
103 * @parent: parent resource
104 * @size_params: size parameters
106 * @resource_list: list of child resources
107 * @occ_get: occupancy getter callback
108 * @occ_get_priv: occupancy getter callback priv
110 struct devlink_resource {
116 struct devlink_resource *parent;
117 struct devlink_resource_size_params size_params;
118 struct list_head list;
119 struct list_head resource_list;
120 devlink_resource_occ_get_t *occ_get;
124 void *devlink_priv(struct devlink *devlink)
126 return &devlink->priv;
128 EXPORT_SYMBOL_GPL(devlink_priv);
130 struct devlink *priv_to_devlink(void *priv)
132 return container_of(priv, struct devlink, priv);
134 EXPORT_SYMBOL_GPL(priv_to_devlink);
136 struct device *devlink_to_dev(const struct devlink *devlink)
140 EXPORT_SYMBOL_GPL(devlink_to_dev);
142 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
144 .name = "destination mac",
145 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
150 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
152 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
153 .fields = devlink_dpipe_fields_ethernet,
154 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
157 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
159 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
161 .name = "destination ip",
162 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
167 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
169 .id = DEVLINK_DPIPE_HEADER_IPV4,
170 .fields = devlink_dpipe_fields_ipv4,
171 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
174 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
176 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
178 .name = "destination ip",
179 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
184 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
186 .id = DEVLINK_DPIPE_HEADER_IPV6,
187 .fields = devlink_dpipe_fields_ipv6,
188 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
191 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
193 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
194 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
195 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
197 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
198 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
199 [DEVLINK_PORT_FN_ATTR_STATE] =
200 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
201 DEVLINK_PORT_FN_STATE_ACTIVE),
204 static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
205 [DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
208 static DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
209 #define DEVLINK_REGISTERED XA_MARK_1
210 #define DEVLINK_UNREGISTERING XA_MARK_2
212 /* devlink instances are open to the access from the user space after
213 * devlink_register() call. Such logical barrier allows us to have certain
214 * expectations related to locking.
216 * Before *_register() - we are in initialization stage and no parallel
217 * access possible to the devlink instance. All drivers perform that phase
218 * by implicitly holding device_lock.
220 * After *_register() - users and driver can access devlink instance at
223 #define ASSERT_DEVLINK_REGISTERED(d) \
224 WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
225 #define ASSERT_DEVLINK_NOT_REGISTERED(d) \
226 WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
228 struct net *devlink_net(const struct devlink *devlink)
230 return read_pnet(&devlink->_net);
232 EXPORT_SYMBOL_GPL(devlink_net);
234 static void __devlink_put_rcu(struct rcu_head *head)
236 struct devlink *devlink = container_of(head, struct devlink, rcu);
238 complete(&devlink->comp);
241 void devlink_put(struct devlink *devlink)
243 if (refcount_dec_and_test(&devlink->refcount))
244 /* Make sure unregister operation that may await the completion
245 * is unblocked only after all users are after the end of
248 call_rcu(&devlink->rcu, __devlink_put_rcu);
251 struct devlink *__must_check devlink_try_get(struct devlink *devlink)
253 if (refcount_inc_not_zero(&devlink->refcount))
258 void devl_assert_locked(struct devlink *devlink)
260 lockdep_assert_held(&devlink->lock);
262 EXPORT_SYMBOL_GPL(devl_assert_locked);
264 #ifdef CONFIG_LOCKDEP
265 /* For use in conjunction with LOCKDEP only e.g. rcu_dereference_protected() */
266 bool devl_lock_is_held(struct devlink *devlink)
268 return lockdep_is_held(&devlink->lock);
270 EXPORT_SYMBOL_GPL(devl_lock_is_held);
273 void devl_lock(struct devlink *devlink)
275 mutex_lock(&devlink->lock);
277 EXPORT_SYMBOL_GPL(devl_lock);
279 int devl_trylock(struct devlink *devlink)
281 return mutex_trylock(&devlink->lock);
283 EXPORT_SYMBOL_GPL(devl_trylock);
285 void devl_unlock(struct devlink *devlink)
287 mutex_unlock(&devlink->lock);
289 EXPORT_SYMBOL_GPL(devl_unlock);
291 static struct devlink *
292 devlinks_xa_find_get(struct net *net, unsigned long *indexp, xa_mark_t filter,
293 void * (*xa_find_fn)(struct xarray *, unsigned long *,
294 unsigned long, xa_mark_t))
296 struct devlink *devlink;
300 devlink = xa_find_fn(&devlinks, indexp, ULONG_MAX, DEVLINK_REGISTERED);
304 /* In case devlink_unregister() was already called and "unregistering"
305 * mark was set, do not allow to get a devlink reference here.
306 * This prevents live-lock of devlink_unregister() wait for completion.
308 if (xa_get_mark(&devlinks, *indexp, DEVLINK_UNREGISTERING))
311 /* For a possible retry, the xa_find_after() should be always used */
312 xa_find_fn = xa_find_after;
313 if (!devlink_try_get(devlink))
315 if (!net_eq(devlink_net(devlink), net)) {
316 devlink_put(devlink);
324 static struct devlink *devlinks_xa_find_get_first(struct net *net,
325 unsigned long *indexp,
328 return devlinks_xa_find_get(net, indexp, filter, xa_find);
331 static struct devlink *devlinks_xa_find_get_next(struct net *net,
332 unsigned long *indexp,
335 return devlinks_xa_find_get(net, indexp, filter, xa_find_after);
338 /* Iterate over devlink pointers which were possible to get reference to.
339 * devlink_put() needs to be called for each iterated devlink pointer
340 * in loop body in order to release the reference.
342 #define devlinks_xa_for_each_get(net, index, devlink, filter) \
344 devlink = devlinks_xa_find_get_first(net, &index, filter); \
345 devlink; devlink = devlinks_xa_find_get_next(net, &index, filter))
347 #define devlinks_xa_for_each_registered_get(net, index, devlink) \
348 devlinks_xa_for_each_get(net, index, devlink, DEVLINK_REGISTERED)
350 static struct devlink *devlink_get_from_attrs(struct net *net,
351 struct nlattr **attrs)
353 struct devlink *devlink;
358 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
359 return ERR_PTR(-EINVAL);
361 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
362 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
364 devlinks_xa_for_each_registered_get(net, index, devlink) {
365 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
366 strcmp(dev_name(devlink->dev), devname) == 0)
368 devlink_put(devlink);
371 return ERR_PTR(-ENODEV);
374 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
375 unsigned int port_index)
377 struct devlink_port *devlink_port;
379 list_for_each_entry(devlink_port, &devlink->port_list, list) {
380 if (devlink_port->index == port_index)
386 static bool devlink_port_index_exists(struct devlink *devlink,
387 unsigned int port_index)
389 return devlink_port_get_by_index(devlink, port_index);
392 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
393 struct nlattr **attrs)
395 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
396 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
397 struct devlink_port *devlink_port;
399 devlink_port = devlink_port_get_by_index(devlink, port_index);
401 return ERR_PTR(-ENODEV);
404 return ERR_PTR(-EINVAL);
407 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
408 struct genl_info *info)
410 return devlink_port_get_from_attrs(devlink, info->attrs);
414 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
416 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
420 devlink_rate_is_node(struct devlink_rate *devlink_rate)
422 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
425 static struct devlink_rate *
426 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
428 struct devlink_rate *devlink_rate;
429 struct devlink_port *devlink_port;
431 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
432 if (IS_ERR(devlink_port))
433 return ERR_CAST(devlink_port);
434 devlink_rate = devlink_port->devlink_rate;
435 return devlink_rate ?: ERR_PTR(-ENODEV);
438 static struct devlink_rate *
439 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
441 static struct devlink_rate *devlink_rate;
443 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
444 if (devlink_rate_is_node(devlink_rate) &&
445 !strcmp(node_name, devlink_rate->name))
448 return ERR_PTR(-ENODEV);
451 static struct devlink_rate *
452 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
454 const char *rate_node_name;
457 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
458 return ERR_PTR(-EINVAL);
459 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
460 len = strlen(rate_node_name);
461 /* Name cannot be empty or decimal number */
462 if (!len || strspn(rate_node_name, "0123456789") == len)
463 return ERR_PTR(-EINVAL);
465 return devlink_rate_node_get_by_name(devlink, rate_node_name);
468 static struct devlink_rate *
469 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
471 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
474 static struct devlink_rate *
475 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
477 struct nlattr **attrs = info->attrs;
479 if (attrs[DEVLINK_ATTR_PORT_INDEX])
480 return devlink_rate_leaf_get_from_info(devlink, info);
481 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
482 return devlink_rate_node_get_from_info(devlink, info);
484 return ERR_PTR(-EINVAL);
487 static struct devlink_linecard *
488 devlink_linecard_get_by_index(struct devlink *devlink,
489 unsigned int linecard_index)
491 struct devlink_linecard *devlink_linecard;
493 list_for_each_entry(devlink_linecard, &devlink->linecard_list, list) {
494 if (devlink_linecard->index == linecard_index)
495 return devlink_linecard;
500 static bool devlink_linecard_index_exists(struct devlink *devlink,
501 unsigned int linecard_index)
503 return devlink_linecard_get_by_index(devlink, linecard_index);
506 static struct devlink_linecard *
507 devlink_linecard_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
509 if (attrs[DEVLINK_ATTR_LINECARD_INDEX]) {
510 u32 linecard_index = nla_get_u32(attrs[DEVLINK_ATTR_LINECARD_INDEX]);
511 struct devlink_linecard *linecard;
513 mutex_lock(&devlink->linecards_lock);
514 linecard = devlink_linecard_get_by_index(devlink, linecard_index);
516 refcount_inc(&linecard->refcount);
517 mutex_unlock(&devlink->linecards_lock);
519 return ERR_PTR(-ENODEV);
522 return ERR_PTR(-EINVAL);
525 static struct devlink_linecard *
526 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info)
528 return devlink_linecard_get_from_attrs(devlink, info->attrs);
531 static void devlink_linecard_put(struct devlink_linecard *linecard)
533 if (refcount_dec_and_test(&linecard->refcount)) {
534 mutex_destroy(&linecard->state_lock);
540 struct list_head list;
543 u16 ingress_pools_count;
544 u16 egress_pools_count;
545 u16 ingress_tc_count;
549 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
551 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
554 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
555 unsigned int sb_index)
557 struct devlink_sb *devlink_sb;
559 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
560 if (devlink_sb->index == sb_index)
566 static bool devlink_sb_index_exists(struct devlink *devlink,
567 unsigned int sb_index)
569 return devlink_sb_get_by_index(devlink, sb_index);
572 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
573 struct nlattr **attrs)
575 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
576 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
577 struct devlink_sb *devlink_sb;
579 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
581 return ERR_PTR(-ENODEV);
584 return ERR_PTR(-EINVAL);
587 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
588 struct genl_info *info)
590 return devlink_sb_get_from_attrs(devlink, info->attrs);
593 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
594 struct nlattr **attrs,
599 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
602 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
603 if (val >= devlink_sb_pool_count(devlink_sb))
609 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
610 struct genl_info *info,
613 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
618 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
619 enum devlink_sb_pool_type *p_pool_type)
623 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
626 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
627 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
628 val != DEVLINK_SB_POOL_TYPE_EGRESS)
635 devlink_sb_pool_type_get_from_info(struct genl_info *info,
636 enum devlink_sb_pool_type *p_pool_type)
638 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
642 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
643 enum devlink_sb_threshold_type *p_th_type)
647 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
650 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
651 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
652 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
659 devlink_sb_th_type_get_from_info(struct genl_info *info,
660 enum devlink_sb_threshold_type *p_th_type)
662 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
666 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
667 struct nlattr **attrs,
668 enum devlink_sb_pool_type pool_type,
673 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
676 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
677 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
678 val >= devlink_sb->ingress_tc_count)
680 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
681 val >= devlink_sb->egress_tc_count)
688 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
689 struct genl_info *info,
690 enum devlink_sb_pool_type pool_type,
693 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
694 pool_type, p_tc_index);
697 struct devlink_region {
698 struct devlink *devlink;
699 struct devlink_port *port;
700 struct list_head list;
702 const struct devlink_region_ops *ops;
703 const struct devlink_port_region_ops *port_ops;
705 struct mutex snapshot_lock; /* protects snapshot_list,
706 * max_snapshots and cur_snapshots
709 struct list_head snapshot_list;
715 struct devlink_snapshot {
716 struct list_head list;
717 struct devlink_region *region;
722 static struct devlink_region *
723 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
725 struct devlink_region *region;
727 list_for_each_entry(region, &devlink->region_list, list)
728 if (!strcmp(region->ops->name, region_name))
734 static struct devlink_region *
735 devlink_port_region_get_by_name(struct devlink_port *port,
736 const char *region_name)
738 struct devlink_region *region;
740 list_for_each_entry(region, &port->region_list, list)
741 if (!strcmp(region->ops->name, region_name))
747 static struct devlink_snapshot *
748 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
750 struct devlink_snapshot *snapshot;
752 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
753 if (snapshot->id == id)
759 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
760 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
761 #define DEVLINK_NL_FLAG_NEED_RATE BIT(2)
762 #define DEVLINK_NL_FLAG_NEED_RATE_NODE BIT(3)
763 #define DEVLINK_NL_FLAG_NEED_LINECARD BIT(4)
765 static int devlink_nl_pre_doit(const struct genl_ops *ops,
766 struct sk_buff *skb, struct genl_info *info)
768 struct devlink_linecard *linecard;
769 struct devlink_port *devlink_port;
770 struct devlink *devlink;
773 devlink = devlink_get_from_attrs(genl_info_net(info), info->attrs);
775 return PTR_ERR(devlink);
777 info->user_ptr[0] = devlink;
778 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
779 devlink_port = devlink_port_get_from_info(devlink, info);
780 if (IS_ERR(devlink_port)) {
781 err = PTR_ERR(devlink_port);
784 info->user_ptr[1] = devlink_port;
785 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
786 devlink_port = devlink_port_get_from_info(devlink, info);
787 if (!IS_ERR(devlink_port))
788 info->user_ptr[1] = devlink_port;
789 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE) {
790 struct devlink_rate *devlink_rate;
792 devlink_rate = devlink_rate_get_from_info(devlink, info);
793 if (IS_ERR(devlink_rate)) {
794 err = PTR_ERR(devlink_rate);
797 info->user_ptr[1] = devlink_rate;
798 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE_NODE) {
799 struct devlink_rate *rate_node;
801 rate_node = devlink_rate_node_get_from_info(devlink, info);
802 if (IS_ERR(rate_node)) {
803 err = PTR_ERR(rate_node);
806 info->user_ptr[1] = rate_node;
807 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_LINECARD) {
808 linecard = devlink_linecard_get_from_info(devlink, info);
809 if (IS_ERR(linecard)) {
810 err = PTR_ERR(linecard);
813 info->user_ptr[1] = linecard;
818 devl_unlock(devlink);
819 devlink_put(devlink);
823 static void devlink_nl_post_doit(const struct genl_ops *ops,
824 struct sk_buff *skb, struct genl_info *info)
826 struct devlink_linecard *linecard;
827 struct devlink *devlink;
829 devlink = info->user_ptr[0];
830 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_LINECARD) {
831 linecard = info->user_ptr[1];
832 devlink_linecard_put(linecard);
834 devl_unlock(devlink);
835 devlink_put(devlink);
838 static struct genl_family devlink_nl_family;
840 enum devlink_multicast_groups {
841 DEVLINK_MCGRP_CONFIG,
844 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
845 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
848 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
850 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
852 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
857 static int devlink_nl_put_nested_handle(struct sk_buff *msg, struct devlink *devlink)
859 struct nlattr *nested_attr;
861 nested_attr = nla_nest_start(msg, DEVLINK_ATTR_NESTED_DEVLINK);
864 if (devlink_nl_put_handle(msg, devlink))
865 goto nla_put_failure;
867 nla_nest_end(msg, nested_attr);
871 nla_nest_cancel(msg, nested_attr);
875 struct devlink_reload_combination {
876 enum devlink_reload_action action;
877 enum devlink_reload_limit limit;
880 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
882 /* can't reinitialize driver with no down time */
883 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
884 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
889 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
890 enum devlink_reload_limit limit)
894 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
895 if (devlink_reload_invalid_combinations[i].action == action &&
896 devlink_reload_invalid_combinations[i].limit == limit)
902 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
904 return test_bit(action, &devlink->ops->reload_actions);
908 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
910 return test_bit(limit, &devlink->ops->reload_limits);
913 static int devlink_reload_stat_put(struct sk_buff *msg,
914 enum devlink_reload_limit limit, u32 value)
916 struct nlattr *reload_stats_entry;
918 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
919 if (!reload_stats_entry)
922 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
923 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
924 goto nla_put_failure;
925 nla_nest_end(msg, reload_stats_entry);
929 nla_nest_cancel(msg, reload_stats_entry);
933 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
935 struct nlattr *reload_stats_attr, *act_info, *act_stats;
940 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
942 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
944 if (!reload_stats_attr)
947 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
949 !devlink_reload_action_is_supported(devlink, i)) ||
950 i == DEVLINK_RELOAD_ACTION_UNSPEC)
952 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
954 goto nla_put_failure;
956 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
957 goto action_info_nest_cancel;
958 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
960 goto action_info_nest_cancel;
962 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
963 /* Remote stats are shown even if not locally supported.
964 * Stats of actions with unspecified limit are shown
965 * though drivers don't need to register unspecified
968 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
969 !devlink_reload_limit_is_supported(devlink, j)) ||
970 devlink_reload_combination_is_invalid(i, j))
973 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
975 value = devlink->stats.reload_stats[stat_idx];
977 value = devlink->stats.remote_reload_stats[stat_idx];
978 if (devlink_reload_stat_put(msg, j, value))
979 goto action_stats_nest_cancel;
981 nla_nest_end(msg, act_stats);
982 nla_nest_end(msg, act_info);
984 nla_nest_end(msg, reload_stats_attr);
987 action_stats_nest_cancel:
988 nla_nest_cancel(msg, act_stats);
989 action_info_nest_cancel:
990 nla_nest_cancel(msg, act_info);
992 nla_nest_cancel(msg, reload_stats_attr);
996 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
997 enum devlink_command cmd, u32 portid,
1000 struct nlattr *dev_stats;
1003 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1007 if (devlink_nl_put_handle(msg, devlink))
1008 goto nla_put_failure;
1009 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
1010 goto nla_put_failure;
1012 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
1014 goto nla_put_failure;
1016 if (devlink_reload_stats_put(msg, devlink, false))
1017 goto dev_stats_nest_cancel;
1018 if (devlink_reload_stats_put(msg, devlink, true))
1019 goto dev_stats_nest_cancel;
1021 nla_nest_end(msg, dev_stats);
1022 genlmsg_end(msg, hdr);
1025 dev_stats_nest_cancel:
1026 nla_nest_cancel(msg, dev_stats);
1028 genlmsg_cancel(msg, hdr);
1032 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
1034 struct sk_buff *msg;
1037 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
1038 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
1040 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1044 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
1050 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
1051 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1054 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
1055 struct devlink_port *devlink_port)
1057 struct devlink_port_attrs *attrs = &devlink_port->attrs;
1059 if (!devlink_port->attrs_set)
1062 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
1065 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
1067 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
1069 switch (devlink_port->attrs.flavour) {
1070 case DEVLINK_PORT_FLAVOUR_PCI_PF:
1071 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
1072 attrs->pci_pf.controller) ||
1073 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
1075 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
1078 case DEVLINK_PORT_FLAVOUR_PCI_VF:
1079 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
1080 attrs->pci_vf.controller) ||
1081 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
1082 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
1084 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
1087 case DEVLINK_PORT_FLAVOUR_PCI_SF:
1088 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
1089 attrs->pci_sf.controller) ||
1090 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
1091 attrs->pci_sf.pf) ||
1092 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
1096 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
1097 case DEVLINK_PORT_FLAVOUR_CPU:
1098 case DEVLINK_PORT_FLAVOUR_DSA:
1099 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
1100 attrs->phys.port_number))
1104 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
1105 attrs->phys.port_number))
1107 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
1108 attrs->phys.split_subport_number))
1117 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
1118 struct devlink_port *port,
1119 struct sk_buff *msg,
1120 struct netlink_ext_ack *extack,
1123 u8 hw_addr[MAX_ADDR_LEN];
1127 if (!ops->port_function_hw_addr_get)
1130 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
1133 if (err == -EOPNOTSUPP)
1137 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
1140 *msg_updated = true;
1144 static int devlink_nl_rate_fill(struct sk_buff *msg,
1145 struct devlink_rate *devlink_rate,
1146 enum devlink_command cmd, u32 portid, u32 seq,
1147 int flags, struct netlink_ext_ack *extack)
1149 struct devlink *devlink = devlink_rate->devlink;
1152 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1156 if (devlink_nl_put_handle(msg, devlink))
1157 goto nla_put_failure;
1159 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
1160 goto nla_put_failure;
1162 if (devlink_rate_is_leaf(devlink_rate)) {
1163 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
1164 devlink_rate->devlink_port->index))
1165 goto nla_put_failure;
1166 } else if (devlink_rate_is_node(devlink_rate)) {
1167 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
1168 devlink_rate->name))
1169 goto nla_put_failure;
1172 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
1173 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
1174 goto nla_put_failure;
1176 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
1177 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
1178 goto nla_put_failure;
1180 if (devlink_rate->parent)
1181 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
1182 devlink_rate->parent->name))
1183 goto nla_put_failure;
1185 genlmsg_end(msg, hdr);
1189 genlmsg_cancel(msg, hdr);
1194 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
1196 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
1197 state == DEVLINK_PORT_FN_STATE_ACTIVE;
1201 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
1203 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
1204 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
1207 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
1208 struct devlink_port *port,
1209 struct sk_buff *msg,
1210 struct netlink_ext_ack *extack,
1213 enum devlink_port_fn_opstate opstate;
1214 enum devlink_port_fn_state state;
1217 if (!ops->port_fn_state_get)
1220 err = ops->port_fn_state_get(port, &state, &opstate, extack);
1222 if (err == -EOPNOTSUPP)
1226 if (!devlink_port_fn_state_valid(state)) {
1228 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
1231 if (!devlink_port_fn_opstate_valid(opstate)) {
1233 NL_SET_ERR_MSG_MOD(extack,
1234 "Invalid operational state read from driver");
1237 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
1238 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
1240 *msg_updated = true;
1245 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
1246 struct netlink_ext_ack *extack)
1248 const struct devlink_ops *ops;
1249 struct nlattr *function_attr;
1250 bool msg_updated = false;
1253 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
1257 ops = port->devlink->ops;
1258 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
1262 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
1264 if (err || !msg_updated)
1265 nla_nest_cancel(msg, function_attr);
1267 nla_nest_end(msg, function_attr);
1271 static int devlink_nl_port_fill(struct sk_buff *msg,
1272 struct devlink_port *devlink_port,
1273 enum devlink_command cmd, u32 portid, u32 seq,
1274 int flags, struct netlink_ext_ack *extack)
1276 struct devlink *devlink = devlink_port->devlink;
1279 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1283 if (devlink_nl_put_handle(msg, devlink))
1284 goto nla_put_failure;
1285 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1286 goto nla_put_failure;
1288 /* Hold rtnl lock while accessing port's netdev attributes. */
1290 spin_lock_bh(&devlink_port->type_lock);
1291 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
1292 goto nla_put_failure_type_locked;
1293 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1294 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1295 devlink_port->desired_type))
1296 goto nla_put_failure_type_locked;
1297 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1298 struct net *net = devlink_net(devlink_port->devlink);
1299 struct net_device *netdev = devlink_port->type_dev;
1301 if (netdev && net_eq(net, dev_net(netdev)) &&
1302 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1304 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1306 goto nla_put_failure_type_locked;
1308 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1309 struct ib_device *ibdev = devlink_port->type_dev;
1312 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1314 goto nla_put_failure_type_locked;
1316 spin_unlock_bh(&devlink_port->type_lock);
1318 if (devlink_nl_port_attrs_put(msg, devlink_port))
1319 goto nla_put_failure;
1320 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1321 goto nla_put_failure;
1322 if (devlink_port->linecard &&
1323 nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
1324 devlink_port->linecard->index))
1325 goto nla_put_failure;
1327 genlmsg_end(msg, hdr);
1330 nla_put_failure_type_locked:
1331 spin_unlock_bh(&devlink_port->type_lock);
1334 genlmsg_cancel(msg, hdr);
1338 static void devlink_port_notify(struct devlink_port *devlink_port,
1339 enum devlink_command cmd)
1341 struct devlink *devlink = devlink_port->devlink;
1342 struct sk_buff *msg;
1345 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1347 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1350 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1354 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1360 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1361 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1364 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1365 enum devlink_command cmd)
1367 struct devlink *devlink = devlink_rate->devlink;
1368 struct sk_buff *msg;
1371 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1373 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1376 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1380 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1386 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1387 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1390 static int devlink_nl_cmd_rate_get_dumpit(struct sk_buff *msg,
1391 struct netlink_callback *cb)
1393 struct devlink_rate *devlink_rate;
1394 struct devlink *devlink;
1395 int start = cb->args[0];
1396 unsigned long index;
1400 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1402 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1403 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1404 u32 id = NETLINK_CB(cb->skb).portid;
1410 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1414 devl_unlock(devlink);
1415 devlink_put(devlink);
1420 devl_unlock(devlink);
1421 devlink_put(devlink);
1424 if (err != -EMSGSIZE)
1431 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1432 struct genl_info *info)
1434 struct devlink_rate *devlink_rate = info->user_ptr[1];
1435 struct sk_buff *msg;
1438 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1442 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1443 info->snd_portid, info->snd_seq, 0,
1450 return genlmsg_reply(msg, info);
1454 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1455 struct devlink_rate *parent)
1458 if (parent == devlink_rate)
1460 parent = parent->parent;
1465 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1467 struct devlink *devlink = info->user_ptr[0];
1468 struct sk_buff *msg;
1471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1475 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1476 info->snd_portid, info->snd_seq, 0);
1482 return genlmsg_reply(msg, info);
1485 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
1486 struct netlink_callback *cb)
1488 struct devlink *devlink;
1489 int start = cb->args[0];
1490 unsigned long index;
1494 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1497 devlink_put(devlink);
1501 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1502 NETLINK_CB(cb->skb).portid,
1503 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1504 devlink_put(devlink);
1514 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1515 struct genl_info *info)
1517 struct devlink_port *devlink_port = info->user_ptr[1];
1518 struct sk_buff *msg;
1521 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1525 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1526 info->snd_portid, info->snd_seq, 0,
1533 return genlmsg_reply(msg, info);
1536 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
1537 struct netlink_callback *cb)
1539 struct devlink *devlink;
1540 struct devlink_port *devlink_port;
1541 int start = cb->args[0];
1542 unsigned long index;
1546 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1548 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1553 err = devlink_nl_port_fill(msg, devlink_port,
1555 NETLINK_CB(cb->skb).portid,
1557 NLM_F_MULTI, cb->extack);
1559 devl_unlock(devlink);
1560 devlink_put(devlink);
1565 devl_unlock(devlink);
1566 devlink_put(devlink);
1573 static int devlink_port_type_set(struct devlink_port *devlink_port,
1574 enum devlink_port_type port_type)
1579 if (!devlink_port->devlink->ops->port_type_set)
1582 if (port_type == devlink_port->type)
1585 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1590 devlink_port->desired_type = port_type;
1591 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1595 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1596 const struct nlattr *attr,
1597 struct netlink_ext_ack *extack)
1599 const struct devlink_ops *ops = port->devlink->ops;
1603 hw_addr = nla_data(attr);
1604 hw_addr_len = nla_len(attr);
1605 if (hw_addr_len > MAX_ADDR_LEN) {
1606 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1609 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1610 if (hw_addr_len != ETH_ALEN) {
1611 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1614 if (!is_unicast_ether_addr(hw_addr)) {
1615 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1620 if (!ops->port_function_hw_addr_set) {
1621 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
1625 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1629 static int devlink_port_fn_state_set(struct devlink_port *port,
1630 const struct nlattr *attr,
1631 struct netlink_ext_ack *extack)
1633 enum devlink_port_fn_state state;
1634 const struct devlink_ops *ops;
1636 state = nla_get_u8(attr);
1637 ops = port->devlink->ops;
1638 if (!ops->port_fn_state_set) {
1639 NL_SET_ERR_MSG_MOD(extack,
1640 "Function does not support state setting");
1643 return ops->port_fn_state_set(port, state, extack);
1646 static int devlink_port_function_set(struct devlink_port *port,
1647 const struct nlattr *attr,
1648 struct netlink_ext_ack *extack)
1650 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1653 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1654 devlink_function_nl_policy, extack);
1656 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1660 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1662 err = devlink_port_function_hw_addr_set(port, attr, extack);
1666 /* Keep this as the last function attribute set, so that when
1667 * multiple port function attributes are set along with state,
1668 * Those can be applied first before activating the state.
1670 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1672 err = devlink_port_fn_state_set(port, attr, extack);
1675 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1679 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1680 struct genl_info *info)
1682 struct devlink_port *devlink_port = info->user_ptr[1];
1685 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1686 enum devlink_port_type port_type;
1688 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1689 err = devlink_port_type_set(devlink_port, port_type);
1694 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1695 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1696 struct netlink_ext_ack *extack = info->extack;
1698 err = devlink_port_function_set(devlink_port, attr, extack);
1706 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1707 struct genl_info *info)
1709 struct devlink_port *devlink_port = info->user_ptr[1];
1710 struct devlink *devlink = info->user_ptr[0];
1713 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
1715 if (!devlink->ops->port_split)
1718 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1720 if (!devlink_port->attrs.splittable) {
1721 /* Split ports cannot be split. */
1722 if (devlink_port->attrs.split)
1723 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1725 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1729 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1730 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1734 return devlink->ops->port_split(devlink, devlink_port, count,
1738 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1739 struct genl_info *info)
1741 struct devlink_port *devlink_port = info->user_ptr[1];
1742 struct devlink *devlink = info->user_ptr[0];
1744 if (!devlink->ops->port_unsplit)
1746 return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1749 static int devlink_port_new_notify(struct devlink *devlink,
1750 unsigned int port_index,
1751 struct genl_info *info)
1753 struct devlink_port *devlink_port;
1754 struct sk_buff *msg;
1757 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1761 lockdep_assert_held(&devlink->lock);
1762 devlink_port = devlink_port_get_by_index(devlink, port_index);
1763 if (!devlink_port) {
1768 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1769 info->snd_portid, info->snd_seq, 0, NULL);
1773 return genlmsg_reply(msg, info);
1780 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1781 struct genl_info *info)
1783 struct netlink_ext_ack *extack = info->extack;
1784 struct devlink_port_new_attrs new_attrs = {};
1785 struct devlink *devlink = info->user_ptr[0];
1786 unsigned int new_port_index;
1789 if (!devlink->ops->port_new || !devlink->ops->port_del)
1792 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1793 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1794 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1797 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1799 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1801 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1802 /* Port index of the new port being created by driver. */
1803 new_attrs.port_index =
1804 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1805 new_attrs.port_index_valid = true;
1807 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1808 new_attrs.controller =
1809 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1810 new_attrs.controller_valid = true;
1812 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1813 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1814 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1815 new_attrs.sfnum_valid = true;
1818 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1823 err = devlink_port_new_notify(devlink, new_port_index, info);
1824 if (err && err != -ENODEV) {
1825 /* Fail to send the response; destroy newly created port. */
1826 devlink->ops->port_del(devlink, new_port_index, extack);
1831 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1832 struct genl_info *info)
1834 struct netlink_ext_ack *extack = info->extack;
1835 struct devlink *devlink = info->user_ptr[0];
1836 unsigned int port_index;
1838 if (!devlink->ops->port_del)
1841 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_INDEX)) {
1842 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1845 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1847 return devlink->ops->port_del(devlink, port_index, extack);
1851 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1852 struct genl_info *info,
1853 struct nlattr *nla_parent)
1855 struct devlink *devlink = devlink_rate->devlink;
1856 const char *parent_name = nla_data(nla_parent);
1857 const struct devlink_ops *ops = devlink->ops;
1858 size_t len = strlen(parent_name);
1859 struct devlink_rate *parent;
1860 int err = -EOPNOTSUPP;
1862 parent = devlink_rate->parent;
1863 if (parent && len) {
1864 NL_SET_ERR_MSG_MOD(info->extack, "Rate object already has parent.");
1866 } else if (parent && !len) {
1867 if (devlink_rate_is_leaf(devlink_rate))
1868 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1869 devlink_rate->priv, NULL,
1871 else if (devlink_rate_is_node(devlink_rate))
1872 err = ops->rate_node_parent_set(devlink_rate, NULL,
1873 devlink_rate->priv, NULL,
1878 refcount_dec(&parent->refcnt);
1879 devlink_rate->parent = NULL;
1880 } else if (!parent && len) {
1881 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1885 if (parent == devlink_rate) {
1886 NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1890 if (devlink_rate_is_node(devlink_rate) &&
1891 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1892 NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1896 if (devlink_rate_is_leaf(devlink_rate))
1897 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1898 devlink_rate->priv, parent->priv,
1900 else if (devlink_rate_is_node(devlink_rate))
1901 err = ops->rate_node_parent_set(devlink_rate, parent,
1902 devlink_rate->priv, parent->priv,
1907 refcount_inc(&parent->refcnt);
1908 devlink_rate->parent = parent;
1914 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1915 const struct devlink_ops *ops,
1916 struct genl_info *info)
1918 struct nlattr *nla_parent, **attrs = info->attrs;
1919 int err = -EOPNOTSUPP;
1922 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1923 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1924 if (devlink_rate_is_leaf(devlink_rate))
1925 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1926 rate, info->extack);
1927 else if (devlink_rate_is_node(devlink_rate))
1928 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1929 rate, info->extack);
1932 devlink_rate->tx_share = rate;
1935 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1936 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1937 if (devlink_rate_is_leaf(devlink_rate))
1938 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1939 rate, info->extack);
1940 else if (devlink_rate_is_node(devlink_rate))
1941 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1942 rate, info->extack);
1945 devlink_rate->tx_max = rate;
1948 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1950 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1959 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1960 struct genl_info *info,
1961 enum devlink_rate_type type)
1963 struct nlattr **attrs = info->attrs;
1965 if (type == DEVLINK_RATE_TYPE_LEAF) {
1966 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1967 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1970 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1971 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1974 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1975 !ops->rate_leaf_parent_set) {
1976 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1979 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1980 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1981 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1984 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1985 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1988 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1989 !ops->rate_node_parent_set) {
1990 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1994 WARN(1, "Unknown type of rate object");
2001 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
2002 struct genl_info *info)
2004 struct devlink_rate *devlink_rate = info->user_ptr[1];
2005 struct devlink *devlink = devlink_rate->devlink;
2006 const struct devlink_ops *ops = devlink->ops;
2009 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
2012 err = devlink_nl_rate_set(devlink_rate, ops, info);
2015 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
2019 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
2020 struct genl_info *info)
2022 struct devlink *devlink = info->user_ptr[0];
2023 struct devlink_rate *rate_node;
2024 const struct devlink_ops *ops;
2028 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
2029 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
2033 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
2036 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
2037 if (!IS_ERR(rate_node))
2039 else if (rate_node == ERR_PTR(-EINVAL))
2042 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
2046 rate_node->devlink = devlink;
2047 rate_node->type = DEVLINK_RATE_TYPE_NODE;
2048 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
2049 if (!rate_node->name) {
2054 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
2058 err = devlink_nl_rate_set(rate_node, ops, info);
2062 refcount_set(&rate_node->refcnt, 1);
2063 list_add(&rate_node->list, &devlink->rate_list);
2064 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
2068 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2070 kfree(rate_node->name);
2076 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
2077 struct genl_info *info)
2079 struct devlink_rate *rate_node = info->user_ptr[1];
2080 struct devlink *devlink = rate_node->devlink;
2081 const struct devlink_ops *ops = devlink->ops;
2084 if (refcount_read(&rate_node->refcnt) > 1) {
2085 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
2089 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
2090 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2091 if (rate_node->parent)
2092 refcount_dec(&rate_node->parent->refcnt);
2093 list_del(&rate_node->list);
2094 kfree(rate_node->name);
2099 struct devlink_linecard_type {
2104 static int devlink_nl_linecard_fill(struct sk_buff *msg,
2105 struct devlink *devlink,
2106 struct devlink_linecard *linecard,
2107 enum devlink_command cmd, u32 portid,
2109 struct netlink_ext_ack *extack)
2111 struct devlink_linecard_type *linecard_type;
2112 struct nlattr *attr;
2116 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2120 if (devlink_nl_put_handle(msg, devlink))
2121 goto nla_put_failure;
2122 if (nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, linecard->index))
2123 goto nla_put_failure;
2124 if (nla_put_u8(msg, DEVLINK_ATTR_LINECARD_STATE, linecard->state))
2125 goto nla_put_failure;
2126 if (linecard->type &&
2127 nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE, linecard->type))
2128 goto nla_put_failure;
2130 if (linecard->types_count) {
2131 attr = nla_nest_start(msg,
2132 DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES);
2134 goto nla_put_failure;
2135 for (i = 0; i < linecard->types_count; i++) {
2136 linecard_type = &linecard->types[i];
2137 if (nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE,
2138 linecard_type->type)) {
2139 nla_nest_cancel(msg, attr);
2140 goto nla_put_failure;
2143 nla_nest_end(msg, attr);
2146 if (linecard->nested_devlink &&
2147 devlink_nl_put_nested_handle(msg, linecard->nested_devlink))
2148 goto nla_put_failure;
2150 genlmsg_end(msg, hdr);
2154 genlmsg_cancel(msg, hdr);
2158 static void devlink_linecard_notify(struct devlink_linecard *linecard,
2159 enum devlink_command cmd)
2161 struct devlink *devlink = linecard->devlink;
2162 struct sk_buff *msg;
2165 WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW &&
2166 cmd != DEVLINK_CMD_LINECARD_DEL);
2168 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
2171 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2175 err = devlink_nl_linecard_fill(msg, devlink, linecard, cmd, 0, 0, 0,
2182 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2183 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2186 static int devlink_nl_cmd_linecard_get_doit(struct sk_buff *skb,
2187 struct genl_info *info)
2189 struct devlink_linecard *linecard = info->user_ptr[1];
2190 struct devlink *devlink = linecard->devlink;
2191 struct sk_buff *msg;
2194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2198 mutex_lock(&linecard->state_lock);
2199 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2200 DEVLINK_CMD_LINECARD_NEW,
2201 info->snd_portid, info->snd_seq, 0,
2203 mutex_unlock(&linecard->state_lock);
2209 return genlmsg_reply(msg, info);
2212 static int devlink_nl_cmd_linecard_get_dumpit(struct sk_buff *msg,
2213 struct netlink_callback *cb)
2215 struct devlink_linecard *linecard;
2216 struct devlink *devlink;
2217 int start = cb->args[0];
2218 unsigned long index;
2222 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2223 mutex_lock(&devlink->linecards_lock);
2224 list_for_each_entry(linecard, &devlink->linecard_list, list) {
2229 mutex_lock(&linecard->state_lock);
2230 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2231 DEVLINK_CMD_LINECARD_NEW,
2232 NETLINK_CB(cb->skb).portid,
2236 mutex_unlock(&linecard->state_lock);
2238 mutex_unlock(&devlink->linecards_lock);
2239 devlink_put(devlink);
2244 mutex_unlock(&devlink->linecards_lock);
2245 devlink_put(devlink);
2252 static struct devlink_linecard_type *
2253 devlink_linecard_type_lookup(struct devlink_linecard *linecard,
2256 struct devlink_linecard_type *linecard_type;
2259 for (i = 0; i < linecard->types_count; i++) {
2260 linecard_type = &linecard->types[i];
2261 if (!strcmp(type, linecard_type->type))
2262 return linecard_type;
2267 static int devlink_linecard_type_set(struct devlink_linecard *linecard,
2269 struct netlink_ext_ack *extack)
2271 const struct devlink_linecard_ops *ops = linecard->ops;
2272 struct devlink_linecard_type *linecard_type;
2275 mutex_lock(&linecard->state_lock);
2276 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2277 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2281 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2282 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2287 linecard_type = devlink_linecard_type_lookup(linecard, type);
2288 if (!linecard_type) {
2289 NL_SET_ERR_MSG_MOD(extack, "Unsupported line card type provided");
2294 if (linecard->state != DEVLINK_LINECARD_STATE_UNPROVISIONED &&
2295 linecard->state != DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2296 NL_SET_ERR_MSG_MOD(extack, "Line card already provisioned");
2298 /* Check if the line card is provisioned in the same
2299 * way the user asks. In case it is, make the operation
2300 * to return success.
2302 if (ops->same_provision &&
2303 ops->same_provision(linecard, linecard->priv,
2304 linecard_type->type,
2305 linecard_type->priv))
2310 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING;
2311 linecard->type = linecard_type->type;
2312 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2313 mutex_unlock(&linecard->state_lock);
2314 err = ops->provision(linecard, linecard->priv, linecard_type->type,
2315 linecard_type->priv, extack);
2317 /* Provisioning failed. Assume the linecard is unprovisioned
2318 * for future operations.
2320 mutex_lock(&linecard->state_lock);
2321 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2322 linecard->type = NULL;
2323 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2324 mutex_unlock(&linecard->state_lock);
2329 mutex_unlock(&linecard->state_lock);
2333 static int devlink_linecard_type_unset(struct devlink_linecard *linecard,
2334 struct netlink_ext_ack *extack)
2338 mutex_lock(&linecard->state_lock);
2339 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2340 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2344 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2345 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2349 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2350 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2351 linecard->type = NULL;
2352 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2357 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONED) {
2358 NL_SET_ERR_MSG_MOD(extack, "Line card is not provisioned");
2362 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONING;
2363 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2364 mutex_unlock(&linecard->state_lock);
2365 err = linecard->ops->unprovision(linecard, linecard->priv,
2368 /* Unprovisioning failed. Assume the linecard is unprovisioned
2369 * for future operations.
2371 mutex_lock(&linecard->state_lock);
2372 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2373 linecard->type = NULL;
2374 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2375 mutex_unlock(&linecard->state_lock);
2380 mutex_unlock(&linecard->state_lock);
2384 static int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
2385 struct genl_info *info)
2387 struct devlink_linecard *linecard = info->user_ptr[1];
2388 struct netlink_ext_ack *extack = info->extack;
2391 if (info->attrs[DEVLINK_ATTR_LINECARD_TYPE]) {
2394 type = nla_data(info->attrs[DEVLINK_ATTR_LINECARD_TYPE]);
2395 if (strcmp(type, "")) {
2396 err = devlink_linecard_type_set(linecard, type, extack);
2400 err = devlink_linecard_type_unset(linecard, extack);
2409 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
2410 struct devlink_sb *devlink_sb,
2411 enum devlink_command cmd, u32 portid,
2416 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2420 if (devlink_nl_put_handle(msg, devlink))
2421 goto nla_put_failure;
2422 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2423 goto nla_put_failure;
2424 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
2425 goto nla_put_failure;
2426 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
2427 devlink_sb->ingress_pools_count))
2428 goto nla_put_failure;
2429 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
2430 devlink_sb->egress_pools_count))
2431 goto nla_put_failure;
2432 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
2433 devlink_sb->ingress_tc_count))
2434 goto nla_put_failure;
2435 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
2436 devlink_sb->egress_tc_count))
2437 goto nla_put_failure;
2439 genlmsg_end(msg, hdr);
2443 genlmsg_cancel(msg, hdr);
2447 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2448 struct genl_info *info)
2450 struct devlink *devlink = info->user_ptr[0];
2451 struct devlink_sb *devlink_sb;
2452 struct sk_buff *msg;
2455 devlink_sb = devlink_sb_get_from_info(devlink, info);
2456 if (IS_ERR(devlink_sb))
2457 return PTR_ERR(devlink_sb);
2459 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2463 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2465 info->snd_portid, info->snd_seq, 0);
2471 return genlmsg_reply(msg, info);
2474 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
2475 struct netlink_callback *cb)
2477 struct devlink *devlink;
2478 struct devlink_sb *devlink_sb;
2479 int start = cb->args[0];
2480 unsigned long index;
2484 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2486 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2491 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2493 NETLINK_CB(cb->skb).portid,
2497 devl_unlock(devlink);
2498 devlink_put(devlink);
2503 devl_unlock(devlink);
2504 devlink_put(devlink);
2511 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2512 struct devlink_sb *devlink_sb,
2513 u16 pool_index, enum devlink_command cmd,
2514 u32 portid, u32 seq, int flags)
2516 struct devlink_sb_pool_info pool_info;
2520 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2521 pool_index, &pool_info);
2525 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2529 if (devlink_nl_put_handle(msg, devlink))
2530 goto nla_put_failure;
2531 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2532 goto nla_put_failure;
2533 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2534 goto nla_put_failure;
2535 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2536 goto nla_put_failure;
2537 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2538 goto nla_put_failure;
2539 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2540 pool_info.threshold_type))
2541 goto nla_put_failure;
2542 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2543 pool_info.cell_size))
2544 goto nla_put_failure;
2546 genlmsg_end(msg, hdr);
2550 genlmsg_cancel(msg, hdr);
2554 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2555 struct genl_info *info)
2557 struct devlink *devlink = info->user_ptr[0];
2558 struct devlink_sb *devlink_sb;
2559 struct sk_buff *msg;
2563 devlink_sb = devlink_sb_get_from_info(devlink, info);
2564 if (IS_ERR(devlink_sb))
2565 return PTR_ERR(devlink_sb);
2567 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2572 if (!devlink->ops->sb_pool_get)
2575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2579 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2580 DEVLINK_CMD_SB_POOL_NEW,
2581 info->snd_portid, info->snd_seq, 0);
2587 return genlmsg_reply(msg, info);
2590 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2591 struct devlink *devlink,
2592 struct devlink_sb *devlink_sb,
2593 u32 portid, u32 seq)
2595 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2599 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2600 if (*p_idx < start) {
2604 err = devlink_nl_sb_pool_fill(msg, devlink,
2607 DEVLINK_CMD_SB_POOL_NEW,
2608 portid, seq, NLM_F_MULTI);
2616 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
2617 struct netlink_callback *cb)
2619 struct devlink *devlink;
2620 struct devlink_sb *devlink_sb;
2621 int start = cb->args[0];
2622 unsigned long index;
2626 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2627 if (!devlink->ops->sb_pool_get)
2631 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2632 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
2634 NETLINK_CB(cb->skb).portid,
2635 cb->nlh->nlmsg_seq);
2636 if (err == -EOPNOTSUPP) {
2639 devl_unlock(devlink);
2640 devlink_put(devlink);
2644 devl_unlock(devlink);
2646 devlink_put(devlink);
2649 if (err != -EMSGSIZE)
2656 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2657 u16 pool_index, u32 size,
2658 enum devlink_sb_threshold_type threshold_type,
2659 struct netlink_ext_ack *extack)
2662 const struct devlink_ops *ops = devlink->ops;
2664 if (ops->sb_pool_set)
2665 return ops->sb_pool_set(devlink, sb_index, pool_index,
2666 size, threshold_type, extack);
2670 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2671 struct genl_info *info)
2673 struct devlink *devlink = info->user_ptr[0];
2674 enum devlink_sb_threshold_type threshold_type;
2675 struct devlink_sb *devlink_sb;
2680 devlink_sb = devlink_sb_get_from_info(devlink, info);
2681 if (IS_ERR(devlink_sb))
2682 return PTR_ERR(devlink_sb);
2684 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2689 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2693 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_POOL_SIZE))
2696 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2697 return devlink_sb_pool_set(devlink, devlink_sb->index,
2698 pool_index, size, threshold_type,
2702 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2703 struct devlink *devlink,
2704 struct devlink_port *devlink_port,
2705 struct devlink_sb *devlink_sb,
2707 enum devlink_command cmd,
2708 u32 portid, u32 seq, int flags)
2710 const struct devlink_ops *ops = devlink->ops;
2715 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2716 pool_index, &threshold);
2720 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2724 if (devlink_nl_put_handle(msg, devlink))
2725 goto nla_put_failure;
2726 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2727 goto nla_put_failure;
2728 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2729 goto nla_put_failure;
2730 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2731 goto nla_put_failure;
2732 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2733 goto nla_put_failure;
2735 if (ops->sb_occ_port_pool_get) {
2739 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2740 pool_index, &cur, &max);
2741 if (err && err != -EOPNOTSUPP)
2742 goto sb_occ_get_failure;
2744 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2745 goto nla_put_failure;
2746 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2747 goto nla_put_failure;
2751 genlmsg_end(msg, hdr);
2757 genlmsg_cancel(msg, hdr);
2761 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2762 struct genl_info *info)
2764 struct devlink_port *devlink_port = info->user_ptr[1];
2765 struct devlink *devlink = devlink_port->devlink;
2766 struct devlink_sb *devlink_sb;
2767 struct sk_buff *msg;
2771 devlink_sb = devlink_sb_get_from_info(devlink, info);
2772 if (IS_ERR(devlink_sb))
2773 return PTR_ERR(devlink_sb);
2775 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2780 if (!devlink->ops->sb_port_pool_get)
2783 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2787 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2788 devlink_sb, pool_index,
2789 DEVLINK_CMD_SB_PORT_POOL_NEW,
2790 info->snd_portid, info->snd_seq, 0);
2796 return genlmsg_reply(msg, info);
2799 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2800 struct devlink *devlink,
2801 struct devlink_sb *devlink_sb,
2802 u32 portid, u32 seq)
2804 struct devlink_port *devlink_port;
2805 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2809 list_for_each_entry(devlink_port, &devlink->port_list, list) {
2810 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2811 if (*p_idx < start) {
2815 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2819 DEVLINK_CMD_SB_PORT_POOL_NEW,
2830 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
2831 struct netlink_callback *cb)
2833 struct devlink *devlink;
2834 struct devlink_sb *devlink_sb;
2835 int start = cb->args[0];
2836 unsigned long index;
2840 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2841 if (!devlink->ops->sb_port_pool_get)
2845 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2846 err = __sb_port_pool_get_dumpit(msg, start, &idx,
2847 devlink, devlink_sb,
2848 NETLINK_CB(cb->skb).portid,
2849 cb->nlh->nlmsg_seq);
2850 if (err == -EOPNOTSUPP) {
2853 devl_unlock(devlink);
2854 devlink_put(devlink);
2858 devl_unlock(devlink);
2860 devlink_put(devlink);
2863 if (err != -EMSGSIZE)
2870 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2871 unsigned int sb_index, u16 pool_index,
2873 struct netlink_ext_ack *extack)
2876 const struct devlink_ops *ops = devlink_port->devlink->ops;
2878 if (ops->sb_port_pool_set)
2879 return ops->sb_port_pool_set(devlink_port, sb_index,
2880 pool_index, threshold, extack);
2884 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2885 struct genl_info *info)
2887 struct devlink_port *devlink_port = info->user_ptr[1];
2888 struct devlink *devlink = info->user_ptr[0];
2889 struct devlink_sb *devlink_sb;
2894 devlink_sb = devlink_sb_get_from_info(devlink, info);
2895 if (IS_ERR(devlink_sb))
2896 return PTR_ERR(devlink_sb);
2898 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2903 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
2906 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2907 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2908 pool_index, threshold, info->extack);
2912 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2913 struct devlink_port *devlink_port,
2914 struct devlink_sb *devlink_sb, u16 tc_index,
2915 enum devlink_sb_pool_type pool_type,
2916 enum devlink_command cmd,
2917 u32 portid, u32 seq, int flags)
2919 const struct devlink_ops *ops = devlink->ops;
2925 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2926 tc_index, pool_type,
2927 &pool_index, &threshold);
2931 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2935 if (devlink_nl_put_handle(msg, devlink))
2936 goto nla_put_failure;
2937 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2938 goto nla_put_failure;
2939 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2940 goto nla_put_failure;
2941 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2942 goto nla_put_failure;
2943 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2944 goto nla_put_failure;
2945 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2946 goto nla_put_failure;
2947 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2948 goto nla_put_failure;
2950 if (ops->sb_occ_tc_port_bind_get) {
2954 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2956 tc_index, pool_type,
2958 if (err && err != -EOPNOTSUPP)
2961 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2962 goto nla_put_failure;
2963 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2964 goto nla_put_failure;
2968 genlmsg_end(msg, hdr);
2972 genlmsg_cancel(msg, hdr);
2976 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2977 struct genl_info *info)
2979 struct devlink_port *devlink_port = info->user_ptr[1];
2980 struct devlink *devlink = devlink_port->devlink;
2981 struct devlink_sb *devlink_sb;
2982 struct sk_buff *msg;
2983 enum devlink_sb_pool_type pool_type;
2987 devlink_sb = devlink_sb_get_from_info(devlink, info);
2988 if (IS_ERR(devlink_sb))
2989 return PTR_ERR(devlink_sb);
2991 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2995 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2996 pool_type, &tc_index);
3000 if (!devlink->ops->sb_tc_pool_bind_get)
3003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3007 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
3008 devlink_sb, tc_index, pool_type,
3009 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3017 return genlmsg_reply(msg, info);
3020 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
3021 int start, int *p_idx,
3022 struct devlink *devlink,
3023 struct devlink_sb *devlink_sb,
3024 u32 portid, u32 seq)
3026 struct devlink_port *devlink_port;
3030 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3032 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
3033 if (*p_idx < start) {
3037 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
3041 DEVLINK_SB_POOL_TYPE_INGRESS,
3042 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3050 tc_index < devlink_sb->egress_tc_count; tc_index++) {
3051 if (*p_idx < start) {
3055 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
3059 DEVLINK_SB_POOL_TYPE_EGRESS,
3060 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3072 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
3073 struct netlink_callback *cb)
3075 struct devlink *devlink;
3076 struct devlink_sb *devlink_sb;
3077 int start = cb->args[0];
3078 unsigned long index;
3082 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
3083 if (!devlink->ops->sb_tc_pool_bind_get)
3087 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
3088 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
3091 NETLINK_CB(cb->skb).portid,
3092 cb->nlh->nlmsg_seq);
3093 if (err == -EOPNOTSUPP) {
3096 devl_unlock(devlink);
3097 devlink_put(devlink);
3101 devl_unlock(devlink);
3103 devlink_put(devlink);
3106 if (err != -EMSGSIZE)
3113 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
3114 unsigned int sb_index, u16 tc_index,
3115 enum devlink_sb_pool_type pool_type,
3116 u16 pool_index, u32 threshold,
3117 struct netlink_ext_ack *extack)
3120 const struct devlink_ops *ops = devlink_port->devlink->ops;
3122 if (ops->sb_tc_pool_bind_set)
3123 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
3124 tc_index, pool_type,
3125 pool_index, threshold, extack);
3129 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
3130 struct genl_info *info)
3132 struct devlink_port *devlink_port = info->user_ptr[1];
3133 struct devlink *devlink = info->user_ptr[0];
3134 enum devlink_sb_pool_type pool_type;
3135 struct devlink_sb *devlink_sb;
3141 devlink_sb = devlink_sb_get_from_info(devlink, info);
3142 if (IS_ERR(devlink_sb))
3143 return PTR_ERR(devlink_sb);
3145 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
3149 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
3150 pool_type, &tc_index);
3154 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
3159 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
3162 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
3163 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
3164 tc_index, pool_type,
3165 pool_index, threshold, info->extack);
3168 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
3169 struct genl_info *info)
3171 struct devlink *devlink = info->user_ptr[0];
3172 const struct devlink_ops *ops = devlink->ops;
3173 struct devlink_sb *devlink_sb;
3175 devlink_sb = devlink_sb_get_from_info(devlink, info);
3176 if (IS_ERR(devlink_sb))
3177 return PTR_ERR(devlink_sb);
3179 if (ops->sb_occ_snapshot)
3180 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
3184 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
3185 struct genl_info *info)
3187 struct devlink *devlink = info->user_ptr[0];
3188 const struct devlink_ops *ops = devlink->ops;
3189 struct devlink_sb *devlink_sb;
3191 devlink_sb = devlink_sb_get_from_info(devlink, info);
3192 if (IS_ERR(devlink_sb))
3193 return PTR_ERR(devlink_sb);
3195 if (ops->sb_occ_max_clear)
3196 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
3200 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
3201 enum devlink_command cmd, u32 portid,
3204 const struct devlink_ops *ops = devlink->ops;
3205 enum devlink_eswitch_encap_mode encap_mode;
3211 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3215 err = devlink_nl_put_handle(msg, devlink);
3217 goto nla_put_failure;
3219 if (ops->eswitch_mode_get) {
3220 err = ops->eswitch_mode_get(devlink, &mode);
3222 goto nla_put_failure;
3223 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
3225 goto nla_put_failure;
3228 if (ops->eswitch_inline_mode_get) {
3229 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
3231 goto nla_put_failure;
3232 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
3235 goto nla_put_failure;
3238 if (ops->eswitch_encap_mode_get) {
3239 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
3241 goto nla_put_failure;
3242 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
3244 goto nla_put_failure;
3247 genlmsg_end(msg, hdr);
3251 genlmsg_cancel(msg, hdr);
3255 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
3256 struct genl_info *info)
3258 struct devlink *devlink = info->user_ptr[0];
3259 struct sk_buff *msg;
3262 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3266 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
3267 info->snd_portid, info->snd_seq, 0);
3274 return genlmsg_reply(msg, info);
3277 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
3278 struct netlink_ext_ack *extack)
3280 struct devlink_rate *devlink_rate;
3282 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
3283 if (devlink_rate_is_node(devlink_rate)) {
3284 NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
3290 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
3291 struct genl_info *info)
3293 struct devlink *devlink = info->user_ptr[0];
3294 const struct devlink_ops *ops = devlink->ops;
3295 enum devlink_eswitch_encap_mode encap_mode;
3300 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
3301 if (!ops->eswitch_mode_set)
3303 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
3304 err = devlink_rate_nodes_check(devlink, mode, info->extack);
3307 err = ops->eswitch_mode_set(devlink, mode, info->extack);
3312 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
3313 if (!ops->eswitch_inline_mode_set)
3315 inline_mode = nla_get_u8(
3316 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
3317 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
3323 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
3324 if (!ops->eswitch_encap_mode_set)
3326 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
3327 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
3336 int devlink_dpipe_match_put(struct sk_buff *skb,
3337 struct devlink_dpipe_match *match)
3339 struct devlink_dpipe_header *header = match->header;
3340 struct devlink_dpipe_field *field = &header->fields[match->field_id];
3341 struct nlattr *match_attr;
3343 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
3347 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
3348 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
3349 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3350 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3351 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3352 goto nla_put_failure;
3354 nla_nest_end(skb, match_attr);
3358 nla_nest_cancel(skb, match_attr);
3361 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
3363 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
3364 struct sk_buff *skb)
3366 struct nlattr *matches_attr;
3368 matches_attr = nla_nest_start_noflag(skb,
3369 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
3373 if (table->table_ops->matches_dump(table->priv, skb))
3374 goto nla_put_failure;
3376 nla_nest_end(skb, matches_attr);
3380 nla_nest_cancel(skb, matches_attr);
3384 int devlink_dpipe_action_put(struct sk_buff *skb,
3385 struct devlink_dpipe_action *action)
3387 struct devlink_dpipe_header *header = action->header;
3388 struct devlink_dpipe_field *field = &header->fields[action->field_id];
3389 struct nlattr *action_attr;
3391 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
3395 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
3396 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
3397 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3398 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3399 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3400 goto nla_put_failure;
3402 nla_nest_end(skb, action_attr);
3406 nla_nest_cancel(skb, action_attr);
3409 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
3411 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
3412 struct sk_buff *skb)
3414 struct nlattr *actions_attr;
3416 actions_attr = nla_nest_start_noflag(skb,
3417 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
3421 if (table->table_ops->actions_dump(table->priv, skb))
3422 goto nla_put_failure;
3424 nla_nest_end(skb, actions_attr);
3428 nla_nest_cancel(skb, actions_attr);
3432 static int devlink_dpipe_table_put(struct sk_buff *skb,
3433 struct devlink_dpipe_table *table)
3435 struct nlattr *table_attr;
3438 table_size = table->table_ops->size_get(table->priv);
3439 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
3443 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
3444 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
3446 goto nla_put_failure;
3447 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
3448 table->counters_enabled))
3449 goto nla_put_failure;
3451 if (table->resource_valid) {
3452 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
3453 table->resource_id, DEVLINK_ATTR_PAD) ||
3454 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
3455 table->resource_units, DEVLINK_ATTR_PAD))
3456 goto nla_put_failure;
3458 if (devlink_dpipe_matches_put(table, skb))
3459 goto nla_put_failure;
3461 if (devlink_dpipe_actions_put(table, skb))
3462 goto nla_put_failure;
3464 nla_nest_end(skb, table_attr);
3468 nla_nest_cancel(skb, table_attr);
3472 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
3473 struct genl_info *info)
3478 err = genlmsg_reply(*pskb, info);
3482 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3488 static int devlink_dpipe_tables_fill(struct genl_info *info,
3489 enum devlink_command cmd, int flags,
3490 struct list_head *dpipe_tables,
3491 const char *table_name)
3493 struct devlink *devlink = info->user_ptr[0];
3494 struct devlink_dpipe_table *table;
3495 struct nlattr *tables_attr;
3496 struct sk_buff *skb = NULL;
3497 struct nlmsghdr *nlh;
3503 table = list_first_entry(dpipe_tables,
3504 struct devlink_dpipe_table, list);
3506 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3510 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3511 &devlink_nl_family, NLM_F_MULTI, cmd);
3517 if (devlink_nl_put_handle(skb, devlink))
3518 goto nla_put_failure;
3519 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3521 goto nla_put_failure;
3525 list_for_each_entry_from(table, dpipe_tables, list) {
3527 err = devlink_dpipe_table_put(skb, table);
3535 if (!strcmp(table->name, table_name)) {
3536 err = devlink_dpipe_table_put(skb, table);
3544 nla_nest_end(skb, tables_attr);
3545 genlmsg_end(skb, hdr);
3550 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3551 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3553 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3559 return genlmsg_reply(skb, info);
3568 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3569 struct genl_info *info)
3571 struct devlink *devlink = info->user_ptr[0];
3572 const char *table_name = NULL;
3574 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3575 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3577 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3578 &devlink->dpipe_table_list,
3582 static int devlink_dpipe_value_put(struct sk_buff *skb,
3583 struct devlink_dpipe_value *value)
3585 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3586 value->value_size, value->value))
3589 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3590 value->value_size, value->mask))
3592 if (value->mapping_valid)
3593 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3594 value->mapping_value))
3599 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3600 struct devlink_dpipe_value *value)
3604 if (devlink_dpipe_action_put(skb, value->action))
3606 if (devlink_dpipe_value_put(skb, value))
3611 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3612 struct devlink_dpipe_value *values,
3613 unsigned int values_count)
3615 struct nlattr *action_attr;
3619 for (i = 0; i < values_count; i++) {
3620 action_attr = nla_nest_start_noflag(skb,
3621 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3624 err = devlink_dpipe_action_value_put(skb, &values[i]);
3626 goto err_action_value_put;
3627 nla_nest_end(skb, action_attr);
3631 err_action_value_put:
3632 nla_nest_cancel(skb, action_attr);
3636 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3637 struct devlink_dpipe_value *value)
3641 if (devlink_dpipe_match_put(skb, value->match))
3643 if (devlink_dpipe_value_put(skb, value))
3648 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3649 struct devlink_dpipe_value *values,
3650 unsigned int values_count)
3652 struct nlattr *match_attr;
3656 for (i = 0; i < values_count; i++) {
3657 match_attr = nla_nest_start_noflag(skb,
3658 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3661 err = devlink_dpipe_match_value_put(skb, &values[i]);
3663 goto err_match_value_put;
3664 nla_nest_end(skb, match_attr);
3668 err_match_value_put:
3669 nla_nest_cancel(skb, match_attr);
3673 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3674 struct devlink_dpipe_entry *entry)
3676 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3679 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3683 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3685 goto nla_put_failure;
3686 if (entry->counter_valid)
3687 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3688 entry->counter, DEVLINK_ATTR_PAD))
3689 goto nla_put_failure;
3691 matches_attr = nla_nest_start_noflag(skb,
3692 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3694 goto nla_put_failure;
3696 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3697 entry->match_values_count);
3699 nla_nest_cancel(skb, matches_attr);
3700 goto err_match_values_put;
3702 nla_nest_end(skb, matches_attr);
3704 actions_attr = nla_nest_start_noflag(skb,
3705 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3707 goto nla_put_failure;
3709 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3710 entry->action_values_count);
3712 nla_nest_cancel(skb, actions_attr);
3713 goto err_action_values_put;
3715 nla_nest_end(skb, actions_attr);
3717 nla_nest_end(skb, entry_attr);
3722 err_match_values_put:
3723 err_action_values_put:
3724 nla_nest_cancel(skb, entry_attr);
3728 static struct devlink_dpipe_table *
3729 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3730 const char *table_name, struct devlink *devlink)
3732 struct devlink_dpipe_table *table;
3733 list_for_each_entry_rcu(table, dpipe_tables, list,
3734 lockdep_is_held(&devlink->lock)) {
3735 if (!strcmp(table->name, table_name))
3741 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3743 struct devlink *devlink;
3746 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3751 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3752 dump_ctx->info->snd_portid,
3753 dump_ctx->info->snd_seq,
3754 &devlink_nl_family, NLM_F_MULTI,
3757 goto nla_put_failure;
3759 devlink = dump_ctx->info->user_ptr[0];
3760 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3761 goto nla_put_failure;
3762 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3763 DEVLINK_ATTR_DPIPE_ENTRIES);
3764 if (!dump_ctx->nest)
3765 goto nla_put_failure;
3769 nlmsg_free(dump_ctx->skb);
3772 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3774 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3775 struct devlink_dpipe_entry *entry)
3777 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3779 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3781 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3783 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3784 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3787 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3789 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3792 unsigned int value_count, value_index;
3793 struct devlink_dpipe_value *value;
3795 value = entry->action_values;
3796 value_count = entry->action_values_count;
3797 for (value_index = 0; value_index < value_count; value_index++) {
3798 kfree(value[value_index].value);
3799 kfree(value[value_index].mask);
3802 value = entry->match_values;
3803 value_count = entry->match_values_count;
3804 for (value_index = 0; value_index < value_count; value_index++) {
3805 kfree(value[value_index].value);
3806 kfree(value[value_index].mask);
3809 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3811 static int devlink_dpipe_entries_fill(struct genl_info *info,
3812 enum devlink_command cmd, int flags,
3813 struct devlink_dpipe_table *table)
3815 struct devlink_dpipe_dump_ctx dump_ctx;
3816 struct nlmsghdr *nlh;
3819 dump_ctx.skb = NULL;
3821 dump_ctx.info = info;
3823 err = table->table_ops->entries_dump(table->priv,
3824 table->counters_enabled,
3830 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3831 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3833 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3838 return genlmsg_reply(dump_ctx.skb, info);
3841 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3842 struct genl_info *info)
3844 struct devlink *devlink = info->user_ptr[0];
3845 struct devlink_dpipe_table *table;
3846 const char *table_name;
3848 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
3851 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3852 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3853 table_name, devlink);
3857 if (!table->table_ops->entries_dump)
3860 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3864 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3865 const struct devlink_dpipe_header *header)
3867 struct devlink_dpipe_field *field;
3868 struct nlattr *field_attr;
3871 for (i = 0; i < header->fields_count; i++) {
3872 field = &header->fields[i];
3873 field_attr = nla_nest_start_noflag(skb,
3874 DEVLINK_ATTR_DPIPE_FIELD);
3877 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3878 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3879 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3880 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3881 goto nla_put_failure;
3882 nla_nest_end(skb, field_attr);
3887 nla_nest_cancel(skb, field_attr);
3891 static int devlink_dpipe_header_put(struct sk_buff *skb,
3892 struct devlink_dpipe_header *header)
3894 struct nlattr *fields_attr, *header_attr;
3897 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3901 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3902 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3903 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3904 goto nla_put_failure;
3906 fields_attr = nla_nest_start_noflag(skb,
3907 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3909 goto nla_put_failure;
3911 err = devlink_dpipe_fields_put(skb, header);
3913 nla_nest_cancel(skb, fields_attr);
3914 goto nla_put_failure;
3916 nla_nest_end(skb, fields_attr);
3917 nla_nest_end(skb, header_attr);
3922 nla_nest_cancel(skb, header_attr);
3926 static int devlink_dpipe_headers_fill(struct genl_info *info,
3927 enum devlink_command cmd, int flags,
3928 struct devlink_dpipe_headers *
3931 struct devlink *devlink = info->user_ptr[0];
3932 struct nlattr *headers_attr;
3933 struct sk_buff *skb = NULL;
3934 struct nlmsghdr *nlh;
3941 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3945 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3946 &devlink_nl_family, NLM_F_MULTI, cmd);
3952 if (devlink_nl_put_handle(skb, devlink))
3953 goto nla_put_failure;
3954 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3956 goto nla_put_failure;
3959 for (; i < dpipe_headers->headers_count; i++) {
3960 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3968 nla_nest_end(skb, headers_attr);
3969 genlmsg_end(skb, hdr);
3970 if (i != dpipe_headers->headers_count)
3974 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3975 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3977 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3982 return genlmsg_reply(skb, info);
3991 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3992 struct genl_info *info)
3994 struct devlink *devlink = info->user_ptr[0];
3996 if (!devlink->dpipe_headers)
3998 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3999 0, devlink->dpipe_headers);
4002 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
4003 const char *table_name,
4006 struct devlink_dpipe_table *table;
4008 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4009 table_name, devlink);
4013 if (table->counter_control_extern)
4016 if (!(table->counters_enabled ^ enable))
4019 table->counters_enabled = enable;
4020 if (table->table_ops->counters_set_update)
4021 table->table_ops->counters_set_update(table->priv, enable);
4025 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
4026 struct genl_info *info)
4028 struct devlink *devlink = info->user_ptr[0];
4029 const char *table_name;
4030 bool counters_enable;
4032 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
4033 GENL_REQ_ATTR_CHECK(info,
4034 DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
4037 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
4038 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
4040 return devlink_dpipe_table_counters_set(devlink, table_name,
4044 static struct devlink_resource *
4045 devlink_resource_find(struct devlink *devlink,
4046 struct devlink_resource *resource, u64 resource_id)
4048 struct list_head *resource_list;
4051 resource_list = &resource->resource_list;
4053 resource_list = &devlink->resource_list;
4055 list_for_each_entry(resource, resource_list, list) {
4056 struct devlink_resource *child_resource;
4058 if (resource->id == resource_id)
4061 child_resource = devlink_resource_find(devlink, resource,
4064 return child_resource;
4070 devlink_resource_validate_children(struct devlink_resource *resource)
4072 struct devlink_resource *child_resource;
4073 bool size_valid = true;
4076 if (list_empty(&resource->resource_list))
4079 list_for_each_entry(child_resource, &resource->resource_list, list)
4080 parts_size += child_resource->size_new;
4082 if (parts_size > resource->size_new)
4085 resource->size_valid = size_valid;
4089 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
4090 struct netlink_ext_ack *extack)
4095 if (size > resource->size_params.size_max) {
4096 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
4100 if (size < resource->size_params.size_min) {
4101 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
4105 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
4107 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
4114 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
4115 struct genl_info *info)
4117 struct devlink *devlink = info->user_ptr[0];
4118 struct devlink_resource *resource;
4123 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_ID) ||
4124 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_SIZE))
4126 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
4128 resource = devlink_resource_find(devlink, NULL, resource_id);
4132 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
4133 err = devlink_resource_validate_size(resource, size, info->extack);
4137 resource->size_new = size;
4138 devlink_resource_validate_children(resource);
4139 if (resource->parent)
4140 devlink_resource_validate_children(resource->parent);
4145 devlink_resource_size_params_put(struct devlink_resource *resource,
4146 struct sk_buff *skb)
4148 struct devlink_resource_size_params *size_params;
4150 size_params = &resource->size_params;
4151 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
4152 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
4153 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
4154 size_params->size_max, DEVLINK_ATTR_PAD) ||
4155 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
4156 size_params->size_min, DEVLINK_ATTR_PAD) ||
4157 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
4162 static int devlink_resource_occ_put(struct devlink_resource *resource,
4163 struct sk_buff *skb)
4165 if (!resource->occ_get)
4167 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
4168 resource->occ_get(resource->occ_get_priv),
4172 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
4173 struct devlink_resource *resource)
4175 struct devlink_resource *child_resource;
4176 struct nlattr *child_resource_attr;
4177 struct nlattr *resource_attr;
4179 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
4183 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
4184 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
4185 DEVLINK_ATTR_PAD) ||
4186 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
4188 goto nla_put_failure;
4189 if (resource->size != resource->size_new)
4190 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
4191 resource->size_new, DEVLINK_ATTR_PAD);
4192 if (devlink_resource_occ_put(resource, skb))
4193 goto nla_put_failure;
4194 if (devlink_resource_size_params_put(resource, skb))
4195 goto nla_put_failure;
4196 if (list_empty(&resource->resource_list))
4199 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
4200 resource->size_valid))
4201 goto nla_put_failure;
4203 child_resource_attr = nla_nest_start_noflag(skb,
4204 DEVLINK_ATTR_RESOURCE_LIST);
4205 if (!child_resource_attr)
4206 goto nla_put_failure;
4208 list_for_each_entry(child_resource, &resource->resource_list, list) {
4209 if (devlink_resource_put(devlink, skb, child_resource))
4210 goto resource_put_failure;
4213 nla_nest_end(skb, child_resource_attr);
4215 nla_nest_end(skb, resource_attr);
4218 resource_put_failure:
4219 nla_nest_cancel(skb, child_resource_attr);
4221 nla_nest_cancel(skb, resource_attr);
4225 static int devlink_resource_fill(struct genl_info *info,
4226 enum devlink_command cmd, int flags)
4228 struct devlink *devlink = info->user_ptr[0];
4229 struct devlink_resource *resource;
4230 struct nlattr *resources_attr;
4231 struct sk_buff *skb = NULL;
4232 struct nlmsghdr *nlh;
4238 resource = list_first_entry(&devlink->resource_list,
4239 struct devlink_resource, list);
4241 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4245 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4246 &devlink_nl_family, NLM_F_MULTI, cmd);
4252 if (devlink_nl_put_handle(skb, devlink))
4253 goto nla_put_failure;
4255 resources_attr = nla_nest_start_noflag(skb,
4256 DEVLINK_ATTR_RESOURCE_LIST);
4257 if (!resources_attr)
4258 goto nla_put_failure;
4262 list_for_each_entry_from(resource, &devlink->resource_list, list) {
4263 err = devlink_resource_put(devlink, skb, resource);
4266 goto err_resource_put;
4272 nla_nest_end(skb, resources_attr);
4273 genlmsg_end(skb, hdr);
4277 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4278 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4280 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4285 return genlmsg_reply(skb, info);
4294 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
4295 struct genl_info *info)
4297 struct devlink *devlink = info->user_ptr[0];
4299 if (list_empty(&devlink->resource_list))
4302 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
4306 devlink_resources_validate(struct devlink *devlink,
4307 struct devlink_resource *resource,
4308 struct genl_info *info)
4310 struct list_head *resource_list;
4314 resource_list = &resource->resource_list;
4316 resource_list = &devlink->resource_list;
4318 list_for_each_entry(resource, resource_list, list) {
4319 if (!resource->size_valid)
4321 err = devlink_resources_validate(devlink, resource, info);
4328 static struct net *devlink_netns_get(struct sk_buff *skb,
4329 struct genl_info *info)
4331 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
4332 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
4333 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
4336 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
4337 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
4338 return ERR_PTR(-EINVAL);
4341 if (netns_pid_attr) {
4342 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
4343 } else if (netns_fd_attr) {
4344 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
4345 } else if (netns_id_attr) {
4346 net = get_net_ns_by_id(sock_net(skb->sk),
4347 nla_get_u32(netns_id_attr));
4349 net = ERR_PTR(-EINVAL);
4352 net = ERR_PTR(-EINVAL);
4355 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
4356 return ERR_PTR(-EINVAL);
4358 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
4360 return ERR_PTR(-EPERM);
4365 static void devlink_param_notify(struct devlink *devlink,
4366 unsigned int port_index,
4367 struct devlink_param_item *param_item,
4368 enum devlink_command cmd);
4370 static void devlink_ns_change_notify(struct devlink *devlink,
4371 struct net *dest_net, struct net *curr_net,
4374 struct devlink_param_item *param_item;
4375 enum devlink_command cmd;
4377 /* Userspace needs to be notified about devlink objects
4378 * removed from original and entering new network namespace.
4379 * The rest of the devlink objects are re-created during
4380 * reload process so the notifications are generated separatelly.
4383 if (!dest_net || net_eq(dest_net, curr_net))
4387 devlink_notify(devlink, DEVLINK_CMD_NEW);
4389 cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
4390 list_for_each_entry(param_item, &devlink->param_list, list)
4391 devlink_param_notify(devlink, 0, param_item, cmd);
4394 devlink_notify(devlink, DEVLINK_CMD_DEL);
4397 static bool devlink_reload_supported(const struct devlink_ops *ops)
4399 return ops->reload_down && ops->reload_up;
4402 static void devlink_reload_failed_set(struct devlink *devlink,
4405 if (devlink->reload_failed == reload_failed)
4407 devlink->reload_failed = reload_failed;
4408 devlink_notify(devlink, DEVLINK_CMD_NEW);
4411 bool devlink_is_reload_failed(const struct devlink *devlink)
4413 return devlink->reload_failed;
4415 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
4418 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
4419 enum devlink_reload_limit limit, u32 actions_performed)
4421 unsigned long actions = actions_performed;
4425 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
4426 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
4427 reload_stats[stat_idx]++;
4429 devlink_notify(devlink, DEVLINK_CMD_NEW);
4433 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
4434 u32 actions_performed)
4436 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
4441 * devlink_remote_reload_actions_performed - Update devlink on reload actions
4442 * performed which are not a direct result of devlink reload call.
4444 * This should be called by a driver after performing reload actions in case it was not
4445 * a result of devlink reload call. For example fw_activate was performed as a result
4446 * of devlink reload triggered fw_activate on another host.
4447 * The motivation for this function is to keep data on reload actions performed on this
4448 * function whether it was done due to direct devlink reload call or not.
4451 * @limit: reload limit
4452 * @actions_performed: bitmask of actions performed
4454 void devlink_remote_reload_actions_performed(struct devlink *devlink,
4455 enum devlink_reload_limit limit,
4456 u32 actions_performed)
4458 if (WARN_ON(!actions_performed ||
4459 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
4460 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
4461 limit > DEVLINK_RELOAD_LIMIT_MAX))
4464 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
4467 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
4469 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
4470 enum devlink_reload_action action, enum devlink_reload_limit limit,
4471 u32 *actions_performed, struct netlink_ext_ack *extack)
4473 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
4474 struct net *curr_net;
4477 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
4478 sizeof(remote_reload_stats));
4480 curr_net = devlink_net(devlink);
4481 devlink_ns_change_notify(devlink, dest_net, curr_net, false);
4482 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
4486 if (dest_net && !net_eq(dest_net, curr_net))
4487 write_pnet(&devlink->_net, dest_net);
4489 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
4490 devlink_reload_failed_set(devlink, !!err);
4494 devlink_ns_change_notify(devlink, dest_net, curr_net, true);
4495 WARN_ON(!(*actions_performed & BIT(action)));
4496 /* Catch driver on updating the remote action within devlink reload */
4497 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
4498 sizeof(remote_reload_stats)));
4499 devlink_reload_stats_update(devlink, limit, *actions_performed);
4504 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
4505 enum devlink_command cmd, struct genl_info *info)
4507 struct sk_buff *msg;
4510 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4514 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
4518 if (devlink_nl_put_handle(msg, devlink))
4519 goto nla_put_failure;
4521 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4523 goto nla_put_failure;
4524 genlmsg_end(msg, hdr);
4526 return genlmsg_reply(msg, info);
4529 genlmsg_cancel(msg, hdr);
4535 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4537 struct devlink *devlink = info->user_ptr[0];
4538 enum devlink_reload_action action;
4539 enum devlink_reload_limit limit;
4540 struct net *dest_net = NULL;
4541 u32 actions_performed;
4544 if (!(devlink->features & DEVLINK_F_RELOAD))
4547 err = devlink_resources_validate(devlink, NULL, info);
4549 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4553 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4554 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4556 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4558 if (!devlink_reload_action_is_supported(devlink, action)) {
4559 NL_SET_ERR_MSG_MOD(info->extack,
4560 "Requested reload action is not supported by the driver");
4564 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4565 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4566 struct nla_bitfield32 limits;
4567 u32 limits_selected;
4569 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4570 limits_selected = limits.value & limits.selector;
4571 if (!limits_selected) {
4572 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4575 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4576 if (limits_selected & BIT(limit))
4578 /* UAPI enables multiselection, but currently it is not used */
4579 if (limits_selected != BIT(limit)) {
4580 NL_SET_ERR_MSG_MOD(info->extack,
4581 "Multiselection of limit is not supported");
4584 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4585 NL_SET_ERR_MSG_MOD(info->extack,
4586 "Requested limit is not supported by the driver");
4589 if (devlink_reload_combination_is_invalid(action, limit)) {
4590 NL_SET_ERR_MSG_MOD(info->extack,
4591 "Requested limit is invalid for this action");
4595 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4596 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4597 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4598 dest_net = devlink_netns_get(skb, info);
4599 if (IS_ERR(dest_net))
4600 return PTR_ERR(dest_net);
4603 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4610 /* For backward compatibility generate reply only if attributes used by user */
4611 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4614 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4615 DEVLINK_CMD_RELOAD, info);
4618 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4619 struct devlink *devlink,
4620 enum devlink_command cmd,
4621 struct devlink_flash_notify *params)
4625 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4629 if (devlink_nl_put_handle(msg, devlink))
4630 goto nla_put_failure;
4632 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4635 if (params->status_msg &&
4636 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4637 params->status_msg))
4638 goto nla_put_failure;
4639 if (params->component &&
4640 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4642 goto nla_put_failure;
4643 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4644 params->done, DEVLINK_ATTR_PAD))
4645 goto nla_put_failure;
4646 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4647 params->total, DEVLINK_ATTR_PAD))
4648 goto nla_put_failure;
4649 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4650 params->timeout, DEVLINK_ATTR_PAD))
4651 goto nla_put_failure;
4654 genlmsg_end(msg, hdr);
4658 genlmsg_cancel(msg, hdr);
4662 static void __devlink_flash_update_notify(struct devlink *devlink,
4663 enum devlink_command cmd,
4664 struct devlink_flash_notify *params)
4666 struct sk_buff *msg;
4669 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4670 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4671 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4673 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4676 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4680 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4684 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4685 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4692 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4694 struct devlink_flash_notify params = {};
4696 __devlink_flash_update_notify(devlink,
4697 DEVLINK_CMD_FLASH_UPDATE,
4701 static void devlink_flash_update_end_notify(struct devlink *devlink)
4703 struct devlink_flash_notify params = {};
4705 __devlink_flash_update_notify(devlink,
4706 DEVLINK_CMD_FLASH_UPDATE_END,
4710 void devlink_flash_update_status_notify(struct devlink *devlink,
4711 const char *status_msg,
4712 const char *component,
4714 unsigned long total)
4716 struct devlink_flash_notify params = {
4717 .status_msg = status_msg,
4718 .component = component,
4723 __devlink_flash_update_notify(devlink,
4724 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4727 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4729 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4730 const char *status_msg,
4731 const char *component,
4732 unsigned long timeout)
4734 struct devlink_flash_notify params = {
4735 .status_msg = status_msg,
4736 .component = component,
4740 __devlink_flash_update_notify(devlink,
4741 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4744 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4746 struct devlink_info_req {
4747 struct sk_buff *msg;
4748 void (*version_cb)(const char *version_name,
4749 enum devlink_info_version_type version_type,
4750 void *version_cb_priv);
4751 void *version_cb_priv;
4754 struct devlink_flash_component_lookup_ctx {
4755 const char *lookup_name;
4756 bool lookup_name_found;
4760 devlink_flash_component_lookup_cb(const char *version_name,
4761 enum devlink_info_version_type version_type,
4762 void *version_cb_priv)
4764 struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
4766 if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
4767 lookup_ctx->lookup_name_found)
4770 lookup_ctx->lookup_name_found =
4771 !strcmp(lookup_ctx->lookup_name, version_name);
4774 static int devlink_flash_component_get(struct devlink *devlink,
4775 struct nlattr *nla_component,
4776 const char **p_component,
4777 struct netlink_ext_ack *extack)
4779 struct devlink_flash_component_lookup_ctx lookup_ctx = {};
4780 struct devlink_info_req req = {};
4781 const char *component;
4787 component = nla_data(nla_component);
4789 if (!devlink->ops->info_get) {
4790 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4791 "component update is not supported by this device");
4795 lookup_ctx.lookup_name = component;
4796 req.version_cb = devlink_flash_component_lookup_cb;
4797 req.version_cb_priv = &lookup_ctx;
4799 ret = devlink->ops->info_get(devlink, &req, NULL);
4803 if (!lookup_ctx.lookup_name_found) {
4804 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4805 "selected component is not supported by this device");
4808 *p_component = component;
4812 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4813 struct genl_info *info)
4815 struct nlattr *nla_overwrite_mask, *nla_file_name;
4816 struct devlink_flash_update_params params = {};
4817 struct devlink *devlink = info->user_ptr[0];
4818 const char *file_name;
4819 u32 supported_params;
4822 if (!devlink->ops->flash_update)
4825 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
4828 ret = devlink_flash_component_get(devlink,
4829 info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
4830 ¶ms.component, info->extack);
4834 supported_params = devlink->ops->supported_flash_update_params;
4836 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4837 if (nla_overwrite_mask) {
4838 struct nla_bitfield32 sections;
4840 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4841 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4842 "overwrite settings are not supported by this device");
4845 sections = nla_get_bitfield32(nla_overwrite_mask);
4846 params.overwrite_mask = sections.value & sections.selector;
4849 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4850 file_name = nla_data(nla_file_name);
4851 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
4853 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4857 devlink_flash_update_begin_notify(devlink);
4858 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
4859 devlink_flash_update_end_notify(devlink);
4861 release_firmware(params.fw);
4867 devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
4868 u32 portid, u32 seq, int flags,
4869 struct netlink_ext_ack *extack)
4871 struct nlattr *selftests;
4876 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
4877 DEVLINK_CMD_SELFTESTS_GET);
4882 if (devlink_nl_put_handle(msg, devlink))
4883 goto err_cancel_msg;
4885 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4887 goto err_cancel_msg;
4889 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4890 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4891 if (devlink->ops->selftest_check(devlink, i, extack)) {
4892 err = nla_put_flag(msg, i);
4894 goto err_cancel_msg;
4898 nla_nest_end(msg, selftests);
4899 genlmsg_end(msg, hdr);
4903 genlmsg_cancel(msg, hdr);
4907 static int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb,
4908 struct genl_info *info)
4910 struct devlink *devlink = info->user_ptr[0];
4911 struct sk_buff *msg;
4914 if (!devlink->ops->selftest_check)
4917 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4921 err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
4922 info->snd_seq, 0, info->extack);
4928 return genlmsg_reply(msg, info);
4931 static int devlink_nl_cmd_selftests_get_dumpit(struct sk_buff *msg,
4932 struct netlink_callback *cb)
4934 struct devlink *devlink;
4935 int start = cb->args[0];
4936 unsigned long index;
4940 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
4941 if (idx < start || !devlink->ops->selftest_check)
4945 err = devlink_nl_selftests_fill(msg, devlink,
4946 NETLINK_CB(cb->skb).portid,
4947 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4949 devl_unlock(devlink);
4951 devlink_put(devlink);
4956 devlink_put(devlink);
4959 if (err != -EMSGSIZE)
4966 static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
4967 enum devlink_selftest_status test_status)
4969 struct nlattr *result_attr;
4971 result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
4975 if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
4976 nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
4978 goto nla_put_failure;
4980 nla_nest_end(skb, result_attr);
4984 nla_nest_cancel(skb, result_attr);
4988 static int devlink_nl_cmd_selftests_run(struct sk_buff *skb,
4989 struct genl_info *info)
4991 struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
4992 struct devlink *devlink = info->user_ptr[0];
4993 struct nlattr *attrs, *selftests;
4994 struct sk_buff *msg;
4999 if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
5002 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
5005 attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
5007 err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
5008 devlink_selftest_nl_policy, info->extack);
5012 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5017 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
5018 &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
5022 if (devlink_nl_put_handle(msg, devlink))
5023 goto genlmsg_cancel;
5025 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
5027 goto genlmsg_cancel;
5029 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
5030 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
5031 enum devlink_selftest_status test_status;
5033 if (nla_get_flag(tb[i])) {
5034 if (!devlink->ops->selftest_check(devlink, i,
5036 if (devlink_selftest_result_put(msg, i,
5037 DEVLINK_SELFTEST_STATUS_SKIP))
5038 goto selftests_nest_cancel;
5042 test_status = devlink->ops->selftest_run(devlink, i,
5044 if (devlink_selftest_result_put(msg, i, test_status))
5045 goto selftests_nest_cancel;
5049 nla_nest_end(msg, selftests);
5050 genlmsg_end(msg, hdr);
5051 return genlmsg_reply(msg, info);
5053 selftests_nest_cancel:
5054 nla_nest_cancel(msg, selftests);
5056 genlmsg_cancel(msg, hdr);
5062 static const struct devlink_param devlink_param_generic[] = {
5064 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
5065 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
5066 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
5069 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
5070 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
5071 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
5074 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
5075 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
5076 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
5079 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
5080 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
5081 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
5084 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
5085 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
5086 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
5089 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
5090 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
5091 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
5094 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
5095 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
5096 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
5099 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
5100 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
5101 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
5104 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
5105 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
5106 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
5109 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
5110 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
5111 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
5114 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
5115 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
5116 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
5119 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
5120 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
5121 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
5124 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
5125 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
5126 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
5129 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
5130 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
5131 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
5134 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
5135 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
5136 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
5139 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
5140 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
5141 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
5144 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
5145 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
5146 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
5150 static int devlink_param_generic_verify(const struct devlink_param *param)
5152 /* verify it match generic parameter by id and name */
5153 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
5155 if (strcmp(param->name, devlink_param_generic[param->id].name))
5158 WARN_ON(param->type != devlink_param_generic[param->id].type);
5163 static int devlink_param_driver_verify(const struct devlink_param *param)
5167 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
5169 /* verify no such name in generic params */
5170 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
5171 if (!strcmp(param->name, devlink_param_generic[i].name))
5177 static struct devlink_param_item *
5178 devlink_param_find_by_name(struct list_head *param_list,
5179 const char *param_name)
5181 struct devlink_param_item *param_item;
5183 list_for_each_entry(param_item, param_list, list)
5184 if (!strcmp(param_item->param->name, param_name))
5189 static struct devlink_param_item *
5190 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
5192 struct devlink_param_item *param_item;
5194 list_for_each_entry(param_item, param_list, list)
5195 if (param_item->param->id == param_id)
5201 devlink_param_cmode_is_supported(const struct devlink_param *param,
5202 enum devlink_param_cmode cmode)
5204 return test_bit(cmode, ¶m->supported_cmodes);
5207 static int devlink_param_get(struct devlink *devlink,
5208 const struct devlink_param *param,
5209 struct devlink_param_gset_ctx *ctx)
5211 if (!param->get || devlink->reload_failed)
5213 return param->get(devlink, param->id, ctx);
5216 static int devlink_param_set(struct devlink *devlink,
5217 const struct devlink_param *param,
5218 struct devlink_param_gset_ctx *ctx)
5220 if (!param->set || devlink->reload_failed)
5222 return param->set(devlink, param->id, ctx);
5226 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
5228 switch (param_type) {
5229 case DEVLINK_PARAM_TYPE_U8:
5231 case DEVLINK_PARAM_TYPE_U16:
5233 case DEVLINK_PARAM_TYPE_U32:
5235 case DEVLINK_PARAM_TYPE_STRING:
5237 case DEVLINK_PARAM_TYPE_BOOL:
5245 devlink_nl_param_value_fill_one(struct sk_buff *msg,
5246 enum devlink_param_type type,
5247 enum devlink_param_cmode cmode,
5248 union devlink_param_value val)
5250 struct nlattr *param_value_attr;
5252 param_value_attr = nla_nest_start_noflag(msg,
5253 DEVLINK_ATTR_PARAM_VALUE);
5254 if (!param_value_attr)
5255 goto nla_put_failure;
5257 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
5258 goto value_nest_cancel;
5261 case DEVLINK_PARAM_TYPE_U8:
5262 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
5263 goto value_nest_cancel;
5265 case DEVLINK_PARAM_TYPE_U16:
5266 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
5267 goto value_nest_cancel;
5269 case DEVLINK_PARAM_TYPE_U32:
5270 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
5271 goto value_nest_cancel;
5273 case DEVLINK_PARAM_TYPE_STRING:
5274 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
5276 goto value_nest_cancel;
5278 case DEVLINK_PARAM_TYPE_BOOL:
5280 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
5281 goto value_nest_cancel;
5285 nla_nest_end(msg, param_value_attr);
5289 nla_nest_cancel(msg, param_value_attr);
5294 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
5295 unsigned int port_index,
5296 struct devlink_param_item *param_item,
5297 enum devlink_command cmd,
5298 u32 portid, u32 seq, int flags)
5300 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
5301 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
5302 const struct devlink_param *param = param_item->param;
5303 struct devlink_param_gset_ctx ctx;
5304 struct nlattr *param_values_list;
5305 struct nlattr *param_attr;
5311 /* Get value from driver part to driverinit configuration mode */
5312 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5313 if (!devlink_param_cmode_is_supported(param, i))
5315 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5316 if (!param_item->driverinit_value_valid)
5318 param_value[i] = param_item->driverinit_value;
5321 err = devlink_param_get(devlink, param, &ctx);
5324 param_value[i] = ctx.val;
5326 param_value_set[i] = true;
5329 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5333 if (devlink_nl_put_handle(msg, devlink))
5334 goto genlmsg_cancel;
5336 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
5337 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
5338 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
5339 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
5340 goto genlmsg_cancel;
5342 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
5344 goto genlmsg_cancel;
5345 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
5346 goto param_nest_cancel;
5347 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
5348 goto param_nest_cancel;
5350 nla_type = devlink_param_type_to_nla_type(param->type);
5352 goto param_nest_cancel;
5353 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
5354 goto param_nest_cancel;
5356 param_values_list = nla_nest_start_noflag(msg,
5357 DEVLINK_ATTR_PARAM_VALUES_LIST);
5358 if (!param_values_list)
5359 goto param_nest_cancel;
5361 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5362 if (!param_value_set[i])
5364 err = devlink_nl_param_value_fill_one(msg, param->type,
5367 goto values_list_nest_cancel;
5370 nla_nest_end(msg, param_values_list);
5371 nla_nest_end(msg, param_attr);
5372 genlmsg_end(msg, hdr);
5375 values_list_nest_cancel:
5376 nla_nest_end(msg, param_values_list);
5378 nla_nest_cancel(msg, param_attr);
5380 genlmsg_cancel(msg, hdr);
5384 static void devlink_param_notify(struct devlink *devlink,
5385 unsigned int port_index,
5386 struct devlink_param_item *param_item,
5387 enum devlink_command cmd)
5389 struct sk_buff *msg;
5392 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
5393 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
5394 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
5395 ASSERT_DEVLINK_REGISTERED(devlink);
5397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5400 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
5407 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
5408 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5411 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
5412 struct netlink_callback *cb)
5414 struct devlink_param_item *param_item;
5415 struct devlink *devlink;
5416 int start = cb->args[0];
5417 unsigned long index;
5421 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
5423 list_for_each_entry(param_item, &devlink->param_list, list) {
5428 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5429 DEVLINK_CMD_PARAM_GET,
5430 NETLINK_CB(cb->skb).portid,
5433 if (err == -EOPNOTSUPP) {
5436 devl_unlock(devlink);
5437 devlink_put(devlink);
5442 devl_unlock(devlink);
5443 devlink_put(devlink);
5446 if (err != -EMSGSIZE)
5454 devlink_param_type_get_from_info(struct genl_info *info,
5455 enum devlink_param_type *param_type)
5457 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
5460 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
5462 *param_type = DEVLINK_PARAM_TYPE_U8;
5465 *param_type = DEVLINK_PARAM_TYPE_U16;
5468 *param_type = DEVLINK_PARAM_TYPE_U32;
5471 *param_type = DEVLINK_PARAM_TYPE_STRING;
5474 *param_type = DEVLINK_PARAM_TYPE_BOOL;
5484 devlink_param_value_get_from_info(const struct devlink_param *param,
5485 struct genl_info *info,
5486 union devlink_param_value *value)
5488 struct nlattr *param_data;
5491 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
5493 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
5496 switch (param->type) {
5497 case DEVLINK_PARAM_TYPE_U8:
5498 if (nla_len(param_data) != sizeof(u8))
5500 value->vu8 = nla_get_u8(param_data);
5502 case DEVLINK_PARAM_TYPE_U16:
5503 if (nla_len(param_data) != sizeof(u16))
5505 value->vu16 = nla_get_u16(param_data);
5507 case DEVLINK_PARAM_TYPE_U32:
5508 if (nla_len(param_data) != sizeof(u32))
5510 value->vu32 = nla_get_u32(param_data);
5512 case DEVLINK_PARAM_TYPE_STRING:
5513 len = strnlen(nla_data(param_data), nla_len(param_data));
5514 if (len == nla_len(param_data) ||
5515 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
5517 strcpy(value->vstr, nla_data(param_data));
5519 case DEVLINK_PARAM_TYPE_BOOL:
5520 if (param_data && nla_len(param_data))
5522 value->vbool = nla_get_flag(param_data);
5528 static struct devlink_param_item *
5529 devlink_param_get_from_info(struct list_head *param_list,
5530 struct genl_info *info)
5534 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_NAME))
5537 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
5538 return devlink_param_find_by_name(param_list, param_name);
5541 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
5542 struct genl_info *info)
5544 struct devlink *devlink = info->user_ptr[0];
5545 struct devlink_param_item *param_item;
5546 struct sk_buff *msg;
5549 param_item = devlink_param_get_from_info(&devlink->param_list, info);
5553 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5557 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5558 DEVLINK_CMD_PARAM_GET,
5559 info->snd_portid, info->snd_seq, 0);
5565 return genlmsg_reply(msg, info);
5568 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
5569 unsigned int port_index,
5570 struct list_head *param_list,
5571 struct genl_info *info,
5572 enum devlink_command cmd)
5574 enum devlink_param_type param_type;
5575 struct devlink_param_gset_ctx ctx;
5576 enum devlink_param_cmode cmode;
5577 struct devlink_param_item *param_item;
5578 const struct devlink_param *param;
5579 union devlink_param_value value;
5582 param_item = devlink_param_get_from_info(param_list, info);
5585 param = param_item->param;
5586 err = devlink_param_type_get_from_info(info, ¶m_type);
5589 if (param_type != param->type)
5591 err = devlink_param_value_get_from_info(param, info, &value);
5594 if (param->validate) {
5595 err = param->validate(devlink, param->id, value, info->extack);
5600 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_CMODE))
5602 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
5603 if (!devlink_param_cmode_is_supported(param, cmode))
5606 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5607 if (param->type == DEVLINK_PARAM_TYPE_STRING)
5608 strcpy(param_item->driverinit_value.vstr, value.vstr);
5610 param_item->driverinit_value = value;
5611 param_item->driverinit_value_valid = true;
5617 err = devlink_param_set(devlink, param, &ctx);
5622 devlink_param_notify(devlink, port_index, param_item, cmd);
5626 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
5627 struct genl_info *info)
5629 struct devlink *devlink = info->user_ptr[0];
5631 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
5632 info, DEVLINK_CMD_PARAM_NEW);
5635 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
5636 struct netlink_callback *cb)
5638 NL_SET_ERR_MSG_MOD(cb->extack, "Port params are not supported");
5642 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
5643 struct genl_info *info)
5645 NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5649 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
5650 struct genl_info *info)
5652 NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5656 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
5657 struct devlink *devlink,
5658 struct devlink_snapshot *snapshot)
5660 struct nlattr *snap_attr;
5663 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5667 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5669 goto nla_put_failure;
5671 nla_nest_end(msg, snap_attr);
5675 nla_nest_cancel(msg, snap_attr);
5679 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5680 struct devlink *devlink,
5681 struct devlink_region *region)
5683 struct devlink_snapshot *snapshot;
5684 struct nlattr *snapshots_attr;
5687 snapshots_attr = nla_nest_start_noflag(msg,
5688 DEVLINK_ATTR_REGION_SNAPSHOTS);
5689 if (!snapshots_attr)
5692 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
5693 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5695 goto nla_put_failure;
5698 nla_nest_end(msg, snapshots_attr);
5702 nla_nest_cancel(msg, snapshots_attr);
5706 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5707 enum devlink_command cmd, u32 portid,
5709 struct devlink_region *region)
5714 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5718 err = devlink_nl_put_handle(msg, devlink);
5720 goto nla_put_failure;
5723 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5724 region->port->index);
5726 goto nla_put_failure;
5729 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5731 goto nla_put_failure;
5733 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5737 goto nla_put_failure;
5739 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
5740 region->max_snapshots);
5742 goto nla_put_failure;
5744 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5746 goto nla_put_failure;
5748 genlmsg_end(msg, hdr);
5752 genlmsg_cancel(msg, hdr);
5756 static struct sk_buff *
5757 devlink_nl_region_notify_build(struct devlink_region *region,
5758 struct devlink_snapshot *snapshot,
5759 enum devlink_command cmd, u32 portid, u32 seq)
5761 struct devlink *devlink = region->devlink;
5762 struct sk_buff *msg;
5767 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5769 return ERR_PTR(-ENOMEM);
5771 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5777 err = devlink_nl_put_handle(msg, devlink);
5779 goto out_cancel_msg;
5782 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5783 region->port->index);
5785 goto out_cancel_msg;
5788 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5791 goto out_cancel_msg;
5794 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5797 goto out_cancel_msg;
5799 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5800 region->size, DEVLINK_ATTR_PAD);
5802 goto out_cancel_msg;
5804 genlmsg_end(msg, hdr);
5809 genlmsg_cancel(msg, hdr);
5812 return ERR_PTR(err);
5815 static void devlink_nl_region_notify(struct devlink_region *region,
5816 struct devlink_snapshot *snapshot,
5817 enum devlink_command cmd)
5819 struct devlink *devlink = region->devlink;
5820 struct sk_buff *msg;
5822 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5823 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
5826 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5830 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
5831 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5835 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5836 * @devlink: devlink instance
5837 * @id: the snapshot id
5839 * Track when a new snapshot begins using an id. Load the count for the
5840 * given id from the snapshot xarray, increment it, and store it back.
5842 * Called when a new snapshot is created with the given id.
5844 * The id *must* have been previously allocated by
5845 * devlink_region_snapshot_id_get().
5847 * Returns 0 on success, or an error on failure.
5849 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5851 unsigned long count;
5855 xa_lock(&devlink->snapshot_ids);
5856 p = xa_load(&devlink->snapshot_ids, id);
5862 if (WARN_ON(!xa_is_value(p))) {
5867 count = xa_to_value(p);
5870 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5873 xa_unlock(&devlink->snapshot_ids);
5878 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5879 * @devlink: devlink instance
5880 * @id: the snapshot id
5882 * Track when a snapshot is deleted and stops using an id. Load the count
5883 * for the given id from the snapshot xarray, decrement it, and store it
5886 * If the count reaches zero, erase this id from the xarray, freeing it
5887 * up for future re-use by devlink_region_snapshot_id_get().
5889 * Called when a snapshot using the given id is deleted, and when the
5890 * initial allocator of the id is finished using it.
5892 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5894 unsigned long count;
5897 xa_lock(&devlink->snapshot_ids);
5898 p = xa_load(&devlink->snapshot_ids, id);
5902 if (WARN_ON(!xa_is_value(p)))
5905 count = xa_to_value(p);
5909 __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5912 /* If this was the last user, we can erase this id */
5913 __xa_erase(&devlink->snapshot_ids, id);
5916 xa_unlock(&devlink->snapshot_ids);
5920 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
5921 * @devlink: devlink instance
5922 * @id: the snapshot id
5924 * Mark the given snapshot id as used by inserting a zero value into the
5927 * This must be called while holding the devlink instance lock. Unlike
5928 * devlink_snapshot_id_get, the initial reference count is zero, not one.
5929 * It is expected that the id will immediately be used before
5930 * releasing the devlink instance lock.
5932 * Returns zero on success, or an error code if the snapshot id could not
5935 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5939 xa_lock(&devlink->snapshot_ids);
5940 if (xa_load(&devlink->snapshot_ids, id)) {
5941 xa_unlock(&devlink->snapshot_ids);
5944 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5946 xa_unlock(&devlink->snapshot_ids);
5951 * __devlink_region_snapshot_id_get - get snapshot ID
5952 * @devlink: devlink instance
5953 * @id: storage to return snapshot id
5955 * Allocates a new snapshot id. Returns zero on success, or a negative
5956 * error on failure. Must be called while holding the devlink instance
5959 * Snapshot IDs are tracked using an xarray which stores the number of
5960 * users of the snapshot id.
5962 * Note that the caller of this function counts as a 'user', in order to
5963 * avoid race conditions. The caller must release its hold on the
5964 * snapshot by using devlink_region_snapshot_id_put.
5966 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5968 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5969 xa_limit_32b, GFP_KERNEL);
5973 * __devlink_region_snapshot_create - create a new snapshot
5974 * This will add a new snapshot of a region. The snapshot
5975 * will be stored on the region struct and can be accessed
5976 * from devlink. This is useful for future analyses of snapshots.
5977 * Multiple snapshots can be created on a region.
5978 * The @snapshot_id should be obtained using the getter function.
5980 * Must be called only while holding the region snapshot lock.
5982 * @region: devlink region of the snapshot
5983 * @data: snapshot data
5984 * @snapshot_id: snapshot id to be created
5987 __devlink_region_snapshot_create(struct devlink_region *region,
5988 u8 *data, u32 snapshot_id)
5990 struct devlink *devlink = region->devlink;
5991 struct devlink_snapshot *snapshot;
5994 lockdep_assert_held(®ion->snapshot_lock);
5996 /* check if region can hold one more snapshot */
5997 if (region->cur_snapshots == region->max_snapshots)
6000 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
6003 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
6007 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
6009 goto err_snapshot_id_increment;
6011 snapshot->id = snapshot_id;
6012 snapshot->region = region;
6013 snapshot->data = data;
6015 list_add_tail(&snapshot->list, ®ion->snapshot_list);
6017 region->cur_snapshots++;
6019 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
6022 err_snapshot_id_increment:
6027 static void devlink_region_snapshot_del(struct devlink_region *region,
6028 struct devlink_snapshot *snapshot)
6030 struct devlink *devlink = region->devlink;
6032 lockdep_assert_held(®ion->snapshot_lock);
6034 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
6035 region->cur_snapshots--;
6036 list_del(&snapshot->list);
6037 region->ops->destructor(snapshot->data);
6038 __devlink_snapshot_id_decrement(devlink, snapshot->id);
6042 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
6043 struct genl_info *info)
6045 struct devlink *devlink = info->user_ptr[0];
6046 struct devlink_port *port = NULL;
6047 struct devlink_region *region;
6048 const char *region_name;
6049 struct sk_buff *msg;
6053 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME))
6056 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6057 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6059 port = devlink_port_get_by_index(devlink, index);
6064 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6066 region = devlink_port_region_get_by_name(port, region_name);
6068 region = devlink_region_get_by_name(devlink, region_name);
6073 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6077 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
6078 info->snd_portid, info->snd_seq, 0,
6085 return genlmsg_reply(msg, info);
6088 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
6089 struct netlink_callback *cb,
6090 struct devlink_port *port,
6094 struct devlink_region *region;
6097 list_for_each_entry(region, &port->region_list, list) {
6102 err = devlink_nl_region_fill(msg, port->devlink,
6103 DEVLINK_CMD_REGION_GET,
6104 NETLINK_CB(cb->skb).portid,
6106 NLM_F_MULTI, region);
6116 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
6117 struct netlink_callback *cb,
6118 struct devlink *devlink,
6122 struct devlink_region *region;
6123 struct devlink_port *port;
6127 list_for_each_entry(region, &devlink->region_list, list) {
6132 err = devlink_nl_region_fill(msg, devlink,
6133 DEVLINK_CMD_REGION_GET,
6134 NETLINK_CB(cb->skb).portid,
6136 NLM_F_MULTI, region);
6142 list_for_each_entry(port, &devlink->port_list, list) {
6143 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
6150 devl_unlock(devlink);
6154 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
6155 struct netlink_callback *cb)
6157 struct devlink *devlink;
6158 int start = cb->args[0];
6159 unsigned long index;
6163 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
6164 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
6166 devlink_put(devlink);
6175 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
6176 struct genl_info *info)
6178 struct devlink *devlink = info->user_ptr[0];
6179 struct devlink_snapshot *snapshot;
6180 struct devlink_port *port = NULL;
6181 struct devlink_region *region;
6182 const char *region_name;
6186 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) ||
6187 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID))
6190 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6191 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6193 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6194 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6196 port = devlink_port_get_by_index(devlink, index);
6202 region = devlink_port_region_get_by_name(port, region_name);
6204 region = devlink_region_get_by_name(devlink, region_name);
6209 mutex_lock(®ion->snapshot_lock);
6210 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6212 mutex_unlock(®ion->snapshot_lock);
6216 devlink_region_snapshot_del(region, snapshot);
6217 mutex_unlock(®ion->snapshot_lock);
6222 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
6224 struct devlink *devlink = info->user_ptr[0];
6225 struct devlink_snapshot *snapshot;
6226 struct devlink_port *port = NULL;
6227 struct nlattr *snapshot_id_attr;
6228 struct devlink_region *region;
6229 const char *region_name;
6235 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) {
6236 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
6240 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6242 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6243 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6245 port = devlink_port_get_by_index(devlink, index);
6251 region = devlink_port_region_get_by_name(port, region_name);
6253 region = devlink_region_get_by_name(devlink, region_name);
6256 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
6260 if (!region->ops->snapshot) {
6261 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
6265 mutex_lock(®ion->snapshot_lock);
6267 if (region->cur_snapshots == region->max_snapshots) {
6268 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
6273 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6274 if (snapshot_id_attr) {
6275 snapshot_id = nla_get_u32(snapshot_id_attr);
6277 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6278 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
6283 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
6287 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
6289 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
6295 err = region->port_ops->snapshot(port, region->port_ops,
6296 info->extack, &data);
6298 err = region->ops->snapshot(devlink, region->ops,
6299 info->extack, &data);
6301 goto err_snapshot_capture;
6303 err = __devlink_region_snapshot_create(region, data, snapshot_id);
6305 goto err_snapshot_create;
6307 if (!snapshot_id_attr) {
6308 struct sk_buff *msg;
6310 snapshot = devlink_region_snapshot_get_by_id(region,
6312 if (WARN_ON(!snapshot)) {
6317 msg = devlink_nl_region_notify_build(region, snapshot,
6318 DEVLINK_CMD_REGION_NEW,
6321 err = PTR_ERR_OR_ZERO(msg);
6325 err = genlmsg_reply(msg, info);
6330 mutex_unlock(®ion->snapshot_lock);
6333 err_snapshot_create:
6334 region->ops->destructor(data);
6335 err_snapshot_capture:
6336 __devlink_snapshot_id_decrement(devlink, snapshot_id);
6337 mutex_unlock(®ion->snapshot_lock);
6341 devlink_region_snapshot_del(region, snapshot);
6343 mutex_unlock(®ion->snapshot_lock);
6347 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
6348 struct devlink *devlink,
6349 u8 *chunk, u32 chunk_size,
6352 struct nlattr *chunk_attr;
6355 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
6359 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
6361 goto nla_put_failure;
6363 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
6366 goto nla_put_failure;
6368 nla_nest_end(msg, chunk_attr);
6372 nla_nest_cancel(msg, chunk_attr);
6376 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
6378 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
6379 struct devlink *devlink,
6380 struct devlink_region *region,
6381 struct nlattr **attrs,
6386 struct devlink_snapshot *snapshot;
6387 u64 curr_offset = start_offset;
6391 *new_offset = start_offset;
6393 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6394 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6398 while (curr_offset < end_offset) {
6402 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
6403 data_size = end_offset - curr_offset;
6405 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
6407 data = &snapshot->data[curr_offset];
6408 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
6414 curr_offset += data_size;
6416 *new_offset = curr_offset;
6421 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
6422 struct netlink_callback *cb)
6424 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6425 u64 ret_offset, start_offset, end_offset = U64_MAX;
6426 struct nlattr **attrs = info->attrs;
6427 struct devlink_port *port = NULL;
6428 struct devlink_region *region;
6429 struct nlattr *chunks_attr;
6430 const char *region_name;
6431 struct devlink *devlink;
6436 start_offset = *((u64 *)&cb->args[0]);
6438 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6439 if (IS_ERR(devlink))
6440 return PTR_ERR(devlink);
6444 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
6445 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
6450 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6451 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6453 port = devlink_port_get_by_index(devlink, index);
6460 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
6463 region = devlink_port_region_get_by_name(port, region_name);
6465 region = devlink_region_get_by_name(devlink, region_name);
6472 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
6473 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
6476 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6478 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6479 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
6482 if (end_offset > region->size)
6483 end_offset = region->size;
6485 /* return 0 if there is no further data to read */
6486 if (start_offset == end_offset) {
6491 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6492 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
6493 DEVLINK_CMD_REGION_READ);
6499 err = devlink_nl_put_handle(skb, devlink);
6501 goto nla_put_failure;
6504 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
6505 region->port->index);
6507 goto nla_put_failure;
6510 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
6512 goto nla_put_failure;
6514 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
6517 goto nla_put_failure;
6520 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
6523 end_offset, &ret_offset);
6525 if (err && err != -EMSGSIZE)
6526 goto nla_put_failure;
6528 /* Check if there was any progress done to prevent infinite loop */
6529 if (ret_offset == start_offset) {
6531 goto nla_put_failure;
6534 *((u64 *)&cb->args[0]) = ret_offset;
6536 nla_nest_end(skb, chunks_attr);
6537 genlmsg_end(skb, hdr);
6538 devl_unlock(devlink);
6539 devlink_put(devlink);
6543 genlmsg_cancel(skb, hdr);
6545 devl_unlock(devlink);
6546 devlink_put(devlink);
6550 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
6554 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
6556 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
6558 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
6562 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
6564 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
6566 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
6571 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
6574 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
6576 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
6577 const char *version_name,
6578 const char *version_value,
6579 enum devlink_info_version_type version_type)
6581 struct nlattr *nest;
6584 if (req->version_cb)
6585 req->version_cb(version_name, version_type,
6586 req->version_cb_priv);
6591 nest = nla_nest_start_noflag(req->msg, attr);
6595 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
6598 goto nla_put_failure;
6600 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
6603 goto nla_put_failure;
6605 nla_nest_end(req->msg, nest);
6610 nla_nest_cancel(req->msg, nest);
6614 int devlink_info_version_fixed_put(struct devlink_info_req *req,
6615 const char *version_name,
6616 const char *version_value)
6618 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
6619 version_name, version_value,
6620 DEVLINK_INFO_VERSION_TYPE_NONE);
6622 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
6624 int devlink_info_version_stored_put(struct devlink_info_req *req,
6625 const char *version_name,
6626 const char *version_value)
6628 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6629 version_name, version_value,
6630 DEVLINK_INFO_VERSION_TYPE_NONE);
6632 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
6634 int devlink_info_version_stored_put_ext(struct devlink_info_req *req,
6635 const char *version_name,
6636 const char *version_value,
6637 enum devlink_info_version_type version_type)
6639 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6640 version_name, version_value,
6643 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
6645 int devlink_info_version_running_put(struct devlink_info_req *req,
6646 const char *version_name,
6647 const char *version_value)
6649 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6650 version_name, version_value,
6651 DEVLINK_INFO_VERSION_TYPE_NONE);
6653 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
6655 int devlink_info_version_running_put_ext(struct devlink_info_req *req,
6656 const char *version_name,
6657 const char *version_value,
6658 enum devlink_info_version_type version_type)
6660 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6661 version_name, version_value,
6664 EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
6667 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
6668 enum devlink_command cmd, u32 portid,
6669 u32 seq, int flags, struct netlink_ext_ack *extack)
6671 struct devlink_info_req req = {};
6675 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6680 if (devlink_nl_put_handle(msg, devlink))
6681 goto err_cancel_msg;
6684 err = devlink->ops->info_get(devlink, &req, extack);
6686 goto err_cancel_msg;
6688 genlmsg_end(msg, hdr);
6692 genlmsg_cancel(msg, hdr);
6696 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
6697 struct genl_info *info)
6699 struct devlink *devlink = info->user_ptr[0];
6700 struct sk_buff *msg;
6703 if (!devlink->ops->info_get)
6706 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6710 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6711 info->snd_portid, info->snd_seq, 0,
6718 return genlmsg_reply(msg, info);
6721 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
6722 struct netlink_callback *cb)
6724 struct devlink *devlink;
6725 int start = cb->args[0];
6726 unsigned long index;
6730 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
6731 if (idx < start || !devlink->ops->info_get)
6735 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6736 NETLINK_CB(cb->skb).portid,
6737 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6739 devl_unlock(devlink);
6740 if (err == -EOPNOTSUPP)
6743 devlink_put(devlink);
6748 devlink_put(devlink);
6751 if (err != -EMSGSIZE)
6758 struct devlink_fmsg_item {
6759 struct list_head list;
6766 struct devlink_fmsg {
6767 struct list_head item_list;
6768 bool putting_binary; /* This flag forces enclosing of binary data
6769 * in an array brackets. It forces using
6770 * of designated API:
6771 * devlink_fmsg_binary_pair_nest_start()
6772 * devlink_fmsg_binary_pair_nest_end()
6776 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6778 struct devlink_fmsg *fmsg;
6780 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6784 INIT_LIST_HEAD(&fmsg->item_list);
6789 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6791 struct devlink_fmsg_item *item, *tmp;
6793 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6794 list_del(&item->list);
6800 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6803 struct devlink_fmsg_item *item;
6805 item = kzalloc(sizeof(*item), GFP_KERNEL);
6809 item->attrtype = attrtype;
6810 list_add_tail(&item->list, &fmsg->item_list);
6815 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6817 if (fmsg->putting_binary)
6820 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6822 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6824 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6826 if (fmsg->putting_binary)
6829 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6832 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6834 if (fmsg->putting_binary)
6837 return devlink_fmsg_nest_end(fmsg);
6839 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6841 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6843 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6845 struct devlink_fmsg_item *item;
6847 if (fmsg->putting_binary)
6850 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6853 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6857 item->nla_type = NLA_NUL_STRING;
6858 item->len = strlen(name) + 1;
6859 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6860 memcpy(&item->value, name, item->len);
6861 list_add_tail(&item->list, &fmsg->item_list);
6866 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6870 if (fmsg->putting_binary)
6873 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6877 err = devlink_fmsg_put_name(fmsg, name);
6883 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6885 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6887 if (fmsg->putting_binary)
6890 return devlink_fmsg_nest_end(fmsg);
6892 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6894 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6899 if (fmsg->putting_binary)
6902 err = devlink_fmsg_pair_nest_start(fmsg, name);
6906 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6912 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6914 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6918 if (fmsg->putting_binary)
6921 err = devlink_fmsg_nest_end(fmsg);
6925 err = devlink_fmsg_nest_end(fmsg);
6931 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6933 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6938 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6942 fmsg->putting_binary = true;
6945 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6947 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6949 if (!fmsg->putting_binary)
6952 fmsg->putting_binary = false;
6953 return devlink_fmsg_arr_pair_nest_end(fmsg);
6955 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6957 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6958 const void *value, u16 value_len,
6961 struct devlink_fmsg_item *item;
6963 if (value_len > DEVLINK_FMSG_MAX_SIZE)
6966 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
6970 item->nla_type = value_nla_type;
6971 item->len = value_len;
6972 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6973 memcpy(&item->value, value, item->len);
6974 list_add_tail(&item->list, &fmsg->item_list);
6979 static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
6981 if (fmsg->putting_binary)
6984 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
6987 static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
6989 if (fmsg->putting_binary)
6992 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
6995 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
6997 if (fmsg->putting_binary)
7000 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
7002 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
7004 static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
7006 if (fmsg->putting_binary)
7009 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
7012 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
7014 if (fmsg->putting_binary)
7017 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
7020 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
7022 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
7025 if (!fmsg->putting_binary)
7028 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
7030 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
7032 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
7037 err = devlink_fmsg_pair_nest_start(fmsg, name);
7041 err = devlink_fmsg_bool_put(fmsg, value);
7045 err = devlink_fmsg_pair_nest_end(fmsg);
7051 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
7053 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
7058 err = devlink_fmsg_pair_nest_start(fmsg, name);
7062 err = devlink_fmsg_u8_put(fmsg, value);
7066 err = devlink_fmsg_pair_nest_end(fmsg);
7072 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
7074 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
7079 err = devlink_fmsg_pair_nest_start(fmsg, name);
7083 err = devlink_fmsg_u32_put(fmsg, value);
7087 err = devlink_fmsg_pair_nest_end(fmsg);
7093 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
7095 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
7100 err = devlink_fmsg_pair_nest_start(fmsg, name);
7104 err = devlink_fmsg_u64_put(fmsg, value);
7108 err = devlink_fmsg_pair_nest_end(fmsg);
7114 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
7116 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
7121 err = devlink_fmsg_pair_nest_start(fmsg, name);
7125 err = devlink_fmsg_string_put(fmsg, value);
7129 err = devlink_fmsg_pair_nest_end(fmsg);
7135 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
7137 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
7138 const void *value, u32 value_len)
7145 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
7149 for (offset = 0; offset < value_len; offset += data_size) {
7150 data_size = value_len - offset;
7151 if (data_size > DEVLINK_FMSG_MAX_SIZE)
7152 data_size = DEVLINK_FMSG_MAX_SIZE;
7153 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
7156 /* Exit from loop with a break (instead of
7157 * return) to make sure putting_binary is turned off in
7158 * devlink_fmsg_binary_pair_nest_end
7162 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
7168 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
7171 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7173 switch (msg->nla_type) {
7178 case NLA_NUL_STRING:
7180 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
7188 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7190 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
7193 switch (msg->nla_type) {
7195 /* Always provide flag data, regardless of its value */
7196 tmp = *(bool *) msg->value;
7198 return nla_put_u8(skb, attrtype, tmp);
7200 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
7202 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
7204 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
7206 case NLA_NUL_STRING:
7207 return nla_put_string(skb, attrtype, (char *) &msg->value);
7209 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
7216 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7219 struct devlink_fmsg_item *item;
7220 struct nlattr *fmsg_nlattr;
7224 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
7228 list_for_each_entry(item, &fmsg->item_list, list) {
7234 switch (item->attrtype) {
7235 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7236 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7237 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7238 case DEVLINK_ATTR_FMSG_NEST_END:
7239 err = nla_put_flag(skb, item->attrtype);
7241 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
7242 err = devlink_fmsg_item_fill_type(item, skb);
7245 err = devlink_fmsg_item_fill_data(item, skb);
7247 case DEVLINK_ATTR_FMSG_OBJ_NAME:
7248 err = nla_put_string(skb, item->attrtype,
7249 (char *) &item->value);
7261 nla_nest_end(skb, fmsg_nlattr);
7265 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
7266 struct genl_info *info,
7267 enum devlink_command cmd, int flags)
7269 struct nlmsghdr *nlh;
7270 struct sk_buff *skb;
7277 int tmp_index = index;
7279 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7283 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
7284 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
7287 goto nla_put_failure;
7290 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7293 else if (err != -EMSGSIZE || tmp_index == index)
7294 goto nla_put_failure;
7296 genlmsg_end(skb, hdr);
7297 err = genlmsg_reply(skb, info);
7302 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7305 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
7306 NLMSG_DONE, 0, flags | NLM_F_MULTI);
7309 goto nla_put_failure;
7312 return genlmsg_reply(skb, info);
7319 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7320 struct netlink_callback *cb,
7321 enum devlink_command cmd)
7323 int index = cb->args[0];
7324 int tmp_index = index;
7328 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7329 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
7332 goto nla_put_failure;
7335 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7336 if ((err && err != -EMSGSIZE) || tmp_index == index)
7337 goto nla_put_failure;
7339 cb->args[0] = index;
7340 genlmsg_end(skb, hdr);
7344 genlmsg_cancel(skb, hdr);
7348 struct devlink_health_reporter {
7349 struct list_head list;
7351 const struct devlink_health_reporter_ops *ops;
7352 struct devlink *devlink;
7353 struct devlink_port *devlink_port;
7354 struct devlink_fmsg *dump_fmsg;
7355 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
7356 u64 graceful_period;
7364 u64 last_recovery_ts;
7365 refcount_t refcount;
7369 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
7371 return reporter->priv;
7373 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
7375 static struct devlink_health_reporter *
7376 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
7377 struct mutex *list_lock,
7378 const char *reporter_name)
7380 struct devlink_health_reporter *reporter;
7382 lockdep_assert_held(list_lock);
7383 list_for_each_entry(reporter, reporter_list, list)
7384 if (!strcmp(reporter->ops->name, reporter_name))
7389 static struct devlink_health_reporter *
7390 devlink_health_reporter_find_by_name(struct devlink *devlink,
7391 const char *reporter_name)
7393 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
7394 &devlink->reporters_lock,
7398 static struct devlink_health_reporter *
7399 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
7400 const char *reporter_name)
7402 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
7403 &devlink_port->reporters_lock,
7407 static struct devlink_health_reporter *
7408 __devlink_health_reporter_create(struct devlink *devlink,
7409 const struct devlink_health_reporter_ops *ops,
7410 u64 graceful_period, void *priv)
7412 struct devlink_health_reporter *reporter;
7414 if (WARN_ON(graceful_period && !ops->recover))
7415 return ERR_PTR(-EINVAL);
7417 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
7419 return ERR_PTR(-ENOMEM);
7421 reporter->priv = priv;
7422 reporter->ops = ops;
7423 reporter->devlink = devlink;
7424 reporter->graceful_period = graceful_period;
7425 reporter->auto_recover = !!ops->recover;
7426 reporter->auto_dump = !!ops->dump;
7427 mutex_init(&reporter->dump_lock);
7428 refcount_set(&reporter->refcount, 1);
7433 * devlink_port_health_reporter_create - create devlink health reporter for
7434 * specified port instance
7436 * @port: devlink_port which should contain the new reporter
7438 * @graceful_period: to avoid recovery loops, in msecs
7441 struct devlink_health_reporter *
7442 devlink_port_health_reporter_create(struct devlink_port *port,
7443 const struct devlink_health_reporter_ops *ops,
7444 u64 graceful_period, void *priv)
7446 struct devlink_health_reporter *reporter;
7448 mutex_lock(&port->reporters_lock);
7449 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
7450 &port->reporters_lock, ops->name)) {
7451 reporter = ERR_PTR(-EEXIST);
7455 reporter = __devlink_health_reporter_create(port->devlink, ops,
7456 graceful_period, priv);
7457 if (IS_ERR(reporter))
7460 reporter->devlink_port = port;
7461 list_add_tail(&reporter->list, &port->reporter_list);
7463 mutex_unlock(&port->reporters_lock);
7466 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
7469 * devlink_health_reporter_create - create devlink health reporter
7473 * @graceful_period: to avoid recovery loops, in msecs
7476 struct devlink_health_reporter *
7477 devlink_health_reporter_create(struct devlink *devlink,
7478 const struct devlink_health_reporter_ops *ops,
7479 u64 graceful_period, void *priv)
7481 struct devlink_health_reporter *reporter;
7483 mutex_lock(&devlink->reporters_lock);
7484 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
7485 reporter = ERR_PTR(-EEXIST);
7489 reporter = __devlink_health_reporter_create(devlink, ops,
7490 graceful_period, priv);
7491 if (IS_ERR(reporter))
7494 list_add_tail(&reporter->list, &devlink->reporter_list);
7496 mutex_unlock(&devlink->reporters_lock);
7499 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
7502 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
7504 mutex_destroy(&reporter->dump_lock);
7505 if (reporter->dump_fmsg)
7506 devlink_fmsg_free(reporter->dump_fmsg);
7511 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
7513 if (refcount_dec_and_test(&reporter->refcount))
7514 devlink_health_reporter_free(reporter);
7518 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7520 list_del(&reporter->list);
7521 devlink_health_reporter_put(reporter);
7525 * devlink_health_reporter_destroy - destroy devlink health reporter
7527 * @reporter: devlink health reporter to destroy
7530 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7532 struct mutex *lock = &reporter->devlink->reporters_lock;
7535 __devlink_health_reporter_destroy(reporter);
7538 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
7541 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
7543 * @reporter: devlink health reporter to destroy
7546 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
7548 struct mutex *lock = &reporter->devlink_port->reporters_lock;
7551 __devlink_health_reporter_destroy(reporter);
7554 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
7557 devlink_nl_health_reporter_fill(struct sk_buff *msg,
7558 struct devlink_health_reporter *reporter,
7559 enum devlink_command cmd, u32 portid,
7562 struct devlink *devlink = reporter->devlink;
7563 struct nlattr *reporter_attr;
7566 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7570 if (devlink_nl_put_handle(msg, devlink))
7571 goto genlmsg_cancel;
7573 if (reporter->devlink_port) {
7574 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
7575 goto genlmsg_cancel;
7577 reporter_attr = nla_nest_start_noflag(msg,
7578 DEVLINK_ATTR_HEALTH_REPORTER);
7580 goto genlmsg_cancel;
7581 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
7582 reporter->ops->name))
7583 goto reporter_nest_cancel;
7584 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
7585 reporter->health_state))
7586 goto reporter_nest_cancel;
7587 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
7588 reporter->error_count, DEVLINK_ATTR_PAD))
7589 goto reporter_nest_cancel;
7590 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
7591 reporter->recovery_count, DEVLINK_ATTR_PAD))
7592 goto reporter_nest_cancel;
7593 if (reporter->ops->recover &&
7594 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
7595 reporter->graceful_period,
7597 goto reporter_nest_cancel;
7598 if (reporter->ops->recover &&
7599 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
7600 reporter->auto_recover))
7601 goto reporter_nest_cancel;
7602 if (reporter->dump_fmsg &&
7603 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
7604 jiffies_to_msecs(reporter->dump_ts),
7606 goto reporter_nest_cancel;
7607 if (reporter->dump_fmsg &&
7608 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
7609 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
7610 goto reporter_nest_cancel;
7611 if (reporter->ops->dump &&
7612 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
7613 reporter->auto_dump))
7614 goto reporter_nest_cancel;
7616 nla_nest_end(msg, reporter_attr);
7617 genlmsg_end(msg, hdr);
7620 reporter_nest_cancel:
7621 nla_nest_end(msg, reporter_attr);
7623 genlmsg_cancel(msg, hdr);
7627 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7628 enum devlink_command cmd)
7630 struct devlink *devlink = reporter->devlink;
7631 struct sk_buff *msg;
7634 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7635 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7641 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7647 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7648 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7652 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7654 reporter->recovery_count++;
7655 reporter->last_recovery_ts = jiffies;
7657 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7660 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7661 void *priv_ctx, struct netlink_ext_ack *extack)
7665 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7668 if (!reporter->ops->recover)
7671 err = reporter->ops->recover(reporter, priv_ctx, extack);
7675 devlink_health_reporter_recovery_done(reporter);
7676 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7677 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7683 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7685 if (!reporter->dump_fmsg)
7687 devlink_fmsg_free(reporter->dump_fmsg);
7688 reporter->dump_fmsg = NULL;
7691 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7693 struct netlink_ext_ack *extack)
7697 if (!reporter->ops->dump)
7700 if (reporter->dump_fmsg)
7703 reporter->dump_fmsg = devlink_fmsg_alloc();
7704 if (!reporter->dump_fmsg) {
7709 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7713 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7718 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7722 reporter->dump_ts = jiffies;
7723 reporter->dump_real_ts = ktime_get_real_ns();
7728 devlink_health_dump_clear(reporter);
7732 int devlink_health_report(struct devlink_health_reporter *reporter,
7733 const char *msg, void *priv_ctx)
7735 enum devlink_health_reporter_state prev_health_state;
7736 struct devlink *devlink = reporter->devlink;
7737 unsigned long recover_ts_threshold;
7740 /* write a log message of the current error */
7742 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7743 reporter->error_count++;
7744 prev_health_state = reporter->health_state;
7745 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7746 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7748 /* abort if the previous error wasn't recovered */
7749 recover_ts_threshold = reporter->last_recovery_ts +
7750 msecs_to_jiffies(reporter->graceful_period);
7751 if (reporter->auto_recover &&
7752 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7753 (reporter->last_recovery_ts && reporter->recovery_count &&
7754 time_is_after_jiffies(recover_ts_threshold)))) {
7755 trace_devlink_health_recover_aborted(devlink,
7756 reporter->ops->name,
7757 reporter->health_state,
7759 reporter->last_recovery_ts);
7763 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7765 if (reporter->auto_dump) {
7766 mutex_lock(&reporter->dump_lock);
7767 /* store current dump of current error, for later analysis */
7768 devlink_health_do_dump(reporter, priv_ctx, NULL);
7769 mutex_unlock(&reporter->dump_lock);
7772 if (!reporter->auto_recover)
7776 ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL);
7777 devl_unlock(devlink);
7781 EXPORT_SYMBOL_GPL(devlink_health_report);
7783 static struct devlink_health_reporter *
7784 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7785 struct nlattr **attrs)
7787 struct devlink_health_reporter *reporter;
7788 struct devlink_port *devlink_port;
7789 char *reporter_name;
7791 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7794 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7795 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7796 if (IS_ERR(devlink_port)) {
7797 mutex_lock(&devlink->reporters_lock);
7798 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7800 refcount_inc(&reporter->refcount);
7801 mutex_unlock(&devlink->reporters_lock);
7803 mutex_lock(&devlink_port->reporters_lock);
7804 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7806 refcount_inc(&reporter->refcount);
7807 mutex_unlock(&devlink_port->reporters_lock);
7813 static struct devlink_health_reporter *
7814 devlink_health_reporter_get_from_info(struct devlink *devlink,
7815 struct genl_info *info)
7817 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7820 static struct devlink_health_reporter *
7821 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7823 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7824 struct devlink_health_reporter *reporter;
7825 struct nlattr **attrs = info->attrs;
7826 struct devlink *devlink;
7828 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
7829 if (IS_ERR(devlink))
7832 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7833 devlink_put(devlink);
7838 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7839 enum devlink_health_reporter_state state)
7841 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7842 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7845 if (reporter->health_state == state)
7848 reporter->health_state = state;
7849 trace_devlink_health_reporter_state_update(reporter->devlink,
7850 reporter->ops->name, state);
7851 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7853 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7855 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7856 struct genl_info *info)
7858 struct devlink *devlink = info->user_ptr[0];
7859 struct devlink_health_reporter *reporter;
7860 struct sk_buff *msg;
7863 reporter = devlink_health_reporter_get_from_info(devlink, info);
7867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7873 err = devlink_nl_health_reporter_fill(msg, reporter,
7874 DEVLINK_CMD_HEALTH_REPORTER_GET,
7875 info->snd_portid, info->snd_seq,
7882 err = genlmsg_reply(msg, info);
7884 devlink_health_reporter_put(reporter);
7889 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7890 struct netlink_callback *cb)
7892 struct devlink_health_reporter *reporter;
7893 struct devlink_port *port;
7894 struct devlink *devlink;
7895 int start = cb->args[0];
7896 unsigned long index;
7900 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
7901 mutex_lock(&devlink->reporters_lock);
7902 list_for_each_entry(reporter, &devlink->reporter_list,
7908 err = devlink_nl_health_reporter_fill(
7909 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7910 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7913 mutex_unlock(&devlink->reporters_lock);
7914 devlink_put(devlink);
7919 mutex_unlock(&devlink->reporters_lock);
7920 devlink_put(devlink);
7923 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
7925 list_for_each_entry(port, &devlink->port_list, list) {
7926 mutex_lock(&port->reporters_lock);
7927 list_for_each_entry(reporter, &port->reporter_list, list) {
7932 err = devlink_nl_health_reporter_fill(
7934 DEVLINK_CMD_HEALTH_REPORTER_GET,
7935 NETLINK_CB(cb->skb).portid,
7936 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7938 mutex_unlock(&port->reporters_lock);
7939 devl_unlock(devlink);
7940 devlink_put(devlink);
7945 mutex_unlock(&port->reporters_lock);
7947 devl_unlock(devlink);
7948 devlink_put(devlink);
7956 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7957 struct genl_info *info)
7959 struct devlink *devlink = info->user_ptr[0];
7960 struct devlink_health_reporter *reporter;
7963 reporter = devlink_health_reporter_get_from_info(devlink, info);
7967 if (!reporter->ops->recover &&
7968 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
7969 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
7973 if (!reporter->ops->dump &&
7974 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
7979 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7980 reporter->graceful_period =
7981 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
7983 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7984 reporter->auto_recover =
7985 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
7987 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7988 reporter->auto_dump =
7989 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
7991 devlink_health_reporter_put(reporter);
7994 devlink_health_reporter_put(reporter);
7998 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
7999 struct genl_info *info)
8001 struct devlink *devlink = info->user_ptr[0];
8002 struct devlink_health_reporter *reporter;
8005 reporter = devlink_health_reporter_get_from_info(devlink, info);
8009 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
8011 devlink_health_reporter_put(reporter);
8015 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
8016 struct genl_info *info)
8018 struct devlink *devlink = info->user_ptr[0];
8019 struct devlink_health_reporter *reporter;
8020 struct devlink_fmsg *fmsg;
8023 reporter = devlink_health_reporter_get_from_info(devlink, info);
8027 if (!reporter->ops->diagnose) {
8028 devlink_health_reporter_put(reporter);
8032 fmsg = devlink_fmsg_alloc();
8034 devlink_health_reporter_put(reporter);
8038 err = devlink_fmsg_obj_nest_start(fmsg);
8042 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
8046 err = devlink_fmsg_obj_nest_end(fmsg);
8050 err = devlink_fmsg_snd(fmsg, info,
8051 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
8054 devlink_fmsg_free(fmsg);
8055 devlink_health_reporter_put(reporter);
8060 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
8061 struct netlink_callback *cb)
8063 struct devlink_health_reporter *reporter;
8064 u64 start = cb->args[0];
8067 reporter = devlink_health_reporter_get_from_cb(cb);
8071 if (!reporter->ops->dump) {
8075 mutex_lock(&reporter->dump_lock);
8077 err = devlink_health_do_dump(reporter, NULL, cb->extack);
8080 cb->args[1] = reporter->dump_ts;
8082 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
8083 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
8088 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
8089 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
8091 mutex_unlock(&reporter->dump_lock);
8093 devlink_health_reporter_put(reporter);
8098 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
8099 struct genl_info *info)
8101 struct devlink *devlink = info->user_ptr[0];
8102 struct devlink_health_reporter *reporter;
8104 reporter = devlink_health_reporter_get_from_info(devlink, info);
8108 if (!reporter->ops->dump) {
8109 devlink_health_reporter_put(reporter);
8113 mutex_lock(&reporter->dump_lock);
8114 devlink_health_dump_clear(reporter);
8115 mutex_unlock(&reporter->dump_lock);
8116 devlink_health_reporter_put(reporter);
8120 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
8121 struct genl_info *info)
8123 struct devlink *devlink = info->user_ptr[0];
8124 struct devlink_health_reporter *reporter;
8127 reporter = devlink_health_reporter_get_from_info(devlink, info);
8131 if (!reporter->ops->test) {
8132 devlink_health_reporter_put(reporter);
8136 err = reporter->ops->test(reporter, info->extack);
8138 devlink_health_reporter_put(reporter);
8142 struct devlink_stats {
8143 u64_stats_t rx_bytes;
8144 u64_stats_t rx_packets;
8145 struct u64_stats_sync syncp;
8149 * struct devlink_trap_policer_item - Packet trap policer attributes.
8150 * @policer: Immutable packet trap policer attributes.
8151 * @rate: Rate in packets / sec.
8152 * @burst: Burst size in packets.
8153 * @list: trap_policer_list member.
8155 * Describes packet trap policer attributes. Created by devlink during trap
8156 * policer registration.
8158 struct devlink_trap_policer_item {
8159 const struct devlink_trap_policer *policer;
8162 struct list_head list;
8166 * struct devlink_trap_group_item - Packet trap group attributes.
8167 * @group: Immutable packet trap group attributes.
8168 * @policer_item: Associated policer item. Can be NULL.
8169 * @list: trap_group_list member.
8170 * @stats: Trap group statistics.
8172 * Describes packet trap group attributes. Created by devlink during trap
8173 * group registration.
8175 struct devlink_trap_group_item {
8176 const struct devlink_trap_group *group;
8177 struct devlink_trap_policer_item *policer_item;
8178 struct list_head list;
8179 struct devlink_stats __percpu *stats;
8183 * struct devlink_trap_item - Packet trap attributes.
8184 * @trap: Immutable packet trap attributes.
8185 * @group_item: Associated group item.
8186 * @list: trap_list member.
8187 * @action: Trap action.
8188 * @stats: Trap statistics.
8189 * @priv: Driver private information.
8191 * Describes both mutable and immutable packet trap attributes. Created by
8192 * devlink during trap registration and used for all trap related operations.
8194 struct devlink_trap_item {
8195 const struct devlink_trap *trap;
8196 struct devlink_trap_group_item *group_item;
8197 struct list_head list;
8198 enum devlink_trap_action action;
8199 struct devlink_stats __percpu *stats;
8203 static struct devlink_trap_policer_item *
8204 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
8206 struct devlink_trap_policer_item *policer_item;
8208 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8209 if (policer_item->policer->id == id)
8210 return policer_item;
8216 static struct devlink_trap_item *
8217 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
8219 struct devlink_trap_item *trap_item;
8221 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8222 if (!strcmp(trap_item->trap->name, name))
8229 static struct devlink_trap_item *
8230 devlink_trap_item_get_from_info(struct devlink *devlink,
8231 struct genl_info *info)
8233 struct nlattr *attr;
8235 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
8237 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
8239 return devlink_trap_item_lookup(devlink, nla_data(attr));
8243 devlink_trap_action_get_from_info(struct genl_info *info,
8244 enum devlink_trap_action *p_trap_action)
8248 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
8250 case DEVLINK_TRAP_ACTION_DROP:
8251 case DEVLINK_TRAP_ACTION_TRAP:
8252 case DEVLINK_TRAP_ACTION_MIRROR:
8253 *p_trap_action = val;
8262 static int devlink_trap_metadata_put(struct sk_buff *msg,
8263 const struct devlink_trap *trap)
8265 struct nlattr *attr;
8267 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
8271 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
8272 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
8273 goto nla_put_failure;
8274 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
8275 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
8276 goto nla_put_failure;
8278 nla_nest_end(msg, attr);
8283 nla_nest_cancel(msg, attr);
8287 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
8288 struct devlink_stats *stats)
8292 memset(stats, 0, sizeof(*stats));
8293 for_each_possible_cpu(i) {
8294 struct devlink_stats *cpu_stats;
8295 u64 rx_packets, rx_bytes;
8298 cpu_stats = per_cpu_ptr(trap_stats, i);
8300 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
8301 rx_packets = u64_stats_read(&cpu_stats->rx_packets);
8302 rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
8303 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
8305 u64_stats_add(&stats->rx_packets, rx_packets);
8306 u64_stats_add(&stats->rx_bytes, rx_bytes);
8311 devlink_trap_group_stats_put(struct sk_buff *msg,
8312 struct devlink_stats __percpu *trap_stats)
8314 struct devlink_stats stats;
8315 struct nlattr *attr;
8317 devlink_trap_stats_read(trap_stats, &stats);
8319 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8323 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8324 u64_stats_read(&stats.rx_packets),
8326 goto nla_put_failure;
8328 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8329 u64_stats_read(&stats.rx_bytes),
8331 goto nla_put_failure;
8333 nla_nest_end(msg, attr);
8338 nla_nest_cancel(msg, attr);
8342 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8343 const struct devlink_trap_item *trap_item)
8345 struct devlink_stats stats;
8346 struct nlattr *attr;
8350 if (devlink->ops->trap_drop_counter_get) {
8351 err = devlink->ops->trap_drop_counter_get(devlink,
8358 devlink_trap_stats_read(trap_item->stats, &stats);
8360 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8364 if (devlink->ops->trap_drop_counter_get &&
8365 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8367 goto nla_put_failure;
8369 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8370 u64_stats_read(&stats.rx_packets),
8372 goto nla_put_failure;
8374 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8375 u64_stats_read(&stats.rx_bytes),
8377 goto nla_put_failure;
8379 nla_nest_end(msg, attr);
8384 nla_nest_cancel(msg, attr);
8388 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8389 const struct devlink_trap_item *trap_item,
8390 enum devlink_command cmd, u32 portid, u32 seq,
8393 struct devlink_trap_group_item *group_item = trap_item->group_item;
8397 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8401 if (devlink_nl_put_handle(msg, devlink))
8402 goto nla_put_failure;
8404 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8405 group_item->group->name))
8406 goto nla_put_failure;
8408 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8409 goto nla_put_failure;
8411 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8412 goto nla_put_failure;
8414 if (trap_item->trap->generic &&
8415 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8416 goto nla_put_failure;
8418 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8419 goto nla_put_failure;
8421 err = devlink_trap_metadata_put(msg, trap_item->trap);
8423 goto nla_put_failure;
8425 err = devlink_trap_stats_put(msg, devlink, trap_item);
8427 goto nla_put_failure;
8429 genlmsg_end(msg, hdr);
8434 genlmsg_cancel(msg, hdr);
8438 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8439 struct genl_info *info)
8441 struct netlink_ext_ack *extack = info->extack;
8442 struct devlink *devlink = info->user_ptr[0];
8443 struct devlink_trap_item *trap_item;
8444 struct sk_buff *msg;
8447 if (list_empty(&devlink->trap_list))
8450 trap_item = devlink_trap_item_get_from_info(devlink, info);
8452 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8460 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8461 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8466 return genlmsg_reply(msg, info);
8473 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
8474 struct netlink_callback *cb)
8476 struct devlink_trap_item *trap_item;
8477 struct devlink *devlink;
8478 int start = cb->args[0];
8479 unsigned long index;
8483 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
8485 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8490 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8491 DEVLINK_CMD_TRAP_NEW,
8492 NETLINK_CB(cb->skb).portid,
8496 devl_unlock(devlink);
8497 devlink_put(devlink);
8502 devl_unlock(devlink);
8503 devlink_put(devlink);
8510 static int __devlink_trap_action_set(struct devlink *devlink,
8511 struct devlink_trap_item *trap_item,
8512 enum devlink_trap_action trap_action,
8513 struct netlink_ext_ack *extack)
8517 if (trap_item->action != trap_action &&
8518 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8519 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8523 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8524 trap_action, extack);
8528 trap_item->action = trap_action;
8533 static int devlink_trap_action_set(struct devlink *devlink,
8534 struct devlink_trap_item *trap_item,
8535 struct genl_info *info)
8537 enum devlink_trap_action trap_action;
8540 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8543 err = devlink_trap_action_get_from_info(info, &trap_action);
8545 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8549 return __devlink_trap_action_set(devlink, trap_item, trap_action,
8553 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8554 struct genl_info *info)
8556 struct netlink_ext_ack *extack = info->extack;
8557 struct devlink *devlink = info->user_ptr[0];
8558 struct devlink_trap_item *trap_item;
8560 if (list_empty(&devlink->trap_list))
8563 trap_item = devlink_trap_item_get_from_info(devlink, info);
8565 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8569 return devlink_trap_action_set(devlink, trap_item, info);
8572 static struct devlink_trap_group_item *
8573 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8575 struct devlink_trap_group_item *group_item;
8577 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8578 if (!strcmp(group_item->group->name, name))
8585 static struct devlink_trap_group_item *
8586 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8588 struct devlink_trap_group_item *group_item;
8590 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8591 if (group_item->group->id == id)
8598 static struct devlink_trap_group_item *
8599 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8600 struct genl_info *info)
8604 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8606 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8608 return devlink_trap_group_item_lookup(devlink, name);
8612 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8613 const struct devlink_trap_group_item *group_item,
8614 enum devlink_command cmd, u32 portid, u32 seq,
8620 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8624 if (devlink_nl_put_handle(msg, devlink))
8625 goto nla_put_failure;
8627 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8628 group_item->group->name))
8629 goto nla_put_failure;
8631 if (group_item->group->generic &&
8632 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8633 goto nla_put_failure;
8635 if (group_item->policer_item &&
8636 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8637 group_item->policer_item->policer->id))
8638 goto nla_put_failure;
8640 err = devlink_trap_group_stats_put(msg, group_item->stats);
8642 goto nla_put_failure;
8644 genlmsg_end(msg, hdr);
8649 genlmsg_cancel(msg, hdr);
8653 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8654 struct genl_info *info)
8656 struct netlink_ext_ack *extack = info->extack;
8657 struct devlink *devlink = info->user_ptr[0];
8658 struct devlink_trap_group_item *group_item;
8659 struct sk_buff *msg;
8662 if (list_empty(&devlink->trap_group_list))
8665 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8667 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8675 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8676 DEVLINK_CMD_TRAP_GROUP_NEW,
8677 info->snd_portid, info->snd_seq, 0);
8679 goto err_trap_group_fill;
8681 return genlmsg_reply(msg, info);
8683 err_trap_group_fill:
8688 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8689 struct netlink_callback *cb)
8691 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8692 struct devlink_trap_group_item *group_item;
8693 u32 portid = NETLINK_CB(cb->skb).portid;
8694 struct devlink *devlink;
8695 int start = cb->args[0];
8696 unsigned long index;
8700 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
8702 list_for_each_entry(group_item, &devlink->trap_group_list,
8708 err = devlink_nl_trap_group_fill(msg, devlink,
8714 devl_unlock(devlink);
8715 devlink_put(devlink);
8720 devl_unlock(devlink);
8721 devlink_put(devlink);
8729 __devlink_trap_group_action_set(struct devlink *devlink,
8730 struct devlink_trap_group_item *group_item,
8731 enum devlink_trap_action trap_action,
8732 struct netlink_ext_ack *extack)
8734 const char *group_name = group_item->group->name;
8735 struct devlink_trap_item *trap_item;
8738 if (devlink->ops->trap_group_action_set) {
8739 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8740 trap_action, extack);
8744 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8745 if (strcmp(trap_item->group_item->group->name, group_name))
8747 if (trap_item->action != trap_action &&
8748 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8750 trap_item->action = trap_action;
8756 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8757 if (strcmp(trap_item->group_item->group->name, group_name))
8759 err = __devlink_trap_action_set(devlink, trap_item,
8760 trap_action, extack);
8769 devlink_trap_group_action_set(struct devlink *devlink,
8770 struct devlink_trap_group_item *group_item,
8771 struct genl_info *info, bool *p_modified)
8773 enum devlink_trap_action trap_action;
8776 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8779 err = devlink_trap_action_get_from_info(info, &trap_action);
8781 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8785 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8795 static int devlink_trap_group_set(struct devlink *devlink,
8796 struct devlink_trap_group_item *group_item,
8797 struct genl_info *info)
8799 struct devlink_trap_policer_item *policer_item;
8800 struct netlink_ext_ack *extack = info->extack;
8801 const struct devlink_trap_policer *policer;
8802 struct nlattr **attrs = info->attrs;
8805 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8808 if (!devlink->ops->trap_group_set)
8811 policer_item = group_item->policer_item;
8812 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8815 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8816 policer_item = devlink_trap_policer_item_lookup(devlink,
8818 if (policer_id && !policer_item) {
8819 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8823 policer = policer_item ? policer_item->policer : NULL;
8825 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8830 group_item->policer_item = policer_item;
8835 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8836 struct genl_info *info)
8838 struct netlink_ext_ack *extack = info->extack;
8839 struct devlink *devlink = info->user_ptr[0];
8840 struct devlink_trap_group_item *group_item;
8841 bool modified = false;
8844 if (list_empty(&devlink->trap_group_list))
8847 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8849 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8853 err = devlink_trap_group_action_set(devlink, group_item, info,
8858 err = devlink_trap_group_set(devlink, group_item, info);
8860 goto err_trap_group_set;
8866 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8870 static struct devlink_trap_policer_item *
8871 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8872 struct genl_info *info)
8876 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8878 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8880 return devlink_trap_policer_item_lookup(devlink, id);
8884 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8885 const struct devlink_trap_policer *policer)
8887 struct nlattr *attr;
8891 if (!devlink->ops->trap_policer_counter_get)
8894 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8898 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8902 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8904 goto nla_put_failure;
8906 nla_nest_end(msg, attr);
8911 nla_nest_cancel(msg, attr);
8916 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8917 const struct devlink_trap_policer_item *policer_item,
8918 enum devlink_command cmd, u32 portid, u32 seq,
8924 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8928 if (devlink_nl_put_handle(msg, devlink))
8929 goto nla_put_failure;
8931 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8932 policer_item->policer->id))
8933 goto nla_put_failure;
8935 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8936 policer_item->rate, DEVLINK_ATTR_PAD))
8937 goto nla_put_failure;
8939 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8940 policer_item->burst, DEVLINK_ATTR_PAD))
8941 goto nla_put_failure;
8943 err = devlink_trap_policer_stats_put(msg, devlink,
8944 policer_item->policer);
8946 goto nla_put_failure;
8948 genlmsg_end(msg, hdr);
8953 genlmsg_cancel(msg, hdr);
8957 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8958 struct genl_info *info)
8960 struct devlink_trap_policer_item *policer_item;
8961 struct netlink_ext_ack *extack = info->extack;
8962 struct devlink *devlink = info->user_ptr[0];
8963 struct sk_buff *msg;
8966 if (list_empty(&devlink->trap_policer_list))
8969 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8970 if (!policer_item) {
8971 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8975 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8979 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8980 DEVLINK_CMD_TRAP_POLICER_NEW,
8981 info->snd_portid, info->snd_seq, 0);
8983 goto err_trap_policer_fill;
8985 return genlmsg_reply(msg, info);
8987 err_trap_policer_fill:
8992 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
8993 struct netlink_callback *cb)
8995 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
8996 struct devlink_trap_policer_item *policer_item;
8997 u32 portid = NETLINK_CB(cb->skb).portid;
8998 struct devlink *devlink;
8999 int start = cb->args[0];
9000 unsigned long index;
9004 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
9006 list_for_each_entry(policer_item, &devlink->trap_policer_list,
9012 err = devlink_nl_trap_policer_fill(msg, devlink,
9018 devl_unlock(devlink);
9019 devlink_put(devlink);
9024 devl_unlock(devlink);
9025 devlink_put(devlink);
9033 devlink_trap_policer_set(struct devlink *devlink,
9034 struct devlink_trap_policer_item *policer_item,
9035 struct genl_info *info)
9037 struct netlink_ext_ack *extack = info->extack;
9038 struct nlattr **attrs = info->attrs;
9042 rate = policer_item->rate;
9043 burst = policer_item->burst;
9045 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
9046 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
9048 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
9049 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
9051 if (rate < policer_item->policer->min_rate) {
9052 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
9056 if (rate > policer_item->policer->max_rate) {
9057 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
9061 if (burst < policer_item->policer->min_burst) {
9062 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
9066 if (burst > policer_item->policer->max_burst) {
9067 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
9071 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
9072 rate, burst, info->extack);
9076 policer_item->rate = rate;
9077 policer_item->burst = burst;
9082 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
9083 struct genl_info *info)
9085 struct devlink_trap_policer_item *policer_item;
9086 struct netlink_ext_ack *extack = info->extack;
9087 struct devlink *devlink = info->user_ptr[0];
9089 if (list_empty(&devlink->trap_policer_list))
9092 if (!devlink->ops->trap_policer_set)
9095 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
9096 if (!policer_item) {
9097 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
9101 return devlink_trap_policer_set(devlink, policer_item, info);
9104 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
9105 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
9106 DEVLINK_ATTR_TRAP_POLICER_ID },
9107 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
9108 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
9109 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
9110 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
9111 DEVLINK_PORT_TYPE_IB),
9112 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
9113 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
9114 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
9115 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
9116 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
9117 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
9118 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
9119 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
9120 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
9121 DEVLINK_ESWITCH_MODE_SWITCHDEV),
9122 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
9123 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
9124 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
9125 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
9126 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
9127 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
9128 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
9129 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
9130 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
9131 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
9132 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
9133 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
9134 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
9135 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
9136 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
9137 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
9138 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
9139 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
9140 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
9141 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
9142 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
9143 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
9144 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
9145 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
9146 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
9147 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
9148 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
9149 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
9150 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
9151 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
9152 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
9153 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
9154 DEVLINK_RELOAD_ACTION_MAX),
9155 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
9156 [DEVLINK_ATTR_PORT_FLAVOUR] = { .type = NLA_U16 },
9157 [DEVLINK_ATTR_PORT_PCI_PF_NUMBER] = { .type = NLA_U16 },
9158 [DEVLINK_ATTR_PORT_PCI_SF_NUMBER] = { .type = NLA_U32 },
9159 [DEVLINK_ATTR_PORT_CONTROLLER_NUMBER] = { .type = NLA_U32 },
9160 [DEVLINK_ATTR_RATE_TYPE] = { .type = NLA_U16 },
9161 [DEVLINK_ATTR_RATE_TX_SHARE] = { .type = NLA_U64 },
9162 [DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 },
9163 [DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING },
9164 [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING },
9165 [DEVLINK_ATTR_LINECARD_INDEX] = { .type = NLA_U32 },
9166 [DEVLINK_ATTR_LINECARD_TYPE] = { .type = NLA_NUL_STRING },
9167 [DEVLINK_ATTR_SELFTESTS] = { .type = NLA_NESTED },
9170 static const struct genl_small_ops devlink_nl_ops[] = {
9172 .cmd = DEVLINK_CMD_GET,
9173 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9174 .doit = devlink_nl_cmd_get_doit,
9175 .dumpit = devlink_nl_cmd_get_dumpit,
9176 /* can be retrieved by unprivileged users */
9179 .cmd = DEVLINK_CMD_PORT_GET,
9180 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9181 .doit = devlink_nl_cmd_port_get_doit,
9182 .dumpit = devlink_nl_cmd_port_get_dumpit,
9183 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9184 /* can be retrieved by unprivileged users */
9187 .cmd = DEVLINK_CMD_PORT_SET,
9188 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9189 .doit = devlink_nl_cmd_port_set_doit,
9190 .flags = GENL_ADMIN_PERM,
9191 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9194 .cmd = DEVLINK_CMD_RATE_GET,
9195 .doit = devlink_nl_cmd_rate_get_doit,
9196 .dumpit = devlink_nl_cmd_rate_get_dumpit,
9197 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9198 /* can be retrieved by unprivileged users */
9201 .cmd = DEVLINK_CMD_RATE_SET,
9202 .doit = devlink_nl_cmd_rate_set_doit,
9203 .flags = GENL_ADMIN_PERM,
9204 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9207 .cmd = DEVLINK_CMD_RATE_NEW,
9208 .doit = devlink_nl_cmd_rate_new_doit,
9209 .flags = GENL_ADMIN_PERM,
9212 .cmd = DEVLINK_CMD_RATE_DEL,
9213 .doit = devlink_nl_cmd_rate_del_doit,
9214 .flags = GENL_ADMIN_PERM,
9215 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9218 .cmd = DEVLINK_CMD_PORT_SPLIT,
9219 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9220 .doit = devlink_nl_cmd_port_split_doit,
9221 .flags = GENL_ADMIN_PERM,
9222 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9225 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9226 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9227 .doit = devlink_nl_cmd_port_unsplit_doit,
9228 .flags = GENL_ADMIN_PERM,
9229 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9232 .cmd = DEVLINK_CMD_PORT_NEW,
9233 .doit = devlink_nl_cmd_port_new_doit,
9234 .flags = GENL_ADMIN_PERM,
9237 .cmd = DEVLINK_CMD_PORT_DEL,
9238 .doit = devlink_nl_cmd_port_del_doit,
9239 .flags = GENL_ADMIN_PERM,
9242 .cmd = DEVLINK_CMD_LINECARD_GET,
9243 .doit = devlink_nl_cmd_linecard_get_doit,
9244 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9245 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9246 /* can be retrieved by unprivileged users */
9249 .cmd = DEVLINK_CMD_LINECARD_SET,
9250 .doit = devlink_nl_cmd_linecard_set_doit,
9251 .flags = GENL_ADMIN_PERM,
9252 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9255 .cmd = DEVLINK_CMD_SB_GET,
9256 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9257 .doit = devlink_nl_cmd_sb_get_doit,
9258 .dumpit = devlink_nl_cmd_sb_get_dumpit,
9259 /* can be retrieved by unprivileged users */
9262 .cmd = DEVLINK_CMD_SB_POOL_GET,
9263 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9264 .doit = devlink_nl_cmd_sb_pool_get_doit,
9265 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
9266 /* can be retrieved by unprivileged users */
9269 .cmd = DEVLINK_CMD_SB_POOL_SET,
9270 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9271 .doit = devlink_nl_cmd_sb_pool_set_doit,
9272 .flags = GENL_ADMIN_PERM,
9275 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9276 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9277 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9278 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
9279 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9280 /* can be retrieved by unprivileged users */
9283 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9284 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9285 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9286 .flags = GENL_ADMIN_PERM,
9287 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9290 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9291 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9292 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9293 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
9294 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9295 /* can be retrieved by unprivileged users */
9298 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9299 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9300 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9301 .flags = GENL_ADMIN_PERM,
9302 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9305 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9306 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9307 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9308 .flags = GENL_ADMIN_PERM,
9311 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9312 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9313 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9314 .flags = GENL_ADMIN_PERM,
9317 .cmd = DEVLINK_CMD_ESWITCH_GET,
9318 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9319 .doit = devlink_nl_cmd_eswitch_get_doit,
9320 .flags = GENL_ADMIN_PERM,
9323 .cmd = DEVLINK_CMD_ESWITCH_SET,
9324 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9325 .doit = devlink_nl_cmd_eswitch_set_doit,
9326 .flags = GENL_ADMIN_PERM,
9329 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9330 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9331 .doit = devlink_nl_cmd_dpipe_table_get,
9332 /* can be retrieved by unprivileged users */
9335 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9336 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9337 .doit = devlink_nl_cmd_dpipe_entries_get,
9338 /* can be retrieved by unprivileged users */
9341 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9342 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9343 .doit = devlink_nl_cmd_dpipe_headers_get,
9344 /* can be retrieved by unprivileged users */
9347 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9348 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9349 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9350 .flags = GENL_ADMIN_PERM,
9353 .cmd = DEVLINK_CMD_RESOURCE_SET,
9354 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9355 .doit = devlink_nl_cmd_resource_set,
9356 .flags = GENL_ADMIN_PERM,
9359 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9360 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9361 .doit = devlink_nl_cmd_resource_dump,
9362 /* can be retrieved by unprivileged users */
9365 .cmd = DEVLINK_CMD_RELOAD,
9366 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9367 .doit = devlink_nl_cmd_reload,
9368 .flags = GENL_ADMIN_PERM,
9371 .cmd = DEVLINK_CMD_PARAM_GET,
9372 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9373 .doit = devlink_nl_cmd_param_get_doit,
9374 .dumpit = devlink_nl_cmd_param_get_dumpit,
9375 /* can be retrieved by unprivileged users */
9378 .cmd = DEVLINK_CMD_PARAM_SET,
9379 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9380 .doit = devlink_nl_cmd_param_set_doit,
9381 .flags = GENL_ADMIN_PERM,
9384 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9385 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9386 .doit = devlink_nl_cmd_port_param_get_doit,
9387 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9388 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9389 /* can be retrieved by unprivileged users */
9392 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9393 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9394 .doit = devlink_nl_cmd_port_param_set_doit,
9395 .flags = GENL_ADMIN_PERM,
9396 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9399 .cmd = DEVLINK_CMD_REGION_GET,
9400 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9401 .doit = devlink_nl_cmd_region_get_doit,
9402 .dumpit = devlink_nl_cmd_region_get_dumpit,
9403 .flags = GENL_ADMIN_PERM,
9406 .cmd = DEVLINK_CMD_REGION_NEW,
9407 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9408 .doit = devlink_nl_cmd_region_new,
9409 .flags = GENL_ADMIN_PERM,
9412 .cmd = DEVLINK_CMD_REGION_DEL,
9413 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9414 .doit = devlink_nl_cmd_region_del,
9415 .flags = GENL_ADMIN_PERM,
9418 .cmd = DEVLINK_CMD_REGION_READ,
9419 .validate = GENL_DONT_VALIDATE_STRICT |
9420 GENL_DONT_VALIDATE_DUMP_STRICT,
9421 .dumpit = devlink_nl_cmd_region_read_dumpit,
9422 .flags = GENL_ADMIN_PERM,
9425 .cmd = DEVLINK_CMD_INFO_GET,
9426 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9427 .doit = devlink_nl_cmd_info_get_doit,
9428 .dumpit = devlink_nl_cmd_info_get_dumpit,
9429 /* can be retrieved by unprivileged users */
9432 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9433 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9434 .doit = devlink_nl_cmd_health_reporter_get_doit,
9435 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9436 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9437 /* can be retrieved by unprivileged users */
9440 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9441 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9442 .doit = devlink_nl_cmd_health_reporter_set_doit,
9443 .flags = GENL_ADMIN_PERM,
9444 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9447 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9448 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9449 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9450 .flags = GENL_ADMIN_PERM,
9451 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9454 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9455 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9456 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9457 .flags = GENL_ADMIN_PERM,
9458 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9461 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9462 .validate = GENL_DONT_VALIDATE_STRICT |
9463 GENL_DONT_VALIDATE_DUMP_STRICT,
9464 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9465 .flags = GENL_ADMIN_PERM,
9468 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9469 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9470 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9471 .flags = GENL_ADMIN_PERM,
9472 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9475 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9476 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9477 .doit = devlink_nl_cmd_health_reporter_test_doit,
9478 .flags = GENL_ADMIN_PERM,
9479 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9482 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9483 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9484 .doit = devlink_nl_cmd_flash_update,
9485 .flags = GENL_ADMIN_PERM,
9488 .cmd = DEVLINK_CMD_TRAP_GET,
9489 .doit = devlink_nl_cmd_trap_get_doit,
9490 .dumpit = devlink_nl_cmd_trap_get_dumpit,
9491 /* can be retrieved by unprivileged users */
9494 .cmd = DEVLINK_CMD_TRAP_SET,
9495 .doit = devlink_nl_cmd_trap_set_doit,
9496 .flags = GENL_ADMIN_PERM,
9499 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9500 .doit = devlink_nl_cmd_trap_group_get_doit,
9501 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
9502 /* can be retrieved by unprivileged users */
9505 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9506 .doit = devlink_nl_cmd_trap_group_set_doit,
9507 .flags = GENL_ADMIN_PERM,
9510 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9511 .doit = devlink_nl_cmd_trap_policer_get_doit,
9512 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
9513 /* can be retrieved by unprivileged users */
9516 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9517 .doit = devlink_nl_cmd_trap_policer_set_doit,
9518 .flags = GENL_ADMIN_PERM,
9521 .cmd = DEVLINK_CMD_SELFTESTS_GET,
9522 .doit = devlink_nl_cmd_selftests_get_doit,
9523 .dumpit = devlink_nl_cmd_selftests_get_dumpit
9524 /* can be retrieved by unprivileged users */
9527 .cmd = DEVLINK_CMD_SELFTESTS_RUN,
9528 .doit = devlink_nl_cmd_selftests_run,
9529 .flags = GENL_ADMIN_PERM,
9533 static struct genl_family devlink_nl_family __ro_after_init = {
9534 .name = DEVLINK_GENL_NAME,
9535 .version = DEVLINK_GENL_VERSION,
9536 .maxattr = DEVLINK_ATTR_MAX,
9537 .policy = devlink_nl_policy,
9539 .parallel_ops = true,
9540 .pre_doit = devlink_nl_pre_doit,
9541 .post_doit = devlink_nl_post_doit,
9542 .module = THIS_MODULE,
9543 .small_ops = devlink_nl_ops,
9544 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
9545 .resv_start_op = DEVLINK_CMD_SELFTESTS_RUN + 1,
9546 .mcgrps = devlink_nl_mcgrps,
9547 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
9550 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9552 const struct devlink_reload_combination *comb;
9555 if (!devlink_reload_supported(ops)) {
9556 if (WARN_ON(ops->reload_actions))
9561 if (WARN_ON(!ops->reload_actions ||
9562 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9563 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9566 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9567 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9570 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
9571 comb = &devlink_reload_invalid_combinations[i];
9572 if (ops->reload_actions == BIT(comb->action) &&
9573 ops->reload_limits == BIT(comb->limit))
9580 * devlink_set_features - Set devlink supported features
9583 * @features: devlink support features
9585 * This interface allows us to set reload ops separatelly from
9586 * the devlink_alloc.
9588 void devlink_set_features(struct devlink *devlink, u64 features)
9590 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9592 WARN_ON(features & DEVLINK_F_RELOAD &&
9593 !devlink_reload_supported(devlink->ops));
9594 devlink->features = features;
9596 EXPORT_SYMBOL_GPL(devlink_set_features);
9599 * devlink_alloc_ns - Allocate new devlink instance resources
9600 * in specific namespace
9603 * @priv_size: size of user private data
9604 * @net: net namespace
9605 * @dev: parent device
9607 * Allocate new devlink instance resources, including devlink index
9610 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
9611 size_t priv_size, struct net *net,
9614 struct devlink *devlink;
9618 WARN_ON(!ops || !dev);
9619 if (!devlink_reload_actions_valid(ops))
9622 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
9626 ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
9627 &last_id, GFP_KERNEL);
9635 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
9636 write_pnet(&devlink->_net, net);
9637 INIT_LIST_HEAD(&devlink->port_list);
9638 INIT_LIST_HEAD(&devlink->rate_list);
9639 INIT_LIST_HEAD(&devlink->linecard_list);
9640 INIT_LIST_HEAD(&devlink->sb_list);
9641 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
9642 INIT_LIST_HEAD(&devlink->resource_list);
9643 INIT_LIST_HEAD(&devlink->param_list);
9644 INIT_LIST_HEAD(&devlink->region_list);
9645 INIT_LIST_HEAD(&devlink->reporter_list);
9646 INIT_LIST_HEAD(&devlink->trap_list);
9647 INIT_LIST_HEAD(&devlink->trap_group_list);
9648 INIT_LIST_HEAD(&devlink->trap_policer_list);
9649 lockdep_register_key(&devlink->lock_key);
9650 mutex_init(&devlink->lock);
9651 lockdep_set_class(&devlink->lock, &devlink->lock_key);
9652 mutex_init(&devlink->reporters_lock);
9653 mutex_init(&devlink->linecards_lock);
9654 refcount_set(&devlink->refcount, 1);
9655 init_completion(&devlink->comp);
9659 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
9662 devlink_trap_policer_notify(struct devlink *devlink,
9663 const struct devlink_trap_policer_item *policer_item,
9664 enum devlink_command cmd);
9666 devlink_trap_group_notify(struct devlink *devlink,
9667 const struct devlink_trap_group_item *group_item,
9668 enum devlink_command cmd);
9669 static void devlink_trap_notify(struct devlink *devlink,
9670 const struct devlink_trap_item *trap_item,
9671 enum devlink_command cmd);
9673 static void devlink_notify_register(struct devlink *devlink)
9675 struct devlink_trap_policer_item *policer_item;
9676 struct devlink_trap_group_item *group_item;
9677 struct devlink_param_item *param_item;
9678 struct devlink_trap_item *trap_item;
9679 struct devlink_port *devlink_port;
9680 struct devlink_linecard *linecard;
9681 struct devlink_rate *rate_node;
9682 struct devlink_region *region;
9684 devlink_notify(devlink, DEVLINK_CMD_NEW);
9685 list_for_each_entry(linecard, &devlink->linecard_list, list)
9686 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9688 list_for_each_entry(devlink_port, &devlink->port_list, list)
9689 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9691 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9692 devlink_trap_policer_notify(devlink, policer_item,
9693 DEVLINK_CMD_TRAP_POLICER_NEW);
9695 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9696 devlink_trap_group_notify(devlink, group_item,
9697 DEVLINK_CMD_TRAP_GROUP_NEW);
9699 list_for_each_entry(trap_item, &devlink->trap_list, list)
9700 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9702 list_for_each_entry(rate_node, &devlink->rate_list, list)
9703 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9705 list_for_each_entry(region, &devlink->region_list, list)
9706 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9708 list_for_each_entry(param_item, &devlink->param_list, list)
9709 devlink_param_notify(devlink, 0, param_item,
9710 DEVLINK_CMD_PARAM_NEW);
9713 static void devlink_notify_unregister(struct devlink *devlink)
9715 struct devlink_trap_policer_item *policer_item;
9716 struct devlink_trap_group_item *group_item;
9717 struct devlink_param_item *param_item;
9718 struct devlink_trap_item *trap_item;
9719 struct devlink_port *devlink_port;
9720 struct devlink_rate *rate_node;
9721 struct devlink_region *region;
9723 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9724 devlink_param_notify(devlink, 0, param_item,
9725 DEVLINK_CMD_PARAM_DEL);
9727 list_for_each_entry_reverse(region, &devlink->region_list, list)
9728 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9730 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9731 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9733 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9734 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9736 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9737 devlink_trap_group_notify(devlink, group_item,
9738 DEVLINK_CMD_TRAP_GROUP_DEL);
9739 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9741 devlink_trap_policer_notify(devlink, policer_item,
9742 DEVLINK_CMD_TRAP_POLICER_DEL);
9744 list_for_each_entry_reverse(devlink_port, &devlink->port_list, list)
9745 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9746 devlink_notify(devlink, DEVLINK_CMD_DEL);
9750 * devlink_register - Register devlink instance
9754 void devlink_register(struct devlink *devlink)
9756 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9757 /* Make sure that we are in .probe() routine */
9759 xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9760 devlink_notify_register(devlink);
9762 EXPORT_SYMBOL_GPL(devlink_register);
9765 * devlink_unregister - Unregister devlink instance
9769 void devlink_unregister(struct devlink *devlink)
9771 ASSERT_DEVLINK_REGISTERED(devlink);
9772 /* Make sure that we are in .remove() routine */
9774 xa_set_mark(&devlinks, devlink->index, DEVLINK_UNREGISTERING);
9775 devlink_put(devlink);
9776 wait_for_completion(&devlink->comp);
9778 devlink_notify_unregister(devlink);
9779 xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9780 xa_clear_mark(&devlinks, devlink->index, DEVLINK_UNREGISTERING);
9782 EXPORT_SYMBOL_GPL(devlink_unregister);
9785 * devlink_free - Free devlink instance resources
9789 void devlink_free(struct devlink *devlink)
9791 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9793 mutex_destroy(&devlink->linecards_lock);
9794 mutex_destroy(&devlink->reporters_lock);
9795 mutex_destroy(&devlink->lock);
9796 lockdep_unregister_key(&devlink->lock_key);
9797 WARN_ON(!list_empty(&devlink->trap_policer_list));
9798 WARN_ON(!list_empty(&devlink->trap_group_list));
9799 WARN_ON(!list_empty(&devlink->trap_list));
9800 WARN_ON(!list_empty(&devlink->reporter_list));
9801 WARN_ON(!list_empty(&devlink->region_list));
9802 WARN_ON(!list_empty(&devlink->param_list));
9803 WARN_ON(!list_empty(&devlink->resource_list));
9804 WARN_ON(!list_empty(&devlink->dpipe_table_list));
9805 WARN_ON(!list_empty(&devlink->sb_list));
9806 WARN_ON(!list_empty(&devlink->rate_list));
9807 WARN_ON(!list_empty(&devlink->linecard_list));
9808 WARN_ON(!list_empty(&devlink->port_list));
9810 xa_destroy(&devlink->snapshot_ids);
9811 xa_erase(&devlinks, devlink->index);
9815 EXPORT_SYMBOL_GPL(devlink_free);
9817 static void devlink_port_type_warn(struct work_struct *work)
9819 WARN(true, "Type was not set for devlink port.");
9822 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9824 /* Ignore CPU and DSA flavours. */
9825 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9826 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9827 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9830 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9832 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9834 if (!devlink_port_type_should_warn(devlink_port))
9836 /* Schedule a work to WARN in case driver does not set port
9837 * type within timeout.
9839 schedule_delayed_work(&devlink_port->type_warn_dw,
9840 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9843 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9845 if (!devlink_port_type_should_warn(devlink_port))
9847 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9851 * devl_port_register() - Register devlink port
9854 * @devlink_port: devlink port
9855 * @port_index: driver-specific numerical identifier of the port
9857 * Register devlink port with provided port index. User can use
9858 * any indexing, even hw-related one. devlink_port structure
9859 * is convenient to be embedded inside user driver private structure.
9860 * Note that the caller should take care of zeroing the devlink_port
9863 int devl_port_register(struct devlink *devlink,
9864 struct devlink_port *devlink_port,
9865 unsigned int port_index)
9867 devl_assert_locked(devlink);
9869 if (devlink_port_index_exists(devlink, port_index))
9872 WARN_ON(devlink_port->devlink);
9873 devlink_port->devlink = devlink;
9874 devlink_port->index = port_index;
9875 spin_lock_init(&devlink_port->type_lock);
9876 INIT_LIST_HEAD(&devlink_port->reporter_list);
9877 mutex_init(&devlink_port->reporters_lock);
9878 list_add_tail(&devlink_port->list, &devlink->port_list);
9879 INIT_LIST_HEAD(&devlink_port->region_list);
9881 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9882 devlink_port_type_warn_schedule(devlink_port);
9883 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9886 EXPORT_SYMBOL_GPL(devl_port_register);
9889 * devlink_port_register - Register devlink port
9892 * @devlink_port: devlink port
9893 * @port_index: driver-specific numerical identifier of the port
9895 * Register devlink port with provided port index. User can use
9896 * any indexing, even hw-related one. devlink_port structure
9897 * is convenient to be embedded inside user driver private structure.
9898 * Note that the caller should take care of zeroing the devlink_port
9901 * Context: Takes and release devlink->lock <mutex>.
9903 int devlink_port_register(struct devlink *devlink,
9904 struct devlink_port *devlink_port,
9905 unsigned int port_index)
9910 err = devl_port_register(devlink, devlink_port, port_index);
9911 devl_unlock(devlink);
9914 EXPORT_SYMBOL_GPL(devlink_port_register);
9917 * devl_port_unregister() - Unregister devlink port
9919 * @devlink_port: devlink port
9921 void devl_port_unregister(struct devlink_port *devlink_port)
9923 lockdep_assert_held(&devlink_port->devlink->lock);
9925 devlink_port_type_warn_cancel(devlink_port);
9926 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9927 list_del(&devlink_port->list);
9928 WARN_ON(!list_empty(&devlink_port->reporter_list));
9929 WARN_ON(!list_empty(&devlink_port->region_list));
9930 mutex_destroy(&devlink_port->reporters_lock);
9932 EXPORT_SYMBOL_GPL(devl_port_unregister);
9935 * devlink_port_unregister - Unregister devlink port
9937 * @devlink_port: devlink port
9939 * Context: Takes and release devlink->lock <mutex>.
9941 void devlink_port_unregister(struct devlink_port *devlink_port)
9943 struct devlink *devlink = devlink_port->devlink;
9946 devl_port_unregister(devlink_port);
9947 devl_unlock(devlink);
9949 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9951 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9952 enum devlink_port_type type,
9955 if (WARN_ON(!devlink_port->devlink))
9957 devlink_port_type_warn_cancel(devlink_port);
9958 spin_lock_bh(&devlink_port->type_lock);
9959 devlink_port->type = type;
9960 devlink_port->type_dev = type_dev;
9961 spin_unlock_bh(&devlink_port->type_lock);
9962 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9965 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9966 struct net_device *netdev)
9968 const struct net_device_ops *ops = netdev->netdev_ops;
9970 /* If driver registers devlink port, it should set devlink port
9971 * attributes accordingly so the compat functions are called
9972 * and the original ops are not used.
9974 if (ops->ndo_get_phys_port_name) {
9975 /* Some drivers use the same set of ndos for netdevs
9976 * that have devlink_port registered and also for
9977 * those who don't. Make sure that ndo_get_phys_port_name
9978 * returns -EOPNOTSUPP here in case it is defined.
9981 char name[IFNAMSIZ];
9984 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9985 WARN_ON(err != -EOPNOTSUPP);
9987 if (ops->ndo_get_port_parent_id) {
9988 /* Some drivers use the same set of ndos for netdevs
9989 * that have devlink_port registered and also for
9990 * those who don't. Make sure that ndo_get_port_parent_id
9991 * returns -EOPNOTSUPP here in case it is defined.
9994 struct netdev_phys_item_id ppid;
9997 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9998 WARN_ON(err != -EOPNOTSUPP);
10003 * devlink_port_type_eth_set - Set port type to Ethernet
10005 * @devlink_port: devlink port
10006 * @netdev: related netdevice
10008 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
10009 struct net_device *netdev)
10012 devlink_port_type_netdev_checks(devlink_port, netdev);
10014 dev_warn(devlink_port->devlink->dev,
10015 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
10016 devlink_port->index);
10018 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
10020 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
10023 * devlink_port_type_ib_set - Set port type to InfiniBand
10025 * @devlink_port: devlink port
10026 * @ibdev: related IB device
10028 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
10029 struct ib_device *ibdev)
10031 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
10033 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
10036 * devlink_port_type_clear - Clear port type
10038 * @devlink_port: devlink port
10040 void devlink_port_type_clear(struct devlink_port *devlink_port)
10042 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
10043 devlink_port_type_warn_schedule(devlink_port);
10045 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
10047 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
10048 enum devlink_port_flavour flavour)
10050 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10052 devlink_port->attrs_set = true;
10053 attrs->flavour = flavour;
10054 if (attrs->switch_id.id_len) {
10055 devlink_port->switch_port = true;
10056 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
10057 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
10059 devlink_port->switch_port = false;
10065 * devlink_port_attrs_set - Set port attributes
10067 * @devlink_port: devlink port
10068 * @attrs: devlink port attrs
10070 void devlink_port_attrs_set(struct devlink_port *devlink_port,
10071 struct devlink_port_attrs *attrs)
10075 if (WARN_ON(devlink_port->devlink))
10077 devlink_port->attrs = *attrs;
10078 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
10081 WARN_ON(attrs->splittable && attrs->split);
10083 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
10086 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
10088 * @devlink_port: devlink port
10089 * @controller: associated controller number for the devlink port instance
10090 * @pf: associated PF for the devlink port instance
10091 * @external: indicates if the port is for an external controller
10093 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
10094 u16 pf, bool external)
10096 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10099 if (WARN_ON(devlink_port->devlink))
10101 ret = __devlink_port_attrs_set(devlink_port,
10102 DEVLINK_PORT_FLAVOUR_PCI_PF);
10105 attrs->pci_pf.controller = controller;
10106 attrs->pci_pf.pf = pf;
10107 attrs->pci_pf.external = external;
10109 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
10112 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
10114 * @devlink_port: devlink port
10115 * @controller: associated controller number for the devlink port instance
10116 * @pf: associated PF for the devlink port instance
10117 * @vf: associated VF of a PF for the devlink port instance
10118 * @external: indicates if the port is for an external controller
10120 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
10121 u16 pf, u16 vf, bool external)
10123 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10126 if (WARN_ON(devlink_port->devlink))
10128 ret = __devlink_port_attrs_set(devlink_port,
10129 DEVLINK_PORT_FLAVOUR_PCI_VF);
10132 attrs->pci_vf.controller = controller;
10133 attrs->pci_vf.pf = pf;
10134 attrs->pci_vf.vf = vf;
10135 attrs->pci_vf.external = external;
10137 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
10140 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
10142 * @devlink_port: devlink port
10143 * @controller: associated controller number for the devlink port instance
10144 * @pf: associated PF for the devlink port instance
10145 * @sf: associated SF of a PF for the devlink port instance
10146 * @external: indicates if the port is for an external controller
10148 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
10149 u16 pf, u32 sf, bool external)
10151 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10154 if (WARN_ON(devlink_port->devlink))
10156 ret = __devlink_port_attrs_set(devlink_port,
10157 DEVLINK_PORT_FLAVOUR_PCI_SF);
10160 attrs->pci_sf.controller = controller;
10161 attrs->pci_sf.pf = pf;
10162 attrs->pci_sf.sf = sf;
10163 attrs->pci_sf.external = external;
10165 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
10168 * devl_rate_leaf_create - create devlink rate leaf
10169 * @devlink_port: devlink port object to create rate object on
10170 * @priv: driver private data
10172 * Create devlink rate object of type leaf on provided @devlink_port.
10174 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
10176 struct devlink *devlink = devlink_port->devlink;
10177 struct devlink_rate *devlink_rate;
10179 devl_assert_locked(devlink_port->devlink);
10181 if (WARN_ON(devlink_port->devlink_rate))
10184 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
10188 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
10189 devlink_rate->devlink = devlink;
10190 devlink_rate->devlink_port = devlink_port;
10191 devlink_rate->priv = priv;
10192 list_add_tail(&devlink_rate->list, &devlink->rate_list);
10193 devlink_port->devlink_rate = devlink_rate;
10194 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10198 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10201 * devl_rate_leaf_destroy - destroy devlink rate leaf
10203 * @devlink_port: devlink port linked to the rate object
10205 * Destroy the devlink rate object of type leaf on provided @devlink_port.
10207 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10209 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10211 devl_assert_locked(devlink_port->devlink);
10215 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10216 if (devlink_rate->parent)
10217 refcount_dec(&devlink_rate->parent->refcnt);
10218 list_del(&devlink_rate->list);
10219 devlink_port->devlink_rate = NULL;
10220 kfree(devlink_rate);
10222 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10225 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10226 * @devlink: devlink instance
10228 * Unset parent for all rate objects and destroy all rate nodes
10229 * on specified device.
10231 void devl_rate_nodes_destroy(struct devlink *devlink)
10233 static struct devlink_rate *devlink_rate, *tmp;
10234 const struct devlink_ops *ops = devlink->ops;
10236 devl_assert_locked(devlink);
10238 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10239 if (!devlink_rate->parent)
10242 refcount_dec(&devlink_rate->parent->refcnt);
10243 if (devlink_rate_is_leaf(devlink_rate))
10244 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10246 else if (devlink_rate_is_node(devlink_rate))
10247 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10250 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10251 if (devlink_rate_is_node(devlink_rate)) {
10252 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10253 list_del(&devlink_rate->list);
10254 kfree(devlink_rate->name);
10255 kfree(devlink_rate);
10259 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10262 * devlink_port_linecard_set - Link port with a linecard
10264 * @devlink_port: devlink port
10265 * @linecard: devlink linecard
10267 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10268 struct devlink_linecard *linecard)
10270 if (WARN_ON(devlink_port->devlink))
10272 devlink_port->linecard = linecard;
10274 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10276 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10277 char *name, size_t len)
10279 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10282 if (!devlink_port->attrs_set)
10283 return -EOPNOTSUPP;
10285 switch (attrs->flavour) {
10286 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10287 if (devlink_port->linecard)
10288 n = snprintf(name, len, "l%u",
10289 devlink_port->linecard->index);
10291 n += snprintf(name + n, len - n, "p%u",
10292 attrs->phys.port_number);
10293 if (n < len && attrs->split)
10294 n += snprintf(name + n, len - n, "s%u",
10295 attrs->phys.split_subport_number);
10297 case DEVLINK_PORT_FLAVOUR_CPU:
10298 case DEVLINK_PORT_FLAVOUR_DSA:
10299 case DEVLINK_PORT_FLAVOUR_UNUSED:
10300 /* As CPU and DSA ports do not have a netdevice associated
10301 * case should not ever happen.
10305 case DEVLINK_PORT_FLAVOUR_PCI_PF:
10306 if (attrs->pci_pf.external) {
10307 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10313 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10315 case DEVLINK_PORT_FLAVOUR_PCI_VF:
10316 if (attrs->pci_vf.external) {
10317 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10323 n = snprintf(name, len, "pf%uvf%u",
10324 attrs->pci_vf.pf, attrs->pci_vf.vf);
10326 case DEVLINK_PORT_FLAVOUR_PCI_SF:
10327 if (attrs->pci_sf.external) {
10328 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10334 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10337 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10338 return -EOPNOTSUPP;
10347 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10349 struct devlink_linecard_type *linecard_type;
10350 unsigned int count;
10353 count = linecard->ops->types_count(linecard, linecard->priv);
10354 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10356 if (!linecard->types)
10358 linecard->types_count = count;
10360 for (i = 0; i < count; i++) {
10361 linecard_type = &linecard->types[i];
10362 linecard->ops->types_get(linecard, linecard->priv, i,
10363 &linecard_type->type,
10364 &linecard_type->priv);
10369 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10371 kfree(linecard->types);
10375 * devlink_linecard_create - Create devlink linecard
10377 * @devlink: devlink
10378 * @linecard_index: driver-specific numerical identifier of the linecard
10379 * @ops: linecards ops
10380 * @priv: user priv pointer
10382 * Create devlink linecard instance with provided linecard index.
10383 * Caller can use any indexing, even hw-related one.
10385 * Return: Line card structure or an ERR_PTR() encoded error code.
10387 struct devlink_linecard *
10388 devlink_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10389 const struct devlink_linecard_ops *ops, void *priv)
10391 struct devlink_linecard *linecard;
10394 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10395 !ops->types_count || !ops->types_get))
10396 return ERR_PTR(-EINVAL);
10398 mutex_lock(&devlink->linecards_lock);
10399 if (devlink_linecard_index_exists(devlink, linecard_index)) {
10400 mutex_unlock(&devlink->linecards_lock);
10401 return ERR_PTR(-EEXIST);
10404 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10406 mutex_unlock(&devlink->linecards_lock);
10407 return ERR_PTR(-ENOMEM);
10410 linecard->devlink = devlink;
10411 linecard->index = linecard_index;
10412 linecard->ops = ops;
10413 linecard->priv = priv;
10414 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10415 mutex_init(&linecard->state_lock);
10417 err = devlink_linecard_types_init(linecard);
10419 mutex_destroy(&linecard->state_lock);
10421 mutex_unlock(&devlink->linecards_lock);
10422 return ERR_PTR(err);
10425 list_add_tail(&linecard->list, &devlink->linecard_list);
10426 refcount_set(&linecard->refcount, 1);
10427 mutex_unlock(&devlink->linecards_lock);
10428 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10431 EXPORT_SYMBOL_GPL(devlink_linecard_create);
10434 * devlink_linecard_destroy - Destroy devlink linecard
10436 * @linecard: devlink linecard
10438 void devlink_linecard_destroy(struct devlink_linecard *linecard)
10440 struct devlink *devlink = linecard->devlink;
10442 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10443 mutex_lock(&devlink->linecards_lock);
10444 list_del(&linecard->list);
10445 devlink_linecard_types_fini(linecard);
10446 mutex_unlock(&devlink->linecards_lock);
10447 devlink_linecard_put(linecard);
10449 EXPORT_SYMBOL_GPL(devlink_linecard_destroy);
10452 * devlink_linecard_provision_set - Set provisioning on linecard
10454 * @linecard: devlink linecard
10455 * @type: linecard type
10457 * This is either called directly from the provision() op call or
10458 * as a result of the provision() op call asynchronously.
10460 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10463 mutex_lock(&linecard->state_lock);
10464 WARN_ON(linecard->type && strcmp(linecard->type, type));
10465 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10466 linecard->type = type;
10467 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10468 mutex_unlock(&linecard->state_lock);
10470 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10473 * devlink_linecard_provision_clear - Clear provisioning on linecard
10475 * @linecard: devlink linecard
10477 * This is either called directly from the unprovision() op call or
10478 * as a result of the unprovision() op call asynchronously.
10480 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10482 mutex_lock(&linecard->state_lock);
10483 WARN_ON(linecard->nested_devlink);
10484 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10485 linecard->type = NULL;
10486 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10487 mutex_unlock(&linecard->state_lock);
10489 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10492 * devlink_linecard_provision_fail - Fail provisioning on linecard
10494 * @linecard: devlink linecard
10496 * This is either called directly from the provision() op call or
10497 * as a result of the provision() op call asynchronously.
10499 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10501 mutex_lock(&linecard->state_lock);
10502 WARN_ON(linecard->nested_devlink);
10503 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10504 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10505 mutex_unlock(&linecard->state_lock);
10507 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10510 * devlink_linecard_activate - Set linecard active
10512 * @linecard: devlink linecard
10514 void devlink_linecard_activate(struct devlink_linecard *linecard)
10516 mutex_lock(&linecard->state_lock);
10517 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10518 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10519 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10520 mutex_unlock(&linecard->state_lock);
10522 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10525 * devlink_linecard_deactivate - Set linecard inactive
10527 * @linecard: devlink linecard
10529 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10531 mutex_lock(&linecard->state_lock);
10532 switch (linecard->state) {
10533 case DEVLINK_LINECARD_STATE_ACTIVE:
10534 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10535 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10537 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10538 /* Line card is being deactivated as part
10539 * of unprovisioning flow.
10546 mutex_unlock(&linecard->state_lock);
10548 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10551 * devlink_linecard_nested_dl_set - Attach/detach nested devlink
10552 * instance to linecard.
10554 * @linecard: devlink linecard
10555 * @nested_devlink: devlink instance to attach or NULL to detach
10557 void devlink_linecard_nested_dl_set(struct devlink_linecard *linecard,
10558 struct devlink *nested_devlink)
10560 mutex_lock(&linecard->state_lock);
10561 linecard->nested_devlink = nested_devlink;
10562 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10563 mutex_unlock(&linecard->state_lock);
10565 EXPORT_SYMBOL_GPL(devlink_linecard_nested_dl_set);
10567 int devl_sb_register(struct devlink *devlink, unsigned int sb_index,
10568 u32 size, u16 ingress_pools_count,
10569 u16 egress_pools_count, u16 ingress_tc_count,
10570 u16 egress_tc_count)
10572 struct devlink_sb *devlink_sb;
10574 lockdep_assert_held(&devlink->lock);
10576 if (devlink_sb_index_exists(devlink, sb_index))
10579 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10582 devlink_sb->index = sb_index;
10583 devlink_sb->size = size;
10584 devlink_sb->ingress_pools_count = ingress_pools_count;
10585 devlink_sb->egress_pools_count = egress_pools_count;
10586 devlink_sb->ingress_tc_count = ingress_tc_count;
10587 devlink_sb->egress_tc_count = egress_tc_count;
10588 list_add_tail(&devlink_sb->list, &devlink->sb_list);
10591 EXPORT_SYMBOL_GPL(devl_sb_register);
10593 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10594 u32 size, u16 ingress_pools_count,
10595 u16 egress_pools_count, u16 ingress_tc_count,
10596 u16 egress_tc_count)
10600 devl_lock(devlink);
10601 err = devl_sb_register(devlink, sb_index, size, ingress_pools_count,
10602 egress_pools_count, ingress_tc_count,
10604 devl_unlock(devlink);
10607 EXPORT_SYMBOL_GPL(devlink_sb_register);
10609 void devl_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10611 struct devlink_sb *devlink_sb;
10613 lockdep_assert_held(&devlink->lock);
10615 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10616 WARN_ON(!devlink_sb);
10617 list_del(&devlink_sb->list);
10620 EXPORT_SYMBOL_GPL(devl_sb_unregister);
10622 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10624 devl_lock(devlink);
10625 devl_sb_unregister(devlink, sb_index);
10626 devl_unlock(devlink);
10628 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10631 * devl_dpipe_headers_register - register dpipe headers
10633 * @devlink: devlink
10634 * @dpipe_headers: dpipe header array
10636 * Register the headers supported by hardware.
10638 void devl_dpipe_headers_register(struct devlink *devlink,
10639 struct devlink_dpipe_headers *dpipe_headers)
10641 lockdep_assert_held(&devlink->lock);
10643 devlink->dpipe_headers = dpipe_headers;
10645 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
10648 * devl_dpipe_headers_unregister - unregister dpipe headers
10650 * @devlink: devlink
10652 * Unregister the headers supported by hardware.
10654 void devl_dpipe_headers_unregister(struct devlink *devlink)
10656 lockdep_assert_held(&devlink->lock);
10658 devlink->dpipe_headers = NULL;
10660 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
10663 * devlink_dpipe_table_counter_enabled - check if counter allocation
10665 * @devlink: devlink
10666 * @table_name: tables name
10668 * Used by driver to check if counter allocation is required.
10669 * After counter allocation is turned on the table entries
10670 * are updated to include counter statistics.
10672 * After that point on the driver must respect the counter
10673 * state so that each entry added to the table is added
10676 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10677 const char *table_name)
10679 struct devlink_dpipe_table *table;
10683 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10684 table_name, devlink);
10687 enabled = table->counters_enabled;
10691 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10694 * devl_dpipe_table_register - register dpipe table
10696 * @devlink: devlink
10697 * @table_name: table name
10698 * @table_ops: table ops
10700 * @counter_control_extern: external control for counters
10702 int devl_dpipe_table_register(struct devlink *devlink,
10703 const char *table_name,
10704 struct devlink_dpipe_table_ops *table_ops,
10705 void *priv, bool counter_control_extern)
10707 struct devlink_dpipe_table *table;
10709 lockdep_assert_held(&devlink->lock);
10711 if (WARN_ON(!table_ops->size_get))
10714 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10718 table = kzalloc(sizeof(*table), GFP_KERNEL);
10722 table->name = table_name;
10723 table->table_ops = table_ops;
10724 table->priv = priv;
10725 table->counter_control_extern = counter_control_extern;
10727 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10731 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
10734 * devl_dpipe_table_unregister - unregister dpipe table
10736 * @devlink: devlink
10737 * @table_name: table name
10739 void devl_dpipe_table_unregister(struct devlink *devlink,
10740 const char *table_name)
10742 struct devlink_dpipe_table *table;
10744 lockdep_assert_held(&devlink->lock);
10746 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10747 table_name, devlink);
10750 list_del_rcu(&table->list);
10751 kfree_rcu(table, rcu);
10753 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
10756 * devl_resource_register - devlink resource register
10758 * @devlink: devlink
10759 * @resource_name: resource's name
10760 * @resource_size: resource's size
10761 * @resource_id: resource's id
10762 * @parent_resource_id: resource's parent id
10763 * @size_params: size parameters
10765 * Generic resources should reuse the same names across drivers.
10766 * Please see the generic resources list at:
10767 * Documentation/networking/devlink/devlink-resource.rst
10769 int devl_resource_register(struct devlink *devlink,
10770 const char *resource_name,
10773 u64 parent_resource_id,
10774 const struct devlink_resource_size_params *size_params)
10776 struct devlink_resource *resource;
10777 struct list_head *resource_list;
10778 bool top_hierarchy;
10780 lockdep_assert_held(&devlink->lock);
10782 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10784 resource = devlink_resource_find(devlink, NULL, resource_id);
10788 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10792 if (top_hierarchy) {
10793 resource_list = &devlink->resource_list;
10795 struct devlink_resource *parent_resource;
10797 parent_resource = devlink_resource_find(devlink, NULL,
10798 parent_resource_id);
10799 if (parent_resource) {
10800 resource_list = &parent_resource->resource_list;
10801 resource->parent = parent_resource;
10808 resource->name = resource_name;
10809 resource->size = resource_size;
10810 resource->size_new = resource_size;
10811 resource->id = resource_id;
10812 resource->size_valid = true;
10813 memcpy(&resource->size_params, size_params,
10814 sizeof(resource->size_params));
10815 INIT_LIST_HEAD(&resource->resource_list);
10816 list_add_tail(&resource->list, resource_list);
10820 EXPORT_SYMBOL_GPL(devl_resource_register);
10823 * devlink_resource_register - devlink resource register
10825 * @devlink: devlink
10826 * @resource_name: resource's name
10827 * @resource_size: resource's size
10828 * @resource_id: resource's id
10829 * @parent_resource_id: resource's parent id
10830 * @size_params: size parameters
10832 * Generic resources should reuse the same names across drivers.
10833 * Please see the generic resources list at:
10834 * Documentation/networking/devlink/devlink-resource.rst
10836 * Context: Takes and release devlink->lock <mutex>.
10838 int devlink_resource_register(struct devlink *devlink,
10839 const char *resource_name,
10842 u64 parent_resource_id,
10843 const struct devlink_resource_size_params *size_params)
10847 devl_lock(devlink);
10848 err = devl_resource_register(devlink, resource_name, resource_size,
10849 resource_id, parent_resource_id, size_params);
10850 devl_unlock(devlink);
10853 EXPORT_SYMBOL_GPL(devlink_resource_register);
10855 static void devlink_resource_unregister(struct devlink *devlink,
10856 struct devlink_resource *resource)
10858 struct devlink_resource *tmp, *child_resource;
10860 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10862 devlink_resource_unregister(devlink, child_resource);
10863 list_del(&child_resource->list);
10864 kfree(child_resource);
10869 * devl_resources_unregister - free all resources
10871 * @devlink: devlink
10873 void devl_resources_unregister(struct devlink *devlink)
10875 struct devlink_resource *tmp, *child_resource;
10877 lockdep_assert_held(&devlink->lock);
10879 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10881 devlink_resource_unregister(devlink, child_resource);
10882 list_del(&child_resource->list);
10883 kfree(child_resource);
10886 EXPORT_SYMBOL_GPL(devl_resources_unregister);
10889 * devlink_resources_unregister - free all resources
10891 * @devlink: devlink
10893 * Context: Takes and release devlink->lock <mutex>.
10895 void devlink_resources_unregister(struct devlink *devlink)
10897 devl_lock(devlink);
10898 devl_resources_unregister(devlink);
10899 devl_unlock(devlink);
10901 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10904 * devl_resource_size_get - get and update size
10906 * @devlink: devlink
10907 * @resource_id: the requested resource id
10908 * @p_resource_size: ptr to update
10910 int devl_resource_size_get(struct devlink *devlink,
10912 u64 *p_resource_size)
10914 struct devlink_resource *resource;
10916 lockdep_assert_held(&devlink->lock);
10918 resource = devlink_resource_find(devlink, NULL, resource_id);
10921 *p_resource_size = resource->size_new;
10922 resource->size = resource->size_new;
10925 EXPORT_SYMBOL_GPL(devl_resource_size_get);
10928 * devl_dpipe_table_resource_set - set the resource id
10930 * @devlink: devlink
10931 * @table_name: table name
10932 * @resource_id: resource id
10933 * @resource_units: number of resource's units consumed per table's entry
10935 int devl_dpipe_table_resource_set(struct devlink *devlink,
10936 const char *table_name, u64 resource_id,
10937 u64 resource_units)
10939 struct devlink_dpipe_table *table;
10941 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10942 table_name, devlink);
10946 table->resource_id = resource_id;
10947 table->resource_units = resource_units;
10948 table->resource_valid = true;
10951 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);
10954 * devl_resource_occ_get_register - register occupancy getter
10956 * @devlink: devlink
10957 * @resource_id: resource id
10958 * @occ_get: occupancy getter callback
10959 * @occ_get_priv: occupancy getter callback priv
10961 void devl_resource_occ_get_register(struct devlink *devlink,
10963 devlink_resource_occ_get_t *occ_get,
10964 void *occ_get_priv)
10966 struct devlink_resource *resource;
10968 lockdep_assert_held(&devlink->lock);
10970 resource = devlink_resource_find(devlink, NULL, resource_id);
10971 if (WARN_ON(!resource))
10973 WARN_ON(resource->occ_get);
10975 resource->occ_get = occ_get;
10976 resource->occ_get_priv = occ_get_priv;
10978 EXPORT_SYMBOL_GPL(devl_resource_occ_get_register);
10981 * devlink_resource_occ_get_register - register occupancy getter
10983 * @devlink: devlink
10984 * @resource_id: resource id
10985 * @occ_get: occupancy getter callback
10986 * @occ_get_priv: occupancy getter callback priv
10988 * Context: Takes and release devlink->lock <mutex>.
10990 void devlink_resource_occ_get_register(struct devlink *devlink,
10992 devlink_resource_occ_get_t *occ_get,
10993 void *occ_get_priv)
10995 devl_lock(devlink);
10996 devl_resource_occ_get_register(devlink, resource_id,
10997 occ_get, occ_get_priv);
10998 devl_unlock(devlink);
11000 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
11003 * devl_resource_occ_get_unregister - unregister occupancy getter
11005 * @devlink: devlink
11006 * @resource_id: resource id
11008 void devl_resource_occ_get_unregister(struct devlink *devlink,
11011 struct devlink_resource *resource;
11013 lockdep_assert_held(&devlink->lock);
11015 resource = devlink_resource_find(devlink, NULL, resource_id);
11016 if (WARN_ON(!resource))
11018 WARN_ON(!resource->occ_get);
11020 resource->occ_get = NULL;
11021 resource->occ_get_priv = NULL;
11023 EXPORT_SYMBOL_GPL(devl_resource_occ_get_unregister);
11026 * devlink_resource_occ_get_unregister - unregister occupancy getter
11028 * @devlink: devlink
11029 * @resource_id: resource id
11031 * Context: Takes and release devlink->lock <mutex>.
11033 void devlink_resource_occ_get_unregister(struct devlink *devlink,
11036 devl_lock(devlink);
11037 devl_resource_occ_get_unregister(devlink, resource_id);
11038 devl_unlock(devlink);
11040 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
11042 static int devlink_param_verify(const struct devlink_param *param)
11044 if (!param || !param->name || !param->supported_cmodes)
11046 if (param->generic)
11047 return devlink_param_generic_verify(param);
11049 return devlink_param_driver_verify(param);
11053 * devlink_params_register - register configuration parameters
11055 * @devlink: devlink
11056 * @params: configuration parameters array
11057 * @params_count: number of parameters provided
11059 * Register the configuration parameters supported by the driver.
11061 int devlink_params_register(struct devlink *devlink,
11062 const struct devlink_param *params,
11063 size_t params_count)
11065 const struct devlink_param *param = params;
11068 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11070 for (i = 0; i < params_count; i++, param++) {
11071 err = devlink_param_register(devlink, param);
11081 for (param--; i > 0; i--, param--)
11082 devlink_param_unregister(devlink, param);
11085 EXPORT_SYMBOL_GPL(devlink_params_register);
11088 * devlink_params_unregister - unregister configuration parameters
11089 * @devlink: devlink
11090 * @params: configuration parameters to unregister
11091 * @params_count: number of parameters provided
11093 void devlink_params_unregister(struct devlink *devlink,
11094 const struct devlink_param *params,
11095 size_t params_count)
11097 const struct devlink_param *param = params;
11100 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11102 for (i = 0; i < params_count; i++, param++)
11103 devlink_param_unregister(devlink, param);
11105 EXPORT_SYMBOL_GPL(devlink_params_unregister);
11108 * devlink_param_register - register one configuration parameter
11110 * @devlink: devlink
11111 * @param: one configuration parameter
11113 * Register the configuration parameter supported by the driver.
11114 * Return: returns 0 on successful registration or error code otherwise.
11116 int devlink_param_register(struct devlink *devlink,
11117 const struct devlink_param *param)
11119 struct devlink_param_item *param_item;
11121 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11123 WARN_ON(devlink_param_verify(param));
11124 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
11126 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
11127 WARN_ON(param->get || param->set);
11129 WARN_ON(!param->get || !param->set);
11131 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
11135 param_item->param = param;
11137 list_add_tail(¶m_item->list, &devlink->param_list);
11140 EXPORT_SYMBOL_GPL(devlink_param_register);
11143 * devlink_param_unregister - unregister one configuration parameter
11144 * @devlink: devlink
11145 * @param: configuration parameter to unregister
11147 void devlink_param_unregister(struct devlink *devlink,
11148 const struct devlink_param *param)
11150 struct devlink_param_item *param_item;
11152 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11155 devlink_param_find_by_name(&devlink->param_list, param->name);
11156 WARN_ON(!param_item);
11157 list_del(¶m_item->list);
11160 EXPORT_SYMBOL_GPL(devlink_param_unregister);
11163 * devlink_param_driverinit_value_get - get configuration parameter
11164 * value for driver initializing
11166 * @devlink: devlink
11167 * @param_id: parameter ID
11168 * @init_val: value of parameter in driverinit configuration mode
11170 * This function should be used by the driver to get driverinit
11171 * configuration for initialization after reload command.
11173 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
11174 union devlink_param_value *init_val)
11176 struct devlink_param_item *param_item;
11178 if (!devlink_reload_supported(devlink->ops))
11179 return -EOPNOTSUPP;
11181 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11185 if (!param_item->driverinit_value_valid ||
11186 !devlink_param_cmode_is_supported(param_item->param,
11187 DEVLINK_PARAM_CMODE_DRIVERINIT))
11188 return -EOPNOTSUPP;
11190 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11191 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
11193 *init_val = param_item->driverinit_value;
11197 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
11200 * devlink_param_driverinit_value_set - set value of configuration
11201 * parameter for driverinit
11202 * configuration mode
11204 * @devlink: devlink
11205 * @param_id: parameter ID
11206 * @init_val: value of parameter to set for driverinit configuration mode
11208 * This function should be used by the driver to set driverinit
11209 * configuration mode default value.
11211 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
11212 union devlink_param_value init_val)
11214 struct devlink_param_item *param_item;
11216 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11218 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11222 if (!devlink_param_cmode_is_supported(param_item->param,
11223 DEVLINK_PARAM_CMODE_DRIVERINIT))
11224 return -EOPNOTSUPP;
11226 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11227 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
11229 param_item->driverinit_value = init_val;
11230 param_item->driverinit_value_valid = true;
11233 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
11236 * devlink_param_value_changed - notify devlink on a parameter's value
11237 * change. Should be called by the driver
11238 * right after the change.
11240 * @devlink: devlink
11241 * @param_id: parameter ID
11243 * This function should be used by the driver to notify devlink on value
11244 * change, excluding driverinit configuration mode.
11245 * For driverinit configuration mode driver should use the function
11247 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11249 struct devlink_param_item *param_item;
11251 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11252 WARN_ON(!param_item);
11254 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11256 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11259 * devl_region_create - create a new address region
11261 * @devlink: devlink
11262 * @ops: region operations and name
11263 * @region_max_snapshots: Maximum supported number of snapshots for region
11264 * @region_size: size of region
11266 struct devlink_region *devl_region_create(struct devlink *devlink,
11267 const struct devlink_region_ops *ops,
11268 u32 region_max_snapshots,
11271 struct devlink_region *region;
11273 devl_assert_locked(devlink);
11275 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11276 return ERR_PTR(-EINVAL);
11278 if (devlink_region_get_by_name(devlink, ops->name))
11279 return ERR_PTR(-EEXIST);
11281 region = kzalloc(sizeof(*region), GFP_KERNEL);
11283 return ERR_PTR(-ENOMEM);
11285 region->devlink = devlink;
11286 region->max_snapshots = region_max_snapshots;
11288 region->size = region_size;
11289 INIT_LIST_HEAD(®ion->snapshot_list);
11290 mutex_init(®ion->snapshot_lock);
11291 list_add_tail(®ion->list, &devlink->region_list);
11292 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11296 EXPORT_SYMBOL_GPL(devl_region_create);
11299 * devlink_region_create - create a new address region
11301 * @devlink: devlink
11302 * @ops: region operations and name
11303 * @region_max_snapshots: Maximum supported number of snapshots for region
11304 * @region_size: size of region
11306 * Context: Takes and release devlink->lock <mutex>.
11308 struct devlink_region *
11309 devlink_region_create(struct devlink *devlink,
11310 const struct devlink_region_ops *ops,
11311 u32 region_max_snapshots, u64 region_size)
11313 struct devlink_region *region;
11315 devl_lock(devlink);
11316 region = devl_region_create(devlink, ops, region_max_snapshots,
11318 devl_unlock(devlink);
11321 EXPORT_SYMBOL_GPL(devlink_region_create);
11324 * devlink_port_region_create - create a new address region for a port
11326 * @port: devlink port
11327 * @ops: region operations and name
11328 * @region_max_snapshots: Maximum supported number of snapshots for region
11329 * @region_size: size of region
11331 * Context: Takes and release devlink->lock <mutex>.
11333 struct devlink_region *
11334 devlink_port_region_create(struct devlink_port *port,
11335 const struct devlink_port_region_ops *ops,
11336 u32 region_max_snapshots, u64 region_size)
11338 struct devlink *devlink = port->devlink;
11339 struct devlink_region *region;
11342 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11343 return ERR_PTR(-EINVAL);
11345 devl_lock(devlink);
11347 if (devlink_port_region_get_by_name(port, ops->name)) {
11352 region = kzalloc(sizeof(*region), GFP_KERNEL);
11358 region->devlink = devlink;
11359 region->port = port;
11360 region->max_snapshots = region_max_snapshots;
11361 region->port_ops = ops;
11362 region->size = region_size;
11363 INIT_LIST_HEAD(®ion->snapshot_list);
11364 mutex_init(®ion->snapshot_lock);
11365 list_add_tail(®ion->list, &port->region_list);
11366 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11368 devl_unlock(devlink);
11372 devl_unlock(devlink);
11373 return ERR_PTR(err);
11375 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11378 * devl_region_destroy - destroy address region
11380 * @region: devlink region to destroy
11382 void devl_region_destroy(struct devlink_region *region)
11384 struct devlink *devlink = region->devlink;
11385 struct devlink_snapshot *snapshot, *ts;
11387 devl_assert_locked(devlink);
11389 /* Free all snapshots of region */
11390 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
11391 devlink_region_snapshot_del(region, snapshot);
11393 list_del(®ion->list);
11394 mutex_destroy(®ion->snapshot_lock);
11396 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11399 EXPORT_SYMBOL_GPL(devl_region_destroy);
11402 * devlink_region_destroy - destroy address region
11404 * @region: devlink region to destroy
11406 * Context: Takes and release devlink->lock <mutex>.
11408 void devlink_region_destroy(struct devlink_region *region)
11410 struct devlink *devlink = region->devlink;
11412 devl_lock(devlink);
11413 devl_region_destroy(region);
11414 devl_unlock(devlink);
11416 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11419 * devlink_region_snapshot_id_get - get snapshot ID
11421 * This callback should be called when adding a new snapshot,
11422 * Driver should use the same id for multiple snapshots taken
11423 * on multiple regions at the same time/by the same trigger.
11425 * The caller of this function must use devlink_region_snapshot_id_put
11426 * when finished creating regions using this id.
11428 * Returns zero on success, or a negative error code on failure.
11430 * @devlink: devlink
11431 * @id: storage to return id
11433 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11435 return __devlink_region_snapshot_id_get(devlink, id);
11437 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11440 * devlink_region_snapshot_id_put - put snapshot ID reference
11442 * This should be called by a driver after finishing creating snapshots
11443 * with an id. Doing so ensures that the ID can later be released in the
11444 * event that all snapshots using it have been destroyed.
11446 * @devlink: devlink
11447 * @id: id to release reference on
11449 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11451 __devlink_snapshot_id_decrement(devlink, id);
11453 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11456 * devlink_region_snapshot_create - create a new snapshot
11457 * This will add a new snapshot of a region. The snapshot
11458 * will be stored on the region struct and can be accessed
11459 * from devlink. This is useful for future analyses of snapshots.
11460 * Multiple snapshots can be created on a region.
11461 * The @snapshot_id should be obtained using the getter function.
11463 * @region: devlink region of the snapshot
11464 * @data: snapshot data
11465 * @snapshot_id: snapshot id to be created
11467 int devlink_region_snapshot_create(struct devlink_region *region,
11468 u8 *data, u32 snapshot_id)
11472 mutex_lock(®ion->snapshot_lock);
11473 err = __devlink_region_snapshot_create(region, data, snapshot_id);
11474 mutex_unlock(®ion->snapshot_lock);
11477 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11479 #define DEVLINK_TRAP(_id, _type) \
11481 .type = DEVLINK_TRAP_TYPE_##_type, \
11482 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
11483 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
11486 static const struct devlink_trap devlink_trap_generic[] = {
11487 DEVLINK_TRAP(SMAC_MC, DROP),
11488 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11489 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11490 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11491 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11492 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11493 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11494 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11495 DEVLINK_TRAP(TAIL_DROP, DROP),
11496 DEVLINK_TRAP(NON_IP_PACKET, DROP),
11497 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11498 DEVLINK_TRAP(DIP_LB, DROP),
11499 DEVLINK_TRAP(SIP_MC, DROP),
11500 DEVLINK_TRAP(SIP_LB, DROP),
11501 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11502 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11503 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11504 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11505 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11506 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11507 DEVLINK_TRAP(RPF, EXCEPTION),
11508 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11509 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11510 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11511 DEVLINK_TRAP(NON_ROUTABLE, DROP),
11512 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11513 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11514 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11515 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11516 DEVLINK_TRAP(STP, CONTROL),
11517 DEVLINK_TRAP(LACP, CONTROL),
11518 DEVLINK_TRAP(LLDP, CONTROL),
11519 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11520 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11521 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11522 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11523 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11524 DEVLINK_TRAP(MLD_QUERY, CONTROL),
11525 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11526 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11527 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11528 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11529 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11530 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11531 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11532 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11533 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11534 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11535 DEVLINK_TRAP(IPV4_BFD, CONTROL),
11536 DEVLINK_TRAP(IPV6_BFD, CONTROL),
11537 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11538 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11539 DEVLINK_TRAP(IPV4_BGP, CONTROL),
11540 DEVLINK_TRAP(IPV6_BGP, CONTROL),
11541 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11542 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11543 DEVLINK_TRAP(IPV4_PIM, CONTROL),
11544 DEVLINK_TRAP(IPV6_PIM, CONTROL),
11545 DEVLINK_TRAP(UC_LB, CONTROL),
11546 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11547 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11548 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11549 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11550 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11551 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11552 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11553 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11554 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11555 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11556 DEVLINK_TRAP(PTP_EVENT, CONTROL),
11557 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11558 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11559 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11560 DEVLINK_TRAP(EARLY_DROP, DROP),
11561 DEVLINK_TRAP(VXLAN_PARSING, DROP),
11562 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11563 DEVLINK_TRAP(VLAN_PARSING, DROP),
11564 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11565 DEVLINK_TRAP(MPLS_PARSING, DROP),
11566 DEVLINK_TRAP(ARP_PARSING, DROP),
11567 DEVLINK_TRAP(IP_1_PARSING, DROP),
11568 DEVLINK_TRAP(IP_N_PARSING, DROP),
11569 DEVLINK_TRAP(GRE_PARSING, DROP),
11570 DEVLINK_TRAP(UDP_PARSING, DROP),
11571 DEVLINK_TRAP(TCP_PARSING, DROP),
11572 DEVLINK_TRAP(IPSEC_PARSING, DROP),
11573 DEVLINK_TRAP(SCTP_PARSING, DROP),
11574 DEVLINK_TRAP(DCCP_PARSING, DROP),
11575 DEVLINK_TRAP(GTP_PARSING, DROP),
11576 DEVLINK_TRAP(ESP_PARSING, DROP),
11577 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11578 DEVLINK_TRAP(DMAC_FILTER, DROP),
11581 #define DEVLINK_TRAP_GROUP(_id) \
11583 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
11584 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
11587 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11588 DEVLINK_TRAP_GROUP(L2_DROPS),
11589 DEVLINK_TRAP_GROUP(L3_DROPS),
11590 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11591 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11592 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11593 DEVLINK_TRAP_GROUP(ACL_DROPS),
11594 DEVLINK_TRAP_GROUP(STP),
11595 DEVLINK_TRAP_GROUP(LACP),
11596 DEVLINK_TRAP_GROUP(LLDP),
11597 DEVLINK_TRAP_GROUP(MC_SNOOPING),
11598 DEVLINK_TRAP_GROUP(DHCP),
11599 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11600 DEVLINK_TRAP_GROUP(BFD),
11601 DEVLINK_TRAP_GROUP(OSPF),
11602 DEVLINK_TRAP_GROUP(BGP),
11603 DEVLINK_TRAP_GROUP(VRRP),
11604 DEVLINK_TRAP_GROUP(PIM),
11605 DEVLINK_TRAP_GROUP(UC_LB),
11606 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11607 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11608 DEVLINK_TRAP_GROUP(IPV6),
11609 DEVLINK_TRAP_GROUP(PTP_EVENT),
11610 DEVLINK_TRAP_GROUP(PTP_GENERAL),
11611 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11612 DEVLINK_TRAP_GROUP(ACL_TRAP),
11613 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11616 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11618 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11621 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11624 if (trap->type != devlink_trap_generic[trap->id].type)
11630 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11634 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11637 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11638 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11645 static int devlink_trap_verify(const struct devlink_trap *trap)
11647 if (!trap || !trap->name)
11651 return devlink_trap_generic_verify(trap);
11653 return devlink_trap_driver_verify(trap);
11657 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11659 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11662 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11669 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11673 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11676 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11677 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11684 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11686 if (group->generic)
11687 return devlink_trap_group_generic_verify(group);
11689 return devlink_trap_group_driver_verify(group);
11693 devlink_trap_group_notify(struct devlink *devlink,
11694 const struct devlink_trap_group_item *group_item,
11695 enum devlink_command cmd)
11697 struct sk_buff *msg;
11700 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11701 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11702 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11705 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11709 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11716 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11717 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11721 devlink_trap_item_group_link(struct devlink *devlink,
11722 struct devlink_trap_item *trap_item)
11724 u16 group_id = trap_item->trap->init_group_id;
11725 struct devlink_trap_group_item *group_item;
11727 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11728 if (WARN_ON_ONCE(!group_item))
11731 trap_item->group_item = group_item;
11736 static void devlink_trap_notify(struct devlink *devlink,
11737 const struct devlink_trap_item *trap_item,
11738 enum devlink_command cmd)
11740 struct sk_buff *msg;
11743 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11744 cmd != DEVLINK_CMD_TRAP_DEL);
11745 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11748 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11752 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11758 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11759 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11763 devlink_trap_register(struct devlink *devlink,
11764 const struct devlink_trap *trap, void *priv)
11766 struct devlink_trap_item *trap_item;
11769 if (devlink_trap_item_lookup(devlink, trap->name))
11772 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11776 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11777 if (!trap_item->stats) {
11779 goto err_stats_alloc;
11782 trap_item->trap = trap;
11783 trap_item->action = trap->init_action;
11784 trap_item->priv = priv;
11786 err = devlink_trap_item_group_link(devlink, trap_item);
11788 goto err_group_link;
11790 err = devlink->ops->trap_init(devlink, trap, trap_item);
11792 goto err_trap_init;
11794 list_add_tail(&trap_item->list, &devlink->trap_list);
11795 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11801 free_percpu(trap_item->stats);
11807 static void devlink_trap_unregister(struct devlink *devlink,
11808 const struct devlink_trap *trap)
11810 struct devlink_trap_item *trap_item;
11812 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11813 if (WARN_ON_ONCE(!trap_item))
11816 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11817 list_del(&trap_item->list);
11818 if (devlink->ops->trap_fini)
11819 devlink->ops->trap_fini(devlink, trap, trap_item);
11820 free_percpu(trap_item->stats);
11824 static void devlink_trap_disable(struct devlink *devlink,
11825 const struct devlink_trap *trap)
11827 struct devlink_trap_item *trap_item;
11829 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11830 if (WARN_ON_ONCE(!trap_item))
11833 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11835 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11839 * devl_traps_register - Register packet traps with devlink.
11840 * @devlink: devlink.
11841 * @traps: Packet traps.
11842 * @traps_count: Count of provided packet traps.
11843 * @priv: Driver private information.
11845 * Return: Non-zero value on failure.
11847 int devl_traps_register(struct devlink *devlink,
11848 const struct devlink_trap *traps,
11849 size_t traps_count, void *priv)
11853 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11856 devl_assert_locked(devlink);
11857 for (i = 0; i < traps_count; i++) {
11858 const struct devlink_trap *trap = &traps[i];
11860 err = devlink_trap_verify(trap);
11862 goto err_trap_verify;
11864 err = devlink_trap_register(devlink, trap, priv);
11866 goto err_trap_register;
11873 for (i--; i >= 0; i--)
11874 devlink_trap_unregister(devlink, &traps[i]);
11877 EXPORT_SYMBOL_GPL(devl_traps_register);
11880 * devlink_traps_register - Register packet traps with devlink.
11881 * @devlink: devlink.
11882 * @traps: Packet traps.
11883 * @traps_count: Count of provided packet traps.
11884 * @priv: Driver private information.
11886 * Context: Takes and release devlink->lock <mutex>.
11888 * Return: Non-zero value on failure.
11890 int devlink_traps_register(struct devlink *devlink,
11891 const struct devlink_trap *traps,
11892 size_t traps_count, void *priv)
11896 devl_lock(devlink);
11897 err = devl_traps_register(devlink, traps, traps_count, priv);
11898 devl_unlock(devlink);
11901 EXPORT_SYMBOL_GPL(devlink_traps_register);
11904 * devl_traps_unregister - Unregister packet traps from devlink.
11905 * @devlink: devlink.
11906 * @traps: Packet traps.
11907 * @traps_count: Count of provided packet traps.
11909 void devl_traps_unregister(struct devlink *devlink,
11910 const struct devlink_trap *traps,
11911 size_t traps_count)
11915 devl_assert_locked(devlink);
11916 /* Make sure we do not have any packets in-flight while unregistering
11917 * traps by disabling all of them and waiting for a grace period.
11919 for (i = traps_count - 1; i >= 0; i--)
11920 devlink_trap_disable(devlink, &traps[i]);
11922 for (i = traps_count - 1; i >= 0; i--)
11923 devlink_trap_unregister(devlink, &traps[i]);
11925 EXPORT_SYMBOL_GPL(devl_traps_unregister);
11928 * devlink_traps_unregister - Unregister packet traps from devlink.
11929 * @devlink: devlink.
11930 * @traps: Packet traps.
11931 * @traps_count: Count of provided packet traps.
11933 * Context: Takes and release devlink->lock <mutex>.
11935 void devlink_traps_unregister(struct devlink *devlink,
11936 const struct devlink_trap *traps,
11937 size_t traps_count)
11939 devl_lock(devlink);
11940 devl_traps_unregister(devlink, traps, traps_count);
11941 devl_unlock(devlink);
11943 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11946 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11949 struct devlink_stats *stats;
11951 stats = this_cpu_ptr(trap_stats);
11952 u64_stats_update_begin(&stats->syncp);
11953 u64_stats_add(&stats->rx_bytes, skb_len);
11954 u64_stats_inc(&stats->rx_packets);
11955 u64_stats_update_end(&stats->syncp);
11959 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11960 const struct devlink_trap_item *trap_item,
11961 struct devlink_port *in_devlink_port,
11962 const struct flow_action_cookie *fa_cookie)
11964 metadata->trap_name = trap_item->trap->name;
11965 metadata->trap_group_name = trap_item->group_item->group->name;
11966 metadata->fa_cookie = fa_cookie;
11967 metadata->trap_type = trap_item->trap->type;
11969 spin_lock(&in_devlink_port->type_lock);
11970 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11971 metadata->input_dev = in_devlink_port->type_dev;
11972 spin_unlock(&in_devlink_port->type_lock);
11976 * devlink_trap_report - Report trapped packet to drop monitor.
11977 * @devlink: devlink.
11978 * @skb: Trapped packet.
11979 * @trap_ctx: Trap context.
11980 * @in_devlink_port: Input devlink port.
11981 * @fa_cookie: Flow action cookie. Could be NULL.
11983 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11984 void *trap_ctx, struct devlink_port *in_devlink_port,
11985 const struct flow_action_cookie *fa_cookie)
11988 struct devlink_trap_item *trap_item = trap_ctx;
11990 devlink_trap_stats_update(trap_item->stats, skb->len);
11991 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11993 if (trace_devlink_trap_report_enabled()) {
11994 struct devlink_trap_metadata metadata = {};
11996 devlink_trap_report_metadata_set(&metadata, trap_item,
11997 in_devlink_port, fa_cookie);
11998 trace_devlink_trap_report(devlink, skb, &metadata);
12001 EXPORT_SYMBOL_GPL(devlink_trap_report);
12004 * devlink_trap_ctx_priv - Trap context to driver private information.
12005 * @trap_ctx: Trap context.
12007 * Return: Driver private information passed during registration.
12009 void *devlink_trap_ctx_priv(void *trap_ctx)
12011 struct devlink_trap_item *trap_item = trap_ctx;
12013 return trap_item->priv;
12015 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
12018 devlink_trap_group_item_policer_link(struct devlink *devlink,
12019 struct devlink_trap_group_item *group_item)
12021 u32 policer_id = group_item->group->init_policer_id;
12022 struct devlink_trap_policer_item *policer_item;
12024 if (policer_id == 0)
12027 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
12028 if (WARN_ON_ONCE(!policer_item))
12031 group_item->policer_item = policer_item;
12037 devlink_trap_group_register(struct devlink *devlink,
12038 const struct devlink_trap_group *group)
12040 struct devlink_trap_group_item *group_item;
12043 if (devlink_trap_group_item_lookup(devlink, group->name))
12046 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
12050 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
12051 if (!group_item->stats) {
12053 goto err_stats_alloc;
12056 group_item->group = group;
12058 err = devlink_trap_group_item_policer_link(devlink, group_item);
12060 goto err_policer_link;
12062 if (devlink->ops->trap_group_init) {
12063 err = devlink->ops->trap_group_init(devlink, group);
12065 goto err_group_init;
12068 list_add_tail(&group_item->list, &devlink->trap_group_list);
12069 devlink_trap_group_notify(devlink, group_item,
12070 DEVLINK_CMD_TRAP_GROUP_NEW);
12076 free_percpu(group_item->stats);
12083 devlink_trap_group_unregister(struct devlink *devlink,
12084 const struct devlink_trap_group *group)
12086 struct devlink_trap_group_item *group_item;
12088 group_item = devlink_trap_group_item_lookup(devlink, group->name);
12089 if (WARN_ON_ONCE(!group_item))
12092 devlink_trap_group_notify(devlink, group_item,
12093 DEVLINK_CMD_TRAP_GROUP_DEL);
12094 list_del(&group_item->list);
12095 free_percpu(group_item->stats);
12100 * devl_trap_groups_register - Register packet trap groups with devlink.
12101 * @devlink: devlink.
12102 * @groups: Packet trap groups.
12103 * @groups_count: Count of provided packet trap groups.
12105 * Return: Non-zero value on failure.
12107 int devl_trap_groups_register(struct devlink *devlink,
12108 const struct devlink_trap_group *groups,
12109 size_t groups_count)
12113 devl_assert_locked(devlink);
12114 for (i = 0; i < groups_count; i++) {
12115 const struct devlink_trap_group *group = &groups[i];
12117 err = devlink_trap_group_verify(group);
12119 goto err_trap_group_verify;
12121 err = devlink_trap_group_register(devlink, group);
12123 goto err_trap_group_register;
12128 err_trap_group_register:
12129 err_trap_group_verify:
12130 for (i--; i >= 0; i--)
12131 devlink_trap_group_unregister(devlink, &groups[i]);
12134 EXPORT_SYMBOL_GPL(devl_trap_groups_register);
12137 * devlink_trap_groups_register - Register packet trap groups with devlink.
12138 * @devlink: devlink.
12139 * @groups: Packet trap groups.
12140 * @groups_count: Count of provided packet trap groups.
12142 * Context: Takes and release devlink->lock <mutex>.
12144 * Return: Non-zero value on failure.
12146 int devlink_trap_groups_register(struct devlink *devlink,
12147 const struct devlink_trap_group *groups,
12148 size_t groups_count)
12152 devl_lock(devlink);
12153 err = devl_trap_groups_register(devlink, groups, groups_count);
12154 devl_unlock(devlink);
12157 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
12160 * devl_trap_groups_unregister - Unregister packet trap groups from devlink.
12161 * @devlink: devlink.
12162 * @groups: Packet trap groups.
12163 * @groups_count: Count of provided packet trap groups.
12165 void devl_trap_groups_unregister(struct devlink *devlink,
12166 const struct devlink_trap_group *groups,
12167 size_t groups_count)
12171 devl_assert_locked(devlink);
12172 for (i = groups_count - 1; i >= 0; i--)
12173 devlink_trap_group_unregister(devlink, &groups[i]);
12175 EXPORT_SYMBOL_GPL(devl_trap_groups_unregister);
12178 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
12179 * @devlink: devlink.
12180 * @groups: Packet trap groups.
12181 * @groups_count: Count of provided packet trap groups.
12183 * Context: Takes and release devlink->lock <mutex>.
12185 void devlink_trap_groups_unregister(struct devlink *devlink,
12186 const struct devlink_trap_group *groups,
12187 size_t groups_count)
12189 devl_lock(devlink);
12190 devl_trap_groups_unregister(devlink, groups, groups_count);
12191 devl_unlock(devlink);
12193 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
12196 devlink_trap_policer_notify(struct devlink *devlink,
12197 const struct devlink_trap_policer_item *policer_item,
12198 enum devlink_command cmd)
12200 struct sk_buff *msg;
12203 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
12204 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
12205 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
12208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12212 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
12219 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
12220 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
12224 devlink_trap_policer_register(struct devlink *devlink,
12225 const struct devlink_trap_policer *policer)
12227 struct devlink_trap_policer_item *policer_item;
12230 if (devlink_trap_policer_item_lookup(devlink, policer->id))
12233 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
12237 policer_item->policer = policer;
12238 policer_item->rate = policer->init_rate;
12239 policer_item->burst = policer->init_burst;
12241 if (devlink->ops->trap_policer_init) {
12242 err = devlink->ops->trap_policer_init(devlink, policer);
12244 goto err_policer_init;
12247 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
12248 devlink_trap_policer_notify(devlink, policer_item,
12249 DEVLINK_CMD_TRAP_POLICER_NEW);
12254 kfree(policer_item);
12259 devlink_trap_policer_unregister(struct devlink *devlink,
12260 const struct devlink_trap_policer *policer)
12262 struct devlink_trap_policer_item *policer_item;
12264 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
12265 if (WARN_ON_ONCE(!policer_item))
12268 devlink_trap_policer_notify(devlink, policer_item,
12269 DEVLINK_CMD_TRAP_POLICER_DEL);
12270 list_del(&policer_item->list);
12271 if (devlink->ops->trap_policer_fini)
12272 devlink->ops->trap_policer_fini(devlink, policer);
12273 kfree(policer_item);
12277 * devl_trap_policers_register - Register packet trap policers with devlink.
12278 * @devlink: devlink.
12279 * @policers: Packet trap policers.
12280 * @policers_count: Count of provided packet trap policers.
12282 * Return: Non-zero value on failure.
12285 devl_trap_policers_register(struct devlink *devlink,
12286 const struct devlink_trap_policer *policers,
12287 size_t policers_count)
12291 devl_assert_locked(devlink);
12292 for (i = 0; i < policers_count; i++) {
12293 const struct devlink_trap_policer *policer = &policers[i];
12295 if (WARN_ON(policer->id == 0 ||
12296 policer->max_rate < policer->min_rate ||
12297 policer->max_burst < policer->min_burst)) {
12299 goto err_trap_policer_verify;
12302 err = devlink_trap_policer_register(devlink, policer);
12304 goto err_trap_policer_register;
12308 err_trap_policer_register:
12309 err_trap_policer_verify:
12310 for (i--; i >= 0; i--)
12311 devlink_trap_policer_unregister(devlink, &policers[i]);
12314 EXPORT_SYMBOL_GPL(devl_trap_policers_register);
12317 * devl_trap_policers_unregister - Unregister packet trap policers from devlink.
12318 * @devlink: devlink.
12319 * @policers: Packet trap policers.
12320 * @policers_count: Count of provided packet trap policers.
12323 devl_trap_policers_unregister(struct devlink *devlink,
12324 const struct devlink_trap_policer *policers,
12325 size_t policers_count)
12329 devl_assert_locked(devlink);
12330 for (i = policers_count - 1; i >= 0; i--)
12331 devlink_trap_policer_unregister(devlink, &policers[i]);
12333 EXPORT_SYMBOL_GPL(devl_trap_policers_unregister);
12335 static void __devlink_compat_running_version(struct devlink *devlink,
12336 char *buf, size_t len)
12338 struct devlink_info_req req = {};
12339 const struct nlattr *nlattr;
12340 struct sk_buff *msg;
12343 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12348 err = devlink->ops->info_get(devlink, &req, NULL);
12352 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12353 const struct nlattr *kv;
12356 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12359 nla_for_each_nested(kv, nlattr, rem_kv) {
12360 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12363 strlcat(buf, nla_data(kv), len);
12364 strlcat(buf, " ", len);
12371 static struct devlink_port *netdev_to_devlink_port(struct net_device *dev)
12373 if (!dev->netdev_ops->ndo_get_devlink_port)
12376 return dev->netdev_ops->ndo_get_devlink_port(dev);
12379 void devlink_compat_running_version(struct devlink *devlink,
12380 char *buf, size_t len)
12382 if (!devlink->ops->info_get)
12385 devl_lock(devlink);
12386 __devlink_compat_running_version(devlink, buf, len);
12387 devl_unlock(devlink);
12390 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12392 struct devlink_flash_update_params params = {};
12395 if (!devlink->ops->flash_update)
12396 return -EOPNOTSUPP;
12398 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
12402 devl_lock(devlink);
12403 devlink_flash_update_begin_notify(devlink);
12404 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
12405 devlink_flash_update_end_notify(devlink);
12406 devl_unlock(devlink);
12408 release_firmware(params.fw);
12413 int devlink_compat_phys_port_name_get(struct net_device *dev,
12414 char *name, size_t len)
12416 struct devlink_port *devlink_port;
12418 /* RTNL mutex is held here which ensures that devlink_port
12419 * instance cannot disappear in the middle. No need to take
12420 * any devlink lock as only permanent values are accessed.
12424 devlink_port = netdev_to_devlink_port(dev);
12426 return -EOPNOTSUPP;
12428 return __devlink_port_phys_port_name_get(devlink_port, name, len);
12431 int devlink_compat_switch_id_get(struct net_device *dev,
12432 struct netdev_phys_item_id *ppid)
12434 struct devlink_port *devlink_port;
12436 /* Caller must hold RTNL mutex or reference to dev, which ensures that
12437 * devlink_port instance cannot disappear in the middle. No need to take
12438 * any devlink lock as only permanent values are accessed.
12440 devlink_port = netdev_to_devlink_port(dev);
12441 if (!devlink_port || !devlink_port->switch_port)
12442 return -EOPNOTSUPP;
12444 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
12449 static void __net_exit devlink_pernet_pre_exit(struct net *net)
12451 struct devlink *devlink;
12452 u32 actions_performed;
12453 unsigned long index;
12456 /* In case network namespace is getting destroyed, reload
12457 * all devlink instances from this namespace into init_net.
12459 devlinks_xa_for_each_registered_get(net, index, devlink) {
12460 WARN_ON(!(devlink->features & DEVLINK_F_RELOAD));
12461 mutex_lock(&devlink->lock);
12462 err = devlink_reload(devlink, &init_net,
12463 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
12464 DEVLINK_RELOAD_LIMIT_UNSPEC,
12465 &actions_performed, NULL);
12466 mutex_unlock(&devlink->lock);
12467 if (err && err != -EOPNOTSUPP)
12468 pr_warn("Failed to reload devlink instance into init_net\n");
12469 devlink_put(devlink);
12473 static struct pernet_operations devlink_pernet_ops __net_initdata = {
12474 .pre_exit = devlink_pernet_pre_exit,
12477 static int __init devlink_init(void)
12481 err = genl_register_family(&devlink_nl_family);
12484 err = register_pernet_subsys(&devlink_pernet_ops);
12491 subsys_initcall(devlink_init);