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 const struct devlink_ops *ops;
59 struct xarray snapshot_ids;
60 struct devlink_dev_stats stats;
63 /* Serializes access to devlink instance specific objects such as
64 * port, sb, dpipe, resource, params, region, traps and more.
69 struct completion comp;
70 char priv[] __aligned(NETDEV_ALIGN);
74 * struct devlink_resource - devlink resource
75 * @name: name of the resource
76 * @id: id, per devlink instance
77 * @size: size of the resource
78 * @size_new: updated size of the resource, reload is needed
79 * @size_valid: valid in case the total size of the resource is valid
80 * including its children
81 * @parent: parent resource
82 * @size_params: size parameters
84 * @resource_list: list of child resources
85 * @occ_get: occupancy getter callback
86 * @occ_get_priv: occupancy getter callback priv
88 struct devlink_resource {
94 struct devlink_resource *parent;
95 struct devlink_resource_size_params size_params;
96 struct list_head list;
97 struct list_head resource_list;
98 devlink_resource_occ_get_t *occ_get;
102 void *devlink_priv(struct devlink *devlink)
104 return &devlink->priv;
106 EXPORT_SYMBOL_GPL(devlink_priv);
108 struct devlink *priv_to_devlink(void *priv)
110 return container_of(priv, struct devlink, priv);
112 EXPORT_SYMBOL_GPL(priv_to_devlink);
114 struct device *devlink_to_dev(const struct devlink *devlink)
118 EXPORT_SYMBOL_GPL(devlink_to_dev);
120 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
122 .name = "destination mac",
123 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
128 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
130 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
131 .fields = devlink_dpipe_fields_ethernet,
132 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
135 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
137 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
139 .name = "destination ip",
140 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
145 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
147 .id = DEVLINK_DPIPE_HEADER_IPV4,
148 .fields = devlink_dpipe_fields_ipv4,
149 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
152 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
154 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
156 .name = "destination ip",
157 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
162 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
164 .id = DEVLINK_DPIPE_HEADER_IPV6,
165 .fields = devlink_dpipe_fields_ipv6,
166 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
169 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
171 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
172 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
173 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
175 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
176 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
177 [DEVLINK_PORT_FN_ATTR_STATE] =
178 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
179 DEVLINK_PORT_FN_STATE_ACTIVE),
182 static DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
183 #define DEVLINK_REGISTERED XA_MARK_1
185 /* devlink instances are open to the access from the user space after
186 * devlink_register() call. Such logical barrier allows us to have certain
187 * expectations related to locking.
189 * Before *_register() - we are in initialization stage and no parallel
190 * access possible to the devlink instance. All drivers perform that phase
191 * by implicitly holding device_lock.
193 * After *_register() - users and driver can access devlink instance at
196 #define ASSERT_DEVLINK_REGISTERED(d) \
197 WARN_ON_ONCE(!xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
198 #define ASSERT_DEVLINK_NOT_REGISTERED(d) \
199 WARN_ON_ONCE(xa_get_mark(&devlinks, (d)->index, DEVLINK_REGISTERED))
203 * An overall lock guarding every operation coming from userspace.
204 * It also guards devlink devices list and it is taken when
205 * driver registers/unregisters it.
207 static DEFINE_MUTEX(devlink_mutex);
209 struct net *devlink_net(const struct devlink *devlink)
211 return read_pnet(&devlink->_net);
213 EXPORT_SYMBOL_GPL(devlink_net);
215 void devlink_put(struct devlink *devlink)
217 if (refcount_dec_and_test(&devlink->refcount))
218 complete(&devlink->comp);
221 struct devlink *__must_check devlink_try_get(struct devlink *devlink)
223 if (refcount_inc_not_zero(&devlink->refcount))
228 void devl_assert_locked(struct devlink *devlink)
230 lockdep_assert_held(&devlink->lock);
232 EXPORT_SYMBOL_GPL(devl_assert_locked);
234 #ifdef CONFIG_LOCKDEP
235 /* For use in conjunction with LOCKDEP only e.g. rcu_dereference_protected() */
236 bool devl_lock_is_held(struct devlink *devlink)
238 return lockdep_is_held(&devlink->lock);
240 EXPORT_SYMBOL_GPL(devl_lock_is_held);
243 void devl_lock(struct devlink *devlink)
245 mutex_lock(&devlink->lock);
247 EXPORT_SYMBOL_GPL(devl_lock);
249 void devl_unlock(struct devlink *devlink)
251 mutex_unlock(&devlink->lock);
253 EXPORT_SYMBOL_GPL(devl_unlock);
255 static struct devlink *devlink_get_from_attrs(struct net *net,
256 struct nlattr **attrs)
258 struct devlink *devlink;
264 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
265 return ERR_PTR(-EINVAL);
267 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
268 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
270 lockdep_assert_held(&devlink_mutex);
272 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
273 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
274 strcmp(dev_name(devlink->dev), devname) == 0 &&
275 net_eq(devlink_net(devlink), net)) {
281 if (!found || !devlink_try_get(devlink))
282 devlink = ERR_PTR(-ENODEV);
287 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
288 unsigned int port_index)
290 struct devlink_port *devlink_port;
292 list_for_each_entry(devlink_port, &devlink->port_list, list) {
293 if (devlink_port->index == port_index)
299 static bool devlink_port_index_exists(struct devlink *devlink,
300 unsigned int port_index)
302 return devlink_port_get_by_index(devlink, port_index);
305 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
306 struct nlattr **attrs)
308 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
309 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
310 struct devlink_port *devlink_port;
312 devlink_port = devlink_port_get_by_index(devlink, port_index);
314 return ERR_PTR(-ENODEV);
317 return ERR_PTR(-EINVAL);
320 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
321 struct genl_info *info)
323 return devlink_port_get_from_attrs(devlink, info->attrs);
327 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
329 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
333 devlink_rate_is_node(struct devlink_rate *devlink_rate)
335 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
338 static struct devlink_rate *
339 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
341 struct devlink_rate *devlink_rate;
342 struct devlink_port *devlink_port;
344 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
345 if (IS_ERR(devlink_port))
346 return ERR_CAST(devlink_port);
347 devlink_rate = devlink_port->devlink_rate;
348 return devlink_rate ?: ERR_PTR(-ENODEV);
351 static struct devlink_rate *
352 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
354 static struct devlink_rate *devlink_rate;
356 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
357 if (devlink_rate_is_node(devlink_rate) &&
358 !strcmp(node_name, devlink_rate->name))
361 return ERR_PTR(-ENODEV);
364 static struct devlink_rate *
365 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
367 const char *rate_node_name;
370 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
371 return ERR_PTR(-EINVAL);
372 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
373 len = strlen(rate_node_name);
374 /* Name cannot be empty or decimal number */
375 if (!len || strspn(rate_node_name, "0123456789") == len)
376 return ERR_PTR(-EINVAL);
378 return devlink_rate_node_get_by_name(devlink, rate_node_name);
381 static struct devlink_rate *
382 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
384 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
387 static struct devlink_rate *
388 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
390 struct nlattr **attrs = info->attrs;
392 if (attrs[DEVLINK_ATTR_PORT_INDEX])
393 return devlink_rate_leaf_get_from_info(devlink, info);
394 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
395 return devlink_rate_node_get_from_info(devlink, info);
397 return ERR_PTR(-EINVAL);
401 struct list_head list;
404 u16 ingress_pools_count;
405 u16 egress_pools_count;
406 u16 ingress_tc_count;
410 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
412 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
415 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
416 unsigned int sb_index)
418 struct devlink_sb *devlink_sb;
420 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
421 if (devlink_sb->index == sb_index)
427 static bool devlink_sb_index_exists(struct devlink *devlink,
428 unsigned int sb_index)
430 return devlink_sb_get_by_index(devlink, sb_index);
433 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
434 struct nlattr **attrs)
436 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
437 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
438 struct devlink_sb *devlink_sb;
440 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
442 return ERR_PTR(-ENODEV);
445 return ERR_PTR(-EINVAL);
448 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
449 struct genl_info *info)
451 return devlink_sb_get_from_attrs(devlink, info->attrs);
454 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
455 struct nlattr **attrs,
460 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
463 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
464 if (val >= devlink_sb_pool_count(devlink_sb))
470 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
471 struct genl_info *info,
474 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
479 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
480 enum devlink_sb_pool_type *p_pool_type)
484 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
487 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
488 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
489 val != DEVLINK_SB_POOL_TYPE_EGRESS)
496 devlink_sb_pool_type_get_from_info(struct genl_info *info,
497 enum devlink_sb_pool_type *p_pool_type)
499 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
503 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
504 enum devlink_sb_threshold_type *p_th_type)
508 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
511 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
512 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
513 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
520 devlink_sb_th_type_get_from_info(struct genl_info *info,
521 enum devlink_sb_threshold_type *p_th_type)
523 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
527 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
528 struct nlattr **attrs,
529 enum devlink_sb_pool_type pool_type,
534 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
537 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
538 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
539 val >= devlink_sb->ingress_tc_count)
541 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
542 val >= devlink_sb->egress_tc_count)
549 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
550 struct genl_info *info,
551 enum devlink_sb_pool_type pool_type,
554 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
555 pool_type, p_tc_index);
558 struct devlink_region {
559 struct devlink *devlink;
560 struct devlink_port *port;
561 struct list_head list;
563 const struct devlink_region_ops *ops;
564 const struct devlink_port_region_ops *port_ops;
566 struct list_head snapshot_list;
572 struct devlink_snapshot {
573 struct list_head list;
574 struct devlink_region *region;
579 static struct devlink_region *
580 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
582 struct devlink_region *region;
584 list_for_each_entry(region, &devlink->region_list, list)
585 if (!strcmp(region->ops->name, region_name))
591 static struct devlink_region *
592 devlink_port_region_get_by_name(struct devlink_port *port,
593 const char *region_name)
595 struct devlink_region *region;
597 list_for_each_entry(region, &port->region_list, list)
598 if (!strcmp(region->ops->name, region_name))
604 static struct devlink_snapshot *
605 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
607 struct devlink_snapshot *snapshot;
609 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
610 if (snapshot->id == id)
616 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
617 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
618 #define DEVLINK_NL_FLAG_NEED_RATE BIT(2)
619 #define DEVLINK_NL_FLAG_NEED_RATE_NODE BIT(3)
621 /* The per devlink instance lock is taken by default in the pre-doit
622 * operation, yet several commands do not require this. The global
623 * devlink lock is taken and protects from disruption by user-calls.
625 #define DEVLINK_NL_FLAG_NO_LOCK BIT(4)
627 static int devlink_nl_pre_doit(const struct genl_ops *ops,
628 struct sk_buff *skb, struct genl_info *info)
630 struct devlink_port *devlink_port;
631 struct devlink *devlink;
634 mutex_lock(&devlink_mutex);
635 devlink = devlink_get_from_attrs(genl_info_net(info), info->attrs);
636 if (IS_ERR(devlink)) {
637 mutex_unlock(&devlink_mutex);
638 return PTR_ERR(devlink);
640 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
641 mutex_lock(&devlink->lock);
642 info->user_ptr[0] = devlink;
643 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
644 devlink_port = devlink_port_get_from_info(devlink, info);
645 if (IS_ERR(devlink_port)) {
646 err = PTR_ERR(devlink_port);
649 info->user_ptr[1] = devlink_port;
650 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
651 devlink_port = devlink_port_get_from_info(devlink, info);
652 if (!IS_ERR(devlink_port))
653 info->user_ptr[1] = devlink_port;
654 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE) {
655 struct devlink_rate *devlink_rate;
657 devlink_rate = devlink_rate_get_from_info(devlink, info);
658 if (IS_ERR(devlink_rate)) {
659 err = PTR_ERR(devlink_rate);
662 info->user_ptr[1] = devlink_rate;
663 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE_NODE) {
664 struct devlink_rate *rate_node;
666 rate_node = devlink_rate_node_get_from_info(devlink, info);
667 if (IS_ERR(rate_node)) {
668 err = PTR_ERR(rate_node);
671 info->user_ptr[1] = rate_node;
676 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
677 mutex_unlock(&devlink->lock);
678 devlink_put(devlink);
679 mutex_unlock(&devlink_mutex);
683 static void devlink_nl_post_doit(const struct genl_ops *ops,
684 struct sk_buff *skb, struct genl_info *info)
686 struct devlink *devlink;
688 devlink = info->user_ptr[0];
689 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
690 mutex_unlock(&devlink->lock);
691 devlink_put(devlink);
692 mutex_unlock(&devlink_mutex);
695 static struct genl_family devlink_nl_family;
697 enum devlink_multicast_groups {
698 DEVLINK_MCGRP_CONFIG,
701 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
702 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
705 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
707 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
709 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
714 struct devlink_reload_combination {
715 enum devlink_reload_action action;
716 enum devlink_reload_limit limit;
719 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
721 /* can't reinitialize driver with no down time */
722 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
723 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
728 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
729 enum devlink_reload_limit limit)
733 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
734 if (devlink_reload_invalid_combinations[i].action == action &&
735 devlink_reload_invalid_combinations[i].limit == limit)
741 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
743 return test_bit(action, &devlink->ops->reload_actions);
747 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
749 return test_bit(limit, &devlink->ops->reload_limits);
752 static int devlink_reload_stat_put(struct sk_buff *msg,
753 enum devlink_reload_limit limit, u32 value)
755 struct nlattr *reload_stats_entry;
757 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
758 if (!reload_stats_entry)
761 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
762 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
763 goto nla_put_failure;
764 nla_nest_end(msg, reload_stats_entry);
768 nla_nest_cancel(msg, reload_stats_entry);
772 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
774 struct nlattr *reload_stats_attr, *act_info, *act_stats;
779 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
781 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
783 if (!reload_stats_attr)
786 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
788 !devlink_reload_action_is_supported(devlink, i)) ||
789 i == DEVLINK_RELOAD_ACTION_UNSPEC)
791 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
793 goto nla_put_failure;
795 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
796 goto action_info_nest_cancel;
797 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
799 goto action_info_nest_cancel;
801 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
802 /* Remote stats are shown even if not locally supported.
803 * Stats of actions with unspecified limit are shown
804 * though drivers don't need to register unspecified
807 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
808 !devlink_reload_limit_is_supported(devlink, j)) ||
809 devlink_reload_combination_is_invalid(i, j))
812 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
814 value = devlink->stats.reload_stats[stat_idx];
816 value = devlink->stats.remote_reload_stats[stat_idx];
817 if (devlink_reload_stat_put(msg, j, value))
818 goto action_stats_nest_cancel;
820 nla_nest_end(msg, act_stats);
821 nla_nest_end(msg, act_info);
823 nla_nest_end(msg, reload_stats_attr);
826 action_stats_nest_cancel:
827 nla_nest_cancel(msg, act_stats);
828 action_info_nest_cancel:
829 nla_nest_cancel(msg, act_info);
831 nla_nest_cancel(msg, reload_stats_attr);
835 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
836 enum devlink_command cmd, u32 portid,
839 struct nlattr *dev_stats;
842 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
846 if (devlink_nl_put_handle(msg, devlink))
847 goto nla_put_failure;
848 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
849 goto nla_put_failure;
851 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
853 goto nla_put_failure;
855 if (devlink_reload_stats_put(msg, devlink, false))
856 goto dev_stats_nest_cancel;
857 if (devlink_reload_stats_put(msg, devlink, true))
858 goto dev_stats_nest_cancel;
860 nla_nest_end(msg, dev_stats);
861 genlmsg_end(msg, hdr);
864 dev_stats_nest_cancel:
865 nla_nest_cancel(msg, dev_stats);
867 genlmsg_cancel(msg, hdr);
871 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
876 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
877 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
879 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
883 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
889 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
890 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
893 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
894 struct devlink_port *devlink_port)
896 struct devlink_port_attrs *attrs = &devlink_port->attrs;
898 if (!devlink_port->attrs_set)
901 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
904 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
906 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
908 switch (devlink_port->attrs.flavour) {
909 case DEVLINK_PORT_FLAVOUR_PCI_PF:
910 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
911 attrs->pci_pf.controller) ||
912 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
914 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
917 case DEVLINK_PORT_FLAVOUR_PCI_VF:
918 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
919 attrs->pci_vf.controller) ||
920 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
921 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
923 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
926 case DEVLINK_PORT_FLAVOUR_PCI_SF:
927 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
928 attrs->pci_sf.controller) ||
929 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
931 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
935 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
936 case DEVLINK_PORT_FLAVOUR_CPU:
937 case DEVLINK_PORT_FLAVOUR_DSA:
938 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
939 attrs->phys.port_number))
943 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
944 attrs->phys.port_number))
946 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
947 attrs->phys.split_subport_number))
956 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
957 struct devlink_port *port,
959 struct netlink_ext_ack *extack,
962 u8 hw_addr[MAX_ADDR_LEN];
966 if (!ops->port_function_hw_addr_get)
969 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
972 if (err == -EOPNOTSUPP)
976 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
983 static int devlink_nl_rate_fill(struct sk_buff *msg,
984 struct devlink_rate *devlink_rate,
985 enum devlink_command cmd, u32 portid, u32 seq,
986 int flags, struct netlink_ext_ack *extack)
988 struct devlink *devlink = devlink_rate->devlink;
991 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
995 if (devlink_nl_put_handle(msg, devlink))
996 goto nla_put_failure;
998 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
999 goto nla_put_failure;
1001 if (devlink_rate_is_leaf(devlink_rate)) {
1002 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
1003 devlink_rate->devlink_port->index))
1004 goto nla_put_failure;
1005 } else if (devlink_rate_is_node(devlink_rate)) {
1006 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
1007 devlink_rate->name))
1008 goto nla_put_failure;
1011 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
1012 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
1013 goto nla_put_failure;
1015 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
1016 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
1017 goto nla_put_failure;
1019 if (devlink_rate->parent)
1020 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
1021 devlink_rate->parent->name))
1022 goto nla_put_failure;
1024 genlmsg_end(msg, hdr);
1028 genlmsg_cancel(msg, hdr);
1033 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
1035 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
1036 state == DEVLINK_PORT_FN_STATE_ACTIVE;
1040 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
1042 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
1043 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
1046 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
1047 struct devlink_port *port,
1048 struct sk_buff *msg,
1049 struct netlink_ext_ack *extack,
1052 enum devlink_port_fn_opstate opstate;
1053 enum devlink_port_fn_state state;
1056 if (!ops->port_fn_state_get)
1059 err = ops->port_fn_state_get(port, &state, &opstate, extack);
1061 if (err == -EOPNOTSUPP)
1065 if (!devlink_port_fn_state_valid(state)) {
1067 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
1070 if (!devlink_port_fn_opstate_valid(opstate)) {
1072 NL_SET_ERR_MSG_MOD(extack,
1073 "Invalid operational state read from driver");
1076 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
1077 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
1079 *msg_updated = true;
1084 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
1085 struct netlink_ext_ack *extack)
1087 const struct devlink_ops *ops;
1088 struct nlattr *function_attr;
1089 bool msg_updated = false;
1092 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
1096 ops = port->devlink->ops;
1097 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
1101 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
1103 if (err || !msg_updated)
1104 nla_nest_cancel(msg, function_attr);
1106 nla_nest_end(msg, function_attr);
1110 static int devlink_nl_port_fill(struct sk_buff *msg,
1111 struct devlink_port *devlink_port,
1112 enum devlink_command cmd, u32 portid, u32 seq,
1113 int flags, struct netlink_ext_ack *extack)
1115 struct devlink *devlink = devlink_port->devlink;
1118 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1122 if (devlink_nl_put_handle(msg, devlink))
1123 goto nla_put_failure;
1124 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1125 goto nla_put_failure;
1127 /* Hold rtnl lock while accessing port's netdev attributes. */
1129 spin_lock_bh(&devlink_port->type_lock);
1130 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
1131 goto nla_put_failure_type_locked;
1132 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1133 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1134 devlink_port->desired_type))
1135 goto nla_put_failure_type_locked;
1136 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1137 struct net *net = devlink_net(devlink_port->devlink);
1138 struct net_device *netdev = devlink_port->type_dev;
1140 if (netdev && net_eq(net, dev_net(netdev)) &&
1141 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1143 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1145 goto nla_put_failure_type_locked;
1147 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1148 struct ib_device *ibdev = devlink_port->type_dev;
1151 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1153 goto nla_put_failure_type_locked;
1155 spin_unlock_bh(&devlink_port->type_lock);
1157 if (devlink_nl_port_attrs_put(msg, devlink_port))
1158 goto nla_put_failure;
1159 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1160 goto nla_put_failure;
1162 genlmsg_end(msg, hdr);
1165 nla_put_failure_type_locked:
1166 spin_unlock_bh(&devlink_port->type_lock);
1169 genlmsg_cancel(msg, hdr);
1173 static void devlink_port_notify(struct devlink_port *devlink_port,
1174 enum devlink_command cmd)
1176 struct devlink *devlink = devlink_port->devlink;
1177 struct sk_buff *msg;
1180 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1182 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1185 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1189 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1195 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1196 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1199 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1200 enum devlink_command cmd)
1202 struct devlink *devlink = devlink_rate->devlink;
1203 struct sk_buff *msg;
1206 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1208 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1215 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1221 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1222 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1225 static int devlink_nl_cmd_rate_get_dumpit(struct sk_buff *msg,
1226 struct netlink_callback *cb)
1228 struct devlink_rate *devlink_rate;
1229 struct devlink *devlink;
1230 int start = cb->args[0];
1231 unsigned long index;
1235 mutex_lock(&devlink_mutex);
1236 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1237 if (!devlink_try_get(devlink))
1240 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1243 mutex_lock(&devlink->lock);
1244 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1245 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1246 u32 id = NETLINK_CB(cb->skb).portid;
1252 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1256 mutex_unlock(&devlink->lock);
1257 devlink_put(devlink);
1262 mutex_unlock(&devlink->lock);
1264 devlink_put(devlink);
1267 mutex_unlock(&devlink_mutex);
1268 if (err != -EMSGSIZE)
1275 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1276 struct genl_info *info)
1278 struct devlink_rate *devlink_rate = info->user_ptr[1];
1279 struct sk_buff *msg;
1282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1286 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1287 info->snd_portid, info->snd_seq, 0,
1294 return genlmsg_reply(msg, info);
1298 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1299 struct devlink_rate *parent)
1302 if (parent == devlink_rate)
1304 parent = parent->parent;
1309 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1311 struct devlink *devlink = info->user_ptr[0];
1312 struct sk_buff *msg;
1315 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1319 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1320 info->snd_portid, info->snd_seq, 0);
1326 return genlmsg_reply(msg, info);
1329 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
1330 struct netlink_callback *cb)
1332 struct devlink *devlink;
1333 int start = cb->args[0];
1334 unsigned long index;
1338 mutex_lock(&devlink_mutex);
1339 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1340 if (!devlink_try_get(devlink))
1343 if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) {
1344 devlink_put(devlink);
1350 devlink_put(devlink);
1354 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1355 NETLINK_CB(cb->skb).portid,
1356 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1357 devlink_put(devlink);
1363 mutex_unlock(&devlink_mutex);
1369 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1370 struct genl_info *info)
1372 struct devlink_port *devlink_port = info->user_ptr[1];
1373 struct sk_buff *msg;
1376 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1380 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1381 info->snd_portid, info->snd_seq, 0,
1388 return genlmsg_reply(msg, info);
1391 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
1392 struct netlink_callback *cb)
1394 struct devlink *devlink;
1395 struct devlink_port *devlink_port;
1396 int start = cb->args[0];
1397 unsigned long index;
1401 mutex_lock(&devlink_mutex);
1402 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1403 if (!devlink_try_get(devlink))
1406 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1409 mutex_lock(&devlink->lock);
1410 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1415 err = devlink_nl_port_fill(msg, devlink_port,
1417 NETLINK_CB(cb->skb).portid,
1419 NLM_F_MULTI, cb->extack);
1421 mutex_unlock(&devlink->lock);
1422 devlink_put(devlink);
1427 mutex_unlock(&devlink->lock);
1429 devlink_put(devlink);
1432 mutex_unlock(&devlink_mutex);
1438 static int devlink_port_type_set(struct devlink_port *devlink_port,
1439 enum devlink_port_type port_type)
1444 if (!devlink_port->devlink->ops->port_type_set)
1447 if (port_type == devlink_port->type)
1450 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1455 devlink_port->desired_type = port_type;
1456 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1460 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1461 const struct nlattr *attr,
1462 struct netlink_ext_ack *extack)
1464 const struct devlink_ops *ops = port->devlink->ops;
1468 hw_addr = nla_data(attr);
1469 hw_addr_len = nla_len(attr);
1470 if (hw_addr_len > MAX_ADDR_LEN) {
1471 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1474 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1475 if (hw_addr_len != ETH_ALEN) {
1476 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1479 if (!is_unicast_ether_addr(hw_addr)) {
1480 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1485 if (!ops->port_function_hw_addr_set) {
1486 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
1490 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1494 static int devlink_port_fn_state_set(struct devlink_port *port,
1495 const struct nlattr *attr,
1496 struct netlink_ext_ack *extack)
1498 enum devlink_port_fn_state state;
1499 const struct devlink_ops *ops;
1501 state = nla_get_u8(attr);
1502 ops = port->devlink->ops;
1503 if (!ops->port_fn_state_set) {
1504 NL_SET_ERR_MSG_MOD(extack,
1505 "Function does not support state setting");
1508 return ops->port_fn_state_set(port, state, extack);
1511 static int devlink_port_function_set(struct devlink_port *port,
1512 const struct nlattr *attr,
1513 struct netlink_ext_ack *extack)
1515 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1518 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1519 devlink_function_nl_policy, extack);
1521 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1525 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1527 err = devlink_port_function_hw_addr_set(port, attr, extack);
1531 /* Keep this as the last function attribute set, so that when
1532 * multiple port function attributes are set along with state,
1533 * Those can be applied first before activating the state.
1535 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1537 err = devlink_port_fn_state_set(port, attr, extack);
1540 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1544 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1545 struct genl_info *info)
1547 struct devlink_port *devlink_port = info->user_ptr[1];
1550 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1551 enum devlink_port_type port_type;
1553 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1554 err = devlink_port_type_set(devlink_port, port_type);
1559 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1560 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1561 struct netlink_ext_ack *extack = info->extack;
1563 err = devlink_port_function_set(devlink_port, attr, extack);
1571 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1572 struct genl_info *info)
1574 struct devlink_port *devlink_port = info->user_ptr[1];
1575 struct devlink *devlink = info->user_ptr[0];
1578 if (!info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
1580 if (!devlink->ops->port_split)
1583 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1585 if (!devlink_port->attrs.splittable) {
1586 /* Split ports cannot be split. */
1587 if (devlink_port->attrs.split)
1588 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1590 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1594 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1595 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1599 return devlink->ops->port_split(devlink, devlink_port, count,
1603 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1604 struct genl_info *info)
1606 struct devlink_port *devlink_port = info->user_ptr[1];
1607 struct devlink *devlink = info->user_ptr[0];
1609 if (!devlink->ops->port_unsplit)
1611 return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1614 static int devlink_port_new_notifiy(struct devlink *devlink,
1615 unsigned int port_index,
1616 struct genl_info *info)
1618 struct devlink_port *devlink_port;
1619 struct sk_buff *msg;
1622 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1626 mutex_lock(&devlink->lock);
1627 devlink_port = devlink_port_get_by_index(devlink, port_index);
1628 if (!devlink_port) {
1633 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1634 info->snd_portid, info->snd_seq, 0, NULL);
1638 err = genlmsg_reply(msg, info);
1639 mutex_unlock(&devlink->lock);
1643 mutex_unlock(&devlink->lock);
1648 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1649 struct genl_info *info)
1651 struct netlink_ext_ack *extack = info->extack;
1652 struct devlink_port_new_attrs new_attrs = {};
1653 struct devlink *devlink = info->user_ptr[0];
1654 unsigned int new_port_index;
1657 if (!devlink->ops->port_new || !devlink->ops->port_del)
1660 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1661 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1662 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1665 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1667 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1669 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1670 /* Port index of the new port being created by driver. */
1671 new_attrs.port_index =
1672 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1673 new_attrs.port_index_valid = true;
1675 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1676 new_attrs.controller =
1677 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1678 new_attrs.controller_valid = true;
1680 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1681 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1682 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1683 new_attrs.sfnum_valid = true;
1686 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1691 err = devlink_port_new_notifiy(devlink, new_port_index, info);
1692 if (err && err != -ENODEV) {
1693 /* Fail to send the response; destroy newly created port. */
1694 devlink->ops->port_del(devlink, new_port_index, extack);
1699 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1700 struct genl_info *info)
1702 struct netlink_ext_ack *extack = info->extack;
1703 struct devlink *devlink = info->user_ptr[0];
1704 unsigned int port_index;
1706 if (!devlink->ops->port_del)
1709 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1710 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1713 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1715 return devlink->ops->port_del(devlink, port_index, extack);
1719 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1720 struct genl_info *info,
1721 struct nlattr *nla_parent)
1723 struct devlink *devlink = devlink_rate->devlink;
1724 const char *parent_name = nla_data(nla_parent);
1725 const struct devlink_ops *ops = devlink->ops;
1726 size_t len = strlen(parent_name);
1727 struct devlink_rate *parent;
1728 int err = -EOPNOTSUPP;
1730 parent = devlink_rate->parent;
1731 if (parent && len) {
1732 NL_SET_ERR_MSG_MOD(info->extack, "Rate object already has parent.");
1734 } else if (parent && !len) {
1735 if (devlink_rate_is_leaf(devlink_rate))
1736 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1737 devlink_rate->priv, NULL,
1739 else if (devlink_rate_is_node(devlink_rate))
1740 err = ops->rate_node_parent_set(devlink_rate, NULL,
1741 devlink_rate->priv, NULL,
1746 refcount_dec(&parent->refcnt);
1747 devlink_rate->parent = NULL;
1748 } else if (!parent && len) {
1749 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1753 if (parent == devlink_rate) {
1754 NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1758 if (devlink_rate_is_node(devlink_rate) &&
1759 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1760 NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1764 if (devlink_rate_is_leaf(devlink_rate))
1765 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1766 devlink_rate->priv, parent->priv,
1768 else if (devlink_rate_is_node(devlink_rate))
1769 err = ops->rate_node_parent_set(devlink_rate, parent,
1770 devlink_rate->priv, parent->priv,
1775 refcount_inc(&parent->refcnt);
1776 devlink_rate->parent = parent;
1782 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1783 const struct devlink_ops *ops,
1784 struct genl_info *info)
1786 struct nlattr *nla_parent, **attrs = info->attrs;
1787 int err = -EOPNOTSUPP;
1790 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1791 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1792 if (devlink_rate_is_leaf(devlink_rate))
1793 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1794 rate, info->extack);
1795 else if (devlink_rate_is_node(devlink_rate))
1796 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1797 rate, info->extack);
1800 devlink_rate->tx_share = rate;
1803 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1804 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1805 if (devlink_rate_is_leaf(devlink_rate))
1806 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1807 rate, info->extack);
1808 else if (devlink_rate_is_node(devlink_rate))
1809 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1810 rate, info->extack);
1813 devlink_rate->tx_max = rate;
1816 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1818 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1827 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1828 struct genl_info *info,
1829 enum devlink_rate_type type)
1831 struct nlattr **attrs = info->attrs;
1833 if (type == DEVLINK_RATE_TYPE_LEAF) {
1834 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1835 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1838 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1839 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1842 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1843 !ops->rate_leaf_parent_set) {
1844 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1847 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1848 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1849 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1852 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1853 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1856 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1857 !ops->rate_node_parent_set) {
1858 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1862 WARN(1, "Unknown type of rate object");
1869 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1870 struct genl_info *info)
1872 struct devlink_rate *devlink_rate = info->user_ptr[1];
1873 struct devlink *devlink = devlink_rate->devlink;
1874 const struct devlink_ops *ops = devlink->ops;
1877 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1880 err = devlink_nl_rate_set(devlink_rate, ops, info);
1883 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1887 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1888 struct genl_info *info)
1890 struct devlink *devlink = info->user_ptr[0];
1891 struct devlink_rate *rate_node;
1892 const struct devlink_ops *ops;
1896 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1897 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
1901 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1904 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1905 if (!IS_ERR(rate_node))
1907 else if (rate_node == ERR_PTR(-EINVAL))
1910 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
1914 rate_node->devlink = devlink;
1915 rate_node->type = DEVLINK_RATE_TYPE_NODE;
1916 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
1917 if (!rate_node->name) {
1922 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
1926 err = devlink_nl_rate_set(rate_node, ops, info);
1930 refcount_set(&rate_node->refcnt, 1);
1931 list_add(&rate_node->list, &devlink->rate_list);
1932 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
1936 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1938 kfree(rate_node->name);
1944 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
1945 struct genl_info *info)
1947 struct devlink_rate *rate_node = info->user_ptr[1];
1948 struct devlink *devlink = rate_node->devlink;
1949 const struct devlink_ops *ops = devlink->ops;
1952 if (refcount_read(&rate_node->refcnt) > 1) {
1953 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
1957 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
1958 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1959 if (rate_node->parent)
1960 refcount_dec(&rate_node->parent->refcnt);
1961 list_del(&rate_node->list);
1962 kfree(rate_node->name);
1967 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
1968 struct devlink_sb *devlink_sb,
1969 enum devlink_command cmd, u32 portid,
1974 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1978 if (devlink_nl_put_handle(msg, devlink))
1979 goto nla_put_failure;
1980 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1981 goto nla_put_failure;
1982 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
1983 goto nla_put_failure;
1984 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
1985 devlink_sb->ingress_pools_count))
1986 goto nla_put_failure;
1987 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
1988 devlink_sb->egress_pools_count))
1989 goto nla_put_failure;
1990 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
1991 devlink_sb->ingress_tc_count))
1992 goto nla_put_failure;
1993 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
1994 devlink_sb->egress_tc_count))
1995 goto nla_put_failure;
1997 genlmsg_end(msg, hdr);
2001 genlmsg_cancel(msg, hdr);
2005 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2006 struct genl_info *info)
2008 struct devlink *devlink = info->user_ptr[0];
2009 struct devlink_sb *devlink_sb;
2010 struct sk_buff *msg;
2013 devlink_sb = devlink_sb_get_from_info(devlink, info);
2014 if (IS_ERR(devlink_sb))
2015 return PTR_ERR(devlink_sb);
2017 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2021 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2023 info->snd_portid, info->snd_seq, 0);
2029 return genlmsg_reply(msg, info);
2032 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
2033 struct netlink_callback *cb)
2035 struct devlink *devlink;
2036 struct devlink_sb *devlink_sb;
2037 int start = cb->args[0];
2038 unsigned long index;
2042 mutex_lock(&devlink_mutex);
2043 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2044 if (!devlink_try_get(devlink))
2047 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
2050 mutex_lock(&devlink->lock);
2051 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2056 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2058 NETLINK_CB(cb->skb).portid,
2062 mutex_unlock(&devlink->lock);
2063 devlink_put(devlink);
2068 mutex_unlock(&devlink->lock);
2070 devlink_put(devlink);
2073 mutex_unlock(&devlink_mutex);
2079 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2080 struct devlink_sb *devlink_sb,
2081 u16 pool_index, enum devlink_command cmd,
2082 u32 portid, u32 seq, int flags)
2084 struct devlink_sb_pool_info pool_info;
2088 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2089 pool_index, &pool_info);
2093 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2097 if (devlink_nl_put_handle(msg, devlink))
2098 goto nla_put_failure;
2099 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2100 goto nla_put_failure;
2101 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2102 goto nla_put_failure;
2103 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2104 goto nla_put_failure;
2105 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2106 goto nla_put_failure;
2107 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2108 pool_info.threshold_type))
2109 goto nla_put_failure;
2110 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2111 pool_info.cell_size))
2112 goto nla_put_failure;
2114 genlmsg_end(msg, hdr);
2118 genlmsg_cancel(msg, hdr);
2122 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2123 struct genl_info *info)
2125 struct devlink *devlink = info->user_ptr[0];
2126 struct devlink_sb *devlink_sb;
2127 struct sk_buff *msg;
2131 devlink_sb = devlink_sb_get_from_info(devlink, info);
2132 if (IS_ERR(devlink_sb))
2133 return PTR_ERR(devlink_sb);
2135 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2140 if (!devlink->ops->sb_pool_get)
2143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2147 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2148 DEVLINK_CMD_SB_POOL_NEW,
2149 info->snd_portid, info->snd_seq, 0);
2155 return genlmsg_reply(msg, info);
2158 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2159 struct devlink *devlink,
2160 struct devlink_sb *devlink_sb,
2161 u32 portid, u32 seq)
2163 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2167 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2168 if (*p_idx < start) {
2172 err = devlink_nl_sb_pool_fill(msg, devlink,
2175 DEVLINK_CMD_SB_POOL_NEW,
2176 portid, seq, NLM_F_MULTI);
2184 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
2185 struct netlink_callback *cb)
2187 struct devlink *devlink;
2188 struct devlink_sb *devlink_sb;
2189 int start = cb->args[0];
2190 unsigned long index;
2194 mutex_lock(&devlink_mutex);
2195 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2196 if (!devlink_try_get(devlink))
2199 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2200 !devlink->ops->sb_pool_get)
2203 mutex_lock(&devlink->lock);
2204 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2205 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
2207 NETLINK_CB(cb->skb).portid,
2208 cb->nlh->nlmsg_seq);
2209 if (err == -EOPNOTSUPP) {
2212 mutex_unlock(&devlink->lock);
2213 devlink_put(devlink);
2217 mutex_unlock(&devlink->lock);
2219 devlink_put(devlink);
2222 mutex_unlock(&devlink_mutex);
2224 if (err != -EMSGSIZE)
2231 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2232 u16 pool_index, u32 size,
2233 enum devlink_sb_threshold_type threshold_type,
2234 struct netlink_ext_ack *extack)
2237 const struct devlink_ops *ops = devlink->ops;
2239 if (ops->sb_pool_set)
2240 return ops->sb_pool_set(devlink, sb_index, pool_index,
2241 size, threshold_type, extack);
2245 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2246 struct genl_info *info)
2248 struct devlink *devlink = info->user_ptr[0];
2249 enum devlink_sb_threshold_type threshold_type;
2250 struct devlink_sb *devlink_sb;
2255 devlink_sb = devlink_sb_get_from_info(devlink, info);
2256 if (IS_ERR(devlink_sb))
2257 return PTR_ERR(devlink_sb);
2259 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2264 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2268 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
2271 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2272 return devlink_sb_pool_set(devlink, devlink_sb->index,
2273 pool_index, size, threshold_type,
2277 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2278 struct devlink *devlink,
2279 struct devlink_port *devlink_port,
2280 struct devlink_sb *devlink_sb,
2282 enum devlink_command cmd,
2283 u32 portid, u32 seq, int flags)
2285 const struct devlink_ops *ops = devlink->ops;
2290 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2291 pool_index, &threshold);
2295 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2299 if (devlink_nl_put_handle(msg, devlink))
2300 goto nla_put_failure;
2301 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2302 goto nla_put_failure;
2303 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2304 goto nla_put_failure;
2305 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2306 goto nla_put_failure;
2307 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2308 goto nla_put_failure;
2310 if (ops->sb_occ_port_pool_get) {
2314 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2315 pool_index, &cur, &max);
2316 if (err && err != -EOPNOTSUPP)
2317 goto sb_occ_get_failure;
2319 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2320 goto nla_put_failure;
2321 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2322 goto nla_put_failure;
2326 genlmsg_end(msg, hdr);
2332 genlmsg_cancel(msg, hdr);
2336 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2337 struct genl_info *info)
2339 struct devlink_port *devlink_port = info->user_ptr[1];
2340 struct devlink *devlink = devlink_port->devlink;
2341 struct devlink_sb *devlink_sb;
2342 struct sk_buff *msg;
2346 devlink_sb = devlink_sb_get_from_info(devlink, info);
2347 if (IS_ERR(devlink_sb))
2348 return PTR_ERR(devlink_sb);
2350 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2355 if (!devlink->ops->sb_port_pool_get)
2358 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2362 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2363 devlink_sb, pool_index,
2364 DEVLINK_CMD_SB_PORT_POOL_NEW,
2365 info->snd_portid, info->snd_seq, 0);
2371 return genlmsg_reply(msg, info);
2374 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2375 struct devlink *devlink,
2376 struct devlink_sb *devlink_sb,
2377 u32 portid, u32 seq)
2379 struct devlink_port *devlink_port;
2380 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2384 list_for_each_entry(devlink_port, &devlink->port_list, list) {
2385 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2386 if (*p_idx < start) {
2390 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2394 DEVLINK_CMD_SB_PORT_POOL_NEW,
2405 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
2406 struct netlink_callback *cb)
2408 struct devlink *devlink;
2409 struct devlink_sb *devlink_sb;
2410 int start = cb->args[0];
2411 unsigned long index;
2415 mutex_lock(&devlink_mutex);
2416 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2417 if (!devlink_try_get(devlink))
2420 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2421 !devlink->ops->sb_port_pool_get)
2424 mutex_lock(&devlink->lock);
2425 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2426 err = __sb_port_pool_get_dumpit(msg, start, &idx,
2427 devlink, devlink_sb,
2428 NETLINK_CB(cb->skb).portid,
2429 cb->nlh->nlmsg_seq);
2430 if (err == -EOPNOTSUPP) {
2433 mutex_unlock(&devlink->lock);
2434 devlink_put(devlink);
2438 mutex_unlock(&devlink->lock);
2440 devlink_put(devlink);
2443 mutex_unlock(&devlink_mutex);
2445 if (err != -EMSGSIZE)
2452 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2453 unsigned int sb_index, u16 pool_index,
2455 struct netlink_ext_ack *extack)
2458 const struct devlink_ops *ops = devlink_port->devlink->ops;
2460 if (ops->sb_port_pool_set)
2461 return ops->sb_port_pool_set(devlink_port, sb_index,
2462 pool_index, threshold, extack);
2466 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2467 struct genl_info *info)
2469 struct devlink_port *devlink_port = info->user_ptr[1];
2470 struct devlink *devlink = info->user_ptr[0];
2471 struct devlink_sb *devlink_sb;
2476 devlink_sb = devlink_sb_get_from_info(devlink, info);
2477 if (IS_ERR(devlink_sb))
2478 return PTR_ERR(devlink_sb);
2480 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2485 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
2488 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2489 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2490 pool_index, threshold, info->extack);
2494 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2495 struct devlink_port *devlink_port,
2496 struct devlink_sb *devlink_sb, u16 tc_index,
2497 enum devlink_sb_pool_type pool_type,
2498 enum devlink_command cmd,
2499 u32 portid, u32 seq, int flags)
2501 const struct devlink_ops *ops = devlink->ops;
2507 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2508 tc_index, pool_type,
2509 &pool_index, &threshold);
2513 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2517 if (devlink_nl_put_handle(msg, devlink))
2518 goto nla_put_failure;
2519 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2520 goto nla_put_failure;
2521 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2522 goto nla_put_failure;
2523 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2524 goto nla_put_failure;
2525 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2526 goto nla_put_failure;
2527 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2528 goto nla_put_failure;
2529 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2530 goto nla_put_failure;
2532 if (ops->sb_occ_tc_port_bind_get) {
2536 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2538 tc_index, pool_type,
2540 if (err && err != -EOPNOTSUPP)
2543 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2544 goto nla_put_failure;
2545 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2546 goto nla_put_failure;
2550 genlmsg_end(msg, hdr);
2554 genlmsg_cancel(msg, hdr);
2558 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2559 struct genl_info *info)
2561 struct devlink_port *devlink_port = info->user_ptr[1];
2562 struct devlink *devlink = devlink_port->devlink;
2563 struct devlink_sb *devlink_sb;
2564 struct sk_buff *msg;
2565 enum devlink_sb_pool_type pool_type;
2569 devlink_sb = devlink_sb_get_from_info(devlink, info);
2570 if (IS_ERR(devlink_sb))
2571 return PTR_ERR(devlink_sb);
2573 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2577 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2578 pool_type, &tc_index);
2582 if (!devlink->ops->sb_tc_pool_bind_get)
2585 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2589 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2590 devlink_sb, tc_index, pool_type,
2591 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2599 return genlmsg_reply(msg, info);
2602 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2603 int start, int *p_idx,
2604 struct devlink *devlink,
2605 struct devlink_sb *devlink_sb,
2606 u32 portid, u32 seq)
2608 struct devlink_port *devlink_port;
2612 list_for_each_entry(devlink_port, &devlink->port_list, list) {
2614 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
2615 if (*p_idx < start) {
2619 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2623 DEVLINK_SB_POOL_TYPE_INGRESS,
2624 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2632 tc_index < devlink_sb->egress_tc_count; tc_index++) {
2633 if (*p_idx < start) {
2637 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2641 DEVLINK_SB_POOL_TYPE_EGRESS,
2642 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2654 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2655 struct netlink_callback *cb)
2657 struct devlink *devlink;
2658 struct devlink_sb *devlink_sb;
2659 int start = cb->args[0];
2660 unsigned long index;
2664 mutex_lock(&devlink_mutex);
2665 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2666 if (!devlink_try_get(devlink))
2669 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2670 !devlink->ops->sb_tc_pool_bind_get)
2673 mutex_lock(&devlink->lock);
2674 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2675 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
2678 NETLINK_CB(cb->skb).portid,
2679 cb->nlh->nlmsg_seq);
2680 if (err == -EOPNOTSUPP) {
2683 mutex_unlock(&devlink->lock);
2684 devlink_put(devlink);
2688 mutex_unlock(&devlink->lock);
2690 devlink_put(devlink);
2693 mutex_unlock(&devlink_mutex);
2695 if (err != -EMSGSIZE)
2702 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
2703 unsigned int sb_index, u16 tc_index,
2704 enum devlink_sb_pool_type pool_type,
2705 u16 pool_index, u32 threshold,
2706 struct netlink_ext_ack *extack)
2709 const struct devlink_ops *ops = devlink_port->devlink->ops;
2711 if (ops->sb_tc_pool_bind_set)
2712 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
2713 tc_index, pool_type,
2714 pool_index, threshold, extack);
2718 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
2719 struct genl_info *info)
2721 struct devlink_port *devlink_port = info->user_ptr[1];
2722 struct devlink *devlink = info->user_ptr[0];
2723 enum devlink_sb_pool_type pool_type;
2724 struct devlink_sb *devlink_sb;
2730 devlink_sb = devlink_sb_get_from_info(devlink, info);
2731 if (IS_ERR(devlink_sb))
2732 return PTR_ERR(devlink_sb);
2734 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2738 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2739 pool_type, &tc_index);
2743 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2748 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
2751 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2752 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
2753 tc_index, pool_type,
2754 pool_index, threshold, info->extack);
2757 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
2758 struct genl_info *info)
2760 struct devlink *devlink = info->user_ptr[0];
2761 const struct devlink_ops *ops = devlink->ops;
2762 struct devlink_sb *devlink_sb;
2764 devlink_sb = devlink_sb_get_from_info(devlink, info);
2765 if (IS_ERR(devlink_sb))
2766 return PTR_ERR(devlink_sb);
2768 if (ops->sb_occ_snapshot)
2769 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
2773 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
2774 struct genl_info *info)
2776 struct devlink *devlink = info->user_ptr[0];
2777 const struct devlink_ops *ops = devlink->ops;
2778 struct devlink_sb *devlink_sb;
2780 devlink_sb = devlink_sb_get_from_info(devlink, info);
2781 if (IS_ERR(devlink_sb))
2782 return PTR_ERR(devlink_sb);
2784 if (ops->sb_occ_max_clear)
2785 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
2789 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
2790 enum devlink_command cmd, u32 portid,
2793 const struct devlink_ops *ops = devlink->ops;
2794 enum devlink_eswitch_encap_mode encap_mode;
2800 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2804 err = devlink_nl_put_handle(msg, devlink);
2806 goto nla_put_failure;
2808 if (ops->eswitch_mode_get) {
2809 err = ops->eswitch_mode_get(devlink, &mode);
2811 goto nla_put_failure;
2812 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
2814 goto nla_put_failure;
2817 if (ops->eswitch_inline_mode_get) {
2818 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
2820 goto nla_put_failure;
2821 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
2824 goto nla_put_failure;
2827 if (ops->eswitch_encap_mode_get) {
2828 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
2830 goto nla_put_failure;
2831 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
2833 goto nla_put_failure;
2836 genlmsg_end(msg, hdr);
2840 genlmsg_cancel(msg, hdr);
2844 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
2845 struct genl_info *info)
2847 struct devlink *devlink = info->user_ptr[0];
2848 struct sk_buff *msg;
2851 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2855 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
2856 info->snd_portid, info->snd_seq, 0);
2863 return genlmsg_reply(msg, info);
2866 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
2867 struct netlink_ext_ack *extack)
2869 struct devlink_rate *devlink_rate;
2871 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
2872 if (devlink_rate_is_node(devlink_rate)) {
2873 NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
2879 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
2880 struct genl_info *info)
2882 struct devlink *devlink = info->user_ptr[0];
2883 const struct devlink_ops *ops = devlink->ops;
2884 enum devlink_eswitch_encap_mode encap_mode;
2889 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
2890 if (!ops->eswitch_mode_set)
2892 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
2893 err = devlink_rate_nodes_check(devlink, mode, info->extack);
2896 err = ops->eswitch_mode_set(devlink, mode, info->extack);
2901 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2902 if (!ops->eswitch_inline_mode_set)
2904 inline_mode = nla_get_u8(
2905 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
2906 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
2912 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2913 if (!ops->eswitch_encap_mode_set)
2915 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
2916 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
2925 int devlink_dpipe_match_put(struct sk_buff *skb,
2926 struct devlink_dpipe_match *match)
2928 struct devlink_dpipe_header *header = match->header;
2929 struct devlink_dpipe_field *field = &header->fields[match->field_id];
2930 struct nlattr *match_attr;
2932 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
2936 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
2937 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
2938 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2939 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2940 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2941 goto nla_put_failure;
2943 nla_nest_end(skb, match_attr);
2947 nla_nest_cancel(skb, match_attr);
2950 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
2952 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
2953 struct sk_buff *skb)
2955 struct nlattr *matches_attr;
2957 matches_attr = nla_nest_start_noflag(skb,
2958 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
2962 if (table->table_ops->matches_dump(table->priv, skb))
2963 goto nla_put_failure;
2965 nla_nest_end(skb, matches_attr);
2969 nla_nest_cancel(skb, matches_attr);
2973 int devlink_dpipe_action_put(struct sk_buff *skb,
2974 struct devlink_dpipe_action *action)
2976 struct devlink_dpipe_header *header = action->header;
2977 struct devlink_dpipe_field *field = &header->fields[action->field_id];
2978 struct nlattr *action_attr;
2980 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
2984 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
2985 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
2986 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2987 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2988 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2989 goto nla_put_failure;
2991 nla_nest_end(skb, action_attr);
2995 nla_nest_cancel(skb, action_attr);
2998 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
3000 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
3001 struct sk_buff *skb)
3003 struct nlattr *actions_attr;
3005 actions_attr = nla_nest_start_noflag(skb,
3006 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
3010 if (table->table_ops->actions_dump(table->priv, skb))
3011 goto nla_put_failure;
3013 nla_nest_end(skb, actions_attr);
3017 nla_nest_cancel(skb, actions_attr);
3021 static int devlink_dpipe_table_put(struct sk_buff *skb,
3022 struct devlink_dpipe_table *table)
3024 struct nlattr *table_attr;
3027 table_size = table->table_ops->size_get(table->priv);
3028 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
3032 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
3033 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
3035 goto nla_put_failure;
3036 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
3037 table->counters_enabled))
3038 goto nla_put_failure;
3040 if (table->resource_valid) {
3041 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
3042 table->resource_id, DEVLINK_ATTR_PAD) ||
3043 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
3044 table->resource_units, DEVLINK_ATTR_PAD))
3045 goto nla_put_failure;
3047 if (devlink_dpipe_matches_put(table, skb))
3048 goto nla_put_failure;
3050 if (devlink_dpipe_actions_put(table, skb))
3051 goto nla_put_failure;
3053 nla_nest_end(skb, table_attr);
3057 nla_nest_cancel(skb, table_attr);
3061 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
3062 struct genl_info *info)
3067 err = genlmsg_reply(*pskb, info);
3071 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3077 static int devlink_dpipe_tables_fill(struct genl_info *info,
3078 enum devlink_command cmd, int flags,
3079 struct list_head *dpipe_tables,
3080 const char *table_name)
3082 struct devlink *devlink = info->user_ptr[0];
3083 struct devlink_dpipe_table *table;
3084 struct nlattr *tables_attr;
3085 struct sk_buff *skb = NULL;
3086 struct nlmsghdr *nlh;
3092 table = list_first_entry(dpipe_tables,
3093 struct devlink_dpipe_table, list);
3095 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3099 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3100 &devlink_nl_family, NLM_F_MULTI, cmd);
3106 if (devlink_nl_put_handle(skb, devlink))
3107 goto nla_put_failure;
3108 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3110 goto nla_put_failure;
3114 list_for_each_entry_from(table, dpipe_tables, list) {
3116 err = devlink_dpipe_table_put(skb, table);
3124 if (!strcmp(table->name, table_name)) {
3125 err = devlink_dpipe_table_put(skb, table);
3133 nla_nest_end(skb, tables_attr);
3134 genlmsg_end(skb, hdr);
3139 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3140 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3142 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3148 return genlmsg_reply(skb, info);
3157 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3158 struct genl_info *info)
3160 struct devlink *devlink = info->user_ptr[0];
3161 const char *table_name = NULL;
3163 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3164 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3166 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3167 &devlink->dpipe_table_list,
3171 static int devlink_dpipe_value_put(struct sk_buff *skb,
3172 struct devlink_dpipe_value *value)
3174 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3175 value->value_size, value->value))
3178 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3179 value->value_size, value->mask))
3181 if (value->mapping_valid)
3182 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3183 value->mapping_value))
3188 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3189 struct devlink_dpipe_value *value)
3193 if (devlink_dpipe_action_put(skb, value->action))
3195 if (devlink_dpipe_value_put(skb, value))
3200 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3201 struct devlink_dpipe_value *values,
3202 unsigned int values_count)
3204 struct nlattr *action_attr;
3208 for (i = 0; i < values_count; i++) {
3209 action_attr = nla_nest_start_noflag(skb,
3210 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3213 err = devlink_dpipe_action_value_put(skb, &values[i]);
3215 goto err_action_value_put;
3216 nla_nest_end(skb, action_attr);
3220 err_action_value_put:
3221 nla_nest_cancel(skb, action_attr);
3225 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3226 struct devlink_dpipe_value *value)
3230 if (devlink_dpipe_match_put(skb, value->match))
3232 if (devlink_dpipe_value_put(skb, value))
3237 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3238 struct devlink_dpipe_value *values,
3239 unsigned int values_count)
3241 struct nlattr *match_attr;
3245 for (i = 0; i < values_count; i++) {
3246 match_attr = nla_nest_start_noflag(skb,
3247 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3250 err = devlink_dpipe_match_value_put(skb, &values[i]);
3252 goto err_match_value_put;
3253 nla_nest_end(skb, match_attr);
3257 err_match_value_put:
3258 nla_nest_cancel(skb, match_attr);
3262 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3263 struct devlink_dpipe_entry *entry)
3265 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3268 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3272 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3274 goto nla_put_failure;
3275 if (entry->counter_valid)
3276 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3277 entry->counter, DEVLINK_ATTR_PAD))
3278 goto nla_put_failure;
3280 matches_attr = nla_nest_start_noflag(skb,
3281 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3283 goto nla_put_failure;
3285 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3286 entry->match_values_count);
3288 nla_nest_cancel(skb, matches_attr);
3289 goto err_match_values_put;
3291 nla_nest_end(skb, matches_attr);
3293 actions_attr = nla_nest_start_noflag(skb,
3294 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3296 goto nla_put_failure;
3298 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3299 entry->action_values_count);
3301 nla_nest_cancel(skb, actions_attr);
3302 goto err_action_values_put;
3304 nla_nest_end(skb, actions_attr);
3306 nla_nest_end(skb, entry_attr);
3311 err_match_values_put:
3312 err_action_values_put:
3313 nla_nest_cancel(skb, entry_attr);
3317 static struct devlink_dpipe_table *
3318 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3319 const char *table_name, struct devlink *devlink)
3321 struct devlink_dpipe_table *table;
3322 list_for_each_entry_rcu(table, dpipe_tables, list,
3323 lockdep_is_held(&devlink->lock)) {
3324 if (!strcmp(table->name, table_name))
3330 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3332 struct devlink *devlink;
3335 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3340 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3341 dump_ctx->info->snd_portid,
3342 dump_ctx->info->snd_seq,
3343 &devlink_nl_family, NLM_F_MULTI,
3346 goto nla_put_failure;
3348 devlink = dump_ctx->info->user_ptr[0];
3349 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3350 goto nla_put_failure;
3351 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3352 DEVLINK_ATTR_DPIPE_ENTRIES);
3353 if (!dump_ctx->nest)
3354 goto nla_put_failure;
3358 nlmsg_free(dump_ctx->skb);
3361 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3363 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3364 struct devlink_dpipe_entry *entry)
3366 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3368 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3370 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3372 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3373 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3376 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3378 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3381 unsigned int value_count, value_index;
3382 struct devlink_dpipe_value *value;
3384 value = entry->action_values;
3385 value_count = entry->action_values_count;
3386 for (value_index = 0; value_index < value_count; value_index++) {
3387 kfree(value[value_index].value);
3388 kfree(value[value_index].mask);
3391 value = entry->match_values;
3392 value_count = entry->match_values_count;
3393 for (value_index = 0; value_index < value_count; value_index++) {
3394 kfree(value[value_index].value);
3395 kfree(value[value_index].mask);
3398 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3400 static int devlink_dpipe_entries_fill(struct genl_info *info,
3401 enum devlink_command cmd, int flags,
3402 struct devlink_dpipe_table *table)
3404 struct devlink_dpipe_dump_ctx dump_ctx;
3405 struct nlmsghdr *nlh;
3408 dump_ctx.skb = NULL;
3410 dump_ctx.info = info;
3412 err = table->table_ops->entries_dump(table->priv,
3413 table->counters_enabled,
3419 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3420 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3422 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3427 return genlmsg_reply(dump_ctx.skb, info);
3430 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3431 struct genl_info *info)
3433 struct devlink *devlink = info->user_ptr[0];
3434 struct devlink_dpipe_table *table;
3435 const char *table_name;
3437 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3440 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3441 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3442 table_name, devlink);
3446 if (!table->table_ops->entries_dump)
3449 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3453 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3454 const struct devlink_dpipe_header *header)
3456 struct devlink_dpipe_field *field;
3457 struct nlattr *field_attr;
3460 for (i = 0; i < header->fields_count; i++) {
3461 field = &header->fields[i];
3462 field_attr = nla_nest_start_noflag(skb,
3463 DEVLINK_ATTR_DPIPE_FIELD);
3466 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3467 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3468 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3469 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3470 goto nla_put_failure;
3471 nla_nest_end(skb, field_attr);
3476 nla_nest_cancel(skb, field_attr);
3480 static int devlink_dpipe_header_put(struct sk_buff *skb,
3481 struct devlink_dpipe_header *header)
3483 struct nlattr *fields_attr, *header_attr;
3486 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3490 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3491 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3492 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3493 goto nla_put_failure;
3495 fields_attr = nla_nest_start_noflag(skb,
3496 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3498 goto nla_put_failure;
3500 err = devlink_dpipe_fields_put(skb, header);
3502 nla_nest_cancel(skb, fields_attr);
3503 goto nla_put_failure;
3505 nla_nest_end(skb, fields_attr);
3506 nla_nest_end(skb, header_attr);
3511 nla_nest_cancel(skb, header_attr);
3515 static int devlink_dpipe_headers_fill(struct genl_info *info,
3516 enum devlink_command cmd, int flags,
3517 struct devlink_dpipe_headers *
3520 struct devlink *devlink = info->user_ptr[0];
3521 struct nlattr *headers_attr;
3522 struct sk_buff *skb = NULL;
3523 struct nlmsghdr *nlh;
3530 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3534 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3535 &devlink_nl_family, NLM_F_MULTI, cmd);
3541 if (devlink_nl_put_handle(skb, devlink))
3542 goto nla_put_failure;
3543 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3545 goto nla_put_failure;
3548 for (; i < dpipe_headers->headers_count; i++) {
3549 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3557 nla_nest_end(skb, headers_attr);
3558 genlmsg_end(skb, hdr);
3559 if (i != dpipe_headers->headers_count)
3563 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3564 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3566 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3571 return genlmsg_reply(skb, info);
3580 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3581 struct genl_info *info)
3583 struct devlink *devlink = info->user_ptr[0];
3585 if (!devlink->dpipe_headers)
3587 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3588 0, devlink->dpipe_headers);
3591 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3592 const char *table_name,
3595 struct devlink_dpipe_table *table;
3597 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3598 table_name, devlink);
3602 if (table->counter_control_extern)
3605 if (!(table->counters_enabled ^ enable))
3608 table->counters_enabled = enable;
3609 if (table->table_ops->counters_set_update)
3610 table->table_ops->counters_set_update(table->priv, enable);
3614 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
3615 struct genl_info *info)
3617 struct devlink *devlink = info->user_ptr[0];
3618 const char *table_name;
3619 bool counters_enable;
3621 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
3622 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
3625 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3626 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
3628 return devlink_dpipe_table_counters_set(devlink, table_name,
3632 static struct devlink_resource *
3633 devlink_resource_find(struct devlink *devlink,
3634 struct devlink_resource *resource, u64 resource_id)
3636 struct list_head *resource_list;
3639 resource_list = &resource->resource_list;
3641 resource_list = &devlink->resource_list;
3643 list_for_each_entry(resource, resource_list, list) {
3644 struct devlink_resource *child_resource;
3646 if (resource->id == resource_id)
3649 child_resource = devlink_resource_find(devlink, resource,
3652 return child_resource;
3658 devlink_resource_validate_children(struct devlink_resource *resource)
3660 struct devlink_resource *child_resource;
3661 bool size_valid = true;
3664 if (list_empty(&resource->resource_list))
3667 list_for_each_entry(child_resource, &resource->resource_list, list)
3668 parts_size += child_resource->size_new;
3670 if (parts_size > resource->size_new)
3673 resource->size_valid = size_valid;
3677 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
3678 struct netlink_ext_ack *extack)
3683 if (size > resource->size_params.size_max) {
3684 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
3688 if (size < resource->size_params.size_min) {
3689 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
3693 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
3695 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
3702 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
3703 struct genl_info *info)
3705 struct devlink *devlink = info->user_ptr[0];
3706 struct devlink_resource *resource;
3711 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
3712 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
3714 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
3716 resource = devlink_resource_find(devlink, NULL, resource_id);
3720 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
3721 err = devlink_resource_validate_size(resource, size, info->extack);
3725 resource->size_new = size;
3726 devlink_resource_validate_children(resource);
3727 if (resource->parent)
3728 devlink_resource_validate_children(resource->parent);
3733 devlink_resource_size_params_put(struct devlink_resource *resource,
3734 struct sk_buff *skb)
3736 struct devlink_resource_size_params *size_params;
3738 size_params = &resource->size_params;
3739 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
3740 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
3741 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
3742 size_params->size_max, DEVLINK_ATTR_PAD) ||
3743 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
3744 size_params->size_min, DEVLINK_ATTR_PAD) ||
3745 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
3750 static int devlink_resource_occ_put(struct devlink_resource *resource,
3751 struct sk_buff *skb)
3753 if (!resource->occ_get)
3755 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
3756 resource->occ_get(resource->occ_get_priv),
3760 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
3761 struct devlink_resource *resource)
3763 struct devlink_resource *child_resource;
3764 struct nlattr *child_resource_attr;
3765 struct nlattr *resource_attr;
3767 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
3771 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
3772 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
3773 DEVLINK_ATTR_PAD) ||
3774 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
3776 goto nla_put_failure;
3777 if (resource->size != resource->size_new)
3778 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
3779 resource->size_new, DEVLINK_ATTR_PAD);
3780 if (devlink_resource_occ_put(resource, skb))
3781 goto nla_put_failure;
3782 if (devlink_resource_size_params_put(resource, skb))
3783 goto nla_put_failure;
3784 if (list_empty(&resource->resource_list))
3787 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
3788 resource->size_valid))
3789 goto nla_put_failure;
3791 child_resource_attr = nla_nest_start_noflag(skb,
3792 DEVLINK_ATTR_RESOURCE_LIST);
3793 if (!child_resource_attr)
3794 goto nla_put_failure;
3796 list_for_each_entry(child_resource, &resource->resource_list, list) {
3797 if (devlink_resource_put(devlink, skb, child_resource))
3798 goto resource_put_failure;
3801 nla_nest_end(skb, child_resource_attr);
3803 nla_nest_end(skb, resource_attr);
3806 resource_put_failure:
3807 nla_nest_cancel(skb, child_resource_attr);
3809 nla_nest_cancel(skb, resource_attr);
3813 static int devlink_resource_fill(struct genl_info *info,
3814 enum devlink_command cmd, int flags)
3816 struct devlink *devlink = info->user_ptr[0];
3817 struct devlink_resource *resource;
3818 struct nlattr *resources_attr;
3819 struct sk_buff *skb = NULL;
3820 struct nlmsghdr *nlh;
3826 resource = list_first_entry(&devlink->resource_list,
3827 struct devlink_resource, list);
3829 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3833 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3834 &devlink_nl_family, NLM_F_MULTI, cmd);
3840 if (devlink_nl_put_handle(skb, devlink))
3841 goto nla_put_failure;
3843 resources_attr = nla_nest_start_noflag(skb,
3844 DEVLINK_ATTR_RESOURCE_LIST);
3845 if (!resources_attr)
3846 goto nla_put_failure;
3850 list_for_each_entry_from(resource, &devlink->resource_list, list) {
3851 err = devlink_resource_put(devlink, skb, resource);
3854 goto err_resource_put;
3860 nla_nest_end(skb, resources_attr);
3861 genlmsg_end(skb, hdr);
3865 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3866 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3868 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3873 return genlmsg_reply(skb, info);
3882 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
3883 struct genl_info *info)
3885 struct devlink *devlink = info->user_ptr[0];
3887 if (list_empty(&devlink->resource_list))
3890 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
3894 devlink_resources_validate(struct devlink *devlink,
3895 struct devlink_resource *resource,
3896 struct genl_info *info)
3898 struct list_head *resource_list;
3902 resource_list = &resource->resource_list;
3904 resource_list = &devlink->resource_list;
3906 list_for_each_entry(resource, resource_list, list) {
3907 if (!resource->size_valid)
3909 err = devlink_resources_validate(devlink, resource, info);
3916 static struct net *devlink_netns_get(struct sk_buff *skb,
3917 struct genl_info *info)
3919 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
3920 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
3921 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
3924 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
3925 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
3926 return ERR_PTR(-EINVAL);
3929 if (netns_pid_attr) {
3930 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
3931 } else if (netns_fd_attr) {
3932 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
3933 } else if (netns_id_attr) {
3934 net = get_net_ns_by_id(sock_net(skb->sk),
3935 nla_get_u32(netns_id_attr));
3937 net = ERR_PTR(-EINVAL);
3940 net = ERR_PTR(-EINVAL);
3943 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
3944 return ERR_PTR(-EINVAL);
3946 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
3948 return ERR_PTR(-EPERM);
3953 static void devlink_param_notify(struct devlink *devlink,
3954 unsigned int port_index,
3955 struct devlink_param_item *param_item,
3956 enum devlink_command cmd);
3958 static void devlink_ns_change_notify(struct devlink *devlink,
3959 struct net *dest_net, struct net *curr_net,
3962 struct devlink_param_item *param_item;
3963 enum devlink_command cmd;
3965 /* Userspace needs to be notified about devlink objects
3966 * removed from original and entering new network namespace.
3967 * The rest of the devlink objects are re-created during
3968 * reload process so the notifications are generated separatelly.
3971 if (!dest_net || net_eq(dest_net, curr_net))
3975 devlink_notify(devlink, DEVLINK_CMD_NEW);
3977 cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
3978 list_for_each_entry(param_item, &devlink->param_list, list)
3979 devlink_param_notify(devlink, 0, param_item, cmd);
3982 devlink_notify(devlink, DEVLINK_CMD_DEL);
3985 static bool devlink_reload_supported(const struct devlink_ops *ops)
3987 return ops->reload_down && ops->reload_up;
3990 static void devlink_reload_failed_set(struct devlink *devlink,
3993 if (devlink->reload_failed == reload_failed)
3995 devlink->reload_failed = reload_failed;
3996 devlink_notify(devlink, DEVLINK_CMD_NEW);
3999 bool devlink_is_reload_failed(const struct devlink *devlink)
4001 return devlink->reload_failed;
4003 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
4006 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
4007 enum devlink_reload_limit limit, u32 actions_performed)
4009 unsigned long actions = actions_performed;
4013 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
4014 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
4015 reload_stats[stat_idx]++;
4017 devlink_notify(devlink, DEVLINK_CMD_NEW);
4021 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
4022 u32 actions_performed)
4024 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
4029 * devlink_remote_reload_actions_performed - Update devlink on reload actions
4030 * performed which are not a direct result of devlink reload call.
4032 * This should be called by a driver after performing reload actions in case it was not
4033 * a result of devlink reload call. For example fw_activate was performed as a result
4034 * of devlink reload triggered fw_activate on another host.
4035 * The motivation for this function is to keep data on reload actions performed on this
4036 * function whether it was done due to direct devlink reload call or not.
4039 * @limit: reload limit
4040 * @actions_performed: bitmask of actions performed
4042 void devlink_remote_reload_actions_performed(struct devlink *devlink,
4043 enum devlink_reload_limit limit,
4044 u32 actions_performed)
4046 if (WARN_ON(!actions_performed ||
4047 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
4048 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
4049 limit > DEVLINK_RELOAD_LIMIT_MAX))
4052 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
4055 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
4057 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
4058 enum devlink_reload_action action, enum devlink_reload_limit limit,
4059 u32 *actions_performed, struct netlink_ext_ack *extack)
4061 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
4062 struct net *curr_net;
4065 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
4066 sizeof(remote_reload_stats));
4068 curr_net = devlink_net(devlink);
4069 devlink_ns_change_notify(devlink, dest_net, curr_net, false);
4070 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
4074 if (dest_net && !net_eq(dest_net, curr_net))
4075 write_pnet(&devlink->_net, dest_net);
4077 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
4078 devlink_reload_failed_set(devlink, !!err);
4082 devlink_ns_change_notify(devlink, dest_net, curr_net, true);
4083 WARN_ON(!(*actions_performed & BIT(action)));
4084 /* Catch driver on updating the remote action within devlink reload */
4085 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
4086 sizeof(remote_reload_stats)));
4087 devlink_reload_stats_update(devlink, limit, *actions_performed);
4092 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
4093 enum devlink_command cmd, struct genl_info *info)
4095 struct sk_buff *msg;
4098 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4102 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
4106 if (devlink_nl_put_handle(msg, devlink))
4107 goto nla_put_failure;
4109 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4111 goto nla_put_failure;
4112 genlmsg_end(msg, hdr);
4114 return genlmsg_reply(msg, info);
4117 genlmsg_cancel(msg, hdr);
4123 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4125 struct devlink *devlink = info->user_ptr[0];
4126 enum devlink_reload_action action;
4127 enum devlink_reload_limit limit;
4128 struct net *dest_net = NULL;
4129 u32 actions_performed;
4132 if (!(devlink->features & DEVLINK_F_RELOAD))
4135 err = devlink_resources_validate(devlink, NULL, info);
4137 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4141 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4142 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4144 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4146 if (!devlink_reload_action_is_supported(devlink, action)) {
4147 NL_SET_ERR_MSG_MOD(info->extack,
4148 "Requested reload action is not supported by the driver");
4152 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4153 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4154 struct nla_bitfield32 limits;
4155 u32 limits_selected;
4157 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4158 limits_selected = limits.value & limits.selector;
4159 if (!limits_selected) {
4160 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4163 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4164 if (limits_selected & BIT(limit))
4166 /* UAPI enables multiselection, but currently it is not used */
4167 if (limits_selected != BIT(limit)) {
4168 NL_SET_ERR_MSG_MOD(info->extack,
4169 "Multiselection of limit is not supported");
4172 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4173 NL_SET_ERR_MSG_MOD(info->extack,
4174 "Requested limit is not supported by the driver");
4177 if (devlink_reload_combination_is_invalid(action, limit)) {
4178 NL_SET_ERR_MSG_MOD(info->extack,
4179 "Requested limit is invalid for this action");
4183 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4184 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4185 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4186 dest_net = devlink_netns_get(skb, info);
4187 if (IS_ERR(dest_net))
4188 return PTR_ERR(dest_net);
4191 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4198 /* For backward compatibility generate reply only if attributes used by user */
4199 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4202 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4203 DEVLINK_CMD_RELOAD, info);
4206 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4207 struct devlink *devlink,
4208 enum devlink_command cmd,
4209 struct devlink_flash_notify *params)
4213 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4217 if (devlink_nl_put_handle(msg, devlink))
4218 goto nla_put_failure;
4220 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4223 if (params->status_msg &&
4224 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4225 params->status_msg))
4226 goto nla_put_failure;
4227 if (params->component &&
4228 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4230 goto nla_put_failure;
4231 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4232 params->done, DEVLINK_ATTR_PAD))
4233 goto nla_put_failure;
4234 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4235 params->total, DEVLINK_ATTR_PAD))
4236 goto nla_put_failure;
4237 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4238 params->timeout, DEVLINK_ATTR_PAD))
4239 goto nla_put_failure;
4242 genlmsg_end(msg, hdr);
4246 genlmsg_cancel(msg, hdr);
4250 static void __devlink_flash_update_notify(struct devlink *devlink,
4251 enum devlink_command cmd,
4252 struct devlink_flash_notify *params)
4254 struct sk_buff *msg;
4257 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4258 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4259 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4261 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4264 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4268 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4272 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4273 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4280 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4282 struct devlink_flash_notify params = {};
4284 __devlink_flash_update_notify(devlink,
4285 DEVLINK_CMD_FLASH_UPDATE,
4289 static void devlink_flash_update_end_notify(struct devlink *devlink)
4291 struct devlink_flash_notify params = {};
4293 __devlink_flash_update_notify(devlink,
4294 DEVLINK_CMD_FLASH_UPDATE_END,
4298 void devlink_flash_update_status_notify(struct devlink *devlink,
4299 const char *status_msg,
4300 const char *component,
4302 unsigned long total)
4304 struct devlink_flash_notify params = {
4305 .status_msg = status_msg,
4306 .component = component,
4311 __devlink_flash_update_notify(devlink,
4312 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4315 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4317 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4318 const char *status_msg,
4319 const char *component,
4320 unsigned long timeout)
4322 struct devlink_flash_notify params = {
4323 .status_msg = status_msg,
4324 .component = component,
4328 __devlink_flash_update_notify(devlink,
4329 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4332 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4334 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4335 struct genl_info *info)
4337 struct nlattr *nla_component, *nla_overwrite_mask, *nla_file_name;
4338 struct devlink_flash_update_params params = {};
4339 struct devlink *devlink = info->user_ptr[0];
4340 const char *file_name;
4341 u32 supported_params;
4344 if (!devlink->ops->flash_update)
4347 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
4350 supported_params = devlink->ops->supported_flash_update_params;
4352 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
4353 if (nla_component) {
4354 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
4355 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
4356 "component update is not supported by this device");
4359 params.component = nla_data(nla_component);
4362 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4363 if (nla_overwrite_mask) {
4364 struct nla_bitfield32 sections;
4366 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4367 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4368 "overwrite settings are not supported by this device");
4371 sections = nla_get_bitfield32(nla_overwrite_mask);
4372 params.overwrite_mask = sections.value & sections.selector;
4375 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4376 file_name = nla_data(nla_file_name);
4377 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
4379 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4383 devlink_flash_update_begin_notify(devlink);
4384 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
4385 devlink_flash_update_end_notify(devlink);
4387 release_firmware(params.fw);
4392 static const struct devlink_param devlink_param_generic[] = {
4394 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
4395 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
4396 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
4399 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
4400 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
4401 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
4404 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
4405 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
4406 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
4409 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
4410 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
4411 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
4414 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
4415 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
4416 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
4419 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
4420 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
4421 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
4424 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
4425 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
4426 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
4429 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
4430 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
4431 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
4434 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
4435 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
4436 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
4439 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
4440 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
4441 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
4444 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
4445 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
4446 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
4449 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
4450 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
4451 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
4454 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
4455 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
4456 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
4459 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
4460 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
4461 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
4464 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
4465 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
4466 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
4469 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
4470 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
4471 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
4474 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
4475 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
4476 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
4480 static int devlink_param_generic_verify(const struct devlink_param *param)
4482 /* verify it match generic parameter by id and name */
4483 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
4485 if (strcmp(param->name, devlink_param_generic[param->id].name))
4488 WARN_ON(param->type != devlink_param_generic[param->id].type);
4493 static int devlink_param_driver_verify(const struct devlink_param *param)
4497 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
4499 /* verify no such name in generic params */
4500 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
4501 if (!strcmp(param->name, devlink_param_generic[i].name))
4507 static struct devlink_param_item *
4508 devlink_param_find_by_name(struct list_head *param_list,
4509 const char *param_name)
4511 struct devlink_param_item *param_item;
4513 list_for_each_entry(param_item, param_list, list)
4514 if (!strcmp(param_item->param->name, param_name))
4519 static struct devlink_param_item *
4520 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
4522 struct devlink_param_item *param_item;
4524 list_for_each_entry(param_item, param_list, list)
4525 if (param_item->param->id == param_id)
4531 devlink_param_cmode_is_supported(const struct devlink_param *param,
4532 enum devlink_param_cmode cmode)
4534 return test_bit(cmode, ¶m->supported_cmodes);
4537 static int devlink_param_get(struct devlink *devlink,
4538 const struct devlink_param *param,
4539 struct devlink_param_gset_ctx *ctx)
4543 return param->get(devlink, param->id, ctx);
4546 static int devlink_param_set(struct devlink *devlink,
4547 const struct devlink_param *param,
4548 struct devlink_param_gset_ctx *ctx)
4552 return param->set(devlink, param->id, ctx);
4556 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
4558 switch (param_type) {
4559 case DEVLINK_PARAM_TYPE_U8:
4561 case DEVLINK_PARAM_TYPE_U16:
4563 case DEVLINK_PARAM_TYPE_U32:
4565 case DEVLINK_PARAM_TYPE_STRING:
4567 case DEVLINK_PARAM_TYPE_BOOL:
4575 devlink_nl_param_value_fill_one(struct sk_buff *msg,
4576 enum devlink_param_type type,
4577 enum devlink_param_cmode cmode,
4578 union devlink_param_value val)
4580 struct nlattr *param_value_attr;
4582 param_value_attr = nla_nest_start_noflag(msg,
4583 DEVLINK_ATTR_PARAM_VALUE);
4584 if (!param_value_attr)
4585 goto nla_put_failure;
4587 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
4588 goto value_nest_cancel;
4591 case DEVLINK_PARAM_TYPE_U8:
4592 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
4593 goto value_nest_cancel;
4595 case DEVLINK_PARAM_TYPE_U16:
4596 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
4597 goto value_nest_cancel;
4599 case DEVLINK_PARAM_TYPE_U32:
4600 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
4601 goto value_nest_cancel;
4603 case DEVLINK_PARAM_TYPE_STRING:
4604 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
4606 goto value_nest_cancel;
4608 case DEVLINK_PARAM_TYPE_BOOL:
4610 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
4611 goto value_nest_cancel;
4615 nla_nest_end(msg, param_value_attr);
4619 nla_nest_cancel(msg, param_value_attr);
4624 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
4625 unsigned int port_index,
4626 struct devlink_param_item *param_item,
4627 enum devlink_command cmd,
4628 u32 portid, u32 seq, int flags)
4630 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
4631 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
4632 const struct devlink_param *param = param_item->param;
4633 struct devlink_param_gset_ctx ctx;
4634 struct nlattr *param_values_list;
4635 struct nlattr *param_attr;
4641 /* Get value from driver part to driverinit configuration mode */
4642 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
4643 if (!devlink_param_cmode_is_supported(param, i))
4645 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4646 if (!param_item->driverinit_value_valid)
4648 param_value[i] = param_item->driverinit_value;
4651 err = devlink_param_get(devlink, param, &ctx);
4654 param_value[i] = ctx.val;
4656 param_value_set[i] = true;
4659 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4663 if (devlink_nl_put_handle(msg, devlink))
4664 goto genlmsg_cancel;
4666 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
4667 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
4668 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
4669 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
4670 goto genlmsg_cancel;
4672 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
4674 goto genlmsg_cancel;
4675 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
4676 goto param_nest_cancel;
4677 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
4678 goto param_nest_cancel;
4680 nla_type = devlink_param_type_to_nla_type(param->type);
4682 goto param_nest_cancel;
4683 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
4684 goto param_nest_cancel;
4686 param_values_list = nla_nest_start_noflag(msg,
4687 DEVLINK_ATTR_PARAM_VALUES_LIST);
4688 if (!param_values_list)
4689 goto param_nest_cancel;
4691 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
4692 if (!param_value_set[i])
4694 err = devlink_nl_param_value_fill_one(msg, param->type,
4697 goto values_list_nest_cancel;
4700 nla_nest_end(msg, param_values_list);
4701 nla_nest_end(msg, param_attr);
4702 genlmsg_end(msg, hdr);
4705 values_list_nest_cancel:
4706 nla_nest_end(msg, param_values_list);
4708 nla_nest_cancel(msg, param_attr);
4710 genlmsg_cancel(msg, hdr);
4714 static void devlink_param_notify(struct devlink *devlink,
4715 unsigned int port_index,
4716 struct devlink_param_item *param_item,
4717 enum devlink_command cmd)
4719 struct sk_buff *msg;
4722 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
4723 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
4724 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
4725 ASSERT_DEVLINK_REGISTERED(devlink);
4727 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4730 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
4737 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4738 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4741 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
4742 struct netlink_callback *cb)
4744 struct devlink_param_item *param_item;
4745 struct devlink *devlink;
4746 int start = cb->args[0];
4747 unsigned long index;
4751 mutex_lock(&devlink_mutex);
4752 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
4753 if (!devlink_try_get(devlink))
4756 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4759 mutex_lock(&devlink->lock);
4760 list_for_each_entry(param_item, &devlink->param_list, list) {
4765 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
4766 DEVLINK_CMD_PARAM_GET,
4767 NETLINK_CB(cb->skb).portid,
4770 if (err == -EOPNOTSUPP) {
4773 mutex_unlock(&devlink->lock);
4774 devlink_put(devlink);
4779 mutex_unlock(&devlink->lock);
4781 devlink_put(devlink);
4784 mutex_unlock(&devlink_mutex);
4786 if (err != -EMSGSIZE)
4794 devlink_param_type_get_from_info(struct genl_info *info,
4795 enum devlink_param_type *param_type)
4797 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
4800 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
4802 *param_type = DEVLINK_PARAM_TYPE_U8;
4805 *param_type = DEVLINK_PARAM_TYPE_U16;
4808 *param_type = DEVLINK_PARAM_TYPE_U32;
4811 *param_type = DEVLINK_PARAM_TYPE_STRING;
4814 *param_type = DEVLINK_PARAM_TYPE_BOOL;
4824 devlink_param_value_get_from_info(const struct devlink_param *param,
4825 struct genl_info *info,
4826 union devlink_param_value *value)
4828 struct nlattr *param_data;
4831 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
4833 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
4836 switch (param->type) {
4837 case DEVLINK_PARAM_TYPE_U8:
4838 if (nla_len(param_data) != sizeof(u8))
4840 value->vu8 = nla_get_u8(param_data);
4842 case DEVLINK_PARAM_TYPE_U16:
4843 if (nla_len(param_data) != sizeof(u16))
4845 value->vu16 = nla_get_u16(param_data);
4847 case DEVLINK_PARAM_TYPE_U32:
4848 if (nla_len(param_data) != sizeof(u32))
4850 value->vu32 = nla_get_u32(param_data);
4852 case DEVLINK_PARAM_TYPE_STRING:
4853 len = strnlen(nla_data(param_data), nla_len(param_data));
4854 if (len == nla_len(param_data) ||
4855 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
4857 strcpy(value->vstr, nla_data(param_data));
4859 case DEVLINK_PARAM_TYPE_BOOL:
4860 if (param_data && nla_len(param_data))
4862 value->vbool = nla_get_flag(param_data);
4868 static struct devlink_param_item *
4869 devlink_param_get_from_info(struct list_head *param_list,
4870 struct genl_info *info)
4874 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
4877 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
4878 return devlink_param_find_by_name(param_list, param_name);
4881 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
4882 struct genl_info *info)
4884 struct devlink *devlink = info->user_ptr[0];
4885 struct devlink_param_item *param_item;
4886 struct sk_buff *msg;
4889 param_item = devlink_param_get_from_info(&devlink->param_list, info);
4893 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4897 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
4898 DEVLINK_CMD_PARAM_GET,
4899 info->snd_portid, info->snd_seq, 0);
4905 return genlmsg_reply(msg, info);
4908 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
4909 unsigned int port_index,
4910 struct list_head *param_list,
4911 struct genl_info *info,
4912 enum devlink_command cmd)
4914 enum devlink_param_type param_type;
4915 struct devlink_param_gset_ctx ctx;
4916 enum devlink_param_cmode cmode;
4917 struct devlink_param_item *param_item;
4918 const struct devlink_param *param;
4919 union devlink_param_value value;
4922 param_item = devlink_param_get_from_info(param_list, info);
4925 param = param_item->param;
4926 err = devlink_param_type_get_from_info(info, ¶m_type);
4929 if (param_type != param->type)
4931 err = devlink_param_value_get_from_info(param, info, &value);
4934 if (param->validate) {
4935 err = param->validate(devlink, param->id, value, info->extack);
4940 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
4942 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
4943 if (!devlink_param_cmode_is_supported(param, cmode))
4946 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4947 if (param->type == DEVLINK_PARAM_TYPE_STRING)
4948 strcpy(param_item->driverinit_value.vstr, value.vstr);
4950 param_item->driverinit_value = value;
4951 param_item->driverinit_value_valid = true;
4957 err = devlink_param_set(devlink, param, &ctx);
4962 devlink_param_notify(devlink, port_index, param_item, cmd);
4966 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
4967 struct genl_info *info)
4969 struct devlink *devlink = info->user_ptr[0];
4971 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
4972 info, DEVLINK_CMD_PARAM_NEW);
4975 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
4976 struct netlink_callback *cb)
4978 struct devlink_param_item *param_item;
4979 struct devlink_port *devlink_port;
4980 struct devlink *devlink;
4981 int start = cb->args[0];
4982 unsigned long index;
4986 mutex_lock(&devlink_mutex);
4987 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
4988 if (!devlink_try_get(devlink))
4991 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4994 mutex_lock(&devlink->lock);
4995 list_for_each_entry(devlink_port, &devlink->port_list, list) {
4996 list_for_each_entry(param_item,
4997 &devlink_port->param_list, list) {
5002 err = devlink_nl_param_fill(msg,
5003 devlink_port->devlink,
5004 devlink_port->index, param_item,
5005 DEVLINK_CMD_PORT_PARAM_GET,
5006 NETLINK_CB(cb->skb).portid,
5009 if (err == -EOPNOTSUPP) {
5012 mutex_unlock(&devlink->lock);
5013 devlink_put(devlink);
5019 mutex_unlock(&devlink->lock);
5021 devlink_put(devlink);
5024 mutex_unlock(&devlink_mutex);
5026 if (err != -EMSGSIZE)
5033 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
5034 struct genl_info *info)
5036 struct devlink_port *devlink_port = info->user_ptr[1];
5037 struct devlink_param_item *param_item;
5038 struct sk_buff *msg;
5041 param_item = devlink_param_get_from_info(&devlink_port->param_list,
5046 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5050 err = devlink_nl_param_fill(msg, devlink_port->devlink,
5051 devlink_port->index, param_item,
5052 DEVLINK_CMD_PORT_PARAM_GET,
5053 info->snd_portid, info->snd_seq, 0);
5059 return genlmsg_reply(msg, info);
5062 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
5063 struct genl_info *info)
5065 struct devlink_port *devlink_port = info->user_ptr[1];
5067 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
5068 devlink_port->index,
5069 &devlink_port->param_list, info,
5070 DEVLINK_CMD_PORT_PARAM_NEW);
5073 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
5074 struct devlink *devlink,
5075 struct devlink_snapshot *snapshot)
5077 struct nlattr *snap_attr;
5080 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5084 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5086 goto nla_put_failure;
5088 nla_nest_end(msg, snap_attr);
5092 nla_nest_cancel(msg, snap_attr);
5096 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5097 struct devlink *devlink,
5098 struct devlink_region *region)
5100 struct devlink_snapshot *snapshot;
5101 struct nlattr *snapshots_attr;
5104 snapshots_attr = nla_nest_start_noflag(msg,
5105 DEVLINK_ATTR_REGION_SNAPSHOTS);
5106 if (!snapshots_attr)
5109 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
5110 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5112 goto nla_put_failure;
5115 nla_nest_end(msg, snapshots_attr);
5119 nla_nest_cancel(msg, snapshots_attr);
5123 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5124 enum devlink_command cmd, u32 portid,
5126 struct devlink_region *region)
5131 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5135 err = devlink_nl_put_handle(msg, devlink);
5137 goto nla_put_failure;
5140 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5141 region->port->index);
5143 goto nla_put_failure;
5146 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5148 goto nla_put_failure;
5150 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5154 goto nla_put_failure;
5156 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
5157 region->max_snapshots);
5159 goto nla_put_failure;
5161 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5163 goto nla_put_failure;
5165 genlmsg_end(msg, hdr);
5169 genlmsg_cancel(msg, hdr);
5173 static struct sk_buff *
5174 devlink_nl_region_notify_build(struct devlink_region *region,
5175 struct devlink_snapshot *snapshot,
5176 enum devlink_command cmd, u32 portid, u32 seq)
5178 struct devlink *devlink = region->devlink;
5179 struct sk_buff *msg;
5184 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5186 return ERR_PTR(-ENOMEM);
5188 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5194 err = devlink_nl_put_handle(msg, devlink);
5196 goto out_cancel_msg;
5199 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5200 region->port->index);
5202 goto out_cancel_msg;
5205 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5208 goto out_cancel_msg;
5211 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5214 goto out_cancel_msg;
5216 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5217 region->size, DEVLINK_ATTR_PAD);
5219 goto out_cancel_msg;
5221 genlmsg_end(msg, hdr);
5226 genlmsg_cancel(msg, hdr);
5229 return ERR_PTR(err);
5232 static void devlink_nl_region_notify(struct devlink_region *region,
5233 struct devlink_snapshot *snapshot,
5234 enum devlink_command cmd)
5236 struct devlink *devlink = region->devlink;
5237 struct sk_buff *msg;
5239 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5240 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
5243 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5247 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
5248 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5252 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5253 * @devlink: devlink instance
5254 * @id: the snapshot id
5256 * Track when a new snapshot begins using an id. Load the count for the
5257 * given id from the snapshot xarray, increment it, and store it back.
5259 * Called when a new snapshot is created with the given id.
5261 * The id *must* have been previously allocated by
5262 * devlink_region_snapshot_id_get().
5264 * Returns 0 on success, or an error on failure.
5266 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5268 unsigned long count;
5271 lockdep_assert_held(&devlink->lock);
5273 p = xa_load(&devlink->snapshot_ids, id);
5277 if (WARN_ON(!xa_is_value(p)))
5280 count = xa_to_value(p);
5283 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5288 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5289 * @devlink: devlink instance
5290 * @id: the snapshot id
5292 * Track when a snapshot is deleted and stops using an id. Load the count
5293 * for the given id from the snapshot xarray, decrement it, and store it
5296 * If the count reaches zero, erase this id from the xarray, freeing it
5297 * up for future re-use by devlink_region_snapshot_id_get().
5299 * Called when a snapshot using the given id is deleted, and when the
5300 * initial allocator of the id is finished using it.
5302 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5304 unsigned long count;
5307 lockdep_assert_held(&devlink->lock);
5309 p = xa_load(&devlink->snapshot_ids, id);
5313 if (WARN_ON(!xa_is_value(p)))
5316 count = xa_to_value(p);
5320 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5323 /* If this was the last user, we can erase this id */
5324 xa_erase(&devlink->snapshot_ids, id);
5329 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
5330 * @devlink: devlink instance
5331 * @id: the snapshot id
5333 * Mark the given snapshot id as used by inserting a zero value into the
5336 * This must be called while holding the devlink instance lock. Unlike
5337 * devlink_snapshot_id_get, the initial reference count is zero, not one.
5338 * It is expected that the id will immediately be used before
5339 * releasing the devlink instance lock.
5341 * Returns zero on success, or an error code if the snapshot id could not
5344 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5346 lockdep_assert_held(&devlink->lock);
5348 if (xa_load(&devlink->snapshot_ids, id))
5351 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5356 * __devlink_region_snapshot_id_get - get snapshot ID
5357 * @devlink: devlink instance
5358 * @id: storage to return snapshot id
5360 * Allocates a new snapshot id. Returns zero on success, or a negative
5361 * error on failure. Must be called while holding the devlink instance
5364 * Snapshot IDs are tracked using an xarray which stores the number of
5365 * users of the snapshot id.
5367 * Note that the caller of this function counts as a 'user', in order to
5368 * avoid race conditions. The caller must release its hold on the
5369 * snapshot by using devlink_region_snapshot_id_put.
5371 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5373 lockdep_assert_held(&devlink->lock);
5375 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5376 xa_limit_32b, GFP_KERNEL);
5380 * __devlink_region_snapshot_create - create a new snapshot
5381 * This will add a new snapshot of a region. The snapshot
5382 * will be stored on the region struct and can be accessed
5383 * from devlink. This is useful for future analyses of snapshots.
5384 * Multiple snapshots can be created on a region.
5385 * The @snapshot_id should be obtained using the getter function.
5387 * Must be called only while holding the devlink instance lock.
5389 * @region: devlink region of the snapshot
5390 * @data: snapshot data
5391 * @snapshot_id: snapshot id to be created
5394 __devlink_region_snapshot_create(struct devlink_region *region,
5395 u8 *data, u32 snapshot_id)
5397 struct devlink *devlink = region->devlink;
5398 struct devlink_snapshot *snapshot;
5401 lockdep_assert_held(&devlink->lock);
5403 /* check if region can hold one more snapshot */
5404 if (region->cur_snapshots == region->max_snapshots)
5407 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
5410 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5414 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
5416 goto err_snapshot_id_increment;
5418 snapshot->id = snapshot_id;
5419 snapshot->region = region;
5420 snapshot->data = data;
5422 list_add_tail(&snapshot->list, ®ion->snapshot_list);
5424 region->cur_snapshots++;
5426 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5429 err_snapshot_id_increment:
5434 static void devlink_region_snapshot_del(struct devlink_region *region,
5435 struct devlink_snapshot *snapshot)
5437 struct devlink *devlink = region->devlink;
5439 lockdep_assert_held(&devlink->lock);
5441 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
5442 region->cur_snapshots--;
5443 list_del(&snapshot->list);
5444 region->ops->destructor(snapshot->data);
5445 __devlink_snapshot_id_decrement(devlink, snapshot->id);
5449 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
5450 struct genl_info *info)
5452 struct devlink *devlink = info->user_ptr[0];
5453 struct devlink_port *port = NULL;
5454 struct devlink_region *region;
5455 const char *region_name;
5456 struct sk_buff *msg;
5460 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
5463 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5464 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5466 port = devlink_port_get_by_index(devlink, index);
5471 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5473 region = devlink_port_region_get_by_name(port, region_name);
5475 region = devlink_region_get_by_name(devlink, region_name);
5480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5484 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
5485 info->snd_portid, info->snd_seq, 0,
5492 return genlmsg_reply(msg, info);
5495 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
5496 struct netlink_callback *cb,
5497 struct devlink_port *port,
5501 struct devlink_region *region;
5504 list_for_each_entry(region, &port->region_list, list) {
5509 err = devlink_nl_region_fill(msg, port->devlink,
5510 DEVLINK_CMD_REGION_GET,
5511 NETLINK_CB(cb->skb).portid,
5513 NLM_F_MULTI, region);
5523 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
5524 struct netlink_callback *cb,
5525 struct devlink *devlink,
5529 struct devlink_region *region;
5530 struct devlink_port *port;
5533 mutex_lock(&devlink->lock);
5534 list_for_each_entry(region, &devlink->region_list, list) {
5539 err = devlink_nl_region_fill(msg, devlink,
5540 DEVLINK_CMD_REGION_GET,
5541 NETLINK_CB(cb->skb).portid,
5543 NLM_F_MULTI, region);
5549 list_for_each_entry(port, &devlink->port_list, list) {
5550 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
5557 mutex_unlock(&devlink->lock);
5561 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
5562 struct netlink_callback *cb)
5564 struct devlink *devlink;
5565 int start = cb->args[0];
5566 unsigned long index;
5570 mutex_lock(&devlink_mutex);
5571 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
5572 if (!devlink_try_get(devlink))
5575 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5578 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
5581 devlink_put(devlink);
5586 mutex_unlock(&devlink_mutex);
5591 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
5592 struct genl_info *info)
5594 struct devlink *devlink = info->user_ptr[0];
5595 struct devlink_snapshot *snapshot;
5596 struct devlink_port *port = NULL;
5597 struct devlink_region *region;
5598 const char *region_name;
5602 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
5603 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
5606 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5607 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
5609 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5610 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5612 port = devlink_port_get_by_index(devlink, index);
5618 region = devlink_port_region_get_by_name(port, region_name);
5620 region = devlink_region_get_by_name(devlink, region_name);
5625 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
5629 devlink_region_snapshot_del(region, snapshot);
5634 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
5636 struct devlink *devlink = info->user_ptr[0];
5637 struct devlink_snapshot *snapshot;
5638 struct devlink_port *port = NULL;
5639 struct nlattr *snapshot_id_attr;
5640 struct devlink_region *region;
5641 const char *region_name;
5647 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
5648 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
5652 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5654 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5655 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5657 port = devlink_port_get_by_index(devlink, index);
5663 region = devlink_port_region_get_by_name(port, region_name);
5665 region = devlink_region_get_by_name(devlink, region_name);
5668 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
5672 if (!region->ops->snapshot) {
5673 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
5677 if (region->cur_snapshots == region->max_snapshots) {
5678 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
5682 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
5683 if (snapshot_id_attr) {
5684 snapshot_id = nla_get_u32(snapshot_id_attr);
5686 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
5687 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
5691 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
5695 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
5697 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
5703 err = region->port_ops->snapshot(port, region->port_ops,
5704 info->extack, &data);
5706 err = region->ops->snapshot(devlink, region->ops,
5707 info->extack, &data);
5709 goto err_snapshot_capture;
5711 err = __devlink_region_snapshot_create(region, data, snapshot_id);
5713 goto err_snapshot_create;
5715 if (!snapshot_id_attr) {
5716 struct sk_buff *msg;
5718 snapshot = devlink_region_snapshot_get_by_id(region,
5720 if (WARN_ON(!snapshot))
5723 msg = devlink_nl_region_notify_build(region, snapshot,
5724 DEVLINK_CMD_REGION_NEW,
5727 err = PTR_ERR_OR_ZERO(msg);
5731 err = genlmsg_reply(msg, info);
5738 err_snapshot_create:
5739 region->ops->destructor(data);
5740 err_snapshot_capture:
5741 __devlink_snapshot_id_decrement(devlink, snapshot_id);
5745 devlink_region_snapshot_del(region, snapshot);
5749 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
5750 struct devlink *devlink,
5751 u8 *chunk, u32 chunk_size,
5754 struct nlattr *chunk_attr;
5757 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
5761 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
5763 goto nla_put_failure;
5765 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
5768 goto nla_put_failure;
5770 nla_nest_end(msg, chunk_attr);
5774 nla_nest_cancel(msg, chunk_attr);
5778 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
5780 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
5781 struct devlink *devlink,
5782 struct devlink_region *region,
5783 struct nlattr **attrs,
5788 struct devlink_snapshot *snapshot;
5789 u64 curr_offset = start_offset;
5793 *new_offset = start_offset;
5795 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
5796 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
5800 while (curr_offset < end_offset) {
5804 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
5805 data_size = end_offset - curr_offset;
5807 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
5809 data = &snapshot->data[curr_offset];
5810 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
5816 curr_offset += data_size;
5818 *new_offset = curr_offset;
5823 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
5824 struct netlink_callback *cb)
5826 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
5827 u64 ret_offset, start_offset, end_offset = U64_MAX;
5828 struct nlattr **attrs = info->attrs;
5829 struct devlink_port *port = NULL;
5830 struct devlink_region *region;
5831 struct nlattr *chunks_attr;
5832 const char *region_name;
5833 struct devlink *devlink;
5838 start_offset = *((u64 *)&cb->args[0]);
5840 mutex_lock(&devlink_mutex);
5841 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
5842 if (IS_ERR(devlink)) {
5843 err = PTR_ERR(devlink);
5847 mutex_lock(&devlink->lock);
5849 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
5850 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
5855 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5856 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5858 port = devlink_port_get_by_index(devlink, index);
5865 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
5868 region = devlink_port_region_get_by_name(port, region_name);
5870 region = devlink_region_get_by_name(devlink, region_name);
5877 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
5878 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
5881 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
5883 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
5884 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
5887 if (end_offset > region->size)
5888 end_offset = region->size;
5890 /* return 0 if there is no further data to read */
5891 if (start_offset == end_offset) {
5896 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5897 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
5898 DEVLINK_CMD_REGION_READ);
5904 err = devlink_nl_put_handle(skb, devlink);
5906 goto nla_put_failure;
5909 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
5910 region->port->index);
5912 goto nla_put_failure;
5915 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
5917 goto nla_put_failure;
5919 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
5922 goto nla_put_failure;
5925 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
5928 end_offset, &ret_offset);
5930 if (err && err != -EMSGSIZE)
5931 goto nla_put_failure;
5933 /* Check if there was any progress done to prevent infinite loop */
5934 if (ret_offset == start_offset) {
5936 goto nla_put_failure;
5939 *((u64 *)&cb->args[0]) = ret_offset;
5941 nla_nest_end(skb, chunks_attr);
5942 genlmsg_end(skb, hdr);
5943 mutex_unlock(&devlink->lock);
5944 devlink_put(devlink);
5945 mutex_unlock(&devlink_mutex);
5950 genlmsg_cancel(skb, hdr);
5952 mutex_unlock(&devlink->lock);
5953 devlink_put(devlink);
5955 mutex_unlock(&devlink_mutex);
5959 struct devlink_info_req {
5960 struct sk_buff *msg;
5963 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
5965 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
5967 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
5969 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
5971 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
5973 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
5975 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
5978 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
5981 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
5983 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
5984 const char *version_name,
5985 const char *version_value)
5987 struct nlattr *nest;
5990 nest = nla_nest_start_noflag(req->msg, attr);
5994 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
5997 goto nla_put_failure;
5999 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
6002 goto nla_put_failure;
6004 nla_nest_end(req->msg, nest);
6009 nla_nest_cancel(req->msg, nest);
6013 int devlink_info_version_fixed_put(struct devlink_info_req *req,
6014 const char *version_name,
6015 const char *version_value)
6017 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
6018 version_name, version_value);
6020 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
6022 int devlink_info_version_stored_put(struct devlink_info_req *req,
6023 const char *version_name,
6024 const char *version_value)
6026 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6027 version_name, version_value);
6029 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
6031 int devlink_info_version_running_put(struct devlink_info_req *req,
6032 const char *version_name,
6033 const char *version_value)
6035 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6036 version_name, version_value);
6038 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
6041 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
6042 enum devlink_command cmd, u32 portid,
6043 u32 seq, int flags, struct netlink_ext_ack *extack)
6045 struct devlink_info_req req;
6049 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6054 if (devlink_nl_put_handle(msg, devlink))
6055 goto err_cancel_msg;
6058 err = devlink->ops->info_get(devlink, &req, extack);
6060 goto err_cancel_msg;
6062 genlmsg_end(msg, hdr);
6066 genlmsg_cancel(msg, hdr);
6070 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
6071 struct genl_info *info)
6073 struct devlink *devlink = info->user_ptr[0];
6074 struct sk_buff *msg;
6077 if (!devlink->ops->info_get)
6080 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6084 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6085 info->snd_portid, info->snd_seq, 0,
6092 return genlmsg_reply(msg, info);
6095 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
6096 struct netlink_callback *cb)
6098 struct devlink *devlink;
6099 int start = cb->args[0];
6100 unsigned long index;
6104 mutex_lock(&devlink_mutex);
6105 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
6106 if (!devlink_try_get(devlink))
6109 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6112 if (idx < start || !devlink->ops->info_get)
6115 mutex_lock(&devlink->lock);
6116 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6117 NETLINK_CB(cb->skb).portid,
6118 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6120 mutex_unlock(&devlink->lock);
6121 if (err == -EOPNOTSUPP)
6124 devlink_put(devlink);
6130 devlink_put(devlink);
6132 mutex_unlock(&devlink_mutex);
6134 if (err != -EMSGSIZE)
6141 struct devlink_fmsg_item {
6142 struct list_head list;
6149 struct devlink_fmsg {
6150 struct list_head item_list;
6151 bool putting_binary; /* This flag forces enclosing of binary data
6152 * in an array brackets. It forces using
6153 * of designated API:
6154 * devlink_fmsg_binary_pair_nest_start()
6155 * devlink_fmsg_binary_pair_nest_end()
6159 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6161 struct devlink_fmsg *fmsg;
6163 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6167 INIT_LIST_HEAD(&fmsg->item_list);
6172 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6174 struct devlink_fmsg_item *item, *tmp;
6176 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6177 list_del(&item->list);
6183 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6186 struct devlink_fmsg_item *item;
6188 item = kzalloc(sizeof(*item), GFP_KERNEL);
6192 item->attrtype = attrtype;
6193 list_add_tail(&item->list, &fmsg->item_list);
6198 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6200 if (fmsg->putting_binary)
6203 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6205 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6207 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6209 if (fmsg->putting_binary)
6212 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6215 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6217 if (fmsg->putting_binary)
6220 return devlink_fmsg_nest_end(fmsg);
6222 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6224 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6226 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6228 struct devlink_fmsg_item *item;
6230 if (fmsg->putting_binary)
6233 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6236 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6240 item->nla_type = NLA_NUL_STRING;
6241 item->len = strlen(name) + 1;
6242 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6243 memcpy(&item->value, name, item->len);
6244 list_add_tail(&item->list, &fmsg->item_list);
6249 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6253 if (fmsg->putting_binary)
6256 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6260 err = devlink_fmsg_put_name(fmsg, name);
6266 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6268 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6270 if (fmsg->putting_binary)
6273 return devlink_fmsg_nest_end(fmsg);
6275 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6277 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6282 if (fmsg->putting_binary)
6285 err = devlink_fmsg_pair_nest_start(fmsg, name);
6289 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6295 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6297 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6301 if (fmsg->putting_binary)
6304 err = devlink_fmsg_nest_end(fmsg);
6308 err = devlink_fmsg_nest_end(fmsg);
6314 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6316 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6321 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6325 fmsg->putting_binary = true;
6328 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6330 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6332 if (!fmsg->putting_binary)
6335 fmsg->putting_binary = false;
6336 return devlink_fmsg_arr_pair_nest_end(fmsg);
6338 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6340 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6341 const void *value, u16 value_len,
6344 struct devlink_fmsg_item *item;
6346 if (value_len > DEVLINK_FMSG_MAX_SIZE)
6349 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
6353 item->nla_type = value_nla_type;
6354 item->len = value_len;
6355 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6356 memcpy(&item->value, value, item->len);
6357 list_add_tail(&item->list, &fmsg->item_list);
6362 static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
6364 if (fmsg->putting_binary)
6367 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
6370 static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
6372 if (fmsg->putting_binary)
6375 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
6378 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
6380 if (fmsg->putting_binary)
6383 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
6385 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
6387 static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
6389 if (fmsg->putting_binary)
6392 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
6395 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
6397 if (fmsg->putting_binary)
6400 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
6403 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
6405 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
6408 if (!fmsg->putting_binary)
6411 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
6413 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
6415 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
6420 err = devlink_fmsg_pair_nest_start(fmsg, name);
6424 err = devlink_fmsg_bool_put(fmsg, value);
6428 err = devlink_fmsg_pair_nest_end(fmsg);
6434 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
6436 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
6441 err = devlink_fmsg_pair_nest_start(fmsg, name);
6445 err = devlink_fmsg_u8_put(fmsg, value);
6449 err = devlink_fmsg_pair_nest_end(fmsg);
6455 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
6457 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
6462 err = devlink_fmsg_pair_nest_start(fmsg, name);
6466 err = devlink_fmsg_u32_put(fmsg, value);
6470 err = devlink_fmsg_pair_nest_end(fmsg);
6476 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
6478 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
6483 err = devlink_fmsg_pair_nest_start(fmsg, name);
6487 err = devlink_fmsg_u64_put(fmsg, value);
6491 err = devlink_fmsg_pair_nest_end(fmsg);
6497 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
6499 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
6504 err = devlink_fmsg_pair_nest_start(fmsg, name);
6508 err = devlink_fmsg_string_put(fmsg, value);
6512 err = devlink_fmsg_pair_nest_end(fmsg);
6518 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
6520 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
6521 const void *value, u32 value_len)
6528 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
6532 for (offset = 0; offset < value_len; offset += data_size) {
6533 data_size = value_len - offset;
6534 if (data_size > DEVLINK_FMSG_MAX_SIZE)
6535 data_size = DEVLINK_FMSG_MAX_SIZE;
6536 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
6539 /* Exit from loop with a break (instead of
6540 * return) to make sure putting_binary is turned off in
6541 * devlink_fmsg_binary_pair_nest_end
6545 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
6551 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
6554 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
6556 switch (msg->nla_type) {
6561 case NLA_NUL_STRING:
6563 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
6571 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
6573 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6576 switch (msg->nla_type) {
6578 /* Always provide flag data, regardless of its value */
6579 tmp = *(bool *) msg->value;
6581 return nla_put_u8(skb, attrtype, tmp);
6583 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
6585 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
6587 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
6589 case NLA_NUL_STRING:
6590 return nla_put_string(skb, attrtype, (char *) &msg->value);
6592 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
6599 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
6602 struct devlink_fmsg_item *item;
6603 struct nlattr *fmsg_nlattr;
6607 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
6611 list_for_each_entry(item, &fmsg->item_list, list) {
6617 switch (item->attrtype) {
6618 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
6619 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
6620 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
6621 case DEVLINK_ATTR_FMSG_NEST_END:
6622 err = nla_put_flag(skb, item->attrtype);
6624 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
6625 err = devlink_fmsg_item_fill_type(item, skb);
6628 err = devlink_fmsg_item_fill_data(item, skb);
6630 case DEVLINK_ATTR_FMSG_OBJ_NAME:
6631 err = nla_put_string(skb, item->attrtype,
6632 (char *) &item->value);
6644 nla_nest_end(skb, fmsg_nlattr);
6648 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
6649 struct genl_info *info,
6650 enum devlink_command cmd, int flags)
6652 struct nlmsghdr *nlh;
6653 struct sk_buff *skb;
6660 int tmp_index = index;
6662 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
6666 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
6667 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
6670 goto nla_put_failure;
6673 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
6676 else if (err != -EMSGSIZE || tmp_index == index)
6677 goto nla_put_failure;
6679 genlmsg_end(skb, hdr);
6680 err = genlmsg_reply(skb, info);
6685 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
6688 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
6689 NLMSG_DONE, 0, flags | NLM_F_MULTI);
6692 goto nla_put_failure;
6695 return genlmsg_reply(skb, info);
6702 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
6703 struct netlink_callback *cb,
6704 enum devlink_command cmd)
6706 int index = cb->args[0];
6707 int tmp_index = index;
6711 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6712 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
6715 goto nla_put_failure;
6718 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
6719 if ((err && err != -EMSGSIZE) || tmp_index == index)
6720 goto nla_put_failure;
6722 cb->args[0] = index;
6723 genlmsg_end(skb, hdr);
6727 genlmsg_cancel(skb, hdr);
6731 struct devlink_health_reporter {
6732 struct list_head list;
6734 const struct devlink_health_reporter_ops *ops;
6735 struct devlink *devlink;
6736 struct devlink_port *devlink_port;
6737 struct devlink_fmsg *dump_fmsg;
6738 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
6739 u64 graceful_period;
6747 u64 last_recovery_ts;
6748 refcount_t refcount;
6752 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
6754 return reporter->priv;
6756 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
6758 static struct devlink_health_reporter *
6759 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
6760 struct mutex *list_lock,
6761 const char *reporter_name)
6763 struct devlink_health_reporter *reporter;
6765 lockdep_assert_held(list_lock);
6766 list_for_each_entry(reporter, reporter_list, list)
6767 if (!strcmp(reporter->ops->name, reporter_name))
6772 static struct devlink_health_reporter *
6773 devlink_health_reporter_find_by_name(struct devlink *devlink,
6774 const char *reporter_name)
6776 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
6777 &devlink->reporters_lock,
6781 static struct devlink_health_reporter *
6782 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
6783 const char *reporter_name)
6785 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
6786 &devlink_port->reporters_lock,
6790 static struct devlink_health_reporter *
6791 __devlink_health_reporter_create(struct devlink *devlink,
6792 const struct devlink_health_reporter_ops *ops,
6793 u64 graceful_period, void *priv)
6795 struct devlink_health_reporter *reporter;
6797 if (WARN_ON(graceful_period && !ops->recover))
6798 return ERR_PTR(-EINVAL);
6800 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
6802 return ERR_PTR(-ENOMEM);
6804 reporter->priv = priv;
6805 reporter->ops = ops;
6806 reporter->devlink = devlink;
6807 reporter->graceful_period = graceful_period;
6808 reporter->auto_recover = !!ops->recover;
6809 reporter->auto_dump = !!ops->dump;
6810 mutex_init(&reporter->dump_lock);
6811 refcount_set(&reporter->refcount, 1);
6816 * devlink_port_health_reporter_create - create devlink health reporter for
6817 * specified port instance
6819 * @port: devlink_port which should contain the new reporter
6821 * @graceful_period: to avoid recovery loops, in msecs
6824 struct devlink_health_reporter *
6825 devlink_port_health_reporter_create(struct devlink_port *port,
6826 const struct devlink_health_reporter_ops *ops,
6827 u64 graceful_period, void *priv)
6829 struct devlink_health_reporter *reporter;
6831 mutex_lock(&port->reporters_lock);
6832 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
6833 &port->reporters_lock, ops->name)) {
6834 reporter = ERR_PTR(-EEXIST);
6838 reporter = __devlink_health_reporter_create(port->devlink, ops,
6839 graceful_period, priv);
6840 if (IS_ERR(reporter))
6843 reporter->devlink_port = port;
6844 list_add_tail(&reporter->list, &port->reporter_list);
6846 mutex_unlock(&port->reporters_lock);
6849 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
6852 * devlink_health_reporter_create - create devlink health reporter
6856 * @graceful_period: to avoid recovery loops, in msecs
6859 struct devlink_health_reporter *
6860 devlink_health_reporter_create(struct devlink *devlink,
6861 const struct devlink_health_reporter_ops *ops,
6862 u64 graceful_period, void *priv)
6864 struct devlink_health_reporter *reporter;
6866 mutex_lock(&devlink->reporters_lock);
6867 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
6868 reporter = ERR_PTR(-EEXIST);
6872 reporter = __devlink_health_reporter_create(devlink, ops,
6873 graceful_period, priv);
6874 if (IS_ERR(reporter))
6877 list_add_tail(&reporter->list, &devlink->reporter_list);
6879 mutex_unlock(&devlink->reporters_lock);
6882 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
6885 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
6887 mutex_destroy(&reporter->dump_lock);
6888 if (reporter->dump_fmsg)
6889 devlink_fmsg_free(reporter->dump_fmsg);
6894 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
6896 if (refcount_dec_and_test(&reporter->refcount))
6897 devlink_health_reporter_free(reporter);
6901 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
6903 list_del(&reporter->list);
6904 devlink_health_reporter_put(reporter);
6908 * devlink_health_reporter_destroy - destroy devlink health reporter
6910 * @reporter: devlink health reporter to destroy
6913 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
6915 struct mutex *lock = &reporter->devlink->reporters_lock;
6918 __devlink_health_reporter_destroy(reporter);
6921 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
6924 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
6926 * @reporter: devlink health reporter to destroy
6929 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
6931 struct mutex *lock = &reporter->devlink_port->reporters_lock;
6934 __devlink_health_reporter_destroy(reporter);
6937 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
6940 devlink_nl_health_reporter_fill(struct sk_buff *msg,
6941 struct devlink_health_reporter *reporter,
6942 enum devlink_command cmd, u32 portid,
6945 struct devlink *devlink = reporter->devlink;
6946 struct nlattr *reporter_attr;
6949 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6953 if (devlink_nl_put_handle(msg, devlink))
6954 goto genlmsg_cancel;
6956 if (reporter->devlink_port) {
6957 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
6958 goto genlmsg_cancel;
6960 reporter_attr = nla_nest_start_noflag(msg,
6961 DEVLINK_ATTR_HEALTH_REPORTER);
6963 goto genlmsg_cancel;
6964 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
6965 reporter->ops->name))
6966 goto reporter_nest_cancel;
6967 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
6968 reporter->health_state))
6969 goto reporter_nest_cancel;
6970 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
6971 reporter->error_count, DEVLINK_ATTR_PAD))
6972 goto reporter_nest_cancel;
6973 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
6974 reporter->recovery_count, DEVLINK_ATTR_PAD))
6975 goto reporter_nest_cancel;
6976 if (reporter->ops->recover &&
6977 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
6978 reporter->graceful_period,
6980 goto reporter_nest_cancel;
6981 if (reporter->ops->recover &&
6982 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
6983 reporter->auto_recover))
6984 goto reporter_nest_cancel;
6985 if (reporter->dump_fmsg &&
6986 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
6987 jiffies_to_msecs(reporter->dump_ts),
6989 goto reporter_nest_cancel;
6990 if (reporter->dump_fmsg &&
6991 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
6992 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
6993 goto reporter_nest_cancel;
6994 if (reporter->ops->dump &&
6995 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
6996 reporter->auto_dump))
6997 goto reporter_nest_cancel;
6999 nla_nest_end(msg, reporter_attr);
7000 genlmsg_end(msg, hdr);
7003 reporter_nest_cancel:
7004 nla_nest_end(msg, reporter_attr);
7006 genlmsg_cancel(msg, hdr);
7010 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7011 enum devlink_command cmd)
7013 struct devlink *devlink = reporter->devlink;
7014 struct sk_buff *msg;
7017 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7018 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7020 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7024 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7030 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7031 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7035 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7037 reporter->recovery_count++;
7038 reporter->last_recovery_ts = jiffies;
7040 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7043 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7044 void *priv_ctx, struct netlink_ext_ack *extack)
7048 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7051 if (!reporter->ops->recover)
7054 err = reporter->ops->recover(reporter, priv_ctx, extack);
7058 devlink_health_reporter_recovery_done(reporter);
7059 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7060 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7066 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7068 if (!reporter->dump_fmsg)
7070 devlink_fmsg_free(reporter->dump_fmsg);
7071 reporter->dump_fmsg = NULL;
7074 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7076 struct netlink_ext_ack *extack)
7080 if (!reporter->ops->dump)
7083 if (reporter->dump_fmsg)
7086 reporter->dump_fmsg = devlink_fmsg_alloc();
7087 if (!reporter->dump_fmsg) {
7092 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7096 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7101 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7105 reporter->dump_ts = jiffies;
7106 reporter->dump_real_ts = ktime_get_real_ns();
7111 devlink_health_dump_clear(reporter);
7115 int devlink_health_report(struct devlink_health_reporter *reporter,
7116 const char *msg, void *priv_ctx)
7118 enum devlink_health_reporter_state prev_health_state;
7119 struct devlink *devlink = reporter->devlink;
7120 unsigned long recover_ts_threshold;
7122 /* write a log message of the current error */
7124 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7125 reporter->error_count++;
7126 prev_health_state = reporter->health_state;
7127 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7128 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7130 /* abort if the previous error wasn't recovered */
7131 recover_ts_threshold = reporter->last_recovery_ts +
7132 msecs_to_jiffies(reporter->graceful_period);
7133 if (reporter->auto_recover &&
7134 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7135 (reporter->last_recovery_ts && reporter->recovery_count &&
7136 time_is_after_jiffies(recover_ts_threshold)))) {
7137 trace_devlink_health_recover_aborted(devlink,
7138 reporter->ops->name,
7139 reporter->health_state,
7141 reporter->last_recovery_ts);
7145 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7147 if (reporter->auto_dump) {
7148 mutex_lock(&reporter->dump_lock);
7149 /* store current dump of current error, for later analysis */
7150 devlink_health_do_dump(reporter, priv_ctx, NULL);
7151 mutex_unlock(&reporter->dump_lock);
7154 if (reporter->auto_recover)
7155 return devlink_health_reporter_recover(reporter,
7160 EXPORT_SYMBOL_GPL(devlink_health_report);
7162 static struct devlink_health_reporter *
7163 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7164 struct nlattr **attrs)
7166 struct devlink_health_reporter *reporter;
7167 struct devlink_port *devlink_port;
7168 char *reporter_name;
7170 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7173 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7174 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7175 if (IS_ERR(devlink_port)) {
7176 mutex_lock(&devlink->reporters_lock);
7177 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7179 refcount_inc(&reporter->refcount);
7180 mutex_unlock(&devlink->reporters_lock);
7182 mutex_lock(&devlink_port->reporters_lock);
7183 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7185 refcount_inc(&reporter->refcount);
7186 mutex_unlock(&devlink_port->reporters_lock);
7192 static struct devlink_health_reporter *
7193 devlink_health_reporter_get_from_info(struct devlink *devlink,
7194 struct genl_info *info)
7196 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7199 static struct devlink_health_reporter *
7200 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7202 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7203 struct devlink_health_reporter *reporter;
7204 struct nlattr **attrs = info->attrs;
7205 struct devlink *devlink;
7207 mutex_lock(&devlink_mutex);
7208 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
7209 if (IS_ERR(devlink))
7212 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7213 devlink_put(devlink);
7214 mutex_unlock(&devlink_mutex);
7217 mutex_unlock(&devlink_mutex);
7222 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7223 enum devlink_health_reporter_state state)
7225 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7226 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7229 if (reporter->health_state == state)
7232 reporter->health_state = state;
7233 trace_devlink_health_reporter_state_update(reporter->devlink,
7234 reporter->ops->name, state);
7235 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7237 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7239 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7240 struct genl_info *info)
7242 struct devlink *devlink = info->user_ptr[0];
7243 struct devlink_health_reporter *reporter;
7244 struct sk_buff *msg;
7247 reporter = devlink_health_reporter_get_from_info(devlink, info);
7251 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7257 err = devlink_nl_health_reporter_fill(msg, reporter,
7258 DEVLINK_CMD_HEALTH_REPORTER_GET,
7259 info->snd_portid, info->snd_seq,
7266 err = genlmsg_reply(msg, info);
7268 devlink_health_reporter_put(reporter);
7273 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7274 struct netlink_callback *cb)
7276 struct devlink_health_reporter *reporter;
7277 struct devlink_port *port;
7278 struct devlink *devlink;
7279 int start = cb->args[0];
7280 unsigned long index;
7284 mutex_lock(&devlink_mutex);
7285 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7286 if (!devlink_try_get(devlink))
7289 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7292 mutex_lock(&devlink->reporters_lock);
7293 list_for_each_entry(reporter, &devlink->reporter_list,
7299 err = devlink_nl_health_reporter_fill(
7300 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7301 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7304 mutex_unlock(&devlink->reporters_lock);
7305 devlink_put(devlink);
7310 mutex_unlock(&devlink->reporters_lock);
7312 devlink_put(devlink);
7315 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7316 if (!devlink_try_get(devlink))
7319 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7322 mutex_lock(&devlink->lock);
7323 list_for_each_entry(port, &devlink->port_list, list) {
7324 mutex_lock(&port->reporters_lock);
7325 list_for_each_entry(reporter, &port->reporter_list, list) {
7330 err = devlink_nl_health_reporter_fill(
7332 DEVLINK_CMD_HEALTH_REPORTER_GET,
7333 NETLINK_CB(cb->skb).portid,
7334 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7336 mutex_unlock(&port->reporters_lock);
7337 mutex_unlock(&devlink->lock);
7338 devlink_put(devlink);
7343 mutex_unlock(&port->reporters_lock);
7345 mutex_unlock(&devlink->lock);
7347 devlink_put(devlink);
7350 mutex_unlock(&devlink_mutex);
7357 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7358 struct genl_info *info)
7360 struct devlink *devlink = info->user_ptr[0];
7361 struct devlink_health_reporter *reporter;
7364 reporter = devlink_health_reporter_get_from_info(devlink, info);
7368 if (!reporter->ops->recover &&
7369 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
7370 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
7374 if (!reporter->ops->dump &&
7375 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
7380 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7381 reporter->graceful_period =
7382 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
7384 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7385 reporter->auto_recover =
7386 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
7388 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7389 reporter->auto_dump =
7390 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
7392 devlink_health_reporter_put(reporter);
7395 devlink_health_reporter_put(reporter);
7399 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
7400 struct genl_info *info)
7402 struct devlink *devlink = info->user_ptr[0];
7403 struct devlink_health_reporter *reporter;
7406 reporter = devlink_health_reporter_get_from_info(devlink, info);
7410 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
7412 devlink_health_reporter_put(reporter);
7416 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
7417 struct genl_info *info)
7419 struct devlink *devlink = info->user_ptr[0];
7420 struct devlink_health_reporter *reporter;
7421 struct devlink_fmsg *fmsg;
7424 reporter = devlink_health_reporter_get_from_info(devlink, info);
7428 if (!reporter->ops->diagnose) {
7429 devlink_health_reporter_put(reporter);
7433 fmsg = devlink_fmsg_alloc();
7435 devlink_health_reporter_put(reporter);
7439 err = devlink_fmsg_obj_nest_start(fmsg);
7443 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
7447 err = devlink_fmsg_obj_nest_end(fmsg);
7451 err = devlink_fmsg_snd(fmsg, info,
7452 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
7455 devlink_fmsg_free(fmsg);
7456 devlink_health_reporter_put(reporter);
7461 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
7462 struct netlink_callback *cb)
7464 struct devlink_health_reporter *reporter;
7465 u64 start = cb->args[0];
7468 reporter = devlink_health_reporter_get_from_cb(cb);
7472 if (!reporter->ops->dump) {
7476 mutex_lock(&reporter->dump_lock);
7478 err = devlink_health_do_dump(reporter, NULL, cb->extack);
7481 cb->args[1] = reporter->dump_ts;
7483 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
7484 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
7489 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
7490 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
7492 mutex_unlock(&reporter->dump_lock);
7494 devlink_health_reporter_put(reporter);
7499 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
7500 struct genl_info *info)
7502 struct devlink *devlink = info->user_ptr[0];
7503 struct devlink_health_reporter *reporter;
7505 reporter = devlink_health_reporter_get_from_info(devlink, info);
7509 if (!reporter->ops->dump) {
7510 devlink_health_reporter_put(reporter);
7514 mutex_lock(&reporter->dump_lock);
7515 devlink_health_dump_clear(reporter);
7516 mutex_unlock(&reporter->dump_lock);
7517 devlink_health_reporter_put(reporter);
7521 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
7522 struct genl_info *info)
7524 struct devlink *devlink = info->user_ptr[0];
7525 struct devlink_health_reporter *reporter;
7528 reporter = devlink_health_reporter_get_from_info(devlink, info);
7532 if (!reporter->ops->test) {
7533 devlink_health_reporter_put(reporter);
7537 err = reporter->ops->test(reporter, info->extack);
7539 devlink_health_reporter_put(reporter);
7543 struct devlink_stats {
7546 struct u64_stats_sync syncp;
7550 * struct devlink_trap_policer_item - Packet trap policer attributes.
7551 * @policer: Immutable packet trap policer attributes.
7552 * @rate: Rate in packets / sec.
7553 * @burst: Burst size in packets.
7554 * @list: trap_policer_list member.
7556 * Describes packet trap policer attributes. Created by devlink during trap
7557 * policer registration.
7559 struct devlink_trap_policer_item {
7560 const struct devlink_trap_policer *policer;
7563 struct list_head list;
7567 * struct devlink_trap_group_item - Packet trap group attributes.
7568 * @group: Immutable packet trap group attributes.
7569 * @policer_item: Associated policer item. Can be NULL.
7570 * @list: trap_group_list member.
7571 * @stats: Trap group statistics.
7573 * Describes packet trap group attributes. Created by devlink during trap
7574 * group registration.
7576 struct devlink_trap_group_item {
7577 const struct devlink_trap_group *group;
7578 struct devlink_trap_policer_item *policer_item;
7579 struct list_head list;
7580 struct devlink_stats __percpu *stats;
7584 * struct devlink_trap_item - Packet trap attributes.
7585 * @trap: Immutable packet trap attributes.
7586 * @group_item: Associated group item.
7587 * @list: trap_list member.
7588 * @action: Trap action.
7589 * @stats: Trap statistics.
7590 * @priv: Driver private information.
7592 * Describes both mutable and immutable packet trap attributes. Created by
7593 * devlink during trap registration and used for all trap related operations.
7595 struct devlink_trap_item {
7596 const struct devlink_trap *trap;
7597 struct devlink_trap_group_item *group_item;
7598 struct list_head list;
7599 enum devlink_trap_action action;
7600 struct devlink_stats __percpu *stats;
7604 static struct devlink_trap_policer_item *
7605 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
7607 struct devlink_trap_policer_item *policer_item;
7609 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
7610 if (policer_item->policer->id == id)
7611 return policer_item;
7617 static struct devlink_trap_item *
7618 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
7620 struct devlink_trap_item *trap_item;
7622 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7623 if (!strcmp(trap_item->trap->name, name))
7630 static struct devlink_trap_item *
7631 devlink_trap_item_get_from_info(struct devlink *devlink,
7632 struct genl_info *info)
7634 struct nlattr *attr;
7636 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
7638 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
7640 return devlink_trap_item_lookup(devlink, nla_data(attr));
7644 devlink_trap_action_get_from_info(struct genl_info *info,
7645 enum devlink_trap_action *p_trap_action)
7649 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
7651 case DEVLINK_TRAP_ACTION_DROP:
7652 case DEVLINK_TRAP_ACTION_TRAP:
7653 case DEVLINK_TRAP_ACTION_MIRROR:
7654 *p_trap_action = val;
7663 static int devlink_trap_metadata_put(struct sk_buff *msg,
7664 const struct devlink_trap *trap)
7666 struct nlattr *attr;
7668 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
7672 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
7673 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
7674 goto nla_put_failure;
7675 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
7676 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
7677 goto nla_put_failure;
7679 nla_nest_end(msg, attr);
7684 nla_nest_cancel(msg, attr);
7688 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
7689 struct devlink_stats *stats)
7693 memset(stats, 0, sizeof(*stats));
7694 for_each_possible_cpu(i) {
7695 struct devlink_stats *cpu_stats;
7696 u64 rx_packets, rx_bytes;
7699 cpu_stats = per_cpu_ptr(trap_stats, i);
7701 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7702 rx_packets = cpu_stats->rx_packets;
7703 rx_bytes = cpu_stats->rx_bytes;
7704 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7706 stats->rx_packets += rx_packets;
7707 stats->rx_bytes += rx_bytes;
7712 devlink_trap_group_stats_put(struct sk_buff *msg,
7713 struct devlink_stats __percpu *trap_stats)
7715 struct devlink_stats stats;
7716 struct nlattr *attr;
7718 devlink_trap_stats_read(trap_stats, &stats);
7720 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7724 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
7725 stats.rx_packets, DEVLINK_ATTR_PAD))
7726 goto nla_put_failure;
7728 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
7729 stats.rx_bytes, DEVLINK_ATTR_PAD))
7730 goto nla_put_failure;
7732 nla_nest_end(msg, attr);
7737 nla_nest_cancel(msg, attr);
7741 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
7742 const struct devlink_trap_item *trap_item)
7744 struct devlink_stats stats;
7745 struct nlattr *attr;
7749 if (devlink->ops->trap_drop_counter_get) {
7750 err = devlink->ops->trap_drop_counter_get(devlink,
7757 devlink_trap_stats_read(trap_item->stats, &stats);
7759 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7763 if (devlink->ops->trap_drop_counter_get &&
7764 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
7766 goto nla_put_failure;
7768 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
7769 stats.rx_packets, DEVLINK_ATTR_PAD))
7770 goto nla_put_failure;
7772 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
7773 stats.rx_bytes, DEVLINK_ATTR_PAD))
7774 goto nla_put_failure;
7776 nla_nest_end(msg, attr);
7781 nla_nest_cancel(msg, attr);
7785 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
7786 const struct devlink_trap_item *trap_item,
7787 enum devlink_command cmd, u32 portid, u32 seq,
7790 struct devlink_trap_group_item *group_item = trap_item->group_item;
7794 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7798 if (devlink_nl_put_handle(msg, devlink))
7799 goto nla_put_failure;
7801 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7802 group_item->group->name))
7803 goto nla_put_failure;
7805 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
7806 goto nla_put_failure;
7808 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
7809 goto nla_put_failure;
7811 if (trap_item->trap->generic &&
7812 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7813 goto nla_put_failure;
7815 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
7816 goto nla_put_failure;
7818 err = devlink_trap_metadata_put(msg, trap_item->trap);
7820 goto nla_put_failure;
7822 err = devlink_trap_stats_put(msg, devlink, trap_item);
7824 goto nla_put_failure;
7826 genlmsg_end(msg, hdr);
7831 genlmsg_cancel(msg, hdr);
7835 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
7836 struct genl_info *info)
7838 struct netlink_ext_ack *extack = info->extack;
7839 struct devlink *devlink = info->user_ptr[0];
7840 struct devlink_trap_item *trap_item;
7841 struct sk_buff *msg;
7844 if (list_empty(&devlink->trap_list))
7847 trap_item = devlink_trap_item_get_from_info(devlink, info);
7849 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
7853 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7857 err = devlink_nl_trap_fill(msg, devlink, trap_item,
7858 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
7863 return genlmsg_reply(msg, info);
7870 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
7871 struct netlink_callback *cb)
7873 struct devlink_trap_item *trap_item;
7874 struct devlink *devlink;
7875 int start = cb->args[0];
7876 unsigned long index;
7880 mutex_lock(&devlink_mutex);
7881 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7882 if (!devlink_try_get(devlink))
7885 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7888 mutex_lock(&devlink->lock);
7889 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7894 err = devlink_nl_trap_fill(msg, devlink, trap_item,
7895 DEVLINK_CMD_TRAP_NEW,
7896 NETLINK_CB(cb->skb).portid,
7900 mutex_unlock(&devlink->lock);
7901 devlink_put(devlink);
7906 mutex_unlock(&devlink->lock);
7908 devlink_put(devlink);
7911 mutex_unlock(&devlink_mutex);
7917 static int __devlink_trap_action_set(struct devlink *devlink,
7918 struct devlink_trap_item *trap_item,
7919 enum devlink_trap_action trap_action,
7920 struct netlink_ext_ack *extack)
7924 if (trap_item->action != trap_action &&
7925 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
7926 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
7930 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
7931 trap_action, extack);
7935 trap_item->action = trap_action;
7940 static int devlink_trap_action_set(struct devlink *devlink,
7941 struct devlink_trap_item *trap_item,
7942 struct genl_info *info)
7944 enum devlink_trap_action trap_action;
7947 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
7950 err = devlink_trap_action_get_from_info(info, &trap_action);
7952 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
7956 return __devlink_trap_action_set(devlink, trap_item, trap_action,
7960 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
7961 struct genl_info *info)
7963 struct netlink_ext_ack *extack = info->extack;
7964 struct devlink *devlink = info->user_ptr[0];
7965 struct devlink_trap_item *trap_item;
7967 if (list_empty(&devlink->trap_list))
7970 trap_item = devlink_trap_item_get_from_info(devlink, info);
7972 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
7976 return devlink_trap_action_set(devlink, trap_item, info);
7979 static struct devlink_trap_group_item *
7980 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
7982 struct devlink_trap_group_item *group_item;
7984 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
7985 if (!strcmp(group_item->group->name, name))
7992 static struct devlink_trap_group_item *
7993 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
7995 struct devlink_trap_group_item *group_item;
7997 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
7998 if (group_item->group->id == id)
8005 static struct devlink_trap_group_item *
8006 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8007 struct genl_info *info)
8011 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8013 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8015 return devlink_trap_group_item_lookup(devlink, name);
8019 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8020 const struct devlink_trap_group_item *group_item,
8021 enum devlink_command cmd, u32 portid, u32 seq,
8027 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8031 if (devlink_nl_put_handle(msg, devlink))
8032 goto nla_put_failure;
8034 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8035 group_item->group->name))
8036 goto nla_put_failure;
8038 if (group_item->group->generic &&
8039 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8040 goto nla_put_failure;
8042 if (group_item->policer_item &&
8043 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8044 group_item->policer_item->policer->id))
8045 goto nla_put_failure;
8047 err = devlink_trap_group_stats_put(msg, group_item->stats);
8049 goto nla_put_failure;
8051 genlmsg_end(msg, hdr);
8056 genlmsg_cancel(msg, hdr);
8060 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8061 struct genl_info *info)
8063 struct netlink_ext_ack *extack = info->extack;
8064 struct devlink *devlink = info->user_ptr[0];
8065 struct devlink_trap_group_item *group_item;
8066 struct sk_buff *msg;
8069 if (list_empty(&devlink->trap_group_list))
8072 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8074 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8078 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8082 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8083 DEVLINK_CMD_TRAP_GROUP_NEW,
8084 info->snd_portid, info->snd_seq, 0);
8086 goto err_trap_group_fill;
8088 return genlmsg_reply(msg, info);
8090 err_trap_group_fill:
8095 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8096 struct netlink_callback *cb)
8098 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8099 struct devlink_trap_group_item *group_item;
8100 u32 portid = NETLINK_CB(cb->skb).portid;
8101 struct devlink *devlink;
8102 int start = cb->args[0];
8103 unsigned long index;
8107 mutex_lock(&devlink_mutex);
8108 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8109 if (!devlink_try_get(devlink))
8112 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8115 mutex_lock(&devlink->lock);
8116 list_for_each_entry(group_item, &devlink->trap_group_list,
8122 err = devlink_nl_trap_group_fill(msg, devlink,
8128 mutex_unlock(&devlink->lock);
8129 devlink_put(devlink);
8134 mutex_unlock(&devlink->lock);
8136 devlink_put(devlink);
8139 mutex_unlock(&devlink_mutex);
8146 __devlink_trap_group_action_set(struct devlink *devlink,
8147 struct devlink_trap_group_item *group_item,
8148 enum devlink_trap_action trap_action,
8149 struct netlink_ext_ack *extack)
8151 const char *group_name = group_item->group->name;
8152 struct devlink_trap_item *trap_item;
8155 if (devlink->ops->trap_group_action_set) {
8156 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8157 trap_action, extack);
8161 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8162 if (strcmp(trap_item->group_item->group->name, group_name))
8164 if (trap_item->action != trap_action &&
8165 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8167 trap_item->action = trap_action;
8173 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8174 if (strcmp(trap_item->group_item->group->name, group_name))
8176 err = __devlink_trap_action_set(devlink, trap_item,
8177 trap_action, extack);
8186 devlink_trap_group_action_set(struct devlink *devlink,
8187 struct devlink_trap_group_item *group_item,
8188 struct genl_info *info, bool *p_modified)
8190 enum devlink_trap_action trap_action;
8193 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8196 err = devlink_trap_action_get_from_info(info, &trap_action);
8198 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8202 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8212 static int devlink_trap_group_set(struct devlink *devlink,
8213 struct devlink_trap_group_item *group_item,
8214 struct genl_info *info)
8216 struct devlink_trap_policer_item *policer_item;
8217 struct netlink_ext_ack *extack = info->extack;
8218 const struct devlink_trap_policer *policer;
8219 struct nlattr **attrs = info->attrs;
8222 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8225 if (!devlink->ops->trap_group_set)
8228 policer_item = group_item->policer_item;
8229 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8232 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8233 policer_item = devlink_trap_policer_item_lookup(devlink,
8235 if (policer_id && !policer_item) {
8236 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8240 policer = policer_item ? policer_item->policer : NULL;
8242 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8247 group_item->policer_item = policer_item;
8252 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8253 struct genl_info *info)
8255 struct netlink_ext_ack *extack = info->extack;
8256 struct devlink *devlink = info->user_ptr[0];
8257 struct devlink_trap_group_item *group_item;
8258 bool modified = false;
8261 if (list_empty(&devlink->trap_group_list))
8264 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8266 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8270 err = devlink_trap_group_action_set(devlink, group_item, info,
8275 err = devlink_trap_group_set(devlink, group_item, info);
8277 goto err_trap_group_set;
8283 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8287 static struct devlink_trap_policer_item *
8288 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8289 struct genl_info *info)
8293 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8295 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8297 return devlink_trap_policer_item_lookup(devlink, id);
8301 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8302 const struct devlink_trap_policer *policer)
8304 struct nlattr *attr;
8308 if (!devlink->ops->trap_policer_counter_get)
8311 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8315 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8319 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8321 goto nla_put_failure;
8323 nla_nest_end(msg, attr);
8328 nla_nest_cancel(msg, attr);
8333 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8334 const struct devlink_trap_policer_item *policer_item,
8335 enum devlink_command cmd, u32 portid, u32 seq,
8341 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8345 if (devlink_nl_put_handle(msg, devlink))
8346 goto nla_put_failure;
8348 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8349 policer_item->policer->id))
8350 goto nla_put_failure;
8352 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8353 policer_item->rate, DEVLINK_ATTR_PAD))
8354 goto nla_put_failure;
8356 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8357 policer_item->burst, DEVLINK_ATTR_PAD))
8358 goto nla_put_failure;
8360 err = devlink_trap_policer_stats_put(msg, devlink,
8361 policer_item->policer);
8363 goto nla_put_failure;
8365 genlmsg_end(msg, hdr);
8370 genlmsg_cancel(msg, hdr);
8374 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8375 struct genl_info *info)
8377 struct devlink_trap_policer_item *policer_item;
8378 struct netlink_ext_ack *extack = info->extack;
8379 struct devlink *devlink = info->user_ptr[0];
8380 struct sk_buff *msg;
8383 if (list_empty(&devlink->trap_policer_list))
8386 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8387 if (!policer_item) {
8388 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8392 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8396 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8397 DEVLINK_CMD_TRAP_POLICER_NEW,
8398 info->snd_portid, info->snd_seq, 0);
8400 goto err_trap_policer_fill;
8402 return genlmsg_reply(msg, info);
8404 err_trap_policer_fill:
8409 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
8410 struct netlink_callback *cb)
8412 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
8413 struct devlink_trap_policer_item *policer_item;
8414 u32 portid = NETLINK_CB(cb->skb).portid;
8415 struct devlink *devlink;
8416 int start = cb->args[0];
8417 unsigned long index;
8421 mutex_lock(&devlink_mutex);
8422 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8423 if (!devlink_try_get(devlink))
8426 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8429 mutex_lock(&devlink->lock);
8430 list_for_each_entry(policer_item, &devlink->trap_policer_list,
8436 err = devlink_nl_trap_policer_fill(msg, devlink,
8442 mutex_unlock(&devlink->lock);
8443 devlink_put(devlink);
8448 mutex_unlock(&devlink->lock);
8450 devlink_put(devlink);
8453 mutex_unlock(&devlink_mutex);
8460 devlink_trap_policer_set(struct devlink *devlink,
8461 struct devlink_trap_policer_item *policer_item,
8462 struct genl_info *info)
8464 struct netlink_ext_ack *extack = info->extack;
8465 struct nlattr **attrs = info->attrs;
8469 rate = policer_item->rate;
8470 burst = policer_item->burst;
8472 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
8473 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
8475 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
8476 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
8478 if (rate < policer_item->policer->min_rate) {
8479 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
8483 if (rate > policer_item->policer->max_rate) {
8484 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
8488 if (burst < policer_item->policer->min_burst) {
8489 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
8493 if (burst > policer_item->policer->max_burst) {
8494 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
8498 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
8499 rate, burst, info->extack);
8503 policer_item->rate = rate;
8504 policer_item->burst = burst;
8509 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
8510 struct genl_info *info)
8512 struct devlink_trap_policer_item *policer_item;
8513 struct netlink_ext_ack *extack = info->extack;
8514 struct devlink *devlink = info->user_ptr[0];
8516 if (list_empty(&devlink->trap_policer_list))
8519 if (!devlink->ops->trap_policer_set)
8522 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8523 if (!policer_item) {
8524 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8528 return devlink_trap_policer_set(devlink, policer_item, info);
8531 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
8532 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
8533 DEVLINK_ATTR_TRAP_POLICER_ID },
8534 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
8535 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
8536 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
8537 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
8538 DEVLINK_PORT_TYPE_IB),
8539 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
8540 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
8541 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
8542 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
8543 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
8544 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
8545 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
8546 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
8547 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
8548 DEVLINK_ESWITCH_MODE_SWITCHDEV),
8549 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
8550 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
8551 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
8552 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
8553 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
8554 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
8555 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
8556 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
8557 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
8558 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
8559 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
8560 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
8561 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
8562 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
8563 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
8564 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
8565 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
8566 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
8567 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
8568 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
8569 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
8570 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
8571 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
8572 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
8573 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
8574 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
8575 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
8576 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
8577 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
8578 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
8579 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
8580 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
8581 DEVLINK_RELOAD_ACTION_MAX),
8582 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
8583 [DEVLINK_ATTR_PORT_FLAVOUR] = { .type = NLA_U16 },
8584 [DEVLINK_ATTR_PORT_PCI_PF_NUMBER] = { .type = NLA_U16 },
8585 [DEVLINK_ATTR_PORT_PCI_SF_NUMBER] = { .type = NLA_U32 },
8586 [DEVLINK_ATTR_PORT_CONTROLLER_NUMBER] = { .type = NLA_U32 },
8587 [DEVLINK_ATTR_RATE_TYPE] = { .type = NLA_U16 },
8588 [DEVLINK_ATTR_RATE_TX_SHARE] = { .type = NLA_U64 },
8589 [DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 },
8590 [DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING },
8591 [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING },
8594 static const struct genl_small_ops devlink_nl_ops[] = {
8596 .cmd = DEVLINK_CMD_GET,
8597 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8598 .doit = devlink_nl_cmd_get_doit,
8599 .dumpit = devlink_nl_cmd_get_dumpit,
8600 /* can be retrieved by unprivileged users */
8603 .cmd = DEVLINK_CMD_PORT_GET,
8604 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8605 .doit = devlink_nl_cmd_port_get_doit,
8606 .dumpit = devlink_nl_cmd_port_get_dumpit,
8607 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8608 /* can be retrieved by unprivileged users */
8611 .cmd = DEVLINK_CMD_PORT_SET,
8612 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8613 .doit = devlink_nl_cmd_port_set_doit,
8614 .flags = GENL_ADMIN_PERM,
8615 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8618 .cmd = DEVLINK_CMD_RATE_GET,
8619 .doit = devlink_nl_cmd_rate_get_doit,
8620 .dumpit = devlink_nl_cmd_rate_get_dumpit,
8621 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
8622 /* can be retrieved by unprivileged users */
8625 .cmd = DEVLINK_CMD_RATE_SET,
8626 .doit = devlink_nl_cmd_rate_set_doit,
8627 .flags = GENL_ADMIN_PERM,
8628 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
8631 .cmd = DEVLINK_CMD_RATE_NEW,
8632 .doit = devlink_nl_cmd_rate_new_doit,
8633 .flags = GENL_ADMIN_PERM,
8636 .cmd = DEVLINK_CMD_RATE_DEL,
8637 .doit = devlink_nl_cmd_rate_del_doit,
8638 .flags = GENL_ADMIN_PERM,
8639 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
8642 .cmd = DEVLINK_CMD_PORT_SPLIT,
8643 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8644 .doit = devlink_nl_cmd_port_split_doit,
8645 .flags = GENL_ADMIN_PERM,
8646 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8649 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
8650 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8651 .doit = devlink_nl_cmd_port_unsplit_doit,
8652 .flags = GENL_ADMIN_PERM,
8653 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8656 .cmd = DEVLINK_CMD_PORT_NEW,
8657 .doit = devlink_nl_cmd_port_new_doit,
8658 .flags = GENL_ADMIN_PERM,
8659 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8662 .cmd = DEVLINK_CMD_PORT_DEL,
8663 .doit = devlink_nl_cmd_port_del_doit,
8664 .flags = GENL_ADMIN_PERM,
8665 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8668 .cmd = DEVLINK_CMD_SB_GET,
8669 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8670 .doit = devlink_nl_cmd_sb_get_doit,
8671 .dumpit = devlink_nl_cmd_sb_get_dumpit,
8672 /* can be retrieved by unprivileged users */
8675 .cmd = DEVLINK_CMD_SB_POOL_GET,
8676 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8677 .doit = devlink_nl_cmd_sb_pool_get_doit,
8678 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
8679 /* can be retrieved by unprivileged users */
8682 .cmd = DEVLINK_CMD_SB_POOL_SET,
8683 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8684 .doit = devlink_nl_cmd_sb_pool_set_doit,
8685 .flags = GENL_ADMIN_PERM,
8688 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
8689 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8690 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
8691 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
8692 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8693 /* can be retrieved by unprivileged users */
8696 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
8697 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8698 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
8699 .flags = GENL_ADMIN_PERM,
8700 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8703 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
8704 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8705 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
8706 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
8707 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8708 /* can be retrieved by unprivileged users */
8711 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
8712 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8713 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
8714 .flags = GENL_ADMIN_PERM,
8715 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8718 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
8719 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8720 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
8721 .flags = GENL_ADMIN_PERM,
8724 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
8725 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8726 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
8727 .flags = GENL_ADMIN_PERM,
8730 .cmd = DEVLINK_CMD_ESWITCH_GET,
8731 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8732 .doit = devlink_nl_cmd_eswitch_get_doit,
8733 .flags = GENL_ADMIN_PERM,
8736 .cmd = DEVLINK_CMD_ESWITCH_SET,
8737 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8738 .doit = devlink_nl_cmd_eswitch_set_doit,
8739 .flags = GENL_ADMIN_PERM,
8742 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
8743 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8744 .doit = devlink_nl_cmd_dpipe_table_get,
8745 /* can be retrieved by unprivileged users */
8748 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
8749 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8750 .doit = devlink_nl_cmd_dpipe_entries_get,
8751 /* can be retrieved by unprivileged users */
8754 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
8755 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8756 .doit = devlink_nl_cmd_dpipe_headers_get,
8757 /* can be retrieved by unprivileged users */
8760 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
8761 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8762 .doit = devlink_nl_cmd_dpipe_table_counters_set,
8763 .flags = GENL_ADMIN_PERM,
8766 .cmd = DEVLINK_CMD_RESOURCE_SET,
8767 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8768 .doit = devlink_nl_cmd_resource_set,
8769 .flags = GENL_ADMIN_PERM,
8772 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
8773 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8774 .doit = devlink_nl_cmd_resource_dump,
8775 /* can be retrieved by unprivileged users */
8778 .cmd = DEVLINK_CMD_RELOAD,
8779 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8780 .doit = devlink_nl_cmd_reload,
8781 .flags = GENL_ADMIN_PERM,
8782 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8785 .cmd = DEVLINK_CMD_PARAM_GET,
8786 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8787 .doit = devlink_nl_cmd_param_get_doit,
8788 .dumpit = devlink_nl_cmd_param_get_dumpit,
8789 /* can be retrieved by unprivileged users */
8792 .cmd = DEVLINK_CMD_PARAM_SET,
8793 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8794 .doit = devlink_nl_cmd_param_set_doit,
8795 .flags = GENL_ADMIN_PERM,
8798 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
8799 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8800 .doit = devlink_nl_cmd_port_param_get_doit,
8801 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
8802 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8803 /* can be retrieved by unprivileged users */
8806 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
8807 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8808 .doit = devlink_nl_cmd_port_param_set_doit,
8809 .flags = GENL_ADMIN_PERM,
8810 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8813 .cmd = DEVLINK_CMD_REGION_GET,
8814 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8815 .doit = devlink_nl_cmd_region_get_doit,
8816 .dumpit = devlink_nl_cmd_region_get_dumpit,
8817 .flags = GENL_ADMIN_PERM,
8820 .cmd = DEVLINK_CMD_REGION_NEW,
8821 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8822 .doit = devlink_nl_cmd_region_new,
8823 .flags = GENL_ADMIN_PERM,
8826 .cmd = DEVLINK_CMD_REGION_DEL,
8827 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8828 .doit = devlink_nl_cmd_region_del,
8829 .flags = GENL_ADMIN_PERM,
8832 .cmd = DEVLINK_CMD_REGION_READ,
8833 .validate = GENL_DONT_VALIDATE_STRICT |
8834 GENL_DONT_VALIDATE_DUMP_STRICT,
8835 .dumpit = devlink_nl_cmd_region_read_dumpit,
8836 .flags = GENL_ADMIN_PERM,
8839 .cmd = DEVLINK_CMD_INFO_GET,
8840 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8841 .doit = devlink_nl_cmd_info_get_doit,
8842 .dumpit = devlink_nl_cmd_info_get_dumpit,
8843 /* can be retrieved by unprivileged users */
8846 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
8847 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8848 .doit = devlink_nl_cmd_health_reporter_get_doit,
8849 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
8850 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8851 DEVLINK_NL_FLAG_NO_LOCK,
8852 /* can be retrieved by unprivileged users */
8855 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
8856 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8857 .doit = devlink_nl_cmd_health_reporter_set_doit,
8858 .flags = GENL_ADMIN_PERM,
8859 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8860 DEVLINK_NL_FLAG_NO_LOCK,
8863 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
8864 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8865 .doit = devlink_nl_cmd_health_reporter_recover_doit,
8866 .flags = GENL_ADMIN_PERM,
8867 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8868 DEVLINK_NL_FLAG_NO_LOCK,
8871 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
8872 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8873 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
8874 .flags = GENL_ADMIN_PERM,
8875 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8876 DEVLINK_NL_FLAG_NO_LOCK,
8879 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
8880 .validate = GENL_DONT_VALIDATE_STRICT |
8881 GENL_DONT_VALIDATE_DUMP_STRICT,
8882 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
8883 .flags = GENL_ADMIN_PERM,
8886 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
8887 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8888 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
8889 .flags = GENL_ADMIN_PERM,
8890 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8891 DEVLINK_NL_FLAG_NO_LOCK,
8894 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
8895 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8896 .doit = devlink_nl_cmd_health_reporter_test_doit,
8897 .flags = GENL_ADMIN_PERM,
8898 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8899 DEVLINK_NL_FLAG_NO_LOCK,
8902 .cmd = DEVLINK_CMD_FLASH_UPDATE,
8903 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8904 .doit = devlink_nl_cmd_flash_update,
8905 .flags = GENL_ADMIN_PERM,
8908 .cmd = DEVLINK_CMD_TRAP_GET,
8909 .doit = devlink_nl_cmd_trap_get_doit,
8910 .dumpit = devlink_nl_cmd_trap_get_dumpit,
8911 /* can be retrieved by unprivileged users */
8914 .cmd = DEVLINK_CMD_TRAP_SET,
8915 .doit = devlink_nl_cmd_trap_set_doit,
8916 .flags = GENL_ADMIN_PERM,
8919 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
8920 .doit = devlink_nl_cmd_trap_group_get_doit,
8921 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
8922 /* can be retrieved by unprivileged users */
8925 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
8926 .doit = devlink_nl_cmd_trap_group_set_doit,
8927 .flags = GENL_ADMIN_PERM,
8930 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
8931 .doit = devlink_nl_cmd_trap_policer_get_doit,
8932 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
8933 /* can be retrieved by unprivileged users */
8936 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
8937 .doit = devlink_nl_cmd_trap_policer_set_doit,
8938 .flags = GENL_ADMIN_PERM,
8942 static struct genl_family devlink_nl_family __ro_after_init = {
8943 .name = DEVLINK_GENL_NAME,
8944 .version = DEVLINK_GENL_VERSION,
8945 .maxattr = DEVLINK_ATTR_MAX,
8946 .policy = devlink_nl_policy,
8948 .pre_doit = devlink_nl_pre_doit,
8949 .post_doit = devlink_nl_post_doit,
8950 .module = THIS_MODULE,
8951 .small_ops = devlink_nl_ops,
8952 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
8953 .mcgrps = devlink_nl_mcgrps,
8954 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
8957 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
8959 const struct devlink_reload_combination *comb;
8962 if (!devlink_reload_supported(ops)) {
8963 if (WARN_ON(ops->reload_actions))
8968 if (WARN_ON(!ops->reload_actions ||
8969 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
8970 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
8973 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
8974 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
8977 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
8978 comb = &devlink_reload_invalid_combinations[i];
8979 if (ops->reload_actions == BIT(comb->action) &&
8980 ops->reload_limits == BIT(comb->limit))
8987 * devlink_set_features - Set devlink supported features
8990 * @features: devlink support features
8992 * This interface allows us to set reload ops separatelly from
8993 * the devlink_alloc.
8995 void devlink_set_features(struct devlink *devlink, u64 features)
8997 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
8999 WARN_ON(features & DEVLINK_F_RELOAD &&
9000 !devlink_reload_supported(devlink->ops));
9001 devlink->features = features;
9003 EXPORT_SYMBOL_GPL(devlink_set_features);
9006 * devlink_alloc_ns - Allocate new devlink instance resources
9007 * in specific namespace
9010 * @priv_size: size of user private data
9011 * @net: net namespace
9012 * @dev: parent device
9014 * Allocate new devlink instance resources, including devlink index
9017 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
9018 size_t priv_size, struct net *net,
9021 struct devlink *devlink;
9025 WARN_ON(!ops || !dev);
9026 if (!devlink_reload_actions_valid(ops))
9029 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
9033 ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
9034 &last_id, GFP_KERNEL);
9042 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
9043 write_pnet(&devlink->_net, net);
9044 INIT_LIST_HEAD(&devlink->port_list);
9045 INIT_LIST_HEAD(&devlink->rate_list);
9046 INIT_LIST_HEAD(&devlink->sb_list);
9047 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
9048 INIT_LIST_HEAD(&devlink->resource_list);
9049 INIT_LIST_HEAD(&devlink->param_list);
9050 INIT_LIST_HEAD(&devlink->region_list);
9051 INIT_LIST_HEAD(&devlink->reporter_list);
9052 INIT_LIST_HEAD(&devlink->trap_list);
9053 INIT_LIST_HEAD(&devlink->trap_group_list);
9054 INIT_LIST_HEAD(&devlink->trap_policer_list);
9055 mutex_init(&devlink->lock);
9056 mutex_init(&devlink->reporters_lock);
9057 refcount_set(&devlink->refcount, 1);
9058 init_completion(&devlink->comp);
9062 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
9065 devlink_trap_policer_notify(struct devlink *devlink,
9066 const struct devlink_trap_policer_item *policer_item,
9067 enum devlink_command cmd);
9069 devlink_trap_group_notify(struct devlink *devlink,
9070 const struct devlink_trap_group_item *group_item,
9071 enum devlink_command cmd);
9072 static void devlink_trap_notify(struct devlink *devlink,
9073 const struct devlink_trap_item *trap_item,
9074 enum devlink_command cmd);
9076 static void devlink_notify_register(struct devlink *devlink)
9078 struct devlink_trap_policer_item *policer_item;
9079 struct devlink_trap_group_item *group_item;
9080 struct devlink_param_item *param_item;
9081 struct devlink_trap_item *trap_item;
9082 struct devlink_port *devlink_port;
9083 struct devlink_rate *rate_node;
9084 struct devlink_region *region;
9086 devlink_notify(devlink, DEVLINK_CMD_NEW);
9087 list_for_each_entry(devlink_port, &devlink->port_list, list)
9088 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9090 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9091 devlink_trap_policer_notify(devlink, policer_item,
9092 DEVLINK_CMD_TRAP_POLICER_NEW);
9094 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9095 devlink_trap_group_notify(devlink, group_item,
9096 DEVLINK_CMD_TRAP_GROUP_NEW);
9098 list_for_each_entry(trap_item, &devlink->trap_list, list)
9099 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9101 list_for_each_entry(rate_node, &devlink->rate_list, list)
9102 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9104 list_for_each_entry(region, &devlink->region_list, list)
9105 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9107 list_for_each_entry(param_item, &devlink->param_list, list)
9108 devlink_param_notify(devlink, 0, param_item,
9109 DEVLINK_CMD_PARAM_NEW);
9112 static void devlink_notify_unregister(struct devlink *devlink)
9114 struct devlink_trap_policer_item *policer_item;
9115 struct devlink_trap_group_item *group_item;
9116 struct devlink_param_item *param_item;
9117 struct devlink_trap_item *trap_item;
9118 struct devlink_port *devlink_port;
9119 struct devlink_rate *rate_node;
9120 struct devlink_region *region;
9122 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9123 devlink_param_notify(devlink, 0, param_item,
9124 DEVLINK_CMD_PARAM_DEL);
9126 list_for_each_entry_reverse(region, &devlink->region_list, list)
9127 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9129 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9130 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9132 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9133 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9135 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9136 devlink_trap_group_notify(devlink, group_item,
9137 DEVLINK_CMD_TRAP_GROUP_DEL);
9138 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9140 devlink_trap_policer_notify(devlink, policer_item,
9141 DEVLINK_CMD_TRAP_POLICER_DEL);
9143 list_for_each_entry_reverse(devlink_port, &devlink->port_list, list)
9144 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9145 devlink_notify(devlink, DEVLINK_CMD_DEL);
9149 * devlink_register - Register devlink instance
9153 void devlink_register(struct devlink *devlink)
9155 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9156 /* Make sure that we are in .probe() routine */
9158 mutex_lock(&devlink_mutex);
9159 xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9160 devlink_notify_register(devlink);
9161 mutex_unlock(&devlink_mutex);
9163 EXPORT_SYMBOL_GPL(devlink_register);
9166 * devlink_unregister - Unregister devlink instance
9170 void devlink_unregister(struct devlink *devlink)
9172 ASSERT_DEVLINK_REGISTERED(devlink);
9173 /* Make sure that we are in .remove() routine */
9175 devlink_put(devlink);
9176 wait_for_completion(&devlink->comp);
9178 mutex_lock(&devlink_mutex);
9179 devlink_notify_unregister(devlink);
9180 xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
9181 mutex_unlock(&devlink_mutex);
9183 EXPORT_SYMBOL_GPL(devlink_unregister);
9186 * devlink_free - Free devlink instance resources
9190 void devlink_free(struct devlink *devlink)
9192 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
9194 mutex_destroy(&devlink->reporters_lock);
9195 mutex_destroy(&devlink->lock);
9196 WARN_ON(!list_empty(&devlink->trap_policer_list));
9197 WARN_ON(!list_empty(&devlink->trap_group_list));
9198 WARN_ON(!list_empty(&devlink->trap_list));
9199 WARN_ON(!list_empty(&devlink->reporter_list));
9200 WARN_ON(!list_empty(&devlink->region_list));
9201 WARN_ON(!list_empty(&devlink->param_list));
9202 WARN_ON(!list_empty(&devlink->resource_list));
9203 WARN_ON(!list_empty(&devlink->dpipe_table_list));
9204 WARN_ON(!list_empty(&devlink->sb_list));
9205 WARN_ON(!list_empty(&devlink->rate_list));
9206 WARN_ON(!list_empty(&devlink->port_list));
9208 xa_destroy(&devlink->snapshot_ids);
9209 xa_erase(&devlinks, devlink->index);
9213 EXPORT_SYMBOL_GPL(devlink_free);
9215 static void devlink_port_type_warn(struct work_struct *work)
9217 WARN(true, "Type was not set for devlink port.");
9220 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9222 /* Ignore CPU and DSA flavours. */
9223 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9224 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9225 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9228 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9230 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9232 if (!devlink_port_type_should_warn(devlink_port))
9234 /* Schedule a work to WARN in case driver does not set port
9235 * type within timeout.
9237 schedule_delayed_work(&devlink_port->type_warn_dw,
9238 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9241 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9243 if (!devlink_port_type_should_warn(devlink_port))
9245 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9248 int devl_port_register(struct devlink *devlink,
9249 struct devlink_port *devlink_port,
9250 unsigned int port_index)
9252 lockdep_assert_held(&devlink->lock);
9254 if (devlink_port_index_exists(devlink, port_index))
9257 WARN_ON(devlink_port->devlink);
9258 devlink_port->devlink = devlink;
9259 devlink_port->index = port_index;
9260 spin_lock_init(&devlink_port->type_lock);
9261 INIT_LIST_HEAD(&devlink_port->reporter_list);
9262 mutex_init(&devlink_port->reporters_lock);
9263 list_add_tail(&devlink_port->list, &devlink->port_list);
9264 INIT_LIST_HEAD(&devlink_port->param_list);
9265 INIT_LIST_HEAD(&devlink_port->region_list);
9267 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9268 devlink_port_type_warn_schedule(devlink_port);
9269 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9272 EXPORT_SYMBOL_GPL(devl_port_register);
9275 * devlink_port_register - Register devlink port
9278 * @devlink_port: devlink port
9279 * @port_index: driver-specific numerical identifier of the port
9281 * Register devlink port with provided port index. User can use
9282 * any indexing, even hw-related one. devlink_port structure
9283 * is convenient to be embedded inside user driver private structure.
9284 * Note that the caller should take care of zeroing the devlink_port
9287 int devlink_port_register(struct devlink *devlink,
9288 struct devlink_port *devlink_port,
9289 unsigned int port_index)
9293 mutex_lock(&devlink->lock);
9294 err = devl_port_register(devlink, devlink_port, port_index);
9295 mutex_unlock(&devlink->lock);
9298 EXPORT_SYMBOL_GPL(devlink_port_register);
9300 void devl_port_unregister(struct devlink_port *devlink_port)
9302 lockdep_assert_held(&devlink_port->devlink->lock);
9304 devlink_port_type_warn_cancel(devlink_port);
9305 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9306 list_del(&devlink_port->list);
9307 WARN_ON(!list_empty(&devlink_port->reporter_list));
9308 WARN_ON(!list_empty(&devlink_port->region_list));
9309 mutex_destroy(&devlink_port->reporters_lock);
9311 EXPORT_SYMBOL_GPL(devl_port_unregister);
9314 * devlink_port_unregister - Unregister devlink port
9316 * @devlink_port: devlink port
9318 void devlink_port_unregister(struct devlink_port *devlink_port)
9320 struct devlink *devlink = devlink_port->devlink;
9322 mutex_lock(&devlink->lock);
9323 devl_port_unregister(devlink_port);
9324 mutex_unlock(&devlink->lock);
9326 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9328 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9329 enum devlink_port_type type,
9332 if (WARN_ON(!devlink_port->devlink))
9334 devlink_port_type_warn_cancel(devlink_port);
9335 spin_lock_bh(&devlink_port->type_lock);
9336 devlink_port->type = type;
9337 devlink_port->type_dev = type_dev;
9338 spin_unlock_bh(&devlink_port->type_lock);
9339 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9342 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9343 struct net_device *netdev)
9345 const struct net_device_ops *ops = netdev->netdev_ops;
9347 /* If driver registers devlink port, it should set devlink port
9348 * attributes accordingly so the compat functions are called
9349 * and the original ops are not used.
9351 if (ops->ndo_get_phys_port_name) {
9352 /* Some drivers use the same set of ndos for netdevs
9353 * that have devlink_port registered and also for
9354 * those who don't. Make sure that ndo_get_phys_port_name
9355 * returns -EOPNOTSUPP here in case it is defined.
9358 char name[IFNAMSIZ];
9361 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9362 WARN_ON(err != -EOPNOTSUPP);
9364 if (ops->ndo_get_port_parent_id) {
9365 /* Some drivers use the same set of ndos for netdevs
9366 * that have devlink_port registered and also for
9367 * those who don't. Make sure that ndo_get_port_parent_id
9368 * returns -EOPNOTSUPP here in case it is defined.
9371 struct netdev_phys_item_id ppid;
9374 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9375 WARN_ON(err != -EOPNOTSUPP);
9380 * devlink_port_type_eth_set - Set port type to Ethernet
9382 * @devlink_port: devlink port
9383 * @netdev: related netdevice
9385 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
9386 struct net_device *netdev)
9389 devlink_port_type_netdev_checks(devlink_port, netdev);
9391 dev_warn(devlink_port->devlink->dev,
9392 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9393 devlink_port->index);
9395 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
9397 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9400 * devlink_port_type_ib_set - Set port type to InfiniBand
9402 * @devlink_port: devlink port
9403 * @ibdev: related IB device
9405 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9406 struct ib_device *ibdev)
9408 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9410 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9413 * devlink_port_type_clear - Clear port type
9415 * @devlink_port: devlink port
9417 void devlink_port_type_clear(struct devlink_port *devlink_port)
9419 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9420 devlink_port_type_warn_schedule(devlink_port);
9422 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9424 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9425 enum devlink_port_flavour flavour)
9427 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9429 devlink_port->attrs_set = true;
9430 attrs->flavour = flavour;
9431 if (attrs->switch_id.id_len) {
9432 devlink_port->switch_port = true;
9433 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9434 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9436 devlink_port->switch_port = false;
9442 * devlink_port_attrs_set - Set port attributes
9444 * @devlink_port: devlink port
9445 * @attrs: devlink port attrs
9447 void devlink_port_attrs_set(struct devlink_port *devlink_port,
9448 struct devlink_port_attrs *attrs)
9452 if (WARN_ON(devlink_port->devlink))
9454 devlink_port->attrs = *attrs;
9455 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
9458 WARN_ON(attrs->splittable && attrs->split);
9460 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
9463 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
9465 * @devlink_port: devlink port
9466 * @controller: associated controller number for the devlink port instance
9467 * @pf: associated PF for the devlink port instance
9468 * @external: indicates if the port is for an external controller
9470 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
9471 u16 pf, bool external)
9473 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9476 if (WARN_ON(devlink_port->devlink))
9478 ret = __devlink_port_attrs_set(devlink_port,
9479 DEVLINK_PORT_FLAVOUR_PCI_PF);
9482 attrs->pci_pf.controller = controller;
9483 attrs->pci_pf.pf = pf;
9484 attrs->pci_pf.external = external;
9486 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
9489 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
9491 * @devlink_port: devlink port
9492 * @controller: associated controller number for the devlink port instance
9493 * @pf: associated PF for the devlink port instance
9494 * @vf: associated VF of a PF for the devlink port instance
9495 * @external: indicates if the port is for an external controller
9497 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
9498 u16 pf, u16 vf, bool external)
9500 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9503 if (WARN_ON(devlink_port->devlink))
9505 ret = __devlink_port_attrs_set(devlink_port,
9506 DEVLINK_PORT_FLAVOUR_PCI_VF);
9509 attrs->pci_vf.controller = controller;
9510 attrs->pci_vf.pf = pf;
9511 attrs->pci_vf.vf = vf;
9512 attrs->pci_vf.external = external;
9514 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
9517 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
9519 * @devlink_port: devlink port
9520 * @controller: associated controller number for the devlink port instance
9521 * @pf: associated PF for the devlink port instance
9522 * @sf: associated SF of a PF for the devlink port instance
9523 * @external: indicates if the port is for an external controller
9525 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
9526 u16 pf, u32 sf, bool external)
9528 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9531 if (WARN_ON(devlink_port->devlink))
9533 ret = __devlink_port_attrs_set(devlink_port,
9534 DEVLINK_PORT_FLAVOUR_PCI_SF);
9537 attrs->pci_sf.controller = controller;
9538 attrs->pci_sf.pf = pf;
9539 attrs->pci_sf.sf = sf;
9540 attrs->pci_sf.external = external;
9542 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
9545 * devl_rate_leaf_create - create devlink rate leaf
9546 * @devlink_port: devlink port object to create rate object on
9547 * @priv: driver private data
9549 * Create devlink rate object of type leaf on provided @devlink_port.
9551 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
9553 struct devlink *devlink = devlink_port->devlink;
9554 struct devlink_rate *devlink_rate;
9556 devl_assert_locked(devlink_port->devlink);
9558 if (WARN_ON(devlink_port->devlink_rate))
9561 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
9565 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
9566 devlink_rate->devlink = devlink;
9567 devlink_rate->devlink_port = devlink_port;
9568 devlink_rate->priv = priv;
9569 list_add_tail(&devlink_rate->list, &devlink->rate_list);
9570 devlink_port->devlink_rate = devlink_rate;
9571 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
9575 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
9578 devlink_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
9580 struct devlink *devlink = devlink_port->devlink;
9583 mutex_lock(&devlink->lock);
9584 ret = devl_rate_leaf_create(devlink_port, priv);
9585 mutex_unlock(&devlink->lock);
9589 EXPORT_SYMBOL_GPL(devlink_rate_leaf_create);
9591 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
9593 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
9595 devl_assert_locked(devlink_port->devlink);
9599 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
9600 if (devlink_rate->parent)
9601 refcount_dec(&devlink_rate->parent->refcnt);
9602 list_del(&devlink_rate->list);
9603 devlink_port->devlink_rate = NULL;
9604 kfree(devlink_rate);
9606 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
9609 * devlink_rate_leaf_destroy - destroy devlink rate leaf
9611 * @devlink_port: devlink port linked to the rate object
9613 * Context: Takes and release devlink->lock <mutex>.
9615 void devlink_rate_leaf_destroy(struct devlink_port *devlink_port)
9617 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
9618 struct devlink *devlink = devlink_port->devlink;
9623 mutex_lock(&devlink->lock);
9624 devl_rate_leaf_destroy(devlink_port);
9625 mutex_unlock(&devlink->lock);
9627 EXPORT_SYMBOL_GPL(devlink_rate_leaf_destroy);
9630 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
9631 * @devlink: devlink instance
9633 * Unset parent for all rate objects and destroy all rate nodes
9634 * on specified device.
9636 void devl_rate_nodes_destroy(struct devlink *devlink)
9638 static struct devlink_rate *devlink_rate, *tmp;
9639 const struct devlink_ops *ops = devlink->ops;
9641 devl_assert_locked(devlink);
9643 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
9644 if (!devlink_rate->parent)
9647 refcount_dec(&devlink_rate->parent->refcnt);
9648 if (devlink_rate_is_leaf(devlink_rate))
9649 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
9651 else if (devlink_rate_is_node(devlink_rate))
9652 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
9655 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
9656 if (devlink_rate_is_node(devlink_rate)) {
9657 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
9658 list_del(&devlink_rate->list);
9659 kfree(devlink_rate->name);
9660 kfree(devlink_rate);
9664 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
9667 * devlink_rate_nodes_destroy - destroy all devlink rate nodes on device
9669 * @devlink: devlink instance
9671 * Unset parent for all rate objects and destroy all rate nodes
9672 * on specified device.
9674 * Context: Takes and release devlink->lock <mutex>.
9676 void devlink_rate_nodes_destroy(struct devlink *devlink)
9678 mutex_lock(&devlink->lock);
9679 devl_rate_nodes_destroy(devlink);
9680 mutex_unlock(&devlink->lock);
9682 EXPORT_SYMBOL_GPL(devlink_rate_nodes_destroy);
9684 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
9685 char *name, size_t len)
9687 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9690 if (!devlink_port->attrs_set)
9693 switch (attrs->flavour) {
9694 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
9695 n = snprintf(name, len, "p%u", attrs->phys.port_number);
9696 if (n < len && attrs->split)
9697 n += snprintf(name + n, len - n, "s%u",
9698 attrs->phys.split_subport_number);
9700 case DEVLINK_PORT_FLAVOUR_CPU:
9701 case DEVLINK_PORT_FLAVOUR_DSA:
9702 case DEVLINK_PORT_FLAVOUR_UNUSED:
9703 /* As CPU and DSA ports do not have a netdevice associated
9704 * case should not ever happen.
9708 case DEVLINK_PORT_FLAVOUR_PCI_PF:
9709 if (attrs->pci_pf.external) {
9710 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
9716 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
9718 case DEVLINK_PORT_FLAVOUR_PCI_VF:
9719 if (attrs->pci_vf.external) {
9720 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
9726 n = snprintf(name, len, "pf%uvf%u",
9727 attrs->pci_vf.pf, attrs->pci_vf.vf);
9729 case DEVLINK_PORT_FLAVOUR_PCI_SF:
9730 if (attrs->pci_sf.external) {
9731 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
9737 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
9740 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
9750 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
9751 u32 size, u16 ingress_pools_count,
9752 u16 egress_pools_count, u16 ingress_tc_count,
9753 u16 egress_tc_count)
9755 struct devlink_sb *devlink_sb;
9758 mutex_lock(&devlink->lock);
9759 if (devlink_sb_index_exists(devlink, sb_index)) {
9764 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
9769 devlink_sb->index = sb_index;
9770 devlink_sb->size = size;
9771 devlink_sb->ingress_pools_count = ingress_pools_count;
9772 devlink_sb->egress_pools_count = egress_pools_count;
9773 devlink_sb->ingress_tc_count = ingress_tc_count;
9774 devlink_sb->egress_tc_count = egress_tc_count;
9775 list_add_tail(&devlink_sb->list, &devlink->sb_list);
9777 mutex_unlock(&devlink->lock);
9780 EXPORT_SYMBOL_GPL(devlink_sb_register);
9782 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
9784 struct devlink_sb *devlink_sb;
9786 mutex_lock(&devlink->lock);
9787 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
9788 WARN_ON(!devlink_sb);
9789 list_del(&devlink_sb->list);
9790 mutex_unlock(&devlink->lock);
9793 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
9796 * devlink_dpipe_headers_register - register dpipe headers
9799 * @dpipe_headers: dpipe header array
9801 * Register the headers supported by hardware.
9803 int devlink_dpipe_headers_register(struct devlink *devlink,
9804 struct devlink_dpipe_headers *dpipe_headers)
9806 mutex_lock(&devlink->lock);
9807 devlink->dpipe_headers = dpipe_headers;
9808 mutex_unlock(&devlink->lock);
9811 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
9814 * devlink_dpipe_headers_unregister - unregister dpipe headers
9818 * Unregister the headers supported by hardware.
9820 void devlink_dpipe_headers_unregister(struct devlink *devlink)
9822 mutex_lock(&devlink->lock);
9823 devlink->dpipe_headers = NULL;
9824 mutex_unlock(&devlink->lock);
9826 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
9829 * devlink_dpipe_table_counter_enabled - check if counter allocation
9832 * @table_name: tables name
9834 * Used by driver to check if counter allocation is required.
9835 * After counter allocation is turned on the table entries
9836 * are updated to include counter statistics.
9838 * After that point on the driver must respect the counter
9839 * state so that each entry added to the table is added
9842 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
9843 const char *table_name)
9845 struct devlink_dpipe_table *table;
9849 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
9850 table_name, devlink);
9853 enabled = table->counters_enabled;
9857 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
9860 * devlink_dpipe_table_register - register dpipe table
9863 * @table_name: table name
9864 * @table_ops: table ops
9866 * @counter_control_extern: external control for counters
9868 int devlink_dpipe_table_register(struct devlink *devlink,
9869 const char *table_name,
9870 struct devlink_dpipe_table_ops *table_ops,
9871 void *priv, bool counter_control_extern)
9873 struct devlink_dpipe_table *table;
9876 if (WARN_ON(!table_ops->size_get))
9879 mutex_lock(&devlink->lock);
9881 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
9887 table = kzalloc(sizeof(*table), GFP_KERNEL);
9893 table->name = table_name;
9894 table->table_ops = table_ops;
9896 table->counter_control_extern = counter_control_extern;
9898 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
9900 mutex_unlock(&devlink->lock);
9903 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
9906 * devlink_dpipe_table_unregister - unregister dpipe table
9909 * @table_name: table name
9911 void devlink_dpipe_table_unregister(struct devlink *devlink,
9912 const char *table_name)
9914 struct devlink_dpipe_table *table;
9916 mutex_lock(&devlink->lock);
9917 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
9918 table_name, devlink);
9921 list_del_rcu(&table->list);
9922 mutex_unlock(&devlink->lock);
9923 kfree_rcu(table, rcu);
9926 mutex_unlock(&devlink->lock);
9928 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
9931 * devlink_resource_register - devlink resource register
9934 * @resource_name: resource's name
9935 * @resource_size: resource's size
9936 * @resource_id: resource's id
9937 * @parent_resource_id: resource's parent id
9938 * @size_params: size parameters
9940 * Generic resources should reuse the same names across drivers.
9941 * Please see the generic resources list at:
9942 * Documentation/networking/devlink/devlink-resource.rst
9944 int devlink_resource_register(struct devlink *devlink,
9945 const char *resource_name,
9948 u64 parent_resource_id,
9949 const struct devlink_resource_size_params *size_params)
9951 struct devlink_resource *resource;
9952 struct list_head *resource_list;
9956 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
9958 mutex_lock(&devlink->lock);
9959 resource = devlink_resource_find(devlink, NULL, resource_id);
9965 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
9971 if (top_hierarchy) {
9972 resource_list = &devlink->resource_list;
9974 struct devlink_resource *parent_resource;
9976 parent_resource = devlink_resource_find(devlink, NULL,
9977 parent_resource_id);
9978 if (parent_resource) {
9979 resource_list = &parent_resource->resource_list;
9980 resource->parent = parent_resource;
9988 resource->name = resource_name;
9989 resource->size = resource_size;
9990 resource->size_new = resource_size;
9991 resource->id = resource_id;
9992 resource->size_valid = true;
9993 memcpy(&resource->size_params, size_params,
9994 sizeof(resource->size_params));
9995 INIT_LIST_HEAD(&resource->resource_list);
9996 list_add_tail(&resource->list, resource_list);
9998 mutex_unlock(&devlink->lock);
10001 EXPORT_SYMBOL_GPL(devlink_resource_register);
10003 static void devlink_resource_unregister(struct devlink *devlink,
10004 struct devlink_resource *resource)
10006 struct devlink_resource *tmp, *child_resource;
10008 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10010 devlink_resource_unregister(devlink, child_resource);
10011 list_del(&child_resource->list);
10012 kfree(child_resource);
10017 * devlink_resources_unregister - free all resources
10019 * @devlink: devlink
10021 void devlink_resources_unregister(struct devlink *devlink)
10023 struct devlink_resource *tmp, *child_resource;
10025 mutex_lock(&devlink->lock);
10027 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10029 devlink_resource_unregister(devlink, child_resource);
10030 list_del(&child_resource->list);
10031 kfree(child_resource);
10034 mutex_unlock(&devlink->lock);
10036 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10039 * devlink_resource_size_get - get and update size
10041 * @devlink: devlink
10042 * @resource_id: the requested resource id
10043 * @p_resource_size: ptr to update
10045 int devlink_resource_size_get(struct devlink *devlink,
10047 u64 *p_resource_size)
10049 struct devlink_resource *resource;
10052 mutex_lock(&devlink->lock);
10053 resource = devlink_resource_find(devlink, NULL, resource_id);
10058 *p_resource_size = resource->size_new;
10059 resource->size = resource->size_new;
10061 mutex_unlock(&devlink->lock);
10064 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
10067 * devlink_dpipe_table_resource_set - set the resource id
10069 * @devlink: devlink
10070 * @table_name: table name
10071 * @resource_id: resource id
10072 * @resource_units: number of resource's units consumed per table's entry
10074 int devlink_dpipe_table_resource_set(struct devlink *devlink,
10075 const char *table_name, u64 resource_id,
10076 u64 resource_units)
10078 struct devlink_dpipe_table *table;
10081 mutex_lock(&devlink->lock);
10082 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10083 table_name, devlink);
10088 table->resource_id = resource_id;
10089 table->resource_units = resource_units;
10090 table->resource_valid = true;
10092 mutex_unlock(&devlink->lock);
10095 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
10098 * devlink_resource_occ_get_register - register occupancy getter
10100 * @devlink: devlink
10101 * @resource_id: resource id
10102 * @occ_get: occupancy getter callback
10103 * @occ_get_priv: occupancy getter callback priv
10105 void devlink_resource_occ_get_register(struct devlink *devlink,
10107 devlink_resource_occ_get_t *occ_get,
10108 void *occ_get_priv)
10110 struct devlink_resource *resource;
10112 mutex_lock(&devlink->lock);
10113 resource = devlink_resource_find(devlink, NULL, resource_id);
10114 if (WARN_ON(!resource))
10116 WARN_ON(resource->occ_get);
10118 resource->occ_get = occ_get;
10119 resource->occ_get_priv = occ_get_priv;
10121 mutex_unlock(&devlink->lock);
10123 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10126 * devlink_resource_occ_get_unregister - unregister occupancy getter
10128 * @devlink: devlink
10129 * @resource_id: resource id
10131 void devlink_resource_occ_get_unregister(struct devlink *devlink,
10134 struct devlink_resource *resource;
10136 mutex_lock(&devlink->lock);
10137 resource = devlink_resource_find(devlink, NULL, resource_id);
10138 if (WARN_ON(!resource))
10140 WARN_ON(!resource->occ_get);
10142 resource->occ_get = NULL;
10143 resource->occ_get_priv = NULL;
10145 mutex_unlock(&devlink->lock);
10147 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
10149 static int devlink_param_verify(const struct devlink_param *param)
10151 if (!param || !param->name || !param->supported_cmodes)
10153 if (param->generic)
10154 return devlink_param_generic_verify(param);
10156 return devlink_param_driver_verify(param);
10160 * devlink_params_register - register configuration parameters
10162 * @devlink: devlink
10163 * @params: configuration parameters array
10164 * @params_count: number of parameters provided
10166 * Register the configuration parameters supported by the driver.
10168 int devlink_params_register(struct devlink *devlink,
10169 const struct devlink_param *params,
10170 size_t params_count)
10172 const struct devlink_param *param = params;
10175 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10177 for (i = 0; i < params_count; i++, param++) {
10178 err = devlink_param_register(devlink, param);
10188 for (param--; i > 0; i--, param--)
10189 devlink_param_unregister(devlink, param);
10192 EXPORT_SYMBOL_GPL(devlink_params_register);
10195 * devlink_params_unregister - unregister configuration parameters
10196 * @devlink: devlink
10197 * @params: configuration parameters to unregister
10198 * @params_count: number of parameters provided
10200 void devlink_params_unregister(struct devlink *devlink,
10201 const struct devlink_param *params,
10202 size_t params_count)
10204 const struct devlink_param *param = params;
10207 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10209 for (i = 0; i < params_count; i++, param++)
10210 devlink_param_unregister(devlink, param);
10212 EXPORT_SYMBOL_GPL(devlink_params_unregister);
10215 * devlink_param_register - register one configuration parameter
10217 * @devlink: devlink
10218 * @param: one configuration parameter
10220 * Register the configuration parameter supported by the driver.
10221 * Return: returns 0 on successful registration or error code otherwise.
10223 int devlink_param_register(struct devlink *devlink,
10224 const struct devlink_param *param)
10226 struct devlink_param_item *param_item;
10228 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10230 WARN_ON(devlink_param_verify(param));
10231 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
10233 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
10234 WARN_ON(param->get || param->set);
10236 WARN_ON(!param->get || !param->set);
10238 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
10242 param_item->param = param;
10244 list_add_tail(¶m_item->list, &devlink->param_list);
10247 EXPORT_SYMBOL_GPL(devlink_param_register);
10250 * devlink_param_unregister - unregister one configuration parameter
10251 * @devlink: devlink
10252 * @param: configuration parameter to unregister
10254 void devlink_param_unregister(struct devlink *devlink,
10255 const struct devlink_param *param)
10257 struct devlink_param_item *param_item;
10259 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10262 devlink_param_find_by_name(&devlink->param_list, param->name);
10263 WARN_ON(!param_item);
10264 list_del(¶m_item->list);
10267 EXPORT_SYMBOL_GPL(devlink_param_unregister);
10270 * devlink_param_driverinit_value_get - get configuration parameter
10271 * value for driver initializing
10273 * @devlink: devlink
10274 * @param_id: parameter ID
10275 * @init_val: value of parameter in driverinit configuration mode
10277 * This function should be used by the driver to get driverinit
10278 * configuration for initialization after reload command.
10280 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
10281 union devlink_param_value *init_val)
10283 struct devlink_param_item *param_item;
10285 if (!devlink_reload_supported(devlink->ops))
10286 return -EOPNOTSUPP;
10288 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10292 if (!param_item->driverinit_value_valid ||
10293 !devlink_param_cmode_is_supported(param_item->param,
10294 DEVLINK_PARAM_CMODE_DRIVERINIT))
10295 return -EOPNOTSUPP;
10297 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10298 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
10300 *init_val = param_item->driverinit_value;
10304 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
10307 * devlink_param_driverinit_value_set - set value of configuration
10308 * parameter for driverinit
10309 * configuration mode
10311 * @devlink: devlink
10312 * @param_id: parameter ID
10313 * @init_val: value of parameter to set for driverinit configuration mode
10315 * This function should be used by the driver to set driverinit
10316 * configuration mode default value.
10318 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
10319 union devlink_param_value init_val)
10321 struct devlink_param_item *param_item;
10323 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
10325 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10329 if (!devlink_param_cmode_is_supported(param_item->param,
10330 DEVLINK_PARAM_CMODE_DRIVERINIT))
10331 return -EOPNOTSUPP;
10333 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10334 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
10336 param_item->driverinit_value = init_val;
10337 param_item->driverinit_value_valid = true;
10340 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
10343 * devlink_param_value_changed - notify devlink on a parameter's value
10344 * change. Should be called by the driver
10345 * right after the change.
10347 * @devlink: devlink
10348 * @param_id: parameter ID
10350 * This function should be used by the driver to notify devlink on value
10351 * change, excluding driverinit configuration mode.
10352 * For driverinit configuration mode driver should use the function
10354 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
10356 struct devlink_param_item *param_item;
10358 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10359 WARN_ON(!param_item);
10361 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
10363 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
10366 * devlink_region_create - create a new address region
10368 * @devlink: devlink
10369 * @ops: region operations and name
10370 * @region_max_snapshots: Maximum supported number of snapshots for region
10371 * @region_size: size of region
10373 struct devlink_region *
10374 devlink_region_create(struct devlink *devlink,
10375 const struct devlink_region_ops *ops,
10376 u32 region_max_snapshots, u64 region_size)
10378 struct devlink_region *region;
10381 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
10382 return ERR_PTR(-EINVAL);
10384 mutex_lock(&devlink->lock);
10386 if (devlink_region_get_by_name(devlink, ops->name)) {
10391 region = kzalloc(sizeof(*region), GFP_KERNEL);
10397 region->devlink = devlink;
10398 region->max_snapshots = region_max_snapshots;
10400 region->size = region_size;
10401 INIT_LIST_HEAD(®ion->snapshot_list);
10402 list_add_tail(®ion->list, &devlink->region_list);
10403 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
10405 mutex_unlock(&devlink->lock);
10409 mutex_unlock(&devlink->lock);
10410 return ERR_PTR(err);
10412 EXPORT_SYMBOL_GPL(devlink_region_create);
10415 * devlink_port_region_create - create a new address region for a port
10417 * @port: devlink port
10418 * @ops: region operations and name
10419 * @region_max_snapshots: Maximum supported number of snapshots for region
10420 * @region_size: size of region
10422 struct devlink_region *
10423 devlink_port_region_create(struct devlink_port *port,
10424 const struct devlink_port_region_ops *ops,
10425 u32 region_max_snapshots, u64 region_size)
10427 struct devlink *devlink = port->devlink;
10428 struct devlink_region *region;
10431 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
10432 return ERR_PTR(-EINVAL);
10434 mutex_lock(&devlink->lock);
10436 if (devlink_port_region_get_by_name(port, ops->name)) {
10441 region = kzalloc(sizeof(*region), GFP_KERNEL);
10447 region->devlink = devlink;
10448 region->port = port;
10449 region->max_snapshots = region_max_snapshots;
10450 region->port_ops = ops;
10451 region->size = region_size;
10452 INIT_LIST_HEAD(®ion->snapshot_list);
10453 list_add_tail(®ion->list, &port->region_list);
10454 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
10456 mutex_unlock(&devlink->lock);
10460 mutex_unlock(&devlink->lock);
10461 return ERR_PTR(err);
10463 EXPORT_SYMBOL_GPL(devlink_port_region_create);
10466 * devlink_region_destroy - destroy address region
10468 * @region: devlink region to destroy
10470 void devlink_region_destroy(struct devlink_region *region)
10472 struct devlink *devlink = region->devlink;
10473 struct devlink_snapshot *snapshot, *ts;
10475 mutex_lock(&devlink->lock);
10477 /* Free all snapshots of region */
10478 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
10479 devlink_region_snapshot_del(region, snapshot);
10481 list_del(®ion->list);
10483 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
10484 mutex_unlock(&devlink->lock);
10487 EXPORT_SYMBOL_GPL(devlink_region_destroy);
10490 * devlink_region_snapshot_id_get - get snapshot ID
10492 * This callback should be called when adding a new snapshot,
10493 * Driver should use the same id for multiple snapshots taken
10494 * on multiple regions at the same time/by the same trigger.
10496 * The caller of this function must use devlink_region_snapshot_id_put
10497 * when finished creating regions using this id.
10499 * Returns zero on success, or a negative error code on failure.
10501 * @devlink: devlink
10502 * @id: storage to return id
10504 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
10508 mutex_lock(&devlink->lock);
10509 err = __devlink_region_snapshot_id_get(devlink, id);
10510 mutex_unlock(&devlink->lock);
10514 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
10517 * devlink_region_snapshot_id_put - put snapshot ID reference
10519 * This should be called by a driver after finishing creating snapshots
10520 * with an id. Doing so ensures that the ID can later be released in the
10521 * event that all snapshots using it have been destroyed.
10523 * @devlink: devlink
10524 * @id: id to release reference on
10526 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
10528 mutex_lock(&devlink->lock);
10529 __devlink_snapshot_id_decrement(devlink, id);
10530 mutex_unlock(&devlink->lock);
10532 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
10535 * devlink_region_snapshot_create - create a new snapshot
10536 * This will add a new snapshot of a region. The snapshot
10537 * will be stored on the region struct and can be accessed
10538 * from devlink. This is useful for future analyses of snapshots.
10539 * Multiple snapshots can be created on a region.
10540 * The @snapshot_id should be obtained using the getter function.
10542 * @region: devlink region of the snapshot
10543 * @data: snapshot data
10544 * @snapshot_id: snapshot id to be created
10546 int devlink_region_snapshot_create(struct devlink_region *region,
10547 u8 *data, u32 snapshot_id)
10549 struct devlink *devlink = region->devlink;
10552 mutex_lock(&devlink->lock);
10553 err = __devlink_region_snapshot_create(region, data, snapshot_id);
10554 mutex_unlock(&devlink->lock);
10558 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
10560 #define DEVLINK_TRAP(_id, _type) \
10562 .type = DEVLINK_TRAP_TYPE_##_type, \
10563 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
10564 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
10567 static const struct devlink_trap devlink_trap_generic[] = {
10568 DEVLINK_TRAP(SMAC_MC, DROP),
10569 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
10570 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
10571 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
10572 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
10573 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
10574 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
10575 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
10576 DEVLINK_TRAP(TAIL_DROP, DROP),
10577 DEVLINK_TRAP(NON_IP_PACKET, DROP),
10578 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
10579 DEVLINK_TRAP(DIP_LB, DROP),
10580 DEVLINK_TRAP(SIP_MC, DROP),
10581 DEVLINK_TRAP(SIP_LB, DROP),
10582 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
10583 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
10584 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
10585 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
10586 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
10587 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
10588 DEVLINK_TRAP(RPF, EXCEPTION),
10589 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
10590 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
10591 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
10592 DEVLINK_TRAP(NON_ROUTABLE, DROP),
10593 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
10594 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
10595 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
10596 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
10597 DEVLINK_TRAP(STP, CONTROL),
10598 DEVLINK_TRAP(LACP, CONTROL),
10599 DEVLINK_TRAP(LLDP, CONTROL),
10600 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
10601 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
10602 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
10603 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
10604 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
10605 DEVLINK_TRAP(MLD_QUERY, CONTROL),
10606 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
10607 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
10608 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
10609 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
10610 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
10611 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
10612 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
10613 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
10614 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
10615 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
10616 DEVLINK_TRAP(IPV4_BFD, CONTROL),
10617 DEVLINK_TRAP(IPV6_BFD, CONTROL),
10618 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
10619 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
10620 DEVLINK_TRAP(IPV4_BGP, CONTROL),
10621 DEVLINK_TRAP(IPV6_BGP, CONTROL),
10622 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
10623 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
10624 DEVLINK_TRAP(IPV4_PIM, CONTROL),
10625 DEVLINK_TRAP(IPV6_PIM, CONTROL),
10626 DEVLINK_TRAP(UC_LB, CONTROL),
10627 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
10628 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
10629 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
10630 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
10631 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
10632 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
10633 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
10634 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
10635 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
10636 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
10637 DEVLINK_TRAP(PTP_EVENT, CONTROL),
10638 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
10639 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
10640 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
10641 DEVLINK_TRAP(EARLY_DROP, DROP),
10642 DEVLINK_TRAP(VXLAN_PARSING, DROP),
10643 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
10644 DEVLINK_TRAP(VLAN_PARSING, DROP),
10645 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
10646 DEVLINK_TRAP(MPLS_PARSING, DROP),
10647 DEVLINK_TRAP(ARP_PARSING, DROP),
10648 DEVLINK_TRAP(IP_1_PARSING, DROP),
10649 DEVLINK_TRAP(IP_N_PARSING, DROP),
10650 DEVLINK_TRAP(GRE_PARSING, DROP),
10651 DEVLINK_TRAP(UDP_PARSING, DROP),
10652 DEVLINK_TRAP(TCP_PARSING, DROP),
10653 DEVLINK_TRAP(IPSEC_PARSING, DROP),
10654 DEVLINK_TRAP(SCTP_PARSING, DROP),
10655 DEVLINK_TRAP(DCCP_PARSING, DROP),
10656 DEVLINK_TRAP(GTP_PARSING, DROP),
10657 DEVLINK_TRAP(ESP_PARSING, DROP),
10658 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
10659 DEVLINK_TRAP(DMAC_FILTER, DROP),
10662 #define DEVLINK_TRAP_GROUP(_id) \
10664 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
10665 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
10668 static const struct devlink_trap_group devlink_trap_group_generic[] = {
10669 DEVLINK_TRAP_GROUP(L2_DROPS),
10670 DEVLINK_TRAP_GROUP(L3_DROPS),
10671 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
10672 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
10673 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
10674 DEVLINK_TRAP_GROUP(ACL_DROPS),
10675 DEVLINK_TRAP_GROUP(STP),
10676 DEVLINK_TRAP_GROUP(LACP),
10677 DEVLINK_TRAP_GROUP(LLDP),
10678 DEVLINK_TRAP_GROUP(MC_SNOOPING),
10679 DEVLINK_TRAP_GROUP(DHCP),
10680 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
10681 DEVLINK_TRAP_GROUP(BFD),
10682 DEVLINK_TRAP_GROUP(OSPF),
10683 DEVLINK_TRAP_GROUP(BGP),
10684 DEVLINK_TRAP_GROUP(VRRP),
10685 DEVLINK_TRAP_GROUP(PIM),
10686 DEVLINK_TRAP_GROUP(UC_LB),
10687 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
10688 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
10689 DEVLINK_TRAP_GROUP(IPV6),
10690 DEVLINK_TRAP_GROUP(PTP_EVENT),
10691 DEVLINK_TRAP_GROUP(PTP_GENERAL),
10692 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
10693 DEVLINK_TRAP_GROUP(ACL_TRAP),
10694 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
10697 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
10699 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
10702 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
10705 if (trap->type != devlink_trap_generic[trap->id].type)
10711 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
10715 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
10718 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
10719 if (!strcmp(trap->name, devlink_trap_generic[i].name))
10726 static int devlink_trap_verify(const struct devlink_trap *trap)
10728 if (!trap || !trap->name)
10732 return devlink_trap_generic_verify(trap);
10734 return devlink_trap_driver_verify(trap);
10738 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
10740 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
10743 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
10750 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
10754 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
10757 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
10758 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
10765 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
10767 if (group->generic)
10768 return devlink_trap_group_generic_verify(group);
10770 return devlink_trap_group_driver_verify(group);
10774 devlink_trap_group_notify(struct devlink *devlink,
10775 const struct devlink_trap_group_item *group_item,
10776 enum devlink_command cmd)
10778 struct sk_buff *msg;
10781 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
10782 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
10783 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
10786 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10790 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
10797 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10798 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10802 devlink_trap_item_group_link(struct devlink *devlink,
10803 struct devlink_trap_item *trap_item)
10805 u16 group_id = trap_item->trap->init_group_id;
10806 struct devlink_trap_group_item *group_item;
10808 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
10809 if (WARN_ON_ONCE(!group_item))
10812 trap_item->group_item = group_item;
10817 static void devlink_trap_notify(struct devlink *devlink,
10818 const struct devlink_trap_item *trap_item,
10819 enum devlink_command cmd)
10821 struct sk_buff *msg;
10824 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
10825 cmd != DEVLINK_CMD_TRAP_DEL);
10826 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
10829 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10833 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
10839 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10840 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10844 devlink_trap_register(struct devlink *devlink,
10845 const struct devlink_trap *trap, void *priv)
10847 struct devlink_trap_item *trap_item;
10850 if (devlink_trap_item_lookup(devlink, trap->name))
10853 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
10857 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
10858 if (!trap_item->stats) {
10860 goto err_stats_alloc;
10863 trap_item->trap = trap;
10864 trap_item->action = trap->init_action;
10865 trap_item->priv = priv;
10867 err = devlink_trap_item_group_link(devlink, trap_item);
10869 goto err_group_link;
10871 err = devlink->ops->trap_init(devlink, trap, trap_item);
10873 goto err_trap_init;
10875 list_add_tail(&trap_item->list, &devlink->trap_list);
10876 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
10882 free_percpu(trap_item->stats);
10888 static void devlink_trap_unregister(struct devlink *devlink,
10889 const struct devlink_trap *trap)
10891 struct devlink_trap_item *trap_item;
10893 trap_item = devlink_trap_item_lookup(devlink, trap->name);
10894 if (WARN_ON_ONCE(!trap_item))
10897 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
10898 list_del(&trap_item->list);
10899 if (devlink->ops->trap_fini)
10900 devlink->ops->trap_fini(devlink, trap, trap_item);
10901 free_percpu(trap_item->stats);
10905 static void devlink_trap_disable(struct devlink *devlink,
10906 const struct devlink_trap *trap)
10908 struct devlink_trap_item *trap_item;
10910 trap_item = devlink_trap_item_lookup(devlink, trap->name);
10911 if (WARN_ON_ONCE(!trap_item))
10914 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
10916 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
10920 * devlink_traps_register - Register packet traps with devlink.
10921 * @devlink: devlink.
10922 * @traps: Packet traps.
10923 * @traps_count: Count of provided packet traps.
10924 * @priv: Driver private information.
10926 * Return: Non-zero value on failure.
10928 int devlink_traps_register(struct devlink *devlink,
10929 const struct devlink_trap *traps,
10930 size_t traps_count, void *priv)
10934 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
10937 mutex_lock(&devlink->lock);
10938 for (i = 0; i < traps_count; i++) {
10939 const struct devlink_trap *trap = &traps[i];
10941 err = devlink_trap_verify(trap);
10943 goto err_trap_verify;
10945 err = devlink_trap_register(devlink, trap, priv);
10947 goto err_trap_register;
10949 mutex_unlock(&devlink->lock);
10955 for (i--; i >= 0; i--)
10956 devlink_trap_unregister(devlink, &traps[i]);
10957 mutex_unlock(&devlink->lock);
10960 EXPORT_SYMBOL_GPL(devlink_traps_register);
10963 * devlink_traps_unregister - Unregister packet traps from devlink.
10964 * @devlink: devlink.
10965 * @traps: Packet traps.
10966 * @traps_count: Count of provided packet traps.
10968 void devlink_traps_unregister(struct devlink *devlink,
10969 const struct devlink_trap *traps,
10970 size_t traps_count)
10974 mutex_lock(&devlink->lock);
10975 /* Make sure we do not have any packets in-flight while unregistering
10976 * traps by disabling all of them and waiting for a grace period.
10978 for (i = traps_count - 1; i >= 0; i--)
10979 devlink_trap_disable(devlink, &traps[i]);
10981 for (i = traps_count - 1; i >= 0; i--)
10982 devlink_trap_unregister(devlink, &traps[i]);
10983 mutex_unlock(&devlink->lock);
10985 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
10988 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
10991 struct devlink_stats *stats;
10993 stats = this_cpu_ptr(trap_stats);
10994 u64_stats_update_begin(&stats->syncp);
10995 stats->rx_bytes += skb_len;
10996 stats->rx_packets++;
10997 u64_stats_update_end(&stats->syncp);
11001 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11002 const struct devlink_trap_item *trap_item,
11003 struct devlink_port *in_devlink_port,
11004 const struct flow_action_cookie *fa_cookie)
11006 metadata->trap_name = trap_item->trap->name;
11007 metadata->trap_group_name = trap_item->group_item->group->name;
11008 metadata->fa_cookie = fa_cookie;
11009 metadata->trap_type = trap_item->trap->type;
11011 spin_lock(&in_devlink_port->type_lock);
11012 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11013 metadata->input_dev = in_devlink_port->type_dev;
11014 spin_unlock(&in_devlink_port->type_lock);
11018 * devlink_trap_report - Report trapped packet to drop monitor.
11019 * @devlink: devlink.
11020 * @skb: Trapped packet.
11021 * @trap_ctx: Trap context.
11022 * @in_devlink_port: Input devlink port.
11023 * @fa_cookie: Flow action cookie. Could be NULL.
11025 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11026 void *trap_ctx, struct devlink_port *in_devlink_port,
11027 const struct flow_action_cookie *fa_cookie)
11030 struct devlink_trap_item *trap_item = trap_ctx;
11032 devlink_trap_stats_update(trap_item->stats, skb->len);
11033 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11035 if (trace_devlink_trap_report_enabled()) {
11036 struct devlink_trap_metadata metadata = {};
11038 devlink_trap_report_metadata_set(&metadata, trap_item,
11039 in_devlink_port, fa_cookie);
11040 trace_devlink_trap_report(devlink, skb, &metadata);
11043 EXPORT_SYMBOL_GPL(devlink_trap_report);
11046 * devlink_trap_ctx_priv - Trap context to driver private information.
11047 * @trap_ctx: Trap context.
11049 * Return: Driver private information passed during registration.
11051 void *devlink_trap_ctx_priv(void *trap_ctx)
11053 struct devlink_trap_item *trap_item = trap_ctx;
11055 return trap_item->priv;
11057 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
11060 devlink_trap_group_item_policer_link(struct devlink *devlink,
11061 struct devlink_trap_group_item *group_item)
11063 u32 policer_id = group_item->group->init_policer_id;
11064 struct devlink_trap_policer_item *policer_item;
11066 if (policer_id == 0)
11069 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
11070 if (WARN_ON_ONCE(!policer_item))
11073 group_item->policer_item = policer_item;
11079 devlink_trap_group_register(struct devlink *devlink,
11080 const struct devlink_trap_group *group)
11082 struct devlink_trap_group_item *group_item;
11085 if (devlink_trap_group_item_lookup(devlink, group->name))
11088 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
11092 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11093 if (!group_item->stats) {
11095 goto err_stats_alloc;
11098 group_item->group = group;
11100 err = devlink_trap_group_item_policer_link(devlink, group_item);
11102 goto err_policer_link;
11104 if (devlink->ops->trap_group_init) {
11105 err = devlink->ops->trap_group_init(devlink, group);
11107 goto err_group_init;
11110 list_add_tail(&group_item->list, &devlink->trap_group_list);
11111 devlink_trap_group_notify(devlink, group_item,
11112 DEVLINK_CMD_TRAP_GROUP_NEW);
11118 free_percpu(group_item->stats);
11125 devlink_trap_group_unregister(struct devlink *devlink,
11126 const struct devlink_trap_group *group)
11128 struct devlink_trap_group_item *group_item;
11130 group_item = devlink_trap_group_item_lookup(devlink, group->name);
11131 if (WARN_ON_ONCE(!group_item))
11134 devlink_trap_group_notify(devlink, group_item,
11135 DEVLINK_CMD_TRAP_GROUP_DEL);
11136 list_del(&group_item->list);
11137 free_percpu(group_item->stats);
11142 * devlink_trap_groups_register - Register packet trap groups with devlink.
11143 * @devlink: devlink.
11144 * @groups: Packet trap groups.
11145 * @groups_count: Count of provided packet trap groups.
11147 * Return: Non-zero value on failure.
11149 int devlink_trap_groups_register(struct devlink *devlink,
11150 const struct devlink_trap_group *groups,
11151 size_t groups_count)
11155 mutex_lock(&devlink->lock);
11156 for (i = 0; i < groups_count; i++) {
11157 const struct devlink_trap_group *group = &groups[i];
11159 err = devlink_trap_group_verify(group);
11161 goto err_trap_group_verify;
11163 err = devlink_trap_group_register(devlink, group);
11165 goto err_trap_group_register;
11167 mutex_unlock(&devlink->lock);
11171 err_trap_group_register:
11172 err_trap_group_verify:
11173 for (i--; i >= 0; i--)
11174 devlink_trap_group_unregister(devlink, &groups[i]);
11175 mutex_unlock(&devlink->lock);
11178 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
11181 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
11182 * @devlink: devlink.
11183 * @groups: Packet trap groups.
11184 * @groups_count: Count of provided packet trap groups.
11186 void devlink_trap_groups_unregister(struct devlink *devlink,
11187 const struct devlink_trap_group *groups,
11188 size_t groups_count)
11192 mutex_lock(&devlink->lock);
11193 for (i = groups_count - 1; i >= 0; i--)
11194 devlink_trap_group_unregister(devlink, &groups[i]);
11195 mutex_unlock(&devlink->lock);
11197 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
11200 devlink_trap_policer_notify(struct devlink *devlink,
11201 const struct devlink_trap_policer_item *policer_item,
11202 enum devlink_command cmd)
11204 struct sk_buff *msg;
11207 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
11208 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
11209 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11212 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11216 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
11223 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11224 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11228 devlink_trap_policer_register(struct devlink *devlink,
11229 const struct devlink_trap_policer *policer)
11231 struct devlink_trap_policer_item *policer_item;
11234 if (devlink_trap_policer_item_lookup(devlink, policer->id))
11237 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
11241 policer_item->policer = policer;
11242 policer_item->rate = policer->init_rate;
11243 policer_item->burst = policer->init_burst;
11245 if (devlink->ops->trap_policer_init) {
11246 err = devlink->ops->trap_policer_init(devlink, policer);
11248 goto err_policer_init;
11251 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
11252 devlink_trap_policer_notify(devlink, policer_item,
11253 DEVLINK_CMD_TRAP_POLICER_NEW);
11258 kfree(policer_item);
11263 devlink_trap_policer_unregister(struct devlink *devlink,
11264 const struct devlink_trap_policer *policer)
11266 struct devlink_trap_policer_item *policer_item;
11268 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
11269 if (WARN_ON_ONCE(!policer_item))
11272 devlink_trap_policer_notify(devlink, policer_item,
11273 DEVLINK_CMD_TRAP_POLICER_DEL);
11274 list_del(&policer_item->list);
11275 if (devlink->ops->trap_policer_fini)
11276 devlink->ops->trap_policer_fini(devlink, policer);
11277 kfree(policer_item);
11281 * devlink_trap_policers_register - Register packet trap policers with devlink.
11282 * @devlink: devlink.
11283 * @policers: Packet trap policers.
11284 * @policers_count: Count of provided packet trap policers.
11286 * Return: Non-zero value on failure.
11289 devlink_trap_policers_register(struct devlink *devlink,
11290 const struct devlink_trap_policer *policers,
11291 size_t policers_count)
11295 mutex_lock(&devlink->lock);
11296 for (i = 0; i < policers_count; i++) {
11297 const struct devlink_trap_policer *policer = &policers[i];
11299 if (WARN_ON(policer->id == 0 ||
11300 policer->max_rate < policer->min_rate ||
11301 policer->max_burst < policer->min_burst)) {
11303 goto err_trap_policer_verify;
11306 err = devlink_trap_policer_register(devlink, policer);
11308 goto err_trap_policer_register;
11310 mutex_unlock(&devlink->lock);
11314 err_trap_policer_register:
11315 err_trap_policer_verify:
11316 for (i--; i >= 0; i--)
11317 devlink_trap_policer_unregister(devlink, &policers[i]);
11318 mutex_unlock(&devlink->lock);
11321 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
11324 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
11325 * @devlink: devlink.
11326 * @policers: Packet trap policers.
11327 * @policers_count: Count of provided packet trap policers.
11330 devlink_trap_policers_unregister(struct devlink *devlink,
11331 const struct devlink_trap_policer *policers,
11332 size_t policers_count)
11336 mutex_lock(&devlink->lock);
11337 for (i = policers_count - 1; i >= 0; i--)
11338 devlink_trap_policer_unregister(devlink, &policers[i]);
11339 mutex_unlock(&devlink->lock);
11341 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
11343 static void __devlink_compat_running_version(struct devlink *devlink,
11344 char *buf, size_t len)
11346 const struct nlattr *nlattr;
11347 struct devlink_info_req req;
11348 struct sk_buff *msg;
11351 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11356 err = devlink->ops->info_get(devlink, &req, NULL);
11360 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
11361 const struct nlattr *kv;
11364 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
11367 nla_for_each_nested(kv, nlattr, rem_kv) {
11368 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
11371 strlcat(buf, nla_data(kv), len);
11372 strlcat(buf, " ", len);
11379 static struct devlink_port *netdev_to_devlink_port(struct net_device *dev)
11381 if (!dev->netdev_ops->ndo_get_devlink_port)
11384 return dev->netdev_ops->ndo_get_devlink_port(dev);
11387 void devlink_compat_running_version(struct devlink *devlink,
11388 char *buf, size_t len)
11390 if (!devlink->ops->info_get)
11393 mutex_lock(&devlink->lock);
11394 __devlink_compat_running_version(devlink, buf, len);
11395 mutex_unlock(&devlink->lock);
11398 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
11400 struct devlink_flash_update_params params = {};
11403 if (!devlink->ops->flash_update)
11404 return -EOPNOTSUPP;
11406 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
11410 mutex_lock(&devlink->lock);
11411 devlink_flash_update_begin_notify(devlink);
11412 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
11413 devlink_flash_update_end_notify(devlink);
11414 mutex_unlock(&devlink->lock);
11416 release_firmware(params.fw);
11421 int devlink_compat_phys_port_name_get(struct net_device *dev,
11422 char *name, size_t len)
11424 struct devlink_port *devlink_port;
11426 /* RTNL mutex is held here which ensures that devlink_port
11427 * instance cannot disappear in the middle. No need to take
11428 * any devlink lock as only permanent values are accessed.
11432 devlink_port = netdev_to_devlink_port(dev);
11434 return -EOPNOTSUPP;
11436 return __devlink_port_phys_port_name_get(devlink_port, name, len);
11439 int devlink_compat_switch_id_get(struct net_device *dev,
11440 struct netdev_phys_item_id *ppid)
11442 struct devlink_port *devlink_port;
11444 /* Caller must hold RTNL mutex or reference to dev, which ensures that
11445 * devlink_port instance cannot disappear in the middle. No need to take
11446 * any devlink lock as only permanent values are accessed.
11448 devlink_port = netdev_to_devlink_port(dev);
11449 if (!devlink_port || !devlink_port->switch_port)
11450 return -EOPNOTSUPP;
11452 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
11457 static void __net_exit devlink_pernet_pre_exit(struct net *net)
11459 struct devlink *devlink;
11460 u32 actions_performed;
11461 unsigned long index;
11464 /* In case network namespace is getting destroyed, reload
11465 * all devlink instances from this namespace into init_net.
11467 mutex_lock(&devlink_mutex);
11468 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
11469 if (!devlink_try_get(devlink))
11472 if (!net_eq(devlink_net(devlink), net))
11475 WARN_ON(!(devlink->features & DEVLINK_F_RELOAD));
11476 err = devlink_reload(devlink, &init_net,
11477 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
11478 DEVLINK_RELOAD_LIMIT_UNSPEC,
11479 &actions_performed, NULL);
11480 if (err && err != -EOPNOTSUPP)
11481 pr_warn("Failed to reload devlink instance into init_net\n");
11483 devlink_put(devlink);
11485 mutex_unlock(&devlink_mutex);
11488 static struct pernet_operations devlink_pernet_ops __net_initdata = {
11489 .pre_exit = devlink_pernet_pre_exit,
11492 static int __init devlink_init(void)
11496 err = genl_register_family(&devlink_nl_family);
11499 err = register_pernet_subsys(&devlink_pernet_ops);
11506 subsys_initcall(devlink_init);