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 struct mutex *list_lock,
7285 const char *reporter_name)
7287 struct devlink_health_reporter *reporter;
7289 lockdep_assert_held(list_lock);
7290 list_for_each_entry(reporter, reporter_list, list)
7291 if (!strcmp(reporter->ops->name, reporter_name))
7296 static struct devlink_health_reporter *
7297 devlink_health_reporter_find_by_name(struct devlink *devlink,
7298 const char *reporter_name)
7300 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
7301 &devlink->reporters_lock,
7305 static struct devlink_health_reporter *
7306 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
7307 const char *reporter_name)
7309 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
7310 &devlink_port->reporters_lock,
7314 static struct devlink_health_reporter *
7315 __devlink_health_reporter_create(struct devlink *devlink,
7316 const struct devlink_health_reporter_ops *ops,
7317 u64 graceful_period, void *priv)
7319 struct devlink_health_reporter *reporter;
7321 if (WARN_ON(graceful_period && !ops->recover))
7322 return ERR_PTR(-EINVAL);
7324 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
7326 return ERR_PTR(-ENOMEM);
7328 reporter->priv = priv;
7329 reporter->ops = ops;
7330 reporter->devlink = devlink;
7331 reporter->graceful_period = graceful_period;
7332 reporter->auto_recover = !!ops->recover;
7333 reporter->auto_dump = !!ops->dump;
7334 mutex_init(&reporter->dump_lock);
7335 refcount_set(&reporter->refcount, 1);
7340 * devl_port_health_reporter_create - create devlink health reporter for
7341 * specified port instance
7343 * @port: devlink_port which should contain the new reporter
7345 * @graceful_period: to avoid recovery loops, in msecs
7348 struct devlink_health_reporter *
7349 devl_port_health_reporter_create(struct devlink_port *port,
7350 const struct devlink_health_reporter_ops *ops,
7351 u64 graceful_period, void *priv)
7353 struct devlink_health_reporter *reporter;
7355 devl_assert_locked(port->devlink);
7356 mutex_lock(&port->reporters_lock);
7357 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
7358 &port->reporters_lock, ops->name)) {
7359 reporter = ERR_PTR(-EEXIST);
7363 reporter = __devlink_health_reporter_create(port->devlink, ops,
7364 graceful_period, priv);
7365 if (IS_ERR(reporter))
7368 reporter->devlink_port = port;
7369 list_add_tail(&reporter->list, &port->reporter_list);
7371 mutex_unlock(&port->reporters_lock);
7374 EXPORT_SYMBOL_GPL(devl_port_health_reporter_create);
7376 struct devlink_health_reporter *
7377 devlink_port_health_reporter_create(struct devlink_port *port,
7378 const struct devlink_health_reporter_ops *ops,
7379 u64 graceful_period, void *priv)
7381 struct devlink_health_reporter *reporter;
7382 struct devlink *devlink = port->devlink;
7385 reporter = devl_port_health_reporter_create(port, ops,
7386 graceful_period, priv);
7387 devl_unlock(devlink);
7390 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
7393 * devl_health_reporter_create - create devlink health reporter
7397 * @graceful_period: to avoid recovery loops, in msecs
7400 struct devlink_health_reporter *
7401 devl_health_reporter_create(struct devlink *devlink,
7402 const struct devlink_health_reporter_ops *ops,
7403 u64 graceful_period, void *priv)
7405 struct devlink_health_reporter *reporter;
7407 devl_assert_locked(devlink);
7408 mutex_lock(&devlink->reporters_lock);
7409 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
7410 reporter = ERR_PTR(-EEXIST);
7414 reporter = __devlink_health_reporter_create(devlink, ops,
7415 graceful_period, priv);
7416 if (IS_ERR(reporter))
7419 list_add_tail(&reporter->list, &devlink->reporter_list);
7421 mutex_unlock(&devlink->reporters_lock);
7424 EXPORT_SYMBOL_GPL(devl_health_reporter_create);
7426 struct devlink_health_reporter *
7427 devlink_health_reporter_create(struct devlink *devlink,
7428 const struct devlink_health_reporter_ops *ops,
7429 u64 graceful_period, void *priv)
7431 struct devlink_health_reporter *reporter;
7434 reporter = devl_health_reporter_create(devlink, ops,
7435 graceful_period, priv);
7436 devl_unlock(devlink);
7439 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
7442 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
7444 mutex_destroy(&reporter->dump_lock);
7445 if (reporter->dump_fmsg)
7446 devlink_fmsg_free(reporter->dump_fmsg);
7451 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
7453 if (refcount_dec_and_test(&reporter->refcount))
7454 devlink_health_reporter_free(reporter);
7458 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7460 list_del(&reporter->list);
7461 devlink_health_reporter_put(reporter);
7465 * devl_health_reporter_destroy - destroy devlink health reporter
7467 * @reporter: devlink health reporter to destroy
7470 devl_health_reporter_destroy(struct devlink_health_reporter *reporter)
7472 struct mutex *lock = &reporter->devlink->reporters_lock;
7474 devl_assert_locked(reporter->devlink);
7477 __devlink_health_reporter_destroy(reporter);
7480 EXPORT_SYMBOL_GPL(devl_health_reporter_destroy);
7483 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7485 struct devlink *devlink = reporter->devlink;
7488 devl_health_reporter_destroy(reporter);
7489 devl_unlock(devlink);
7491 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
7494 * devl_port_health_reporter_destroy - destroy devlink port health reporter
7496 * @reporter: devlink health reporter to destroy
7499 devl_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
7501 struct mutex *lock = &reporter->devlink_port->reporters_lock;
7503 devl_assert_locked(reporter->devlink);
7506 __devlink_health_reporter_destroy(reporter);
7509 EXPORT_SYMBOL_GPL(devl_port_health_reporter_destroy);
7512 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
7514 struct devlink *devlink = reporter->devlink;
7517 devl_port_health_reporter_destroy(reporter);
7518 devl_unlock(devlink);
7520 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
7523 devlink_nl_health_reporter_fill(struct sk_buff *msg,
7524 struct devlink_health_reporter *reporter,
7525 enum devlink_command cmd, u32 portid,
7528 struct devlink *devlink = reporter->devlink;
7529 struct nlattr *reporter_attr;
7532 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7536 if (devlink_nl_put_handle(msg, devlink))
7537 goto genlmsg_cancel;
7539 if (reporter->devlink_port) {
7540 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
7541 goto genlmsg_cancel;
7543 reporter_attr = nla_nest_start_noflag(msg,
7544 DEVLINK_ATTR_HEALTH_REPORTER);
7546 goto genlmsg_cancel;
7547 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
7548 reporter->ops->name))
7549 goto reporter_nest_cancel;
7550 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
7551 reporter->health_state))
7552 goto reporter_nest_cancel;
7553 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
7554 reporter->error_count, DEVLINK_ATTR_PAD))
7555 goto reporter_nest_cancel;
7556 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
7557 reporter->recovery_count, DEVLINK_ATTR_PAD))
7558 goto reporter_nest_cancel;
7559 if (reporter->ops->recover &&
7560 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
7561 reporter->graceful_period,
7563 goto reporter_nest_cancel;
7564 if (reporter->ops->recover &&
7565 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
7566 reporter->auto_recover))
7567 goto reporter_nest_cancel;
7568 if (reporter->dump_fmsg &&
7569 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
7570 jiffies_to_msecs(reporter->dump_ts),
7572 goto reporter_nest_cancel;
7573 if (reporter->dump_fmsg &&
7574 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
7575 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
7576 goto reporter_nest_cancel;
7577 if (reporter->ops->dump &&
7578 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
7579 reporter->auto_dump))
7580 goto reporter_nest_cancel;
7582 nla_nest_end(msg, reporter_attr);
7583 genlmsg_end(msg, hdr);
7586 reporter_nest_cancel:
7587 nla_nest_end(msg, reporter_attr);
7589 genlmsg_cancel(msg, hdr);
7593 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7594 enum devlink_command cmd)
7596 struct devlink *devlink = reporter->devlink;
7597 struct sk_buff *msg;
7600 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7601 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7603 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7607 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7613 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7614 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7618 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7620 reporter->recovery_count++;
7621 reporter->last_recovery_ts = jiffies;
7623 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7626 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7627 void *priv_ctx, struct netlink_ext_ack *extack)
7631 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7634 if (!reporter->ops->recover)
7637 err = reporter->ops->recover(reporter, priv_ctx, extack);
7641 devlink_health_reporter_recovery_done(reporter);
7642 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7643 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7649 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7651 if (!reporter->dump_fmsg)
7653 devlink_fmsg_free(reporter->dump_fmsg);
7654 reporter->dump_fmsg = NULL;
7657 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7659 struct netlink_ext_ack *extack)
7663 if (!reporter->ops->dump)
7666 if (reporter->dump_fmsg)
7669 reporter->dump_fmsg = devlink_fmsg_alloc();
7670 if (!reporter->dump_fmsg) {
7675 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7679 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7684 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7688 reporter->dump_ts = jiffies;
7689 reporter->dump_real_ts = ktime_get_real_ns();
7694 devlink_health_dump_clear(reporter);
7698 int devlink_health_report(struct devlink_health_reporter *reporter,
7699 const char *msg, void *priv_ctx)
7701 enum devlink_health_reporter_state prev_health_state;
7702 struct devlink *devlink = reporter->devlink;
7703 unsigned long recover_ts_threshold;
7706 /* write a log message of the current error */
7708 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7709 reporter->error_count++;
7710 prev_health_state = reporter->health_state;
7711 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7712 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7714 /* abort if the previous error wasn't recovered */
7715 recover_ts_threshold = reporter->last_recovery_ts +
7716 msecs_to_jiffies(reporter->graceful_period);
7717 if (reporter->auto_recover &&
7718 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7719 (reporter->last_recovery_ts && reporter->recovery_count &&
7720 time_is_after_jiffies(recover_ts_threshold)))) {
7721 trace_devlink_health_recover_aborted(devlink,
7722 reporter->ops->name,
7723 reporter->health_state,
7725 reporter->last_recovery_ts);
7729 if (reporter->auto_dump) {
7730 mutex_lock(&reporter->dump_lock);
7731 /* store current dump of current error, for later analysis */
7732 devlink_health_do_dump(reporter, priv_ctx, NULL);
7733 mutex_unlock(&reporter->dump_lock);
7736 if (!reporter->auto_recover)
7740 ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL);
7741 devl_unlock(devlink);
7745 EXPORT_SYMBOL_GPL(devlink_health_report);
7747 static struct devlink_health_reporter *
7748 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7749 struct nlattr **attrs)
7751 struct devlink_health_reporter *reporter;
7752 struct devlink_port *devlink_port;
7753 char *reporter_name;
7755 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7758 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7759 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7760 if (IS_ERR(devlink_port)) {
7761 mutex_lock(&devlink->reporters_lock);
7762 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7764 refcount_inc(&reporter->refcount);
7765 mutex_unlock(&devlink->reporters_lock);
7767 mutex_lock(&devlink_port->reporters_lock);
7768 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7770 refcount_inc(&reporter->refcount);
7771 mutex_unlock(&devlink_port->reporters_lock);
7777 static struct devlink_health_reporter *
7778 devlink_health_reporter_get_from_info(struct devlink *devlink,
7779 struct genl_info *info)
7781 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7784 static struct devlink_health_reporter *
7785 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7787 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7788 struct devlink_health_reporter *reporter;
7789 struct nlattr **attrs = info->attrs;
7790 struct devlink *devlink;
7792 devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs);
7793 if (IS_ERR(devlink))
7795 devl_unlock(devlink);
7797 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7798 devlink_put(devlink);
7803 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7804 enum devlink_health_reporter_state state)
7806 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7807 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7810 if (reporter->health_state == state)
7813 reporter->health_state = state;
7814 trace_devlink_health_reporter_state_update(reporter->devlink,
7815 reporter->ops->name, state);
7816 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7818 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7820 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7821 struct genl_info *info)
7823 struct devlink *devlink = info->user_ptr[0];
7824 struct devlink_health_reporter *reporter;
7825 struct sk_buff *msg;
7828 reporter = devlink_health_reporter_get_from_info(devlink, info);
7832 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7838 err = devlink_nl_health_reporter_fill(msg, reporter,
7839 DEVLINK_CMD_HEALTH_REPORTER_GET,
7840 info->snd_portid, info->snd_seq,
7847 err = genlmsg_reply(msg, info);
7849 devlink_health_reporter_put(reporter);
7854 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7855 struct netlink_callback *cb)
7857 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7858 struct devlink *devlink;
7861 devlink_dump_for_each_instance_get(msg, state, devlink) {
7862 struct devlink_health_reporter *reporter;
7863 struct devlink_port *port;
7864 unsigned long port_index;
7868 if (!devl_is_registered(devlink))
7871 mutex_lock(&devlink->reporters_lock);
7873 list_for_each_entry(reporter, &devlink->reporter_list,
7875 if (idx < state->idx) {
7879 err = devlink_nl_health_reporter_fill(
7880 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7881 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7884 mutex_unlock(&devlink->reporters_lock);
7885 devl_unlock(devlink);
7886 devlink_put(devlink);
7892 mutex_unlock(&devlink->reporters_lock);
7894 xa_for_each(&devlink->ports, port_index, port) {
7895 mutex_lock(&port->reporters_lock);
7896 list_for_each_entry(reporter, &port->reporter_list, list) {
7897 if (idx < state->idx) {
7901 err = devlink_nl_health_reporter_fill(
7903 DEVLINK_CMD_HEALTH_REPORTER_GET,
7904 NETLINK_CB(cb->skb).portid,
7905 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7907 mutex_unlock(&port->reporters_lock);
7908 devl_unlock(devlink);
7909 devlink_put(devlink);
7915 mutex_unlock(&port->reporters_lock);
7918 devl_unlock(devlink);
7919 devlink_put(devlink);
7926 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7927 struct genl_info *info)
7929 struct devlink *devlink = info->user_ptr[0];
7930 struct devlink_health_reporter *reporter;
7933 reporter = devlink_health_reporter_get_from_info(devlink, info);
7937 if (!reporter->ops->recover &&
7938 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
7939 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
7943 if (!reporter->ops->dump &&
7944 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
7949 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7950 reporter->graceful_period =
7951 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
7953 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7954 reporter->auto_recover =
7955 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
7957 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7958 reporter->auto_dump =
7959 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
7961 devlink_health_reporter_put(reporter);
7964 devlink_health_reporter_put(reporter);
7968 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
7969 struct genl_info *info)
7971 struct devlink *devlink = info->user_ptr[0];
7972 struct devlink_health_reporter *reporter;
7975 reporter = devlink_health_reporter_get_from_info(devlink, info);
7979 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
7981 devlink_health_reporter_put(reporter);
7985 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
7986 struct genl_info *info)
7988 struct devlink *devlink = info->user_ptr[0];
7989 struct devlink_health_reporter *reporter;
7990 struct devlink_fmsg *fmsg;
7993 reporter = devlink_health_reporter_get_from_info(devlink, info);
7997 if (!reporter->ops->diagnose) {
7998 devlink_health_reporter_put(reporter);
8002 fmsg = devlink_fmsg_alloc();
8004 devlink_health_reporter_put(reporter);
8008 err = devlink_fmsg_obj_nest_start(fmsg);
8012 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
8016 err = devlink_fmsg_obj_nest_end(fmsg);
8020 err = devlink_fmsg_snd(fmsg, info,
8021 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
8024 devlink_fmsg_free(fmsg);
8025 devlink_health_reporter_put(reporter);
8030 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
8031 struct netlink_callback *cb)
8033 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8034 struct devlink_health_reporter *reporter;
8037 reporter = devlink_health_reporter_get_from_cb(cb);
8041 if (!reporter->ops->dump) {
8045 mutex_lock(&reporter->dump_lock);
8047 err = devlink_health_do_dump(reporter, NULL, cb->extack);
8050 state->dump_ts = reporter->dump_ts;
8052 if (!reporter->dump_fmsg || state->dump_ts != reporter->dump_ts) {
8053 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
8058 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
8059 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
8061 mutex_unlock(&reporter->dump_lock);
8063 devlink_health_reporter_put(reporter);
8068 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
8069 struct genl_info *info)
8071 struct devlink *devlink = info->user_ptr[0];
8072 struct devlink_health_reporter *reporter;
8074 reporter = devlink_health_reporter_get_from_info(devlink, info);
8078 if (!reporter->ops->dump) {
8079 devlink_health_reporter_put(reporter);
8083 mutex_lock(&reporter->dump_lock);
8084 devlink_health_dump_clear(reporter);
8085 mutex_unlock(&reporter->dump_lock);
8086 devlink_health_reporter_put(reporter);
8090 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
8091 struct genl_info *info)
8093 struct devlink *devlink = info->user_ptr[0];
8094 struct devlink_health_reporter *reporter;
8097 reporter = devlink_health_reporter_get_from_info(devlink, info);
8101 if (!reporter->ops->test) {
8102 devlink_health_reporter_put(reporter);
8106 err = reporter->ops->test(reporter, info->extack);
8108 devlink_health_reporter_put(reporter);
8112 struct devlink_stats {
8113 u64_stats_t rx_bytes;
8114 u64_stats_t rx_packets;
8115 struct u64_stats_sync syncp;
8119 * struct devlink_trap_policer_item - Packet trap policer attributes.
8120 * @policer: Immutable packet trap policer attributes.
8121 * @rate: Rate in packets / sec.
8122 * @burst: Burst size in packets.
8123 * @list: trap_policer_list member.
8125 * Describes packet trap policer attributes. Created by devlink during trap
8126 * policer registration.
8128 struct devlink_trap_policer_item {
8129 const struct devlink_trap_policer *policer;
8132 struct list_head list;
8136 * struct devlink_trap_group_item - Packet trap group attributes.
8137 * @group: Immutable packet trap group attributes.
8138 * @policer_item: Associated policer item. Can be NULL.
8139 * @list: trap_group_list member.
8140 * @stats: Trap group statistics.
8142 * Describes packet trap group attributes. Created by devlink during trap
8143 * group registration.
8145 struct devlink_trap_group_item {
8146 const struct devlink_trap_group *group;
8147 struct devlink_trap_policer_item *policer_item;
8148 struct list_head list;
8149 struct devlink_stats __percpu *stats;
8153 * struct devlink_trap_item - Packet trap attributes.
8154 * @trap: Immutable packet trap attributes.
8155 * @group_item: Associated group item.
8156 * @list: trap_list member.
8157 * @action: Trap action.
8158 * @stats: Trap statistics.
8159 * @priv: Driver private information.
8161 * Describes both mutable and immutable packet trap attributes. Created by
8162 * devlink during trap registration and used for all trap related operations.
8164 struct devlink_trap_item {
8165 const struct devlink_trap *trap;
8166 struct devlink_trap_group_item *group_item;
8167 struct list_head list;
8168 enum devlink_trap_action action;
8169 struct devlink_stats __percpu *stats;
8173 static struct devlink_trap_policer_item *
8174 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
8176 struct devlink_trap_policer_item *policer_item;
8178 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8179 if (policer_item->policer->id == id)
8180 return policer_item;
8186 static struct devlink_trap_item *
8187 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
8189 struct devlink_trap_item *trap_item;
8191 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8192 if (!strcmp(trap_item->trap->name, name))
8199 static struct devlink_trap_item *
8200 devlink_trap_item_get_from_info(struct devlink *devlink,
8201 struct genl_info *info)
8203 struct nlattr *attr;
8205 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
8207 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
8209 return devlink_trap_item_lookup(devlink, nla_data(attr));
8213 devlink_trap_action_get_from_info(struct genl_info *info,
8214 enum devlink_trap_action *p_trap_action)
8218 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
8220 case DEVLINK_TRAP_ACTION_DROP:
8221 case DEVLINK_TRAP_ACTION_TRAP:
8222 case DEVLINK_TRAP_ACTION_MIRROR:
8223 *p_trap_action = val;
8232 static int devlink_trap_metadata_put(struct sk_buff *msg,
8233 const struct devlink_trap *trap)
8235 struct nlattr *attr;
8237 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
8241 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
8242 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
8243 goto nla_put_failure;
8244 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
8245 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
8246 goto nla_put_failure;
8248 nla_nest_end(msg, attr);
8253 nla_nest_cancel(msg, attr);
8257 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
8258 struct devlink_stats *stats)
8262 memset(stats, 0, sizeof(*stats));
8263 for_each_possible_cpu(i) {
8264 struct devlink_stats *cpu_stats;
8265 u64 rx_packets, rx_bytes;
8268 cpu_stats = per_cpu_ptr(trap_stats, i);
8270 start = u64_stats_fetch_begin(&cpu_stats->syncp);
8271 rx_packets = u64_stats_read(&cpu_stats->rx_packets);
8272 rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
8273 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
8275 u64_stats_add(&stats->rx_packets, rx_packets);
8276 u64_stats_add(&stats->rx_bytes, rx_bytes);
8281 devlink_trap_group_stats_put(struct sk_buff *msg,
8282 struct devlink_stats __percpu *trap_stats)
8284 struct devlink_stats stats;
8285 struct nlattr *attr;
8287 devlink_trap_stats_read(trap_stats, &stats);
8289 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8293 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8294 u64_stats_read(&stats.rx_packets),
8296 goto nla_put_failure;
8298 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8299 u64_stats_read(&stats.rx_bytes),
8301 goto nla_put_failure;
8303 nla_nest_end(msg, attr);
8308 nla_nest_cancel(msg, attr);
8312 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8313 const struct devlink_trap_item *trap_item)
8315 struct devlink_stats stats;
8316 struct nlattr *attr;
8320 if (devlink->ops->trap_drop_counter_get) {
8321 err = devlink->ops->trap_drop_counter_get(devlink,
8328 devlink_trap_stats_read(trap_item->stats, &stats);
8330 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8334 if (devlink->ops->trap_drop_counter_get &&
8335 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8337 goto nla_put_failure;
8339 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8340 u64_stats_read(&stats.rx_packets),
8342 goto nla_put_failure;
8344 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8345 u64_stats_read(&stats.rx_bytes),
8347 goto nla_put_failure;
8349 nla_nest_end(msg, attr);
8354 nla_nest_cancel(msg, attr);
8358 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8359 const struct devlink_trap_item *trap_item,
8360 enum devlink_command cmd, u32 portid, u32 seq,
8363 struct devlink_trap_group_item *group_item = trap_item->group_item;
8367 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8371 if (devlink_nl_put_handle(msg, devlink))
8372 goto nla_put_failure;
8374 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8375 group_item->group->name))
8376 goto nla_put_failure;
8378 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8379 goto nla_put_failure;
8381 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8382 goto nla_put_failure;
8384 if (trap_item->trap->generic &&
8385 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8386 goto nla_put_failure;
8388 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8389 goto nla_put_failure;
8391 err = devlink_trap_metadata_put(msg, trap_item->trap);
8393 goto nla_put_failure;
8395 err = devlink_trap_stats_put(msg, devlink, trap_item);
8397 goto nla_put_failure;
8399 genlmsg_end(msg, hdr);
8404 genlmsg_cancel(msg, hdr);
8408 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8409 struct genl_info *info)
8411 struct netlink_ext_ack *extack = info->extack;
8412 struct devlink *devlink = info->user_ptr[0];
8413 struct devlink_trap_item *trap_item;
8414 struct sk_buff *msg;
8417 if (list_empty(&devlink->trap_list))
8420 trap_item = devlink_trap_item_get_from_info(devlink, info);
8422 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8426 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8430 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8431 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8436 return genlmsg_reply(msg, info);
8444 devlink_nl_cmd_trap_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
8445 struct netlink_callback *cb)
8447 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8448 struct devlink_trap_item *trap_item;
8452 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8453 if (idx < state->idx) {
8457 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8458 DEVLINK_CMD_TRAP_NEW,
8459 NETLINK_CB(cb->skb).portid,
8472 const struct devlink_gen_cmd devl_gen_trap = {
8473 .dump_one = devlink_nl_cmd_trap_get_dump_one,
8476 static int __devlink_trap_action_set(struct devlink *devlink,
8477 struct devlink_trap_item *trap_item,
8478 enum devlink_trap_action trap_action,
8479 struct netlink_ext_ack *extack)
8483 if (trap_item->action != trap_action &&
8484 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8485 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8489 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8490 trap_action, extack);
8494 trap_item->action = trap_action;
8499 static int devlink_trap_action_set(struct devlink *devlink,
8500 struct devlink_trap_item *trap_item,
8501 struct genl_info *info)
8503 enum devlink_trap_action trap_action;
8506 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8509 err = devlink_trap_action_get_from_info(info, &trap_action);
8511 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8515 return __devlink_trap_action_set(devlink, trap_item, trap_action,
8519 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8520 struct genl_info *info)
8522 struct netlink_ext_ack *extack = info->extack;
8523 struct devlink *devlink = info->user_ptr[0];
8524 struct devlink_trap_item *trap_item;
8526 if (list_empty(&devlink->trap_list))
8529 trap_item = devlink_trap_item_get_from_info(devlink, info);
8531 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8535 return devlink_trap_action_set(devlink, trap_item, info);
8538 static struct devlink_trap_group_item *
8539 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8541 struct devlink_trap_group_item *group_item;
8543 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8544 if (!strcmp(group_item->group->name, name))
8551 static struct devlink_trap_group_item *
8552 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8554 struct devlink_trap_group_item *group_item;
8556 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8557 if (group_item->group->id == id)
8564 static struct devlink_trap_group_item *
8565 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8566 struct genl_info *info)
8570 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8572 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8574 return devlink_trap_group_item_lookup(devlink, name);
8578 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8579 const struct devlink_trap_group_item *group_item,
8580 enum devlink_command cmd, u32 portid, u32 seq,
8586 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8590 if (devlink_nl_put_handle(msg, devlink))
8591 goto nla_put_failure;
8593 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8594 group_item->group->name))
8595 goto nla_put_failure;
8597 if (group_item->group->generic &&
8598 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8599 goto nla_put_failure;
8601 if (group_item->policer_item &&
8602 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8603 group_item->policer_item->policer->id))
8604 goto nla_put_failure;
8606 err = devlink_trap_group_stats_put(msg, group_item->stats);
8608 goto nla_put_failure;
8610 genlmsg_end(msg, hdr);
8615 genlmsg_cancel(msg, hdr);
8619 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8620 struct genl_info *info)
8622 struct netlink_ext_ack *extack = info->extack;
8623 struct devlink *devlink = info->user_ptr[0];
8624 struct devlink_trap_group_item *group_item;
8625 struct sk_buff *msg;
8628 if (list_empty(&devlink->trap_group_list))
8631 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8633 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8637 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8641 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8642 DEVLINK_CMD_TRAP_GROUP_NEW,
8643 info->snd_portid, info->snd_seq, 0);
8645 goto err_trap_group_fill;
8647 return genlmsg_reply(msg, info);
8649 err_trap_group_fill:
8655 devlink_nl_cmd_trap_group_get_dump_one(struct sk_buff *msg,
8656 struct devlink *devlink,
8657 struct netlink_callback *cb)
8659 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8660 struct devlink_trap_group_item *group_item;
8665 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8666 if (idx < state->idx) {
8670 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8671 DEVLINK_CMD_TRAP_GROUP_NEW,
8672 NETLINK_CB(cb->skb).portid,
8685 const struct devlink_gen_cmd devl_gen_trap_group = {
8686 .dump_one = devlink_nl_cmd_trap_group_get_dump_one,
8690 __devlink_trap_group_action_set(struct devlink *devlink,
8691 struct devlink_trap_group_item *group_item,
8692 enum devlink_trap_action trap_action,
8693 struct netlink_ext_ack *extack)
8695 const char *group_name = group_item->group->name;
8696 struct devlink_trap_item *trap_item;
8699 if (devlink->ops->trap_group_action_set) {
8700 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8701 trap_action, extack);
8705 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8706 if (strcmp(trap_item->group_item->group->name, group_name))
8708 if (trap_item->action != trap_action &&
8709 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8711 trap_item->action = trap_action;
8717 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8718 if (strcmp(trap_item->group_item->group->name, group_name))
8720 err = __devlink_trap_action_set(devlink, trap_item,
8721 trap_action, extack);
8730 devlink_trap_group_action_set(struct devlink *devlink,
8731 struct devlink_trap_group_item *group_item,
8732 struct genl_info *info, bool *p_modified)
8734 enum devlink_trap_action trap_action;
8737 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8740 err = devlink_trap_action_get_from_info(info, &trap_action);
8742 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8746 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8756 static int devlink_trap_group_set(struct devlink *devlink,
8757 struct devlink_trap_group_item *group_item,
8758 struct genl_info *info)
8760 struct devlink_trap_policer_item *policer_item;
8761 struct netlink_ext_ack *extack = info->extack;
8762 const struct devlink_trap_policer *policer;
8763 struct nlattr **attrs = info->attrs;
8767 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8770 if (!devlink->ops->trap_group_set)
8773 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8774 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
8775 if (policer_id && !policer_item) {
8776 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8779 policer = policer_item ? policer_item->policer : NULL;
8781 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8786 group_item->policer_item = policer_item;
8791 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8792 struct genl_info *info)
8794 struct netlink_ext_ack *extack = info->extack;
8795 struct devlink *devlink = info->user_ptr[0];
8796 struct devlink_trap_group_item *group_item;
8797 bool modified = false;
8800 if (list_empty(&devlink->trap_group_list))
8803 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8805 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8809 err = devlink_trap_group_action_set(devlink, group_item, info,
8814 err = devlink_trap_group_set(devlink, group_item, info);
8816 goto err_trap_group_set;
8822 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8826 static struct devlink_trap_policer_item *
8827 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8828 struct genl_info *info)
8832 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8834 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8836 return devlink_trap_policer_item_lookup(devlink, id);
8840 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8841 const struct devlink_trap_policer *policer)
8843 struct nlattr *attr;
8847 if (!devlink->ops->trap_policer_counter_get)
8850 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8854 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8858 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8860 goto nla_put_failure;
8862 nla_nest_end(msg, attr);
8867 nla_nest_cancel(msg, attr);
8872 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8873 const struct devlink_trap_policer_item *policer_item,
8874 enum devlink_command cmd, u32 portid, u32 seq,
8880 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8884 if (devlink_nl_put_handle(msg, devlink))
8885 goto nla_put_failure;
8887 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8888 policer_item->policer->id))
8889 goto nla_put_failure;
8891 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8892 policer_item->rate, DEVLINK_ATTR_PAD))
8893 goto nla_put_failure;
8895 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8896 policer_item->burst, DEVLINK_ATTR_PAD))
8897 goto nla_put_failure;
8899 err = devlink_trap_policer_stats_put(msg, devlink,
8900 policer_item->policer);
8902 goto nla_put_failure;
8904 genlmsg_end(msg, hdr);
8909 genlmsg_cancel(msg, hdr);
8913 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8914 struct genl_info *info)
8916 struct devlink_trap_policer_item *policer_item;
8917 struct netlink_ext_ack *extack = info->extack;
8918 struct devlink *devlink = info->user_ptr[0];
8919 struct sk_buff *msg;
8922 if (list_empty(&devlink->trap_policer_list))
8925 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8926 if (!policer_item) {
8927 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8935 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8936 DEVLINK_CMD_TRAP_POLICER_NEW,
8937 info->snd_portid, info->snd_seq, 0);
8939 goto err_trap_policer_fill;
8941 return genlmsg_reply(msg, info);
8943 err_trap_policer_fill:
8949 devlink_nl_cmd_trap_policer_get_dump_one(struct sk_buff *msg,
8950 struct devlink *devlink,
8951 struct netlink_callback *cb)
8953 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8954 struct devlink_trap_policer_item *policer_item;
8958 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8959 if (idx < state->idx) {
8963 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8964 DEVLINK_CMD_TRAP_POLICER_NEW,
8965 NETLINK_CB(cb->skb).portid,
8978 const struct devlink_gen_cmd devl_gen_trap_policer = {
8979 .dump_one = devlink_nl_cmd_trap_policer_get_dump_one,
8983 devlink_trap_policer_set(struct devlink *devlink,
8984 struct devlink_trap_policer_item *policer_item,
8985 struct genl_info *info)
8987 struct netlink_ext_ack *extack = info->extack;
8988 struct nlattr **attrs = info->attrs;
8992 rate = policer_item->rate;
8993 burst = policer_item->burst;
8995 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
8996 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
8998 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
8999 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
9001 if (rate < policer_item->policer->min_rate) {
9002 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
9006 if (rate > policer_item->policer->max_rate) {
9007 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
9011 if (burst < policer_item->policer->min_burst) {
9012 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
9016 if (burst > policer_item->policer->max_burst) {
9017 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
9021 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
9022 rate, burst, info->extack);
9026 policer_item->rate = rate;
9027 policer_item->burst = burst;
9032 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
9033 struct genl_info *info)
9035 struct devlink_trap_policer_item *policer_item;
9036 struct netlink_ext_ack *extack = info->extack;
9037 struct devlink *devlink = info->user_ptr[0];
9039 if (list_empty(&devlink->trap_policer_list))
9042 if (!devlink->ops->trap_policer_set)
9045 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
9046 if (!policer_item) {
9047 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
9051 return devlink_trap_policer_set(devlink, policer_item, info);
9054 const struct genl_small_ops devlink_nl_ops[56] = {
9056 .cmd = DEVLINK_CMD_GET,
9057 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9058 .doit = devlink_nl_cmd_get_doit,
9059 .dumpit = devlink_nl_instance_iter_dump,
9060 /* can be retrieved by unprivileged users */
9063 .cmd = DEVLINK_CMD_PORT_GET,
9064 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9065 .doit = devlink_nl_cmd_port_get_doit,
9066 .dumpit = devlink_nl_instance_iter_dump,
9067 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9068 /* can be retrieved by unprivileged users */
9071 .cmd = DEVLINK_CMD_PORT_SET,
9072 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9073 .doit = devlink_nl_cmd_port_set_doit,
9074 .flags = GENL_ADMIN_PERM,
9075 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9078 .cmd = DEVLINK_CMD_RATE_GET,
9079 .doit = devlink_nl_cmd_rate_get_doit,
9080 .dumpit = devlink_nl_instance_iter_dump,
9081 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9082 /* can be retrieved by unprivileged users */
9085 .cmd = DEVLINK_CMD_RATE_SET,
9086 .doit = devlink_nl_cmd_rate_set_doit,
9087 .flags = GENL_ADMIN_PERM,
9088 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9091 .cmd = DEVLINK_CMD_RATE_NEW,
9092 .doit = devlink_nl_cmd_rate_new_doit,
9093 .flags = GENL_ADMIN_PERM,
9096 .cmd = DEVLINK_CMD_RATE_DEL,
9097 .doit = devlink_nl_cmd_rate_del_doit,
9098 .flags = GENL_ADMIN_PERM,
9099 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9102 .cmd = DEVLINK_CMD_PORT_SPLIT,
9103 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9104 .doit = devlink_nl_cmd_port_split_doit,
9105 .flags = GENL_ADMIN_PERM,
9106 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9109 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9110 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9111 .doit = devlink_nl_cmd_port_unsplit_doit,
9112 .flags = GENL_ADMIN_PERM,
9113 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9116 .cmd = DEVLINK_CMD_PORT_NEW,
9117 .doit = devlink_nl_cmd_port_new_doit,
9118 .flags = GENL_ADMIN_PERM,
9121 .cmd = DEVLINK_CMD_PORT_DEL,
9122 .doit = devlink_nl_cmd_port_del_doit,
9123 .flags = GENL_ADMIN_PERM,
9126 .cmd = DEVLINK_CMD_LINECARD_GET,
9127 .doit = devlink_nl_cmd_linecard_get_doit,
9128 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9129 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9130 /* can be retrieved by unprivileged users */
9133 .cmd = DEVLINK_CMD_LINECARD_SET,
9134 .doit = devlink_nl_cmd_linecard_set_doit,
9135 .flags = GENL_ADMIN_PERM,
9136 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9139 .cmd = DEVLINK_CMD_SB_GET,
9140 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9141 .doit = devlink_nl_cmd_sb_get_doit,
9142 .dumpit = devlink_nl_instance_iter_dump,
9143 /* can be retrieved by unprivileged users */
9146 .cmd = DEVLINK_CMD_SB_POOL_GET,
9147 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9148 .doit = devlink_nl_cmd_sb_pool_get_doit,
9149 .dumpit = devlink_nl_instance_iter_dump,
9150 /* can be retrieved by unprivileged users */
9153 .cmd = DEVLINK_CMD_SB_POOL_SET,
9154 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9155 .doit = devlink_nl_cmd_sb_pool_set_doit,
9156 .flags = GENL_ADMIN_PERM,
9159 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9160 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9161 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9162 .dumpit = devlink_nl_instance_iter_dump,
9163 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9164 /* can be retrieved by unprivileged users */
9167 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9168 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9169 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9170 .flags = GENL_ADMIN_PERM,
9171 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9174 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9175 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9176 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9177 .dumpit = devlink_nl_instance_iter_dump,
9178 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9179 /* can be retrieved by unprivileged users */
9182 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9183 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9184 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9185 .flags = GENL_ADMIN_PERM,
9186 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9189 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9190 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9191 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9192 .flags = GENL_ADMIN_PERM,
9195 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9196 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9197 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9198 .flags = GENL_ADMIN_PERM,
9201 .cmd = DEVLINK_CMD_ESWITCH_GET,
9202 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9203 .doit = devlink_nl_cmd_eswitch_get_doit,
9204 .flags = GENL_ADMIN_PERM,
9207 .cmd = DEVLINK_CMD_ESWITCH_SET,
9208 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9209 .doit = devlink_nl_cmd_eswitch_set_doit,
9210 .flags = GENL_ADMIN_PERM,
9213 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9214 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9215 .doit = devlink_nl_cmd_dpipe_table_get,
9216 /* can be retrieved by unprivileged users */
9219 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9220 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9221 .doit = devlink_nl_cmd_dpipe_entries_get,
9222 /* can be retrieved by unprivileged users */
9225 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9226 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9227 .doit = devlink_nl_cmd_dpipe_headers_get,
9228 /* can be retrieved by unprivileged users */
9231 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9232 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9233 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9234 .flags = GENL_ADMIN_PERM,
9237 .cmd = DEVLINK_CMD_RESOURCE_SET,
9238 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9239 .doit = devlink_nl_cmd_resource_set,
9240 .flags = GENL_ADMIN_PERM,
9243 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9244 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9245 .doit = devlink_nl_cmd_resource_dump,
9246 /* can be retrieved by unprivileged users */
9249 .cmd = DEVLINK_CMD_RELOAD,
9250 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9251 .doit = devlink_nl_cmd_reload,
9252 .flags = GENL_ADMIN_PERM,
9255 .cmd = DEVLINK_CMD_PARAM_GET,
9256 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9257 .doit = devlink_nl_cmd_param_get_doit,
9258 .dumpit = devlink_nl_instance_iter_dump,
9259 /* can be retrieved by unprivileged users */
9262 .cmd = DEVLINK_CMD_PARAM_SET,
9263 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9264 .doit = devlink_nl_cmd_param_set_doit,
9265 .flags = GENL_ADMIN_PERM,
9268 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9269 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9270 .doit = devlink_nl_cmd_port_param_get_doit,
9271 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9272 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9273 /* can be retrieved by unprivileged users */
9276 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9277 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9278 .doit = devlink_nl_cmd_port_param_set_doit,
9279 .flags = GENL_ADMIN_PERM,
9280 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9283 .cmd = DEVLINK_CMD_REGION_GET,
9284 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9285 .doit = devlink_nl_cmd_region_get_doit,
9286 .dumpit = devlink_nl_instance_iter_dump,
9287 .flags = GENL_ADMIN_PERM,
9290 .cmd = DEVLINK_CMD_REGION_NEW,
9291 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9292 .doit = devlink_nl_cmd_region_new,
9293 .flags = GENL_ADMIN_PERM,
9296 .cmd = DEVLINK_CMD_REGION_DEL,
9297 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9298 .doit = devlink_nl_cmd_region_del,
9299 .flags = GENL_ADMIN_PERM,
9302 .cmd = DEVLINK_CMD_REGION_READ,
9303 .validate = GENL_DONT_VALIDATE_STRICT |
9304 GENL_DONT_VALIDATE_DUMP_STRICT,
9305 .dumpit = devlink_nl_cmd_region_read_dumpit,
9306 .flags = GENL_ADMIN_PERM,
9309 .cmd = DEVLINK_CMD_INFO_GET,
9310 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9311 .doit = devlink_nl_cmd_info_get_doit,
9312 .dumpit = devlink_nl_instance_iter_dump,
9313 /* can be retrieved by unprivileged users */
9316 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9317 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9318 .doit = devlink_nl_cmd_health_reporter_get_doit,
9319 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9320 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9321 /* can be retrieved by unprivileged users */
9324 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9325 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9326 .doit = devlink_nl_cmd_health_reporter_set_doit,
9327 .flags = GENL_ADMIN_PERM,
9328 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9331 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9332 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9333 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9334 .flags = GENL_ADMIN_PERM,
9335 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9338 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9339 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9340 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9341 .flags = GENL_ADMIN_PERM,
9342 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9345 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9346 .validate = GENL_DONT_VALIDATE_STRICT |
9347 GENL_DONT_VALIDATE_DUMP_STRICT,
9348 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9349 .flags = GENL_ADMIN_PERM,
9352 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9353 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9354 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9355 .flags = GENL_ADMIN_PERM,
9356 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9359 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9360 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9361 .doit = devlink_nl_cmd_health_reporter_test_doit,
9362 .flags = GENL_ADMIN_PERM,
9363 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9366 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9367 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9368 .doit = devlink_nl_cmd_flash_update,
9369 .flags = GENL_ADMIN_PERM,
9372 .cmd = DEVLINK_CMD_TRAP_GET,
9373 .doit = devlink_nl_cmd_trap_get_doit,
9374 .dumpit = devlink_nl_instance_iter_dump,
9375 /* can be retrieved by unprivileged users */
9378 .cmd = DEVLINK_CMD_TRAP_SET,
9379 .doit = devlink_nl_cmd_trap_set_doit,
9380 .flags = GENL_ADMIN_PERM,
9383 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9384 .doit = devlink_nl_cmd_trap_group_get_doit,
9385 .dumpit = devlink_nl_instance_iter_dump,
9386 /* can be retrieved by unprivileged users */
9389 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9390 .doit = devlink_nl_cmd_trap_group_set_doit,
9391 .flags = GENL_ADMIN_PERM,
9394 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9395 .doit = devlink_nl_cmd_trap_policer_get_doit,
9396 .dumpit = devlink_nl_instance_iter_dump,
9397 /* can be retrieved by unprivileged users */
9400 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9401 .doit = devlink_nl_cmd_trap_policer_set_doit,
9402 .flags = GENL_ADMIN_PERM,
9405 .cmd = DEVLINK_CMD_SELFTESTS_GET,
9406 .doit = devlink_nl_cmd_selftests_get_doit,
9407 .dumpit = devlink_nl_instance_iter_dump,
9408 /* can be retrieved by unprivileged users */
9411 .cmd = DEVLINK_CMD_SELFTESTS_RUN,
9412 .doit = devlink_nl_cmd_selftests_run,
9413 .flags = GENL_ADMIN_PERM,
9415 /* -- No new ops here! Use split ops going forward! -- */
9418 bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9420 const struct devlink_reload_combination *comb;
9423 if (!devlink_reload_supported(ops)) {
9424 if (WARN_ON(ops->reload_actions))
9429 if (WARN_ON(!ops->reload_actions ||
9430 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9431 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9434 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9435 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9438 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
9439 comb = &devlink_reload_invalid_combinations[i];
9440 if (ops->reload_actions == BIT(comb->action) &&
9441 ops->reload_limits == BIT(comb->limit))
9448 devlink_trap_policer_notify(struct devlink *devlink,
9449 const struct devlink_trap_policer_item *policer_item,
9450 enum devlink_command cmd);
9452 devlink_trap_group_notify(struct devlink *devlink,
9453 const struct devlink_trap_group_item *group_item,
9454 enum devlink_command cmd);
9455 static void devlink_trap_notify(struct devlink *devlink,
9456 const struct devlink_trap_item *trap_item,
9457 enum devlink_command cmd);
9459 void devlink_notify_register(struct devlink *devlink)
9461 struct devlink_trap_policer_item *policer_item;
9462 struct devlink_trap_group_item *group_item;
9463 struct devlink_param_item *param_item;
9464 struct devlink_trap_item *trap_item;
9465 struct devlink_port *devlink_port;
9466 struct devlink_linecard *linecard;
9467 struct devlink_rate *rate_node;
9468 struct devlink_region *region;
9469 unsigned long port_index;
9471 devlink_notify(devlink, DEVLINK_CMD_NEW);
9472 list_for_each_entry(linecard, &devlink->linecard_list, list)
9473 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9475 xa_for_each(&devlink->ports, port_index, devlink_port)
9476 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9478 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9479 devlink_trap_policer_notify(devlink, policer_item,
9480 DEVLINK_CMD_TRAP_POLICER_NEW);
9482 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9483 devlink_trap_group_notify(devlink, group_item,
9484 DEVLINK_CMD_TRAP_GROUP_NEW);
9486 list_for_each_entry(trap_item, &devlink->trap_list, list)
9487 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9489 list_for_each_entry(rate_node, &devlink->rate_list, list)
9490 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9492 list_for_each_entry(region, &devlink->region_list, list)
9493 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9495 list_for_each_entry(param_item, &devlink->param_list, list)
9496 devlink_param_notify(devlink, 0, param_item,
9497 DEVLINK_CMD_PARAM_NEW);
9500 void devlink_notify_unregister(struct devlink *devlink)
9502 struct devlink_trap_policer_item *policer_item;
9503 struct devlink_trap_group_item *group_item;
9504 struct devlink_param_item *param_item;
9505 struct devlink_trap_item *trap_item;
9506 struct devlink_port *devlink_port;
9507 struct devlink_rate *rate_node;
9508 struct devlink_region *region;
9509 unsigned long port_index;
9511 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9512 devlink_param_notify(devlink, 0, param_item,
9513 DEVLINK_CMD_PARAM_DEL);
9515 list_for_each_entry_reverse(region, &devlink->region_list, list)
9516 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9518 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9519 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9521 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9522 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9524 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9525 devlink_trap_group_notify(devlink, group_item,
9526 DEVLINK_CMD_TRAP_GROUP_DEL);
9527 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9529 devlink_trap_policer_notify(devlink, policer_item,
9530 DEVLINK_CMD_TRAP_POLICER_DEL);
9532 xa_for_each(&devlink->ports, port_index, devlink_port)
9533 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9534 devlink_notify(devlink, DEVLINK_CMD_DEL);
9537 static void devlink_port_type_warn(struct work_struct *work)
9539 WARN(true, "Type was not set for devlink port.");
9542 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9544 /* Ignore CPU and DSA flavours. */
9545 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9546 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9547 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9550 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9552 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9554 if (!devlink_port_type_should_warn(devlink_port))
9556 /* Schedule a work to WARN in case driver does not set port
9557 * type within timeout.
9559 schedule_delayed_work(&devlink_port->type_warn_dw,
9560 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9563 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9565 if (!devlink_port_type_should_warn(devlink_port))
9567 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9571 * devlink_port_init() - Init devlink port
9574 * @devlink_port: devlink port
9576 * Initialize essencial stuff that is needed for functions
9577 * that may be called before devlink port registration.
9578 * Call to this function is optional and not needed
9579 * in case the driver does not use such functions.
9581 void devlink_port_init(struct devlink *devlink,
9582 struct devlink_port *devlink_port)
9584 if (devlink_port->initialized)
9586 devlink_port->devlink = devlink;
9587 INIT_LIST_HEAD(&devlink_port->region_list);
9588 devlink_port->initialized = true;
9590 EXPORT_SYMBOL_GPL(devlink_port_init);
9593 * devlink_port_fini() - Deinitialize devlink port
9595 * @devlink_port: devlink port
9597 * Deinitialize essencial stuff that is in use for functions
9598 * that may be called after devlink port unregistration.
9599 * Call to this function is optional and not needed
9600 * in case the driver does not use such functions.
9602 void devlink_port_fini(struct devlink_port *devlink_port)
9604 WARN_ON(!list_empty(&devlink_port->region_list));
9606 EXPORT_SYMBOL_GPL(devlink_port_fini);
9609 * devl_port_register() - Register devlink port
9612 * @devlink_port: devlink port
9613 * @port_index: driver-specific numerical identifier of the port
9615 * Register devlink port with provided port index. User can use
9616 * any indexing, even hw-related one. devlink_port structure
9617 * is convenient to be embedded inside user driver private structure.
9618 * Note that the caller should take care of zeroing the devlink_port
9621 int devl_port_register(struct devlink *devlink,
9622 struct devlink_port *devlink_port,
9623 unsigned int port_index)
9627 devl_assert_locked(devlink);
9629 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9631 devlink_port_init(devlink, devlink_port);
9632 devlink_port->registered = true;
9633 devlink_port->index = port_index;
9634 spin_lock_init(&devlink_port->type_lock);
9635 INIT_LIST_HEAD(&devlink_port->reporter_list);
9636 mutex_init(&devlink_port->reporters_lock);
9637 err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
9639 mutex_destroy(&devlink_port->reporters_lock);
9643 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9644 devlink_port_type_warn_schedule(devlink_port);
9645 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9648 EXPORT_SYMBOL_GPL(devl_port_register);
9651 * devlink_port_register - Register devlink port
9654 * @devlink_port: devlink port
9655 * @port_index: driver-specific numerical identifier of the port
9657 * Register devlink port with provided port index. User can use
9658 * any indexing, even hw-related one. devlink_port structure
9659 * is convenient to be embedded inside user driver private structure.
9660 * Note that the caller should take care of zeroing the devlink_port
9663 * Context: Takes and release devlink->lock <mutex>.
9665 int devlink_port_register(struct devlink *devlink,
9666 struct devlink_port *devlink_port,
9667 unsigned int port_index)
9672 err = devl_port_register(devlink, devlink_port, port_index);
9673 devl_unlock(devlink);
9676 EXPORT_SYMBOL_GPL(devlink_port_register);
9679 * devl_port_unregister() - Unregister devlink port
9681 * @devlink_port: devlink port
9683 void devl_port_unregister(struct devlink_port *devlink_port)
9685 lockdep_assert_held(&devlink_port->devlink->lock);
9686 WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
9688 devlink_port_type_warn_cancel(devlink_port);
9689 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9690 xa_erase(&devlink_port->devlink->ports, devlink_port->index);
9691 WARN_ON(!list_empty(&devlink_port->reporter_list));
9692 mutex_destroy(&devlink_port->reporters_lock);
9693 devlink_port->registered = false;
9695 EXPORT_SYMBOL_GPL(devl_port_unregister);
9698 * devlink_port_unregister - Unregister devlink port
9700 * @devlink_port: devlink port
9702 * Context: Takes and release devlink->lock <mutex>.
9704 void devlink_port_unregister(struct devlink_port *devlink_port)
9706 struct devlink *devlink = devlink_port->devlink;
9709 devl_port_unregister(devlink_port);
9710 devl_unlock(devlink);
9712 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9714 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9715 struct net_device *netdev)
9717 const struct net_device_ops *ops = netdev->netdev_ops;
9719 /* If driver registers devlink port, it should set devlink port
9720 * attributes accordingly so the compat functions are called
9721 * and the original ops are not used.
9723 if (ops->ndo_get_phys_port_name) {
9724 /* Some drivers use the same set of ndos for netdevs
9725 * that have devlink_port registered and also for
9726 * those who don't. Make sure that ndo_get_phys_port_name
9727 * returns -EOPNOTSUPP here in case it is defined.
9730 char name[IFNAMSIZ];
9733 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9734 WARN_ON(err != -EOPNOTSUPP);
9736 if (ops->ndo_get_port_parent_id) {
9737 /* Some drivers use the same set of ndos for netdevs
9738 * that have devlink_port registered and also for
9739 * those who don't. Make sure that ndo_get_port_parent_id
9740 * returns -EOPNOTSUPP here in case it is defined.
9743 struct netdev_phys_item_id ppid;
9746 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9747 WARN_ON(err != -EOPNOTSUPP);
9751 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9752 enum devlink_port_type type,
9755 struct net_device *netdev = type_dev;
9757 ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
9759 if (type == DEVLINK_PORT_TYPE_NOTSET) {
9760 devlink_port_type_warn_schedule(devlink_port);
9762 devlink_port_type_warn_cancel(devlink_port);
9763 if (type == DEVLINK_PORT_TYPE_ETH && netdev)
9764 devlink_port_type_netdev_checks(devlink_port, netdev);
9767 spin_lock_bh(&devlink_port->type_lock);
9768 devlink_port->type = type;
9770 case DEVLINK_PORT_TYPE_ETH:
9771 devlink_port->type_eth.netdev = netdev;
9774 devlink_port->type_eth.ifindex = netdev->ifindex;
9775 BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
9776 sizeof(netdev->name));
9777 strcpy(devlink_port->type_eth.ifname, netdev->name);
9780 case DEVLINK_PORT_TYPE_IB:
9781 devlink_port->type_ib.ibdev = type_dev;
9786 spin_unlock_bh(&devlink_port->type_lock);
9787 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9791 * devlink_port_type_eth_set - Set port type to Ethernet
9793 * @devlink_port: devlink port
9795 * If driver is calling this, most likely it is doing something wrong.
9797 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
9799 dev_warn(devlink_port->devlink->dev,
9800 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9801 devlink_port->index);
9802 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
9804 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9807 * devlink_port_type_ib_set - Set port type to InfiniBand
9809 * @devlink_port: devlink port
9810 * @ibdev: related IB device
9812 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9813 struct ib_device *ibdev)
9815 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9817 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9820 * devlink_port_type_clear - Clear port type
9822 * @devlink_port: devlink port
9824 * If driver is calling this for clearing Ethernet type, most likely
9825 * it is doing something wrong.
9827 void devlink_port_type_clear(struct devlink_port *devlink_port)
9829 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9830 dev_warn(devlink_port->devlink->dev,
9831 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
9832 devlink_port->index);
9833 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9835 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9837 int devlink_port_netdevice_event(struct notifier_block *nb,
9838 unsigned long event, void *ptr)
9840 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
9841 struct devlink_port *devlink_port = netdev->devlink_port;
9842 struct devlink *devlink;
9844 devlink = container_of(nb, struct devlink, netdevice_nb);
9846 if (!devlink_port || devlink_port->devlink != devlink)
9850 case NETDEV_POST_INIT:
9851 /* Set the type but not netdev pointer. It is going to be set
9852 * later on by NETDEV_REGISTER event. Happens once during
9853 * netdevice register
9855 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
9858 case NETDEV_REGISTER:
9859 case NETDEV_CHANGENAME:
9860 /* Set the netdev on top of previously set type. Note this
9861 * event happens also during net namespace change so here
9862 * we take into account netdev pointer appearing in this
9865 __devlink_port_type_set(devlink_port, devlink_port->type,
9868 case NETDEV_UNREGISTER:
9869 /* Clear netdev pointer, but not the type. This event happens
9870 * also during net namespace change so we need to clear
9871 * pointer to netdev that is going to another net namespace.
9873 __devlink_port_type_set(devlink_port, devlink_port->type,
9876 case NETDEV_PRE_UNINIT:
9877 /* Clear the type and the netdev pointer. Happens one during
9878 * netdevice unregister.
9880 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
9888 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9889 enum devlink_port_flavour flavour)
9891 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9893 devlink_port->attrs_set = true;
9894 attrs->flavour = flavour;
9895 if (attrs->switch_id.id_len) {
9896 devlink_port->switch_port = true;
9897 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9898 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9900 devlink_port->switch_port = false;
9906 * devlink_port_attrs_set - Set port attributes
9908 * @devlink_port: devlink port
9909 * @attrs: devlink port attrs
9911 void devlink_port_attrs_set(struct devlink_port *devlink_port,
9912 struct devlink_port_attrs *attrs)
9916 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9918 devlink_port->attrs = *attrs;
9919 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
9922 WARN_ON(attrs->splittable && attrs->split);
9924 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
9927 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
9929 * @devlink_port: devlink port
9930 * @controller: associated controller number for the devlink port instance
9931 * @pf: associated PF for the devlink port instance
9932 * @external: indicates if the port is for an external controller
9934 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
9935 u16 pf, bool external)
9937 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9940 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9942 ret = __devlink_port_attrs_set(devlink_port,
9943 DEVLINK_PORT_FLAVOUR_PCI_PF);
9946 attrs->pci_pf.controller = controller;
9947 attrs->pci_pf.pf = pf;
9948 attrs->pci_pf.external = external;
9950 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
9953 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
9955 * @devlink_port: devlink port
9956 * @controller: associated controller number for the devlink port instance
9957 * @pf: associated PF for the devlink port instance
9958 * @vf: associated VF of a PF for the devlink port instance
9959 * @external: indicates if the port is for an external controller
9961 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
9962 u16 pf, u16 vf, bool external)
9964 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9967 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9969 ret = __devlink_port_attrs_set(devlink_port,
9970 DEVLINK_PORT_FLAVOUR_PCI_VF);
9973 attrs->pci_vf.controller = controller;
9974 attrs->pci_vf.pf = pf;
9975 attrs->pci_vf.vf = vf;
9976 attrs->pci_vf.external = external;
9978 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
9981 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
9983 * @devlink_port: devlink port
9984 * @controller: associated controller number for the devlink port instance
9985 * @pf: associated PF for the devlink port instance
9986 * @sf: associated SF of a PF for the devlink port instance
9987 * @external: indicates if the port is for an external controller
9989 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
9990 u16 pf, u32 sf, bool external)
9992 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9995 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9997 ret = __devlink_port_attrs_set(devlink_port,
9998 DEVLINK_PORT_FLAVOUR_PCI_SF);
10001 attrs->pci_sf.controller = controller;
10002 attrs->pci_sf.pf = pf;
10003 attrs->pci_sf.sf = sf;
10004 attrs->pci_sf.external = external;
10006 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
10009 * devl_rate_node_create - create devlink rate node
10010 * @devlink: devlink instance
10011 * @priv: driver private data
10012 * @node_name: name of the resulting node
10013 * @parent: parent devlink_rate struct
10015 * Create devlink rate object of type node
10017 struct devlink_rate *
10018 devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name,
10019 struct devlink_rate *parent)
10021 struct devlink_rate *rate_node;
10023 rate_node = devlink_rate_node_get_by_name(devlink, node_name);
10024 if (!IS_ERR(rate_node))
10025 return ERR_PTR(-EEXIST);
10027 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
10029 return ERR_PTR(-ENOMEM);
10032 rate_node->parent = parent;
10033 refcount_inc(&rate_node->parent->refcnt);
10036 rate_node->type = DEVLINK_RATE_TYPE_NODE;
10037 rate_node->devlink = devlink;
10038 rate_node->priv = priv;
10040 rate_node->name = kstrdup(node_name, GFP_KERNEL);
10041 if (!rate_node->name) {
10043 return ERR_PTR(-ENOMEM);
10046 refcount_set(&rate_node->refcnt, 1);
10047 list_add(&rate_node->list, &devlink->rate_list);
10048 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
10051 EXPORT_SYMBOL_GPL(devl_rate_node_create);
10054 * devl_rate_leaf_create - create devlink rate leaf
10055 * @devlink_port: devlink port object to create rate object on
10056 * @priv: driver private data
10057 * @parent: parent devlink_rate struct
10059 * Create devlink rate object of type leaf on provided @devlink_port.
10061 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv,
10062 struct devlink_rate *parent)
10064 struct devlink *devlink = devlink_port->devlink;
10065 struct devlink_rate *devlink_rate;
10067 devl_assert_locked(devlink_port->devlink);
10069 if (WARN_ON(devlink_port->devlink_rate))
10072 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
10077 devlink_rate->parent = parent;
10078 refcount_inc(&devlink_rate->parent->refcnt);
10081 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
10082 devlink_rate->devlink = devlink;
10083 devlink_rate->devlink_port = devlink_port;
10084 devlink_rate->priv = priv;
10085 list_add_tail(&devlink_rate->list, &devlink->rate_list);
10086 devlink_port->devlink_rate = devlink_rate;
10087 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10091 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10094 * devl_rate_leaf_destroy - destroy devlink rate leaf
10096 * @devlink_port: devlink port linked to the rate object
10098 * Destroy the devlink rate object of type leaf on provided @devlink_port.
10100 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10102 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10104 devl_assert_locked(devlink_port->devlink);
10108 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10109 if (devlink_rate->parent)
10110 refcount_dec(&devlink_rate->parent->refcnt);
10111 list_del(&devlink_rate->list);
10112 devlink_port->devlink_rate = NULL;
10113 kfree(devlink_rate);
10115 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10118 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10119 * @devlink: devlink instance
10121 * Unset parent for all rate objects and destroy all rate nodes
10122 * on specified device.
10124 void devl_rate_nodes_destroy(struct devlink *devlink)
10126 static struct devlink_rate *devlink_rate, *tmp;
10127 const struct devlink_ops *ops = devlink->ops;
10129 devl_assert_locked(devlink);
10131 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10132 if (!devlink_rate->parent)
10135 refcount_dec(&devlink_rate->parent->refcnt);
10136 if (devlink_rate_is_leaf(devlink_rate))
10137 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10139 else if (devlink_rate_is_node(devlink_rate))
10140 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10143 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10144 if (devlink_rate_is_node(devlink_rate)) {
10145 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10146 list_del(&devlink_rate->list);
10147 kfree(devlink_rate->name);
10148 kfree(devlink_rate);
10152 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10155 * devlink_port_linecard_set - Link port with a linecard
10157 * @devlink_port: devlink port
10158 * @linecard: devlink linecard
10160 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10161 struct devlink_linecard *linecard)
10163 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10165 devlink_port->linecard = linecard;
10167 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10169 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10170 char *name, size_t len)
10172 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10175 if (!devlink_port->attrs_set)
10176 return -EOPNOTSUPP;
10178 switch (attrs->flavour) {
10179 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10180 if (devlink_port->linecard)
10181 n = snprintf(name, len, "l%u",
10182 devlink_port->linecard->index);
10184 n += snprintf(name + n, len - n, "p%u",
10185 attrs->phys.port_number);
10186 if (n < len && attrs->split)
10187 n += snprintf(name + n, len - n, "s%u",
10188 attrs->phys.split_subport_number);
10190 case DEVLINK_PORT_FLAVOUR_CPU:
10191 case DEVLINK_PORT_FLAVOUR_DSA:
10192 case DEVLINK_PORT_FLAVOUR_UNUSED:
10193 /* As CPU and DSA ports do not have a netdevice associated
10194 * case should not ever happen.
10198 case DEVLINK_PORT_FLAVOUR_PCI_PF:
10199 if (attrs->pci_pf.external) {
10200 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10206 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10208 case DEVLINK_PORT_FLAVOUR_PCI_VF:
10209 if (attrs->pci_vf.external) {
10210 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10216 n = snprintf(name, len, "pf%uvf%u",
10217 attrs->pci_vf.pf, attrs->pci_vf.vf);
10219 case DEVLINK_PORT_FLAVOUR_PCI_SF:
10220 if (attrs->pci_sf.external) {
10221 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10227 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10230 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10231 return -EOPNOTSUPP;
10240 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10242 struct devlink_linecard_type *linecard_type;
10243 unsigned int count;
10246 count = linecard->ops->types_count(linecard, linecard->priv);
10247 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10249 if (!linecard->types)
10251 linecard->types_count = count;
10253 for (i = 0; i < count; i++) {
10254 linecard_type = &linecard->types[i];
10255 linecard->ops->types_get(linecard, linecard->priv, i,
10256 &linecard_type->type,
10257 &linecard_type->priv);
10262 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10264 kfree(linecard->types);
10268 * devl_linecard_create - Create devlink linecard
10270 * @devlink: devlink
10271 * @linecard_index: driver-specific numerical identifier of the linecard
10272 * @ops: linecards ops
10273 * @priv: user priv pointer
10275 * Create devlink linecard instance with provided linecard index.
10276 * Caller can use any indexing, even hw-related one.
10278 * Return: Line card structure or an ERR_PTR() encoded error code.
10280 struct devlink_linecard *
10281 devl_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10282 const struct devlink_linecard_ops *ops, void *priv)
10284 struct devlink_linecard *linecard;
10287 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10288 !ops->types_count || !ops->types_get))
10289 return ERR_PTR(-EINVAL);
10291 if (devlink_linecard_index_exists(devlink, linecard_index))
10292 return ERR_PTR(-EEXIST);
10294 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10296 return ERR_PTR(-ENOMEM);
10298 linecard->devlink = devlink;
10299 linecard->index = linecard_index;
10300 linecard->ops = ops;
10301 linecard->priv = priv;
10302 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10303 mutex_init(&linecard->state_lock);
10305 err = devlink_linecard_types_init(linecard);
10307 mutex_destroy(&linecard->state_lock);
10309 return ERR_PTR(err);
10312 list_add_tail(&linecard->list, &devlink->linecard_list);
10313 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10316 EXPORT_SYMBOL_GPL(devl_linecard_create);
10319 * devl_linecard_destroy - Destroy devlink linecard
10321 * @linecard: devlink linecard
10323 void devl_linecard_destroy(struct devlink_linecard *linecard)
10325 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10326 list_del(&linecard->list);
10327 devlink_linecard_types_fini(linecard);
10328 mutex_destroy(&linecard->state_lock);
10331 EXPORT_SYMBOL_GPL(devl_linecard_destroy);
10334 * devlink_linecard_provision_set - Set provisioning on linecard
10336 * @linecard: devlink linecard
10337 * @type: linecard type
10339 * This is either called directly from the provision() op call or
10340 * as a result of the provision() op call asynchronously.
10342 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10345 mutex_lock(&linecard->state_lock);
10346 WARN_ON(linecard->type && strcmp(linecard->type, type));
10347 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10348 linecard->type = type;
10349 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10350 mutex_unlock(&linecard->state_lock);
10352 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10355 * devlink_linecard_provision_clear - Clear provisioning on linecard
10357 * @linecard: devlink linecard
10359 * This is either called directly from the unprovision() op call or
10360 * as a result of the unprovision() op call asynchronously.
10362 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10364 mutex_lock(&linecard->state_lock);
10365 WARN_ON(linecard->nested_devlink);
10366 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10367 linecard->type = NULL;
10368 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10369 mutex_unlock(&linecard->state_lock);
10371 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10374 * devlink_linecard_provision_fail - Fail provisioning on linecard
10376 * @linecard: devlink linecard
10378 * This is either called directly from the provision() op call or
10379 * as a result of the provision() op call asynchronously.
10381 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10383 mutex_lock(&linecard->state_lock);
10384 WARN_ON(linecard->nested_devlink);
10385 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10386 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10387 mutex_unlock(&linecard->state_lock);
10389 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10392 * devlink_linecard_activate - Set linecard active
10394 * @linecard: devlink linecard
10396 void devlink_linecard_activate(struct devlink_linecard *linecard)
10398 mutex_lock(&linecard->state_lock);
10399 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10400 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10401 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10402 mutex_unlock(&linecard->state_lock);
10404 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10407 * devlink_linecard_deactivate - Set linecard inactive
10409 * @linecard: devlink linecard
10411 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10413 mutex_lock(&linecard->state_lock);
10414 switch (linecard->state) {
10415 case DEVLINK_LINECARD_STATE_ACTIVE:
10416 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10417 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10419 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10420 /* Line card is being deactivated as part
10421 * of unprovisioning flow.
10428 mutex_unlock(&linecard->state_lock);
10430 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10433 * devlink_linecard_nested_dl_set - Attach/detach nested devlink
10434 * instance to linecard.
10436 * @linecard: devlink linecard
10437 * @nested_devlink: devlink instance to attach or NULL to detach
10439 void devlink_linecard_nested_dl_set(struct devlink_linecard *linecard,
10440 struct devlink *nested_devlink)
10442 mutex_lock(&linecard->state_lock);
10443 linecard->nested_devlink = nested_devlink;
10444 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10445 mutex_unlock(&linecard->state_lock);
10447 EXPORT_SYMBOL_GPL(devlink_linecard_nested_dl_set);
10449 int devl_sb_register(struct devlink *devlink, unsigned int sb_index,
10450 u32 size, u16 ingress_pools_count,
10451 u16 egress_pools_count, u16 ingress_tc_count,
10452 u16 egress_tc_count)
10454 struct devlink_sb *devlink_sb;
10456 lockdep_assert_held(&devlink->lock);
10458 if (devlink_sb_index_exists(devlink, sb_index))
10461 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10464 devlink_sb->index = sb_index;
10465 devlink_sb->size = size;
10466 devlink_sb->ingress_pools_count = ingress_pools_count;
10467 devlink_sb->egress_pools_count = egress_pools_count;
10468 devlink_sb->ingress_tc_count = ingress_tc_count;
10469 devlink_sb->egress_tc_count = egress_tc_count;
10470 list_add_tail(&devlink_sb->list, &devlink->sb_list);
10473 EXPORT_SYMBOL_GPL(devl_sb_register);
10475 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10476 u32 size, u16 ingress_pools_count,
10477 u16 egress_pools_count, u16 ingress_tc_count,
10478 u16 egress_tc_count)
10482 devl_lock(devlink);
10483 err = devl_sb_register(devlink, sb_index, size, ingress_pools_count,
10484 egress_pools_count, ingress_tc_count,
10486 devl_unlock(devlink);
10489 EXPORT_SYMBOL_GPL(devlink_sb_register);
10491 void devl_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10493 struct devlink_sb *devlink_sb;
10495 lockdep_assert_held(&devlink->lock);
10497 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10498 WARN_ON(!devlink_sb);
10499 list_del(&devlink_sb->list);
10502 EXPORT_SYMBOL_GPL(devl_sb_unregister);
10504 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10506 devl_lock(devlink);
10507 devl_sb_unregister(devlink, sb_index);
10508 devl_unlock(devlink);
10510 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10513 * devl_dpipe_headers_register - register dpipe headers
10515 * @devlink: devlink
10516 * @dpipe_headers: dpipe header array
10518 * Register the headers supported by hardware.
10520 void devl_dpipe_headers_register(struct devlink *devlink,
10521 struct devlink_dpipe_headers *dpipe_headers)
10523 lockdep_assert_held(&devlink->lock);
10525 devlink->dpipe_headers = dpipe_headers;
10527 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
10530 * devl_dpipe_headers_unregister - unregister dpipe headers
10532 * @devlink: devlink
10534 * Unregister the headers supported by hardware.
10536 void devl_dpipe_headers_unregister(struct devlink *devlink)
10538 lockdep_assert_held(&devlink->lock);
10540 devlink->dpipe_headers = NULL;
10542 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
10545 * devlink_dpipe_table_counter_enabled - check if counter allocation
10547 * @devlink: devlink
10548 * @table_name: tables name
10550 * Used by driver to check if counter allocation is required.
10551 * After counter allocation is turned on the table entries
10552 * are updated to include counter statistics.
10554 * After that point on the driver must respect the counter
10555 * state so that each entry added to the table is added
10558 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10559 const char *table_name)
10561 struct devlink_dpipe_table *table;
10565 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10566 table_name, devlink);
10569 enabled = table->counters_enabled;
10573 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10576 * devl_dpipe_table_register - register dpipe table
10578 * @devlink: devlink
10579 * @table_name: table name
10580 * @table_ops: table ops
10582 * @counter_control_extern: external control for counters
10584 int devl_dpipe_table_register(struct devlink *devlink,
10585 const char *table_name,
10586 struct devlink_dpipe_table_ops *table_ops,
10587 void *priv, bool counter_control_extern)
10589 struct devlink_dpipe_table *table;
10591 lockdep_assert_held(&devlink->lock);
10593 if (WARN_ON(!table_ops->size_get))
10596 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10600 table = kzalloc(sizeof(*table), GFP_KERNEL);
10604 table->name = table_name;
10605 table->table_ops = table_ops;
10606 table->priv = priv;
10607 table->counter_control_extern = counter_control_extern;
10609 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10613 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
10616 * devl_dpipe_table_unregister - unregister dpipe table
10618 * @devlink: devlink
10619 * @table_name: table name
10621 void devl_dpipe_table_unregister(struct devlink *devlink,
10622 const char *table_name)
10624 struct devlink_dpipe_table *table;
10626 lockdep_assert_held(&devlink->lock);
10628 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10629 table_name, devlink);
10632 list_del_rcu(&table->list);
10633 kfree_rcu(table, rcu);
10635 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
10638 * devl_resource_register - devlink resource register
10640 * @devlink: devlink
10641 * @resource_name: resource's name
10642 * @resource_size: resource's size
10643 * @resource_id: resource's id
10644 * @parent_resource_id: resource's parent id
10645 * @size_params: size parameters
10647 * Generic resources should reuse the same names across drivers.
10648 * Please see the generic resources list at:
10649 * Documentation/networking/devlink/devlink-resource.rst
10651 int devl_resource_register(struct devlink *devlink,
10652 const char *resource_name,
10655 u64 parent_resource_id,
10656 const struct devlink_resource_size_params *size_params)
10658 struct devlink_resource *resource;
10659 struct list_head *resource_list;
10660 bool top_hierarchy;
10662 lockdep_assert_held(&devlink->lock);
10664 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10666 resource = devlink_resource_find(devlink, NULL, resource_id);
10670 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10674 if (top_hierarchy) {
10675 resource_list = &devlink->resource_list;
10677 struct devlink_resource *parent_resource;
10679 parent_resource = devlink_resource_find(devlink, NULL,
10680 parent_resource_id);
10681 if (parent_resource) {
10682 resource_list = &parent_resource->resource_list;
10683 resource->parent = parent_resource;
10690 resource->name = resource_name;
10691 resource->size = resource_size;
10692 resource->size_new = resource_size;
10693 resource->id = resource_id;
10694 resource->size_valid = true;
10695 memcpy(&resource->size_params, size_params,
10696 sizeof(resource->size_params));
10697 INIT_LIST_HEAD(&resource->resource_list);
10698 list_add_tail(&resource->list, resource_list);
10702 EXPORT_SYMBOL_GPL(devl_resource_register);
10705 * devlink_resource_register - devlink resource register
10707 * @devlink: devlink
10708 * @resource_name: resource's name
10709 * @resource_size: resource's size
10710 * @resource_id: resource's id
10711 * @parent_resource_id: resource's parent id
10712 * @size_params: size parameters
10714 * Generic resources should reuse the same names across drivers.
10715 * Please see the generic resources list at:
10716 * Documentation/networking/devlink/devlink-resource.rst
10718 * Context: Takes and release devlink->lock <mutex>.
10720 int devlink_resource_register(struct devlink *devlink,
10721 const char *resource_name,
10724 u64 parent_resource_id,
10725 const struct devlink_resource_size_params *size_params)
10729 devl_lock(devlink);
10730 err = devl_resource_register(devlink, resource_name, resource_size,
10731 resource_id, parent_resource_id, size_params);
10732 devl_unlock(devlink);
10735 EXPORT_SYMBOL_GPL(devlink_resource_register);
10737 static void devlink_resource_unregister(struct devlink *devlink,
10738 struct devlink_resource *resource)
10740 struct devlink_resource *tmp, *child_resource;
10742 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10744 devlink_resource_unregister(devlink, child_resource);
10745 list_del(&child_resource->list);
10746 kfree(child_resource);
10751 * devl_resources_unregister - free all resources
10753 * @devlink: devlink
10755 void devl_resources_unregister(struct devlink *devlink)
10757 struct devlink_resource *tmp, *child_resource;
10759 lockdep_assert_held(&devlink->lock);
10761 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10763 devlink_resource_unregister(devlink, child_resource);
10764 list_del(&child_resource->list);
10765 kfree(child_resource);
10768 EXPORT_SYMBOL_GPL(devl_resources_unregister);
10771 * devlink_resources_unregister - free all resources
10773 * @devlink: devlink
10775 * Context: Takes and release devlink->lock <mutex>.
10777 void devlink_resources_unregister(struct devlink *devlink)
10779 devl_lock(devlink);
10780 devl_resources_unregister(devlink);
10781 devl_unlock(devlink);
10783 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10786 * devl_resource_size_get - get and update size
10788 * @devlink: devlink
10789 * @resource_id: the requested resource id
10790 * @p_resource_size: ptr to update
10792 int devl_resource_size_get(struct devlink *devlink,
10794 u64 *p_resource_size)
10796 struct devlink_resource *resource;
10798 lockdep_assert_held(&devlink->lock);
10800 resource = devlink_resource_find(devlink, NULL, resource_id);
10803 *p_resource_size = resource->size_new;
10804 resource->size = resource->size_new;
10807 EXPORT_SYMBOL_GPL(devl_resource_size_get);
10810 * devl_dpipe_table_resource_set - set the resource id
10812 * @devlink: devlink
10813 * @table_name: table name
10814 * @resource_id: resource id
10815 * @resource_units: number of resource's units consumed per table's entry
10817 int devl_dpipe_table_resource_set(struct devlink *devlink,
10818 const char *table_name, u64 resource_id,
10819 u64 resource_units)
10821 struct devlink_dpipe_table *table;
10823 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10824 table_name, devlink);
10828 table->resource_id = resource_id;
10829 table->resource_units = resource_units;
10830 table->resource_valid = true;
10833 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);
10836 * devl_resource_occ_get_register - register occupancy getter
10838 * @devlink: devlink
10839 * @resource_id: resource id
10840 * @occ_get: occupancy getter callback
10841 * @occ_get_priv: occupancy getter callback priv
10843 void devl_resource_occ_get_register(struct devlink *devlink,
10845 devlink_resource_occ_get_t *occ_get,
10846 void *occ_get_priv)
10848 struct devlink_resource *resource;
10850 lockdep_assert_held(&devlink->lock);
10852 resource = devlink_resource_find(devlink, NULL, resource_id);
10853 if (WARN_ON(!resource))
10855 WARN_ON(resource->occ_get);
10857 resource->occ_get = occ_get;
10858 resource->occ_get_priv = occ_get_priv;
10860 EXPORT_SYMBOL_GPL(devl_resource_occ_get_register);
10863 * devlink_resource_occ_get_register - register occupancy getter
10865 * @devlink: devlink
10866 * @resource_id: resource id
10867 * @occ_get: occupancy getter callback
10868 * @occ_get_priv: occupancy getter callback priv
10870 * Context: Takes and release devlink->lock <mutex>.
10872 void devlink_resource_occ_get_register(struct devlink *devlink,
10874 devlink_resource_occ_get_t *occ_get,
10875 void *occ_get_priv)
10877 devl_lock(devlink);
10878 devl_resource_occ_get_register(devlink, resource_id,
10879 occ_get, occ_get_priv);
10880 devl_unlock(devlink);
10882 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10885 * devl_resource_occ_get_unregister - unregister occupancy getter
10887 * @devlink: devlink
10888 * @resource_id: resource id
10890 void devl_resource_occ_get_unregister(struct devlink *devlink,
10893 struct devlink_resource *resource;
10895 lockdep_assert_held(&devlink->lock);
10897 resource = devlink_resource_find(devlink, NULL, resource_id);
10898 if (WARN_ON(!resource))
10900 WARN_ON(!resource->occ_get);
10902 resource->occ_get = NULL;
10903 resource->occ_get_priv = NULL;
10905 EXPORT_SYMBOL_GPL(devl_resource_occ_get_unregister);
10908 * devlink_resource_occ_get_unregister - unregister occupancy getter
10910 * @devlink: devlink
10911 * @resource_id: resource id
10913 * Context: Takes and release devlink->lock <mutex>.
10915 void devlink_resource_occ_get_unregister(struct devlink *devlink,
10918 devl_lock(devlink);
10919 devl_resource_occ_get_unregister(devlink, resource_id);
10920 devl_unlock(devlink);
10922 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
10924 static int devlink_param_verify(const struct devlink_param *param)
10926 if (!param || !param->name || !param->supported_cmodes)
10928 if (param->generic)
10929 return devlink_param_generic_verify(param);
10931 return devlink_param_driver_verify(param);
10935 * devlink_params_register - register configuration parameters
10937 * @devlink: devlink
10938 * @params: configuration parameters array
10939 * @params_count: number of parameters provided
10941 * Register the configuration parameters supported by the driver.
10943 int devlink_params_register(struct devlink *devlink,
10944 const struct devlink_param *params,
10945 size_t params_count)
10947 const struct devlink_param *param = params;
10950 for (i = 0; i < params_count; i++, param++) {
10951 err = devlink_param_register(devlink, param);
10961 for (param--; i > 0; i--, param--)
10962 devlink_param_unregister(devlink, param);
10965 EXPORT_SYMBOL_GPL(devlink_params_register);
10968 * devlink_params_unregister - unregister configuration parameters
10969 * @devlink: devlink
10970 * @params: configuration parameters to unregister
10971 * @params_count: number of parameters provided
10973 void devlink_params_unregister(struct devlink *devlink,
10974 const struct devlink_param *params,
10975 size_t params_count)
10977 const struct devlink_param *param = params;
10980 for (i = 0; i < params_count; i++, param++)
10981 devlink_param_unregister(devlink, param);
10983 EXPORT_SYMBOL_GPL(devlink_params_unregister);
10986 * devlink_param_register - register one configuration parameter
10988 * @devlink: devlink
10989 * @param: one configuration parameter
10991 * Register the configuration parameter supported by the driver.
10992 * Return: returns 0 on successful registration or error code otherwise.
10994 int devlink_param_register(struct devlink *devlink,
10995 const struct devlink_param *param)
10997 struct devlink_param_item *param_item;
10999 WARN_ON(devlink_param_verify(param));
11000 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
11002 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
11003 WARN_ON(param->get || param->set);
11005 WARN_ON(!param->get || !param->set);
11007 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
11011 param_item->param = param;
11013 list_add_tail(¶m_item->list, &devlink->param_list);
11014 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11017 EXPORT_SYMBOL_GPL(devlink_param_register);
11020 * devlink_param_unregister - unregister one configuration parameter
11021 * @devlink: devlink
11022 * @param: configuration parameter to unregister
11024 void devlink_param_unregister(struct devlink *devlink,
11025 const struct devlink_param *param)
11027 struct devlink_param_item *param_item;
11030 devlink_param_find_by_name(&devlink->param_list, param->name);
11031 WARN_ON(!param_item);
11032 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_DEL);
11033 list_del(¶m_item->list);
11036 EXPORT_SYMBOL_GPL(devlink_param_unregister);
11039 * devlink_param_driverinit_value_get - get configuration parameter
11040 * value for driver initializing
11042 * @devlink: devlink
11043 * @param_id: parameter ID
11044 * @init_val: value of parameter in driverinit configuration mode
11046 * This function should be used by the driver to get driverinit
11047 * configuration for initialization after reload command.
11049 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
11050 union devlink_param_value *init_val)
11052 struct devlink_param_item *param_item;
11054 if (!devlink_reload_supported(devlink->ops))
11055 return -EOPNOTSUPP;
11057 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11061 if (!param_item->driverinit_value_valid ||
11062 !devlink_param_cmode_is_supported(param_item->param,
11063 DEVLINK_PARAM_CMODE_DRIVERINIT))
11064 return -EOPNOTSUPP;
11066 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11067 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
11069 *init_val = param_item->driverinit_value;
11073 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
11076 * devlink_param_driverinit_value_set - set value of configuration
11077 * parameter for driverinit
11078 * configuration mode
11080 * @devlink: devlink
11081 * @param_id: parameter ID
11082 * @init_val: value of parameter to set for driverinit configuration mode
11084 * This function should be used by the driver to set driverinit
11085 * configuration mode default value.
11087 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
11088 union devlink_param_value init_val)
11090 struct devlink_param_item *param_item;
11092 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11096 if (!devlink_param_cmode_is_supported(param_item->param,
11097 DEVLINK_PARAM_CMODE_DRIVERINIT))
11098 return -EOPNOTSUPP;
11100 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11101 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
11103 param_item->driverinit_value = init_val;
11104 param_item->driverinit_value_valid = true;
11106 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11109 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
11112 * devlink_param_value_changed - notify devlink on a parameter's value
11113 * change. Should be called by the driver
11114 * right after the change.
11116 * @devlink: devlink
11117 * @param_id: parameter ID
11119 * This function should be used by the driver to notify devlink on value
11120 * change, excluding driverinit configuration mode.
11121 * For driverinit configuration mode driver should use the function
11123 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11125 struct devlink_param_item *param_item;
11127 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11128 WARN_ON(!param_item);
11130 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11132 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11135 * devl_region_create - create a new address region
11137 * @devlink: devlink
11138 * @ops: region operations and name
11139 * @region_max_snapshots: Maximum supported number of snapshots for region
11140 * @region_size: size of region
11142 struct devlink_region *devl_region_create(struct devlink *devlink,
11143 const struct devlink_region_ops *ops,
11144 u32 region_max_snapshots,
11147 struct devlink_region *region;
11149 devl_assert_locked(devlink);
11151 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11152 return ERR_PTR(-EINVAL);
11154 if (devlink_region_get_by_name(devlink, ops->name))
11155 return ERR_PTR(-EEXIST);
11157 region = kzalloc(sizeof(*region), GFP_KERNEL);
11159 return ERR_PTR(-ENOMEM);
11161 region->devlink = devlink;
11162 region->max_snapshots = region_max_snapshots;
11164 region->size = region_size;
11165 INIT_LIST_HEAD(®ion->snapshot_list);
11166 mutex_init(®ion->snapshot_lock);
11167 list_add_tail(®ion->list, &devlink->region_list);
11168 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11172 EXPORT_SYMBOL_GPL(devl_region_create);
11175 * devlink_region_create - create a new address region
11177 * @devlink: devlink
11178 * @ops: region operations and name
11179 * @region_max_snapshots: Maximum supported number of snapshots for region
11180 * @region_size: size of region
11182 * Context: Takes and release devlink->lock <mutex>.
11184 struct devlink_region *
11185 devlink_region_create(struct devlink *devlink,
11186 const struct devlink_region_ops *ops,
11187 u32 region_max_snapshots, u64 region_size)
11189 struct devlink_region *region;
11191 devl_lock(devlink);
11192 region = devl_region_create(devlink, ops, region_max_snapshots,
11194 devl_unlock(devlink);
11197 EXPORT_SYMBOL_GPL(devlink_region_create);
11200 * devlink_port_region_create - create a new address region for a port
11202 * @port: devlink port
11203 * @ops: region operations and name
11204 * @region_max_snapshots: Maximum supported number of snapshots for region
11205 * @region_size: size of region
11207 * Context: Takes and release devlink->lock <mutex>.
11209 struct devlink_region *
11210 devlink_port_region_create(struct devlink_port *port,
11211 const struct devlink_port_region_ops *ops,
11212 u32 region_max_snapshots, u64 region_size)
11214 struct devlink *devlink = port->devlink;
11215 struct devlink_region *region;
11218 ASSERT_DEVLINK_PORT_INITIALIZED(port);
11220 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11221 return ERR_PTR(-EINVAL);
11223 devl_lock(devlink);
11225 if (devlink_port_region_get_by_name(port, ops->name)) {
11230 region = kzalloc(sizeof(*region), GFP_KERNEL);
11236 region->devlink = devlink;
11237 region->port = port;
11238 region->max_snapshots = region_max_snapshots;
11239 region->port_ops = ops;
11240 region->size = region_size;
11241 INIT_LIST_HEAD(®ion->snapshot_list);
11242 mutex_init(®ion->snapshot_lock);
11243 list_add_tail(®ion->list, &port->region_list);
11244 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11246 devl_unlock(devlink);
11250 devl_unlock(devlink);
11251 return ERR_PTR(err);
11253 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11256 * devl_region_destroy - destroy address region
11258 * @region: devlink region to destroy
11260 void devl_region_destroy(struct devlink_region *region)
11262 struct devlink *devlink = region->devlink;
11263 struct devlink_snapshot *snapshot, *ts;
11265 devl_assert_locked(devlink);
11267 /* Free all snapshots of region */
11268 mutex_lock(®ion->snapshot_lock);
11269 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
11270 devlink_region_snapshot_del(region, snapshot);
11271 mutex_unlock(®ion->snapshot_lock);
11273 list_del(®ion->list);
11274 mutex_destroy(®ion->snapshot_lock);
11276 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11279 EXPORT_SYMBOL_GPL(devl_region_destroy);
11282 * devlink_region_destroy - destroy address region
11284 * @region: devlink region to destroy
11286 * Context: Takes and release devlink->lock <mutex>.
11288 void devlink_region_destroy(struct devlink_region *region)
11290 struct devlink *devlink = region->devlink;
11292 devl_lock(devlink);
11293 devl_region_destroy(region);
11294 devl_unlock(devlink);
11296 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11299 * devlink_region_snapshot_id_get - get snapshot ID
11301 * This callback should be called when adding a new snapshot,
11302 * Driver should use the same id for multiple snapshots taken
11303 * on multiple regions at the same time/by the same trigger.
11305 * The caller of this function must use devlink_region_snapshot_id_put
11306 * when finished creating regions using this id.
11308 * Returns zero on success, or a negative error code on failure.
11310 * @devlink: devlink
11311 * @id: storage to return id
11313 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11315 return __devlink_region_snapshot_id_get(devlink, id);
11317 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11320 * devlink_region_snapshot_id_put - put snapshot ID reference
11322 * This should be called by a driver after finishing creating snapshots
11323 * with an id. Doing so ensures that the ID can later be released in the
11324 * event that all snapshots using it have been destroyed.
11326 * @devlink: devlink
11327 * @id: id to release reference on
11329 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11331 __devlink_snapshot_id_decrement(devlink, id);
11333 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11336 * devlink_region_snapshot_create - create a new snapshot
11337 * This will add a new snapshot of a region. The snapshot
11338 * will be stored on the region struct and can be accessed
11339 * from devlink. This is useful for future analyses of snapshots.
11340 * Multiple snapshots can be created on a region.
11341 * The @snapshot_id should be obtained using the getter function.
11343 * @region: devlink region of the snapshot
11344 * @data: snapshot data
11345 * @snapshot_id: snapshot id to be created
11347 int devlink_region_snapshot_create(struct devlink_region *region,
11348 u8 *data, u32 snapshot_id)
11352 mutex_lock(®ion->snapshot_lock);
11353 err = __devlink_region_snapshot_create(region, data, snapshot_id);
11354 mutex_unlock(®ion->snapshot_lock);
11357 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11359 #define DEVLINK_TRAP(_id, _type) \
11361 .type = DEVLINK_TRAP_TYPE_##_type, \
11362 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
11363 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
11366 static const struct devlink_trap devlink_trap_generic[] = {
11367 DEVLINK_TRAP(SMAC_MC, DROP),
11368 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11369 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11370 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11371 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11372 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11373 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11374 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11375 DEVLINK_TRAP(TAIL_DROP, DROP),
11376 DEVLINK_TRAP(NON_IP_PACKET, DROP),
11377 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11378 DEVLINK_TRAP(DIP_LB, DROP),
11379 DEVLINK_TRAP(SIP_MC, DROP),
11380 DEVLINK_TRAP(SIP_LB, DROP),
11381 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11382 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11383 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11384 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11385 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11386 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11387 DEVLINK_TRAP(RPF, EXCEPTION),
11388 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11389 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11390 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11391 DEVLINK_TRAP(NON_ROUTABLE, DROP),
11392 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11393 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11394 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11395 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11396 DEVLINK_TRAP(STP, CONTROL),
11397 DEVLINK_TRAP(LACP, CONTROL),
11398 DEVLINK_TRAP(LLDP, CONTROL),
11399 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11400 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11401 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11402 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11403 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11404 DEVLINK_TRAP(MLD_QUERY, CONTROL),
11405 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11406 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11407 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11408 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11409 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11410 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11411 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11412 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11413 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11414 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11415 DEVLINK_TRAP(IPV4_BFD, CONTROL),
11416 DEVLINK_TRAP(IPV6_BFD, CONTROL),
11417 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11418 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11419 DEVLINK_TRAP(IPV4_BGP, CONTROL),
11420 DEVLINK_TRAP(IPV6_BGP, CONTROL),
11421 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11422 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11423 DEVLINK_TRAP(IPV4_PIM, CONTROL),
11424 DEVLINK_TRAP(IPV6_PIM, CONTROL),
11425 DEVLINK_TRAP(UC_LB, CONTROL),
11426 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11427 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11428 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11429 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11430 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11431 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11432 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11433 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11434 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11435 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11436 DEVLINK_TRAP(PTP_EVENT, CONTROL),
11437 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11438 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11439 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11440 DEVLINK_TRAP(EARLY_DROP, DROP),
11441 DEVLINK_TRAP(VXLAN_PARSING, DROP),
11442 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11443 DEVLINK_TRAP(VLAN_PARSING, DROP),
11444 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11445 DEVLINK_TRAP(MPLS_PARSING, DROP),
11446 DEVLINK_TRAP(ARP_PARSING, DROP),
11447 DEVLINK_TRAP(IP_1_PARSING, DROP),
11448 DEVLINK_TRAP(IP_N_PARSING, DROP),
11449 DEVLINK_TRAP(GRE_PARSING, DROP),
11450 DEVLINK_TRAP(UDP_PARSING, DROP),
11451 DEVLINK_TRAP(TCP_PARSING, DROP),
11452 DEVLINK_TRAP(IPSEC_PARSING, DROP),
11453 DEVLINK_TRAP(SCTP_PARSING, DROP),
11454 DEVLINK_TRAP(DCCP_PARSING, DROP),
11455 DEVLINK_TRAP(GTP_PARSING, DROP),
11456 DEVLINK_TRAP(ESP_PARSING, DROP),
11457 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11458 DEVLINK_TRAP(DMAC_FILTER, DROP),
11459 DEVLINK_TRAP(EAPOL, CONTROL),
11460 DEVLINK_TRAP(LOCKED_PORT, DROP),
11463 #define DEVLINK_TRAP_GROUP(_id) \
11465 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
11466 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
11469 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11470 DEVLINK_TRAP_GROUP(L2_DROPS),
11471 DEVLINK_TRAP_GROUP(L3_DROPS),
11472 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11473 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11474 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11475 DEVLINK_TRAP_GROUP(ACL_DROPS),
11476 DEVLINK_TRAP_GROUP(STP),
11477 DEVLINK_TRAP_GROUP(LACP),
11478 DEVLINK_TRAP_GROUP(LLDP),
11479 DEVLINK_TRAP_GROUP(MC_SNOOPING),
11480 DEVLINK_TRAP_GROUP(DHCP),
11481 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11482 DEVLINK_TRAP_GROUP(BFD),
11483 DEVLINK_TRAP_GROUP(OSPF),
11484 DEVLINK_TRAP_GROUP(BGP),
11485 DEVLINK_TRAP_GROUP(VRRP),
11486 DEVLINK_TRAP_GROUP(PIM),
11487 DEVLINK_TRAP_GROUP(UC_LB),
11488 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11489 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11490 DEVLINK_TRAP_GROUP(IPV6),
11491 DEVLINK_TRAP_GROUP(PTP_EVENT),
11492 DEVLINK_TRAP_GROUP(PTP_GENERAL),
11493 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11494 DEVLINK_TRAP_GROUP(ACL_TRAP),
11495 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11496 DEVLINK_TRAP_GROUP(EAPOL),
11499 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11501 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11504 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11507 if (trap->type != devlink_trap_generic[trap->id].type)
11513 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11517 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11520 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11521 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11528 static int devlink_trap_verify(const struct devlink_trap *trap)
11530 if (!trap || !trap->name)
11534 return devlink_trap_generic_verify(trap);
11536 return devlink_trap_driver_verify(trap);
11540 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11542 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11545 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11552 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11556 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11559 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11560 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11567 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11569 if (group->generic)
11570 return devlink_trap_group_generic_verify(group);
11572 return devlink_trap_group_driver_verify(group);
11576 devlink_trap_group_notify(struct devlink *devlink,
11577 const struct devlink_trap_group_item *group_item,
11578 enum devlink_command cmd)
11580 struct sk_buff *msg;
11583 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11584 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11585 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11588 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11592 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11599 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11600 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11604 devlink_trap_item_group_link(struct devlink *devlink,
11605 struct devlink_trap_item *trap_item)
11607 u16 group_id = trap_item->trap->init_group_id;
11608 struct devlink_trap_group_item *group_item;
11610 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11611 if (WARN_ON_ONCE(!group_item))
11614 trap_item->group_item = group_item;
11619 static void devlink_trap_notify(struct devlink *devlink,
11620 const struct devlink_trap_item *trap_item,
11621 enum devlink_command cmd)
11623 struct sk_buff *msg;
11626 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11627 cmd != DEVLINK_CMD_TRAP_DEL);
11628 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11631 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11635 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11641 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11642 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11646 devlink_trap_register(struct devlink *devlink,
11647 const struct devlink_trap *trap, void *priv)
11649 struct devlink_trap_item *trap_item;
11652 if (devlink_trap_item_lookup(devlink, trap->name))
11655 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11659 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11660 if (!trap_item->stats) {
11662 goto err_stats_alloc;
11665 trap_item->trap = trap;
11666 trap_item->action = trap->init_action;
11667 trap_item->priv = priv;
11669 err = devlink_trap_item_group_link(devlink, trap_item);
11671 goto err_group_link;
11673 err = devlink->ops->trap_init(devlink, trap, trap_item);
11675 goto err_trap_init;
11677 list_add_tail(&trap_item->list, &devlink->trap_list);
11678 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11684 free_percpu(trap_item->stats);
11690 static void devlink_trap_unregister(struct devlink *devlink,
11691 const struct devlink_trap *trap)
11693 struct devlink_trap_item *trap_item;
11695 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11696 if (WARN_ON_ONCE(!trap_item))
11699 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11700 list_del(&trap_item->list);
11701 if (devlink->ops->trap_fini)
11702 devlink->ops->trap_fini(devlink, trap, trap_item);
11703 free_percpu(trap_item->stats);
11707 static void devlink_trap_disable(struct devlink *devlink,
11708 const struct devlink_trap *trap)
11710 struct devlink_trap_item *trap_item;
11712 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11713 if (WARN_ON_ONCE(!trap_item))
11716 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11718 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11722 * devl_traps_register - Register packet traps with devlink.
11723 * @devlink: devlink.
11724 * @traps: Packet traps.
11725 * @traps_count: Count of provided packet traps.
11726 * @priv: Driver private information.
11728 * Return: Non-zero value on failure.
11730 int devl_traps_register(struct devlink *devlink,
11731 const struct devlink_trap *traps,
11732 size_t traps_count, void *priv)
11736 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11739 devl_assert_locked(devlink);
11740 for (i = 0; i < traps_count; i++) {
11741 const struct devlink_trap *trap = &traps[i];
11743 err = devlink_trap_verify(trap);
11745 goto err_trap_verify;
11747 err = devlink_trap_register(devlink, trap, priv);
11749 goto err_trap_register;
11756 for (i--; i >= 0; i--)
11757 devlink_trap_unregister(devlink, &traps[i]);
11760 EXPORT_SYMBOL_GPL(devl_traps_register);
11763 * devlink_traps_register - Register packet traps with devlink.
11764 * @devlink: devlink.
11765 * @traps: Packet traps.
11766 * @traps_count: Count of provided packet traps.
11767 * @priv: Driver private information.
11769 * Context: Takes and release devlink->lock <mutex>.
11771 * Return: Non-zero value on failure.
11773 int devlink_traps_register(struct devlink *devlink,
11774 const struct devlink_trap *traps,
11775 size_t traps_count, void *priv)
11779 devl_lock(devlink);
11780 err = devl_traps_register(devlink, traps, traps_count, priv);
11781 devl_unlock(devlink);
11784 EXPORT_SYMBOL_GPL(devlink_traps_register);
11787 * devl_traps_unregister - Unregister packet traps from devlink.
11788 * @devlink: devlink.
11789 * @traps: Packet traps.
11790 * @traps_count: Count of provided packet traps.
11792 void devl_traps_unregister(struct devlink *devlink,
11793 const struct devlink_trap *traps,
11794 size_t traps_count)
11798 devl_assert_locked(devlink);
11799 /* Make sure we do not have any packets in-flight while unregistering
11800 * traps by disabling all of them and waiting for a grace period.
11802 for (i = traps_count - 1; i >= 0; i--)
11803 devlink_trap_disable(devlink, &traps[i]);
11805 for (i = traps_count - 1; i >= 0; i--)
11806 devlink_trap_unregister(devlink, &traps[i]);
11808 EXPORT_SYMBOL_GPL(devl_traps_unregister);
11811 * devlink_traps_unregister - Unregister packet traps from devlink.
11812 * @devlink: devlink.
11813 * @traps: Packet traps.
11814 * @traps_count: Count of provided packet traps.
11816 * Context: Takes and release devlink->lock <mutex>.
11818 void devlink_traps_unregister(struct devlink *devlink,
11819 const struct devlink_trap *traps,
11820 size_t traps_count)
11822 devl_lock(devlink);
11823 devl_traps_unregister(devlink, traps, traps_count);
11824 devl_unlock(devlink);
11826 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11829 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11832 struct devlink_stats *stats;
11834 stats = this_cpu_ptr(trap_stats);
11835 u64_stats_update_begin(&stats->syncp);
11836 u64_stats_add(&stats->rx_bytes, skb_len);
11837 u64_stats_inc(&stats->rx_packets);
11838 u64_stats_update_end(&stats->syncp);
11842 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11843 const struct devlink_trap_item *trap_item,
11844 struct devlink_port *in_devlink_port,
11845 const struct flow_action_cookie *fa_cookie)
11847 metadata->trap_name = trap_item->trap->name;
11848 metadata->trap_group_name = trap_item->group_item->group->name;
11849 metadata->fa_cookie = fa_cookie;
11850 metadata->trap_type = trap_item->trap->type;
11852 spin_lock(&in_devlink_port->type_lock);
11853 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11854 metadata->input_dev = in_devlink_port->type_eth.netdev;
11855 spin_unlock(&in_devlink_port->type_lock);
11859 * devlink_trap_report - Report trapped packet to drop monitor.
11860 * @devlink: devlink.
11861 * @skb: Trapped packet.
11862 * @trap_ctx: Trap context.
11863 * @in_devlink_port: Input devlink port.
11864 * @fa_cookie: Flow action cookie. Could be NULL.
11866 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11867 void *trap_ctx, struct devlink_port *in_devlink_port,
11868 const struct flow_action_cookie *fa_cookie)
11871 struct devlink_trap_item *trap_item = trap_ctx;
11873 devlink_trap_stats_update(trap_item->stats, skb->len);
11874 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11876 if (trace_devlink_trap_report_enabled()) {
11877 struct devlink_trap_metadata metadata = {};
11879 devlink_trap_report_metadata_set(&metadata, trap_item,
11880 in_devlink_port, fa_cookie);
11881 trace_devlink_trap_report(devlink, skb, &metadata);
11884 EXPORT_SYMBOL_GPL(devlink_trap_report);
11887 * devlink_trap_ctx_priv - Trap context to driver private information.
11888 * @trap_ctx: Trap context.
11890 * Return: Driver private information passed during registration.
11892 void *devlink_trap_ctx_priv(void *trap_ctx)
11894 struct devlink_trap_item *trap_item = trap_ctx;
11896 return trap_item->priv;
11898 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
11901 devlink_trap_group_item_policer_link(struct devlink *devlink,
11902 struct devlink_trap_group_item *group_item)
11904 u32 policer_id = group_item->group->init_policer_id;
11905 struct devlink_trap_policer_item *policer_item;
11907 if (policer_id == 0)
11910 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
11911 if (WARN_ON_ONCE(!policer_item))
11914 group_item->policer_item = policer_item;
11920 devlink_trap_group_register(struct devlink *devlink,
11921 const struct devlink_trap_group *group)
11923 struct devlink_trap_group_item *group_item;
11926 if (devlink_trap_group_item_lookup(devlink, group->name))
11929 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
11933 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11934 if (!group_item->stats) {
11936 goto err_stats_alloc;
11939 group_item->group = group;
11941 err = devlink_trap_group_item_policer_link(devlink, group_item);
11943 goto err_policer_link;
11945 if (devlink->ops->trap_group_init) {
11946 err = devlink->ops->trap_group_init(devlink, group);
11948 goto err_group_init;
11951 list_add_tail(&group_item->list, &devlink->trap_group_list);
11952 devlink_trap_group_notify(devlink, group_item,
11953 DEVLINK_CMD_TRAP_GROUP_NEW);
11959 free_percpu(group_item->stats);
11966 devlink_trap_group_unregister(struct devlink *devlink,
11967 const struct devlink_trap_group *group)
11969 struct devlink_trap_group_item *group_item;
11971 group_item = devlink_trap_group_item_lookup(devlink, group->name);
11972 if (WARN_ON_ONCE(!group_item))
11975 devlink_trap_group_notify(devlink, group_item,
11976 DEVLINK_CMD_TRAP_GROUP_DEL);
11977 list_del(&group_item->list);
11978 free_percpu(group_item->stats);
11983 * devl_trap_groups_register - Register packet trap groups with devlink.
11984 * @devlink: devlink.
11985 * @groups: Packet trap groups.
11986 * @groups_count: Count of provided packet trap groups.
11988 * Return: Non-zero value on failure.
11990 int devl_trap_groups_register(struct devlink *devlink,
11991 const struct devlink_trap_group *groups,
11992 size_t groups_count)
11996 devl_assert_locked(devlink);
11997 for (i = 0; i < groups_count; i++) {
11998 const struct devlink_trap_group *group = &groups[i];
12000 err = devlink_trap_group_verify(group);
12002 goto err_trap_group_verify;
12004 err = devlink_trap_group_register(devlink, group);
12006 goto err_trap_group_register;
12011 err_trap_group_register:
12012 err_trap_group_verify:
12013 for (i--; i >= 0; i--)
12014 devlink_trap_group_unregister(devlink, &groups[i]);
12017 EXPORT_SYMBOL_GPL(devl_trap_groups_register);
12020 * devlink_trap_groups_register - Register packet trap groups with devlink.
12021 * @devlink: devlink.
12022 * @groups: Packet trap groups.
12023 * @groups_count: Count of provided packet trap groups.
12025 * Context: Takes and release devlink->lock <mutex>.
12027 * Return: Non-zero value on failure.
12029 int devlink_trap_groups_register(struct devlink *devlink,
12030 const struct devlink_trap_group *groups,
12031 size_t groups_count)
12035 devl_lock(devlink);
12036 err = devl_trap_groups_register(devlink, groups, groups_count);
12037 devl_unlock(devlink);
12040 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
12043 * devl_trap_groups_unregister - Unregister packet trap groups from devlink.
12044 * @devlink: devlink.
12045 * @groups: Packet trap groups.
12046 * @groups_count: Count of provided packet trap groups.
12048 void devl_trap_groups_unregister(struct devlink *devlink,
12049 const struct devlink_trap_group *groups,
12050 size_t groups_count)
12054 devl_assert_locked(devlink);
12055 for (i = groups_count - 1; i >= 0; i--)
12056 devlink_trap_group_unregister(devlink, &groups[i]);
12058 EXPORT_SYMBOL_GPL(devl_trap_groups_unregister);
12061 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
12062 * @devlink: devlink.
12063 * @groups: Packet trap groups.
12064 * @groups_count: Count of provided packet trap groups.
12066 * Context: Takes and release devlink->lock <mutex>.
12068 void devlink_trap_groups_unregister(struct devlink *devlink,
12069 const struct devlink_trap_group *groups,
12070 size_t groups_count)
12072 devl_lock(devlink);
12073 devl_trap_groups_unregister(devlink, groups, groups_count);
12074 devl_unlock(devlink);
12076 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
12079 devlink_trap_policer_notify(struct devlink *devlink,
12080 const struct devlink_trap_policer_item *policer_item,
12081 enum devlink_command cmd)
12083 struct sk_buff *msg;
12086 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
12087 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
12088 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
12091 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12095 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
12102 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
12103 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
12107 devlink_trap_policer_register(struct devlink *devlink,
12108 const struct devlink_trap_policer *policer)
12110 struct devlink_trap_policer_item *policer_item;
12113 if (devlink_trap_policer_item_lookup(devlink, policer->id))
12116 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
12120 policer_item->policer = policer;
12121 policer_item->rate = policer->init_rate;
12122 policer_item->burst = policer->init_burst;
12124 if (devlink->ops->trap_policer_init) {
12125 err = devlink->ops->trap_policer_init(devlink, policer);
12127 goto err_policer_init;
12130 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
12131 devlink_trap_policer_notify(devlink, policer_item,
12132 DEVLINK_CMD_TRAP_POLICER_NEW);
12137 kfree(policer_item);
12142 devlink_trap_policer_unregister(struct devlink *devlink,
12143 const struct devlink_trap_policer *policer)
12145 struct devlink_trap_policer_item *policer_item;
12147 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
12148 if (WARN_ON_ONCE(!policer_item))
12151 devlink_trap_policer_notify(devlink, policer_item,
12152 DEVLINK_CMD_TRAP_POLICER_DEL);
12153 list_del(&policer_item->list);
12154 if (devlink->ops->trap_policer_fini)
12155 devlink->ops->trap_policer_fini(devlink, policer);
12156 kfree(policer_item);
12160 * devl_trap_policers_register - Register packet trap policers with devlink.
12161 * @devlink: devlink.
12162 * @policers: Packet trap policers.
12163 * @policers_count: Count of provided packet trap policers.
12165 * Return: Non-zero value on failure.
12168 devl_trap_policers_register(struct devlink *devlink,
12169 const struct devlink_trap_policer *policers,
12170 size_t policers_count)
12174 devl_assert_locked(devlink);
12175 for (i = 0; i < policers_count; i++) {
12176 const struct devlink_trap_policer *policer = &policers[i];
12178 if (WARN_ON(policer->id == 0 ||
12179 policer->max_rate < policer->min_rate ||
12180 policer->max_burst < policer->min_burst)) {
12182 goto err_trap_policer_verify;
12185 err = devlink_trap_policer_register(devlink, policer);
12187 goto err_trap_policer_register;
12191 err_trap_policer_register:
12192 err_trap_policer_verify:
12193 for (i--; i >= 0; i--)
12194 devlink_trap_policer_unregister(devlink, &policers[i]);
12197 EXPORT_SYMBOL_GPL(devl_trap_policers_register);
12200 * devl_trap_policers_unregister - Unregister packet trap policers from devlink.
12201 * @devlink: devlink.
12202 * @policers: Packet trap policers.
12203 * @policers_count: Count of provided packet trap policers.
12206 devl_trap_policers_unregister(struct devlink *devlink,
12207 const struct devlink_trap_policer *policers,
12208 size_t policers_count)
12212 devl_assert_locked(devlink);
12213 for (i = policers_count - 1; i >= 0; i--)
12214 devlink_trap_policer_unregister(devlink, &policers[i]);
12216 EXPORT_SYMBOL_GPL(devl_trap_policers_unregister);
12218 static void __devlink_compat_running_version(struct devlink *devlink,
12219 char *buf, size_t len)
12221 struct devlink_info_req req = {};
12222 const struct nlattr *nlattr;
12223 struct sk_buff *msg;
12226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12231 err = devlink->ops->info_get(devlink, &req, NULL);
12235 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12236 const struct nlattr *kv;
12239 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12242 nla_for_each_nested(kv, nlattr, rem_kv) {
12243 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12246 strlcat(buf, nla_data(kv), len);
12247 strlcat(buf, " ", len);
12254 void devlink_compat_running_version(struct devlink *devlink,
12255 char *buf, size_t len)
12257 if (!devlink->ops->info_get)
12260 devl_lock(devlink);
12261 if (devl_is_registered(devlink))
12262 __devlink_compat_running_version(devlink, buf, len);
12263 devl_unlock(devlink);
12266 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12268 struct devlink_flash_update_params params = {};
12271 devl_lock(devlink);
12272 if (!devl_is_registered(devlink)) {
12277 if (!devlink->ops->flash_update) {
12282 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
12286 devlink_flash_update_begin_notify(devlink);
12287 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
12288 devlink_flash_update_end_notify(devlink);
12290 release_firmware(params.fw);
12292 devl_unlock(devlink);
12297 int devlink_compat_phys_port_name_get(struct net_device *dev,
12298 char *name, size_t len)
12300 struct devlink_port *devlink_port;
12302 /* RTNL mutex is held here which ensures that devlink_port
12303 * instance cannot disappear in the middle. No need to take
12304 * any devlink lock as only permanent values are accessed.
12308 devlink_port = dev->devlink_port;
12310 return -EOPNOTSUPP;
12312 return __devlink_port_phys_port_name_get(devlink_port, name, len);
12315 int devlink_compat_switch_id_get(struct net_device *dev,
12316 struct netdev_phys_item_id *ppid)
12318 struct devlink_port *devlink_port;
12320 /* Caller must hold RTNL mutex or reference to dev, which ensures that
12321 * devlink_port instance cannot disappear in the middle. No need to take
12322 * any devlink lock as only permanent values are accessed.
12324 devlink_port = dev->devlink_port;
12325 if (!devlink_port || !devlink_port->switch_port)
12326 return -EOPNOTSUPP;
12328 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));