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;
41 const struct devlink_linecard_ops *ops;
43 enum devlink_linecard_state state;
44 struct mutex state_lock; /* Protects state */
46 struct devlink_linecard_type *types;
47 unsigned int types_count;
48 struct devlink *nested_devlink;
52 * struct devlink_resource - devlink resource
53 * @name: name of the resource
54 * @id: id, per devlink instance
55 * @size: size of the resource
56 * @size_new: updated size of the resource, reload is needed
57 * @size_valid: valid in case the total size of the resource is valid
58 * including its children
59 * @parent: parent resource
60 * @size_params: size parameters
62 * @resource_list: list of child resources
63 * @occ_get: occupancy getter callback
64 * @occ_get_priv: occupancy getter callback priv
66 struct devlink_resource {
72 struct devlink_resource *parent;
73 struct devlink_resource_size_params size_params;
74 struct list_head list;
75 struct list_head resource_list;
76 devlink_resource_occ_get_t *occ_get;
80 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
82 .name = "destination mac",
83 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
88 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
90 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
91 .fields = devlink_dpipe_fields_ethernet,
92 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
95 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ethernet);
97 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
99 .name = "destination ip",
100 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
105 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
107 .id = DEVLINK_DPIPE_HEADER_IPV4,
108 .fields = devlink_dpipe_fields_ipv4,
109 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
112 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv4);
114 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
116 .name = "destination ip",
117 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
122 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
124 .id = DEVLINK_DPIPE_HEADER_IPV6,
125 .fields = devlink_dpipe_fields_ipv6,
126 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
129 EXPORT_SYMBOL_GPL(devlink_dpipe_header_ipv6);
131 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
132 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
133 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
135 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \
136 (_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1)
138 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
139 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
140 [DEVLINK_PORT_FN_ATTR_STATE] =
141 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
142 DEVLINK_PORT_FN_STATE_ACTIVE),
143 [DEVLINK_PORT_FN_ATTR_CAPS] =
144 NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK),
147 static const struct nla_policy devlink_selftest_nl_policy[DEVLINK_ATTR_SELFTEST_ID_MAX + 1] = {
148 [DEVLINK_ATTR_SELFTEST_ID_FLASH] = { .type = NLA_FLAG },
151 #define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port) \
152 WARN_ON_ONCE(!(devlink_port)->registered)
153 #define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port) \
154 WARN_ON_ONCE((devlink_port)->registered)
155 #define ASSERT_DEVLINK_PORT_INITIALIZED(devlink_port) \
156 WARN_ON_ONCE(!(devlink_port)->initialized)
158 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
159 unsigned int port_index)
161 return xa_load(&devlink->ports, port_index);
164 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
165 struct nlattr **attrs)
167 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
168 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
169 struct devlink_port *devlink_port;
171 devlink_port = devlink_port_get_by_index(devlink, port_index);
173 return ERR_PTR(-ENODEV);
176 return ERR_PTR(-EINVAL);
179 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
180 struct genl_info *info)
182 return devlink_port_get_from_attrs(devlink, info->attrs);
186 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
188 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
192 devlink_rate_is_node(struct devlink_rate *devlink_rate)
194 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
197 static struct devlink_rate *
198 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
200 struct devlink_rate *devlink_rate;
201 struct devlink_port *devlink_port;
203 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
204 if (IS_ERR(devlink_port))
205 return ERR_CAST(devlink_port);
206 devlink_rate = devlink_port->devlink_rate;
207 return devlink_rate ?: ERR_PTR(-ENODEV);
210 static struct devlink_rate *
211 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
213 static struct devlink_rate *devlink_rate;
215 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
216 if (devlink_rate_is_node(devlink_rate) &&
217 !strcmp(node_name, devlink_rate->name))
220 return ERR_PTR(-ENODEV);
223 static struct devlink_rate *
224 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
226 const char *rate_node_name;
229 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
230 return ERR_PTR(-EINVAL);
231 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
232 len = strlen(rate_node_name);
233 /* Name cannot be empty or decimal number */
234 if (!len || strspn(rate_node_name, "0123456789") == len)
235 return ERR_PTR(-EINVAL);
237 return devlink_rate_node_get_by_name(devlink, rate_node_name);
240 struct devlink_rate *
241 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
243 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
246 struct devlink_rate *
247 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
249 struct nlattr **attrs = info->attrs;
251 if (attrs[DEVLINK_ATTR_PORT_INDEX])
252 return devlink_rate_leaf_get_from_info(devlink, info);
253 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
254 return devlink_rate_node_get_from_info(devlink, info);
256 return ERR_PTR(-EINVAL);
259 static struct devlink_linecard *
260 devlink_linecard_get_by_index(struct devlink *devlink,
261 unsigned int linecard_index)
263 struct devlink_linecard *devlink_linecard;
265 list_for_each_entry(devlink_linecard, &devlink->linecard_list, list) {
266 if (devlink_linecard->index == linecard_index)
267 return devlink_linecard;
272 static bool devlink_linecard_index_exists(struct devlink *devlink,
273 unsigned int linecard_index)
275 return devlink_linecard_get_by_index(devlink, linecard_index);
278 static struct devlink_linecard *
279 devlink_linecard_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
281 if (attrs[DEVLINK_ATTR_LINECARD_INDEX]) {
282 u32 linecard_index = nla_get_u32(attrs[DEVLINK_ATTR_LINECARD_INDEX]);
283 struct devlink_linecard *linecard;
285 mutex_lock(&devlink->linecards_lock);
286 linecard = devlink_linecard_get_by_index(devlink, linecard_index);
288 refcount_inc(&linecard->refcount);
289 mutex_unlock(&devlink->linecards_lock);
291 return ERR_PTR(-ENODEV);
294 return ERR_PTR(-EINVAL);
297 struct devlink_linecard *
298 devlink_linecard_get_from_info(struct devlink *devlink, struct genl_info *info)
300 return devlink_linecard_get_from_attrs(devlink, info->attrs);
303 void devlink_linecard_put(struct devlink_linecard *linecard)
305 if (refcount_dec_and_test(&linecard->refcount)) {
306 mutex_destroy(&linecard->state_lock);
312 struct list_head list;
315 u16 ingress_pools_count;
316 u16 egress_pools_count;
317 u16 ingress_tc_count;
321 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
323 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
326 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
327 unsigned int sb_index)
329 struct devlink_sb *devlink_sb;
331 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
332 if (devlink_sb->index == sb_index)
338 static bool devlink_sb_index_exists(struct devlink *devlink,
339 unsigned int sb_index)
341 return devlink_sb_get_by_index(devlink, sb_index);
344 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
345 struct nlattr **attrs)
347 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
348 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
349 struct devlink_sb *devlink_sb;
351 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
353 return ERR_PTR(-ENODEV);
356 return ERR_PTR(-EINVAL);
359 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
360 struct genl_info *info)
362 return devlink_sb_get_from_attrs(devlink, info->attrs);
365 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
366 struct nlattr **attrs,
371 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
374 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
375 if (val >= devlink_sb_pool_count(devlink_sb))
381 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
382 struct genl_info *info,
385 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
390 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
391 enum devlink_sb_pool_type *p_pool_type)
395 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
398 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
399 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
400 val != DEVLINK_SB_POOL_TYPE_EGRESS)
407 devlink_sb_pool_type_get_from_info(struct genl_info *info,
408 enum devlink_sb_pool_type *p_pool_type)
410 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
414 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
415 enum devlink_sb_threshold_type *p_th_type)
419 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
422 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
423 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
424 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
431 devlink_sb_th_type_get_from_info(struct genl_info *info,
432 enum devlink_sb_threshold_type *p_th_type)
434 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
438 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
439 struct nlattr **attrs,
440 enum devlink_sb_pool_type pool_type,
445 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
448 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
449 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
450 val >= devlink_sb->ingress_tc_count)
452 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
453 val >= devlink_sb->egress_tc_count)
459 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps,
460 u32 cap, bool is_enable)
462 caps->selector |= cap;
467 static int devlink_port_fn_roce_fill(const struct devlink_ops *ops,
468 struct devlink_port *devlink_port,
469 struct nla_bitfield32 *caps,
470 struct netlink_ext_ack *extack)
475 if (!ops->port_fn_roce_get)
478 err = ops->port_fn_roce_get(devlink_port, &is_enable, extack);
480 if (err == -EOPNOTSUPP)
485 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable);
489 static int devlink_port_fn_migratable_fill(const struct devlink_ops *ops,
490 struct devlink_port *devlink_port,
491 struct nla_bitfield32 *caps,
492 struct netlink_ext_ack *extack)
497 if (!ops->port_fn_migratable_get ||
498 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
501 err = ops->port_fn_migratable_get(devlink_port, &is_enable, extack);
503 if (err == -EOPNOTSUPP)
508 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable);
512 static int devlink_port_fn_caps_fill(const struct devlink_ops *ops,
513 struct devlink_port *devlink_port,
515 struct netlink_ext_ack *extack,
518 struct nla_bitfield32 caps = {};
521 err = devlink_port_fn_roce_fill(ops, devlink_port, &caps, extack);
525 err = devlink_port_fn_migratable_fill(ops, devlink_port, &caps, extack);
531 err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value,
541 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
542 struct genl_info *info,
543 enum devlink_sb_pool_type pool_type,
546 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
547 pool_type, p_tc_index);
550 struct devlink_region {
551 struct devlink *devlink;
552 struct devlink_port *port;
553 struct list_head list;
555 const struct devlink_region_ops *ops;
556 const struct devlink_port_region_ops *port_ops;
558 struct mutex snapshot_lock; /* protects snapshot_list,
559 * max_snapshots and cur_snapshots
562 struct list_head snapshot_list;
568 struct devlink_snapshot {
569 struct list_head list;
570 struct devlink_region *region;
575 static struct devlink_region *
576 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
578 struct devlink_region *region;
580 list_for_each_entry(region, &devlink->region_list, list)
581 if (!strcmp(region->ops->name, region_name))
587 static struct devlink_region *
588 devlink_port_region_get_by_name(struct devlink_port *port,
589 const char *region_name)
591 struct devlink_region *region;
593 list_for_each_entry(region, &port->region_list, list)
594 if (!strcmp(region->ops->name, region_name))
600 static struct devlink_snapshot *
601 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
603 struct devlink_snapshot *snapshot;
605 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
606 if (snapshot->id == id)
612 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
614 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
616 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
621 static int devlink_nl_put_nested_handle(struct sk_buff *msg, struct devlink *devlink)
623 struct nlattr *nested_attr;
625 nested_attr = nla_nest_start(msg, DEVLINK_ATTR_NESTED_DEVLINK);
628 if (devlink_nl_put_handle(msg, devlink))
629 goto nla_put_failure;
631 nla_nest_end(msg, nested_attr);
635 nla_nest_cancel(msg, nested_attr);
639 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
641 if (devlink_nl_put_handle(msg, devlink_port->devlink))
643 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
648 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
650 struct devlink *devlink = devlink_port->devlink;
652 return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */
653 + nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */
654 + nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */
657 struct devlink_reload_combination {
658 enum devlink_reload_action action;
659 enum devlink_reload_limit limit;
662 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
664 /* can't reinitialize driver with no down time */
665 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
666 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
671 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
672 enum devlink_reload_limit limit)
676 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
677 if (devlink_reload_invalid_combinations[i].action == action &&
678 devlink_reload_invalid_combinations[i].limit == limit)
684 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
686 return test_bit(action, &devlink->ops->reload_actions);
690 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
692 return test_bit(limit, &devlink->ops->reload_limits);
695 static int devlink_reload_stat_put(struct sk_buff *msg,
696 enum devlink_reload_limit limit, u32 value)
698 struct nlattr *reload_stats_entry;
700 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
701 if (!reload_stats_entry)
704 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
705 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
706 goto nla_put_failure;
707 nla_nest_end(msg, reload_stats_entry);
711 nla_nest_cancel(msg, reload_stats_entry);
715 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
717 struct nlattr *reload_stats_attr, *act_info, *act_stats;
722 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
724 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
726 if (!reload_stats_attr)
729 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
731 !devlink_reload_action_is_supported(devlink, i)) ||
732 i == DEVLINK_RELOAD_ACTION_UNSPEC)
734 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
736 goto nla_put_failure;
738 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
739 goto action_info_nest_cancel;
740 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
742 goto action_info_nest_cancel;
744 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
745 /* Remote stats are shown even if not locally supported.
746 * Stats of actions with unspecified limit are shown
747 * though drivers don't need to register unspecified
750 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
751 !devlink_reload_limit_is_supported(devlink, j)) ||
752 devlink_reload_combination_is_invalid(i, j))
755 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
757 value = devlink->stats.reload_stats[stat_idx];
759 value = devlink->stats.remote_reload_stats[stat_idx];
760 if (devlink_reload_stat_put(msg, j, value))
761 goto action_stats_nest_cancel;
763 nla_nest_end(msg, act_stats);
764 nla_nest_end(msg, act_info);
766 nla_nest_end(msg, reload_stats_attr);
769 action_stats_nest_cancel:
770 nla_nest_cancel(msg, act_stats);
771 action_info_nest_cancel:
772 nla_nest_cancel(msg, act_info);
774 nla_nest_cancel(msg, reload_stats_attr);
778 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
779 enum devlink_command cmd, u32 portid,
782 struct nlattr *dev_stats;
785 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
789 if (devlink_nl_put_handle(msg, devlink))
790 goto nla_put_failure;
791 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
792 goto nla_put_failure;
794 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
796 goto nla_put_failure;
798 if (devlink_reload_stats_put(msg, devlink, false))
799 goto dev_stats_nest_cancel;
800 if (devlink_reload_stats_put(msg, devlink, true))
801 goto dev_stats_nest_cancel;
803 nla_nest_end(msg, dev_stats);
804 genlmsg_end(msg, hdr);
807 dev_stats_nest_cancel:
808 nla_nest_cancel(msg, dev_stats);
810 genlmsg_cancel(msg, hdr);
814 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
819 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
820 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
826 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
832 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
833 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
836 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
837 struct devlink_port *devlink_port)
839 struct devlink_port_attrs *attrs = &devlink_port->attrs;
841 if (!devlink_port->attrs_set)
844 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
847 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
849 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
851 switch (devlink_port->attrs.flavour) {
852 case DEVLINK_PORT_FLAVOUR_PCI_PF:
853 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
854 attrs->pci_pf.controller) ||
855 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
857 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
860 case DEVLINK_PORT_FLAVOUR_PCI_VF:
861 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
862 attrs->pci_vf.controller) ||
863 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
864 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
866 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
869 case DEVLINK_PORT_FLAVOUR_PCI_SF:
870 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
871 attrs->pci_sf.controller) ||
872 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
874 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
878 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
879 case DEVLINK_PORT_FLAVOUR_CPU:
880 case DEVLINK_PORT_FLAVOUR_DSA:
881 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
882 attrs->phys.port_number))
886 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
887 attrs->phys.port_number))
889 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
890 attrs->phys.split_subport_number))
899 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
900 struct devlink_port *port,
902 struct netlink_ext_ack *extack,
905 u8 hw_addr[MAX_ADDR_LEN];
909 if (!ops->port_function_hw_addr_get)
912 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
915 if (err == -EOPNOTSUPP)
919 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
926 static int devlink_nl_rate_fill(struct sk_buff *msg,
927 struct devlink_rate *devlink_rate,
928 enum devlink_command cmd, u32 portid, u32 seq,
929 int flags, struct netlink_ext_ack *extack)
931 struct devlink *devlink = devlink_rate->devlink;
934 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
938 if (devlink_nl_put_handle(msg, devlink))
939 goto nla_put_failure;
941 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
942 goto nla_put_failure;
944 if (devlink_rate_is_leaf(devlink_rate)) {
945 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
946 devlink_rate->devlink_port->index))
947 goto nla_put_failure;
948 } else if (devlink_rate_is_node(devlink_rate)) {
949 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
951 goto nla_put_failure;
954 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
955 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
956 goto nla_put_failure;
958 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
959 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
960 goto nla_put_failure;
962 if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_PRIORITY,
963 devlink_rate->tx_priority))
964 goto nla_put_failure;
966 if (nla_put_u32(msg, DEVLINK_ATTR_RATE_TX_WEIGHT,
967 devlink_rate->tx_weight))
968 goto nla_put_failure;
970 if (devlink_rate->parent)
971 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
972 devlink_rate->parent->name))
973 goto nla_put_failure;
975 genlmsg_end(msg, hdr);
979 genlmsg_cancel(msg, hdr);
984 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
986 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
987 state == DEVLINK_PORT_FN_STATE_ACTIVE;
991 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
993 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
994 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
997 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
998 struct devlink_port *port,
1000 struct netlink_ext_ack *extack,
1003 enum devlink_port_fn_opstate opstate;
1004 enum devlink_port_fn_state state;
1007 if (!ops->port_fn_state_get)
1010 err = ops->port_fn_state_get(port, &state, &opstate, extack);
1012 if (err == -EOPNOTSUPP)
1016 if (!devlink_port_fn_state_valid(state)) {
1018 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
1021 if (!devlink_port_fn_opstate_valid(opstate)) {
1023 NL_SET_ERR_MSG_MOD(extack,
1024 "Invalid operational state read from driver");
1027 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
1028 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
1030 *msg_updated = true;
1035 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable,
1036 struct netlink_ext_ack *extack)
1038 const struct devlink_ops *ops = devlink_port->devlink->ops;
1040 return ops->port_fn_migratable_set(devlink_port, enable, extack);
1044 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable,
1045 struct netlink_ext_ack *extack)
1047 const struct devlink_ops *ops = devlink_port->devlink->ops;
1049 return ops->port_fn_roce_set(devlink_port, enable, extack);
1052 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port,
1053 const struct nlattr *attr,
1054 struct netlink_ext_ack *extack)
1056 struct nla_bitfield32 caps;
1060 caps = nla_get_bitfield32(attr);
1061 caps_value = caps.value & caps.selector;
1062 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE) {
1063 err = devlink_port_fn_roce_set(devlink_port,
1064 caps_value & DEVLINK_PORT_FN_CAP_ROCE,
1069 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1070 err = devlink_port_fn_mig_set(devlink_port, caps_value &
1071 DEVLINK_PORT_FN_CAP_MIGRATABLE,
1080 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
1081 struct netlink_ext_ack *extack)
1083 const struct devlink_ops *ops;
1084 struct nlattr *function_attr;
1085 bool msg_updated = false;
1088 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
1092 ops = port->devlink->ops;
1093 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
1097 err = devlink_port_fn_caps_fill(ops, port, msg, extack,
1101 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
1103 if (err || !msg_updated)
1104 nla_nest_cancel(msg, function_attr);
1106 nla_nest_end(msg, function_attr);
1110 static int devlink_nl_port_fill(struct sk_buff *msg,
1111 struct devlink_port *devlink_port,
1112 enum devlink_command cmd, u32 portid, u32 seq,
1113 int flags, struct netlink_ext_ack *extack)
1115 struct devlink *devlink = devlink_port->devlink;
1118 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1122 if (devlink_nl_put_handle(msg, devlink))
1123 goto nla_put_failure;
1124 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1125 goto nla_put_failure;
1127 spin_lock_bh(&devlink_port->type_lock);
1128 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
1129 goto nla_put_failure_type_locked;
1130 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1131 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1132 devlink_port->desired_type))
1133 goto nla_put_failure_type_locked;
1134 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1135 if (devlink_port->type_eth.netdev &&
1136 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1137 devlink_port->type_eth.ifindex) ||
1138 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1139 devlink_port->type_eth.ifname)))
1140 goto nla_put_failure_type_locked;
1142 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1143 struct ib_device *ibdev = devlink_port->type_ib.ibdev;
1146 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1148 goto nla_put_failure_type_locked;
1150 spin_unlock_bh(&devlink_port->type_lock);
1151 if (devlink_nl_port_attrs_put(msg, devlink_port))
1152 goto nla_put_failure;
1153 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1154 goto nla_put_failure;
1155 if (devlink_port->linecard &&
1156 nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX,
1157 devlink_port->linecard->index))
1158 goto nla_put_failure;
1160 genlmsg_end(msg, hdr);
1163 nla_put_failure_type_locked:
1164 spin_unlock_bh(&devlink_port->type_lock);
1166 genlmsg_cancel(msg, hdr);
1170 static void devlink_port_notify(struct devlink_port *devlink_port,
1171 enum devlink_command cmd)
1173 struct devlink *devlink = devlink_port->devlink;
1174 struct sk_buff *msg;
1177 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1179 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1182 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1186 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1192 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1193 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1196 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1197 enum devlink_command cmd)
1199 struct devlink *devlink = devlink_rate->devlink;
1200 struct sk_buff *msg;
1203 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1205 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
1208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1212 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1218 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
1219 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1222 static int devlink_nl_cmd_rate_get_dumpit(struct sk_buff *msg,
1223 struct netlink_callback *cb)
1225 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1226 struct devlink_rate *devlink_rate;
1227 struct devlink *devlink;
1228 int start = state->idx;
1229 unsigned long index;
1233 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1235 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1236 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1237 u32 id = NETLINK_CB(cb->skb).portid;
1243 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1247 devl_unlock(devlink);
1248 devlink_put(devlink);
1253 devl_unlock(devlink);
1254 devlink_put(devlink);
1257 if (err != -EMSGSIZE)
1264 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1265 struct genl_info *info)
1267 struct devlink_rate *devlink_rate = info->user_ptr[1];
1268 struct sk_buff *msg;
1271 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1275 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1276 info->snd_portid, info->snd_seq, 0,
1283 return genlmsg_reply(msg, info);
1287 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1288 struct devlink_rate *parent)
1291 if (parent == devlink_rate)
1293 parent = parent->parent;
1298 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1300 struct devlink *devlink = info->user_ptr[0];
1301 struct sk_buff *msg;
1304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1308 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1309 info->snd_portid, info->snd_seq, 0);
1315 return genlmsg_reply(msg, info);
1318 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
1319 struct netlink_callback *cb)
1321 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1322 struct devlink *devlink;
1323 int start = state->idx;
1324 unsigned long index;
1328 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1331 devlink_put(devlink);
1336 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1337 NETLINK_CB(cb->skb).portid,
1338 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1339 devl_unlock(devlink);
1340 devlink_put(devlink);
1351 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1352 struct genl_info *info)
1354 struct devlink_port *devlink_port = info->user_ptr[1];
1355 struct sk_buff *msg;
1358 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1362 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1363 info->snd_portid, info->snd_seq, 0,
1370 return genlmsg_reply(msg, info);
1373 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
1374 struct netlink_callback *cb)
1376 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
1377 struct devlink *devlink;
1378 struct devlink_port *devlink_port;
1379 unsigned long index, port_index;
1380 int start = state->idx;
1384 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
1386 xa_for_each(&devlink->ports, port_index, devlink_port) {
1391 err = devlink_nl_port_fill(msg, devlink_port,
1393 NETLINK_CB(cb->skb).portid,
1395 NLM_F_MULTI, cb->extack);
1397 devl_unlock(devlink);
1398 devlink_put(devlink);
1403 devl_unlock(devlink);
1404 devlink_put(devlink);
1411 static int devlink_port_type_set(struct devlink_port *devlink_port,
1412 enum devlink_port_type port_type)
1417 if (!devlink_port->devlink->ops->port_type_set)
1420 if (port_type == devlink_port->type)
1423 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1428 devlink_port->desired_type = port_type;
1429 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1433 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1434 const struct nlattr *attr,
1435 struct netlink_ext_ack *extack)
1437 const struct devlink_ops *ops = port->devlink->ops;
1441 hw_addr = nla_data(attr);
1442 hw_addr_len = nla_len(attr);
1443 if (hw_addr_len > MAX_ADDR_LEN) {
1444 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1447 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1448 if (hw_addr_len != ETH_ALEN) {
1449 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1452 if (!is_unicast_ether_addr(hw_addr)) {
1453 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1458 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1462 static int devlink_port_fn_state_set(struct devlink_port *port,
1463 const struct nlattr *attr,
1464 struct netlink_ext_ack *extack)
1466 enum devlink_port_fn_state state;
1467 const struct devlink_ops *ops;
1469 state = nla_get_u8(attr);
1470 ops = port->devlink->ops;
1471 return ops->port_fn_state_set(port, state, extack);
1474 static int devlink_port_function_validate(struct devlink_port *devlink_port,
1476 struct netlink_ext_ack *extack)
1478 const struct devlink_ops *ops = devlink_port->devlink->ops;
1479 struct nlattr *attr;
1481 if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] &&
1482 !ops->port_function_hw_addr_set) {
1483 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1484 "Port doesn't support function attributes");
1487 if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
1488 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
1489 "Function does not support state setting");
1492 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1494 struct nla_bitfield32 caps;
1496 caps = nla_get_bitfield32(attr);
1497 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE &&
1498 !ops->port_fn_roce_set) {
1499 NL_SET_ERR_MSG_ATTR(extack, attr,
1500 "Port doesn't support RoCE function attribute");
1503 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) {
1504 if (!ops->port_fn_migratable_set) {
1505 NL_SET_ERR_MSG_ATTR(extack, attr,
1506 "Port doesn't support migratable function attribute");
1509 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) {
1510 NL_SET_ERR_MSG_ATTR(extack, attr,
1511 "migratable function attribute supported for VFs only");
1519 static int devlink_port_function_set(struct devlink_port *port,
1520 const struct nlattr *attr,
1521 struct netlink_ext_ack *extack)
1523 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1526 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1527 devlink_function_nl_policy, extack);
1529 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1533 err = devlink_port_function_validate(port, tb, extack);
1537 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1539 err = devlink_port_function_hw_addr_set(port, attr, extack);
1544 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
1546 err = devlink_port_fn_caps_set(port, attr, extack);
1551 /* Keep this as the last function attribute set, so that when
1552 * multiple port function attributes are set along with state,
1553 * Those can be applied first before activating the state.
1555 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1557 err = devlink_port_fn_state_set(port, attr, extack);
1560 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1564 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1565 struct genl_info *info)
1567 struct devlink_port *devlink_port = info->user_ptr[1];
1570 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1571 enum devlink_port_type port_type;
1573 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1574 err = devlink_port_type_set(devlink_port, port_type);
1579 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1580 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1581 struct netlink_ext_ack *extack = info->extack;
1583 err = devlink_port_function_set(devlink_port, attr, extack);
1591 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1592 struct genl_info *info)
1594 struct devlink_port *devlink_port = info->user_ptr[1];
1595 struct devlink *devlink = info->user_ptr[0];
1598 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
1600 if (!devlink->ops->port_split)
1603 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1605 if (!devlink_port->attrs.splittable) {
1606 /* Split ports cannot be split. */
1607 if (devlink_port->attrs.split)
1608 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1610 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1614 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1615 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1619 return devlink->ops->port_split(devlink, devlink_port, count,
1623 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1624 struct genl_info *info)
1626 struct devlink_port *devlink_port = info->user_ptr[1];
1627 struct devlink *devlink = info->user_ptr[0];
1629 if (!devlink->ops->port_unsplit)
1631 return devlink->ops->port_unsplit(devlink, devlink_port, info->extack);
1634 static int devlink_port_new_notify(struct devlink *devlink,
1635 unsigned int port_index,
1636 struct genl_info *info)
1638 struct devlink_port *devlink_port;
1639 struct sk_buff *msg;
1642 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1646 lockdep_assert_held(&devlink->lock);
1647 devlink_port = devlink_port_get_by_index(devlink, port_index);
1648 if (!devlink_port) {
1653 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1654 info->snd_portid, info->snd_seq, 0, NULL);
1658 return genlmsg_reply(msg, info);
1665 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1666 struct genl_info *info)
1668 struct netlink_ext_ack *extack = info->extack;
1669 struct devlink_port_new_attrs new_attrs = {};
1670 struct devlink *devlink = info->user_ptr[0];
1671 unsigned int new_port_index;
1674 if (!devlink->ops->port_new || !devlink->ops->port_del)
1677 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1678 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1679 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1682 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1684 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1686 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1687 /* Port index of the new port being created by driver. */
1688 new_attrs.port_index =
1689 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1690 new_attrs.port_index_valid = true;
1692 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1693 new_attrs.controller =
1694 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1695 new_attrs.controller_valid = true;
1697 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1698 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1699 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1700 new_attrs.sfnum_valid = true;
1703 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1708 err = devlink_port_new_notify(devlink, new_port_index, info);
1709 if (err && err != -ENODEV) {
1710 /* Fail to send the response; destroy newly created port. */
1711 devlink->ops->port_del(devlink, new_port_index, extack);
1716 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1717 struct genl_info *info)
1719 struct netlink_ext_ack *extack = info->extack;
1720 struct devlink *devlink = info->user_ptr[0];
1721 unsigned int port_index;
1723 if (!devlink->ops->port_del)
1726 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_INDEX)) {
1727 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1730 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1732 return devlink->ops->port_del(devlink, port_index, extack);
1736 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1737 struct genl_info *info,
1738 struct nlattr *nla_parent)
1740 struct devlink *devlink = devlink_rate->devlink;
1741 const char *parent_name = nla_data(nla_parent);
1742 const struct devlink_ops *ops = devlink->ops;
1743 size_t len = strlen(parent_name);
1744 struct devlink_rate *parent;
1745 int err = -EOPNOTSUPP;
1747 parent = devlink_rate->parent;
1749 if (parent && !len) {
1750 if (devlink_rate_is_leaf(devlink_rate))
1751 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1752 devlink_rate->priv, NULL,
1754 else if (devlink_rate_is_node(devlink_rate))
1755 err = ops->rate_node_parent_set(devlink_rate, NULL,
1756 devlink_rate->priv, NULL,
1761 refcount_dec(&parent->refcnt);
1762 devlink_rate->parent = NULL;
1764 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1768 if (parent == devlink_rate) {
1769 NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1773 if (devlink_rate_is_node(devlink_rate) &&
1774 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1775 NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1779 if (devlink_rate_is_leaf(devlink_rate))
1780 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1781 devlink_rate->priv, parent->priv,
1783 else if (devlink_rate_is_node(devlink_rate))
1784 err = ops->rate_node_parent_set(devlink_rate, parent,
1785 devlink_rate->priv, parent->priv,
1790 if (devlink_rate->parent)
1791 /* we're reassigning to other parent in this case */
1792 refcount_dec(&devlink_rate->parent->refcnt);
1794 refcount_inc(&parent->refcnt);
1795 devlink_rate->parent = parent;
1801 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1802 const struct devlink_ops *ops,
1803 struct genl_info *info)
1805 struct nlattr *nla_parent, **attrs = info->attrs;
1806 int err = -EOPNOTSUPP;
1811 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1812 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1813 if (devlink_rate_is_leaf(devlink_rate))
1814 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1815 rate, info->extack);
1816 else if (devlink_rate_is_node(devlink_rate))
1817 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1818 rate, info->extack);
1821 devlink_rate->tx_share = rate;
1824 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1825 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1826 if (devlink_rate_is_leaf(devlink_rate))
1827 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1828 rate, info->extack);
1829 else if (devlink_rate_is_node(devlink_rate))
1830 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1831 rate, info->extack);
1834 devlink_rate->tx_max = rate;
1837 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]) {
1838 priority = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_PRIORITY]);
1839 if (devlink_rate_is_leaf(devlink_rate))
1840 err = ops->rate_leaf_tx_priority_set(devlink_rate, devlink_rate->priv,
1841 priority, info->extack);
1842 else if (devlink_rate_is_node(devlink_rate))
1843 err = ops->rate_node_tx_priority_set(devlink_rate, devlink_rate->priv,
1844 priority, info->extack);
1848 devlink_rate->tx_priority = priority;
1851 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]) {
1852 weight = nla_get_u32(attrs[DEVLINK_ATTR_RATE_TX_WEIGHT]);
1853 if (devlink_rate_is_leaf(devlink_rate))
1854 err = ops->rate_leaf_tx_weight_set(devlink_rate, devlink_rate->priv,
1855 weight, info->extack);
1856 else if (devlink_rate_is_node(devlink_rate))
1857 err = ops->rate_node_tx_weight_set(devlink_rate, devlink_rate->priv,
1858 weight, info->extack);
1862 devlink_rate->tx_weight = weight;
1865 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1867 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1876 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1877 struct genl_info *info,
1878 enum devlink_rate_type type)
1880 struct nlattr **attrs = info->attrs;
1882 if (type == DEVLINK_RATE_TYPE_LEAF) {
1883 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1884 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1887 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1888 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1891 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1892 !ops->rate_leaf_parent_set) {
1893 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1896 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_leaf_tx_priority_set) {
1897 NL_SET_ERR_MSG_ATTR(info->extack,
1898 attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1899 "TX priority set isn't supported for the leafs");
1902 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_leaf_tx_weight_set) {
1903 NL_SET_ERR_MSG_ATTR(info->extack,
1904 attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1905 "TX weight set isn't supported for the leafs");
1908 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1909 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1910 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1913 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1914 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1917 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1918 !ops->rate_node_parent_set) {
1919 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1922 if (attrs[DEVLINK_ATTR_RATE_TX_PRIORITY] && !ops->rate_node_tx_priority_set) {
1923 NL_SET_ERR_MSG_ATTR(info->extack,
1924 attrs[DEVLINK_ATTR_RATE_TX_PRIORITY],
1925 "TX priority set isn't supported for the nodes");
1928 if (attrs[DEVLINK_ATTR_RATE_TX_WEIGHT] && !ops->rate_node_tx_weight_set) {
1929 NL_SET_ERR_MSG_ATTR(info->extack,
1930 attrs[DEVLINK_ATTR_RATE_TX_WEIGHT],
1931 "TX weight set isn't supported for the nodes");
1935 WARN(1, "Unknown type of rate object");
1942 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1943 struct genl_info *info)
1945 struct devlink_rate *devlink_rate = info->user_ptr[1];
1946 struct devlink *devlink = devlink_rate->devlink;
1947 const struct devlink_ops *ops = devlink->ops;
1950 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1953 err = devlink_nl_rate_set(devlink_rate, ops, info);
1956 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1960 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1961 struct genl_info *info)
1963 struct devlink *devlink = info->user_ptr[0];
1964 struct devlink_rate *rate_node;
1965 const struct devlink_ops *ops;
1969 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1970 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
1974 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1977 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1978 if (!IS_ERR(rate_node))
1980 else if (rate_node == ERR_PTR(-EINVAL))
1983 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
1987 rate_node->devlink = devlink;
1988 rate_node->type = DEVLINK_RATE_TYPE_NODE;
1989 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
1990 if (!rate_node->name) {
1995 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
1999 err = devlink_nl_rate_set(rate_node, ops, info);
2003 refcount_set(&rate_node->refcnt, 1);
2004 list_add(&rate_node->list, &devlink->rate_list);
2005 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
2009 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2011 kfree(rate_node->name);
2017 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
2018 struct genl_info *info)
2020 struct devlink_rate *rate_node = info->user_ptr[1];
2021 struct devlink *devlink = rate_node->devlink;
2022 const struct devlink_ops *ops = devlink->ops;
2025 if (refcount_read(&rate_node->refcnt) > 1) {
2026 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
2030 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
2031 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
2032 if (rate_node->parent)
2033 refcount_dec(&rate_node->parent->refcnt);
2034 list_del(&rate_node->list);
2035 kfree(rate_node->name);
2040 struct devlink_linecard_type {
2045 static int devlink_nl_linecard_fill(struct sk_buff *msg,
2046 struct devlink *devlink,
2047 struct devlink_linecard *linecard,
2048 enum devlink_command cmd, u32 portid,
2050 struct netlink_ext_ack *extack)
2052 struct devlink_linecard_type *linecard_type;
2053 struct nlattr *attr;
2057 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2061 if (devlink_nl_put_handle(msg, devlink))
2062 goto nla_put_failure;
2063 if (nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, linecard->index))
2064 goto nla_put_failure;
2065 if (nla_put_u8(msg, DEVLINK_ATTR_LINECARD_STATE, linecard->state))
2066 goto nla_put_failure;
2067 if (linecard->type &&
2068 nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE, linecard->type))
2069 goto nla_put_failure;
2071 if (linecard->types_count) {
2072 attr = nla_nest_start(msg,
2073 DEVLINK_ATTR_LINECARD_SUPPORTED_TYPES);
2075 goto nla_put_failure;
2076 for (i = 0; i < linecard->types_count; i++) {
2077 linecard_type = &linecard->types[i];
2078 if (nla_put_string(msg, DEVLINK_ATTR_LINECARD_TYPE,
2079 linecard_type->type)) {
2080 nla_nest_cancel(msg, attr);
2081 goto nla_put_failure;
2084 nla_nest_end(msg, attr);
2087 if (linecard->nested_devlink &&
2088 devlink_nl_put_nested_handle(msg, linecard->nested_devlink))
2089 goto nla_put_failure;
2091 genlmsg_end(msg, hdr);
2095 genlmsg_cancel(msg, hdr);
2099 static void devlink_linecard_notify(struct devlink_linecard *linecard,
2100 enum devlink_command cmd)
2102 struct devlink *devlink = linecard->devlink;
2103 struct sk_buff *msg;
2106 WARN_ON(cmd != DEVLINK_CMD_LINECARD_NEW &&
2107 cmd != DEVLINK_CMD_LINECARD_DEL);
2109 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
2112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2116 err = devlink_nl_linecard_fill(msg, devlink, linecard, cmd, 0, 0, 0,
2123 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2124 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2127 static int devlink_nl_cmd_linecard_get_doit(struct sk_buff *skb,
2128 struct genl_info *info)
2130 struct devlink_linecard *linecard = info->user_ptr[1];
2131 struct devlink *devlink = linecard->devlink;
2132 struct sk_buff *msg;
2135 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2139 mutex_lock(&linecard->state_lock);
2140 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2141 DEVLINK_CMD_LINECARD_NEW,
2142 info->snd_portid, info->snd_seq, 0,
2144 mutex_unlock(&linecard->state_lock);
2150 return genlmsg_reply(msg, info);
2153 static int devlink_nl_cmd_linecard_get_dumpit(struct sk_buff *msg,
2154 struct netlink_callback *cb)
2156 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2157 struct devlink_linecard *linecard;
2158 struct devlink *devlink;
2159 int start = state->idx;
2160 unsigned long index;
2164 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2165 mutex_lock(&devlink->linecards_lock);
2166 list_for_each_entry(linecard, &devlink->linecard_list, list) {
2171 mutex_lock(&linecard->state_lock);
2172 err = devlink_nl_linecard_fill(msg, devlink, linecard,
2173 DEVLINK_CMD_LINECARD_NEW,
2174 NETLINK_CB(cb->skb).portid,
2178 mutex_unlock(&linecard->state_lock);
2180 mutex_unlock(&devlink->linecards_lock);
2181 devlink_put(devlink);
2186 mutex_unlock(&devlink->linecards_lock);
2187 devlink_put(devlink);
2194 static struct devlink_linecard_type *
2195 devlink_linecard_type_lookup(struct devlink_linecard *linecard,
2198 struct devlink_linecard_type *linecard_type;
2201 for (i = 0; i < linecard->types_count; i++) {
2202 linecard_type = &linecard->types[i];
2203 if (!strcmp(type, linecard_type->type))
2204 return linecard_type;
2209 static int devlink_linecard_type_set(struct devlink_linecard *linecard,
2211 struct netlink_ext_ack *extack)
2213 const struct devlink_linecard_ops *ops = linecard->ops;
2214 struct devlink_linecard_type *linecard_type;
2217 mutex_lock(&linecard->state_lock);
2218 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2219 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2223 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2224 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2229 linecard_type = devlink_linecard_type_lookup(linecard, type);
2230 if (!linecard_type) {
2231 NL_SET_ERR_MSG_MOD(extack, "Unsupported line card type provided");
2236 if (linecard->state != DEVLINK_LINECARD_STATE_UNPROVISIONED &&
2237 linecard->state != DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2238 NL_SET_ERR_MSG_MOD(extack, "Line card already provisioned");
2240 /* Check if the line card is provisioned in the same
2241 * way the user asks. In case it is, make the operation
2242 * to return success.
2244 if (ops->same_provision &&
2245 ops->same_provision(linecard, linecard->priv,
2246 linecard_type->type,
2247 linecard_type->priv))
2252 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING;
2253 linecard->type = linecard_type->type;
2254 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2255 mutex_unlock(&linecard->state_lock);
2256 err = ops->provision(linecard, linecard->priv, linecard_type->type,
2257 linecard_type->priv, extack);
2259 /* Provisioning failed. Assume the linecard is unprovisioned
2260 * for future operations.
2262 mutex_lock(&linecard->state_lock);
2263 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2264 linecard->type = NULL;
2265 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2266 mutex_unlock(&linecard->state_lock);
2271 mutex_unlock(&linecard->state_lock);
2275 static int devlink_linecard_type_unset(struct devlink_linecard *linecard,
2276 struct netlink_ext_ack *extack)
2280 mutex_lock(&linecard->state_lock);
2281 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING) {
2282 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being provisioned");
2286 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONING) {
2287 NL_SET_ERR_MSG_MOD(extack, "Line card is currently being unprovisioned");
2291 if (linecard->state == DEVLINK_LINECARD_STATE_PROVISIONING_FAILED) {
2292 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2293 linecard->type = NULL;
2294 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2299 if (linecard->state == DEVLINK_LINECARD_STATE_UNPROVISIONED) {
2300 NL_SET_ERR_MSG_MOD(extack, "Line card is not provisioned");
2304 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONING;
2305 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2306 mutex_unlock(&linecard->state_lock);
2307 err = linecard->ops->unprovision(linecard, linecard->priv,
2310 /* Unprovisioning failed. Assume the linecard is unprovisioned
2311 * for future operations.
2313 mutex_lock(&linecard->state_lock);
2314 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
2315 linecard->type = NULL;
2316 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
2317 mutex_unlock(&linecard->state_lock);
2322 mutex_unlock(&linecard->state_lock);
2326 static int devlink_nl_cmd_linecard_set_doit(struct sk_buff *skb,
2327 struct genl_info *info)
2329 struct devlink_linecard *linecard = info->user_ptr[1];
2330 struct netlink_ext_ack *extack = info->extack;
2333 if (info->attrs[DEVLINK_ATTR_LINECARD_TYPE]) {
2336 type = nla_data(info->attrs[DEVLINK_ATTR_LINECARD_TYPE]);
2337 if (strcmp(type, "")) {
2338 err = devlink_linecard_type_set(linecard, type, extack);
2342 err = devlink_linecard_type_unset(linecard, extack);
2351 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
2352 struct devlink_sb *devlink_sb,
2353 enum devlink_command cmd, u32 portid,
2358 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2362 if (devlink_nl_put_handle(msg, devlink))
2363 goto nla_put_failure;
2364 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2365 goto nla_put_failure;
2366 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
2367 goto nla_put_failure;
2368 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
2369 devlink_sb->ingress_pools_count))
2370 goto nla_put_failure;
2371 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
2372 devlink_sb->egress_pools_count))
2373 goto nla_put_failure;
2374 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
2375 devlink_sb->ingress_tc_count))
2376 goto nla_put_failure;
2377 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
2378 devlink_sb->egress_tc_count))
2379 goto nla_put_failure;
2381 genlmsg_end(msg, hdr);
2385 genlmsg_cancel(msg, hdr);
2389 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
2390 struct genl_info *info)
2392 struct devlink *devlink = info->user_ptr[0];
2393 struct devlink_sb *devlink_sb;
2394 struct sk_buff *msg;
2397 devlink_sb = devlink_sb_get_from_info(devlink, info);
2398 if (IS_ERR(devlink_sb))
2399 return PTR_ERR(devlink_sb);
2401 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2405 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2407 info->snd_portid, info->snd_seq, 0);
2413 return genlmsg_reply(msg, info);
2416 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
2417 struct netlink_callback *cb)
2419 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2420 struct devlink *devlink;
2421 struct devlink_sb *devlink_sb;
2422 int start = state->idx;
2423 unsigned long index;
2427 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2429 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2434 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
2436 NETLINK_CB(cb->skb).portid,
2440 devl_unlock(devlink);
2441 devlink_put(devlink);
2446 devl_unlock(devlink);
2447 devlink_put(devlink);
2454 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
2455 struct devlink_sb *devlink_sb,
2456 u16 pool_index, enum devlink_command cmd,
2457 u32 portid, u32 seq, int flags)
2459 struct devlink_sb_pool_info pool_info;
2463 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
2464 pool_index, &pool_info);
2468 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2472 if (devlink_nl_put_handle(msg, devlink))
2473 goto nla_put_failure;
2474 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2475 goto nla_put_failure;
2476 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2477 goto nla_put_failure;
2478 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
2479 goto nla_put_failure;
2480 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
2481 goto nla_put_failure;
2482 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
2483 pool_info.threshold_type))
2484 goto nla_put_failure;
2485 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
2486 pool_info.cell_size))
2487 goto nla_put_failure;
2489 genlmsg_end(msg, hdr);
2493 genlmsg_cancel(msg, hdr);
2497 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2498 struct genl_info *info)
2500 struct devlink *devlink = info->user_ptr[0];
2501 struct devlink_sb *devlink_sb;
2502 struct sk_buff *msg;
2506 devlink_sb = devlink_sb_get_from_info(devlink, info);
2507 if (IS_ERR(devlink_sb))
2508 return PTR_ERR(devlink_sb);
2510 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2515 if (!devlink->ops->sb_pool_get)
2518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2522 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2523 DEVLINK_CMD_SB_POOL_NEW,
2524 info->snd_portid, info->snd_seq, 0);
2530 return genlmsg_reply(msg, info);
2533 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2534 struct devlink *devlink,
2535 struct devlink_sb *devlink_sb,
2536 u32 portid, u32 seq)
2538 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2542 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2543 if (*p_idx < start) {
2547 err = devlink_nl_sb_pool_fill(msg, devlink,
2550 DEVLINK_CMD_SB_POOL_NEW,
2551 portid, seq, NLM_F_MULTI);
2559 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
2560 struct netlink_callback *cb)
2562 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2563 struct devlink *devlink;
2564 struct devlink_sb *devlink_sb;
2565 int start = state->idx;
2566 unsigned long index;
2570 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2571 if (!devlink->ops->sb_pool_get)
2575 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2576 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
2578 NETLINK_CB(cb->skb).portid,
2579 cb->nlh->nlmsg_seq);
2580 if (err == -EOPNOTSUPP) {
2583 devl_unlock(devlink);
2584 devlink_put(devlink);
2588 devl_unlock(devlink);
2590 devlink_put(devlink);
2593 if (err != -EMSGSIZE)
2600 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2601 u16 pool_index, u32 size,
2602 enum devlink_sb_threshold_type threshold_type,
2603 struct netlink_ext_ack *extack)
2606 const struct devlink_ops *ops = devlink->ops;
2608 if (ops->sb_pool_set)
2609 return ops->sb_pool_set(devlink, sb_index, pool_index,
2610 size, threshold_type, extack);
2614 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2615 struct genl_info *info)
2617 struct devlink *devlink = info->user_ptr[0];
2618 enum devlink_sb_threshold_type threshold_type;
2619 struct devlink_sb *devlink_sb;
2624 devlink_sb = devlink_sb_get_from_info(devlink, info);
2625 if (IS_ERR(devlink_sb))
2626 return PTR_ERR(devlink_sb);
2628 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2633 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2637 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_POOL_SIZE))
2640 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2641 return devlink_sb_pool_set(devlink, devlink_sb->index,
2642 pool_index, size, threshold_type,
2646 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2647 struct devlink *devlink,
2648 struct devlink_port *devlink_port,
2649 struct devlink_sb *devlink_sb,
2651 enum devlink_command cmd,
2652 u32 portid, u32 seq, int flags)
2654 const struct devlink_ops *ops = devlink->ops;
2659 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2660 pool_index, &threshold);
2664 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2668 if (devlink_nl_put_handle(msg, devlink))
2669 goto nla_put_failure;
2670 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2671 goto nla_put_failure;
2672 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2673 goto nla_put_failure;
2674 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2675 goto nla_put_failure;
2676 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2677 goto nla_put_failure;
2679 if (ops->sb_occ_port_pool_get) {
2683 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2684 pool_index, &cur, &max);
2685 if (err && err != -EOPNOTSUPP)
2686 goto sb_occ_get_failure;
2688 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2689 goto nla_put_failure;
2690 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2691 goto nla_put_failure;
2695 genlmsg_end(msg, hdr);
2701 genlmsg_cancel(msg, hdr);
2705 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2706 struct genl_info *info)
2708 struct devlink_port *devlink_port = info->user_ptr[1];
2709 struct devlink *devlink = devlink_port->devlink;
2710 struct devlink_sb *devlink_sb;
2711 struct sk_buff *msg;
2715 devlink_sb = devlink_sb_get_from_info(devlink, info);
2716 if (IS_ERR(devlink_sb))
2717 return PTR_ERR(devlink_sb);
2719 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2724 if (!devlink->ops->sb_port_pool_get)
2727 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2731 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2732 devlink_sb, pool_index,
2733 DEVLINK_CMD_SB_PORT_POOL_NEW,
2734 info->snd_portid, info->snd_seq, 0);
2740 return genlmsg_reply(msg, info);
2743 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2744 struct devlink *devlink,
2745 struct devlink_sb *devlink_sb,
2746 u32 portid, u32 seq)
2748 struct devlink_port *devlink_port;
2749 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2750 unsigned long port_index;
2754 xa_for_each(&devlink->ports, port_index, devlink_port) {
2755 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2756 if (*p_idx < start) {
2760 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2764 DEVLINK_CMD_SB_PORT_POOL_NEW,
2775 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
2776 struct netlink_callback *cb)
2778 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
2779 struct devlink *devlink;
2780 struct devlink_sb *devlink_sb;
2781 int start = state->idx;
2782 unsigned long index;
2786 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
2787 if (!devlink->ops->sb_port_pool_get)
2791 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2792 err = __sb_port_pool_get_dumpit(msg, start, &idx,
2793 devlink, devlink_sb,
2794 NETLINK_CB(cb->skb).portid,
2795 cb->nlh->nlmsg_seq);
2796 if (err == -EOPNOTSUPP) {
2799 devl_unlock(devlink);
2800 devlink_put(devlink);
2804 devl_unlock(devlink);
2806 devlink_put(devlink);
2809 if (err != -EMSGSIZE)
2816 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2817 unsigned int sb_index, u16 pool_index,
2819 struct netlink_ext_ack *extack)
2822 const struct devlink_ops *ops = devlink_port->devlink->ops;
2824 if (ops->sb_port_pool_set)
2825 return ops->sb_port_pool_set(devlink_port, sb_index,
2826 pool_index, threshold, extack);
2830 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2831 struct genl_info *info)
2833 struct devlink_port *devlink_port = info->user_ptr[1];
2834 struct devlink *devlink = info->user_ptr[0];
2835 struct devlink_sb *devlink_sb;
2840 devlink_sb = devlink_sb_get_from_info(devlink, info);
2841 if (IS_ERR(devlink_sb))
2842 return PTR_ERR(devlink_sb);
2844 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2849 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
2852 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2853 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2854 pool_index, threshold, info->extack);
2858 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2859 struct devlink_port *devlink_port,
2860 struct devlink_sb *devlink_sb, u16 tc_index,
2861 enum devlink_sb_pool_type pool_type,
2862 enum devlink_command cmd,
2863 u32 portid, u32 seq, int flags)
2865 const struct devlink_ops *ops = devlink->ops;
2871 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2872 tc_index, pool_type,
2873 &pool_index, &threshold);
2877 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2881 if (devlink_nl_put_handle(msg, devlink))
2882 goto nla_put_failure;
2883 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2884 goto nla_put_failure;
2885 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2886 goto nla_put_failure;
2887 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2888 goto nla_put_failure;
2889 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2890 goto nla_put_failure;
2891 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2892 goto nla_put_failure;
2893 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2894 goto nla_put_failure;
2896 if (ops->sb_occ_tc_port_bind_get) {
2900 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2902 tc_index, pool_type,
2904 if (err && err != -EOPNOTSUPP)
2907 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2908 goto nla_put_failure;
2909 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2910 goto nla_put_failure;
2914 genlmsg_end(msg, hdr);
2918 genlmsg_cancel(msg, hdr);
2922 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2923 struct genl_info *info)
2925 struct devlink_port *devlink_port = info->user_ptr[1];
2926 struct devlink *devlink = devlink_port->devlink;
2927 struct devlink_sb *devlink_sb;
2928 struct sk_buff *msg;
2929 enum devlink_sb_pool_type pool_type;
2933 devlink_sb = devlink_sb_get_from_info(devlink, info);
2934 if (IS_ERR(devlink_sb))
2935 return PTR_ERR(devlink_sb);
2937 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2941 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2942 pool_type, &tc_index);
2946 if (!devlink->ops->sb_tc_pool_bind_get)
2949 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2953 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2954 devlink_sb, tc_index, pool_type,
2955 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2963 return genlmsg_reply(msg, info);
2966 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2967 int start, int *p_idx,
2968 struct devlink *devlink,
2969 struct devlink_sb *devlink_sb,
2970 u32 portid, u32 seq)
2972 struct devlink_port *devlink_port;
2973 unsigned long port_index;
2977 xa_for_each(&devlink->ports, port_index, devlink_port) {
2979 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
2980 if (*p_idx < start) {
2984 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2988 DEVLINK_SB_POOL_TYPE_INGRESS,
2989 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2997 tc_index < devlink_sb->egress_tc_count; tc_index++) {
2998 if (*p_idx < start) {
3002 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
3006 DEVLINK_SB_POOL_TYPE_EGRESS,
3007 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
3019 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
3020 struct netlink_callback *cb)
3022 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
3023 struct devlink *devlink;
3024 struct devlink_sb *devlink_sb;
3025 int start = state->idx;
3026 unsigned long index;
3030 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
3031 if (!devlink->ops->sb_tc_pool_bind_get)
3035 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
3036 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
3039 NETLINK_CB(cb->skb).portid,
3040 cb->nlh->nlmsg_seq);
3041 if (err == -EOPNOTSUPP) {
3044 devl_unlock(devlink);
3045 devlink_put(devlink);
3049 devl_unlock(devlink);
3051 devlink_put(devlink);
3054 if (err != -EMSGSIZE)
3061 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
3062 unsigned int sb_index, u16 tc_index,
3063 enum devlink_sb_pool_type pool_type,
3064 u16 pool_index, u32 threshold,
3065 struct netlink_ext_ack *extack)
3068 const struct devlink_ops *ops = devlink_port->devlink->ops;
3070 if (ops->sb_tc_pool_bind_set)
3071 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
3072 tc_index, pool_type,
3073 pool_index, threshold, extack);
3077 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
3078 struct genl_info *info)
3080 struct devlink_port *devlink_port = info->user_ptr[1];
3081 struct devlink *devlink = info->user_ptr[0];
3082 enum devlink_sb_pool_type pool_type;
3083 struct devlink_sb *devlink_sb;
3089 devlink_sb = devlink_sb_get_from_info(devlink, info);
3090 if (IS_ERR(devlink_sb))
3091 return PTR_ERR(devlink_sb);
3093 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
3097 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
3098 pool_type, &tc_index);
3102 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
3107 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SB_THRESHOLD))
3110 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
3111 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
3112 tc_index, pool_type,
3113 pool_index, threshold, info->extack);
3116 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
3117 struct genl_info *info)
3119 struct devlink *devlink = info->user_ptr[0];
3120 const struct devlink_ops *ops = devlink->ops;
3121 struct devlink_sb *devlink_sb;
3123 devlink_sb = devlink_sb_get_from_info(devlink, info);
3124 if (IS_ERR(devlink_sb))
3125 return PTR_ERR(devlink_sb);
3127 if (ops->sb_occ_snapshot)
3128 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
3132 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
3133 struct genl_info *info)
3135 struct devlink *devlink = info->user_ptr[0];
3136 const struct devlink_ops *ops = devlink->ops;
3137 struct devlink_sb *devlink_sb;
3139 devlink_sb = devlink_sb_get_from_info(devlink, info);
3140 if (IS_ERR(devlink_sb))
3141 return PTR_ERR(devlink_sb);
3143 if (ops->sb_occ_max_clear)
3144 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
3148 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
3149 enum devlink_command cmd, u32 portid,
3152 const struct devlink_ops *ops = devlink->ops;
3153 enum devlink_eswitch_encap_mode encap_mode;
3159 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3163 err = devlink_nl_put_handle(msg, devlink);
3165 goto nla_put_failure;
3167 if (ops->eswitch_mode_get) {
3168 err = ops->eswitch_mode_get(devlink, &mode);
3170 goto nla_put_failure;
3171 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
3173 goto nla_put_failure;
3176 if (ops->eswitch_inline_mode_get) {
3177 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
3179 goto nla_put_failure;
3180 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
3183 goto nla_put_failure;
3186 if (ops->eswitch_encap_mode_get) {
3187 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
3189 goto nla_put_failure;
3190 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
3192 goto nla_put_failure;
3195 genlmsg_end(msg, hdr);
3199 genlmsg_cancel(msg, hdr);
3203 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
3204 struct genl_info *info)
3206 struct devlink *devlink = info->user_ptr[0];
3207 struct sk_buff *msg;
3210 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3214 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
3215 info->snd_portid, info->snd_seq, 0);
3222 return genlmsg_reply(msg, info);
3225 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
3226 struct netlink_ext_ack *extack)
3228 struct devlink_rate *devlink_rate;
3230 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
3231 if (devlink_rate_is_node(devlink_rate)) {
3232 NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
3238 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
3239 struct genl_info *info)
3241 struct devlink *devlink = info->user_ptr[0];
3242 const struct devlink_ops *ops = devlink->ops;
3243 enum devlink_eswitch_encap_mode encap_mode;
3248 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
3249 if (!ops->eswitch_mode_set)
3251 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
3252 err = devlink_rate_nodes_check(devlink, mode, info->extack);
3255 err = ops->eswitch_mode_set(devlink, mode, info->extack);
3260 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
3261 if (!ops->eswitch_inline_mode_set)
3263 inline_mode = nla_get_u8(
3264 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
3265 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
3271 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
3272 if (!ops->eswitch_encap_mode_set)
3274 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
3275 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
3284 int devlink_dpipe_match_put(struct sk_buff *skb,
3285 struct devlink_dpipe_match *match)
3287 struct devlink_dpipe_header *header = match->header;
3288 struct devlink_dpipe_field *field = &header->fields[match->field_id];
3289 struct nlattr *match_attr;
3291 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
3295 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
3296 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
3297 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3298 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3299 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3300 goto nla_put_failure;
3302 nla_nest_end(skb, match_attr);
3306 nla_nest_cancel(skb, match_attr);
3309 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
3311 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
3312 struct sk_buff *skb)
3314 struct nlattr *matches_attr;
3316 matches_attr = nla_nest_start_noflag(skb,
3317 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
3321 if (table->table_ops->matches_dump(table->priv, skb))
3322 goto nla_put_failure;
3324 nla_nest_end(skb, matches_attr);
3328 nla_nest_cancel(skb, matches_attr);
3332 int devlink_dpipe_action_put(struct sk_buff *skb,
3333 struct devlink_dpipe_action *action)
3335 struct devlink_dpipe_header *header = action->header;
3336 struct devlink_dpipe_field *field = &header->fields[action->field_id];
3337 struct nlattr *action_attr;
3339 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
3343 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
3344 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
3345 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3346 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3347 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3348 goto nla_put_failure;
3350 nla_nest_end(skb, action_attr);
3354 nla_nest_cancel(skb, action_attr);
3357 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
3359 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
3360 struct sk_buff *skb)
3362 struct nlattr *actions_attr;
3364 actions_attr = nla_nest_start_noflag(skb,
3365 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
3369 if (table->table_ops->actions_dump(table->priv, skb))
3370 goto nla_put_failure;
3372 nla_nest_end(skb, actions_attr);
3376 nla_nest_cancel(skb, actions_attr);
3380 static int devlink_dpipe_table_put(struct sk_buff *skb,
3381 struct devlink_dpipe_table *table)
3383 struct nlattr *table_attr;
3386 table_size = table->table_ops->size_get(table->priv);
3387 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
3391 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
3392 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
3394 goto nla_put_failure;
3395 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
3396 table->counters_enabled))
3397 goto nla_put_failure;
3399 if (table->resource_valid) {
3400 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
3401 table->resource_id, DEVLINK_ATTR_PAD) ||
3402 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
3403 table->resource_units, DEVLINK_ATTR_PAD))
3404 goto nla_put_failure;
3406 if (devlink_dpipe_matches_put(table, skb))
3407 goto nla_put_failure;
3409 if (devlink_dpipe_actions_put(table, skb))
3410 goto nla_put_failure;
3412 nla_nest_end(skb, table_attr);
3416 nla_nest_cancel(skb, table_attr);
3420 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
3421 struct genl_info *info)
3426 err = genlmsg_reply(*pskb, info);
3430 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
3436 static int devlink_dpipe_tables_fill(struct genl_info *info,
3437 enum devlink_command cmd, int flags,
3438 struct list_head *dpipe_tables,
3439 const char *table_name)
3441 struct devlink *devlink = info->user_ptr[0];
3442 struct devlink_dpipe_table *table;
3443 struct nlattr *tables_attr;
3444 struct sk_buff *skb = NULL;
3445 struct nlmsghdr *nlh;
3451 table = list_first_entry(dpipe_tables,
3452 struct devlink_dpipe_table, list);
3454 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3458 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3459 &devlink_nl_family, NLM_F_MULTI, cmd);
3465 if (devlink_nl_put_handle(skb, devlink))
3466 goto nla_put_failure;
3467 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3469 goto nla_put_failure;
3473 list_for_each_entry_from(table, dpipe_tables, list) {
3475 err = devlink_dpipe_table_put(skb, table);
3483 if (!strcmp(table->name, table_name)) {
3484 err = devlink_dpipe_table_put(skb, table);
3492 nla_nest_end(skb, tables_attr);
3493 genlmsg_end(skb, hdr);
3498 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3499 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3501 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3507 return genlmsg_reply(skb, info);
3516 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3517 struct genl_info *info)
3519 struct devlink *devlink = info->user_ptr[0];
3520 const char *table_name = NULL;
3522 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3523 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3525 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3526 &devlink->dpipe_table_list,
3530 static int devlink_dpipe_value_put(struct sk_buff *skb,
3531 struct devlink_dpipe_value *value)
3533 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3534 value->value_size, value->value))
3537 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3538 value->value_size, value->mask))
3540 if (value->mapping_valid)
3541 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3542 value->mapping_value))
3547 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3548 struct devlink_dpipe_value *value)
3552 if (devlink_dpipe_action_put(skb, value->action))
3554 if (devlink_dpipe_value_put(skb, value))
3559 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3560 struct devlink_dpipe_value *values,
3561 unsigned int values_count)
3563 struct nlattr *action_attr;
3567 for (i = 0; i < values_count; i++) {
3568 action_attr = nla_nest_start_noflag(skb,
3569 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3572 err = devlink_dpipe_action_value_put(skb, &values[i]);
3574 goto err_action_value_put;
3575 nla_nest_end(skb, action_attr);
3579 err_action_value_put:
3580 nla_nest_cancel(skb, action_attr);
3584 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3585 struct devlink_dpipe_value *value)
3589 if (devlink_dpipe_match_put(skb, value->match))
3591 if (devlink_dpipe_value_put(skb, value))
3596 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3597 struct devlink_dpipe_value *values,
3598 unsigned int values_count)
3600 struct nlattr *match_attr;
3604 for (i = 0; i < values_count; i++) {
3605 match_attr = nla_nest_start_noflag(skb,
3606 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3609 err = devlink_dpipe_match_value_put(skb, &values[i]);
3611 goto err_match_value_put;
3612 nla_nest_end(skb, match_attr);
3616 err_match_value_put:
3617 nla_nest_cancel(skb, match_attr);
3621 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3622 struct devlink_dpipe_entry *entry)
3624 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3627 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3631 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3633 goto nla_put_failure;
3634 if (entry->counter_valid)
3635 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3636 entry->counter, DEVLINK_ATTR_PAD))
3637 goto nla_put_failure;
3639 matches_attr = nla_nest_start_noflag(skb,
3640 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3642 goto nla_put_failure;
3644 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3645 entry->match_values_count);
3647 nla_nest_cancel(skb, matches_attr);
3648 goto err_match_values_put;
3650 nla_nest_end(skb, matches_attr);
3652 actions_attr = nla_nest_start_noflag(skb,
3653 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3655 goto nla_put_failure;
3657 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3658 entry->action_values_count);
3660 nla_nest_cancel(skb, actions_attr);
3661 goto err_action_values_put;
3663 nla_nest_end(skb, actions_attr);
3665 nla_nest_end(skb, entry_attr);
3670 err_match_values_put:
3671 err_action_values_put:
3672 nla_nest_cancel(skb, entry_attr);
3676 static struct devlink_dpipe_table *
3677 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3678 const char *table_name, struct devlink *devlink)
3680 struct devlink_dpipe_table *table;
3681 list_for_each_entry_rcu(table, dpipe_tables, list,
3682 lockdep_is_held(&devlink->lock)) {
3683 if (!strcmp(table->name, table_name))
3689 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3691 struct devlink *devlink;
3694 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3699 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3700 dump_ctx->info->snd_portid,
3701 dump_ctx->info->snd_seq,
3702 &devlink_nl_family, NLM_F_MULTI,
3705 goto nla_put_failure;
3707 devlink = dump_ctx->info->user_ptr[0];
3708 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3709 goto nla_put_failure;
3710 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3711 DEVLINK_ATTR_DPIPE_ENTRIES);
3712 if (!dump_ctx->nest)
3713 goto nla_put_failure;
3717 nlmsg_free(dump_ctx->skb);
3720 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3722 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3723 struct devlink_dpipe_entry *entry)
3725 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3727 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3729 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3731 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3732 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3735 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3737 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3740 unsigned int value_count, value_index;
3741 struct devlink_dpipe_value *value;
3743 value = entry->action_values;
3744 value_count = entry->action_values_count;
3745 for (value_index = 0; value_index < value_count; value_index++) {
3746 kfree(value[value_index].value);
3747 kfree(value[value_index].mask);
3750 value = entry->match_values;
3751 value_count = entry->match_values_count;
3752 for (value_index = 0; value_index < value_count; value_index++) {
3753 kfree(value[value_index].value);
3754 kfree(value[value_index].mask);
3757 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_clear);
3759 static int devlink_dpipe_entries_fill(struct genl_info *info,
3760 enum devlink_command cmd, int flags,
3761 struct devlink_dpipe_table *table)
3763 struct devlink_dpipe_dump_ctx dump_ctx;
3764 struct nlmsghdr *nlh;
3767 dump_ctx.skb = NULL;
3769 dump_ctx.info = info;
3771 err = table->table_ops->entries_dump(table->priv,
3772 table->counters_enabled,
3778 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3779 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3781 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3786 return genlmsg_reply(dump_ctx.skb, info);
3789 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3790 struct genl_info *info)
3792 struct devlink *devlink = info->user_ptr[0];
3793 struct devlink_dpipe_table *table;
3794 const char *table_name;
3796 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME))
3799 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3800 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3801 table_name, devlink);
3805 if (!table->table_ops->entries_dump)
3808 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3812 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3813 const struct devlink_dpipe_header *header)
3815 struct devlink_dpipe_field *field;
3816 struct nlattr *field_attr;
3819 for (i = 0; i < header->fields_count; i++) {
3820 field = &header->fields[i];
3821 field_attr = nla_nest_start_noflag(skb,
3822 DEVLINK_ATTR_DPIPE_FIELD);
3825 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3826 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3827 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3828 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3829 goto nla_put_failure;
3830 nla_nest_end(skb, field_attr);
3835 nla_nest_cancel(skb, field_attr);
3839 static int devlink_dpipe_header_put(struct sk_buff *skb,
3840 struct devlink_dpipe_header *header)
3842 struct nlattr *fields_attr, *header_attr;
3845 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3849 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3850 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3851 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3852 goto nla_put_failure;
3854 fields_attr = nla_nest_start_noflag(skb,
3855 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3857 goto nla_put_failure;
3859 err = devlink_dpipe_fields_put(skb, header);
3861 nla_nest_cancel(skb, fields_attr);
3862 goto nla_put_failure;
3864 nla_nest_end(skb, fields_attr);
3865 nla_nest_end(skb, header_attr);
3870 nla_nest_cancel(skb, header_attr);
3874 static int devlink_dpipe_headers_fill(struct genl_info *info,
3875 enum devlink_command cmd, int flags,
3876 struct devlink_dpipe_headers *
3879 struct devlink *devlink = info->user_ptr[0];
3880 struct nlattr *headers_attr;
3881 struct sk_buff *skb = NULL;
3882 struct nlmsghdr *nlh;
3889 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3893 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3894 &devlink_nl_family, NLM_F_MULTI, cmd);
3900 if (devlink_nl_put_handle(skb, devlink))
3901 goto nla_put_failure;
3902 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3904 goto nla_put_failure;
3907 for (; i < dpipe_headers->headers_count; i++) {
3908 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3916 nla_nest_end(skb, headers_attr);
3917 genlmsg_end(skb, hdr);
3918 if (i != dpipe_headers->headers_count)
3922 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3923 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3925 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3930 return genlmsg_reply(skb, info);
3939 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3940 struct genl_info *info)
3942 struct devlink *devlink = info->user_ptr[0];
3944 if (!devlink->dpipe_headers)
3946 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3947 0, devlink->dpipe_headers);
3950 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3951 const char *table_name,
3954 struct devlink_dpipe_table *table;
3956 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3957 table_name, devlink);
3961 if (table->counter_control_extern)
3964 if (!(table->counters_enabled ^ enable))
3967 table->counters_enabled = enable;
3968 if (table->table_ops->counters_set_update)
3969 table->table_ops->counters_set_update(table->priv, enable);
3973 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
3974 struct genl_info *info)
3976 struct devlink *devlink = info->user_ptr[0];
3977 const char *table_name;
3978 bool counters_enable;
3980 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_DPIPE_TABLE_NAME) ||
3981 GENL_REQ_ATTR_CHECK(info,
3982 DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED))
3985 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3986 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
3988 return devlink_dpipe_table_counters_set(devlink, table_name,
3992 static struct devlink_resource *
3993 devlink_resource_find(struct devlink *devlink,
3994 struct devlink_resource *resource, u64 resource_id)
3996 struct list_head *resource_list;
3999 resource_list = &resource->resource_list;
4001 resource_list = &devlink->resource_list;
4003 list_for_each_entry(resource, resource_list, list) {
4004 struct devlink_resource *child_resource;
4006 if (resource->id == resource_id)
4009 child_resource = devlink_resource_find(devlink, resource,
4012 return child_resource;
4018 devlink_resource_validate_children(struct devlink_resource *resource)
4020 struct devlink_resource *child_resource;
4021 bool size_valid = true;
4024 if (list_empty(&resource->resource_list))
4027 list_for_each_entry(child_resource, &resource->resource_list, list)
4028 parts_size += child_resource->size_new;
4030 if (parts_size > resource->size_new)
4033 resource->size_valid = size_valid;
4037 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
4038 struct netlink_ext_ack *extack)
4043 if (size > resource->size_params.size_max) {
4044 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
4048 if (size < resource->size_params.size_min) {
4049 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
4053 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
4055 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
4062 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
4063 struct genl_info *info)
4065 struct devlink *devlink = info->user_ptr[0];
4066 struct devlink_resource *resource;
4071 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_ID) ||
4072 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_RESOURCE_SIZE))
4074 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
4076 resource = devlink_resource_find(devlink, NULL, resource_id);
4080 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
4081 err = devlink_resource_validate_size(resource, size, info->extack);
4085 resource->size_new = size;
4086 devlink_resource_validate_children(resource);
4087 if (resource->parent)
4088 devlink_resource_validate_children(resource->parent);
4093 devlink_resource_size_params_put(struct devlink_resource *resource,
4094 struct sk_buff *skb)
4096 struct devlink_resource_size_params *size_params;
4098 size_params = &resource->size_params;
4099 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
4100 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
4101 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
4102 size_params->size_max, DEVLINK_ATTR_PAD) ||
4103 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
4104 size_params->size_min, DEVLINK_ATTR_PAD) ||
4105 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
4110 static int devlink_resource_occ_put(struct devlink_resource *resource,
4111 struct sk_buff *skb)
4113 if (!resource->occ_get)
4115 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
4116 resource->occ_get(resource->occ_get_priv),
4120 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
4121 struct devlink_resource *resource)
4123 struct devlink_resource *child_resource;
4124 struct nlattr *child_resource_attr;
4125 struct nlattr *resource_attr;
4127 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
4131 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
4132 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
4133 DEVLINK_ATTR_PAD) ||
4134 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
4136 goto nla_put_failure;
4137 if (resource->size != resource->size_new &&
4138 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
4139 resource->size_new, DEVLINK_ATTR_PAD))
4140 goto nla_put_failure;
4141 if (devlink_resource_occ_put(resource, skb))
4142 goto nla_put_failure;
4143 if (devlink_resource_size_params_put(resource, skb))
4144 goto nla_put_failure;
4145 if (list_empty(&resource->resource_list))
4148 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
4149 resource->size_valid))
4150 goto nla_put_failure;
4152 child_resource_attr = nla_nest_start_noflag(skb,
4153 DEVLINK_ATTR_RESOURCE_LIST);
4154 if (!child_resource_attr)
4155 goto nla_put_failure;
4157 list_for_each_entry(child_resource, &resource->resource_list, list) {
4158 if (devlink_resource_put(devlink, skb, child_resource))
4159 goto resource_put_failure;
4162 nla_nest_end(skb, child_resource_attr);
4164 nla_nest_end(skb, resource_attr);
4167 resource_put_failure:
4168 nla_nest_cancel(skb, child_resource_attr);
4170 nla_nest_cancel(skb, resource_attr);
4174 static int devlink_resource_fill(struct genl_info *info,
4175 enum devlink_command cmd, int flags)
4177 struct devlink *devlink = info->user_ptr[0];
4178 struct devlink_resource *resource;
4179 struct nlattr *resources_attr;
4180 struct sk_buff *skb = NULL;
4181 struct nlmsghdr *nlh;
4187 resource = list_first_entry(&devlink->resource_list,
4188 struct devlink_resource, list);
4190 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4194 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4195 &devlink_nl_family, NLM_F_MULTI, cmd);
4201 if (devlink_nl_put_handle(skb, devlink))
4202 goto nla_put_failure;
4204 resources_attr = nla_nest_start_noflag(skb,
4205 DEVLINK_ATTR_RESOURCE_LIST);
4206 if (!resources_attr)
4207 goto nla_put_failure;
4211 list_for_each_entry_from(resource, &devlink->resource_list, list) {
4212 err = devlink_resource_put(devlink, skb, resource);
4215 goto err_resource_put;
4221 nla_nest_end(skb, resources_attr);
4222 genlmsg_end(skb, hdr);
4226 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4227 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4229 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
4234 return genlmsg_reply(skb, info);
4243 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
4244 struct genl_info *info)
4246 struct devlink *devlink = info->user_ptr[0];
4248 if (list_empty(&devlink->resource_list))
4251 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
4255 devlink_resources_validate(struct devlink *devlink,
4256 struct devlink_resource *resource,
4257 struct genl_info *info)
4259 struct list_head *resource_list;
4263 resource_list = &resource->resource_list;
4265 resource_list = &devlink->resource_list;
4267 list_for_each_entry(resource, resource_list, list) {
4268 if (!resource->size_valid)
4270 err = devlink_resources_validate(devlink, resource, info);
4277 static struct net *devlink_netns_get(struct sk_buff *skb,
4278 struct genl_info *info)
4280 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
4281 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
4282 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
4285 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
4286 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
4287 return ERR_PTR(-EINVAL);
4290 if (netns_pid_attr) {
4291 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
4292 } else if (netns_fd_attr) {
4293 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
4294 } else if (netns_id_attr) {
4295 net = get_net_ns_by_id(sock_net(skb->sk),
4296 nla_get_u32(netns_id_attr));
4298 net = ERR_PTR(-EINVAL);
4301 net = ERR_PTR(-EINVAL);
4304 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
4305 return ERR_PTR(-EINVAL);
4307 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
4309 return ERR_PTR(-EPERM);
4314 static void devlink_param_notify(struct devlink *devlink,
4315 unsigned int port_index,
4316 struct devlink_param_item *param_item,
4317 enum devlink_command cmd);
4319 static void devlink_ns_change_notify(struct devlink *devlink,
4320 struct net *dest_net, struct net *curr_net,
4323 struct devlink_param_item *param_item;
4324 enum devlink_command cmd;
4326 /* Userspace needs to be notified about devlink objects
4327 * removed from original and entering new network namespace.
4328 * The rest of the devlink objects are re-created during
4329 * reload process so the notifications are generated separatelly.
4332 if (!dest_net || net_eq(dest_net, curr_net))
4336 devlink_notify(devlink, DEVLINK_CMD_NEW);
4338 cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
4339 list_for_each_entry(param_item, &devlink->param_list, list)
4340 devlink_param_notify(devlink, 0, param_item, cmd);
4343 devlink_notify(devlink, DEVLINK_CMD_DEL);
4346 static void devlink_reload_failed_set(struct devlink *devlink,
4349 if (devlink->reload_failed == reload_failed)
4351 devlink->reload_failed = reload_failed;
4352 devlink_notify(devlink, DEVLINK_CMD_NEW);
4355 bool devlink_is_reload_failed(const struct devlink *devlink)
4357 return devlink->reload_failed;
4359 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
4362 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
4363 enum devlink_reload_limit limit, u32 actions_performed)
4365 unsigned long actions = actions_performed;
4369 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
4370 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
4371 reload_stats[stat_idx]++;
4373 devlink_notify(devlink, DEVLINK_CMD_NEW);
4377 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
4378 u32 actions_performed)
4380 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
4385 * devlink_remote_reload_actions_performed - Update devlink on reload actions
4386 * performed which are not a direct result of devlink reload call.
4388 * This should be called by a driver after performing reload actions in case it was not
4389 * a result of devlink reload call. For example fw_activate was performed as a result
4390 * of devlink reload triggered fw_activate on another host.
4391 * The motivation for this function is to keep data on reload actions performed on this
4392 * function whether it was done due to direct devlink reload call or not.
4395 * @limit: reload limit
4396 * @actions_performed: bitmask of actions performed
4398 void devlink_remote_reload_actions_performed(struct devlink *devlink,
4399 enum devlink_reload_limit limit,
4400 u32 actions_performed)
4402 if (WARN_ON(!actions_performed ||
4403 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
4404 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
4405 limit > DEVLINK_RELOAD_LIMIT_MAX))
4408 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
4411 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
4413 int devlink_reload(struct devlink *devlink, struct net *dest_net,
4414 enum devlink_reload_action action,
4415 enum devlink_reload_limit limit,
4416 u32 *actions_performed, struct netlink_ext_ack *extack)
4418 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
4419 struct net *curr_net;
4422 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
4423 sizeof(remote_reload_stats));
4425 curr_net = devlink_net(devlink);
4426 devlink_ns_change_notify(devlink, dest_net, curr_net, false);
4427 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
4431 if (dest_net && !net_eq(dest_net, curr_net)) {
4432 move_netdevice_notifier_net(curr_net, dest_net,
4433 &devlink->netdevice_nb);
4434 write_pnet(&devlink->_net, dest_net);
4437 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
4438 devlink_reload_failed_set(devlink, !!err);
4442 devlink_ns_change_notify(devlink, dest_net, curr_net, true);
4443 WARN_ON(!(*actions_performed & BIT(action)));
4444 /* Catch driver on updating the remote action within devlink reload */
4445 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
4446 sizeof(remote_reload_stats)));
4447 devlink_reload_stats_update(devlink, limit, *actions_performed);
4452 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
4453 enum devlink_command cmd, struct genl_info *info)
4455 struct sk_buff *msg;
4458 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4462 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
4466 if (devlink_nl_put_handle(msg, devlink))
4467 goto nla_put_failure;
4469 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4471 goto nla_put_failure;
4472 genlmsg_end(msg, hdr);
4474 return genlmsg_reply(msg, info);
4477 genlmsg_cancel(msg, hdr);
4483 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4485 struct devlink *devlink = info->user_ptr[0];
4486 enum devlink_reload_action action;
4487 enum devlink_reload_limit limit;
4488 struct net *dest_net = NULL;
4489 u32 actions_performed;
4492 if (!(devlink->features & DEVLINK_F_RELOAD))
4495 err = devlink_resources_validate(devlink, NULL, info);
4497 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4501 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4502 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4504 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4506 if (!devlink_reload_action_is_supported(devlink, action)) {
4507 NL_SET_ERR_MSG_MOD(info->extack,
4508 "Requested reload action is not supported by the driver");
4512 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4513 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4514 struct nla_bitfield32 limits;
4515 u32 limits_selected;
4517 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4518 limits_selected = limits.value & limits.selector;
4519 if (!limits_selected) {
4520 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4523 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4524 if (limits_selected & BIT(limit))
4526 /* UAPI enables multiselection, but currently it is not used */
4527 if (limits_selected != BIT(limit)) {
4528 NL_SET_ERR_MSG_MOD(info->extack,
4529 "Multiselection of limit is not supported");
4532 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4533 NL_SET_ERR_MSG_MOD(info->extack,
4534 "Requested limit is not supported by the driver");
4537 if (devlink_reload_combination_is_invalid(action, limit)) {
4538 NL_SET_ERR_MSG_MOD(info->extack,
4539 "Requested limit is invalid for this action");
4543 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4544 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4545 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4546 dest_net = devlink_netns_get(skb, info);
4547 if (IS_ERR(dest_net))
4548 return PTR_ERR(dest_net);
4551 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4558 /* For backward compatibility generate reply only if attributes used by user */
4559 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4562 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4563 DEVLINK_CMD_RELOAD, info);
4566 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4567 struct devlink *devlink,
4568 enum devlink_command cmd,
4569 struct devlink_flash_notify *params)
4573 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4577 if (devlink_nl_put_handle(msg, devlink))
4578 goto nla_put_failure;
4580 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4583 if (params->status_msg &&
4584 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4585 params->status_msg))
4586 goto nla_put_failure;
4587 if (params->component &&
4588 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4590 goto nla_put_failure;
4591 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4592 params->done, DEVLINK_ATTR_PAD))
4593 goto nla_put_failure;
4594 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4595 params->total, DEVLINK_ATTR_PAD))
4596 goto nla_put_failure;
4597 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4598 params->timeout, DEVLINK_ATTR_PAD))
4599 goto nla_put_failure;
4602 genlmsg_end(msg, hdr);
4606 genlmsg_cancel(msg, hdr);
4610 static void __devlink_flash_update_notify(struct devlink *devlink,
4611 enum devlink_command cmd,
4612 struct devlink_flash_notify *params)
4614 struct sk_buff *msg;
4617 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4618 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4619 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4621 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
4624 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4628 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4632 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4633 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4640 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4642 struct devlink_flash_notify params = {};
4644 __devlink_flash_update_notify(devlink,
4645 DEVLINK_CMD_FLASH_UPDATE,
4649 static void devlink_flash_update_end_notify(struct devlink *devlink)
4651 struct devlink_flash_notify params = {};
4653 __devlink_flash_update_notify(devlink,
4654 DEVLINK_CMD_FLASH_UPDATE_END,
4658 void devlink_flash_update_status_notify(struct devlink *devlink,
4659 const char *status_msg,
4660 const char *component,
4662 unsigned long total)
4664 struct devlink_flash_notify params = {
4665 .status_msg = status_msg,
4666 .component = component,
4671 __devlink_flash_update_notify(devlink,
4672 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4675 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4677 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4678 const char *status_msg,
4679 const char *component,
4680 unsigned long timeout)
4682 struct devlink_flash_notify params = {
4683 .status_msg = status_msg,
4684 .component = component,
4688 __devlink_flash_update_notify(devlink,
4689 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4692 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4694 struct devlink_info_req {
4695 struct sk_buff *msg;
4696 void (*version_cb)(const char *version_name,
4697 enum devlink_info_version_type version_type,
4698 void *version_cb_priv);
4699 void *version_cb_priv;
4702 struct devlink_flash_component_lookup_ctx {
4703 const char *lookup_name;
4704 bool lookup_name_found;
4708 devlink_flash_component_lookup_cb(const char *version_name,
4709 enum devlink_info_version_type version_type,
4710 void *version_cb_priv)
4712 struct devlink_flash_component_lookup_ctx *lookup_ctx = version_cb_priv;
4714 if (version_type != DEVLINK_INFO_VERSION_TYPE_COMPONENT ||
4715 lookup_ctx->lookup_name_found)
4718 lookup_ctx->lookup_name_found =
4719 !strcmp(lookup_ctx->lookup_name, version_name);
4722 static int devlink_flash_component_get(struct devlink *devlink,
4723 struct nlattr *nla_component,
4724 const char **p_component,
4725 struct netlink_ext_ack *extack)
4727 struct devlink_flash_component_lookup_ctx lookup_ctx = {};
4728 struct devlink_info_req req = {};
4729 const char *component;
4735 component = nla_data(nla_component);
4737 if (!devlink->ops->info_get) {
4738 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4739 "component update is not supported by this device");
4743 lookup_ctx.lookup_name = component;
4744 req.version_cb = devlink_flash_component_lookup_cb;
4745 req.version_cb_priv = &lookup_ctx;
4747 ret = devlink->ops->info_get(devlink, &req, NULL);
4751 if (!lookup_ctx.lookup_name_found) {
4752 NL_SET_ERR_MSG_ATTR(extack, nla_component,
4753 "selected component is not supported by this device");
4756 *p_component = component;
4760 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4761 struct genl_info *info)
4763 struct nlattr *nla_overwrite_mask, *nla_file_name;
4764 struct devlink_flash_update_params params = {};
4765 struct devlink *devlink = info->user_ptr[0];
4766 const char *file_name;
4767 u32 supported_params;
4770 if (!devlink->ops->flash_update)
4773 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME))
4776 ret = devlink_flash_component_get(devlink,
4777 info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT],
4778 ¶ms.component, info->extack);
4782 supported_params = devlink->ops->supported_flash_update_params;
4784 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4785 if (nla_overwrite_mask) {
4786 struct nla_bitfield32 sections;
4788 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4789 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4790 "overwrite settings are not supported by this device");
4793 sections = nla_get_bitfield32(nla_overwrite_mask);
4794 params.overwrite_mask = sections.value & sections.selector;
4797 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4798 file_name = nla_data(nla_file_name);
4799 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
4801 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4805 devlink_flash_update_begin_notify(devlink);
4806 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
4807 devlink_flash_update_end_notify(devlink);
4809 release_firmware(params.fw);
4815 devlink_nl_selftests_fill(struct sk_buff *msg, struct devlink *devlink,
4816 u32 portid, u32 seq, int flags,
4817 struct netlink_ext_ack *extack)
4819 struct nlattr *selftests;
4824 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags,
4825 DEVLINK_CMD_SELFTESTS_GET);
4830 if (devlink_nl_put_handle(msg, devlink))
4831 goto err_cancel_msg;
4833 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4835 goto err_cancel_msg;
4837 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4838 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4839 if (devlink->ops->selftest_check(devlink, i, extack)) {
4840 err = nla_put_flag(msg, i);
4842 goto err_cancel_msg;
4846 nla_nest_end(msg, selftests);
4847 genlmsg_end(msg, hdr);
4851 genlmsg_cancel(msg, hdr);
4855 static int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb,
4856 struct genl_info *info)
4858 struct devlink *devlink = info->user_ptr[0];
4859 struct sk_buff *msg;
4862 if (!devlink->ops->selftest_check)
4865 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4869 err = devlink_nl_selftests_fill(msg, devlink, info->snd_portid,
4870 info->snd_seq, 0, info->extack);
4876 return genlmsg_reply(msg, info);
4879 static int devlink_nl_cmd_selftests_get_dumpit(struct sk_buff *msg,
4880 struct netlink_callback *cb)
4882 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
4883 struct devlink *devlink;
4884 int start = state->idx;
4885 unsigned long index;
4889 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
4890 if (idx < start || !devlink->ops->selftest_check)
4894 err = devlink_nl_selftests_fill(msg, devlink,
4895 NETLINK_CB(cb->skb).portid,
4896 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4898 devl_unlock(devlink);
4900 devlink_put(devlink);
4905 devlink_put(devlink);
4908 if (err != -EMSGSIZE)
4915 static int devlink_selftest_result_put(struct sk_buff *skb, unsigned int id,
4916 enum devlink_selftest_status test_status)
4918 struct nlattr *result_attr;
4920 result_attr = nla_nest_start(skb, DEVLINK_ATTR_SELFTEST_RESULT);
4924 if (nla_put_u32(skb, DEVLINK_ATTR_SELFTEST_RESULT_ID, id) ||
4925 nla_put_u8(skb, DEVLINK_ATTR_SELFTEST_RESULT_STATUS,
4927 goto nla_put_failure;
4929 nla_nest_end(skb, result_attr);
4933 nla_nest_cancel(skb, result_attr);
4937 static int devlink_nl_cmd_selftests_run(struct sk_buff *skb,
4938 struct genl_info *info)
4940 struct nlattr *tb[DEVLINK_ATTR_SELFTEST_ID_MAX + 1];
4941 struct devlink *devlink = info->user_ptr[0];
4942 struct nlattr *attrs, *selftests;
4943 struct sk_buff *msg;
4948 if (!devlink->ops->selftest_run || !devlink->ops->selftest_check)
4951 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_SELFTESTS))
4954 attrs = info->attrs[DEVLINK_ATTR_SELFTESTS];
4956 err = nla_parse_nested(tb, DEVLINK_ATTR_SELFTEST_ID_MAX, attrs,
4957 devlink_selftest_nl_policy, info->extack);
4961 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4966 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
4967 &devlink_nl_family, 0, DEVLINK_CMD_SELFTESTS_RUN);
4971 if (devlink_nl_put_handle(msg, devlink))
4972 goto genlmsg_cancel;
4974 selftests = nla_nest_start(msg, DEVLINK_ATTR_SELFTESTS);
4976 goto genlmsg_cancel;
4978 for (i = DEVLINK_ATTR_SELFTEST_ID_UNSPEC + 1;
4979 i <= DEVLINK_ATTR_SELFTEST_ID_MAX; i++) {
4980 enum devlink_selftest_status test_status;
4982 if (nla_get_flag(tb[i])) {
4983 if (!devlink->ops->selftest_check(devlink, i,
4985 if (devlink_selftest_result_put(msg, i,
4986 DEVLINK_SELFTEST_STATUS_SKIP))
4987 goto selftests_nest_cancel;
4991 test_status = devlink->ops->selftest_run(devlink, i,
4993 if (devlink_selftest_result_put(msg, i, test_status))
4994 goto selftests_nest_cancel;
4998 nla_nest_end(msg, selftests);
4999 genlmsg_end(msg, hdr);
5000 return genlmsg_reply(msg, info);
5002 selftests_nest_cancel:
5003 nla_nest_cancel(msg, selftests);
5005 genlmsg_cancel(msg, hdr);
5011 static const struct devlink_param devlink_param_generic[] = {
5013 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
5014 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
5015 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
5018 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
5019 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
5020 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
5023 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
5024 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
5025 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
5028 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
5029 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
5030 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
5033 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
5034 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
5035 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
5038 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
5039 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
5040 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
5043 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
5044 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
5045 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
5048 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
5049 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
5050 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
5053 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
5054 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
5055 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
5058 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
5059 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
5060 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
5063 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
5064 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
5065 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
5068 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
5069 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
5070 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
5073 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
5074 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
5075 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
5078 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
5079 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
5080 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
5083 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
5084 .name = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_NAME,
5085 .type = DEVLINK_PARAM_GENERIC_ENABLE_IWARP_TYPE,
5088 .id = DEVLINK_PARAM_GENERIC_ID_IO_EQ_SIZE,
5089 .name = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_NAME,
5090 .type = DEVLINK_PARAM_GENERIC_IO_EQ_SIZE_TYPE,
5093 .id = DEVLINK_PARAM_GENERIC_ID_EVENT_EQ_SIZE,
5094 .name = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_NAME,
5095 .type = DEVLINK_PARAM_GENERIC_EVENT_EQ_SIZE_TYPE,
5099 static int devlink_param_generic_verify(const struct devlink_param *param)
5101 /* verify it match generic parameter by id and name */
5102 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
5104 if (strcmp(param->name, devlink_param_generic[param->id].name))
5107 WARN_ON(param->type != devlink_param_generic[param->id].type);
5112 static int devlink_param_driver_verify(const struct devlink_param *param)
5116 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
5118 /* verify no such name in generic params */
5119 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
5120 if (!strcmp(param->name, devlink_param_generic[i].name))
5126 static struct devlink_param_item *
5127 devlink_param_find_by_name(struct list_head *param_list,
5128 const char *param_name)
5130 struct devlink_param_item *param_item;
5132 list_for_each_entry(param_item, param_list, list)
5133 if (!strcmp(param_item->param->name, param_name))
5138 static struct devlink_param_item *
5139 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
5141 struct devlink_param_item *param_item;
5143 list_for_each_entry(param_item, param_list, list)
5144 if (param_item->param->id == param_id)
5150 devlink_param_cmode_is_supported(const struct devlink_param *param,
5151 enum devlink_param_cmode cmode)
5153 return test_bit(cmode, ¶m->supported_cmodes);
5156 static int devlink_param_get(struct devlink *devlink,
5157 const struct devlink_param *param,
5158 struct devlink_param_gset_ctx *ctx)
5160 if (!param->get || devlink->reload_failed)
5162 return param->get(devlink, param->id, ctx);
5165 static int devlink_param_set(struct devlink *devlink,
5166 const struct devlink_param *param,
5167 struct devlink_param_gset_ctx *ctx)
5169 if (!param->set || devlink->reload_failed)
5171 return param->set(devlink, param->id, ctx);
5175 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
5177 switch (param_type) {
5178 case DEVLINK_PARAM_TYPE_U8:
5180 case DEVLINK_PARAM_TYPE_U16:
5182 case DEVLINK_PARAM_TYPE_U32:
5184 case DEVLINK_PARAM_TYPE_STRING:
5186 case DEVLINK_PARAM_TYPE_BOOL:
5194 devlink_nl_param_value_fill_one(struct sk_buff *msg,
5195 enum devlink_param_type type,
5196 enum devlink_param_cmode cmode,
5197 union devlink_param_value val)
5199 struct nlattr *param_value_attr;
5201 param_value_attr = nla_nest_start_noflag(msg,
5202 DEVLINK_ATTR_PARAM_VALUE);
5203 if (!param_value_attr)
5204 goto nla_put_failure;
5206 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
5207 goto value_nest_cancel;
5210 case DEVLINK_PARAM_TYPE_U8:
5211 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
5212 goto value_nest_cancel;
5214 case DEVLINK_PARAM_TYPE_U16:
5215 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
5216 goto value_nest_cancel;
5218 case DEVLINK_PARAM_TYPE_U32:
5219 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
5220 goto value_nest_cancel;
5222 case DEVLINK_PARAM_TYPE_STRING:
5223 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
5225 goto value_nest_cancel;
5227 case DEVLINK_PARAM_TYPE_BOOL:
5229 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
5230 goto value_nest_cancel;
5234 nla_nest_end(msg, param_value_attr);
5238 nla_nest_cancel(msg, param_value_attr);
5243 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
5244 unsigned int port_index,
5245 struct devlink_param_item *param_item,
5246 enum devlink_command cmd,
5247 u32 portid, u32 seq, int flags)
5249 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
5250 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
5251 const struct devlink_param *param = param_item->param;
5252 struct devlink_param_gset_ctx ctx;
5253 struct nlattr *param_values_list;
5254 struct nlattr *param_attr;
5260 /* Get value from driver part to driverinit configuration mode */
5261 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5262 if (!devlink_param_cmode_is_supported(param, i))
5264 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5265 if (!param_item->driverinit_value_valid)
5267 param_value[i] = param_item->driverinit_value;
5270 err = devlink_param_get(devlink, param, &ctx);
5273 param_value[i] = ctx.val;
5275 param_value_set[i] = true;
5278 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5282 if (devlink_nl_put_handle(msg, devlink))
5283 goto genlmsg_cancel;
5285 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
5286 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
5287 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
5288 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
5289 goto genlmsg_cancel;
5291 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
5293 goto genlmsg_cancel;
5294 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
5295 goto param_nest_cancel;
5296 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
5297 goto param_nest_cancel;
5299 nla_type = devlink_param_type_to_nla_type(param->type);
5301 goto param_nest_cancel;
5302 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
5303 goto param_nest_cancel;
5305 param_values_list = nla_nest_start_noflag(msg,
5306 DEVLINK_ATTR_PARAM_VALUES_LIST);
5307 if (!param_values_list)
5308 goto param_nest_cancel;
5310 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
5311 if (!param_value_set[i])
5313 err = devlink_nl_param_value_fill_one(msg, param->type,
5316 goto values_list_nest_cancel;
5319 nla_nest_end(msg, param_values_list);
5320 nla_nest_end(msg, param_attr);
5321 genlmsg_end(msg, hdr);
5324 values_list_nest_cancel:
5325 nla_nest_end(msg, param_values_list);
5327 nla_nest_cancel(msg, param_attr);
5329 genlmsg_cancel(msg, hdr);
5333 static void devlink_param_notify(struct devlink *devlink,
5334 unsigned int port_index,
5335 struct devlink_param_item *param_item,
5336 enum devlink_command cmd)
5338 struct sk_buff *msg;
5341 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
5342 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
5343 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
5344 ASSERT_DEVLINK_REGISTERED(devlink);
5346 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5349 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
5356 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
5357 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5360 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
5361 struct netlink_callback *cb)
5363 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
5364 struct devlink_param_item *param_item;
5365 struct devlink *devlink;
5366 int start = state->idx;
5367 unsigned long index;
5371 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
5373 list_for_each_entry(param_item, &devlink->param_list, list) {
5378 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5379 DEVLINK_CMD_PARAM_GET,
5380 NETLINK_CB(cb->skb).portid,
5383 if (err == -EOPNOTSUPP) {
5386 devl_unlock(devlink);
5387 devlink_put(devlink);
5392 devl_unlock(devlink);
5393 devlink_put(devlink);
5396 if (err != -EMSGSIZE)
5404 devlink_param_type_get_from_info(struct genl_info *info,
5405 enum devlink_param_type *param_type)
5407 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE))
5410 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
5412 *param_type = DEVLINK_PARAM_TYPE_U8;
5415 *param_type = DEVLINK_PARAM_TYPE_U16;
5418 *param_type = DEVLINK_PARAM_TYPE_U32;
5421 *param_type = DEVLINK_PARAM_TYPE_STRING;
5424 *param_type = DEVLINK_PARAM_TYPE_BOOL;
5434 devlink_param_value_get_from_info(const struct devlink_param *param,
5435 struct genl_info *info,
5436 union devlink_param_value *value)
5438 struct nlattr *param_data;
5441 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
5443 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
5446 switch (param->type) {
5447 case DEVLINK_PARAM_TYPE_U8:
5448 if (nla_len(param_data) != sizeof(u8))
5450 value->vu8 = nla_get_u8(param_data);
5452 case DEVLINK_PARAM_TYPE_U16:
5453 if (nla_len(param_data) != sizeof(u16))
5455 value->vu16 = nla_get_u16(param_data);
5457 case DEVLINK_PARAM_TYPE_U32:
5458 if (nla_len(param_data) != sizeof(u32))
5460 value->vu32 = nla_get_u32(param_data);
5462 case DEVLINK_PARAM_TYPE_STRING:
5463 len = strnlen(nla_data(param_data), nla_len(param_data));
5464 if (len == nla_len(param_data) ||
5465 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
5467 strcpy(value->vstr, nla_data(param_data));
5469 case DEVLINK_PARAM_TYPE_BOOL:
5470 if (param_data && nla_len(param_data))
5472 value->vbool = nla_get_flag(param_data);
5478 static struct devlink_param_item *
5479 devlink_param_get_from_info(struct list_head *param_list,
5480 struct genl_info *info)
5484 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_NAME))
5487 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
5488 return devlink_param_find_by_name(param_list, param_name);
5491 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
5492 struct genl_info *info)
5494 struct devlink *devlink = info->user_ptr[0];
5495 struct devlink_param_item *param_item;
5496 struct sk_buff *msg;
5499 param_item = devlink_param_get_from_info(&devlink->param_list, info);
5503 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5507 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
5508 DEVLINK_CMD_PARAM_GET,
5509 info->snd_portid, info->snd_seq, 0);
5515 return genlmsg_reply(msg, info);
5518 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
5519 unsigned int port_index,
5520 struct list_head *param_list,
5521 struct genl_info *info,
5522 enum devlink_command cmd)
5524 enum devlink_param_type param_type;
5525 struct devlink_param_gset_ctx ctx;
5526 enum devlink_param_cmode cmode;
5527 struct devlink_param_item *param_item;
5528 const struct devlink_param *param;
5529 union devlink_param_value value;
5532 param_item = devlink_param_get_from_info(param_list, info);
5535 param = param_item->param;
5536 err = devlink_param_type_get_from_info(info, ¶m_type);
5539 if (param_type != param->type)
5541 err = devlink_param_value_get_from_info(param, info, &value);
5544 if (param->validate) {
5545 err = param->validate(devlink, param->id, value, info->extack);
5550 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_VALUE_CMODE))
5552 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
5553 if (!devlink_param_cmode_is_supported(param, cmode))
5556 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
5557 if (param->type == DEVLINK_PARAM_TYPE_STRING)
5558 strcpy(param_item->driverinit_value.vstr, value.vstr);
5560 param_item->driverinit_value = value;
5561 param_item->driverinit_value_valid = true;
5567 err = devlink_param_set(devlink, param, &ctx);
5572 devlink_param_notify(devlink, port_index, param_item, cmd);
5576 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
5577 struct genl_info *info)
5579 struct devlink *devlink = info->user_ptr[0];
5581 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
5582 info, DEVLINK_CMD_PARAM_NEW);
5585 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
5586 struct netlink_callback *cb)
5588 NL_SET_ERR_MSG_MOD(cb->extack, "Port params are not supported");
5592 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
5593 struct genl_info *info)
5595 NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5599 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
5600 struct genl_info *info)
5602 NL_SET_ERR_MSG_MOD(info->extack, "Port params are not supported");
5606 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
5607 struct devlink *devlink,
5608 struct devlink_snapshot *snapshot)
5610 struct nlattr *snap_attr;
5613 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5617 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5619 goto nla_put_failure;
5621 nla_nest_end(msg, snap_attr);
5625 nla_nest_cancel(msg, snap_attr);
5629 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5630 struct devlink *devlink,
5631 struct devlink_region *region)
5633 struct devlink_snapshot *snapshot;
5634 struct nlattr *snapshots_attr;
5637 snapshots_attr = nla_nest_start_noflag(msg,
5638 DEVLINK_ATTR_REGION_SNAPSHOTS);
5639 if (!snapshots_attr)
5642 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
5643 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5645 goto nla_put_failure;
5648 nla_nest_end(msg, snapshots_attr);
5652 nla_nest_cancel(msg, snapshots_attr);
5656 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5657 enum devlink_command cmd, u32 portid,
5659 struct devlink_region *region)
5664 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5668 err = devlink_nl_put_handle(msg, devlink);
5670 goto nla_put_failure;
5673 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5674 region->port->index);
5676 goto nla_put_failure;
5679 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5681 goto nla_put_failure;
5683 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5687 goto nla_put_failure;
5689 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_MAX_SNAPSHOTS,
5690 region->max_snapshots);
5692 goto nla_put_failure;
5694 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5696 goto nla_put_failure;
5698 genlmsg_end(msg, hdr);
5702 genlmsg_cancel(msg, hdr);
5706 static struct sk_buff *
5707 devlink_nl_region_notify_build(struct devlink_region *region,
5708 struct devlink_snapshot *snapshot,
5709 enum devlink_command cmd, u32 portid, u32 seq)
5711 struct devlink *devlink = region->devlink;
5712 struct sk_buff *msg;
5717 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5719 return ERR_PTR(-ENOMEM);
5721 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5727 err = devlink_nl_put_handle(msg, devlink);
5729 goto out_cancel_msg;
5732 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5733 region->port->index);
5735 goto out_cancel_msg;
5738 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5741 goto out_cancel_msg;
5744 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5747 goto out_cancel_msg;
5749 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5750 region->size, DEVLINK_ATTR_PAD);
5752 goto out_cancel_msg;
5754 genlmsg_end(msg, hdr);
5759 genlmsg_cancel(msg, hdr);
5762 return ERR_PTR(err);
5765 static void devlink_nl_region_notify(struct devlink_region *region,
5766 struct devlink_snapshot *snapshot,
5767 enum devlink_command cmd)
5769 struct devlink *devlink = region->devlink;
5770 struct sk_buff *msg;
5772 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5773 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
5776 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5780 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
5781 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5785 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5786 * @devlink: devlink instance
5787 * @id: the snapshot id
5789 * Track when a new snapshot begins using an id. Load the count for the
5790 * given id from the snapshot xarray, increment it, and store it back.
5792 * Called when a new snapshot is created with the given id.
5794 * The id *must* have been previously allocated by
5795 * devlink_region_snapshot_id_get().
5797 * Returns 0 on success, or an error on failure.
5799 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5801 unsigned long count;
5805 xa_lock(&devlink->snapshot_ids);
5806 p = xa_load(&devlink->snapshot_ids, id);
5812 if (WARN_ON(!xa_is_value(p))) {
5817 count = xa_to_value(p);
5820 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5823 xa_unlock(&devlink->snapshot_ids);
5828 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5829 * @devlink: devlink instance
5830 * @id: the snapshot id
5832 * Track when a snapshot is deleted and stops using an id. Load the count
5833 * for the given id from the snapshot xarray, decrement it, and store it
5836 * If the count reaches zero, erase this id from the xarray, freeing it
5837 * up for future re-use by devlink_region_snapshot_id_get().
5839 * Called when a snapshot using the given id is deleted, and when the
5840 * initial allocator of the id is finished using it.
5842 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5844 unsigned long count;
5847 xa_lock(&devlink->snapshot_ids);
5848 p = xa_load(&devlink->snapshot_ids, id);
5852 if (WARN_ON(!xa_is_value(p)))
5855 count = xa_to_value(p);
5859 __xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5862 /* If this was the last user, we can erase this id */
5863 __xa_erase(&devlink->snapshot_ids, id);
5866 xa_unlock(&devlink->snapshot_ids);
5870 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
5871 * @devlink: devlink instance
5872 * @id: the snapshot id
5874 * Mark the given snapshot id as used by inserting a zero value into the
5877 * This must be called while holding the devlink instance lock. Unlike
5878 * devlink_snapshot_id_get, the initial reference count is zero, not one.
5879 * It is expected that the id will immediately be used before
5880 * releasing the devlink instance lock.
5882 * Returns zero on success, or an error code if the snapshot id could not
5885 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5889 xa_lock(&devlink->snapshot_ids);
5890 if (xa_load(&devlink->snapshot_ids, id)) {
5891 xa_unlock(&devlink->snapshot_ids);
5894 err = xa_err(__xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5896 xa_unlock(&devlink->snapshot_ids);
5901 * __devlink_region_snapshot_id_get - get snapshot ID
5902 * @devlink: devlink instance
5903 * @id: storage to return snapshot id
5905 * Allocates a new snapshot id. Returns zero on success, or a negative
5906 * error on failure. Must be called while holding the devlink instance
5909 * Snapshot IDs are tracked using an xarray which stores the number of
5910 * users of the snapshot id.
5912 * Note that the caller of this function counts as a 'user', in order to
5913 * avoid race conditions. The caller must release its hold on the
5914 * snapshot by using devlink_region_snapshot_id_put.
5916 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5918 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5919 xa_limit_32b, GFP_KERNEL);
5923 * __devlink_region_snapshot_create - create a new snapshot
5924 * This will add a new snapshot of a region. The snapshot
5925 * will be stored on the region struct and can be accessed
5926 * from devlink. This is useful for future analyses of snapshots.
5927 * Multiple snapshots can be created on a region.
5928 * The @snapshot_id should be obtained using the getter function.
5930 * Must be called only while holding the region snapshot lock.
5932 * @region: devlink region of the snapshot
5933 * @data: snapshot data
5934 * @snapshot_id: snapshot id to be created
5937 __devlink_region_snapshot_create(struct devlink_region *region,
5938 u8 *data, u32 snapshot_id)
5940 struct devlink *devlink = region->devlink;
5941 struct devlink_snapshot *snapshot;
5944 lockdep_assert_held(®ion->snapshot_lock);
5946 /* check if region can hold one more snapshot */
5947 if (region->cur_snapshots == region->max_snapshots)
5950 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
5953 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5957 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
5959 goto err_snapshot_id_increment;
5961 snapshot->id = snapshot_id;
5962 snapshot->region = region;
5963 snapshot->data = data;
5965 list_add_tail(&snapshot->list, ®ion->snapshot_list);
5967 region->cur_snapshots++;
5969 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5972 err_snapshot_id_increment:
5977 static void devlink_region_snapshot_del(struct devlink_region *region,
5978 struct devlink_snapshot *snapshot)
5980 struct devlink *devlink = region->devlink;
5982 lockdep_assert_held(®ion->snapshot_lock);
5984 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
5985 region->cur_snapshots--;
5986 list_del(&snapshot->list);
5987 region->ops->destructor(snapshot->data);
5988 __devlink_snapshot_id_decrement(devlink, snapshot->id);
5992 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
5993 struct genl_info *info)
5995 struct devlink *devlink = info->user_ptr[0];
5996 struct devlink_port *port = NULL;
5997 struct devlink_region *region;
5998 const char *region_name;
5999 struct sk_buff *msg;
6003 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME))
6006 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6007 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6009 port = devlink_port_get_by_index(devlink, index);
6014 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6016 region = devlink_port_region_get_by_name(port, region_name);
6018 region = devlink_region_get_by_name(devlink, region_name);
6023 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6027 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
6028 info->snd_portid, info->snd_seq, 0,
6035 return genlmsg_reply(msg, info);
6038 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
6039 struct netlink_callback *cb,
6040 struct devlink_port *port,
6044 struct devlink_region *region;
6047 list_for_each_entry(region, &port->region_list, list) {
6052 err = devlink_nl_region_fill(msg, port->devlink,
6053 DEVLINK_CMD_REGION_GET,
6054 NETLINK_CB(cb->skb).portid,
6056 NLM_F_MULTI, region);
6066 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
6067 struct netlink_callback *cb,
6068 struct devlink *devlink,
6072 struct devlink_region *region;
6073 struct devlink_port *port;
6074 unsigned long port_index;
6078 list_for_each_entry(region, &devlink->region_list, list) {
6083 err = devlink_nl_region_fill(msg, devlink,
6084 DEVLINK_CMD_REGION_GET,
6085 NETLINK_CB(cb->skb).portid,
6087 NLM_F_MULTI, region);
6093 xa_for_each(&devlink->ports, port_index, port) {
6094 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
6101 devl_unlock(devlink);
6105 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
6106 struct netlink_callback *cb)
6108 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6109 struct devlink *devlink;
6110 int start = state->idx;
6111 unsigned long index;
6115 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
6116 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
6118 devlink_put(devlink);
6127 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
6128 struct genl_info *info)
6130 struct devlink *devlink = info->user_ptr[0];
6131 struct devlink_snapshot *snapshot;
6132 struct devlink_port *port = NULL;
6133 struct devlink_region *region;
6134 const char *region_name;
6138 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME) ||
6139 GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_SNAPSHOT_ID))
6142 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6143 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
6145 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6146 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6148 port = devlink_port_get_by_index(devlink, index);
6154 region = devlink_port_region_get_by_name(port, region_name);
6156 region = devlink_region_get_by_name(devlink, region_name);
6161 mutex_lock(®ion->snapshot_lock);
6162 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6164 mutex_unlock(®ion->snapshot_lock);
6168 devlink_region_snapshot_del(region, snapshot);
6169 mutex_unlock(®ion->snapshot_lock);
6174 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
6176 struct devlink *devlink = info->user_ptr[0];
6177 struct devlink_snapshot *snapshot;
6178 struct devlink_port *port = NULL;
6179 struct nlattr *snapshot_id_attr;
6180 struct devlink_region *region;
6181 const char *region_name;
6187 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_REGION_NAME)) {
6188 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
6192 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
6194 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6195 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6197 port = devlink_port_get_by_index(devlink, index);
6203 region = devlink_port_region_get_by_name(port, region_name);
6205 region = devlink_region_get_by_name(devlink, region_name);
6208 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
6212 if (!region->ops->snapshot) {
6213 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
6217 mutex_lock(®ion->snapshot_lock);
6219 if (region->cur_snapshots == region->max_snapshots) {
6220 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
6225 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6226 if (snapshot_id_attr) {
6227 snapshot_id = nla_get_u32(snapshot_id_attr);
6229 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6230 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
6235 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
6239 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
6241 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
6247 err = region->port_ops->snapshot(port, region->port_ops,
6248 info->extack, &data);
6250 err = region->ops->snapshot(devlink, region->ops,
6251 info->extack, &data);
6253 goto err_snapshot_capture;
6255 err = __devlink_region_snapshot_create(region, data, snapshot_id);
6257 goto err_snapshot_create;
6259 if (!snapshot_id_attr) {
6260 struct sk_buff *msg;
6262 snapshot = devlink_region_snapshot_get_by_id(region,
6264 if (WARN_ON(!snapshot)) {
6269 msg = devlink_nl_region_notify_build(region, snapshot,
6270 DEVLINK_CMD_REGION_NEW,
6273 err = PTR_ERR_OR_ZERO(msg);
6277 err = genlmsg_reply(msg, info);
6282 mutex_unlock(®ion->snapshot_lock);
6285 err_snapshot_create:
6286 region->ops->destructor(data);
6287 err_snapshot_capture:
6288 __devlink_snapshot_id_decrement(devlink, snapshot_id);
6289 mutex_unlock(®ion->snapshot_lock);
6293 devlink_region_snapshot_del(region, snapshot);
6295 mutex_unlock(®ion->snapshot_lock);
6299 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
6300 u8 *chunk, u32 chunk_size,
6303 struct nlattr *chunk_attr;
6306 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
6310 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
6312 goto nla_put_failure;
6314 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
6317 goto nla_put_failure;
6319 nla_nest_end(msg, chunk_attr);
6323 nla_nest_cancel(msg, chunk_attr);
6327 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
6329 typedef int devlink_chunk_fill_t(void *cb_priv, u8 *chunk, u32 chunk_size,
6331 struct netlink_ext_ack *extack);
6334 devlink_nl_region_read_fill(struct sk_buff *skb, devlink_chunk_fill_t *cb,
6335 void *cb_priv, u64 start_offset, u64 end_offset,
6336 u64 *new_offset, struct netlink_ext_ack *extack)
6338 u64 curr_offset = start_offset;
6342 /* Allocate and re-use a single buffer */
6343 data = kmalloc(DEVLINK_REGION_READ_CHUNK_SIZE, GFP_KERNEL);
6347 *new_offset = start_offset;
6349 while (curr_offset < end_offset) {
6352 data_size = min_t(u32, end_offset - curr_offset,
6353 DEVLINK_REGION_READ_CHUNK_SIZE);
6355 err = cb(cb_priv, data, data_size, curr_offset, extack);
6359 err = devlink_nl_cmd_region_read_chunk_fill(skb, data, data_size, curr_offset);
6363 curr_offset += data_size;
6365 *new_offset = curr_offset;
6373 devlink_region_snapshot_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6375 struct netlink_ext_ack __always_unused *extack)
6377 struct devlink_snapshot *snapshot = cb_priv;
6379 memcpy(chunk, &snapshot->data[curr_offset], chunk_size);
6385 devlink_region_port_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6386 u64 curr_offset, struct netlink_ext_ack *extack)
6388 struct devlink_region *region = cb_priv;
6390 return region->port_ops->read(region->port, region->port_ops, extack,
6391 curr_offset, chunk_size, chunk);
6395 devlink_region_direct_fill(void *cb_priv, u8 *chunk, u32 chunk_size,
6396 u64 curr_offset, struct netlink_ext_ack *extack)
6398 struct devlink_region *region = cb_priv;
6400 return region->ops->read(region->devlink, region->ops, extack,
6401 curr_offset, chunk_size, chunk);
6404 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
6405 struct netlink_callback *cb)
6407 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6408 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6409 struct nlattr *chunks_attr, *region_attr, *snapshot_attr;
6410 u64 ret_offset, start_offset, end_offset = U64_MAX;
6411 struct nlattr **attrs = info->attrs;
6412 struct devlink_port *port = NULL;
6413 devlink_chunk_fill_t *region_cb;
6414 struct devlink_region *region;
6415 const char *region_name;
6416 struct devlink *devlink;
6418 void *region_cb_priv;
6422 start_offset = state->start_offset;
6424 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6425 if (IS_ERR(devlink))
6426 return PTR_ERR(devlink);
6430 if (!attrs[DEVLINK_ATTR_REGION_NAME]) {
6431 NL_SET_ERR_MSG(cb->extack, "No region name provided");
6436 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
6437 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
6439 port = devlink_port_get_by_index(devlink, index);
6446 region_attr = attrs[DEVLINK_ATTR_REGION_NAME];
6447 region_name = nla_data(region_attr);
6450 region = devlink_port_region_get_by_name(port, region_name);
6452 region = devlink_region_get_by_name(devlink, region_name);
6455 NL_SET_ERR_MSG_ATTR(cb->extack, region_attr, "Requested region does not exist");
6460 snapshot_attr = attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
6461 if (!snapshot_attr) {
6462 if (!nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
6463 NL_SET_ERR_MSG(cb->extack, "No snapshot id provided");
6468 if (!region->ops->read) {
6469 NL_SET_ERR_MSG(cb->extack, "Requested region does not support direct read");
6475 region_cb = &devlink_region_port_direct_fill;
6477 region_cb = &devlink_region_direct_fill;
6478 region_cb_priv = region;
6480 struct devlink_snapshot *snapshot;
6483 if (nla_get_flag(attrs[DEVLINK_ATTR_REGION_DIRECT])) {
6484 NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Direct region read does not use snapshot");
6489 snapshot_id = nla_get_u32(snapshot_attr);
6490 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
6492 NL_SET_ERR_MSG_ATTR(cb->extack, snapshot_attr, "Requested snapshot does not exist");
6496 region_cb = &devlink_region_snapshot_fill;
6497 region_cb_priv = snapshot;
6500 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
6501 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
6504 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6506 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
6507 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
6510 if (end_offset > region->size)
6511 end_offset = region->size;
6513 /* return 0 if there is no further data to read */
6514 if (start_offset == end_offset) {
6519 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6520 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
6521 DEVLINK_CMD_REGION_READ);
6527 err = devlink_nl_put_handle(skb, devlink);
6529 goto nla_put_failure;
6532 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
6533 region->port->index);
6535 goto nla_put_failure;
6538 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
6540 goto nla_put_failure;
6542 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
6545 goto nla_put_failure;
6548 err = devlink_nl_region_read_fill(skb, region_cb, region_cb_priv,
6549 start_offset, end_offset, &ret_offset,
6552 if (err && err != -EMSGSIZE)
6553 goto nla_put_failure;
6555 /* Check if there was any progress done to prevent infinite loop */
6556 if (ret_offset == start_offset) {
6558 goto nla_put_failure;
6561 state->start_offset = ret_offset;
6563 nla_nest_end(skb, chunks_attr);
6564 genlmsg_end(skb, hdr);
6565 devl_unlock(devlink);
6566 devlink_put(devlink);
6570 genlmsg_cancel(skb, hdr);
6572 devl_unlock(devlink);
6573 devlink_put(devlink);
6577 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
6581 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
6583 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
6585 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
6590 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
6593 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
6595 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
6596 const char *version_name,
6597 const char *version_value,
6598 enum devlink_info_version_type version_type)
6600 struct nlattr *nest;
6603 if (req->version_cb)
6604 req->version_cb(version_name, version_type,
6605 req->version_cb_priv);
6610 nest = nla_nest_start_noflag(req->msg, attr);
6614 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
6617 goto nla_put_failure;
6619 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
6622 goto nla_put_failure;
6624 nla_nest_end(req->msg, nest);
6629 nla_nest_cancel(req->msg, nest);
6633 int devlink_info_version_fixed_put(struct devlink_info_req *req,
6634 const char *version_name,
6635 const char *version_value)
6637 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
6638 version_name, version_value,
6639 DEVLINK_INFO_VERSION_TYPE_NONE);
6641 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
6643 int devlink_info_version_stored_put(struct devlink_info_req *req,
6644 const char *version_name,
6645 const char *version_value)
6647 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6648 version_name, version_value,
6649 DEVLINK_INFO_VERSION_TYPE_NONE);
6651 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
6653 int devlink_info_version_stored_put_ext(struct devlink_info_req *req,
6654 const char *version_name,
6655 const char *version_value,
6656 enum devlink_info_version_type version_type)
6658 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
6659 version_name, version_value,
6662 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put_ext);
6664 int devlink_info_version_running_put(struct devlink_info_req *req,
6665 const char *version_name,
6666 const char *version_value)
6668 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6669 version_name, version_value,
6670 DEVLINK_INFO_VERSION_TYPE_NONE);
6672 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
6674 int devlink_info_version_running_put_ext(struct devlink_info_req *req,
6675 const char *version_name,
6676 const char *version_value,
6677 enum devlink_info_version_type version_type)
6679 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
6680 version_name, version_value,
6683 EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
6685 static int devlink_nl_driver_info_get(struct device_driver *drv,
6686 struct devlink_info_req *req)
6692 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME,
6699 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
6700 enum devlink_command cmd, u32 portid,
6701 u32 seq, int flags, struct netlink_ext_ack *extack)
6703 struct device *dev = devlink_to_dev(devlink);
6704 struct devlink_info_req req = {};
6708 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6713 if (devlink_nl_put_handle(msg, devlink))
6714 goto err_cancel_msg;
6717 if (devlink->ops->info_get) {
6718 err = devlink->ops->info_get(devlink, &req, extack);
6720 goto err_cancel_msg;
6723 err = devlink_nl_driver_info_get(dev->driver, &req);
6725 goto err_cancel_msg;
6727 genlmsg_end(msg, hdr);
6731 genlmsg_cancel(msg, hdr);
6735 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
6736 struct genl_info *info)
6738 struct devlink *devlink = info->user_ptr[0];
6739 struct sk_buff *msg;
6742 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6746 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6747 info->snd_portid, info->snd_seq, 0,
6754 return genlmsg_reply(msg, info);
6757 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
6758 struct netlink_callback *cb)
6760 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
6761 struct devlink *devlink;
6762 int start = state->idx;
6763 unsigned long index;
6767 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
6772 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6773 NETLINK_CB(cb->skb).portid,
6774 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6776 devl_unlock(devlink);
6777 if (err == -EOPNOTSUPP)
6780 devlink_put(devlink);
6785 devlink_put(devlink);
6788 if (err != -EMSGSIZE)
6795 struct devlink_fmsg_item {
6796 struct list_head list;
6803 struct devlink_fmsg {
6804 struct list_head item_list;
6805 bool putting_binary; /* This flag forces enclosing of binary data
6806 * in an array brackets. It forces using
6807 * of designated API:
6808 * devlink_fmsg_binary_pair_nest_start()
6809 * devlink_fmsg_binary_pair_nest_end()
6813 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6815 struct devlink_fmsg *fmsg;
6817 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6821 INIT_LIST_HEAD(&fmsg->item_list);
6826 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6828 struct devlink_fmsg_item *item, *tmp;
6830 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6831 list_del(&item->list);
6837 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6840 struct devlink_fmsg_item *item;
6842 item = kzalloc(sizeof(*item), GFP_KERNEL);
6846 item->attrtype = attrtype;
6847 list_add_tail(&item->list, &fmsg->item_list);
6852 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6854 if (fmsg->putting_binary)
6857 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6859 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6861 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6863 if (fmsg->putting_binary)
6866 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6869 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6871 if (fmsg->putting_binary)
6874 return devlink_fmsg_nest_end(fmsg);
6876 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6878 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6880 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6882 struct devlink_fmsg_item *item;
6884 if (fmsg->putting_binary)
6887 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6890 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6894 item->nla_type = NLA_NUL_STRING;
6895 item->len = strlen(name) + 1;
6896 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6897 memcpy(&item->value, name, item->len);
6898 list_add_tail(&item->list, &fmsg->item_list);
6903 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6907 if (fmsg->putting_binary)
6910 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6914 err = devlink_fmsg_put_name(fmsg, name);
6920 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6922 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6924 if (fmsg->putting_binary)
6927 return devlink_fmsg_nest_end(fmsg);
6929 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6931 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6936 if (fmsg->putting_binary)
6939 err = devlink_fmsg_pair_nest_start(fmsg, name);
6943 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6949 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6951 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6955 if (fmsg->putting_binary)
6958 err = devlink_fmsg_nest_end(fmsg);
6962 err = devlink_fmsg_nest_end(fmsg);
6968 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6970 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6975 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6979 fmsg->putting_binary = true;
6982 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6984 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6986 if (!fmsg->putting_binary)
6989 fmsg->putting_binary = false;
6990 return devlink_fmsg_arr_pair_nest_end(fmsg);
6992 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6994 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6995 const void *value, u16 value_len,
6998 struct devlink_fmsg_item *item;
7000 if (value_len > DEVLINK_FMSG_MAX_SIZE)
7003 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
7007 item->nla_type = value_nla_type;
7008 item->len = value_len;
7009 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
7010 memcpy(&item->value, value, item->len);
7011 list_add_tail(&item->list, &fmsg->item_list);
7016 static int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
7018 if (fmsg->putting_binary)
7021 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
7024 static int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
7026 if (fmsg->putting_binary)
7029 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
7032 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
7034 if (fmsg->putting_binary)
7037 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
7039 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
7041 static int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
7043 if (fmsg->putting_binary)
7046 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
7049 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
7051 if (fmsg->putting_binary)
7054 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
7057 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
7059 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
7062 if (!fmsg->putting_binary)
7065 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
7067 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
7069 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
7074 err = devlink_fmsg_pair_nest_start(fmsg, name);
7078 err = devlink_fmsg_bool_put(fmsg, value);
7082 err = devlink_fmsg_pair_nest_end(fmsg);
7088 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
7090 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
7095 err = devlink_fmsg_pair_nest_start(fmsg, name);
7099 err = devlink_fmsg_u8_put(fmsg, value);
7103 err = devlink_fmsg_pair_nest_end(fmsg);
7109 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
7111 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
7116 err = devlink_fmsg_pair_nest_start(fmsg, name);
7120 err = devlink_fmsg_u32_put(fmsg, value);
7124 err = devlink_fmsg_pair_nest_end(fmsg);
7130 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
7132 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
7137 err = devlink_fmsg_pair_nest_start(fmsg, name);
7141 err = devlink_fmsg_u64_put(fmsg, value);
7145 err = devlink_fmsg_pair_nest_end(fmsg);
7151 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
7153 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
7158 err = devlink_fmsg_pair_nest_start(fmsg, name);
7162 err = devlink_fmsg_string_put(fmsg, value);
7166 err = devlink_fmsg_pair_nest_end(fmsg);
7172 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
7174 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
7175 const void *value, u32 value_len)
7182 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
7186 for (offset = 0; offset < value_len; offset += data_size) {
7187 data_size = value_len - offset;
7188 if (data_size > DEVLINK_FMSG_MAX_SIZE)
7189 data_size = DEVLINK_FMSG_MAX_SIZE;
7190 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
7193 /* Exit from loop with a break (instead of
7194 * return) to make sure putting_binary is turned off in
7195 * devlink_fmsg_binary_pair_nest_end
7199 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
7205 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
7208 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7210 switch (msg->nla_type) {
7215 case NLA_NUL_STRING:
7217 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
7225 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
7227 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
7230 switch (msg->nla_type) {
7232 /* Always provide flag data, regardless of its value */
7233 tmp = *(bool *) msg->value;
7235 return nla_put_u8(skb, attrtype, tmp);
7237 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
7239 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
7241 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
7243 case NLA_NUL_STRING:
7244 return nla_put_string(skb, attrtype, (char *) &msg->value);
7246 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
7253 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7256 struct devlink_fmsg_item *item;
7257 struct nlattr *fmsg_nlattr;
7261 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
7265 list_for_each_entry(item, &fmsg->item_list, list) {
7271 switch (item->attrtype) {
7272 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
7273 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
7274 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
7275 case DEVLINK_ATTR_FMSG_NEST_END:
7276 err = nla_put_flag(skb, item->attrtype);
7278 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
7279 err = devlink_fmsg_item_fill_type(item, skb);
7282 err = devlink_fmsg_item_fill_data(item, skb);
7284 case DEVLINK_ATTR_FMSG_OBJ_NAME:
7285 err = nla_put_string(skb, item->attrtype,
7286 (char *) &item->value);
7298 nla_nest_end(skb, fmsg_nlattr);
7302 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
7303 struct genl_info *info,
7304 enum devlink_command cmd, int flags)
7306 struct nlmsghdr *nlh;
7307 struct sk_buff *skb;
7314 int tmp_index = index;
7316 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7320 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
7321 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
7324 goto nla_put_failure;
7327 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7330 else if (err != -EMSGSIZE || tmp_index == index)
7331 goto nla_put_failure;
7333 genlmsg_end(skb, hdr);
7334 err = genlmsg_reply(skb, info);
7339 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
7342 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
7343 NLMSG_DONE, 0, flags | NLM_F_MULTI);
7346 goto nla_put_failure;
7349 return genlmsg_reply(skb, info);
7356 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
7357 struct netlink_callback *cb,
7358 enum devlink_command cmd)
7360 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7361 int index = state->idx;
7362 int tmp_index = index;
7366 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7367 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
7370 goto nla_put_failure;
7373 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
7374 if ((err && err != -EMSGSIZE) || tmp_index == index)
7375 goto nla_put_failure;
7378 genlmsg_end(skb, hdr);
7382 genlmsg_cancel(skb, hdr);
7386 struct devlink_health_reporter {
7387 struct list_head list;
7389 const struct devlink_health_reporter_ops *ops;
7390 struct devlink *devlink;
7391 struct devlink_port *devlink_port;
7392 struct devlink_fmsg *dump_fmsg;
7393 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
7394 u64 graceful_period;
7402 u64 last_recovery_ts;
7403 refcount_t refcount;
7407 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
7409 return reporter->priv;
7411 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
7413 static struct devlink_health_reporter *
7414 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
7415 struct mutex *list_lock,
7416 const char *reporter_name)
7418 struct devlink_health_reporter *reporter;
7420 lockdep_assert_held(list_lock);
7421 list_for_each_entry(reporter, reporter_list, list)
7422 if (!strcmp(reporter->ops->name, reporter_name))
7427 static struct devlink_health_reporter *
7428 devlink_health_reporter_find_by_name(struct devlink *devlink,
7429 const char *reporter_name)
7431 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
7432 &devlink->reporters_lock,
7436 static struct devlink_health_reporter *
7437 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
7438 const char *reporter_name)
7440 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
7441 &devlink_port->reporters_lock,
7445 static struct devlink_health_reporter *
7446 __devlink_health_reporter_create(struct devlink *devlink,
7447 const struct devlink_health_reporter_ops *ops,
7448 u64 graceful_period, void *priv)
7450 struct devlink_health_reporter *reporter;
7452 if (WARN_ON(graceful_period && !ops->recover))
7453 return ERR_PTR(-EINVAL);
7455 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
7457 return ERR_PTR(-ENOMEM);
7459 reporter->priv = priv;
7460 reporter->ops = ops;
7461 reporter->devlink = devlink;
7462 reporter->graceful_period = graceful_period;
7463 reporter->auto_recover = !!ops->recover;
7464 reporter->auto_dump = !!ops->dump;
7465 mutex_init(&reporter->dump_lock);
7466 refcount_set(&reporter->refcount, 1);
7471 * devlink_port_health_reporter_create - create devlink health reporter for
7472 * specified port instance
7474 * @port: devlink_port which should contain the new reporter
7476 * @graceful_period: to avoid recovery loops, in msecs
7479 struct devlink_health_reporter *
7480 devlink_port_health_reporter_create(struct devlink_port *port,
7481 const struct devlink_health_reporter_ops *ops,
7482 u64 graceful_period, void *priv)
7484 struct devlink_health_reporter *reporter;
7486 mutex_lock(&port->reporters_lock);
7487 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
7488 &port->reporters_lock, ops->name)) {
7489 reporter = ERR_PTR(-EEXIST);
7493 reporter = __devlink_health_reporter_create(port->devlink, ops,
7494 graceful_period, priv);
7495 if (IS_ERR(reporter))
7498 reporter->devlink_port = port;
7499 list_add_tail(&reporter->list, &port->reporter_list);
7501 mutex_unlock(&port->reporters_lock);
7504 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
7507 * devlink_health_reporter_create - create devlink health reporter
7511 * @graceful_period: to avoid recovery loops, in msecs
7514 struct devlink_health_reporter *
7515 devlink_health_reporter_create(struct devlink *devlink,
7516 const struct devlink_health_reporter_ops *ops,
7517 u64 graceful_period, void *priv)
7519 struct devlink_health_reporter *reporter;
7521 mutex_lock(&devlink->reporters_lock);
7522 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
7523 reporter = ERR_PTR(-EEXIST);
7527 reporter = __devlink_health_reporter_create(devlink, ops,
7528 graceful_period, priv);
7529 if (IS_ERR(reporter))
7532 list_add_tail(&reporter->list, &devlink->reporter_list);
7534 mutex_unlock(&devlink->reporters_lock);
7537 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
7540 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
7542 mutex_destroy(&reporter->dump_lock);
7543 if (reporter->dump_fmsg)
7544 devlink_fmsg_free(reporter->dump_fmsg);
7549 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
7551 if (refcount_dec_and_test(&reporter->refcount))
7552 devlink_health_reporter_free(reporter);
7556 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7558 list_del(&reporter->list);
7559 devlink_health_reporter_put(reporter);
7563 * devlink_health_reporter_destroy - destroy devlink health reporter
7565 * @reporter: devlink health reporter to destroy
7568 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
7570 struct mutex *lock = &reporter->devlink->reporters_lock;
7573 __devlink_health_reporter_destroy(reporter);
7576 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
7579 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
7581 * @reporter: devlink health reporter to destroy
7584 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
7586 struct mutex *lock = &reporter->devlink_port->reporters_lock;
7589 __devlink_health_reporter_destroy(reporter);
7592 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
7595 devlink_nl_health_reporter_fill(struct sk_buff *msg,
7596 struct devlink_health_reporter *reporter,
7597 enum devlink_command cmd, u32 portid,
7600 struct devlink *devlink = reporter->devlink;
7601 struct nlattr *reporter_attr;
7604 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7608 if (devlink_nl_put_handle(msg, devlink))
7609 goto genlmsg_cancel;
7611 if (reporter->devlink_port) {
7612 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
7613 goto genlmsg_cancel;
7615 reporter_attr = nla_nest_start_noflag(msg,
7616 DEVLINK_ATTR_HEALTH_REPORTER);
7618 goto genlmsg_cancel;
7619 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
7620 reporter->ops->name))
7621 goto reporter_nest_cancel;
7622 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
7623 reporter->health_state))
7624 goto reporter_nest_cancel;
7625 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
7626 reporter->error_count, DEVLINK_ATTR_PAD))
7627 goto reporter_nest_cancel;
7628 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
7629 reporter->recovery_count, DEVLINK_ATTR_PAD))
7630 goto reporter_nest_cancel;
7631 if (reporter->ops->recover &&
7632 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
7633 reporter->graceful_period,
7635 goto reporter_nest_cancel;
7636 if (reporter->ops->recover &&
7637 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
7638 reporter->auto_recover))
7639 goto reporter_nest_cancel;
7640 if (reporter->dump_fmsg &&
7641 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
7642 jiffies_to_msecs(reporter->dump_ts),
7644 goto reporter_nest_cancel;
7645 if (reporter->dump_fmsg &&
7646 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
7647 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
7648 goto reporter_nest_cancel;
7649 if (reporter->ops->dump &&
7650 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
7651 reporter->auto_dump))
7652 goto reporter_nest_cancel;
7654 nla_nest_end(msg, reporter_attr);
7655 genlmsg_end(msg, hdr);
7658 reporter_nest_cancel:
7659 nla_nest_end(msg, reporter_attr);
7661 genlmsg_cancel(msg, hdr);
7665 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
7666 enum devlink_command cmd)
7668 struct devlink *devlink = reporter->devlink;
7669 struct sk_buff *msg;
7672 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7673 WARN_ON(!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED));
7675 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7679 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
7685 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
7686 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
7690 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
7692 reporter->recovery_count++;
7693 reporter->last_recovery_ts = jiffies;
7695 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
7698 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
7699 void *priv_ctx, struct netlink_ext_ack *extack)
7703 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
7706 if (!reporter->ops->recover)
7709 err = reporter->ops->recover(reporter, priv_ctx, extack);
7713 devlink_health_reporter_recovery_done(reporter);
7714 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
7715 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7721 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
7723 if (!reporter->dump_fmsg)
7725 devlink_fmsg_free(reporter->dump_fmsg);
7726 reporter->dump_fmsg = NULL;
7729 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
7731 struct netlink_ext_ack *extack)
7735 if (!reporter->ops->dump)
7738 if (reporter->dump_fmsg)
7741 reporter->dump_fmsg = devlink_fmsg_alloc();
7742 if (!reporter->dump_fmsg) {
7747 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7751 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7756 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7760 reporter->dump_ts = jiffies;
7761 reporter->dump_real_ts = ktime_get_real_ns();
7766 devlink_health_dump_clear(reporter);
7770 int devlink_health_report(struct devlink_health_reporter *reporter,
7771 const char *msg, void *priv_ctx)
7773 enum devlink_health_reporter_state prev_health_state;
7774 struct devlink *devlink = reporter->devlink;
7775 unsigned long recover_ts_threshold;
7778 /* write a log message of the current error */
7780 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7781 reporter->error_count++;
7782 prev_health_state = reporter->health_state;
7783 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7784 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7786 /* abort if the previous error wasn't recovered */
7787 recover_ts_threshold = reporter->last_recovery_ts +
7788 msecs_to_jiffies(reporter->graceful_period);
7789 if (reporter->auto_recover &&
7790 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7791 (reporter->last_recovery_ts && reporter->recovery_count &&
7792 time_is_after_jiffies(recover_ts_threshold)))) {
7793 trace_devlink_health_recover_aborted(devlink,
7794 reporter->ops->name,
7795 reporter->health_state,
7797 reporter->last_recovery_ts);
7801 if (reporter->auto_dump) {
7802 mutex_lock(&reporter->dump_lock);
7803 /* store current dump of current error, for later analysis */
7804 devlink_health_do_dump(reporter, priv_ctx, NULL);
7805 mutex_unlock(&reporter->dump_lock);
7808 if (!reporter->auto_recover)
7812 ret = devlink_health_reporter_recover(reporter, priv_ctx, NULL);
7813 devl_unlock(devlink);
7817 EXPORT_SYMBOL_GPL(devlink_health_report);
7819 static struct devlink_health_reporter *
7820 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7821 struct nlattr **attrs)
7823 struct devlink_health_reporter *reporter;
7824 struct devlink_port *devlink_port;
7825 char *reporter_name;
7827 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7830 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7831 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7832 if (IS_ERR(devlink_port)) {
7833 mutex_lock(&devlink->reporters_lock);
7834 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7836 refcount_inc(&reporter->refcount);
7837 mutex_unlock(&devlink->reporters_lock);
7839 mutex_lock(&devlink_port->reporters_lock);
7840 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7842 refcount_inc(&reporter->refcount);
7843 mutex_unlock(&devlink_port->reporters_lock);
7849 static struct devlink_health_reporter *
7850 devlink_health_reporter_get_from_info(struct devlink *devlink,
7851 struct genl_info *info)
7853 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7856 static struct devlink_health_reporter *
7857 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7859 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7860 struct devlink_health_reporter *reporter;
7861 struct nlattr **attrs = info->attrs;
7862 struct devlink *devlink;
7864 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
7865 if (IS_ERR(devlink))
7868 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7869 devlink_put(devlink);
7874 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7875 enum devlink_health_reporter_state state)
7877 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7878 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7881 if (reporter->health_state == state)
7884 reporter->health_state = state;
7885 trace_devlink_health_reporter_state_update(reporter->devlink,
7886 reporter->ops->name, state);
7887 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7889 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7891 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7892 struct genl_info *info)
7894 struct devlink *devlink = info->user_ptr[0];
7895 struct devlink_health_reporter *reporter;
7896 struct sk_buff *msg;
7899 reporter = devlink_health_reporter_get_from_info(devlink, info);
7903 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7909 err = devlink_nl_health_reporter_fill(msg, reporter,
7910 DEVLINK_CMD_HEALTH_REPORTER_GET,
7911 info->snd_portid, info->snd_seq,
7918 err = genlmsg_reply(msg, info);
7920 devlink_health_reporter_put(reporter);
7925 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7926 struct netlink_callback *cb)
7928 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
7929 struct devlink_health_reporter *reporter;
7930 unsigned long index, port_index;
7931 struct devlink_port *port;
7932 struct devlink *devlink;
7933 int start = state->idx;
7937 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
7938 mutex_lock(&devlink->reporters_lock);
7939 list_for_each_entry(reporter, &devlink->reporter_list,
7945 err = devlink_nl_health_reporter_fill(
7946 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7947 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7950 mutex_unlock(&devlink->reporters_lock);
7951 devlink_put(devlink);
7956 mutex_unlock(&devlink->reporters_lock);
7957 devlink_put(devlink);
7960 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
7962 xa_for_each(&devlink->ports, port_index, port) {
7963 mutex_lock(&port->reporters_lock);
7964 list_for_each_entry(reporter, &port->reporter_list, list) {
7969 err = devlink_nl_health_reporter_fill(
7971 DEVLINK_CMD_HEALTH_REPORTER_GET,
7972 NETLINK_CB(cb->skb).portid,
7973 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7975 mutex_unlock(&port->reporters_lock);
7976 devl_unlock(devlink);
7977 devlink_put(devlink);
7982 mutex_unlock(&port->reporters_lock);
7984 devl_unlock(devlink);
7985 devlink_put(devlink);
7993 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7994 struct genl_info *info)
7996 struct devlink *devlink = info->user_ptr[0];
7997 struct devlink_health_reporter *reporter;
8000 reporter = devlink_health_reporter_get_from_info(devlink, info);
8004 if (!reporter->ops->recover &&
8005 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
8006 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
8010 if (!reporter->ops->dump &&
8011 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
8016 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
8017 reporter->graceful_period =
8018 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
8020 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
8021 reporter->auto_recover =
8022 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
8024 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
8025 reporter->auto_dump =
8026 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
8028 devlink_health_reporter_put(reporter);
8031 devlink_health_reporter_put(reporter);
8035 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
8036 struct genl_info *info)
8038 struct devlink *devlink = info->user_ptr[0];
8039 struct devlink_health_reporter *reporter;
8042 reporter = devlink_health_reporter_get_from_info(devlink, info);
8046 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
8048 devlink_health_reporter_put(reporter);
8052 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
8053 struct genl_info *info)
8055 struct devlink *devlink = info->user_ptr[0];
8056 struct devlink_health_reporter *reporter;
8057 struct devlink_fmsg *fmsg;
8060 reporter = devlink_health_reporter_get_from_info(devlink, info);
8064 if (!reporter->ops->diagnose) {
8065 devlink_health_reporter_put(reporter);
8069 fmsg = devlink_fmsg_alloc();
8071 devlink_health_reporter_put(reporter);
8075 err = devlink_fmsg_obj_nest_start(fmsg);
8079 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
8083 err = devlink_fmsg_obj_nest_end(fmsg);
8087 err = devlink_fmsg_snd(fmsg, info,
8088 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
8091 devlink_fmsg_free(fmsg);
8092 devlink_health_reporter_put(reporter);
8097 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
8098 struct netlink_callback *cb)
8100 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8101 struct devlink_health_reporter *reporter;
8104 reporter = devlink_health_reporter_get_from_cb(cb);
8108 if (!reporter->ops->dump) {
8112 mutex_lock(&reporter->dump_lock);
8114 err = devlink_health_do_dump(reporter, NULL, cb->extack);
8117 state->dump_ts = reporter->dump_ts;
8119 if (!reporter->dump_fmsg || state->dump_ts != reporter->dump_ts) {
8120 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
8125 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
8126 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
8128 mutex_unlock(&reporter->dump_lock);
8130 devlink_health_reporter_put(reporter);
8135 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
8136 struct genl_info *info)
8138 struct devlink *devlink = info->user_ptr[0];
8139 struct devlink_health_reporter *reporter;
8141 reporter = devlink_health_reporter_get_from_info(devlink, info);
8145 if (!reporter->ops->dump) {
8146 devlink_health_reporter_put(reporter);
8150 mutex_lock(&reporter->dump_lock);
8151 devlink_health_dump_clear(reporter);
8152 mutex_unlock(&reporter->dump_lock);
8153 devlink_health_reporter_put(reporter);
8157 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
8158 struct genl_info *info)
8160 struct devlink *devlink = info->user_ptr[0];
8161 struct devlink_health_reporter *reporter;
8164 reporter = devlink_health_reporter_get_from_info(devlink, info);
8168 if (!reporter->ops->test) {
8169 devlink_health_reporter_put(reporter);
8173 err = reporter->ops->test(reporter, info->extack);
8175 devlink_health_reporter_put(reporter);
8179 struct devlink_stats {
8180 u64_stats_t rx_bytes;
8181 u64_stats_t rx_packets;
8182 struct u64_stats_sync syncp;
8186 * struct devlink_trap_policer_item - Packet trap policer attributes.
8187 * @policer: Immutable packet trap policer attributes.
8188 * @rate: Rate in packets / sec.
8189 * @burst: Burst size in packets.
8190 * @list: trap_policer_list member.
8192 * Describes packet trap policer attributes. Created by devlink during trap
8193 * policer registration.
8195 struct devlink_trap_policer_item {
8196 const struct devlink_trap_policer *policer;
8199 struct list_head list;
8203 * struct devlink_trap_group_item - Packet trap group attributes.
8204 * @group: Immutable packet trap group attributes.
8205 * @policer_item: Associated policer item. Can be NULL.
8206 * @list: trap_group_list member.
8207 * @stats: Trap group statistics.
8209 * Describes packet trap group attributes. Created by devlink during trap
8210 * group registration.
8212 struct devlink_trap_group_item {
8213 const struct devlink_trap_group *group;
8214 struct devlink_trap_policer_item *policer_item;
8215 struct list_head list;
8216 struct devlink_stats __percpu *stats;
8220 * struct devlink_trap_item - Packet trap attributes.
8221 * @trap: Immutable packet trap attributes.
8222 * @group_item: Associated group item.
8223 * @list: trap_list member.
8224 * @action: Trap action.
8225 * @stats: Trap statistics.
8226 * @priv: Driver private information.
8228 * Describes both mutable and immutable packet trap attributes. Created by
8229 * devlink during trap registration and used for all trap related operations.
8231 struct devlink_trap_item {
8232 const struct devlink_trap *trap;
8233 struct devlink_trap_group_item *group_item;
8234 struct list_head list;
8235 enum devlink_trap_action action;
8236 struct devlink_stats __percpu *stats;
8240 static struct devlink_trap_policer_item *
8241 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
8243 struct devlink_trap_policer_item *policer_item;
8245 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
8246 if (policer_item->policer->id == id)
8247 return policer_item;
8253 static struct devlink_trap_item *
8254 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
8256 struct devlink_trap_item *trap_item;
8258 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8259 if (!strcmp(trap_item->trap->name, name))
8266 static struct devlink_trap_item *
8267 devlink_trap_item_get_from_info(struct devlink *devlink,
8268 struct genl_info *info)
8270 struct nlattr *attr;
8272 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
8274 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
8276 return devlink_trap_item_lookup(devlink, nla_data(attr));
8280 devlink_trap_action_get_from_info(struct genl_info *info,
8281 enum devlink_trap_action *p_trap_action)
8285 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
8287 case DEVLINK_TRAP_ACTION_DROP:
8288 case DEVLINK_TRAP_ACTION_TRAP:
8289 case DEVLINK_TRAP_ACTION_MIRROR:
8290 *p_trap_action = val;
8299 static int devlink_trap_metadata_put(struct sk_buff *msg,
8300 const struct devlink_trap *trap)
8302 struct nlattr *attr;
8304 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
8308 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
8309 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
8310 goto nla_put_failure;
8311 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
8312 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
8313 goto nla_put_failure;
8315 nla_nest_end(msg, attr);
8320 nla_nest_cancel(msg, attr);
8324 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
8325 struct devlink_stats *stats)
8329 memset(stats, 0, sizeof(*stats));
8330 for_each_possible_cpu(i) {
8331 struct devlink_stats *cpu_stats;
8332 u64 rx_packets, rx_bytes;
8335 cpu_stats = per_cpu_ptr(trap_stats, i);
8337 start = u64_stats_fetch_begin(&cpu_stats->syncp);
8338 rx_packets = u64_stats_read(&cpu_stats->rx_packets);
8339 rx_bytes = u64_stats_read(&cpu_stats->rx_bytes);
8340 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
8342 u64_stats_add(&stats->rx_packets, rx_packets);
8343 u64_stats_add(&stats->rx_bytes, rx_bytes);
8348 devlink_trap_group_stats_put(struct sk_buff *msg,
8349 struct devlink_stats __percpu *trap_stats)
8351 struct devlink_stats stats;
8352 struct nlattr *attr;
8354 devlink_trap_stats_read(trap_stats, &stats);
8356 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8360 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8361 u64_stats_read(&stats.rx_packets),
8363 goto nla_put_failure;
8365 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8366 u64_stats_read(&stats.rx_bytes),
8368 goto nla_put_failure;
8370 nla_nest_end(msg, attr);
8375 nla_nest_cancel(msg, attr);
8379 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
8380 const struct devlink_trap_item *trap_item)
8382 struct devlink_stats stats;
8383 struct nlattr *attr;
8387 if (devlink->ops->trap_drop_counter_get) {
8388 err = devlink->ops->trap_drop_counter_get(devlink,
8395 devlink_trap_stats_read(trap_item->stats, &stats);
8397 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8401 if (devlink->ops->trap_drop_counter_get &&
8402 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8404 goto nla_put_failure;
8406 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
8407 u64_stats_read(&stats.rx_packets),
8409 goto nla_put_failure;
8411 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
8412 u64_stats_read(&stats.rx_bytes),
8414 goto nla_put_failure;
8416 nla_nest_end(msg, attr);
8421 nla_nest_cancel(msg, attr);
8425 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
8426 const struct devlink_trap_item *trap_item,
8427 enum devlink_command cmd, u32 portid, u32 seq,
8430 struct devlink_trap_group_item *group_item = trap_item->group_item;
8434 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8438 if (devlink_nl_put_handle(msg, devlink))
8439 goto nla_put_failure;
8441 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8442 group_item->group->name))
8443 goto nla_put_failure;
8445 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
8446 goto nla_put_failure;
8448 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
8449 goto nla_put_failure;
8451 if (trap_item->trap->generic &&
8452 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8453 goto nla_put_failure;
8455 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
8456 goto nla_put_failure;
8458 err = devlink_trap_metadata_put(msg, trap_item->trap);
8460 goto nla_put_failure;
8462 err = devlink_trap_stats_put(msg, devlink, trap_item);
8464 goto nla_put_failure;
8466 genlmsg_end(msg, hdr);
8471 genlmsg_cancel(msg, hdr);
8475 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
8476 struct genl_info *info)
8478 struct netlink_ext_ack *extack = info->extack;
8479 struct devlink *devlink = info->user_ptr[0];
8480 struct devlink_trap_item *trap_item;
8481 struct sk_buff *msg;
8484 if (list_empty(&devlink->trap_list))
8487 trap_item = devlink_trap_item_get_from_info(devlink, info);
8489 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8493 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8497 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8498 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
8503 return genlmsg_reply(msg, info);
8510 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
8511 struct netlink_callback *cb)
8513 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8514 struct devlink_trap_item *trap_item;
8515 struct devlink *devlink;
8516 int start = state->idx;
8517 unsigned long index;
8521 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
8523 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8528 err = devlink_nl_trap_fill(msg, devlink, trap_item,
8529 DEVLINK_CMD_TRAP_NEW,
8530 NETLINK_CB(cb->skb).portid,
8534 devl_unlock(devlink);
8535 devlink_put(devlink);
8540 devl_unlock(devlink);
8541 devlink_put(devlink);
8548 static int __devlink_trap_action_set(struct devlink *devlink,
8549 struct devlink_trap_item *trap_item,
8550 enum devlink_trap_action trap_action,
8551 struct netlink_ext_ack *extack)
8555 if (trap_item->action != trap_action &&
8556 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
8557 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
8561 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
8562 trap_action, extack);
8566 trap_item->action = trap_action;
8571 static int devlink_trap_action_set(struct devlink *devlink,
8572 struct devlink_trap_item *trap_item,
8573 struct genl_info *info)
8575 enum devlink_trap_action trap_action;
8578 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8581 err = devlink_trap_action_get_from_info(info, &trap_action);
8583 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8587 return __devlink_trap_action_set(devlink, trap_item, trap_action,
8591 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
8592 struct genl_info *info)
8594 struct netlink_ext_ack *extack = info->extack;
8595 struct devlink *devlink = info->user_ptr[0];
8596 struct devlink_trap_item *trap_item;
8598 if (list_empty(&devlink->trap_list))
8601 trap_item = devlink_trap_item_get_from_info(devlink, info);
8603 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
8607 return devlink_trap_action_set(devlink, trap_item, info);
8610 static struct devlink_trap_group_item *
8611 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
8613 struct devlink_trap_group_item *group_item;
8615 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8616 if (!strcmp(group_item->group->name, name))
8623 static struct devlink_trap_group_item *
8624 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
8626 struct devlink_trap_group_item *group_item;
8628 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
8629 if (group_item->group->id == id)
8636 static struct devlink_trap_group_item *
8637 devlink_trap_group_item_get_from_info(struct devlink *devlink,
8638 struct genl_info *info)
8642 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
8644 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
8646 return devlink_trap_group_item_lookup(devlink, name);
8650 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
8651 const struct devlink_trap_group_item *group_item,
8652 enum devlink_command cmd, u32 portid, u32 seq,
8658 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8662 if (devlink_nl_put_handle(msg, devlink))
8663 goto nla_put_failure;
8665 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
8666 group_item->group->name))
8667 goto nla_put_failure;
8669 if (group_item->group->generic &&
8670 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
8671 goto nla_put_failure;
8673 if (group_item->policer_item &&
8674 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8675 group_item->policer_item->policer->id))
8676 goto nla_put_failure;
8678 err = devlink_trap_group_stats_put(msg, group_item->stats);
8680 goto nla_put_failure;
8682 genlmsg_end(msg, hdr);
8687 genlmsg_cancel(msg, hdr);
8691 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
8692 struct genl_info *info)
8694 struct netlink_ext_ack *extack = info->extack;
8695 struct devlink *devlink = info->user_ptr[0];
8696 struct devlink_trap_group_item *group_item;
8697 struct sk_buff *msg;
8700 if (list_empty(&devlink->trap_group_list))
8703 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8705 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8709 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8713 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
8714 DEVLINK_CMD_TRAP_GROUP_NEW,
8715 info->snd_portid, info->snd_seq, 0);
8717 goto err_trap_group_fill;
8719 return genlmsg_reply(msg, info);
8721 err_trap_group_fill:
8726 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8727 struct netlink_callback *cb)
8729 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
8730 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8731 struct devlink_trap_group_item *group_item;
8732 u32 portid = NETLINK_CB(cb->skb).portid;
8733 struct devlink *devlink;
8734 int start = state->idx;
8735 unsigned long index;
8739 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
8741 list_for_each_entry(group_item, &devlink->trap_group_list,
8747 err = devlink_nl_trap_group_fill(msg, devlink,
8753 devl_unlock(devlink);
8754 devlink_put(devlink);
8759 devl_unlock(devlink);
8760 devlink_put(devlink);
8768 __devlink_trap_group_action_set(struct devlink *devlink,
8769 struct devlink_trap_group_item *group_item,
8770 enum devlink_trap_action trap_action,
8771 struct netlink_ext_ack *extack)
8773 const char *group_name = group_item->group->name;
8774 struct devlink_trap_item *trap_item;
8777 if (devlink->ops->trap_group_action_set) {
8778 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8779 trap_action, extack);
8783 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8784 if (strcmp(trap_item->group_item->group->name, group_name))
8786 if (trap_item->action != trap_action &&
8787 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8789 trap_item->action = trap_action;
8795 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8796 if (strcmp(trap_item->group_item->group->name, group_name))
8798 err = __devlink_trap_action_set(devlink, trap_item,
8799 trap_action, extack);
8808 devlink_trap_group_action_set(struct devlink *devlink,
8809 struct devlink_trap_group_item *group_item,
8810 struct genl_info *info, bool *p_modified)
8812 enum devlink_trap_action trap_action;
8815 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8818 err = devlink_trap_action_get_from_info(info, &trap_action);
8820 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8824 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8834 static int devlink_trap_group_set(struct devlink *devlink,
8835 struct devlink_trap_group_item *group_item,
8836 struct genl_info *info)
8838 struct devlink_trap_policer_item *policer_item;
8839 struct netlink_ext_ack *extack = info->extack;
8840 const struct devlink_trap_policer *policer;
8841 struct nlattr **attrs = info->attrs;
8844 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8847 if (!devlink->ops->trap_group_set)
8850 policer_item = group_item->policer_item;
8851 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8854 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8855 policer_item = devlink_trap_policer_item_lookup(devlink,
8857 if (policer_id && !policer_item) {
8858 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8862 policer = policer_item ? policer_item->policer : NULL;
8864 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8869 group_item->policer_item = policer_item;
8874 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8875 struct genl_info *info)
8877 struct netlink_ext_ack *extack = info->extack;
8878 struct devlink *devlink = info->user_ptr[0];
8879 struct devlink_trap_group_item *group_item;
8880 bool modified = false;
8883 if (list_empty(&devlink->trap_group_list))
8886 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8888 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8892 err = devlink_trap_group_action_set(devlink, group_item, info,
8897 err = devlink_trap_group_set(devlink, group_item, info);
8899 goto err_trap_group_set;
8905 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8909 static struct devlink_trap_policer_item *
8910 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8911 struct genl_info *info)
8915 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8917 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8919 return devlink_trap_policer_item_lookup(devlink, id);
8923 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8924 const struct devlink_trap_policer *policer)
8926 struct nlattr *attr;
8930 if (!devlink->ops->trap_policer_counter_get)
8933 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8937 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8941 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8943 goto nla_put_failure;
8945 nla_nest_end(msg, attr);
8950 nla_nest_cancel(msg, attr);
8955 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8956 const struct devlink_trap_policer_item *policer_item,
8957 enum devlink_command cmd, u32 portid, u32 seq,
8963 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8967 if (devlink_nl_put_handle(msg, devlink))
8968 goto nla_put_failure;
8970 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8971 policer_item->policer->id))
8972 goto nla_put_failure;
8974 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8975 policer_item->rate, DEVLINK_ATTR_PAD))
8976 goto nla_put_failure;
8978 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8979 policer_item->burst, DEVLINK_ATTR_PAD))
8980 goto nla_put_failure;
8982 err = devlink_trap_policer_stats_put(msg, devlink,
8983 policer_item->policer);
8985 goto nla_put_failure;
8987 genlmsg_end(msg, hdr);
8992 genlmsg_cancel(msg, hdr);
8996 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8997 struct genl_info *info)
8999 struct devlink_trap_policer_item *policer_item;
9000 struct netlink_ext_ack *extack = info->extack;
9001 struct devlink *devlink = info->user_ptr[0];
9002 struct sk_buff *msg;
9005 if (list_empty(&devlink->trap_policer_list))
9008 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
9009 if (!policer_item) {
9010 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
9014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9018 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
9019 DEVLINK_CMD_TRAP_POLICER_NEW,
9020 info->snd_portid, info->snd_seq, 0);
9022 goto err_trap_policer_fill;
9024 return genlmsg_reply(msg, info);
9026 err_trap_policer_fill:
9031 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
9032 struct netlink_callback *cb)
9034 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
9035 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
9036 struct devlink_trap_policer_item *policer_item;
9037 u32 portid = NETLINK_CB(cb->skb).portid;
9038 struct devlink *devlink;
9039 int start = state->idx;
9040 unsigned long index;
9044 devlinks_xa_for_each_registered_get(sock_net(msg->sk), index, devlink) {
9046 list_for_each_entry(policer_item, &devlink->trap_policer_list,
9052 err = devlink_nl_trap_policer_fill(msg, devlink,
9058 devl_unlock(devlink);
9059 devlink_put(devlink);
9064 devl_unlock(devlink);
9065 devlink_put(devlink);
9073 devlink_trap_policer_set(struct devlink *devlink,
9074 struct devlink_trap_policer_item *policer_item,
9075 struct genl_info *info)
9077 struct netlink_ext_ack *extack = info->extack;
9078 struct nlattr **attrs = info->attrs;
9082 rate = policer_item->rate;
9083 burst = policer_item->burst;
9085 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
9086 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
9088 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
9089 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
9091 if (rate < policer_item->policer->min_rate) {
9092 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
9096 if (rate > policer_item->policer->max_rate) {
9097 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
9101 if (burst < policer_item->policer->min_burst) {
9102 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
9106 if (burst > policer_item->policer->max_burst) {
9107 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
9111 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
9112 rate, burst, info->extack);
9116 policer_item->rate = rate;
9117 policer_item->burst = burst;
9122 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
9123 struct genl_info *info)
9125 struct devlink_trap_policer_item *policer_item;
9126 struct netlink_ext_ack *extack = info->extack;
9127 struct devlink *devlink = info->user_ptr[0];
9129 if (list_empty(&devlink->trap_policer_list))
9132 if (!devlink->ops->trap_policer_set)
9135 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
9136 if (!policer_item) {
9137 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
9141 return devlink_trap_policer_set(devlink, policer_item, info);
9144 const struct genl_small_ops devlink_nl_ops[56] = {
9146 .cmd = DEVLINK_CMD_GET,
9147 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9148 .doit = devlink_nl_cmd_get_doit,
9149 .dumpit = devlink_nl_cmd_get_dumpit,
9150 /* can be retrieved by unprivileged users */
9153 .cmd = DEVLINK_CMD_PORT_GET,
9154 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9155 .doit = devlink_nl_cmd_port_get_doit,
9156 .dumpit = devlink_nl_cmd_port_get_dumpit,
9157 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9158 /* can be retrieved by unprivileged users */
9161 .cmd = DEVLINK_CMD_PORT_SET,
9162 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9163 .doit = devlink_nl_cmd_port_set_doit,
9164 .flags = GENL_ADMIN_PERM,
9165 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9168 .cmd = DEVLINK_CMD_RATE_GET,
9169 .doit = devlink_nl_cmd_rate_get_doit,
9170 .dumpit = devlink_nl_cmd_rate_get_dumpit,
9171 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9172 /* can be retrieved by unprivileged users */
9175 .cmd = DEVLINK_CMD_RATE_SET,
9176 .doit = devlink_nl_cmd_rate_set_doit,
9177 .flags = GENL_ADMIN_PERM,
9178 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
9181 .cmd = DEVLINK_CMD_RATE_NEW,
9182 .doit = devlink_nl_cmd_rate_new_doit,
9183 .flags = GENL_ADMIN_PERM,
9186 .cmd = DEVLINK_CMD_RATE_DEL,
9187 .doit = devlink_nl_cmd_rate_del_doit,
9188 .flags = GENL_ADMIN_PERM,
9189 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
9192 .cmd = DEVLINK_CMD_PORT_SPLIT,
9193 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9194 .doit = devlink_nl_cmd_port_split_doit,
9195 .flags = GENL_ADMIN_PERM,
9196 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9199 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
9200 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9201 .doit = devlink_nl_cmd_port_unsplit_doit,
9202 .flags = GENL_ADMIN_PERM,
9203 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9206 .cmd = DEVLINK_CMD_PORT_NEW,
9207 .doit = devlink_nl_cmd_port_new_doit,
9208 .flags = GENL_ADMIN_PERM,
9211 .cmd = DEVLINK_CMD_PORT_DEL,
9212 .doit = devlink_nl_cmd_port_del_doit,
9213 .flags = GENL_ADMIN_PERM,
9216 .cmd = DEVLINK_CMD_LINECARD_GET,
9217 .doit = devlink_nl_cmd_linecard_get_doit,
9218 .dumpit = devlink_nl_cmd_linecard_get_dumpit,
9219 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9220 /* can be retrieved by unprivileged users */
9223 .cmd = DEVLINK_CMD_LINECARD_SET,
9224 .doit = devlink_nl_cmd_linecard_set_doit,
9225 .flags = GENL_ADMIN_PERM,
9226 .internal_flags = DEVLINK_NL_FLAG_NEED_LINECARD,
9229 .cmd = DEVLINK_CMD_SB_GET,
9230 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9231 .doit = devlink_nl_cmd_sb_get_doit,
9232 .dumpit = devlink_nl_cmd_sb_get_dumpit,
9233 /* can be retrieved by unprivileged users */
9236 .cmd = DEVLINK_CMD_SB_POOL_GET,
9237 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9238 .doit = devlink_nl_cmd_sb_pool_get_doit,
9239 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
9240 /* can be retrieved by unprivileged users */
9243 .cmd = DEVLINK_CMD_SB_POOL_SET,
9244 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9245 .doit = devlink_nl_cmd_sb_pool_set_doit,
9246 .flags = GENL_ADMIN_PERM,
9249 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
9250 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9251 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
9252 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
9253 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9254 /* can be retrieved by unprivileged users */
9257 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
9258 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9259 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
9260 .flags = GENL_ADMIN_PERM,
9261 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9264 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
9265 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9266 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
9267 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
9268 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9269 /* can be retrieved by unprivileged users */
9272 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
9273 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9274 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
9275 .flags = GENL_ADMIN_PERM,
9276 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9279 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
9280 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9281 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
9282 .flags = GENL_ADMIN_PERM,
9285 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
9286 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9287 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
9288 .flags = GENL_ADMIN_PERM,
9291 .cmd = DEVLINK_CMD_ESWITCH_GET,
9292 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9293 .doit = devlink_nl_cmd_eswitch_get_doit,
9294 .flags = GENL_ADMIN_PERM,
9297 .cmd = DEVLINK_CMD_ESWITCH_SET,
9298 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9299 .doit = devlink_nl_cmd_eswitch_set_doit,
9300 .flags = GENL_ADMIN_PERM,
9303 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
9304 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9305 .doit = devlink_nl_cmd_dpipe_table_get,
9306 /* can be retrieved by unprivileged users */
9309 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
9310 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9311 .doit = devlink_nl_cmd_dpipe_entries_get,
9312 /* can be retrieved by unprivileged users */
9315 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
9316 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9317 .doit = devlink_nl_cmd_dpipe_headers_get,
9318 /* can be retrieved by unprivileged users */
9321 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
9322 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9323 .doit = devlink_nl_cmd_dpipe_table_counters_set,
9324 .flags = GENL_ADMIN_PERM,
9327 .cmd = DEVLINK_CMD_RESOURCE_SET,
9328 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9329 .doit = devlink_nl_cmd_resource_set,
9330 .flags = GENL_ADMIN_PERM,
9333 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
9334 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9335 .doit = devlink_nl_cmd_resource_dump,
9336 /* can be retrieved by unprivileged users */
9339 .cmd = DEVLINK_CMD_RELOAD,
9340 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9341 .doit = devlink_nl_cmd_reload,
9342 .flags = GENL_ADMIN_PERM,
9345 .cmd = DEVLINK_CMD_PARAM_GET,
9346 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9347 .doit = devlink_nl_cmd_param_get_doit,
9348 .dumpit = devlink_nl_cmd_param_get_dumpit,
9349 /* can be retrieved by unprivileged users */
9352 .cmd = DEVLINK_CMD_PARAM_SET,
9353 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9354 .doit = devlink_nl_cmd_param_set_doit,
9355 .flags = GENL_ADMIN_PERM,
9358 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
9359 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9360 .doit = devlink_nl_cmd_port_param_get_doit,
9361 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
9362 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9363 /* can be retrieved by unprivileged users */
9366 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
9367 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9368 .doit = devlink_nl_cmd_port_param_set_doit,
9369 .flags = GENL_ADMIN_PERM,
9370 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
9373 .cmd = DEVLINK_CMD_REGION_GET,
9374 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9375 .doit = devlink_nl_cmd_region_get_doit,
9376 .dumpit = devlink_nl_cmd_region_get_dumpit,
9377 .flags = GENL_ADMIN_PERM,
9380 .cmd = DEVLINK_CMD_REGION_NEW,
9381 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9382 .doit = devlink_nl_cmd_region_new,
9383 .flags = GENL_ADMIN_PERM,
9386 .cmd = DEVLINK_CMD_REGION_DEL,
9387 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9388 .doit = devlink_nl_cmd_region_del,
9389 .flags = GENL_ADMIN_PERM,
9392 .cmd = DEVLINK_CMD_REGION_READ,
9393 .validate = GENL_DONT_VALIDATE_STRICT |
9394 GENL_DONT_VALIDATE_DUMP_STRICT,
9395 .dumpit = devlink_nl_cmd_region_read_dumpit,
9396 .flags = GENL_ADMIN_PERM,
9399 .cmd = DEVLINK_CMD_INFO_GET,
9400 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9401 .doit = devlink_nl_cmd_info_get_doit,
9402 .dumpit = devlink_nl_cmd_info_get_dumpit,
9403 /* can be retrieved by unprivileged users */
9406 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
9407 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9408 .doit = devlink_nl_cmd_health_reporter_get_doit,
9409 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
9410 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9411 /* can be retrieved by unprivileged users */
9414 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
9415 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9416 .doit = devlink_nl_cmd_health_reporter_set_doit,
9417 .flags = GENL_ADMIN_PERM,
9418 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9421 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
9422 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9423 .doit = devlink_nl_cmd_health_reporter_recover_doit,
9424 .flags = GENL_ADMIN_PERM,
9425 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9428 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
9429 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9430 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
9431 .flags = GENL_ADMIN_PERM,
9432 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9435 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
9436 .validate = GENL_DONT_VALIDATE_STRICT |
9437 GENL_DONT_VALIDATE_DUMP_STRICT,
9438 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
9439 .flags = GENL_ADMIN_PERM,
9442 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
9443 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9444 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
9445 .flags = GENL_ADMIN_PERM,
9446 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9449 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
9450 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9451 .doit = devlink_nl_cmd_health_reporter_test_doit,
9452 .flags = GENL_ADMIN_PERM,
9453 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT,
9456 .cmd = DEVLINK_CMD_FLASH_UPDATE,
9457 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
9458 .doit = devlink_nl_cmd_flash_update,
9459 .flags = GENL_ADMIN_PERM,
9462 .cmd = DEVLINK_CMD_TRAP_GET,
9463 .doit = devlink_nl_cmd_trap_get_doit,
9464 .dumpit = devlink_nl_cmd_trap_get_dumpit,
9465 /* can be retrieved by unprivileged users */
9468 .cmd = DEVLINK_CMD_TRAP_SET,
9469 .doit = devlink_nl_cmd_trap_set_doit,
9470 .flags = GENL_ADMIN_PERM,
9473 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
9474 .doit = devlink_nl_cmd_trap_group_get_doit,
9475 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
9476 /* can be retrieved by unprivileged users */
9479 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
9480 .doit = devlink_nl_cmd_trap_group_set_doit,
9481 .flags = GENL_ADMIN_PERM,
9484 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
9485 .doit = devlink_nl_cmd_trap_policer_get_doit,
9486 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
9487 /* can be retrieved by unprivileged users */
9490 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
9491 .doit = devlink_nl_cmd_trap_policer_set_doit,
9492 .flags = GENL_ADMIN_PERM,
9495 .cmd = DEVLINK_CMD_SELFTESTS_GET,
9496 .doit = devlink_nl_cmd_selftests_get_doit,
9497 .dumpit = devlink_nl_cmd_selftests_get_dumpit
9498 /* can be retrieved by unprivileged users */
9501 .cmd = DEVLINK_CMD_SELFTESTS_RUN,
9502 .doit = devlink_nl_cmd_selftests_run,
9503 .flags = GENL_ADMIN_PERM,
9505 /* -- No new ops here! Use split ops going forward! -- */
9508 bool devlink_reload_actions_valid(const struct devlink_ops *ops)
9510 const struct devlink_reload_combination *comb;
9513 if (!devlink_reload_supported(ops)) {
9514 if (WARN_ON(ops->reload_actions))
9519 if (WARN_ON(!ops->reload_actions ||
9520 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
9521 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
9524 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
9525 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
9528 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
9529 comb = &devlink_reload_invalid_combinations[i];
9530 if (ops->reload_actions == BIT(comb->action) &&
9531 ops->reload_limits == BIT(comb->limit))
9538 devlink_trap_policer_notify(struct devlink *devlink,
9539 const struct devlink_trap_policer_item *policer_item,
9540 enum devlink_command cmd);
9542 devlink_trap_group_notify(struct devlink *devlink,
9543 const struct devlink_trap_group_item *group_item,
9544 enum devlink_command cmd);
9545 static void devlink_trap_notify(struct devlink *devlink,
9546 const struct devlink_trap_item *trap_item,
9547 enum devlink_command cmd);
9549 void devlink_notify_register(struct devlink *devlink)
9551 struct devlink_trap_policer_item *policer_item;
9552 struct devlink_trap_group_item *group_item;
9553 struct devlink_param_item *param_item;
9554 struct devlink_trap_item *trap_item;
9555 struct devlink_port *devlink_port;
9556 struct devlink_linecard *linecard;
9557 struct devlink_rate *rate_node;
9558 struct devlink_region *region;
9559 unsigned long port_index;
9561 devlink_notify(devlink, DEVLINK_CMD_NEW);
9562 list_for_each_entry(linecard, &devlink->linecard_list, list)
9563 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
9565 xa_for_each(&devlink->ports, port_index, devlink_port)
9566 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9568 list_for_each_entry(policer_item, &devlink->trap_policer_list, list)
9569 devlink_trap_policer_notify(devlink, policer_item,
9570 DEVLINK_CMD_TRAP_POLICER_NEW);
9572 list_for_each_entry(group_item, &devlink->trap_group_list, list)
9573 devlink_trap_group_notify(devlink, group_item,
9574 DEVLINK_CMD_TRAP_GROUP_NEW);
9576 list_for_each_entry(trap_item, &devlink->trap_list, list)
9577 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9579 list_for_each_entry(rate_node, &devlink->rate_list, list)
9580 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
9582 list_for_each_entry(region, &devlink->region_list, list)
9583 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9585 list_for_each_entry(param_item, &devlink->param_list, list)
9586 devlink_param_notify(devlink, 0, param_item,
9587 DEVLINK_CMD_PARAM_NEW);
9590 void devlink_notify_unregister(struct devlink *devlink)
9592 struct devlink_trap_policer_item *policer_item;
9593 struct devlink_trap_group_item *group_item;
9594 struct devlink_param_item *param_item;
9595 struct devlink_trap_item *trap_item;
9596 struct devlink_port *devlink_port;
9597 struct devlink_rate *rate_node;
9598 struct devlink_region *region;
9599 unsigned long port_index;
9601 list_for_each_entry_reverse(param_item, &devlink->param_list, list)
9602 devlink_param_notify(devlink, 0, param_item,
9603 DEVLINK_CMD_PARAM_DEL);
9605 list_for_each_entry_reverse(region, &devlink->region_list, list)
9606 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9608 list_for_each_entry_reverse(rate_node, &devlink->rate_list, list)
9609 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
9611 list_for_each_entry_reverse(trap_item, &devlink->trap_list, list)
9612 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9614 list_for_each_entry_reverse(group_item, &devlink->trap_group_list, list)
9615 devlink_trap_group_notify(devlink, group_item,
9616 DEVLINK_CMD_TRAP_GROUP_DEL);
9617 list_for_each_entry_reverse(policer_item, &devlink->trap_policer_list,
9619 devlink_trap_policer_notify(devlink, policer_item,
9620 DEVLINK_CMD_TRAP_POLICER_DEL);
9622 xa_for_each(&devlink->ports, port_index, devlink_port)
9623 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9624 devlink_notify(devlink, DEVLINK_CMD_DEL);
9627 static void devlink_port_type_warn(struct work_struct *work)
9629 WARN(true, "Type was not set for devlink port.");
9632 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9634 /* Ignore CPU and DSA flavours. */
9635 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9636 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9637 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9640 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9642 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9644 if (!devlink_port_type_should_warn(devlink_port))
9646 /* Schedule a work to WARN in case driver does not set port
9647 * type within timeout.
9649 schedule_delayed_work(&devlink_port->type_warn_dw,
9650 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9653 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9655 if (!devlink_port_type_should_warn(devlink_port))
9657 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9661 * devlink_port_init() - Init devlink port
9664 * @devlink_port: devlink port
9666 * Initialize essencial stuff that is needed for functions
9667 * that may be called before devlink port registration.
9668 * Call to this function is optional and not needed
9669 * in case the driver does not use such functions.
9671 void devlink_port_init(struct devlink *devlink,
9672 struct devlink_port *devlink_port)
9674 if (devlink_port->initialized)
9676 devlink_port->devlink = devlink;
9677 INIT_LIST_HEAD(&devlink_port->region_list);
9678 devlink_port->initialized = true;
9680 EXPORT_SYMBOL_GPL(devlink_port_init);
9683 * devlink_port_fini() - Deinitialize devlink port
9685 * @devlink_port: devlink port
9687 * Deinitialize essencial stuff that is in use for functions
9688 * that may be called after devlink port unregistration.
9689 * Call to this function is optional and not needed
9690 * in case the driver does not use such functions.
9692 void devlink_port_fini(struct devlink_port *devlink_port)
9694 WARN_ON(!list_empty(&devlink_port->region_list));
9696 EXPORT_SYMBOL_GPL(devlink_port_fini);
9699 * devl_port_register() - Register devlink port
9702 * @devlink_port: devlink port
9703 * @port_index: driver-specific numerical identifier of the port
9705 * Register devlink port with provided port index. User can use
9706 * any indexing, even hw-related one. devlink_port structure
9707 * is convenient to be embedded inside user driver private structure.
9708 * Note that the caller should take care of zeroing the devlink_port
9711 int devl_port_register(struct devlink *devlink,
9712 struct devlink_port *devlink_port,
9713 unsigned int port_index)
9717 devl_assert_locked(devlink);
9719 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
9721 devlink_port_init(devlink, devlink_port);
9722 devlink_port->registered = true;
9723 devlink_port->index = port_index;
9724 spin_lock_init(&devlink_port->type_lock);
9725 INIT_LIST_HEAD(&devlink_port->reporter_list);
9726 mutex_init(&devlink_port->reporters_lock);
9727 err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL);
9729 mutex_destroy(&devlink_port->reporters_lock);
9733 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9734 devlink_port_type_warn_schedule(devlink_port);
9735 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9738 EXPORT_SYMBOL_GPL(devl_port_register);
9741 * devlink_port_register - Register devlink port
9744 * @devlink_port: devlink port
9745 * @port_index: driver-specific numerical identifier of the port
9747 * Register devlink port with provided port index. User can use
9748 * any indexing, even hw-related one. devlink_port structure
9749 * is convenient to be embedded inside user driver private structure.
9750 * Note that the caller should take care of zeroing the devlink_port
9753 * Context: Takes and release devlink->lock <mutex>.
9755 int devlink_port_register(struct devlink *devlink,
9756 struct devlink_port *devlink_port,
9757 unsigned int port_index)
9762 err = devl_port_register(devlink, devlink_port, port_index);
9763 devl_unlock(devlink);
9766 EXPORT_SYMBOL_GPL(devlink_port_register);
9769 * devl_port_unregister() - Unregister devlink port
9771 * @devlink_port: devlink port
9773 void devl_port_unregister(struct devlink_port *devlink_port)
9775 lockdep_assert_held(&devlink_port->devlink->lock);
9776 WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
9778 devlink_port_type_warn_cancel(devlink_port);
9779 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9780 xa_erase(&devlink_port->devlink->ports, devlink_port->index);
9781 WARN_ON(!list_empty(&devlink_port->reporter_list));
9782 mutex_destroy(&devlink_port->reporters_lock);
9783 devlink_port->registered = false;
9785 EXPORT_SYMBOL_GPL(devl_port_unregister);
9788 * devlink_port_unregister - Unregister devlink port
9790 * @devlink_port: devlink port
9792 * Context: Takes and release devlink->lock <mutex>.
9794 void devlink_port_unregister(struct devlink_port *devlink_port)
9796 struct devlink *devlink = devlink_port->devlink;
9799 devl_port_unregister(devlink_port);
9800 devl_unlock(devlink);
9802 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9804 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9805 struct net_device *netdev)
9807 const struct net_device_ops *ops = netdev->netdev_ops;
9809 /* If driver registers devlink port, it should set devlink port
9810 * attributes accordingly so the compat functions are called
9811 * and the original ops are not used.
9813 if (ops->ndo_get_phys_port_name) {
9814 /* Some drivers use the same set of ndos for netdevs
9815 * that have devlink_port registered and also for
9816 * those who don't. Make sure that ndo_get_phys_port_name
9817 * returns -EOPNOTSUPP here in case it is defined.
9820 char name[IFNAMSIZ];
9823 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9824 WARN_ON(err != -EOPNOTSUPP);
9826 if (ops->ndo_get_port_parent_id) {
9827 /* Some drivers use the same set of ndos for netdevs
9828 * that have devlink_port registered and also for
9829 * those who don't. Make sure that ndo_get_port_parent_id
9830 * returns -EOPNOTSUPP here in case it is defined.
9833 struct netdev_phys_item_id ppid;
9836 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9837 WARN_ON(err != -EOPNOTSUPP);
9841 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9842 enum devlink_port_type type,
9845 struct net_device *netdev = type_dev;
9847 ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
9849 if (type == DEVLINK_PORT_TYPE_NOTSET) {
9850 devlink_port_type_warn_schedule(devlink_port);
9852 devlink_port_type_warn_cancel(devlink_port);
9853 if (type == DEVLINK_PORT_TYPE_ETH && netdev)
9854 devlink_port_type_netdev_checks(devlink_port, netdev);
9857 spin_lock_bh(&devlink_port->type_lock);
9858 devlink_port->type = type;
9860 case DEVLINK_PORT_TYPE_ETH:
9861 devlink_port->type_eth.netdev = netdev;
9864 devlink_port->type_eth.ifindex = netdev->ifindex;
9865 BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) !=
9866 sizeof(netdev->name));
9867 strcpy(devlink_port->type_eth.ifname, netdev->name);
9870 case DEVLINK_PORT_TYPE_IB:
9871 devlink_port->type_ib.ibdev = type_dev;
9876 spin_unlock_bh(&devlink_port->type_lock);
9877 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9881 * devlink_port_type_eth_set - Set port type to Ethernet
9883 * @devlink_port: devlink port
9885 * If driver is calling this, most likely it is doing something wrong.
9887 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
9889 dev_warn(devlink_port->devlink->dev,
9890 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9891 devlink_port->index);
9892 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL);
9894 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9897 * devlink_port_type_ib_set - Set port type to InfiniBand
9899 * @devlink_port: devlink port
9900 * @ibdev: related IB device
9902 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9903 struct ib_device *ibdev)
9905 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9907 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9910 * devlink_port_type_clear - Clear port type
9912 * @devlink_port: devlink port
9914 * If driver is calling this for clearing Ethernet type, most likely
9915 * it is doing something wrong.
9917 void devlink_port_type_clear(struct devlink_port *devlink_port)
9919 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9920 dev_warn(devlink_port->devlink->dev,
9921 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n",
9922 devlink_port->index);
9923 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9925 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9927 int devlink_port_netdevice_event(struct notifier_block *nb,
9928 unsigned long event, void *ptr)
9930 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
9931 struct devlink_port *devlink_port = netdev->devlink_port;
9932 struct devlink *devlink;
9934 devlink = container_of(nb, struct devlink, netdevice_nb);
9936 if (!devlink_port || devlink_port->devlink != devlink)
9940 case NETDEV_POST_INIT:
9941 /* Set the type but not netdev pointer. It is going to be set
9942 * later on by NETDEV_REGISTER event. Happens once during
9943 * netdevice register
9945 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
9948 case NETDEV_REGISTER:
9949 case NETDEV_CHANGENAME:
9950 /* Set the netdev on top of previously set type. Note this
9951 * event happens also during net namespace change so here
9952 * we take into account netdev pointer appearing in this
9955 __devlink_port_type_set(devlink_port, devlink_port->type,
9958 case NETDEV_UNREGISTER:
9959 /* Clear netdev pointer, but not the type. This event happens
9960 * also during net namespace change so we need to clear
9961 * pointer to netdev that is going to another net namespace.
9963 __devlink_port_type_set(devlink_port, devlink_port->type,
9966 case NETDEV_PRE_UNINIT:
9967 /* Clear the type and the netdev pointer. Happens one during
9968 * netdevice unregister.
9970 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
9978 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9979 enum devlink_port_flavour flavour)
9981 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9983 devlink_port->attrs_set = true;
9984 attrs->flavour = flavour;
9985 if (attrs->switch_id.id_len) {
9986 devlink_port->switch_port = true;
9987 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9988 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9990 devlink_port->switch_port = false;
9996 * devlink_port_attrs_set - Set port attributes
9998 * @devlink_port: devlink port
9999 * @attrs: devlink port attrs
10001 void devlink_port_attrs_set(struct devlink_port *devlink_port,
10002 struct devlink_port_attrs *attrs)
10006 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10008 devlink_port->attrs = *attrs;
10009 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
10012 WARN_ON(attrs->splittable && attrs->split);
10014 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
10017 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
10019 * @devlink_port: devlink port
10020 * @controller: associated controller number for the devlink port instance
10021 * @pf: associated PF for the devlink port instance
10022 * @external: indicates if the port is for an external controller
10024 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
10025 u16 pf, bool external)
10027 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10030 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10032 ret = __devlink_port_attrs_set(devlink_port,
10033 DEVLINK_PORT_FLAVOUR_PCI_PF);
10036 attrs->pci_pf.controller = controller;
10037 attrs->pci_pf.pf = pf;
10038 attrs->pci_pf.external = external;
10040 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
10043 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
10045 * @devlink_port: devlink port
10046 * @controller: associated controller number for the devlink port instance
10047 * @pf: associated PF for the devlink port instance
10048 * @vf: associated VF of a PF for the devlink port instance
10049 * @external: indicates if the port is for an external controller
10051 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
10052 u16 pf, u16 vf, bool external)
10054 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10057 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10059 ret = __devlink_port_attrs_set(devlink_port,
10060 DEVLINK_PORT_FLAVOUR_PCI_VF);
10063 attrs->pci_vf.controller = controller;
10064 attrs->pci_vf.pf = pf;
10065 attrs->pci_vf.vf = vf;
10066 attrs->pci_vf.external = external;
10068 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
10071 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
10073 * @devlink_port: devlink port
10074 * @controller: associated controller number for the devlink port instance
10075 * @pf: associated PF for the devlink port instance
10076 * @sf: associated SF of a PF for the devlink port instance
10077 * @external: indicates if the port is for an external controller
10079 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
10080 u16 pf, u32 sf, bool external)
10082 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10085 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10087 ret = __devlink_port_attrs_set(devlink_port,
10088 DEVLINK_PORT_FLAVOUR_PCI_SF);
10091 attrs->pci_sf.controller = controller;
10092 attrs->pci_sf.pf = pf;
10093 attrs->pci_sf.sf = sf;
10094 attrs->pci_sf.external = external;
10096 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
10099 * devl_rate_node_create - create devlink rate node
10100 * @devlink: devlink instance
10101 * @priv: driver private data
10102 * @node_name: name of the resulting node
10103 * @parent: parent devlink_rate struct
10105 * Create devlink rate object of type node
10107 struct devlink_rate *
10108 devl_rate_node_create(struct devlink *devlink, void *priv, char *node_name,
10109 struct devlink_rate *parent)
10111 struct devlink_rate *rate_node;
10113 rate_node = devlink_rate_node_get_by_name(devlink, node_name);
10114 if (!IS_ERR(rate_node))
10115 return ERR_PTR(-EEXIST);
10117 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
10119 return ERR_PTR(-ENOMEM);
10122 rate_node->parent = parent;
10123 refcount_inc(&rate_node->parent->refcnt);
10126 rate_node->type = DEVLINK_RATE_TYPE_NODE;
10127 rate_node->devlink = devlink;
10128 rate_node->priv = priv;
10130 rate_node->name = kstrdup(node_name, GFP_KERNEL);
10131 if (!rate_node->name) {
10133 return ERR_PTR(-ENOMEM);
10136 refcount_set(&rate_node->refcnt, 1);
10137 list_add(&rate_node->list, &devlink->rate_list);
10138 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
10141 EXPORT_SYMBOL_GPL(devl_rate_node_create);
10144 * devl_rate_leaf_create - create devlink rate leaf
10145 * @devlink_port: devlink port object to create rate object on
10146 * @priv: driver private data
10147 * @parent: parent devlink_rate struct
10149 * Create devlink rate object of type leaf on provided @devlink_port.
10151 int devl_rate_leaf_create(struct devlink_port *devlink_port, void *priv,
10152 struct devlink_rate *parent)
10154 struct devlink *devlink = devlink_port->devlink;
10155 struct devlink_rate *devlink_rate;
10157 devl_assert_locked(devlink_port->devlink);
10159 if (WARN_ON(devlink_port->devlink_rate))
10162 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
10167 devlink_rate->parent = parent;
10168 refcount_inc(&devlink_rate->parent->refcnt);
10171 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
10172 devlink_rate->devlink = devlink;
10173 devlink_rate->devlink_port = devlink_port;
10174 devlink_rate->priv = priv;
10175 list_add_tail(&devlink_rate->list, &devlink->rate_list);
10176 devlink_port->devlink_rate = devlink_rate;
10177 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
10181 EXPORT_SYMBOL_GPL(devl_rate_leaf_create);
10184 * devl_rate_leaf_destroy - destroy devlink rate leaf
10186 * @devlink_port: devlink port linked to the rate object
10188 * Destroy the devlink rate object of type leaf on provided @devlink_port.
10190 void devl_rate_leaf_destroy(struct devlink_port *devlink_port)
10192 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
10194 devl_assert_locked(devlink_port->devlink);
10198 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
10199 if (devlink_rate->parent)
10200 refcount_dec(&devlink_rate->parent->refcnt);
10201 list_del(&devlink_rate->list);
10202 devlink_port->devlink_rate = NULL;
10203 kfree(devlink_rate);
10205 EXPORT_SYMBOL_GPL(devl_rate_leaf_destroy);
10208 * devl_rate_nodes_destroy - destroy all devlink rate nodes on device
10209 * @devlink: devlink instance
10211 * Unset parent for all rate objects and destroy all rate nodes
10212 * on specified device.
10214 void devl_rate_nodes_destroy(struct devlink *devlink)
10216 static struct devlink_rate *devlink_rate, *tmp;
10217 const struct devlink_ops *ops = devlink->ops;
10219 devl_assert_locked(devlink);
10221 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
10222 if (!devlink_rate->parent)
10225 refcount_dec(&devlink_rate->parent->refcnt);
10226 if (devlink_rate_is_leaf(devlink_rate))
10227 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
10229 else if (devlink_rate_is_node(devlink_rate))
10230 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
10233 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
10234 if (devlink_rate_is_node(devlink_rate)) {
10235 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
10236 list_del(&devlink_rate->list);
10237 kfree(devlink_rate->name);
10238 kfree(devlink_rate);
10242 EXPORT_SYMBOL_GPL(devl_rate_nodes_destroy);
10245 * devlink_port_linecard_set - Link port with a linecard
10247 * @devlink_port: devlink port
10248 * @linecard: devlink linecard
10250 void devlink_port_linecard_set(struct devlink_port *devlink_port,
10251 struct devlink_linecard *linecard)
10253 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
10255 devlink_port->linecard = linecard;
10257 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
10259 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
10260 char *name, size_t len)
10262 struct devlink_port_attrs *attrs = &devlink_port->attrs;
10265 if (!devlink_port->attrs_set)
10266 return -EOPNOTSUPP;
10268 switch (attrs->flavour) {
10269 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
10270 if (devlink_port->linecard)
10271 n = snprintf(name, len, "l%u",
10272 devlink_port->linecard->index);
10274 n += snprintf(name + n, len - n, "p%u",
10275 attrs->phys.port_number);
10276 if (n < len && attrs->split)
10277 n += snprintf(name + n, len - n, "s%u",
10278 attrs->phys.split_subport_number);
10280 case DEVLINK_PORT_FLAVOUR_CPU:
10281 case DEVLINK_PORT_FLAVOUR_DSA:
10282 case DEVLINK_PORT_FLAVOUR_UNUSED:
10283 /* As CPU and DSA ports do not have a netdevice associated
10284 * case should not ever happen.
10288 case DEVLINK_PORT_FLAVOUR_PCI_PF:
10289 if (attrs->pci_pf.external) {
10290 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
10296 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
10298 case DEVLINK_PORT_FLAVOUR_PCI_VF:
10299 if (attrs->pci_vf.external) {
10300 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
10306 n = snprintf(name, len, "pf%uvf%u",
10307 attrs->pci_vf.pf, attrs->pci_vf.vf);
10309 case DEVLINK_PORT_FLAVOUR_PCI_SF:
10310 if (attrs->pci_sf.external) {
10311 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
10317 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
10320 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
10321 return -EOPNOTSUPP;
10330 static int devlink_linecard_types_init(struct devlink_linecard *linecard)
10332 struct devlink_linecard_type *linecard_type;
10333 unsigned int count;
10336 count = linecard->ops->types_count(linecard, linecard->priv);
10337 linecard->types = kmalloc_array(count, sizeof(*linecard_type),
10339 if (!linecard->types)
10341 linecard->types_count = count;
10343 for (i = 0; i < count; i++) {
10344 linecard_type = &linecard->types[i];
10345 linecard->ops->types_get(linecard, linecard->priv, i,
10346 &linecard_type->type,
10347 &linecard_type->priv);
10352 static void devlink_linecard_types_fini(struct devlink_linecard *linecard)
10354 kfree(linecard->types);
10358 * devlink_linecard_create - Create devlink linecard
10360 * @devlink: devlink
10361 * @linecard_index: driver-specific numerical identifier of the linecard
10362 * @ops: linecards ops
10363 * @priv: user priv pointer
10365 * Create devlink linecard instance with provided linecard index.
10366 * Caller can use any indexing, even hw-related one.
10368 * Return: Line card structure or an ERR_PTR() encoded error code.
10370 struct devlink_linecard *
10371 devlink_linecard_create(struct devlink *devlink, unsigned int linecard_index,
10372 const struct devlink_linecard_ops *ops, void *priv)
10374 struct devlink_linecard *linecard;
10377 if (WARN_ON(!ops || !ops->provision || !ops->unprovision ||
10378 !ops->types_count || !ops->types_get))
10379 return ERR_PTR(-EINVAL);
10381 mutex_lock(&devlink->linecards_lock);
10382 if (devlink_linecard_index_exists(devlink, linecard_index)) {
10383 mutex_unlock(&devlink->linecards_lock);
10384 return ERR_PTR(-EEXIST);
10387 linecard = kzalloc(sizeof(*linecard), GFP_KERNEL);
10389 mutex_unlock(&devlink->linecards_lock);
10390 return ERR_PTR(-ENOMEM);
10393 linecard->devlink = devlink;
10394 linecard->index = linecard_index;
10395 linecard->ops = ops;
10396 linecard->priv = priv;
10397 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10398 mutex_init(&linecard->state_lock);
10400 err = devlink_linecard_types_init(linecard);
10402 mutex_destroy(&linecard->state_lock);
10404 mutex_unlock(&devlink->linecards_lock);
10405 return ERR_PTR(err);
10408 list_add_tail(&linecard->list, &devlink->linecard_list);
10409 refcount_set(&linecard->refcount, 1);
10410 mutex_unlock(&devlink->linecards_lock);
10411 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10414 EXPORT_SYMBOL_GPL(devlink_linecard_create);
10417 * devlink_linecard_destroy - Destroy devlink linecard
10419 * @linecard: devlink linecard
10421 void devlink_linecard_destroy(struct devlink_linecard *linecard)
10423 struct devlink *devlink = linecard->devlink;
10425 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_DEL);
10426 mutex_lock(&devlink->linecards_lock);
10427 list_del(&linecard->list);
10428 devlink_linecard_types_fini(linecard);
10429 mutex_unlock(&devlink->linecards_lock);
10430 devlink_linecard_put(linecard);
10432 EXPORT_SYMBOL_GPL(devlink_linecard_destroy);
10435 * devlink_linecard_provision_set - Set provisioning on linecard
10437 * @linecard: devlink linecard
10438 * @type: linecard type
10440 * This is either called directly from the provision() op call or
10441 * as a result of the provision() op call asynchronously.
10443 void devlink_linecard_provision_set(struct devlink_linecard *linecard,
10446 mutex_lock(&linecard->state_lock);
10447 WARN_ON(linecard->type && strcmp(linecard->type, type));
10448 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10449 linecard->type = type;
10450 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10451 mutex_unlock(&linecard->state_lock);
10453 EXPORT_SYMBOL_GPL(devlink_linecard_provision_set);
10456 * devlink_linecard_provision_clear - Clear provisioning on linecard
10458 * @linecard: devlink linecard
10460 * This is either called directly from the unprovision() op call or
10461 * as a result of the unprovision() op call asynchronously.
10463 void devlink_linecard_provision_clear(struct devlink_linecard *linecard)
10465 mutex_lock(&linecard->state_lock);
10466 WARN_ON(linecard->nested_devlink);
10467 linecard->state = DEVLINK_LINECARD_STATE_UNPROVISIONED;
10468 linecard->type = NULL;
10469 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10470 mutex_unlock(&linecard->state_lock);
10472 EXPORT_SYMBOL_GPL(devlink_linecard_provision_clear);
10475 * devlink_linecard_provision_fail - Fail provisioning on linecard
10477 * @linecard: devlink linecard
10479 * This is either called directly from the provision() op call or
10480 * as a result of the provision() op call asynchronously.
10482 void devlink_linecard_provision_fail(struct devlink_linecard *linecard)
10484 mutex_lock(&linecard->state_lock);
10485 WARN_ON(linecard->nested_devlink);
10486 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONING_FAILED;
10487 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10488 mutex_unlock(&linecard->state_lock);
10490 EXPORT_SYMBOL_GPL(devlink_linecard_provision_fail);
10493 * devlink_linecard_activate - Set linecard active
10495 * @linecard: devlink linecard
10497 void devlink_linecard_activate(struct devlink_linecard *linecard)
10499 mutex_lock(&linecard->state_lock);
10500 WARN_ON(linecard->state != DEVLINK_LINECARD_STATE_PROVISIONED);
10501 linecard->state = DEVLINK_LINECARD_STATE_ACTIVE;
10502 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10503 mutex_unlock(&linecard->state_lock);
10505 EXPORT_SYMBOL_GPL(devlink_linecard_activate);
10508 * devlink_linecard_deactivate - Set linecard inactive
10510 * @linecard: devlink linecard
10512 void devlink_linecard_deactivate(struct devlink_linecard *linecard)
10514 mutex_lock(&linecard->state_lock);
10515 switch (linecard->state) {
10516 case DEVLINK_LINECARD_STATE_ACTIVE:
10517 linecard->state = DEVLINK_LINECARD_STATE_PROVISIONED;
10518 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10520 case DEVLINK_LINECARD_STATE_UNPROVISIONING:
10521 /* Line card is being deactivated as part
10522 * of unprovisioning flow.
10529 mutex_unlock(&linecard->state_lock);
10531 EXPORT_SYMBOL_GPL(devlink_linecard_deactivate);
10534 * devlink_linecard_nested_dl_set - Attach/detach nested devlink
10535 * instance to linecard.
10537 * @linecard: devlink linecard
10538 * @nested_devlink: devlink instance to attach or NULL to detach
10540 void devlink_linecard_nested_dl_set(struct devlink_linecard *linecard,
10541 struct devlink *nested_devlink)
10543 mutex_lock(&linecard->state_lock);
10544 linecard->nested_devlink = nested_devlink;
10545 devlink_linecard_notify(linecard, DEVLINK_CMD_LINECARD_NEW);
10546 mutex_unlock(&linecard->state_lock);
10548 EXPORT_SYMBOL_GPL(devlink_linecard_nested_dl_set);
10550 int devl_sb_register(struct devlink *devlink, unsigned int sb_index,
10551 u32 size, u16 ingress_pools_count,
10552 u16 egress_pools_count, u16 ingress_tc_count,
10553 u16 egress_tc_count)
10555 struct devlink_sb *devlink_sb;
10557 lockdep_assert_held(&devlink->lock);
10559 if (devlink_sb_index_exists(devlink, sb_index))
10562 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
10565 devlink_sb->index = sb_index;
10566 devlink_sb->size = size;
10567 devlink_sb->ingress_pools_count = ingress_pools_count;
10568 devlink_sb->egress_pools_count = egress_pools_count;
10569 devlink_sb->ingress_tc_count = ingress_tc_count;
10570 devlink_sb->egress_tc_count = egress_tc_count;
10571 list_add_tail(&devlink_sb->list, &devlink->sb_list);
10574 EXPORT_SYMBOL_GPL(devl_sb_register);
10576 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
10577 u32 size, u16 ingress_pools_count,
10578 u16 egress_pools_count, u16 ingress_tc_count,
10579 u16 egress_tc_count)
10583 devl_lock(devlink);
10584 err = devl_sb_register(devlink, sb_index, size, ingress_pools_count,
10585 egress_pools_count, ingress_tc_count,
10587 devl_unlock(devlink);
10590 EXPORT_SYMBOL_GPL(devlink_sb_register);
10592 void devl_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10594 struct devlink_sb *devlink_sb;
10596 lockdep_assert_held(&devlink->lock);
10598 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
10599 WARN_ON(!devlink_sb);
10600 list_del(&devlink_sb->list);
10603 EXPORT_SYMBOL_GPL(devl_sb_unregister);
10605 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
10607 devl_lock(devlink);
10608 devl_sb_unregister(devlink, sb_index);
10609 devl_unlock(devlink);
10611 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
10614 * devl_dpipe_headers_register - register dpipe headers
10616 * @devlink: devlink
10617 * @dpipe_headers: dpipe header array
10619 * Register the headers supported by hardware.
10621 void devl_dpipe_headers_register(struct devlink *devlink,
10622 struct devlink_dpipe_headers *dpipe_headers)
10624 lockdep_assert_held(&devlink->lock);
10626 devlink->dpipe_headers = dpipe_headers;
10628 EXPORT_SYMBOL_GPL(devl_dpipe_headers_register);
10631 * devl_dpipe_headers_unregister - unregister dpipe headers
10633 * @devlink: devlink
10635 * Unregister the headers supported by hardware.
10637 void devl_dpipe_headers_unregister(struct devlink *devlink)
10639 lockdep_assert_held(&devlink->lock);
10641 devlink->dpipe_headers = NULL;
10643 EXPORT_SYMBOL_GPL(devl_dpipe_headers_unregister);
10646 * devlink_dpipe_table_counter_enabled - check if counter allocation
10648 * @devlink: devlink
10649 * @table_name: tables name
10651 * Used by driver to check if counter allocation is required.
10652 * After counter allocation is turned on the table entries
10653 * are updated to include counter statistics.
10655 * After that point on the driver must respect the counter
10656 * state so that each entry added to the table is added
10659 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
10660 const char *table_name)
10662 struct devlink_dpipe_table *table;
10666 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10667 table_name, devlink);
10670 enabled = table->counters_enabled;
10674 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
10677 * devl_dpipe_table_register - register dpipe table
10679 * @devlink: devlink
10680 * @table_name: table name
10681 * @table_ops: table ops
10683 * @counter_control_extern: external control for counters
10685 int devl_dpipe_table_register(struct devlink *devlink,
10686 const char *table_name,
10687 struct devlink_dpipe_table_ops *table_ops,
10688 void *priv, bool counter_control_extern)
10690 struct devlink_dpipe_table *table;
10692 lockdep_assert_held(&devlink->lock);
10694 if (WARN_ON(!table_ops->size_get))
10697 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
10701 table = kzalloc(sizeof(*table), GFP_KERNEL);
10705 table->name = table_name;
10706 table->table_ops = table_ops;
10707 table->priv = priv;
10708 table->counter_control_extern = counter_control_extern;
10710 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
10714 EXPORT_SYMBOL_GPL(devl_dpipe_table_register);
10717 * devl_dpipe_table_unregister - unregister dpipe table
10719 * @devlink: devlink
10720 * @table_name: table name
10722 void devl_dpipe_table_unregister(struct devlink *devlink,
10723 const char *table_name)
10725 struct devlink_dpipe_table *table;
10727 lockdep_assert_held(&devlink->lock);
10729 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10730 table_name, devlink);
10733 list_del_rcu(&table->list);
10734 kfree_rcu(table, rcu);
10736 EXPORT_SYMBOL_GPL(devl_dpipe_table_unregister);
10739 * devl_resource_register - devlink resource register
10741 * @devlink: devlink
10742 * @resource_name: resource's name
10743 * @resource_size: resource's size
10744 * @resource_id: resource's id
10745 * @parent_resource_id: resource's parent id
10746 * @size_params: size parameters
10748 * Generic resources should reuse the same names across drivers.
10749 * Please see the generic resources list at:
10750 * Documentation/networking/devlink/devlink-resource.rst
10752 int devl_resource_register(struct devlink *devlink,
10753 const char *resource_name,
10756 u64 parent_resource_id,
10757 const struct devlink_resource_size_params *size_params)
10759 struct devlink_resource *resource;
10760 struct list_head *resource_list;
10761 bool top_hierarchy;
10763 lockdep_assert_held(&devlink->lock);
10765 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
10767 resource = devlink_resource_find(devlink, NULL, resource_id);
10771 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
10775 if (top_hierarchy) {
10776 resource_list = &devlink->resource_list;
10778 struct devlink_resource *parent_resource;
10780 parent_resource = devlink_resource_find(devlink, NULL,
10781 parent_resource_id);
10782 if (parent_resource) {
10783 resource_list = &parent_resource->resource_list;
10784 resource->parent = parent_resource;
10791 resource->name = resource_name;
10792 resource->size = resource_size;
10793 resource->size_new = resource_size;
10794 resource->id = resource_id;
10795 resource->size_valid = true;
10796 memcpy(&resource->size_params, size_params,
10797 sizeof(resource->size_params));
10798 INIT_LIST_HEAD(&resource->resource_list);
10799 list_add_tail(&resource->list, resource_list);
10803 EXPORT_SYMBOL_GPL(devl_resource_register);
10806 * devlink_resource_register - devlink resource register
10808 * @devlink: devlink
10809 * @resource_name: resource's name
10810 * @resource_size: resource's size
10811 * @resource_id: resource's id
10812 * @parent_resource_id: resource's parent id
10813 * @size_params: size parameters
10815 * Generic resources should reuse the same names across drivers.
10816 * Please see the generic resources list at:
10817 * Documentation/networking/devlink/devlink-resource.rst
10819 * Context: Takes and release devlink->lock <mutex>.
10821 int devlink_resource_register(struct devlink *devlink,
10822 const char *resource_name,
10825 u64 parent_resource_id,
10826 const struct devlink_resource_size_params *size_params)
10830 devl_lock(devlink);
10831 err = devl_resource_register(devlink, resource_name, resource_size,
10832 resource_id, parent_resource_id, size_params);
10833 devl_unlock(devlink);
10836 EXPORT_SYMBOL_GPL(devlink_resource_register);
10838 static void devlink_resource_unregister(struct devlink *devlink,
10839 struct devlink_resource *resource)
10841 struct devlink_resource *tmp, *child_resource;
10843 list_for_each_entry_safe(child_resource, tmp, &resource->resource_list,
10845 devlink_resource_unregister(devlink, child_resource);
10846 list_del(&child_resource->list);
10847 kfree(child_resource);
10852 * devl_resources_unregister - free all resources
10854 * @devlink: devlink
10856 void devl_resources_unregister(struct devlink *devlink)
10858 struct devlink_resource *tmp, *child_resource;
10860 lockdep_assert_held(&devlink->lock);
10862 list_for_each_entry_safe(child_resource, tmp, &devlink->resource_list,
10864 devlink_resource_unregister(devlink, child_resource);
10865 list_del(&child_resource->list);
10866 kfree(child_resource);
10869 EXPORT_SYMBOL_GPL(devl_resources_unregister);
10872 * devlink_resources_unregister - free all resources
10874 * @devlink: devlink
10876 * Context: Takes and release devlink->lock <mutex>.
10878 void devlink_resources_unregister(struct devlink *devlink)
10880 devl_lock(devlink);
10881 devl_resources_unregister(devlink);
10882 devl_unlock(devlink);
10884 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
10887 * devl_resource_size_get - get and update size
10889 * @devlink: devlink
10890 * @resource_id: the requested resource id
10891 * @p_resource_size: ptr to update
10893 int devl_resource_size_get(struct devlink *devlink,
10895 u64 *p_resource_size)
10897 struct devlink_resource *resource;
10899 lockdep_assert_held(&devlink->lock);
10901 resource = devlink_resource_find(devlink, NULL, resource_id);
10904 *p_resource_size = resource->size_new;
10905 resource->size = resource->size_new;
10908 EXPORT_SYMBOL_GPL(devl_resource_size_get);
10911 * devl_dpipe_table_resource_set - set the resource id
10913 * @devlink: devlink
10914 * @table_name: table name
10915 * @resource_id: resource id
10916 * @resource_units: number of resource's units consumed per table's entry
10918 int devl_dpipe_table_resource_set(struct devlink *devlink,
10919 const char *table_name, u64 resource_id,
10920 u64 resource_units)
10922 struct devlink_dpipe_table *table;
10924 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
10925 table_name, devlink);
10929 table->resource_id = resource_id;
10930 table->resource_units = resource_units;
10931 table->resource_valid = true;
10934 EXPORT_SYMBOL_GPL(devl_dpipe_table_resource_set);
10937 * devl_resource_occ_get_register - register occupancy getter
10939 * @devlink: devlink
10940 * @resource_id: resource id
10941 * @occ_get: occupancy getter callback
10942 * @occ_get_priv: occupancy getter callback priv
10944 void devl_resource_occ_get_register(struct devlink *devlink,
10946 devlink_resource_occ_get_t *occ_get,
10947 void *occ_get_priv)
10949 struct devlink_resource *resource;
10951 lockdep_assert_held(&devlink->lock);
10953 resource = devlink_resource_find(devlink, NULL, resource_id);
10954 if (WARN_ON(!resource))
10956 WARN_ON(resource->occ_get);
10958 resource->occ_get = occ_get;
10959 resource->occ_get_priv = occ_get_priv;
10961 EXPORT_SYMBOL_GPL(devl_resource_occ_get_register);
10964 * devlink_resource_occ_get_register - register occupancy getter
10966 * @devlink: devlink
10967 * @resource_id: resource id
10968 * @occ_get: occupancy getter callback
10969 * @occ_get_priv: occupancy getter callback priv
10971 * Context: Takes and release devlink->lock <mutex>.
10973 void devlink_resource_occ_get_register(struct devlink *devlink,
10975 devlink_resource_occ_get_t *occ_get,
10976 void *occ_get_priv)
10978 devl_lock(devlink);
10979 devl_resource_occ_get_register(devlink, resource_id,
10980 occ_get, occ_get_priv);
10981 devl_unlock(devlink);
10983 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
10986 * devl_resource_occ_get_unregister - unregister occupancy getter
10988 * @devlink: devlink
10989 * @resource_id: resource id
10991 void devl_resource_occ_get_unregister(struct devlink *devlink,
10994 struct devlink_resource *resource;
10996 lockdep_assert_held(&devlink->lock);
10998 resource = devlink_resource_find(devlink, NULL, resource_id);
10999 if (WARN_ON(!resource))
11001 WARN_ON(!resource->occ_get);
11003 resource->occ_get = NULL;
11004 resource->occ_get_priv = NULL;
11006 EXPORT_SYMBOL_GPL(devl_resource_occ_get_unregister);
11009 * devlink_resource_occ_get_unregister - unregister occupancy getter
11011 * @devlink: devlink
11012 * @resource_id: resource id
11014 * Context: Takes and release devlink->lock <mutex>.
11016 void devlink_resource_occ_get_unregister(struct devlink *devlink,
11019 devl_lock(devlink);
11020 devl_resource_occ_get_unregister(devlink, resource_id);
11021 devl_unlock(devlink);
11023 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
11025 static int devlink_param_verify(const struct devlink_param *param)
11027 if (!param || !param->name || !param->supported_cmodes)
11029 if (param->generic)
11030 return devlink_param_generic_verify(param);
11032 return devlink_param_driver_verify(param);
11036 * devlink_params_register - register configuration parameters
11038 * @devlink: devlink
11039 * @params: configuration parameters array
11040 * @params_count: number of parameters provided
11042 * Register the configuration parameters supported by the driver.
11044 int devlink_params_register(struct devlink *devlink,
11045 const struct devlink_param *params,
11046 size_t params_count)
11048 const struct devlink_param *param = params;
11051 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11053 for (i = 0; i < params_count; i++, param++) {
11054 err = devlink_param_register(devlink, param);
11064 for (param--; i > 0; i--, param--)
11065 devlink_param_unregister(devlink, param);
11068 EXPORT_SYMBOL_GPL(devlink_params_register);
11071 * devlink_params_unregister - unregister configuration parameters
11072 * @devlink: devlink
11073 * @params: configuration parameters to unregister
11074 * @params_count: number of parameters provided
11076 void devlink_params_unregister(struct devlink *devlink,
11077 const struct devlink_param *params,
11078 size_t params_count)
11080 const struct devlink_param *param = params;
11083 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11085 for (i = 0; i < params_count; i++, param++)
11086 devlink_param_unregister(devlink, param);
11088 EXPORT_SYMBOL_GPL(devlink_params_unregister);
11091 * devlink_param_register - register one configuration parameter
11093 * @devlink: devlink
11094 * @param: one configuration parameter
11096 * Register the configuration parameter supported by the driver.
11097 * Return: returns 0 on successful registration or error code otherwise.
11099 int devlink_param_register(struct devlink *devlink,
11100 const struct devlink_param *param)
11102 struct devlink_param_item *param_item;
11104 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11106 WARN_ON(devlink_param_verify(param));
11107 WARN_ON(devlink_param_find_by_name(&devlink->param_list, param->name));
11109 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
11110 WARN_ON(param->get || param->set);
11112 WARN_ON(!param->get || !param->set);
11114 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
11118 param_item->param = param;
11120 list_add_tail(¶m_item->list, &devlink->param_list);
11123 EXPORT_SYMBOL_GPL(devlink_param_register);
11126 * devlink_param_unregister - unregister one configuration parameter
11127 * @devlink: devlink
11128 * @param: configuration parameter to unregister
11130 void devlink_param_unregister(struct devlink *devlink,
11131 const struct devlink_param *param)
11133 struct devlink_param_item *param_item;
11135 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11138 devlink_param_find_by_name(&devlink->param_list, param->name);
11139 WARN_ON(!param_item);
11140 list_del(¶m_item->list);
11143 EXPORT_SYMBOL_GPL(devlink_param_unregister);
11146 * devlink_param_driverinit_value_get - get configuration parameter
11147 * value for driver initializing
11149 * @devlink: devlink
11150 * @param_id: parameter ID
11151 * @init_val: value of parameter in driverinit configuration mode
11153 * This function should be used by the driver to get driverinit
11154 * configuration for initialization after reload command.
11156 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
11157 union devlink_param_value *init_val)
11159 struct devlink_param_item *param_item;
11161 if (!devlink_reload_supported(devlink->ops))
11162 return -EOPNOTSUPP;
11164 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11168 if (!param_item->driverinit_value_valid ||
11169 !devlink_param_cmode_is_supported(param_item->param,
11170 DEVLINK_PARAM_CMODE_DRIVERINIT))
11171 return -EOPNOTSUPP;
11173 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11174 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
11176 *init_val = param_item->driverinit_value;
11180 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
11183 * devlink_param_driverinit_value_set - set value of configuration
11184 * parameter for driverinit
11185 * configuration mode
11187 * @devlink: devlink
11188 * @param_id: parameter ID
11189 * @init_val: value of parameter to set for driverinit configuration mode
11191 * This function should be used by the driver to set driverinit
11192 * configuration mode default value.
11194 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
11195 union devlink_param_value init_val)
11197 struct devlink_param_item *param_item;
11199 ASSERT_DEVLINK_NOT_REGISTERED(devlink);
11201 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11205 if (!devlink_param_cmode_is_supported(param_item->param,
11206 DEVLINK_PARAM_CMODE_DRIVERINIT))
11207 return -EOPNOTSUPP;
11209 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
11210 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
11212 param_item->driverinit_value = init_val;
11213 param_item->driverinit_value_valid = true;
11216 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
11219 * devlink_param_value_changed - notify devlink on a parameter's value
11220 * change. Should be called by the driver
11221 * right after the change.
11223 * @devlink: devlink
11224 * @param_id: parameter ID
11226 * This function should be used by the driver to notify devlink on value
11227 * change, excluding driverinit configuration mode.
11228 * For driverinit configuration mode driver should use the function
11230 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
11232 struct devlink_param_item *param_item;
11234 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
11235 WARN_ON(!param_item);
11237 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
11239 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
11242 * devl_region_create - create a new address region
11244 * @devlink: devlink
11245 * @ops: region operations and name
11246 * @region_max_snapshots: Maximum supported number of snapshots for region
11247 * @region_size: size of region
11249 struct devlink_region *devl_region_create(struct devlink *devlink,
11250 const struct devlink_region_ops *ops,
11251 u32 region_max_snapshots,
11254 struct devlink_region *region;
11256 devl_assert_locked(devlink);
11258 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11259 return ERR_PTR(-EINVAL);
11261 if (devlink_region_get_by_name(devlink, ops->name))
11262 return ERR_PTR(-EEXIST);
11264 region = kzalloc(sizeof(*region), GFP_KERNEL);
11266 return ERR_PTR(-ENOMEM);
11268 region->devlink = devlink;
11269 region->max_snapshots = region_max_snapshots;
11271 region->size = region_size;
11272 INIT_LIST_HEAD(®ion->snapshot_list);
11273 mutex_init(®ion->snapshot_lock);
11274 list_add_tail(®ion->list, &devlink->region_list);
11275 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11279 EXPORT_SYMBOL_GPL(devl_region_create);
11282 * devlink_region_create - create a new address region
11284 * @devlink: devlink
11285 * @ops: region operations and name
11286 * @region_max_snapshots: Maximum supported number of snapshots for region
11287 * @region_size: size of region
11289 * Context: Takes and release devlink->lock <mutex>.
11291 struct devlink_region *
11292 devlink_region_create(struct devlink *devlink,
11293 const struct devlink_region_ops *ops,
11294 u32 region_max_snapshots, u64 region_size)
11296 struct devlink_region *region;
11298 devl_lock(devlink);
11299 region = devl_region_create(devlink, ops, region_max_snapshots,
11301 devl_unlock(devlink);
11304 EXPORT_SYMBOL_GPL(devlink_region_create);
11307 * devlink_port_region_create - create a new address region for a port
11309 * @port: devlink port
11310 * @ops: region operations and name
11311 * @region_max_snapshots: Maximum supported number of snapshots for region
11312 * @region_size: size of region
11314 * Context: Takes and release devlink->lock <mutex>.
11316 struct devlink_region *
11317 devlink_port_region_create(struct devlink_port *port,
11318 const struct devlink_port_region_ops *ops,
11319 u32 region_max_snapshots, u64 region_size)
11321 struct devlink *devlink = port->devlink;
11322 struct devlink_region *region;
11325 ASSERT_DEVLINK_PORT_INITIALIZED(port);
11327 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
11328 return ERR_PTR(-EINVAL);
11330 devl_lock(devlink);
11332 if (devlink_port_region_get_by_name(port, ops->name)) {
11337 region = kzalloc(sizeof(*region), GFP_KERNEL);
11343 region->devlink = devlink;
11344 region->port = port;
11345 region->max_snapshots = region_max_snapshots;
11346 region->port_ops = ops;
11347 region->size = region_size;
11348 INIT_LIST_HEAD(®ion->snapshot_list);
11349 mutex_init(®ion->snapshot_lock);
11350 list_add_tail(®ion->list, &port->region_list);
11351 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
11353 devl_unlock(devlink);
11357 devl_unlock(devlink);
11358 return ERR_PTR(err);
11360 EXPORT_SYMBOL_GPL(devlink_port_region_create);
11363 * devl_region_destroy - destroy address region
11365 * @region: devlink region to destroy
11367 void devl_region_destroy(struct devlink_region *region)
11369 struct devlink *devlink = region->devlink;
11370 struct devlink_snapshot *snapshot, *ts;
11372 devl_assert_locked(devlink);
11374 /* Free all snapshots of region */
11375 mutex_lock(®ion->snapshot_lock);
11376 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
11377 devlink_region_snapshot_del(region, snapshot);
11378 mutex_unlock(®ion->snapshot_lock);
11380 list_del(®ion->list);
11381 mutex_destroy(®ion->snapshot_lock);
11383 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
11386 EXPORT_SYMBOL_GPL(devl_region_destroy);
11389 * devlink_region_destroy - destroy address region
11391 * @region: devlink region to destroy
11393 * Context: Takes and release devlink->lock <mutex>.
11395 void devlink_region_destroy(struct devlink_region *region)
11397 struct devlink *devlink = region->devlink;
11399 devl_lock(devlink);
11400 devl_region_destroy(region);
11401 devl_unlock(devlink);
11403 EXPORT_SYMBOL_GPL(devlink_region_destroy);
11406 * devlink_region_snapshot_id_get - get snapshot ID
11408 * This callback should be called when adding a new snapshot,
11409 * Driver should use the same id for multiple snapshots taken
11410 * on multiple regions at the same time/by the same trigger.
11412 * The caller of this function must use devlink_region_snapshot_id_put
11413 * when finished creating regions using this id.
11415 * Returns zero on success, or a negative error code on failure.
11417 * @devlink: devlink
11418 * @id: storage to return id
11420 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
11422 return __devlink_region_snapshot_id_get(devlink, id);
11424 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
11427 * devlink_region_snapshot_id_put - put snapshot ID reference
11429 * This should be called by a driver after finishing creating snapshots
11430 * with an id. Doing so ensures that the ID can later be released in the
11431 * event that all snapshots using it have been destroyed.
11433 * @devlink: devlink
11434 * @id: id to release reference on
11436 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
11438 __devlink_snapshot_id_decrement(devlink, id);
11440 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
11443 * devlink_region_snapshot_create - create a new snapshot
11444 * This will add a new snapshot of a region. The snapshot
11445 * will be stored on the region struct and can be accessed
11446 * from devlink. This is useful for future analyses of snapshots.
11447 * Multiple snapshots can be created on a region.
11448 * The @snapshot_id should be obtained using the getter function.
11450 * @region: devlink region of the snapshot
11451 * @data: snapshot data
11452 * @snapshot_id: snapshot id to be created
11454 int devlink_region_snapshot_create(struct devlink_region *region,
11455 u8 *data, u32 snapshot_id)
11459 mutex_lock(®ion->snapshot_lock);
11460 err = __devlink_region_snapshot_create(region, data, snapshot_id);
11461 mutex_unlock(®ion->snapshot_lock);
11464 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
11466 #define DEVLINK_TRAP(_id, _type) \
11468 .type = DEVLINK_TRAP_TYPE_##_type, \
11469 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
11470 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
11473 static const struct devlink_trap devlink_trap_generic[] = {
11474 DEVLINK_TRAP(SMAC_MC, DROP),
11475 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
11476 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
11477 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
11478 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
11479 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
11480 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
11481 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
11482 DEVLINK_TRAP(TAIL_DROP, DROP),
11483 DEVLINK_TRAP(NON_IP_PACKET, DROP),
11484 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
11485 DEVLINK_TRAP(DIP_LB, DROP),
11486 DEVLINK_TRAP(SIP_MC, DROP),
11487 DEVLINK_TRAP(SIP_LB, DROP),
11488 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
11489 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
11490 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
11491 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
11492 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
11493 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
11494 DEVLINK_TRAP(RPF, EXCEPTION),
11495 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
11496 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
11497 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
11498 DEVLINK_TRAP(NON_ROUTABLE, DROP),
11499 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
11500 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
11501 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
11502 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
11503 DEVLINK_TRAP(STP, CONTROL),
11504 DEVLINK_TRAP(LACP, CONTROL),
11505 DEVLINK_TRAP(LLDP, CONTROL),
11506 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
11507 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
11508 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
11509 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
11510 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
11511 DEVLINK_TRAP(MLD_QUERY, CONTROL),
11512 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
11513 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
11514 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
11515 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
11516 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
11517 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
11518 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
11519 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
11520 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
11521 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
11522 DEVLINK_TRAP(IPV4_BFD, CONTROL),
11523 DEVLINK_TRAP(IPV6_BFD, CONTROL),
11524 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
11525 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
11526 DEVLINK_TRAP(IPV4_BGP, CONTROL),
11527 DEVLINK_TRAP(IPV6_BGP, CONTROL),
11528 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
11529 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
11530 DEVLINK_TRAP(IPV4_PIM, CONTROL),
11531 DEVLINK_TRAP(IPV6_PIM, CONTROL),
11532 DEVLINK_TRAP(UC_LB, CONTROL),
11533 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
11534 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
11535 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
11536 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
11537 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
11538 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
11539 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
11540 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
11541 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
11542 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
11543 DEVLINK_TRAP(PTP_EVENT, CONTROL),
11544 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
11545 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
11546 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
11547 DEVLINK_TRAP(EARLY_DROP, DROP),
11548 DEVLINK_TRAP(VXLAN_PARSING, DROP),
11549 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
11550 DEVLINK_TRAP(VLAN_PARSING, DROP),
11551 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
11552 DEVLINK_TRAP(MPLS_PARSING, DROP),
11553 DEVLINK_TRAP(ARP_PARSING, DROP),
11554 DEVLINK_TRAP(IP_1_PARSING, DROP),
11555 DEVLINK_TRAP(IP_N_PARSING, DROP),
11556 DEVLINK_TRAP(GRE_PARSING, DROP),
11557 DEVLINK_TRAP(UDP_PARSING, DROP),
11558 DEVLINK_TRAP(TCP_PARSING, DROP),
11559 DEVLINK_TRAP(IPSEC_PARSING, DROP),
11560 DEVLINK_TRAP(SCTP_PARSING, DROP),
11561 DEVLINK_TRAP(DCCP_PARSING, DROP),
11562 DEVLINK_TRAP(GTP_PARSING, DROP),
11563 DEVLINK_TRAP(ESP_PARSING, DROP),
11564 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
11565 DEVLINK_TRAP(DMAC_FILTER, DROP),
11566 DEVLINK_TRAP(EAPOL, CONTROL),
11567 DEVLINK_TRAP(LOCKED_PORT, DROP),
11570 #define DEVLINK_TRAP_GROUP(_id) \
11572 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
11573 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
11576 static const struct devlink_trap_group devlink_trap_group_generic[] = {
11577 DEVLINK_TRAP_GROUP(L2_DROPS),
11578 DEVLINK_TRAP_GROUP(L3_DROPS),
11579 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
11580 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
11581 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
11582 DEVLINK_TRAP_GROUP(ACL_DROPS),
11583 DEVLINK_TRAP_GROUP(STP),
11584 DEVLINK_TRAP_GROUP(LACP),
11585 DEVLINK_TRAP_GROUP(LLDP),
11586 DEVLINK_TRAP_GROUP(MC_SNOOPING),
11587 DEVLINK_TRAP_GROUP(DHCP),
11588 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
11589 DEVLINK_TRAP_GROUP(BFD),
11590 DEVLINK_TRAP_GROUP(OSPF),
11591 DEVLINK_TRAP_GROUP(BGP),
11592 DEVLINK_TRAP_GROUP(VRRP),
11593 DEVLINK_TRAP_GROUP(PIM),
11594 DEVLINK_TRAP_GROUP(UC_LB),
11595 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
11596 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
11597 DEVLINK_TRAP_GROUP(IPV6),
11598 DEVLINK_TRAP_GROUP(PTP_EVENT),
11599 DEVLINK_TRAP_GROUP(PTP_GENERAL),
11600 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
11601 DEVLINK_TRAP_GROUP(ACL_TRAP),
11602 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
11603 DEVLINK_TRAP_GROUP(EAPOL),
11606 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
11608 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
11611 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
11614 if (trap->type != devlink_trap_generic[trap->id].type)
11620 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
11624 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
11627 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
11628 if (!strcmp(trap->name, devlink_trap_generic[i].name))
11635 static int devlink_trap_verify(const struct devlink_trap *trap)
11637 if (!trap || !trap->name)
11641 return devlink_trap_generic_verify(trap);
11643 return devlink_trap_driver_verify(trap);
11647 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
11649 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11652 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
11659 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
11663 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
11666 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
11667 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
11674 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
11676 if (group->generic)
11677 return devlink_trap_group_generic_verify(group);
11679 return devlink_trap_group_driver_verify(group);
11683 devlink_trap_group_notify(struct devlink *devlink,
11684 const struct devlink_trap_group_item *group_item,
11685 enum devlink_command cmd)
11687 struct sk_buff *msg;
11690 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
11691 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
11692 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11695 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11699 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
11706 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11707 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11711 devlink_trap_item_group_link(struct devlink *devlink,
11712 struct devlink_trap_item *trap_item)
11714 u16 group_id = trap_item->trap->init_group_id;
11715 struct devlink_trap_group_item *group_item;
11717 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
11718 if (WARN_ON_ONCE(!group_item))
11721 trap_item->group_item = group_item;
11726 static void devlink_trap_notify(struct devlink *devlink,
11727 const struct devlink_trap_item *trap_item,
11728 enum devlink_command cmd)
11730 struct sk_buff *msg;
11733 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
11734 cmd != DEVLINK_CMD_TRAP_DEL);
11735 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
11738 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11742 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
11748 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11749 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11753 devlink_trap_register(struct devlink *devlink,
11754 const struct devlink_trap *trap, void *priv)
11756 struct devlink_trap_item *trap_item;
11759 if (devlink_trap_item_lookup(devlink, trap->name))
11762 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
11766 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11767 if (!trap_item->stats) {
11769 goto err_stats_alloc;
11772 trap_item->trap = trap;
11773 trap_item->action = trap->init_action;
11774 trap_item->priv = priv;
11776 err = devlink_trap_item_group_link(devlink, trap_item);
11778 goto err_group_link;
11780 err = devlink->ops->trap_init(devlink, trap, trap_item);
11782 goto err_trap_init;
11784 list_add_tail(&trap_item->list, &devlink->trap_list);
11785 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
11791 free_percpu(trap_item->stats);
11797 static void devlink_trap_unregister(struct devlink *devlink,
11798 const struct devlink_trap *trap)
11800 struct devlink_trap_item *trap_item;
11802 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11803 if (WARN_ON_ONCE(!trap_item))
11806 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
11807 list_del(&trap_item->list);
11808 if (devlink->ops->trap_fini)
11809 devlink->ops->trap_fini(devlink, trap, trap_item);
11810 free_percpu(trap_item->stats);
11814 static void devlink_trap_disable(struct devlink *devlink,
11815 const struct devlink_trap *trap)
11817 struct devlink_trap_item *trap_item;
11819 trap_item = devlink_trap_item_lookup(devlink, trap->name);
11820 if (WARN_ON_ONCE(!trap_item))
11823 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
11825 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
11829 * devl_traps_register - Register packet traps with devlink.
11830 * @devlink: devlink.
11831 * @traps: Packet traps.
11832 * @traps_count: Count of provided packet traps.
11833 * @priv: Driver private information.
11835 * Return: Non-zero value on failure.
11837 int devl_traps_register(struct devlink *devlink,
11838 const struct devlink_trap *traps,
11839 size_t traps_count, void *priv)
11843 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
11846 devl_assert_locked(devlink);
11847 for (i = 0; i < traps_count; i++) {
11848 const struct devlink_trap *trap = &traps[i];
11850 err = devlink_trap_verify(trap);
11852 goto err_trap_verify;
11854 err = devlink_trap_register(devlink, trap, priv);
11856 goto err_trap_register;
11863 for (i--; i >= 0; i--)
11864 devlink_trap_unregister(devlink, &traps[i]);
11867 EXPORT_SYMBOL_GPL(devl_traps_register);
11870 * devlink_traps_register - Register packet traps with devlink.
11871 * @devlink: devlink.
11872 * @traps: Packet traps.
11873 * @traps_count: Count of provided packet traps.
11874 * @priv: Driver private information.
11876 * Context: Takes and release devlink->lock <mutex>.
11878 * Return: Non-zero value on failure.
11880 int devlink_traps_register(struct devlink *devlink,
11881 const struct devlink_trap *traps,
11882 size_t traps_count, void *priv)
11886 devl_lock(devlink);
11887 err = devl_traps_register(devlink, traps, traps_count, priv);
11888 devl_unlock(devlink);
11891 EXPORT_SYMBOL_GPL(devlink_traps_register);
11894 * devl_traps_unregister - Unregister packet traps from devlink.
11895 * @devlink: devlink.
11896 * @traps: Packet traps.
11897 * @traps_count: Count of provided packet traps.
11899 void devl_traps_unregister(struct devlink *devlink,
11900 const struct devlink_trap *traps,
11901 size_t traps_count)
11905 devl_assert_locked(devlink);
11906 /* Make sure we do not have any packets in-flight while unregistering
11907 * traps by disabling all of them and waiting for a grace period.
11909 for (i = traps_count - 1; i >= 0; i--)
11910 devlink_trap_disable(devlink, &traps[i]);
11912 for (i = traps_count - 1; i >= 0; i--)
11913 devlink_trap_unregister(devlink, &traps[i]);
11915 EXPORT_SYMBOL_GPL(devl_traps_unregister);
11918 * devlink_traps_unregister - Unregister packet traps from devlink.
11919 * @devlink: devlink.
11920 * @traps: Packet traps.
11921 * @traps_count: Count of provided packet traps.
11923 * Context: Takes and release devlink->lock <mutex>.
11925 void devlink_traps_unregister(struct devlink *devlink,
11926 const struct devlink_trap *traps,
11927 size_t traps_count)
11929 devl_lock(devlink);
11930 devl_traps_unregister(devlink, traps, traps_count);
11931 devl_unlock(devlink);
11933 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11936 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11939 struct devlink_stats *stats;
11941 stats = this_cpu_ptr(trap_stats);
11942 u64_stats_update_begin(&stats->syncp);
11943 u64_stats_add(&stats->rx_bytes, skb_len);
11944 u64_stats_inc(&stats->rx_packets);
11945 u64_stats_update_end(&stats->syncp);
11949 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11950 const struct devlink_trap_item *trap_item,
11951 struct devlink_port *in_devlink_port,
11952 const struct flow_action_cookie *fa_cookie)
11954 metadata->trap_name = trap_item->trap->name;
11955 metadata->trap_group_name = trap_item->group_item->group->name;
11956 metadata->fa_cookie = fa_cookie;
11957 metadata->trap_type = trap_item->trap->type;
11959 spin_lock(&in_devlink_port->type_lock);
11960 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11961 metadata->input_dev = in_devlink_port->type_eth.netdev;
11962 spin_unlock(&in_devlink_port->type_lock);
11966 * devlink_trap_report - Report trapped packet to drop monitor.
11967 * @devlink: devlink.
11968 * @skb: Trapped packet.
11969 * @trap_ctx: Trap context.
11970 * @in_devlink_port: Input devlink port.
11971 * @fa_cookie: Flow action cookie. Could be NULL.
11973 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11974 void *trap_ctx, struct devlink_port *in_devlink_port,
11975 const struct flow_action_cookie *fa_cookie)
11978 struct devlink_trap_item *trap_item = trap_ctx;
11980 devlink_trap_stats_update(trap_item->stats, skb->len);
11981 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11983 if (trace_devlink_trap_report_enabled()) {
11984 struct devlink_trap_metadata metadata = {};
11986 devlink_trap_report_metadata_set(&metadata, trap_item,
11987 in_devlink_port, fa_cookie);
11988 trace_devlink_trap_report(devlink, skb, &metadata);
11991 EXPORT_SYMBOL_GPL(devlink_trap_report);
11994 * devlink_trap_ctx_priv - Trap context to driver private information.
11995 * @trap_ctx: Trap context.
11997 * Return: Driver private information passed during registration.
11999 void *devlink_trap_ctx_priv(void *trap_ctx)
12001 struct devlink_trap_item *trap_item = trap_ctx;
12003 return trap_item->priv;
12005 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
12008 devlink_trap_group_item_policer_link(struct devlink *devlink,
12009 struct devlink_trap_group_item *group_item)
12011 u32 policer_id = group_item->group->init_policer_id;
12012 struct devlink_trap_policer_item *policer_item;
12014 if (policer_id == 0)
12017 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
12018 if (WARN_ON_ONCE(!policer_item))
12021 group_item->policer_item = policer_item;
12027 devlink_trap_group_register(struct devlink *devlink,
12028 const struct devlink_trap_group *group)
12030 struct devlink_trap_group_item *group_item;
12033 if (devlink_trap_group_item_lookup(devlink, group->name))
12036 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
12040 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
12041 if (!group_item->stats) {
12043 goto err_stats_alloc;
12046 group_item->group = group;
12048 err = devlink_trap_group_item_policer_link(devlink, group_item);
12050 goto err_policer_link;
12052 if (devlink->ops->trap_group_init) {
12053 err = devlink->ops->trap_group_init(devlink, group);
12055 goto err_group_init;
12058 list_add_tail(&group_item->list, &devlink->trap_group_list);
12059 devlink_trap_group_notify(devlink, group_item,
12060 DEVLINK_CMD_TRAP_GROUP_NEW);
12066 free_percpu(group_item->stats);
12073 devlink_trap_group_unregister(struct devlink *devlink,
12074 const struct devlink_trap_group *group)
12076 struct devlink_trap_group_item *group_item;
12078 group_item = devlink_trap_group_item_lookup(devlink, group->name);
12079 if (WARN_ON_ONCE(!group_item))
12082 devlink_trap_group_notify(devlink, group_item,
12083 DEVLINK_CMD_TRAP_GROUP_DEL);
12084 list_del(&group_item->list);
12085 free_percpu(group_item->stats);
12090 * devl_trap_groups_register - Register packet trap groups with devlink.
12091 * @devlink: devlink.
12092 * @groups: Packet trap groups.
12093 * @groups_count: Count of provided packet trap groups.
12095 * Return: Non-zero value on failure.
12097 int devl_trap_groups_register(struct devlink *devlink,
12098 const struct devlink_trap_group *groups,
12099 size_t groups_count)
12103 devl_assert_locked(devlink);
12104 for (i = 0; i < groups_count; i++) {
12105 const struct devlink_trap_group *group = &groups[i];
12107 err = devlink_trap_group_verify(group);
12109 goto err_trap_group_verify;
12111 err = devlink_trap_group_register(devlink, group);
12113 goto err_trap_group_register;
12118 err_trap_group_register:
12119 err_trap_group_verify:
12120 for (i--; i >= 0; i--)
12121 devlink_trap_group_unregister(devlink, &groups[i]);
12124 EXPORT_SYMBOL_GPL(devl_trap_groups_register);
12127 * devlink_trap_groups_register - Register packet trap groups with devlink.
12128 * @devlink: devlink.
12129 * @groups: Packet trap groups.
12130 * @groups_count: Count of provided packet trap groups.
12132 * Context: Takes and release devlink->lock <mutex>.
12134 * Return: Non-zero value on failure.
12136 int devlink_trap_groups_register(struct devlink *devlink,
12137 const struct devlink_trap_group *groups,
12138 size_t groups_count)
12142 devl_lock(devlink);
12143 err = devl_trap_groups_register(devlink, groups, groups_count);
12144 devl_unlock(devlink);
12147 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
12150 * devl_trap_groups_unregister - Unregister packet trap groups from devlink.
12151 * @devlink: devlink.
12152 * @groups: Packet trap groups.
12153 * @groups_count: Count of provided packet trap groups.
12155 void devl_trap_groups_unregister(struct devlink *devlink,
12156 const struct devlink_trap_group *groups,
12157 size_t groups_count)
12161 devl_assert_locked(devlink);
12162 for (i = groups_count - 1; i >= 0; i--)
12163 devlink_trap_group_unregister(devlink, &groups[i]);
12165 EXPORT_SYMBOL_GPL(devl_trap_groups_unregister);
12168 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
12169 * @devlink: devlink.
12170 * @groups: Packet trap groups.
12171 * @groups_count: Count of provided packet trap groups.
12173 * Context: Takes and release devlink->lock <mutex>.
12175 void devlink_trap_groups_unregister(struct devlink *devlink,
12176 const struct devlink_trap_group *groups,
12177 size_t groups_count)
12179 devl_lock(devlink);
12180 devl_trap_groups_unregister(devlink, groups, groups_count);
12181 devl_unlock(devlink);
12183 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
12186 devlink_trap_policer_notify(struct devlink *devlink,
12187 const struct devlink_trap_policer_item *policer_item,
12188 enum devlink_command cmd)
12190 struct sk_buff *msg;
12193 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
12194 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
12195 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
12198 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12202 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
12209 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
12210 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
12214 devlink_trap_policer_register(struct devlink *devlink,
12215 const struct devlink_trap_policer *policer)
12217 struct devlink_trap_policer_item *policer_item;
12220 if (devlink_trap_policer_item_lookup(devlink, policer->id))
12223 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
12227 policer_item->policer = policer;
12228 policer_item->rate = policer->init_rate;
12229 policer_item->burst = policer->init_burst;
12231 if (devlink->ops->trap_policer_init) {
12232 err = devlink->ops->trap_policer_init(devlink, policer);
12234 goto err_policer_init;
12237 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
12238 devlink_trap_policer_notify(devlink, policer_item,
12239 DEVLINK_CMD_TRAP_POLICER_NEW);
12244 kfree(policer_item);
12249 devlink_trap_policer_unregister(struct devlink *devlink,
12250 const struct devlink_trap_policer *policer)
12252 struct devlink_trap_policer_item *policer_item;
12254 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
12255 if (WARN_ON_ONCE(!policer_item))
12258 devlink_trap_policer_notify(devlink, policer_item,
12259 DEVLINK_CMD_TRAP_POLICER_DEL);
12260 list_del(&policer_item->list);
12261 if (devlink->ops->trap_policer_fini)
12262 devlink->ops->trap_policer_fini(devlink, policer);
12263 kfree(policer_item);
12267 * devl_trap_policers_register - Register packet trap policers with devlink.
12268 * @devlink: devlink.
12269 * @policers: Packet trap policers.
12270 * @policers_count: Count of provided packet trap policers.
12272 * Return: Non-zero value on failure.
12275 devl_trap_policers_register(struct devlink *devlink,
12276 const struct devlink_trap_policer *policers,
12277 size_t policers_count)
12281 devl_assert_locked(devlink);
12282 for (i = 0; i < policers_count; i++) {
12283 const struct devlink_trap_policer *policer = &policers[i];
12285 if (WARN_ON(policer->id == 0 ||
12286 policer->max_rate < policer->min_rate ||
12287 policer->max_burst < policer->min_burst)) {
12289 goto err_trap_policer_verify;
12292 err = devlink_trap_policer_register(devlink, policer);
12294 goto err_trap_policer_register;
12298 err_trap_policer_register:
12299 err_trap_policer_verify:
12300 for (i--; i >= 0; i--)
12301 devlink_trap_policer_unregister(devlink, &policers[i]);
12304 EXPORT_SYMBOL_GPL(devl_trap_policers_register);
12307 * devl_trap_policers_unregister - Unregister packet trap policers from devlink.
12308 * @devlink: devlink.
12309 * @policers: Packet trap policers.
12310 * @policers_count: Count of provided packet trap policers.
12313 devl_trap_policers_unregister(struct devlink *devlink,
12314 const struct devlink_trap_policer *policers,
12315 size_t policers_count)
12319 devl_assert_locked(devlink);
12320 for (i = policers_count - 1; i >= 0; i--)
12321 devlink_trap_policer_unregister(devlink, &policers[i]);
12323 EXPORT_SYMBOL_GPL(devl_trap_policers_unregister);
12325 static void __devlink_compat_running_version(struct devlink *devlink,
12326 char *buf, size_t len)
12328 struct devlink_info_req req = {};
12329 const struct nlattr *nlattr;
12330 struct sk_buff *msg;
12333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
12338 err = devlink->ops->info_get(devlink, &req, NULL);
12342 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
12343 const struct nlattr *kv;
12346 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
12349 nla_for_each_nested(kv, nlattr, rem_kv) {
12350 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
12353 strlcat(buf, nla_data(kv), len);
12354 strlcat(buf, " ", len);
12361 void devlink_compat_running_version(struct devlink *devlink,
12362 char *buf, size_t len)
12364 if (!devlink->ops->info_get)
12367 devl_lock(devlink);
12368 __devlink_compat_running_version(devlink, buf, len);
12369 devl_unlock(devlink);
12372 int devlink_compat_flash_update(struct devlink *devlink, const char *file_name)
12374 struct devlink_flash_update_params params = {};
12377 if (!devlink->ops->flash_update)
12378 return -EOPNOTSUPP;
12380 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
12384 devl_lock(devlink);
12385 devlink_flash_update_begin_notify(devlink);
12386 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
12387 devlink_flash_update_end_notify(devlink);
12388 devl_unlock(devlink);
12390 release_firmware(params.fw);
12395 int devlink_compat_phys_port_name_get(struct net_device *dev,
12396 char *name, size_t len)
12398 struct devlink_port *devlink_port;
12400 /* RTNL mutex is held here which ensures that devlink_port
12401 * instance cannot disappear in the middle. No need to take
12402 * any devlink lock as only permanent values are accessed.
12406 devlink_port = dev->devlink_port;
12408 return -EOPNOTSUPP;
12410 return __devlink_port_phys_port_name_get(devlink_port, name, len);
12413 int devlink_compat_switch_id_get(struct net_device *dev,
12414 struct netdev_phys_item_id *ppid)
12416 struct devlink_port *devlink_port;
12418 /* Caller must hold RTNL mutex or reference to dev, which ensures that
12419 * devlink_port instance cannot disappear in the middle. No need to take
12420 * any devlink lock as only permanent values are accessed.
12422 devlink_port = dev->devlink_port;
12423 if (!devlink_port || !devlink_port->switch_port)
12424 return -EOPNOTSUPP;
12426 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));