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 {
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 = cpu_stats->rx_packets;
8108 rx_bytes = cpu_stats->rx_bytes;
8109 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
8111 stats->rx_packets += rx_packets;
8112 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 stats.rx_packets, DEVLINK_ATTR_PAD))
8131 goto nla_put_failure;
8133 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8134 stats.rx_bytes, DEVLINK_ATTR_PAD))
8135 goto nla_put_failure;
8137 nla_nest_end(msg, attr);
8142 nla_nest_cancel(msg, attr);
8146 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8147 const struct devlink_trap_item *trap_item)
8149 struct devlink_stats stats;
8150 struct nlattr *attr;
8154 if (devlink->ops->trap_drop_counter_get) {
8155 err = devlink->ops->trap_drop_counter_get(devlink,
8162 devlink_trap_stats_read(trap_item->stats, &stats);
8164 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8168 if (devlink->ops->trap_drop_counter_get &&
8169 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8171 goto nla_put_failure;
8173 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8174 stats.rx_packets, DEVLINK_ATTR_PAD))
8175 goto nla_put_failure;
8177 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8178 stats.rx_bytes, DEVLINK_ATTR_PAD))
8179 goto nla_put_failure;
8181 nla_nest_end(msg, attr);
8186 nla_nest_cancel(msg, attr);
8190 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8191 const struct devlink_trap_item *trap_item,
8192 enum devlink_command cmd, u32 portid, u32 seq,
8195 struct devlink_trap_group_item *group_item = trap_item->group_item;
8199 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8203 if (devlink_nl_put_handle(msg, devlink))
8204 goto nla_put_failure;
8206 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8207 group_item->group->name))
8208 goto nla_put_failure;
8210 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8211 goto nla_put_failure;
8213 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8214 goto nla_put_failure;
8216 if (trap_item->trap->generic &&
8217 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8218 goto nla_put_failure;
8220 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8221 goto nla_put_failure;
8223 err = devlink_trap_metadata_put(msg, trap_item->trap);
8225 goto nla_put_failure;
8227 err = devlink_trap_stats_put(msg, devlink, trap_item);
8229 goto nla_put_failure;
8231 genlmsg_end(msg, hdr);
8236 genlmsg_cancel(msg, hdr);
8240 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8241 struct genl_info *info)
8243 struct netlink_ext_ack *extack = info->extack;
8244 struct devlink *devlink = info->user_ptr[0];
8245 struct devlink_trap_item *trap_item;
8246 struct sk_buff *msg;
8249 if (list_empty(&devlink->trap_list))
8252 trap_item = devlink_trap_item_get_from_info(devlink, info);
8254 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8262 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8263 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8268 return genlmsg_reply(msg, info);
8275 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
8276 struct netlink_callback *cb)
8278 struct devlink_trap_item *trap_item;
8279 struct devlink *devlink;
8280 int start = cb->args[0];
8281 unsigned long index;
8285 mutex_lock(&devlink_mutex);
8286 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8287 if (!devlink_try_get(devlink))
8290 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8293 mutex_lock(&devlink->lock);
8294 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8299 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8300 DEVLINK_CMD_TRAP_NEW,
8301 NETLINK_CB(cb->skb).portid,
8305 mutex_unlock(&devlink->lock);
8306 devlink_put(devlink);
8311 mutex_unlock(&devlink->lock);
8313 devlink_put(devlink);
8316 mutex_unlock(&devlink_mutex);
8322 static int __devlink_trap_action_set(struct devlink *devlink,
8323 struct devlink_trap_item *trap_item,
8324 enum devlink_trap_action trap_action,
8325 struct netlink_ext_ack *extack)
8329 if (trap_item->action != trap_action &&
8330 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8331 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8335 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8336 trap_action, extack);
8340 trap_item->action = trap_action;
8345 static int devlink_trap_action_set(struct devlink *devlink,
8346 struct devlink_trap_item *trap_item,
8347 struct genl_info *info)
8349 enum devlink_trap_action trap_action;
8352 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8355 err = devlink_trap_action_get_from_info(info, &trap_action);
8357 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8361 return __devlink_trap_action_set(devlink, trap_item, trap_action,
8365 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8366 struct genl_info *info)
8368 struct netlink_ext_ack *extack = info->extack;
8369 struct devlink *devlink = info->user_ptr[0];
8370 struct devlink_trap_item *trap_item;
8372 if (list_empty(&devlink->trap_list))
8375 trap_item = devlink_trap_item_get_from_info(devlink, info);
8377 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8381 return devlink_trap_action_set(devlink, trap_item, info);
8384 static struct devlink_trap_group_item *
8385 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8387 struct devlink_trap_group_item *group_item;
8389 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8390 if (!strcmp(group_item->group->name, name))
8397 static struct devlink_trap_group_item *
8398 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8400 struct devlink_trap_group_item *group_item;
8402 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8403 if (group_item->group->id == id)
8410 static struct devlink_trap_group_item *
8411 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8412 struct genl_info *info)
8416 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8418 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8420 return devlink_trap_group_item_lookup(devlink, name);
8424 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8425 const struct devlink_trap_group_item *group_item,
8426 enum devlink_command cmd, u32 portid, u32 seq,
8432 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8436 if (devlink_nl_put_handle(msg, devlink))
8437 goto nla_put_failure;
8439 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8440 group_item->group->name))
8441 goto nla_put_failure;
8443 if (group_item->group->generic &&
8444 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8445 goto nla_put_failure;
8447 if (group_item->policer_item &&
8448 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8449 group_item->policer_item->policer->id))
8450 goto nla_put_failure;
8452 err = devlink_trap_group_stats_put(msg, group_item->stats);
8454 goto nla_put_failure;
8456 genlmsg_end(msg, hdr);
8461 genlmsg_cancel(msg, hdr);
8465 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8466 struct genl_info *info)
8468 struct netlink_ext_ack *extack = info->extack;
8469 struct devlink *devlink = info->user_ptr[0];
8470 struct devlink_trap_group_item *group_item;
8471 struct sk_buff *msg;
8474 if (list_empty(&devlink->trap_group_list))
8477 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8479 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8487 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8488 DEVLINK_CMD_TRAP_GROUP_NEW,
8489 info->snd_portid, info->snd_seq, 0);
8491 goto err_trap_group_fill;
8493 return genlmsg_reply(msg, info);
8495 err_trap_group_fill:
8500 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8501 struct netlink_callback *cb)
8503 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8504 struct devlink_trap_group_item *group_item;
8505 u32 portid = NETLINK_CB(cb->skb).portid;
8506 struct devlink *devlink;
8507 int start = cb->args[0];
8508 unsigned long index;
8512 mutex_lock(&devlink_mutex);
8513 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8514 if (!devlink_try_get(devlink))
8517 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8520 mutex_lock(&devlink->lock);
8521 list_for_each_entry(group_item, &devlink->trap_group_list,
8527 err = devlink_nl_trap_group_fill(msg, devlink,
8533 mutex_unlock(&devlink->lock);
8534 devlink_put(devlink);
8539 mutex_unlock(&devlink->lock);
8541 devlink_put(devlink);
8544 mutex_unlock(&devlink_mutex);
8551 __devlink_trap_group_action_set(struct devlink *devlink,
8552 struct devlink_trap_group_item *group_item,
8553 enum devlink_trap_action trap_action,
8554 struct netlink_ext_ack *extack)
8556 const char *group_name = group_item->group->name;
8557 struct devlink_trap_item *trap_item;
8560 if (devlink->ops->trap_group_action_set) {
8561 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8562 trap_action, extack);
8566 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8567 if (strcmp(trap_item->group_item->group->name, group_name))
8569 if (trap_item->action != trap_action &&
8570 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8572 trap_item->action = trap_action;
8578 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8579 if (strcmp(trap_item->group_item->group->name, group_name))
8581 err = __devlink_trap_action_set(devlink, trap_item,
8582 trap_action, extack);
8591 devlink_trap_group_action_set(struct devlink *devlink,
8592 struct devlink_trap_group_item *group_item,
8593 struct genl_info *info, bool *p_modified)
8595 enum devlink_trap_action trap_action;
8598 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8601 err = devlink_trap_action_get_from_info(info, &trap_action);
8603 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8607 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8617 static int devlink_trap_group_set(struct devlink *devlink,
8618 struct devlink_trap_group_item *group_item,
8619 struct genl_info *info)
8621 struct devlink_trap_policer_item *policer_item;
8622 struct netlink_ext_ack *extack = info->extack;
8623 const struct devlink_trap_policer *policer;
8624 struct nlattr **attrs = info->attrs;
8627 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8630 if (!devlink->ops->trap_group_set)
8633 policer_item = group_item->policer_item;
8634 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8637 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8638 policer_item = devlink_trap_policer_item_lookup(devlink,
8640 if (policer_id && !policer_item) {
8641 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8645 policer = policer_item ? policer_item->policer : NULL;
8647 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8652 group_item->policer_item = policer_item;
8657 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8658 struct genl_info *info)
8660 struct netlink_ext_ack *extack = info->extack;
8661 struct devlink *devlink = info->user_ptr[0];
8662 struct devlink_trap_group_item *group_item;
8663 bool modified = false;
8666 if (list_empty(&devlink->trap_group_list))
8669 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8671 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8675 err = devlink_trap_group_action_set(devlink, group_item, info,
8680 err = devlink_trap_group_set(devlink, group_item, info);
8682 goto err_trap_group_set;
8688 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8692 static struct devlink_trap_policer_item *
8693 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8694 struct genl_info *info)
8698 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8700 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8702 return devlink_trap_policer_item_lookup(devlink, id);
8706 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8707 const struct devlink_trap_policer *policer)
8709 struct nlattr *attr;
8713 if (!devlink->ops->trap_policer_counter_get)
8716 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8720 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8724 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8726 goto nla_put_failure;
8728 nla_nest_end(msg, attr);
8733 nla_nest_cancel(msg, attr);
8738 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8739 const struct devlink_trap_policer_item *policer_item,
8740 enum devlink_command cmd, u32 portid, u32 seq,
8746 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8750 if (devlink_nl_put_handle(msg, devlink))
8751 goto nla_put_failure;
8753 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8754 policer_item->policer->id))
8755 goto nla_put_failure;
8757 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8758 policer_item->rate, DEVLINK_ATTR_PAD))
8759 goto nla_put_failure;
8761 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8762 policer_item->burst, DEVLINK_ATTR_PAD))
8763 goto nla_put_failure;
8765 err = devlink_trap_policer_stats_put(msg, devlink,
8766 policer_item->policer);
8768 goto nla_put_failure;
8770 genlmsg_end(msg, hdr);
8775 genlmsg_cancel(msg, hdr);
8779 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8780 struct genl_info *info)
8782 struct devlink_trap_policer_item *policer_item;
8783 struct netlink_ext_ack *extack = info->extack;
8784 struct devlink *devlink = info->user_ptr[0];
8785 struct sk_buff *msg;
8788 if (list_empty(&devlink->trap_policer_list))
8791 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8792 if (!policer_item) {
8793 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8797 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8801 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8802 DEVLINK_CMD_TRAP_POLICER_NEW,
8803 info->snd_portid, info->snd_seq, 0);
8805 goto err_trap_policer_fill;
8807 return genlmsg_reply(msg, info);
8809 err_trap_policer_fill:
8814 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
8815 struct netlink_callback *cb)
8817 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
8818 struct devlink_trap_policer_item *policer_item;
8819 u32 portid = NETLINK_CB(cb->skb).portid;
8820 struct devlink *devlink;
8821 int start = cb->args[0];
8822 unsigned long index;
8826 mutex_lock(&devlink_mutex);
8827 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8828 if (!devlink_try_get(devlink))
8831 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8834 mutex_lock(&devlink->lock);
8835 list_for_each_entry(policer_item, &devlink->trap_policer_list,
8841 err = devlink_nl_trap_policer_fill(msg, devlink,
8847 mutex_unlock(&devlink->lock);
8848 devlink_put(devlink);
8853 mutex_unlock(&devlink->lock);
8855 devlink_put(devlink);
8858 mutex_unlock(&devlink_mutex);
8865 devlink_trap_policer_set(struct devlink *devlink,
8866 struct devlink_trap_policer_item *policer_item,
8867 struct genl_info *info)
8869 struct netlink_ext_ack *extack = info->extack;
8870 struct nlattr **attrs = info->attrs;
8874 rate = policer_item->rate;
8875 burst = policer_item->burst;
8877 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
8878 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
8880 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
8881 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
8883 if (rate < policer_item->policer->min_rate) {
8884 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
8888 if (rate > policer_item->policer->max_rate) {
8889 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
8893 if (burst < policer_item->policer->min_burst) {
8894 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
8898 if (burst > policer_item->policer->max_burst) {
8899 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
8903 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
8904 rate, burst, info->extack);
8908 policer_item->rate = rate;
8909 policer_item->burst = burst;
8914 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
8915 struct genl_info *info)
8917 struct devlink_trap_policer_item *policer_item;
8918 struct netlink_ext_ack *extack = info->extack;
8919 struct devlink *devlink = info->user_ptr[0];
8921 if (list_empty(&devlink->trap_policer_list))
8924 if (!devlink->ops->trap_policer_set)
8927 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8928 if (!policer_item) {
8929 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8933 return devlink_trap_policer_set(devlink, policer_item, info);
8936 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
8937 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
8938 DEVLINK_ATTR_TRAP_POLICER_ID },
8939 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
8940 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
8941 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
8942 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
8943 DEVLINK_PORT_TYPE_IB),
8944 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
8945 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
8946 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
8947 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
8948 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
8949 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
8950 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
8951 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
8952 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
8953 DEVLINK_ESWITCH_MODE_SWITCHDEV),
8954 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
8955 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
8956 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
8957 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
8958 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
8959 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
8960 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
8961 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
8962 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
8963 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
8964 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
8965 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
8966 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
8967 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
8968 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
8969 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
8970 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
8971 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
8972 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
8973 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
8974 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
8975 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
8976 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
8977 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
8978 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
8979 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
8980 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
8981 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
8982 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
8983 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
8984 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
8985 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
8986 DEVLINK_RELOAD_ACTION_MAX),
8987 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
8988 [DEVLINK_ATTR_PORT_FLAVOUR] = { .type = NLA_U16 },
8989 [DEVLINK_ATTR_PORT_PCI_PF_NUMBER] = { .type = NLA_U16 },
8990 [DEVLINK_ATTR_PORT_PCI_SF_NUMBER] = { .type = NLA_U32 },
8991 [DEVLINK_ATTR_PORT_CONTROLLER_NUMBER] = { .type = NLA_U32 },
8992 [DEVLINK_ATTR_RATE_TYPE] = { .type = NLA_U16 },
8993 [DEVLINK_ATTR_RATE_TX_SHARE] = { .type = NLA_U64 },
8994 [DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 },
8995 [DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING },
8996 [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING },
8997 [DEVLINK_ATTR_LINECARD_INDEX] = { .type = NLA_U32 },
8998 [DEVLINK_ATTR_LINECARD_TYPE] = { .type = NLA_NUL_STRING },
9001 static const struct genl_small_ops devlink_nl_ops[] = {
9003 .cmd = DEVLINK_CMD_GET,
9004 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9005 .doit = devlink_nl_cmd_get_doit,
9006 .dumpit = devlink_nl_cmd_get_dumpit,
9007 /* can be retrieved by unprivileged users */
9010 .cmd = DEVLINK_CMD_PORT_GET,
9011 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9012 .doit = devlink_nl_cmd_port_get_doit,
9013 .dumpit = devlink_nl_cmd_port_get_dumpit,
9014 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9015 /* can be retrieved by unprivileged users */
9018 .cmd = DEVLINK_CMD_PORT_SET,
9019 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9020 .doit = devlink_nl_cmd_port_set_doit,
9021 .flags = GENL_ADMIN_PERM,
9022 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9025 .cmd = DEVLINK_CMD_RATE_GET,
9026 .doit = devlink_nl_cmd_rate_get_doit,
9027 .dumpit = devlink_nl_cmd_rate_get_dumpit,
9028 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9029 /* can be retrieved by unprivileged users */
9032 .cmd = DEVLINK_CMD_RATE_SET,
9033 .doit = devlink_nl_cmd_rate_set_doit,
9034 .flags = GENL_ADMIN_PERM,
9035 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9038 .cmd = DEVLINK_CMD_RATE_NEW,
9039 .doit = devlink_nl_cmd_rate_new_doit,
9040 .flags = GENL_ADMIN_PERM,
9043 .cmd = DEVLINK_CMD_RATE_DEL,
9044 .doit = devlink_nl_cmd_rate_del_doit,
9045 .flags = GENL_ADMIN_PERM,
9046 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9049 .cmd = DEVLINK_CMD_PORT_SPLIT,
9050 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9051 .doit = devlink_nl_cmd_port_split_doit,
9052 .flags = GENL_ADMIN_PERM,
9053 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9056 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9057 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9058 .doit = devlink_nl_cmd_port_unsplit_doit,
9059 .flags = GENL_ADMIN_PERM,
9060 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9063 .cmd = DEVLINK_CMD_PORT_NEW,
9064 .doit = devlink_nl_cmd_port_new_doit,
9065 .flags = GENL_ADMIN_PERM,
9066 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
9069 .cmd = DEVLINK_CMD_PORT_DEL,
9070 .doit = devlink_nl_cmd_port_del_doit,
9071 .flags = GENL_ADMIN_PERM,
9072 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
9075 .cmd = DEVLINK_CMD_LINECARD_GET,
9076 .doit = devlink_nl_cmd_linecard_get_doit,
9077 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9078 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9079 /* can be retrieved by unprivileged users */
9082 .cmd = DEVLINK_CMD_LINECARD_SET,
9083 .doit = devlink_nl_cmd_linecard_set_doit,
9084 .flags = GENL_ADMIN_PERM,
9085 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9088 .cmd = DEVLINK_CMD_SB_GET,
9089 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9090 .doit = devlink_nl_cmd_sb_get_doit,
9091 .dumpit = devlink_nl_cmd_sb_get_dumpit,
9092 /* can be retrieved by unprivileged users */
9095 .cmd = DEVLINK_CMD_SB_POOL_GET,
9096 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9097 .doit = devlink_nl_cmd_sb_pool_get_doit,
9098 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
9099 /* can be retrieved by unprivileged users */
9102 .cmd = DEVLINK_CMD_SB_POOL_SET,
9103 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9104 .doit = devlink_nl_cmd_sb_pool_set_doit,
9105 .flags = GENL_ADMIN_PERM,
9108 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9109 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9110 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9111 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
9112 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9113 /* can be retrieved by unprivileged users */
9116 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9117 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9118 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9119 .flags = GENL_ADMIN_PERM,
9120 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9123 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9124 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9125 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9126 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
9127 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9128 /* can be retrieved by unprivileged users */
9131 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9132 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9133 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9134 .flags = GENL_ADMIN_PERM,
9135 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9138 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9139 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9140 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9141 .flags = GENL_ADMIN_PERM,
9144 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9145 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9146 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9147 .flags = GENL_ADMIN_PERM,
9150 .cmd = DEVLINK_CMD_ESWITCH_GET,
9151 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9152 .doit = devlink_nl_cmd_eswitch_get_doit,
9153 .flags = GENL_ADMIN_PERM,
9156 .cmd = DEVLINK_CMD_ESWITCH_SET,
9157 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9158 .doit = devlink_nl_cmd_eswitch_set_doit,
9159 .flags = GENL_ADMIN_PERM,
9162 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9163 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9164 .doit = devlink_nl_cmd_dpipe_table_get,
9165 /* can be retrieved by unprivileged users */
9168 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9169 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9170 .doit = devlink_nl_cmd_dpipe_entries_get,
9171 /* can be retrieved by unprivileged users */
9174 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9175 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9176 .doit = devlink_nl_cmd_dpipe_headers_get,
9177 /* can be retrieved by unprivileged users */
9180 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9181 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9182 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9183 .flags = GENL_ADMIN_PERM,
9186 .cmd = DEVLINK_CMD_RESOURCE_SET,
9187 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9188 .doit = devlink_nl_cmd_resource_set,
9189 .flags = GENL_ADMIN_PERM,
9192 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9193 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9194 .doit = devlink_nl_cmd_resource_dump,
9195 /* can be retrieved by unprivileged users */
9198 .cmd = DEVLINK_CMD_RELOAD,
9199 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9200 .doit = devlink_nl_cmd_reload,
9201 .flags = GENL_ADMIN_PERM,
9202 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
9205 .cmd = DEVLINK_CMD_PARAM_GET,
9206 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9207 .doit = devlink_nl_cmd_param_get_doit,
9208 .dumpit = devlink_nl_cmd_param_get_dumpit,
9209 /* can be retrieved by unprivileged users */
9212 .cmd = DEVLINK_CMD_PARAM_SET,
9213 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9214 .doit = devlink_nl_cmd_param_set_doit,
9215 .flags = GENL_ADMIN_PERM,
9218 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9219 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9220 .doit = devlink_nl_cmd_port_param_get_doit,
9221 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9222 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9223 /* can be retrieved by unprivileged users */
9226 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9227 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9228 .doit = devlink_nl_cmd_port_param_set_doit,
9229 .flags = GENL_ADMIN_PERM,
9230 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9233 .cmd = DEVLINK_CMD_REGION_GET,
9234 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9235 .doit = devlink_nl_cmd_region_get_doit,
9236 .dumpit = devlink_nl_cmd_region_get_dumpit,
9237 .flags = GENL_ADMIN_PERM,
9240 .cmd = DEVLINK_CMD_REGION_NEW,
9241 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9242 .doit = devlink_nl_cmd_region_new,
9243 .flags = GENL_ADMIN_PERM,
9246 .cmd = DEVLINK_CMD_REGION_DEL,
9247 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9248 .doit = devlink_nl_cmd_region_del,
9249 .flags = GENL_ADMIN_PERM,
9252 .cmd = DEVLINK_CMD_REGION_READ,
9253 .validate = GENL_DONT_VALIDATE_STRICT |
9254 GENL_DONT_VALIDATE_DUMP_STRICT,
9255 .dumpit = devlink_nl_cmd_region_read_dumpit,
9256 .flags = GENL_ADMIN_PERM,
9259 .cmd = DEVLINK_CMD_INFO_GET,
9260 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9261 .doit = devlink_nl_cmd_info_get_doit,
9262 .dumpit = devlink_nl_cmd_info_get_dumpit,
9263 /* can be retrieved by unprivileged users */
9266 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9267 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9268 .doit = devlink_nl_cmd_health_reporter_get_doit,
9269 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9270 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9271 DEVLINK_NL_FLAG_NO_LOCK,
9272 /* can be retrieved by unprivileged users */
9275 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9276 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9277 .doit = devlink_nl_cmd_health_reporter_set_doit,
9278 .flags = GENL_ADMIN_PERM,
9279 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9280 DEVLINK_NL_FLAG_NO_LOCK,
9283 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9284 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9285 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9286 .flags = GENL_ADMIN_PERM,
9287 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9288 DEVLINK_NL_FLAG_NO_LOCK,
9291 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9292 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9293 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9294 .flags = GENL_ADMIN_PERM,
9295 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9296 DEVLINK_NL_FLAG_NO_LOCK,
9299 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9300 .validate = GENL_DONT_VALIDATE_STRICT |
9301 GENL_DONT_VALIDATE_DUMP_STRICT,
9302 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9303 .flags = GENL_ADMIN_PERM,
9306 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9307 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9308 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9309 .flags = GENL_ADMIN_PERM,
9310 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9311 DEVLINK_NL_FLAG_NO_LOCK,
9314 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9315 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9316 .doit = devlink_nl_cmd_health_reporter_test_doit,
9317 .flags = GENL_ADMIN_PERM,
9318 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
9319 DEVLINK_NL_FLAG_NO_LOCK,
9322 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9323 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9324 .doit = devlink_nl_cmd_flash_update,
9325 .flags = GENL_ADMIN_PERM,
9328 .cmd = DEVLINK_CMD_TRAP_GET,
9329 .doit = devlink_nl_cmd_trap_get_doit,
9330 .dumpit = devlink_nl_cmd_trap_get_dumpit,
9331 /* can be retrieved by unprivileged users */
9334 .cmd = DEVLINK_CMD_TRAP_SET,
9335 .doit = devlink_nl_cmd_trap_set_doit,
9336 .flags = GENL_ADMIN_PERM,
9339 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9340 .doit = devlink_nl_cmd_trap_group_get_doit,
9341 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
9342 /* can be retrieved by unprivileged users */
9345 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9346 .doit = devlink_nl_cmd_trap_group_set_doit,
9347 .flags = GENL_ADMIN_PERM,
9350 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9351 .doit = devlink_nl_cmd_trap_policer_get_doit,
9352 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
9353 /* can be retrieved by unprivileged users */
9356 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9357 .doit = devlink_nl_cmd_trap_policer_set_doit,
9358 .flags = GENL_ADMIN_PERM,
9362 static struct genl_family devlink_nl_family __ro_after_init = {
9363 .name = DEVLINK_GENL_NAME,
9364 .version = DEVLINK_GENL_VERSION,
9365 .maxattr = DEVLINK_ATTR_MAX,
9366 .policy = devlink_nl_policy,
9368 .pre_doit = devlink_nl_pre_doit,
9369 .post_doit = devlink_nl_post_doit,
9370 .module = THIS_MODULE,
9371 .small_ops = devlink_nl_ops,
9372 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
9373 .mcgrps = devlink_nl_mcgrps,
9374 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
9377 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9379 const struct devlink_reload_combination *comb;
9382 if (!devlink_reload_supported(ops)) {
9383 if (WARN_ON(ops->reload_actions))
9388 if (WARN_ON(!ops->reload_actions ||
9389 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9390 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9393 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9394 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9397 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
9398 comb = &devlink_reload_invalid_combinations[i];
9399 if (ops->reload_actions == BIT(comb->action) &&
9400 ops->reload_limits == BIT(comb->limit))
9407 * devlink_set_features - Set devlink supported features
9410 * @features: devlink support features
9412 * This interface allows us to set reload ops separatelly from
9413 * the devlink_alloc.
9415 void devlink_set_features(struct devlink *devlink, u64 features)
9417 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9419 WARN_ON(features & DEVLINK_F_RELOAD &&
9420 !devlink_reload_supported(devlink->ops));
9421 devlink->features = features;
9423 EXPORT_SYMBOL_GPL(devlink_set_features);
9426 * devlink_alloc_ns - Allocate new devlink instance resources
9427 * in specific namespace
9430 * @priv_size: size of user private data
9431 * @net: net namespace
9432 * @dev: parent device
9434 * Allocate new devlink instance resources, including devlink index
9437 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
9438 size_t priv_size, struct net *net,
9441 struct devlink *devlink;
9445 WARN_ON(!ops || !dev);
9446 if (!devlink_reload_actions_valid(ops))
9449 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
9453 ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
9454 &last_id, GFP_KERNEL);
9462 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
9463 write_pnet(&devlink->_net, net);
9464 INIT_LIST_HEAD(&devlink->port_list);
9465 INIT_LIST_HEAD(&devlink->rate_list);
9466 INIT_LIST_HEAD(&devlink->linecard_list);
9467 INIT_LIST_HEAD(&devlink->sb_list);
9468 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
9469 INIT_LIST_HEAD(&devlink->resource_list);
9470 INIT_LIST_HEAD(&devlink->param_list);
9471 INIT_LIST_HEAD(&devlink->region_list);
9472 INIT_LIST_HEAD(&devlink->reporter_list);
9473 INIT_LIST_HEAD(&devlink->trap_list);
9474 INIT_LIST_HEAD(&devlink->trap_group_list);
9475 INIT_LIST_HEAD(&devlink->trap_policer_list);
9476 mutex_init(&devlink->lock);
9477 mutex_init(&devlink->reporters_lock);
9478 mutex_init(&devlink->linecards_lock);
9479 refcount_set(&devlink->refcount, 1);
9480 init_completion(&devlink->comp);
9484 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
9487 devlink_trap_policer_notify(struct devlink *devlink,
9488 const struct devlink_trap_policer_item *policer_item,
9489 enum devlink_command cmd);
9491 devlink_trap_group_notify(struct devlink *devlink,
9492 const struct devlink_trap_group_item *group_item,
9493 enum devlink_command cmd);
9494 static void devlink_trap_notify(struct devlink *devlink,
9495 const struct devlink_trap_item *trap_item,
9496 enum devlink_command cmd);
9498 static void devlink_notify_register(struct devlink *devlink)
9500 struct devlink_trap_policer_item *policer_item;
9501 struct devlink_trap_group_item *group_item;
9502 struct devlink_param_item *param_item;
9503 struct devlink_trap_item *trap_item;
9504 struct devlink_port *devlink_port;
9505 struct devlink_linecard *linecard;
9506 struct devlink_rate *rate_node;
9507 struct devlink_region *region;
9509 devlink_notify(devlink, DEVLINK_CMD_NEW);
9510 list_for_each_entry(linecard, &devlink->linecard_list, list)
9511 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9513 list_for_each_entry(devlink_port, &devlink->port_list, list)
9514 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9516 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9517 devlink_trap_policer_notify(devlink, policer_item,
9518 DEVLINK_CMD_TRAP_POLICER_NEW);
9520 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9521 devlink_trap_group_notify(devlink, group_item,
9522 DEVLINK_CMD_TRAP_GROUP_NEW);
9524 list_for_each_entry(trap_item, &devlink->trap_list, list)
9525 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9527 list_for_each_entry(rate_node, &devlink->rate_list, list)
9528 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9530 list_for_each_entry(region, &devlink->region_list, list)
9531 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9533 list_for_each_entry(param_item, &devlink->param_list, list)
9534 devlink_param_notify(devlink, 0, param_item,
9535 DEVLINK_CMD_PARAM_NEW);
9538 static void devlink_notify_unregister(struct devlink *devlink)
9540 struct devlink_trap_policer_item *policer_item;
9541 struct devlink_trap_group_item *group_item;
9542 struct devlink_param_item *param_item;
9543 struct devlink_trap_item *trap_item;
9544 struct devlink_port *devlink_port;
9545 struct devlink_rate *rate_node;
9546 struct devlink_region *region;
9548 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9549 devlink_param_notify(devlink, 0, param_item,
9550 DEVLINK_CMD_PARAM_DEL);
9552 list_for_each_entry_reverse(region, &devlink->region_list, list)
9553 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9555 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9556 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9558 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9559 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9561 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9562 devlink_trap_group_notify(devlink, group_item,
9563 DEVLINK_CMD_TRAP_GROUP_DEL);
9564 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9566 devlink_trap_policer_notify(devlink, policer_item,
9567 DEVLINK_CMD_TRAP_POLICER_DEL);
9569 list_for_each_entry_reverse(devlink_port, &devlink->port_list, list)
9570 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9571 devlink_notify(devlink, DEVLINK_CMD_DEL);
9575 * devlink_register - Register devlink instance
9579 void devlink_register(struct devlink *devlink)
9581 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9582 /* Make sure that we are in .probe() routine */
9584 mutex_lock(&devlink_mutex);
9585 xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9586 devlink_notify_register(devlink);
9587 mutex_unlock(&devlink_mutex);
9589 EXPORT_SYMBOL_GPL(devlink_register);
9592 * devlink_unregister - Unregister devlink instance
9596 void devlink_unregister(struct devlink *devlink)
9598 ASSERT_DEVLINK_REGISTERED(devlink);
9599 /* Make sure that we are in .remove() routine */
9601 devlink_put(devlink);
9602 wait_for_completion(&devlink->comp);
9604 mutex_lock(&devlink_mutex);
9605 devlink_notify_unregister(devlink);
9606 xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9607 mutex_unlock(&devlink_mutex);
9609 EXPORT_SYMBOL_GPL(devlink_unregister);
9612 * devlink_free - Free devlink instance resources
9616 void devlink_free(struct devlink *devlink)
9618 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9620 mutex_destroy(&devlink->linecards_lock);
9621 mutex_destroy(&devlink->reporters_lock);
9622 mutex_destroy(&devlink->lock);
9623 WARN_ON(!list_empty(&devlink->trap_policer_list));
9624 WARN_ON(!list_empty(&devlink->trap_group_list));
9625 WARN_ON(!list_empty(&devlink->trap_list));
9626 WARN_ON(!list_empty(&devlink->reporter_list));
9627 WARN_ON(!list_empty(&devlink->region_list));
9628 WARN_ON(!list_empty(&devlink->param_list));
9629 WARN_ON(!list_empty(&devlink->resource_list));
9630 WARN_ON(!list_empty(&devlink->dpipe_table_list));
9631 WARN_ON(!list_empty(&devlink->sb_list));
9632 WARN_ON(!list_empty(&devlink->rate_list));
9633 WARN_ON(!list_empty(&devlink->linecard_list));
9634 WARN_ON(!list_empty(&devlink->port_list));
9636 xa_destroy(&devlink->snapshot_ids);
9637 xa_erase(&devlinks, devlink->index);
9641 EXPORT_SYMBOL_GPL(devlink_free);
9643 static void devlink_port_type_warn(struct work_struct *work)
9645 WARN(true, "Type was not set for devlink port.");
9648 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9650 /* Ignore CPU and DSA flavours. */
9651 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9652 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9653 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9656 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9658 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9660 if (!devlink_port_type_should_warn(devlink_port))
9662 /* Schedule a work to WARN in case driver does not set port
9663 * type within timeout.
9665 schedule_delayed_work(&devlink_port->type_warn_dw,
9666 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9669 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9671 if (!devlink_port_type_should_warn(devlink_port))
9673 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9676 int devl_port_register(struct devlink *devlink,
9677 struct devlink_port *devlink_port,
9678 unsigned int port_index)
9680 lockdep_assert_held(&devlink->lock);
9682 if (devlink_port_index_exists(devlink, port_index))
9685 WARN_ON(devlink_port->devlink);
9686 devlink_port->devlink = devlink;
9687 devlink_port->index = port_index;
9688 spin_lock_init(&devlink_port->type_lock);
9689 INIT_LIST_HEAD(&devlink_port->reporter_list);
9690 mutex_init(&devlink_port->reporters_lock);
9691 list_add_tail(&devlink_port->list, &devlink->port_list);
9692 INIT_LIST_HEAD(&devlink_port->param_list);
9693 INIT_LIST_HEAD(&devlink_port->region_list);
9695 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9696 devlink_port_type_warn_schedule(devlink_port);
9697 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9700 EXPORT_SYMBOL_GPL(devl_port_register);
9703 * devlink_port_register - Register devlink port
9706 * @devlink_port: devlink port
9707 * @port_index: driver-specific numerical identifier of the port
9709 * Register devlink port with provided port index. User can use
9710 * any indexing, even hw-related one. devlink_port structure
9711 * is convenient to be embedded inside user driver private structure.
9712 * Note that the caller should take care of zeroing the devlink_port
9715 int devlink_port_register(struct devlink *devlink,
9716 struct devlink_port *devlink_port,
9717 unsigned int port_index)
9721 mutex_lock(&devlink->lock);
9722 err = devl_port_register(devlink, devlink_port, port_index);
9723 mutex_unlock(&devlink->lock);
9726 EXPORT_SYMBOL_GPL(devlink_port_register);
9728 void devl_port_unregister(struct devlink_port *devlink_port)
9730 lockdep_assert_held(&devlink_port->devlink->lock);
9732 devlink_port_type_warn_cancel(devlink_port);
9733 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9734 list_del(&devlink_port->list);
9735 WARN_ON(!list_empty(&devlink_port->reporter_list));
9736 WARN_ON(!list_empty(&devlink_port->region_list));
9737 mutex_destroy(&devlink_port->reporters_lock);
9739 EXPORT_SYMBOL_GPL(devl_port_unregister);
9742 * devlink_port_unregister - Unregister devlink port
9744 * @devlink_port: devlink port
9746 void devlink_port_unregister(struct devlink_port *devlink_port)
9748 struct devlink *devlink = devlink_port->devlink;
9750 mutex_lock(&devlink->lock);
9751 devl_port_unregister(devlink_port);
9752 mutex_unlock(&devlink->lock);
9754 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9756 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9757 enum devlink_port_type type,
9760 if (WARN_ON(!devlink_port->devlink))
9762 devlink_port_type_warn_cancel(devlink_port);
9763 spin_lock_bh(&devlink_port->type_lock);
9764 devlink_port->type = type;
9765 devlink_port->type_dev = type_dev;
9766 spin_unlock_bh(&devlink_port->type_lock);
9767 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9770 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9771 struct net_device *netdev)
9773 const struct net_device_ops *ops = netdev->netdev_ops;
9775 /* If driver registers devlink port, it should set devlink port
9776 * attributes accordingly so the compat functions are called
9777 * and the original ops are not used.
9779 if (ops->ndo_get_phys_port_name) {
9780 /* Some drivers use the same set of ndos for netdevs
9781 * that have devlink_port registered and also for
9782 * those who don't. Make sure that ndo_get_phys_port_name
9783 * returns -EOPNOTSUPP here in case it is defined.
9786 char name[IFNAMSIZ];
9789 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9790 WARN_ON(err != -EOPNOTSUPP);
9792 if (ops->ndo_get_port_parent_id) {
9793 /* Some drivers use the same set of ndos for netdevs
9794 * that have devlink_port registered and also for
9795 * those who don't. Make sure that ndo_get_port_parent_id
9796 * returns -EOPNOTSUPP here in case it is defined.
9799 struct netdev_phys_item_id ppid;
9802 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9803 WARN_ON(err != -EOPNOTSUPP);
9808 * devlink_port_type_eth_set - Set port type to Ethernet
9810 * @devlink_port: devlink port
9811 * @netdev: related netdevice
9813 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
9814 struct net_device *netdev)
9817 devlink_port_type_netdev_checks(devlink_port, netdev);
9819 dev_warn(devlink_port->devlink->dev,
9820 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9821 devlink_port->index);
9823 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
9825 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9828 * devlink_port_type_ib_set - Set port type to InfiniBand
9830 * @devlink_port: devlink port
9831 * @ibdev: related IB device
9833 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9834 struct ib_device *ibdev)
9836 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9838 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9841 * devlink_port_type_clear - Clear port type
9843 * @devlink_port: devlink port
9845 void devlink_port_type_clear(struct devlink_port *devlink_port)
9847 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9848 devlink_port_type_warn_schedule(devlink_port);
9850 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9852 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9853 enum devlink_port_flavour flavour)
9855 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9857 devlink_port->attrs_set = true;
9858 attrs->flavour = flavour;
9859 if (attrs->switch_id.id_len) {
9860 devlink_port->switch_port = true;
9861 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9862 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9864 devlink_port->switch_port = false;
9870 * devlink_port_attrs_set - Set port attributes
9872 * @devlink_port: devlink port
9873 * @attrs: devlink port attrs
9875 void devlink_port_attrs_set(struct devlink_port *devlink_port,
9876 struct devlink_port_attrs *attrs)
9880 if (WARN_ON(devlink_port->devlink))
9882 devlink_port->attrs = *attrs;
9883 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
9886 WARN_ON(attrs->splittable && attrs->split);
9888 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
9891 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
9893 * @devlink_port: devlink port
9894 * @controller: associated controller number for the devlink port instance
9895 * @pf: associated PF for the devlink port instance
9896 * @external: indicates if the port is for an external controller
9898 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
9899 u16 pf, bool external)
9901 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9904 if (WARN_ON(devlink_port->devlink))
9906 ret = __devlink_port_attrs_set(devlink_port,
9907 DEVLINK_PORT_FLAVOUR_PCI_PF);
9910 attrs->pci_pf.controller = controller;
9911 attrs->pci_pf.pf = pf;
9912 attrs->pci_pf.external = external;
9914 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
9917 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
9919 * @devlink_port: devlink port
9920 * @controller: associated controller number for the devlink port instance
9921 * @pf: associated PF for the devlink port instance
9922 * @vf: associated VF of a PF for the devlink port instance
9923 * @external: indicates if the port is for an external controller
9925 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
9926 u16 pf, u16 vf, bool external)
9928 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9931 if (WARN_ON(devlink_port->devlink))
9933 ret = __devlink_port_attrs_set(devlink_port,
9934 DEVLINK_PORT_FLAVOUR_PCI_VF);
9937 attrs->pci_vf.controller = controller;
9938 attrs->pci_vf.pf = pf;
9939 attrs->pci_vf.vf = vf;
9940 attrs->pci_vf.external = external;
9942 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
9945 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
9947 * @devlink_port: devlink port
9948 * @controller: associated controller number for the devlink port instance
9949 * @pf: associated PF for the devlink port instance
9950 * @sf: associated SF of a PF for the devlink port instance
9951 * @external: indicates if the port is for an external controller
9953 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
9954 u16 pf, u32 sf, bool external)
9956 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9959 if (WARN_ON(devlink_port->devlink))
9961 ret = __devlink_port_attrs_set(devlink_port,
9962 DEVLINK_PORT_FLAVOUR_PCI_SF);
9965 attrs->pci_sf.controller = controller;
9966 attrs->pci_sf.pf = pf;
9967 attrs->pci_sf.sf = sf;
9968 attrs->pci_sf.external = external;
9970 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
9973 * devl_rate_leaf_create - create devlink rate leaf
9974 * @devlink_port: devlink port object to create rate object on
9975 * @priv: driver private data
9977 * Create devlink rate object of type leaf on provided @devlink_port.
9979 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
9981 struct devlink *devlink = devlink_port->devlink;
9982 struct devlink_rate *devlink_rate;
9984 devl_assert_locked(devlink_port->devlink);
9986 if (WARN_ON(devlink_port->devlink_rate))
9989 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
9993 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
9994 devlink_rate->devlink = devlink;
9995 devlink_rate->devlink_port = devlink_port;
9996 devlink_rate->priv = priv;
9997 list_add_tail(&devlink_rate->list, &devlink->rate_list);
9998 devlink_port->devlink_rate = devlink_rate;
9999 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10003 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10006 devlink_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
10008 struct devlink *devlink = devlink_port->devlink;
10011 mutex_lock(&devlink->lock);
10012 ret = devl_rate_leaf_create(devlink_port, priv);
10013 mutex_unlock(&devlink->lock);
10017 EXPORT_SYMBOL_GPL(devlink_rate_leaf_create);
10019 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10021 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10023 devl_assert_locked(devlink_port->devlink);
10027 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10028 if (devlink_rate->parent)
10029 refcount_dec(&devlink_rate->parent->refcnt);
10030 list_del(&devlink_rate->list);
10031 devlink_port->devlink_rate = NULL;
10032 kfree(devlink_rate);
10034 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10037 * devlink_rate_leaf_destroy - destroy devlink rate leaf
10039 * @devlink_port: devlink port linked to the rate object
10041 * Context: Takes and release devlink->lock <mutex>.
10043 void devlink_rate_leaf_destroy(struct devlink_port *devlink_port)
10045 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10046 struct devlink *devlink = devlink_port->devlink;
10051 mutex_lock(&devlink->lock);
10052 devl_rate_leaf_destroy(devlink_port);
10053 mutex_unlock(&devlink->lock);
10055 EXPORT_SYMBOL_GPL(devlink_rate_leaf_destroy);
10058 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10059 * @devlink: devlink instance
10061 * Unset parent for all rate objects and destroy all rate nodes
10062 * on specified device.
10064 void devl_rate_nodes_destroy(struct devlink *devlink)
10066 static struct devlink_rate *devlink_rate, *tmp;
10067 const struct devlink_ops *ops = devlink->ops;
10069 devl_assert_locked(devlink);
10071 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10072 if (!devlink_rate->parent)
10075 refcount_dec(&devlink_rate->parent->refcnt);
10076 if (devlink_rate_is_leaf(devlink_rate))
10077 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10079 else if (devlink_rate_is_node(devlink_rate))
10080 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10083 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10084 if (devlink_rate_is_node(devlink_rate)) {
10085 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10086 list_del(&devlink_rate->list);
10087 kfree(devlink_rate->name);
10088 kfree(devlink_rate);
10092 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10095 * devlink_rate_nodes_destroy - destroy all devlink rate nodes on device
10097 * @devlink: devlink instance
10099 * Unset parent for all rate objects and destroy all rate nodes
10100 * on specified device.
10102 * Context: Takes and release devlink->lock <mutex>.
10104 void devlink_rate_nodes_destroy(struct devlink *devlink)
10106 mutex_lock(&devlink->lock);
10107 devl_rate_nodes_destroy(devlink);
10108 mutex_unlock(&devlink->lock);
10110 EXPORT_SYMBOL_GPL(devlink_rate_nodes_destroy);
10113 * devlink_port_linecard_set - Link port with a linecard
10115 * @devlink_port: devlink port
10116 * @linecard: devlink linecard
10118 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10119 struct devlink_linecard *linecard)
10121 if (WARN_ON(devlink_port->devlink))
10123 devlink_port->linecard = linecard;
10125 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10127 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10128 char *name, size_t len)
10130 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10133 if (!devlink_port->attrs_set)
10134 return -EOPNOTSUPP;
10136 switch (attrs->flavour) {
10137 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10138 if (devlink_port->linecard)
10139 n = snprintf(name, len, "l%u",
10140 devlink_port->linecard->index);
10142 n += snprintf(name + n, len - n, "p%u",
10143 attrs->phys.port_number);
10144 if (n < len && attrs->split)
10145 n += snprintf(name + n, len - n, "s%u",
10146 attrs->phys.split_subport_number);
10148 case DEVLINK_PORT_FLAVOUR_CPU:
10149 case DEVLINK_PORT_FLAVOUR_DSA:
10150 case DEVLINK_PORT_FLAVOUR_UNUSED:
10151 /* As CPU and DSA ports do not have a netdevice associated
10152 * case should not ever happen.
10156 case DEVLINK_PORT_FLAVOUR_PCI_PF:
10157 if (attrs->pci_pf.external) {
10158 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10164 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10166 case DEVLINK_PORT_FLAVOUR_PCI_VF:
10167 if (attrs->pci_vf.external) {
10168 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10174 n = snprintf(name, len, "pf%uvf%u",
10175 attrs->pci_vf.pf, attrs->pci_vf.vf);
10177 case DEVLINK_PORT_FLAVOUR_PCI_SF:
10178 if (attrs->pci_sf.external) {
10179 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10185 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10188 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10189 return -EOPNOTSUPP;
10198 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10200 struct devlink_linecard_type *linecard_type;
10201 unsigned int count;
10204 count = linecard->ops->types_count(linecard, linecard->priv);
10205 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10207 if (!linecard->types)
10209 linecard->types_count = count;
10211 for (i = 0; i < count; i++) {
10212 linecard_type = &linecard->types[i];
10213 linecard->ops->types_get(linecard, linecard->priv, i,
10214 &linecard_type->type,
10215 &linecard_type->priv);
10220 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10222 kfree(linecard->types);
10226 * devlink_linecard_create - Create devlink linecard
10228 * @devlink: devlink
10229 * @linecard_index: driver-specific numerical identifier of the linecard
10230 * @ops: linecards ops
10231 * @priv: user priv pointer
10233 * Create devlink linecard instance with provided linecard index.
10234 * Caller can use any indexing, even hw-related one.
10236 * Return: Line card structure or an ERR_PTR() encoded error code.
10238 struct devlink_linecard *
10239 devlink_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10240 const struct devlink_linecard_ops *ops, void *priv)
10242 struct devlink_linecard *linecard;
10245 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10246 !ops->types_count || !ops->types_get))
10247 return ERR_PTR(-EINVAL);
10249 mutex_lock(&devlink->linecards_lock);
10250 if (devlink_linecard_index_exists(devlink, linecard_index)) {
10251 mutex_unlock(&devlink->linecards_lock);
10252 return ERR_PTR(-EEXIST);
10255 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10257 mutex_unlock(&devlink->linecards_lock);
10258 return ERR_PTR(-ENOMEM);
10261 linecard->devlink = devlink;
10262 linecard->index = linecard_index;
10263 linecard->ops = ops;
10264 linecard->priv = priv;
10265 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10266 mutex_init(&linecard->state_lock);
10268 err = devlink_linecard_types_init(linecard);
10270 mutex_destroy(&linecard->state_lock);
10272 mutex_unlock(&devlink->linecards_lock);
10273 return ERR_PTR(err);
10276 list_add_tail(&linecard->list, &devlink->linecard_list);
10277 refcount_set(&linecard->refcount, 1);
10278 mutex_unlock(&devlink->linecards_lock);
10279 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10282 EXPORT_SYMBOL_GPL(devlink_linecard_create);
10285 * devlink_linecard_destroy - Destroy devlink linecard
10287 * @linecard: devlink linecard
10289 void devlink_linecard_destroy(struct devlink_linecard *linecard)
10291 struct devlink *devlink = linecard->devlink;
10293 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10294 mutex_lock(&devlink->linecards_lock);
10295 list_del(&linecard->list);
10296 devlink_linecard_types_fini(linecard);
10297 mutex_unlock(&devlink->linecards_lock);
10298 devlink_linecard_put(linecard);
10300 EXPORT_SYMBOL_GPL(devlink_linecard_destroy);
10303 * devlink_linecard_provision_set - Set provisioning on linecard
10305 * @linecard: devlink linecard
10306 * @type: linecard type
10308 * This is either called directly from the provision() op call or
10309 * as a result of the provision() op call asynchronously.
10311 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10314 mutex_lock(&linecard->state_lock);
10315 WARN_ON(linecard->type && strcmp(linecard->type, type));
10316 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10317 linecard->type = type;
10318 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10319 mutex_unlock(&linecard->state_lock);
10321 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10324 * devlink_linecard_provision_clear - Clear provisioning on linecard
10326 * @linecard: devlink linecard
10328 * This is either called directly from the unprovision() op call or
10329 * as a result of the unprovision() op call asynchronously.
10331 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10333 mutex_lock(&linecard->state_lock);
10334 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10335 linecard->type = NULL;
10336 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10337 mutex_unlock(&linecard->state_lock);
10339 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10342 * devlink_linecard_provision_fail - Fail provisioning on linecard
10344 * @linecard: devlink linecard
10346 * This is either called directly from the provision() op call or
10347 * as a result of the provision() op call asynchronously.
10349 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10351 mutex_lock(&linecard->state_lock);
10352 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10353 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10354 mutex_unlock(&linecard->state_lock);
10356 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10359 * devlink_linecard_activate - Set linecard active
10361 * @linecard: devlink linecard
10363 void devlink_linecard_activate(struct devlink_linecard *linecard)
10365 mutex_lock(&linecard->state_lock);
10366 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10367 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10368 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10369 mutex_unlock(&linecard->state_lock);
10371 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10374 * devlink_linecard_deactivate - Set linecard inactive
10376 * @linecard: devlink linecard
10378 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10380 mutex_lock(&linecard->state_lock);
10381 switch (linecard->state) {
10382 case DEVLINK_LINECARD_STATE_ACTIVE:
10383 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10384 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10386 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10387 /* Line card is being deactivated as part
10388 * of unprovisioning flow.
10395 mutex_unlock(&linecard->state_lock);
10397 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10399 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10400 u32 size, u16 ingress_pools_count,
10401 u16 egress_pools_count, u16 ingress_tc_count,
10402 u16 egress_tc_count)
10404 struct devlink_sb *devlink_sb;
10407 mutex_lock(&devlink->lock);
10408 if (devlink_sb_index_exists(devlink, sb_index)) {
10413 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10418 devlink_sb->index = sb_index;
10419 devlink_sb->size = size;
10420 devlink_sb->ingress_pools_count = ingress_pools_count;
10421 devlink_sb->egress_pools_count = egress_pools_count;
10422 devlink_sb->ingress_tc_count = ingress_tc_count;
10423 devlink_sb->egress_tc_count = egress_tc_count;
10424 list_add_tail(&devlink_sb->list, &devlink->sb_list);
10426 mutex_unlock(&devlink->lock);
10429 EXPORT_SYMBOL_GPL(devlink_sb_register);
10431 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10433 struct devlink_sb *devlink_sb;
10435 mutex_lock(&devlink->lock);
10436 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10437 WARN_ON(!devlink_sb);
10438 list_del(&devlink_sb->list);
10439 mutex_unlock(&devlink->lock);
10442 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10445 * devlink_dpipe_headers_register - register dpipe headers
10447 * @devlink: devlink
10448 * @dpipe_headers: dpipe header array
10450 * Register the headers supported by hardware.
10452 int devlink_dpipe_headers_register(struct devlink *devlink,
10453 struct devlink_dpipe_headers *dpipe_headers)
10455 mutex_lock(&devlink->lock);
10456 devlink->dpipe_headers = dpipe_headers;
10457 mutex_unlock(&devlink->lock);
10460 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
10463 * devlink_dpipe_headers_unregister - unregister dpipe headers
10465 * @devlink: devlink
10467 * Unregister the headers supported by hardware.
10469 void devlink_dpipe_headers_unregister(struct devlink *devlink)
10471 mutex_lock(&devlink->lock);
10472 devlink->dpipe_headers = NULL;
10473 mutex_unlock(&devlink->lock);
10475 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
10478 * devlink_dpipe_table_counter_enabled - check if counter allocation
10480 * @devlink: devlink
10481 * @table_name: tables name
10483 * Used by driver to check if counter allocation is required.
10484 * After counter allocation is turned on the table entries
10485 * are updated to include counter statistics.
10487 * After that point on the driver must respect the counter
10488 * state so that each entry added to the table is added
10491 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10492 const char *table_name)
10494 struct devlink_dpipe_table *table;
10498 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10499 table_name, devlink);
10502 enabled = table->counters_enabled;
10506 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10509 * devlink_dpipe_table_register - register dpipe table
10511 * @devlink: devlink
10512 * @table_name: table name
10513 * @table_ops: table ops
10515 * @counter_control_extern: external control for counters
10517 int devlink_dpipe_table_register(struct devlink *devlink,
10518 const char *table_name,
10519 struct devlink_dpipe_table_ops *table_ops,
10520 void *priv, bool counter_control_extern)
10522 struct devlink_dpipe_table *table;
10525 if (WARN_ON(!table_ops->size_get))
10528 mutex_lock(&devlink->lock);
10530 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10536 table = kzalloc(sizeof(*table), GFP_KERNEL);
10542 table->name = table_name;
10543 table->table_ops = table_ops;
10544 table->priv = priv;
10545 table->counter_control_extern = counter_control_extern;
10547 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10549 mutex_unlock(&devlink->lock);
10552 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
10555 * devlink_dpipe_table_unregister - unregister dpipe table
10557 * @devlink: devlink
10558 * @table_name: table name
10560 void devlink_dpipe_table_unregister(struct devlink *devlink,
10561 const char *table_name)
10563 struct devlink_dpipe_table *table;
10565 mutex_lock(&devlink->lock);
10566 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10567 table_name, devlink);
10570 list_del_rcu(&table->list);
10571 mutex_unlock(&devlink->lock);
10572 kfree_rcu(table, rcu);
10575 mutex_unlock(&devlink->lock);
10577 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
10580 * devlink_resource_register - devlink resource register
10582 * @devlink: devlink
10583 * @resource_name: resource's name
10584 * @resource_size: resource's size
10585 * @resource_id: resource's id
10586 * @parent_resource_id: resource's parent id
10587 * @size_params: size parameters
10589 * Generic resources should reuse the same names across drivers.
10590 * Please see the generic resources list at:
10591 * Documentation/networking/devlink/devlink-resource.rst
10593 int devlink_resource_register(struct devlink *devlink,
10594 const char *resource_name,
10597 u64 parent_resource_id,
10598 const struct devlink_resource_size_params *size_params)
10600 struct devlink_resource *resource;
10601 struct list_head *resource_list;
10602 bool top_hierarchy;
10605 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10607 mutex_lock(&devlink->lock);
10608 resource = devlink_resource_find(devlink, NULL, resource_id);
10614 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10620 if (top_hierarchy) {
10621 resource_list = &devlink->resource_list;
10623 struct devlink_resource *parent_resource;
10625 parent_resource = devlink_resource_find(devlink, NULL,
10626 parent_resource_id);
10627 if (parent_resource) {
10628 resource_list = &parent_resource->resource_list;
10629 resource->parent = parent_resource;
10637 resource->name = resource_name;
10638 resource->size = resource_size;
10639 resource->size_new = resource_size;
10640 resource->id = resource_id;
10641 resource->size_valid = true;
10642 memcpy(&resource->size_params, size_params,
10643 sizeof(resource->size_params));
10644 INIT_LIST_HEAD(&resource->resource_list);
10645 list_add_tail(&resource->list, resource_list);
10647 mutex_unlock(&devlink->lock);
10650 EXPORT_SYMBOL_GPL(devlink_resource_register);
10652 static void devlink_resource_unregister(struct devlink *devlink,
10653 struct devlink_resource *resource)
10655 struct devlink_resource *tmp, *child_resource;
10657 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10659 devlink_resource_unregister(devlink, child_resource);
10660 list_del(&child_resource->list);
10661 kfree(child_resource);
10666 * devlink_resources_unregister - free all resources
10668 * @devlink: devlink
10670 void devlink_resources_unregister(struct devlink *devlink)
10672 struct devlink_resource *tmp, *child_resource;
10674 mutex_lock(&devlink->lock);
10676 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10678 devlink_resource_unregister(devlink, child_resource);
10679 list_del(&child_resource->list);
10680 kfree(child_resource);
10683 mutex_unlock(&devlink->lock);
10685 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10688 * devlink_resource_size_get - get and update size
10690 * @devlink: devlink
10691 * @resource_id: the requested resource id
10692 * @p_resource_size: ptr to update
10694 int devlink_resource_size_get(struct devlink *devlink,
10696 u64 *p_resource_size)
10698 struct devlink_resource *resource;
10701 mutex_lock(&devlink->lock);
10702 resource = devlink_resource_find(devlink, NULL, resource_id);
10707 *p_resource_size = resource->size_new;
10708 resource->size = resource->size_new;
10710 mutex_unlock(&devlink->lock);
10713 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
10716 * devlink_dpipe_table_resource_set - set the resource id
10718 * @devlink: devlink
10719 * @table_name: table name
10720 * @resource_id: resource id
10721 * @resource_units: number of resource's units consumed per table's entry
10723 int devlink_dpipe_table_resource_set(struct devlink *devlink,
10724 const char *table_name, u64 resource_id,
10725 u64 resource_units)
10727 struct devlink_dpipe_table *table;
10730 mutex_lock(&devlink->lock);
10731 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10732 table_name, devlink);
10737 table->resource_id = resource_id;
10738 table->resource_units = resource_units;
10739 table->resource_valid = true;
10741 mutex_unlock(&devlink->lock);
10744 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
10747 * devlink_resource_occ_get_register - register occupancy getter
10749 * @devlink: devlink
10750 * @resource_id: resource id
10751 * @occ_get: occupancy getter callback
10752 * @occ_get_priv: occupancy getter callback priv
10754 void devlink_resource_occ_get_register(struct devlink *devlink,
10756 devlink_resource_occ_get_t *occ_get,
10757 void *occ_get_priv)
10759 struct devlink_resource *resource;
10761 mutex_lock(&devlink->lock);
10762 resource = devlink_resource_find(devlink, NULL, resource_id);
10763 if (WARN_ON(!resource))
10765 WARN_ON(resource->occ_get);
10767 resource->occ_get = occ_get;
10768 resource->occ_get_priv = occ_get_priv;
10770 mutex_unlock(&devlink->lock);
10772 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10775 * devlink_resource_occ_get_unregister - unregister occupancy getter
10777 * @devlink: devlink
10778 * @resource_id: resource id
10780 void devlink_resource_occ_get_unregister(struct devlink *devlink,
10783 struct devlink_resource *resource;
10785 mutex_lock(&devlink->lock);
10786 resource = devlink_resource_find(devlink, NULL, resource_id);
10787 if (WARN_ON(!resource))
10789 WARN_ON(!resource->occ_get);
10791 resource->occ_get = NULL;
10792 resource->occ_get_priv = NULL;
10794 mutex_unlock(&devlink->lock);
10796 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
10798 static int devlink_param_verify(const struct devlink_param *param)
10800 if (!param || !param->name || !param->supported_cmodes)
10802 if (param->generic)
10803 return devlink_param_generic_verify(param);
10805 return devlink_param_driver_verify(param);
10809 * devlink_params_register - register configuration parameters
10811 * @devlink: devlink
10812 * @params: configuration parameters array
10813 * @params_count: number of parameters provided
10815 * Register the configuration parameters supported by the driver.
10817 int devlink_params_register(struct devlink *devlink,
10818 const struct devlink_param *params,
10819 size_t params_count)
10821 const struct devlink_param *param = params;
10824 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10826 for (i = 0; i < params_count; i++, param++) {
10827 err = devlink_param_register(devlink, param);
10837 for (param--; i > 0; i--, param--)
10838 devlink_param_unregister(devlink, param);
10841 EXPORT_SYMBOL_GPL(devlink_params_register);
10844 * devlink_params_unregister - unregister configuration parameters
10845 * @devlink: devlink
10846 * @params: configuration parameters to unregister
10847 * @params_count: number of parameters provided
10849 void devlink_params_unregister(struct devlink *devlink,
10850 const struct devlink_param *params,
10851 size_t params_count)
10853 const struct devlink_param *param = params;
10856 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10858 for (i = 0; i < params_count; i++, param++)
10859 devlink_param_unregister(devlink, param);
10861 EXPORT_SYMBOL_GPL(devlink_params_unregister);
10864 * devlink_param_register - register one configuration parameter
10866 * @devlink: devlink
10867 * @param: one configuration parameter
10869 * Register the configuration parameter supported by the driver.
10870 * Return: returns 0 on successful registration or error code otherwise.
10872 int devlink_param_register(struct devlink *devlink,
10873 const struct devlink_param *param)
10875 struct devlink_param_item *param_item;
10877 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10879 WARN_ON(devlink_param_verify(param));
10880 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
10882 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
10883 WARN_ON(param->get || param->set);
10885 WARN_ON(!param->get || !param->set);
10887 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
10891 param_item->param = param;
10893 list_add_tail(¶m_item->list, &devlink->param_list);
10896 EXPORT_SYMBOL_GPL(devlink_param_register);
10899 * devlink_param_unregister - unregister one configuration parameter
10900 * @devlink: devlink
10901 * @param: configuration parameter to unregister
10903 void devlink_param_unregister(struct devlink *devlink,
10904 const struct devlink_param *param)
10906 struct devlink_param_item *param_item;
10908 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10911 devlink_param_find_by_name(&devlink->param_list, param->name);
10912 WARN_ON(!param_item);
10913 list_del(¶m_item->list);
10916 EXPORT_SYMBOL_GPL(devlink_param_unregister);
10919 * devlink_param_driverinit_value_get - get configuration parameter
10920 * value for driver initializing
10922 * @devlink: devlink
10923 * @param_id: parameter ID
10924 * @init_val: value of parameter in driverinit configuration mode
10926 * This function should be used by the driver to get driverinit
10927 * configuration for initialization after reload command.
10929 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
10930 union devlink_param_value *init_val)
10932 struct devlink_param_item *param_item;
10934 if (!devlink_reload_supported(devlink->ops))
10935 return -EOPNOTSUPP;
10937 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10941 if (!param_item->driverinit_value_valid ||
10942 !devlink_param_cmode_is_supported(param_item->param,
10943 DEVLINK_PARAM_CMODE_DRIVERINIT))
10944 return -EOPNOTSUPP;
10946 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10947 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
10949 *init_val = param_item->driverinit_value;
10953 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
10956 * devlink_param_driverinit_value_set - set value of configuration
10957 * parameter for driverinit
10958 * configuration mode
10960 * @devlink: devlink
10961 * @param_id: parameter ID
10962 * @init_val: value of parameter to set for driverinit configuration mode
10964 * This function should be used by the driver to set driverinit
10965 * configuration mode default value.
10967 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
10968 union devlink_param_value init_val)
10970 struct devlink_param_item *param_item;
10972 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10974 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10978 if (!devlink_param_cmode_is_supported(param_item->param,
10979 DEVLINK_PARAM_CMODE_DRIVERINIT))
10980 return -EOPNOTSUPP;
10982 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10983 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
10985 param_item->driverinit_value = init_val;
10986 param_item->driverinit_value_valid = true;
10989 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
10992 * devlink_param_value_changed - notify devlink on a parameter's value
10993 * change. Should be called by the driver
10994 * right after the change.
10996 * @devlink: devlink
10997 * @param_id: parameter ID
10999 * This function should be used by the driver to notify devlink on value
11000 * change, excluding driverinit configuration mode.
11001 * For driverinit configuration mode driver should use the function
11003 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11005 struct devlink_param_item *param_item;
11007 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11008 WARN_ON(!param_item);
11010 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11012 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11015 * devlink_region_create - create a new address region
11017 * @devlink: devlink
11018 * @ops: region operations and name
11019 * @region_max_snapshots: Maximum supported number of snapshots for region
11020 * @region_size: size of region
11022 struct devlink_region *
11023 devlink_region_create(struct devlink *devlink,
11024 const struct devlink_region_ops *ops,
11025 u32 region_max_snapshots, u64 region_size)
11027 struct devlink_region *region;
11030 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11031 return ERR_PTR(-EINVAL);
11033 mutex_lock(&devlink->lock);
11035 if (devlink_region_get_by_name(devlink, ops->name)) {
11040 region = kzalloc(sizeof(*region), GFP_KERNEL);
11046 region->devlink = devlink;
11047 region->max_snapshots = region_max_snapshots;
11049 region->size = region_size;
11050 INIT_LIST_HEAD(®ion->snapshot_list);
11051 list_add_tail(®ion->list, &devlink->region_list);
11052 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11054 mutex_unlock(&devlink->lock);
11058 mutex_unlock(&devlink->lock);
11059 return ERR_PTR(err);
11061 EXPORT_SYMBOL_GPL(devlink_region_create);
11064 * devlink_port_region_create - create a new address region for a port
11066 * @port: devlink port
11067 * @ops: region operations and name
11068 * @region_max_snapshots: Maximum supported number of snapshots for region
11069 * @region_size: size of region
11071 struct devlink_region *
11072 devlink_port_region_create(struct devlink_port *port,
11073 const struct devlink_port_region_ops *ops,
11074 u32 region_max_snapshots, u64 region_size)
11076 struct devlink *devlink = port->devlink;
11077 struct devlink_region *region;
11080 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11081 return ERR_PTR(-EINVAL);
11083 mutex_lock(&devlink->lock);
11085 if (devlink_port_region_get_by_name(port, ops->name)) {
11090 region = kzalloc(sizeof(*region), GFP_KERNEL);
11096 region->devlink = devlink;
11097 region->port = port;
11098 region->max_snapshots = region_max_snapshots;
11099 region->port_ops = ops;
11100 region->size = region_size;
11101 INIT_LIST_HEAD(®ion->snapshot_list);
11102 list_add_tail(®ion->list, &port->region_list);
11103 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11105 mutex_unlock(&devlink->lock);
11109 mutex_unlock(&devlink->lock);
11110 return ERR_PTR(err);
11112 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11115 * devlink_region_destroy - destroy address region
11117 * @region: devlink region to destroy
11119 void devlink_region_destroy(struct devlink_region *region)
11121 struct devlink *devlink = region->devlink;
11122 struct devlink_snapshot *snapshot, *ts;
11124 mutex_lock(&devlink->lock);
11126 /* Free all snapshots of region */
11127 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
11128 devlink_region_snapshot_del(region, snapshot);
11130 list_del(®ion->list);
11132 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11133 mutex_unlock(&devlink->lock);
11136 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11139 * devlink_region_snapshot_id_get - get snapshot ID
11141 * This callback should be called when adding a new snapshot,
11142 * Driver should use the same id for multiple snapshots taken
11143 * on multiple regions at the same time/by the same trigger.
11145 * The caller of this function must use devlink_region_snapshot_id_put
11146 * when finished creating regions using this id.
11148 * Returns zero on success, or a negative error code on failure.
11150 * @devlink: devlink
11151 * @id: storage to return id
11153 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11157 mutex_lock(&devlink->lock);
11158 err = __devlink_region_snapshot_id_get(devlink, id);
11159 mutex_unlock(&devlink->lock);
11163 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11166 * devlink_region_snapshot_id_put - put snapshot ID reference
11168 * This should be called by a driver after finishing creating snapshots
11169 * with an id. Doing so ensures that the ID can later be released in the
11170 * event that all snapshots using it have been destroyed.
11172 * @devlink: devlink
11173 * @id: id to release reference on
11175 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11177 mutex_lock(&devlink->lock);
11178 __devlink_snapshot_id_decrement(devlink, id);
11179 mutex_unlock(&devlink->lock);
11181 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11184 * devlink_region_snapshot_create - create a new snapshot
11185 * This will add a new snapshot of a region. The snapshot
11186 * will be stored on the region struct and can be accessed
11187 * from devlink. This is useful for future analyses of snapshots.
11188 * Multiple snapshots can be created on a region.
11189 * The @snapshot_id should be obtained using the getter function.
11191 * @region: devlink region of the snapshot
11192 * @data: snapshot data
11193 * @snapshot_id: snapshot id to be created
11195 int devlink_region_snapshot_create(struct devlink_region *region,
11196 u8 *data, u32 snapshot_id)
11198 struct devlink *devlink = region->devlink;
11201 mutex_lock(&devlink->lock);
11202 err = __devlink_region_snapshot_create(region, data, snapshot_id);
11203 mutex_unlock(&devlink->lock);
11207 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11209 #define DEVLINK_TRAP(_id, _type) \
11211 .type = DEVLINK_TRAP_TYPE_##_type, \
11212 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
11213 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
11216 static const struct devlink_trap devlink_trap_generic[] = {
11217 DEVLINK_TRAP(SMAC_MC, DROP),
11218 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11219 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11220 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11221 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11222 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11223 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11224 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11225 DEVLINK_TRAP(TAIL_DROP, DROP),
11226 DEVLINK_TRAP(NON_IP_PACKET, DROP),
11227 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11228 DEVLINK_TRAP(DIP_LB, DROP),
11229 DEVLINK_TRAP(SIP_MC, DROP),
11230 DEVLINK_TRAP(SIP_LB, DROP),
11231 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11232 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11233 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11234 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11235 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11236 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11237 DEVLINK_TRAP(RPF, EXCEPTION),
11238 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11239 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11240 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11241 DEVLINK_TRAP(NON_ROUTABLE, DROP),
11242 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11243 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11244 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11245 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11246 DEVLINK_TRAP(STP, CONTROL),
11247 DEVLINK_TRAP(LACP, CONTROL),
11248 DEVLINK_TRAP(LLDP, CONTROL),
11249 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11250 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11251 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11252 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11253 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11254 DEVLINK_TRAP(MLD_QUERY, CONTROL),
11255 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11256 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11257 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11258 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11259 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11260 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11261 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11262 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11263 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11264 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11265 DEVLINK_TRAP(IPV4_BFD, CONTROL),
11266 DEVLINK_TRAP(IPV6_BFD, CONTROL),
11267 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11268 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11269 DEVLINK_TRAP(IPV4_BGP, CONTROL),
11270 DEVLINK_TRAP(IPV6_BGP, CONTROL),
11271 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11272 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11273 DEVLINK_TRAP(IPV4_PIM, CONTROL),
11274 DEVLINK_TRAP(IPV6_PIM, CONTROL),
11275 DEVLINK_TRAP(UC_LB, CONTROL),
11276 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11277 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11278 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11279 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11280 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11281 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11282 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11283 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11284 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11285 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11286 DEVLINK_TRAP(PTP_EVENT, CONTROL),
11287 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11288 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11289 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11290 DEVLINK_TRAP(EARLY_DROP, DROP),
11291 DEVLINK_TRAP(VXLAN_PARSING, DROP),
11292 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11293 DEVLINK_TRAP(VLAN_PARSING, DROP),
11294 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11295 DEVLINK_TRAP(MPLS_PARSING, DROP),
11296 DEVLINK_TRAP(ARP_PARSING, DROP),
11297 DEVLINK_TRAP(IP_1_PARSING, DROP),
11298 DEVLINK_TRAP(IP_N_PARSING, DROP),
11299 DEVLINK_TRAP(GRE_PARSING, DROP),
11300 DEVLINK_TRAP(UDP_PARSING, DROP),
11301 DEVLINK_TRAP(TCP_PARSING, DROP),
11302 DEVLINK_TRAP(IPSEC_PARSING, DROP),
11303 DEVLINK_TRAP(SCTP_PARSING, DROP),
11304 DEVLINK_TRAP(DCCP_PARSING, DROP),
11305 DEVLINK_TRAP(GTP_PARSING, DROP),
11306 DEVLINK_TRAP(ESP_PARSING, DROP),
11307 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11308 DEVLINK_TRAP(DMAC_FILTER, DROP),
11311 #define DEVLINK_TRAP_GROUP(_id) \
11313 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
11314 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
11317 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11318 DEVLINK_TRAP_GROUP(L2_DROPS),
11319 DEVLINK_TRAP_GROUP(L3_DROPS),
11320 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11321 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11322 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11323 DEVLINK_TRAP_GROUP(ACL_DROPS),
11324 DEVLINK_TRAP_GROUP(STP),
11325 DEVLINK_TRAP_GROUP(LACP),
11326 DEVLINK_TRAP_GROUP(LLDP),
11327 DEVLINK_TRAP_GROUP(MC_SNOOPING),
11328 DEVLINK_TRAP_GROUP(DHCP),
11329 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11330 DEVLINK_TRAP_GROUP(BFD),
11331 DEVLINK_TRAP_GROUP(OSPF),
11332 DEVLINK_TRAP_GROUP(BGP),
11333 DEVLINK_TRAP_GROUP(VRRP),
11334 DEVLINK_TRAP_GROUP(PIM),
11335 DEVLINK_TRAP_GROUP(UC_LB),
11336 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11337 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11338 DEVLINK_TRAP_GROUP(IPV6),
11339 DEVLINK_TRAP_GROUP(PTP_EVENT),
11340 DEVLINK_TRAP_GROUP(PTP_GENERAL),
11341 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11342 DEVLINK_TRAP_GROUP(ACL_TRAP),
11343 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11346 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11348 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11351 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11354 if (trap->type != devlink_trap_generic[trap->id].type)
11360 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11364 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11367 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11368 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11375 static int devlink_trap_verify(const struct devlink_trap *trap)
11377 if (!trap || !trap->name)
11381 return devlink_trap_generic_verify(trap);
11383 return devlink_trap_driver_verify(trap);
11387 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11389 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11392 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11399 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11403 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11406 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11407 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11414 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11416 if (group->generic)
11417 return devlink_trap_group_generic_verify(group);
11419 return devlink_trap_group_driver_verify(group);
11423 devlink_trap_group_notify(struct devlink *devlink,
11424 const struct devlink_trap_group_item *group_item,
11425 enum devlink_command cmd)
11427 struct sk_buff *msg;
11430 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11431 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11432 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11435 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11439 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11446 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11447 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11451 devlink_trap_item_group_link(struct devlink *devlink,
11452 struct devlink_trap_item *trap_item)
11454 u16 group_id = trap_item->trap->init_group_id;
11455 struct devlink_trap_group_item *group_item;
11457 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11458 if (WARN_ON_ONCE(!group_item))
11461 trap_item->group_item = group_item;
11466 static void devlink_trap_notify(struct devlink *devlink,
11467 const struct devlink_trap_item *trap_item,
11468 enum devlink_command cmd)
11470 struct sk_buff *msg;
11473 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11474 cmd != DEVLINK_CMD_TRAP_DEL);
11475 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11478 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11482 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11488 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11489 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11493 devlink_trap_register(struct devlink *devlink,
11494 const struct devlink_trap *trap, void *priv)
11496 struct devlink_trap_item *trap_item;
11499 if (devlink_trap_item_lookup(devlink, trap->name))
11502 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11506 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11507 if (!trap_item->stats) {
11509 goto err_stats_alloc;
11512 trap_item->trap = trap;
11513 trap_item->action = trap->init_action;
11514 trap_item->priv = priv;
11516 err = devlink_trap_item_group_link(devlink, trap_item);
11518 goto err_group_link;
11520 err = devlink->ops->trap_init(devlink, trap, trap_item);
11522 goto err_trap_init;
11524 list_add_tail(&trap_item->list, &devlink->trap_list);
11525 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11531 free_percpu(trap_item->stats);
11537 static void devlink_trap_unregister(struct devlink *devlink,
11538 const struct devlink_trap *trap)
11540 struct devlink_trap_item *trap_item;
11542 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11543 if (WARN_ON_ONCE(!trap_item))
11546 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11547 list_del(&trap_item->list);
11548 if (devlink->ops->trap_fini)
11549 devlink->ops->trap_fini(devlink, trap, trap_item);
11550 free_percpu(trap_item->stats);
11554 static void devlink_trap_disable(struct devlink *devlink,
11555 const struct devlink_trap *trap)
11557 struct devlink_trap_item *trap_item;
11559 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11560 if (WARN_ON_ONCE(!trap_item))
11563 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11565 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11569 * devlink_traps_register - Register packet traps with devlink.
11570 * @devlink: devlink.
11571 * @traps: Packet traps.
11572 * @traps_count: Count of provided packet traps.
11573 * @priv: Driver private information.
11575 * Return: Non-zero value on failure.
11577 int devlink_traps_register(struct devlink *devlink,
11578 const struct devlink_trap *traps,
11579 size_t traps_count, void *priv)
11583 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11586 mutex_lock(&devlink->lock);
11587 for (i = 0; i < traps_count; i++) {
11588 const struct devlink_trap *trap = &traps[i];
11590 err = devlink_trap_verify(trap);
11592 goto err_trap_verify;
11594 err = devlink_trap_register(devlink, trap, priv);
11596 goto err_trap_register;
11598 mutex_unlock(&devlink->lock);
11604 for (i--; i >= 0; i--)
11605 devlink_trap_unregister(devlink, &traps[i]);
11606 mutex_unlock(&devlink->lock);
11609 EXPORT_SYMBOL_GPL(devlink_traps_register);
11612 * devlink_traps_unregister - Unregister packet traps from devlink.
11613 * @devlink: devlink.
11614 * @traps: Packet traps.
11615 * @traps_count: Count of provided packet traps.
11617 void devlink_traps_unregister(struct devlink *devlink,
11618 const struct devlink_trap *traps,
11619 size_t traps_count)
11623 mutex_lock(&devlink->lock);
11624 /* Make sure we do not have any packets in-flight while unregistering
11625 * traps by disabling all of them and waiting for a grace period.
11627 for (i = traps_count - 1; i >= 0; i--)
11628 devlink_trap_disable(devlink, &traps[i]);
11630 for (i = traps_count - 1; i >= 0; i--)
11631 devlink_trap_unregister(devlink, &traps[i]);
11632 mutex_unlock(&devlink->lock);
11634 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11637 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11640 struct devlink_stats *stats;
11642 stats = this_cpu_ptr(trap_stats);
11643 u64_stats_update_begin(&stats->syncp);
11644 stats->rx_bytes += skb_len;
11645 stats->rx_packets++;
11646 u64_stats_update_end(&stats->syncp);
11650 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11651 const struct devlink_trap_item *trap_item,
11652 struct devlink_port *in_devlink_port,
11653 const struct flow_action_cookie *fa_cookie)
11655 metadata->trap_name = trap_item->trap->name;
11656 metadata->trap_group_name = trap_item->group_item->group->name;
11657 metadata->fa_cookie = fa_cookie;
11658 metadata->trap_type = trap_item->trap->type;
11660 spin_lock(&in_devlink_port->type_lock);
11661 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11662 metadata->input_dev = in_devlink_port->type_dev;
11663 spin_unlock(&in_devlink_port->type_lock);
11667 * devlink_trap_report - Report trapped packet to drop monitor.
11668 * @devlink: devlink.
11669 * @skb: Trapped packet.
11670 * @trap_ctx: Trap context.
11671 * @in_devlink_port: Input devlink port.
11672 * @fa_cookie: Flow action cookie. Could be NULL.
11674 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11675 void *trap_ctx, struct devlink_port *in_devlink_port,
11676 const struct flow_action_cookie *fa_cookie)
11679 struct devlink_trap_item *trap_item = trap_ctx;
11681 devlink_trap_stats_update(trap_item->stats, skb->len);
11682 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11684 if (trace_devlink_trap_report_enabled()) {
11685 struct devlink_trap_metadata metadata = {};
11687 devlink_trap_report_metadata_set(&metadata, trap_item,
11688 in_devlink_port, fa_cookie);
11689 trace_devlink_trap_report(devlink, skb, &metadata);
11692 EXPORT_SYMBOL_GPL(devlink_trap_report);
11695 * devlink_trap_ctx_priv - Trap context to driver private information.
11696 * @trap_ctx: Trap context.
11698 * Return: Driver private information passed during registration.
11700 void *devlink_trap_ctx_priv(void *trap_ctx)
11702 struct devlink_trap_item *trap_item = trap_ctx;
11704 return trap_item->priv;
11706 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
11709 devlink_trap_group_item_policer_link(struct devlink *devlink,
11710 struct devlink_trap_group_item *group_item)
11712 u32 policer_id = group_item->group->init_policer_id;
11713 struct devlink_trap_policer_item *policer_item;
11715 if (policer_id == 0)
11718 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
11719 if (WARN_ON_ONCE(!policer_item))
11722 group_item->policer_item = policer_item;
11728 devlink_trap_group_register(struct devlink *devlink,
11729 const struct devlink_trap_group *group)
11731 struct devlink_trap_group_item *group_item;
11734 if (devlink_trap_group_item_lookup(devlink, group->name))
11737 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
11741 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11742 if (!group_item->stats) {
11744 goto err_stats_alloc;
11747 group_item->group = group;
11749 err = devlink_trap_group_item_policer_link(devlink, group_item);
11751 goto err_policer_link;
11753 if (devlink->ops->trap_group_init) {
11754 err = devlink->ops->trap_group_init(devlink, group);
11756 goto err_group_init;
11759 list_add_tail(&group_item->list, &devlink->trap_group_list);
11760 devlink_trap_group_notify(devlink, group_item,
11761 DEVLINK_CMD_TRAP_GROUP_NEW);
11767 free_percpu(group_item->stats);
11774 devlink_trap_group_unregister(struct devlink *devlink,
11775 const struct devlink_trap_group *group)
11777 struct devlink_trap_group_item *group_item;
11779 group_item = devlink_trap_group_item_lookup(devlink, group->name);
11780 if (WARN_ON_ONCE(!group_item))
11783 devlink_trap_group_notify(devlink, group_item,
11784 DEVLINK_CMD_TRAP_GROUP_DEL);
11785 list_del(&group_item->list);
11786 free_percpu(group_item->stats);
11791 * devlink_trap_groups_register - Register packet trap groups with devlink.
11792 * @devlink: devlink.
11793 * @groups: Packet trap groups.
11794 * @groups_count: Count of provided packet trap groups.
11796 * Return: Non-zero value on failure.
11798 int devlink_trap_groups_register(struct devlink *devlink,
11799 const struct devlink_trap_group *groups,
11800 size_t groups_count)
11804 mutex_lock(&devlink->lock);
11805 for (i = 0; i < groups_count; i++) {
11806 const struct devlink_trap_group *group = &groups[i];
11808 err = devlink_trap_group_verify(group);
11810 goto err_trap_group_verify;
11812 err = devlink_trap_group_register(devlink, group);
11814 goto err_trap_group_register;
11816 mutex_unlock(&devlink->lock);
11820 err_trap_group_register:
11821 err_trap_group_verify:
11822 for (i--; i >= 0; i--)
11823 devlink_trap_group_unregister(devlink, &groups[i]);
11824 mutex_unlock(&devlink->lock);
11827 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
11830 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
11831 * @devlink: devlink.
11832 * @groups: Packet trap groups.
11833 * @groups_count: Count of provided packet trap groups.
11835 void devlink_trap_groups_unregister(struct devlink *devlink,
11836 const struct devlink_trap_group *groups,
11837 size_t groups_count)
11841 mutex_lock(&devlink->lock);
11842 for (i = groups_count - 1; i >= 0; i--)
11843 devlink_trap_group_unregister(devlink, &groups[i]);
11844 mutex_unlock(&devlink->lock);
11846 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
11849 devlink_trap_policer_notify(struct devlink *devlink,
11850 const struct devlink_trap_policer_item *policer_item,
11851 enum devlink_command cmd)
11853 struct sk_buff *msg;
11856 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
11857 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
11858 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11861 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11865 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
11872 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11873 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11877 devlink_trap_policer_register(struct devlink *devlink,
11878 const struct devlink_trap_policer *policer)
11880 struct devlink_trap_policer_item *policer_item;
11883 if (devlink_trap_policer_item_lookup(devlink, policer->id))
11886 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
11890 policer_item->policer = policer;
11891 policer_item->rate = policer->init_rate;
11892 policer_item->burst = policer->init_burst;
11894 if (devlink->ops->trap_policer_init) {
11895 err = devlink->ops->trap_policer_init(devlink, policer);
11897 goto err_policer_init;
11900 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
11901 devlink_trap_policer_notify(devlink, policer_item,
11902 DEVLINK_CMD_TRAP_POLICER_NEW);
11907 kfree(policer_item);
11912 devlink_trap_policer_unregister(struct devlink *devlink,
11913 const struct devlink_trap_policer *policer)
11915 struct devlink_trap_policer_item *policer_item;
11917 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
11918 if (WARN_ON_ONCE(!policer_item))
11921 devlink_trap_policer_notify(devlink, policer_item,
11922 DEVLINK_CMD_TRAP_POLICER_DEL);
11923 list_del(&policer_item->list);
11924 if (devlink->ops->trap_policer_fini)
11925 devlink->ops->trap_policer_fini(devlink, policer);
11926 kfree(policer_item);
11930 * devlink_trap_policers_register - Register packet trap policers with devlink.
11931 * @devlink: devlink.
11932 * @policers: Packet trap policers.
11933 * @policers_count: Count of provided packet trap policers.
11935 * Return: Non-zero value on failure.
11938 devlink_trap_policers_register(struct devlink *devlink,
11939 const struct devlink_trap_policer *policers,
11940 size_t policers_count)
11944 mutex_lock(&devlink->lock);
11945 for (i = 0; i < policers_count; i++) {
11946 const struct devlink_trap_policer *policer = &policers[i];
11948 if (WARN_ON(policer->id == 0 ||
11949 policer->max_rate < policer->min_rate ||
11950 policer->max_burst < policer->min_burst)) {
11952 goto err_trap_policer_verify;
11955 err = devlink_trap_policer_register(devlink, policer);
11957 goto err_trap_policer_register;
11959 mutex_unlock(&devlink->lock);
11963 err_trap_policer_register:
11964 err_trap_policer_verify:
11965 for (i--; i >= 0; i--)
11966 devlink_trap_policer_unregister(devlink, &policers[i]);
11967 mutex_unlock(&devlink->lock);
11970 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
11973 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
11974 * @devlink: devlink.
11975 * @policers: Packet trap policers.
11976 * @policers_count: Count of provided packet trap policers.
11979 devlink_trap_policers_unregister(struct devlink *devlink,
11980 const struct devlink_trap_policer *policers,
11981 size_t policers_count)
11985 mutex_lock(&devlink->lock);
11986 for (i = policers_count - 1; i >= 0; i--)
11987 devlink_trap_policer_unregister(devlink, &policers[i]);
11988 mutex_unlock(&devlink->lock);
11990 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
11992 static void __devlink_compat_running_version(struct devlink *devlink,
11993 char *buf, size_t len)
11995 const struct nlattr *nlattr;
11996 struct devlink_info_req req;
11997 struct sk_buff *msg;
12000 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12005 err = devlink->ops->info_get(devlink, &req, NULL);
12009 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12010 const struct nlattr *kv;
12013 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12016 nla_for_each_nested(kv, nlattr, rem_kv) {
12017 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12020 strlcat(buf, nla_data(kv), len);
12021 strlcat(buf, " ", len);
12028 static struct devlink_port *netdev_to_devlink_port(struct net_device *dev)
12030 if (!dev->netdev_ops->ndo_get_devlink_port)
12033 return dev->netdev_ops->ndo_get_devlink_port(dev);
12036 void devlink_compat_running_version(struct devlink *devlink,
12037 char *buf, size_t len)
12039 if (!devlink->ops->info_get)
12042 mutex_lock(&devlink->lock);
12043 __devlink_compat_running_version(devlink, buf, len);
12044 mutex_unlock(&devlink->lock);
12047 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12049 struct devlink_flash_update_params params = {};
12052 if (!devlink->ops->flash_update)
12053 return -EOPNOTSUPP;
12055 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
12059 mutex_lock(&devlink->lock);
12060 devlink_flash_update_begin_notify(devlink);
12061 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
12062 devlink_flash_update_end_notify(devlink);
12063 mutex_unlock(&devlink->lock);
12065 release_firmware(params.fw);
12070 int devlink_compat_phys_port_name_get(struct net_device *dev,
12071 char *name, size_t len)
12073 struct devlink_port *devlink_port;
12075 /* RTNL mutex is held here which ensures that devlink_port
12076 * instance cannot disappear in the middle. No need to take
12077 * any devlink lock as only permanent values are accessed.
12081 devlink_port = netdev_to_devlink_port(dev);
12083 return -EOPNOTSUPP;
12085 return __devlink_port_phys_port_name_get(devlink_port, name, len);
12088 int devlink_compat_switch_id_get(struct net_device *dev,
12089 struct netdev_phys_item_id *ppid)
12091 struct devlink_port *devlink_port;
12093 /* Caller must hold RTNL mutex or reference to dev, which ensures that
12094 * devlink_port instance cannot disappear in the middle. No need to take
12095 * any devlink lock as only permanent values are accessed.
12097 devlink_port = netdev_to_devlink_port(dev);
12098 if (!devlink_port || !devlink_port->switch_port)
12099 return -EOPNOTSUPP;
12101 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
12106 static void __net_exit devlink_pernet_pre_exit(struct net *net)
12108 struct devlink *devlink;
12109 u32 actions_performed;
12110 unsigned long index;
12113 /* In case network namespace is getting destroyed, reload
12114 * all devlink instances from this namespace into init_net.
12116 mutex_lock(&devlink_mutex);
12117 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
12118 if (!devlink_try_get(devlink))
12121 if (!net_eq(devlink_net(devlink), net))
12124 WARN_ON(!(devlink->features & DEVLINK_F_RELOAD));
12125 err = devlink_reload(devlink, &init_net,
12126 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
12127 DEVLINK_RELOAD_LIMIT_UNSPEC,
12128 &actions_performed, NULL);
12129 if (err && err != -EOPNOTSUPP)
12130 pr_warn("Failed to reload devlink instance into init_net\n");
12132 devlink_put(devlink);
12134 mutex_unlock(&devlink_mutex);
12137 static struct pernet_operations devlink_pernet_ops __net_initdata = {
12138 .pre_exit = devlink_pernet_pre_exit,
12141 static int __init devlink_init(void)
12145 err = genl_register_family(&devlink_nl_family);
12148 err = register_pernet_subsys(&devlink_pernet_ops);
12155 subsys_initcall(devlink_init);