1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/core/devlink.c - Network physical/parent device Netlink interface
5 * Heavily inspired by net/wireless/
6 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
7 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
10 #include <linux/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.
71 struct completion comp;
72 char priv[] __aligned(NETDEV_ALIGN);
75 struct devlink_linecard_ops;
76 struct devlink_linecard_type;
78 struct devlink_linecard {
79 struct list_head list;
80 struct devlink *devlink;
83 const struct devlink_linecard_ops *ops;
85 enum devlink_linecard_state state;
86 struct mutex state_lock; /* Protects state */
88 struct devlink_linecard_type *types;
89 unsigned int types_count;
93 * struct devlink_resource - devlink resource
94 * @name: name of the resource
95 * @id: id, per devlink instance
96 * @size: size of the resource
97 * @size_new: updated size of the resource, reload is needed
98 * @size_valid: valid in case the total size of the resource is valid
99 * including its children
100 * @parent: parent resource
101 * @size_params: size parameters
103 * @resource_list: list of child resources
104 * @occ_get: occupancy getter callback
105 * @occ_get_priv: occupancy getter callback priv
107 struct devlink_resource {
113 struct devlink_resource *parent;
114 struct devlink_resource_size_params size_params;
115 struct list_head list;
116 struct list_head resource_list;
117 devlink_resource_occ_get_t *occ_get;
121 void *devlink_priv(struct devlink *devlink)
123 return &devlink->priv;
125 EXPORT_SYMBOL_GPL(devlink_priv);
127 struct devlink *priv_to_devlink(void *priv)
129 return container_of(priv, struct devlink, priv);
131 EXPORT_SYMBOL_GPL(priv_to_devlink);
133 struct device *devlink_to_dev(const struct devlink *devlink)
137 EXPORT_SYMBOL_GPL(devlink_to_dev);
139 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
141 .name = "destination mac",
142 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
147 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
149 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
150 .fields = devlink_dpipe_fields_ethernet,
151 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
154 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
156 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
158 .name = "destination ip",
159 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
164 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
166 .id = DEVLINK_DPIPE_HEADER_IPV4,
167 .fields = devlink_dpipe_fields_ipv4,
168 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
171 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
173 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
175 .name = "destination ip",
176 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
181 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
183 .id = DEVLINK_DPIPE_HEADER_IPV6,
184 .fields = devlink_dpipe_fields_ipv6,
185 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
188 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
190 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
191 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
192 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
194 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
195 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
196 [DEVLINK_PORT_FN_ATTR_STATE] =
197 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
198 DEVLINK_PORT_FN_STATE_ACTIVE),
201 static DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
202 #define DEVLINK_REGISTERED XA_MARK_1
204 /* devlink instances are open to the access from the user space after
205 * devlink_register() call. Such logical barrier allows us to have certain
206 * expectations related to locking.
208 * Before *_register() - we are in initialization stage and no parallel
209 * access possible to the devlink instance. All drivers perform that phase
210 * by implicitly holding device_lock.
212 * After *_register() - users and driver can access devlink instance at
215 #define ASSERT_DEVLINK_REGISTERED(d) \
216 WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
217 #define ASSERT_DEVLINK_NOT_REGISTERED(d) \
218 WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
222 * An overall lock guarding every operation coming from userspace.
223 * It also guards devlink devices list and it is taken when
224 * driver registers/unregisters it.
226 static DEFINE_MUTEX(devlink_mutex);
228 struct net *devlink_net(const struct devlink *devlink)
230 return read_pnet(&devlink->_net);
232 EXPORT_SYMBOL_GPL(devlink_net);
234 void devlink_put(struct devlink *devlink)
236 if (refcount_dec_and_test(&devlink->refcount))
237 complete(&devlink->comp);
240 struct devlink *__must_check devlink_try_get(struct devlink *devlink)
242 if (refcount_inc_not_zero(&devlink->refcount))
247 void devl_assert_locked(struct devlink *devlink)
249 lockdep_assert_held(&devlink->lock);
251 EXPORT_SYMBOL_GPL(devl_assert_locked);
253 #ifdef CONFIG_LOCKDEP
254 /* For use in conjunction with LOCKDEP only e.g. rcu_dereference_protected() */
255 bool devl_lock_is_held(struct devlink *devlink)
257 return lockdep_is_held(&devlink->lock);
259 EXPORT_SYMBOL_GPL(devl_lock_is_held);
262 void devl_lock(struct devlink *devlink)
264 mutex_lock(&devlink->lock);
266 EXPORT_SYMBOL_GPL(devl_lock);
268 void devl_unlock(struct devlink *devlink)
270 mutex_unlock(&devlink->lock);
272 EXPORT_SYMBOL_GPL(devl_unlock);
274 static struct devlink *devlink_get_from_attrs(struct net *net,
275 struct nlattr **attrs)
277 struct devlink *devlink;
283 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
284 return ERR_PTR(-EINVAL);
286 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
287 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
289 lockdep_assert_held(&devlink_mutex);
291 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
292 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
293 strcmp(dev_name(devlink->dev), devname) == 0 &&
294 net_eq(devlink_net(devlink), net)) {
300 if (!found || !devlink_try_get(devlink))
301 devlink = ERR_PTR(-ENODEV);
306 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
307 unsigned int port_index)
309 struct devlink_port *devlink_port;
311 list_for_each_entry(devlink_port, &devlink->port_list, list) {
312 if (devlink_port->index == port_index)
318 static bool devlink_port_index_exists(struct devlink *devlink,
319 unsigned int port_index)
321 return devlink_port_get_by_index(devlink, port_index);
324 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
325 struct nlattr **attrs)
327 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
328 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
329 struct devlink_port *devlink_port;
331 devlink_port = devlink_port_get_by_index(devlink, port_index);
333 return ERR_PTR(-ENODEV);
336 return ERR_PTR(-EINVAL);
339 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
340 struct genl_info *info)
342 return devlink_port_get_from_attrs(devlink, info->attrs);
346 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
348 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
352 devlink_rate_is_node(struct devlink_rate *devlink_rate)
354 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
357 static struct devlink_rate *
358 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
360 struct devlink_rate *devlink_rate;
361 struct devlink_port *devlink_port;
363 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
364 if (IS_ERR(devlink_port))
365 return ERR_CAST(devlink_port);
366 devlink_rate = devlink_port->devlink_rate;
367 return devlink_rate ?: ERR_PTR(-ENODEV);
370 static struct devlink_rate *
371 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
373 static struct devlink_rate *devlink_rate;
375 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
376 if (devlink_rate_is_node(devlink_rate) &&
377 !strcmp(node_name, devlink_rate->name))
380 return ERR_PTR(-ENODEV);
383 static struct devlink_rate *
384 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
386 const char *rate_node_name;
389 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
390 return ERR_PTR(-EINVAL);
391 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
392 len = strlen(rate_node_name);
393 /* Name cannot be empty or decimal number */
394 if (!len || strspn(rate_node_name, "0123456789") == len)
395 return ERR_PTR(-EINVAL);
397 return devlink_rate_node_get_by_name(devlink, rate_node_name);
400 static struct devlink_rate *
401 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
403 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
406 static struct devlink_rate *
407 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
409 struct nlattr **attrs = info->attrs;
411 if (attrs[DEVLINK_ATTR_PORT_INDEX])
412 return devlink_rate_leaf_get_from_info(devlink, info);
413 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
414 return devlink_rate_node_get_from_info(devlink, info);
416 return ERR_PTR(-EINVAL);
419 static struct devlink_linecard *
420 devlink_linecard_get_by_index(struct devlink *devlink,
421 unsigned int linecard_index)
423 struct devlink_linecard *devlink_linecard;
425 list_for_each_entry(devlink_linecard, &devlink->linecard_list, list) {
426 if (devlink_linecard->index == linecard_index)
427 return devlink_linecard;
432 static bool devlink_linecard_index_exists(struct devlink *devlink,
433 unsigned int linecard_index)
435 return devlink_linecard_get_by_index(devlink, linecard_index);
438 static struct devlink_linecard *
439 devlink_linecard_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
441 if (attrs[DEVLINK_ATTR_LINECARD_INDEX]) {
442 u32 linecard_index = nla_get_u32(attrs[DEVLINK_ATTR_LINECARD_INDEX]);
443 struct devlink_linecard *linecard;
445 mutex_lock(&devlink->linecards_lock);
446 linecard = devlink_linecard_get_by_index(devlink, linecard_index);
448 refcount_inc(&linecard->refcount);
449 mutex_unlock(&devlink->linecards_lock);
451 return ERR_PTR(-ENODEV);
454 return ERR_PTR(-EINVAL);
457 static struct devlink_linecard *
458 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info)
460 return devlink_linecard_get_from_attrs(devlink, info->attrs);
463 static void devlink_linecard_put(struct devlink_linecard *linecard)
465 if (refcount_dec_and_test(&linecard->refcount)) {
466 mutex_destroy(&linecard->state_lock);
472 struct list_head list;
475 u16 ingress_pools_count;
476 u16 egress_pools_count;
477 u16 ingress_tc_count;
481 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
483 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
486 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
487 unsigned int sb_index)
489 struct devlink_sb *devlink_sb;
491 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
492 if (devlink_sb->index == sb_index)
498 static bool devlink_sb_index_exists(struct devlink *devlink,
499 unsigned int sb_index)
501 return devlink_sb_get_by_index(devlink, sb_index);
504 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
505 struct nlattr **attrs)
507 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
508 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
509 struct devlink_sb *devlink_sb;
511 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
513 return ERR_PTR(-ENODEV);
516 return ERR_PTR(-EINVAL);
519 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
520 struct genl_info *info)
522 return devlink_sb_get_from_attrs(devlink, info->attrs);
525 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
526 struct nlattr **attrs,
531 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
534 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
535 if (val >= devlink_sb_pool_count(devlink_sb))
541 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
542 struct genl_info *info,
545 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
550 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
551 enum devlink_sb_pool_type *p_pool_type)
555 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
558 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
559 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
560 val != DEVLINK_SB_POOL_TYPE_EGRESS)
567 devlink_sb_pool_type_get_from_info(struct genl_info *info,
568 enum devlink_sb_pool_type *p_pool_type)
570 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
574 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
575 enum devlink_sb_threshold_type *p_th_type)
579 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
582 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
583 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
584 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
591 devlink_sb_th_type_get_from_info(struct genl_info *info,
592 enum devlink_sb_threshold_type *p_th_type)
594 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
598 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
599 struct nlattr **attrs,
600 enum devlink_sb_pool_type pool_type,
605 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
608 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
609 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
610 val >= devlink_sb->ingress_tc_count)
612 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
613 val >= devlink_sb->egress_tc_count)
620 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
621 struct genl_info *info,
622 enum devlink_sb_pool_type pool_type,
625 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
626 pool_type, p_tc_index);
629 struct devlink_region {
630 struct devlink *devlink;
631 struct devlink_port *port;
632 struct list_head list;
634 const struct devlink_region_ops *ops;
635 const struct devlink_port_region_ops *port_ops;
637 struct list_head snapshot_list;
643 struct devlink_snapshot {
644 struct list_head list;
645 struct devlink_region *region;
650 static struct devlink_region *
651 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
653 struct devlink_region *region;
655 list_for_each_entry(region, &devlink->region_list, list)
656 if (!strcmp(region->ops->name, region_name))
662 static struct devlink_region *
663 devlink_port_region_get_by_name(struct devlink_port *port,
664 const char *region_name)
666 struct devlink_region *region;
668 list_for_each_entry(region, &port->region_list, list)
669 if (!strcmp(region->ops->name, region_name))
675 static struct devlink_snapshot *
676 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
678 struct devlink_snapshot *snapshot;
680 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
681 if (snapshot->id == id)
687 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
688 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
689 #define DEVLINK_NL_FLAG_NEED_RATE BIT(2)
690 #define DEVLINK_NL_FLAG_NEED_RATE_NODE BIT(3)
691 #define DEVLINK_NL_FLAG_NEED_LINECARD BIT(4)
693 /* The per devlink instance lock is taken by default in the pre-doit
694 * operation, yet several commands do not require this. The global
695 * devlink lock is taken and protects from disruption by user-calls.
697 #define DEVLINK_NL_FLAG_NO_LOCK BIT(5)
699 static int devlink_nl_pre_doit(const struct genl_ops *ops,
700 struct sk_buff *skb, struct genl_info *info)
702 struct devlink_linecard *linecard;
703 struct devlink_port *devlink_port;
704 struct devlink *devlink;
707 mutex_lock(&devlink_mutex);
708 devlink = devlink_get_from_attrs(genl_info_net(info), info->attrs);
709 if (IS_ERR(devlink)) {
710 mutex_unlock(&devlink_mutex);
711 return PTR_ERR(devlink);
713 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
714 mutex_lock(&devlink->lock);
715 info->user_ptr[0] = devlink;
716 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
717 devlink_port = devlink_port_get_from_info(devlink, info);
718 if (IS_ERR(devlink_port)) {
719 err = PTR_ERR(devlink_port);
722 info->user_ptr[1] = devlink_port;
723 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
724 devlink_port = devlink_port_get_from_info(devlink, info);
725 if (!IS_ERR(devlink_port))
726 info->user_ptr[1] = devlink_port;
727 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE) {
728 struct devlink_rate *devlink_rate;
730 devlink_rate = devlink_rate_get_from_info(devlink, info);
731 if (IS_ERR(devlink_rate)) {
732 err = PTR_ERR(devlink_rate);
735 info->user_ptr[1] = devlink_rate;
736 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE_NODE) {
737 struct devlink_rate *rate_node;
739 rate_node = devlink_rate_node_get_from_info(devlink, info);
740 if (IS_ERR(rate_node)) {
741 err = PTR_ERR(rate_node);
744 info->user_ptr[1] = rate_node;
745 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_LINECARD) {
746 linecard = devlink_linecard_get_from_info(devlink, info);
747 if (IS_ERR(linecard)) {
748 err = PTR_ERR(linecard);
751 info->user_ptr[1] = linecard;
756 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
757 mutex_unlock(&devlink->lock);
758 devlink_put(devlink);
759 mutex_unlock(&devlink_mutex);
763 static void devlink_nl_post_doit(const struct genl_ops *ops,
764 struct sk_buff *skb, struct genl_info *info)
766 struct devlink_linecard *linecard;
767 struct devlink *devlink;
769 devlink = info->user_ptr[0];
770 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_LINECARD) {
771 linecard = info->user_ptr[1];
772 devlink_linecard_put(linecard);
774 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
775 mutex_unlock(&devlink->lock);
776 devlink_put(devlink);
777 mutex_unlock(&devlink_mutex);
780 static struct genl_family devlink_nl_family;
782 enum devlink_multicast_groups {
783 DEVLINK_MCGRP_CONFIG,
786 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
787 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
790 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
792 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
794 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
799 struct devlink_reload_combination {
800 enum devlink_reload_action action;
801 enum devlink_reload_limit limit;
804 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
806 /* can't reinitialize driver with no down time */
807 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
808 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
813 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
814 enum devlink_reload_limit limit)
818 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
819 if (devlink_reload_invalid_combinations[i].action == action &&
820 devlink_reload_invalid_combinations[i].limit == limit)
826 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
828 return test_bit(action, &devlink->ops->reload_actions);
832 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
834 return test_bit(limit, &devlink->ops->reload_limits);
837 static int devlink_reload_stat_put(struct sk_buff *msg,
838 enum devlink_reload_limit limit, u32 value)
840 struct nlattr *reload_stats_entry;
842 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
843 if (!reload_stats_entry)
846 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
847 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
848 goto nla_put_failure;
849 nla_nest_end(msg, reload_stats_entry);
853 nla_nest_cancel(msg, reload_stats_entry);
857 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
859 struct nlattr *reload_stats_attr, *act_info, *act_stats;
864 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
866 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
868 if (!reload_stats_attr)
871 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
873 !devlink_reload_action_is_supported(devlink, i)) ||
874 i == DEVLINK_RELOAD_ACTION_UNSPEC)
876 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
878 goto nla_put_failure;
880 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
881 goto action_info_nest_cancel;
882 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
884 goto action_info_nest_cancel;
886 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
887 /* Remote stats are shown even if not locally supported.
888 * Stats of actions with unspecified limit are shown
889 * though drivers don't need to register unspecified
892 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
893 !devlink_reload_limit_is_supported(devlink, j)) ||
894 devlink_reload_combination_is_invalid(i, j))
897 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
899 value = devlink->stats.reload_stats[stat_idx];
901 value = devlink->stats.remote_reload_stats[stat_idx];
902 if (devlink_reload_stat_put(msg, j, value))
903 goto action_stats_nest_cancel;
905 nla_nest_end(msg, act_stats);
906 nla_nest_end(msg, act_info);
908 nla_nest_end(msg, reload_stats_attr);
911 action_stats_nest_cancel:
912 nla_nest_cancel(msg, act_stats);
913 action_info_nest_cancel:
914 nla_nest_cancel(msg, act_info);
916 nla_nest_cancel(msg, reload_stats_attr);
920 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
921 enum devlink_command cmd, u32 portid,
924 struct nlattr *dev_stats;
927 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
931 if (devlink_nl_put_handle(msg, devlink))
932 goto nla_put_failure;
933 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
934 goto nla_put_failure;
936 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
938 goto nla_put_failure;
940 if (devlink_reload_stats_put(msg, devlink, false))
941 goto dev_stats_nest_cancel;
942 if (devlink_reload_stats_put(msg, devlink, true))
943 goto dev_stats_nest_cancel;
945 nla_nest_end(msg, dev_stats);
946 genlmsg_end(msg, hdr);
949 dev_stats_nest_cancel:
950 nla_nest_cancel(msg, dev_stats);
952 genlmsg_cancel(msg, hdr);
956 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
961 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
962 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
964 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
968 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
974 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
975 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
978 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
979 struct devlink_port *devlink_port)
981 struct devlink_port_attrs *attrs = &devlink_port->attrs;
983 if (!devlink_port->attrs_set)
986 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
989 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
991 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
993 switch (devlink_port->attrs.flavour) {
994 case DEVLINK_PORT_FLAVOUR_PCI_PF:
995 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
996 attrs->pci_pf.controller) ||
997 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
999 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
1002 case DEVLINK_PORT_FLAVOUR_PCI_VF:
1003 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
1004 attrs->pci_vf.controller) ||
1005 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
1006 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
1008 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
1011 case DEVLINK_PORT_FLAVOUR_PCI_SF:
1012 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
1013 attrs->pci_sf.controller) ||
1014 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
1015 attrs->pci_sf.pf) ||
1016 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
1020 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
1021 case DEVLINK_PORT_FLAVOUR_CPU:
1022 case DEVLINK_PORT_FLAVOUR_DSA:
1023 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
1024 attrs->phys.port_number))
1028 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
1029 attrs->phys.port_number))
1031 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
1032 attrs->phys.split_subport_number))
1041 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
1042 struct devlink_port *port,
1043 struct sk_buff *msg,
1044 struct netlink_ext_ack *extack,
1047 u8 hw_addr[MAX_ADDR_LEN];
1051 if (!ops->port_function_hw_addr_get)
1054 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
1057 if (err == -EOPNOTSUPP)
1061 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
1064 *msg_updated = true;
1068 static int devlink_nl_rate_fill(struct sk_buff *msg,
1069 struct devlink_rate *devlink_rate,
1070 enum devlink_command cmd, u32 portid, u32 seq,
1071 int flags, struct netlink_ext_ack *extack)
1073 struct devlink *devlink = devlink_rate->devlink;
1076 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1080 if (devlink_nl_put_handle(msg, devlink))
1081 goto nla_put_failure;
1083 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
1084 goto nla_put_failure;
1086 if (devlink_rate_is_leaf(devlink_rate)) {
1087 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
1088 devlink_rate->devlink_port->index))
1089 goto nla_put_failure;
1090 } else if (devlink_rate_is_node(devlink_rate)) {
1091 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
1092 devlink_rate->name))
1093 goto nla_put_failure;
1096 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
1097 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
1098 goto nla_put_failure;
1100 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
1101 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
1102 goto nla_put_failure;
1104 if (devlink_rate->parent)
1105 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
1106 devlink_rate->parent->name))
1107 goto nla_put_failure;
1109 genlmsg_end(msg, hdr);
1113 genlmsg_cancel(msg, hdr);
1118 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
1120 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
1121 state == DEVLINK_PORT_FN_STATE_ACTIVE;
1125 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
1127 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
1128 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
1131 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
1132 struct devlink_port *port,
1133 struct sk_buff *msg,
1134 struct netlink_ext_ack *extack,
1137 enum devlink_port_fn_opstate opstate;
1138 enum devlink_port_fn_state state;
1141 if (!ops->port_fn_state_get)
1144 err = ops->port_fn_state_get(port, &state, &opstate, extack);
1146 if (err == -EOPNOTSUPP)
1150 if (!devlink_port_fn_state_valid(state)) {
1152 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
1155 if (!devlink_port_fn_opstate_valid(opstate)) {
1157 NL_SET_ERR_MSG_MOD(extack,
1158 "Invalid operational state read from driver");
1161 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
1162 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
1164 *msg_updated = true;
1169 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
1170 struct netlink_ext_ack *extack)
1172 const struct devlink_ops *ops;
1173 struct nlattr *function_attr;
1174 bool msg_updated = false;
1177 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
1181 ops = port->devlink->ops;
1182 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
1186 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
1188 if (err || !msg_updated)
1189 nla_nest_cancel(msg, function_attr);
1191 nla_nest_end(msg, function_attr);
1195 static int devlink_nl_port_fill(struct sk_buff *msg,
1196 struct devlink_port *devlink_port,
1197 enum devlink_command cmd, u32 portid, u32 seq,
1198 int flags, struct netlink_ext_ack *extack)
1200 struct devlink *devlink = devlink_port->devlink;
1203 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1207 if (devlink_nl_put_handle(msg, devlink))
1208 goto nla_put_failure;
1209 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1210 goto nla_put_failure;
1212 /* Hold rtnl lock while accessing port's netdev attributes. */
1214 spin_lock_bh(&devlink_port->type_lock);
1215 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
1216 goto nla_put_failure_type_locked;
1217 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1218 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1219 devlink_port->desired_type))
1220 goto nla_put_failure_type_locked;
1221 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1222 struct net *net = devlink_net(devlink_port->devlink);
1223 struct net_device *netdev = devlink_port->type_dev;
1225 if (netdev && net_eq(net, dev_net(netdev)) &&
1226 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1228 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1230 goto nla_put_failure_type_locked;
1232 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1233 struct ib_device *ibdev = devlink_port->type_dev;
1236 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1238 goto nla_put_failure_type_locked;
1240 spin_unlock_bh(&devlink_port->type_lock);
1242 if (devlink_nl_port_attrs_put(msg, devlink_port))
1243 goto nla_put_failure;
1244 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1245 goto nla_put_failure;
1246 if (devlink_port->linecard &&
1247 nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
1248 devlink_port->linecard->index))
1249 goto nla_put_failure;
1251 genlmsg_end(msg, hdr);
1254 nla_put_failure_type_locked:
1255 spin_unlock_bh(&devlink_port->type_lock);
1258 genlmsg_cancel(msg, hdr);
1262 static void devlink_port_notify(struct devlink_port *devlink_port,
1263 enum devlink_command cmd)
1265 struct devlink *devlink = devlink_port->devlink;
1266 struct sk_buff *msg;
1269 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1271 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1274 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1278 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1284 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1285 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1288 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1289 enum devlink_command cmd)
1291 struct devlink *devlink = devlink_rate->devlink;
1292 struct sk_buff *msg;
1295 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1297 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1300 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1304 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1310 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1311 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1314 static int devlink_nl_cmd_rate_get_dumpit(struct sk_buff *msg,
1315 struct netlink_callback *cb)
1317 struct devlink_rate *devlink_rate;
1318 struct devlink *devlink;
1319 int start = cb->args[0];
1320 unsigned long index;
1324 mutex_lock(&devlink_mutex);
1325 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1326 if (!devlink_try_get(devlink))
1329 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1332 mutex_lock(&devlink->lock);
1333 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1334 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1335 u32 id = NETLINK_CB(cb->skb).portid;
1341 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1345 mutex_unlock(&devlink->lock);
1346 devlink_put(devlink);
1351 mutex_unlock(&devlink->lock);
1353 devlink_put(devlink);
1356 mutex_unlock(&devlink_mutex);
1357 if (err != -EMSGSIZE)
1364 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1365 struct genl_info *info)
1367 struct devlink_rate *devlink_rate = info->user_ptr[1];
1368 struct sk_buff *msg;
1371 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1375 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1376 info->snd_portid, info->snd_seq, 0,
1383 return genlmsg_reply(msg, info);
1387 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1388 struct devlink_rate *parent)
1391 if (parent == devlink_rate)
1393 parent = parent->parent;
1398 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1400 struct devlink *devlink = info->user_ptr[0];
1401 struct sk_buff *msg;
1404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1408 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1409 info->snd_portid, info->snd_seq, 0);
1415 return genlmsg_reply(msg, info);
1418 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
1419 struct netlink_callback *cb)
1421 struct devlink *devlink;
1422 int start = cb->args[0];
1423 unsigned long index;
1427 mutex_lock(&devlink_mutex);
1428 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1429 if (!devlink_try_get(devlink))
1432 if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) {
1433 devlink_put(devlink);
1439 devlink_put(devlink);
1443 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1444 NETLINK_CB(cb->skb).portid,
1445 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1446 devlink_put(devlink);
1452 mutex_unlock(&devlink_mutex);
1458 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1459 struct genl_info *info)
1461 struct devlink_port *devlink_port = info->user_ptr[1];
1462 struct sk_buff *msg;
1465 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1469 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1470 info->snd_portid, info->snd_seq, 0,
1477 return genlmsg_reply(msg, info);
1480 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
1481 struct netlink_callback *cb)
1483 struct devlink *devlink;
1484 struct devlink_port *devlink_port;
1485 int start = cb->args[0];
1486 unsigned long index;
1490 mutex_lock(&devlink_mutex);
1491 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1492 if (!devlink_try_get(devlink))
1495 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1498 mutex_lock(&devlink->lock);
1499 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1504 err = devlink_nl_port_fill(msg, devlink_port,
1506 NETLINK_CB(cb->skb).portid,
1508 NLM_F_MULTI, cb->extack);
1510 mutex_unlock(&devlink->lock);
1511 devlink_put(devlink);
1516 mutex_unlock(&devlink->lock);
1518 devlink_put(devlink);
1521 mutex_unlock(&devlink_mutex);
1527 static int devlink_port_type_set(struct devlink_port *devlink_port,
1528 enum devlink_port_type port_type)
1533 if (!devlink_port->devlink->ops->port_type_set)
1536 if (port_type == devlink_port->type)
1539 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1544 devlink_port->desired_type = port_type;
1545 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1549 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1550 const struct nlattr *attr,
1551 struct netlink_ext_ack *extack)
1553 const struct devlink_ops *ops = port->devlink->ops;
1557 hw_addr = nla_data(attr);
1558 hw_addr_len = nla_len(attr);
1559 if (hw_addr_len > MAX_ADDR_LEN) {
1560 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1563 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1564 if (hw_addr_len != ETH_ALEN) {
1565 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1568 if (!is_unicast_ether_addr(hw_addr)) {
1569 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1574 if (!ops->port_function_hw_addr_set) {
1575 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
1579 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1583 static int devlink_port_fn_state_set(struct devlink_port *port,
1584 const struct nlattr *attr,
1585 struct netlink_ext_ack *extack)
1587 enum devlink_port_fn_state state;
1588 const struct devlink_ops *ops;
1590 state = nla_get_u8(attr);
1591 ops = port->devlink->ops;
1592 if (!ops->port_fn_state_set) {
1593 NL_SET_ERR_MSG_MOD(extack,
1594 "Function does not support state setting");
1597 return ops->port_fn_state_set(port, state, extack);
1600 static int devlink_port_function_set(struct devlink_port *port,
1601 const struct nlattr *attr,
1602 struct netlink_ext_ack *extack)
1604 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1607 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1608 devlink_function_nl_policy, extack);
1610 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1614 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1616 err = devlink_port_function_hw_addr_set(port, attr, extack);
1620 /* Keep this as the last function attribute set, so that when
1621 * multiple port function attributes are set along with state,
1622 * Those can be applied first before activating the state.
1624 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1626 err = devlink_port_fn_state_set(port, attr, extack);
1629 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1633 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1634 struct genl_info *info)
1636 struct devlink_port *devlink_port = info->user_ptr[1];
1639 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1640 enum devlink_port_type port_type;
1642 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1643 err = devlink_port_type_set(devlink_port, port_type);
1648 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1649 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1650 struct netlink_ext_ack *extack = info->extack;
1652 err = devlink_port_function_set(devlink_port, attr, extack);
1660 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1661 struct genl_info *info)
1663 struct devlink_port *devlink_port = info->user_ptr[1];
1664 struct devlink *devlink = info->user_ptr[0];
1667 if (!info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
1669 if (!devlink->ops->port_split)
1672 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1674 if (!devlink_port->attrs.splittable) {
1675 /* Split ports cannot be split. */
1676 if (devlink_port->attrs.split)
1677 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1679 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1683 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1684 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1688 return devlink->ops->port_split(devlink, devlink_port, count,
1692 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1693 struct genl_info *info)
1695 struct devlink_port *devlink_port = info->user_ptr[1];
1696 struct devlink *devlink = info->user_ptr[0];
1698 if (!devlink->ops->port_unsplit)
1700 return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1703 static int devlink_port_new_notifiy(struct devlink *devlink,
1704 unsigned int port_index,
1705 struct genl_info *info)
1707 struct devlink_port *devlink_port;
1708 struct sk_buff *msg;
1711 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1715 mutex_lock(&devlink->lock);
1716 devlink_port = devlink_port_get_by_index(devlink, port_index);
1717 if (!devlink_port) {
1722 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1723 info->snd_portid, info->snd_seq, 0, NULL);
1727 err = genlmsg_reply(msg, info);
1728 mutex_unlock(&devlink->lock);
1732 mutex_unlock(&devlink->lock);
1737 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1738 struct genl_info *info)
1740 struct netlink_ext_ack *extack = info->extack;
1741 struct devlink_port_new_attrs new_attrs = {};
1742 struct devlink *devlink = info->user_ptr[0];
1743 unsigned int new_port_index;
1746 if (!devlink->ops->port_new || !devlink->ops->port_del)
1749 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1750 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1751 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1754 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1756 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1758 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1759 /* Port index of the new port being created by driver. */
1760 new_attrs.port_index =
1761 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1762 new_attrs.port_index_valid = true;
1764 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1765 new_attrs.controller =
1766 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1767 new_attrs.controller_valid = true;
1769 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1770 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1771 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1772 new_attrs.sfnum_valid = true;
1775 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1780 err = devlink_port_new_notifiy(devlink, new_port_index, info);
1781 if (err && err != -ENODEV) {
1782 /* Fail to send the response; destroy newly created port. */
1783 devlink->ops->port_del(devlink, new_port_index, extack);
1788 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1789 struct genl_info *info)
1791 struct netlink_ext_ack *extack = info->extack;
1792 struct devlink *devlink = info->user_ptr[0];
1793 unsigned int port_index;
1795 if (!devlink->ops->port_del)
1798 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1799 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1802 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1804 return devlink->ops->port_del(devlink, port_index, extack);
1808 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1809 struct genl_info *info,
1810 struct nlattr *nla_parent)
1812 struct devlink *devlink = devlink_rate->devlink;
1813 const char *parent_name = nla_data(nla_parent);
1814 const struct devlink_ops *ops = devlink->ops;
1815 size_t len = strlen(parent_name);
1816 struct devlink_rate *parent;
1817 int err = -EOPNOTSUPP;
1819 parent = devlink_rate->parent;
1820 if (parent && len) {
1821 NL_SET_ERR_MSG_MOD(info->extack, "Rate object already has parent.");
1823 } else if (parent && !len) {
1824 if (devlink_rate_is_leaf(devlink_rate))
1825 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1826 devlink_rate->priv, NULL,
1828 else if (devlink_rate_is_node(devlink_rate))
1829 err = ops->rate_node_parent_set(devlink_rate, NULL,
1830 devlink_rate->priv, NULL,
1835 refcount_dec(&parent->refcnt);
1836 devlink_rate->parent = NULL;
1837 } else if (!parent && len) {
1838 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1842 if (parent == devlink_rate) {
1843 NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1847 if (devlink_rate_is_node(devlink_rate) &&
1848 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1849 NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1853 if (devlink_rate_is_leaf(devlink_rate))
1854 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1855 devlink_rate->priv, parent->priv,
1857 else if (devlink_rate_is_node(devlink_rate))
1858 err = ops->rate_node_parent_set(devlink_rate, parent,
1859 devlink_rate->priv, parent->priv,
1864 refcount_inc(&parent->refcnt);
1865 devlink_rate->parent = parent;
1871 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1872 const struct devlink_ops *ops,
1873 struct genl_info *info)
1875 struct nlattr *nla_parent, **attrs = info->attrs;
1876 int err = -EOPNOTSUPP;
1879 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1880 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1881 if (devlink_rate_is_leaf(devlink_rate))
1882 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1883 rate, info->extack);
1884 else if (devlink_rate_is_node(devlink_rate))
1885 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1886 rate, info->extack);
1889 devlink_rate->tx_share = rate;
1892 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1893 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1894 if (devlink_rate_is_leaf(devlink_rate))
1895 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1896 rate, info->extack);
1897 else if (devlink_rate_is_node(devlink_rate))
1898 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1899 rate, info->extack);
1902 devlink_rate->tx_max = rate;
1905 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1907 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1916 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1917 struct genl_info *info,
1918 enum devlink_rate_type type)
1920 struct nlattr **attrs = info->attrs;
1922 if (type == DEVLINK_RATE_TYPE_LEAF) {
1923 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1924 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1927 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1928 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1931 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1932 !ops->rate_leaf_parent_set) {
1933 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1936 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1937 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1938 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1941 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1942 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1945 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1946 !ops->rate_node_parent_set) {
1947 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1951 WARN(1, "Unknown type of rate object");
1958 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1959 struct genl_info *info)
1961 struct devlink_rate *devlink_rate = info->user_ptr[1];
1962 struct devlink *devlink = devlink_rate->devlink;
1963 const struct devlink_ops *ops = devlink->ops;
1966 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1969 err = devlink_nl_rate_set(devlink_rate, ops, info);
1972 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1976 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1977 struct genl_info *info)
1979 struct devlink *devlink = info->user_ptr[0];
1980 struct devlink_rate *rate_node;
1981 const struct devlink_ops *ops;
1985 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1986 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
1990 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1993 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1994 if (!IS_ERR(rate_node))
1996 else if (rate_node == ERR_PTR(-EINVAL))
1999 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
2003 rate_node->devlink = devlink;
2004 rate_node->type = DEVLINK_RATE_TYPE_NODE;
2005 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
2006 if (!rate_node->name) {
2011 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
2015 err = devlink_nl_rate_set(rate_node, ops, info);
2019 refcount_set(&rate_node->refcnt, 1);
2020 list_add(&rate_node->list, &devlink->rate_list);
2021 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
2025 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2027 kfree(rate_node->name);
2033 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
2034 struct genl_info *info)
2036 struct devlink_rate *rate_node = info->user_ptr[1];
2037 struct devlink *devlink = rate_node->devlink;
2038 const struct devlink_ops *ops = devlink->ops;
2041 if (refcount_read(&rate_node->refcnt) > 1) {
2042 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
2046 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
2047 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2048 if (rate_node->parent)
2049 refcount_dec(&rate_node->parent->refcnt);
2050 list_del(&rate_node->list);
2051 kfree(rate_node->name);
2056 struct devlink_linecard_type {
2061 static int devlink_nl_linecard_fill(struct sk_buff *msg,
2062 struct devlink *devlink,
2063 struct devlink_linecard *linecard,
2064 enum devlink_command cmd, u32 portid,
2066 struct netlink_ext_ack *extack)
2068 struct devlink_linecard_type *linecard_type;
2069 struct nlattr *attr;
2073 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2077 if (devlink_nl_put_handle(msg, devlink))
2078 goto nla_put_failure;
2079 if (nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, linecard->index))
2080 goto nla_put_failure;
2081 if (nla_put_u8(msg, DEVLINK_ATTR_LINECARD_STATE, linecard->state))
2082 goto nla_put_failure;
2083 if (linecard->type &&
2084 nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE, linecard->type))
2085 goto nla_put_failure;
2087 if (linecard->types_count) {
2088 attr = nla_nest_start(msg,
2089 DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES);
2091 goto nla_put_failure;
2092 for (i = 0; i < linecard->types_count; i++) {
2093 linecard_type = &linecard->types[i];
2094 if (nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE,
2095 linecard_type->type)) {
2096 nla_nest_cancel(msg, attr);
2097 goto nla_put_failure;
2100 nla_nest_end(msg, attr);
2103 genlmsg_end(msg, hdr);
2107 genlmsg_cancel(msg, hdr);
2111 static void devlink_linecard_notify(struct devlink_linecard *linecard,
2112 enum devlink_command cmd)
2114 struct devlink *devlink = linecard->devlink;
2115 struct sk_buff *msg;
2118 WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW &&
2119 cmd != DEVLINK_CMD_LINECARD_DEL);
2121 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
2124 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2128 err = devlink_nl_linecard_fill(msg, devlink, linecard, cmd, 0, 0, 0,
2135 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2136 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2139 static int devlink_nl_cmd_linecard_get_doit(struct sk_buff *skb,
2140 struct genl_info *info)
2142 struct devlink_linecard *linecard = info->user_ptr[1];
2143 struct devlink *devlink = linecard->devlink;
2144 struct sk_buff *msg;
2147 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2151 mutex_lock(&linecard->state_lock);
2152 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2153 DEVLINK_CMD_LINECARD_NEW,
2154 info->snd_portid, info->snd_seq, 0,
2156 mutex_unlock(&linecard->state_lock);
2162 return genlmsg_reply(msg, info);
2165 static int devlink_nl_cmd_linecard_get_dumpit(struct sk_buff *msg,
2166 struct netlink_callback *cb)
2168 struct devlink_linecard *linecard;
2169 struct devlink *devlink;
2170 int start = cb->args[0];
2171 unsigned long index;
2175 mutex_lock(&devlink_mutex);
2176 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2177 if (!devlink_try_get(devlink))
2180 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2183 mutex_lock(&devlink->linecards_lock);
2184 list_for_each_entry(linecard, &devlink->linecard_list, list) {
2189 mutex_lock(&linecard->state_lock);
2190 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2191 DEVLINK_CMD_LINECARD_NEW,
2192 NETLINK_CB(cb->skb).portid,
2196 mutex_unlock(&linecard->state_lock);
2198 mutex_unlock(&devlink->linecards_lock);
2199 devlink_put(devlink);
2204 mutex_unlock(&devlink->linecards_lock);
2206 devlink_put(devlink);
2209 mutex_unlock(&devlink_mutex);
2215 static struct devlink_linecard_type *
2216 devlink_linecard_type_lookup(struct devlink_linecard *linecard,
2219 struct devlink_linecard_type *linecard_type;
2222 for (i = 0; i < linecard->types_count; i++) {
2223 linecard_type = &linecard->types[i];
2224 if (!strcmp(type, linecard_type->type))
2225 return linecard_type;
2230 static int devlink_linecard_type_set(struct devlink_linecard *linecard,
2232 struct netlink_ext_ack *extack)
2234 const struct devlink_linecard_ops *ops = linecard->ops;
2235 struct devlink_linecard_type *linecard_type;
2238 mutex_lock(&linecard->state_lock);
2239 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2240 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2244 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2245 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2250 linecard_type = devlink_linecard_type_lookup(linecard, type);
2251 if (!linecard_type) {
2252 NL_SET_ERR_MSG_MOD(extack, "Unsupported line card type provided");
2257 if (linecard->state != DEVLINK_LINECARD_STATE_UNPROVISIONED &&
2258 linecard->state != DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2259 NL_SET_ERR_MSG_MOD(extack, "Line card already provisioned");
2261 /* Check if the line card is provisioned in the same
2262 * way the user asks. In case it is, make the operation
2263 * to return success.
2265 if (ops->same_provision &&
2266 ops->same_provision(linecard, linecard->priv,
2267 linecard_type->type,
2268 linecard_type->priv))
2273 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING;
2274 linecard->type = linecard_type->type;
2275 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2276 mutex_unlock(&linecard->state_lock);
2277 err = ops->provision(linecard, linecard->priv, linecard_type->type,
2278 linecard_type->priv, extack);
2280 /* Provisioning failed. Assume the linecard is unprovisioned
2281 * for future operations.
2283 mutex_lock(&linecard->state_lock);
2284 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2285 linecard->type = NULL;
2286 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2287 mutex_unlock(&linecard->state_lock);
2292 mutex_unlock(&linecard->state_lock);
2296 static int devlink_linecard_type_unset(struct devlink_linecard *linecard,
2297 struct netlink_ext_ack *extack)
2301 mutex_lock(&linecard->state_lock);
2302 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2303 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2307 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2308 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2312 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2313 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2314 linecard->type = NULL;
2315 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2320 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONED) {
2321 NL_SET_ERR_MSG_MOD(extack, "Line card is not provisioned");
2325 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONING;
2326 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2327 mutex_unlock(&linecard->state_lock);
2328 err = linecard->ops->unprovision(linecard, linecard->priv,
2331 /* Unprovisioning failed. Assume the linecard is unprovisioned
2332 * for future operations.
2334 mutex_lock(&linecard->state_lock);
2335 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2336 linecard->type = NULL;
2337 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2338 mutex_unlock(&linecard->state_lock);
2343 mutex_unlock(&linecard->state_lock);
2347 static int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
2348 struct genl_info *info)
2350 struct devlink_linecard *linecard = info->user_ptr[1];
2351 struct netlink_ext_ack *extack = info->extack;
2354 if (info->attrs[DEVLINK_ATTR_LINECARD_TYPE]) {
2357 type = nla_data(info->attrs[DEVLINK_ATTR_LINECARD_TYPE]);
2358 if (strcmp(type, "")) {
2359 err = devlink_linecard_type_set(linecard, type, extack);
2363 err = devlink_linecard_type_unset(linecard, extack);
2372 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
2373 struct devlink_sb *devlink_sb,
2374 enum devlink_command cmd, u32 portid,
2379 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2383 if (devlink_nl_put_handle(msg, devlink))
2384 goto nla_put_failure;
2385 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2386 goto nla_put_failure;
2387 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
2388 goto nla_put_failure;
2389 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
2390 devlink_sb->ingress_pools_count))
2391 goto nla_put_failure;
2392 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
2393 devlink_sb->egress_pools_count))
2394 goto nla_put_failure;
2395 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
2396 devlink_sb->ingress_tc_count))
2397 goto nla_put_failure;
2398 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
2399 devlink_sb->egress_tc_count))
2400 goto nla_put_failure;
2402 genlmsg_end(msg, hdr);
2406 genlmsg_cancel(msg, hdr);
2410 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2411 struct genl_info *info)
2413 struct devlink *devlink = info->user_ptr[0];
2414 struct devlink_sb *devlink_sb;
2415 struct sk_buff *msg;
2418 devlink_sb = devlink_sb_get_from_info(devlink, info);
2419 if (IS_ERR(devlink_sb))
2420 return PTR_ERR(devlink_sb);
2422 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2426 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2428 info->snd_portid, info->snd_seq, 0);
2434 return genlmsg_reply(msg, info);
2437 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
2438 struct netlink_callback *cb)
2440 struct devlink *devlink;
2441 struct devlink_sb *devlink_sb;
2442 int start = cb->args[0];
2443 unsigned long index;
2447 mutex_lock(&devlink_mutex);
2448 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2449 if (!devlink_try_get(devlink))
2452 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2455 mutex_lock(&devlink->lock);
2456 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2461 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2463 NETLINK_CB(cb->skb).portid,
2467 mutex_unlock(&devlink->lock);
2468 devlink_put(devlink);
2473 mutex_unlock(&devlink->lock);
2475 devlink_put(devlink);
2478 mutex_unlock(&devlink_mutex);
2484 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2485 struct devlink_sb *devlink_sb,
2486 u16 pool_index, enum devlink_command cmd,
2487 u32 portid, u32 seq, int flags)
2489 struct devlink_sb_pool_info pool_info;
2493 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2494 pool_index, &pool_info);
2498 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2502 if (devlink_nl_put_handle(msg, devlink))
2503 goto nla_put_failure;
2504 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2505 goto nla_put_failure;
2506 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2507 goto nla_put_failure;
2508 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2509 goto nla_put_failure;
2510 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2511 goto nla_put_failure;
2512 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2513 pool_info.threshold_type))
2514 goto nla_put_failure;
2515 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2516 pool_info.cell_size))
2517 goto nla_put_failure;
2519 genlmsg_end(msg, hdr);
2523 genlmsg_cancel(msg, hdr);
2527 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2528 struct genl_info *info)
2530 struct devlink *devlink = info->user_ptr[0];
2531 struct devlink_sb *devlink_sb;
2532 struct sk_buff *msg;
2536 devlink_sb = devlink_sb_get_from_info(devlink, info);
2537 if (IS_ERR(devlink_sb))
2538 return PTR_ERR(devlink_sb);
2540 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2545 if (!devlink->ops->sb_pool_get)
2548 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2552 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2553 DEVLINK_CMD_SB_POOL_NEW,
2554 info->snd_portid, info->snd_seq, 0);
2560 return genlmsg_reply(msg, info);
2563 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2564 struct devlink *devlink,
2565 struct devlink_sb *devlink_sb,
2566 u32 portid, u32 seq)
2568 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2572 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2573 if (*p_idx < start) {
2577 err = devlink_nl_sb_pool_fill(msg, devlink,
2580 DEVLINK_CMD_SB_POOL_NEW,
2581 portid, seq, NLM_F_MULTI);
2589 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
2590 struct netlink_callback *cb)
2592 struct devlink *devlink;
2593 struct devlink_sb *devlink_sb;
2594 int start = cb->args[0];
2595 unsigned long index;
2599 mutex_lock(&devlink_mutex);
2600 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2601 if (!devlink_try_get(devlink))
2604 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2605 !devlink->ops->sb_pool_get)
2608 mutex_lock(&devlink->lock);
2609 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2610 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
2612 NETLINK_CB(cb->skb).portid,
2613 cb->nlh->nlmsg_seq);
2614 if (err == -EOPNOTSUPP) {
2617 mutex_unlock(&devlink->lock);
2618 devlink_put(devlink);
2622 mutex_unlock(&devlink->lock);
2624 devlink_put(devlink);
2627 mutex_unlock(&devlink_mutex);
2629 if (err != -EMSGSIZE)
2636 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2637 u16 pool_index, u32 size,
2638 enum devlink_sb_threshold_type threshold_type,
2639 struct netlink_ext_ack *extack)
2642 const struct devlink_ops *ops = devlink->ops;
2644 if (ops->sb_pool_set)
2645 return ops->sb_pool_set(devlink, sb_index, pool_index,
2646 size, threshold_type, extack);
2650 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2651 struct genl_info *info)
2653 struct devlink *devlink = info->user_ptr[0];
2654 enum devlink_sb_threshold_type threshold_type;
2655 struct devlink_sb *devlink_sb;
2660 devlink_sb = devlink_sb_get_from_info(devlink, info);
2661 if (IS_ERR(devlink_sb))
2662 return PTR_ERR(devlink_sb);
2664 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2669 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2673 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
2676 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2677 return devlink_sb_pool_set(devlink, devlink_sb->index,
2678 pool_index, size, threshold_type,
2682 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2683 struct devlink *devlink,
2684 struct devlink_port *devlink_port,
2685 struct devlink_sb *devlink_sb,
2687 enum devlink_command cmd,
2688 u32 portid, u32 seq, int flags)
2690 const struct devlink_ops *ops = devlink->ops;
2695 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2696 pool_index, &threshold);
2700 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2704 if (devlink_nl_put_handle(msg, devlink))
2705 goto nla_put_failure;
2706 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2707 goto nla_put_failure;
2708 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2709 goto nla_put_failure;
2710 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2711 goto nla_put_failure;
2712 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2713 goto nla_put_failure;
2715 if (ops->sb_occ_port_pool_get) {
2719 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2720 pool_index, &cur, &max);
2721 if (err && err != -EOPNOTSUPP)
2722 goto sb_occ_get_failure;
2724 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2725 goto nla_put_failure;
2726 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2727 goto nla_put_failure;
2731 genlmsg_end(msg, hdr);
2737 genlmsg_cancel(msg, hdr);
2741 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2742 struct genl_info *info)
2744 struct devlink_port *devlink_port = info->user_ptr[1];
2745 struct devlink *devlink = devlink_port->devlink;
2746 struct devlink_sb *devlink_sb;
2747 struct sk_buff *msg;
2751 devlink_sb = devlink_sb_get_from_info(devlink, info);
2752 if (IS_ERR(devlink_sb))
2753 return PTR_ERR(devlink_sb);
2755 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2760 if (!devlink->ops->sb_port_pool_get)
2763 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2767 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2768 devlink_sb, pool_index,
2769 DEVLINK_CMD_SB_PORT_POOL_NEW,
2770 info->snd_portid, info->snd_seq, 0);
2776 return genlmsg_reply(msg, info);
2779 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2780 struct devlink *devlink,
2781 struct devlink_sb *devlink_sb,
2782 u32 portid, u32 seq)
2784 struct devlink_port *devlink_port;
2785 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2789 list_for_each_entry(devlink_port, &devlink->port_list, list) {
2790 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2791 if (*p_idx < start) {
2795 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2799 DEVLINK_CMD_SB_PORT_POOL_NEW,
2810 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
2811 struct netlink_callback *cb)
2813 struct devlink *devlink;
2814 struct devlink_sb *devlink_sb;
2815 int start = cb->args[0];
2816 unsigned long index;
2820 mutex_lock(&devlink_mutex);
2821 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2822 if (!devlink_try_get(devlink))
2825 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2826 !devlink->ops->sb_port_pool_get)
2829 mutex_lock(&devlink->lock);
2830 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2831 err = __sb_port_pool_get_dumpit(msg, start, &idx,
2832 devlink, devlink_sb,
2833 NETLINK_CB(cb->skb).portid,
2834 cb->nlh->nlmsg_seq);
2835 if (err == -EOPNOTSUPP) {
2838 mutex_unlock(&devlink->lock);
2839 devlink_put(devlink);
2843 mutex_unlock(&devlink->lock);
2845 devlink_put(devlink);
2848 mutex_unlock(&devlink_mutex);
2850 if (err != -EMSGSIZE)
2857 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2858 unsigned int sb_index, u16 pool_index,
2860 struct netlink_ext_ack *extack)
2863 const struct devlink_ops *ops = devlink_port->devlink->ops;
2865 if (ops->sb_port_pool_set)
2866 return ops->sb_port_pool_set(devlink_port, sb_index,
2867 pool_index, threshold, extack);
2871 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2872 struct genl_info *info)
2874 struct devlink_port *devlink_port = info->user_ptr[1];
2875 struct devlink *devlink = info->user_ptr[0];
2876 struct devlink_sb *devlink_sb;
2881 devlink_sb = devlink_sb_get_from_info(devlink, info);
2882 if (IS_ERR(devlink_sb))
2883 return PTR_ERR(devlink_sb);
2885 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2890 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
2893 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2894 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2895 pool_index, threshold, info->extack);
2899 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2900 struct devlink_port *devlink_port,
2901 struct devlink_sb *devlink_sb, u16 tc_index,
2902 enum devlink_sb_pool_type pool_type,
2903 enum devlink_command cmd,
2904 u32 portid, u32 seq, int flags)
2906 const struct devlink_ops *ops = devlink->ops;
2912 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2913 tc_index, pool_type,
2914 &pool_index, &threshold);
2918 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2922 if (devlink_nl_put_handle(msg, devlink))
2923 goto nla_put_failure;
2924 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2925 goto nla_put_failure;
2926 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2927 goto nla_put_failure;
2928 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2929 goto nla_put_failure;
2930 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2931 goto nla_put_failure;
2932 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2933 goto nla_put_failure;
2934 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2935 goto nla_put_failure;
2937 if (ops->sb_occ_tc_port_bind_get) {
2941 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2943 tc_index, pool_type,
2945 if (err && err != -EOPNOTSUPP)
2948 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2949 goto nla_put_failure;
2950 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2951 goto nla_put_failure;
2955 genlmsg_end(msg, hdr);
2959 genlmsg_cancel(msg, hdr);
2963 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2964 struct genl_info *info)
2966 struct devlink_port *devlink_port = info->user_ptr[1];
2967 struct devlink *devlink = devlink_port->devlink;
2968 struct devlink_sb *devlink_sb;
2969 struct sk_buff *msg;
2970 enum devlink_sb_pool_type pool_type;
2974 devlink_sb = devlink_sb_get_from_info(devlink, info);
2975 if (IS_ERR(devlink_sb))
2976 return PTR_ERR(devlink_sb);
2978 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2982 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2983 pool_type, &tc_index);
2987 if (!devlink->ops->sb_tc_pool_bind_get)
2990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2994 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2995 devlink_sb, tc_index, pool_type,
2996 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3004 return genlmsg_reply(msg, info);
3007 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
3008 int start, int *p_idx,
3009 struct devlink *devlink,
3010 struct devlink_sb *devlink_sb,
3011 u32 portid, u32 seq)
3013 struct devlink_port *devlink_port;
3017 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3019 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
3020 if (*p_idx < start) {
3024 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
3028 DEVLINK_SB_POOL_TYPE_INGRESS,
3029 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3037 tc_index < devlink_sb->egress_tc_count; tc_index++) {
3038 if (*p_idx < start) {
3042 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
3046 DEVLINK_SB_POOL_TYPE_EGRESS,
3047 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3059 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
3060 struct netlink_callback *cb)
3062 struct devlink *devlink;
3063 struct devlink_sb *devlink_sb;
3064 int start = cb->args[0];
3065 unsigned long index;
3069 mutex_lock(&devlink_mutex);
3070 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
3071 if (!devlink_try_get(devlink))
3074 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
3075 !devlink->ops->sb_tc_pool_bind_get)
3078 mutex_lock(&devlink->lock);
3079 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
3080 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
3083 NETLINK_CB(cb->skb).portid,
3084 cb->nlh->nlmsg_seq);
3085 if (err == -EOPNOTSUPP) {
3088 mutex_unlock(&devlink->lock);
3089 devlink_put(devlink);
3093 mutex_unlock(&devlink->lock);
3095 devlink_put(devlink);
3098 mutex_unlock(&devlink_mutex);
3100 if (err != -EMSGSIZE)
3107 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
3108 unsigned int sb_index, u16 tc_index,
3109 enum devlink_sb_pool_type pool_type,
3110 u16 pool_index, u32 threshold,
3111 struct netlink_ext_ack *extack)
3114 const struct devlink_ops *ops = devlink_port->devlink->ops;
3116 if (ops->sb_tc_pool_bind_set)
3117 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
3118 tc_index, pool_type,
3119 pool_index, threshold, extack);
3123 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
3124 struct genl_info *info)
3126 struct devlink_port *devlink_port = info->user_ptr[1];
3127 struct devlink *devlink = info->user_ptr[0];
3128 enum devlink_sb_pool_type pool_type;
3129 struct devlink_sb *devlink_sb;
3135 devlink_sb = devlink_sb_get_from_info(devlink, info);
3136 if (IS_ERR(devlink_sb))
3137 return PTR_ERR(devlink_sb);
3139 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
3143 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
3144 pool_type, &tc_index);
3148 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
3153 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
3156 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
3157 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
3158 tc_index, pool_type,
3159 pool_index, threshold, info->extack);
3162 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
3163 struct genl_info *info)
3165 struct devlink *devlink = info->user_ptr[0];
3166 const struct devlink_ops *ops = devlink->ops;
3167 struct devlink_sb *devlink_sb;
3169 devlink_sb = devlink_sb_get_from_info(devlink, info);
3170 if (IS_ERR(devlink_sb))
3171 return PTR_ERR(devlink_sb);
3173 if (ops->sb_occ_snapshot)
3174 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
3178 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
3179 struct genl_info *info)
3181 struct devlink *devlink = info->user_ptr[0];
3182 const struct devlink_ops *ops = devlink->ops;
3183 struct devlink_sb *devlink_sb;
3185 devlink_sb = devlink_sb_get_from_info(devlink, info);
3186 if (IS_ERR(devlink_sb))
3187 return PTR_ERR(devlink_sb);
3189 if (ops->sb_occ_max_clear)
3190 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
3194 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
3195 enum devlink_command cmd, u32 portid,
3198 const struct devlink_ops *ops = devlink->ops;
3199 enum devlink_eswitch_encap_mode encap_mode;
3205 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3209 err = devlink_nl_put_handle(msg, devlink);
3211 goto nla_put_failure;
3213 if (ops->eswitch_mode_get) {
3214 err = ops->eswitch_mode_get(devlink, &mode);
3216 goto nla_put_failure;
3217 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
3219 goto nla_put_failure;
3222 if (ops->eswitch_inline_mode_get) {
3223 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
3225 goto nla_put_failure;
3226 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
3229 goto nla_put_failure;
3232 if (ops->eswitch_encap_mode_get) {
3233 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
3235 goto nla_put_failure;
3236 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
3238 goto nla_put_failure;
3241 genlmsg_end(msg, hdr);
3245 genlmsg_cancel(msg, hdr);
3249 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
3250 struct genl_info *info)
3252 struct devlink *devlink = info->user_ptr[0];
3253 struct sk_buff *msg;
3256 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3260 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
3261 info->snd_portid, info->snd_seq, 0);
3268 return genlmsg_reply(msg, info);
3271 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
3272 struct netlink_ext_ack *extack)
3274 struct devlink_rate *devlink_rate;
3276 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
3277 if (devlink_rate_is_node(devlink_rate)) {
3278 NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
3284 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
3285 struct genl_info *info)
3287 struct devlink *devlink = info->user_ptr[0];
3288 const struct devlink_ops *ops = devlink->ops;
3289 enum devlink_eswitch_encap_mode encap_mode;
3294 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
3295 if (!ops->eswitch_mode_set)
3297 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
3298 err = devlink_rate_nodes_check(devlink, mode, info->extack);
3301 err = ops->eswitch_mode_set(devlink, mode, info->extack);
3306 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
3307 if (!ops->eswitch_inline_mode_set)
3309 inline_mode = nla_get_u8(
3310 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
3311 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
3317 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
3318 if (!ops->eswitch_encap_mode_set)
3320 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
3321 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
3330 int devlink_dpipe_match_put(struct sk_buff *skb,
3331 struct devlink_dpipe_match *match)
3333 struct devlink_dpipe_header *header = match->header;
3334 struct devlink_dpipe_field *field = &header->fields[match->field_id];
3335 struct nlattr *match_attr;
3337 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
3341 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
3342 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
3343 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3344 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3345 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3346 goto nla_put_failure;
3348 nla_nest_end(skb, match_attr);
3352 nla_nest_cancel(skb, match_attr);
3355 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
3357 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
3358 struct sk_buff *skb)
3360 struct nlattr *matches_attr;
3362 matches_attr = nla_nest_start_noflag(skb,
3363 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
3367 if (table->table_ops->matches_dump(table->priv, skb))
3368 goto nla_put_failure;
3370 nla_nest_end(skb, matches_attr);
3374 nla_nest_cancel(skb, matches_attr);
3378 int devlink_dpipe_action_put(struct sk_buff *skb,
3379 struct devlink_dpipe_action *action)
3381 struct devlink_dpipe_header *header = action->header;
3382 struct devlink_dpipe_field *field = &header->fields[action->field_id];
3383 struct nlattr *action_attr;
3385 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
3389 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
3390 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
3391 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3392 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3393 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3394 goto nla_put_failure;
3396 nla_nest_end(skb, action_attr);
3400 nla_nest_cancel(skb, action_attr);
3403 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
3405 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
3406 struct sk_buff *skb)
3408 struct nlattr *actions_attr;
3410 actions_attr = nla_nest_start_noflag(skb,
3411 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
3415 if (table->table_ops->actions_dump(table->priv, skb))
3416 goto nla_put_failure;
3418 nla_nest_end(skb, actions_attr);
3422 nla_nest_cancel(skb, actions_attr);
3426 static int devlink_dpipe_table_put(struct sk_buff *skb,
3427 struct devlink_dpipe_table *table)
3429 struct nlattr *table_attr;
3432 table_size = table->table_ops->size_get(table->priv);
3433 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
3437 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
3438 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
3440 goto nla_put_failure;
3441 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
3442 table->counters_enabled))
3443 goto nla_put_failure;
3445 if (table->resource_valid) {
3446 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
3447 table->resource_id, DEVLINK_ATTR_PAD) ||
3448 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
3449 table->resource_units, DEVLINK_ATTR_PAD))
3450 goto nla_put_failure;
3452 if (devlink_dpipe_matches_put(table, skb))
3453 goto nla_put_failure;
3455 if (devlink_dpipe_actions_put(table, skb))
3456 goto nla_put_failure;
3458 nla_nest_end(skb, table_attr);
3462 nla_nest_cancel(skb, table_attr);
3466 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
3467 struct genl_info *info)
3472 err = genlmsg_reply(*pskb, info);
3476 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3482 static int devlink_dpipe_tables_fill(struct genl_info *info,
3483 enum devlink_command cmd, int flags,
3484 struct list_head *dpipe_tables,
3485 const char *table_name)
3487 struct devlink *devlink = info->user_ptr[0];
3488 struct devlink_dpipe_table *table;
3489 struct nlattr *tables_attr;
3490 struct sk_buff *skb = NULL;
3491 struct nlmsghdr *nlh;
3497 table = list_first_entry(dpipe_tables,
3498 struct devlink_dpipe_table, list);
3500 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3504 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3505 &devlink_nl_family, NLM_F_MULTI, cmd);
3511 if (devlink_nl_put_handle(skb, devlink))
3512 goto nla_put_failure;
3513 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3515 goto nla_put_failure;
3519 list_for_each_entry_from(table, dpipe_tables, list) {
3521 err = devlink_dpipe_table_put(skb, table);
3529 if (!strcmp(table->name, table_name)) {
3530 err = devlink_dpipe_table_put(skb, table);
3538 nla_nest_end(skb, tables_attr);
3539 genlmsg_end(skb, hdr);
3544 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3545 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3547 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3553 return genlmsg_reply(skb, info);
3562 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3563 struct genl_info *info)
3565 struct devlink *devlink = info->user_ptr[0];
3566 const char *table_name = NULL;
3568 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3569 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3571 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3572 &devlink->dpipe_table_list,
3576 static int devlink_dpipe_value_put(struct sk_buff *skb,
3577 struct devlink_dpipe_value *value)
3579 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3580 value->value_size, value->value))
3583 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3584 value->value_size, value->mask))
3586 if (value->mapping_valid)
3587 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3588 value->mapping_value))
3593 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3594 struct devlink_dpipe_value *value)
3598 if (devlink_dpipe_action_put(skb, value->action))
3600 if (devlink_dpipe_value_put(skb, value))
3605 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3606 struct devlink_dpipe_value *values,
3607 unsigned int values_count)
3609 struct nlattr *action_attr;
3613 for (i = 0; i < values_count; i++) {
3614 action_attr = nla_nest_start_noflag(skb,
3615 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3618 err = devlink_dpipe_action_value_put(skb, &values[i]);
3620 goto err_action_value_put;
3621 nla_nest_end(skb, action_attr);
3625 err_action_value_put:
3626 nla_nest_cancel(skb, action_attr);
3630 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3631 struct devlink_dpipe_value *value)
3635 if (devlink_dpipe_match_put(skb, value->match))
3637 if (devlink_dpipe_value_put(skb, value))
3642 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3643 struct devlink_dpipe_value *values,
3644 unsigned int values_count)
3646 struct nlattr *match_attr;
3650 for (i = 0; i < values_count; i++) {
3651 match_attr = nla_nest_start_noflag(skb,
3652 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3655 err = devlink_dpipe_match_value_put(skb, &values[i]);
3657 goto err_match_value_put;
3658 nla_nest_end(skb, match_attr);
3662 err_match_value_put:
3663 nla_nest_cancel(skb, match_attr);
3667 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3668 struct devlink_dpipe_entry *entry)
3670 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3673 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3677 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3679 goto nla_put_failure;
3680 if (entry->counter_valid)
3681 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3682 entry->counter, DEVLINK_ATTR_PAD))
3683 goto nla_put_failure;
3685 matches_attr = nla_nest_start_noflag(skb,
3686 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3688 goto nla_put_failure;
3690 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3691 entry->match_values_count);
3693 nla_nest_cancel(skb, matches_attr);
3694 goto err_match_values_put;
3696 nla_nest_end(skb, matches_attr);
3698 actions_attr = nla_nest_start_noflag(skb,
3699 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3701 goto nla_put_failure;
3703 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3704 entry->action_values_count);
3706 nla_nest_cancel(skb, actions_attr);
3707 goto err_action_values_put;
3709 nla_nest_end(skb, actions_attr);
3711 nla_nest_end(skb, entry_attr);
3716 err_match_values_put:
3717 err_action_values_put:
3718 nla_nest_cancel(skb, entry_attr);
3722 static struct devlink_dpipe_table *
3723 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3724 const char *table_name, struct devlink *devlink)
3726 struct devlink_dpipe_table *table;
3727 list_for_each_entry_rcu(table, dpipe_tables, list,
3728 lockdep_is_held(&devlink->lock)) {
3729 if (!strcmp(table->name, table_name))
3735 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3737 struct devlink *devlink;
3740 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3745 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3746 dump_ctx->info->snd_portid,
3747 dump_ctx->info->snd_seq,
3748 &devlink_nl_family, NLM_F_MULTI,
3751 goto nla_put_failure;
3753 devlink = dump_ctx->info->user_ptr[0];
3754 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3755 goto nla_put_failure;
3756 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3757 DEVLINK_ATTR_DPIPE_ENTRIES);
3758 if (!dump_ctx->nest)
3759 goto nla_put_failure;
3763 nlmsg_free(dump_ctx->skb);
3766 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3768 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3769 struct devlink_dpipe_entry *entry)
3771 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3773 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3775 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3777 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3778 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3781 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3783 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3786 unsigned int value_count, value_index;
3787 struct devlink_dpipe_value *value;
3789 value = entry->action_values;
3790 value_count = entry->action_values_count;
3791 for (value_index = 0; value_index < value_count; value_index++) {
3792 kfree(value[value_index].value);
3793 kfree(value[value_index].mask);
3796 value = entry->match_values;
3797 value_count = entry->match_values_count;
3798 for (value_index = 0; value_index < value_count; value_index++) {
3799 kfree(value[value_index].value);
3800 kfree(value[value_index].mask);
3803 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3805 static int devlink_dpipe_entries_fill(struct genl_info *info,
3806 enum devlink_command cmd, int flags,
3807 struct devlink_dpipe_table *table)
3809 struct devlink_dpipe_dump_ctx dump_ctx;
3810 struct nlmsghdr *nlh;
3813 dump_ctx.skb = NULL;
3815 dump_ctx.info = info;
3817 err = table->table_ops->entries_dump(table->priv,
3818 table->counters_enabled,
3824 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3825 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3827 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3832 return genlmsg_reply(dump_ctx.skb, info);
3835 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3836 struct genl_info *info)
3838 struct devlink *devlink = info->user_ptr[0];
3839 struct devlink_dpipe_table *table;
3840 const char *table_name;
3842 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3845 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3846 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3847 table_name, devlink);
3851 if (!table->table_ops->entries_dump)
3854 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3858 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3859 const struct devlink_dpipe_header *header)
3861 struct devlink_dpipe_field *field;
3862 struct nlattr *field_attr;
3865 for (i = 0; i < header->fields_count; i++) {
3866 field = &header->fields[i];
3867 field_attr = nla_nest_start_noflag(skb,
3868 DEVLINK_ATTR_DPIPE_FIELD);
3871 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3872 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3873 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3874 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3875 goto nla_put_failure;
3876 nla_nest_end(skb, field_attr);
3881 nla_nest_cancel(skb, field_attr);
3885 static int devlink_dpipe_header_put(struct sk_buff *skb,
3886 struct devlink_dpipe_header *header)
3888 struct nlattr *fields_attr, *header_attr;
3891 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3895 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3896 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3897 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3898 goto nla_put_failure;
3900 fields_attr = nla_nest_start_noflag(skb,
3901 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3903 goto nla_put_failure;
3905 err = devlink_dpipe_fields_put(skb, header);
3907 nla_nest_cancel(skb, fields_attr);
3908 goto nla_put_failure;
3910 nla_nest_end(skb, fields_attr);
3911 nla_nest_end(skb, header_attr);
3916 nla_nest_cancel(skb, header_attr);
3920 static int devlink_dpipe_headers_fill(struct genl_info *info,
3921 enum devlink_command cmd, int flags,
3922 struct devlink_dpipe_headers *
3925 struct devlink *devlink = info->user_ptr[0];
3926 struct nlattr *headers_attr;
3927 struct sk_buff *skb = NULL;
3928 struct nlmsghdr *nlh;
3935 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3939 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3940 &devlink_nl_family, NLM_F_MULTI, cmd);
3946 if (devlink_nl_put_handle(skb, devlink))
3947 goto nla_put_failure;
3948 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3950 goto nla_put_failure;
3953 for (; i < dpipe_headers->headers_count; i++) {
3954 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3962 nla_nest_end(skb, headers_attr);
3963 genlmsg_end(skb, hdr);
3964 if (i != dpipe_headers->headers_count)
3968 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3969 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3971 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3976 return genlmsg_reply(skb, info);
3985 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3986 struct genl_info *info)
3988 struct devlink *devlink = info->user_ptr[0];
3990 if (!devlink->dpipe_headers)
3992 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3993 0, devlink->dpipe_headers);
3996 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3997 const char *table_name,
4000 struct devlink_dpipe_table *table;
4002 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
4003 table_name, devlink);
4007 if (table->counter_control_extern)
4010 if (!(table->counters_enabled ^ enable))
4013 table->counters_enabled = enable;
4014 if (table->table_ops->counters_set_update)
4015 table->table_ops->counters_set_update(table->priv, enable);
4019 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
4020 struct genl_info *info)
4022 struct devlink *devlink = info->user_ptr[0];
4023 const char *table_name;
4024 bool counters_enable;
4026 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
4027 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
4030 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
4031 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
4033 return devlink_dpipe_table_counters_set(devlink, table_name,
4037 static struct devlink_resource *
4038 devlink_resource_find(struct devlink *devlink,
4039 struct devlink_resource *resource, u64 resource_id)
4041 struct list_head *resource_list;
4044 resource_list = &resource->resource_list;
4046 resource_list = &devlink->resource_list;
4048 list_for_each_entry(resource, resource_list, list) {
4049 struct devlink_resource *child_resource;
4051 if (resource->id == resource_id)
4054 child_resource = devlink_resource_find(devlink, resource,
4057 return child_resource;
4063 devlink_resource_validate_children(struct devlink_resource *resource)
4065 struct devlink_resource *child_resource;
4066 bool size_valid = true;
4069 if (list_empty(&resource->resource_list))
4072 list_for_each_entry(child_resource, &resource->resource_list, list)
4073 parts_size += child_resource->size_new;
4075 if (parts_size > resource->size_new)
4078 resource->size_valid = size_valid;
4082 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
4083 struct netlink_ext_ack *extack)
4088 if (size > resource->size_params.size_max) {
4089 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
4093 if (size < resource->size_params.size_min) {
4094 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
4098 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
4100 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
4107 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
4108 struct genl_info *info)
4110 struct devlink *devlink = info->user_ptr[0];
4111 struct devlink_resource *resource;
4116 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
4117 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
4119 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
4121 resource = devlink_resource_find(devlink, NULL, resource_id);
4125 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
4126 err = devlink_resource_validate_size(resource, size, info->extack);
4130 resource->size_new = size;
4131 devlink_resource_validate_children(resource);
4132 if (resource->parent)
4133 devlink_resource_validate_children(resource->parent);
4138 devlink_resource_size_params_put(struct devlink_resource *resource,
4139 struct sk_buff *skb)
4141 struct devlink_resource_size_params *size_params;
4143 size_params = &resource->size_params;
4144 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
4145 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
4146 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
4147 size_params->size_max, DEVLINK_ATTR_PAD) ||
4148 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
4149 size_params->size_min, DEVLINK_ATTR_PAD) ||
4150 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
4155 static int devlink_resource_occ_put(struct devlink_resource *resource,
4156 struct sk_buff *skb)
4158 if (!resource->occ_get)
4160 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
4161 resource->occ_get(resource->occ_get_priv),
4165 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
4166 struct devlink_resource *resource)
4168 struct devlink_resource *child_resource;
4169 struct nlattr *child_resource_attr;
4170 struct nlattr *resource_attr;
4172 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
4176 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
4177 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
4178 DEVLINK_ATTR_PAD) ||
4179 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
4181 goto nla_put_failure;
4182 if (resource->size != resource->size_new)
4183 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
4184 resource->size_new, DEVLINK_ATTR_PAD);
4185 if (devlink_resource_occ_put(resource, skb))
4186 goto nla_put_failure;
4187 if (devlink_resource_size_params_put(resource, skb))
4188 goto nla_put_failure;
4189 if (list_empty(&resource->resource_list))
4192 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
4193 resource->size_valid))
4194 goto nla_put_failure;
4196 child_resource_attr = nla_nest_start_noflag(skb,
4197 DEVLINK_ATTR_RESOURCE_LIST);
4198 if (!child_resource_attr)
4199 goto nla_put_failure;
4201 list_for_each_entry(child_resource, &resource->resource_list, list) {
4202 if (devlink_resource_put(devlink, skb, child_resource))
4203 goto resource_put_failure;
4206 nla_nest_end(skb, child_resource_attr);
4208 nla_nest_end(skb, resource_attr);
4211 resource_put_failure:
4212 nla_nest_cancel(skb, child_resource_attr);
4214 nla_nest_cancel(skb, resource_attr);
4218 static int devlink_resource_fill(struct genl_info *info,
4219 enum devlink_command cmd, int flags)
4221 struct devlink *devlink = info->user_ptr[0];
4222 struct devlink_resource *resource;
4223 struct nlattr *resources_attr;
4224 struct sk_buff *skb = NULL;
4225 struct nlmsghdr *nlh;
4231 resource = list_first_entry(&devlink->resource_list,
4232 struct devlink_resource, list);
4234 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4238 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4239 &devlink_nl_family, NLM_F_MULTI, cmd);
4245 if (devlink_nl_put_handle(skb, devlink))
4246 goto nla_put_failure;
4248 resources_attr = nla_nest_start_noflag(skb,
4249 DEVLINK_ATTR_RESOURCE_LIST);
4250 if (!resources_attr)
4251 goto nla_put_failure;
4255 list_for_each_entry_from(resource, &devlink->resource_list, list) {
4256 err = devlink_resource_put(devlink, skb, resource);
4259 goto err_resource_put;
4265 nla_nest_end(skb, resources_attr);
4266 genlmsg_end(skb, hdr);
4270 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4271 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4273 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4278 return genlmsg_reply(skb, info);
4287 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
4288 struct genl_info *info)
4290 struct devlink *devlink = info->user_ptr[0];
4292 if (list_empty(&devlink->resource_list))
4295 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
4299 devlink_resources_validate(struct devlink *devlink,
4300 struct devlink_resource *resource,
4301 struct genl_info *info)
4303 struct list_head *resource_list;
4307 resource_list = &resource->resource_list;
4309 resource_list = &devlink->resource_list;
4311 list_for_each_entry(resource, resource_list, list) {
4312 if (!resource->size_valid)
4314 err = devlink_resources_validate(devlink, resource, info);
4321 static struct net *devlink_netns_get(struct sk_buff *skb,
4322 struct genl_info *info)
4324 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
4325 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
4326 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
4329 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
4330 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
4331 return ERR_PTR(-EINVAL);
4334 if (netns_pid_attr) {
4335 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
4336 } else if (netns_fd_attr) {
4337 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
4338 } else if (netns_id_attr) {
4339 net = get_net_ns_by_id(sock_net(skb->sk),
4340 nla_get_u32(netns_id_attr));
4342 net = ERR_PTR(-EINVAL);
4345 net = ERR_PTR(-EINVAL);
4348 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
4349 return ERR_PTR(-EINVAL);
4351 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
4353 return ERR_PTR(-EPERM);
4358 static void devlink_param_notify(struct devlink *devlink,
4359 unsigned int port_index,
4360 struct devlink_param_item *param_item,
4361 enum devlink_command cmd);
4363 static void devlink_ns_change_notify(struct devlink *devlink,
4364 struct net *dest_net, struct net *curr_net,
4367 struct devlink_param_item *param_item;
4368 enum devlink_command cmd;
4370 /* Userspace needs to be notified about devlink objects
4371 * removed from original and entering new network namespace.
4372 * The rest of the devlink objects are re-created during
4373 * reload process so the notifications are generated separatelly.
4376 if (!dest_net || net_eq(dest_net, curr_net))
4380 devlink_notify(devlink, DEVLINK_CMD_NEW);
4382 cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
4383 list_for_each_entry(param_item, &devlink->param_list, list)
4384 devlink_param_notify(devlink, 0, param_item, cmd);
4387 devlink_notify(devlink, DEVLINK_CMD_DEL);
4390 static bool devlink_reload_supported(const struct devlink_ops *ops)
4392 return ops->reload_down && ops->reload_up;
4395 static void devlink_reload_failed_set(struct devlink *devlink,
4398 if (devlink->reload_failed == reload_failed)
4400 devlink->reload_failed = reload_failed;
4401 devlink_notify(devlink, DEVLINK_CMD_NEW);
4404 bool devlink_is_reload_failed(const struct devlink *devlink)
4406 return devlink->reload_failed;
4408 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
4411 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
4412 enum devlink_reload_limit limit, u32 actions_performed)
4414 unsigned long actions = actions_performed;
4418 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
4419 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
4420 reload_stats[stat_idx]++;
4422 devlink_notify(devlink, DEVLINK_CMD_NEW);
4426 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
4427 u32 actions_performed)
4429 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
4434 * devlink_remote_reload_actions_performed - Update devlink on reload actions
4435 * performed which are not a direct result of devlink reload call.
4437 * This should be called by a driver after performing reload actions in case it was not
4438 * a result of devlink reload call. For example fw_activate was performed as a result
4439 * of devlink reload triggered fw_activate on another host.
4440 * The motivation for this function is to keep data on reload actions performed on this
4441 * function whether it was done due to direct devlink reload call or not.
4444 * @limit: reload limit
4445 * @actions_performed: bitmask of actions performed
4447 void devlink_remote_reload_actions_performed(struct devlink *devlink,
4448 enum devlink_reload_limit limit,
4449 u32 actions_performed)
4451 if (WARN_ON(!actions_performed ||
4452 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
4453 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
4454 limit > DEVLINK_RELOAD_LIMIT_MAX))
4457 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
4460 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
4462 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
4463 enum devlink_reload_action action, enum devlink_reload_limit limit,
4464 u32 *actions_performed, struct netlink_ext_ack *extack)
4466 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
4467 struct net *curr_net;
4470 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
4471 sizeof(remote_reload_stats));
4473 curr_net = devlink_net(devlink);
4474 devlink_ns_change_notify(devlink, dest_net, curr_net, false);
4475 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
4479 if (dest_net && !net_eq(dest_net, curr_net))
4480 write_pnet(&devlink->_net, dest_net);
4482 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
4483 devlink_reload_failed_set(devlink, !!err);
4487 devlink_ns_change_notify(devlink, dest_net, curr_net, true);
4488 WARN_ON(!(*actions_performed & BIT(action)));
4489 /* Catch driver on updating the remote action within devlink reload */
4490 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
4491 sizeof(remote_reload_stats)));
4492 devlink_reload_stats_update(devlink, limit, *actions_performed);
4497 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
4498 enum devlink_command cmd, struct genl_info *info)
4500 struct sk_buff *msg;
4503 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4507 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
4511 if (devlink_nl_put_handle(msg, devlink))
4512 goto nla_put_failure;
4514 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4516 goto nla_put_failure;
4517 genlmsg_end(msg, hdr);
4519 return genlmsg_reply(msg, info);
4522 genlmsg_cancel(msg, hdr);
4528 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4530 struct devlink *devlink = info->user_ptr[0];
4531 enum devlink_reload_action action;
4532 enum devlink_reload_limit limit;
4533 struct net *dest_net = NULL;
4534 u32 actions_performed;
4537 if (!(devlink->features & DEVLINK_F_RELOAD))
4540 err = devlink_resources_validate(devlink, NULL, info);
4542 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4546 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4547 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4549 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4551 if (!devlink_reload_action_is_supported(devlink, action)) {
4552 NL_SET_ERR_MSG_MOD(info->extack,
4553 "Requested reload action is not supported by the driver");
4557 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4558 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4559 struct nla_bitfield32 limits;
4560 u32 limits_selected;
4562 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4563 limits_selected = limits.value & limits.selector;
4564 if (!limits_selected) {
4565 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4568 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4569 if (limits_selected & BIT(limit))
4571 /* UAPI enables multiselection, but currently it is not used */
4572 if (limits_selected != BIT(limit)) {
4573 NL_SET_ERR_MSG_MOD(info->extack,
4574 "Multiselection of limit is not supported");
4577 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4578 NL_SET_ERR_MSG_MOD(info->extack,
4579 "Requested limit is not supported by the driver");
4582 if (devlink_reload_combination_is_invalid(action, limit)) {
4583 NL_SET_ERR_MSG_MOD(info->extack,
4584 "Requested limit is invalid for this action");
4588 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4589 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4590 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4591 dest_net = devlink_netns_get(skb, info);
4592 if (IS_ERR(dest_net))
4593 return PTR_ERR(dest_net);
4596 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4603 /* For backward compatibility generate reply only if attributes used by user */
4604 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4607 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4608 DEVLINK_CMD_RELOAD, info);
4611 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4612 struct devlink *devlink,
4613 enum devlink_command cmd,
4614 struct devlink_flash_notify *params)
4618 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4622 if (devlink_nl_put_handle(msg, devlink))
4623 goto nla_put_failure;
4625 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4628 if (params->status_msg &&
4629 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4630 params->status_msg))
4631 goto nla_put_failure;
4632 if (params->component &&
4633 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4635 goto nla_put_failure;
4636 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4637 params->done, DEVLINK_ATTR_PAD))
4638 goto nla_put_failure;
4639 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4640 params->total, DEVLINK_ATTR_PAD))
4641 goto nla_put_failure;
4642 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4643 params->timeout, DEVLINK_ATTR_PAD))
4644 goto nla_put_failure;
4647 genlmsg_end(msg, hdr);
4651 genlmsg_cancel(msg, hdr);
4655 static void __devlink_flash_update_notify(struct devlink *devlink,
4656 enum devlink_command cmd,
4657 struct devlink_flash_notify *params)
4659 struct sk_buff *msg;
4662 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4663 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4664 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4666 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4669 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4673 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4677 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4678 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4685 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4687 struct devlink_flash_notify params = {};
4689 __devlink_flash_update_notify(devlink,
4690 DEVLINK_CMD_FLASH_UPDATE,
4694 static void devlink_flash_update_end_notify(struct devlink *devlink)
4696 struct devlink_flash_notify params = {};
4698 __devlink_flash_update_notify(devlink,
4699 DEVLINK_CMD_FLASH_UPDATE_END,
4703 void devlink_flash_update_status_notify(struct devlink *devlink,
4704 const char *status_msg,
4705 const char *component,
4707 unsigned long total)
4709 struct devlink_flash_notify params = {
4710 .status_msg = status_msg,
4711 .component = component,
4716 __devlink_flash_update_notify(devlink,
4717 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4720 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4722 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4723 const char *status_msg,
4724 const char *component,
4725 unsigned long timeout)
4727 struct devlink_flash_notify params = {
4728 .status_msg = status_msg,
4729 .component = component,
4733 __devlink_flash_update_notify(devlink,
4734 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4737 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4739 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4740 struct genl_info *info)
4742 struct nlattr *nla_component, *nla_overwrite_mask, *nla_file_name;
4743 struct devlink_flash_update_params params = {};
4744 struct devlink *devlink = info->user_ptr[0];
4745 const char *file_name;
4746 u32 supported_params;
4749 if (!devlink->ops->flash_update)
4752 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
4755 supported_params = devlink->ops->supported_flash_update_params;
4757 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
4758 if (nla_component) {
4759 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
4760 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
4761 "component update is not supported by this device");
4764 params.component = nla_data(nla_component);
4767 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4768 if (nla_overwrite_mask) {
4769 struct nla_bitfield32 sections;
4771 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4772 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4773 "overwrite settings are not supported by this device");
4776 sections = nla_get_bitfield32(nla_overwrite_mask);
4777 params.overwrite_mask = sections.value & sections.selector;
4780 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4781 file_name = nla_data(nla_file_name);
4782 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
4784 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4788 devlink_flash_update_begin_notify(devlink);
4789 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
4790 devlink_flash_update_end_notify(devlink);
4792 release_firmware(params.fw);
4797 static const struct devlink_param devlink_param_generic[] = {
4799 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
4800 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
4801 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
4804 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
4805 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
4806 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
4809 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
4810 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
4811 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
4814 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
4815 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
4816 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
4819 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
4820 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
4821 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
4824 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
4825 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
4826 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
4829 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
4830 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
4831 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
4834 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
4835 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
4836 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
4839 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
4840 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
4841 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
4844 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
4845 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
4846 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
4849 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
4850 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
4851 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
4854 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
4855 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
4856 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
4859 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
4860 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
4861 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
4864 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
4865 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
4866 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
4869 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
4870 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
4871 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
4874 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
4875 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
4876 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
4879 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
4880 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
4881 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
4885 static int devlink_param_generic_verify(const struct devlink_param *param)
4887 /* verify it match generic parameter by id and name */
4888 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
4890 if (strcmp(param->name, devlink_param_generic[param->id].name))
4893 WARN_ON(param->type != devlink_param_generic[param->id].type);
4898 static int devlink_param_driver_verify(const struct devlink_param *param)
4902 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
4904 /* verify no such name in generic params */
4905 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
4906 if (!strcmp(param->name, devlink_param_generic[i].name))
4912 static struct devlink_param_item *
4913 devlink_param_find_by_name(struct list_head *param_list,
4914 const char *param_name)
4916 struct devlink_param_item *param_item;
4918 list_for_each_entry(param_item, param_list, list)
4919 if (!strcmp(param_item->param->name, param_name))
4924 static struct devlink_param_item *
4925 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
4927 struct devlink_param_item *param_item;
4929 list_for_each_entry(param_item, param_list, list)
4930 if (param_item->param->id == param_id)
4936 devlink_param_cmode_is_supported(const struct devlink_param *param,
4937 enum devlink_param_cmode cmode)
4939 return test_bit(cmode, ¶m->supported_cmodes);
4942 static int devlink_param_get(struct devlink *devlink,
4943 const struct devlink_param *param,
4944 struct devlink_param_gset_ctx *ctx)
4948 return param->get(devlink, param->id, ctx);
4951 static int devlink_param_set(struct devlink *devlink,
4952 const struct devlink_param *param,
4953 struct devlink_param_gset_ctx *ctx)
4957 return param->set(devlink, param->id, ctx);
4961 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
4963 switch (param_type) {
4964 case DEVLINK_PARAM_TYPE_U8:
4966 case DEVLINK_PARAM_TYPE_U16:
4968 case DEVLINK_PARAM_TYPE_U32:
4970 case DEVLINK_PARAM_TYPE_STRING:
4972 case DEVLINK_PARAM_TYPE_BOOL:
4980 devlink_nl_param_value_fill_one(struct sk_buff *msg,
4981 enum devlink_param_type type,
4982 enum devlink_param_cmode cmode,
4983 union devlink_param_value val)
4985 struct nlattr *param_value_attr;
4987 param_value_attr = nla_nest_start_noflag(msg,
4988 DEVLINK_ATTR_PARAM_VALUE);
4989 if (!param_value_attr)
4990 goto nla_put_failure;
4992 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
4993 goto value_nest_cancel;
4996 case DEVLINK_PARAM_TYPE_U8:
4997 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
4998 goto value_nest_cancel;
5000 case DEVLINK_PARAM_TYPE_U16:
5001 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
5002 goto value_nest_cancel;
5004 case DEVLINK_PARAM_TYPE_U32:
5005 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
5006 goto value_nest_cancel;
5008 case DEVLINK_PARAM_TYPE_STRING:
5009 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
5011 goto value_nest_cancel;
5013 case DEVLINK_PARAM_TYPE_BOOL:
5015 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
5016 goto value_nest_cancel;
5020 nla_nest_end(msg, param_value_attr);
5024 nla_nest_cancel(msg, param_value_attr);
5029 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
5030 unsigned int port_index,
5031 struct devlink_param_item *param_item,
5032 enum devlink_command cmd,
5033 u32 portid, u32 seq, int flags)
5035 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
5036 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
5037 const struct devlink_param *param = param_item->param;
5038 struct devlink_param_gset_ctx ctx;
5039 struct nlattr *param_values_list;
5040 struct nlattr *param_attr;
5046 /* Get value from driver part to driverinit configuration mode */
5047 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5048 if (!devlink_param_cmode_is_supported(param, i))
5050 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5051 if (!param_item->driverinit_value_valid)
5053 param_value[i] = param_item->driverinit_value;
5056 err = devlink_param_get(devlink, param, &ctx);
5059 param_value[i] = ctx.val;
5061 param_value_set[i] = true;
5064 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5068 if (devlink_nl_put_handle(msg, devlink))
5069 goto genlmsg_cancel;
5071 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
5072 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
5073 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
5074 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
5075 goto genlmsg_cancel;
5077 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
5079 goto genlmsg_cancel;
5080 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
5081 goto param_nest_cancel;
5082 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
5083 goto param_nest_cancel;
5085 nla_type = devlink_param_type_to_nla_type(param->type);
5087 goto param_nest_cancel;
5088 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
5089 goto param_nest_cancel;
5091 param_values_list = nla_nest_start_noflag(msg,
5092 DEVLINK_ATTR_PARAM_VALUES_LIST);
5093 if (!param_values_list)
5094 goto param_nest_cancel;
5096 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5097 if (!param_value_set[i])
5099 err = devlink_nl_param_value_fill_one(msg, param->type,
5102 goto values_list_nest_cancel;
5105 nla_nest_end(msg, param_values_list);
5106 nla_nest_end(msg, param_attr);
5107 genlmsg_end(msg, hdr);
5110 values_list_nest_cancel:
5111 nla_nest_end(msg, param_values_list);
5113 nla_nest_cancel(msg, param_attr);
5115 genlmsg_cancel(msg, hdr);
5119 static void devlink_param_notify(struct devlink *devlink,
5120 unsigned int port_index,
5121 struct devlink_param_item *param_item,
5122 enum devlink_command cmd)
5124 struct sk_buff *msg;
5127 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
5128 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
5129 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
5130 ASSERT_DEVLINK_REGISTERED(devlink);
5132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5135 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
5142 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
5143 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5146 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
5147 struct netlink_callback *cb)
5149 struct devlink_param_item *param_item;
5150 struct devlink *devlink;
5151 int start = cb->args[0];
5152 unsigned long index;
5156 mutex_lock(&devlink_mutex);
5157 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
5158 if (!devlink_try_get(devlink))
5161 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5164 mutex_lock(&devlink->lock);
5165 list_for_each_entry(param_item, &devlink->param_list, list) {
5170 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5171 DEVLINK_CMD_PARAM_GET,
5172 NETLINK_CB(cb->skb).portid,
5175 if (err == -EOPNOTSUPP) {
5178 mutex_unlock(&devlink->lock);
5179 devlink_put(devlink);
5184 mutex_unlock(&devlink->lock);
5186 devlink_put(devlink);
5189 mutex_unlock(&devlink_mutex);
5191 if (err != -EMSGSIZE)
5199 devlink_param_type_get_from_info(struct genl_info *info,
5200 enum devlink_param_type *param_type)
5202 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
5205 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
5207 *param_type = DEVLINK_PARAM_TYPE_U8;
5210 *param_type = DEVLINK_PARAM_TYPE_U16;
5213 *param_type = DEVLINK_PARAM_TYPE_U32;
5216 *param_type = DEVLINK_PARAM_TYPE_STRING;
5219 *param_type = DEVLINK_PARAM_TYPE_BOOL;
5229 devlink_param_value_get_from_info(const struct devlink_param *param,
5230 struct genl_info *info,
5231 union devlink_param_value *value)
5233 struct nlattr *param_data;
5236 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
5238 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
5241 switch (param->type) {
5242 case DEVLINK_PARAM_TYPE_U8:
5243 if (nla_len(param_data) != sizeof(u8))
5245 value->vu8 = nla_get_u8(param_data);
5247 case DEVLINK_PARAM_TYPE_U16:
5248 if (nla_len(param_data) != sizeof(u16))
5250 value->vu16 = nla_get_u16(param_data);
5252 case DEVLINK_PARAM_TYPE_U32:
5253 if (nla_len(param_data) != sizeof(u32))
5255 value->vu32 = nla_get_u32(param_data);
5257 case DEVLINK_PARAM_TYPE_STRING:
5258 len = strnlen(nla_data(param_data), nla_len(param_data));
5259 if (len == nla_len(param_data) ||
5260 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
5262 strcpy(value->vstr, nla_data(param_data));
5264 case DEVLINK_PARAM_TYPE_BOOL:
5265 if (param_data && nla_len(param_data))
5267 value->vbool = nla_get_flag(param_data);
5273 static struct devlink_param_item *
5274 devlink_param_get_from_info(struct list_head *param_list,
5275 struct genl_info *info)
5279 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
5282 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
5283 return devlink_param_find_by_name(param_list, param_name);
5286 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
5287 struct genl_info *info)
5289 struct devlink *devlink = info->user_ptr[0];
5290 struct devlink_param_item *param_item;
5291 struct sk_buff *msg;
5294 param_item = devlink_param_get_from_info(&devlink->param_list, info);
5298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5302 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5303 DEVLINK_CMD_PARAM_GET,
5304 info->snd_portid, info->snd_seq, 0);
5310 return genlmsg_reply(msg, info);
5313 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
5314 unsigned int port_index,
5315 struct list_head *param_list,
5316 struct genl_info *info,
5317 enum devlink_command cmd)
5319 enum devlink_param_type param_type;
5320 struct devlink_param_gset_ctx ctx;
5321 enum devlink_param_cmode cmode;
5322 struct devlink_param_item *param_item;
5323 const struct devlink_param *param;
5324 union devlink_param_value value;
5327 param_item = devlink_param_get_from_info(param_list, info);
5330 param = param_item->param;
5331 err = devlink_param_type_get_from_info(info, ¶m_type);
5334 if (param_type != param->type)
5336 err = devlink_param_value_get_from_info(param, info, &value);
5339 if (param->validate) {
5340 err = param->validate(devlink, param->id, value, info->extack);
5345 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
5347 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
5348 if (!devlink_param_cmode_is_supported(param, cmode))
5351 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5352 if (param->type == DEVLINK_PARAM_TYPE_STRING)
5353 strcpy(param_item->driverinit_value.vstr, value.vstr);
5355 param_item->driverinit_value = value;
5356 param_item->driverinit_value_valid = true;
5362 err = devlink_param_set(devlink, param, &ctx);
5367 devlink_param_notify(devlink, port_index, param_item, cmd);
5371 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
5372 struct genl_info *info)
5374 struct devlink *devlink = info->user_ptr[0];
5376 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
5377 info, DEVLINK_CMD_PARAM_NEW);
5380 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
5381 struct netlink_callback *cb)
5383 struct devlink_param_item *param_item;
5384 struct devlink_port *devlink_port;
5385 struct devlink *devlink;
5386 int start = cb->args[0];
5387 unsigned long index;
5391 mutex_lock(&devlink_mutex);
5392 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
5393 if (!devlink_try_get(devlink))
5396 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5399 mutex_lock(&devlink->lock);
5400 list_for_each_entry(devlink_port, &devlink->port_list, list) {
5401 list_for_each_entry(param_item,
5402 &devlink_port->param_list, list) {
5407 err = devlink_nl_param_fill(msg,
5408 devlink_port->devlink,
5409 devlink_port->index, param_item,
5410 DEVLINK_CMD_PORT_PARAM_GET,
5411 NETLINK_CB(cb->skb).portid,
5414 if (err == -EOPNOTSUPP) {
5417 mutex_unlock(&devlink->lock);
5418 devlink_put(devlink);
5424 mutex_unlock(&devlink->lock);
5426 devlink_put(devlink);
5429 mutex_unlock(&devlink_mutex);
5431 if (err != -EMSGSIZE)
5438 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
5439 struct genl_info *info)
5441 struct devlink_port *devlink_port = info->user_ptr[1];
5442 struct devlink_param_item *param_item;
5443 struct sk_buff *msg;
5446 param_item = devlink_param_get_from_info(&devlink_port->param_list,
5451 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5455 err = devlink_nl_param_fill(msg, devlink_port->devlink,
5456 devlink_port->index, param_item,
5457 DEVLINK_CMD_PORT_PARAM_GET,
5458 info->snd_portid, info->snd_seq, 0);
5464 return genlmsg_reply(msg, info);
5467 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
5468 struct genl_info *info)
5470 struct devlink_port *devlink_port = info->user_ptr[1];
5472 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
5473 devlink_port->index,
5474 &devlink_port->param_list, info,
5475 DEVLINK_CMD_PORT_PARAM_NEW);
5478 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
5479 struct devlink *devlink,
5480 struct devlink_snapshot *snapshot)
5482 struct nlattr *snap_attr;
5485 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5489 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5491 goto nla_put_failure;
5493 nla_nest_end(msg, snap_attr);
5497 nla_nest_cancel(msg, snap_attr);
5501 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5502 struct devlink *devlink,
5503 struct devlink_region *region)
5505 struct devlink_snapshot *snapshot;
5506 struct nlattr *snapshots_attr;
5509 snapshots_attr = nla_nest_start_noflag(msg,
5510 DEVLINK_ATTR_REGION_SNAPSHOTS);
5511 if (!snapshots_attr)
5514 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
5515 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5517 goto nla_put_failure;
5520 nla_nest_end(msg, snapshots_attr);
5524 nla_nest_cancel(msg, snapshots_attr);
5528 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5529 enum devlink_command cmd, u32 portid,
5531 struct devlink_region *region)
5536 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5540 err = devlink_nl_put_handle(msg, devlink);
5542 goto nla_put_failure;
5545 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5546 region->port->index);
5548 goto nla_put_failure;
5551 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5553 goto nla_put_failure;
5555 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5559 goto nla_put_failure;
5561 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
5562 region->max_snapshots);
5564 goto nla_put_failure;
5566 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5568 goto nla_put_failure;
5570 genlmsg_end(msg, hdr);
5574 genlmsg_cancel(msg, hdr);
5578 static struct sk_buff *
5579 devlink_nl_region_notify_build(struct devlink_region *region,
5580 struct devlink_snapshot *snapshot,
5581 enum devlink_command cmd, u32 portid, u32 seq)
5583 struct devlink *devlink = region->devlink;
5584 struct sk_buff *msg;
5589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5591 return ERR_PTR(-ENOMEM);
5593 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5599 err = devlink_nl_put_handle(msg, devlink);
5601 goto out_cancel_msg;
5604 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5605 region->port->index);
5607 goto out_cancel_msg;
5610 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5613 goto out_cancel_msg;
5616 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5619 goto out_cancel_msg;
5621 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5622 region->size, DEVLINK_ATTR_PAD);
5624 goto out_cancel_msg;
5626 genlmsg_end(msg, hdr);
5631 genlmsg_cancel(msg, hdr);
5634 return ERR_PTR(err);
5637 static void devlink_nl_region_notify(struct devlink_region *region,
5638 struct devlink_snapshot *snapshot,
5639 enum devlink_command cmd)
5641 struct devlink *devlink = region->devlink;
5642 struct sk_buff *msg;
5644 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5645 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
5648 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5652 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
5653 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5657 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5658 * @devlink: devlink instance
5659 * @id: the snapshot id
5661 * Track when a new snapshot begins using an id. Load the count for the
5662 * given id from the snapshot xarray, increment it, and store it back.
5664 * Called when a new snapshot is created with the given id.
5666 * The id *must* have been previously allocated by
5667 * devlink_region_snapshot_id_get().
5669 * Returns 0 on success, or an error on failure.
5671 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5673 unsigned long count;
5676 lockdep_assert_held(&devlink->lock);
5678 p = xa_load(&devlink->snapshot_ids, id);
5682 if (WARN_ON(!xa_is_value(p)))
5685 count = xa_to_value(p);
5688 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5693 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5694 * @devlink: devlink instance
5695 * @id: the snapshot id
5697 * Track when a snapshot is deleted and stops using an id. Load the count
5698 * for the given id from the snapshot xarray, decrement it, and store it
5701 * If the count reaches zero, erase this id from the xarray, freeing it
5702 * up for future re-use by devlink_region_snapshot_id_get().
5704 * Called when a snapshot using the given id is deleted, and when the
5705 * initial allocator of the id is finished using it.
5707 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5709 unsigned long count;
5712 lockdep_assert_held(&devlink->lock);
5714 p = xa_load(&devlink->snapshot_ids, id);
5718 if (WARN_ON(!xa_is_value(p)))
5721 count = xa_to_value(p);
5725 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5728 /* If this was the last user, we can erase this id */
5729 xa_erase(&devlink->snapshot_ids, id);
5734 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
5735 * @devlink: devlink instance
5736 * @id: the snapshot id
5738 * Mark the given snapshot id as used by inserting a zero value into the
5741 * This must be called while holding the devlink instance lock. Unlike
5742 * devlink_snapshot_id_get, the initial reference count is zero, not one.
5743 * It is expected that the id will immediately be used before
5744 * releasing the devlink instance lock.
5746 * Returns zero on success, or an error code if the snapshot id could not
5749 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5751 lockdep_assert_held(&devlink->lock);
5753 if (xa_load(&devlink->snapshot_ids, id))
5756 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5761 * __devlink_region_snapshot_id_get - get snapshot ID
5762 * @devlink: devlink instance
5763 * @id: storage to return snapshot id
5765 * Allocates a new snapshot id. Returns zero on success, or a negative
5766 * error on failure. Must be called while holding the devlink instance
5769 * Snapshot IDs are tracked using an xarray which stores the number of
5770 * users of the snapshot id.
5772 * Note that the caller of this function counts as a 'user', in order to
5773 * avoid race conditions. The caller must release its hold on the
5774 * snapshot by using devlink_region_snapshot_id_put.
5776 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5778 lockdep_assert_held(&devlink->lock);
5780 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5781 xa_limit_32b, GFP_KERNEL);
5785 * __devlink_region_snapshot_create - create a new snapshot
5786 * This will add a new snapshot of a region. The snapshot
5787 * will be stored on the region struct and can be accessed
5788 * from devlink. This is useful for future analyses of snapshots.
5789 * Multiple snapshots can be created on a region.
5790 * The @snapshot_id should be obtained using the getter function.
5792 * Must be called only while holding the devlink instance lock.
5794 * @region: devlink region of the snapshot
5795 * @data: snapshot data
5796 * @snapshot_id: snapshot id to be created
5799 __devlink_region_snapshot_create(struct devlink_region *region,
5800 u8 *data, u32 snapshot_id)
5802 struct devlink *devlink = region->devlink;
5803 struct devlink_snapshot *snapshot;
5806 lockdep_assert_held(&devlink->lock);
5808 /* check if region can hold one more snapshot */
5809 if (region->cur_snapshots == region->max_snapshots)
5812 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
5815 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5819 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
5821 goto err_snapshot_id_increment;
5823 snapshot->id = snapshot_id;
5824 snapshot->region = region;
5825 snapshot->data = data;
5827 list_add_tail(&snapshot->list, ®ion->snapshot_list);
5829 region->cur_snapshots++;
5831 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5834 err_snapshot_id_increment:
5839 static void devlink_region_snapshot_del(struct devlink_region *region,
5840 struct devlink_snapshot *snapshot)
5842 struct devlink *devlink = region->devlink;
5844 lockdep_assert_held(&devlink->lock);
5846 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
5847 region->cur_snapshots--;
5848 list_del(&snapshot->list);
5849 region->ops->destructor(snapshot->data);
5850 __devlink_snapshot_id_decrement(devlink, snapshot->id);
5854 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
5855 struct genl_info *info)
5857 struct devlink *devlink = info->user_ptr[0];
5858 struct devlink_port *port = NULL;
5859 struct devlink_region *region;
5860 const char *region_name;
5861 struct sk_buff *msg;
5865 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
5868 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5869 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5871 port = devlink_port_get_by_index(devlink, index);
5876 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5878 region = devlink_port_region_get_by_name(port, region_name);
5880 region = devlink_region_get_by_name(devlink, region_name);
5885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5889 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
5890 info->snd_portid, info->snd_seq, 0,
5897 return genlmsg_reply(msg, info);
5900 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
5901 struct netlink_callback *cb,
5902 struct devlink_port *port,
5906 struct devlink_region *region;
5909 list_for_each_entry(region, &port->region_list, list) {
5914 err = devlink_nl_region_fill(msg, port->devlink,
5915 DEVLINK_CMD_REGION_GET,
5916 NETLINK_CB(cb->skb).portid,
5918 NLM_F_MULTI, region);
5928 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
5929 struct netlink_callback *cb,
5930 struct devlink *devlink,
5934 struct devlink_region *region;
5935 struct devlink_port *port;
5938 mutex_lock(&devlink->lock);
5939 list_for_each_entry(region, &devlink->region_list, list) {
5944 err = devlink_nl_region_fill(msg, devlink,
5945 DEVLINK_CMD_REGION_GET,
5946 NETLINK_CB(cb->skb).portid,
5948 NLM_F_MULTI, region);
5954 list_for_each_entry(port, &devlink->port_list, list) {
5955 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
5962 mutex_unlock(&devlink->lock);
5966 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
5967 struct netlink_callback *cb)
5969 struct devlink *devlink;
5970 int start = cb->args[0];
5971 unsigned long index;
5975 mutex_lock(&devlink_mutex);
5976 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
5977 if (!devlink_try_get(devlink))
5980 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5983 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
5986 devlink_put(devlink);
5991 mutex_unlock(&devlink_mutex);
5996 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
5997 struct genl_info *info)
5999 struct devlink *devlink = info->user_ptr[0];
6000 struct devlink_snapshot *snapshot;
6001 struct devlink_port *port = NULL;
6002 struct devlink_region *region;
6003 const char *region_name;
6007 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
6008 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
6011 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6012 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6014 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6015 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6017 port = devlink_port_get_by_index(devlink, index);
6023 region = devlink_port_region_get_by_name(port, region_name);
6025 region = devlink_region_get_by_name(devlink, region_name);
6030 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6034 devlink_region_snapshot_del(region, snapshot);
6039 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
6041 struct devlink *devlink = info->user_ptr[0];
6042 struct devlink_snapshot *snapshot;
6043 struct devlink_port *port = NULL;
6044 struct nlattr *snapshot_id_attr;
6045 struct devlink_region *region;
6046 const char *region_name;
6052 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
6053 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
6057 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6059 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6060 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6062 port = devlink_port_get_by_index(devlink, index);
6068 region = devlink_port_region_get_by_name(port, region_name);
6070 region = devlink_region_get_by_name(devlink, region_name);
6073 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
6077 if (!region->ops->snapshot) {
6078 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
6082 if (region->cur_snapshots == region->max_snapshots) {
6083 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
6087 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6088 if (snapshot_id_attr) {
6089 snapshot_id = nla_get_u32(snapshot_id_attr);
6091 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6092 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
6096 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
6100 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
6102 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
6108 err = region->port_ops->snapshot(port, region->port_ops,
6109 info->extack, &data);
6111 err = region->ops->snapshot(devlink, region->ops,
6112 info->extack, &data);
6114 goto err_snapshot_capture;
6116 err = __devlink_region_snapshot_create(region, data, snapshot_id);
6118 goto err_snapshot_create;
6120 if (!snapshot_id_attr) {
6121 struct sk_buff *msg;
6123 snapshot = devlink_region_snapshot_get_by_id(region,
6125 if (WARN_ON(!snapshot))
6128 msg = devlink_nl_region_notify_build(region, snapshot,
6129 DEVLINK_CMD_REGION_NEW,
6132 err = PTR_ERR_OR_ZERO(msg);
6136 err = genlmsg_reply(msg, info);
6143 err_snapshot_create:
6144 region->ops->destructor(data);
6145 err_snapshot_capture:
6146 __devlink_snapshot_id_decrement(devlink, snapshot_id);
6150 devlink_region_snapshot_del(region, snapshot);
6154 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
6155 struct devlink *devlink,
6156 u8 *chunk, u32 chunk_size,
6159 struct nlattr *chunk_attr;
6162 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
6166 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
6168 goto nla_put_failure;
6170 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
6173 goto nla_put_failure;
6175 nla_nest_end(msg, chunk_attr);
6179 nla_nest_cancel(msg, chunk_attr);
6183 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
6185 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
6186 struct devlink *devlink,
6187 struct devlink_region *region,
6188 struct nlattr **attrs,
6193 struct devlink_snapshot *snapshot;
6194 u64 curr_offset = start_offset;
6198 *new_offset = start_offset;
6200 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6201 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6205 while (curr_offset < end_offset) {
6209 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
6210 data_size = end_offset - curr_offset;
6212 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
6214 data = &snapshot->data[curr_offset];
6215 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
6221 curr_offset += data_size;
6223 *new_offset = curr_offset;
6228 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
6229 struct netlink_callback *cb)
6231 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6232 u64 ret_offset, start_offset, end_offset = U64_MAX;
6233 struct nlattr **attrs = info->attrs;
6234 struct devlink_port *port = NULL;
6235 struct devlink_region *region;
6236 struct nlattr *chunks_attr;
6237 const char *region_name;
6238 struct devlink *devlink;
6243 start_offset = *((u64 *)&cb->args[0]);
6245 mutex_lock(&devlink_mutex);
6246 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6247 if (IS_ERR(devlink)) {
6248 err = PTR_ERR(devlink);
6252 mutex_lock(&devlink->lock);
6254 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
6255 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
6260 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6261 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6263 port = devlink_port_get_by_index(devlink, index);
6270 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
6273 region = devlink_port_region_get_by_name(port, region_name);
6275 region = devlink_region_get_by_name(devlink, region_name);
6282 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
6283 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
6286 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6288 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6289 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
6292 if (end_offset > region->size)
6293 end_offset = region->size;
6295 /* return 0 if there is no further data to read */
6296 if (start_offset == end_offset) {
6301 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6302 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
6303 DEVLINK_CMD_REGION_READ);
6309 err = devlink_nl_put_handle(skb, devlink);
6311 goto nla_put_failure;
6314 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
6315 region->port->index);
6317 goto nla_put_failure;
6320 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
6322 goto nla_put_failure;
6324 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
6327 goto nla_put_failure;
6330 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
6333 end_offset, &ret_offset);
6335 if (err && err != -EMSGSIZE)
6336 goto nla_put_failure;
6338 /* Check if there was any progress done to prevent infinite loop */
6339 if (ret_offset == start_offset) {
6341 goto nla_put_failure;
6344 *((u64 *)&cb->args[0]) = ret_offset;
6346 nla_nest_end(skb, chunks_attr);
6347 genlmsg_end(skb, hdr);
6348 mutex_unlock(&devlink->lock);
6349 devlink_put(devlink);
6350 mutex_unlock(&devlink_mutex);
6355 genlmsg_cancel(skb, hdr);
6357 mutex_unlock(&devlink->lock);
6358 devlink_put(devlink);
6360 mutex_unlock(&devlink_mutex);
6364 struct devlink_info_req {
6365 struct sk_buff *msg;
6368 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
6370 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
6372 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
6374 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
6376 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
6378 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
6380 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
6383 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
6386 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
6388 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
6389 const char *version_name,
6390 const char *version_value)
6392 struct nlattr *nest;
6395 nest = nla_nest_start_noflag(req->msg, attr);
6399 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
6402 goto nla_put_failure;
6404 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
6407 goto nla_put_failure;
6409 nla_nest_end(req->msg, nest);
6414 nla_nest_cancel(req->msg, nest);
6418 int devlink_info_version_fixed_put(struct devlink_info_req *req,
6419 const char *version_name,
6420 const char *version_value)
6422 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
6423 version_name, version_value);
6425 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
6427 int devlink_info_version_stored_put(struct devlink_info_req *req,
6428 const char *version_name,
6429 const char *version_value)
6431 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6432 version_name, version_value);
6434 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
6436 int devlink_info_version_running_put(struct devlink_info_req *req,
6437 const char *version_name,
6438 const char *version_value)
6440 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6441 version_name, version_value);
6443 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
6446 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
6447 enum devlink_command cmd, u32 portid,
6448 u32 seq, int flags, struct netlink_ext_ack *extack)
6450 struct devlink_info_req req;
6454 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6459 if (devlink_nl_put_handle(msg, devlink))
6460 goto err_cancel_msg;
6463 err = devlink->ops->info_get(devlink, &req, extack);
6465 goto err_cancel_msg;
6467 genlmsg_end(msg, hdr);
6471 genlmsg_cancel(msg, hdr);
6475 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
6476 struct genl_info *info)
6478 struct devlink *devlink = info->user_ptr[0];
6479 struct sk_buff *msg;
6482 if (!devlink->ops->info_get)
6485 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6489 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6490 info->snd_portid, info->snd_seq, 0,
6497 return genlmsg_reply(msg, info);
6500 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
6501 struct netlink_callback *cb)
6503 struct devlink *devlink;
6504 int start = cb->args[0];
6505 unsigned long index;
6509 mutex_lock(&devlink_mutex);
6510 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
6511 if (!devlink_try_get(devlink))
6514 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6517 if (idx < start || !devlink->ops->info_get)
6520 mutex_lock(&devlink->lock);
6521 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6522 NETLINK_CB(cb->skb).portid,
6523 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6525 mutex_unlock(&devlink->lock);
6526 if (err == -EOPNOTSUPP)
6529 devlink_put(devlink);
6535 devlink_put(devlink);
6537 mutex_unlock(&devlink_mutex);
6539 if (err != -EMSGSIZE)
6546 struct devlink_fmsg_item {
6547 struct list_head list;
6554 struct devlink_fmsg {
6555 struct list_head item_list;
6556 bool putting_binary; /* This flag forces enclosing of binary data
6557 * in an array brackets. It forces using
6558 * of designated API:
6559 * devlink_fmsg_binary_pair_nest_start()
6560 * devlink_fmsg_binary_pair_nest_end()
6564 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6566 struct devlink_fmsg *fmsg;
6568 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6572 INIT_LIST_HEAD(&fmsg->item_list);
6577 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6579 struct devlink_fmsg_item *item, *tmp;
6581 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6582 list_del(&item->list);
6588 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6591 struct devlink_fmsg_item *item;
6593 item = kzalloc(sizeof(*item), GFP_KERNEL);
6597 item->attrtype = attrtype;
6598 list_add_tail(&item->list, &fmsg->item_list);
6603 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6605 if (fmsg->putting_binary)
6608 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6610 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6612 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6614 if (fmsg->putting_binary)
6617 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6620 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6622 if (fmsg->putting_binary)
6625 return devlink_fmsg_nest_end(fmsg);
6627 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6629 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6631 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6633 struct devlink_fmsg_item *item;
6635 if (fmsg->putting_binary)
6638 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6641 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6645 item->nla_type = NLA_NUL_STRING;
6646 item->len = strlen(name) + 1;
6647 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6648 memcpy(&item->value, name, item->len);
6649 list_add_tail(&item->list, &fmsg->item_list);
6654 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6658 if (fmsg->putting_binary)
6661 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6665 err = devlink_fmsg_put_name(fmsg, name);
6671 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6673 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6675 if (fmsg->putting_binary)
6678 return devlink_fmsg_nest_end(fmsg);
6680 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6682 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6687 if (fmsg->putting_binary)
6690 err = devlink_fmsg_pair_nest_start(fmsg, name);
6694 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6700 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6702 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6706 if (fmsg->putting_binary)
6709 err = devlink_fmsg_nest_end(fmsg);
6713 err = devlink_fmsg_nest_end(fmsg);
6719 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6721 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6726 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6730 fmsg->putting_binary = true;
6733 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6735 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6737 if (!fmsg->putting_binary)
6740 fmsg->putting_binary = false;
6741 return devlink_fmsg_arr_pair_nest_end(fmsg);
6743 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6745 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6746 const void *value, u16 value_len,
6749 struct devlink_fmsg_item *item;
6751 if (value_len > DEVLINK_FMSG_MAX_SIZE)
6754 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
6758 item->nla_type = value_nla_type;
6759 item->len = value_len;
6760 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6761 memcpy(&item->value, value, item->len);
6762 list_add_tail(&item->list, &fmsg->item_list);
6767 static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
6769 if (fmsg->putting_binary)
6772 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
6775 static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
6777 if (fmsg->putting_binary)
6780 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
6783 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
6785 if (fmsg->putting_binary)
6788 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
6790 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
6792 static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
6794 if (fmsg->putting_binary)
6797 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
6800 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
6802 if (fmsg->putting_binary)
6805 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
6808 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
6810 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
6813 if (!fmsg->putting_binary)
6816 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
6818 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
6820 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
6825 err = devlink_fmsg_pair_nest_start(fmsg, name);
6829 err = devlink_fmsg_bool_put(fmsg, value);
6833 err = devlink_fmsg_pair_nest_end(fmsg);
6839 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
6841 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
6846 err = devlink_fmsg_pair_nest_start(fmsg, name);
6850 err = devlink_fmsg_u8_put(fmsg, value);
6854 err = devlink_fmsg_pair_nest_end(fmsg);
6860 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
6862 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
6867 err = devlink_fmsg_pair_nest_start(fmsg, name);
6871 err = devlink_fmsg_u32_put(fmsg, value);
6875 err = devlink_fmsg_pair_nest_end(fmsg);
6881 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
6883 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
6888 err = devlink_fmsg_pair_nest_start(fmsg, name);
6892 err = devlink_fmsg_u64_put(fmsg, value);
6896 err = devlink_fmsg_pair_nest_end(fmsg);
6902 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
6904 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
6909 err = devlink_fmsg_pair_nest_start(fmsg, name);
6913 err = devlink_fmsg_string_put(fmsg, value);
6917 err = devlink_fmsg_pair_nest_end(fmsg);
6923 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
6925 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
6926 const void *value, u32 value_len)
6933 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
6937 for (offset = 0; offset < value_len; offset += data_size) {
6938 data_size = value_len - offset;
6939 if (data_size > DEVLINK_FMSG_MAX_SIZE)
6940 data_size = DEVLINK_FMSG_MAX_SIZE;
6941 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
6944 /* Exit from loop with a break (instead of
6945 * return) to make sure putting_binary is turned off in
6946 * devlink_fmsg_binary_pair_nest_end
6950 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
6956 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
6959 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
6961 switch (msg->nla_type) {
6966 case NLA_NUL_STRING:
6968 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
6976 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
6978 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6981 switch (msg->nla_type) {
6983 /* Always provide flag data, regardless of its value */
6984 tmp = *(bool *) msg->value;
6986 return nla_put_u8(skb, attrtype, tmp);
6988 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
6990 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
6992 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
6994 case NLA_NUL_STRING:
6995 return nla_put_string(skb, attrtype, (char *) &msg->value);
6997 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
7004 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7007 struct devlink_fmsg_item *item;
7008 struct nlattr *fmsg_nlattr;
7012 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
7016 list_for_each_entry(item, &fmsg->item_list, list) {
7022 switch (item->attrtype) {
7023 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7024 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7025 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7026 case DEVLINK_ATTR_FMSG_NEST_END:
7027 err = nla_put_flag(skb, item->attrtype);
7029 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
7030 err = devlink_fmsg_item_fill_type(item, skb);
7033 err = devlink_fmsg_item_fill_data(item, skb);
7035 case DEVLINK_ATTR_FMSG_OBJ_NAME:
7036 err = nla_put_string(skb, item->attrtype,
7037 (char *) &item->value);
7049 nla_nest_end(skb, fmsg_nlattr);
7053 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
7054 struct genl_info *info,
7055 enum devlink_command cmd, int flags)
7057 struct nlmsghdr *nlh;
7058 struct sk_buff *skb;
7065 int tmp_index = index;
7067 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7071 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
7072 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
7075 goto nla_put_failure;
7078 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7081 else if (err != -EMSGSIZE || tmp_index == index)
7082 goto nla_put_failure;
7084 genlmsg_end(skb, hdr);
7085 err = genlmsg_reply(skb, info);
7090 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7093 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
7094 NLMSG_DONE, 0, flags | NLM_F_MULTI);
7097 goto nla_put_failure;
7100 return genlmsg_reply(skb, info);
7107 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7108 struct netlink_callback *cb,
7109 enum devlink_command cmd)
7111 int index = cb->args[0];
7112 int tmp_index = index;
7116 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7117 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
7120 goto nla_put_failure;
7123 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7124 if ((err && err != -EMSGSIZE) || tmp_index == index)
7125 goto nla_put_failure;
7127 cb->args[0] = index;
7128 genlmsg_end(skb, hdr);
7132 genlmsg_cancel(skb, hdr);
7136 struct devlink_health_reporter {
7137 struct list_head list;
7139 const struct devlink_health_reporter_ops *ops;
7140 struct devlink *devlink;
7141 struct devlink_port *devlink_port;
7142 struct devlink_fmsg *dump_fmsg;
7143 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
7144 u64 graceful_period;
7152 u64 last_recovery_ts;
7153 refcount_t refcount;
7157 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
7159 return reporter->priv;
7161 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
7163 static struct devlink_health_reporter *
7164 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
7165 struct mutex *list_lock,
7166 const char *reporter_name)
7168 struct devlink_health_reporter *reporter;
7170 lockdep_assert_held(list_lock);
7171 list_for_each_entry(reporter, reporter_list, list)
7172 if (!strcmp(reporter->ops->name, reporter_name))
7177 static struct devlink_health_reporter *
7178 devlink_health_reporter_find_by_name(struct devlink *devlink,
7179 const char *reporter_name)
7181 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
7182 &devlink->reporters_lock,
7186 static struct devlink_health_reporter *
7187 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
7188 const char *reporter_name)
7190 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
7191 &devlink_port->reporters_lock,
7195 static struct devlink_health_reporter *
7196 __devlink_health_reporter_create(struct devlink *devlink,
7197 const struct devlink_health_reporter_ops *ops,
7198 u64 graceful_period, void *priv)
7200 struct devlink_health_reporter *reporter;
7202 if (WARN_ON(graceful_period && !ops->recover))
7203 return ERR_PTR(-EINVAL);
7205 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
7207 return ERR_PTR(-ENOMEM);
7209 reporter->priv = priv;
7210 reporter->ops = ops;
7211 reporter->devlink = devlink;
7212 reporter->graceful_period = graceful_period;
7213 reporter->auto_recover = !!ops->recover;
7214 reporter->auto_dump = !!ops->dump;
7215 mutex_init(&reporter->dump_lock);
7216 refcount_set(&reporter->refcount, 1);
7221 * devlink_port_health_reporter_create - create devlink health reporter for
7222 * specified port instance
7224 * @port: devlink_port which should contain the new reporter
7226 * @graceful_period: to avoid recovery loops, in msecs
7229 struct devlink_health_reporter *
7230 devlink_port_health_reporter_create(struct devlink_port *port,
7231 const struct devlink_health_reporter_ops *ops,
7232 u64 graceful_period, void *priv)
7234 struct devlink_health_reporter *reporter;
7236 mutex_lock(&port->reporters_lock);
7237 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
7238 &port->reporters_lock, ops->name)) {
7239 reporter = ERR_PTR(-EEXIST);
7243 reporter = __devlink_health_reporter_create(port->devlink, ops,
7244 graceful_period, priv);
7245 if (IS_ERR(reporter))
7248 reporter->devlink_port = port;
7249 list_add_tail(&reporter->list, &port->reporter_list);
7251 mutex_unlock(&port->reporters_lock);
7254 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
7257 * devlink_health_reporter_create - create devlink health reporter
7261 * @graceful_period: to avoid recovery loops, in msecs
7264 struct devlink_health_reporter *
7265 devlink_health_reporter_create(struct devlink *devlink,
7266 const struct devlink_health_reporter_ops *ops,
7267 u64 graceful_period, void *priv)
7269 struct devlink_health_reporter *reporter;
7271 mutex_lock(&devlink->reporters_lock);
7272 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
7273 reporter = ERR_PTR(-EEXIST);
7277 reporter = __devlink_health_reporter_create(devlink, ops,
7278 graceful_period, priv);
7279 if (IS_ERR(reporter))
7282 list_add_tail(&reporter->list, &devlink->reporter_list);
7284 mutex_unlock(&devlink->reporters_lock);
7287 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
7290 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
7292 mutex_destroy(&reporter->dump_lock);
7293 if (reporter->dump_fmsg)
7294 devlink_fmsg_free(reporter->dump_fmsg);
7299 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
7301 if (refcount_dec_and_test(&reporter->refcount))
7302 devlink_health_reporter_free(reporter);
7306 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7308 list_del(&reporter->list);
7309 devlink_health_reporter_put(reporter);
7313 * devlink_health_reporter_destroy - destroy devlink health reporter
7315 * @reporter: devlink health reporter to destroy
7318 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7320 struct mutex *lock = &reporter->devlink->reporters_lock;
7323 __devlink_health_reporter_destroy(reporter);
7326 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
7329 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
7331 * @reporter: devlink health reporter to destroy
7334 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
7336 struct mutex *lock = &reporter->devlink_port->reporters_lock;
7339 __devlink_health_reporter_destroy(reporter);
7342 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
7345 devlink_nl_health_reporter_fill(struct sk_buff *msg,
7346 struct devlink_health_reporter *reporter,
7347 enum devlink_command cmd, u32 portid,
7350 struct devlink *devlink = reporter->devlink;
7351 struct nlattr *reporter_attr;
7354 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7358 if (devlink_nl_put_handle(msg, devlink))
7359 goto genlmsg_cancel;
7361 if (reporter->devlink_port) {
7362 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
7363 goto genlmsg_cancel;
7365 reporter_attr = nla_nest_start_noflag(msg,
7366 DEVLINK_ATTR_HEALTH_REPORTER);
7368 goto genlmsg_cancel;
7369 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
7370 reporter->ops->name))
7371 goto reporter_nest_cancel;
7372 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
7373 reporter->health_state))
7374 goto reporter_nest_cancel;
7375 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
7376 reporter->error_count, DEVLINK_ATTR_PAD))
7377 goto reporter_nest_cancel;
7378 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
7379 reporter->recovery_count, DEVLINK_ATTR_PAD))
7380 goto reporter_nest_cancel;
7381 if (reporter->ops->recover &&
7382 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
7383 reporter->graceful_period,
7385 goto reporter_nest_cancel;
7386 if (reporter->ops->recover &&
7387 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
7388 reporter->auto_recover))
7389 goto reporter_nest_cancel;
7390 if (reporter->dump_fmsg &&
7391 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
7392 jiffies_to_msecs(reporter->dump_ts),
7394 goto reporter_nest_cancel;
7395 if (reporter->dump_fmsg &&
7396 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
7397 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
7398 goto reporter_nest_cancel;
7399 if (reporter->ops->dump &&
7400 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
7401 reporter->auto_dump))
7402 goto reporter_nest_cancel;
7404 nla_nest_end(msg, reporter_attr);
7405 genlmsg_end(msg, hdr);
7408 reporter_nest_cancel:
7409 nla_nest_end(msg, reporter_attr);
7411 genlmsg_cancel(msg, hdr);
7415 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7416 enum devlink_command cmd)
7418 struct devlink *devlink = reporter->devlink;
7419 struct sk_buff *msg;
7422 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7423 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7425 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7429 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7435 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7436 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7440 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7442 reporter->recovery_count++;
7443 reporter->last_recovery_ts = jiffies;
7445 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7448 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7449 void *priv_ctx, struct netlink_ext_ack *extack)
7453 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7456 if (!reporter->ops->recover)
7459 err = reporter->ops->recover(reporter, priv_ctx, extack);
7463 devlink_health_reporter_recovery_done(reporter);
7464 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7465 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7471 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7473 if (!reporter->dump_fmsg)
7475 devlink_fmsg_free(reporter->dump_fmsg);
7476 reporter->dump_fmsg = NULL;
7479 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7481 struct netlink_ext_ack *extack)
7485 if (!reporter->ops->dump)
7488 if (reporter->dump_fmsg)
7491 reporter->dump_fmsg = devlink_fmsg_alloc();
7492 if (!reporter->dump_fmsg) {
7497 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7501 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7506 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7510 reporter->dump_ts = jiffies;
7511 reporter->dump_real_ts = ktime_get_real_ns();
7516 devlink_health_dump_clear(reporter);
7520 int devlink_health_report(struct devlink_health_reporter *reporter,
7521 const char *msg, void *priv_ctx)
7523 enum devlink_health_reporter_state prev_health_state;
7524 struct devlink *devlink = reporter->devlink;
7525 unsigned long recover_ts_threshold;
7527 /* write a log message of the current error */
7529 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7530 reporter->error_count++;
7531 prev_health_state = reporter->health_state;
7532 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7533 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7535 /* abort if the previous error wasn't recovered */
7536 recover_ts_threshold = reporter->last_recovery_ts +
7537 msecs_to_jiffies(reporter->graceful_period);
7538 if (reporter->auto_recover &&
7539 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7540 (reporter->last_recovery_ts && reporter->recovery_count &&
7541 time_is_after_jiffies(recover_ts_threshold)))) {
7542 trace_devlink_health_recover_aborted(devlink,
7543 reporter->ops->name,
7544 reporter->health_state,
7546 reporter->last_recovery_ts);
7550 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7552 if (reporter->auto_dump) {
7553 mutex_lock(&reporter->dump_lock);
7554 /* store current dump of current error, for later analysis */
7555 devlink_health_do_dump(reporter, priv_ctx, NULL);
7556 mutex_unlock(&reporter->dump_lock);
7559 if (reporter->auto_recover)
7560 return devlink_health_reporter_recover(reporter,
7565 EXPORT_SYMBOL_GPL(devlink_health_report);
7567 static struct devlink_health_reporter *
7568 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7569 struct nlattr **attrs)
7571 struct devlink_health_reporter *reporter;
7572 struct devlink_port *devlink_port;
7573 char *reporter_name;
7575 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7578 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7579 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7580 if (IS_ERR(devlink_port)) {
7581 mutex_lock(&devlink->reporters_lock);
7582 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7584 refcount_inc(&reporter->refcount);
7585 mutex_unlock(&devlink->reporters_lock);
7587 mutex_lock(&devlink_port->reporters_lock);
7588 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7590 refcount_inc(&reporter->refcount);
7591 mutex_unlock(&devlink_port->reporters_lock);
7597 static struct devlink_health_reporter *
7598 devlink_health_reporter_get_from_info(struct devlink *devlink,
7599 struct genl_info *info)
7601 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7604 static struct devlink_health_reporter *
7605 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7607 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7608 struct devlink_health_reporter *reporter;
7609 struct nlattr **attrs = info->attrs;
7610 struct devlink *devlink;
7612 mutex_lock(&devlink_mutex);
7613 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
7614 if (IS_ERR(devlink))
7617 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7618 devlink_put(devlink);
7619 mutex_unlock(&devlink_mutex);
7622 mutex_unlock(&devlink_mutex);
7627 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7628 enum devlink_health_reporter_state state)
7630 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7631 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7634 if (reporter->health_state == state)
7637 reporter->health_state = state;
7638 trace_devlink_health_reporter_state_update(reporter->devlink,
7639 reporter->ops->name, state);
7640 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7642 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7644 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7645 struct genl_info *info)
7647 struct devlink *devlink = info->user_ptr[0];
7648 struct devlink_health_reporter *reporter;
7649 struct sk_buff *msg;
7652 reporter = devlink_health_reporter_get_from_info(devlink, info);
7656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7662 err = devlink_nl_health_reporter_fill(msg, reporter,
7663 DEVLINK_CMD_HEALTH_REPORTER_GET,
7664 info->snd_portid, info->snd_seq,
7671 err = genlmsg_reply(msg, info);
7673 devlink_health_reporter_put(reporter);
7678 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7679 struct netlink_callback *cb)
7681 struct devlink_health_reporter *reporter;
7682 struct devlink_port *port;
7683 struct devlink *devlink;
7684 int start = cb->args[0];
7685 unsigned long index;
7689 mutex_lock(&devlink_mutex);
7690 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7691 if (!devlink_try_get(devlink))
7694 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7697 mutex_lock(&devlink->reporters_lock);
7698 list_for_each_entry(reporter, &devlink->reporter_list,
7704 err = devlink_nl_health_reporter_fill(
7705 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7706 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7709 mutex_unlock(&devlink->reporters_lock);
7710 devlink_put(devlink);
7715 mutex_unlock(&devlink->reporters_lock);
7717 devlink_put(devlink);
7720 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7721 if (!devlink_try_get(devlink))
7724 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7727 mutex_lock(&devlink->lock);
7728 list_for_each_entry(port, &devlink->port_list, list) {
7729 mutex_lock(&port->reporters_lock);
7730 list_for_each_entry(reporter, &port->reporter_list, list) {
7735 err = devlink_nl_health_reporter_fill(
7737 DEVLINK_CMD_HEALTH_REPORTER_GET,
7738 NETLINK_CB(cb->skb).portid,
7739 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7741 mutex_unlock(&port->reporters_lock);
7742 mutex_unlock(&devlink->lock);
7743 devlink_put(devlink);
7748 mutex_unlock(&port->reporters_lock);
7750 mutex_unlock(&devlink->lock);
7752 devlink_put(devlink);
7755 mutex_unlock(&devlink_mutex);
7762 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7763 struct genl_info *info)
7765 struct devlink *devlink = info->user_ptr[0];
7766 struct devlink_health_reporter *reporter;
7769 reporter = devlink_health_reporter_get_from_info(devlink, info);
7773 if (!reporter->ops->recover &&
7774 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
7775 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
7779 if (!reporter->ops->dump &&
7780 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
7785 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7786 reporter->graceful_period =
7787 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
7789 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7790 reporter->auto_recover =
7791 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
7793 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7794 reporter->auto_dump =
7795 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
7797 devlink_health_reporter_put(reporter);
7800 devlink_health_reporter_put(reporter);
7804 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
7805 struct genl_info *info)
7807 struct devlink *devlink = info->user_ptr[0];
7808 struct devlink_health_reporter *reporter;
7811 reporter = devlink_health_reporter_get_from_info(devlink, info);
7815 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
7817 devlink_health_reporter_put(reporter);
7821 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
7822 struct genl_info *info)
7824 struct devlink *devlink = info->user_ptr[0];
7825 struct devlink_health_reporter *reporter;
7826 struct devlink_fmsg *fmsg;
7829 reporter = devlink_health_reporter_get_from_info(devlink, info);
7833 if (!reporter->ops->diagnose) {
7834 devlink_health_reporter_put(reporter);
7838 fmsg = devlink_fmsg_alloc();
7840 devlink_health_reporter_put(reporter);
7844 err = devlink_fmsg_obj_nest_start(fmsg);
7848 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
7852 err = devlink_fmsg_obj_nest_end(fmsg);
7856 err = devlink_fmsg_snd(fmsg, info,
7857 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
7860 devlink_fmsg_free(fmsg);
7861 devlink_health_reporter_put(reporter);
7866 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
7867 struct netlink_callback *cb)
7869 struct devlink_health_reporter *reporter;
7870 u64 start = cb->args[0];
7873 reporter = devlink_health_reporter_get_from_cb(cb);
7877 if (!reporter->ops->dump) {
7881 mutex_lock(&reporter->dump_lock);
7883 err = devlink_health_do_dump(reporter, NULL, cb->extack);
7886 cb->args[1] = reporter->dump_ts;
7888 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
7889 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
7894 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
7895 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
7897 mutex_unlock(&reporter->dump_lock);
7899 devlink_health_reporter_put(reporter);
7904 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
7905 struct genl_info *info)
7907 struct devlink *devlink = info->user_ptr[0];
7908 struct devlink_health_reporter *reporter;
7910 reporter = devlink_health_reporter_get_from_info(devlink, info);
7914 if (!reporter->ops->dump) {
7915 devlink_health_reporter_put(reporter);
7919 mutex_lock(&reporter->dump_lock);
7920 devlink_health_dump_clear(reporter);
7921 mutex_unlock(&reporter->dump_lock);
7922 devlink_health_reporter_put(reporter);
7926 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
7927 struct genl_info *info)
7929 struct devlink *devlink = info->user_ptr[0];
7930 struct devlink_health_reporter *reporter;
7933 reporter = devlink_health_reporter_get_from_info(devlink, info);
7937 if (!reporter->ops->test) {
7938 devlink_health_reporter_put(reporter);
7942 err = reporter->ops->test(reporter, info->extack);
7944 devlink_health_reporter_put(reporter);
7948 struct devlink_stats {
7949 u64_stats_t rx_bytes;
7950 u64_stats_t rx_packets;
7951 struct u64_stats_sync syncp;
7955 * struct devlink_trap_policer_item - Packet trap policer attributes.
7956 * @policer: Immutable packet trap policer attributes.
7957 * @rate: Rate in packets / sec.
7958 * @burst: Burst size in packets.
7959 * @list: trap_policer_list member.
7961 * Describes packet trap policer attributes. Created by devlink during trap
7962 * policer registration.
7964 struct devlink_trap_policer_item {
7965 const struct devlink_trap_policer *policer;
7968 struct list_head list;
7972 * struct devlink_trap_group_item - Packet trap group attributes.
7973 * @group: Immutable packet trap group attributes.
7974 * @policer_item: Associated policer item. Can be NULL.
7975 * @list: trap_group_list member.
7976 * @stats: Trap group statistics.
7978 * Describes packet trap group attributes. Created by devlink during trap
7979 * group registration.
7981 struct devlink_trap_group_item {
7982 const struct devlink_trap_group *group;
7983 struct devlink_trap_policer_item *policer_item;
7984 struct list_head list;
7985 struct devlink_stats __percpu *stats;
7989 * struct devlink_trap_item - Packet trap attributes.
7990 * @trap: Immutable packet trap attributes.
7991 * @group_item: Associated group item.
7992 * @list: trap_list member.
7993 * @action: Trap action.
7994 * @stats: Trap statistics.
7995 * @priv: Driver private information.
7997 * Describes both mutable and immutable packet trap attributes. Created by
7998 * devlink during trap registration and used for all trap related operations.
8000 struct devlink_trap_item {
8001 const struct devlink_trap *trap;
8002 struct devlink_trap_group_item *group_item;
8003 struct list_head list;
8004 enum devlink_trap_action action;
8005 struct devlink_stats __percpu *stats;
8009 static struct devlink_trap_policer_item *
8010 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
8012 struct devlink_trap_policer_item *policer_item;
8014 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8015 if (policer_item->policer->id == id)
8016 return policer_item;
8022 static struct devlink_trap_item *
8023 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
8025 struct devlink_trap_item *trap_item;
8027 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8028 if (!strcmp(trap_item->trap->name, name))
8035 static struct devlink_trap_item *
8036 devlink_trap_item_get_from_info(struct devlink *devlink,
8037 struct genl_info *info)
8039 struct nlattr *attr;
8041 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
8043 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
8045 return devlink_trap_item_lookup(devlink, nla_data(attr));
8049 devlink_trap_action_get_from_info(struct genl_info *info,
8050 enum devlink_trap_action *p_trap_action)
8054 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
8056 case DEVLINK_TRAP_ACTION_DROP:
8057 case DEVLINK_TRAP_ACTION_TRAP:
8058 case DEVLINK_TRAP_ACTION_MIRROR:
8059 *p_trap_action = val;
8068 static int devlink_trap_metadata_put(struct sk_buff *msg,
8069 const struct devlink_trap *trap)
8071 struct nlattr *attr;
8073 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
8077 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
8078 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
8079 goto nla_put_failure;
8080 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
8081 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
8082 goto nla_put_failure;
8084 nla_nest_end(msg, attr);
8089 nla_nest_cancel(msg, attr);
8093 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
8094 struct devlink_stats *stats)
8098 memset(stats, 0, sizeof(*stats));
8099 for_each_possible_cpu(i) {
8100 struct devlink_stats *cpu_stats;
8101 u64 rx_packets, rx_bytes;
8104 cpu_stats = per_cpu_ptr(trap_stats, i);
8106 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
8107 rx_packets = u64_stats_read(&cpu_stats->rx_packets);
8108 rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
8109 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
8111 u64_stats_add(&stats->rx_packets, rx_packets);
8112 u64_stats_add(&stats->rx_bytes, rx_bytes);
8117 devlink_trap_group_stats_put(struct sk_buff *msg,
8118 struct devlink_stats __percpu *trap_stats)
8120 struct devlink_stats stats;
8121 struct nlattr *attr;
8123 devlink_trap_stats_read(trap_stats, &stats);
8125 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8129 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8130 u64_stats_read(&stats.rx_packets),
8132 goto nla_put_failure;
8134 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8135 u64_stats_read(&stats.rx_bytes),
8137 goto nla_put_failure;
8139 nla_nest_end(msg, attr);
8144 nla_nest_cancel(msg, attr);
8148 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8149 const struct devlink_trap_item *trap_item)
8151 struct devlink_stats stats;
8152 struct nlattr *attr;
8156 if (devlink->ops->trap_drop_counter_get) {
8157 err = devlink->ops->trap_drop_counter_get(devlink,
8164 devlink_trap_stats_read(trap_item->stats, &stats);
8166 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8170 if (devlink->ops->trap_drop_counter_get &&
8171 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8173 goto nla_put_failure;
8175 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8176 u64_stats_read(&stats.rx_packets),
8178 goto nla_put_failure;
8180 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8181 u64_stats_read(&stats.rx_bytes),
8183 goto nla_put_failure;
8185 nla_nest_end(msg, attr);
8190 nla_nest_cancel(msg, attr);
8194 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8195 const struct devlink_trap_item *trap_item,
8196 enum devlink_command cmd, u32 portid, u32 seq,
8199 struct devlink_trap_group_item *group_item = trap_item->group_item;
8203 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8207 if (devlink_nl_put_handle(msg, devlink))
8208 goto nla_put_failure;
8210 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8211 group_item->group->name))
8212 goto nla_put_failure;
8214 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8215 goto nla_put_failure;
8217 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8218 goto nla_put_failure;
8220 if (trap_item->trap->generic &&
8221 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8222 goto nla_put_failure;
8224 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8225 goto nla_put_failure;
8227 err = devlink_trap_metadata_put(msg, trap_item->trap);
8229 goto nla_put_failure;
8231 err = devlink_trap_stats_put(msg, devlink, trap_item);
8233 goto nla_put_failure;
8235 genlmsg_end(msg, hdr);
8240 genlmsg_cancel(msg, hdr);
8244 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8245 struct genl_info *info)
8247 struct netlink_ext_ack *extack = info->extack;
8248 struct devlink *devlink = info->user_ptr[0];
8249 struct devlink_trap_item *trap_item;
8250 struct sk_buff *msg;
8253 if (list_empty(&devlink->trap_list))
8256 trap_item = devlink_trap_item_get_from_info(devlink, info);
8258 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8262 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8266 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8267 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8272 return genlmsg_reply(msg, info);
8279 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
8280 struct netlink_callback *cb)
8282 struct devlink_trap_item *trap_item;
8283 struct devlink *devlink;
8284 int start = cb->args[0];
8285 unsigned long index;
8289 mutex_lock(&devlink_mutex);
8290 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8291 if (!devlink_try_get(devlink))
8294 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8297 mutex_lock(&devlink->lock);
8298 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8303 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8304 DEVLINK_CMD_TRAP_NEW,
8305 NETLINK_CB(cb->skb).portid,
8309 mutex_unlock(&devlink->lock);
8310 devlink_put(devlink);
8315 mutex_unlock(&devlink->lock);
8317 devlink_put(devlink);
8320 mutex_unlock(&devlink_mutex);
8326 static int __devlink_trap_action_set(struct devlink *devlink,
8327 struct devlink_trap_item *trap_item,
8328 enum devlink_trap_action trap_action,
8329 struct netlink_ext_ack *extack)
8333 if (trap_item->action != trap_action &&
8334 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8335 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8339 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8340 trap_action, extack);
8344 trap_item->action = trap_action;
8349 static int devlink_trap_action_set(struct devlink *devlink,
8350 struct devlink_trap_item *trap_item,
8351 struct genl_info *info)
8353 enum devlink_trap_action trap_action;
8356 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8359 err = devlink_trap_action_get_from_info(info, &trap_action);
8361 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8365 return __devlink_trap_action_set(devlink, trap_item, trap_action,
8369 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8370 struct genl_info *info)
8372 struct netlink_ext_ack *extack = info->extack;
8373 struct devlink *devlink = info->user_ptr[0];
8374 struct devlink_trap_item *trap_item;
8376 if (list_empty(&devlink->trap_list))
8379 trap_item = devlink_trap_item_get_from_info(devlink, info);
8381 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8385 return devlink_trap_action_set(devlink, trap_item, info);
8388 static struct devlink_trap_group_item *
8389 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8391 struct devlink_trap_group_item *group_item;
8393 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8394 if (!strcmp(group_item->group->name, name))
8401 static struct devlink_trap_group_item *
8402 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8404 struct devlink_trap_group_item *group_item;
8406 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8407 if (group_item->group->id == id)
8414 static struct devlink_trap_group_item *
8415 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8416 struct genl_info *info)
8420 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8422 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8424 return devlink_trap_group_item_lookup(devlink, name);
8428 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8429 const struct devlink_trap_group_item *group_item,
8430 enum devlink_command cmd, u32 portid, u32 seq,
8436 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8440 if (devlink_nl_put_handle(msg, devlink))
8441 goto nla_put_failure;
8443 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8444 group_item->group->name))
8445 goto nla_put_failure;
8447 if (group_item->group->generic &&
8448 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8449 goto nla_put_failure;
8451 if (group_item->policer_item &&
8452 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8453 group_item->policer_item->policer->id))
8454 goto nla_put_failure;
8456 err = devlink_trap_group_stats_put(msg, group_item->stats);
8458 goto nla_put_failure;
8460 genlmsg_end(msg, hdr);
8465 genlmsg_cancel(msg, hdr);
8469 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8470 struct genl_info *info)
8472 struct netlink_ext_ack *extack = info->extack;
8473 struct devlink *devlink = info->user_ptr[0];
8474 struct devlink_trap_group_item *group_item;
8475 struct sk_buff *msg;
8478 if (list_empty(&devlink->trap_group_list))
8481 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8483 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8487 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8491 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8492 DEVLINK_CMD_TRAP_GROUP_NEW,
8493 info->snd_portid, info->snd_seq, 0);
8495 goto err_trap_group_fill;
8497 return genlmsg_reply(msg, info);
8499 err_trap_group_fill:
8504 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8505 struct netlink_callback *cb)
8507 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8508 struct devlink_trap_group_item *group_item;
8509 u32 portid = NETLINK_CB(cb->skb).portid;
8510 struct devlink *devlink;
8511 int start = cb->args[0];
8512 unsigned long index;
8516 mutex_lock(&devlink_mutex);
8517 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8518 if (!devlink_try_get(devlink))
8521 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8524 mutex_lock(&devlink->lock);
8525 list_for_each_entry(group_item, &devlink->trap_group_list,
8531 err = devlink_nl_trap_group_fill(msg, devlink,
8537 mutex_unlock(&devlink->lock);
8538 devlink_put(devlink);
8543 mutex_unlock(&devlink->lock);
8545 devlink_put(devlink);
8548 mutex_unlock(&devlink_mutex);
8555 __devlink_trap_group_action_set(struct devlink *devlink,
8556 struct devlink_trap_group_item *group_item,
8557 enum devlink_trap_action trap_action,
8558 struct netlink_ext_ack *extack)
8560 const char *group_name = group_item->group->name;
8561 struct devlink_trap_item *trap_item;
8564 if (devlink->ops->trap_group_action_set) {
8565 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8566 trap_action, extack);
8570 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8571 if (strcmp(trap_item->group_item->group->name, group_name))
8573 if (trap_item->action != trap_action &&
8574 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8576 trap_item->action = trap_action;
8582 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8583 if (strcmp(trap_item->group_item->group->name, group_name))
8585 err = __devlink_trap_action_set(devlink, trap_item,
8586 trap_action, extack);
8595 devlink_trap_group_action_set(struct devlink *devlink,
8596 struct devlink_trap_group_item *group_item,
8597 struct genl_info *info, bool *p_modified)
8599 enum devlink_trap_action trap_action;
8602 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8605 err = devlink_trap_action_get_from_info(info, &trap_action);
8607 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8611 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8621 static int devlink_trap_group_set(struct devlink *devlink,
8622 struct devlink_trap_group_item *group_item,
8623 struct genl_info *info)
8625 struct devlink_trap_policer_item *policer_item;
8626 struct netlink_ext_ack *extack = info->extack;
8627 const struct devlink_trap_policer *policer;
8628 struct nlattr **attrs = info->attrs;
8631 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8634 if (!devlink->ops->trap_group_set)
8637 policer_item = group_item->policer_item;
8638 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8641 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8642 policer_item = devlink_trap_policer_item_lookup(devlink,
8644 if (policer_id && !policer_item) {
8645 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8649 policer = policer_item ? policer_item->policer : NULL;
8651 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8656 group_item->policer_item = policer_item;
8661 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8662 struct genl_info *info)
8664 struct netlink_ext_ack *extack = info->extack;
8665 struct devlink *devlink = info->user_ptr[0];
8666 struct devlink_trap_group_item *group_item;
8667 bool modified = false;
8670 if (list_empty(&devlink->trap_group_list))
8673 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8675 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8679 err = devlink_trap_group_action_set(devlink, group_item, info,
8684 err = devlink_trap_group_set(devlink, group_item, info);
8686 goto err_trap_group_set;
8692 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8696 static struct devlink_trap_policer_item *
8697 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8698 struct genl_info *info)
8702 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8704 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8706 return devlink_trap_policer_item_lookup(devlink, id);
8710 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8711 const struct devlink_trap_policer *policer)
8713 struct nlattr *attr;
8717 if (!devlink->ops->trap_policer_counter_get)
8720 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8724 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8728 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8730 goto nla_put_failure;
8732 nla_nest_end(msg, attr);
8737 nla_nest_cancel(msg, attr);
8742 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8743 const struct devlink_trap_policer_item *policer_item,
8744 enum devlink_command cmd, u32 portid, u32 seq,
8750 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8754 if (devlink_nl_put_handle(msg, devlink))
8755 goto nla_put_failure;
8757 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8758 policer_item->policer->id))
8759 goto nla_put_failure;
8761 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8762 policer_item->rate, DEVLINK_ATTR_PAD))
8763 goto nla_put_failure;
8765 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8766 policer_item->burst, DEVLINK_ATTR_PAD))
8767 goto nla_put_failure;
8769 err = devlink_trap_policer_stats_put(msg, devlink,
8770 policer_item->policer);
8772 goto nla_put_failure;
8774 genlmsg_end(msg, hdr);
8779 genlmsg_cancel(msg, hdr);
8783 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8784 struct genl_info *info)
8786 struct devlink_trap_policer_item *policer_item;
8787 struct netlink_ext_ack *extack = info->extack;
8788 struct devlink *devlink = info->user_ptr[0];
8789 struct sk_buff *msg;
8792 if (list_empty(&devlink->trap_policer_list))
8795 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8796 if (!policer_item) {
8797 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8801 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8805 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8806 DEVLINK_CMD_TRAP_POLICER_NEW,
8807 info->snd_portid, info->snd_seq, 0);
8809 goto err_trap_policer_fill;
8811 return genlmsg_reply(msg, info);
8813 err_trap_policer_fill:
8818 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
8819 struct netlink_callback *cb)
8821 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
8822 struct devlink_trap_policer_item *policer_item;
8823 u32 portid = NETLINK_CB(cb->skb).portid;
8824 struct devlink *devlink;
8825 int start = cb->args[0];
8826 unsigned long index;
8830 mutex_lock(&devlink_mutex);
8831 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8832 if (!devlink_try_get(devlink))
8835 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8838 mutex_lock(&devlink->lock);
8839 list_for_each_entry(policer_item, &devlink->trap_policer_list,
8845 err = devlink_nl_trap_policer_fill(msg, devlink,
8851 mutex_unlock(&devlink->lock);
8852 devlink_put(devlink);
8857 mutex_unlock(&devlink->lock);
8859 devlink_put(devlink);
8862 mutex_unlock(&devlink_mutex);
8869 devlink_trap_policer_set(struct devlink *devlink,
8870 struct devlink_trap_policer_item *policer_item,
8871 struct genl_info *info)
8873 struct netlink_ext_ack *extack = info->extack;
8874 struct nlattr **attrs = info->attrs;
8878 rate = policer_item->rate;
8879 burst = policer_item->burst;
8881 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
8882 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
8884 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
8885 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
8887 if (rate < policer_item->policer->min_rate) {
8888 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
8892 if (rate > policer_item->policer->max_rate) {
8893 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
8897 if (burst < policer_item->policer->min_burst) {
8898 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
8902 if (burst > policer_item->policer->max_burst) {
8903 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
8907 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
8908 rate, burst, info->extack);
8912 policer_item->rate = rate;
8913 policer_item->burst = burst;
8918 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
8919 struct genl_info *info)
8921 struct devlink_trap_policer_item *policer_item;
8922 struct netlink_ext_ack *extack = info->extack;
8923 struct devlink *devlink = info->user_ptr[0];
8925 if (list_empty(&devlink->trap_policer_list))
8928 if (!devlink->ops->trap_policer_set)
8931 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8932 if (!policer_item) {
8933 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8937 return devlink_trap_policer_set(devlink, policer_item, info);
8940 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
8941 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
8942 DEVLINK_ATTR_TRAP_POLICER_ID },
8943 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
8944 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
8945 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
8946 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
8947 DEVLINK_PORT_TYPE_IB),
8948 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
8949 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
8950 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
8951 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
8952 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
8953 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
8954 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
8955 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
8956 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
8957 DEVLINK_ESWITCH_MODE_SWITCHDEV),
8958 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
8959 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
8960 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
8961 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
8962 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
8963 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
8964 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
8965 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
8966 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
8967 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
8968 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
8969 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
8970 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
8971 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
8972 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
8973 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
8974 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
8975 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
8976 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
8977 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
8978 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
8979 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
8980 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
8981 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
8982 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
8983 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
8984 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
8985 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
8986 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
8987 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
8988 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
8989 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
8990 DEVLINK_RELOAD_ACTION_MAX),
8991 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
8992 [DEVLINK_ATTR_PORT_FLAVOUR] = { .type = NLA_U16 },
8993 [DEVLINK_ATTR_PORT_PCI_PF_NUMBER] = { .type = NLA_U16 },
8994 [DEVLINK_ATTR_PORT_PCI_SF_NUMBER] = { .type = NLA_U32 },
8995 [DEVLINK_ATTR_PORT_CONTROLLER_NUMBER] = { .type = NLA_U32 },
8996 [DEVLINK_ATTR_RATE_TYPE] = { .type = NLA_U16 },
8997 [DEVLINK_ATTR_RATE_TX_SHARE] = { .type = NLA_U64 },
8998 [DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 },
8999 [DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING },
9000 [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING },
9001 [DEVLINK_ATTR_LINECARD_INDEX] = { .type = NLA_U32 },
9002 [DEVLINK_ATTR_LINECARD_TYPE] = { .type = NLA_NUL_STRING },
9005 static const struct genl_small_ops devlink_nl_ops[] = {
9007 .cmd = DEVLINK_CMD_GET,
9008 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9009 .doit = devlink_nl_cmd_get_doit,
9010 .dumpit = devlink_nl_cmd_get_dumpit,
9011 /* can be retrieved by unprivileged users */
9014 .cmd = DEVLINK_CMD_PORT_GET,
9015 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9016 .doit = devlink_nl_cmd_port_get_doit,
9017 .dumpit = devlink_nl_cmd_port_get_dumpit,
9018 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9019 /* can be retrieved by unprivileged users */
9022 .cmd = DEVLINK_CMD_PORT_SET,
9023 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9024 .doit = devlink_nl_cmd_port_set_doit,
9025 .flags = GENL_ADMIN_PERM,
9026 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9029 .cmd = DEVLINK_CMD_RATE_GET,
9030 .doit = devlink_nl_cmd_rate_get_doit,
9031 .dumpit = devlink_nl_cmd_rate_get_dumpit,
9032 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9033 /* can be retrieved by unprivileged users */
9036 .cmd = DEVLINK_CMD_RATE_SET,
9037 .doit = devlink_nl_cmd_rate_set_doit,
9038 .flags = GENL_ADMIN_PERM,
9039 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9042 .cmd = DEVLINK_CMD_RATE_NEW,
9043 .doit = devlink_nl_cmd_rate_new_doit,
9044 .flags = GENL_ADMIN_PERM,
9047 .cmd = DEVLINK_CMD_RATE_DEL,
9048 .doit = devlink_nl_cmd_rate_del_doit,
9049 .flags = GENL_ADMIN_PERM,
9050 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9053 .cmd = DEVLINK_CMD_PORT_SPLIT,
9054 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9055 .doit = devlink_nl_cmd_port_split_doit,
9056 .flags = GENL_ADMIN_PERM,
9057 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9060 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9061 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9062 .doit = devlink_nl_cmd_port_unsplit_doit,
9063 .flags = GENL_ADMIN_PERM,
9064 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9067 .cmd = DEVLINK_CMD_PORT_NEW,
9068 .doit = devlink_nl_cmd_port_new_doit,
9069 .flags = GENL_ADMIN_PERM,
9070 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
9073 .cmd = DEVLINK_CMD_PORT_DEL,
9074 .doit = devlink_nl_cmd_port_del_doit,
9075 .flags = GENL_ADMIN_PERM,
9076 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
9079 .cmd = DEVLINK_CMD_LINECARD_GET,
9080 .doit = devlink_nl_cmd_linecard_get_doit,
9081 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9082 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9083 /* can be retrieved by unprivileged users */
9086 .cmd = DEVLINK_CMD_LINECARD_SET,
9087 .doit = devlink_nl_cmd_linecard_set_doit,
9088 .flags = GENL_ADMIN_PERM,
9089 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9092 .cmd = DEVLINK_CMD_SB_GET,
9093 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9094 .doit = devlink_nl_cmd_sb_get_doit,
9095 .dumpit = devlink_nl_cmd_sb_get_dumpit,
9096 /* can be retrieved by unprivileged users */
9099 .cmd = DEVLINK_CMD_SB_POOL_GET,
9100 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9101 .doit = devlink_nl_cmd_sb_pool_get_doit,
9102 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
9103 /* can be retrieved by unprivileged users */
9106 .cmd = DEVLINK_CMD_SB_POOL_SET,
9107 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9108 .doit = devlink_nl_cmd_sb_pool_set_doit,
9109 .flags = GENL_ADMIN_PERM,
9112 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9113 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9114 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9115 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
9116 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9117 /* can be retrieved by unprivileged users */
9120 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9121 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9122 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9123 .flags = GENL_ADMIN_PERM,
9124 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9127 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9128 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9129 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9130 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
9131 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9132 /* can be retrieved by unprivileged users */
9135 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9136 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9137 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9138 .flags = GENL_ADMIN_PERM,
9139 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9142 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9143 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9144 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9145 .flags = GENL_ADMIN_PERM,
9148 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9149 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9150 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9151 .flags = GENL_ADMIN_PERM,
9154 .cmd = DEVLINK_CMD_ESWITCH_GET,
9155 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9156 .doit = devlink_nl_cmd_eswitch_get_doit,
9157 .flags = GENL_ADMIN_PERM,
9160 .cmd = DEVLINK_CMD_ESWITCH_SET,
9161 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9162 .doit = devlink_nl_cmd_eswitch_set_doit,
9163 .flags = GENL_ADMIN_PERM,
9166 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9167 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9168 .doit = devlink_nl_cmd_dpipe_table_get,
9169 /* can be retrieved by unprivileged users */
9172 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9173 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9174 .doit = devlink_nl_cmd_dpipe_entries_get,
9175 /* can be retrieved by unprivileged users */
9178 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9179 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9180 .doit = devlink_nl_cmd_dpipe_headers_get,
9181 /* can be retrieved by unprivileged users */
9184 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9185 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9186 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9187 .flags = GENL_ADMIN_PERM,
9190 .cmd = DEVLINK_CMD_RESOURCE_SET,
9191 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9192 .doit = devlink_nl_cmd_resource_set,
9193 .flags = GENL_ADMIN_PERM,
9196 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9197 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9198 .doit = devlink_nl_cmd_resource_dump,
9199 /* can be retrieved by unprivileged users */
9202 .cmd = DEVLINK_CMD_RELOAD,
9203 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9204 .doit = devlink_nl_cmd_reload,
9205 .flags = GENL_ADMIN_PERM,
9206 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
9209 .cmd = DEVLINK_CMD_PARAM_GET,
9210 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9211 .doit = devlink_nl_cmd_param_get_doit,
9212 .dumpit = devlink_nl_cmd_param_get_dumpit,
9213 /* can be retrieved by unprivileged users */
9216 .cmd = DEVLINK_CMD_PARAM_SET,
9217 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9218 .doit = devlink_nl_cmd_param_set_doit,
9219 .flags = GENL_ADMIN_PERM,
9222 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9223 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9224 .doit = devlink_nl_cmd_port_param_get_doit,
9225 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9226 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9227 /* can be retrieved by unprivileged users */
9230 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9231 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9232 .doit = devlink_nl_cmd_port_param_set_doit,
9233 .flags = GENL_ADMIN_PERM,
9234 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9237 .cmd = DEVLINK_CMD_REGION_GET,
9238 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9239 .doit = devlink_nl_cmd_region_get_doit,
9240 .dumpit = devlink_nl_cmd_region_get_dumpit,
9241 .flags = GENL_ADMIN_PERM,
9244 .cmd = DEVLINK_CMD_REGION_NEW,
9245 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9246 .doit = devlink_nl_cmd_region_new,
9247 .flags = GENL_ADMIN_PERM,
9250 .cmd = DEVLINK_CMD_REGION_DEL,
9251 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9252 .doit = devlink_nl_cmd_region_del,
9253 .flags = GENL_ADMIN_PERM,
9256 .cmd = DEVLINK_CMD_REGION_READ,
9257 .validate = GENL_DONT_VALIDATE_STRICT |
9258 GENL_DONT_VALIDATE_DUMP_STRICT,
9259 .dumpit = devlink_nl_cmd_region_read_dumpit,
9260 .flags = GENL_ADMIN_PERM,
9263 .cmd = DEVLINK_CMD_INFO_GET,
9264 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9265 .doit = devlink_nl_cmd_info_get_doit,
9266 .dumpit = devlink_nl_cmd_info_get_dumpit,
9267 /* can be retrieved by unprivileged users */
9270 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9271 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9272 .doit = devlink_nl_cmd_health_reporter_get_doit,
9273 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9274 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9275 DEVLINK_NL_FLAG_NO_LOCK,
9276 /* can be retrieved by unprivileged users */
9279 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9280 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9281 .doit = devlink_nl_cmd_health_reporter_set_doit,
9282 .flags = GENL_ADMIN_PERM,
9283 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9284 DEVLINK_NL_FLAG_NO_LOCK,
9287 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9288 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9289 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9290 .flags = GENL_ADMIN_PERM,
9291 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9292 DEVLINK_NL_FLAG_NO_LOCK,
9295 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9296 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9297 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9298 .flags = GENL_ADMIN_PERM,
9299 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9300 DEVLINK_NL_FLAG_NO_LOCK,
9303 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9304 .validate = GENL_DONT_VALIDATE_STRICT |
9305 GENL_DONT_VALIDATE_DUMP_STRICT,
9306 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9307 .flags = GENL_ADMIN_PERM,
9310 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9311 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9312 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9313 .flags = GENL_ADMIN_PERM,
9314 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9315 DEVLINK_NL_FLAG_NO_LOCK,
9318 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9319 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9320 .doit = devlink_nl_cmd_health_reporter_test_doit,
9321 .flags = GENL_ADMIN_PERM,
9322 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9323 DEVLINK_NL_FLAG_NO_LOCK,
9326 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9327 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9328 .doit = devlink_nl_cmd_flash_update,
9329 .flags = GENL_ADMIN_PERM,
9332 .cmd = DEVLINK_CMD_TRAP_GET,
9333 .doit = devlink_nl_cmd_trap_get_doit,
9334 .dumpit = devlink_nl_cmd_trap_get_dumpit,
9335 /* can be retrieved by unprivileged users */
9338 .cmd = DEVLINK_CMD_TRAP_SET,
9339 .doit = devlink_nl_cmd_trap_set_doit,
9340 .flags = GENL_ADMIN_PERM,
9343 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9344 .doit = devlink_nl_cmd_trap_group_get_doit,
9345 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
9346 /* can be retrieved by unprivileged users */
9349 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9350 .doit = devlink_nl_cmd_trap_group_set_doit,
9351 .flags = GENL_ADMIN_PERM,
9354 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9355 .doit = devlink_nl_cmd_trap_policer_get_doit,
9356 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
9357 /* can be retrieved by unprivileged users */
9360 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9361 .doit = devlink_nl_cmd_trap_policer_set_doit,
9362 .flags = GENL_ADMIN_PERM,
9366 static struct genl_family devlink_nl_family __ro_after_init = {
9367 .name = DEVLINK_GENL_NAME,
9368 .version = DEVLINK_GENL_VERSION,
9369 .maxattr = DEVLINK_ATTR_MAX,
9370 .policy = devlink_nl_policy,
9372 .pre_doit = devlink_nl_pre_doit,
9373 .post_doit = devlink_nl_post_doit,
9374 .module = THIS_MODULE,
9375 .small_ops = devlink_nl_ops,
9376 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
9377 .mcgrps = devlink_nl_mcgrps,
9378 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
9381 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9383 const struct devlink_reload_combination *comb;
9386 if (!devlink_reload_supported(ops)) {
9387 if (WARN_ON(ops->reload_actions))
9392 if (WARN_ON(!ops->reload_actions ||
9393 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9394 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9397 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9398 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9401 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
9402 comb = &devlink_reload_invalid_combinations[i];
9403 if (ops->reload_actions == BIT(comb->action) &&
9404 ops->reload_limits == BIT(comb->limit))
9411 * devlink_set_features - Set devlink supported features
9414 * @features: devlink support features
9416 * This interface allows us to set reload ops separatelly from
9417 * the devlink_alloc.
9419 void devlink_set_features(struct devlink *devlink, u64 features)
9421 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9423 WARN_ON(features & DEVLINK_F_RELOAD &&
9424 !devlink_reload_supported(devlink->ops));
9425 devlink->features = features;
9427 EXPORT_SYMBOL_GPL(devlink_set_features);
9430 * devlink_alloc_ns - Allocate new devlink instance resources
9431 * in specific namespace
9434 * @priv_size: size of user private data
9435 * @net: net namespace
9436 * @dev: parent device
9438 * Allocate new devlink instance resources, including devlink index
9441 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
9442 size_t priv_size, struct net *net,
9445 struct devlink *devlink;
9449 WARN_ON(!ops || !dev);
9450 if (!devlink_reload_actions_valid(ops))
9453 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
9457 ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
9458 &last_id, GFP_KERNEL);
9466 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
9467 write_pnet(&devlink->_net, net);
9468 INIT_LIST_HEAD(&devlink->port_list);
9469 INIT_LIST_HEAD(&devlink->rate_list);
9470 INIT_LIST_HEAD(&devlink->linecard_list);
9471 INIT_LIST_HEAD(&devlink->sb_list);
9472 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
9473 INIT_LIST_HEAD(&devlink->resource_list);
9474 INIT_LIST_HEAD(&devlink->param_list);
9475 INIT_LIST_HEAD(&devlink->region_list);
9476 INIT_LIST_HEAD(&devlink->reporter_list);
9477 INIT_LIST_HEAD(&devlink->trap_list);
9478 INIT_LIST_HEAD(&devlink->trap_group_list);
9479 INIT_LIST_HEAD(&devlink->trap_policer_list);
9480 mutex_init(&devlink->lock);
9481 mutex_init(&devlink->reporters_lock);
9482 mutex_init(&devlink->linecards_lock);
9483 refcount_set(&devlink->refcount, 1);
9484 init_completion(&devlink->comp);
9488 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
9491 devlink_trap_policer_notify(struct devlink *devlink,
9492 const struct devlink_trap_policer_item *policer_item,
9493 enum devlink_command cmd);
9495 devlink_trap_group_notify(struct devlink *devlink,
9496 const struct devlink_trap_group_item *group_item,
9497 enum devlink_command cmd);
9498 static void devlink_trap_notify(struct devlink *devlink,
9499 const struct devlink_trap_item *trap_item,
9500 enum devlink_command cmd);
9502 static void devlink_notify_register(struct devlink *devlink)
9504 struct devlink_trap_policer_item *policer_item;
9505 struct devlink_trap_group_item *group_item;
9506 struct devlink_param_item *param_item;
9507 struct devlink_trap_item *trap_item;
9508 struct devlink_port *devlink_port;
9509 struct devlink_linecard *linecard;
9510 struct devlink_rate *rate_node;
9511 struct devlink_region *region;
9513 devlink_notify(devlink, DEVLINK_CMD_NEW);
9514 list_for_each_entry(linecard, &devlink->linecard_list, list)
9515 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9517 list_for_each_entry(devlink_port, &devlink->port_list, list)
9518 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9520 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9521 devlink_trap_policer_notify(devlink, policer_item,
9522 DEVLINK_CMD_TRAP_POLICER_NEW);
9524 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9525 devlink_trap_group_notify(devlink, group_item,
9526 DEVLINK_CMD_TRAP_GROUP_NEW);
9528 list_for_each_entry(trap_item, &devlink->trap_list, list)
9529 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9531 list_for_each_entry(rate_node, &devlink->rate_list, list)
9532 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9534 list_for_each_entry(region, &devlink->region_list, list)
9535 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9537 list_for_each_entry(param_item, &devlink->param_list, list)
9538 devlink_param_notify(devlink, 0, param_item,
9539 DEVLINK_CMD_PARAM_NEW);
9542 static void devlink_notify_unregister(struct devlink *devlink)
9544 struct devlink_trap_policer_item *policer_item;
9545 struct devlink_trap_group_item *group_item;
9546 struct devlink_param_item *param_item;
9547 struct devlink_trap_item *trap_item;
9548 struct devlink_port *devlink_port;
9549 struct devlink_rate *rate_node;
9550 struct devlink_region *region;
9552 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9553 devlink_param_notify(devlink, 0, param_item,
9554 DEVLINK_CMD_PARAM_DEL);
9556 list_for_each_entry_reverse(region, &devlink->region_list, list)
9557 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9559 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9560 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9562 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9563 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9565 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9566 devlink_trap_group_notify(devlink, group_item,
9567 DEVLINK_CMD_TRAP_GROUP_DEL);
9568 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9570 devlink_trap_policer_notify(devlink, policer_item,
9571 DEVLINK_CMD_TRAP_POLICER_DEL);
9573 list_for_each_entry_reverse(devlink_port, &devlink->port_list, list)
9574 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9575 devlink_notify(devlink, DEVLINK_CMD_DEL);
9579 * devlink_register - Register devlink instance
9583 void devlink_register(struct devlink *devlink)
9585 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9586 /* Make sure that we are in .probe() routine */
9588 mutex_lock(&devlink_mutex);
9589 xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9590 devlink_notify_register(devlink);
9591 mutex_unlock(&devlink_mutex);
9593 EXPORT_SYMBOL_GPL(devlink_register);
9596 * devlink_unregister - Unregister devlink instance
9600 void devlink_unregister(struct devlink *devlink)
9602 ASSERT_DEVLINK_REGISTERED(devlink);
9603 /* Make sure that we are in .remove() routine */
9605 devlink_put(devlink);
9606 wait_for_completion(&devlink->comp);
9608 mutex_lock(&devlink_mutex);
9609 devlink_notify_unregister(devlink);
9610 xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9611 mutex_unlock(&devlink_mutex);
9613 EXPORT_SYMBOL_GPL(devlink_unregister);
9616 * devlink_free - Free devlink instance resources
9620 void devlink_free(struct devlink *devlink)
9622 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9624 mutex_destroy(&devlink->linecards_lock);
9625 mutex_destroy(&devlink->reporters_lock);
9626 mutex_destroy(&devlink->lock);
9627 WARN_ON(!list_empty(&devlink->trap_policer_list));
9628 WARN_ON(!list_empty(&devlink->trap_group_list));
9629 WARN_ON(!list_empty(&devlink->trap_list));
9630 WARN_ON(!list_empty(&devlink->reporter_list));
9631 WARN_ON(!list_empty(&devlink->region_list));
9632 WARN_ON(!list_empty(&devlink->param_list));
9633 WARN_ON(!list_empty(&devlink->resource_list));
9634 WARN_ON(!list_empty(&devlink->dpipe_table_list));
9635 WARN_ON(!list_empty(&devlink->sb_list));
9636 WARN_ON(!list_empty(&devlink->rate_list));
9637 WARN_ON(!list_empty(&devlink->linecard_list));
9638 WARN_ON(!list_empty(&devlink->port_list));
9640 xa_destroy(&devlink->snapshot_ids);
9641 xa_erase(&devlinks, devlink->index);
9645 EXPORT_SYMBOL_GPL(devlink_free);
9647 static void devlink_port_type_warn(struct work_struct *work)
9649 WARN(true, "Type was not set for devlink port.");
9652 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9654 /* Ignore CPU and DSA flavours. */
9655 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9656 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9657 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9660 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9662 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9664 if (!devlink_port_type_should_warn(devlink_port))
9666 /* Schedule a work to WARN in case driver does not set port
9667 * type within timeout.
9669 schedule_delayed_work(&devlink_port->type_warn_dw,
9670 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9673 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9675 if (!devlink_port_type_should_warn(devlink_port))
9677 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9680 int devl_port_register(struct devlink *devlink,
9681 struct devlink_port *devlink_port,
9682 unsigned int port_index)
9684 lockdep_assert_held(&devlink->lock);
9686 if (devlink_port_index_exists(devlink, port_index))
9689 WARN_ON(devlink_port->devlink);
9690 devlink_port->devlink = devlink;
9691 devlink_port->index = port_index;
9692 spin_lock_init(&devlink_port->type_lock);
9693 INIT_LIST_HEAD(&devlink_port->reporter_list);
9694 mutex_init(&devlink_port->reporters_lock);
9695 list_add_tail(&devlink_port->list, &devlink->port_list);
9696 INIT_LIST_HEAD(&devlink_port->param_list);
9697 INIT_LIST_HEAD(&devlink_port->region_list);
9699 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9700 devlink_port_type_warn_schedule(devlink_port);
9701 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9704 EXPORT_SYMBOL_GPL(devl_port_register);
9707 * devlink_port_register - Register devlink port
9710 * @devlink_port: devlink port
9711 * @port_index: driver-specific numerical identifier of the port
9713 * Register devlink port with provided port index. User can use
9714 * any indexing, even hw-related one. devlink_port structure
9715 * is convenient to be embedded inside user driver private structure.
9716 * Note that the caller should take care of zeroing the devlink_port
9719 int devlink_port_register(struct devlink *devlink,
9720 struct devlink_port *devlink_port,
9721 unsigned int port_index)
9725 mutex_lock(&devlink->lock);
9726 err = devl_port_register(devlink, devlink_port, port_index);
9727 mutex_unlock(&devlink->lock);
9730 EXPORT_SYMBOL_GPL(devlink_port_register);
9732 void devl_port_unregister(struct devlink_port *devlink_port)
9734 lockdep_assert_held(&devlink_port->devlink->lock);
9736 devlink_port_type_warn_cancel(devlink_port);
9737 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9738 list_del(&devlink_port->list);
9739 WARN_ON(!list_empty(&devlink_port->reporter_list));
9740 WARN_ON(!list_empty(&devlink_port->region_list));
9741 mutex_destroy(&devlink_port->reporters_lock);
9743 EXPORT_SYMBOL_GPL(devl_port_unregister);
9746 * devlink_port_unregister - Unregister devlink port
9748 * @devlink_port: devlink port
9750 void devlink_port_unregister(struct devlink_port *devlink_port)
9752 struct devlink *devlink = devlink_port->devlink;
9754 mutex_lock(&devlink->lock);
9755 devl_port_unregister(devlink_port);
9756 mutex_unlock(&devlink->lock);
9758 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9760 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9761 enum devlink_port_type type,
9764 if (WARN_ON(!devlink_port->devlink))
9766 devlink_port_type_warn_cancel(devlink_port);
9767 spin_lock_bh(&devlink_port->type_lock);
9768 devlink_port->type = type;
9769 devlink_port->type_dev = type_dev;
9770 spin_unlock_bh(&devlink_port->type_lock);
9771 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9774 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9775 struct net_device *netdev)
9777 const struct net_device_ops *ops = netdev->netdev_ops;
9779 /* If driver registers devlink port, it should set devlink port
9780 * attributes accordingly so the compat functions are called
9781 * and the original ops are not used.
9783 if (ops->ndo_get_phys_port_name) {
9784 /* Some drivers use the same set of ndos for netdevs
9785 * that have devlink_port registered and also for
9786 * those who don't. Make sure that ndo_get_phys_port_name
9787 * returns -EOPNOTSUPP here in case it is defined.
9790 char name[IFNAMSIZ];
9793 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9794 WARN_ON(err != -EOPNOTSUPP);
9796 if (ops->ndo_get_port_parent_id) {
9797 /* Some drivers use the same set of ndos for netdevs
9798 * that have devlink_port registered and also for
9799 * those who don't. Make sure that ndo_get_port_parent_id
9800 * returns -EOPNOTSUPP here in case it is defined.
9803 struct netdev_phys_item_id ppid;
9806 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9807 WARN_ON(err != -EOPNOTSUPP);
9812 * devlink_port_type_eth_set - Set port type to Ethernet
9814 * @devlink_port: devlink port
9815 * @netdev: related netdevice
9817 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
9818 struct net_device *netdev)
9821 devlink_port_type_netdev_checks(devlink_port, netdev);
9823 dev_warn(devlink_port->devlink->dev,
9824 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9825 devlink_port->index);
9827 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
9829 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9832 * devlink_port_type_ib_set - Set port type to InfiniBand
9834 * @devlink_port: devlink port
9835 * @ibdev: related IB device
9837 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9838 struct ib_device *ibdev)
9840 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9842 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9845 * devlink_port_type_clear - Clear port type
9847 * @devlink_port: devlink port
9849 void devlink_port_type_clear(struct devlink_port *devlink_port)
9851 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9852 devlink_port_type_warn_schedule(devlink_port);
9854 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9856 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9857 enum devlink_port_flavour flavour)
9859 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9861 devlink_port->attrs_set = true;
9862 attrs->flavour = flavour;
9863 if (attrs->switch_id.id_len) {
9864 devlink_port->switch_port = true;
9865 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9866 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9868 devlink_port->switch_port = false;
9874 * devlink_port_attrs_set - Set port attributes
9876 * @devlink_port: devlink port
9877 * @attrs: devlink port attrs
9879 void devlink_port_attrs_set(struct devlink_port *devlink_port,
9880 struct devlink_port_attrs *attrs)
9884 if (WARN_ON(devlink_port->devlink))
9886 devlink_port->attrs = *attrs;
9887 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
9890 WARN_ON(attrs->splittable && attrs->split);
9892 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
9895 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
9897 * @devlink_port: devlink port
9898 * @controller: associated controller number for the devlink port instance
9899 * @pf: associated PF for the devlink port instance
9900 * @external: indicates if the port is for an external controller
9902 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
9903 u16 pf, bool external)
9905 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9908 if (WARN_ON(devlink_port->devlink))
9910 ret = __devlink_port_attrs_set(devlink_port,
9911 DEVLINK_PORT_FLAVOUR_PCI_PF);
9914 attrs->pci_pf.controller = controller;
9915 attrs->pci_pf.pf = pf;
9916 attrs->pci_pf.external = external;
9918 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
9921 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
9923 * @devlink_port: devlink port
9924 * @controller: associated controller number for the devlink port instance
9925 * @pf: associated PF for the devlink port instance
9926 * @vf: associated VF of a PF for the devlink port instance
9927 * @external: indicates if the port is for an external controller
9929 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
9930 u16 pf, u16 vf, bool external)
9932 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9935 if (WARN_ON(devlink_port->devlink))
9937 ret = __devlink_port_attrs_set(devlink_port,
9938 DEVLINK_PORT_FLAVOUR_PCI_VF);
9941 attrs->pci_vf.controller = controller;
9942 attrs->pci_vf.pf = pf;
9943 attrs->pci_vf.vf = vf;
9944 attrs->pci_vf.external = external;
9946 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
9949 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
9951 * @devlink_port: devlink port
9952 * @controller: associated controller number for the devlink port instance
9953 * @pf: associated PF for the devlink port instance
9954 * @sf: associated SF of a PF for the devlink port instance
9955 * @external: indicates if the port is for an external controller
9957 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
9958 u16 pf, u32 sf, bool external)
9960 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9963 if (WARN_ON(devlink_port->devlink))
9965 ret = __devlink_port_attrs_set(devlink_port,
9966 DEVLINK_PORT_FLAVOUR_PCI_SF);
9969 attrs->pci_sf.controller = controller;
9970 attrs->pci_sf.pf = pf;
9971 attrs->pci_sf.sf = sf;
9972 attrs->pci_sf.external = external;
9974 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
9977 * devl_rate_leaf_create - create devlink rate leaf
9978 * @devlink_port: devlink port object to create rate object on
9979 * @priv: driver private data
9981 * Create devlink rate object of type leaf on provided @devlink_port.
9983 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
9985 struct devlink *devlink = devlink_port->devlink;
9986 struct devlink_rate *devlink_rate;
9988 devl_assert_locked(devlink_port->devlink);
9990 if (WARN_ON(devlink_port->devlink_rate))
9993 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
9997 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
9998 devlink_rate->devlink = devlink;
9999 devlink_rate->devlink_port = devlink_port;
10000 devlink_rate->priv = priv;
10001 list_add_tail(&devlink_rate->list, &devlink->rate_list);
10002 devlink_port->devlink_rate = devlink_rate;
10003 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10007 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10010 devlink_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
10012 struct devlink *devlink = devlink_port->devlink;
10015 mutex_lock(&devlink->lock);
10016 ret = devl_rate_leaf_create(devlink_port, priv);
10017 mutex_unlock(&devlink->lock);
10021 EXPORT_SYMBOL_GPL(devlink_rate_leaf_create);
10023 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10025 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10027 devl_assert_locked(devlink_port->devlink);
10031 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10032 if (devlink_rate->parent)
10033 refcount_dec(&devlink_rate->parent->refcnt);
10034 list_del(&devlink_rate->list);
10035 devlink_port->devlink_rate = NULL;
10036 kfree(devlink_rate);
10038 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10041 * devlink_rate_leaf_destroy - destroy devlink rate leaf
10043 * @devlink_port: devlink port linked to the rate object
10045 * Context: Takes and release devlink->lock <mutex>.
10047 void devlink_rate_leaf_destroy(struct devlink_port *devlink_port)
10049 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10050 struct devlink *devlink = devlink_port->devlink;
10055 mutex_lock(&devlink->lock);
10056 devl_rate_leaf_destroy(devlink_port);
10057 mutex_unlock(&devlink->lock);
10059 EXPORT_SYMBOL_GPL(devlink_rate_leaf_destroy);
10062 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10063 * @devlink: devlink instance
10065 * Unset parent for all rate objects and destroy all rate nodes
10066 * on specified device.
10068 void devl_rate_nodes_destroy(struct devlink *devlink)
10070 static struct devlink_rate *devlink_rate, *tmp;
10071 const struct devlink_ops *ops = devlink->ops;
10073 devl_assert_locked(devlink);
10075 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10076 if (!devlink_rate->parent)
10079 refcount_dec(&devlink_rate->parent->refcnt);
10080 if (devlink_rate_is_leaf(devlink_rate))
10081 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10083 else if (devlink_rate_is_node(devlink_rate))
10084 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10087 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10088 if (devlink_rate_is_node(devlink_rate)) {
10089 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10090 list_del(&devlink_rate->list);
10091 kfree(devlink_rate->name);
10092 kfree(devlink_rate);
10096 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10099 * devlink_rate_nodes_destroy - destroy all devlink rate nodes on device
10101 * @devlink: devlink instance
10103 * Unset parent for all rate objects and destroy all rate nodes
10104 * on specified device.
10106 * Context: Takes and release devlink->lock <mutex>.
10108 void devlink_rate_nodes_destroy(struct devlink *devlink)
10110 mutex_lock(&devlink->lock);
10111 devl_rate_nodes_destroy(devlink);
10112 mutex_unlock(&devlink->lock);
10114 EXPORT_SYMBOL_GPL(devlink_rate_nodes_destroy);
10117 * devlink_port_linecard_set - Link port with a linecard
10119 * @devlink_port: devlink port
10120 * @linecard: devlink linecard
10122 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10123 struct devlink_linecard *linecard)
10125 if (WARN_ON(devlink_port->devlink))
10127 devlink_port->linecard = linecard;
10129 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10131 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10132 char *name, size_t len)
10134 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10137 if (!devlink_port->attrs_set)
10138 return -EOPNOTSUPP;
10140 switch (attrs->flavour) {
10141 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10142 if (devlink_port->linecard)
10143 n = snprintf(name, len, "l%u",
10144 devlink_port->linecard->index);
10146 n += snprintf(name + n, len - n, "p%u",
10147 attrs->phys.port_number);
10148 if (n < len && attrs->split)
10149 n += snprintf(name + n, len - n, "s%u",
10150 attrs->phys.split_subport_number);
10152 case DEVLINK_PORT_FLAVOUR_CPU:
10153 case DEVLINK_PORT_FLAVOUR_DSA:
10154 case DEVLINK_PORT_FLAVOUR_UNUSED:
10155 /* As CPU and DSA ports do not have a netdevice associated
10156 * case should not ever happen.
10160 case DEVLINK_PORT_FLAVOUR_PCI_PF:
10161 if (attrs->pci_pf.external) {
10162 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10168 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10170 case DEVLINK_PORT_FLAVOUR_PCI_VF:
10171 if (attrs->pci_vf.external) {
10172 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10178 n = snprintf(name, len, "pf%uvf%u",
10179 attrs->pci_vf.pf, attrs->pci_vf.vf);
10181 case DEVLINK_PORT_FLAVOUR_PCI_SF:
10182 if (attrs->pci_sf.external) {
10183 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10189 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10192 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10193 return -EOPNOTSUPP;
10202 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10204 struct devlink_linecard_type *linecard_type;
10205 unsigned int count;
10208 count = linecard->ops->types_count(linecard, linecard->priv);
10209 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10211 if (!linecard->types)
10213 linecard->types_count = count;
10215 for (i = 0; i < count; i++) {
10216 linecard_type = &linecard->types[i];
10217 linecard->ops->types_get(linecard, linecard->priv, i,
10218 &linecard_type->type,
10219 &linecard_type->priv);
10224 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10226 kfree(linecard->types);
10230 * devlink_linecard_create - Create devlink linecard
10232 * @devlink: devlink
10233 * @linecard_index: driver-specific numerical identifier of the linecard
10234 * @ops: linecards ops
10235 * @priv: user priv pointer
10237 * Create devlink linecard instance with provided linecard index.
10238 * Caller can use any indexing, even hw-related one.
10240 * Return: Line card structure or an ERR_PTR() encoded error code.
10242 struct devlink_linecard *
10243 devlink_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10244 const struct devlink_linecard_ops *ops, void *priv)
10246 struct devlink_linecard *linecard;
10249 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10250 !ops->types_count || !ops->types_get))
10251 return ERR_PTR(-EINVAL);
10253 mutex_lock(&devlink->linecards_lock);
10254 if (devlink_linecard_index_exists(devlink, linecard_index)) {
10255 mutex_unlock(&devlink->linecards_lock);
10256 return ERR_PTR(-EEXIST);
10259 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10261 mutex_unlock(&devlink->linecards_lock);
10262 return ERR_PTR(-ENOMEM);
10265 linecard->devlink = devlink;
10266 linecard->index = linecard_index;
10267 linecard->ops = ops;
10268 linecard->priv = priv;
10269 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10270 mutex_init(&linecard->state_lock);
10272 err = devlink_linecard_types_init(linecard);
10274 mutex_destroy(&linecard->state_lock);
10276 mutex_unlock(&devlink->linecards_lock);
10277 return ERR_PTR(err);
10280 list_add_tail(&linecard->list, &devlink->linecard_list);
10281 refcount_set(&linecard->refcount, 1);
10282 mutex_unlock(&devlink->linecards_lock);
10283 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10286 EXPORT_SYMBOL_GPL(devlink_linecard_create);
10289 * devlink_linecard_destroy - Destroy devlink linecard
10291 * @linecard: devlink linecard
10293 void devlink_linecard_destroy(struct devlink_linecard *linecard)
10295 struct devlink *devlink = linecard->devlink;
10297 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10298 mutex_lock(&devlink->linecards_lock);
10299 list_del(&linecard->list);
10300 devlink_linecard_types_fini(linecard);
10301 mutex_unlock(&devlink->linecards_lock);
10302 devlink_linecard_put(linecard);
10304 EXPORT_SYMBOL_GPL(devlink_linecard_destroy);
10307 * devlink_linecard_provision_set - Set provisioning on linecard
10309 * @linecard: devlink linecard
10310 * @type: linecard type
10312 * This is either called directly from the provision() op call or
10313 * as a result of the provision() op call asynchronously.
10315 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10318 mutex_lock(&linecard->state_lock);
10319 WARN_ON(linecard->type && strcmp(linecard->type, type));
10320 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10321 linecard->type = type;
10322 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10323 mutex_unlock(&linecard->state_lock);
10325 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10328 * devlink_linecard_provision_clear - Clear provisioning on linecard
10330 * @linecard: devlink linecard
10332 * This is either called directly from the unprovision() op call or
10333 * as a result of the unprovision() op call asynchronously.
10335 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10337 mutex_lock(&linecard->state_lock);
10338 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10339 linecard->type = NULL;
10340 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10341 mutex_unlock(&linecard->state_lock);
10343 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10346 * devlink_linecard_provision_fail - Fail provisioning on linecard
10348 * @linecard: devlink linecard
10350 * This is either called directly from the provision() op call or
10351 * as a result of the provision() op call asynchronously.
10353 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10355 mutex_lock(&linecard->state_lock);
10356 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10357 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10358 mutex_unlock(&linecard->state_lock);
10360 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10363 * devlink_linecard_activate - Set linecard active
10365 * @linecard: devlink linecard
10367 void devlink_linecard_activate(struct devlink_linecard *linecard)
10369 mutex_lock(&linecard->state_lock);
10370 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10371 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10372 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10373 mutex_unlock(&linecard->state_lock);
10375 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10378 * devlink_linecard_deactivate - Set linecard inactive
10380 * @linecard: devlink linecard
10382 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10384 mutex_lock(&linecard->state_lock);
10385 switch (linecard->state) {
10386 case DEVLINK_LINECARD_STATE_ACTIVE:
10387 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10388 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10390 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10391 /* Line card is being deactivated as part
10392 * of unprovisioning flow.
10399 mutex_unlock(&linecard->state_lock);
10401 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10403 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10404 u32 size, u16 ingress_pools_count,
10405 u16 egress_pools_count, u16 ingress_tc_count,
10406 u16 egress_tc_count)
10408 struct devlink_sb *devlink_sb;
10411 mutex_lock(&devlink->lock);
10412 if (devlink_sb_index_exists(devlink, sb_index)) {
10417 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10422 devlink_sb->index = sb_index;
10423 devlink_sb->size = size;
10424 devlink_sb->ingress_pools_count = ingress_pools_count;
10425 devlink_sb->egress_pools_count = egress_pools_count;
10426 devlink_sb->ingress_tc_count = ingress_tc_count;
10427 devlink_sb->egress_tc_count = egress_tc_count;
10428 list_add_tail(&devlink_sb->list, &devlink->sb_list);
10430 mutex_unlock(&devlink->lock);
10433 EXPORT_SYMBOL_GPL(devlink_sb_register);
10435 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10437 struct devlink_sb *devlink_sb;
10439 mutex_lock(&devlink->lock);
10440 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10441 WARN_ON(!devlink_sb);
10442 list_del(&devlink_sb->list);
10443 mutex_unlock(&devlink->lock);
10446 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10449 * devlink_dpipe_headers_register - register dpipe headers
10451 * @devlink: devlink
10452 * @dpipe_headers: dpipe header array
10454 * Register the headers supported by hardware.
10456 int devlink_dpipe_headers_register(struct devlink *devlink,
10457 struct devlink_dpipe_headers *dpipe_headers)
10459 mutex_lock(&devlink->lock);
10460 devlink->dpipe_headers = dpipe_headers;
10461 mutex_unlock(&devlink->lock);
10464 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
10467 * devlink_dpipe_headers_unregister - unregister dpipe headers
10469 * @devlink: devlink
10471 * Unregister the headers supported by hardware.
10473 void devlink_dpipe_headers_unregister(struct devlink *devlink)
10475 mutex_lock(&devlink->lock);
10476 devlink->dpipe_headers = NULL;
10477 mutex_unlock(&devlink->lock);
10479 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
10482 * devlink_dpipe_table_counter_enabled - check if counter allocation
10484 * @devlink: devlink
10485 * @table_name: tables name
10487 * Used by driver to check if counter allocation is required.
10488 * After counter allocation is turned on the table entries
10489 * are updated to include counter statistics.
10491 * After that point on the driver must respect the counter
10492 * state so that each entry added to the table is added
10495 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10496 const char *table_name)
10498 struct devlink_dpipe_table *table;
10502 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10503 table_name, devlink);
10506 enabled = table->counters_enabled;
10510 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10513 * devlink_dpipe_table_register - register dpipe table
10515 * @devlink: devlink
10516 * @table_name: table name
10517 * @table_ops: table ops
10519 * @counter_control_extern: external control for counters
10521 int devlink_dpipe_table_register(struct devlink *devlink,
10522 const char *table_name,
10523 struct devlink_dpipe_table_ops *table_ops,
10524 void *priv, bool counter_control_extern)
10526 struct devlink_dpipe_table *table;
10529 if (WARN_ON(!table_ops->size_get))
10532 mutex_lock(&devlink->lock);
10534 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10540 table = kzalloc(sizeof(*table), GFP_KERNEL);
10546 table->name = table_name;
10547 table->table_ops = table_ops;
10548 table->priv = priv;
10549 table->counter_control_extern = counter_control_extern;
10551 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10553 mutex_unlock(&devlink->lock);
10556 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
10559 * devlink_dpipe_table_unregister - unregister dpipe table
10561 * @devlink: devlink
10562 * @table_name: table name
10564 void devlink_dpipe_table_unregister(struct devlink *devlink,
10565 const char *table_name)
10567 struct devlink_dpipe_table *table;
10569 mutex_lock(&devlink->lock);
10570 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10571 table_name, devlink);
10574 list_del_rcu(&table->list);
10575 mutex_unlock(&devlink->lock);
10576 kfree_rcu(table, rcu);
10579 mutex_unlock(&devlink->lock);
10581 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
10584 * devlink_resource_register - devlink resource register
10586 * @devlink: devlink
10587 * @resource_name: resource's name
10588 * @resource_size: resource's size
10589 * @resource_id: resource's id
10590 * @parent_resource_id: resource's parent id
10591 * @size_params: size parameters
10593 * Generic resources should reuse the same names across drivers.
10594 * Please see the generic resources list at:
10595 * Documentation/networking/devlink/devlink-resource.rst
10597 int devlink_resource_register(struct devlink *devlink,
10598 const char *resource_name,
10601 u64 parent_resource_id,
10602 const struct devlink_resource_size_params *size_params)
10604 struct devlink_resource *resource;
10605 struct list_head *resource_list;
10606 bool top_hierarchy;
10609 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10611 mutex_lock(&devlink->lock);
10612 resource = devlink_resource_find(devlink, NULL, resource_id);
10618 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10624 if (top_hierarchy) {
10625 resource_list = &devlink->resource_list;
10627 struct devlink_resource *parent_resource;
10629 parent_resource = devlink_resource_find(devlink, NULL,
10630 parent_resource_id);
10631 if (parent_resource) {
10632 resource_list = &parent_resource->resource_list;
10633 resource->parent = parent_resource;
10641 resource->name = resource_name;
10642 resource->size = resource_size;
10643 resource->size_new = resource_size;
10644 resource->id = resource_id;
10645 resource->size_valid = true;
10646 memcpy(&resource->size_params, size_params,
10647 sizeof(resource->size_params));
10648 INIT_LIST_HEAD(&resource->resource_list);
10649 list_add_tail(&resource->list, resource_list);
10651 mutex_unlock(&devlink->lock);
10654 EXPORT_SYMBOL_GPL(devlink_resource_register);
10656 static void devlink_resource_unregister(struct devlink *devlink,
10657 struct devlink_resource *resource)
10659 struct devlink_resource *tmp, *child_resource;
10661 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10663 devlink_resource_unregister(devlink, child_resource);
10664 list_del(&child_resource->list);
10665 kfree(child_resource);
10670 * devlink_resources_unregister - free all resources
10672 * @devlink: devlink
10674 void devlink_resources_unregister(struct devlink *devlink)
10676 struct devlink_resource *tmp, *child_resource;
10678 mutex_lock(&devlink->lock);
10680 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10682 devlink_resource_unregister(devlink, child_resource);
10683 list_del(&child_resource->list);
10684 kfree(child_resource);
10687 mutex_unlock(&devlink->lock);
10689 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10692 * devlink_resource_size_get - get and update size
10694 * @devlink: devlink
10695 * @resource_id: the requested resource id
10696 * @p_resource_size: ptr to update
10698 int devlink_resource_size_get(struct devlink *devlink,
10700 u64 *p_resource_size)
10702 struct devlink_resource *resource;
10705 mutex_lock(&devlink->lock);
10706 resource = devlink_resource_find(devlink, NULL, resource_id);
10711 *p_resource_size = resource->size_new;
10712 resource->size = resource->size_new;
10714 mutex_unlock(&devlink->lock);
10717 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
10720 * devlink_dpipe_table_resource_set - set the resource id
10722 * @devlink: devlink
10723 * @table_name: table name
10724 * @resource_id: resource id
10725 * @resource_units: number of resource's units consumed per table's entry
10727 int devlink_dpipe_table_resource_set(struct devlink *devlink,
10728 const char *table_name, u64 resource_id,
10729 u64 resource_units)
10731 struct devlink_dpipe_table *table;
10734 mutex_lock(&devlink->lock);
10735 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10736 table_name, devlink);
10741 table->resource_id = resource_id;
10742 table->resource_units = resource_units;
10743 table->resource_valid = true;
10745 mutex_unlock(&devlink->lock);
10748 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
10751 * devlink_resource_occ_get_register - register occupancy getter
10753 * @devlink: devlink
10754 * @resource_id: resource id
10755 * @occ_get: occupancy getter callback
10756 * @occ_get_priv: occupancy getter callback priv
10758 void devlink_resource_occ_get_register(struct devlink *devlink,
10760 devlink_resource_occ_get_t *occ_get,
10761 void *occ_get_priv)
10763 struct devlink_resource *resource;
10765 mutex_lock(&devlink->lock);
10766 resource = devlink_resource_find(devlink, NULL, resource_id);
10767 if (WARN_ON(!resource))
10769 WARN_ON(resource->occ_get);
10771 resource->occ_get = occ_get;
10772 resource->occ_get_priv = occ_get_priv;
10774 mutex_unlock(&devlink->lock);
10776 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10779 * devlink_resource_occ_get_unregister - unregister occupancy getter
10781 * @devlink: devlink
10782 * @resource_id: resource id
10784 void devlink_resource_occ_get_unregister(struct devlink *devlink,
10787 struct devlink_resource *resource;
10789 mutex_lock(&devlink->lock);
10790 resource = devlink_resource_find(devlink, NULL, resource_id);
10791 if (WARN_ON(!resource))
10793 WARN_ON(!resource->occ_get);
10795 resource->occ_get = NULL;
10796 resource->occ_get_priv = NULL;
10798 mutex_unlock(&devlink->lock);
10800 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
10802 static int devlink_param_verify(const struct devlink_param *param)
10804 if (!param || !param->name || !param->supported_cmodes)
10806 if (param->generic)
10807 return devlink_param_generic_verify(param);
10809 return devlink_param_driver_verify(param);
10813 * devlink_params_register - register configuration parameters
10815 * @devlink: devlink
10816 * @params: configuration parameters array
10817 * @params_count: number of parameters provided
10819 * Register the configuration parameters supported by the driver.
10821 int devlink_params_register(struct devlink *devlink,
10822 const struct devlink_param *params,
10823 size_t params_count)
10825 const struct devlink_param *param = params;
10828 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10830 for (i = 0; i < params_count; i++, param++) {
10831 err = devlink_param_register(devlink, param);
10841 for (param--; i > 0; i--, param--)
10842 devlink_param_unregister(devlink, param);
10845 EXPORT_SYMBOL_GPL(devlink_params_register);
10848 * devlink_params_unregister - unregister configuration parameters
10849 * @devlink: devlink
10850 * @params: configuration parameters to unregister
10851 * @params_count: number of parameters provided
10853 void devlink_params_unregister(struct devlink *devlink,
10854 const struct devlink_param *params,
10855 size_t params_count)
10857 const struct devlink_param *param = params;
10860 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10862 for (i = 0; i < params_count; i++, param++)
10863 devlink_param_unregister(devlink, param);
10865 EXPORT_SYMBOL_GPL(devlink_params_unregister);
10868 * devlink_param_register - register one configuration parameter
10870 * @devlink: devlink
10871 * @param: one configuration parameter
10873 * Register the configuration parameter supported by the driver.
10874 * Return: returns 0 on successful registration or error code otherwise.
10876 int devlink_param_register(struct devlink *devlink,
10877 const struct devlink_param *param)
10879 struct devlink_param_item *param_item;
10881 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10883 WARN_ON(devlink_param_verify(param));
10884 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
10886 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
10887 WARN_ON(param->get || param->set);
10889 WARN_ON(!param->get || !param->set);
10891 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
10895 param_item->param = param;
10897 list_add_tail(¶m_item->list, &devlink->param_list);
10900 EXPORT_SYMBOL_GPL(devlink_param_register);
10903 * devlink_param_unregister - unregister one configuration parameter
10904 * @devlink: devlink
10905 * @param: configuration parameter to unregister
10907 void devlink_param_unregister(struct devlink *devlink,
10908 const struct devlink_param *param)
10910 struct devlink_param_item *param_item;
10912 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10915 devlink_param_find_by_name(&devlink->param_list, param->name);
10916 WARN_ON(!param_item);
10917 list_del(¶m_item->list);
10920 EXPORT_SYMBOL_GPL(devlink_param_unregister);
10923 * devlink_param_driverinit_value_get - get configuration parameter
10924 * value for driver initializing
10926 * @devlink: devlink
10927 * @param_id: parameter ID
10928 * @init_val: value of parameter in driverinit configuration mode
10930 * This function should be used by the driver to get driverinit
10931 * configuration for initialization after reload command.
10933 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
10934 union devlink_param_value *init_val)
10936 struct devlink_param_item *param_item;
10938 if (!devlink_reload_supported(devlink->ops))
10939 return -EOPNOTSUPP;
10941 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10945 if (!param_item->driverinit_value_valid ||
10946 !devlink_param_cmode_is_supported(param_item->param,
10947 DEVLINK_PARAM_CMODE_DRIVERINIT))
10948 return -EOPNOTSUPP;
10950 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10951 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
10953 *init_val = param_item->driverinit_value;
10957 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
10960 * devlink_param_driverinit_value_set - set value of configuration
10961 * parameter for driverinit
10962 * configuration mode
10964 * @devlink: devlink
10965 * @param_id: parameter ID
10966 * @init_val: value of parameter to set for driverinit configuration mode
10968 * This function should be used by the driver to set driverinit
10969 * configuration mode default value.
10971 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
10972 union devlink_param_value init_val)
10974 struct devlink_param_item *param_item;
10976 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10978 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10982 if (!devlink_param_cmode_is_supported(param_item->param,
10983 DEVLINK_PARAM_CMODE_DRIVERINIT))
10984 return -EOPNOTSUPP;
10986 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10987 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
10989 param_item->driverinit_value = init_val;
10990 param_item->driverinit_value_valid = true;
10993 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
10996 * devlink_param_value_changed - notify devlink on a parameter's value
10997 * change. Should be called by the driver
10998 * right after the change.
11000 * @devlink: devlink
11001 * @param_id: parameter ID
11003 * This function should be used by the driver to notify devlink on value
11004 * change, excluding driverinit configuration mode.
11005 * For driverinit configuration mode driver should use the function
11007 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11009 struct devlink_param_item *param_item;
11011 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11012 WARN_ON(!param_item);
11014 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11016 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11019 * devlink_region_create - create a new address region
11021 * @devlink: devlink
11022 * @ops: region operations and name
11023 * @region_max_snapshots: Maximum supported number of snapshots for region
11024 * @region_size: size of region
11026 struct devlink_region *
11027 devlink_region_create(struct devlink *devlink,
11028 const struct devlink_region_ops *ops,
11029 u32 region_max_snapshots, u64 region_size)
11031 struct devlink_region *region;
11034 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11035 return ERR_PTR(-EINVAL);
11037 mutex_lock(&devlink->lock);
11039 if (devlink_region_get_by_name(devlink, ops->name)) {
11044 region = kzalloc(sizeof(*region), GFP_KERNEL);
11050 region->devlink = devlink;
11051 region->max_snapshots = region_max_snapshots;
11053 region->size = region_size;
11054 INIT_LIST_HEAD(®ion->snapshot_list);
11055 list_add_tail(®ion->list, &devlink->region_list);
11056 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11058 mutex_unlock(&devlink->lock);
11062 mutex_unlock(&devlink->lock);
11063 return ERR_PTR(err);
11065 EXPORT_SYMBOL_GPL(devlink_region_create);
11068 * devlink_port_region_create - create a new address region for a port
11070 * @port: devlink port
11071 * @ops: region operations and name
11072 * @region_max_snapshots: Maximum supported number of snapshots for region
11073 * @region_size: size of region
11075 struct devlink_region *
11076 devlink_port_region_create(struct devlink_port *port,
11077 const struct devlink_port_region_ops *ops,
11078 u32 region_max_snapshots, u64 region_size)
11080 struct devlink *devlink = port->devlink;
11081 struct devlink_region *region;
11084 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11085 return ERR_PTR(-EINVAL);
11087 mutex_lock(&devlink->lock);
11089 if (devlink_port_region_get_by_name(port, ops->name)) {
11094 region = kzalloc(sizeof(*region), GFP_KERNEL);
11100 region->devlink = devlink;
11101 region->port = port;
11102 region->max_snapshots = region_max_snapshots;
11103 region->port_ops = ops;
11104 region->size = region_size;
11105 INIT_LIST_HEAD(®ion->snapshot_list);
11106 list_add_tail(®ion->list, &port->region_list);
11107 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11109 mutex_unlock(&devlink->lock);
11113 mutex_unlock(&devlink->lock);
11114 return ERR_PTR(err);
11116 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11119 * devlink_region_destroy - destroy address region
11121 * @region: devlink region to destroy
11123 void devlink_region_destroy(struct devlink_region *region)
11125 struct devlink *devlink = region->devlink;
11126 struct devlink_snapshot *snapshot, *ts;
11128 mutex_lock(&devlink->lock);
11130 /* Free all snapshots of region */
11131 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
11132 devlink_region_snapshot_del(region, snapshot);
11134 list_del(®ion->list);
11136 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11137 mutex_unlock(&devlink->lock);
11140 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11143 * devlink_region_snapshot_id_get - get snapshot ID
11145 * This callback should be called when adding a new snapshot,
11146 * Driver should use the same id for multiple snapshots taken
11147 * on multiple regions at the same time/by the same trigger.
11149 * The caller of this function must use devlink_region_snapshot_id_put
11150 * when finished creating regions using this id.
11152 * Returns zero on success, or a negative error code on failure.
11154 * @devlink: devlink
11155 * @id: storage to return id
11157 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11161 mutex_lock(&devlink->lock);
11162 err = __devlink_region_snapshot_id_get(devlink, id);
11163 mutex_unlock(&devlink->lock);
11167 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11170 * devlink_region_snapshot_id_put - put snapshot ID reference
11172 * This should be called by a driver after finishing creating snapshots
11173 * with an id. Doing so ensures that the ID can later be released in the
11174 * event that all snapshots using it have been destroyed.
11176 * @devlink: devlink
11177 * @id: id to release reference on
11179 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11181 mutex_lock(&devlink->lock);
11182 __devlink_snapshot_id_decrement(devlink, id);
11183 mutex_unlock(&devlink->lock);
11185 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11188 * devlink_region_snapshot_create - create a new snapshot
11189 * This will add a new snapshot of a region. The snapshot
11190 * will be stored on the region struct and can be accessed
11191 * from devlink. This is useful for future analyses of snapshots.
11192 * Multiple snapshots can be created on a region.
11193 * The @snapshot_id should be obtained using the getter function.
11195 * @region: devlink region of the snapshot
11196 * @data: snapshot data
11197 * @snapshot_id: snapshot id to be created
11199 int devlink_region_snapshot_create(struct devlink_region *region,
11200 u8 *data, u32 snapshot_id)
11202 struct devlink *devlink = region->devlink;
11205 mutex_lock(&devlink->lock);
11206 err = __devlink_region_snapshot_create(region, data, snapshot_id);
11207 mutex_unlock(&devlink->lock);
11211 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11213 #define DEVLINK_TRAP(_id, _type) \
11215 .type = DEVLINK_TRAP_TYPE_##_type, \
11216 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
11217 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
11220 static const struct devlink_trap devlink_trap_generic[] = {
11221 DEVLINK_TRAP(SMAC_MC, DROP),
11222 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11223 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11224 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11225 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11226 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11227 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11228 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11229 DEVLINK_TRAP(TAIL_DROP, DROP),
11230 DEVLINK_TRAP(NON_IP_PACKET, DROP),
11231 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11232 DEVLINK_TRAP(DIP_LB, DROP),
11233 DEVLINK_TRAP(SIP_MC, DROP),
11234 DEVLINK_TRAP(SIP_LB, DROP),
11235 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11236 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11237 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11238 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11239 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11240 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11241 DEVLINK_TRAP(RPF, EXCEPTION),
11242 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11243 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11244 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11245 DEVLINK_TRAP(NON_ROUTABLE, DROP),
11246 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11247 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11248 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11249 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11250 DEVLINK_TRAP(STP, CONTROL),
11251 DEVLINK_TRAP(LACP, CONTROL),
11252 DEVLINK_TRAP(LLDP, CONTROL),
11253 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11254 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11255 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11256 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11257 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11258 DEVLINK_TRAP(MLD_QUERY, CONTROL),
11259 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11260 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11261 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11262 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11263 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11264 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11265 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11266 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11267 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11268 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11269 DEVLINK_TRAP(IPV4_BFD, CONTROL),
11270 DEVLINK_TRAP(IPV6_BFD, CONTROL),
11271 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11272 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11273 DEVLINK_TRAP(IPV4_BGP, CONTROL),
11274 DEVLINK_TRAP(IPV6_BGP, CONTROL),
11275 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11276 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11277 DEVLINK_TRAP(IPV4_PIM, CONTROL),
11278 DEVLINK_TRAP(IPV6_PIM, CONTROL),
11279 DEVLINK_TRAP(UC_LB, CONTROL),
11280 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11281 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11282 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11283 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11284 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11285 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11286 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11287 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11288 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11289 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11290 DEVLINK_TRAP(PTP_EVENT, CONTROL),
11291 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11292 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11293 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11294 DEVLINK_TRAP(EARLY_DROP, DROP),
11295 DEVLINK_TRAP(VXLAN_PARSING, DROP),
11296 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11297 DEVLINK_TRAP(VLAN_PARSING, DROP),
11298 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11299 DEVLINK_TRAP(MPLS_PARSING, DROP),
11300 DEVLINK_TRAP(ARP_PARSING, DROP),
11301 DEVLINK_TRAP(IP_1_PARSING, DROP),
11302 DEVLINK_TRAP(IP_N_PARSING, DROP),
11303 DEVLINK_TRAP(GRE_PARSING, DROP),
11304 DEVLINK_TRAP(UDP_PARSING, DROP),
11305 DEVLINK_TRAP(TCP_PARSING, DROP),
11306 DEVLINK_TRAP(IPSEC_PARSING, DROP),
11307 DEVLINK_TRAP(SCTP_PARSING, DROP),
11308 DEVLINK_TRAP(DCCP_PARSING, DROP),
11309 DEVLINK_TRAP(GTP_PARSING, DROP),
11310 DEVLINK_TRAP(ESP_PARSING, DROP),
11311 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11312 DEVLINK_TRAP(DMAC_FILTER, DROP),
11315 #define DEVLINK_TRAP_GROUP(_id) \
11317 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
11318 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
11321 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11322 DEVLINK_TRAP_GROUP(L2_DROPS),
11323 DEVLINK_TRAP_GROUP(L3_DROPS),
11324 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11325 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11326 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11327 DEVLINK_TRAP_GROUP(ACL_DROPS),
11328 DEVLINK_TRAP_GROUP(STP),
11329 DEVLINK_TRAP_GROUP(LACP),
11330 DEVLINK_TRAP_GROUP(LLDP),
11331 DEVLINK_TRAP_GROUP(MC_SNOOPING),
11332 DEVLINK_TRAP_GROUP(DHCP),
11333 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11334 DEVLINK_TRAP_GROUP(BFD),
11335 DEVLINK_TRAP_GROUP(OSPF),
11336 DEVLINK_TRAP_GROUP(BGP),
11337 DEVLINK_TRAP_GROUP(VRRP),
11338 DEVLINK_TRAP_GROUP(PIM),
11339 DEVLINK_TRAP_GROUP(UC_LB),
11340 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11341 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11342 DEVLINK_TRAP_GROUP(IPV6),
11343 DEVLINK_TRAP_GROUP(PTP_EVENT),
11344 DEVLINK_TRAP_GROUP(PTP_GENERAL),
11345 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11346 DEVLINK_TRAP_GROUP(ACL_TRAP),
11347 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11350 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11352 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11355 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11358 if (trap->type != devlink_trap_generic[trap->id].type)
11364 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11368 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11371 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11372 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11379 static int devlink_trap_verify(const struct devlink_trap *trap)
11381 if (!trap || !trap->name)
11385 return devlink_trap_generic_verify(trap);
11387 return devlink_trap_driver_verify(trap);
11391 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11393 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11396 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11403 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11407 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11410 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11411 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11418 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11420 if (group->generic)
11421 return devlink_trap_group_generic_verify(group);
11423 return devlink_trap_group_driver_verify(group);
11427 devlink_trap_group_notify(struct devlink *devlink,
11428 const struct devlink_trap_group_item *group_item,
11429 enum devlink_command cmd)
11431 struct sk_buff *msg;
11434 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11435 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11436 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11439 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11443 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11450 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11451 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11455 devlink_trap_item_group_link(struct devlink *devlink,
11456 struct devlink_trap_item *trap_item)
11458 u16 group_id = trap_item->trap->init_group_id;
11459 struct devlink_trap_group_item *group_item;
11461 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11462 if (WARN_ON_ONCE(!group_item))
11465 trap_item->group_item = group_item;
11470 static void devlink_trap_notify(struct devlink *devlink,
11471 const struct devlink_trap_item *trap_item,
11472 enum devlink_command cmd)
11474 struct sk_buff *msg;
11477 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11478 cmd != DEVLINK_CMD_TRAP_DEL);
11479 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11486 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11492 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11493 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11497 devlink_trap_register(struct devlink *devlink,
11498 const struct devlink_trap *trap, void *priv)
11500 struct devlink_trap_item *trap_item;
11503 if (devlink_trap_item_lookup(devlink, trap->name))
11506 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11510 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11511 if (!trap_item->stats) {
11513 goto err_stats_alloc;
11516 trap_item->trap = trap;
11517 trap_item->action = trap->init_action;
11518 trap_item->priv = priv;
11520 err = devlink_trap_item_group_link(devlink, trap_item);
11522 goto err_group_link;
11524 err = devlink->ops->trap_init(devlink, trap, trap_item);
11526 goto err_trap_init;
11528 list_add_tail(&trap_item->list, &devlink->trap_list);
11529 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11535 free_percpu(trap_item->stats);
11541 static void devlink_trap_unregister(struct devlink *devlink,
11542 const struct devlink_trap *trap)
11544 struct devlink_trap_item *trap_item;
11546 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11547 if (WARN_ON_ONCE(!trap_item))
11550 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11551 list_del(&trap_item->list);
11552 if (devlink->ops->trap_fini)
11553 devlink->ops->trap_fini(devlink, trap, trap_item);
11554 free_percpu(trap_item->stats);
11558 static void devlink_trap_disable(struct devlink *devlink,
11559 const struct devlink_trap *trap)
11561 struct devlink_trap_item *trap_item;
11563 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11564 if (WARN_ON_ONCE(!trap_item))
11567 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11569 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11573 * devlink_traps_register - Register packet traps with devlink.
11574 * @devlink: devlink.
11575 * @traps: Packet traps.
11576 * @traps_count: Count of provided packet traps.
11577 * @priv: Driver private information.
11579 * Return: Non-zero value on failure.
11581 int devlink_traps_register(struct devlink *devlink,
11582 const struct devlink_trap *traps,
11583 size_t traps_count, void *priv)
11587 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11590 mutex_lock(&devlink->lock);
11591 for (i = 0; i < traps_count; i++) {
11592 const struct devlink_trap *trap = &traps[i];
11594 err = devlink_trap_verify(trap);
11596 goto err_trap_verify;
11598 err = devlink_trap_register(devlink, trap, priv);
11600 goto err_trap_register;
11602 mutex_unlock(&devlink->lock);
11608 for (i--; i >= 0; i--)
11609 devlink_trap_unregister(devlink, &traps[i]);
11610 mutex_unlock(&devlink->lock);
11613 EXPORT_SYMBOL_GPL(devlink_traps_register);
11616 * devlink_traps_unregister - Unregister packet traps from devlink.
11617 * @devlink: devlink.
11618 * @traps: Packet traps.
11619 * @traps_count: Count of provided packet traps.
11621 void devlink_traps_unregister(struct devlink *devlink,
11622 const struct devlink_trap *traps,
11623 size_t traps_count)
11627 mutex_lock(&devlink->lock);
11628 /* Make sure we do not have any packets in-flight while unregistering
11629 * traps by disabling all of them and waiting for a grace period.
11631 for (i = traps_count - 1; i >= 0; i--)
11632 devlink_trap_disable(devlink, &traps[i]);
11634 for (i = traps_count - 1; i >= 0; i--)
11635 devlink_trap_unregister(devlink, &traps[i]);
11636 mutex_unlock(&devlink->lock);
11638 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11641 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11644 struct devlink_stats *stats;
11646 stats = this_cpu_ptr(trap_stats);
11647 u64_stats_update_begin(&stats->syncp);
11648 u64_stats_add(&stats->rx_bytes, skb_len);
11649 u64_stats_inc(&stats->rx_packets);
11650 u64_stats_update_end(&stats->syncp);
11654 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11655 const struct devlink_trap_item *trap_item,
11656 struct devlink_port *in_devlink_port,
11657 const struct flow_action_cookie *fa_cookie)
11659 metadata->trap_name = trap_item->trap->name;
11660 metadata->trap_group_name = trap_item->group_item->group->name;
11661 metadata->fa_cookie = fa_cookie;
11662 metadata->trap_type = trap_item->trap->type;
11664 spin_lock(&in_devlink_port->type_lock);
11665 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11666 metadata->input_dev = in_devlink_port->type_dev;
11667 spin_unlock(&in_devlink_port->type_lock);
11671 * devlink_trap_report - Report trapped packet to drop monitor.
11672 * @devlink: devlink.
11673 * @skb: Trapped packet.
11674 * @trap_ctx: Trap context.
11675 * @in_devlink_port: Input devlink port.
11676 * @fa_cookie: Flow action cookie. Could be NULL.
11678 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11679 void *trap_ctx, struct devlink_port *in_devlink_port,
11680 const struct flow_action_cookie *fa_cookie)
11683 struct devlink_trap_item *trap_item = trap_ctx;
11685 devlink_trap_stats_update(trap_item->stats, skb->len);
11686 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11688 if (trace_devlink_trap_report_enabled()) {
11689 struct devlink_trap_metadata metadata = {};
11691 devlink_trap_report_metadata_set(&metadata, trap_item,
11692 in_devlink_port, fa_cookie);
11693 trace_devlink_trap_report(devlink, skb, &metadata);
11696 EXPORT_SYMBOL_GPL(devlink_trap_report);
11699 * devlink_trap_ctx_priv - Trap context to driver private information.
11700 * @trap_ctx: Trap context.
11702 * Return: Driver private information passed during registration.
11704 void *devlink_trap_ctx_priv(void *trap_ctx)
11706 struct devlink_trap_item *trap_item = trap_ctx;
11708 return trap_item->priv;
11710 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
11713 devlink_trap_group_item_policer_link(struct devlink *devlink,
11714 struct devlink_trap_group_item *group_item)
11716 u32 policer_id = group_item->group->init_policer_id;
11717 struct devlink_trap_policer_item *policer_item;
11719 if (policer_id == 0)
11722 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
11723 if (WARN_ON_ONCE(!policer_item))
11726 group_item->policer_item = policer_item;
11732 devlink_trap_group_register(struct devlink *devlink,
11733 const struct devlink_trap_group *group)
11735 struct devlink_trap_group_item *group_item;
11738 if (devlink_trap_group_item_lookup(devlink, group->name))
11741 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
11745 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11746 if (!group_item->stats) {
11748 goto err_stats_alloc;
11751 group_item->group = group;
11753 err = devlink_trap_group_item_policer_link(devlink, group_item);
11755 goto err_policer_link;
11757 if (devlink->ops->trap_group_init) {
11758 err = devlink->ops->trap_group_init(devlink, group);
11760 goto err_group_init;
11763 list_add_tail(&group_item->list, &devlink->trap_group_list);
11764 devlink_trap_group_notify(devlink, group_item,
11765 DEVLINK_CMD_TRAP_GROUP_NEW);
11771 free_percpu(group_item->stats);
11778 devlink_trap_group_unregister(struct devlink *devlink,
11779 const struct devlink_trap_group *group)
11781 struct devlink_trap_group_item *group_item;
11783 group_item = devlink_trap_group_item_lookup(devlink, group->name);
11784 if (WARN_ON_ONCE(!group_item))
11787 devlink_trap_group_notify(devlink, group_item,
11788 DEVLINK_CMD_TRAP_GROUP_DEL);
11789 list_del(&group_item->list);
11790 free_percpu(group_item->stats);
11795 * devlink_trap_groups_register - Register packet trap groups with devlink.
11796 * @devlink: devlink.
11797 * @groups: Packet trap groups.
11798 * @groups_count: Count of provided packet trap groups.
11800 * Return: Non-zero value on failure.
11802 int devlink_trap_groups_register(struct devlink *devlink,
11803 const struct devlink_trap_group *groups,
11804 size_t groups_count)
11808 mutex_lock(&devlink->lock);
11809 for (i = 0; i < groups_count; i++) {
11810 const struct devlink_trap_group *group = &groups[i];
11812 err = devlink_trap_group_verify(group);
11814 goto err_trap_group_verify;
11816 err = devlink_trap_group_register(devlink, group);
11818 goto err_trap_group_register;
11820 mutex_unlock(&devlink->lock);
11824 err_trap_group_register:
11825 err_trap_group_verify:
11826 for (i--; i >= 0; i--)
11827 devlink_trap_group_unregister(devlink, &groups[i]);
11828 mutex_unlock(&devlink->lock);
11831 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
11834 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
11835 * @devlink: devlink.
11836 * @groups: Packet trap groups.
11837 * @groups_count: Count of provided packet trap groups.
11839 void devlink_trap_groups_unregister(struct devlink *devlink,
11840 const struct devlink_trap_group *groups,
11841 size_t groups_count)
11845 mutex_lock(&devlink->lock);
11846 for (i = groups_count - 1; i >= 0; i--)
11847 devlink_trap_group_unregister(devlink, &groups[i]);
11848 mutex_unlock(&devlink->lock);
11850 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
11853 devlink_trap_policer_notify(struct devlink *devlink,
11854 const struct devlink_trap_policer_item *policer_item,
11855 enum devlink_command cmd)
11857 struct sk_buff *msg;
11860 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
11861 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
11862 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11865 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11869 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
11876 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11877 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11881 devlink_trap_policer_register(struct devlink *devlink,
11882 const struct devlink_trap_policer *policer)
11884 struct devlink_trap_policer_item *policer_item;
11887 if (devlink_trap_policer_item_lookup(devlink, policer->id))
11890 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
11894 policer_item->policer = policer;
11895 policer_item->rate = policer->init_rate;
11896 policer_item->burst = policer->init_burst;
11898 if (devlink->ops->trap_policer_init) {
11899 err = devlink->ops->trap_policer_init(devlink, policer);
11901 goto err_policer_init;
11904 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
11905 devlink_trap_policer_notify(devlink, policer_item,
11906 DEVLINK_CMD_TRAP_POLICER_NEW);
11911 kfree(policer_item);
11916 devlink_trap_policer_unregister(struct devlink *devlink,
11917 const struct devlink_trap_policer *policer)
11919 struct devlink_trap_policer_item *policer_item;
11921 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
11922 if (WARN_ON_ONCE(!policer_item))
11925 devlink_trap_policer_notify(devlink, policer_item,
11926 DEVLINK_CMD_TRAP_POLICER_DEL);
11927 list_del(&policer_item->list);
11928 if (devlink->ops->trap_policer_fini)
11929 devlink->ops->trap_policer_fini(devlink, policer);
11930 kfree(policer_item);
11934 * devlink_trap_policers_register - Register packet trap policers with devlink.
11935 * @devlink: devlink.
11936 * @policers: Packet trap policers.
11937 * @policers_count: Count of provided packet trap policers.
11939 * Return: Non-zero value on failure.
11942 devlink_trap_policers_register(struct devlink *devlink,
11943 const struct devlink_trap_policer *policers,
11944 size_t policers_count)
11948 mutex_lock(&devlink->lock);
11949 for (i = 0; i < policers_count; i++) {
11950 const struct devlink_trap_policer *policer = &policers[i];
11952 if (WARN_ON(policer->id == 0 ||
11953 policer->max_rate < policer->min_rate ||
11954 policer->max_burst < policer->min_burst)) {
11956 goto err_trap_policer_verify;
11959 err = devlink_trap_policer_register(devlink, policer);
11961 goto err_trap_policer_register;
11963 mutex_unlock(&devlink->lock);
11967 err_trap_policer_register:
11968 err_trap_policer_verify:
11969 for (i--; i >= 0; i--)
11970 devlink_trap_policer_unregister(devlink, &policers[i]);
11971 mutex_unlock(&devlink->lock);
11974 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
11977 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
11978 * @devlink: devlink.
11979 * @policers: Packet trap policers.
11980 * @policers_count: Count of provided packet trap policers.
11983 devlink_trap_policers_unregister(struct devlink *devlink,
11984 const struct devlink_trap_policer *policers,
11985 size_t policers_count)
11989 mutex_lock(&devlink->lock);
11990 for (i = policers_count - 1; i >= 0; i--)
11991 devlink_trap_policer_unregister(devlink, &policers[i]);
11992 mutex_unlock(&devlink->lock);
11994 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
11996 static void __devlink_compat_running_version(struct devlink *devlink,
11997 char *buf, size_t len)
11999 const struct nlattr *nlattr;
12000 struct devlink_info_req req;
12001 struct sk_buff *msg;
12004 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12009 err = devlink->ops->info_get(devlink, &req, NULL);
12013 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12014 const struct nlattr *kv;
12017 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12020 nla_for_each_nested(kv, nlattr, rem_kv) {
12021 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12024 strlcat(buf, nla_data(kv), len);
12025 strlcat(buf, " ", len);
12032 static struct devlink_port *netdev_to_devlink_port(struct net_device *dev)
12034 if (!dev->netdev_ops->ndo_get_devlink_port)
12037 return dev->netdev_ops->ndo_get_devlink_port(dev);
12040 void devlink_compat_running_version(struct devlink *devlink,
12041 char *buf, size_t len)
12043 if (!devlink->ops->info_get)
12046 mutex_lock(&devlink->lock);
12047 __devlink_compat_running_version(devlink, buf, len);
12048 mutex_unlock(&devlink->lock);
12051 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12053 struct devlink_flash_update_params params = {};
12056 if (!devlink->ops->flash_update)
12057 return -EOPNOTSUPP;
12059 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
12063 mutex_lock(&devlink->lock);
12064 devlink_flash_update_begin_notify(devlink);
12065 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
12066 devlink_flash_update_end_notify(devlink);
12067 mutex_unlock(&devlink->lock);
12069 release_firmware(params.fw);
12074 int devlink_compat_phys_port_name_get(struct net_device *dev,
12075 char *name, size_t len)
12077 struct devlink_port *devlink_port;
12079 /* RTNL mutex is held here which ensures that devlink_port
12080 * instance cannot disappear in the middle. No need to take
12081 * any devlink lock as only permanent values are accessed.
12085 devlink_port = netdev_to_devlink_port(dev);
12087 return -EOPNOTSUPP;
12089 return __devlink_port_phys_port_name_get(devlink_port, name, len);
12092 int devlink_compat_switch_id_get(struct net_device *dev,
12093 struct netdev_phys_item_id *ppid)
12095 struct devlink_port *devlink_port;
12097 /* Caller must hold RTNL mutex or reference to dev, which ensures that
12098 * devlink_port instance cannot disappear in the middle. No need to take
12099 * any devlink lock as only permanent values are accessed.
12101 devlink_port = netdev_to_devlink_port(dev);
12102 if (!devlink_port || !devlink_port->switch_port)
12103 return -EOPNOTSUPP;
12105 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
12110 static void __net_exit devlink_pernet_pre_exit(struct net *net)
12112 struct devlink *devlink;
12113 u32 actions_performed;
12114 unsigned long index;
12117 /* In case network namespace is getting destroyed, reload
12118 * all devlink instances from this namespace into init_net.
12120 mutex_lock(&devlink_mutex);
12121 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
12122 if (!devlink_try_get(devlink))
12125 if (!net_eq(devlink_net(devlink), net))
12128 WARN_ON(!(devlink->features & DEVLINK_F_RELOAD));
12129 err = devlink_reload(devlink, &init_net,
12130 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
12131 DEVLINK_RELOAD_LIMIT_UNSPEC,
12132 &actions_performed, NULL);
12133 if (err && err != -EOPNOTSUPP)
12134 pr_warn("Failed to reload devlink instance into init_net\n");
12136 devlink_put(devlink);
12138 mutex_unlock(&devlink_mutex);
12141 static struct pernet_operations devlink_pernet_ops __net_initdata = {
12142 .pre_exit = devlink_pernet_pre_exit,
12145 static int __init devlink_init(void)
12149 err = genl_register_family(&devlink_nl_family);
12152 err = register_pernet_subsys(&devlink_pernet_ops);
12159 subsys_initcall(devlink_init);