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 #include "devl_internal.h"
36 struct devlink_linecard {
37 struct list_head list;
38 struct devlink *devlink;
40 const struct devlink_linecard_ops *ops;
42 enum devlink_linecard_state state;
43 struct mutex state_lock; /* Protects state */
45 struct devlink_linecard_type *types;
46 unsigned int types_count;
47 struct devlink *nested_devlink;
51 * struct devlink_resource - devlink resource
52 * @name: name of the resource
53 * @id: id, per devlink instance
54 * @size: size of the resource
55 * @size_new: updated size of the resource, reload is needed
56 * @size_valid: valid in case the total size of the resource is valid
57 * including its children
58 * @parent: parent resource
59 * @size_params: size parameters
61 * @resource_list: list of child resources
62 * @occ_get: occupancy getter callback
63 * @occ_get_priv: occupancy getter callback priv
65 struct devlink_resource {
71 struct devlink_resource *parent;
72 struct devlink_resource_size_params size_params;
73 struct list_head list;
74 struct list_head resource_list;
75 devlink_resource_occ_get_t *occ_get;
79 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
81 .name = "destination mac",
82 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
87 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
89 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
90 .fields = devlink_dpipe_fields_ethernet,
91 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
94 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
96 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
98 .name = "destination ip",
99 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
104 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
106 .id = DEVLINK_DPIPE_HEADER_IPV4,
107 .fields = devlink_dpipe_fields_ipv4,
108 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
111 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
113 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
115 .name = "destination ip",
116 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
121 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
123 .id = DEVLINK_DPIPE_HEADER_IPV6,
124 .fields = devlink_dpipe_fields_ipv6,
125 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
128 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
130 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
131 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
132 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
134 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \
135 (_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1)
137 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
138 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
139 [DEVLINK_PORT_FN_ATTR_STATE] =
140 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
141 DEVLINK_PORT_FN_STATE_ACTIVE),
142 [DEVLINK_PORT_FN_ATTR_CAPS] =
143 NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK),
146 static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
147 [DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
150 #define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port) \
151 WARN_ON_ONCE(!(devlink_port)->registered)
152 #define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port) \
153 WARN_ON_ONCE((devlink_port)->registered)
154 #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \
155 WARN_ON_ONCE(!(devlink_port)->initialized)
157 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
158 unsigned int port_index)
160 return xa_load(&devlink->ports, port_index);
163 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
164 struct nlattr **attrs)
166 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
167 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
168 struct devlink_port *devlink_port;
170 devlink_port = devlink_port_get_by_index(devlink, port_index);
172 return ERR_PTR(-ENODEV);
175 return ERR_PTR(-EINVAL);
178 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
179 struct genl_info *info)
181 return devlink_port_get_from_attrs(devlink, info->attrs);
185 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
187 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
191 devlink_rate_is_node(struct devlink_rate *devlink_rate)
193 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
196 static struct devlink_rate *
197 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
199 struct devlink_rate *devlink_rate;
200 struct devlink_port *devlink_port;
202 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
203 if (IS_ERR(devlink_port))
204 return ERR_CAST(devlink_port);
205 devlink_rate = devlink_port->devlink_rate;
206 return devlink_rate ?: ERR_PTR(-ENODEV);
209 static struct devlink_rate *
210 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
212 static struct devlink_rate *devlink_rate;
214 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
215 if (devlink_rate_is_node(devlink_rate) &&
216 !strcmp(node_name, devlink_rate->name))
219 return ERR_PTR(-ENODEV);
222 static struct devlink_rate *
223 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
225 const char *rate_node_name;
228 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
229 return ERR_PTR(-EINVAL);
230 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
231 len = strlen(rate_node_name);
232 /* Name cannot be empty or decimal number */
233 if (!len || strspn(rate_node_name, "0123456789") == len)
234 return ERR_PTR(-EINVAL);
236 return devlink_rate_node_get_by_name(devlink, rate_node_name);
239 struct devlink_rate *
240 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
242 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
245 struct devlink_rate *
246 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
248 struct nlattr **attrs = info->attrs;
250 if (attrs[DEVLINK_ATTR_PORT_INDEX])
251 return devlink_rate_leaf_get_from_info(devlink, info);
252 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
253 return devlink_rate_node_get_from_info(devlink, info);
255 return ERR_PTR(-EINVAL);
258 static struct devlink_linecard *
259 devlink_linecard_get_by_index(struct devlink *devlink,
260 unsigned int linecard_index)
262 struct devlink_linecard *devlink_linecard;
264 list_for_each_entry(devlink_linecard, &devlink->linecard_list, list) {
265 if (devlink_linecard->index == linecard_index)
266 return devlink_linecard;
271 static bool devlink_linecard_index_exists(struct devlink *devlink,
272 unsigned int linecard_index)
274 return devlink_linecard_get_by_index(devlink, linecard_index);
277 static struct devlink_linecard *
278 devlink_linecard_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
280 if (attrs[DEVLINK_ATTR_LINECARD_INDEX]) {
281 u32 linecard_index = nla_get_u32(attrs[DEVLINK_ATTR_LINECARD_INDEX]);
282 struct devlink_linecard *linecard;
284 linecard = devlink_linecard_get_by_index(devlink, linecard_index);
286 return ERR_PTR(-ENODEV);
289 return ERR_PTR(-EINVAL);
292 struct devlink_linecard *
293 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info)
295 return devlink_linecard_get_from_attrs(devlink, info->attrs);
299 struct list_head list;
302 u16 ingress_pools_count;
303 u16 egress_pools_count;
304 u16 ingress_tc_count;
308 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
310 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
313 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
314 unsigned int sb_index)
316 struct devlink_sb *devlink_sb;
318 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
319 if (devlink_sb->index == sb_index)
325 static bool devlink_sb_index_exists(struct devlink *devlink,
326 unsigned int sb_index)
328 return devlink_sb_get_by_index(devlink, sb_index);
331 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
332 struct nlattr **attrs)
334 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
335 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
336 struct devlink_sb *devlink_sb;
338 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
340 return ERR_PTR(-ENODEV);
343 return ERR_PTR(-EINVAL);
346 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
347 struct genl_info *info)
349 return devlink_sb_get_from_attrs(devlink, info->attrs);
352 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
353 struct nlattr **attrs,
358 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
361 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
362 if (val >= devlink_sb_pool_count(devlink_sb))
368 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
369 struct genl_info *info,
372 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
377 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
378 enum devlink_sb_pool_type *p_pool_type)
382 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
385 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
386 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
387 val != DEVLINK_SB_POOL_TYPE_EGRESS)
394 devlink_sb_pool_type_get_from_info(struct genl_info *info,
395 enum devlink_sb_pool_type *p_pool_type)
397 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
401 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
402 enum devlink_sb_threshold_type *p_th_type)
406 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
409 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
410 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
411 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
418 devlink_sb_th_type_get_from_info(struct genl_info *info,
419 enum devlink_sb_threshold_type *p_th_type)
421 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
425 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
426 struct nlattr **attrs,
427 enum devlink_sb_pool_type pool_type,
432 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
435 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
436 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
437 val >= devlink_sb->ingress_tc_count)
439 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
440 val >= devlink_sb->egress_tc_count)
446 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps,
447 u32 cap, bool is_enable)
449 caps->selector |= cap;
454 static int devlink_port_fn_roce_fill(const struct devlink_ops *ops,
455 struct devlink_port *devlink_port,
456 struct nla_bitfield32 *caps,
457 struct netlink_ext_ack *extack)
462 if (!ops->port_fn_roce_get)
465 err = ops->port_fn_roce_get(devlink_port, &is_enable, extack);
467 if (err == -EOPNOTSUPP)
472 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable);
476 static int devlink_port_fn_migratable_fill(const struct devlink_ops *ops,
477 struct devlink_port *devlink_port,
478 struct nla_bitfield32 *caps,
479 struct netlink_ext_ack *extack)
484 if (!ops->port_fn_migratable_get ||
485 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
488 err = ops->port_fn_migratable_get(devlink_port, &is_enable, extack);
490 if (err == -EOPNOTSUPP)
495 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable);
499 static int devlink_port_fn_caps_fill(const struct devlink_ops *ops,
500 struct devlink_port *devlink_port,
502 struct netlink_ext_ack *extack,
505 struct nla_bitfield32 caps = {};
508 err = devlink_port_fn_roce_fill(ops, devlink_port, &caps, extack);
512 err = devlink_port_fn_migratable_fill(ops, devlink_port, &caps, extack);
518 err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value,
528 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
529 struct genl_info *info,
530 enum devlink_sb_pool_type pool_type,
533 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
534 pool_type, p_tc_index);
537 struct devlink_region {
538 struct devlink *devlink;
539 struct devlink_port *port;
540 struct list_head list;
542 const struct devlink_region_ops *ops;
543 const struct devlink_port_region_ops *port_ops;
545 struct mutex snapshot_lock; /* protects snapshot_list,
546 * max_snapshots and cur_snapshots
549 struct list_head snapshot_list;
555 struct devlink_snapshot {
556 struct list_head list;
557 struct devlink_region *region;
562 static struct devlink_region *
563 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
565 struct devlink_region *region;
567 list_for_each_entry(region, &devlink->region_list, list)
568 if (!strcmp(region->ops->name, region_name))
574 static struct devlink_region *
575 devlink_port_region_get_by_name(struct devlink_port *port,
576 const char *region_name)
578 struct devlink_region *region;
580 list_for_each_entry(region, &port->region_list, list)
581 if (!strcmp(region->ops->name, region_name))
587 static struct devlink_snapshot *
588 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
590 struct devlink_snapshot *snapshot;
592 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
593 if (snapshot->id == id)
599 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
601 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
603 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
608 static int devlink_nl_put_nested_handle(struct sk_buff *msg, struct devlink *devlink)
610 struct nlattr *nested_attr;
612 nested_attr = nla_nest_start(msg, DEVLINK_ATTR_NESTED_DEVLINK);
615 if (devlink_nl_put_handle(msg, devlink))
616 goto nla_put_failure;
618 nla_nest_end(msg, nested_attr);
622 nla_nest_cancel(msg, nested_attr);
626 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
628 if (devlink_nl_put_handle(msg, devlink_port->devlink))
630 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
635 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
637 struct devlink *devlink = devlink_port->devlink;
639 return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */
640 + nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */
641 + nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */
644 struct devlink_reload_combination {
645 enum devlink_reload_action action;
646 enum devlink_reload_limit limit;
649 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
651 /* can't reinitialize driver with no down time */
652 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
653 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
658 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
659 enum devlink_reload_limit limit)
663 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
664 if (devlink_reload_invalid_combinations[i].action == action &&
665 devlink_reload_invalid_combinations[i].limit == limit)
671 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
673 return test_bit(action, &devlink->ops->reload_actions);
677 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
679 return test_bit(limit, &devlink->ops->reload_limits);
682 static int devlink_reload_stat_put(struct sk_buff *msg,
683 enum devlink_reload_limit limit, u32 value)
685 struct nlattr *reload_stats_entry;
687 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
688 if (!reload_stats_entry)
691 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
692 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
693 goto nla_put_failure;
694 nla_nest_end(msg, reload_stats_entry);
698 nla_nest_cancel(msg, reload_stats_entry);
702 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
704 struct nlattr *reload_stats_attr, *act_info, *act_stats;
709 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
711 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
713 if (!reload_stats_attr)
716 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
718 !devlink_reload_action_is_supported(devlink, i)) ||
719 i == DEVLINK_RELOAD_ACTION_UNSPEC)
721 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
723 goto nla_put_failure;
725 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
726 goto action_info_nest_cancel;
727 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
729 goto action_info_nest_cancel;
731 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
732 /* Remote stats are shown even if not locally supported.
733 * Stats of actions with unspecified limit are shown
734 * though drivers don't need to register unspecified
737 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
738 !devlink_reload_limit_is_supported(devlink, j)) ||
739 devlink_reload_combination_is_invalid(i, j))
742 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
744 value = devlink->stats.reload_stats[stat_idx];
746 value = devlink->stats.remote_reload_stats[stat_idx];
747 if (devlink_reload_stat_put(msg, j, value))
748 goto action_stats_nest_cancel;
750 nla_nest_end(msg, act_stats);
751 nla_nest_end(msg, act_info);
753 nla_nest_end(msg, reload_stats_attr);
756 action_stats_nest_cancel:
757 nla_nest_cancel(msg, act_stats);
758 action_info_nest_cancel:
759 nla_nest_cancel(msg, act_info);
761 nla_nest_cancel(msg, reload_stats_attr);
765 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
766 enum devlink_command cmd, u32 portid,
769 struct nlattr *dev_stats;
772 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
776 if (devlink_nl_put_handle(msg, devlink))
777 goto nla_put_failure;
778 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
779 goto nla_put_failure;
781 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
783 goto nla_put_failure;
785 if (devlink_reload_stats_put(msg, devlink, false))
786 goto dev_stats_nest_cancel;
787 if (devlink_reload_stats_put(msg, devlink, true))
788 goto dev_stats_nest_cancel;
790 nla_nest_end(msg, dev_stats);
791 genlmsg_end(msg, hdr);
794 dev_stats_nest_cancel:
795 nla_nest_cancel(msg, dev_stats);
797 genlmsg_cancel(msg, hdr);
801 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
806 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
807 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
809 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
813 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
819 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
820 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
823 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
824 struct devlink_port *devlink_port)
826 struct devlink_port_attrs *attrs = &devlink_port->attrs;
828 if (!devlink_port->attrs_set)
831 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
834 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
836 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
838 switch (devlink_port->attrs.flavour) {
839 case DEVLINK_PORT_FLAVOUR_PCI_PF:
840 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
841 attrs->pci_pf.controller) ||
842 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
844 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
847 case DEVLINK_PORT_FLAVOUR_PCI_VF:
848 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
849 attrs->pci_vf.controller) ||
850 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
851 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
853 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
856 case DEVLINK_PORT_FLAVOUR_PCI_SF:
857 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
858 attrs->pci_sf.controller) ||
859 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
861 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
865 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
866 case DEVLINK_PORT_FLAVOUR_CPU:
867 case DEVLINK_PORT_FLAVOUR_DSA:
868 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
869 attrs->phys.port_number))
873 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
874 attrs->phys.port_number))
876 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
877 attrs->phys.split_subport_number))
886 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
887 struct devlink_port *port,
889 struct netlink_ext_ack *extack,
892 u8 hw_addr[MAX_ADDR_LEN];
896 if (!ops->port_function_hw_addr_get)
899 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
902 if (err == -EOPNOTSUPP)
906 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
913 static int devlink_nl_rate_fill(struct sk_buff *msg,
914 struct devlink_rate *devlink_rate,
915 enum devlink_command cmd, u32 portid, u32 seq,
916 int flags, struct netlink_ext_ack *extack)
918 struct devlink *devlink = devlink_rate->devlink;
921 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
925 if (devlink_nl_put_handle(msg, devlink))
926 goto nla_put_failure;
928 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
929 goto nla_put_failure;
931 if (devlink_rate_is_leaf(devlink_rate)) {
932 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
933 devlink_rate->devlink_port->index))
934 goto nla_put_failure;
935 } else if (devlink_rate_is_node(devlink_rate)) {
936 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
938 goto nla_put_failure;
941 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
942 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
943 goto nla_put_failure;
945 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
946 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
947 goto nla_put_failure;
949 if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_PRIORITY,
950 devlink_rate->tx_priority))
951 goto nla_put_failure;
953 if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_WEIGHT,
954 devlink_rate->tx_weight))
955 goto nla_put_failure;
957 if (devlink_rate->parent)
958 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
959 devlink_rate->parent->name))
960 goto nla_put_failure;
962 genlmsg_end(msg, hdr);
966 genlmsg_cancel(msg, hdr);
971 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
973 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
974 state == DEVLINK_PORT_FN_STATE_ACTIVE;
978 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
980 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
981 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
984 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
985 struct devlink_port *port,
987 struct netlink_ext_ack *extack,
990 enum devlink_port_fn_opstate opstate;
991 enum devlink_port_fn_state state;
994 if (!ops->port_fn_state_get)
997 err = ops->port_fn_state_get(port, &state, &opstate, extack);
999 if (err == -EOPNOTSUPP)
1003 if (!devlink_port_fn_state_valid(state)) {
1005 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
1008 if (!devlink_port_fn_opstate_valid(opstate)) {
1010 NL_SET_ERR_MSG_MOD(extack,
1011 "Invalid operational state read from driver");
1014 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
1015 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
1017 *msg_updated = true;
1022 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable,
1023 struct netlink_ext_ack *extack)
1025 const struct devlink_ops *ops = devlink_port->devlink->ops;
1027 return ops->port_fn_migratable_set(devlink_port, enable, extack);
1031 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable,
1032 struct netlink_ext_ack *extack)
1034 const struct devlink_ops *ops = devlink_port->devlink->ops;
1036 return ops->port_fn_roce_set(devlink_port, enable, extack);
1039 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port,
1040 const struct nlattr *attr,
1041 struct netlink_ext_ack *extack)
1043 struct nla_bitfield32 caps;
1047 caps = nla_get_bitfield32(attr);
1048 caps_value = caps.value & caps.selector;
1049 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE) {
1050 err = devlink_port_fn_roce_set(devlink_port,
1051 caps_value & DEVLINK_PORT_FN_CAP_ROCE,
1056 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1057 err = devlink_port_fn_mig_set(devlink_port, caps_value &
1058 DEVLINK_PORT_FN_CAP_MIGRATABLE,
1067 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
1068 struct netlink_ext_ack *extack)
1070 const struct devlink_ops *ops;
1071 struct nlattr *function_attr;
1072 bool msg_updated = false;
1075 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
1079 ops = port->devlink->ops;
1080 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
1084 err = devlink_port_fn_caps_fill(ops, port, msg, extack,
1088 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
1090 if (err || !msg_updated)
1091 nla_nest_cancel(msg, function_attr);
1093 nla_nest_end(msg, function_attr);
1097 static int devlink_nl_port_fill(struct sk_buff *msg,
1098 struct devlink_port *devlink_port,
1099 enum devlink_command cmd, u32 portid, u32 seq,
1100 int flags, struct netlink_ext_ack *extack)
1102 struct devlink *devlink = devlink_port->devlink;
1105 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1109 if (devlink_nl_put_handle(msg, devlink))
1110 goto nla_put_failure;
1111 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1112 goto nla_put_failure;
1114 spin_lock_bh(&devlink_port->type_lock);
1115 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
1116 goto nla_put_failure_type_locked;
1117 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1118 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1119 devlink_port->desired_type))
1120 goto nla_put_failure_type_locked;
1121 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1122 if (devlink_port->type_eth.netdev &&
1123 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1124 devlink_port->type_eth.ifindex) ||
1125 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1126 devlink_port->type_eth.ifname)))
1127 goto nla_put_failure_type_locked;
1129 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1130 struct ib_device *ibdev = devlink_port->type_ib.ibdev;
1133 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1135 goto nla_put_failure_type_locked;
1137 spin_unlock_bh(&devlink_port->type_lock);
1138 if (devlink_nl_port_attrs_put(msg, devlink_port))
1139 goto nla_put_failure;
1140 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1141 goto nla_put_failure;
1142 if (devlink_port->linecard &&
1143 nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
1144 devlink_port->linecard->index))
1145 goto nla_put_failure;
1147 genlmsg_end(msg, hdr);
1150 nla_put_failure_type_locked:
1151 spin_unlock_bh(&devlink_port->type_lock);
1153 genlmsg_cancel(msg, hdr);
1157 static void devlink_port_notify(struct devlink_port *devlink_port,
1158 enum devlink_command cmd)
1160 struct devlink *devlink = devlink_port->devlink;
1161 struct sk_buff *msg;
1164 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1166 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1169 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1173 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1179 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1180 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1183 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1184 enum devlink_command cmd)
1186 struct devlink *devlink = devlink_rate->devlink;
1187 struct sk_buff *msg;
1190 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1192 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1195 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1199 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1205 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1206 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1210 devlink_nl_cmd_rate_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
1211 struct netlink_callback *cb)
1213 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1214 struct devlink_rate *devlink_rate;
1218 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1219 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1220 u32 id = NETLINK_CB(cb->skb).portid;
1222 if (idx < state->idx) {
1226 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1239 const struct devlink_gen_cmd devl_gen_rate_get = {
1240 .dump_one = devlink_nl_cmd_rate_get_dump_one,
1243 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1244 struct genl_info *info)
1246 struct devlink_rate *devlink_rate = info->user_ptr[1];
1247 struct sk_buff *msg;
1250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1254 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1255 info->snd_portid, info->snd_seq, 0,
1262 return genlmsg_reply(msg, info);
1266 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1267 struct devlink_rate *parent)
1270 if (parent == devlink_rate)
1272 parent = parent->parent;
1277 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1279 struct devlink *devlink = info->user_ptr[0];
1280 struct sk_buff *msg;
1283 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1287 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1288 info->snd_portid, info->snd_seq, 0);
1294 return genlmsg_reply(msg, info);
1298 devlink_nl_cmd_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
1299 struct netlink_callback *cb)
1301 return devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1302 NETLINK_CB(cb->skb).portid,
1303 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1306 const struct devlink_gen_cmd devl_gen_inst = {
1307 .dump_one = devlink_nl_cmd_get_dump_one,
1310 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1311 struct genl_info *info)
1313 struct devlink_port *devlink_port = info->user_ptr[1];
1314 struct sk_buff *msg;
1317 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1321 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1322 info->snd_portid, info->snd_seq, 0,
1329 return genlmsg_reply(msg, info);
1333 devlink_nl_cmd_port_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
1334 struct netlink_callback *cb)
1336 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1337 struct devlink_port *devlink_port;
1338 unsigned long port_index;
1342 xa_for_each(&devlink->ports, port_index, devlink_port) {
1343 if (idx < state->idx) {
1347 err = devlink_nl_port_fill(msg, devlink_port,
1349 NETLINK_CB(cb->skb).portid,
1351 NLM_F_MULTI, cb->extack);
1362 const struct devlink_gen_cmd devl_gen_port = {
1363 .dump_one = devlink_nl_cmd_port_get_dump_one,
1366 static int devlink_port_type_set(struct devlink_port *devlink_port,
1367 enum devlink_port_type port_type)
1372 if (!devlink_port->devlink->ops->port_type_set)
1375 if (port_type == devlink_port->type)
1378 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1383 devlink_port->desired_type = port_type;
1384 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1388 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1389 const struct nlattr *attr,
1390 struct netlink_ext_ack *extack)
1392 const struct devlink_ops *ops = port->devlink->ops;
1396 hw_addr = nla_data(attr);
1397 hw_addr_len = nla_len(attr);
1398 if (hw_addr_len > MAX_ADDR_LEN) {
1399 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1402 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1403 if (hw_addr_len != ETH_ALEN) {
1404 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1407 if (!is_unicast_ether_addr(hw_addr)) {
1408 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1413 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1417 static int devlink_port_fn_state_set(struct devlink_port *port,
1418 const struct nlattr *attr,
1419 struct netlink_ext_ack *extack)
1421 enum devlink_port_fn_state state;
1422 const struct devlink_ops *ops;
1424 state = nla_get_u8(attr);
1425 ops = port->devlink->ops;
1426 return ops->port_fn_state_set(port, state, extack);
1429 static int devlink_port_function_validate(struct devlink_port *devlink_port,
1431 struct netlink_ext_ack *extack)
1433 const struct devlink_ops *ops = devlink_port->devlink->ops;
1434 struct nlattr *attr;
1436 if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] &&
1437 !ops->port_function_hw_addr_set) {
1438 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1439 "Port doesn't support function attributes");
1442 if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
1443 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1444 "Function does not support state setting");
1447 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1449 struct nla_bitfield32 caps;
1451 caps = nla_get_bitfield32(attr);
1452 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE &&
1453 !ops->port_fn_roce_set) {
1454 NL_SET_ERR_MSG_ATTR(extack, attr,
1455 "Port doesn't support RoCE function attribute");
1458 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1459 if (!ops->port_fn_migratable_set) {
1460 NL_SET_ERR_MSG_ATTR(extack, attr,
1461 "Port doesn't support migratable function attribute");
1464 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
1465 NL_SET_ERR_MSG_ATTR(extack, attr,
1466 "migratable function attribute supported for VFs only");
1474 static int devlink_port_function_set(struct devlink_port *port,
1475 const struct nlattr *attr,
1476 struct netlink_ext_ack *extack)
1478 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1481 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1482 devlink_function_nl_policy, extack);
1484 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1488 err = devlink_port_function_validate(port, tb, extack);
1492 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1494 err = devlink_port_function_hw_addr_set(port, attr, extack);
1499 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1501 err = devlink_port_fn_caps_set(port, attr, extack);
1506 /* Keep this as the last function attribute set, so that when
1507 * multiple port function attributes are set along with state,
1508 * Those can be applied first before activating the state.
1510 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1512 err = devlink_port_fn_state_set(port, attr, extack);
1515 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1519 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1520 struct genl_info *info)
1522 struct devlink_port *devlink_port = info->user_ptr[1];
1525 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1526 enum devlink_port_type port_type;
1528 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1529 err = devlink_port_type_set(devlink_port, port_type);
1534 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1535 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1536 struct netlink_ext_ack *extack = info->extack;
1538 err = devlink_port_function_set(devlink_port, attr, extack);
1546 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1547 struct genl_info *info)
1549 struct devlink_port *devlink_port = info->user_ptr[1];
1550 struct devlink *devlink = info->user_ptr[0];
1553 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
1555 if (!devlink->ops->port_split)
1558 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1560 if (!devlink_port->attrs.splittable) {
1561 /* Split ports cannot be split. */
1562 if (devlink_port->attrs.split)
1563 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1565 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1569 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1570 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1574 return devlink->ops->port_split(devlink, devlink_port, count,
1578 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1579 struct genl_info *info)
1581 struct devlink_port *devlink_port = info->user_ptr[1];
1582 struct devlink *devlink = info->user_ptr[0];
1584 if (!devlink->ops->port_unsplit)
1586 return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1589 static int devlink_port_new_notify(struct devlink *devlink,
1590 unsigned int port_index,
1591 struct genl_info *info)
1593 struct devlink_port *devlink_port;
1594 struct sk_buff *msg;
1597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1601 lockdep_assert_held(&devlink->lock);
1602 devlink_port = devlink_port_get_by_index(devlink, port_index);
1603 if (!devlink_port) {
1608 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1609 info->snd_portid, info->snd_seq, 0, NULL);
1613 return genlmsg_reply(msg, info);
1620 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1621 struct genl_info *info)
1623 struct netlink_ext_ack *extack = info->extack;
1624 struct devlink_port_new_attrs new_attrs = {};
1625 struct devlink *devlink = info->user_ptr[0];
1626 unsigned int new_port_index;
1629 if (!devlink->ops->port_new || !devlink->ops->port_del)
1632 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1633 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1634 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1637 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1639 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1641 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1642 /* Port index of the new port being created by driver. */
1643 new_attrs.port_index =
1644 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1645 new_attrs.port_index_valid = true;
1647 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1648 new_attrs.controller =
1649 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1650 new_attrs.controller_valid = true;
1652 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1653 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1654 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1655 new_attrs.sfnum_valid = true;
1658 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1663 err = devlink_port_new_notify(devlink, new_port_index, info);
1664 if (err && err != -ENODEV) {
1665 /* Fail to send the response; destroy newly created port. */
1666 devlink->ops->port_del(devlink, new_port_index, extack);
1671 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1672 struct genl_info *info)
1674 struct netlink_ext_ack *extack = info->extack;
1675 struct devlink *devlink = info->user_ptr[0];
1676 unsigned int port_index;
1678 if (!devlink->ops->port_del)
1681 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_INDEX)) {
1682 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1685 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1687 return devlink->ops->port_del(devlink, port_index, extack);
1691 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1692 struct genl_info *info,
1693 struct nlattr *nla_parent)
1695 struct devlink *devlink = devlink_rate->devlink;
1696 const char *parent_name = nla_data(nla_parent);
1697 const struct devlink_ops *ops = devlink->ops;
1698 size_t len = strlen(parent_name);
1699 struct devlink_rate *parent;
1700 int err = -EOPNOTSUPP;
1702 parent = devlink_rate->parent;
1704 if (parent && !len) {
1705 if (devlink_rate_is_leaf(devlink_rate))
1706 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1707 devlink_rate->priv, NULL,
1709 else if (devlink_rate_is_node(devlink_rate))
1710 err = ops->rate_node_parent_set(devlink_rate, NULL,
1711 devlink_rate->priv, NULL,
1716 refcount_dec(&parent->refcnt);
1717 devlink_rate->parent = NULL;
1719 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1723 if (parent == devlink_rate) {
1724 NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1728 if (devlink_rate_is_node(devlink_rate) &&
1729 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1730 NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1734 if (devlink_rate_is_leaf(devlink_rate))
1735 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1736 devlink_rate->priv, parent->priv,
1738 else if (devlink_rate_is_node(devlink_rate))
1739 err = ops->rate_node_parent_set(devlink_rate, parent,
1740 devlink_rate->priv, parent->priv,
1745 if (devlink_rate->parent)
1746 /* we're reassigning to other parent in this case */
1747 refcount_dec(&devlink_rate->parent->refcnt);
1749 refcount_inc(&parent->refcnt);
1750 devlink_rate->parent = parent;
1756 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1757 const struct devlink_ops *ops,
1758 struct genl_info *info)
1760 struct nlattr *nla_parent, **attrs = info->attrs;
1761 int err = -EOPNOTSUPP;
1766 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1767 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1768 if (devlink_rate_is_leaf(devlink_rate))
1769 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1770 rate, info->extack);
1771 else if (devlink_rate_is_node(devlink_rate))
1772 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1773 rate, info->extack);
1776 devlink_rate->tx_share = rate;
1779 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1780 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1781 if (devlink_rate_is_leaf(devlink_rate))
1782 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1783 rate, info->extack);
1784 else if (devlink_rate_is_node(devlink_rate))
1785 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1786 rate, info->extack);
1789 devlink_rate->tx_max = rate;
1792 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]) {
1793 priority = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]);
1794 if (devlink_rate_is_leaf(devlink_rate))
1795 err = ops->rate_leaf_tx_priority_set(devlink_rate, devlink_rate->priv,
1796 priority, info->extack);
1797 else if (devlink_rate_is_node(devlink_rate))
1798 err = ops->rate_node_tx_priority_set(devlink_rate, devlink_rate->priv,
1799 priority, info->extack);
1803 devlink_rate->tx_priority = priority;
1806 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]) {
1807 weight = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]);
1808 if (devlink_rate_is_leaf(devlink_rate))
1809 err = ops->rate_leaf_tx_weight_set(devlink_rate, devlink_rate->priv,
1810 weight, info->extack);
1811 else if (devlink_rate_is_node(devlink_rate))
1812 err = ops->rate_node_tx_weight_set(devlink_rate, devlink_rate->priv,
1813 weight, info->extack);
1817 devlink_rate->tx_weight = weight;
1820 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1822 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1831 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1832 struct genl_info *info,
1833 enum devlink_rate_type type)
1835 struct nlattr **attrs = info->attrs;
1837 if (type == DEVLINK_RATE_TYPE_LEAF) {
1838 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1839 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1842 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1843 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1846 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1847 !ops->rate_leaf_parent_set) {
1848 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1851 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_leaf_tx_priority_set) {
1852 NL_SET_ERR_MSG_ATTR(info->extack,
1853 attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1854 "TX priority set isn't supported for the leafs");
1857 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_leaf_tx_weight_set) {
1858 NL_SET_ERR_MSG_ATTR(info->extack,
1859 attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1860 "TX weight set isn't supported for the leafs");
1863 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1864 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1865 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1868 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1869 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1872 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1873 !ops->rate_node_parent_set) {
1874 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1877 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_node_tx_priority_set) {
1878 NL_SET_ERR_MSG_ATTR(info->extack,
1879 attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1880 "TX priority set isn't supported for the nodes");
1883 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_node_tx_weight_set) {
1884 NL_SET_ERR_MSG_ATTR(info->extack,
1885 attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1886 "TX weight set isn't supported for the nodes");
1890 WARN(1, "Unknown type of rate object");
1897 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1898 struct genl_info *info)
1900 struct devlink_rate *devlink_rate = info->user_ptr[1];
1901 struct devlink *devlink = devlink_rate->devlink;
1902 const struct devlink_ops *ops = devlink->ops;
1905 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1908 err = devlink_nl_rate_set(devlink_rate, ops, info);
1911 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1915 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1916 struct genl_info *info)
1918 struct devlink *devlink = info->user_ptr[0];
1919 struct devlink_rate *rate_node;
1920 const struct devlink_ops *ops;
1924 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1925 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
1929 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1932 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1933 if (!IS_ERR(rate_node))
1935 else if (rate_node == ERR_PTR(-EINVAL))
1938 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
1942 rate_node->devlink = devlink;
1943 rate_node->type = DEVLINK_RATE_TYPE_NODE;
1944 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
1945 if (!rate_node->name) {
1950 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
1954 err = devlink_nl_rate_set(rate_node, ops, info);
1958 refcount_set(&rate_node->refcnt, 1);
1959 list_add(&rate_node->list, &devlink->rate_list);
1960 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
1964 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1966 kfree(rate_node->name);
1972 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
1973 struct genl_info *info)
1975 struct devlink_rate *rate_node = info->user_ptr[1];
1976 struct devlink *devlink = rate_node->devlink;
1977 const struct devlink_ops *ops = devlink->ops;
1980 if (refcount_read(&rate_node->refcnt) > 1) {
1981 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
1985 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
1986 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1987 if (rate_node->parent)
1988 refcount_dec(&rate_node->parent->refcnt);
1989 list_del(&rate_node->list);
1990 kfree(rate_node->name);
1995 struct devlink_linecard_type {
2000 static int devlink_nl_linecard_fill(struct sk_buff *msg,
2001 struct devlink *devlink,
2002 struct devlink_linecard *linecard,
2003 enum devlink_command cmd, u32 portid,
2005 struct netlink_ext_ack *extack)
2007 struct devlink_linecard_type *linecard_type;
2008 struct nlattr *attr;
2012 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2016 if (devlink_nl_put_handle(msg, devlink))
2017 goto nla_put_failure;
2018 if (nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, linecard->index))
2019 goto nla_put_failure;
2020 if (nla_put_u8(msg, DEVLINK_ATTR_LINECARD_STATE, linecard->state))
2021 goto nla_put_failure;
2022 if (linecard->type &&
2023 nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE, linecard->type))
2024 goto nla_put_failure;
2026 if (linecard->types_count) {
2027 attr = nla_nest_start(msg,
2028 DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES);
2030 goto nla_put_failure;
2031 for (i = 0; i < linecard->types_count; i++) {
2032 linecard_type = &linecard->types[i];
2033 if (nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE,
2034 linecard_type->type)) {
2035 nla_nest_cancel(msg, attr);
2036 goto nla_put_failure;
2039 nla_nest_end(msg, attr);
2042 if (linecard->nested_devlink &&
2043 devlink_nl_put_nested_handle(msg, linecard->nested_devlink))
2044 goto nla_put_failure;
2046 genlmsg_end(msg, hdr);
2050 genlmsg_cancel(msg, hdr);
2054 static void devlink_linecard_notify(struct devlink_linecard *linecard,
2055 enum devlink_command cmd)
2057 struct devlink *devlink = linecard->devlink;
2058 struct sk_buff *msg;
2061 WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW &&
2062 cmd != DEVLINK_CMD_LINECARD_DEL);
2064 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
2067 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2071 err = devlink_nl_linecard_fill(msg, devlink, linecard, cmd, 0, 0, 0,
2078 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2079 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2082 static int devlink_nl_cmd_linecard_get_doit(struct sk_buff *skb,
2083 struct genl_info *info)
2085 struct devlink_linecard *linecard = info->user_ptr[1];
2086 struct devlink *devlink = linecard->devlink;
2087 struct sk_buff *msg;
2090 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2094 mutex_lock(&linecard->state_lock);
2095 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2096 DEVLINK_CMD_LINECARD_NEW,
2097 info->snd_portid, info->snd_seq, 0,
2099 mutex_unlock(&linecard->state_lock);
2105 return genlmsg_reply(msg, info);
2108 static int devlink_nl_cmd_linecard_get_dumpit(struct sk_buff *msg,
2109 struct netlink_callback *cb)
2111 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2112 struct devlink_linecard *linecard;
2113 struct devlink *devlink;
2116 devlink_dump_for_each_instance_get(msg, state, devlink) {
2120 if (!devl_is_registered(devlink))
2123 list_for_each_entry(linecard, &devlink->linecard_list, list) {
2124 if (idx < state->idx) {
2128 mutex_lock(&linecard->state_lock);
2129 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2130 DEVLINK_CMD_LINECARD_NEW,
2131 NETLINK_CB(cb->skb).portid,
2135 mutex_unlock(&linecard->state_lock);
2137 devl_unlock(devlink);
2138 devlink_put(devlink);
2145 devl_unlock(devlink);
2146 devlink_put(devlink);
2152 static struct devlink_linecard_type *
2153 devlink_linecard_type_lookup(struct devlink_linecard *linecard,
2156 struct devlink_linecard_type *linecard_type;
2159 for (i = 0; i < linecard->types_count; i++) {
2160 linecard_type = &linecard->types[i];
2161 if (!strcmp(type, linecard_type->type))
2162 return linecard_type;
2167 static int devlink_linecard_type_set(struct devlink_linecard *linecard,
2169 struct netlink_ext_ack *extack)
2171 const struct devlink_linecard_ops *ops = linecard->ops;
2172 struct devlink_linecard_type *linecard_type;
2175 mutex_lock(&linecard->state_lock);
2176 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2177 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2181 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2182 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2187 linecard_type = devlink_linecard_type_lookup(linecard, type);
2188 if (!linecard_type) {
2189 NL_SET_ERR_MSG_MOD(extack, "Unsupported line card type provided");
2194 if (linecard->state != DEVLINK_LINECARD_STATE_UNPROVISIONED &&
2195 linecard->state != DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2196 NL_SET_ERR_MSG_MOD(extack, "Line card already provisioned");
2198 /* Check if the line card is provisioned in the same
2199 * way the user asks. In case it is, make the operation
2200 * to return success.
2202 if (ops->same_provision &&
2203 ops->same_provision(linecard, linecard->priv,
2204 linecard_type->type,
2205 linecard_type->priv))
2210 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING;
2211 linecard->type = linecard_type->type;
2212 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2213 mutex_unlock(&linecard->state_lock);
2214 err = ops->provision(linecard, linecard->priv, linecard_type->type,
2215 linecard_type->priv, extack);
2217 /* Provisioning failed. Assume the linecard is unprovisioned
2218 * for future operations.
2220 mutex_lock(&linecard->state_lock);
2221 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2222 linecard->type = NULL;
2223 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2224 mutex_unlock(&linecard->state_lock);
2229 mutex_unlock(&linecard->state_lock);
2233 static int devlink_linecard_type_unset(struct devlink_linecard *linecard,
2234 struct netlink_ext_ack *extack)
2238 mutex_lock(&linecard->state_lock);
2239 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2240 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2244 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2245 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2249 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2250 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2251 linecard->type = NULL;
2252 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2257 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONED) {
2258 NL_SET_ERR_MSG_MOD(extack, "Line card is not provisioned");
2262 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONING;
2263 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2264 mutex_unlock(&linecard->state_lock);
2265 err = linecard->ops->unprovision(linecard, linecard->priv,
2268 /* Unprovisioning failed. Assume the linecard is unprovisioned
2269 * for future operations.
2271 mutex_lock(&linecard->state_lock);
2272 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2273 linecard->type = NULL;
2274 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2275 mutex_unlock(&linecard->state_lock);
2280 mutex_unlock(&linecard->state_lock);
2284 static int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
2285 struct genl_info *info)
2287 struct devlink_linecard *linecard = info->user_ptr[1];
2288 struct netlink_ext_ack *extack = info->extack;
2291 if (info->attrs[DEVLINK_ATTR_LINECARD_TYPE]) {
2294 type = nla_data(info->attrs[DEVLINK_ATTR_LINECARD_TYPE]);
2295 if (strcmp(type, "")) {
2296 err = devlink_linecard_type_set(linecard, type, extack);
2300 err = devlink_linecard_type_unset(linecard, extack);
2309 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
2310 struct devlink_sb *devlink_sb,
2311 enum devlink_command cmd, u32 portid,
2316 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2320 if (devlink_nl_put_handle(msg, devlink))
2321 goto nla_put_failure;
2322 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2323 goto nla_put_failure;
2324 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
2325 goto nla_put_failure;
2326 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
2327 devlink_sb->ingress_pools_count))
2328 goto nla_put_failure;
2329 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
2330 devlink_sb->egress_pools_count))
2331 goto nla_put_failure;
2332 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
2333 devlink_sb->ingress_tc_count))
2334 goto nla_put_failure;
2335 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
2336 devlink_sb->egress_tc_count))
2337 goto nla_put_failure;
2339 genlmsg_end(msg, hdr);
2343 genlmsg_cancel(msg, hdr);
2347 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2348 struct genl_info *info)
2350 struct devlink *devlink = info->user_ptr[0];
2351 struct devlink_sb *devlink_sb;
2352 struct sk_buff *msg;
2355 devlink_sb = devlink_sb_get_from_info(devlink, info);
2356 if (IS_ERR(devlink_sb))
2357 return PTR_ERR(devlink_sb);
2359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2363 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2365 info->snd_portid, info->snd_seq, 0);
2371 return genlmsg_reply(msg, info);
2375 devlink_nl_cmd_sb_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
2376 struct netlink_callback *cb)
2378 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2379 struct devlink_sb *devlink_sb;
2383 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2384 if (idx < state->idx) {
2388 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2390 NETLINK_CB(cb->skb).portid,
2403 const struct devlink_gen_cmd devl_gen_sb = {
2404 .dump_one = devlink_nl_cmd_sb_get_dump_one,
2407 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2408 struct devlink_sb *devlink_sb,
2409 u16 pool_index, enum devlink_command cmd,
2410 u32 portid, u32 seq, int flags)
2412 struct devlink_sb_pool_info pool_info;
2416 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2417 pool_index, &pool_info);
2421 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2425 if (devlink_nl_put_handle(msg, devlink))
2426 goto nla_put_failure;
2427 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2428 goto nla_put_failure;
2429 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2430 goto nla_put_failure;
2431 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2432 goto nla_put_failure;
2433 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2434 goto nla_put_failure;
2435 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2436 pool_info.threshold_type))
2437 goto nla_put_failure;
2438 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2439 pool_info.cell_size))
2440 goto nla_put_failure;
2442 genlmsg_end(msg, hdr);
2446 genlmsg_cancel(msg, hdr);
2450 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2451 struct genl_info *info)
2453 struct devlink *devlink = info->user_ptr[0];
2454 struct devlink_sb *devlink_sb;
2455 struct sk_buff *msg;
2459 devlink_sb = devlink_sb_get_from_info(devlink, info);
2460 if (IS_ERR(devlink_sb))
2461 return PTR_ERR(devlink_sb);
2463 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2468 if (!devlink->ops->sb_pool_get)
2471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2475 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2476 DEVLINK_CMD_SB_POOL_NEW,
2477 info->snd_portid, info->snd_seq, 0);
2483 return genlmsg_reply(msg, info);
2486 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2487 struct devlink *devlink,
2488 struct devlink_sb *devlink_sb,
2489 u32 portid, u32 seq)
2491 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2495 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2496 if (*p_idx < start) {
2500 err = devlink_nl_sb_pool_fill(msg, devlink,
2503 DEVLINK_CMD_SB_POOL_NEW,
2504 portid, seq, NLM_F_MULTI);
2513 devlink_nl_cmd_sb_pool_get_dump_one(struct sk_buff *msg,
2514 struct devlink *devlink,
2515 struct netlink_callback *cb)
2517 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2518 struct devlink_sb *devlink_sb;
2522 if (!devlink->ops->sb_pool_get)
2525 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2526 err = __sb_pool_get_dumpit(msg, state->idx, &idx,
2527 devlink, devlink_sb,
2528 NETLINK_CB(cb->skb).portid,
2529 cb->nlh->nlmsg_seq);
2530 if (err == -EOPNOTSUPP) {
2541 const struct devlink_gen_cmd devl_gen_sb_pool = {
2542 .dump_one = devlink_nl_cmd_sb_pool_get_dump_one,
2545 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2546 u16 pool_index, u32 size,
2547 enum devlink_sb_threshold_type threshold_type,
2548 struct netlink_ext_ack *extack)
2551 const struct devlink_ops *ops = devlink->ops;
2553 if (ops->sb_pool_set)
2554 return ops->sb_pool_set(devlink, sb_index, pool_index,
2555 size, threshold_type, extack);
2559 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2560 struct genl_info *info)
2562 struct devlink *devlink = info->user_ptr[0];
2563 enum devlink_sb_threshold_type threshold_type;
2564 struct devlink_sb *devlink_sb;
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_index_get_from_info(devlink_sb, info,
2578 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2582 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_POOL_SIZE))
2585 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2586 return devlink_sb_pool_set(devlink, devlink_sb->index,
2587 pool_index, size, threshold_type,
2591 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2592 struct devlink *devlink,
2593 struct devlink_port *devlink_port,
2594 struct devlink_sb *devlink_sb,
2596 enum devlink_command cmd,
2597 u32 portid, u32 seq, int flags)
2599 const struct devlink_ops *ops = devlink->ops;
2604 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2605 pool_index, &threshold);
2609 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2613 if (devlink_nl_put_handle(msg, devlink))
2614 goto nla_put_failure;
2615 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2616 goto nla_put_failure;
2617 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2618 goto nla_put_failure;
2619 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2620 goto nla_put_failure;
2621 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2622 goto nla_put_failure;
2624 if (ops->sb_occ_port_pool_get) {
2628 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2629 pool_index, &cur, &max);
2630 if (err && err != -EOPNOTSUPP)
2631 goto sb_occ_get_failure;
2633 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2634 goto nla_put_failure;
2635 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2636 goto nla_put_failure;
2640 genlmsg_end(msg, hdr);
2646 genlmsg_cancel(msg, hdr);
2650 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2651 struct genl_info *info)
2653 struct devlink_port *devlink_port = info->user_ptr[1];
2654 struct devlink *devlink = devlink_port->devlink;
2655 struct devlink_sb *devlink_sb;
2656 struct sk_buff *msg;
2660 devlink_sb = devlink_sb_get_from_info(devlink, info);
2661 if (IS_ERR(devlink_sb))
2662 return PTR_ERR(devlink_sb);
2664 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2669 if (!devlink->ops->sb_port_pool_get)
2672 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2676 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2677 devlink_sb, pool_index,
2678 DEVLINK_CMD_SB_PORT_POOL_NEW,
2679 info->snd_portid, info->snd_seq, 0);
2685 return genlmsg_reply(msg, info);
2688 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2689 struct devlink *devlink,
2690 struct devlink_sb *devlink_sb,
2691 u32 portid, u32 seq)
2693 struct devlink_port *devlink_port;
2694 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2695 unsigned long port_index;
2699 xa_for_each(&devlink->ports, port_index, devlink_port) {
2700 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2701 if (*p_idx < start) {
2705 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2709 DEVLINK_CMD_SB_PORT_POOL_NEW,
2721 devlink_nl_cmd_sb_port_pool_get_dump_one(struct sk_buff *msg,
2722 struct devlink *devlink,
2723 struct netlink_callback *cb)
2725 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2726 struct devlink_sb *devlink_sb;
2730 if (!devlink->ops->sb_port_pool_get)
2733 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2734 err = __sb_port_pool_get_dumpit(msg, state->idx, &idx,
2735 devlink, devlink_sb,
2736 NETLINK_CB(cb->skb).portid,
2737 cb->nlh->nlmsg_seq);
2738 if (err == -EOPNOTSUPP) {
2749 const struct devlink_gen_cmd devl_gen_sb_port_pool = {
2750 .dump_one = devlink_nl_cmd_sb_port_pool_get_dump_one,
2753 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2754 unsigned int sb_index, u16 pool_index,
2756 struct netlink_ext_ack *extack)
2759 const struct devlink_ops *ops = devlink_port->devlink->ops;
2761 if (ops->sb_port_pool_set)
2762 return ops->sb_port_pool_set(devlink_port, sb_index,
2763 pool_index, threshold, extack);
2767 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2768 struct genl_info *info)
2770 struct devlink_port *devlink_port = info->user_ptr[1];
2771 struct devlink *devlink = info->user_ptr[0];
2772 struct devlink_sb *devlink_sb;
2777 devlink_sb = devlink_sb_get_from_info(devlink, info);
2778 if (IS_ERR(devlink_sb))
2779 return PTR_ERR(devlink_sb);
2781 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2786 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
2789 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2790 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2791 pool_index, threshold, info->extack);
2795 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2796 struct devlink_port *devlink_port,
2797 struct devlink_sb *devlink_sb, u16 tc_index,
2798 enum devlink_sb_pool_type pool_type,
2799 enum devlink_command cmd,
2800 u32 portid, u32 seq, int flags)
2802 const struct devlink_ops *ops = devlink->ops;
2808 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2809 tc_index, pool_type,
2810 &pool_index, &threshold);
2814 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2818 if (devlink_nl_put_handle(msg, devlink))
2819 goto nla_put_failure;
2820 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2821 goto nla_put_failure;
2822 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2823 goto nla_put_failure;
2824 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2825 goto nla_put_failure;
2826 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2827 goto nla_put_failure;
2828 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2829 goto nla_put_failure;
2830 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2831 goto nla_put_failure;
2833 if (ops->sb_occ_tc_port_bind_get) {
2837 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2839 tc_index, pool_type,
2841 if (err && err != -EOPNOTSUPP)
2844 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2845 goto nla_put_failure;
2846 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2847 goto nla_put_failure;
2851 genlmsg_end(msg, hdr);
2855 genlmsg_cancel(msg, hdr);
2859 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2860 struct genl_info *info)
2862 struct devlink_port *devlink_port = info->user_ptr[1];
2863 struct devlink *devlink = devlink_port->devlink;
2864 struct devlink_sb *devlink_sb;
2865 struct sk_buff *msg;
2866 enum devlink_sb_pool_type pool_type;
2870 devlink_sb = devlink_sb_get_from_info(devlink, info);
2871 if (IS_ERR(devlink_sb))
2872 return PTR_ERR(devlink_sb);
2874 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2878 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2879 pool_type, &tc_index);
2883 if (!devlink->ops->sb_tc_pool_bind_get)
2886 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2890 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2891 devlink_sb, tc_index, pool_type,
2892 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2900 return genlmsg_reply(msg, info);
2903 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2904 int start, int *p_idx,
2905 struct devlink *devlink,
2906 struct devlink_sb *devlink_sb,
2907 u32 portid, u32 seq)
2909 struct devlink_port *devlink_port;
2910 unsigned long port_index;
2914 xa_for_each(&devlink->ports, port_index, devlink_port) {
2916 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
2917 if (*p_idx < start) {
2921 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2925 DEVLINK_SB_POOL_TYPE_INGRESS,
2926 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2934 tc_index < devlink_sb->egress_tc_count; tc_index++) {
2935 if (*p_idx < start) {
2939 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2943 DEVLINK_SB_POOL_TYPE_EGRESS,
2944 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2956 devlink_nl_cmd_sb_tc_pool_bind_get_dump_one(struct sk_buff *msg,
2957 struct devlink *devlink,
2958 struct netlink_callback *cb)
2960 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2961 struct devlink_sb *devlink_sb;
2965 if (!devlink->ops->sb_tc_pool_bind_get)
2968 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2969 err = __sb_tc_pool_bind_get_dumpit(msg, state->idx, &idx,
2970 devlink, devlink_sb,
2971 NETLINK_CB(cb->skb).portid,
2972 cb->nlh->nlmsg_seq);
2973 if (err == -EOPNOTSUPP) {
2984 const struct devlink_gen_cmd devl_gen_sb_tc_pool_bind = {
2985 .dump_one = devlink_nl_cmd_sb_tc_pool_bind_get_dump_one,
2988 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
2989 unsigned int sb_index, u16 tc_index,
2990 enum devlink_sb_pool_type pool_type,
2991 u16 pool_index, u32 threshold,
2992 struct netlink_ext_ack *extack)
2995 const struct devlink_ops *ops = devlink_port->devlink->ops;
2997 if (ops->sb_tc_pool_bind_set)
2998 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
2999 tc_index, pool_type,
3000 pool_index, threshold, extack);
3004 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
3005 struct genl_info *info)
3007 struct devlink_port *devlink_port = info->user_ptr[1];
3008 struct devlink *devlink = info->user_ptr[0];
3009 enum devlink_sb_pool_type pool_type;
3010 struct devlink_sb *devlink_sb;
3016 devlink_sb = devlink_sb_get_from_info(devlink, info);
3017 if (IS_ERR(devlink_sb))
3018 return PTR_ERR(devlink_sb);
3020 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
3024 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
3025 pool_type, &tc_index);
3029 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
3034 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
3037 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
3038 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
3039 tc_index, pool_type,
3040 pool_index, threshold, info->extack);
3043 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
3044 struct genl_info *info)
3046 struct devlink *devlink = info->user_ptr[0];
3047 const struct devlink_ops *ops = devlink->ops;
3048 struct devlink_sb *devlink_sb;
3050 devlink_sb = devlink_sb_get_from_info(devlink, info);
3051 if (IS_ERR(devlink_sb))
3052 return PTR_ERR(devlink_sb);
3054 if (ops->sb_occ_snapshot)
3055 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
3059 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
3060 struct genl_info *info)
3062 struct devlink *devlink = info->user_ptr[0];
3063 const struct devlink_ops *ops = devlink->ops;
3064 struct devlink_sb *devlink_sb;
3066 devlink_sb = devlink_sb_get_from_info(devlink, info);
3067 if (IS_ERR(devlink_sb))
3068 return PTR_ERR(devlink_sb);
3070 if (ops->sb_occ_max_clear)
3071 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
3075 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
3076 enum devlink_command cmd, u32 portid,
3079 const struct devlink_ops *ops = devlink->ops;
3080 enum devlink_eswitch_encap_mode encap_mode;
3086 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3090 err = devlink_nl_put_handle(msg, devlink);
3092 goto nla_put_failure;
3094 if (ops->eswitch_mode_get) {
3095 err = ops->eswitch_mode_get(devlink, &mode);
3097 goto nla_put_failure;
3098 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
3100 goto nla_put_failure;
3103 if (ops->eswitch_inline_mode_get) {
3104 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
3106 goto nla_put_failure;
3107 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
3110 goto nla_put_failure;
3113 if (ops->eswitch_encap_mode_get) {
3114 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
3116 goto nla_put_failure;
3117 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
3119 goto nla_put_failure;
3122 genlmsg_end(msg, hdr);
3126 genlmsg_cancel(msg, hdr);
3130 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
3131 struct genl_info *info)
3133 struct devlink *devlink = info->user_ptr[0];
3134 struct sk_buff *msg;
3137 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3141 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
3142 info->snd_portid, info->snd_seq, 0);
3149 return genlmsg_reply(msg, info);
3152 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
3153 struct netlink_ext_ack *extack)
3155 struct devlink_rate *devlink_rate;
3157 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
3158 if (devlink_rate_is_node(devlink_rate)) {
3159 NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
3165 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
3166 struct genl_info *info)
3168 struct devlink *devlink = info->user_ptr[0];
3169 const struct devlink_ops *ops = devlink->ops;
3170 enum devlink_eswitch_encap_mode encap_mode;
3175 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
3176 if (!ops->eswitch_mode_set)
3178 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
3179 err = devlink_rate_nodes_check(devlink, mode, info->extack);
3182 err = ops->eswitch_mode_set(devlink, mode, info->extack);
3187 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
3188 if (!ops->eswitch_inline_mode_set)
3190 inline_mode = nla_get_u8(
3191 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
3192 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
3198 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
3199 if (!ops->eswitch_encap_mode_set)
3201 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
3202 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
3211 int devlink_dpipe_match_put(struct sk_buff *skb,
3212 struct devlink_dpipe_match *match)
3214 struct devlink_dpipe_header *header = match->header;
3215 struct devlink_dpipe_field *field = &header->fields[match->field_id];
3216 struct nlattr *match_attr;
3218 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
3222 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
3223 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
3224 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3225 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3226 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3227 goto nla_put_failure;
3229 nla_nest_end(skb, match_attr);
3233 nla_nest_cancel(skb, match_attr);
3236 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
3238 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
3239 struct sk_buff *skb)
3241 struct nlattr *matches_attr;
3243 matches_attr = nla_nest_start_noflag(skb,
3244 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
3248 if (table->table_ops->matches_dump(table->priv, skb))
3249 goto nla_put_failure;
3251 nla_nest_end(skb, matches_attr);
3255 nla_nest_cancel(skb, matches_attr);
3259 int devlink_dpipe_action_put(struct sk_buff *skb,
3260 struct devlink_dpipe_action *action)
3262 struct devlink_dpipe_header *header = action->header;
3263 struct devlink_dpipe_field *field = &header->fields[action->field_id];
3264 struct nlattr *action_attr;
3266 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
3270 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
3271 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
3272 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3273 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3274 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3275 goto nla_put_failure;
3277 nla_nest_end(skb, action_attr);
3281 nla_nest_cancel(skb, action_attr);
3284 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
3286 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
3287 struct sk_buff *skb)
3289 struct nlattr *actions_attr;
3291 actions_attr = nla_nest_start_noflag(skb,
3292 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
3296 if (table->table_ops->actions_dump(table->priv, skb))
3297 goto nla_put_failure;
3299 nla_nest_end(skb, actions_attr);
3303 nla_nest_cancel(skb, actions_attr);
3307 static int devlink_dpipe_table_put(struct sk_buff *skb,
3308 struct devlink_dpipe_table *table)
3310 struct nlattr *table_attr;
3313 table_size = table->table_ops->size_get(table->priv);
3314 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
3318 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
3319 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
3321 goto nla_put_failure;
3322 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
3323 table->counters_enabled))
3324 goto nla_put_failure;
3326 if (table->resource_valid) {
3327 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
3328 table->resource_id, DEVLINK_ATTR_PAD) ||
3329 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
3330 table->resource_units, DEVLINK_ATTR_PAD))
3331 goto nla_put_failure;
3333 if (devlink_dpipe_matches_put(table, skb))
3334 goto nla_put_failure;
3336 if (devlink_dpipe_actions_put(table, skb))
3337 goto nla_put_failure;
3339 nla_nest_end(skb, table_attr);
3343 nla_nest_cancel(skb, table_attr);
3347 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
3348 struct genl_info *info)
3353 err = genlmsg_reply(*pskb, info);
3357 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3363 static int devlink_dpipe_tables_fill(struct genl_info *info,
3364 enum devlink_command cmd, int flags,
3365 struct list_head *dpipe_tables,
3366 const char *table_name)
3368 struct devlink *devlink = info->user_ptr[0];
3369 struct devlink_dpipe_table *table;
3370 struct nlattr *tables_attr;
3371 struct sk_buff *skb = NULL;
3372 struct nlmsghdr *nlh;
3378 table = list_first_entry(dpipe_tables,
3379 struct devlink_dpipe_table, list);
3381 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3385 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3386 &devlink_nl_family, NLM_F_MULTI, cmd);
3392 if (devlink_nl_put_handle(skb, devlink))
3393 goto nla_put_failure;
3394 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3396 goto nla_put_failure;
3400 list_for_each_entry_from(table, dpipe_tables, list) {
3402 err = devlink_dpipe_table_put(skb, table);
3410 if (!strcmp(table->name, table_name)) {
3411 err = devlink_dpipe_table_put(skb, table);
3419 nla_nest_end(skb, tables_attr);
3420 genlmsg_end(skb, hdr);
3425 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3426 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3428 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3434 return genlmsg_reply(skb, info);
3443 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3444 struct genl_info *info)
3446 struct devlink *devlink = info->user_ptr[0];
3447 const char *table_name = NULL;
3449 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3450 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3452 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3453 &devlink->dpipe_table_list,
3457 static int devlink_dpipe_value_put(struct sk_buff *skb,
3458 struct devlink_dpipe_value *value)
3460 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3461 value->value_size, value->value))
3464 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3465 value->value_size, value->mask))
3467 if (value->mapping_valid)
3468 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3469 value->mapping_value))
3474 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3475 struct devlink_dpipe_value *value)
3479 if (devlink_dpipe_action_put(skb, value->action))
3481 if (devlink_dpipe_value_put(skb, value))
3486 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3487 struct devlink_dpipe_value *values,
3488 unsigned int values_count)
3490 struct nlattr *action_attr;
3494 for (i = 0; i < values_count; i++) {
3495 action_attr = nla_nest_start_noflag(skb,
3496 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3499 err = devlink_dpipe_action_value_put(skb, &values[i]);
3501 goto err_action_value_put;
3502 nla_nest_end(skb, action_attr);
3506 err_action_value_put:
3507 nla_nest_cancel(skb, action_attr);
3511 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3512 struct devlink_dpipe_value *value)
3516 if (devlink_dpipe_match_put(skb, value->match))
3518 if (devlink_dpipe_value_put(skb, value))
3523 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3524 struct devlink_dpipe_value *values,
3525 unsigned int values_count)
3527 struct nlattr *match_attr;
3531 for (i = 0; i < values_count; i++) {
3532 match_attr = nla_nest_start_noflag(skb,
3533 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3536 err = devlink_dpipe_match_value_put(skb, &values[i]);
3538 goto err_match_value_put;
3539 nla_nest_end(skb, match_attr);
3543 err_match_value_put:
3544 nla_nest_cancel(skb, match_attr);
3548 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3549 struct devlink_dpipe_entry *entry)
3551 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3554 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3558 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3560 goto nla_put_failure;
3561 if (entry->counter_valid)
3562 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3563 entry->counter, DEVLINK_ATTR_PAD))
3564 goto nla_put_failure;
3566 matches_attr = nla_nest_start_noflag(skb,
3567 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3569 goto nla_put_failure;
3571 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3572 entry->match_values_count);
3574 nla_nest_cancel(skb, matches_attr);
3575 goto err_match_values_put;
3577 nla_nest_end(skb, matches_attr);
3579 actions_attr = nla_nest_start_noflag(skb,
3580 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3582 goto nla_put_failure;
3584 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3585 entry->action_values_count);
3587 nla_nest_cancel(skb, actions_attr);
3588 goto err_action_values_put;
3590 nla_nest_end(skb, actions_attr);
3592 nla_nest_end(skb, entry_attr);
3597 err_match_values_put:
3598 err_action_values_put:
3599 nla_nest_cancel(skb, entry_attr);
3603 static struct devlink_dpipe_table *
3604 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3605 const char *table_name, struct devlink *devlink)
3607 struct devlink_dpipe_table *table;
3608 list_for_each_entry_rcu(table, dpipe_tables, list,
3609 lockdep_is_held(&devlink->lock)) {
3610 if (!strcmp(table->name, table_name))
3616 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3618 struct devlink *devlink;
3621 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3626 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3627 dump_ctx->info->snd_portid,
3628 dump_ctx->info->snd_seq,
3629 &devlink_nl_family, NLM_F_MULTI,
3632 goto nla_put_failure;
3634 devlink = dump_ctx->info->user_ptr[0];
3635 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3636 goto nla_put_failure;
3637 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3638 DEVLINK_ATTR_DPIPE_ENTRIES);
3639 if (!dump_ctx->nest)
3640 goto nla_put_failure;
3644 nlmsg_free(dump_ctx->skb);
3647 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3649 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3650 struct devlink_dpipe_entry *entry)
3652 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3654 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3656 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3658 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3659 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3662 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3664 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3667 unsigned int value_count, value_index;
3668 struct devlink_dpipe_value *value;
3670 value = entry->action_values;
3671 value_count = entry->action_values_count;
3672 for (value_index = 0; value_index < value_count; value_index++) {
3673 kfree(value[value_index].value);
3674 kfree(value[value_index].mask);
3677 value = entry->match_values;
3678 value_count = entry->match_values_count;
3679 for (value_index = 0; value_index < value_count; value_index++) {
3680 kfree(value[value_index].value);
3681 kfree(value[value_index].mask);
3684 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3686 static int devlink_dpipe_entries_fill(struct genl_info *info,
3687 enum devlink_command cmd, int flags,
3688 struct devlink_dpipe_table *table)
3690 struct devlink_dpipe_dump_ctx dump_ctx;
3691 struct nlmsghdr *nlh;
3694 dump_ctx.skb = NULL;
3696 dump_ctx.info = info;
3698 err = table->table_ops->entries_dump(table->priv,
3699 table->counters_enabled,
3705 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3706 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3708 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3713 return genlmsg_reply(dump_ctx.skb, info);
3716 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3717 struct genl_info *info)
3719 struct devlink *devlink = info->user_ptr[0];
3720 struct devlink_dpipe_table *table;
3721 const char *table_name;
3723 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
3726 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3727 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3728 table_name, devlink);
3732 if (!table->table_ops->entries_dump)
3735 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3739 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3740 const struct devlink_dpipe_header *header)
3742 struct devlink_dpipe_field *field;
3743 struct nlattr *field_attr;
3746 for (i = 0; i < header->fields_count; i++) {
3747 field = &header->fields[i];
3748 field_attr = nla_nest_start_noflag(skb,
3749 DEVLINK_ATTR_DPIPE_FIELD);
3752 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3753 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3754 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3755 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3756 goto nla_put_failure;
3757 nla_nest_end(skb, field_attr);
3762 nla_nest_cancel(skb, field_attr);
3766 static int devlink_dpipe_header_put(struct sk_buff *skb,
3767 struct devlink_dpipe_header *header)
3769 struct nlattr *fields_attr, *header_attr;
3772 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3776 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3777 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3778 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3779 goto nla_put_failure;
3781 fields_attr = nla_nest_start_noflag(skb,
3782 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3784 goto nla_put_failure;
3786 err = devlink_dpipe_fields_put(skb, header);
3788 nla_nest_cancel(skb, fields_attr);
3789 goto nla_put_failure;
3791 nla_nest_end(skb, fields_attr);
3792 nla_nest_end(skb, header_attr);
3797 nla_nest_cancel(skb, header_attr);
3801 static int devlink_dpipe_headers_fill(struct genl_info *info,
3802 enum devlink_command cmd, int flags,
3803 struct devlink_dpipe_headers *
3806 struct devlink *devlink = info->user_ptr[0];
3807 struct nlattr *headers_attr;
3808 struct sk_buff *skb = NULL;
3809 struct nlmsghdr *nlh;
3816 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3820 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3821 &devlink_nl_family, NLM_F_MULTI, cmd);
3827 if (devlink_nl_put_handle(skb, devlink))
3828 goto nla_put_failure;
3829 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3831 goto nla_put_failure;
3834 for (; i < dpipe_headers->headers_count; i++) {
3835 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3843 nla_nest_end(skb, headers_attr);
3844 genlmsg_end(skb, hdr);
3845 if (i != dpipe_headers->headers_count)
3849 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3850 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3852 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3857 return genlmsg_reply(skb, info);
3866 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3867 struct genl_info *info)
3869 struct devlink *devlink = info->user_ptr[0];
3871 if (!devlink->dpipe_headers)
3873 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3874 0, devlink->dpipe_headers);
3877 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3878 const char *table_name,
3881 struct devlink_dpipe_table *table;
3883 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3884 table_name, devlink);
3888 if (table->counter_control_extern)
3891 if (!(table->counters_enabled ^ enable))
3894 table->counters_enabled = enable;
3895 if (table->table_ops->counters_set_update)
3896 table->table_ops->counters_set_update(table->priv, enable);
3900 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
3901 struct genl_info *info)
3903 struct devlink *devlink = info->user_ptr[0];
3904 const char *table_name;
3905 bool counters_enable;
3907 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
3908 GENL_REQ_ATTR_CHECK(info,
3909 DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
3912 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3913 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
3915 return devlink_dpipe_table_counters_set(devlink, table_name,
3919 static struct devlink_resource *
3920 devlink_resource_find(struct devlink *devlink,
3921 struct devlink_resource *resource, u64 resource_id)
3923 struct list_head *resource_list;
3926 resource_list = &resource->resource_list;
3928 resource_list = &devlink->resource_list;
3930 list_for_each_entry(resource, resource_list, list) {
3931 struct devlink_resource *child_resource;
3933 if (resource->id == resource_id)
3936 child_resource = devlink_resource_find(devlink, resource,
3939 return child_resource;
3945 devlink_resource_validate_children(struct devlink_resource *resource)
3947 struct devlink_resource *child_resource;
3948 bool size_valid = true;
3951 if (list_empty(&resource->resource_list))
3954 list_for_each_entry(child_resource, &resource->resource_list, list)
3955 parts_size += child_resource->size_new;
3957 if (parts_size > resource->size_new)
3960 resource->size_valid = size_valid;
3964 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
3965 struct netlink_ext_ack *extack)
3970 if (size > resource->size_params.size_max) {
3971 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
3975 if (size < resource->size_params.size_min) {
3976 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
3980 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
3982 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
3989 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
3990 struct genl_info *info)
3992 struct devlink *devlink = info->user_ptr[0];
3993 struct devlink_resource *resource;
3998 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_ID) ||
3999 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_SIZE))
4001 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
4003 resource = devlink_resource_find(devlink, NULL, resource_id);
4007 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
4008 err = devlink_resource_validate_size(resource, size, info->extack);
4012 resource->size_new = size;
4013 devlink_resource_validate_children(resource);
4014 if (resource->parent)
4015 devlink_resource_validate_children(resource->parent);
4020 devlink_resource_size_params_put(struct devlink_resource *resource,
4021 struct sk_buff *skb)
4023 struct devlink_resource_size_params *size_params;
4025 size_params = &resource->size_params;
4026 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
4027 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
4028 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
4029 size_params->size_max, DEVLINK_ATTR_PAD) ||
4030 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
4031 size_params->size_min, DEVLINK_ATTR_PAD) ||
4032 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
4037 static int devlink_resource_occ_put(struct devlink_resource *resource,
4038 struct sk_buff *skb)
4040 if (!resource->occ_get)
4042 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
4043 resource->occ_get(resource->occ_get_priv),
4047 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
4048 struct devlink_resource *resource)
4050 struct devlink_resource *child_resource;
4051 struct nlattr *child_resource_attr;
4052 struct nlattr *resource_attr;
4054 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
4058 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
4059 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
4060 DEVLINK_ATTR_PAD) ||
4061 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
4063 goto nla_put_failure;
4064 if (resource->size != resource->size_new &&
4065 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
4066 resource->size_new, DEVLINK_ATTR_PAD))
4067 goto nla_put_failure;
4068 if (devlink_resource_occ_put(resource, skb))
4069 goto nla_put_failure;
4070 if (devlink_resource_size_params_put(resource, skb))
4071 goto nla_put_failure;
4072 if (list_empty(&resource->resource_list))
4075 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
4076 resource->size_valid))
4077 goto nla_put_failure;
4079 child_resource_attr = nla_nest_start_noflag(skb,
4080 DEVLINK_ATTR_RESOURCE_LIST);
4081 if (!child_resource_attr)
4082 goto nla_put_failure;
4084 list_for_each_entry(child_resource, &resource->resource_list, list) {
4085 if (devlink_resource_put(devlink, skb, child_resource))
4086 goto resource_put_failure;
4089 nla_nest_end(skb, child_resource_attr);
4091 nla_nest_end(skb, resource_attr);
4094 resource_put_failure:
4095 nla_nest_cancel(skb, child_resource_attr);
4097 nla_nest_cancel(skb, resource_attr);
4101 static int devlink_resource_fill(struct genl_info *info,
4102 enum devlink_command cmd, int flags)
4104 struct devlink *devlink = info->user_ptr[0];
4105 struct devlink_resource *resource;
4106 struct nlattr *resources_attr;
4107 struct sk_buff *skb = NULL;
4108 struct nlmsghdr *nlh;
4114 resource = list_first_entry(&devlink->resource_list,
4115 struct devlink_resource, list);
4117 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4121 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4122 &devlink_nl_family, NLM_F_MULTI, cmd);
4128 if (devlink_nl_put_handle(skb, devlink))
4129 goto nla_put_failure;
4131 resources_attr = nla_nest_start_noflag(skb,
4132 DEVLINK_ATTR_RESOURCE_LIST);
4133 if (!resources_attr)
4134 goto nla_put_failure;
4138 list_for_each_entry_from(resource, &devlink->resource_list, list) {
4139 err = devlink_resource_put(devlink, skb, resource);
4142 goto err_resource_put;
4148 nla_nest_end(skb, resources_attr);
4149 genlmsg_end(skb, hdr);
4153 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4154 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4156 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4161 return genlmsg_reply(skb, info);
4170 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
4171 struct genl_info *info)
4173 struct devlink *devlink = info->user_ptr[0];
4175 if (list_empty(&devlink->resource_list))
4178 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
4182 devlink_resources_validate(struct devlink *devlink,
4183 struct devlink_resource *resource,
4184 struct genl_info *info)
4186 struct list_head *resource_list;
4190 resource_list = &resource->resource_list;
4192 resource_list = &devlink->resource_list;
4194 list_for_each_entry(resource, resource_list, list) {
4195 if (!resource->size_valid)
4197 err = devlink_resources_validate(devlink, resource, info);
4204 static struct net *devlink_netns_get(struct sk_buff *skb,
4205 struct genl_info *info)
4207 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
4208 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
4209 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
4212 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
4213 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
4214 return ERR_PTR(-EINVAL);
4217 if (netns_pid_attr) {
4218 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
4219 } else if (netns_fd_attr) {
4220 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
4221 } else if (netns_id_attr) {
4222 net = get_net_ns_by_id(sock_net(skb->sk),
4223 nla_get_u32(netns_id_attr));
4225 net = ERR_PTR(-EINVAL);
4228 net = ERR_PTR(-EINVAL);
4231 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
4232 return ERR_PTR(-EINVAL);
4234 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
4236 return ERR_PTR(-EPERM);
4241 static void devlink_param_notify(struct devlink *devlink,
4242 unsigned int port_index,
4243 struct devlink_param_item *param_item,
4244 enum devlink_command cmd);
4246 static void devlink_ns_change_notify(struct devlink *devlink,
4247 struct net *dest_net, struct net *curr_net,
4250 struct devlink_param_item *param_item;
4251 enum devlink_command cmd;
4253 /* Userspace needs to be notified about devlink objects
4254 * removed from original and entering new network namespace.
4255 * The rest of the devlink objects are re-created during
4256 * reload process so the notifications are generated separatelly.
4259 if (!dest_net || net_eq(dest_net, curr_net))
4263 devlink_notify(devlink, DEVLINK_CMD_NEW);
4265 cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
4266 list_for_each_entry(param_item, &devlink->param_list, list)
4267 devlink_param_notify(devlink, 0, param_item, cmd);
4270 devlink_notify(devlink, DEVLINK_CMD_DEL);
4273 static void devlink_reload_failed_set(struct devlink *devlink,
4276 if (devlink->reload_failed == reload_failed)
4278 devlink->reload_failed = reload_failed;
4279 devlink_notify(devlink, DEVLINK_CMD_NEW);
4282 bool devlink_is_reload_failed(const struct devlink *devlink)
4284 return devlink->reload_failed;
4286 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
4289 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
4290 enum devlink_reload_limit limit, u32 actions_performed)
4292 unsigned long actions = actions_performed;
4296 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
4297 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
4298 reload_stats[stat_idx]++;
4300 devlink_notify(devlink, DEVLINK_CMD_NEW);
4304 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
4305 u32 actions_performed)
4307 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
4312 * devlink_remote_reload_actions_performed - Update devlink on reload actions
4313 * performed which are not a direct result of devlink reload call.
4315 * This should be called by a driver after performing reload actions in case it was not
4316 * a result of devlink reload call. For example fw_activate was performed as a result
4317 * of devlink reload triggered fw_activate on another host.
4318 * The motivation for this function is to keep data on reload actions performed on this
4319 * function whether it was done due to direct devlink reload call or not.
4322 * @limit: reload limit
4323 * @actions_performed: bitmask of actions performed
4325 void devlink_remote_reload_actions_performed(struct devlink *devlink,
4326 enum devlink_reload_limit limit,
4327 u32 actions_performed)
4329 if (WARN_ON(!actions_performed ||
4330 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
4331 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
4332 limit > DEVLINK_RELOAD_LIMIT_MAX))
4335 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
4338 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
4340 int devlink_reload(struct devlink *devlink, struct net *dest_net,
4341 enum devlink_reload_action action,
4342 enum devlink_reload_limit limit,
4343 u32 *actions_performed, struct netlink_ext_ack *extack)
4345 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
4346 struct net *curr_net;
4349 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
4350 sizeof(remote_reload_stats));
4352 curr_net = devlink_net(devlink);
4353 devlink_ns_change_notify(devlink, dest_net, curr_net, false);
4354 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
4358 if (dest_net && !net_eq(dest_net, curr_net)) {
4359 move_netdevice_notifier_net(curr_net, dest_net,
4360 &devlink->netdevice_nb);
4361 write_pnet(&devlink->_net, dest_net);
4364 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
4365 devlink_reload_failed_set(devlink, !!err);
4369 devlink_ns_change_notify(devlink, dest_net, curr_net, true);
4370 WARN_ON(!(*actions_performed & BIT(action)));
4371 /* Catch driver on updating the remote action within devlink reload */
4372 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
4373 sizeof(remote_reload_stats)));
4374 devlink_reload_stats_update(devlink, limit, *actions_performed);
4379 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
4380 enum devlink_command cmd, struct genl_info *info)
4382 struct sk_buff *msg;
4385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4389 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
4393 if (devlink_nl_put_handle(msg, devlink))
4394 goto nla_put_failure;
4396 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4398 goto nla_put_failure;
4399 genlmsg_end(msg, hdr);
4401 return genlmsg_reply(msg, info);
4404 genlmsg_cancel(msg, hdr);
4410 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4412 struct devlink *devlink = info->user_ptr[0];
4413 enum devlink_reload_action action;
4414 enum devlink_reload_limit limit;
4415 struct net *dest_net = NULL;
4416 u32 actions_performed;
4419 if (!(devlink->features & DEVLINK_F_RELOAD))
4422 err = devlink_resources_validate(devlink, NULL, info);
4424 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4428 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4429 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4431 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4433 if (!devlink_reload_action_is_supported(devlink, action)) {
4434 NL_SET_ERR_MSG_MOD(info->extack,
4435 "Requested reload action is not supported by the driver");
4439 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4440 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4441 struct nla_bitfield32 limits;
4442 u32 limits_selected;
4444 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4445 limits_selected = limits.value & limits.selector;
4446 if (!limits_selected) {
4447 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4450 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4451 if (limits_selected & BIT(limit))
4453 /* UAPI enables multiselection, but currently it is not used */
4454 if (limits_selected != BIT(limit)) {
4455 NL_SET_ERR_MSG_MOD(info->extack,
4456 "Multiselection of limit is not supported");
4459 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4460 NL_SET_ERR_MSG_MOD(info->extack,
4461 "Requested limit is not supported by the driver");
4464 if (devlink_reload_combination_is_invalid(action, limit)) {
4465 NL_SET_ERR_MSG_MOD(info->extack,
4466 "Requested limit is invalid for this action");
4470 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4471 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4472 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4473 dest_net = devlink_netns_get(skb, info);
4474 if (IS_ERR(dest_net))
4475 return PTR_ERR(dest_net);
4478 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4485 /* For backward compatibility generate reply only if attributes used by user */
4486 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4489 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4490 DEVLINK_CMD_RELOAD, info);
4493 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4494 struct devlink *devlink,
4495 enum devlink_command cmd,
4496 struct devlink_flash_notify *params)
4500 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4504 if (devlink_nl_put_handle(msg, devlink))
4505 goto nla_put_failure;
4507 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4510 if (params->status_msg &&
4511 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4512 params->status_msg))
4513 goto nla_put_failure;
4514 if (params->component &&
4515 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4517 goto nla_put_failure;
4518 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4519 params->done, DEVLINK_ATTR_PAD))
4520 goto nla_put_failure;
4521 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4522 params->total, DEVLINK_ATTR_PAD))
4523 goto nla_put_failure;
4524 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4525 params->timeout, DEVLINK_ATTR_PAD))
4526 goto nla_put_failure;
4529 genlmsg_end(msg, hdr);
4533 genlmsg_cancel(msg, hdr);
4537 static void __devlink_flash_update_notify(struct devlink *devlink,
4538 enum devlink_command cmd,
4539 struct devlink_flash_notify *params)
4541 struct sk_buff *msg;
4544 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4545 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4546 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4548 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4551 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4555 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4559 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4560 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4567 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4569 struct devlink_flash_notify params = {};
4571 __devlink_flash_update_notify(devlink,
4572 DEVLINK_CMD_FLASH_UPDATE,
4576 static void devlink_flash_update_end_notify(struct devlink *devlink)
4578 struct devlink_flash_notify params = {};
4580 __devlink_flash_update_notify(devlink,
4581 DEVLINK_CMD_FLASH_UPDATE_END,
4585 void devlink_flash_update_status_notify(struct devlink *devlink,
4586 const char *status_msg,
4587 const char *component,
4589 unsigned long total)
4591 struct devlink_flash_notify params = {
4592 .status_msg = status_msg,
4593 .component = component,
4598 __devlink_flash_update_notify(devlink,
4599 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4602 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4604 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4605 const char *status_msg,
4606 const char *component,
4607 unsigned long timeout)
4609 struct devlink_flash_notify params = {
4610 .status_msg = status_msg,
4611 .component = component,
4615 __devlink_flash_update_notify(devlink,
4616 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4619 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4621 struct devlink_info_req {
4622 struct sk_buff *msg;
4623 void (*version_cb)(const char *version_name,
4624 enum devlink_info_version_type version_type,
4625 void *version_cb_priv);
4626 void *version_cb_priv;
4629 struct devlink_flash_component_lookup_ctx {
4630 const char *lookup_name;
4631 bool lookup_name_found;
4635 devlink_flash_component_lookup_cb(const char *version_name,
4636 enum devlink_info_version_type version_type,
4637 void *version_cb_priv)
4639 struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
4641 if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
4642 lookup_ctx->lookup_name_found)
4645 lookup_ctx->lookup_name_found =
4646 !strcmp(lookup_ctx->lookup_name, version_name);
4649 static int devlink_flash_component_get(struct devlink *devlink,
4650 struct nlattr *nla_component,
4651 const char **p_component,
4652 struct netlink_ext_ack *extack)
4654 struct devlink_flash_component_lookup_ctx lookup_ctx = {};
4655 struct devlink_info_req req = {};
4656 const char *component;
4662 component = nla_data(nla_component);
4664 if (!devlink->ops->info_get) {
4665 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4666 "component update is not supported by this device");
4670 lookup_ctx.lookup_name = component;
4671 req.version_cb = devlink_flash_component_lookup_cb;
4672 req.version_cb_priv = &lookup_ctx;
4674 ret = devlink->ops->info_get(devlink, &req, NULL);
4678 if (!lookup_ctx.lookup_name_found) {
4679 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4680 "selected component is not supported by this device");
4683 *p_component = component;
4687 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4688 struct genl_info *info)
4690 struct nlattr *nla_overwrite_mask, *nla_file_name;
4691 struct devlink_flash_update_params params = {};
4692 struct devlink *devlink = info->user_ptr[0];
4693 const char *file_name;
4694 u32 supported_params;
4697 if (!devlink->ops->flash_update)
4700 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
4703 ret = devlink_flash_component_get(devlink,
4704 info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
4705 ¶ms.component, info->extack);
4709 supported_params = devlink->ops->supported_flash_update_params;
4711 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4712 if (nla_overwrite_mask) {
4713 struct nla_bitfield32 sections;
4715 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4716 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4717 "overwrite settings are not supported by this device");
4720 sections = nla_get_bitfield32(nla_overwrite_mask);
4721 params.overwrite_mask = sections.value & sections.selector;
4724 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4725 file_name = nla_data(nla_file_name);
4726 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
4728 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4732 devlink_flash_update_begin_notify(devlink);
4733 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
4734 devlink_flash_update_end_notify(devlink);
4736 release_firmware(params.fw);
4742 devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
4743 u32 portid, u32 seq, int flags,
4744 struct netlink_ext_ack *extack)
4746 struct nlattr *selftests;
4751 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
4752 DEVLINK_CMD_SELFTESTS_GET);
4757 if (devlink_nl_put_handle(msg, devlink))
4758 goto err_cancel_msg;
4760 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4762 goto err_cancel_msg;
4764 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4765 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4766 if (devlink->ops->selftest_check(devlink, i, extack)) {
4767 err = nla_put_flag(msg, i);
4769 goto err_cancel_msg;
4773 nla_nest_end(msg, selftests);
4774 genlmsg_end(msg, hdr);
4778 genlmsg_cancel(msg, hdr);
4782 static int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb,
4783 struct genl_info *info)
4785 struct devlink *devlink = info->user_ptr[0];
4786 struct sk_buff *msg;
4789 if (!devlink->ops->selftest_check)
4792 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4796 err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
4797 info->snd_seq, 0, info->extack);
4803 return genlmsg_reply(msg, info);
4807 devlink_nl_cmd_selftests_get_dump_one(struct sk_buff *msg,
4808 struct devlink *devlink,
4809 struct netlink_callback *cb)
4811 if (!devlink->ops->selftest_check)
4814 return devlink_nl_selftests_fill(msg, devlink,
4815 NETLINK_CB(cb->skb).portid,
4816 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4820 const struct devlink_gen_cmd devl_gen_selftests = {
4821 .dump_one = devlink_nl_cmd_selftests_get_dump_one,
4824 static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
4825 enum devlink_selftest_status test_status)
4827 struct nlattr *result_attr;
4829 result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
4833 if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
4834 nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
4836 goto nla_put_failure;
4838 nla_nest_end(skb, result_attr);
4842 nla_nest_cancel(skb, result_attr);
4846 static int devlink_nl_cmd_selftests_run(struct sk_buff *skb,
4847 struct genl_info *info)
4849 struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
4850 struct devlink *devlink = info->user_ptr[0];
4851 struct nlattr *attrs, *selftests;
4852 struct sk_buff *msg;
4857 if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
4860 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
4863 attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
4865 err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
4866 devlink_selftest_nl_policy, info->extack);
4870 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4875 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
4876 &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
4880 if (devlink_nl_put_handle(msg, devlink))
4881 goto genlmsg_cancel;
4883 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4885 goto genlmsg_cancel;
4887 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4888 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4889 enum devlink_selftest_status test_status;
4891 if (nla_get_flag(tb[i])) {
4892 if (!devlink->ops->selftest_check(devlink, i,
4894 if (devlink_selftest_result_put(msg, i,
4895 DEVLINK_SELFTEST_STATUS_SKIP))
4896 goto selftests_nest_cancel;
4900 test_status = devlink->ops->selftest_run(devlink, i,
4902 if (devlink_selftest_result_put(msg, i, test_status))
4903 goto selftests_nest_cancel;
4907 nla_nest_end(msg, selftests);
4908 genlmsg_end(msg, hdr);
4909 return genlmsg_reply(msg, info);
4911 selftests_nest_cancel:
4912 nla_nest_cancel(msg, selftests);
4914 genlmsg_cancel(msg, hdr);
4920 static const struct devlink_param devlink_param_generic[] = {
4922 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
4923 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
4924 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
4927 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
4928 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
4929 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
4932 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
4933 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
4934 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
4937 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
4938 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
4939 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
4942 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
4943 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
4944 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
4947 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
4948 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
4949 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
4952 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
4953 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
4954 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
4957 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
4958 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
4959 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
4962 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
4963 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
4964 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
4967 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
4968 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
4969 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
4972 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
4973 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
4974 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
4977 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
4978 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
4979 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
4982 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
4983 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
4984 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
4987 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
4988 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
4989 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
4992 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
4993 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
4994 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
4997 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
4998 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
4999 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
5002 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
5003 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
5004 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
5008 static int devlink_param_generic_verify(const struct devlink_param *param)
5010 /* verify it match generic parameter by id and name */
5011 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
5013 if (strcmp(param->name, devlink_param_generic[param->id].name))
5016 WARN_ON(param->type != devlink_param_generic[param->id].type);
5021 static int devlink_param_driver_verify(const struct devlink_param *param)
5025 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
5027 /* verify no such name in generic params */
5028 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
5029 if (!strcmp(param->name, devlink_param_generic[i].name))
5035 static struct devlink_param_item *
5036 devlink_param_find_by_name(struct list_head *param_list,
5037 const char *param_name)
5039 struct devlink_param_item *param_item;
5041 list_for_each_entry(param_item, param_list, list)
5042 if (!strcmp(param_item->param->name, param_name))
5047 static struct devlink_param_item *
5048 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
5050 struct devlink_param_item *param_item;
5052 list_for_each_entry(param_item, param_list, list)
5053 if (param_item->param->id == param_id)
5059 devlink_param_cmode_is_supported(const struct devlink_param *param,
5060 enum devlink_param_cmode cmode)
5062 return test_bit(cmode, ¶m->supported_cmodes);
5065 static int devlink_param_get(struct devlink *devlink,
5066 const struct devlink_param *param,
5067 struct devlink_param_gset_ctx *ctx)
5069 if (!param->get || devlink->reload_failed)
5071 return param->get(devlink, param->id, ctx);
5074 static int devlink_param_set(struct devlink *devlink,
5075 const struct devlink_param *param,
5076 struct devlink_param_gset_ctx *ctx)
5078 if (!param->set || devlink->reload_failed)
5080 return param->set(devlink, param->id, ctx);
5084 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
5086 switch (param_type) {
5087 case DEVLINK_PARAM_TYPE_U8:
5089 case DEVLINK_PARAM_TYPE_U16:
5091 case DEVLINK_PARAM_TYPE_U32:
5093 case DEVLINK_PARAM_TYPE_STRING:
5095 case DEVLINK_PARAM_TYPE_BOOL:
5103 devlink_nl_param_value_fill_one(struct sk_buff *msg,
5104 enum devlink_param_type type,
5105 enum devlink_param_cmode cmode,
5106 union devlink_param_value val)
5108 struct nlattr *param_value_attr;
5110 param_value_attr = nla_nest_start_noflag(msg,
5111 DEVLINK_ATTR_PARAM_VALUE);
5112 if (!param_value_attr)
5113 goto nla_put_failure;
5115 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
5116 goto value_nest_cancel;
5119 case DEVLINK_PARAM_TYPE_U8:
5120 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
5121 goto value_nest_cancel;
5123 case DEVLINK_PARAM_TYPE_U16:
5124 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
5125 goto value_nest_cancel;
5127 case DEVLINK_PARAM_TYPE_U32:
5128 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
5129 goto value_nest_cancel;
5131 case DEVLINK_PARAM_TYPE_STRING:
5132 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
5134 goto value_nest_cancel;
5136 case DEVLINK_PARAM_TYPE_BOOL:
5138 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
5139 goto value_nest_cancel;
5143 nla_nest_end(msg, param_value_attr);
5147 nla_nest_cancel(msg, param_value_attr);
5152 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
5153 unsigned int port_index,
5154 struct devlink_param_item *param_item,
5155 enum devlink_command cmd,
5156 u32 portid, u32 seq, int flags)
5158 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
5159 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
5160 const struct devlink_param *param = param_item->param;
5161 struct devlink_param_gset_ctx ctx;
5162 struct nlattr *param_values_list;
5163 struct nlattr *param_attr;
5169 /* Get value from driver part to driverinit configuration mode */
5170 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5171 if (!devlink_param_cmode_is_supported(param, i))
5173 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5174 if (!param_item->driverinit_value_valid)
5176 param_value[i] = param_item->driverinit_value;
5179 err = devlink_param_get(devlink, param, &ctx);
5182 param_value[i] = ctx.val;
5184 param_value_set[i] = true;
5187 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5191 if (devlink_nl_put_handle(msg, devlink))
5192 goto genlmsg_cancel;
5194 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
5195 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
5196 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
5197 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
5198 goto genlmsg_cancel;
5200 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
5202 goto genlmsg_cancel;
5203 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
5204 goto param_nest_cancel;
5205 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
5206 goto param_nest_cancel;
5208 nla_type = devlink_param_type_to_nla_type(param->type);
5210 goto param_nest_cancel;
5211 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
5212 goto param_nest_cancel;
5214 param_values_list = nla_nest_start_noflag(msg,
5215 DEVLINK_ATTR_PARAM_VALUES_LIST);
5216 if (!param_values_list)
5217 goto param_nest_cancel;
5219 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5220 if (!param_value_set[i])
5222 err = devlink_nl_param_value_fill_one(msg, param->type,
5225 goto values_list_nest_cancel;
5228 nla_nest_end(msg, param_values_list);
5229 nla_nest_end(msg, param_attr);
5230 genlmsg_end(msg, hdr);
5233 values_list_nest_cancel:
5234 nla_nest_end(msg, param_values_list);
5236 nla_nest_cancel(msg, param_attr);
5238 genlmsg_cancel(msg, hdr);
5242 static void devlink_param_notify(struct devlink *devlink,
5243 unsigned int port_index,
5244 struct devlink_param_item *param_item,
5245 enum devlink_command cmd)
5247 struct sk_buff *msg;
5250 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
5251 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
5252 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
5254 /* devlink_notify_register() / devlink_notify_unregister()
5255 * will replay the notifications if the params are added/removed
5256 * outside of the lifetime of the instance.
5258 if (!devl_is_registered(devlink))
5261 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5264 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
5271 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
5272 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5276 devlink_nl_cmd_param_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
5277 struct netlink_callback *cb)
5279 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5280 struct devlink_param_item *param_item;
5284 list_for_each_entry(param_item, &devlink->param_list, list) {
5285 if (idx < state->idx) {
5289 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5290 DEVLINK_CMD_PARAM_GET,
5291 NETLINK_CB(cb->skb).portid,
5294 if (err == -EOPNOTSUPP) {
5306 const struct devlink_gen_cmd devl_gen_param = {
5307 .dump_one = devlink_nl_cmd_param_get_dump_one,
5311 devlink_param_type_get_from_info(struct genl_info *info,
5312 enum devlink_param_type *param_type)
5314 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
5317 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
5319 *param_type = DEVLINK_PARAM_TYPE_U8;
5322 *param_type = DEVLINK_PARAM_TYPE_U16;
5325 *param_type = DEVLINK_PARAM_TYPE_U32;
5328 *param_type = DEVLINK_PARAM_TYPE_STRING;
5331 *param_type = DEVLINK_PARAM_TYPE_BOOL;
5341 devlink_param_value_get_from_info(const struct devlink_param *param,
5342 struct genl_info *info,
5343 union devlink_param_value *value)
5345 struct nlattr *param_data;
5348 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
5350 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
5353 switch (param->type) {
5354 case DEVLINK_PARAM_TYPE_U8:
5355 if (nla_len(param_data) != sizeof(u8))
5357 value->vu8 = nla_get_u8(param_data);
5359 case DEVLINK_PARAM_TYPE_U16:
5360 if (nla_len(param_data) != sizeof(u16))
5362 value->vu16 = nla_get_u16(param_data);
5364 case DEVLINK_PARAM_TYPE_U32:
5365 if (nla_len(param_data) != sizeof(u32))
5367 value->vu32 = nla_get_u32(param_data);
5369 case DEVLINK_PARAM_TYPE_STRING:
5370 len = strnlen(nla_data(param_data), nla_len(param_data));
5371 if (len == nla_len(param_data) ||
5372 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
5374 strcpy(value->vstr, nla_data(param_data));
5376 case DEVLINK_PARAM_TYPE_BOOL:
5377 if (param_data && nla_len(param_data))
5379 value->vbool = nla_get_flag(param_data);
5385 static struct devlink_param_item *
5386 devlink_param_get_from_info(struct list_head *param_list,
5387 struct genl_info *info)
5391 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_NAME))
5394 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
5395 return devlink_param_find_by_name(param_list, param_name);
5398 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
5399 struct genl_info *info)
5401 struct devlink *devlink = info->user_ptr[0];
5402 struct devlink_param_item *param_item;
5403 struct sk_buff *msg;
5406 param_item = devlink_param_get_from_info(&devlink->param_list, info);
5410 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5414 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5415 DEVLINK_CMD_PARAM_GET,
5416 info->snd_portid, info->snd_seq, 0);
5422 return genlmsg_reply(msg, info);
5425 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
5426 unsigned int port_index,
5427 struct list_head *param_list,
5428 struct genl_info *info,
5429 enum devlink_command cmd)
5431 enum devlink_param_type param_type;
5432 struct devlink_param_gset_ctx ctx;
5433 enum devlink_param_cmode cmode;
5434 struct devlink_param_item *param_item;
5435 const struct devlink_param *param;
5436 union devlink_param_value value;
5439 param_item = devlink_param_get_from_info(param_list, info);
5442 param = param_item->param;
5443 err = devlink_param_type_get_from_info(info, ¶m_type);
5446 if (param_type != param->type)
5448 err = devlink_param_value_get_from_info(param, info, &value);
5451 if (param->validate) {
5452 err = param->validate(devlink, param->id, value, info->extack);
5457 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_CMODE))
5459 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
5460 if (!devlink_param_cmode_is_supported(param, cmode))
5463 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5464 if (param->type == DEVLINK_PARAM_TYPE_STRING)
5465 strcpy(param_item->driverinit_value.vstr, value.vstr);
5467 param_item->driverinit_value = value;
5468 param_item->driverinit_value_valid = true;
5474 err = devlink_param_set(devlink, param, &ctx);
5479 devlink_param_notify(devlink, port_index, param_item, cmd);
5483 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
5484 struct genl_info *info)
5486 struct devlink *devlink = info->user_ptr[0];
5488 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
5489 info, DEVLINK_CMD_PARAM_NEW);
5492 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
5493 struct netlink_callback *cb)
5495 NL_SET_ERR_MSG_MOD(cb->extack, "Port params are not supported");
5499 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
5500 struct genl_info *info)
5502 NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5506 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
5507 struct genl_info *info)
5509 NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5513 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
5514 struct devlink *devlink,
5515 struct devlink_snapshot *snapshot)
5517 struct nlattr *snap_attr;
5520 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5524 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5526 goto nla_put_failure;
5528 nla_nest_end(msg, snap_attr);
5532 nla_nest_cancel(msg, snap_attr);
5536 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5537 struct devlink *devlink,
5538 struct devlink_region *region)
5540 struct devlink_snapshot *snapshot;
5541 struct nlattr *snapshots_attr;
5544 snapshots_attr = nla_nest_start_noflag(msg,
5545 DEVLINK_ATTR_REGION_SNAPSHOTS);
5546 if (!snapshots_attr)
5549 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
5550 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5552 goto nla_put_failure;
5555 nla_nest_end(msg, snapshots_attr);
5559 nla_nest_cancel(msg, snapshots_attr);
5563 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5564 enum devlink_command cmd, u32 portid,
5566 struct devlink_region *region)
5571 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5575 err = devlink_nl_put_handle(msg, devlink);
5577 goto nla_put_failure;
5580 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5581 region->port->index);
5583 goto nla_put_failure;
5586 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5588 goto nla_put_failure;
5590 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5594 goto nla_put_failure;
5596 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
5597 region->max_snapshots);
5599 goto nla_put_failure;
5601 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5603 goto nla_put_failure;
5605 genlmsg_end(msg, hdr);
5609 genlmsg_cancel(msg, hdr);
5613 static struct sk_buff *
5614 devlink_nl_region_notify_build(struct devlink_region *region,
5615 struct devlink_snapshot *snapshot,
5616 enum devlink_command cmd, u32 portid, u32 seq)
5618 struct devlink *devlink = region->devlink;
5619 struct sk_buff *msg;
5624 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5626 return ERR_PTR(-ENOMEM);
5628 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5634 err = devlink_nl_put_handle(msg, devlink);
5636 goto out_cancel_msg;
5639 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5640 region->port->index);
5642 goto out_cancel_msg;
5645 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5648 goto out_cancel_msg;
5651 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5654 goto out_cancel_msg;
5656 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5657 region->size, DEVLINK_ATTR_PAD);
5659 goto out_cancel_msg;
5661 genlmsg_end(msg, hdr);
5666 genlmsg_cancel(msg, hdr);
5669 return ERR_PTR(err);
5672 static void devlink_nl_region_notify(struct devlink_region *region,
5673 struct devlink_snapshot *snapshot,
5674 enum devlink_command cmd)
5676 struct devlink *devlink = region->devlink;
5677 struct sk_buff *msg;
5679 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5680 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
5683 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5687 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
5688 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5692 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5693 * @devlink: devlink instance
5694 * @id: the snapshot id
5696 * Track when a new snapshot begins using an id. Load the count for the
5697 * given id from the snapshot xarray, increment it, and store it back.
5699 * Called when a new snapshot is created with the given id.
5701 * The id *must* have been previously allocated by
5702 * devlink_region_snapshot_id_get().
5704 * Returns 0 on success, or an error on failure.
5706 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5708 unsigned long count;
5712 xa_lock(&devlink->snapshot_ids);
5713 p = xa_load(&devlink->snapshot_ids, id);
5719 if (WARN_ON(!xa_is_value(p))) {
5724 count = xa_to_value(p);
5727 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5730 xa_unlock(&devlink->snapshot_ids);
5735 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5736 * @devlink: devlink instance
5737 * @id: the snapshot id
5739 * Track when a snapshot is deleted and stops using an id. Load the count
5740 * for the given id from the snapshot xarray, decrement it, and store it
5743 * If the count reaches zero, erase this id from the xarray, freeing it
5744 * up for future re-use by devlink_region_snapshot_id_get().
5746 * Called when a snapshot using the given id is deleted, and when the
5747 * initial allocator of the id is finished using it.
5749 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5751 unsigned long count;
5754 xa_lock(&devlink->snapshot_ids);
5755 p = xa_load(&devlink->snapshot_ids, id);
5759 if (WARN_ON(!xa_is_value(p)))
5762 count = xa_to_value(p);
5766 __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5769 /* If this was the last user, we can erase this id */
5770 __xa_erase(&devlink->snapshot_ids, id);
5773 xa_unlock(&devlink->snapshot_ids);
5777 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
5778 * @devlink: devlink instance
5779 * @id: the snapshot id
5781 * Mark the given snapshot id as used by inserting a zero value into the
5784 * This must be called while holding the devlink instance lock. Unlike
5785 * devlink_snapshot_id_get, the initial reference count is zero, not one.
5786 * It is expected that the id will immediately be used before
5787 * releasing the devlink instance lock.
5789 * Returns zero on success, or an error code if the snapshot id could not
5792 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5796 xa_lock(&devlink->snapshot_ids);
5797 if (xa_load(&devlink->snapshot_ids, id)) {
5798 xa_unlock(&devlink->snapshot_ids);
5801 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5803 xa_unlock(&devlink->snapshot_ids);
5808 * __devlink_region_snapshot_id_get - get snapshot ID
5809 * @devlink: devlink instance
5810 * @id: storage to return snapshot id
5812 * Allocates a new snapshot id. Returns zero on success, or a negative
5813 * error on failure. Must be called while holding the devlink instance
5816 * Snapshot IDs are tracked using an xarray which stores the number of
5817 * users of the snapshot id.
5819 * Note that the caller of this function counts as a 'user', in order to
5820 * avoid race conditions. The caller must release its hold on the
5821 * snapshot by using devlink_region_snapshot_id_put.
5823 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5825 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5826 xa_limit_32b, GFP_KERNEL);
5830 * __devlink_region_snapshot_create - create a new snapshot
5831 * This will add a new snapshot of a region. The snapshot
5832 * will be stored on the region struct and can be accessed
5833 * from devlink. This is useful for future analyses of snapshots.
5834 * Multiple snapshots can be created on a region.
5835 * The @snapshot_id should be obtained using the getter function.
5837 * Must be called only while holding the region snapshot lock.
5839 * @region: devlink region of the snapshot
5840 * @data: snapshot data
5841 * @snapshot_id: snapshot id to be created
5844 __devlink_region_snapshot_create(struct devlink_region *region,
5845 u8 *data, u32 snapshot_id)
5847 struct devlink *devlink = region->devlink;
5848 struct devlink_snapshot *snapshot;
5851 lockdep_assert_held(®ion->snapshot_lock);
5853 /* check if region can hold one more snapshot */
5854 if (region->cur_snapshots == region->max_snapshots)
5857 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
5860 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5864 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
5866 goto err_snapshot_id_increment;
5868 snapshot->id = snapshot_id;
5869 snapshot->region = region;
5870 snapshot->data = data;
5872 list_add_tail(&snapshot->list, ®ion->snapshot_list);
5874 region->cur_snapshots++;
5876 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5879 err_snapshot_id_increment:
5884 static void devlink_region_snapshot_del(struct devlink_region *region,
5885 struct devlink_snapshot *snapshot)
5887 struct devlink *devlink = region->devlink;
5889 lockdep_assert_held(®ion->snapshot_lock);
5891 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
5892 region->cur_snapshots--;
5893 list_del(&snapshot->list);
5894 region->ops->destructor(snapshot->data);
5895 __devlink_snapshot_id_decrement(devlink, snapshot->id);
5899 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
5900 struct genl_info *info)
5902 struct devlink *devlink = info->user_ptr[0];
5903 struct devlink_port *port = NULL;
5904 struct devlink_region *region;
5905 const char *region_name;
5906 struct sk_buff *msg;
5910 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME))
5913 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5914 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5916 port = devlink_port_get_by_index(devlink, index);
5921 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5923 region = devlink_port_region_get_by_name(port, region_name);
5925 region = devlink_region_get_by_name(devlink, region_name);
5930 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5934 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
5935 info->snd_portid, info->snd_seq, 0,
5942 return genlmsg_reply(msg, info);
5945 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
5946 struct netlink_callback *cb,
5947 struct devlink_port *port,
5951 struct devlink_region *region;
5954 list_for_each_entry(region, &port->region_list, list) {
5959 err = devlink_nl_region_fill(msg, port->devlink,
5960 DEVLINK_CMD_REGION_GET,
5961 NETLINK_CB(cb->skb).portid,
5963 NLM_F_MULTI, region);
5974 devlink_nl_cmd_region_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
5975 struct netlink_callback *cb)
5977 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5978 struct devlink_region *region;
5979 struct devlink_port *port;
5980 unsigned long port_index;
5984 list_for_each_entry(region, &devlink->region_list, list) {
5985 if (idx < state->idx) {
5989 err = devlink_nl_region_fill(msg, devlink,
5990 DEVLINK_CMD_REGION_GET,
5991 NETLINK_CB(cb->skb).portid,
5993 NLM_F_MULTI, region);
6001 xa_for_each(&devlink->ports, port_index, port) {
6002 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, &idx,
6013 const struct devlink_gen_cmd devl_gen_region = {
6014 .dump_one = devlink_nl_cmd_region_get_dump_one,
6017 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
6018 struct genl_info *info)
6020 struct devlink *devlink = info->user_ptr[0];
6021 struct devlink_snapshot *snapshot;
6022 struct devlink_port *port = NULL;
6023 struct devlink_region *region;
6024 const char *region_name;
6028 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) ||
6029 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID))
6032 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6033 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6035 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6036 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6038 port = devlink_port_get_by_index(devlink, index);
6044 region = devlink_port_region_get_by_name(port, region_name);
6046 region = devlink_region_get_by_name(devlink, region_name);
6051 mutex_lock(®ion->snapshot_lock);
6052 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6054 mutex_unlock(®ion->snapshot_lock);
6058 devlink_region_snapshot_del(region, snapshot);
6059 mutex_unlock(®ion->snapshot_lock);
6064 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
6066 struct devlink *devlink = info->user_ptr[0];
6067 struct devlink_snapshot *snapshot;
6068 struct devlink_port *port = NULL;
6069 struct nlattr *snapshot_id_attr;
6070 struct devlink_region *region;
6071 const char *region_name;
6077 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) {
6078 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
6082 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6084 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6085 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6087 port = devlink_port_get_by_index(devlink, index);
6093 region = devlink_port_region_get_by_name(port, region_name);
6095 region = devlink_region_get_by_name(devlink, region_name);
6098 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
6102 if (!region->ops->snapshot) {
6103 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
6107 mutex_lock(®ion->snapshot_lock);
6109 if (region->cur_snapshots == region->max_snapshots) {
6110 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
6115 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6116 if (snapshot_id_attr) {
6117 snapshot_id = nla_get_u32(snapshot_id_attr);
6119 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6120 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
6125 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
6129 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
6131 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
6137 err = region->port_ops->snapshot(port, region->port_ops,
6138 info->extack, &data);
6140 err = region->ops->snapshot(devlink, region->ops,
6141 info->extack, &data);
6143 goto err_snapshot_capture;
6145 err = __devlink_region_snapshot_create(region, data, snapshot_id);
6147 goto err_snapshot_create;
6149 if (!snapshot_id_attr) {
6150 struct sk_buff *msg;
6152 snapshot = devlink_region_snapshot_get_by_id(region,
6154 if (WARN_ON(!snapshot)) {
6159 msg = devlink_nl_region_notify_build(region, snapshot,
6160 DEVLINK_CMD_REGION_NEW,
6163 err = PTR_ERR_OR_ZERO(msg);
6167 err = genlmsg_reply(msg, info);
6172 mutex_unlock(®ion->snapshot_lock);
6175 err_snapshot_create:
6176 region->ops->destructor(data);
6177 err_snapshot_capture:
6178 __devlink_snapshot_id_decrement(devlink, snapshot_id);
6179 mutex_unlock(®ion->snapshot_lock);
6183 devlink_region_snapshot_del(region, snapshot);
6185 mutex_unlock(®ion->snapshot_lock);
6189 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
6190 u8 *chunk, u32 chunk_size,
6193 struct nlattr *chunk_attr;
6196 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
6200 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
6202 goto nla_put_failure;
6204 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
6207 goto nla_put_failure;
6209 nla_nest_end(msg, chunk_attr);
6213 nla_nest_cancel(msg, chunk_attr);
6217 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
6219 typedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size,
6221 struct netlink_ext_ack *extack);
6224 devlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb,
6225 void *cb_priv, u64 start_offset, u64 end_offset,
6226 u64 *new_offset, struct netlink_ext_ack *extack)
6228 u64 curr_offset = start_offset;
6232 /* Allocate and re-use a single buffer */
6233 data = kmalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL);
6237 *new_offset = start_offset;
6239 while (curr_offset < end_offset) {
6242 data_size = min_t(u32, end_offset - curr_offset,
6243 DEVLINK_REGION_READ_CHUNK_SIZE);
6245 err = cb(cb_priv, data, data_size, curr_offset, extack);
6249 err = devlink_nl_cmd_region_read_chunk_fill(skb, data, data_size, curr_offset);
6253 curr_offset += data_size;
6255 *new_offset = curr_offset;
6263 devlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6265 struct netlink_ext_ack __always_unused *extack)
6267 struct devlink_snapshot *snapshot = cb_priv;
6269 memcpy(chunk, &snapshot->data[curr_offset], chunk_size);
6275 devlink_region_port_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6276 u64 curr_offset, struct netlink_ext_ack *extack)
6278 struct devlink_region *region = cb_priv;
6280 return region->port_ops->read(region->port, region->port_ops, extack,
6281 curr_offset, chunk_size, chunk);
6285 devlink_region_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6286 u64 curr_offset, struct netlink_ext_ack *extack)
6288 struct devlink_region *region = cb_priv;
6290 return region->ops->read(region->devlink, region->ops, extack,
6291 curr_offset, chunk_size, chunk);
6294 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
6295 struct netlink_callback *cb)
6297 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6298 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6299 struct nlattr *chunks_attr, *region_attr, *snapshot_attr;
6300 u64 ret_offset, start_offset, end_offset = U64_MAX;
6301 struct nlattr **attrs = info->attrs;
6302 struct devlink_port *port = NULL;
6303 devlink_chunk_fill_t *region_cb;
6304 struct devlink_region *region;
6305 const char *region_name;
6306 struct devlink *devlink;
6308 void *region_cb_priv;
6312 start_offset = state->start_offset;
6314 devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs);
6315 if (IS_ERR(devlink))
6316 return PTR_ERR(devlink);
6318 if (!attrs[DEVLINK_ATTR_REGION_NAME]) {
6319 NL_SET_ERR_MSG(cb->extack, "No region name provided");
6324 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6325 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6327 port = devlink_port_get_by_index(devlink, index);
6334 region_attr = attrs[DEVLINK_ATTR_REGION_NAME];
6335 region_name = nla_data(region_attr);
6338 region = devlink_port_region_get_by_name(port, region_name);
6340 region = devlink_region_get_by_name(devlink, region_name);
6343 NL_SET_ERR_MSG_ATTR(cb->extack, region_attr, "Requested region does not exist");
6348 snapshot_attr = attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6349 if (!snapshot_attr) {
6350 if (!nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
6351 NL_SET_ERR_MSG(cb->extack, "No snapshot id provided");
6356 if (!region->ops->read) {
6357 NL_SET_ERR_MSG(cb->extack, "Requested region does not support direct read");
6363 region_cb = &devlink_region_port_direct_fill;
6365 region_cb = &devlink_region_direct_fill;
6366 region_cb_priv = region;
6368 struct devlink_snapshot *snapshot;
6371 if (nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
6372 NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Direct region read does not use snapshot");
6377 snapshot_id = nla_get_u32(snapshot_attr);
6378 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6380 NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Requested snapshot does not exist");
6384 region_cb = &devlink_region_snapshot_fill;
6385 region_cb_priv = snapshot;
6388 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
6389 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
6392 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6394 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6395 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
6398 if (end_offset > region->size)
6399 end_offset = region->size;
6401 /* return 0 if there is no further data to read */
6402 if (start_offset == end_offset) {
6407 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6408 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
6409 DEVLINK_CMD_REGION_READ);
6415 err = devlink_nl_put_handle(skb, devlink);
6417 goto nla_put_failure;
6420 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
6421 region->port->index);
6423 goto nla_put_failure;
6426 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
6428 goto nla_put_failure;
6430 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
6433 goto nla_put_failure;
6436 err = devlink_nl_region_read_fill(skb, region_cb, region_cb_priv,
6437 start_offset, end_offset, &ret_offset,
6440 if (err && err != -EMSGSIZE)
6441 goto nla_put_failure;
6443 /* Check if there was any progress done to prevent infinite loop */
6444 if (ret_offset == start_offset) {
6446 goto nla_put_failure;
6449 state->start_offset = ret_offset;
6451 nla_nest_end(skb, chunks_attr);
6452 genlmsg_end(skb, hdr);
6453 devl_unlock(devlink);
6454 devlink_put(devlink);
6458 genlmsg_cancel(skb, hdr);
6460 devl_unlock(devlink);
6461 devlink_put(devlink);
6465 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
6469 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
6471 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
6473 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
6478 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
6481 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
6483 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
6484 const char *version_name,
6485 const char *version_value,
6486 enum devlink_info_version_type version_type)
6488 struct nlattr *nest;
6491 if (req->version_cb)
6492 req->version_cb(version_name, version_type,
6493 req->version_cb_priv);
6498 nest = nla_nest_start_noflag(req->msg, attr);
6502 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
6505 goto nla_put_failure;
6507 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
6510 goto nla_put_failure;
6512 nla_nest_end(req->msg, nest);
6517 nla_nest_cancel(req->msg, nest);
6521 int devlink_info_version_fixed_put(struct devlink_info_req *req,
6522 const char *version_name,
6523 const char *version_value)
6525 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
6526 version_name, version_value,
6527 DEVLINK_INFO_VERSION_TYPE_NONE);
6529 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
6531 int devlink_info_version_stored_put(struct devlink_info_req *req,
6532 const char *version_name,
6533 const char *version_value)
6535 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6536 version_name, version_value,
6537 DEVLINK_INFO_VERSION_TYPE_NONE);
6539 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
6541 int devlink_info_version_stored_put_ext(struct devlink_info_req *req,
6542 const char *version_name,
6543 const char *version_value,
6544 enum devlink_info_version_type version_type)
6546 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6547 version_name, version_value,
6550 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
6552 int devlink_info_version_running_put(struct devlink_info_req *req,
6553 const char *version_name,
6554 const char *version_value)
6556 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6557 version_name, version_value,
6558 DEVLINK_INFO_VERSION_TYPE_NONE);
6560 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
6562 int devlink_info_version_running_put_ext(struct devlink_info_req *req,
6563 const char *version_name,
6564 const char *version_value,
6565 enum devlink_info_version_type version_type)
6567 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6568 version_name, version_value,
6571 EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
6573 static int devlink_nl_driver_info_get(struct device_driver *drv,
6574 struct devlink_info_req *req)
6580 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME,
6587 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
6588 enum devlink_command cmd, u32 portid,
6589 u32 seq, int flags, struct netlink_ext_ack *extack)
6591 struct device *dev = devlink_to_dev(devlink);
6592 struct devlink_info_req req = {};
6596 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6601 if (devlink_nl_put_handle(msg, devlink))
6602 goto err_cancel_msg;
6605 if (devlink->ops->info_get) {
6606 err = devlink->ops->info_get(devlink, &req, extack);
6608 goto err_cancel_msg;
6611 err = devlink_nl_driver_info_get(dev->driver, &req);
6613 goto err_cancel_msg;
6615 genlmsg_end(msg, hdr);
6619 genlmsg_cancel(msg, hdr);
6623 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
6624 struct genl_info *info)
6626 struct devlink *devlink = info->user_ptr[0];
6627 struct sk_buff *msg;
6630 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6634 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6635 info->snd_portid, info->snd_seq, 0,
6642 return genlmsg_reply(msg, info);
6646 devlink_nl_cmd_info_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
6647 struct netlink_callback *cb)
6651 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6652 NETLINK_CB(cb->skb).portid,
6653 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6655 if (err == -EOPNOTSUPP)
6660 const struct devlink_gen_cmd devl_gen_info = {
6661 .dump_one = devlink_nl_cmd_info_get_dump_one,
6664 struct devlink_fmsg_item {
6665 struct list_head list;
6672 struct devlink_fmsg {
6673 struct list_head item_list;
6674 bool putting_binary; /* This flag forces enclosing of binary data
6675 * in an array brackets. It forces using
6676 * of designated API:
6677 * devlink_fmsg_binary_pair_nest_start()
6678 * devlink_fmsg_binary_pair_nest_end()
6682 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6684 struct devlink_fmsg *fmsg;
6686 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6690 INIT_LIST_HEAD(&fmsg->item_list);
6695 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6697 struct devlink_fmsg_item *item, *tmp;
6699 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6700 list_del(&item->list);
6706 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6709 struct devlink_fmsg_item *item;
6711 item = kzalloc(sizeof(*item), GFP_KERNEL);
6715 item->attrtype = attrtype;
6716 list_add_tail(&item->list, &fmsg->item_list);
6721 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6723 if (fmsg->putting_binary)
6726 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6728 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6730 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6732 if (fmsg->putting_binary)
6735 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6738 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6740 if (fmsg->putting_binary)
6743 return devlink_fmsg_nest_end(fmsg);
6745 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6747 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6749 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6751 struct devlink_fmsg_item *item;
6753 if (fmsg->putting_binary)
6756 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6759 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6763 item->nla_type = NLA_NUL_STRING;
6764 item->len = strlen(name) + 1;
6765 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6766 memcpy(&item->value, name, item->len);
6767 list_add_tail(&item->list, &fmsg->item_list);
6772 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6776 if (fmsg->putting_binary)
6779 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6783 err = devlink_fmsg_put_name(fmsg, name);
6789 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6791 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6793 if (fmsg->putting_binary)
6796 return devlink_fmsg_nest_end(fmsg);
6798 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6800 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6805 if (fmsg->putting_binary)
6808 err = devlink_fmsg_pair_nest_start(fmsg, name);
6812 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6818 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6820 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6824 if (fmsg->putting_binary)
6827 err = devlink_fmsg_nest_end(fmsg);
6831 err = devlink_fmsg_nest_end(fmsg);
6837 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6839 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6844 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6848 fmsg->putting_binary = true;
6851 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6853 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6855 if (!fmsg->putting_binary)
6858 fmsg->putting_binary = false;
6859 return devlink_fmsg_arr_pair_nest_end(fmsg);
6861 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6863 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6864 const void *value, u16 value_len,
6867 struct devlink_fmsg_item *item;
6869 if (value_len > DEVLINK_FMSG_MAX_SIZE)
6872 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
6876 item->nla_type = value_nla_type;
6877 item->len = value_len;
6878 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6879 memcpy(&item->value, value, item->len);
6880 list_add_tail(&item->list, &fmsg->item_list);
6885 static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
6887 if (fmsg->putting_binary)
6890 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
6893 static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
6895 if (fmsg->putting_binary)
6898 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
6901 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
6903 if (fmsg->putting_binary)
6906 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
6908 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
6910 static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
6912 if (fmsg->putting_binary)
6915 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
6918 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
6920 if (fmsg->putting_binary)
6923 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
6926 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
6928 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
6931 if (!fmsg->putting_binary)
6934 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
6936 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
6938 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
6943 err = devlink_fmsg_pair_nest_start(fmsg, name);
6947 err = devlink_fmsg_bool_put(fmsg, value);
6951 err = devlink_fmsg_pair_nest_end(fmsg);
6957 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
6959 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
6964 err = devlink_fmsg_pair_nest_start(fmsg, name);
6968 err = devlink_fmsg_u8_put(fmsg, value);
6972 err = devlink_fmsg_pair_nest_end(fmsg);
6978 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
6980 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
6985 err = devlink_fmsg_pair_nest_start(fmsg, name);
6989 err = devlink_fmsg_u32_put(fmsg, value);
6993 err = devlink_fmsg_pair_nest_end(fmsg);
6999 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
7001 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
7006 err = devlink_fmsg_pair_nest_start(fmsg, name);
7010 err = devlink_fmsg_u64_put(fmsg, value);
7014 err = devlink_fmsg_pair_nest_end(fmsg);
7020 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
7022 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
7027 err = devlink_fmsg_pair_nest_start(fmsg, name);
7031 err = devlink_fmsg_string_put(fmsg, value);
7035 err = devlink_fmsg_pair_nest_end(fmsg);
7041 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
7043 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
7044 const void *value, u32 value_len)
7051 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
7055 for (offset = 0; offset < value_len; offset += data_size) {
7056 data_size = value_len - offset;
7057 if (data_size > DEVLINK_FMSG_MAX_SIZE)
7058 data_size = DEVLINK_FMSG_MAX_SIZE;
7059 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
7062 /* Exit from loop with a break (instead of
7063 * return) to make sure putting_binary is turned off in
7064 * devlink_fmsg_binary_pair_nest_end
7068 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
7074 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
7077 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7079 switch (msg->nla_type) {
7084 case NLA_NUL_STRING:
7086 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
7094 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7096 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
7099 switch (msg->nla_type) {
7101 /* Always provide flag data, regardless of its value */
7102 tmp = *(bool *) msg->value;
7104 return nla_put_u8(skb, attrtype, tmp);
7106 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
7108 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
7110 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
7112 case NLA_NUL_STRING:
7113 return nla_put_string(skb, attrtype, (char *) &msg->value);
7115 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
7122 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7125 struct devlink_fmsg_item *item;
7126 struct nlattr *fmsg_nlattr;
7130 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
7134 list_for_each_entry(item, &fmsg->item_list, list) {
7140 switch (item->attrtype) {
7141 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7142 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7143 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7144 case DEVLINK_ATTR_FMSG_NEST_END:
7145 err = nla_put_flag(skb, item->attrtype);
7147 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
7148 err = devlink_fmsg_item_fill_type(item, skb);
7151 err = devlink_fmsg_item_fill_data(item, skb);
7153 case DEVLINK_ATTR_FMSG_OBJ_NAME:
7154 err = nla_put_string(skb, item->attrtype,
7155 (char *) &item->value);
7167 nla_nest_end(skb, fmsg_nlattr);
7171 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
7172 struct genl_info *info,
7173 enum devlink_command cmd, int flags)
7175 struct nlmsghdr *nlh;
7176 struct sk_buff *skb;
7183 int tmp_index = index;
7185 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7189 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
7190 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
7193 goto nla_put_failure;
7196 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7199 else if (err != -EMSGSIZE || tmp_index == index)
7200 goto nla_put_failure;
7202 genlmsg_end(skb, hdr);
7203 err = genlmsg_reply(skb, info);
7208 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7211 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
7212 NLMSG_DONE, 0, flags | NLM_F_MULTI);
7215 goto nla_put_failure;
7218 return genlmsg_reply(skb, info);
7225 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7226 struct netlink_callback *cb,
7227 enum devlink_command cmd)
7229 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7230 int index = state->idx;
7231 int tmp_index = index;
7235 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7236 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
7239 goto nla_put_failure;
7242 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7243 if ((err && err != -EMSGSIZE) || tmp_index == index)
7244 goto nla_put_failure;
7247 genlmsg_end(skb, hdr);
7251 genlmsg_cancel(skb, hdr);
7255 struct devlink_health_reporter {
7256 struct list_head list;
7258 const struct devlink_health_reporter_ops *ops;
7259 struct devlink *devlink;
7260 struct devlink_port *devlink_port;
7261 struct devlink_fmsg *dump_fmsg;
7262 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
7263 u64 graceful_period;
7271 u64 last_recovery_ts;
7272 refcount_t refcount;
7276 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
7278 return reporter->priv;
7280 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
7282 static struct devlink_health_reporter *
7283 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
7284 const char *reporter_name)
7286 struct devlink_health_reporter *reporter;
7288 list_for_each_entry(reporter, reporter_list, list)
7289 if (!strcmp(reporter->ops->name, reporter_name))
7294 static struct devlink_health_reporter *
7295 devlink_health_reporter_find_by_name(struct devlink *devlink,
7296 const char *reporter_name)
7298 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
7302 static struct devlink_health_reporter *
7303 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
7304 const char *reporter_name)
7306 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
7310 static struct devlink_health_reporter *
7311 __devlink_health_reporter_create(struct devlink *devlink,
7312 const struct devlink_health_reporter_ops *ops,
7313 u64 graceful_period, void *priv)
7315 struct devlink_health_reporter *reporter;
7317 if (WARN_ON(graceful_period && !ops->recover))
7318 return ERR_PTR(-EINVAL);
7320 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
7322 return ERR_PTR(-ENOMEM);
7324 reporter->priv = priv;
7325 reporter->ops = ops;
7326 reporter->devlink = devlink;
7327 reporter->graceful_period = graceful_period;
7328 reporter->auto_recover = !!ops->recover;
7329 reporter->auto_dump = !!ops->dump;
7330 mutex_init(&reporter->dump_lock);
7331 refcount_set(&reporter->refcount, 1);
7336 * devl_port_health_reporter_create - create devlink health reporter for
7337 * specified port instance
7339 * @port: devlink_port which should contain the new reporter
7341 * @graceful_period: to avoid recovery loops, in msecs
7344 struct devlink_health_reporter *
7345 devl_port_health_reporter_create(struct devlink_port *port,
7346 const struct devlink_health_reporter_ops *ops,
7347 u64 graceful_period, void *priv)
7349 struct devlink_health_reporter *reporter;
7351 devl_assert_locked(port->devlink);
7353 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
7355 return ERR_PTR(-EEXIST);
7357 reporter = __devlink_health_reporter_create(port->devlink, ops,
7358 graceful_period, priv);
7359 if (IS_ERR(reporter))
7362 reporter->devlink_port = port;
7363 list_add_tail(&reporter->list, &port->reporter_list);
7366 EXPORT_SYMBOL_GPL(devl_port_health_reporter_create);
7368 struct devlink_health_reporter *
7369 devlink_port_health_reporter_create(struct devlink_port *port,
7370 const struct devlink_health_reporter_ops *ops,
7371 u64 graceful_period, void *priv)
7373 struct devlink_health_reporter *reporter;
7374 struct devlink *devlink = port->devlink;
7377 reporter = devl_port_health_reporter_create(port, ops,
7378 graceful_period, priv);
7379 devl_unlock(devlink);
7382 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
7385 * devl_health_reporter_create - create devlink health reporter
7389 * @graceful_period: to avoid recovery loops, in msecs
7392 struct devlink_health_reporter *
7393 devl_health_reporter_create(struct devlink *devlink,
7394 const struct devlink_health_reporter_ops *ops,
7395 u64 graceful_period, void *priv)
7397 struct devlink_health_reporter *reporter;
7399 devl_assert_locked(devlink);
7401 if (devlink_health_reporter_find_by_name(devlink, ops->name))
7402 return ERR_PTR(-EEXIST);
7404 reporter = __devlink_health_reporter_create(devlink, ops,
7405 graceful_period, priv);
7406 if (IS_ERR(reporter))
7409 list_add_tail(&reporter->list, &devlink->reporter_list);
7412 EXPORT_SYMBOL_GPL(devl_health_reporter_create);
7414 struct devlink_health_reporter *
7415 devlink_health_reporter_create(struct devlink *devlink,
7416 const struct devlink_health_reporter_ops *ops,
7417 u64 graceful_period, void *priv)
7419 struct devlink_health_reporter *reporter;
7422 reporter = devl_health_reporter_create(devlink, ops,
7423 graceful_period, priv);
7424 devl_unlock(devlink);
7427 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
7430 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
7432 mutex_destroy(&reporter->dump_lock);
7433 if (reporter->dump_fmsg)
7434 devlink_fmsg_free(reporter->dump_fmsg);
7439 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
7441 if (refcount_dec_and_test(&reporter->refcount))
7442 devlink_health_reporter_free(reporter);
7446 * devl_health_reporter_destroy - destroy devlink health reporter
7448 * @reporter: devlink health reporter to destroy
7451 devl_health_reporter_destroy(struct devlink_health_reporter *reporter)
7453 devl_assert_locked(reporter->devlink);
7455 list_del(&reporter->list);
7456 devlink_health_reporter_put(reporter);
7458 EXPORT_SYMBOL_GPL(devl_health_reporter_destroy);
7461 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7463 struct devlink *devlink = reporter->devlink;
7466 devl_health_reporter_destroy(reporter);
7467 devl_unlock(devlink);
7469 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
7472 devlink_nl_health_reporter_fill(struct sk_buff *msg,
7473 struct devlink_health_reporter *reporter,
7474 enum devlink_command cmd, u32 portid,
7477 struct devlink *devlink = reporter->devlink;
7478 struct nlattr *reporter_attr;
7481 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7485 if (devlink_nl_put_handle(msg, devlink))
7486 goto genlmsg_cancel;
7488 if (reporter->devlink_port) {
7489 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
7490 goto genlmsg_cancel;
7492 reporter_attr = nla_nest_start_noflag(msg,
7493 DEVLINK_ATTR_HEALTH_REPORTER);
7495 goto genlmsg_cancel;
7496 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
7497 reporter->ops->name))
7498 goto reporter_nest_cancel;
7499 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
7500 reporter->health_state))
7501 goto reporter_nest_cancel;
7502 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
7503 reporter->error_count, DEVLINK_ATTR_PAD))
7504 goto reporter_nest_cancel;
7505 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
7506 reporter->recovery_count, DEVLINK_ATTR_PAD))
7507 goto reporter_nest_cancel;
7508 if (reporter->ops->recover &&
7509 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
7510 reporter->graceful_period,
7512 goto reporter_nest_cancel;
7513 if (reporter->ops->recover &&
7514 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
7515 reporter->auto_recover))
7516 goto reporter_nest_cancel;
7517 if (reporter->dump_fmsg &&
7518 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
7519 jiffies_to_msecs(reporter->dump_ts),
7521 goto reporter_nest_cancel;
7522 if (reporter->dump_fmsg &&
7523 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
7524 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
7525 goto reporter_nest_cancel;
7526 if (reporter->ops->dump &&
7527 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
7528 reporter->auto_dump))
7529 goto reporter_nest_cancel;
7531 nla_nest_end(msg, reporter_attr);
7532 genlmsg_end(msg, hdr);
7535 reporter_nest_cancel:
7536 nla_nest_end(msg, reporter_attr);
7538 genlmsg_cancel(msg, hdr);
7542 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7543 enum devlink_command cmd)
7545 struct devlink *devlink = reporter->devlink;
7546 struct sk_buff *msg;
7549 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7550 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7556 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7562 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7563 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7567 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7569 reporter->recovery_count++;
7570 reporter->last_recovery_ts = jiffies;
7572 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7575 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7576 void *priv_ctx, struct netlink_ext_ack *extack)
7580 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7583 if (!reporter->ops->recover)
7586 err = reporter->ops->recover(reporter, priv_ctx, extack);
7590 devlink_health_reporter_recovery_done(reporter);
7591 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7592 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7598 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7600 if (!reporter->dump_fmsg)
7602 devlink_fmsg_free(reporter->dump_fmsg);
7603 reporter->dump_fmsg = NULL;
7606 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7608 struct netlink_ext_ack *extack)
7612 if (!reporter->ops->dump)
7615 if (reporter->dump_fmsg)
7618 reporter->dump_fmsg = devlink_fmsg_alloc();
7619 if (!reporter->dump_fmsg) {
7624 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7628 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7633 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7637 reporter->dump_ts = jiffies;
7638 reporter->dump_real_ts = ktime_get_real_ns();
7643 devlink_health_dump_clear(reporter);
7647 int devlink_health_report(struct devlink_health_reporter *reporter,
7648 const char *msg, void *priv_ctx)
7650 enum devlink_health_reporter_state prev_health_state;
7651 struct devlink *devlink = reporter->devlink;
7652 unsigned long recover_ts_threshold;
7655 /* write a log message of the current error */
7657 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7658 reporter->error_count++;
7659 prev_health_state = reporter->health_state;
7660 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7661 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7663 /* abort if the previous error wasn't recovered */
7664 recover_ts_threshold = reporter->last_recovery_ts +
7665 msecs_to_jiffies(reporter->graceful_period);
7666 if (reporter->auto_recover &&
7667 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7668 (reporter->last_recovery_ts && reporter->recovery_count &&
7669 time_is_after_jiffies(recover_ts_threshold)))) {
7670 trace_devlink_health_recover_aborted(devlink,
7671 reporter->ops->name,
7672 reporter->health_state,
7674 reporter->last_recovery_ts);
7678 if (reporter->auto_dump) {
7679 mutex_lock(&reporter->dump_lock);
7680 /* store current dump of current error, for later analysis */
7681 devlink_health_do_dump(reporter, priv_ctx, NULL);
7682 mutex_unlock(&reporter->dump_lock);
7685 if (!reporter->auto_recover)
7689 ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL);
7690 devl_unlock(devlink);
7694 EXPORT_SYMBOL_GPL(devlink_health_report);
7696 static struct devlink_health_reporter *
7697 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7698 struct nlattr **attrs)
7700 struct devlink_health_reporter *reporter;
7701 struct devlink_port *devlink_port;
7702 char *reporter_name;
7704 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7707 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7708 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7709 if (IS_ERR(devlink_port)) {
7710 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7712 refcount_inc(&reporter->refcount);
7714 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7716 refcount_inc(&reporter->refcount);
7722 static struct devlink_health_reporter *
7723 devlink_health_reporter_get_from_info(struct devlink *devlink,
7724 struct genl_info *info)
7726 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7729 static struct devlink_health_reporter *
7730 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7732 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7733 struct devlink_health_reporter *reporter;
7734 struct nlattr **attrs = info->attrs;
7735 struct devlink *devlink;
7737 devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs);
7738 if (IS_ERR(devlink))
7740 devl_unlock(devlink);
7742 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7743 devlink_put(devlink);
7748 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7749 enum devlink_health_reporter_state state)
7751 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7752 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7755 if (reporter->health_state == state)
7758 reporter->health_state = state;
7759 trace_devlink_health_reporter_state_update(reporter->devlink,
7760 reporter->ops->name, state);
7761 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7763 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7765 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7766 struct genl_info *info)
7768 struct devlink *devlink = info->user_ptr[0];
7769 struct devlink_health_reporter *reporter;
7770 struct sk_buff *msg;
7773 reporter = devlink_health_reporter_get_from_info(devlink, info);
7777 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7783 err = devlink_nl_health_reporter_fill(msg, reporter,
7784 DEVLINK_CMD_HEALTH_REPORTER_GET,
7785 info->snd_portid, info->snd_seq,
7792 err = genlmsg_reply(msg, info);
7794 devlink_health_reporter_put(reporter);
7799 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7800 struct netlink_callback *cb)
7802 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7803 struct devlink *devlink;
7806 devlink_dump_for_each_instance_get(msg, state, devlink) {
7807 struct devlink_health_reporter *reporter;
7808 struct devlink_port *port;
7809 unsigned long port_index;
7813 if (!devl_is_registered(devlink))
7816 list_for_each_entry(reporter, &devlink->reporter_list,
7818 if (idx < state->idx) {
7822 err = devlink_nl_health_reporter_fill(
7823 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7824 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7827 devl_unlock(devlink);
7828 devlink_put(devlink);
7835 xa_for_each(&devlink->ports, port_index, port) {
7836 list_for_each_entry(reporter, &port->reporter_list, list) {
7837 if (idx < state->idx) {
7841 err = devlink_nl_health_reporter_fill(
7843 DEVLINK_CMD_HEALTH_REPORTER_GET,
7844 NETLINK_CB(cb->skb).portid,
7845 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7847 devl_unlock(devlink);
7848 devlink_put(devlink);
7856 devl_unlock(devlink);
7857 devlink_put(devlink);
7864 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7865 struct genl_info *info)
7867 struct devlink *devlink = info->user_ptr[0];
7868 struct devlink_health_reporter *reporter;
7871 reporter = devlink_health_reporter_get_from_info(devlink, info);
7875 if (!reporter->ops->recover &&
7876 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
7877 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
7881 if (!reporter->ops->dump &&
7882 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
7887 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7888 reporter->graceful_period =
7889 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
7891 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7892 reporter->auto_recover =
7893 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
7895 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7896 reporter->auto_dump =
7897 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
7899 devlink_health_reporter_put(reporter);
7902 devlink_health_reporter_put(reporter);
7906 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
7907 struct genl_info *info)
7909 struct devlink *devlink = info->user_ptr[0];
7910 struct devlink_health_reporter *reporter;
7913 reporter = devlink_health_reporter_get_from_info(devlink, info);
7917 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
7919 devlink_health_reporter_put(reporter);
7923 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
7924 struct genl_info *info)
7926 struct devlink *devlink = info->user_ptr[0];
7927 struct devlink_health_reporter *reporter;
7928 struct devlink_fmsg *fmsg;
7931 reporter = devlink_health_reporter_get_from_info(devlink, info);
7935 if (!reporter->ops->diagnose) {
7936 devlink_health_reporter_put(reporter);
7940 fmsg = devlink_fmsg_alloc();
7942 devlink_health_reporter_put(reporter);
7946 err = devlink_fmsg_obj_nest_start(fmsg);
7950 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
7954 err = devlink_fmsg_obj_nest_end(fmsg);
7958 err = devlink_fmsg_snd(fmsg, info,
7959 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
7962 devlink_fmsg_free(fmsg);
7963 devlink_health_reporter_put(reporter);
7968 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
7969 struct netlink_callback *cb)
7971 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7972 struct devlink_health_reporter *reporter;
7975 reporter = devlink_health_reporter_get_from_cb(cb);
7979 if (!reporter->ops->dump) {
7983 mutex_lock(&reporter->dump_lock);
7985 err = devlink_health_do_dump(reporter, NULL, cb->extack);
7988 state->dump_ts = reporter->dump_ts;
7990 if (!reporter->dump_fmsg || state->dump_ts != reporter->dump_ts) {
7991 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
7996 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
7997 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
7999 mutex_unlock(&reporter->dump_lock);
8001 devlink_health_reporter_put(reporter);
8006 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
8007 struct genl_info *info)
8009 struct devlink *devlink = info->user_ptr[0];
8010 struct devlink_health_reporter *reporter;
8012 reporter = devlink_health_reporter_get_from_info(devlink, info);
8016 if (!reporter->ops->dump) {
8017 devlink_health_reporter_put(reporter);
8021 mutex_lock(&reporter->dump_lock);
8022 devlink_health_dump_clear(reporter);
8023 mutex_unlock(&reporter->dump_lock);
8024 devlink_health_reporter_put(reporter);
8028 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
8029 struct genl_info *info)
8031 struct devlink *devlink = info->user_ptr[0];
8032 struct devlink_health_reporter *reporter;
8035 reporter = devlink_health_reporter_get_from_info(devlink, info);
8039 if (!reporter->ops->test) {
8040 devlink_health_reporter_put(reporter);
8044 err = reporter->ops->test(reporter, info->extack);
8046 devlink_health_reporter_put(reporter);
8050 struct devlink_stats {
8051 u64_stats_t rx_bytes;
8052 u64_stats_t rx_packets;
8053 struct u64_stats_sync syncp;
8057 * struct devlink_trap_policer_item - Packet trap policer attributes.
8058 * @policer: Immutable packet trap policer attributes.
8059 * @rate: Rate in packets / sec.
8060 * @burst: Burst size in packets.
8061 * @list: trap_policer_list member.
8063 * Describes packet trap policer attributes. Created by devlink during trap
8064 * policer registration.
8066 struct devlink_trap_policer_item {
8067 const struct devlink_trap_policer *policer;
8070 struct list_head list;
8074 * struct devlink_trap_group_item - Packet trap group attributes.
8075 * @group: Immutable packet trap group attributes.
8076 * @policer_item: Associated policer item. Can be NULL.
8077 * @list: trap_group_list member.
8078 * @stats: Trap group statistics.
8080 * Describes packet trap group attributes. Created by devlink during trap
8081 * group registration.
8083 struct devlink_trap_group_item {
8084 const struct devlink_trap_group *group;
8085 struct devlink_trap_policer_item *policer_item;
8086 struct list_head list;
8087 struct devlink_stats __percpu *stats;
8091 * struct devlink_trap_item - Packet trap attributes.
8092 * @trap: Immutable packet trap attributes.
8093 * @group_item: Associated group item.
8094 * @list: trap_list member.
8095 * @action: Trap action.
8096 * @stats: Trap statistics.
8097 * @priv: Driver private information.
8099 * Describes both mutable and immutable packet trap attributes. Created by
8100 * devlink during trap registration and used for all trap related operations.
8102 struct devlink_trap_item {
8103 const struct devlink_trap *trap;
8104 struct devlink_trap_group_item *group_item;
8105 struct list_head list;
8106 enum devlink_trap_action action;
8107 struct devlink_stats __percpu *stats;
8111 static struct devlink_trap_policer_item *
8112 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
8114 struct devlink_trap_policer_item *policer_item;
8116 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8117 if (policer_item->policer->id == id)
8118 return policer_item;
8124 static struct devlink_trap_item *
8125 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
8127 struct devlink_trap_item *trap_item;
8129 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8130 if (!strcmp(trap_item->trap->name, name))
8137 static struct devlink_trap_item *
8138 devlink_trap_item_get_from_info(struct devlink *devlink,
8139 struct genl_info *info)
8141 struct nlattr *attr;
8143 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
8145 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
8147 return devlink_trap_item_lookup(devlink, nla_data(attr));
8151 devlink_trap_action_get_from_info(struct genl_info *info,
8152 enum devlink_trap_action *p_trap_action)
8156 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
8158 case DEVLINK_TRAP_ACTION_DROP:
8159 case DEVLINK_TRAP_ACTION_TRAP:
8160 case DEVLINK_TRAP_ACTION_MIRROR:
8161 *p_trap_action = val;
8170 static int devlink_trap_metadata_put(struct sk_buff *msg,
8171 const struct devlink_trap *trap)
8173 struct nlattr *attr;
8175 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
8179 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
8180 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
8181 goto nla_put_failure;
8182 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
8183 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
8184 goto nla_put_failure;
8186 nla_nest_end(msg, attr);
8191 nla_nest_cancel(msg, attr);
8195 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
8196 struct devlink_stats *stats)
8200 memset(stats, 0, sizeof(*stats));
8201 for_each_possible_cpu(i) {
8202 struct devlink_stats *cpu_stats;
8203 u64 rx_packets, rx_bytes;
8206 cpu_stats = per_cpu_ptr(trap_stats, i);
8208 start = u64_stats_fetch_begin(&cpu_stats->syncp);
8209 rx_packets = u64_stats_read(&cpu_stats->rx_packets);
8210 rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
8211 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
8213 u64_stats_add(&stats->rx_packets, rx_packets);
8214 u64_stats_add(&stats->rx_bytes, rx_bytes);
8219 devlink_trap_group_stats_put(struct sk_buff *msg,
8220 struct devlink_stats __percpu *trap_stats)
8222 struct devlink_stats stats;
8223 struct nlattr *attr;
8225 devlink_trap_stats_read(trap_stats, &stats);
8227 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8231 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8232 u64_stats_read(&stats.rx_packets),
8234 goto nla_put_failure;
8236 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8237 u64_stats_read(&stats.rx_bytes),
8239 goto nla_put_failure;
8241 nla_nest_end(msg, attr);
8246 nla_nest_cancel(msg, attr);
8250 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8251 const struct devlink_trap_item *trap_item)
8253 struct devlink_stats stats;
8254 struct nlattr *attr;
8258 if (devlink->ops->trap_drop_counter_get) {
8259 err = devlink->ops->trap_drop_counter_get(devlink,
8266 devlink_trap_stats_read(trap_item->stats, &stats);
8268 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8272 if (devlink->ops->trap_drop_counter_get &&
8273 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8275 goto nla_put_failure;
8277 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8278 u64_stats_read(&stats.rx_packets),
8280 goto nla_put_failure;
8282 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8283 u64_stats_read(&stats.rx_bytes),
8285 goto nla_put_failure;
8287 nla_nest_end(msg, attr);
8292 nla_nest_cancel(msg, attr);
8296 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8297 const struct devlink_trap_item *trap_item,
8298 enum devlink_command cmd, u32 portid, u32 seq,
8301 struct devlink_trap_group_item *group_item = trap_item->group_item;
8305 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8309 if (devlink_nl_put_handle(msg, devlink))
8310 goto nla_put_failure;
8312 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8313 group_item->group->name))
8314 goto nla_put_failure;
8316 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8317 goto nla_put_failure;
8319 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8320 goto nla_put_failure;
8322 if (trap_item->trap->generic &&
8323 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8324 goto nla_put_failure;
8326 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8327 goto nla_put_failure;
8329 err = devlink_trap_metadata_put(msg, trap_item->trap);
8331 goto nla_put_failure;
8333 err = devlink_trap_stats_put(msg, devlink, trap_item);
8335 goto nla_put_failure;
8337 genlmsg_end(msg, hdr);
8342 genlmsg_cancel(msg, hdr);
8346 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8347 struct genl_info *info)
8349 struct netlink_ext_ack *extack = info->extack;
8350 struct devlink *devlink = info->user_ptr[0];
8351 struct devlink_trap_item *trap_item;
8352 struct sk_buff *msg;
8355 if (list_empty(&devlink->trap_list))
8358 trap_item = devlink_trap_item_get_from_info(devlink, info);
8360 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8364 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8368 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8369 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8374 return genlmsg_reply(msg, info);
8382 devlink_nl_cmd_trap_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
8383 struct netlink_callback *cb)
8385 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8386 struct devlink_trap_item *trap_item;
8390 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8391 if (idx < state->idx) {
8395 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8396 DEVLINK_CMD_TRAP_NEW,
8397 NETLINK_CB(cb->skb).portid,
8410 const struct devlink_gen_cmd devl_gen_trap = {
8411 .dump_one = devlink_nl_cmd_trap_get_dump_one,
8414 static int __devlink_trap_action_set(struct devlink *devlink,
8415 struct devlink_trap_item *trap_item,
8416 enum devlink_trap_action trap_action,
8417 struct netlink_ext_ack *extack)
8421 if (trap_item->action != trap_action &&
8422 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8423 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8427 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8428 trap_action, extack);
8432 trap_item->action = trap_action;
8437 static int devlink_trap_action_set(struct devlink *devlink,
8438 struct devlink_trap_item *trap_item,
8439 struct genl_info *info)
8441 enum devlink_trap_action trap_action;
8444 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8447 err = devlink_trap_action_get_from_info(info, &trap_action);
8449 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8453 return __devlink_trap_action_set(devlink, trap_item, trap_action,
8457 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8458 struct genl_info *info)
8460 struct netlink_ext_ack *extack = info->extack;
8461 struct devlink *devlink = info->user_ptr[0];
8462 struct devlink_trap_item *trap_item;
8464 if (list_empty(&devlink->trap_list))
8467 trap_item = devlink_trap_item_get_from_info(devlink, info);
8469 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8473 return devlink_trap_action_set(devlink, trap_item, info);
8476 static struct devlink_trap_group_item *
8477 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8479 struct devlink_trap_group_item *group_item;
8481 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8482 if (!strcmp(group_item->group->name, name))
8489 static struct devlink_trap_group_item *
8490 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8492 struct devlink_trap_group_item *group_item;
8494 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8495 if (group_item->group->id == id)
8502 static struct devlink_trap_group_item *
8503 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8504 struct genl_info *info)
8508 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8510 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8512 return devlink_trap_group_item_lookup(devlink, name);
8516 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8517 const struct devlink_trap_group_item *group_item,
8518 enum devlink_command cmd, u32 portid, u32 seq,
8524 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8528 if (devlink_nl_put_handle(msg, devlink))
8529 goto nla_put_failure;
8531 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8532 group_item->group->name))
8533 goto nla_put_failure;
8535 if (group_item->group->generic &&
8536 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8537 goto nla_put_failure;
8539 if (group_item->policer_item &&
8540 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8541 group_item->policer_item->policer->id))
8542 goto nla_put_failure;
8544 err = devlink_trap_group_stats_put(msg, group_item->stats);
8546 goto nla_put_failure;
8548 genlmsg_end(msg, hdr);
8553 genlmsg_cancel(msg, hdr);
8557 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8558 struct genl_info *info)
8560 struct netlink_ext_ack *extack = info->extack;
8561 struct devlink *devlink = info->user_ptr[0];
8562 struct devlink_trap_group_item *group_item;
8563 struct sk_buff *msg;
8566 if (list_empty(&devlink->trap_group_list))
8569 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8571 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8579 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8580 DEVLINK_CMD_TRAP_GROUP_NEW,
8581 info->snd_portid, info->snd_seq, 0);
8583 goto err_trap_group_fill;
8585 return genlmsg_reply(msg, info);
8587 err_trap_group_fill:
8593 devlink_nl_cmd_trap_group_get_dump_one(struct sk_buff *msg,
8594 struct devlink *devlink,
8595 struct netlink_callback *cb)
8597 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8598 struct devlink_trap_group_item *group_item;
8603 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8604 if (idx < state->idx) {
8608 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8609 DEVLINK_CMD_TRAP_GROUP_NEW,
8610 NETLINK_CB(cb->skb).portid,
8623 const struct devlink_gen_cmd devl_gen_trap_group = {
8624 .dump_one = devlink_nl_cmd_trap_group_get_dump_one,
8628 __devlink_trap_group_action_set(struct devlink *devlink,
8629 struct devlink_trap_group_item *group_item,
8630 enum devlink_trap_action trap_action,
8631 struct netlink_ext_ack *extack)
8633 const char *group_name = group_item->group->name;
8634 struct devlink_trap_item *trap_item;
8637 if (devlink->ops->trap_group_action_set) {
8638 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8639 trap_action, extack);
8643 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8644 if (strcmp(trap_item->group_item->group->name, group_name))
8646 if (trap_item->action != trap_action &&
8647 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8649 trap_item->action = trap_action;
8655 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8656 if (strcmp(trap_item->group_item->group->name, group_name))
8658 err = __devlink_trap_action_set(devlink, trap_item,
8659 trap_action, extack);
8668 devlink_trap_group_action_set(struct devlink *devlink,
8669 struct devlink_trap_group_item *group_item,
8670 struct genl_info *info, bool *p_modified)
8672 enum devlink_trap_action trap_action;
8675 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8678 err = devlink_trap_action_get_from_info(info, &trap_action);
8680 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8684 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8694 static int devlink_trap_group_set(struct devlink *devlink,
8695 struct devlink_trap_group_item *group_item,
8696 struct genl_info *info)
8698 struct devlink_trap_policer_item *policer_item;
8699 struct netlink_ext_ack *extack = info->extack;
8700 const struct devlink_trap_policer *policer;
8701 struct nlattr **attrs = info->attrs;
8705 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8708 if (!devlink->ops->trap_group_set)
8711 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8712 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
8713 if (policer_id && !policer_item) {
8714 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8717 policer = policer_item ? policer_item->policer : NULL;
8719 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8724 group_item->policer_item = policer_item;
8729 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8730 struct genl_info *info)
8732 struct netlink_ext_ack *extack = info->extack;
8733 struct devlink *devlink = info->user_ptr[0];
8734 struct devlink_trap_group_item *group_item;
8735 bool modified = false;
8738 if (list_empty(&devlink->trap_group_list))
8741 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8743 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8747 err = devlink_trap_group_action_set(devlink, group_item, info,
8752 err = devlink_trap_group_set(devlink, group_item, info);
8754 goto err_trap_group_set;
8760 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8764 static struct devlink_trap_policer_item *
8765 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8766 struct genl_info *info)
8770 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8772 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8774 return devlink_trap_policer_item_lookup(devlink, id);
8778 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8779 const struct devlink_trap_policer *policer)
8781 struct nlattr *attr;
8785 if (!devlink->ops->trap_policer_counter_get)
8788 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8792 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8796 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8798 goto nla_put_failure;
8800 nla_nest_end(msg, attr);
8805 nla_nest_cancel(msg, attr);
8810 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8811 const struct devlink_trap_policer_item *policer_item,
8812 enum devlink_command cmd, u32 portid, u32 seq,
8818 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8822 if (devlink_nl_put_handle(msg, devlink))
8823 goto nla_put_failure;
8825 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8826 policer_item->policer->id))
8827 goto nla_put_failure;
8829 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8830 policer_item->rate, DEVLINK_ATTR_PAD))
8831 goto nla_put_failure;
8833 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8834 policer_item->burst, DEVLINK_ATTR_PAD))
8835 goto nla_put_failure;
8837 err = devlink_trap_policer_stats_put(msg, devlink,
8838 policer_item->policer);
8840 goto nla_put_failure;
8842 genlmsg_end(msg, hdr);
8847 genlmsg_cancel(msg, hdr);
8851 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8852 struct genl_info *info)
8854 struct devlink_trap_policer_item *policer_item;
8855 struct netlink_ext_ack *extack = info->extack;
8856 struct devlink *devlink = info->user_ptr[0];
8857 struct sk_buff *msg;
8860 if (list_empty(&devlink->trap_policer_list))
8863 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8864 if (!policer_item) {
8865 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8873 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8874 DEVLINK_CMD_TRAP_POLICER_NEW,
8875 info->snd_portid, info->snd_seq, 0);
8877 goto err_trap_policer_fill;
8879 return genlmsg_reply(msg, info);
8881 err_trap_policer_fill:
8887 devlink_nl_cmd_trap_policer_get_dump_one(struct sk_buff *msg,
8888 struct devlink *devlink,
8889 struct netlink_callback *cb)
8891 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8892 struct devlink_trap_policer_item *policer_item;
8896 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8897 if (idx < state->idx) {
8901 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8902 DEVLINK_CMD_TRAP_POLICER_NEW,
8903 NETLINK_CB(cb->skb).portid,
8916 const struct devlink_gen_cmd devl_gen_trap_policer = {
8917 .dump_one = devlink_nl_cmd_trap_policer_get_dump_one,
8921 devlink_trap_policer_set(struct devlink *devlink,
8922 struct devlink_trap_policer_item *policer_item,
8923 struct genl_info *info)
8925 struct netlink_ext_ack *extack = info->extack;
8926 struct nlattr **attrs = info->attrs;
8930 rate = policer_item->rate;
8931 burst = policer_item->burst;
8933 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
8934 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
8936 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
8937 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
8939 if (rate < policer_item->policer->min_rate) {
8940 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
8944 if (rate > policer_item->policer->max_rate) {
8945 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
8949 if (burst < policer_item->policer->min_burst) {
8950 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
8954 if (burst > policer_item->policer->max_burst) {
8955 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
8959 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
8960 rate, burst, info->extack);
8964 policer_item->rate = rate;
8965 policer_item->burst = burst;
8970 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
8971 struct genl_info *info)
8973 struct devlink_trap_policer_item *policer_item;
8974 struct netlink_ext_ack *extack = info->extack;
8975 struct devlink *devlink = info->user_ptr[0];
8977 if (list_empty(&devlink->trap_policer_list))
8980 if (!devlink->ops->trap_policer_set)
8983 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8984 if (!policer_item) {
8985 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8989 return devlink_trap_policer_set(devlink, policer_item, info);
8992 const struct genl_small_ops devlink_nl_ops[56] = {
8994 .cmd = DEVLINK_CMD_GET,
8995 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8996 .doit = devlink_nl_cmd_get_doit,
8997 .dumpit = devlink_nl_instance_iter_dump,
8998 /* can be retrieved by unprivileged users */
9001 .cmd = DEVLINK_CMD_PORT_GET,
9002 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9003 .doit = devlink_nl_cmd_port_get_doit,
9004 .dumpit = devlink_nl_instance_iter_dump,
9005 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9006 /* can be retrieved by unprivileged users */
9009 .cmd = DEVLINK_CMD_PORT_SET,
9010 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9011 .doit = devlink_nl_cmd_port_set_doit,
9012 .flags = GENL_ADMIN_PERM,
9013 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9016 .cmd = DEVLINK_CMD_RATE_GET,
9017 .doit = devlink_nl_cmd_rate_get_doit,
9018 .dumpit = devlink_nl_instance_iter_dump,
9019 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9020 /* can be retrieved by unprivileged users */
9023 .cmd = DEVLINK_CMD_RATE_SET,
9024 .doit = devlink_nl_cmd_rate_set_doit,
9025 .flags = GENL_ADMIN_PERM,
9026 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9029 .cmd = DEVLINK_CMD_RATE_NEW,
9030 .doit = devlink_nl_cmd_rate_new_doit,
9031 .flags = GENL_ADMIN_PERM,
9034 .cmd = DEVLINK_CMD_RATE_DEL,
9035 .doit = devlink_nl_cmd_rate_del_doit,
9036 .flags = GENL_ADMIN_PERM,
9037 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9040 .cmd = DEVLINK_CMD_PORT_SPLIT,
9041 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9042 .doit = devlink_nl_cmd_port_split_doit,
9043 .flags = GENL_ADMIN_PERM,
9044 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9047 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9048 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9049 .doit = devlink_nl_cmd_port_unsplit_doit,
9050 .flags = GENL_ADMIN_PERM,
9051 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9054 .cmd = DEVLINK_CMD_PORT_NEW,
9055 .doit = devlink_nl_cmd_port_new_doit,
9056 .flags = GENL_ADMIN_PERM,
9059 .cmd = DEVLINK_CMD_PORT_DEL,
9060 .doit = devlink_nl_cmd_port_del_doit,
9061 .flags = GENL_ADMIN_PERM,
9064 .cmd = DEVLINK_CMD_LINECARD_GET,
9065 .doit = devlink_nl_cmd_linecard_get_doit,
9066 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9067 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9068 /* can be retrieved by unprivileged users */
9071 .cmd = DEVLINK_CMD_LINECARD_SET,
9072 .doit = devlink_nl_cmd_linecard_set_doit,
9073 .flags = GENL_ADMIN_PERM,
9074 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9077 .cmd = DEVLINK_CMD_SB_GET,
9078 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9079 .doit = devlink_nl_cmd_sb_get_doit,
9080 .dumpit = devlink_nl_instance_iter_dump,
9081 /* can be retrieved by unprivileged users */
9084 .cmd = DEVLINK_CMD_SB_POOL_GET,
9085 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9086 .doit = devlink_nl_cmd_sb_pool_get_doit,
9087 .dumpit = devlink_nl_instance_iter_dump,
9088 /* can be retrieved by unprivileged users */
9091 .cmd = DEVLINK_CMD_SB_POOL_SET,
9092 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9093 .doit = devlink_nl_cmd_sb_pool_set_doit,
9094 .flags = GENL_ADMIN_PERM,
9097 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9098 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9099 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9100 .dumpit = devlink_nl_instance_iter_dump,
9101 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9102 /* can be retrieved by unprivileged users */
9105 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9106 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9107 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9108 .flags = GENL_ADMIN_PERM,
9109 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9112 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9113 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9114 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9115 .dumpit = devlink_nl_instance_iter_dump,
9116 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9117 /* can be retrieved by unprivileged users */
9120 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9121 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9122 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9123 .flags = GENL_ADMIN_PERM,
9124 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9127 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9128 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9129 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9130 .flags = GENL_ADMIN_PERM,
9133 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9134 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9135 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9136 .flags = GENL_ADMIN_PERM,
9139 .cmd = DEVLINK_CMD_ESWITCH_GET,
9140 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9141 .doit = devlink_nl_cmd_eswitch_get_doit,
9142 .flags = GENL_ADMIN_PERM,
9145 .cmd = DEVLINK_CMD_ESWITCH_SET,
9146 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9147 .doit = devlink_nl_cmd_eswitch_set_doit,
9148 .flags = GENL_ADMIN_PERM,
9151 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9152 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9153 .doit = devlink_nl_cmd_dpipe_table_get,
9154 /* can be retrieved by unprivileged users */
9157 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9158 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9159 .doit = devlink_nl_cmd_dpipe_entries_get,
9160 /* can be retrieved by unprivileged users */
9163 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9164 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9165 .doit = devlink_nl_cmd_dpipe_headers_get,
9166 /* can be retrieved by unprivileged users */
9169 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9170 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9171 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9172 .flags = GENL_ADMIN_PERM,
9175 .cmd = DEVLINK_CMD_RESOURCE_SET,
9176 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9177 .doit = devlink_nl_cmd_resource_set,
9178 .flags = GENL_ADMIN_PERM,
9181 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9182 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9183 .doit = devlink_nl_cmd_resource_dump,
9184 /* can be retrieved by unprivileged users */
9187 .cmd = DEVLINK_CMD_RELOAD,
9188 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9189 .doit = devlink_nl_cmd_reload,
9190 .flags = GENL_ADMIN_PERM,
9193 .cmd = DEVLINK_CMD_PARAM_GET,
9194 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9195 .doit = devlink_nl_cmd_param_get_doit,
9196 .dumpit = devlink_nl_instance_iter_dump,
9197 /* can be retrieved by unprivileged users */
9200 .cmd = DEVLINK_CMD_PARAM_SET,
9201 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9202 .doit = devlink_nl_cmd_param_set_doit,
9203 .flags = GENL_ADMIN_PERM,
9206 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9207 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9208 .doit = devlink_nl_cmd_port_param_get_doit,
9209 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9210 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9211 /* can be retrieved by unprivileged users */
9214 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9215 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9216 .doit = devlink_nl_cmd_port_param_set_doit,
9217 .flags = GENL_ADMIN_PERM,
9218 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9221 .cmd = DEVLINK_CMD_REGION_GET,
9222 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9223 .doit = devlink_nl_cmd_region_get_doit,
9224 .dumpit = devlink_nl_instance_iter_dump,
9225 .flags = GENL_ADMIN_PERM,
9228 .cmd = DEVLINK_CMD_REGION_NEW,
9229 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9230 .doit = devlink_nl_cmd_region_new,
9231 .flags = GENL_ADMIN_PERM,
9234 .cmd = DEVLINK_CMD_REGION_DEL,
9235 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9236 .doit = devlink_nl_cmd_region_del,
9237 .flags = GENL_ADMIN_PERM,
9240 .cmd = DEVLINK_CMD_REGION_READ,
9241 .validate = GENL_DONT_VALIDATE_STRICT |
9242 GENL_DONT_VALIDATE_DUMP_STRICT,
9243 .dumpit = devlink_nl_cmd_region_read_dumpit,
9244 .flags = GENL_ADMIN_PERM,
9247 .cmd = DEVLINK_CMD_INFO_GET,
9248 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9249 .doit = devlink_nl_cmd_info_get_doit,
9250 .dumpit = devlink_nl_instance_iter_dump,
9251 /* can be retrieved by unprivileged users */
9254 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9255 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9256 .doit = devlink_nl_cmd_health_reporter_get_doit,
9257 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9258 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9259 /* can be retrieved by unprivileged users */
9262 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9263 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9264 .doit = devlink_nl_cmd_health_reporter_set_doit,
9265 .flags = GENL_ADMIN_PERM,
9266 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9269 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9270 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9271 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9272 .flags = GENL_ADMIN_PERM,
9273 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9276 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9277 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9278 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9279 .flags = GENL_ADMIN_PERM,
9280 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9283 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9284 .validate = GENL_DONT_VALIDATE_STRICT |
9285 GENL_DONT_VALIDATE_DUMP_STRICT,
9286 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9287 .flags = GENL_ADMIN_PERM,
9290 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9291 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9292 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9293 .flags = GENL_ADMIN_PERM,
9294 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9297 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9298 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9299 .doit = devlink_nl_cmd_health_reporter_test_doit,
9300 .flags = GENL_ADMIN_PERM,
9301 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9304 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9305 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9306 .doit = devlink_nl_cmd_flash_update,
9307 .flags = GENL_ADMIN_PERM,
9310 .cmd = DEVLINK_CMD_TRAP_GET,
9311 .doit = devlink_nl_cmd_trap_get_doit,
9312 .dumpit = devlink_nl_instance_iter_dump,
9313 /* can be retrieved by unprivileged users */
9316 .cmd = DEVLINK_CMD_TRAP_SET,
9317 .doit = devlink_nl_cmd_trap_set_doit,
9318 .flags = GENL_ADMIN_PERM,
9321 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9322 .doit = devlink_nl_cmd_trap_group_get_doit,
9323 .dumpit = devlink_nl_instance_iter_dump,
9324 /* can be retrieved by unprivileged users */
9327 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9328 .doit = devlink_nl_cmd_trap_group_set_doit,
9329 .flags = GENL_ADMIN_PERM,
9332 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9333 .doit = devlink_nl_cmd_trap_policer_get_doit,
9334 .dumpit = devlink_nl_instance_iter_dump,
9335 /* can be retrieved by unprivileged users */
9338 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9339 .doit = devlink_nl_cmd_trap_policer_set_doit,
9340 .flags = GENL_ADMIN_PERM,
9343 .cmd = DEVLINK_CMD_SELFTESTS_GET,
9344 .doit = devlink_nl_cmd_selftests_get_doit,
9345 .dumpit = devlink_nl_instance_iter_dump,
9346 /* can be retrieved by unprivileged users */
9349 .cmd = DEVLINK_CMD_SELFTESTS_RUN,
9350 .doit = devlink_nl_cmd_selftests_run,
9351 .flags = GENL_ADMIN_PERM,
9353 /* -- No new ops here! Use split ops going forward! -- */
9356 bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9358 const struct devlink_reload_combination *comb;
9361 if (!devlink_reload_supported(ops)) {
9362 if (WARN_ON(ops->reload_actions))
9367 if (WARN_ON(!ops->reload_actions ||
9368 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9369 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9372 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9373 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9376 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
9377 comb = &devlink_reload_invalid_combinations[i];
9378 if (ops->reload_actions == BIT(comb->action) &&
9379 ops->reload_limits == BIT(comb->limit))
9386 devlink_trap_policer_notify(struct devlink *devlink,
9387 const struct devlink_trap_policer_item *policer_item,
9388 enum devlink_command cmd);
9390 devlink_trap_group_notify(struct devlink *devlink,
9391 const struct devlink_trap_group_item *group_item,
9392 enum devlink_command cmd);
9393 static void devlink_trap_notify(struct devlink *devlink,
9394 const struct devlink_trap_item *trap_item,
9395 enum devlink_command cmd);
9397 void devlink_notify_register(struct devlink *devlink)
9399 struct devlink_trap_policer_item *policer_item;
9400 struct devlink_trap_group_item *group_item;
9401 struct devlink_param_item *param_item;
9402 struct devlink_trap_item *trap_item;
9403 struct devlink_port *devlink_port;
9404 struct devlink_linecard *linecard;
9405 struct devlink_rate *rate_node;
9406 struct devlink_region *region;
9407 unsigned long port_index;
9409 devlink_notify(devlink, DEVLINK_CMD_NEW);
9410 list_for_each_entry(linecard, &devlink->linecard_list, list)
9411 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9413 xa_for_each(&devlink->ports, port_index, devlink_port)
9414 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9416 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9417 devlink_trap_policer_notify(devlink, policer_item,
9418 DEVLINK_CMD_TRAP_POLICER_NEW);
9420 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9421 devlink_trap_group_notify(devlink, group_item,
9422 DEVLINK_CMD_TRAP_GROUP_NEW);
9424 list_for_each_entry(trap_item, &devlink->trap_list, list)
9425 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9427 list_for_each_entry(rate_node, &devlink->rate_list, list)
9428 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9430 list_for_each_entry(region, &devlink->region_list, list)
9431 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9433 list_for_each_entry(param_item, &devlink->param_list, list)
9434 devlink_param_notify(devlink, 0, param_item,
9435 DEVLINK_CMD_PARAM_NEW);
9438 void devlink_notify_unregister(struct devlink *devlink)
9440 struct devlink_trap_policer_item *policer_item;
9441 struct devlink_trap_group_item *group_item;
9442 struct devlink_param_item *param_item;
9443 struct devlink_trap_item *trap_item;
9444 struct devlink_port *devlink_port;
9445 struct devlink_rate *rate_node;
9446 struct devlink_region *region;
9447 unsigned long port_index;
9449 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9450 devlink_param_notify(devlink, 0, param_item,
9451 DEVLINK_CMD_PARAM_DEL);
9453 list_for_each_entry_reverse(region, &devlink->region_list, list)
9454 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9456 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9457 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9459 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9460 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9462 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9463 devlink_trap_group_notify(devlink, group_item,
9464 DEVLINK_CMD_TRAP_GROUP_DEL);
9465 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9467 devlink_trap_policer_notify(devlink, policer_item,
9468 DEVLINK_CMD_TRAP_POLICER_DEL);
9470 xa_for_each(&devlink->ports, port_index, devlink_port)
9471 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9472 devlink_notify(devlink, DEVLINK_CMD_DEL);
9475 static void devlink_port_type_warn(struct work_struct *work)
9477 WARN(true, "Type was not set for devlink port.");
9480 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9482 /* Ignore CPU and DSA flavours. */
9483 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9484 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9485 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9488 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9490 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9492 if (!devlink_port_type_should_warn(devlink_port))
9494 /* Schedule a work to WARN in case driver does not set port
9495 * type within timeout.
9497 schedule_delayed_work(&devlink_port->type_warn_dw,
9498 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9501 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9503 if (!devlink_port_type_should_warn(devlink_port))
9505 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9509 * devlink_port_init() - Init devlink port
9512 * @devlink_port: devlink port
9514 * Initialize essencial stuff that is needed for functions
9515 * that may be called before devlink port registration.
9516 * Call to this function is optional and not needed
9517 * in case the driver does not use such functions.
9519 void devlink_port_init(struct devlink *devlink,
9520 struct devlink_port *devlink_port)
9522 if (devlink_port->initialized)
9524 devlink_port->devlink = devlink;
9525 INIT_LIST_HEAD(&devlink_port->region_list);
9526 devlink_port->initialized = true;
9528 EXPORT_SYMBOL_GPL(devlink_port_init);
9531 * devlink_port_fini() - Deinitialize devlink port
9533 * @devlink_port: devlink port
9535 * Deinitialize essencial stuff that is in use for functions
9536 * that may be called after devlink port unregistration.
9537 * Call to this function is optional and not needed
9538 * in case the driver does not use such functions.
9540 void devlink_port_fini(struct devlink_port *devlink_port)
9542 WARN_ON(!list_empty(&devlink_port->region_list));
9544 EXPORT_SYMBOL_GPL(devlink_port_fini);
9547 * devl_port_register() - Register devlink port
9550 * @devlink_port: devlink port
9551 * @port_index: driver-specific numerical identifier of the port
9553 * Register devlink port with provided port index. User can use
9554 * any indexing, even hw-related one. devlink_port structure
9555 * is convenient to be embedded inside user driver private structure.
9556 * Note that the caller should take care of zeroing the devlink_port
9559 int devl_port_register(struct devlink *devlink,
9560 struct devlink_port *devlink_port,
9561 unsigned int port_index)
9565 devl_assert_locked(devlink);
9567 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9569 devlink_port_init(devlink, devlink_port);
9570 devlink_port->registered = true;
9571 devlink_port->index = port_index;
9572 spin_lock_init(&devlink_port->type_lock);
9573 INIT_LIST_HEAD(&devlink_port->reporter_list);
9574 err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
9578 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9579 devlink_port_type_warn_schedule(devlink_port);
9580 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9583 EXPORT_SYMBOL_GPL(devl_port_register);
9586 * devlink_port_register - Register devlink port
9589 * @devlink_port: devlink port
9590 * @port_index: driver-specific numerical identifier of the port
9592 * Register devlink port with provided port index. User can use
9593 * any indexing, even hw-related one. devlink_port structure
9594 * is convenient to be embedded inside user driver private structure.
9595 * Note that the caller should take care of zeroing the devlink_port
9598 * Context: Takes and release devlink->lock <mutex>.
9600 int devlink_port_register(struct devlink *devlink,
9601 struct devlink_port *devlink_port,
9602 unsigned int port_index)
9607 err = devl_port_register(devlink, devlink_port, port_index);
9608 devl_unlock(devlink);
9611 EXPORT_SYMBOL_GPL(devlink_port_register);
9614 * devl_port_unregister() - Unregister devlink port
9616 * @devlink_port: devlink port
9618 void devl_port_unregister(struct devlink_port *devlink_port)
9620 lockdep_assert_held(&devlink_port->devlink->lock);
9621 WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
9623 devlink_port_type_warn_cancel(devlink_port);
9624 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9625 xa_erase(&devlink_port->devlink->ports, devlink_port->index);
9626 WARN_ON(!list_empty(&devlink_port->reporter_list));
9627 devlink_port->registered = false;
9629 EXPORT_SYMBOL_GPL(devl_port_unregister);
9632 * devlink_port_unregister - Unregister devlink port
9634 * @devlink_port: devlink port
9636 * Context: Takes and release devlink->lock <mutex>.
9638 void devlink_port_unregister(struct devlink_port *devlink_port)
9640 struct devlink *devlink = devlink_port->devlink;
9643 devl_port_unregister(devlink_port);
9644 devl_unlock(devlink);
9646 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9648 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9649 struct net_device *netdev)
9651 const struct net_device_ops *ops = netdev->netdev_ops;
9653 /* If driver registers devlink port, it should set devlink port
9654 * attributes accordingly so the compat functions are called
9655 * and the original ops are not used.
9657 if (ops->ndo_get_phys_port_name) {
9658 /* Some drivers use the same set of ndos for netdevs
9659 * that have devlink_port registered and also for
9660 * those who don't. Make sure that ndo_get_phys_port_name
9661 * returns -EOPNOTSUPP here in case it is defined.
9664 char name[IFNAMSIZ];
9667 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9668 WARN_ON(err != -EOPNOTSUPP);
9670 if (ops->ndo_get_port_parent_id) {
9671 /* Some drivers use the same set of ndos for netdevs
9672 * that have devlink_port registered and also for
9673 * those who don't. Make sure that ndo_get_port_parent_id
9674 * returns -EOPNOTSUPP here in case it is defined.
9677 struct netdev_phys_item_id ppid;
9680 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9681 WARN_ON(err != -EOPNOTSUPP);
9685 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9686 enum devlink_port_type type,
9689 struct net_device *netdev = type_dev;
9691 ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
9693 if (type == DEVLINK_PORT_TYPE_NOTSET) {
9694 devlink_port_type_warn_schedule(devlink_port);
9696 devlink_port_type_warn_cancel(devlink_port);
9697 if (type == DEVLINK_PORT_TYPE_ETH && netdev)
9698 devlink_port_type_netdev_checks(devlink_port, netdev);
9701 spin_lock_bh(&devlink_port->type_lock);
9702 devlink_port->type = type;
9704 case DEVLINK_PORT_TYPE_ETH:
9705 devlink_port->type_eth.netdev = netdev;
9708 devlink_port->type_eth.ifindex = netdev->ifindex;
9709 BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
9710 sizeof(netdev->name));
9711 strcpy(devlink_port->type_eth.ifname, netdev->name);
9714 case DEVLINK_PORT_TYPE_IB:
9715 devlink_port->type_ib.ibdev = type_dev;
9720 spin_unlock_bh(&devlink_port->type_lock);
9721 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9725 * devlink_port_type_eth_set - Set port type to Ethernet
9727 * @devlink_port: devlink port
9729 * If driver is calling this, most likely it is doing something wrong.
9731 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
9733 dev_warn(devlink_port->devlink->dev,
9734 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9735 devlink_port->index);
9736 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
9738 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9741 * devlink_port_type_ib_set - Set port type to InfiniBand
9743 * @devlink_port: devlink port
9744 * @ibdev: related IB device
9746 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9747 struct ib_device *ibdev)
9749 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9751 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9754 * devlink_port_type_clear - Clear port type
9756 * @devlink_port: devlink port
9758 * If driver is calling this for clearing Ethernet type, most likely
9759 * it is doing something wrong.
9761 void devlink_port_type_clear(struct devlink_port *devlink_port)
9763 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9764 dev_warn(devlink_port->devlink->dev,
9765 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
9766 devlink_port->index);
9767 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9769 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9771 int devlink_port_netdevice_event(struct notifier_block *nb,
9772 unsigned long event, void *ptr)
9774 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
9775 struct devlink_port *devlink_port = netdev->devlink_port;
9776 struct devlink *devlink;
9778 devlink = container_of(nb, struct devlink, netdevice_nb);
9780 if (!devlink_port || devlink_port->devlink != devlink)
9784 case NETDEV_POST_INIT:
9785 /* Set the type but not netdev pointer. It is going to be set
9786 * later on by NETDEV_REGISTER event. Happens once during
9787 * netdevice register
9789 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
9792 case NETDEV_REGISTER:
9793 case NETDEV_CHANGENAME:
9794 /* Set the netdev on top of previously set type. Note this
9795 * event happens also during net namespace change so here
9796 * we take into account netdev pointer appearing in this
9799 __devlink_port_type_set(devlink_port, devlink_port->type,
9802 case NETDEV_UNREGISTER:
9803 /* Clear netdev pointer, but not the type. This event happens
9804 * also during net namespace change so we need to clear
9805 * pointer to netdev that is going to another net namespace.
9807 __devlink_port_type_set(devlink_port, devlink_port->type,
9810 case NETDEV_PRE_UNINIT:
9811 /* Clear the type and the netdev pointer. Happens one during
9812 * netdevice unregister.
9814 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
9822 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9823 enum devlink_port_flavour flavour)
9825 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9827 devlink_port->attrs_set = true;
9828 attrs->flavour = flavour;
9829 if (attrs->switch_id.id_len) {
9830 devlink_port->switch_port = true;
9831 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9832 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9834 devlink_port->switch_port = false;
9840 * devlink_port_attrs_set - Set port attributes
9842 * @devlink_port: devlink port
9843 * @attrs: devlink port attrs
9845 void devlink_port_attrs_set(struct devlink_port *devlink_port,
9846 struct devlink_port_attrs *attrs)
9850 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9852 devlink_port->attrs = *attrs;
9853 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
9856 WARN_ON(attrs->splittable && attrs->split);
9858 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
9861 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
9863 * @devlink_port: devlink port
9864 * @controller: associated controller number for the devlink port instance
9865 * @pf: associated PF for the devlink port instance
9866 * @external: indicates if the port is for an external controller
9868 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
9869 u16 pf, bool external)
9871 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9874 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9876 ret = __devlink_port_attrs_set(devlink_port,
9877 DEVLINK_PORT_FLAVOUR_PCI_PF);
9880 attrs->pci_pf.controller = controller;
9881 attrs->pci_pf.pf = pf;
9882 attrs->pci_pf.external = external;
9884 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
9887 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
9889 * @devlink_port: devlink port
9890 * @controller: associated controller number for the devlink port instance
9891 * @pf: associated PF for the devlink port instance
9892 * @vf: associated VF of a PF for the devlink port instance
9893 * @external: indicates if the port is for an external controller
9895 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
9896 u16 pf, u16 vf, bool external)
9898 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9901 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9903 ret = __devlink_port_attrs_set(devlink_port,
9904 DEVLINK_PORT_FLAVOUR_PCI_VF);
9907 attrs->pci_vf.controller = controller;
9908 attrs->pci_vf.pf = pf;
9909 attrs->pci_vf.vf = vf;
9910 attrs->pci_vf.external = external;
9912 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
9915 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
9917 * @devlink_port: devlink port
9918 * @controller: associated controller number for the devlink port instance
9919 * @pf: associated PF for the devlink port instance
9920 * @sf: associated SF of a PF for the devlink port instance
9921 * @external: indicates if the port is for an external controller
9923 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
9924 u16 pf, u32 sf, bool external)
9926 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9929 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9931 ret = __devlink_port_attrs_set(devlink_port,
9932 DEVLINK_PORT_FLAVOUR_PCI_SF);
9935 attrs->pci_sf.controller = controller;
9936 attrs->pci_sf.pf = pf;
9937 attrs->pci_sf.sf = sf;
9938 attrs->pci_sf.external = external;
9940 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
9943 * devl_rate_node_create - create devlink rate node
9944 * @devlink: devlink instance
9945 * @priv: driver private data
9946 * @node_name: name of the resulting node
9947 * @parent: parent devlink_rate struct
9949 * Create devlink rate object of type node
9951 struct devlink_rate *
9952 devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name,
9953 struct devlink_rate *parent)
9955 struct devlink_rate *rate_node;
9957 rate_node = devlink_rate_node_get_by_name(devlink, node_name);
9958 if (!IS_ERR(rate_node))
9959 return ERR_PTR(-EEXIST);
9961 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
9963 return ERR_PTR(-ENOMEM);
9966 rate_node->parent = parent;
9967 refcount_inc(&rate_node->parent->refcnt);
9970 rate_node->type = DEVLINK_RATE_TYPE_NODE;
9971 rate_node->devlink = devlink;
9972 rate_node->priv = priv;
9974 rate_node->name = kstrdup(node_name, GFP_KERNEL);
9975 if (!rate_node->name) {
9977 return ERR_PTR(-ENOMEM);
9980 refcount_set(&rate_node->refcnt, 1);
9981 list_add(&rate_node->list, &devlink->rate_list);
9982 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9985 EXPORT_SYMBOL_GPL(devl_rate_node_create);
9988 * devl_rate_leaf_create - create devlink rate leaf
9989 * @devlink_port: devlink port object to create rate object on
9990 * @priv: driver private data
9991 * @parent: parent devlink_rate struct
9993 * Create devlink rate object of type leaf on provided @devlink_port.
9995 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv,
9996 struct devlink_rate *parent)
9998 struct devlink *devlink = devlink_port->devlink;
9999 struct devlink_rate *devlink_rate;
10001 devl_assert_locked(devlink_port->devlink);
10003 if (WARN_ON(devlink_port->devlink_rate))
10006 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
10011 devlink_rate->parent = parent;
10012 refcount_inc(&devlink_rate->parent->refcnt);
10015 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
10016 devlink_rate->devlink = devlink;
10017 devlink_rate->devlink_port = devlink_port;
10018 devlink_rate->priv = priv;
10019 list_add_tail(&devlink_rate->list, &devlink->rate_list);
10020 devlink_port->devlink_rate = devlink_rate;
10021 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10025 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10028 * devl_rate_leaf_destroy - destroy devlink rate leaf
10030 * @devlink_port: devlink port linked to the rate object
10032 * Destroy the devlink rate object of type leaf on provided @devlink_port.
10034 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10036 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10038 devl_assert_locked(devlink_port->devlink);
10042 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10043 if (devlink_rate->parent)
10044 refcount_dec(&devlink_rate->parent->refcnt);
10045 list_del(&devlink_rate->list);
10046 devlink_port->devlink_rate = NULL;
10047 kfree(devlink_rate);
10049 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10052 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10053 * @devlink: devlink instance
10055 * Unset parent for all rate objects and destroy all rate nodes
10056 * on specified device.
10058 void devl_rate_nodes_destroy(struct devlink *devlink)
10060 static struct devlink_rate *devlink_rate, *tmp;
10061 const struct devlink_ops *ops = devlink->ops;
10063 devl_assert_locked(devlink);
10065 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10066 if (!devlink_rate->parent)
10069 refcount_dec(&devlink_rate->parent->refcnt);
10070 if (devlink_rate_is_leaf(devlink_rate))
10071 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10073 else if (devlink_rate_is_node(devlink_rate))
10074 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10077 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10078 if (devlink_rate_is_node(devlink_rate)) {
10079 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10080 list_del(&devlink_rate->list);
10081 kfree(devlink_rate->name);
10082 kfree(devlink_rate);
10086 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10089 * devlink_port_linecard_set - Link port with a linecard
10091 * @devlink_port: devlink port
10092 * @linecard: devlink linecard
10094 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10095 struct devlink_linecard *linecard)
10097 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10099 devlink_port->linecard = linecard;
10101 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10103 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10104 char *name, size_t len)
10106 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10109 if (!devlink_port->attrs_set)
10110 return -EOPNOTSUPP;
10112 switch (attrs->flavour) {
10113 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10114 if (devlink_port->linecard)
10115 n = snprintf(name, len, "l%u",
10116 devlink_port->linecard->index);
10118 n += snprintf(name + n, len - n, "p%u",
10119 attrs->phys.port_number);
10120 if (n < len && attrs->split)
10121 n += snprintf(name + n, len - n, "s%u",
10122 attrs->phys.split_subport_number);
10124 case DEVLINK_PORT_FLAVOUR_CPU:
10125 case DEVLINK_PORT_FLAVOUR_DSA:
10126 case DEVLINK_PORT_FLAVOUR_UNUSED:
10127 /* As CPU and DSA ports do not have a netdevice associated
10128 * case should not ever happen.
10132 case DEVLINK_PORT_FLAVOUR_PCI_PF:
10133 if (attrs->pci_pf.external) {
10134 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10140 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10142 case DEVLINK_PORT_FLAVOUR_PCI_VF:
10143 if (attrs->pci_vf.external) {
10144 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10150 n = snprintf(name, len, "pf%uvf%u",
10151 attrs->pci_vf.pf, attrs->pci_vf.vf);
10153 case DEVLINK_PORT_FLAVOUR_PCI_SF:
10154 if (attrs->pci_sf.external) {
10155 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10161 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10164 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10165 return -EOPNOTSUPP;
10174 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10176 struct devlink_linecard_type *linecard_type;
10177 unsigned int count;
10180 count = linecard->ops->types_count(linecard, linecard->priv);
10181 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10183 if (!linecard->types)
10185 linecard->types_count = count;
10187 for (i = 0; i < count; i++) {
10188 linecard_type = &linecard->types[i];
10189 linecard->ops->types_get(linecard, linecard->priv, i,
10190 &linecard_type->type,
10191 &linecard_type->priv);
10196 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10198 kfree(linecard->types);
10202 * devl_linecard_create - Create devlink linecard
10204 * @devlink: devlink
10205 * @linecard_index: driver-specific numerical identifier of the linecard
10206 * @ops: linecards ops
10207 * @priv: user priv pointer
10209 * Create devlink linecard instance with provided linecard index.
10210 * Caller can use any indexing, even hw-related one.
10212 * Return: Line card structure or an ERR_PTR() encoded error code.
10214 struct devlink_linecard *
10215 devl_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10216 const struct devlink_linecard_ops *ops, void *priv)
10218 struct devlink_linecard *linecard;
10221 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10222 !ops->types_count || !ops->types_get))
10223 return ERR_PTR(-EINVAL);
10225 if (devlink_linecard_index_exists(devlink, linecard_index))
10226 return ERR_PTR(-EEXIST);
10228 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10230 return ERR_PTR(-ENOMEM);
10232 linecard->devlink = devlink;
10233 linecard->index = linecard_index;
10234 linecard->ops = ops;
10235 linecard->priv = priv;
10236 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10237 mutex_init(&linecard->state_lock);
10239 err = devlink_linecard_types_init(linecard);
10241 mutex_destroy(&linecard->state_lock);
10243 return ERR_PTR(err);
10246 list_add_tail(&linecard->list, &devlink->linecard_list);
10247 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10250 EXPORT_SYMBOL_GPL(devl_linecard_create);
10253 * devl_linecard_destroy - Destroy devlink linecard
10255 * @linecard: devlink linecard
10257 void devl_linecard_destroy(struct devlink_linecard *linecard)
10259 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10260 list_del(&linecard->list);
10261 devlink_linecard_types_fini(linecard);
10262 mutex_destroy(&linecard->state_lock);
10265 EXPORT_SYMBOL_GPL(devl_linecard_destroy);
10268 * devlink_linecard_provision_set - Set provisioning on linecard
10270 * @linecard: devlink linecard
10271 * @type: linecard type
10273 * This is either called directly from the provision() op call or
10274 * as a result of the provision() op call asynchronously.
10276 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10279 mutex_lock(&linecard->state_lock);
10280 WARN_ON(linecard->type && strcmp(linecard->type, type));
10281 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10282 linecard->type = type;
10283 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10284 mutex_unlock(&linecard->state_lock);
10286 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10289 * devlink_linecard_provision_clear - Clear provisioning on linecard
10291 * @linecard: devlink linecard
10293 * This is either called directly from the unprovision() op call or
10294 * as a result of the unprovision() op call asynchronously.
10296 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10298 mutex_lock(&linecard->state_lock);
10299 WARN_ON(linecard->nested_devlink);
10300 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10301 linecard->type = NULL;
10302 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10303 mutex_unlock(&linecard->state_lock);
10305 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10308 * devlink_linecard_provision_fail - Fail provisioning on linecard
10310 * @linecard: devlink linecard
10312 * This is either called directly from the provision() op call or
10313 * as a result of the provision() op call asynchronously.
10315 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10317 mutex_lock(&linecard->state_lock);
10318 WARN_ON(linecard->nested_devlink);
10319 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10320 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10321 mutex_unlock(&linecard->state_lock);
10323 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10326 * devlink_linecard_activate - Set linecard active
10328 * @linecard: devlink linecard
10330 void devlink_linecard_activate(struct devlink_linecard *linecard)
10332 mutex_lock(&linecard->state_lock);
10333 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10334 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10335 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10336 mutex_unlock(&linecard->state_lock);
10338 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10341 * devlink_linecard_deactivate - Set linecard inactive
10343 * @linecard: devlink linecard
10345 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10347 mutex_lock(&linecard->state_lock);
10348 switch (linecard->state) {
10349 case DEVLINK_LINECARD_STATE_ACTIVE:
10350 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10351 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10353 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10354 /* Line card is being deactivated as part
10355 * of unprovisioning flow.
10362 mutex_unlock(&linecard->state_lock);
10364 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10367 * devlink_linecard_nested_dl_set - Attach/detach nested devlink
10368 * instance to linecard.
10370 * @linecard: devlink linecard
10371 * @nested_devlink: devlink instance to attach or NULL to detach
10373 void devlink_linecard_nested_dl_set(struct devlink_linecard *linecard,
10374 struct devlink *nested_devlink)
10376 mutex_lock(&linecard->state_lock);
10377 linecard->nested_devlink = nested_devlink;
10378 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10379 mutex_unlock(&linecard->state_lock);
10381 EXPORT_SYMBOL_GPL(devlink_linecard_nested_dl_set);
10383 int devl_sb_register(struct devlink *devlink, unsigned int sb_index,
10384 u32 size, u16 ingress_pools_count,
10385 u16 egress_pools_count, u16 ingress_tc_count,
10386 u16 egress_tc_count)
10388 struct devlink_sb *devlink_sb;
10390 lockdep_assert_held(&devlink->lock);
10392 if (devlink_sb_index_exists(devlink, sb_index))
10395 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10398 devlink_sb->index = sb_index;
10399 devlink_sb->size = size;
10400 devlink_sb->ingress_pools_count = ingress_pools_count;
10401 devlink_sb->egress_pools_count = egress_pools_count;
10402 devlink_sb->ingress_tc_count = ingress_tc_count;
10403 devlink_sb->egress_tc_count = egress_tc_count;
10404 list_add_tail(&devlink_sb->list, &devlink->sb_list);
10407 EXPORT_SYMBOL_GPL(devl_sb_register);
10409 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10410 u32 size, u16 ingress_pools_count,
10411 u16 egress_pools_count, u16 ingress_tc_count,
10412 u16 egress_tc_count)
10416 devl_lock(devlink);
10417 err = devl_sb_register(devlink, sb_index, size, ingress_pools_count,
10418 egress_pools_count, ingress_tc_count,
10420 devl_unlock(devlink);
10423 EXPORT_SYMBOL_GPL(devlink_sb_register);
10425 void devl_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10427 struct devlink_sb *devlink_sb;
10429 lockdep_assert_held(&devlink->lock);
10431 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10432 WARN_ON(!devlink_sb);
10433 list_del(&devlink_sb->list);
10436 EXPORT_SYMBOL_GPL(devl_sb_unregister);
10438 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10440 devl_lock(devlink);
10441 devl_sb_unregister(devlink, sb_index);
10442 devl_unlock(devlink);
10444 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10447 * devl_dpipe_headers_register - register dpipe headers
10449 * @devlink: devlink
10450 * @dpipe_headers: dpipe header array
10452 * Register the headers supported by hardware.
10454 void devl_dpipe_headers_register(struct devlink *devlink,
10455 struct devlink_dpipe_headers *dpipe_headers)
10457 lockdep_assert_held(&devlink->lock);
10459 devlink->dpipe_headers = dpipe_headers;
10461 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
10464 * devl_dpipe_headers_unregister - unregister dpipe headers
10466 * @devlink: devlink
10468 * Unregister the headers supported by hardware.
10470 void devl_dpipe_headers_unregister(struct devlink *devlink)
10472 lockdep_assert_held(&devlink->lock);
10474 devlink->dpipe_headers = NULL;
10476 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
10479 * devlink_dpipe_table_counter_enabled - check if counter allocation
10481 * @devlink: devlink
10482 * @table_name: tables name
10484 * Used by driver to check if counter allocation is required.
10485 * After counter allocation is turned on the table entries
10486 * are updated to include counter statistics.
10488 * After that point on the driver must respect the counter
10489 * state so that each entry added to the table is added
10492 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10493 const char *table_name)
10495 struct devlink_dpipe_table *table;
10499 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10500 table_name, devlink);
10503 enabled = table->counters_enabled;
10507 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10510 * devl_dpipe_table_register - register dpipe table
10512 * @devlink: devlink
10513 * @table_name: table name
10514 * @table_ops: table ops
10516 * @counter_control_extern: external control for counters
10518 int devl_dpipe_table_register(struct devlink *devlink,
10519 const char *table_name,
10520 struct devlink_dpipe_table_ops *table_ops,
10521 void *priv, bool counter_control_extern)
10523 struct devlink_dpipe_table *table;
10525 lockdep_assert_held(&devlink->lock);
10527 if (WARN_ON(!table_ops->size_get))
10530 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10534 table = kzalloc(sizeof(*table), GFP_KERNEL);
10538 table->name = table_name;
10539 table->table_ops = table_ops;
10540 table->priv = priv;
10541 table->counter_control_extern = counter_control_extern;
10543 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10547 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
10550 * devl_dpipe_table_unregister - unregister dpipe table
10552 * @devlink: devlink
10553 * @table_name: table name
10555 void devl_dpipe_table_unregister(struct devlink *devlink,
10556 const char *table_name)
10558 struct devlink_dpipe_table *table;
10560 lockdep_assert_held(&devlink->lock);
10562 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10563 table_name, devlink);
10566 list_del_rcu(&table->list);
10567 kfree_rcu(table, rcu);
10569 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
10572 * devl_resource_register - devlink resource register
10574 * @devlink: devlink
10575 * @resource_name: resource's name
10576 * @resource_size: resource's size
10577 * @resource_id: resource's id
10578 * @parent_resource_id: resource's parent id
10579 * @size_params: size parameters
10581 * Generic resources should reuse the same names across drivers.
10582 * Please see the generic resources list at:
10583 * Documentation/networking/devlink/devlink-resource.rst
10585 int devl_resource_register(struct devlink *devlink,
10586 const char *resource_name,
10589 u64 parent_resource_id,
10590 const struct devlink_resource_size_params *size_params)
10592 struct devlink_resource *resource;
10593 struct list_head *resource_list;
10594 bool top_hierarchy;
10596 lockdep_assert_held(&devlink->lock);
10598 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10600 resource = devlink_resource_find(devlink, NULL, resource_id);
10604 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10608 if (top_hierarchy) {
10609 resource_list = &devlink->resource_list;
10611 struct devlink_resource *parent_resource;
10613 parent_resource = devlink_resource_find(devlink, NULL,
10614 parent_resource_id);
10615 if (parent_resource) {
10616 resource_list = &parent_resource->resource_list;
10617 resource->parent = parent_resource;
10624 resource->name = resource_name;
10625 resource->size = resource_size;
10626 resource->size_new = resource_size;
10627 resource->id = resource_id;
10628 resource->size_valid = true;
10629 memcpy(&resource->size_params, size_params,
10630 sizeof(resource->size_params));
10631 INIT_LIST_HEAD(&resource->resource_list);
10632 list_add_tail(&resource->list, resource_list);
10636 EXPORT_SYMBOL_GPL(devl_resource_register);
10639 * devlink_resource_register - devlink resource register
10641 * @devlink: devlink
10642 * @resource_name: resource's name
10643 * @resource_size: resource's size
10644 * @resource_id: resource's id
10645 * @parent_resource_id: resource's parent id
10646 * @size_params: size parameters
10648 * Generic resources should reuse the same names across drivers.
10649 * Please see the generic resources list at:
10650 * Documentation/networking/devlink/devlink-resource.rst
10652 * Context: Takes and release devlink->lock <mutex>.
10654 int devlink_resource_register(struct devlink *devlink,
10655 const char *resource_name,
10658 u64 parent_resource_id,
10659 const struct devlink_resource_size_params *size_params)
10663 devl_lock(devlink);
10664 err = devl_resource_register(devlink, resource_name, resource_size,
10665 resource_id, parent_resource_id, size_params);
10666 devl_unlock(devlink);
10669 EXPORT_SYMBOL_GPL(devlink_resource_register);
10671 static void devlink_resource_unregister(struct devlink *devlink,
10672 struct devlink_resource *resource)
10674 struct devlink_resource *tmp, *child_resource;
10676 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10678 devlink_resource_unregister(devlink, child_resource);
10679 list_del(&child_resource->list);
10680 kfree(child_resource);
10685 * devl_resources_unregister - free all resources
10687 * @devlink: devlink
10689 void devl_resources_unregister(struct devlink *devlink)
10691 struct devlink_resource *tmp, *child_resource;
10693 lockdep_assert_held(&devlink->lock);
10695 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10697 devlink_resource_unregister(devlink, child_resource);
10698 list_del(&child_resource->list);
10699 kfree(child_resource);
10702 EXPORT_SYMBOL_GPL(devl_resources_unregister);
10705 * devlink_resources_unregister - free all resources
10707 * @devlink: devlink
10709 * Context: Takes and release devlink->lock <mutex>.
10711 void devlink_resources_unregister(struct devlink *devlink)
10713 devl_lock(devlink);
10714 devl_resources_unregister(devlink);
10715 devl_unlock(devlink);
10717 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10720 * devl_resource_size_get - get and update size
10722 * @devlink: devlink
10723 * @resource_id: the requested resource id
10724 * @p_resource_size: ptr to update
10726 int devl_resource_size_get(struct devlink *devlink,
10728 u64 *p_resource_size)
10730 struct devlink_resource *resource;
10732 lockdep_assert_held(&devlink->lock);
10734 resource = devlink_resource_find(devlink, NULL, resource_id);
10737 *p_resource_size = resource->size_new;
10738 resource->size = resource->size_new;
10741 EXPORT_SYMBOL_GPL(devl_resource_size_get);
10744 * devl_dpipe_table_resource_set - set the resource id
10746 * @devlink: devlink
10747 * @table_name: table name
10748 * @resource_id: resource id
10749 * @resource_units: number of resource's units consumed per table's entry
10751 int devl_dpipe_table_resource_set(struct devlink *devlink,
10752 const char *table_name, u64 resource_id,
10753 u64 resource_units)
10755 struct devlink_dpipe_table *table;
10757 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10758 table_name, devlink);
10762 table->resource_id = resource_id;
10763 table->resource_units = resource_units;
10764 table->resource_valid = true;
10767 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);
10770 * devl_resource_occ_get_register - register occupancy getter
10772 * @devlink: devlink
10773 * @resource_id: resource id
10774 * @occ_get: occupancy getter callback
10775 * @occ_get_priv: occupancy getter callback priv
10777 void devl_resource_occ_get_register(struct devlink *devlink,
10779 devlink_resource_occ_get_t *occ_get,
10780 void *occ_get_priv)
10782 struct devlink_resource *resource;
10784 lockdep_assert_held(&devlink->lock);
10786 resource = devlink_resource_find(devlink, NULL, resource_id);
10787 if (WARN_ON(!resource))
10789 WARN_ON(resource->occ_get);
10791 resource->occ_get = occ_get;
10792 resource->occ_get_priv = occ_get_priv;
10794 EXPORT_SYMBOL_GPL(devl_resource_occ_get_register);
10797 * devlink_resource_occ_get_register - register occupancy getter
10799 * @devlink: devlink
10800 * @resource_id: resource id
10801 * @occ_get: occupancy getter callback
10802 * @occ_get_priv: occupancy getter callback priv
10804 * Context: Takes and release devlink->lock <mutex>.
10806 void devlink_resource_occ_get_register(struct devlink *devlink,
10808 devlink_resource_occ_get_t *occ_get,
10809 void *occ_get_priv)
10811 devl_lock(devlink);
10812 devl_resource_occ_get_register(devlink, resource_id,
10813 occ_get, occ_get_priv);
10814 devl_unlock(devlink);
10816 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10819 * devl_resource_occ_get_unregister - unregister occupancy getter
10821 * @devlink: devlink
10822 * @resource_id: resource id
10824 void devl_resource_occ_get_unregister(struct devlink *devlink,
10827 struct devlink_resource *resource;
10829 lockdep_assert_held(&devlink->lock);
10831 resource = devlink_resource_find(devlink, NULL, resource_id);
10832 if (WARN_ON(!resource))
10834 WARN_ON(!resource->occ_get);
10836 resource->occ_get = NULL;
10837 resource->occ_get_priv = NULL;
10839 EXPORT_SYMBOL_GPL(devl_resource_occ_get_unregister);
10842 * devlink_resource_occ_get_unregister - unregister occupancy getter
10844 * @devlink: devlink
10845 * @resource_id: resource id
10847 * Context: Takes and release devlink->lock <mutex>.
10849 void devlink_resource_occ_get_unregister(struct devlink *devlink,
10852 devl_lock(devlink);
10853 devl_resource_occ_get_unregister(devlink, resource_id);
10854 devl_unlock(devlink);
10856 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
10858 static int devlink_param_verify(const struct devlink_param *param)
10860 if (!param || !param->name || !param->supported_cmodes)
10862 if (param->generic)
10863 return devlink_param_generic_verify(param);
10865 return devlink_param_driver_verify(param);
10869 * devlink_params_register - register configuration parameters
10871 * @devlink: devlink
10872 * @params: configuration parameters array
10873 * @params_count: number of parameters provided
10875 * Register the configuration parameters supported by the driver.
10877 int devlink_params_register(struct devlink *devlink,
10878 const struct devlink_param *params,
10879 size_t params_count)
10881 const struct devlink_param *param = params;
10884 for (i = 0; i < params_count; i++, param++) {
10885 err = devlink_param_register(devlink, param);
10895 for (param--; i > 0; i--, param--)
10896 devlink_param_unregister(devlink, param);
10899 EXPORT_SYMBOL_GPL(devlink_params_register);
10902 * devlink_params_unregister - unregister configuration parameters
10903 * @devlink: devlink
10904 * @params: configuration parameters to unregister
10905 * @params_count: number of parameters provided
10907 void devlink_params_unregister(struct devlink *devlink,
10908 const struct devlink_param *params,
10909 size_t params_count)
10911 const struct devlink_param *param = params;
10914 for (i = 0; i < params_count; i++, param++)
10915 devlink_param_unregister(devlink, param);
10917 EXPORT_SYMBOL_GPL(devlink_params_unregister);
10920 * devlink_param_register - register one configuration parameter
10922 * @devlink: devlink
10923 * @param: one configuration parameter
10925 * Register the configuration parameter supported by the driver.
10926 * Return: returns 0 on successful registration or error code otherwise.
10928 int devlink_param_register(struct devlink *devlink,
10929 const struct devlink_param *param)
10931 struct devlink_param_item *param_item;
10933 WARN_ON(devlink_param_verify(param));
10934 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
10936 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
10937 WARN_ON(param->get || param->set);
10939 WARN_ON(!param->get || !param->set);
10941 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
10945 param_item->param = param;
10947 list_add_tail(¶m_item->list, &devlink->param_list);
10948 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
10951 EXPORT_SYMBOL_GPL(devlink_param_register);
10954 * devlink_param_unregister - unregister one configuration parameter
10955 * @devlink: devlink
10956 * @param: configuration parameter to unregister
10958 void devlink_param_unregister(struct devlink *devlink,
10959 const struct devlink_param *param)
10961 struct devlink_param_item *param_item;
10964 devlink_param_find_by_name(&devlink->param_list, param->name);
10965 WARN_ON(!param_item);
10966 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_DEL);
10967 list_del(¶m_item->list);
10970 EXPORT_SYMBOL_GPL(devlink_param_unregister);
10973 * devlink_param_driverinit_value_get - get configuration parameter
10974 * value for driver initializing
10976 * @devlink: devlink
10977 * @param_id: parameter ID
10978 * @init_val: value of parameter in driverinit configuration mode
10980 * This function should be used by the driver to get driverinit
10981 * configuration for initialization after reload command.
10983 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
10984 union devlink_param_value *init_val)
10986 struct devlink_param_item *param_item;
10988 if (!devlink_reload_supported(devlink->ops))
10989 return -EOPNOTSUPP;
10991 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10995 if (!param_item->driverinit_value_valid ||
10996 !devlink_param_cmode_is_supported(param_item->param,
10997 DEVLINK_PARAM_CMODE_DRIVERINIT))
10998 return -EOPNOTSUPP;
11000 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11001 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
11003 *init_val = param_item->driverinit_value;
11007 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
11010 * devlink_param_driverinit_value_set - set value of configuration
11011 * parameter for driverinit
11012 * configuration mode
11014 * @devlink: devlink
11015 * @param_id: parameter ID
11016 * @init_val: value of parameter to set for driverinit configuration mode
11018 * This function should be used by the driver to set driverinit
11019 * configuration mode default value.
11021 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
11022 union devlink_param_value init_val)
11024 struct devlink_param_item *param_item;
11026 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11030 if (!devlink_param_cmode_is_supported(param_item->param,
11031 DEVLINK_PARAM_CMODE_DRIVERINIT))
11032 return -EOPNOTSUPP;
11034 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11035 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
11037 param_item->driverinit_value = init_val;
11038 param_item->driverinit_value_valid = true;
11040 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11043 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
11046 * devlink_param_value_changed - notify devlink on a parameter's value
11047 * change. Should be called by the driver
11048 * right after the change.
11050 * @devlink: devlink
11051 * @param_id: parameter ID
11053 * This function should be used by the driver to notify devlink on value
11054 * change, excluding driverinit configuration mode.
11055 * For driverinit configuration mode driver should use the function
11057 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11059 struct devlink_param_item *param_item;
11061 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11062 WARN_ON(!param_item);
11064 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11066 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11069 * devl_region_create - create a new address region
11071 * @devlink: devlink
11072 * @ops: region operations and name
11073 * @region_max_snapshots: Maximum supported number of snapshots for region
11074 * @region_size: size of region
11076 struct devlink_region *devl_region_create(struct devlink *devlink,
11077 const struct devlink_region_ops *ops,
11078 u32 region_max_snapshots,
11081 struct devlink_region *region;
11083 devl_assert_locked(devlink);
11085 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11086 return ERR_PTR(-EINVAL);
11088 if (devlink_region_get_by_name(devlink, ops->name))
11089 return ERR_PTR(-EEXIST);
11091 region = kzalloc(sizeof(*region), GFP_KERNEL);
11093 return ERR_PTR(-ENOMEM);
11095 region->devlink = devlink;
11096 region->max_snapshots = region_max_snapshots;
11098 region->size = region_size;
11099 INIT_LIST_HEAD(®ion->snapshot_list);
11100 mutex_init(®ion->snapshot_lock);
11101 list_add_tail(®ion->list, &devlink->region_list);
11102 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11106 EXPORT_SYMBOL_GPL(devl_region_create);
11109 * devlink_region_create - create a new address region
11111 * @devlink: devlink
11112 * @ops: region operations and name
11113 * @region_max_snapshots: Maximum supported number of snapshots for region
11114 * @region_size: size of region
11116 * Context: Takes and release devlink->lock <mutex>.
11118 struct devlink_region *
11119 devlink_region_create(struct devlink *devlink,
11120 const struct devlink_region_ops *ops,
11121 u32 region_max_snapshots, u64 region_size)
11123 struct devlink_region *region;
11125 devl_lock(devlink);
11126 region = devl_region_create(devlink, ops, region_max_snapshots,
11128 devl_unlock(devlink);
11131 EXPORT_SYMBOL_GPL(devlink_region_create);
11134 * devlink_port_region_create - create a new address region for a port
11136 * @port: devlink port
11137 * @ops: region operations and name
11138 * @region_max_snapshots: Maximum supported number of snapshots for region
11139 * @region_size: size of region
11141 * Context: Takes and release devlink->lock <mutex>.
11143 struct devlink_region *
11144 devlink_port_region_create(struct devlink_port *port,
11145 const struct devlink_port_region_ops *ops,
11146 u32 region_max_snapshots, u64 region_size)
11148 struct devlink *devlink = port->devlink;
11149 struct devlink_region *region;
11152 ASSERT_DEVLINK_PORT_INITIALIZED(port);
11154 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11155 return ERR_PTR(-EINVAL);
11157 devl_lock(devlink);
11159 if (devlink_port_region_get_by_name(port, ops->name)) {
11164 region = kzalloc(sizeof(*region), GFP_KERNEL);
11170 region->devlink = devlink;
11171 region->port = port;
11172 region->max_snapshots = region_max_snapshots;
11173 region->port_ops = ops;
11174 region->size = region_size;
11175 INIT_LIST_HEAD(®ion->snapshot_list);
11176 mutex_init(®ion->snapshot_lock);
11177 list_add_tail(®ion->list, &port->region_list);
11178 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11180 devl_unlock(devlink);
11184 devl_unlock(devlink);
11185 return ERR_PTR(err);
11187 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11190 * devl_region_destroy - destroy address region
11192 * @region: devlink region to destroy
11194 void devl_region_destroy(struct devlink_region *region)
11196 struct devlink *devlink = region->devlink;
11197 struct devlink_snapshot *snapshot, *ts;
11199 devl_assert_locked(devlink);
11201 /* Free all snapshots of region */
11202 mutex_lock(®ion->snapshot_lock);
11203 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
11204 devlink_region_snapshot_del(region, snapshot);
11205 mutex_unlock(®ion->snapshot_lock);
11207 list_del(®ion->list);
11208 mutex_destroy(®ion->snapshot_lock);
11210 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11213 EXPORT_SYMBOL_GPL(devl_region_destroy);
11216 * devlink_region_destroy - destroy address region
11218 * @region: devlink region to destroy
11220 * Context: Takes and release devlink->lock <mutex>.
11222 void devlink_region_destroy(struct devlink_region *region)
11224 struct devlink *devlink = region->devlink;
11226 devl_lock(devlink);
11227 devl_region_destroy(region);
11228 devl_unlock(devlink);
11230 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11233 * devlink_region_snapshot_id_get - get snapshot ID
11235 * This callback should be called when adding a new snapshot,
11236 * Driver should use the same id for multiple snapshots taken
11237 * on multiple regions at the same time/by the same trigger.
11239 * The caller of this function must use devlink_region_snapshot_id_put
11240 * when finished creating regions using this id.
11242 * Returns zero on success, or a negative error code on failure.
11244 * @devlink: devlink
11245 * @id: storage to return id
11247 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11249 return __devlink_region_snapshot_id_get(devlink, id);
11251 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11254 * devlink_region_snapshot_id_put - put snapshot ID reference
11256 * This should be called by a driver after finishing creating snapshots
11257 * with an id. Doing so ensures that the ID can later be released in the
11258 * event that all snapshots using it have been destroyed.
11260 * @devlink: devlink
11261 * @id: id to release reference on
11263 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11265 __devlink_snapshot_id_decrement(devlink, id);
11267 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11270 * devlink_region_snapshot_create - create a new snapshot
11271 * This will add a new snapshot of a region. The snapshot
11272 * will be stored on the region struct and can be accessed
11273 * from devlink. This is useful for future analyses of snapshots.
11274 * Multiple snapshots can be created on a region.
11275 * The @snapshot_id should be obtained using the getter function.
11277 * @region: devlink region of the snapshot
11278 * @data: snapshot data
11279 * @snapshot_id: snapshot id to be created
11281 int devlink_region_snapshot_create(struct devlink_region *region,
11282 u8 *data, u32 snapshot_id)
11286 mutex_lock(®ion->snapshot_lock);
11287 err = __devlink_region_snapshot_create(region, data, snapshot_id);
11288 mutex_unlock(®ion->snapshot_lock);
11291 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11293 #define DEVLINK_TRAP(_id, _type) \
11295 .type = DEVLINK_TRAP_TYPE_##_type, \
11296 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
11297 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
11300 static const struct devlink_trap devlink_trap_generic[] = {
11301 DEVLINK_TRAP(SMAC_MC, DROP),
11302 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11303 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11304 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11305 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11306 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11307 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11308 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11309 DEVLINK_TRAP(TAIL_DROP, DROP),
11310 DEVLINK_TRAP(NON_IP_PACKET, DROP),
11311 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11312 DEVLINK_TRAP(DIP_LB, DROP),
11313 DEVLINK_TRAP(SIP_MC, DROP),
11314 DEVLINK_TRAP(SIP_LB, DROP),
11315 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11316 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11317 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11318 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11319 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11320 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11321 DEVLINK_TRAP(RPF, EXCEPTION),
11322 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11323 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11324 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11325 DEVLINK_TRAP(NON_ROUTABLE, DROP),
11326 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11327 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11328 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11329 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11330 DEVLINK_TRAP(STP, CONTROL),
11331 DEVLINK_TRAP(LACP, CONTROL),
11332 DEVLINK_TRAP(LLDP, CONTROL),
11333 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11334 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11335 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11336 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11337 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11338 DEVLINK_TRAP(MLD_QUERY, CONTROL),
11339 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11340 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11341 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11342 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11343 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11344 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11345 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11346 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11347 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11348 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11349 DEVLINK_TRAP(IPV4_BFD, CONTROL),
11350 DEVLINK_TRAP(IPV6_BFD, CONTROL),
11351 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11352 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11353 DEVLINK_TRAP(IPV4_BGP, CONTROL),
11354 DEVLINK_TRAP(IPV6_BGP, CONTROL),
11355 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11356 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11357 DEVLINK_TRAP(IPV4_PIM, CONTROL),
11358 DEVLINK_TRAP(IPV6_PIM, CONTROL),
11359 DEVLINK_TRAP(UC_LB, CONTROL),
11360 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11361 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11362 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11363 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11364 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11365 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11366 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11367 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11368 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11369 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11370 DEVLINK_TRAP(PTP_EVENT, CONTROL),
11371 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11372 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11373 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11374 DEVLINK_TRAP(EARLY_DROP, DROP),
11375 DEVLINK_TRAP(VXLAN_PARSING, DROP),
11376 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11377 DEVLINK_TRAP(VLAN_PARSING, DROP),
11378 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11379 DEVLINK_TRAP(MPLS_PARSING, DROP),
11380 DEVLINK_TRAP(ARP_PARSING, DROP),
11381 DEVLINK_TRAP(IP_1_PARSING, DROP),
11382 DEVLINK_TRAP(IP_N_PARSING, DROP),
11383 DEVLINK_TRAP(GRE_PARSING, DROP),
11384 DEVLINK_TRAP(UDP_PARSING, DROP),
11385 DEVLINK_TRAP(TCP_PARSING, DROP),
11386 DEVLINK_TRAP(IPSEC_PARSING, DROP),
11387 DEVLINK_TRAP(SCTP_PARSING, DROP),
11388 DEVLINK_TRAP(DCCP_PARSING, DROP),
11389 DEVLINK_TRAP(GTP_PARSING, DROP),
11390 DEVLINK_TRAP(ESP_PARSING, DROP),
11391 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11392 DEVLINK_TRAP(DMAC_FILTER, DROP),
11393 DEVLINK_TRAP(EAPOL, CONTROL),
11394 DEVLINK_TRAP(LOCKED_PORT, DROP),
11397 #define DEVLINK_TRAP_GROUP(_id) \
11399 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
11400 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
11403 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11404 DEVLINK_TRAP_GROUP(L2_DROPS),
11405 DEVLINK_TRAP_GROUP(L3_DROPS),
11406 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11407 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11408 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11409 DEVLINK_TRAP_GROUP(ACL_DROPS),
11410 DEVLINK_TRAP_GROUP(STP),
11411 DEVLINK_TRAP_GROUP(LACP),
11412 DEVLINK_TRAP_GROUP(LLDP),
11413 DEVLINK_TRAP_GROUP(MC_SNOOPING),
11414 DEVLINK_TRAP_GROUP(DHCP),
11415 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11416 DEVLINK_TRAP_GROUP(BFD),
11417 DEVLINK_TRAP_GROUP(OSPF),
11418 DEVLINK_TRAP_GROUP(BGP),
11419 DEVLINK_TRAP_GROUP(VRRP),
11420 DEVLINK_TRAP_GROUP(PIM),
11421 DEVLINK_TRAP_GROUP(UC_LB),
11422 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11423 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11424 DEVLINK_TRAP_GROUP(IPV6),
11425 DEVLINK_TRAP_GROUP(PTP_EVENT),
11426 DEVLINK_TRAP_GROUP(PTP_GENERAL),
11427 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11428 DEVLINK_TRAP_GROUP(ACL_TRAP),
11429 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11430 DEVLINK_TRAP_GROUP(EAPOL),
11433 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11435 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11438 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11441 if (trap->type != devlink_trap_generic[trap->id].type)
11447 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11451 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11454 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11455 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11462 static int devlink_trap_verify(const struct devlink_trap *trap)
11464 if (!trap || !trap->name)
11468 return devlink_trap_generic_verify(trap);
11470 return devlink_trap_driver_verify(trap);
11474 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11476 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11479 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11486 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11490 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11493 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11494 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11501 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11503 if (group->generic)
11504 return devlink_trap_group_generic_verify(group);
11506 return devlink_trap_group_driver_verify(group);
11510 devlink_trap_group_notify(struct devlink *devlink,
11511 const struct devlink_trap_group_item *group_item,
11512 enum devlink_command cmd)
11514 struct sk_buff *msg;
11517 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11518 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11519 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11522 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11526 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11533 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11534 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11538 devlink_trap_item_group_link(struct devlink *devlink,
11539 struct devlink_trap_item *trap_item)
11541 u16 group_id = trap_item->trap->init_group_id;
11542 struct devlink_trap_group_item *group_item;
11544 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11545 if (WARN_ON_ONCE(!group_item))
11548 trap_item->group_item = group_item;
11553 static void devlink_trap_notify(struct devlink *devlink,
11554 const struct devlink_trap_item *trap_item,
11555 enum devlink_command cmd)
11557 struct sk_buff *msg;
11560 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11561 cmd != DEVLINK_CMD_TRAP_DEL);
11562 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11565 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11569 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11575 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11576 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11580 devlink_trap_register(struct devlink *devlink,
11581 const struct devlink_trap *trap, void *priv)
11583 struct devlink_trap_item *trap_item;
11586 if (devlink_trap_item_lookup(devlink, trap->name))
11589 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11593 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11594 if (!trap_item->stats) {
11596 goto err_stats_alloc;
11599 trap_item->trap = trap;
11600 trap_item->action = trap->init_action;
11601 trap_item->priv = priv;
11603 err = devlink_trap_item_group_link(devlink, trap_item);
11605 goto err_group_link;
11607 err = devlink->ops->trap_init(devlink, trap, trap_item);
11609 goto err_trap_init;
11611 list_add_tail(&trap_item->list, &devlink->trap_list);
11612 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11618 free_percpu(trap_item->stats);
11624 static void devlink_trap_unregister(struct devlink *devlink,
11625 const struct devlink_trap *trap)
11627 struct devlink_trap_item *trap_item;
11629 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11630 if (WARN_ON_ONCE(!trap_item))
11633 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11634 list_del(&trap_item->list);
11635 if (devlink->ops->trap_fini)
11636 devlink->ops->trap_fini(devlink, trap, trap_item);
11637 free_percpu(trap_item->stats);
11641 static void devlink_trap_disable(struct devlink *devlink,
11642 const struct devlink_trap *trap)
11644 struct devlink_trap_item *trap_item;
11646 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11647 if (WARN_ON_ONCE(!trap_item))
11650 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11652 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11656 * devl_traps_register - Register packet traps with devlink.
11657 * @devlink: devlink.
11658 * @traps: Packet traps.
11659 * @traps_count: Count of provided packet traps.
11660 * @priv: Driver private information.
11662 * Return: Non-zero value on failure.
11664 int devl_traps_register(struct devlink *devlink,
11665 const struct devlink_trap *traps,
11666 size_t traps_count, void *priv)
11670 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11673 devl_assert_locked(devlink);
11674 for (i = 0; i < traps_count; i++) {
11675 const struct devlink_trap *trap = &traps[i];
11677 err = devlink_trap_verify(trap);
11679 goto err_trap_verify;
11681 err = devlink_trap_register(devlink, trap, priv);
11683 goto err_trap_register;
11690 for (i--; i >= 0; i--)
11691 devlink_trap_unregister(devlink, &traps[i]);
11694 EXPORT_SYMBOL_GPL(devl_traps_register);
11697 * devlink_traps_register - Register packet traps with devlink.
11698 * @devlink: devlink.
11699 * @traps: Packet traps.
11700 * @traps_count: Count of provided packet traps.
11701 * @priv: Driver private information.
11703 * Context: Takes and release devlink->lock <mutex>.
11705 * Return: Non-zero value on failure.
11707 int devlink_traps_register(struct devlink *devlink,
11708 const struct devlink_trap *traps,
11709 size_t traps_count, void *priv)
11713 devl_lock(devlink);
11714 err = devl_traps_register(devlink, traps, traps_count, priv);
11715 devl_unlock(devlink);
11718 EXPORT_SYMBOL_GPL(devlink_traps_register);
11721 * devl_traps_unregister - Unregister packet traps from devlink.
11722 * @devlink: devlink.
11723 * @traps: Packet traps.
11724 * @traps_count: Count of provided packet traps.
11726 void devl_traps_unregister(struct devlink *devlink,
11727 const struct devlink_trap *traps,
11728 size_t traps_count)
11732 devl_assert_locked(devlink);
11733 /* Make sure we do not have any packets in-flight while unregistering
11734 * traps by disabling all of them and waiting for a grace period.
11736 for (i = traps_count - 1; i >= 0; i--)
11737 devlink_trap_disable(devlink, &traps[i]);
11739 for (i = traps_count - 1; i >= 0; i--)
11740 devlink_trap_unregister(devlink, &traps[i]);
11742 EXPORT_SYMBOL_GPL(devl_traps_unregister);
11745 * devlink_traps_unregister - Unregister packet traps from devlink.
11746 * @devlink: devlink.
11747 * @traps: Packet traps.
11748 * @traps_count: Count of provided packet traps.
11750 * Context: Takes and release devlink->lock <mutex>.
11752 void devlink_traps_unregister(struct devlink *devlink,
11753 const struct devlink_trap *traps,
11754 size_t traps_count)
11756 devl_lock(devlink);
11757 devl_traps_unregister(devlink, traps, traps_count);
11758 devl_unlock(devlink);
11760 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11763 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11766 struct devlink_stats *stats;
11768 stats = this_cpu_ptr(trap_stats);
11769 u64_stats_update_begin(&stats->syncp);
11770 u64_stats_add(&stats->rx_bytes, skb_len);
11771 u64_stats_inc(&stats->rx_packets);
11772 u64_stats_update_end(&stats->syncp);
11776 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11777 const struct devlink_trap_item *trap_item,
11778 struct devlink_port *in_devlink_port,
11779 const struct flow_action_cookie *fa_cookie)
11781 metadata->trap_name = trap_item->trap->name;
11782 metadata->trap_group_name = trap_item->group_item->group->name;
11783 metadata->fa_cookie = fa_cookie;
11784 metadata->trap_type = trap_item->trap->type;
11786 spin_lock(&in_devlink_port->type_lock);
11787 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11788 metadata->input_dev = in_devlink_port->type_eth.netdev;
11789 spin_unlock(&in_devlink_port->type_lock);
11793 * devlink_trap_report - Report trapped packet to drop monitor.
11794 * @devlink: devlink.
11795 * @skb: Trapped packet.
11796 * @trap_ctx: Trap context.
11797 * @in_devlink_port: Input devlink port.
11798 * @fa_cookie: Flow action cookie. Could be NULL.
11800 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11801 void *trap_ctx, struct devlink_port *in_devlink_port,
11802 const struct flow_action_cookie *fa_cookie)
11805 struct devlink_trap_item *trap_item = trap_ctx;
11807 devlink_trap_stats_update(trap_item->stats, skb->len);
11808 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11810 if (trace_devlink_trap_report_enabled()) {
11811 struct devlink_trap_metadata metadata = {};
11813 devlink_trap_report_metadata_set(&metadata, trap_item,
11814 in_devlink_port, fa_cookie);
11815 trace_devlink_trap_report(devlink, skb, &metadata);
11818 EXPORT_SYMBOL_GPL(devlink_trap_report);
11821 * devlink_trap_ctx_priv - Trap context to driver private information.
11822 * @trap_ctx: Trap context.
11824 * Return: Driver private information passed during registration.
11826 void *devlink_trap_ctx_priv(void *trap_ctx)
11828 struct devlink_trap_item *trap_item = trap_ctx;
11830 return trap_item->priv;
11832 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
11835 devlink_trap_group_item_policer_link(struct devlink *devlink,
11836 struct devlink_trap_group_item *group_item)
11838 u32 policer_id = group_item->group->init_policer_id;
11839 struct devlink_trap_policer_item *policer_item;
11841 if (policer_id == 0)
11844 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
11845 if (WARN_ON_ONCE(!policer_item))
11848 group_item->policer_item = policer_item;
11854 devlink_trap_group_register(struct devlink *devlink,
11855 const struct devlink_trap_group *group)
11857 struct devlink_trap_group_item *group_item;
11860 if (devlink_trap_group_item_lookup(devlink, group->name))
11863 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
11867 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11868 if (!group_item->stats) {
11870 goto err_stats_alloc;
11873 group_item->group = group;
11875 err = devlink_trap_group_item_policer_link(devlink, group_item);
11877 goto err_policer_link;
11879 if (devlink->ops->trap_group_init) {
11880 err = devlink->ops->trap_group_init(devlink, group);
11882 goto err_group_init;
11885 list_add_tail(&group_item->list, &devlink->trap_group_list);
11886 devlink_trap_group_notify(devlink, group_item,
11887 DEVLINK_CMD_TRAP_GROUP_NEW);
11893 free_percpu(group_item->stats);
11900 devlink_trap_group_unregister(struct devlink *devlink,
11901 const struct devlink_trap_group *group)
11903 struct devlink_trap_group_item *group_item;
11905 group_item = devlink_trap_group_item_lookup(devlink, group->name);
11906 if (WARN_ON_ONCE(!group_item))
11909 devlink_trap_group_notify(devlink, group_item,
11910 DEVLINK_CMD_TRAP_GROUP_DEL);
11911 list_del(&group_item->list);
11912 free_percpu(group_item->stats);
11917 * devl_trap_groups_register - Register packet trap groups with devlink.
11918 * @devlink: devlink.
11919 * @groups: Packet trap groups.
11920 * @groups_count: Count of provided packet trap groups.
11922 * Return: Non-zero value on failure.
11924 int devl_trap_groups_register(struct devlink *devlink,
11925 const struct devlink_trap_group *groups,
11926 size_t groups_count)
11930 devl_assert_locked(devlink);
11931 for (i = 0; i < groups_count; i++) {
11932 const struct devlink_trap_group *group = &groups[i];
11934 err = devlink_trap_group_verify(group);
11936 goto err_trap_group_verify;
11938 err = devlink_trap_group_register(devlink, group);
11940 goto err_trap_group_register;
11945 err_trap_group_register:
11946 err_trap_group_verify:
11947 for (i--; i >= 0; i--)
11948 devlink_trap_group_unregister(devlink, &groups[i]);
11951 EXPORT_SYMBOL_GPL(devl_trap_groups_register);
11954 * devlink_trap_groups_register - Register packet trap groups with devlink.
11955 * @devlink: devlink.
11956 * @groups: Packet trap groups.
11957 * @groups_count: Count of provided packet trap groups.
11959 * Context: Takes and release devlink->lock <mutex>.
11961 * Return: Non-zero value on failure.
11963 int devlink_trap_groups_register(struct devlink *devlink,
11964 const struct devlink_trap_group *groups,
11965 size_t groups_count)
11969 devl_lock(devlink);
11970 err = devl_trap_groups_register(devlink, groups, groups_count);
11971 devl_unlock(devlink);
11974 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
11977 * devl_trap_groups_unregister - Unregister packet trap groups from devlink.
11978 * @devlink: devlink.
11979 * @groups: Packet trap groups.
11980 * @groups_count: Count of provided packet trap groups.
11982 void devl_trap_groups_unregister(struct devlink *devlink,
11983 const struct devlink_trap_group *groups,
11984 size_t groups_count)
11988 devl_assert_locked(devlink);
11989 for (i = groups_count - 1; i >= 0; i--)
11990 devlink_trap_group_unregister(devlink, &groups[i]);
11992 EXPORT_SYMBOL_GPL(devl_trap_groups_unregister);
11995 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
11996 * @devlink: devlink.
11997 * @groups: Packet trap groups.
11998 * @groups_count: Count of provided packet trap groups.
12000 * Context: Takes and release devlink->lock <mutex>.
12002 void devlink_trap_groups_unregister(struct devlink *devlink,
12003 const struct devlink_trap_group *groups,
12004 size_t groups_count)
12006 devl_lock(devlink);
12007 devl_trap_groups_unregister(devlink, groups, groups_count);
12008 devl_unlock(devlink);
12010 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
12013 devlink_trap_policer_notify(struct devlink *devlink,
12014 const struct devlink_trap_policer_item *policer_item,
12015 enum devlink_command cmd)
12017 struct sk_buff *msg;
12020 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
12021 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
12022 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
12025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12029 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
12036 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
12037 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
12041 devlink_trap_policer_register(struct devlink *devlink,
12042 const struct devlink_trap_policer *policer)
12044 struct devlink_trap_policer_item *policer_item;
12047 if (devlink_trap_policer_item_lookup(devlink, policer->id))
12050 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
12054 policer_item->policer = policer;
12055 policer_item->rate = policer->init_rate;
12056 policer_item->burst = policer->init_burst;
12058 if (devlink->ops->trap_policer_init) {
12059 err = devlink->ops->trap_policer_init(devlink, policer);
12061 goto err_policer_init;
12064 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
12065 devlink_trap_policer_notify(devlink, policer_item,
12066 DEVLINK_CMD_TRAP_POLICER_NEW);
12071 kfree(policer_item);
12076 devlink_trap_policer_unregister(struct devlink *devlink,
12077 const struct devlink_trap_policer *policer)
12079 struct devlink_trap_policer_item *policer_item;
12081 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
12082 if (WARN_ON_ONCE(!policer_item))
12085 devlink_trap_policer_notify(devlink, policer_item,
12086 DEVLINK_CMD_TRAP_POLICER_DEL);
12087 list_del(&policer_item->list);
12088 if (devlink->ops->trap_policer_fini)
12089 devlink->ops->trap_policer_fini(devlink, policer);
12090 kfree(policer_item);
12094 * devl_trap_policers_register - Register packet trap policers with devlink.
12095 * @devlink: devlink.
12096 * @policers: Packet trap policers.
12097 * @policers_count: Count of provided packet trap policers.
12099 * Return: Non-zero value on failure.
12102 devl_trap_policers_register(struct devlink *devlink,
12103 const struct devlink_trap_policer *policers,
12104 size_t policers_count)
12108 devl_assert_locked(devlink);
12109 for (i = 0; i < policers_count; i++) {
12110 const struct devlink_trap_policer *policer = &policers[i];
12112 if (WARN_ON(policer->id == 0 ||
12113 policer->max_rate < policer->min_rate ||
12114 policer->max_burst < policer->min_burst)) {
12116 goto err_trap_policer_verify;
12119 err = devlink_trap_policer_register(devlink, policer);
12121 goto err_trap_policer_register;
12125 err_trap_policer_register:
12126 err_trap_policer_verify:
12127 for (i--; i >= 0; i--)
12128 devlink_trap_policer_unregister(devlink, &policers[i]);
12131 EXPORT_SYMBOL_GPL(devl_trap_policers_register);
12134 * devl_trap_policers_unregister - Unregister packet trap policers from devlink.
12135 * @devlink: devlink.
12136 * @policers: Packet trap policers.
12137 * @policers_count: Count of provided packet trap policers.
12140 devl_trap_policers_unregister(struct devlink *devlink,
12141 const struct devlink_trap_policer *policers,
12142 size_t policers_count)
12146 devl_assert_locked(devlink);
12147 for (i = policers_count - 1; i >= 0; i--)
12148 devlink_trap_policer_unregister(devlink, &policers[i]);
12150 EXPORT_SYMBOL_GPL(devl_trap_policers_unregister);
12152 static void __devlink_compat_running_version(struct devlink *devlink,
12153 char *buf, size_t len)
12155 struct devlink_info_req req = {};
12156 const struct nlattr *nlattr;
12157 struct sk_buff *msg;
12160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12165 err = devlink->ops->info_get(devlink, &req, NULL);
12169 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12170 const struct nlattr *kv;
12173 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12176 nla_for_each_nested(kv, nlattr, rem_kv) {
12177 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12180 strlcat(buf, nla_data(kv), len);
12181 strlcat(buf, " ", len);
12188 void devlink_compat_running_version(struct devlink *devlink,
12189 char *buf, size_t len)
12191 if (!devlink->ops->info_get)
12194 devl_lock(devlink);
12195 if (devl_is_registered(devlink))
12196 __devlink_compat_running_version(devlink, buf, len);
12197 devl_unlock(devlink);
12200 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12202 struct devlink_flash_update_params params = {};
12205 devl_lock(devlink);
12206 if (!devl_is_registered(devlink)) {
12211 if (!devlink->ops->flash_update) {
12216 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
12220 devlink_flash_update_begin_notify(devlink);
12221 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
12222 devlink_flash_update_end_notify(devlink);
12224 release_firmware(params.fw);
12226 devl_unlock(devlink);
12231 int devlink_compat_phys_port_name_get(struct net_device *dev,
12232 char *name, size_t len)
12234 struct devlink_port *devlink_port;
12236 /* RTNL mutex is held here which ensures that devlink_port
12237 * instance cannot disappear in the middle. No need to take
12238 * any devlink lock as only permanent values are accessed.
12242 devlink_port = dev->devlink_port;
12244 return -EOPNOTSUPP;
12246 return __devlink_port_phys_port_name_get(devlink_port, name, len);
12249 int devlink_compat_switch_id_get(struct net_device *dev,
12250 struct netdev_phys_item_id *ppid)
12252 struct devlink_port *devlink_port;
12254 /* Caller must hold RTNL mutex or reference to dev, which ensures that
12255 * devlink_port instance cannot disappear in the middle. No need to take
12256 * any devlink lock as only permanent values are accessed.
12258 devlink_port = dev->devlink_port;
12259 if (!devlink_port || !devlink_port->switch_port)
12260 return -EOPNOTSUPP;
12262 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));