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/kernel.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/gfp.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
17 #include <linux/netdevice.h>
18 #include <linux/spinlock.h>
19 #include <linux/refcount.h>
20 #include <linux/workqueue.h>
21 #include <linux/u64_stats_sync.h>
22 #include <linux/timekeeping.h>
23 #include <rdma/ib_verbs.h>
24 #include <net/netlink.h>
25 #include <net/genetlink.h>
26 #include <net/rtnetlink.h>
27 #include <net/net_namespace.h>
29 #include <net/devlink.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/devlink.h>
33 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
35 .name = "destination mac",
36 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
41 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
43 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
44 .fields = devlink_dpipe_fields_ethernet,
45 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
48 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
50 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
52 .name = "destination ip",
53 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
58 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
60 .id = DEVLINK_DPIPE_HEADER_IPV4,
61 .fields = devlink_dpipe_fields_ipv4,
62 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
65 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
67 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
69 .name = "destination ip",
70 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
75 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
77 .id = DEVLINK_DPIPE_HEADER_IPV6,
78 .fields = devlink_dpipe_fields_ipv6,
79 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
82 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
84 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
85 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
86 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
88 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = {
89 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY },
90 [DEVLINK_PORT_FN_ATTR_STATE] =
91 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE,
92 DEVLINK_PORT_FN_STATE_ACTIVE),
95 static DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
96 #define DEVLINK_REGISTERED XA_MARK_1
100 * An overall lock guarding every operation coming from userspace.
101 * It also guards devlink devices list and it is taken when
102 * driver registers/unregisters it.
104 static DEFINE_MUTEX(devlink_mutex);
106 struct net *devlink_net(const struct devlink *devlink)
108 return read_pnet(&devlink->_net);
110 EXPORT_SYMBOL_GPL(devlink_net);
112 static void devlink_put(struct devlink *devlink)
114 if (refcount_dec_and_test(&devlink->refcount))
115 complete(&devlink->comp);
118 static bool __must_check devlink_try_get(struct devlink *devlink)
120 return refcount_inc_not_zero(&devlink->refcount);
123 static struct devlink *devlink_get_from_attrs(struct net *net,
124 struct nlattr **attrs)
126 struct devlink *devlink;
132 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
133 return ERR_PTR(-EINVAL);
135 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
136 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
138 lockdep_assert_held(&devlink_mutex);
140 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
141 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
142 strcmp(dev_name(devlink->dev), devname) == 0 &&
143 net_eq(devlink_net(devlink), net)) {
149 if (!found || !devlink_try_get(devlink))
150 devlink = ERR_PTR(-ENODEV);
155 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
156 unsigned int port_index)
158 struct devlink_port *devlink_port;
160 list_for_each_entry(devlink_port, &devlink->port_list, list) {
161 if (devlink_port->index == port_index)
167 static bool devlink_port_index_exists(struct devlink *devlink,
168 unsigned int port_index)
170 return devlink_port_get_by_index(devlink, port_index);
173 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
174 struct nlattr **attrs)
176 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
177 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
178 struct devlink_port *devlink_port;
180 devlink_port = devlink_port_get_by_index(devlink, port_index);
182 return ERR_PTR(-ENODEV);
185 return ERR_PTR(-EINVAL);
188 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
189 struct genl_info *info)
191 return devlink_port_get_from_attrs(devlink, info->attrs);
195 devlink_rate_is_leaf(struct devlink_rate *devlink_rate)
197 return devlink_rate->type == DEVLINK_RATE_TYPE_LEAF;
201 devlink_rate_is_node(struct devlink_rate *devlink_rate)
203 return devlink_rate->type == DEVLINK_RATE_TYPE_NODE;
206 static struct devlink_rate *
207 devlink_rate_leaf_get_from_info(struct devlink *devlink, struct genl_info *info)
209 struct devlink_rate *devlink_rate;
210 struct devlink_port *devlink_port;
212 devlink_port = devlink_port_get_from_attrs(devlink, info->attrs);
213 if (IS_ERR(devlink_port))
214 return ERR_CAST(devlink_port);
215 devlink_rate = devlink_port->devlink_rate;
216 return devlink_rate ?: ERR_PTR(-ENODEV);
219 static struct devlink_rate *
220 devlink_rate_node_get_by_name(struct devlink *devlink, const char *node_name)
222 static struct devlink_rate *devlink_rate;
224 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
225 if (devlink_rate_is_node(devlink_rate) &&
226 !strcmp(node_name, devlink_rate->name))
229 return ERR_PTR(-ENODEV);
232 static struct devlink_rate *
233 devlink_rate_node_get_from_attrs(struct devlink *devlink, struct nlattr **attrs)
235 const char *rate_node_name;
238 if (!attrs[DEVLINK_ATTR_RATE_NODE_NAME])
239 return ERR_PTR(-EINVAL);
240 rate_node_name = nla_data(attrs[DEVLINK_ATTR_RATE_NODE_NAME]);
241 len = strlen(rate_node_name);
242 /* Name cannot be empty or decimal number */
243 if (!len || strspn(rate_node_name, "0123456789") == len)
244 return ERR_PTR(-EINVAL);
246 return devlink_rate_node_get_by_name(devlink, rate_node_name);
249 static struct devlink_rate *
250 devlink_rate_node_get_from_info(struct devlink *devlink, struct genl_info *info)
252 return devlink_rate_node_get_from_attrs(devlink, info->attrs);
255 static struct devlink_rate *
256 devlink_rate_get_from_info(struct devlink *devlink, struct genl_info *info)
258 struct nlattr **attrs = info->attrs;
260 if (attrs[DEVLINK_ATTR_PORT_INDEX])
261 return devlink_rate_leaf_get_from_info(devlink, info);
262 else if (attrs[DEVLINK_ATTR_RATE_NODE_NAME])
263 return devlink_rate_node_get_from_info(devlink, info);
265 return ERR_PTR(-EINVAL);
269 struct list_head list;
272 u16 ingress_pools_count;
273 u16 egress_pools_count;
274 u16 ingress_tc_count;
278 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
280 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
283 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
284 unsigned int sb_index)
286 struct devlink_sb *devlink_sb;
288 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
289 if (devlink_sb->index == sb_index)
295 static bool devlink_sb_index_exists(struct devlink *devlink,
296 unsigned int sb_index)
298 return devlink_sb_get_by_index(devlink, sb_index);
301 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
302 struct nlattr **attrs)
304 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
305 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
306 struct devlink_sb *devlink_sb;
308 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
310 return ERR_PTR(-ENODEV);
313 return ERR_PTR(-EINVAL);
316 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
317 struct genl_info *info)
319 return devlink_sb_get_from_attrs(devlink, info->attrs);
322 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
323 struct nlattr **attrs,
328 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
331 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
332 if (val >= devlink_sb_pool_count(devlink_sb))
338 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
339 struct genl_info *info,
342 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
347 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
348 enum devlink_sb_pool_type *p_pool_type)
352 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
355 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
356 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
357 val != DEVLINK_SB_POOL_TYPE_EGRESS)
364 devlink_sb_pool_type_get_from_info(struct genl_info *info,
365 enum devlink_sb_pool_type *p_pool_type)
367 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
371 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
372 enum devlink_sb_threshold_type *p_th_type)
376 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
379 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
380 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
381 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
388 devlink_sb_th_type_get_from_info(struct genl_info *info,
389 enum devlink_sb_threshold_type *p_th_type)
391 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
395 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
396 struct nlattr **attrs,
397 enum devlink_sb_pool_type pool_type,
402 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
405 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
406 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
407 val >= devlink_sb->ingress_tc_count)
409 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
410 val >= devlink_sb->egress_tc_count)
417 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
418 struct genl_info *info,
419 enum devlink_sb_pool_type pool_type,
422 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
423 pool_type, p_tc_index);
426 struct devlink_region {
427 struct devlink *devlink;
428 struct devlink_port *port;
429 struct list_head list;
431 const struct devlink_region_ops *ops;
432 const struct devlink_port_region_ops *port_ops;
434 struct list_head snapshot_list;
440 struct devlink_snapshot {
441 struct list_head list;
442 struct devlink_region *region;
447 static struct devlink_region *
448 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
450 struct devlink_region *region;
452 list_for_each_entry(region, &devlink->region_list, list)
453 if (!strcmp(region->ops->name, region_name))
459 static struct devlink_region *
460 devlink_port_region_get_by_name(struct devlink_port *port,
461 const char *region_name)
463 struct devlink_region *region;
465 list_for_each_entry(region, &port->region_list, list)
466 if (!strcmp(region->ops->name, region_name))
472 static struct devlink_snapshot *
473 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
475 struct devlink_snapshot *snapshot;
477 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
478 if (snapshot->id == id)
484 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
485 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
486 #define DEVLINK_NL_FLAG_NEED_RATE BIT(2)
487 #define DEVLINK_NL_FLAG_NEED_RATE_NODE BIT(3)
489 /* The per devlink instance lock is taken by default in the pre-doit
490 * operation, yet several commands do not require this. The global
491 * devlink lock is taken and protects from disruption by user-calls.
493 #define DEVLINK_NL_FLAG_NO_LOCK BIT(4)
495 static int devlink_nl_pre_doit(const struct genl_ops *ops,
496 struct sk_buff *skb, struct genl_info *info)
498 struct devlink_port *devlink_port;
499 struct devlink *devlink;
502 mutex_lock(&devlink_mutex);
503 devlink = devlink_get_from_attrs(genl_info_net(info), info->attrs);
504 if (IS_ERR(devlink)) {
505 mutex_unlock(&devlink_mutex);
506 return PTR_ERR(devlink);
508 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
509 mutex_lock(&devlink->lock);
510 info->user_ptr[0] = devlink;
511 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
512 devlink_port = devlink_port_get_from_info(devlink, info);
513 if (IS_ERR(devlink_port)) {
514 err = PTR_ERR(devlink_port);
517 info->user_ptr[1] = devlink_port;
518 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
519 devlink_port = devlink_port_get_from_info(devlink, info);
520 if (!IS_ERR(devlink_port))
521 info->user_ptr[1] = devlink_port;
522 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE) {
523 struct devlink_rate *devlink_rate;
525 devlink_rate = devlink_rate_get_from_info(devlink, info);
526 if (IS_ERR(devlink_rate)) {
527 err = PTR_ERR(devlink_rate);
530 info->user_ptr[1] = devlink_rate;
531 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_RATE_NODE) {
532 struct devlink_rate *rate_node;
534 rate_node = devlink_rate_node_get_from_info(devlink, info);
535 if (IS_ERR(rate_node)) {
536 err = PTR_ERR(rate_node);
539 info->user_ptr[1] = rate_node;
544 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
545 mutex_unlock(&devlink->lock);
546 devlink_put(devlink);
547 mutex_unlock(&devlink_mutex);
551 static void devlink_nl_post_doit(const struct genl_ops *ops,
552 struct sk_buff *skb, struct genl_info *info)
554 struct devlink *devlink;
556 devlink = info->user_ptr[0];
557 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
558 mutex_unlock(&devlink->lock);
559 devlink_put(devlink);
560 mutex_unlock(&devlink_mutex);
563 static struct genl_family devlink_nl_family;
565 enum devlink_multicast_groups {
566 DEVLINK_MCGRP_CONFIG,
569 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
570 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
573 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
575 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
577 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
582 struct devlink_reload_combination {
583 enum devlink_reload_action action;
584 enum devlink_reload_limit limit;
587 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
589 /* can't reinitialize driver with no down time */
590 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
591 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
596 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
597 enum devlink_reload_limit limit)
601 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
602 if (devlink_reload_invalid_combinations[i].action == action &&
603 devlink_reload_invalid_combinations[i].limit == limit)
609 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
611 return test_bit(action, &devlink->ops->reload_actions);
615 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
617 return test_bit(limit, &devlink->ops->reload_limits);
620 static int devlink_reload_stat_put(struct sk_buff *msg,
621 enum devlink_reload_limit limit, u32 value)
623 struct nlattr *reload_stats_entry;
625 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
626 if (!reload_stats_entry)
629 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
630 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
631 goto nla_put_failure;
632 nla_nest_end(msg, reload_stats_entry);
636 nla_nest_cancel(msg, reload_stats_entry);
640 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
642 struct nlattr *reload_stats_attr, *act_info, *act_stats;
647 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
649 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
651 if (!reload_stats_attr)
654 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
656 !devlink_reload_action_is_supported(devlink, i)) ||
657 i == DEVLINK_RELOAD_ACTION_UNSPEC)
659 act_info = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_INFO);
661 goto nla_put_failure;
663 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, i))
664 goto action_info_nest_cancel;
665 act_stats = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_ACTION_STATS);
667 goto action_info_nest_cancel;
669 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
670 /* Remote stats are shown even if not locally supported.
671 * Stats of actions with unspecified limit are shown
672 * though drivers don't need to register unspecified
675 if ((!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
676 !devlink_reload_limit_is_supported(devlink, j)) ||
677 devlink_reload_combination_is_invalid(i, j))
680 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
682 value = devlink->stats.reload_stats[stat_idx];
684 value = devlink->stats.remote_reload_stats[stat_idx];
685 if (devlink_reload_stat_put(msg, j, value))
686 goto action_stats_nest_cancel;
688 nla_nest_end(msg, act_stats);
689 nla_nest_end(msg, act_info);
691 nla_nest_end(msg, reload_stats_attr);
694 action_stats_nest_cancel:
695 nla_nest_cancel(msg, act_stats);
696 action_info_nest_cancel:
697 nla_nest_cancel(msg, act_info);
699 nla_nest_cancel(msg, reload_stats_attr);
703 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
704 enum devlink_command cmd, u32 portid,
707 struct nlattr *dev_stats;
710 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
714 if (devlink_nl_put_handle(msg, devlink))
715 goto nla_put_failure;
716 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
717 goto nla_put_failure;
719 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
721 goto nla_put_failure;
723 if (devlink_reload_stats_put(msg, devlink, false))
724 goto dev_stats_nest_cancel;
725 if (devlink_reload_stats_put(msg, devlink, true))
726 goto dev_stats_nest_cancel;
728 nla_nest_end(msg, dev_stats);
729 genlmsg_end(msg, hdr);
732 dev_stats_nest_cancel:
733 nla_nest_cancel(msg, dev_stats);
735 genlmsg_cancel(msg, hdr);
739 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
744 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
750 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
756 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
757 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
760 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
761 struct devlink_port *devlink_port)
763 struct devlink_port_attrs *attrs = &devlink_port->attrs;
765 if (!devlink_port->attrs_set)
768 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
771 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
773 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
775 switch (devlink_port->attrs.flavour) {
776 case DEVLINK_PORT_FLAVOUR_PCI_PF:
777 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
778 attrs->pci_pf.controller) ||
779 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
781 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
784 case DEVLINK_PORT_FLAVOUR_PCI_VF:
785 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
786 attrs->pci_vf.controller) ||
787 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
788 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
790 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
793 case DEVLINK_PORT_FLAVOUR_PCI_SF:
794 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
795 attrs->pci_sf.controller) ||
796 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER,
798 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
802 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
803 case DEVLINK_PORT_FLAVOUR_CPU:
804 case DEVLINK_PORT_FLAVOUR_DSA:
805 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
806 attrs->phys.port_number))
810 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
811 attrs->phys.port_number))
813 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
814 attrs->phys.split_subport_number))
823 static int devlink_port_fn_hw_addr_fill(const struct devlink_ops *ops,
824 struct devlink_port *port,
826 struct netlink_ext_ack *extack,
829 u8 hw_addr[MAX_ADDR_LEN];
833 if (!ops->port_function_hw_addr_get)
836 err = ops->port_function_hw_addr_get(port, hw_addr, &hw_addr_len,
839 if (err == -EOPNOTSUPP)
843 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
850 static int devlink_nl_rate_fill(struct sk_buff *msg,
851 struct devlink_rate *devlink_rate,
852 enum devlink_command cmd, u32 portid, u32 seq,
853 int flags, struct netlink_ext_ack *extack)
855 struct devlink *devlink = devlink_rate->devlink;
858 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
862 if (devlink_nl_put_handle(msg, devlink))
863 goto nla_put_failure;
865 if (nla_put_u16(msg, DEVLINK_ATTR_RATE_TYPE, devlink_rate->type))
866 goto nla_put_failure;
868 if (devlink_rate_is_leaf(devlink_rate)) {
869 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
870 devlink_rate->devlink_port->index))
871 goto nla_put_failure;
872 } else if (devlink_rate_is_node(devlink_rate)) {
873 if (nla_put_string(msg, DEVLINK_ATTR_RATE_NODE_NAME,
875 goto nla_put_failure;
878 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_SHARE,
879 devlink_rate->tx_share, DEVLINK_ATTR_PAD))
880 goto nla_put_failure;
882 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_RATE_TX_MAX,
883 devlink_rate->tx_max, DEVLINK_ATTR_PAD))
884 goto nla_put_failure;
886 if (devlink_rate->parent)
887 if (nla_put_string(msg, DEVLINK_ATTR_RATE_PARENT_NODE_NAME,
888 devlink_rate->parent->name))
889 goto nla_put_failure;
891 genlmsg_end(msg, hdr);
895 genlmsg_cancel(msg, hdr);
900 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
902 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
903 state == DEVLINK_PORT_FN_STATE_ACTIVE;
907 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
909 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
910 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
913 static int devlink_port_fn_state_fill(const struct devlink_ops *ops,
914 struct devlink_port *port,
916 struct netlink_ext_ack *extack,
919 enum devlink_port_fn_opstate opstate;
920 enum devlink_port_fn_state state;
923 if (!ops->port_fn_state_get)
926 err = ops->port_fn_state_get(port, &state, &opstate, extack);
928 if (err == -EOPNOTSUPP)
932 if (!devlink_port_fn_state_valid(state)) {
934 NL_SET_ERR_MSG_MOD(extack, "Invalid state read from driver");
937 if (!devlink_port_fn_opstate_valid(opstate)) {
939 NL_SET_ERR_MSG_MOD(extack,
940 "Invalid operational state read from driver");
943 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
944 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
951 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
952 struct netlink_ext_ack *extack)
954 const struct devlink_ops *ops;
955 struct nlattr *function_attr;
956 bool msg_updated = false;
959 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
963 ops = port->devlink->ops;
964 err = devlink_port_fn_hw_addr_fill(ops, port, msg, extack,
968 err = devlink_port_fn_state_fill(ops, port, msg, extack, &msg_updated);
970 if (err || !msg_updated)
971 nla_nest_cancel(msg, function_attr);
973 nla_nest_end(msg, function_attr);
977 static int devlink_nl_port_fill(struct sk_buff *msg,
978 struct devlink_port *devlink_port,
979 enum devlink_command cmd, u32 portid, u32 seq,
980 int flags, struct netlink_ext_ack *extack)
982 struct devlink *devlink = devlink_port->devlink;
985 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
989 if (devlink_nl_put_handle(msg, devlink))
990 goto nla_put_failure;
991 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
992 goto nla_put_failure;
994 /* Hold rtnl lock while accessing port's netdev attributes. */
996 spin_lock_bh(&devlink_port->type_lock);
997 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
998 goto nla_put_failure_type_locked;
999 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
1000 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
1001 devlink_port->desired_type))
1002 goto nla_put_failure_type_locked;
1003 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
1004 struct net *net = devlink_net(devlink_port->devlink);
1005 struct net_device *netdev = devlink_port->type_dev;
1007 if (netdev && net_eq(net, dev_net(netdev)) &&
1008 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
1010 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
1012 goto nla_put_failure_type_locked;
1014 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
1015 struct ib_device *ibdev = devlink_port->type_dev;
1018 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
1020 goto nla_put_failure_type_locked;
1022 spin_unlock_bh(&devlink_port->type_lock);
1024 if (devlink_nl_port_attrs_put(msg, devlink_port))
1025 goto nla_put_failure;
1026 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
1027 goto nla_put_failure;
1029 genlmsg_end(msg, hdr);
1032 nla_put_failure_type_locked:
1033 spin_unlock_bh(&devlink_port->type_lock);
1036 genlmsg_cancel(msg, hdr);
1040 static void devlink_port_notify(struct devlink_port *devlink_port,
1041 enum devlink_command cmd)
1043 struct sk_buff *msg;
1046 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
1048 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1052 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
1058 genlmsg_multicast_netns(&devlink_nl_family,
1059 devlink_net(devlink_port->devlink), msg, 0,
1060 DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1063 static void devlink_rate_notify(struct devlink_rate *devlink_rate,
1064 enum devlink_command cmd)
1066 struct sk_buff *msg;
1069 WARN_ON(cmd != DEVLINK_CMD_RATE_NEW && cmd != DEVLINK_CMD_RATE_DEL);
1071 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1075 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, 0, 0, 0, NULL);
1081 genlmsg_multicast_netns(&devlink_nl_family,
1082 devlink_net(devlink_rate->devlink), msg, 0,
1083 DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
1086 static int devlink_nl_cmd_rate_get_dumpit(struct sk_buff *msg,
1087 struct netlink_callback *cb)
1089 struct devlink_rate *devlink_rate;
1090 struct devlink *devlink;
1091 int start = cb->args[0];
1092 unsigned long index;
1096 mutex_lock(&devlink_mutex);
1097 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1098 if (!devlink_try_get(devlink))
1101 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1104 mutex_lock(&devlink->lock);
1105 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
1106 enum devlink_command cmd = DEVLINK_CMD_RATE_NEW;
1107 u32 id = NETLINK_CB(cb->skb).portid;
1113 err = devlink_nl_rate_fill(msg, devlink_rate, cmd, id,
1117 mutex_unlock(&devlink->lock);
1118 devlink_put(devlink);
1123 mutex_unlock(&devlink->lock);
1125 devlink_put(devlink);
1128 mutex_unlock(&devlink_mutex);
1129 if (err != -EMSGSIZE)
1136 static int devlink_nl_cmd_rate_get_doit(struct sk_buff *skb,
1137 struct genl_info *info)
1139 struct devlink_rate *devlink_rate = info->user_ptr[1];
1140 struct sk_buff *msg;
1143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1147 err = devlink_nl_rate_fill(msg, devlink_rate, DEVLINK_CMD_RATE_NEW,
1148 info->snd_portid, info->snd_seq, 0,
1155 return genlmsg_reply(msg, info);
1159 devlink_rate_is_parent_node(struct devlink_rate *devlink_rate,
1160 struct devlink_rate *parent)
1163 if (parent == devlink_rate)
1165 parent = parent->parent;
1170 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
1172 struct devlink *devlink = info->user_ptr[0];
1173 struct sk_buff *msg;
1176 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1180 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1181 info->snd_portid, info->snd_seq, 0);
1187 return genlmsg_reply(msg, info);
1190 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
1191 struct netlink_callback *cb)
1193 struct devlink *devlink;
1194 int start = cb->args[0];
1195 unsigned long index;
1199 mutex_lock(&devlink_mutex);
1200 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1201 if (!devlink_try_get(devlink))
1204 if (!net_eq(devlink_net(devlink), sock_net(msg->sk))) {
1205 devlink_put(devlink);
1211 devlink_put(devlink);
1215 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
1216 NETLINK_CB(cb->skb).portid,
1217 cb->nlh->nlmsg_seq, NLM_F_MULTI);
1218 devlink_put(devlink);
1224 mutex_unlock(&devlink_mutex);
1230 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
1231 struct genl_info *info)
1233 struct devlink_port *devlink_port = info->user_ptr[1];
1234 struct sk_buff *msg;
1237 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1241 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
1242 info->snd_portid, info->snd_seq, 0,
1249 return genlmsg_reply(msg, info);
1252 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
1253 struct netlink_callback *cb)
1255 struct devlink *devlink;
1256 struct devlink_port *devlink_port;
1257 int start = cb->args[0];
1258 unsigned long index;
1262 mutex_lock(&devlink_mutex);
1263 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1264 if (!devlink_try_get(devlink))
1267 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1270 mutex_lock(&devlink->lock);
1271 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1276 err = devlink_nl_port_fill(msg, devlink_port,
1278 NETLINK_CB(cb->skb).portid,
1280 NLM_F_MULTI, cb->extack);
1282 mutex_unlock(&devlink->lock);
1283 devlink_put(devlink);
1288 mutex_unlock(&devlink->lock);
1290 devlink_put(devlink);
1293 mutex_unlock(&devlink_mutex);
1299 static int devlink_port_type_set(struct devlink_port *devlink_port,
1300 enum devlink_port_type port_type)
1305 if (!devlink_port->devlink->ops->port_type_set)
1308 if (port_type == devlink_port->type)
1311 err = devlink_port->devlink->ops->port_type_set(devlink_port,
1316 devlink_port->desired_type = port_type;
1317 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1321 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
1322 const struct nlattr *attr,
1323 struct netlink_ext_ack *extack)
1325 const struct devlink_ops *ops = port->devlink->ops;
1329 hw_addr = nla_data(attr);
1330 hw_addr_len = nla_len(attr);
1331 if (hw_addr_len > MAX_ADDR_LEN) {
1332 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
1335 if (port->type == DEVLINK_PORT_TYPE_ETH) {
1336 if (hw_addr_len != ETH_ALEN) {
1337 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
1340 if (!is_unicast_ether_addr(hw_addr)) {
1341 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
1346 if (!ops->port_function_hw_addr_set) {
1347 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
1351 return ops->port_function_hw_addr_set(port, hw_addr, hw_addr_len,
1355 static int devlink_port_fn_state_set(struct devlink_port *port,
1356 const struct nlattr *attr,
1357 struct netlink_ext_ack *extack)
1359 enum devlink_port_fn_state state;
1360 const struct devlink_ops *ops;
1362 state = nla_get_u8(attr);
1363 ops = port->devlink->ops;
1364 if (!ops->port_fn_state_set) {
1365 NL_SET_ERR_MSG_MOD(extack,
1366 "Function does not support state setting");
1369 return ops->port_fn_state_set(port, state, extack);
1372 static int devlink_port_function_set(struct devlink_port *port,
1373 const struct nlattr *attr,
1374 struct netlink_ext_ack *extack)
1376 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1379 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1380 devlink_function_nl_policy, extack);
1382 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1386 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1388 err = devlink_port_function_hw_addr_set(port, attr, extack);
1392 /* Keep this as the last function attribute set, so that when
1393 * multiple port function attributes are set along with state,
1394 * Those can be applied first before activating the state.
1396 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
1398 err = devlink_port_fn_state_set(port, attr, extack);
1401 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1405 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1406 struct genl_info *info)
1408 struct devlink_port *devlink_port = info->user_ptr[1];
1411 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1412 enum devlink_port_type port_type;
1414 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1415 err = devlink_port_type_set(devlink_port, port_type);
1420 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1421 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1422 struct netlink_ext_ack *extack = info->extack;
1424 err = devlink_port_function_set(devlink_port, attr, extack);
1432 static int devlink_port_split(struct devlink *devlink, u32 port_index,
1433 u32 count, struct netlink_ext_ack *extack)
1436 if (devlink->ops->port_split)
1437 return devlink->ops->port_split(devlink, port_index, count,
1442 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1443 struct genl_info *info)
1445 struct devlink *devlink = info->user_ptr[0];
1446 struct devlink_port *devlink_port;
1450 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
1451 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
1454 devlink_port = devlink_port_get_from_info(devlink, info);
1455 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1456 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1458 if (IS_ERR(devlink_port))
1461 if (!devlink_port->attrs.splittable) {
1462 /* Split ports cannot be split. */
1463 if (devlink_port->attrs.split)
1464 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1466 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1470 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1471 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1475 return devlink_port_split(devlink, port_index, count, info->extack);
1478 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
1479 struct netlink_ext_ack *extack)
1482 if (devlink->ops->port_unsplit)
1483 return devlink->ops->port_unsplit(devlink, port_index, extack);
1487 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1488 struct genl_info *info)
1490 struct devlink *devlink = info->user_ptr[0];
1493 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
1496 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1497 return devlink_port_unsplit(devlink, port_index, info->extack);
1500 static int devlink_port_new_notifiy(struct devlink *devlink,
1501 unsigned int port_index,
1502 struct genl_info *info)
1504 struct devlink_port *devlink_port;
1505 struct sk_buff *msg;
1508 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1512 mutex_lock(&devlink->lock);
1513 devlink_port = devlink_port_get_by_index(devlink, port_index);
1514 if (!devlink_port) {
1519 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW,
1520 info->snd_portid, info->snd_seq, 0, NULL);
1524 err = genlmsg_reply(msg, info);
1525 mutex_unlock(&devlink->lock);
1529 mutex_unlock(&devlink->lock);
1534 static int devlink_nl_cmd_port_new_doit(struct sk_buff *skb,
1535 struct genl_info *info)
1537 struct netlink_ext_ack *extack = info->extack;
1538 struct devlink_port_new_attrs new_attrs = {};
1539 struct devlink *devlink = info->user_ptr[0];
1540 unsigned int new_port_index;
1543 if (!devlink->ops->port_new || !devlink->ops->port_del)
1546 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] ||
1547 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) {
1548 NL_SET_ERR_MSG_MOD(extack, "Port flavour or PCI PF are not specified");
1551 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
1553 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
1555 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1556 /* Port index of the new port being created by driver. */
1557 new_attrs.port_index =
1558 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1559 new_attrs.port_index_valid = true;
1561 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) {
1562 new_attrs.controller =
1563 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]);
1564 new_attrs.controller_valid = true;
1566 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF &&
1567 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) {
1568 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]);
1569 new_attrs.sfnum_valid = true;
1572 err = devlink->ops->port_new(devlink, &new_attrs, extack,
1577 err = devlink_port_new_notifiy(devlink, new_port_index, info);
1578 if (err && err != -ENODEV) {
1579 /* Fail to send the response; destroy newly created port. */
1580 devlink->ops->port_del(devlink, new_port_index, extack);
1585 static int devlink_nl_cmd_port_del_doit(struct sk_buff *skb,
1586 struct genl_info *info)
1588 struct netlink_ext_ack *extack = info->extack;
1589 struct devlink *devlink = info->user_ptr[0];
1590 unsigned int port_index;
1592 if (!devlink->ops->port_del)
1595 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
1596 NL_SET_ERR_MSG_MOD(extack, "Port index is not specified");
1599 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1601 return devlink->ops->port_del(devlink, port_index, extack);
1605 devlink_nl_rate_parent_node_set(struct devlink_rate *devlink_rate,
1606 struct genl_info *info,
1607 struct nlattr *nla_parent)
1609 struct devlink *devlink = devlink_rate->devlink;
1610 const char *parent_name = nla_data(nla_parent);
1611 const struct devlink_ops *ops = devlink->ops;
1612 size_t len = strlen(parent_name);
1613 struct devlink_rate *parent;
1614 int err = -EOPNOTSUPP;
1616 parent = devlink_rate->parent;
1617 if (parent && len) {
1618 NL_SET_ERR_MSG_MOD(info->extack, "Rate object already has parent.");
1620 } else if (parent && !len) {
1621 if (devlink_rate_is_leaf(devlink_rate))
1622 err = ops->rate_leaf_parent_set(devlink_rate, NULL,
1623 devlink_rate->priv, NULL,
1625 else if (devlink_rate_is_node(devlink_rate))
1626 err = ops->rate_node_parent_set(devlink_rate, NULL,
1627 devlink_rate->priv, NULL,
1632 refcount_dec(&parent->refcnt);
1633 devlink_rate->parent = NULL;
1634 } else if (!parent && len) {
1635 parent = devlink_rate_node_get_by_name(devlink, parent_name);
1639 if (parent == devlink_rate) {
1640 NL_SET_ERR_MSG_MOD(info->extack, "Parent to self is not allowed");
1644 if (devlink_rate_is_node(devlink_rate) &&
1645 devlink_rate_is_parent_node(devlink_rate, parent->parent)) {
1646 NL_SET_ERR_MSG_MOD(info->extack, "Node is already a parent of parent node.");
1650 if (devlink_rate_is_leaf(devlink_rate))
1651 err = ops->rate_leaf_parent_set(devlink_rate, parent,
1652 devlink_rate->priv, parent->priv,
1654 else if (devlink_rate_is_node(devlink_rate))
1655 err = ops->rate_node_parent_set(devlink_rate, parent,
1656 devlink_rate->priv, parent->priv,
1661 refcount_inc(&parent->refcnt);
1662 devlink_rate->parent = parent;
1668 static int devlink_nl_rate_set(struct devlink_rate *devlink_rate,
1669 const struct devlink_ops *ops,
1670 struct genl_info *info)
1672 struct nlattr *nla_parent, **attrs = info->attrs;
1673 int err = -EOPNOTSUPP;
1676 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE]) {
1677 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_SHARE]);
1678 if (devlink_rate_is_leaf(devlink_rate))
1679 err = ops->rate_leaf_tx_share_set(devlink_rate, devlink_rate->priv,
1680 rate, info->extack);
1681 else if (devlink_rate_is_node(devlink_rate))
1682 err = ops->rate_node_tx_share_set(devlink_rate, devlink_rate->priv,
1683 rate, info->extack);
1686 devlink_rate->tx_share = rate;
1689 if (attrs[DEVLINK_ATTR_RATE_TX_MAX]) {
1690 rate = nla_get_u64(attrs[DEVLINK_ATTR_RATE_TX_MAX]);
1691 if (devlink_rate_is_leaf(devlink_rate))
1692 err = ops->rate_leaf_tx_max_set(devlink_rate, devlink_rate->priv,
1693 rate, info->extack);
1694 else if (devlink_rate_is_node(devlink_rate))
1695 err = ops->rate_node_tx_max_set(devlink_rate, devlink_rate->priv,
1696 rate, info->extack);
1699 devlink_rate->tx_max = rate;
1702 nla_parent = attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME];
1704 err = devlink_nl_rate_parent_node_set(devlink_rate, info,
1713 static bool devlink_rate_set_ops_supported(const struct devlink_ops *ops,
1714 struct genl_info *info,
1715 enum devlink_rate_type type)
1717 struct nlattr **attrs = info->attrs;
1719 if (type == DEVLINK_RATE_TYPE_LEAF) {
1720 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_leaf_tx_share_set) {
1721 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the leafs");
1724 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_leaf_tx_max_set) {
1725 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the leafs");
1728 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1729 !ops->rate_leaf_parent_set) {
1730 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the leafs");
1733 } else if (type == DEVLINK_RATE_TYPE_NODE) {
1734 if (attrs[DEVLINK_ATTR_RATE_TX_SHARE] && !ops->rate_node_tx_share_set) {
1735 NL_SET_ERR_MSG_MOD(info->extack, "TX share set isn't supported for the nodes");
1738 if (attrs[DEVLINK_ATTR_RATE_TX_MAX] && !ops->rate_node_tx_max_set) {
1739 NL_SET_ERR_MSG_MOD(info->extack, "TX max set isn't supported for the nodes");
1742 if (attrs[DEVLINK_ATTR_RATE_PARENT_NODE_NAME] &&
1743 !ops->rate_node_parent_set) {
1744 NL_SET_ERR_MSG_MOD(info->extack, "Parent set isn't supported for the nodes");
1748 WARN(1, "Unknown type of rate object");
1755 static int devlink_nl_cmd_rate_set_doit(struct sk_buff *skb,
1756 struct genl_info *info)
1758 struct devlink_rate *devlink_rate = info->user_ptr[1];
1759 struct devlink *devlink = devlink_rate->devlink;
1760 const struct devlink_ops *ops = devlink->ops;
1763 if (!ops || !devlink_rate_set_ops_supported(ops, info, devlink_rate->type))
1766 err = devlink_nl_rate_set(devlink_rate, ops, info);
1769 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
1773 static int devlink_nl_cmd_rate_new_doit(struct sk_buff *skb,
1774 struct genl_info *info)
1776 struct devlink *devlink = info->user_ptr[0];
1777 struct devlink_rate *rate_node;
1778 const struct devlink_ops *ops;
1782 if (!ops || !ops->rate_node_new || !ops->rate_node_del) {
1783 NL_SET_ERR_MSG_MOD(info->extack, "Rate nodes aren't supported");
1787 if (!devlink_rate_set_ops_supported(ops, info, DEVLINK_RATE_TYPE_NODE))
1790 rate_node = devlink_rate_node_get_from_attrs(devlink, info->attrs);
1791 if (!IS_ERR(rate_node))
1793 else if (rate_node == ERR_PTR(-EINVAL))
1796 rate_node = kzalloc(sizeof(*rate_node), GFP_KERNEL);
1800 rate_node->devlink = devlink;
1801 rate_node->type = DEVLINK_RATE_TYPE_NODE;
1802 rate_node->name = nla_strdup(info->attrs[DEVLINK_ATTR_RATE_NODE_NAME], GFP_KERNEL);
1803 if (!rate_node->name) {
1808 err = ops->rate_node_new(rate_node, &rate_node->priv, info->extack);
1812 err = devlink_nl_rate_set(rate_node, ops, info);
1816 refcount_set(&rate_node->refcnt, 1);
1817 list_add(&rate_node->list, &devlink->rate_list);
1818 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_NEW);
1822 ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1824 kfree(rate_node->name);
1830 static int devlink_nl_cmd_rate_del_doit(struct sk_buff *skb,
1831 struct genl_info *info)
1833 struct devlink_rate *rate_node = info->user_ptr[1];
1834 struct devlink *devlink = rate_node->devlink;
1835 const struct devlink_ops *ops = devlink->ops;
1838 if (refcount_read(&rate_node->refcnt) > 1) {
1839 NL_SET_ERR_MSG_MOD(info->extack, "Node has children. Cannot delete node.");
1843 devlink_rate_notify(rate_node, DEVLINK_CMD_RATE_DEL);
1844 err = ops->rate_node_del(rate_node, rate_node->priv, info->extack);
1845 if (rate_node->parent)
1846 refcount_dec(&rate_node->parent->refcnt);
1847 list_del(&rate_node->list);
1848 kfree(rate_node->name);
1853 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
1854 struct devlink_sb *devlink_sb,
1855 enum devlink_command cmd, u32 portid,
1860 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1864 if (devlink_nl_put_handle(msg, devlink))
1865 goto nla_put_failure;
1866 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1867 goto nla_put_failure;
1868 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
1869 goto nla_put_failure;
1870 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
1871 devlink_sb->ingress_pools_count))
1872 goto nla_put_failure;
1873 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
1874 devlink_sb->egress_pools_count))
1875 goto nla_put_failure;
1876 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
1877 devlink_sb->ingress_tc_count))
1878 goto nla_put_failure;
1879 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
1880 devlink_sb->egress_tc_count))
1881 goto nla_put_failure;
1883 genlmsg_end(msg, hdr);
1887 genlmsg_cancel(msg, hdr);
1891 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
1892 struct genl_info *info)
1894 struct devlink *devlink = info->user_ptr[0];
1895 struct devlink_sb *devlink_sb;
1896 struct sk_buff *msg;
1899 devlink_sb = devlink_sb_get_from_info(devlink, info);
1900 if (IS_ERR(devlink_sb))
1901 return PTR_ERR(devlink_sb);
1903 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1907 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1909 info->snd_portid, info->snd_seq, 0);
1915 return genlmsg_reply(msg, info);
1918 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
1919 struct netlink_callback *cb)
1921 struct devlink *devlink;
1922 struct devlink_sb *devlink_sb;
1923 int start = cb->args[0];
1924 unsigned long index;
1928 mutex_lock(&devlink_mutex);
1929 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
1930 if (!devlink_try_get(devlink))
1933 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1936 mutex_lock(&devlink->lock);
1937 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1942 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1944 NETLINK_CB(cb->skb).portid,
1948 mutex_unlock(&devlink->lock);
1949 devlink_put(devlink);
1954 mutex_unlock(&devlink->lock);
1956 devlink_put(devlink);
1959 mutex_unlock(&devlink_mutex);
1965 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
1966 struct devlink_sb *devlink_sb,
1967 u16 pool_index, enum devlink_command cmd,
1968 u32 portid, u32 seq, int flags)
1970 struct devlink_sb_pool_info pool_info;
1974 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
1975 pool_index, &pool_info);
1979 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1983 if (devlink_nl_put_handle(msg, devlink))
1984 goto nla_put_failure;
1985 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1986 goto nla_put_failure;
1987 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1988 goto nla_put_failure;
1989 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
1990 goto nla_put_failure;
1991 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
1992 goto nla_put_failure;
1993 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
1994 pool_info.threshold_type))
1995 goto nla_put_failure;
1996 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
1997 pool_info.cell_size))
1998 goto nla_put_failure;
2000 genlmsg_end(msg, hdr);
2004 genlmsg_cancel(msg, hdr);
2008 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
2009 struct genl_info *info)
2011 struct devlink *devlink = info->user_ptr[0];
2012 struct devlink_sb *devlink_sb;
2013 struct sk_buff *msg;
2017 devlink_sb = devlink_sb_get_from_info(devlink, info);
2018 if (IS_ERR(devlink_sb))
2019 return PTR_ERR(devlink_sb);
2021 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2026 if (!devlink->ops->sb_pool_get)
2029 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2033 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
2034 DEVLINK_CMD_SB_POOL_NEW,
2035 info->snd_portid, info->snd_seq, 0);
2041 return genlmsg_reply(msg, info);
2044 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2045 struct devlink *devlink,
2046 struct devlink_sb *devlink_sb,
2047 u32 portid, u32 seq)
2049 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2053 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2054 if (*p_idx < start) {
2058 err = devlink_nl_sb_pool_fill(msg, devlink,
2061 DEVLINK_CMD_SB_POOL_NEW,
2062 portid, seq, NLM_F_MULTI);
2070 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
2071 struct netlink_callback *cb)
2073 struct devlink *devlink;
2074 struct devlink_sb *devlink_sb;
2075 int start = cb->args[0];
2076 unsigned long index;
2080 mutex_lock(&devlink_mutex);
2081 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2082 if (!devlink_try_get(devlink))
2085 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2086 !devlink->ops->sb_pool_get)
2089 mutex_lock(&devlink->lock);
2090 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2091 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
2093 NETLINK_CB(cb->skb).portid,
2094 cb->nlh->nlmsg_seq);
2095 if (err == -EOPNOTSUPP) {
2098 mutex_unlock(&devlink->lock);
2099 devlink_put(devlink);
2103 mutex_unlock(&devlink->lock);
2105 devlink_put(devlink);
2108 mutex_unlock(&devlink_mutex);
2110 if (err != -EMSGSIZE)
2117 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
2118 u16 pool_index, u32 size,
2119 enum devlink_sb_threshold_type threshold_type,
2120 struct netlink_ext_ack *extack)
2123 const struct devlink_ops *ops = devlink->ops;
2125 if (ops->sb_pool_set)
2126 return ops->sb_pool_set(devlink, sb_index, pool_index,
2127 size, threshold_type, extack);
2131 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
2132 struct genl_info *info)
2134 struct devlink *devlink = info->user_ptr[0];
2135 enum devlink_sb_threshold_type threshold_type;
2136 struct devlink_sb *devlink_sb;
2141 devlink_sb = devlink_sb_get_from_info(devlink, info);
2142 if (IS_ERR(devlink_sb))
2143 return PTR_ERR(devlink_sb);
2145 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2150 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
2154 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
2157 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
2158 return devlink_sb_pool_set(devlink, devlink_sb->index,
2159 pool_index, size, threshold_type,
2163 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
2164 struct devlink *devlink,
2165 struct devlink_port *devlink_port,
2166 struct devlink_sb *devlink_sb,
2168 enum devlink_command cmd,
2169 u32 portid, u32 seq, int flags)
2171 const struct devlink_ops *ops = devlink->ops;
2176 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
2177 pool_index, &threshold);
2181 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2185 if (devlink_nl_put_handle(msg, devlink))
2186 goto nla_put_failure;
2187 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2188 goto nla_put_failure;
2189 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2190 goto nla_put_failure;
2191 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2192 goto nla_put_failure;
2193 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2194 goto nla_put_failure;
2196 if (ops->sb_occ_port_pool_get) {
2200 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
2201 pool_index, &cur, &max);
2202 if (err && err != -EOPNOTSUPP)
2203 goto sb_occ_get_failure;
2205 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2206 goto nla_put_failure;
2207 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2208 goto nla_put_failure;
2212 genlmsg_end(msg, hdr);
2218 genlmsg_cancel(msg, hdr);
2222 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
2223 struct genl_info *info)
2225 struct devlink_port *devlink_port = info->user_ptr[1];
2226 struct devlink *devlink = devlink_port->devlink;
2227 struct devlink_sb *devlink_sb;
2228 struct sk_buff *msg;
2232 devlink_sb = devlink_sb_get_from_info(devlink, info);
2233 if (IS_ERR(devlink_sb))
2234 return PTR_ERR(devlink_sb);
2236 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2241 if (!devlink->ops->sb_port_pool_get)
2244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2248 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
2249 devlink_sb, pool_index,
2250 DEVLINK_CMD_SB_PORT_POOL_NEW,
2251 info->snd_portid, info->snd_seq, 0);
2257 return genlmsg_reply(msg, info);
2260 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
2261 struct devlink *devlink,
2262 struct devlink_sb *devlink_sb,
2263 u32 portid, u32 seq)
2265 struct devlink_port *devlink_port;
2266 u16 pool_count = devlink_sb_pool_count(devlink_sb);
2270 list_for_each_entry(devlink_port, &devlink->port_list, list) {
2271 for (pool_index = 0; pool_index < pool_count; pool_index++) {
2272 if (*p_idx < start) {
2276 err = devlink_nl_sb_port_pool_fill(msg, devlink,
2280 DEVLINK_CMD_SB_PORT_POOL_NEW,
2291 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
2292 struct netlink_callback *cb)
2294 struct devlink *devlink;
2295 struct devlink_sb *devlink_sb;
2296 int start = cb->args[0];
2297 unsigned long index;
2301 mutex_lock(&devlink_mutex);
2302 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2303 if (!devlink_try_get(devlink))
2306 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2307 !devlink->ops->sb_port_pool_get)
2310 mutex_lock(&devlink->lock);
2311 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2312 err = __sb_port_pool_get_dumpit(msg, start, &idx,
2313 devlink, devlink_sb,
2314 NETLINK_CB(cb->skb).portid,
2315 cb->nlh->nlmsg_seq);
2316 if (err == -EOPNOTSUPP) {
2319 mutex_unlock(&devlink->lock);
2320 devlink_put(devlink);
2324 mutex_unlock(&devlink->lock);
2326 devlink_put(devlink);
2329 mutex_unlock(&devlink_mutex);
2331 if (err != -EMSGSIZE)
2338 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
2339 unsigned int sb_index, u16 pool_index,
2341 struct netlink_ext_ack *extack)
2344 const struct devlink_ops *ops = devlink_port->devlink->ops;
2346 if (ops->sb_port_pool_set)
2347 return ops->sb_port_pool_set(devlink_port, sb_index,
2348 pool_index, threshold, extack);
2352 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
2353 struct genl_info *info)
2355 struct devlink_port *devlink_port = info->user_ptr[1];
2356 struct devlink *devlink = info->user_ptr[0];
2357 struct devlink_sb *devlink_sb;
2362 devlink_sb = devlink_sb_get_from_info(devlink, info);
2363 if (IS_ERR(devlink_sb))
2364 return PTR_ERR(devlink_sb);
2366 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2371 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
2374 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2375 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
2376 pool_index, threshold, info->extack);
2380 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
2381 struct devlink_port *devlink_port,
2382 struct devlink_sb *devlink_sb, u16 tc_index,
2383 enum devlink_sb_pool_type pool_type,
2384 enum devlink_command cmd,
2385 u32 portid, u32 seq, int flags)
2387 const struct devlink_ops *ops = devlink->ops;
2393 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
2394 tc_index, pool_type,
2395 &pool_index, &threshold);
2399 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2403 if (devlink_nl_put_handle(msg, devlink))
2404 goto nla_put_failure;
2405 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
2406 goto nla_put_failure;
2407 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
2408 goto nla_put_failure;
2409 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
2410 goto nla_put_failure;
2411 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
2412 goto nla_put_failure;
2413 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
2414 goto nla_put_failure;
2415 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
2416 goto nla_put_failure;
2418 if (ops->sb_occ_tc_port_bind_get) {
2422 err = ops->sb_occ_tc_port_bind_get(devlink_port,
2424 tc_index, pool_type,
2426 if (err && err != -EOPNOTSUPP)
2429 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
2430 goto nla_put_failure;
2431 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
2432 goto nla_put_failure;
2436 genlmsg_end(msg, hdr);
2440 genlmsg_cancel(msg, hdr);
2444 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
2445 struct genl_info *info)
2447 struct devlink_port *devlink_port = info->user_ptr[1];
2448 struct devlink *devlink = devlink_port->devlink;
2449 struct devlink_sb *devlink_sb;
2450 struct sk_buff *msg;
2451 enum devlink_sb_pool_type pool_type;
2455 devlink_sb = devlink_sb_get_from_info(devlink, info);
2456 if (IS_ERR(devlink_sb))
2457 return PTR_ERR(devlink_sb);
2459 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2463 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2464 pool_type, &tc_index);
2468 if (!devlink->ops->sb_tc_pool_bind_get)
2471 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2475 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
2476 devlink_sb, tc_index, pool_type,
2477 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2485 return genlmsg_reply(msg, info);
2488 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2489 int start, int *p_idx,
2490 struct devlink *devlink,
2491 struct devlink_sb *devlink_sb,
2492 u32 portid, u32 seq)
2494 struct devlink_port *devlink_port;
2498 list_for_each_entry(devlink_port, &devlink->port_list, list) {
2500 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
2501 if (*p_idx < start) {
2505 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2509 DEVLINK_SB_POOL_TYPE_INGRESS,
2510 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2518 tc_index < devlink_sb->egress_tc_count; tc_index++) {
2519 if (*p_idx < start) {
2523 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
2527 DEVLINK_SB_POOL_TYPE_EGRESS,
2528 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
2540 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
2541 struct netlink_callback *cb)
2543 struct devlink *devlink;
2544 struct devlink_sb *devlink_sb;
2545 int start = cb->args[0];
2546 unsigned long index;
2550 mutex_lock(&devlink_mutex);
2551 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
2552 if (!devlink_try_get(devlink))
2555 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
2556 !devlink->ops->sb_tc_pool_bind_get)
2559 mutex_lock(&devlink->lock);
2560 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
2561 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
2564 NETLINK_CB(cb->skb).portid,
2565 cb->nlh->nlmsg_seq);
2566 if (err == -EOPNOTSUPP) {
2569 mutex_unlock(&devlink->lock);
2570 devlink_put(devlink);
2574 mutex_unlock(&devlink->lock);
2576 devlink_put(devlink);
2579 mutex_unlock(&devlink_mutex);
2581 if (err != -EMSGSIZE)
2588 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
2589 unsigned int sb_index, u16 tc_index,
2590 enum devlink_sb_pool_type pool_type,
2591 u16 pool_index, u32 threshold,
2592 struct netlink_ext_ack *extack)
2595 const struct devlink_ops *ops = devlink_port->devlink->ops;
2597 if (ops->sb_tc_pool_bind_set)
2598 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
2599 tc_index, pool_type,
2600 pool_index, threshold, extack);
2604 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
2605 struct genl_info *info)
2607 struct devlink_port *devlink_port = info->user_ptr[1];
2608 struct devlink *devlink = info->user_ptr[0];
2609 enum devlink_sb_pool_type pool_type;
2610 struct devlink_sb *devlink_sb;
2616 devlink_sb = devlink_sb_get_from_info(devlink, info);
2617 if (IS_ERR(devlink_sb))
2618 return PTR_ERR(devlink_sb);
2620 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
2624 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
2625 pool_type, &tc_index);
2629 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
2634 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
2637 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
2638 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
2639 tc_index, pool_type,
2640 pool_index, threshold, info->extack);
2643 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
2644 struct genl_info *info)
2646 struct devlink *devlink = info->user_ptr[0];
2647 const struct devlink_ops *ops = devlink->ops;
2648 struct devlink_sb *devlink_sb;
2650 devlink_sb = devlink_sb_get_from_info(devlink, info);
2651 if (IS_ERR(devlink_sb))
2652 return PTR_ERR(devlink_sb);
2654 if (ops->sb_occ_snapshot)
2655 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
2659 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
2660 struct genl_info *info)
2662 struct devlink *devlink = info->user_ptr[0];
2663 const struct devlink_ops *ops = devlink->ops;
2664 struct devlink_sb *devlink_sb;
2666 devlink_sb = devlink_sb_get_from_info(devlink, info);
2667 if (IS_ERR(devlink_sb))
2668 return PTR_ERR(devlink_sb);
2670 if (ops->sb_occ_max_clear)
2671 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
2675 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
2676 enum devlink_command cmd, u32 portid,
2679 const struct devlink_ops *ops = devlink->ops;
2680 enum devlink_eswitch_encap_mode encap_mode;
2686 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2690 err = devlink_nl_put_handle(msg, devlink);
2692 goto nla_put_failure;
2694 if (ops->eswitch_mode_get) {
2695 err = ops->eswitch_mode_get(devlink, &mode);
2697 goto nla_put_failure;
2698 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
2700 goto nla_put_failure;
2703 if (ops->eswitch_inline_mode_get) {
2704 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
2706 goto nla_put_failure;
2707 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
2710 goto nla_put_failure;
2713 if (ops->eswitch_encap_mode_get) {
2714 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
2716 goto nla_put_failure;
2717 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
2719 goto nla_put_failure;
2722 genlmsg_end(msg, hdr);
2726 genlmsg_cancel(msg, hdr);
2730 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
2731 struct genl_info *info)
2733 struct devlink *devlink = info->user_ptr[0];
2734 struct sk_buff *msg;
2737 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2741 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
2742 info->snd_portid, info->snd_seq, 0);
2749 return genlmsg_reply(msg, info);
2752 static int devlink_rate_nodes_check(struct devlink *devlink, u16 mode,
2753 struct netlink_ext_ack *extack)
2755 struct devlink_rate *devlink_rate;
2757 /* Take the lock to sync with devlink_rate_nodes_destroy() */
2758 mutex_lock(&devlink->lock);
2759 list_for_each_entry(devlink_rate, &devlink->rate_list, list)
2760 if (devlink_rate_is_node(devlink_rate)) {
2761 mutex_unlock(&devlink->lock);
2762 NL_SET_ERR_MSG_MOD(extack, "Rate node(s) exists.");
2765 mutex_unlock(&devlink->lock);
2769 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
2770 struct genl_info *info)
2772 struct devlink *devlink = info->user_ptr[0];
2773 const struct devlink_ops *ops = devlink->ops;
2774 enum devlink_eswitch_encap_mode encap_mode;
2779 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
2780 if (!ops->eswitch_mode_set)
2782 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
2783 err = devlink_rate_nodes_check(devlink, mode, info->extack);
2786 err = ops->eswitch_mode_set(devlink, mode, info->extack);
2791 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2792 if (!ops->eswitch_inline_mode_set)
2794 inline_mode = nla_get_u8(
2795 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
2796 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
2802 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2803 if (!ops->eswitch_encap_mode_set)
2805 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
2806 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
2815 int devlink_dpipe_match_put(struct sk_buff *skb,
2816 struct devlink_dpipe_match *match)
2818 struct devlink_dpipe_header *header = match->header;
2819 struct devlink_dpipe_field *field = &header->fields[match->field_id];
2820 struct nlattr *match_attr;
2822 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
2826 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
2827 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
2828 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2829 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2830 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2831 goto nla_put_failure;
2833 nla_nest_end(skb, match_attr);
2837 nla_nest_cancel(skb, match_attr);
2840 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
2842 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
2843 struct sk_buff *skb)
2845 struct nlattr *matches_attr;
2847 matches_attr = nla_nest_start_noflag(skb,
2848 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
2852 if (table->table_ops->matches_dump(table->priv, skb))
2853 goto nla_put_failure;
2855 nla_nest_end(skb, matches_attr);
2859 nla_nest_cancel(skb, matches_attr);
2863 int devlink_dpipe_action_put(struct sk_buff *skb,
2864 struct devlink_dpipe_action *action)
2866 struct devlink_dpipe_header *header = action->header;
2867 struct devlink_dpipe_field *field = &header->fields[action->field_id];
2868 struct nlattr *action_attr;
2870 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
2874 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
2875 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
2876 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2877 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2878 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2879 goto nla_put_failure;
2881 nla_nest_end(skb, action_attr);
2885 nla_nest_cancel(skb, action_attr);
2888 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
2890 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
2891 struct sk_buff *skb)
2893 struct nlattr *actions_attr;
2895 actions_attr = nla_nest_start_noflag(skb,
2896 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
2900 if (table->table_ops->actions_dump(table->priv, skb))
2901 goto nla_put_failure;
2903 nla_nest_end(skb, actions_attr);
2907 nla_nest_cancel(skb, actions_attr);
2911 static int devlink_dpipe_table_put(struct sk_buff *skb,
2912 struct devlink_dpipe_table *table)
2914 struct nlattr *table_attr;
2917 table_size = table->table_ops->size_get(table->priv);
2918 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
2922 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
2923 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
2925 goto nla_put_failure;
2926 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
2927 table->counters_enabled))
2928 goto nla_put_failure;
2930 if (table->resource_valid) {
2931 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
2932 table->resource_id, DEVLINK_ATTR_PAD) ||
2933 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
2934 table->resource_units, DEVLINK_ATTR_PAD))
2935 goto nla_put_failure;
2937 if (devlink_dpipe_matches_put(table, skb))
2938 goto nla_put_failure;
2940 if (devlink_dpipe_actions_put(table, skb))
2941 goto nla_put_failure;
2943 nla_nest_end(skb, table_attr);
2947 nla_nest_cancel(skb, table_attr);
2951 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
2952 struct genl_info *info)
2957 err = genlmsg_reply(*pskb, info);
2961 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2967 static int devlink_dpipe_tables_fill(struct genl_info *info,
2968 enum devlink_command cmd, int flags,
2969 struct list_head *dpipe_tables,
2970 const char *table_name)
2972 struct devlink *devlink = info->user_ptr[0];
2973 struct devlink_dpipe_table *table;
2974 struct nlattr *tables_attr;
2975 struct sk_buff *skb = NULL;
2976 struct nlmsghdr *nlh;
2982 table = list_first_entry(dpipe_tables,
2983 struct devlink_dpipe_table, list);
2985 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2989 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2990 &devlink_nl_family, NLM_F_MULTI, cmd);
2996 if (devlink_nl_put_handle(skb, devlink))
2997 goto nla_put_failure;
2998 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
3000 goto nla_put_failure;
3004 list_for_each_entry_from(table, dpipe_tables, list) {
3006 err = devlink_dpipe_table_put(skb, table);
3014 if (!strcmp(table->name, table_name)) {
3015 err = devlink_dpipe_table_put(skb, table);
3023 nla_nest_end(skb, tables_attr);
3024 genlmsg_end(skb, hdr);
3029 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3030 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3032 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3038 return genlmsg_reply(skb, info);
3047 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
3048 struct genl_info *info)
3050 struct devlink *devlink = info->user_ptr[0];
3051 const char *table_name = NULL;
3053 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3054 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3056 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
3057 &devlink->dpipe_table_list,
3061 static int devlink_dpipe_value_put(struct sk_buff *skb,
3062 struct devlink_dpipe_value *value)
3064 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
3065 value->value_size, value->value))
3068 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
3069 value->value_size, value->mask))
3071 if (value->mapping_valid)
3072 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
3073 value->mapping_value))
3078 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
3079 struct devlink_dpipe_value *value)
3083 if (devlink_dpipe_action_put(skb, value->action))
3085 if (devlink_dpipe_value_put(skb, value))
3090 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
3091 struct devlink_dpipe_value *values,
3092 unsigned int values_count)
3094 struct nlattr *action_attr;
3098 for (i = 0; i < values_count; i++) {
3099 action_attr = nla_nest_start_noflag(skb,
3100 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
3103 err = devlink_dpipe_action_value_put(skb, &values[i]);
3105 goto err_action_value_put;
3106 nla_nest_end(skb, action_attr);
3110 err_action_value_put:
3111 nla_nest_cancel(skb, action_attr);
3115 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
3116 struct devlink_dpipe_value *value)
3120 if (devlink_dpipe_match_put(skb, value->match))
3122 if (devlink_dpipe_value_put(skb, value))
3127 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
3128 struct devlink_dpipe_value *values,
3129 unsigned int values_count)
3131 struct nlattr *match_attr;
3135 for (i = 0; i < values_count; i++) {
3136 match_attr = nla_nest_start_noflag(skb,
3137 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
3140 err = devlink_dpipe_match_value_put(skb, &values[i]);
3142 goto err_match_value_put;
3143 nla_nest_end(skb, match_attr);
3147 err_match_value_put:
3148 nla_nest_cancel(skb, match_attr);
3152 static int devlink_dpipe_entry_put(struct sk_buff *skb,
3153 struct devlink_dpipe_entry *entry)
3155 struct nlattr *entry_attr, *matches_attr, *actions_attr;
3158 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
3162 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
3164 goto nla_put_failure;
3165 if (entry->counter_valid)
3166 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
3167 entry->counter, DEVLINK_ATTR_PAD))
3168 goto nla_put_failure;
3170 matches_attr = nla_nest_start_noflag(skb,
3171 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
3173 goto nla_put_failure;
3175 err = devlink_dpipe_match_values_put(skb, entry->match_values,
3176 entry->match_values_count);
3178 nla_nest_cancel(skb, matches_attr);
3179 goto err_match_values_put;
3181 nla_nest_end(skb, matches_attr);
3183 actions_attr = nla_nest_start_noflag(skb,
3184 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
3186 goto nla_put_failure;
3188 err = devlink_dpipe_action_values_put(skb, entry->action_values,
3189 entry->action_values_count);
3191 nla_nest_cancel(skb, actions_attr);
3192 goto err_action_values_put;
3194 nla_nest_end(skb, actions_attr);
3196 nla_nest_end(skb, entry_attr);
3201 err_match_values_put:
3202 err_action_values_put:
3203 nla_nest_cancel(skb, entry_attr);
3207 static struct devlink_dpipe_table *
3208 devlink_dpipe_table_find(struct list_head *dpipe_tables,
3209 const char *table_name, struct devlink *devlink)
3211 struct devlink_dpipe_table *table;
3212 list_for_each_entry_rcu(table, dpipe_tables, list,
3213 lockdep_is_held(&devlink->lock)) {
3214 if (!strcmp(table->name, table_name))
3220 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
3222 struct devlink *devlink;
3225 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
3230 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
3231 dump_ctx->info->snd_portid,
3232 dump_ctx->info->snd_seq,
3233 &devlink_nl_family, NLM_F_MULTI,
3236 goto nla_put_failure;
3238 devlink = dump_ctx->info->user_ptr[0];
3239 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
3240 goto nla_put_failure;
3241 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
3242 DEVLINK_ATTR_DPIPE_ENTRIES);
3243 if (!dump_ctx->nest)
3244 goto nla_put_failure;
3248 nlmsg_free(dump_ctx->skb);
3251 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
3253 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
3254 struct devlink_dpipe_entry *entry)
3256 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
3258 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
3260 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
3262 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
3263 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
3266 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
3268 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
3271 unsigned int value_count, value_index;
3272 struct devlink_dpipe_value *value;
3274 value = entry->action_values;
3275 value_count = entry->action_values_count;
3276 for (value_index = 0; value_index < value_count; value_index++) {
3277 kfree(value[value_index].value);
3278 kfree(value[value_index].mask);
3281 value = entry->match_values;
3282 value_count = entry->match_values_count;
3283 for (value_index = 0; value_index < value_count; value_index++) {
3284 kfree(value[value_index].value);
3285 kfree(value[value_index].mask);
3288 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
3290 static int devlink_dpipe_entries_fill(struct genl_info *info,
3291 enum devlink_command cmd, int flags,
3292 struct devlink_dpipe_table *table)
3294 struct devlink_dpipe_dump_ctx dump_ctx;
3295 struct nlmsghdr *nlh;
3298 dump_ctx.skb = NULL;
3300 dump_ctx.info = info;
3302 err = table->table_ops->entries_dump(table->priv,
3303 table->counters_enabled,
3309 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
3310 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3312 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
3317 return genlmsg_reply(dump_ctx.skb, info);
3320 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
3321 struct genl_info *info)
3323 struct devlink *devlink = info->user_ptr[0];
3324 struct devlink_dpipe_table *table;
3325 const char *table_name;
3327 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
3330 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3331 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3332 table_name, devlink);
3336 if (!table->table_ops->entries_dump)
3339 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
3343 static int devlink_dpipe_fields_put(struct sk_buff *skb,
3344 const struct devlink_dpipe_header *header)
3346 struct devlink_dpipe_field *field;
3347 struct nlattr *field_attr;
3350 for (i = 0; i < header->fields_count; i++) {
3351 field = &header->fields[i];
3352 field_attr = nla_nest_start_noflag(skb,
3353 DEVLINK_ATTR_DPIPE_FIELD);
3356 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
3357 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
3358 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
3359 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
3360 goto nla_put_failure;
3361 nla_nest_end(skb, field_attr);
3366 nla_nest_cancel(skb, field_attr);
3370 static int devlink_dpipe_header_put(struct sk_buff *skb,
3371 struct devlink_dpipe_header *header)
3373 struct nlattr *fields_attr, *header_attr;
3376 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
3380 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
3381 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
3382 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
3383 goto nla_put_failure;
3385 fields_attr = nla_nest_start_noflag(skb,
3386 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
3388 goto nla_put_failure;
3390 err = devlink_dpipe_fields_put(skb, header);
3392 nla_nest_cancel(skb, fields_attr);
3393 goto nla_put_failure;
3395 nla_nest_end(skb, fields_attr);
3396 nla_nest_end(skb, header_attr);
3401 nla_nest_cancel(skb, header_attr);
3405 static int devlink_dpipe_headers_fill(struct genl_info *info,
3406 enum devlink_command cmd, int flags,
3407 struct devlink_dpipe_headers *
3410 struct devlink *devlink = info->user_ptr[0];
3411 struct nlattr *headers_attr;
3412 struct sk_buff *skb = NULL;
3413 struct nlmsghdr *nlh;
3420 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3424 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3425 &devlink_nl_family, NLM_F_MULTI, cmd);
3431 if (devlink_nl_put_handle(skb, devlink))
3432 goto nla_put_failure;
3433 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
3435 goto nla_put_failure;
3438 for (; i < dpipe_headers->headers_count; i++) {
3439 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
3447 nla_nest_end(skb, headers_attr);
3448 genlmsg_end(skb, hdr);
3449 if (i != dpipe_headers->headers_count)
3453 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3454 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3456 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3461 return genlmsg_reply(skb, info);
3470 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
3471 struct genl_info *info)
3473 struct devlink *devlink = info->user_ptr[0];
3475 if (!devlink->dpipe_headers)
3477 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
3478 0, devlink->dpipe_headers);
3481 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
3482 const char *table_name,
3485 struct devlink_dpipe_table *table;
3487 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
3488 table_name, devlink);
3492 if (table->counter_control_extern)
3495 if (!(table->counters_enabled ^ enable))
3498 table->counters_enabled = enable;
3499 if (table->table_ops->counters_set_update)
3500 table->table_ops->counters_set_update(table->priv, enable);
3504 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
3505 struct genl_info *info)
3507 struct devlink *devlink = info->user_ptr[0];
3508 const char *table_name;
3509 bool counters_enable;
3511 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
3512 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
3515 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
3516 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
3518 return devlink_dpipe_table_counters_set(devlink, table_name,
3522 static struct devlink_resource *
3523 devlink_resource_find(struct devlink *devlink,
3524 struct devlink_resource *resource, u64 resource_id)
3526 struct list_head *resource_list;
3529 resource_list = &resource->resource_list;
3531 resource_list = &devlink->resource_list;
3533 list_for_each_entry(resource, resource_list, list) {
3534 struct devlink_resource *child_resource;
3536 if (resource->id == resource_id)
3539 child_resource = devlink_resource_find(devlink, resource,
3542 return child_resource;
3548 devlink_resource_validate_children(struct devlink_resource *resource)
3550 struct devlink_resource *child_resource;
3551 bool size_valid = true;
3554 if (list_empty(&resource->resource_list))
3557 list_for_each_entry(child_resource, &resource->resource_list, list)
3558 parts_size += child_resource->size_new;
3560 if (parts_size > resource->size_new)
3563 resource->size_valid = size_valid;
3567 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
3568 struct netlink_ext_ack *extack)
3573 if (size > resource->size_params.size_max) {
3574 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
3578 if (size < resource->size_params.size_min) {
3579 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
3583 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
3585 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
3592 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
3593 struct genl_info *info)
3595 struct devlink *devlink = info->user_ptr[0];
3596 struct devlink_resource *resource;
3601 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
3602 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
3604 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
3606 resource = devlink_resource_find(devlink, NULL, resource_id);
3610 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
3611 err = devlink_resource_validate_size(resource, size, info->extack);
3615 resource->size_new = size;
3616 devlink_resource_validate_children(resource);
3617 if (resource->parent)
3618 devlink_resource_validate_children(resource->parent);
3623 devlink_resource_size_params_put(struct devlink_resource *resource,
3624 struct sk_buff *skb)
3626 struct devlink_resource_size_params *size_params;
3628 size_params = &resource->size_params;
3629 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
3630 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
3631 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
3632 size_params->size_max, DEVLINK_ATTR_PAD) ||
3633 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
3634 size_params->size_min, DEVLINK_ATTR_PAD) ||
3635 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
3640 static int devlink_resource_occ_put(struct devlink_resource *resource,
3641 struct sk_buff *skb)
3643 if (!resource->occ_get)
3645 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
3646 resource->occ_get(resource->occ_get_priv),
3650 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
3651 struct devlink_resource *resource)
3653 struct devlink_resource *child_resource;
3654 struct nlattr *child_resource_attr;
3655 struct nlattr *resource_attr;
3657 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
3661 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
3662 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
3663 DEVLINK_ATTR_PAD) ||
3664 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
3666 goto nla_put_failure;
3667 if (resource->size != resource->size_new)
3668 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
3669 resource->size_new, DEVLINK_ATTR_PAD);
3670 if (devlink_resource_occ_put(resource, skb))
3671 goto nla_put_failure;
3672 if (devlink_resource_size_params_put(resource, skb))
3673 goto nla_put_failure;
3674 if (list_empty(&resource->resource_list))
3677 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
3678 resource->size_valid))
3679 goto nla_put_failure;
3681 child_resource_attr = nla_nest_start_noflag(skb,
3682 DEVLINK_ATTR_RESOURCE_LIST);
3683 if (!child_resource_attr)
3684 goto nla_put_failure;
3686 list_for_each_entry(child_resource, &resource->resource_list, list) {
3687 if (devlink_resource_put(devlink, skb, child_resource))
3688 goto resource_put_failure;
3691 nla_nest_end(skb, child_resource_attr);
3693 nla_nest_end(skb, resource_attr);
3696 resource_put_failure:
3697 nla_nest_cancel(skb, child_resource_attr);
3699 nla_nest_cancel(skb, resource_attr);
3703 static int devlink_resource_fill(struct genl_info *info,
3704 enum devlink_command cmd, int flags)
3706 struct devlink *devlink = info->user_ptr[0];
3707 struct devlink_resource *resource;
3708 struct nlattr *resources_attr;
3709 struct sk_buff *skb = NULL;
3710 struct nlmsghdr *nlh;
3716 resource = list_first_entry(&devlink->resource_list,
3717 struct devlink_resource, list);
3719 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3723 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
3724 &devlink_nl_family, NLM_F_MULTI, cmd);
3730 if (devlink_nl_put_handle(skb, devlink))
3731 goto nla_put_failure;
3733 resources_attr = nla_nest_start_noflag(skb,
3734 DEVLINK_ATTR_RESOURCE_LIST);
3735 if (!resources_attr)
3736 goto nla_put_failure;
3740 list_for_each_entry_from(resource, &devlink->resource_list, list) {
3741 err = devlink_resource_put(devlink, skb, resource);
3744 goto err_resource_put;
3750 nla_nest_end(skb, resources_attr);
3751 genlmsg_end(skb, hdr);
3755 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
3756 NLMSG_DONE, 0, flags | NLM_F_MULTI);
3758 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
3763 return genlmsg_reply(skb, info);
3772 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
3773 struct genl_info *info)
3775 struct devlink *devlink = info->user_ptr[0];
3777 if (list_empty(&devlink->resource_list))
3780 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
3784 devlink_resources_validate(struct devlink *devlink,
3785 struct devlink_resource *resource,
3786 struct genl_info *info)
3788 struct list_head *resource_list;
3792 resource_list = &resource->resource_list;
3794 resource_list = &devlink->resource_list;
3796 list_for_each_entry(resource, resource_list, list) {
3797 if (!resource->size_valid)
3799 err = devlink_resources_validate(devlink, resource, info);
3806 static struct net *devlink_netns_get(struct sk_buff *skb,
3807 struct genl_info *info)
3809 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
3810 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
3811 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
3814 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
3815 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
3816 return ERR_PTR(-EINVAL);
3819 if (netns_pid_attr) {
3820 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
3821 } else if (netns_fd_attr) {
3822 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
3823 } else if (netns_id_attr) {
3824 net = get_net_ns_by_id(sock_net(skb->sk),
3825 nla_get_u32(netns_id_attr));
3827 net = ERR_PTR(-EINVAL);
3830 net = ERR_PTR(-EINVAL);
3833 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
3834 return ERR_PTR(-EINVAL);
3836 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
3838 return ERR_PTR(-EPERM);
3843 static void devlink_param_notify(struct devlink *devlink,
3844 unsigned int port_index,
3845 struct devlink_param_item *param_item,
3846 enum devlink_command cmd);
3848 static void devlink_ns_change_notify(struct devlink *devlink,
3849 struct net *dest_net, struct net *curr_net,
3852 struct devlink_param_item *param_item;
3853 enum devlink_command cmd;
3855 /* Userspace needs to be notified about devlink objects
3856 * removed from original and entering new network namespace.
3857 * The rest of the devlink objects are re-created during
3858 * reload process so the notifications are generated separatelly.
3861 if (!dest_net || net_eq(dest_net, curr_net))
3865 devlink_notify(devlink, DEVLINK_CMD_NEW);
3867 cmd = new ? DEVLINK_CMD_PARAM_NEW : DEVLINK_CMD_PARAM_DEL;
3868 list_for_each_entry(param_item, &devlink->param_list, list)
3869 devlink_param_notify(devlink, 0, param_item, cmd);
3872 devlink_notify(devlink, DEVLINK_CMD_DEL);
3875 static bool devlink_reload_supported(const struct devlink_ops *ops)
3877 return ops->reload_down && ops->reload_up;
3880 static void devlink_reload_failed_set(struct devlink *devlink,
3883 if (devlink->reload_failed == reload_failed)
3885 devlink->reload_failed = reload_failed;
3886 devlink_notify(devlink, DEVLINK_CMD_NEW);
3889 bool devlink_is_reload_failed(const struct devlink *devlink)
3891 return devlink->reload_failed;
3893 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
3896 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
3897 enum devlink_reload_limit limit, u32 actions_performed)
3899 unsigned long actions = actions_performed;
3903 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
3904 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
3905 reload_stats[stat_idx]++;
3907 devlink_notify(devlink, DEVLINK_CMD_NEW);
3911 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
3912 u32 actions_performed)
3914 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
3919 * devlink_remote_reload_actions_performed - Update devlink on reload actions
3920 * performed which are not a direct result of devlink reload call.
3922 * This should be called by a driver after performing reload actions in case it was not
3923 * a result of devlink reload call. For example fw_activate was performed as a result
3924 * of devlink reload triggered fw_activate on another host.
3925 * The motivation for this function is to keep data on reload actions performed on this
3926 * function whether it was done due to direct devlink reload call or not.
3929 * @limit: reload limit
3930 * @actions_performed: bitmask of actions performed
3932 void devlink_remote_reload_actions_performed(struct devlink *devlink,
3933 enum devlink_reload_limit limit,
3934 u32 actions_performed)
3936 if (WARN_ON(!actions_performed ||
3937 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
3938 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
3939 limit > DEVLINK_RELOAD_LIMIT_MAX))
3942 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
3945 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
3947 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
3948 enum devlink_reload_action action, enum devlink_reload_limit limit,
3949 u32 *actions_performed, struct netlink_ext_ack *extack)
3951 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
3952 struct net *curr_net;
3955 if (!devlink->reload_enabled)
3958 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
3959 sizeof(remote_reload_stats));
3961 curr_net = devlink_net(devlink);
3962 devlink_ns_change_notify(devlink, dest_net, curr_net, false);
3963 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
3967 if (dest_net && !net_eq(dest_net, curr_net))
3968 write_pnet(&devlink->_net, dest_net);
3970 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
3971 devlink_reload_failed_set(devlink, !!err);
3975 devlink_ns_change_notify(devlink, dest_net, curr_net, true);
3976 WARN_ON(!(*actions_performed & BIT(action)));
3977 /* Catch driver on updating the remote action within devlink reload */
3978 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
3979 sizeof(remote_reload_stats)));
3980 devlink_reload_stats_update(devlink, limit, *actions_performed);
3985 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
3986 enum devlink_command cmd, struct genl_info *info)
3988 struct sk_buff *msg;
3991 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3995 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
3999 if (devlink_nl_put_handle(msg, devlink))
4000 goto nla_put_failure;
4002 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
4004 goto nla_put_failure;
4005 genlmsg_end(msg, hdr);
4007 return genlmsg_reply(msg, info);
4010 genlmsg_cancel(msg, hdr);
4016 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
4018 struct devlink *devlink = info->user_ptr[0];
4019 enum devlink_reload_action action;
4020 enum devlink_reload_limit limit;
4021 struct net *dest_net = NULL;
4022 u32 actions_performed;
4025 if (!devlink_reload_supported(devlink->ops))
4028 err = devlink_resources_validate(devlink, NULL, info);
4030 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
4034 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
4035 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
4036 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
4037 dest_net = devlink_netns_get(skb, info);
4038 if (IS_ERR(dest_net))
4039 return PTR_ERR(dest_net);
4042 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
4043 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
4045 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
4047 if (!devlink_reload_action_is_supported(devlink, action)) {
4048 NL_SET_ERR_MSG_MOD(info->extack,
4049 "Requested reload action is not supported by the driver");
4053 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
4054 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
4055 struct nla_bitfield32 limits;
4056 u32 limits_selected;
4058 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
4059 limits_selected = limits.value & limits.selector;
4060 if (!limits_selected) {
4061 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
4064 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
4065 if (limits_selected & BIT(limit))
4067 /* UAPI enables multiselection, but currently it is not used */
4068 if (limits_selected != BIT(limit)) {
4069 NL_SET_ERR_MSG_MOD(info->extack,
4070 "Multiselection of limit is not supported");
4073 if (!devlink_reload_limit_is_supported(devlink, limit)) {
4074 NL_SET_ERR_MSG_MOD(info->extack,
4075 "Requested limit is not supported by the driver");
4078 if (devlink_reload_combination_is_invalid(action, limit)) {
4079 NL_SET_ERR_MSG_MOD(info->extack,
4080 "Requested limit is invalid for this action");
4084 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
4091 /* For backward compatibility generate reply only if attributes used by user */
4092 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
4095 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
4096 DEVLINK_CMD_RELOAD, info);
4099 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
4100 struct devlink *devlink,
4101 enum devlink_command cmd,
4102 struct devlink_flash_notify *params)
4106 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
4110 if (devlink_nl_put_handle(msg, devlink))
4111 goto nla_put_failure;
4113 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
4116 if (params->status_msg &&
4117 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
4118 params->status_msg))
4119 goto nla_put_failure;
4120 if (params->component &&
4121 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
4123 goto nla_put_failure;
4124 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
4125 params->done, DEVLINK_ATTR_PAD))
4126 goto nla_put_failure;
4127 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
4128 params->total, DEVLINK_ATTR_PAD))
4129 goto nla_put_failure;
4130 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
4131 params->timeout, DEVLINK_ATTR_PAD))
4132 goto nla_put_failure;
4135 genlmsg_end(msg, hdr);
4139 genlmsg_cancel(msg, hdr);
4143 static void __devlink_flash_update_notify(struct devlink *devlink,
4144 enum devlink_command cmd,
4145 struct devlink_flash_notify *params)
4147 struct sk_buff *msg;
4150 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
4151 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
4152 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
4154 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4158 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
4162 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4163 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4170 static void devlink_flash_update_begin_notify(struct devlink *devlink)
4172 struct devlink_flash_notify params = {};
4174 __devlink_flash_update_notify(devlink,
4175 DEVLINK_CMD_FLASH_UPDATE,
4179 static void devlink_flash_update_end_notify(struct devlink *devlink)
4181 struct devlink_flash_notify params = {};
4183 __devlink_flash_update_notify(devlink,
4184 DEVLINK_CMD_FLASH_UPDATE_END,
4188 void devlink_flash_update_status_notify(struct devlink *devlink,
4189 const char *status_msg,
4190 const char *component,
4192 unsigned long total)
4194 struct devlink_flash_notify params = {
4195 .status_msg = status_msg,
4196 .component = component,
4201 __devlink_flash_update_notify(devlink,
4202 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4205 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
4207 void devlink_flash_update_timeout_notify(struct devlink *devlink,
4208 const char *status_msg,
4209 const char *component,
4210 unsigned long timeout)
4212 struct devlink_flash_notify params = {
4213 .status_msg = status_msg,
4214 .component = component,
4218 __devlink_flash_update_notify(devlink,
4219 DEVLINK_CMD_FLASH_UPDATE_STATUS,
4222 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
4224 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
4225 struct genl_info *info)
4227 struct nlattr *nla_component, *nla_overwrite_mask, *nla_file_name;
4228 struct devlink_flash_update_params params = {};
4229 struct devlink *devlink = info->user_ptr[0];
4230 const char *file_name;
4231 u32 supported_params;
4234 if (!devlink->ops->flash_update)
4237 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
4240 supported_params = devlink->ops->supported_flash_update_params;
4242 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
4243 if (nla_component) {
4244 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
4245 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
4246 "component update is not supported by this device");
4249 params.component = nla_data(nla_component);
4252 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
4253 if (nla_overwrite_mask) {
4254 struct nla_bitfield32 sections;
4256 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
4257 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
4258 "overwrite settings are not supported by this device");
4261 sections = nla_get_bitfield32(nla_overwrite_mask);
4262 params.overwrite_mask = sections.value & sections.selector;
4265 nla_file_name = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME];
4266 file_name = nla_data(nla_file_name);
4267 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
4269 NL_SET_ERR_MSG_ATTR(info->extack, nla_file_name, "failed to locate the requested firmware file");
4273 devlink_flash_update_begin_notify(devlink);
4274 ret = devlink->ops->flash_update(devlink, ¶ms, info->extack);
4275 devlink_flash_update_end_notify(devlink);
4277 release_firmware(params.fw);
4282 static const struct devlink_param devlink_param_generic[] = {
4284 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
4285 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
4286 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
4289 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
4290 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
4291 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
4294 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
4295 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
4296 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
4299 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
4300 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
4301 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
4304 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
4305 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
4306 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
4309 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
4310 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
4311 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
4314 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
4315 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
4316 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
4319 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
4320 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
4321 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
4324 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
4325 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
4326 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
4329 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
4330 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
4331 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
4334 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
4335 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
4336 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
4339 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
4340 .name = DEVLINK_PARAM_GENERIC_ENABLE_ETH_NAME,
4341 .type = DEVLINK_PARAM_GENERIC_ENABLE_ETH_TYPE,
4344 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
4345 .name = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_NAME,
4346 .type = DEVLINK_PARAM_GENERIC_ENABLE_RDMA_TYPE,
4349 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
4350 .name = DEVLINK_PARAM_GENERIC_ENABLE_VNET_NAME,
4351 .type = DEVLINK_PARAM_GENERIC_ENABLE_VNET_TYPE,
4355 static int devlink_param_generic_verify(const struct devlink_param *param)
4357 /* verify it match generic parameter by id and name */
4358 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
4360 if (strcmp(param->name, devlink_param_generic[param->id].name))
4363 WARN_ON(param->type != devlink_param_generic[param->id].type);
4368 static int devlink_param_driver_verify(const struct devlink_param *param)
4372 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
4374 /* verify no such name in generic params */
4375 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
4376 if (!strcmp(param->name, devlink_param_generic[i].name))
4382 static struct devlink_param_item *
4383 devlink_param_find_by_name(struct list_head *param_list,
4384 const char *param_name)
4386 struct devlink_param_item *param_item;
4388 list_for_each_entry(param_item, param_list, list)
4389 if (!strcmp(param_item->param->name, param_name))
4394 static struct devlink_param_item *
4395 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
4397 struct devlink_param_item *param_item;
4399 list_for_each_entry(param_item, param_list, list)
4400 if (param_item->param->id == param_id)
4406 devlink_param_cmode_is_supported(const struct devlink_param *param,
4407 enum devlink_param_cmode cmode)
4409 return test_bit(cmode, ¶m->supported_cmodes);
4412 static int devlink_param_get(struct devlink *devlink,
4413 const struct devlink_param *param,
4414 struct devlink_param_gset_ctx *ctx)
4418 return param->get(devlink, param->id, ctx);
4421 static int devlink_param_set(struct devlink *devlink,
4422 const struct devlink_param *param,
4423 struct devlink_param_gset_ctx *ctx)
4427 return param->set(devlink, param->id, ctx);
4431 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
4433 switch (param_type) {
4434 case DEVLINK_PARAM_TYPE_U8:
4436 case DEVLINK_PARAM_TYPE_U16:
4438 case DEVLINK_PARAM_TYPE_U32:
4440 case DEVLINK_PARAM_TYPE_STRING:
4442 case DEVLINK_PARAM_TYPE_BOOL:
4450 devlink_nl_param_value_fill_one(struct sk_buff *msg,
4451 enum devlink_param_type type,
4452 enum devlink_param_cmode cmode,
4453 union devlink_param_value val)
4455 struct nlattr *param_value_attr;
4457 param_value_attr = nla_nest_start_noflag(msg,
4458 DEVLINK_ATTR_PARAM_VALUE);
4459 if (!param_value_attr)
4460 goto nla_put_failure;
4462 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
4463 goto value_nest_cancel;
4466 case DEVLINK_PARAM_TYPE_U8:
4467 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
4468 goto value_nest_cancel;
4470 case DEVLINK_PARAM_TYPE_U16:
4471 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
4472 goto value_nest_cancel;
4474 case DEVLINK_PARAM_TYPE_U32:
4475 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
4476 goto value_nest_cancel;
4478 case DEVLINK_PARAM_TYPE_STRING:
4479 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
4481 goto value_nest_cancel;
4483 case DEVLINK_PARAM_TYPE_BOOL:
4485 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
4486 goto value_nest_cancel;
4490 nla_nest_end(msg, param_value_attr);
4494 nla_nest_cancel(msg, param_value_attr);
4499 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
4500 unsigned int port_index,
4501 struct devlink_param_item *param_item,
4502 enum devlink_command cmd,
4503 u32 portid, u32 seq, int flags)
4505 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
4506 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
4507 const struct devlink_param *param = param_item->param;
4508 struct devlink_param_gset_ctx ctx;
4509 struct nlattr *param_values_list;
4510 struct nlattr *param_attr;
4516 /* Get value from driver part to driverinit configuration mode */
4517 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
4518 if (!devlink_param_cmode_is_supported(param, i))
4520 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4521 if (!param_item->driverinit_value_valid)
4523 param_value[i] = param_item->driverinit_value;
4525 if (!param_item->published)
4528 err = devlink_param_get(devlink, param, &ctx);
4531 param_value[i] = ctx.val;
4533 param_value_set[i] = true;
4536 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4540 if (devlink_nl_put_handle(msg, devlink))
4541 goto genlmsg_cancel;
4543 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
4544 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
4545 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
4546 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
4547 goto genlmsg_cancel;
4549 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
4551 goto genlmsg_cancel;
4552 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
4553 goto param_nest_cancel;
4554 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
4555 goto param_nest_cancel;
4557 nla_type = devlink_param_type_to_nla_type(param->type);
4559 goto param_nest_cancel;
4560 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
4561 goto param_nest_cancel;
4563 param_values_list = nla_nest_start_noflag(msg,
4564 DEVLINK_ATTR_PARAM_VALUES_LIST);
4565 if (!param_values_list)
4566 goto param_nest_cancel;
4568 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
4569 if (!param_value_set[i])
4571 err = devlink_nl_param_value_fill_one(msg, param->type,
4574 goto values_list_nest_cancel;
4577 nla_nest_end(msg, param_values_list);
4578 nla_nest_end(msg, param_attr);
4579 genlmsg_end(msg, hdr);
4582 values_list_nest_cancel:
4583 nla_nest_end(msg, param_values_list);
4585 nla_nest_cancel(msg, param_attr);
4587 genlmsg_cancel(msg, hdr);
4591 static void devlink_param_notify(struct devlink *devlink,
4592 unsigned int port_index,
4593 struct devlink_param_item *param_item,
4594 enum devlink_command cmd)
4596 struct sk_buff *msg;
4599 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
4600 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
4601 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
4603 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4606 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
4613 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4614 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4617 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
4618 struct netlink_callback *cb)
4620 struct devlink_param_item *param_item;
4621 struct devlink *devlink;
4622 int start = cb->args[0];
4623 unsigned long index;
4627 mutex_lock(&devlink_mutex);
4628 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
4629 if (!devlink_try_get(devlink))
4632 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4635 mutex_lock(&devlink->lock);
4636 list_for_each_entry(param_item, &devlink->param_list, list) {
4641 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
4642 DEVLINK_CMD_PARAM_GET,
4643 NETLINK_CB(cb->skb).portid,
4646 if (err == -EOPNOTSUPP) {
4649 mutex_unlock(&devlink->lock);
4650 devlink_put(devlink);
4655 mutex_unlock(&devlink->lock);
4657 devlink_put(devlink);
4660 mutex_unlock(&devlink_mutex);
4662 if (err != -EMSGSIZE)
4670 devlink_param_type_get_from_info(struct genl_info *info,
4671 enum devlink_param_type *param_type)
4673 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
4676 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
4678 *param_type = DEVLINK_PARAM_TYPE_U8;
4681 *param_type = DEVLINK_PARAM_TYPE_U16;
4684 *param_type = DEVLINK_PARAM_TYPE_U32;
4687 *param_type = DEVLINK_PARAM_TYPE_STRING;
4690 *param_type = DEVLINK_PARAM_TYPE_BOOL;
4700 devlink_param_value_get_from_info(const struct devlink_param *param,
4701 struct genl_info *info,
4702 union devlink_param_value *value)
4704 struct nlattr *param_data;
4707 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
4709 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
4712 switch (param->type) {
4713 case DEVLINK_PARAM_TYPE_U8:
4714 if (nla_len(param_data) != sizeof(u8))
4716 value->vu8 = nla_get_u8(param_data);
4718 case DEVLINK_PARAM_TYPE_U16:
4719 if (nla_len(param_data) != sizeof(u16))
4721 value->vu16 = nla_get_u16(param_data);
4723 case DEVLINK_PARAM_TYPE_U32:
4724 if (nla_len(param_data) != sizeof(u32))
4726 value->vu32 = nla_get_u32(param_data);
4728 case DEVLINK_PARAM_TYPE_STRING:
4729 len = strnlen(nla_data(param_data), nla_len(param_data));
4730 if (len == nla_len(param_data) ||
4731 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
4733 strcpy(value->vstr, nla_data(param_data));
4735 case DEVLINK_PARAM_TYPE_BOOL:
4736 if (param_data && nla_len(param_data))
4738 value->vbool = nla_get_flag(param_data);
4744 static struct devlink_param_item *
4745 devlink_param_get_from_info(struct list_head *param_list,
4746 struct genl_info *info)
4750 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
4753 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
4754 return devlink_param_find_by_name(param_list, param_name);
4757 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
4758 struct genl_info *info)
4760 struct devlink *devlink = info->user_ptr[0];
4761 struct devlink_param_item *param_item;
4762 struct sk_buff *msg;
4765 param_item = devlink_param_get_from_info(&devlink->param_list, info);
4769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4773 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
4774 DEVLINK_CMD_PARAM_GET,
4775 info->snd_portid, info->snd_seq, 0);
4781 return genlmsg_reply(msg, info);
4784 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
4785 unsigned int port_index,
4786 struct list_head *param_list,
4787 struct genl_info *info,
4788 enum devlink_command cmd)
4790 enum devlink_param_type param_type;
4791 struct devlink_param_gset_ctx ctx;
4792 enum devlink_param_cmode cmode;
4793 struct devlink_param_item *param_item;
4794 const struct devlink_param *param;
4795 union devlink_param_value value;
4798 param_item = devlink_param_get_from_info(param_list, info);
4801 param = param_item->param;
4802 err = devlink_param_type_get_from_info(info, ¶m_type);
4805 if (param_type != param->type)
4807 err = devlink_param_value_get_from_info(param, info, &value);
4810 if (param->validate) {
4811 err = param->validate(devlink, param->id, value, info->extack);
4816 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
4818 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
4819 if (!devlink_param_cmode_is_supported(param, cmode))
4822 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
4823 if (param->type == DEVLINK_PARAM_TYPE_STRING)
4824 strcpy(param_item->driverinit_value.vstr, value.vstr);
4826 param_item->driverinit_value = value;
4827 param_item->driverinit_value_valid = true;
4833 err = devlink_param_set(devlink, param, &ctx);
4838 devlink_param_notify(devlink, port_index, param_item, cmd);
4842 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
4843 struct genl_info *info)
4845 struct devlink *devlink = info->user_ptr[0];
4847 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
4848 info, DEVLINK_CMD_PARAM_NEW);
4851 static int devlink_param_register_one(struct devlink *devlink,
4852 unsigned int port_index,
4853 struct list_head *param_list,
4854 const struct devlink_param *param,
4855 enum devlink_command cmd)
4857 struct devlink_param_item *param_item;
4859 if (devlink_param_find_by_name(param_list, param->name))
4862 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
4863 WARN_ON(param->get || param->set);
4865 WARN_ON(!param->get || !param->set);
4867 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
4870 param_item->param = param;
4872 list_add_tail(¶m_item->list, param_list);
4873 devlink_param_notify(devlink, port_index, param_item, cmd);
4877 static void devlink_param_unregister_one(struct devlink *devlink,
4878 unsigned int port_index,
4879 struct list_head *param_list,
4880 const struct devlink_param *param,
4881 enum devlink_command cmd)
4883 struct devlink_param_item *param_item;
4885 param_item = devlink_param_find_by_name(param_list, param->name);
4886 WARN_ON(!param_item);
4887 devlink_param_notify(devlink, port_index, param_item, cmd);
4888 list_del(¶m_item->list);
4892 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
4893 struct netlink_callback *cb)
4895 struct devlink_param_item *param_item;
4896 struct devlink_port *devlink_port;
4897 struct devlink *devlink;
4898 int start = cb->args[0];
4899 unsigned long index;
4903 mutex_lock(&devlink_mutex);
4904 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
4905 if (!devlink_try_get(devlink))
4908 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4911 mutex_lock(&devlink->lock);
4912 list_for_each_entry(devlink_port, &devlink->port_list, list) {
4913 list_for_each_entry(param_item,
4914 &devlink_port->param_list, list) {
4919 err = devlink_nl_param_fill(msg,
4920 devlink_port->devlink,
4921 devlink_port->index, param_item,
4922 DEVLINK_CMD_PORT_PARAM_GET,
4923 NETLINK_CB(cb->skb).portid,
4926 if (err == -EOPNOTSUPP) {
4929 mutex_unlock(&devlink->lock);
4930 devlink_put(devlink);
4936 mutex_unlock(&devlink->lock);
4938 devlink_put(devlink);
4941 mutex_unlock(&devlink_mutex);
4943 if (err != -EMSGSIZE)
4950 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
4951 struct genl_info *info)
4953 struct devlink_port *devlink_port = info->user_ptr[1];
4954 struct devlink_param_item *param_item;
4955 struct sk_buff *msg;
4958 param_item = devlink_param_get_from_info(&devlink_port->param_list,
4963 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4967 err = devlink_nl_param_fill(msg, devlink_port->devlink,
4968 devlink_port->index, param_item,
4969 DEVLINK_CMD_PORT_PARAM_GET,
4970 info->snd_portid, info->snd_seq, 0);
4976 return genlmsg_reply(msg, info);
4979 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
4980 struct genl_info *info)
4982 struct devlink_port *devlink_port = info->user_ptr[1];
4984 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
4985 devlink_port->index,
4986 &devlink_port->param_list, info,
4987 DEVLINK_CMD_PORT_PARAM_NEW);
4990 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
4991 struct devlink *devlink,
4992 struct devlink_snapshot *snapshot)
4994 struct nlattr *snap_attr;
4997 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
5001 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
5003 goto nla_put_failure;
5005 nla_nest_end(msg, snap_attr);
5009 nla_nest_cancel(msg, snap_attr);
5013 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
5014 struct devlink *devlink,
5015 struct devlink_region *region)
5017 struct devlink_snapshot *snapshot;
5018 struct nlattr *snapshots_attr;
5021 snapshots_attr = nla_nest_start_noflag(msg,
5022 DEVLINK_ATTR_REGION_SNAPSHOTS);
5023 if (!snapshots_attr)
5026 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
5027 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
5029 goto nla_put_failure;
5032 nla_nest_end(msg, snapshots_attr);
5036 nla_nest_cancel(msg, snapshots_attr);
5040 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
5041 enum devlink_command cmd, u32 portid,
5043 struct devlink_region *region)
5048 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5052 err = devlink_nl_put_handle(msg, devlink);
5054 goto nla_put_failure;
5057 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5058 region->port->index);
5060 goto nla_put_failure;
5063 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
5065 goto nla_put_failure;
5067 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5071 goto nla_put_failure;
5073 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
5075 goto nla_put_failure;
5077 genlmsg_end(msg, hdr);
5081 genlmsg_cancel(msg, hdr);
5085 static struct sk_buff *
5086 devlink_nl_region_notify_build(struct devlink_region *region,
5087 struct devlink_snapshot *snapshot,
5088 enum devlink_command cmd, u32 portid, u32 seq)
5090 struct devlink *devlink = region->devlink;
5091 struct sk_buff *msg;
5096 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5098 return ERR_PTR(-ENOMEM);
5100 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
5106 err = devlink_nl_put_handle(msg, devlink);
5108 goto out_cancel_msg;
5111 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
5112 region->port->index);
5114 goto out_cancel_msg;
5117 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
5120 goto out_cancel_msg;
5123 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
5126 goto out_cancel_msg;
5128 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
5129 region->size, DEVLINK_ATTR_PAD);
5131 goto out_cancel_msg;
5133 genlmsg_end(msg, hdr);
5138 genlmsg_cancel(msg, hdr);
5141 return ERR_PTR(err);
5144 static void devlink_nl_region_notify(struct devlink_region *region,
5145 struct devlink_snapshot *snapshot,
5146 enum devlink_command cmd)
5148 struct sk_buff *msg;
5150 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
5152 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
5156 genlmsg_multicast_netns(&devlink_nl_family,
5157 devlink_net(region->devlink), msg, 0,
5158 DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5162 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
5163 * @devlink: devlink instance
5164 * @id: the snapshot id
5166 * Track when a new snapshot begins using an id. Load the count for the
5167 * given id from the snapshot xarray, increment it, and store it back.
5169 * Called when a new snapshot is created with the given id.
5171 * The id *must* have been previously allocated by
5172 * devlink_region_snapshot_id_get().
5174 * Returns 0 on success, or an error on failure.
5176 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
5178 unsigned long count;
5181 lockdep_assert_held(&devlink->lock);
5183 p = xa_load(&devlink->snapshot_ids, id);
5187 if (WARN_ON(!xa_is_value(p)))
5190 count = xa_to_value(p);
5193 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5198 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
5199 * @devlink: devlink instance
5200 * @id: the snapshot id
5202 * Track when a snapshot is deleted and stops using an id. Load the count
5203 * for the given id from the snapshot xarray, decrement it, and store it
5206 * If the count reaches zero, erase this id from the xarray, freeing it
5207 * up for future re-use by devlink_region_snapshot_id_get().
5209 * Called when a snapshot using the given id is deleted, and when the
5210 * initial allocator of the id is finished using it.
5212 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
5214 unsigned long count;
5217 lockdep_assert_held(&devlink->lock);
5219 p = xa_load(&devlink->snapshot_ids, id);
5223 if (WARN_ON(!xa_is_value(p)))
5226 count = xa_to_value(p);
5230 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
5233 /* If this was the last user, we can erase this id */
5234 xa_erase(&devlink->snapshot_ids, id);
5239 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
5240 * @devlink: devlink instance
5241 * @id: the snapshot id
5243 * Mark the given snapshot id as used by inserting a zero value into the
5246 * This must be called while holding the devlink instance lock. Unlike
5247 * devlink_snapshot_id_get, the initial reference count is zero, not one.
5248 * It is expected that the id will immediately be used before
5249 * releasing the devlink instance lock.
5251 * Returns zero on success, or an error code if the snapshot id could not
5254 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
5256 lockdep_assert_held(&devlink->lock);
5258 if (xa_load(&devlink->snapshot_ids, id))
5261 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
5266 * __devlink_region_snapshot_id_get - get snapshot ID
5267 * @devlink: devlink instance
5268 * @id: storage to return snapshot id
5270 * Allocates a new snapshot id. Returns zero on success, or a negative
5271 * error on failure. Must be called while holding the devlink instance
5274 * Snapshot IDs are tracked using an xarray which stores the number of
5275 * users of the snapshot id.
5277 * Note that the caller of this function counts as a 'user', in order to
5278 * avoid race conditions. The caller must release its hold on the
5279 * snapshot by using devlink_region_snapshot_id_put.
5281 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
5283 lockdep_assert_held(&devlink->lock);
5285 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
5286 xa_limit_32b, GFP_KERNEL);
5290 * __devlink_region_snapshot_create - create a new snapshot
5291 * This will add a new snapshot of a region. The snapshot
5292 * will be stored on the region struct and can be accessed
5293 * from devlink. This is useful for future analyses of snapshots.
5294 * Multiple snapshots can be created on a region.
5295 * The @snapshot_id should be obtained using the getter function.
5297 * Must be called only while holding the devlink instance lock.
5299 * @region: devlink region of the snapshot
5300 * @data: snapshot data
5301 * @snapshot_id: snapshot id to be created
5304 __devlink_region_snapshot_create(struct devlink_region *region,
5305 u8 *data, u32 snapshot_id)
5307 struct devlink *devlink = region->devlink;
5308 struct devlink_snapshot *snapshot;
5311 lockdep_assert_held(&devlink->lock);
5313 /* check if region can hold one more snapshot */
5314 if (region->cur_snapshots == region->max_snapshots)
5317 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
5320 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
5324 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
5326 goto err_snapshot_id_increment;
5328 snapshot->id = snapshot_id;
5329 snapshot->region = region;
5330 snapshot->data = data;
5332 list_add_tail(&snapshot->list, ®ion->snapshot_list);
5334 region->cur_snapshots++;
5336 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
5339 err_snapshot_id_increment:
5344 static void devlink_region_snapshot_del(struct devlink_region *region,
5345 struct devlink_snapshot *snapshot)
5347 struct devlink *devlink = region->devlink;
5349 lockdep_assert_held(&devlink->lock);
5351 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
5352 region->cur_snapshots--;
5353 list_del(&snapshot->list);
5354 region->ops->destructor(snapshot->data);
5355 __devlink_snapshot_id_decrement(devlink, snapshot->id);
5359 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
5360 struct genl_info *info)
5362 struct devlink *devlink = info->user_ptr[0];
5363 struct devlink_port *port = NULL;
5364 struct devlink_region *region;
5365 const char *region_name;
5366 struct sk_buff *msg;
5370 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
5373 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5374 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5376 port = devlink_port_get_by_index(devlink, index);
5381 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5383 region = devlink_port_region_get_by_name(port, region_name);
5385 region = devlink_region_get_by_name(devlink, region_name);
5390 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5394 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
5395 info->snd_portid, info->snd_seq, 0,
5402 return genlmsg_reply(msg, info);
5405 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
5406 struct netlink_callback *cb,
5407 struct devlink_port *port,
5411 struct devlink_region *region;
5414 list_for_each_entry(region, &port->region_list, list) {
5419 err = devlink_nl_region_fill(msg, port->devlink,
5420 DEVLINK_CMD_REGION_GET,
5421 NETLINK_CB(cb->skb).portid,
5423 NLM_F_MULTI, region);
5433 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
5434 struct netlink_callback *cb,
5435 struct devlink *devlink,
5439 struct devlink_region *region;
5440 struct devlink_port *port;
5443 mutex_lock(&devlink->lock);
5444 list_for_each_entry(region, &devlink->region_list, list) {
5449 err = devlink_nl_region_fill(msg, devlink,
5450 DEVLINK_CMD_REGION_GET,
5451 NETLINK_CB(cb->skb).portid,
5453 NLM_F_MULTI, region);
5459 list_for_each_entry(port, &devlink->port_list, list) {
5460 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
5467 mutex_unlock(&devlink->lock);
5471 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
5472 struct netlink_callback *cb)
5474 struct devlink *devlink;
5475 int start = cb->args[0];
5476 unsigned long index;
5480 mutex_lock(&devlink_mutex);
5481 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
5482 if (!devlink_try_get(devlink))
5485 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5488 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
5491 devlink_put(devlink);
5496 mutex_unlock(&devlink_mutex);
5501 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
5502 struct genl_info *info)
5504 struct devlink *devlink = info->user_ptr[0];
5505 struct devlink_snapshot *snapshot;
5506 struct devlink_port *port = NULL;
5507 struct devlink_region *region;
5508 const char *region_name;
5512 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
5513 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
5516 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5517 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
5519 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5520 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5522 port = devlink_port_get_by_index(devlink, index);
5528 region = devlink_port_region_get_by_name(port, region_name);
5530 region = devlink_region_get_by_name(devlink, region_name);
5535 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
5539 devlink_region_snapshot_del(region, snapshot);
5544 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
5546 struct devlink *devlink = info->user_ptr[0];
5547 struct devlink_snapshot *snapshot;
5548 struct devlink_port *port = NULL;
5549 struct nlattr *snapshot_id_attr;
5550 struct devlink_region *region;
5551 const char *region_name;
5557 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
5558 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
5562 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
5564 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5565 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5567 port = devlink_port_get_by_index(devlink, index);
5573 region = devlink_port_region_get_by_name(port, region_name);
5575 region = devlink_region_get_by_name(devlink, region_name);
5578 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
5582 if (!region->ops->snapshot) {
5583 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
5587 if (region->cur_snapshots == region->max_snapshots) {
5588 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
5592 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
5593 if (snapshot_id_attr) {
5594 snapshot_id = nla_get_u32(snapshot_id_attr);
5596 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
5597 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
5601 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
5605 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
5607 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
5613 err = region->port_ops->snapshot(port, region->port_ops,
5614 info->extack, &data);
5616 err = region->ops->snapshot(devlink, region->ops,
5617 info->extack, &data);
5619 goto err_snapshot_capture;
5621 err = __devlink_region_snapshot_create(region, data, snapshot_id);
5623 goto err_snapshot_create;
5625 if (!snapshot_id_attr) {
5626 struct sk_buff *msg;
5628 snapshot = devlink_region_snapshot_get_by_id(region,
5630 if (WARN_ON(!snapshot))
5633 msg = devlink_nl_region_notify_build(region, snapshot,
5634 DEVLINK_CMD_REGION_NEW,
5637 err = PTR_ERR_OR_ZERO(msg);
5641 err = genlmsg_reply(msg, info);
5648 err_snapshot_create:
5649 region->ops->destructor(data);
5650 err_snapshot_capture:
5651 __devlink_snapshot_id_decrement(devlink, snapshot_id);
5655 devlink_region_snapshot_del(region, snapshot);
5659 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
5660 struct devlink *devlink,
5661 u8 *chunk, u32 chunk_size,
5664 struct nlattr *chunk_attr;
5667 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
5671 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
5673 goto nla_put_failure;
5675 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
5678 goto nla_put_failure;
5680 nla_nest_end(msg, chunk_attr);
5684 nla_nest_cancel(msg, chunk_attr);
5688 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
5690 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
5691 struct devlink *devlink,
5692 struct devlink_region *region,
5693 struct nlattr **attrs,
5698 struct devlink_snapshot *snapshot;
5699 u64 curr_offset = start_offset;
5703 *new_offset = start_offset;
5705 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
5706 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
5710 while (curr_offset < end_offset) {
5714 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
5715 data_size = end_offset - curr_offset;
5717 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
5719 data = &snapshot->data[curr_offset];
5720 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
5726 curr_offset += data_size;
5728 *new_offset = curr_offset;
5733 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
5734 struct netlink_callback *cb)
5736 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
5737 u64 ret_offset, start_offset, end_offset = U64_MAX;
5738 struct nlattr **attrs = info->attrs;
5739 struct devlink_port *port = NULL;
5740 struct devlink_region *region;
5741 struct nlattr *chunks_attr;
5742 const char *region_name;
5743 struct devlink *devlink;
5748 start_offset = *((u64 *)&cb->args[0]);
5750 mutex_lock(&devlink_mutex);
5751 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
5752 if (IS_ERR(devlink)) {
5753 err = PTR_ERR(devlink);
5757 mutex_lock(&devlink->lock);
5759 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
5760 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
5765 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
5766 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
5768 port = devlink_port_get_by_index(devlink, index);
5775 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
5778 region = devlink_port_region_get_by_name(port, region_name);
5780 region = devlink_region_get_by_name(devlink, region_name);
5787 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
5788 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
5791 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
5793 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
5794 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
5797 if (end_offset > region->size)
5798 end_offset = region->size;
5800 /* return 0 if there is no further data to read */
5801 if (start_offset == end_offset) {
5806 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5807 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
5808 DEVLINK_CMD_REGION_READ);
5814 err = devlink_nl_put_handle(skb, devlink);
5816 goto nla_put_failure;
5819 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
5820 region->port->index);
5822 goto nla_put_failure;
5825 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
5827 goto nla_put_failure;
5829 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
5832 goto nla_put_failure;
5835 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
5838 end_offset, &ret_offset);
5840 if (err && err != -EMSGSIZE)
5841 goto nla_put_failure;
5843 /* Check if there was any progress done to prevent infinite loop */
5844 if (ret_offset == start_offset) {
5846 goto nla_put_failure;
5849 *((u64 *)&cb->args[0]) = ret_offset;
5851 nla_nest_end(skb, chunks_attr);
5852 genlmsg_end(skb, hdr);
5853 mutex_unlock(&devlink->lock);
5854 devlink_put(devlink);
5855 mutex_unlock(&devlink_mutex);
5860 genlmsg_cancel(skb, hdr);
5862 mutex_unlock(&devlink->lock);
5863 devlink_put(devlink);
5865 mutex_unlock(&devlink_mutex);
5869 struct devlink_info_req {
5870 struct sk_buff *msg;
5873 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
5875 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
5877 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
5879 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
5881 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
5883 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
5885 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
5888 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
5891 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
5893 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
5894 const char *version_name,
5895 const char *version_value)
5897 struct nlattr *nest;
5900 nest = nla_nest_start_noflag(req->msg, attr);
5904 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
5907 goto nla_put_failure;
5909 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
5912 goto nla_put_failure;
5914 nla_nest_end(req->msg, nest);
5919 nla_nest_cancel(req->msg, nest);
5923 int devlink_info_version_fixed_put(struct devlink_info_req *req,
5924 const char *version_name,
5925 const char *version_value)
5927 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
5928 version_name, version_value);
5930 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
5932 int devlink_info_version_stored_put(struct devlink_info_req *req,
5933 const char *version_name,
5934 const char *version_value)
5936 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
5937 version_name, version_value);
5939 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
5941 int devlink_info_version_running_put(struct devlink_info_req *req,
5942 const char *version_name,
5943 const char *version_value)
5945 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
5946 version_name, version_value);
5948 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
5951 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
5952 enum devlink_command cmd, u32 portid,
5953 u32 seq, int flags, struct netlink_ext_ack *extack)
5955 struct devlink_info_req req;
5959 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5964 if (devlink_nl_put_handle(msg, devlink))
5965 goto err_cancel_msg;
5968 err = devlink->ops->info_get(devlink, &req, extack);
5970 goto err_cancel_msg;
5972 genlmsg_end(msg, hdr);
5976 genlmsg_cancel(msg, hdr);
5980 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
5981 struct genl_info *info)
5983 struct devlink *devlink = info->user_ptr[0];
5984 struct sk_buff *msg;
5987 if (!devlink->ops->info_get)
5990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5994 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5995 info->snd_portid, info->snd_seq, 0,
6002 return genlmsg_reply(msg, info);
6005 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
6006 struct netlink_callback *cb)
6008 struct devlink *devlink;
6009 int start = cb->args[0];
6010 unsigned long index;
6014 mutex_lock(&devlink_mutex);
6015 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
6016 if (!devlink_try_get(devlink))
6019 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6022 if (idx < start || !devlink->ops->info_get)
6025 mutex_lock(&devlink->lock);
6026 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
6027 NETLINK_CB(cb->skb).portid,
6028 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6030 mutex_unlock(&devlink->lock);
6031 if (err == -EOPNOTSUPP)
6034 devlink_put(devlink);
6040 devlink_put(devlink);
6042 mutex_unlock(&devlink_mutex);
6044 if (err != -EMSGSIZE)
6051 struct devlink_fmsg_item {
6052 struct list_head list;
6059 struct devlink_fmsg {
6060 struct list_head item_list;
6061 bool putting_binary; /* This flag forces enclosing of binary data
6062 * in an array brackets. It forces using
6063 * of designated API:
6064 * devlink_fmsg_binary_pair_nest_start()
6065 * devlink_fmsg_binary_pair_nest_end()
6069 static struct devlink_fmsg *devlink_fmsg_alloc(void)
6071 struct devlink_fmsg *fmsg;
6073 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
6077 INIT_LIST_HEAD(&fmsg->item_list);
6082 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
6084 struct devlink_fmsg_item *item, *tmp;
6086 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
6087 list_del(&item->list);
6093 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
6096 struct devlink_fmsg_item *item;
6098 item = kzalloc(sizeof(*item), GFP_KERNEL);
6102 item->attrtype = attrtype;
6103 list_add_tail(&item->list, &fmsg->item_list);
6108 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
6110 if (fmsg->putting_binary)
6113 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
6115 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
6117 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
6119 if (fmsg->putting_binary)
6122 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
6125 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
6127 if (fmsg->putting_binary)
6130 return devlink_fmsg_nest_end(fmsg);
6132 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
6134 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
6136 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
6138 struct devlink_fmsg_item *item;
6140 if (fmsg->putting_binary)
6143 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
6146 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
6150 item->nla_type = NLA_NUL_STRING;
6151 item->len = strlen(name) + 1;
6152 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
6153 memcpy(&item->value, name, item->len);
6154 list_add_tail(&item->list, &fmsg->item_list);
6159 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
6163 if (fmsg->putting_binary)
6166 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
6170 err = devlink_fmsg_put_name(fmsg, name);
6176 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
6178 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
6180 if (fmsg->putting_binary)
6183 return devlink_fmsg_nest_end(fmsg);
6185 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
6187 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
6192 if (fmsg->putting_binary)
6195 err = devlink_fmsg_pair_nest_start(fmsg, name);
6199 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
6205 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
6207 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
6211 if (fmsg->putting_binary)
6214 err = devlink_fmsg_nest_end(fmsg);
6218 err = devlink_fmsg_nest_end(fmsg);
6224 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
6226 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
6231 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
6235 fmsg->putting_binary = true;
6238 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
6240 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
6242 if (!fmsg->putting_binary)
6245 fmsg->putting_binary = false;
6246 return devlink_fmsg_arr_pair_nest_end(fmsg);
6248 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
6250 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
6251 const void *value, u16 value_len,
6254 struct devlink_fmsg_item *item;
6256 if (value_len > DEVLINK_FMSG_MAX_SIZE)
6259 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
6263 item->nla_type = value_nla_type;
6264 item->len = value_len;
6265 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6266 memcpy(&item->value, value, item->len);
6267 list_add_tail(&item->list, &fmsg->item_list);
6272 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
6274 if (fmsg->putting_binary)
6277 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
6279 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
6281 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
6283 if (fmsg->putting_binary)
6286 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
6288 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
6290 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
6292 if (fmsg->putting_binary)
6295 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
6297 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
6299 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
6301 if (fmsg->putting_binary)
6304 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
6306 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
6308 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
6310 if (fmsg->putting_binary)
6313 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
6316 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
6318 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
6321 if (!fmsg->putting_binary)
6324 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
6326 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
6328 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
6333 err = devlink_fmsg_pair_nest_start(fmsg, name);
6337 err = devlink_fmsg_bool_put(fmsg, value);
6341 err = devlink_fmsg_pair_nest_end(fmsg);
6347 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
6349 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
6354 err = devlink_fmsg_pair_nest_start(fmsg, name);
6358 err = devlink_fmsg_u8_put(fmsg, value);
6362 err = devlink_fmsg_pair_nest_end(fmsg);
6368 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
6370 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
6375 err = devlink_fmsg_pair_nest_start(fmsg, name);
6379 err = devlink_fmsg_u32_put(fmsg, value);
6383 err = devlink_fmsg_pair_nest_end(fmsg);
6389 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
6391 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
6396 err = devlink_fmsg_pair_nest_start(fmsg, name);
6400 err = devlink_fmsg_u64_put(fmsg, value);
6404 err = devlink_fmsg_pair_nest_end(fmsg);
6410 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
6412 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
6417 err = devlink_fmsg_pair_nest_start(fmsg, name);
6421 err = devlink_fmsg_string_put(fmsg, value);
6425 err = devlink_fmsg_pair_nest_end(fmsg);
6431 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
6433 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
6434 const void *value, u32 value_len)
6441 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
6445 for (offset = 0; offset < value_len; offset += data_size) {
6446 data_size = value_len - offset;
6447 if (data_size > DEVLINK_FMSG_MAX_SIZE)
6448 data_size = DEVLINK_FMSG_MAX_SIZE;
6449 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
6452 /* Exit from loop with a break (instead of
6453 * return) to make sure putting_binary is turned off in
6454 * devlink_fmsg_binary_pair_nest_end
6458 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
6464 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
6467 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
6469 switch (msg->nla_type) {
6474 case NLA_NUL_STRING:
6476 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
6484 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
6486 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
6489 switch (msg->nla_type) {
6491 /* Always provide flag data, regardless of its value */
6492 tmp = *(bool *) msg->value;
6494 return nla_put_u8(skb, attrtype, tmp);
6496 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
6498 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
6500 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
6502 case NLA_NUL_STRING:
6503 return nla_put_string(skb, attrtype, (char *) &msg->value);
6505 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
6512 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
6515 struct devlink_fmsg_item *item;
6516 struct nlattr *fmsg_nlattr;
6520 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
6524 list_for_each_entry(item, &fmsg->item_list, list) {
6530 switch (item->attrtype) {
6531 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
6532 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
6533 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
6534 case DEVLINK_ATTR_FMSG_NEST_END:
6535 err = nla_put_flag(skb, item->attrtype);
6537 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
6538 err = devlink_fmsg_item_fill_type(item, skb);
6541 err = devlink_fmsg_item_fill_data(item, skb);
6543 case DEVLINK_ATTR_FMSG_OBJ_NAME:
6544 err = nla_put_string(skb, item->attrtype,
6545 (char *) &item->value);
6557 nla_nest_end(skb, fmsg_nlattr);
6561 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
6562 struct genl_info *info,
6563 enum devlink_command cmd, int flags)
6565 struct nlmsghdr *nlh;
6566 struct sk_buff *skb;
6573 int tmp_index = index;
6575 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
6579 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
6580 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
6583 goto nla_put_failure;
6586 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
6589 else if (err != -EMSGSIZE || tmp_index == index)
6590 goto nla_put_failure;
6592 genlmsg_end(skb, hdr);
6593 err = genlmsg_reply(skb, info);
6598 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
6601 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
6602 NLMSG_DONE, 0, flags | NLM_F_MULTI);
6605 goto nla_put_failure;
6608 return genlmsg_reply(skb, info);
6615 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
6616 struct netlink_callback *cb,
6617 enum devlink_command cmd)
6619 int index = cb->args[0];
6620 int tmp_index = index;
6624 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
6625 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
6628 goto nla_put_failure;
6631 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
6632 if ((err && err != -EMSGSIZE) || tmp_index == index)
6633 goto nla_put_failure;
6635 cb->args[0] = index;
6636 genlmsg_end(skb, hdr);
6640 genlmsg_cancel(skb, hdr);
6644 struct devlink_health_reporter {
6645 struct list_head list;
6647 const struct devlink_health_reporter_ops *ops;
6648 struct devlink *devlink;
6649 struct devlink_port *devlink_port;
6650 struct devlink_fmsg *dump_fmsg;
6651 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
6652 u64 graceful_period;
6660 u64 last_recovery_ts;
6661 refcount_t refcount;
6665 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
6667 return reporter->priv;
6669 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
6671 static struct devlink_health_reporter *
6672 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
6673 struct mutex *list_lock,
6674 const char *reporter_name)
6676 struct devlink_health_reporter *reporter;
6678 lockdep_assert_held(list_lock);
6679 list_for_each_entry(reporter, reporter_list, list)
6680 if (!strcmp(reporter->ops->name, reporter_name))
6685 static struct devlink_health_reporter *
6686 devlink_health_reporter_find_by_name(struct devlink *devlink,
6687 const char *reporter_name)
6689 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
6690 &devlink->reporters_lock,
6694 static struct devlink_health_reporter *
6695 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
6696 const char *reporter_name)
6698 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
6699 &devlink_port->reporters_lock,
6703 static struct devlink_health_reporter *
6704 __devlink_health_reporter_create(struct devlink *devlink,
6705 const struct devlink_health_reporter_ops *ops,
6706 u64 graceful_period, void *priv)
6708 struct devlink_health_reporter *reporter;
6710 if (WARN_ON(graceful_period && !ops->recover))
6711 return ERR_PTR(-EINVAL);
6713 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
6715 return ERR_PTR(-ENOMEM);
6717 reporter->priv = priv;
6718 reporter->ops = ops;
6719 reporter->devlink = devlink;
6720 reporter->graceful_period = graceful_period;
6721 reporter->auto_recover = !!ops->recover;
6722 reporter->auto_dump = !!ops->dump;
6723 mutex_init(&reporter->dump_lock);
6724 refcount_set(&reporter->refcount, 1);
6729 * devlink_port_health_reporter_create - create devlink health reporter for
6730 * specified port instance
6732 * @port: devlink_port which should contain the new reporter
6734 * @graceful_period: to avoid recovery loops, in msecs
6737 struct devlink_health_reporter *
6738 devlink_port_health_reporter_create(struct devlink_port *port,
6739 const struct devlink_health_reporter_ops *ops,
6740 u64 graceful_period, void *priv)
6742 struct devlink_health_reporter *reporter;
6744 mutex_lock(&port->reporters_lock);
6745 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
6746 &port->reporters_lock, ops->name)) {
6747 reporter = ERR_PTR(-EEXIST);
6751 reporter = __devlink_health_reporter_create(port->devlink, ops,
6752 graceful_period, priv);
6753 if (IS_ERR(reporter))
6756 reporter->devlink_port = port;
6757 list_add_tail(&reporter->list, &port->reporter_list);
6759 mutex_unlock(&port->reporters_lock);
6762 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
6765 * devlink_health_reporter_create - create devlink health reporter
6769 * @graceful_period: to avoid recovery loops, in msecs
6772 struct devlink_health_reporter *
6773 devlink_health_reporter_create(struct devlink *devlink,
6774 const struct devlink_health_reporter_ops *ops,
6775 u64 graceful_period, void *priv)
6777 struct devlink_health_reporter *reporter;
6779 mutex_lock(&devlink->reporters_lock);
6780 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
6781 reporter = ERR_PTR(-EEXIST);
6785 reporter = __devlink_health_reporter_create(devlink, ops,
6786 graceful_period, priv);
6787 if (IS_ERR(reporter))
6790 list_add_tail(&reporter->list, &devlink->reporter_list);
6792 mutex_unlock(&devlink->reporters_lock);
6795 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
6798 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
6800 mutex_destroy(&reporter->dump_lock);
6801 if (reporter->dump_fmsg)
6802 devlink_fmsg_free(reporter->dump_fmsg);
6807 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
6809 if (refcount_dec_and_test(&reporter->refcount))
6810 devlink_health_reporter_free(reporter);
6814 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
6816 list_del(&reporter->list);
6817 devlink_health_reporter_put(reporter);
6821 * devlink_health_reporter_destroy - destroy devlink health reporter
6823 * @reporter: devlink health reporter to destroy
6826 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
6828 struct mutex *lock = &reporter->devlink->reporters_lock;
6831 __devlink_health_reporter_destroy(reporter);
6834 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
6837 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
6839 * @reporter: devlink health reporter to destroy
6842 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
6844 struct mutex *lock = &reporter->devlink_port->reporters_lock;
6847 __devlink_health_reporter_destroy(reporter);
6850 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
6853 devlink_nl_health_reporter_fill(struct sk_buff *msg,
6854 struct devlink_health_reporter *reporter,
6855 enum devlink_command cmd, u32 portid,
6858 struct devlink *devlink = reporter->devlink;
6859 struct nlattr *reporter_attr;
6862 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6866 if (devlink_nl_put_handle(msg, devlink))
6867 goto genlmsg_cancel;
6869 if (reporter->devlink_port) {
6870 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
6871 goto genlmsg_cancel;
6873 reporter_attr = nla_nest_start_noflag(msg,
6874 DEVLINK_ATTR_HEALTH_REPORTER);
6876 goto genlmsg_cancel;
6877 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
6878 reporter->ops->name))
6879 goto reporter_nest_cancel;
6880 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
6881 reporter->health_state))
6882 goto reporter_nest_cancel;
6883 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
6884 reporter->error_count, DEVLINK_ATTR_PAD))
6885 goto reporter_nest_cancel;
6886 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
6887 reporter->recovery_count, DEVLINK_ATTR_PAD))
6888 goto reporter_nest_cancel;
6889 if (reporter->ops->recover &&
6890 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
6891 reporter->graceful_period,
6893 goto reporter_nest_cancel;
6894 if (reporter->ops->recover &&
6895 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
6896 reporter->auto_recover))
6897 goto reporter_nest_cancel;
6898 if (reporter->dump_fmsg &&
6899 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
6900 jiffies_to_msecs(reporter->dump_ts),
6902 goto reporter_nest_cancel;
6903 if (reporter->dump_fmsg &&
6904 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
6905 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
6906 goto reporter_nest_cancel;
6907 if (reporter->ops->dump &&
6908 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
6909 reporter->auto_dump))
6910 goto reporter_nest_cancel;
6912 nla_nest_end(msg, reporter_attr);
6913 genlmsg_end(msg, hdr);
6916 reporter_nest_cancel:
6917 nla_nest_end(msg, reporter_attr);
6919 genlmsg_cancel(msg, hdr);
6923 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
6924 enum devlink_command cmd)
6926 struct sk_buff *msg;
6929 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6931 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6935 err = devlink_nl_health_reporter_fill(msg, reporter, cmd, 0, 0, 0);
6941 genlmsg_multicast_netns(&devlink_nl_family,
6942 devlink_net(reporter->devlink),
6943 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
6947 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
6949 reporter->recovery_count++;
6950 reporter->last_recovery_ts = jiffies;
6952 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
6955 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
6956 void *priv_ctx, struct netlink_ext_ack *extack)
6960 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
6963 if (!reporter->ops->recover)
6966 err = reporter->ops->recover(reporter, priv_ctx, extack);
6970 devlink_health_reporter_recovery_done(reporter);
6971 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
6972 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6978 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
6980 if (!reporter->dump_fmsg)
6982 devlink_fmsg_free(reporter->dump_fmsg);
6983 reporter->dump_fmsg = NULL;
6986 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
6988 struct netlink_ext_ack *extack)
6992 if (!reporter->ops->dump)
6995 if (reporter->dump_fmsg)
6998 reporter->dump_fmsg = devlink_fmsg_alloc();
6999 if (!reporter->dump_fmsg) {
7004 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
7008 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
7013 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
7017 reporter->dump_ts = jiffies;
7018 reporter->dump_real_ts = ktime_get_real_ns();
7023 devlink_health_dump_clear(reporter);
7027 int devlink_health_report(struct devlink_health_reporter *reporter,
7028 const char *msg, void *priv_ctx)
7030 enum devlink_health_reporter_state prev_health_state;
7031 struct devlink *devlink = reporter->devlink;
7032 unsigned long recover_ts_threshold;
7034 /* write a log message of the current error */
7036 trace_devlink_health_report(devlink, reporter->ops->name, msg);
7037 reporter->error_count++;
7038 prev_health_state = reporter->health_state;
7039 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7040 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7042 /* abort if the previous error wasn't recovered */
7043 recover_ts_threshold = reporter->last_recovery_ts +
7044 msecs_to_jiffies(reporter->graceful_period);
7045 if (reporter->auto_recover &&
7046 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
7047 (reporter->last_recovery_ts && reporter->recovery_count &&
7048 time_is_after_jiffies(recover_ts_threshold)))) {
7049 trace_devlink_health_recover_aborted(devlink,
7050 reporter->ops->name,
7051 reporter->health_state,
7053 reporter->last_recovery_ts);
7057 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
7059 if (reporter->auto_dump) {
7060 mutex_lock(&reporter->dump_lock);
7061 /* store current dump of current error, for later analysis */
7062 devlink_health_do_dump(reporter, priv_ctx, NULL);
7063 mutex_unlock(&reporter->dump_lock);
7066 if (reporter->auto_recover)
7067 return devlink_health_reporter_recover(reporter,
7072 EXPORT_SYMBOL_GPL(devlink_health_report);
7074 static struct devlink_health_reporter *
7075 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
7076 struct nlattr **attrs)
7078 struct devlink_health_reporter *reporter;
7079 struct devlink_port *devlink_port;
7080 char *reporter_name;
7082 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
7085 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
7086 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
7087 if (IS_ERR(devlink_port)) {
7088 mutex_lock(&devlink->reporters_lock);
7089 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
7091 refcount_inc(&reporter->refcount);
7092 mutex_unlock(&devlink->reporters_lock);
7094 mutex_lock(&devlink_port->reporters_lock);
7095 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
7097 refcount_inc(&reporter->refcount);
7098 mutex_unlock(&devlink_port->reporters_lock);
7104 static struct devlink_health_reporter *
7105 devlink_health_reporter_get_from_info(struct devlink *devlink,
7106 struct genl_info *info)
7108 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
7111 static struct devlink_health_reporter *
7112 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
7114 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
7115 struct devlink_health_reporter *reporter;
7116 struct nlattr **attrs = info->attrs;
7117 struct devlink *devlink;
7119 mutex_lock(&devlink_mutex);
7120 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
7121 if (IS_ERR(devlink))
7124 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
7125 devlink_put(devlink);
7126 mutex_unlock(&devlink_mutex);
7129 mutex_unlock(&devlink_mutex);
7134 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
7135 enum devlink_health_reporter_state state)
7137 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
7138 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
7141 if (reporter->health_state == state)
7144 reporter->health_state = state;
7145 trace_devlink_health_reporter_state_update(reporter->devlink,
7146 reporter->ops->name, state);
7147 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
7149 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
7151 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
7152 struct genl_info *info)
7154 struct devlink *devlink = info->user_ptr[0];
7155 struct devlink_health_reporter *reporter;
7156 struct sk_buff *msg;
7159 reporter = devlink_health_reporter_get_from_info(devlink, info);
7163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7169 err = devlink_nl_health_reporter_fill(msg, reporter,
7170 DEVLINK_CMD_HEALTH_REPORTER_GET,
7171 info->snd_portid, info->snd_seq,
7178 err = genlmsg_reply(msg, info);
7180 devlink_health_reporter_put(reporter);
7185 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
7186 struct netlink_callback *cb)
7188 struct devlink_health_reporter *reporter;
7189 struct devlink_port *port;
7190 struct devlink *devlink;
7191 int start = cb->args[0];
7192 unsigned long index;
7196 mutex_lock(&devlink_mutex);
7197 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7198 if (!devlink_try_get(devlink))
7201 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7204 mutex_lock(&devlink->reporters_lock);
7205 list_for_each_entry(reporter, &devlink->reporter_list,
7211 err = devlink_nl_health_reporter_fill(
7212 msg, reporter, DEVLINK_CMD_HEALTH_REPORTER_GET,
7213 NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
7216 mutex_unlock(&devlink->reporters_lock);
7217 devlink_put(devlink);
7222 mutex_unlock(&devlink->reporters_lock);
7224 devlink_put(devlink);
7227 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7228 if (!devlink_try_get(devlink))
7231 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7234 mutex_lock(&devlink->lock);
7235 list_for_each_entry(port, &devlink->port_list, list) {
7236 mutex_lock(&port->reporters_lock);
7237 list_for_each_entry(reporter, &port->reporter_list, list) {
7242 err = devlink_nl_health_reporter_fill(
7244 DEVLINK_CMD_HEALTH_REPORTER_GET,
7245 NETLINK_CB(cb->skb).portid,
7246 cb->nlh->nlmsg_seq, NLM_F_MULTI);
7248 mutex_unlock(&port->reporters_lock);
7249 mutex_unlock(&devlink->lock);
7250 devlink_put(devlink);
7255 mutex_unlock(&port->reporters_lock);
7257 mutex_unlock(&devlink->lock);
7259 devlink_put(devlink);
7262 mutex_unlock(&devlink_mutex);
7269 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
7270 struct genl_info *info)
7272 struct devlink *devlink = info->user_ptr[0];
7273 struct devlink_health_reporter *reporter;
7276 reporter = devlink_health_reporter_get_from_info(devlink, info);
7280 if (!reporter->ops->recover &&
7281 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
7282 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
7286 if (!reporter->ops->dump &&
7287 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
7292 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
7293 reporter->graceful_period =
7294 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
7296 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
7297 reporter->auto_recover =
7298 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
7300 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
7301 reporter->auto_dump =
7302 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
7304 devlink_health_reporter_put(reporter);
7307 devlink_health_reporter_put(reporter);
7311 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
7312 struct genl_info *info)
7314 struct devlink *devlink = info->user_ptr[0];
7315 struct devlink_health_reporter *reporter;
7318 reporter = devlink_health_reporter_get_from_info(devlink, info);
7322 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
7324 devlink_health_reporter_put(reporter);
7328 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
7329 struct genl_info *info)
7331 struct devlink *devlink = info->user_ptr[0];
7332 struct devlink_health_reporter *reporter;
7333 struct devlink_fmsg *fmsg;
7336 reporter = devlink_health_reporter_get_from_info(devlink, info);
7340 if (!reporter->ops->diagnose) {
7341 devlink_health_reporter_put(reporter);
7345 fmsg = devlink_fmsg_alloc();
7347 devlink_health_reporter_put(reporter);
7351 err = devlink_fmsg_obj_nest_start(fmsg);
7355 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
7359 err = devlink_fmsg_obj_nest_end(fmsg);
7363 err = devlink_fmsg_snd(fmsg, info,
7364 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
7367 devlink_fmsg_free(fmsg);
7368 devlink_health_reporter_put(reporter);
7373 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
7374 struct netlink_callback *cb)
7376 struct devlink_health_reporter *reporter;
7377 u64 start = cb->args[0];
7380 reporter = devlink_health_reporter_get_from_cb(cb);
7384 if (!reporter->ops->dump) {
7388 mutex_lock(&reporter->dump_lock);
7390 err = devlink_health_do_dump(reporter, NULL, cb->extack);
7393 cb->args[1] = reporter->dump_ts;
7395 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
7396 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
7401 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
7402 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
7404 mutex_unlock(&reporter->dump_lock);
7406 devlink_health_reporter_put(reporter);
7411 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
7412 struct genl_info *info)
7414 struct devlink *devlink = info->user_ptr[0];
7415 struct devlink_health_reporter *reporter;
7417 reporter = devlink_health_reporter_get_from_info(devlink, info);
7421 if (!reporter->ops->dump) {
7422 devlink_health_reporter_put(reporter);
7426 mutex_lock(&reporter->dump_lock);
7427 devlink_health_dump_clear(reporter);
7428 mutex_unlock(&reporter->dump_lock);
7429 devlink_health_reporter_put(reporter);
7433 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
7434 struct genl_info *info)
7436 struct devlink *devlink = info->user_ptr[0];
7437 struct devlink_health_reporter *reporter;
7440 reporter = devlink_health_reporter_get_from_info(devlink, info);
7444 if (!reporter->ops->test) {
7445 devlink_health_reporter_put(reporter);
7449 err = reporter->ops->test(reporter, info->extack);
7451 devlink_health_reporter_put(reporter);
7455 struct devlink_stats {
7458 struct u64_stats_sync syncp;
7462 * struct devlink_trap_policer_item - Packet trap policer attributes.
7463 * @policer: Immutable packet trap policer attributes.
7464 * @rate: Rate in packets / sec.
7465 * @burst: Burst size in packets.
7466 * @list: trap_policer_list member.
7468 * Describes packet trap policer attributes. Created by devlink during trap
7469 * policer registration.
7471 struct devlink_trap_policer_item {
7472 const struct devlink_trap_policer *policer;
7475 struct list_head list;
7479 * struct devlink_trap_group_item - Packet trap group attributes.
7480 * @group: Immutable packet trap group attributes.
7481 * @policer_item: Associated policer item. Can be NULL.
7482 * @list: trap_group_list member.
7483 * @stats: Trap group statistics.
7485 * Describes packet trap group attributes. Created by devlink during trap
7486 * group registration.
7488 struct devlink_trap_group_item {
7489 const struct devlink_trap_group *group;
7490 struct devlink_trap_policer_item *policer_item;
7491 struct list_head list;
7492 struct devlink_stats __percpu *stats;
7496 * struct devlink_trap_item - Packet trap attributes.
7497 * @trap: Immutable packet trap attributes.
7498 * @group_item: Associated group item.
7499 * @list: trap_list member.
7500 * @action: Trap action.
7501 * @stats: Trap statistics.
7502 * @priv: Driver private information.
7504 * Describes both mutable and immutable packet trap attributes. Created by
7505 * devlink during trap registration and used for all trap related operations.
7507 struct devlink_trap_item {
7508 const struct devlink_trap *trap;
7509 struct devlink_trap_group_item *group_item;
7510 struct list_head list;
7511 enum devlink_trap_action action;
7512 struct devlink_stats __percpu *stats;
7516 static struct devlink_trap_policer_item *
7517 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
7519 struct devlink_trap_policer_item *policer_item;
7521 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
7522 if (policer_item->policer->id == id)
7523 return policer_item;
7529 static struct devlink_trap_item *
7530 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
7532 struct devlink_trap_item *trap_item;
7534 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7535 if (!strcmp(trap_item->trap->name, name))
7542 static struct devlink_trap_item *
7543 devlink_trap_item_get_from_info(struct devlink *devlink,
7544 struct genl_info *info)
7546 struct nlattr *attr;
7548 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
7550 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
7552 return devlink_trap_item_lookup(devlink, nla_data(attr));
7556 devlink_trap_action_get_from_info(struct genl_info *info,
7557 enum devlink_trap_action *p_trap_action)
7561 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
7563 case DEVLINK_TRAP_ACTION_DROP:
7564 case DEVLINK_TRAP_ACTION_TRAP:
7565 case DEVLINK_TRAP_ACTION_MIRROR:
7566 *p_trap_action = val;
7575 static int devlink_trap_metadata_put(struct sk_buff *msg,
7576 const struct devlink_trap *trap)
7578 struct nlattr *attr;
7580 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
7584 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
7585 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
7586 goto nla_put_failure;
7587 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
7588 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
7589 goto nla_put_failure;
7591 nla_nest_end(msg, attr);
7596 nla_nest_cancel(msg, attr);
7600 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
7601 struct devlink_stats *stats)
7605 memset(stats, 0, sizeof(*stats));
7606 for_each_possible_cpu(i) {
7607 struct devlink_stats *cpu_stats;
7608 u64 rx_packets, rx_bytes;
7611 cpu_stats = per_cpu_ptr(trap_stats, i);
7613 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7614 rx_packets = cpu_stats->rx_packets;
7615 rx_bytes = cpu_stats->rx_bytes;
7616 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7618 stats->rx_packets += rx_packets;
7619 stats->rx_bytes += rx_bytes;
7624 devlink_trap_group_stats_put(struct sk_buff *msg,
7625 struct devlink_stats __percpu *trap_stats)
7627 struct devlink_stats stats;
7628 struct nlattr *attr;
7630 devlink_trap_stats_read(trap_stats, &stats);
7632 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7636 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
7637 stats.rx_packets, DEVLINK_ATTR_PAD))
7638 goto nla_put_failure;
7640 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
7641 stats.rx_bytes, DEVLINK_ATTR_PAD))
7642 goto nla_put_failure;
7644 nla_nest_end(msg, attr);
7649 nla_nest_cancel(msg, attr);
7653 static int devlink_trap_stats_put(struct sk_buff *msg, struct devlink *devlink,
7654 const struct devlink_trap_item *trap_item)
7656 struct devlink_stats stats;
7657 struct nlattr *attr;
7661 if (devlink->ops->trap_drop_counter_get) {
7662 err = devlink->ops->trap_drop_counter_get(devlink,
7669 devlink_trap_stats_read(trap_item->stats, &stats);
7671 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7675 if (devlink->ops->trap_drop_counter_get &&
7676 nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
7678 goto nla_put_failure;
7680 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
7681 stats.rx_packets, DEVLINK_ATTR_PAD))
7682 goto nla_put_failure;
7684 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
7685 stats.rx_bytes, DEVLINK_ATTR_PAD))
7686 goto nla_put_failure;
7688 nla_nest_end(msg, attr);
7693 nla_nest_cancel(msg, attr);
7697 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
7698 const struct devlink_trap_item *trap_item,
7699 enum devlink_command cmd, u32 portid, u32 seq,
7702 struct devlink_trap_group_item *group_item = trap_item->group_item;
7706 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7710 if (devlink_nl_put_handle(msg, devlink))
7711 goto nla_put_failure;
7713 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7714 group_item->group->name))
7715 goto nla_put_failure;
7717 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
7718 goto nla_put_failure;
7720 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
7721 goto nla_put_failure;
7723 if (trap_item->trap->generic &&
7724 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7725 goto nla_put_failure;
7727 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
7728 goto nla_put_failure;
7730 err = devlink_trap_metadata_put(msg, trap_item->trap);
7732 goto nla_put_failure;
7734 err = devlink_trap_stats_put(msg, devlink, trap_item);
7736 goto nla_put_failure;
7738 genlmsg_end(msg, hdr);
7743 genlmsg_cancel(msg, hdr);
7747 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
7748 struct genl_info *info)
7750 struct netlink_ext_ack *extack = info->extack;
7751 struct devlink *devlink = info->user_ptr[0];
7752 struct devlink_trap_item *trap_item;
7753 struct sk_buff *msg;
7756 if (list_empty(&devlink->trap_list))
7759 trap_item = devlink_trap_item_get_from_info(devlink, info);
7761 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
7765 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7769 err = devlink_nl_trap_fill(msg, devlink, trap_item,
7770 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
7775 return genlmsg_reply(msg, info);
7782 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
7783 struct netlink_callback *cb)
7785 struct devlink_trap_item *trap_item;
7786 struct devlink *devlink;
7787 int start = cb->args[0];
7788 unsigned long index;
7792 mutex_lock(&devlink_mutex);
7793 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
7794 if (!devlink_try_get(devlink))
7797 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7800 mutex_lock(&devlink->lock);
7801 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7806 err = devlink_nl_trap_fill(msg, devlink, trap_item,
7807 DEVLINK_CMD_TRAP_NEW,
7808 NETLINK_CB(cb->skb).portid,
7812 mutex_unlock(&devlink->lock);
7813 devlink_put(devlink);
7818 mutex_unlock(&devlink->lock);
7820 devlink_put(devlink);
7823 mutex_unlock(&devlink_mutex);
7829 static int __devlink_trap_action_set(struct devlink *devlink,
7830 struct devlink_trap_item *trap_item,
7831 enum devlink_trap_action trap_action,
7832 struct netlink_ext_ack *extack)
7836 if (trap_item->action != trap_action &&
7837 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
7838 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
7842 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
7843 trap_action, extack);
7847 trap_item->action = trap_action;
7852 static int devlink_trap_action_set(struct devlink *devlink,
7853 struct devlink_trap_item *trap_item,
7854 struct genl_info *info)
7856 enum devlink_trap_action trap_action;
7859 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
7862 err = devlink_trap_action_get_from_info(info, &trap_action);
7864 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
7868 return __devlink_trap_action_set(devlink, trap_item, trap_action,
7872 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
7873 struct genl_info *info)
7875 struct netlink_ext_ack *extack = info->extack;
7876 struct devlink *devlink = info->user_ptr[0];
7877 struct devlink_trap_item *trap_item;
7879 if (list_empty(&devlink->trap_list))
7882 trap_item = devlink_trap_item_get_from_info(devlink, info);
7884 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
7888 return devlink_trap_action_set(devlink, trap_item, info);
7891 static struct devlink_trap_group_item *
7892 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
7894 struct devlink_trap_group_item *group_item;
7896 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
7897 if (!strcmp(group_item->group->name, name))
7904 static struct devlink_trap_group_item *
7905 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
7907 struct devlink_trap_group_item *group_item;
7909 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
7910 if (group_item->group->id == id)
7917 static struct devlink_trap_group_item *
7918 devlink_trap_group_item_get_from_info(struct devlink *devlink,
7919 struct genl_info *info)
7923 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
7925 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
7927 return devlink_trap_group_item_lookup(devlink, name);
7931 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
7932 const struct devlink_trap_group_item *group_item,
7933 enum devlink_command cmd, u32 portid, u32 seq,
7939 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7943 if (devlink_nl_put_handle(msg, devlink))
7944 goto nla_put_failure;
7946 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7947 group_item->group->name))
7948 goto nla_put_failure;
7950 if (group_item->group->generic &&
7951 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7952 goto nla_put_failure;
7954 if (group_item->policer_item &&
7955 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7956 group_item->policer_item->policer->id))
7957 goto nla_put_failure;
7959 err = devlink_trap_group_stats_put(msg, group_item->stats);
7961 goto nla_put_failure;
7963 genlmsg_end(msg, hdr);
7968 genlmsg_cancel(msg, hdr);
7972 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
7973 struct genl_info *info)
7975 struct netlink_ext_ack *extack = info->extack;
7976 struct devlink *devlink = info->user_ptr[0];
7977 struct devlink_trap_group_item *group_item;
7978 struct sk_buff *msg;
7981 if (list_empty(&devlink->trap_group_list))
7984 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7986 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7994 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
7995 DEVLINK_CMD_TRAP_GROUP_NEW,
7996 info->snd_portid, info->snd_seq, 0);
7998 goto err_trap_group_fill;
8000 return genlmsg_reply(msg, info);
8002 err_trap_group_fill:
8007 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
8008 struct netlink_callback *cb)
8010 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
8011 struct devlink_trap_group_item *group_item;
8012 u32 portid = NETLINK_CB(cb->skb).portid;
8013 struct devlink *devlink;
8014 int start = cb->args[0];
8015 unsigned long index;
8019 mutex_lock(&devlink_mutex);
8020 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8021 if (!devlink_try_get(devlink))
8024 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8027 mutex_lock(&devlink->lock);
8028 list_for_each_entry(group_item, &devlink->trap_group_list,
8034 err = devlink_nl_trap_group_fill(msg, devlink,
8040 mutex_unlock(&devlink->lock);
8041 devlink_put(devlink);
8046 mutex_unlock(&devlink->lock);
8048 devlink_put(devlink);
8051 mutex_unlock(&devlink_mutex);
8058 __devlink_trap_group_action_set(struct devlink *devlink,
8059 struct devlink_trap_group_item *group_item,
8060 enum devlink_trap_action trap_action,
8061 struct netlink_ext_ack *extack)
8063 const char *group_name = group_item->group->name;
8064 struct devlink_trap_item *trap_item;
8067 if (devlink->ops->trap_group_action_set) {
8068 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
8069 trap_action, extack);
8073 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8074 if (strcmp(trap_item->group_item->group->name, group_name))
8076 if (trap_item->action != trap_action &&
8077 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
8079 trap_item->action = trap_action;
8085 list_for_each_entry(trap_item, &devlink->trap_list, list) {
8086 if (strcmp(trap_item->group_item->group->name, group_name))
8088 err = __devlink_trap_action_set(devlink, trap_item,
8089 trap_action, extack);
8098 devlink_trap_group_action_set(struct devlink *devlink,
8099 struct devlink_trap_group_item *group_item,
8100 struct genl_info *info, bool *p_modified)
8102 enum devlink_trap_action trap_action;
8105 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
8108 err = devlink_trap_action_get_from_info(info, &trap_action);
8110 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
8114 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
8124 static int devlink_trap_group_set(struct devlink *devlink,
8125 struct devlink_trap_group_item *group_item,
8126 struct genl_info *info)
8128 struct devlink_trap_policer_item *policer_item;
8129 struct netlink_ext_ack *extack = info->extack;
8130 const struct devlink_trap_policer *policer;
8131 struct nlattr **attrs = info->attrs;
8134 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8137 if (!devlink->ops->trap_group_set)
8140 policer_item = group_item->policer_item;
8141 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
8144 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8145 policer_item = devlink_trap_policer_item_lookup(devlink,
8147 if (policer_id && !policer_item) {
8148 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8152 policer = policer_item ? policer_item->policer : NULL;
8154 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
8159 group_item->policer_item = policer_item;
8164 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
8165 struct genl_info *info)
8167 struct netlink_ext_ack *extack = info->extack;
8168 struct devlink *devlink = info->user_ptr[0];
8169 struct devlink_trap_group_item *group_item;
8170 bool modified = false;
8173 if (list_empty(&devlink->trap_group_list))
8176 group_item = devlink_trap_group_item_get_from_info(devlink, info);
8178 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
8182 err = devlink_trap_group_action_set(devlink, group_item, info,
8187 err = devlink_trap_group_set(devlink, group_item, info);
8189 goto err_trap_group_set;
8195 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
8199 static struct devlink_trap_policer_item *
8200 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
8201 struct genl_info *info)
8205 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
8207 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
8209 return devlink_trap_policer_item_lookup(devlink, id);
8213 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
8214 const struct devlink_trap_policer *policer)
8216 struct nlattr *attr;
8220 if (!devlink->ops->trap_policer_counter_get)
8223 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
8227 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
8231 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
8233 goto nla_put_failure;
8235 nla_nest_end(msg, attr);
8240 nla_nest_cancel(msg, attr);
8245 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
8246 const struct devlink_trap_policer_item *policer_item,
8247 enum devlink_command cmd, u32 portid, u32 seq,
8253 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
8257 if (devlink_nl_put_handle(msg, devlink))
8258 goto nla_put_failure;
8260 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
8261 policer_item->policer->id))
8262 goto nla_put_failure;
8264 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
8265 policer_item->rate, DEVLINK_ATTR_PAD))
8266 goto nla_put_failure;
8268 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
8269 policer_item->burst, DEVLINK_ATTR_PAD))
8270 goto nla_put_failure;
8272 err = devlink_trap_policer_stats_put(msg, devlink,
8273 policer_item->policer);
8275 goto nla_put_failure;
8277 genlmsg_end(msg, hdr);
8282 genlmsg_cancel(msg, hdr);
8286 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
8287 struct genl_info *info)
8289 struct devlink_trap_policer_item *policer_item;
8290 struct netlink_ext_ack *extack = info->extack;
8291 struct devlink *devlink = info->user_ptr[0];
8292 struct sk_buff *msg;
8295 if (list_empty(&devlink->trap_policer_list))
8298 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8299 if (!policer_item) {
8300 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8308 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
8309 DEVLINK_CMD_TRAP_POLICER_NEW,
8310 info->snd_portid, info->snd_seq, 0);
8312 goto err_trap_policer_fill;
8314 return genlmsg_reply(msg, info);
8316 err_trap_policer_fill:
8321 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
8322 struct netlink_callback *cb)
8324 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
8325 struct devlink_trap_policer_item *policer_item;
8326 u32 portid = NETLINK_CB(cb->skb).portid;
8327 struct devlink *devlink;
8328 int start = cb->args[0];
8329 unsigned long index;
8333 mutex_lock(&devlink_mutex);
8334 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
8335 if (!devlink_try_get(devlink))
8338 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
8341 mutex_lock(&devlink->lock);
8342 list_for_each_entry(policer_item, &devlink->trap_policer_list,
8348 err = devlink_nl_trap_policer_fill(msg, devlink,
8354 mutex_unlock(&devlink->lock);
8355 devlink_put(devlink);
8360 mutex_unlock(&devlink->lock);
8362 devlink_put(devlink);
8365 mutex_unlock(&devlink_mutex);
8372 devlink_trap_policer_set(struct devlink *devlink,
8373 struct devlink_trap_policer_item *policer_item,
8374 struct genl_info *info)
8376 struct netlink_ext_ack *extack = info->extack;
8377 struct nlattr **attrs = info->attrs;
8381 rate = policer_item->rate;
8382 burst = policer_item->burst;
8384 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
8385 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
8387 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
8388 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
8390 if (rate < policer_item->policer->min_rate) {
8391 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
8395 if (rate > policer_item->policer->max_rate) {
8396 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
8400 if (burst < policer_item->policer->min_burst) {
8401 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
8405 if (burst > policer_item->policer->max_burst) {
8406 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
8410 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
8411 rate, burst, info->extack);
8415 policer_item->rate = rate;
8416 policer_item->burst = burst;
8421 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
8422 struct genl_info *info)
8424 struct devlink_trap_policer_item *policer_item;
8425 struct netlink_ext_ack *extack = info->extack;
8426 struct devlink *devlink = info->user_ptr[0];
8428 if (list_empty(&devlink->trap_policer_list))
8431 if (!devlink->ops->trap_policer_set)
8434 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
8435 if (!policer_item) {
8436 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
8440 return devlink_trap_policer_set(devlink, policer_item, info);
8443 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
8444 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
8445 DEVLINK_ATTR_TRAP_POLICER_ID },
8446 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
8447 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
8448 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
8449 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
8450 DEVLINK_PORT_TYPE_IB),
8451 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
8452 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
8453 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
8454 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
8455 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
8456 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
8457 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
8458 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
8459 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
8460 DEVLINK_ESWITCH_MODE_SWITCHDEV),
8461 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
8462 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
8463 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
8464 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
8465 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
8466 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
8467 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
8468 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
8469 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
8470 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
8471 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
8472 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
8473 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
8474 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
8475 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
8476 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
8477 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
8478 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
8479 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
8480 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
8481 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
8482 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
8483 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
8484 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
8485 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
8486 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
8487 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
8488 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
8489 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
8490 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
8491 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
8492 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
8493 DEVLINK_RELOAD_ACTION_MAX),
8494 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
8495 [DEVLINK_ATTR_PORT_FLAVOUR] = { .type = NLA_U16 },
8496 [DEVLINK_ATTR_PORT_PCI_PF_NUMBER] = { .type = NLA_U16 },
8497 [DEVLINK_ATTR_PORT_PCI_SF_NUMBER] = { .type = NLA_U32 },
8498 [DEVLINK_ATTR_PORT_CONTROLLER_NUMBER] = { .type = NLA_U32 },
8499 [DEVLINK_ATTR_RATE_TYPE] = { .type = NLA_U16 },
8500 [DEVLINK_ATTR_RATE_TX_SHARE] = { .type = NLA_U64 },
8501 [DEVLINK_ATTR_RATE_TX_MAX] = { .type = NLA_U64 },
8502 [DEVLINK_ATTR_RATE_NODE_NAME] = { .type = NLA_NUL_STRING },
8503 [DEVLINK_ATTR_RATE_PARENT_NODE_NAME] = { .type = NLA_NUL_STRING },
8506 static const struct genl_small_ops devlink_nl_ops[] = {
8508 .cmd = DEVLINK_CMD_GET,
8509 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8510 .doit = devlink_nl_cmd_get_doit,
8511 .dumpit = devlink_nl_cmd_get_dumpit,
8512 /* can be retrieved by unprivileged users */
8515 .cmd = DEVLINK_CMD_PORT_GET,
8516 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8517 .doit = devlink_nl_cmd_port_get_doit,
8518 .dumpit = devlink_nl_cmd_port_get_dumpit,
8519 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8520 /* can be retrieved by unprivileged users */
8523 .cmd = DEVLINK_CMD_PORT_SET,
8524 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8525 .doit = devlink_nl_cmd_port_set_doit,
8526 .flags = GENL_ADMIN_PERM,
8527 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8530 .cmd = DEVLINK_CMD_RATE_GET,
8531 .doit = devlink_nl_cmd_rate_get_doit,
8532 .dumpit = devlink_nl_cmd_rate_get_dumpit,
8533 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
8534 /* can be retrieved by unprivileged users */
8537 .cmd = DEVLINK_CMD_RATE_SET,
8538 .doit = devlink_nl_cmd_rate_set_doit,
8539 .flags = GENL_ADMIN_PERM,
8540 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE,
8543 .cmd = DEVLINK_CMD_RATE_NEW,
8544 .doit = devlink_nl_cmd_rate_new_doit,
8545 .flags = GENL_ADMIN_PERM,
8548 .cmd = DEVLINK_CMD_RATE_DEL,
8549 .doit = devlink_nl_cmd_rate_del_doit,
8550 .flags = GENL_ADMIN_PERM,
8551 .internal_flags = DEVLINK_NL_FLAG_NEED_RATE_NODE,
8554 .cmd = DEVLINK_CMD_PORT_SPLIT,
8555 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8556 .doit = devlink_nl_cmd_port_split_doit,
8557 .flags = GENL_ADMIN_PERM,
8558 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8561 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
8562 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8563 .doit = devlink_nl_cmd_port_unsplit_doit,
8564 .flags = GENL_ADMIN_PERM,
8565 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8568 .cmd = DEVLINK_CMD_PORT_NEW,
8569 .doit = devlink_nl_cmd_port_new_doit,
8570 .flags = GENL_ADMIN_PERM,
8571 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8574 .cmd = DEVLINK_CMD_PORT_DEL,
8575 .doit = devlink_nl_cmd_port_del_doit,
8576 .flags = GENL_ADMIN_PERM,
8577 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8580 .cmd = DEVLINK_CMD_SB_GET,
8581 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8582 .doit = devlink_nl_cmd_sb_get_doit,
8583 .dumpit = devlink_nl_cmd_sb_get_dumpit,
8584 /* can be retrieved by unprivileged users */
8587 .cmd = DEVLINK_CMD_SB_POOL_GET,
8588 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8589 .doit = devlink_nl_cmd_sb_pool_get_doit,
8590 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
8591 /* can be retrieved by unprivileged users */
8594 .cmd = DEVLINK_CMD_SB_POOL_SET,
8595 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8596 .doit = devlink_nl_cmd_sb_pool_set_doit,
8597 .flags = GENL_ADMIN_PERM,
8600 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
8601 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8602 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
8603 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
8604 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8605 /* can be retrieved by unprivileged users */
8608 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
8609 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8610 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
8611 .flags = GENL_ADMIN_PERM,
8612 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8615 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
8616 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8617 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
8618 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
8619 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8620 /* can be retrieved by unprivileged users */
8623 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
8624 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8625 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
8626 .flags = GENL_ADMIN_PERM,
8627 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8630 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
8631 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8632 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
8633 .flags = GENL_ADMIN_PERM,
8636 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
8637 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8638 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
8639 .flags = GENL_ADMIN_PERM,
8642 .cmd = DEVLINK_CMD_ESWITCH_GET,
8643 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8644 .doit = devlink_nl_cmd_eswitch_get_doit,
8645 .flags = GENL_ADMIN_PERM,
8646 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8649 .cmd = DEVLINK_CMD_ESWITCH_SET,
8650 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8651 .doit = devlink_nl_cmd_eswitch_set_doit,
8652 .flags = GENL_ADMIN_PERM,
8653 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8656 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
8657 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8658 .doit = devlink_nl_cmd_dpipe_table_get,
8659 /* can be retrieved by unprivileged users */
8662 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
8663 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8664 .doit = devlink_nl_cmd_dpipe_entries_get,
8665 /* can be retrieved by unprivileged users */
8668 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
8669 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8670 .doit = devlink_nl_cmd_dpipe_headers_get,
8671 /* can be retrieved by unprivileged users */
8674 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
8675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8676 .doit = devlink_nl_cmd_dpipe_table_counters_set,
8677 .flags = GENL_ADMIN_PERM,
8680 .cmd = DEVLINK_CMD_RESOURCE_SET,
8681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8682 .doit = devlink_nl_cmd_resource_set,
8683 .flags = GENL_ADMIN_PERM,
8686 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
8687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8688 .doit = devlink_nl_cmd_resource_dump,
8689 /* can be retrieved by unprivileged users */
8692 .cmd = DEVLINK_CMD_RELOAD,
8693 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8694 .doit = devlink_nl_cmd_reload,
8695 .flags = GENL_ADMIN_PERM,
8696 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
8699 .cmd = DEVLINK_CMD_PARAM_GET,
8700 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8701 .doit = devlink_nl_cmd_param_get_doit,
8702 .dumpit = devlink_nl_cmd_param_get_dumpit,
8703 /* can be retrieved by unprivileged users */
8706 .cmd = DEVLINK_CMD_PARAM_SET,
8707 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8708 .doit = devlink_nl_cmd_param_set_doit,
8709 .flags = GENL_ADMIN_PERM,
8712 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
8713 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8714 .doit = devlink_nl_cmd_port_param_get_doit,
8715 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
8716 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8717 /* can be retrieved by unprivileged users */
8720 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
8721 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8722 .doit = devlink_nl_cmd_port_param_set_doit,
8723 .flags = GENL_ADMIN_PERM,
8724 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
8727 .cmd = DEVLINK_CMD_REGION_GET,
8728 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8729 .doit = devlink_nl_cmd_region_get_doit,
8730 .dumpit = devlink_nl_cmd_region_get_dumpit,
8731 .flags = GENL_ADMIN_PERM,
8734 .cmd = DEVLINK_CMD_REGION_NEW,
8735 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8736 .doit = devlink_nl_cmd_region_new,
8737 .flags = GENL_ADMIN_PERM,
8740 .cmd = DEVLINK_CMD_REGION_DEL,
8741 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8742 .doit = devlink_nl_cmd_region_del,
8743 .flags = GENL_ADMIN_PERM,
8746 .cmd = DEVLINK_CMD_REGION_READ,
8747 .validate = GENL_DONT_VALIDATE_STRICT |
8748 GENL_DONT_VALIDATE_DUMP_STRICT,
8749 .dumpit = devlink_nl_cmd_region_read_dumpit,
8750 .flags = GENL_ADMIN_PERM,
8753 .cmd = DEVLINK_CMD_INFO_GET,
8754 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8755 .doit = devlink_nl_cmd_info_get_doit,
8756 .dumpit = devlink_nl_cmd_info_get_dumpit,
8757 /* can be retrieved by unprivileged users */
8760 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
8761 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8762 .doit = devlink_nl_cmd_health_reporter_get_doit,
8763 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
8764 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8765 DEVLINK_NL_FLAG_NO_LOCK,
8766 /* can be retrieved by unprivileged users */
8769 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
8770 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8771 .doit = devlink_nl_cmd_health_reporter_set_doit,
8772 .flags = GENL_ADMIN_PERM,
8773 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8774 DEVLINK_NL_FLAG_NO_LOCK,
8777 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
8778 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8779 .doit = devlink_nl_cmd_health_reporter_recover_doit,
8780 .flags = GENL_ADMIN_PERM,
8781 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8782 DEVLINK_NL_FLAG_NO_LOCK,
8785 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
8786 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8787 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
8788 .flags = GENL_ADMIN_PERM,
8789 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8790 DEVLINK_NL_FLAG_NO_LOCK,
8793 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
8794 .validate = GENL_DONT_VALIDATE_STRICT |
8795 GENL_DONT_VALIDATE_DUMP_STRICT,
8796 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
8797 .flags = GENL_ADMIN_PERM,
8798 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8799 DEVLINK_NL_FLAG_NO_LOCK,
8802 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
8803 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8804 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
8805 .flags = GENL_ADMIN_PERM,
8806 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8807 DEVLINK_NL_FLAG_NO_LOCK,
8810 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
8811 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8812 .doit = devlink_nl_cmd_health_reporter_test_doit,
8813 .flags = GENL_ADMIN_PERM,
8814 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
8815 DEVLINK_NL_FLAG_NO_LOCK,
8818 .cmd = DEVLINK_CMD_FLASH_UPDATE,
8819 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
8820 .doit = devlink_nl_cmd_flash_update,
8821 .flags = GENL_ADMIN_PERM,
8824 .cmd = DEVLINK_CMD_TRAP_GET,
8825 .doit = devlink_nl_cmd_trap_get_doit,
8826 .dumpit = devlink_nl_cmd_trap_get_dumpit,
8827 /* can be retrieved by unprivileged users */
8830 .cmd = DEVLINK_CMD_TRAP_SET,
8831 .doit = devlink_nl_cmd_trap_set_doit,
8832 .flags = GENL_ADMIN_PERM,
8835 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
8836 .doit = devlink_nl_cmd_trap_group_get_doit,
8837 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
8838 /* can be retrieved by unprivileged users */
8841 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
8842 .doit = devlink_nl_cmd_trap_group_set_doit,
8843 .flags = GENL_ADMIN_PERM,
8846 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
8847 .doit = devlink_nl_cmd_trap_policer_get_doit,
8848 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
8849 /* can be retrieved by unprivileged users */
8852 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
8853 .doit = devlink_nl_cmd_trap_policer_set_doit,
8854 .flags = GENL_ADMIN_PERM,
8858 static struct genl_family devlink_nl_family __ro_after_init = {
8859 .name = DEVLINK_GENL_NAME,
8860 .version = DEVLINK_GENL_VERSION,
8861 .maxattr = DEVLINK_ATTR_MAX,
8862 .policy = devlink_nl_policy,
8864 .pre_doit = devlink_nl_pre_doit,
8865 .post_doit = devlink_nl_post_doit,
8866 .module = THIS_MODULE,
8867 .small_ops = devlink_nl_ops,
8868 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
8869 .mcgrps = devlink_nl_mcgrps,
8870 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
8873 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
8875 const struct devlink_reload_combination *comb;
8878 if (!devlink_reload_supported(ops)) {
8879 if (WARN_ON(ops->reload_actions))
8884 if (WARN_ON(!ops->reload_actions ||
8885 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
8886 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
8889 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
8890 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
8893 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
8894 comb = &devlink_reload_invalid_combinations[i];
8895 if (ops->reload_actions == BIT(comb->action) &&
8896 ops->reload_limits == BIT(comb->limit))
8903 * devlink_alloc_ns - Allocate new devlink instance resources
8904 * in specific namespace
8907 * @priv_size: size of user private data
8908 * @net: net namespace
8909 * @dev: parent device
8911 * Allocate new devlink instance resources, including devlink index
8914 struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
8915 size_t priv_size, struct net *net,
8918 struct devlink *devlink;
8922 WARN_ON(!ops || !dev);
8923 if (!devlink_reload_actions_valid(ops))
8926 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
8930 ret = xa_alloc_cyclic(&devlinks, &devlink->index, devlink, xa_limit_31b,
8931 &last_id, GFP_KERNEL);
8939 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
8940 write_pnet(&devlink->_net, net);
8941 INIT_LIST_HEAD(&devlink->port_list);
8942 INIT_LIST_HEAD(&devlink->rate_list);
8943 INIT_LIST_HEAD(&devlink->sb_list);
8944 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
8945 INIT_LIST_HEAD(&devlink->resource_list);
8946 INIT_LIST_HEAD(&devlink->param_list);
8947 INIT_LIST_HEAD(&devlink->region_list);
8948 INIT_LIST_HEAD(&devlink->reporter_list);
8949 INIT_LIST_HEAD(&devlink->trap_list);
8950 INIT_LIST_HEAD(&devlink->trap_group_list);
8951 INIT_LIST_HEAD(&devlink->trap_policer_list);
8952 mutex_init(&devlink->lock);
8953 mutex_init(&devlink->reporters_lock);
8954 refcount_set(&devlink->refcount, 1);
8955 init_completion(&devlink->comp);
8959 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
8962 * devlink_register - Register devlink instance
8966 int devlink_register(struct devlink *devlink)
8968 mutex_lock(&devlink_mutex);
8969 xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
8970 devlink_notify(devlink, DEVLINK_CMD_NEW);
8971 mutex_unlock(&devlink_mutex);
8974 EXPORT_SYMBOL_GPL(devlink_register);
8977 * devlink_unregister - Unregister devlink instance
8981 void devlink_unregister(struct devlink *devlink)
8983 devlink_put(devlink);
8984 wait_for_completion(&devlink->comp);
8986 mutex_lock(&devlink_mutex);
8987 WARN_ON(devlink_reload_supported(devlink->ops) &&
8988 devlink->reload_enabled);
8989 devlink_notify(devlink, DEVLINK_CMD_DEL);
8990 xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
8991 mutex_unlock(&devlink_mutex);
8993 EXPORT_SYMBOL_GPL(devlink_unregister);
8996 * devlink_reload_enable - Enable reload of devlink instance
9000 * Should be called at end of device initialization
9001 * process when reload operation is supported.
9003 void devlink_reload_enable(struct devlink *devlink)
9005 mutex_lock(&devlink_mutex);
9006 devlink->reload_enabled = true;
9007 mutex_unlock(&devlink_mutex);
9009 EXPORT_SYMBOL_GPL(devlink_reload_enable);
9012 * devlink_reload_disable - Disable reload of devlink instance
9016 * Should be called at the beginning of device cleanup
9017 * process when reload operation is supported.
9019 void devlink_reload_disable(struct devlink *devlink)
9021 mutex_lock(&devlink_mutex);
9022 /* Mutex is taken which ensures that no reload operation is in
9023 * progress while setting up forbidded flag.
9025 devlink->reload_enabled = false;
9026 mutex_unlock(&devlink_mutex);
9028 EXPORT_SYMBOL_GPL(devlink_reload_disable);
9031 * devlink_free - Free devlink instance resources
9035 void devlink_free(struct devlink *devlink)
9037 mutex_destroy(&devlink->reporters_lock);
9038 mutex_destroy(&devlink->lock);
9039 WARN_ON(!list_empty(&devlink->trap_policer_list));
9040 WARN_ON(!list_empty(&devlink->trap_group_list));
9041 WARN_ON(!list_empty(&devlink->trap_list));
9042 WARN_ON(!list_empty(&devlink->reporter_list));
9043 WARN_ON(!list_empty(&devlink->region_list));
9044 WARN_ON(!list_empty(&devlink->param_list));
9045 WARN_ON(!list_empty(&devlink->resource_list));
9046 WARN_ON(!list_empty(&devlink->dpipe_table_list));
9047 WARN_ON(!list_empty(&devlink->sb_list));
9048 WARN_ON(!list_empty(&devlink->rate_list));
9049 WARN_ON(!list_empty(&devlink->port_list));
9051 xa_destroy(&devlink->snapshot_ids);
9052 xa_erase(&devlinks, devlink->index);
9056 EXPORT_SYMBOL_GPL(devlink_free);
9058 static void devlink_port_type_warn(struct work_struct *work)
9060 WARN(true, "Type was not set for devlink port.");
9063 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
9065 /* Ignore CPU and DSA flavours. */
9066 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
9067 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
9068 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
9071 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
9073 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
9075 if (!devlink_port_type_should_warn(devlink_port))
9077 /* Schedule a work to WARN in case driver does not set port
9078 * type within timeout.
9080 schedule_delayed_work(&devlink_port->type_warn_dw,
9081 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
9084 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
9086 if (!devlink_port_type_should_warn(devlink_port))
9088 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
9092 * devlink_port_register - Register devlink port
9095 * @devlink_port: devlink port
9096 * @port_index: driver-specific numerical identifier of the port
9098 * Register devlink port with provided port index. User can use
9099 * any indexing, even hw-related one. devlink_port structure
9100 * is convenient to be embedded inside user driver private structure.
9101 * Note that the caller should take care of zeroing the devlink_port
9104 int devlink_port_register(struct devlink *devlink,
9105 struct devlink_port *devlink_port,
9106 unsigned int port_index)
9108 mutex_lock(&devlink->lock);
9109 if (devlink_port_index_exists(devlink, port_index)) {
9110 mutex_unlock(&devlink->lock);
9114 WARN_ON(devlink_port->devlink);
9115 devlink_port->devlink = devlink;
9116 devlink_port->index = port_index;
9117 spin_lock_init(&devlink_port->type_lock);
9118 INIT_LIST_HEAD(&devlink_port->reporter_list);
9119 mutex_init(&devlink_port->reporters_lock);
9120 list_add_tail(&devlink_port->list, &devlink->port_list);
9121 INIT_LIST_HEAD(&devlink_port->param_list);
9122 INIT_LIST_HEAD(&devlink_port->region_list);
9123 mutex_unlock(&devlink->lock);
9124 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
9125 devlink_port_type_warn_schedule(devlink_port);
9126 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9129 EXPORT_SYMBOL_GPL(devlink_port_register);
9132 * devlink_port_unregister - Unregister devlink port
9134 * @devlink_port: devlink port
9136 void devlink_port_unregister(struct devlink_port *devlink_port)
9138 struct devlink *devlink = devlink_port->devlink;
9140 devlink_port_type_warn_cancel(devlink_port);
9141 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
9142 mutex_lock(&devlink->lock);
9143 list_del(&devlink_port->list);
9144 mutex_unlock(&devlink->lock);
9145 WARN_ON(!list_empty(&devlink_port->reporter_list));
9146 WARN_ON(!list_empty(&devlink_port->region_list));
9147 mutex_destroy(&devlink_port->reporters_lock);
9149 EXPORT_SYMBOL_GPL(devlink_port_unregister);
9151 static void __devlink_port_type_set(struct devlink_port *devlink_port,
9152 enum devlink_port_type type,
9155 if (WARN_ON(!devlink_port->devlink))
9157 devlink_port_type_warn_cancel(devlink_port);
9158 spin_lock_bh(&devlink_port->type_lock);
9159 devlink_port->type = type;
9160 devlink_port->type_dev = type_dev;
9161 spin_unlock_bh(&devlink_port->type_lock);
9162 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
9165 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
9166 struct net_device *netdev)
9168 const struct net_device_ops *ops = netdev->netdev_ops;
9170 /* If driver registers devlink port, it should set devlink port
9171 * attributes accordingly so the compat functions are called
9172 * and the original ops are not used.
9174 if (ops->ndo_get_phys_port_name) {
9175 /* Some drivers use the same set of ndos for netdevs
9176 * that have devlink_port registered and also for
9177 * those who don't. Make sure that ndo_get_phys_port_name
9178 * returns -EOPNOTSUPP here in case it is defined.
9181 char name[IFNAMSIZ];
9184 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
9185 WARN_ON(err != -EOPNOTSUPP);
9187 if (ops->ndo_get_port_parent_id) {
9188 /* Some drivers use the same set of ndos for netdevs
9189 * that have devlink_port registered and also for
9190 * those who don't. Make sure that ndo_get_port_parent_id
9191 * returns -EOPNOTSUPP here in case it is defined.
9194 struct netdev_phys_item_id ppid;
9197 err = ops->ndo_get_port_parent_id(netdev, &ppid);
9198 WARN_ON(err != -EOPNOTSUPP);
9203 * devlink_port_type_eth_set - Set port type to Ethernet
9205 * @devlink_port: devlink port
9206 * @netdev: related netdevice
9208 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
9209 struct net_device *netdev)
9212 devlink_port_type_netdev_checks(devlink_port, netdev);
9214 dev_warn(devlink_port->devlink->dev,
9215 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
9216 devlink_port->index);
9218 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
9220 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
9223 * devlink_port_type_ib_set - Set port type to InfiniBand
9225 * @devlink_port: devlink port
9226 * @ibdev: related IB device
9228 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
9229 struct ib_device *ibdev)
9231 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
9233 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
9236 * devlink_port_type_clear - Clear port type
9238 * @devlink_port: devlink port
9240 void devlink_port_type_clear(struct devlink_port *devlink_port)
9242 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
9243 devlink_port_type_warn_schedule(devlink_port);
9245 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
9247 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
9248 enum devlink_port_flavour flavour)
9250 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9252 devlink_port->attrs_set = true;
9253 attrs->flavour = flavour;
9254 if (attrs->switch_id.id_len) {
9255 devlink_port->switch_port = true;
9256 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
9257 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
9259 devlink_port->switch_port = false;
9265 * devlink_port_attrs_set - Set port attributes
9267 * @devlink_port: devlink port
9268 * @attrs: devlink port attrs
9270 void devlink_port_attrs_set(struct devlink_port *devlink_port,
9271 struct devlink_port_attrs *attrs)
9275 if (WARN_ON(devlink_port->devlink))
9277 devlink_port->attrs = *attrs;
9278 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
9281 WARN_ON(attrs->splittable && attrs->split);
9283 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
9286 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
9288 * @devlink_port: devlink port
9289 * @controller: associated controller number for the devlink port instance
9290 * @pf: associated PF for the devlink port instance
9291 * @external: indicates if the port is for an external controller
9293 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
9294 u16 pf, bool external)
9296 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9299 if (WARN_ON(devlink_port->devlink))
9301 ret = __devlink_port_attrs_set(devlink_port,
9302 DEVLINK_PORT_FLAVOUR_PCI_PF);
9305 attrs->pci_pf.controller = controller;
9306 attrs->pci_pf.pf = pf;
9307 attrs->pci_pf.external = external;
9309 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
9312 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
9314 * @devlink_port: devlink port
9315 * @controller: associated controller number for the devlink port instance
9316 * @pf: associated PF for the devlink port instance
9317 * @vf: associated VF of a PF for the devlink port instance
9318 * @external: indicates if the port is for an external controller
9320 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
9321 u16 pf, u16 vf, bool external)
9323 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9326 if (WARN_ON(devlink_port->devlink))
9328 ret = __devlink_port_attrs_set(devlink_port,
9329 DEVLINK_PORT_FLAVOUR_PCI_VF);
9332 attrs->pci_vf.controller = controller;
9333 attrs->pci_vf.pf = pf;
9334 attrs->pci_vf.vf = vf;
9335 attrs->pci_vf.external = external;
9337 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
9340 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
9342 * @devlink_port: devlink port
9343 * @controller: associated controller number for the devlink port instance
9344 * @pf: associated PF for the devlink port instance
9345 * @sf: associated SF of a PF for the devlink port instance
9346 * @external: indicates if the port is for an external controller
9348 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
9349 u16 pf, u32 sf, bool external)
9351 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9354 if (WARN_ON(devlink_port->devlink))
9356 ret = __devlink_port_attrs_set(devlink_port,
9357 DEVLINK_PORT_FLAVOUR_PCI_SF);
9360 attrs->pci_sf.controller = controller;
9361 attrs->pci_sf.pf = pf;
9362 attrs->pci_sf.sf = sf;
9363 attrs->pci_sf.external = external;
9365 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
9368 * devlink_rate_leaf_create - create devlink rate leaf
9370 * @devlink_port: devlink port object to create rate object on
9371 * @priv: driver private data
9373 * Create devlink rate object of type leaf on provided @devlink_port.
9374 * Throws call trace if @devlink_port already has a devlink rate object.
9376 * Context: Takes and release devlink->lock <mutex>.
9378 * Return: -ENOMEM if failed to allocate rate object, 0 otherwise.
9381 devlink_rate_leaf_create(struct devlink_port *devlink_port, void *priv)
9383 struct devlink *devlink = devlink_port->devlink;
9384 struct devlink_rate *devlink_rate;
9386 devlink_rate = kzalloc(sizeof(*devlink_rate), GFP_KERNEL);
9390 mutex_lock(&devlink->lock);
9391 WARN_ON(devlink_port->devlink_rate);
9392 devlink_rate->type = DEVLINK_RATE_TYPE_LEAF;
9393 devlink_rate->devlink = devlink;
9394 devlink_rate->devlink_port = devlink_port;
9395 devlink_rate->priv = priv;
9396 list_add_tail(&devlink_rate->list, &devlink->rate_list);
9397 devlink_port->devlink_rate = devlink_rate;
9398 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_NEW);
9399 mutex_unlock(&devlink->lock);
9403 EXPORT_SYMBOL_GPL(devlink_rate_leaf_create);
9406 * devlink_rate_leaf_destroy - destroy devlink rate leaf
9408 * @devlink_port: devlink port linked to the rate object
9410 * Context: Takes and release devlink->lock <mutex>.
9412 void devlink_rate_leaf_destroy(struct devlink_port *devlink_port)
9414 struct devlink_rate *devlink_rate = devlink_port->devlink_rate;
9415 struct devlink *devlink = devlink_port->devlink;
9420 mutex_lock(&devlink->lock);
9421 devlink_rate_notify(devlink_rate, DEVLINK_CMD_RATE_DEL);
9422 if (devlink_rate->parent)
9423 refcount_dec(&devlink_rate->parent->refcnt);
9424 list_del(&devlink_rate->list);
9425 devlink_port->devlink_rate = NULL;
9426 mutex_unlock(&devlink->lock);
9427 kfree(devlink_rate);
9429 EXPORT_SYMBOL_GPL(devlink_rate_leaf_destroy);
9432 * devlink_rate_nodes_destroy - destroy all devlink rate nodes on device
9434 * @devlink: devlink instance
9436 * Unset parent for all rate objects and destroy all rate nodes
9437 * on specified device.
9439 * Context: Takes and release devlink->lock <mutex>.
9441 void devlink_rate_nodes_destroy(struct devlink *devlink)
9443 static struct devlink_rate *devlink_rate, *tmp;
9444 const struct devlink_ops *ops = devlink->ops;
9446 mutex_lock(&devlink->lock);
9447 list_for_each_entry(devlink_rate, &devlink->rate_list, list) {
9448 if (!devlink_rate->parent)
9451 refcount_dec(&devlink_rate->parent->refcnt);
9452 if (devlink_rate_is_leaf(devlink_rate))
9453 ops->rate_leaf_parent_set(devlink_rate, NULL, devlink_rate->priv,
9455 else if (devlink_rate_is_node(devlink_rate))
9456 ops->rate_node_parent_set(devlink_rate, NULL, devlink_rate->priv,
9459 list_for_each_entry_safe(devlink_rate, tmp, &devlink->rate_list, list) {
9460 if (devlink_rate_is_node(devlink_rate)) {
9461 ops->rate_node_del(devlink_rate, devlink_rate->priv, NULL);
9462 list_del(&devlink_rate->list);
9463 kfree(devlink_rate->name);
9464 kfree(devlink_rate);
9467 mutex_unlock(&devlink->lock);
9469 EXPORT_SYMBOL_GPL(devlink_rate_nodes_destroy);
9471 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
9472 char *name, size_t len)
9474 struct devlink_port_attrs *attrs = &devlink_port->attrs;
9477 if (!devlink_port->attrs_set)
9480 switch (attrs->flavour) {
9481 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
9482 n = snprintf(name, len, "p%u", attrs->phys.port_number);
9483 if (n < len && attrs->split)
9484 n += snprintf(name + n, len - n, "s%u",
9485 attrs->phys.split_subport_number);
9487 case DEVLINK_PORT_FLAVOUR_CPU:
9488 case DEVLINK_PORT_FLAVOUR_DSA:
9489 case DEVLINK_PORT_FLAVOUR_UNUSED:
9490 /* As CPU and DSA ports do not have a netdevice associated
9491 * case should not ever happen.
9495 case DEVLINK_PORT_FLAVOUR_PCI_PF:
9496 if (attrs->pci_pf.external) {
9497 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
9503 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
9505 case DEVLINK_PORT_FLAVOUR_PCI_VF:
9506 if (attrs->pci_vf.external) {
9507 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
9513 n = snprintf(name, len, "pf%uvf%u",
9514 attrs->pci_vf.pf, attrs->pci_vf.vf);
9516 case DEVLINK_PORT_FLAVOUR_PCI_SF:
9517 if (attrs->pci_sf.external) {
9518 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
9524 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
9527 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
9537 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
9538 u32 size, u16 ingress_pools_count,
9539 u16 egress_pools_count, u16 ingress_tc_count,
9540 u16 egress_tc_count)
9542 struct devlink_sb *devlink_sb;
9545 mutex_lock(&devlink->lock);
9546 if (devlink_sb_index_exists(devlink, sb_index)) {
9551 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
9556 devlink_sb->index = sb_index;
9557 devlink_sb->size = size;
9558 devlink_sb->ingress_pools_count = ingress_pools_count;
9559 devlink_sb->egress_pools_count = egress_pools_count;
9560 devlink_sb->ingress_tc_count = ingress_tc_count;
9561 devlink_sb->egress_tc_count = egress_tc_count;
9562 list_add_tail(&devlink_sb->list, &devlink->sb_list);
9564 mutex_unlock(&devlink->lock);
9567 EXPORT_SYMBOL_GPL(devlink_sb_register);
9569 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
9571 struct devlink_sb *devlink_sb;
9573 mutex_lock(&devlink->lock);
9574 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
9575 WARN_ON(!devlink_sb);
9576 list_del(&devlink_sb->list);
9577 mutex_unlock(&devlink->lock);
9580 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
9583 * devlink_dpipe_headers_register - register dpipe headers
9586 * @dpipe_headers: dpipe header array
9588 * Register the headers supported by hardware.
9590 int devlink_dpipe_headers_register(struct devlink *devlink,
9591 struct devlink_dpipe_headers *dpipe_headers)
9593 mutex_lock(&devlink->lock);
9594 devlink->dpipe_headers = dpipe_headers;
9595 mutex_unlock(&devlink->lock);
9598 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
9601 * devlink_dpipe_headers_unregister - unregister dpipe headers
9605 * Unregister the headers supported by hardware.
9607 void devlink_dpipe_headers_unregister(struct devlink *devlink)
9609 mutex_lock(&devlink->lock);
9610 devlink->dpipe_headers = NULL;
9611 mutex_unlock(&devlink->lock);
9613 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
9616 * devlink_dpipe_table_counter_enabled - check if counter allocation
9619 * @table_name: tables name
9621 * Used by driver to check if counter allocation is required.
9622 * After counter allocation is turned on the table entries
9623 * are updated to include counter statistics.
9625 * After that point on the driver must respect the counter
9626 * state so that each entry added to the table is added
9629 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
9630 const char *table_name)
9632 struct devlink_dpipe_table *table;
9636 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
9637 table_name, devlink);
9640 enabled = table->counters_enabled;
9644 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
9647 * devlink_dpipe_table_register - register dpipe table
9650 * @table_name: table name
9651 * @table_ops: table ops
9653 * @counter_control_extern: external control for counters
9655 int devlink_dpipe_table_register(struct devlink *devlink,
9656 const char *table_name,
9657 struct devlink_dpipe_table_ops *table_ops,
9658 void *priv, bool counter_control_extern)
9660 struct devlink_dpipe_table *table;
9663 if (WARN_ON(!table_ops->size_get))
9666 mutex_lock(&devlink->lock);
9668 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
9674 table = kzalloc(sizeof(*table), GFP_KERNEL);
9680 table->name = table_name;
9681 table->table_ops = table_ops;
9683 table->counter_control_extern = counter_control_extern;
9685 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
9687 mutex_unlock(&devlink->lock);
9690 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
9693 * devlink_dpipe_table_unregister - unregister dpipe table
9696 * @table_name: table name
9698 void devlink_dpipe_table_unregister(struct devlink *devlink,
9699 const char *table_name)
9701 struct devlink_dpipe_table *table;
9703 mutex_lock(&devlink->lock);
9704 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
9705 table_name, devlink);
9708 list_del_rcu(&table->list);
9709 mutex_unlock(&devlink->lock);
9710 kfree_rcu(table, rcu);
9713 mutex_unlock(&devlink->lock);
9715 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
9718 * devlink_resource_register - devlink resource register
9721 * @resource_name: resource's name
9722 * @resource_size: resource's size
9723 * @resource_id: resource's id
9724 * @parent_resource_id: resource's parent id
9725 * @size_params: size parameters
9727 * Generic resources should reuse the same names across drivers.
9728 * Please see the generic resources list at:
9729 * Documentation/networking/devlink/devlink-resource.rst
9731 int devlink_resource_register(struct devlink *devlink,
9732 const char *resource_name,
9735 u64 parent_resource_id,
9736 const struct devlink_resource_size_params *size_params)
9738 struct devlink_resource *resource;
9739 struct list_head *resource_list;
9743 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
9745 mutex_lock(&devlink->lock);
9746 resource = devlink_resource_find(devlink, NULL, resource_id);
9752 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
9758 if (top_hierarchy) {
9759 resource_list = &devlink->resource_list;
9761 struct devlink_resource *parent_resource;
9763 parent_resource = devlink_resource_find(devlink, NULL,
9764 parent_resource_id);
9765 if (parent_resource) {
9766 resource_list = &parent_resource->resource_list;
9767 resource->parent = parent_resource;
9775 resource->name = resource_name;
9776 resource->size = resource_size;
9777 resource->size_new = resource_size;
9778 resource->id = resource_id;
9779 resource->size_valid = true;
9780 memcpy(&resource->size_params, size_params,
9781 sizeof(resource->size_params));
9782 INIT_LIST_HEAD(&resource->resource_list);
9783 list_add_tail(&resource->list, resource_list);
9785 mutex_unlock(&devlink->lock);
9788 EXPORT_SYMBOL_GPL(devlink_resource_register);
9791 * devlink_resources_unregister - free all resources
9794 * @resource: resource
9796 void devlink_resources_unregister(struct devlink *devlink,
9797 struct devlink_resource *resource)
9799 struct devlink_resource *tmp, *child_resource;
9800 struct list_head *resource_list;
9803 resource_list = &resource->resource_list;
9805 resource_list = &devlink->resource_list;
9808 mutex_lock(&devlink->lock);
9810 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
9811 devlink_resources_unregister(devlink, child_resource);
9812 list_del(&child_resource->list);
9813 kfree(child_resource);
9817 mutex_unlock(&devlink->lock);
9819 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
9822 * devlink_resource_size_get - get and update size
9825 * @resource_id: the requested resource id
9826 * @p_resource_size: ptr to update
9828 int devlink_resource_size_get(struct devlink *devlink,
9830 u64 *p_resource_size)
9832 struct devlink_resource *resource;
9835 mutex_lock(&devlink->lock);
9836 resource = devlink_resource_find(devlink, NULL, resource_id);
9841 *p_resource_size = resource->size_new;
9842 resource->size = resource->size_new;
9844 mutex_unlock(&devlink->lock);
9847 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
9850 * devlink_dpipe_table_resource_set - set the resource id
9853 * @table_name: table name
9854 * @resource_id: resource id
9855 * @resource_units: number of resource's units consumed per table's entry
9857 int devlink_dpipe_table_resource_set(struct devlink *devlink,
9858 const char *table_name, u64 resource_id,
9861 struct devlink_dpipe_table *table;
9864 mutex_lock(&devlink->lock);
9865 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
9866 table_name, devlink);
9871 table->resource_id = resource_id;
9872 table->resource_units = resource_units;
9873 table->resource_valid = true;
9875 mutex_unlock(&devlink->lock);
9878 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
9881 * devlink_resource_occ_get_register - register occupancy getter
9884 * @resource_id: resource id
9885 * @occ_get: occupancy getter callback
9886 * @occ_get_priv: occupancy getter callback priv
9888 void devlink_resource_occ_get_register(struct devlink *devlink,
9890 devlink_resource_occ_get_t *occ_get,
9893 struct devlink_resource *resource;
9895 mutex_lock(&devlink->lock);
9896 resource = devlink_resource_find(devlink, NULL, resource_id);
9897 if (WARN_ON(!resource))
9899 WARN_ON(resource->occ_get);
9901 resource->occ_get = occ_get;
9902 resource->occ_get_priv = occ_get_priv;
9904 mutex_unlock(&devlink->lock);
9906 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
9909 * devlink_resource_occ_get_unregister - unregister occupancy getter
9912 * @resource_id: resource id
9914 void devlink_resource_occ_get_unregister(struct devlink *devlink,
9917 struct devlink_resource *resource;
9919 mutex_lock(&devlink->lock);
9920 resource = devlink_resource_find(devlink, NULL, resource_id);
9921 if (WARN_ON(!resource))
9923 WARN_ON(!resource->occ_get);
9925 resource->occ_get = NULL;
9926 resource->occ_get_priv = NULL;
9928 mutex_unlock(&devlink->lock);
9930 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
9932 static int devlink_param_verify(const struct devlink_param *param)
9934 if (!param || !param->name || !param->supported_cmodes)
9937 return devlink_param_generic_verify(param);
9939 return devlink_param_driver_verify(param);
9942 static int __devlink_param_register_one(struct devlink *devlink,
9943 unsigned int port_index,
9944 struct list_head *param_list,
9945 const struct devlink_param *param,
9946 enum devlink_command reg_cmd)
9950 err = devlink_param_verify(param);
9954 return devlink_param_register_one(devlink, port_index,
9955 param_list, param, reg_cmd);
9958 static int __devlink_params_register(struct devlink *devlink,
9959 unsigned int port_index,
9960 struct list_head *param_list,
9961 const struct devlink_param *params,
9962 size_t params_count,
9963 enum devlink_command reg_cmd,
9964 enum devlink_command unreg_cmd)
9966 const struct devlink_param *param = params;
9970 mutex_lock(&devlink->lock);
9971 for (i = 0; i < params_count; i++, param++) {
9972 err = __devlink_param_register_one(devlink, port_index,
9973 param_list, param, reg_cmd);
9978 mutex_unlock(&devlink->lock);
9984 for (param--; i > 0; i--, param--)
9985 devlink_param_unregister_one(devlink, port_index, param_list,
9988 mutex_unlock(&devlink->lock);
9992 static void __devlink_params_unregister(struct devlink *devlink,
9993 unsigned int port_index,
9994 struct list_head *param_list,
9995 const struct devlink_param *params,
9996 size_t params_count,
9997 enum devlink_command cmd)
9999 const struct devlink_param *param = params;
10002 mutex_lock(&devlink->lock);
10003 for (i = 0; i < params_count; i++, param++)
10004 devlink_param_unregister_one(devlink, 0, param_list, param,
10006 mutex_unlock(&devlink->lock);
10010 * devlink_params_register - register configuration parameters
10012 * @devlink: devlink
10013 * @params: configuration parameters array
10014 * @params_count: number of parameters provided
10016 * Register the configuration parameters supported by the driver.
10018 int devlink_params_register(struct devlink *devlink,
10019 const struct devlink_param *params,
10020 size_t params_count)
10022 return __devlink_params_register(devlink, 0, &devlink->param_list,
10023 params, params_count,
10024 DEVLINK_CMD_PARAM_NEW,
10025 DEVLINK_CMD_PARAM_DEL);
10027 EXPORT_SYMBOL_GPL(devlink_params_register);
10030 * devlink_params_unregister - unregister configuration parameters
10031 * @devlink: devlink
10032 * @params: configuration parameters to unregister
10033 * @params_count: number of parameters provided
10035 void devlink_params_unregister(struct devlink *devlink,
10036 const struct devlink_param *params,
10037 size_t params_count)
10039 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
10040 params, params_count,
10041 DEVLINK_CMD_PARAM_DEL);
10043 EXPORT_SYMBOL_GPL(devlink_params_unregister);
10046 * devlink_param_register - register one configuration parameter
10048 * @devlink: devlink
10049 * @param: one configuration parameter
10051 * Register the configuration parameter supported by the driver.
10052 * Return: returns 0 on successful registration or error code otherwise.
10054 int devlink_param_register(struct devlink *devlink,
10055 const struct devlink_param *param)
10059 mutex_lock(&devlink->lock);
10060 err = __devlink_param_register_one(devlink, 0, &devlink->param_list,
10061 param, DEVLINK_CMD_PARAM_NEW);
10062 mutex_unlock(&devlink->lock);
10065 EXPORT_SYMBOL_GPL(devlink_param_register);
10068 * devlink_param_unregister - unregister one configuration parameter
10069 * @devlink: devlink
10070 * @param: configuration parameter to unregister
10072 void devlink_param_unregister(struct devlink *devlink,
10073 const struct devlink_param *param)
10075 mutex_lock(&devlink->lock);
10076 devlink_param_unregister_one(devlink, 0, &devlink->param_list, param,
10077 DEVLINK_CMD_PARAM_DEL);
10078 mutex_unlock(&devlink->lock);
10080 EXPORT_SYMBOL_GPL(devlink_param_unregister);
10083 * devlink_params_publish - publish configuration parameters
10085 * @devlink: devlink
10087 * Publish previously registered configuration parameters.
10089 void devlink_params_publish(struct devlink *devlink)
10091 struct devlink_param_item *param_item;
10093 list_for_each_entry(param_item, &devlink->param_list, list) {
10094 if (param_item->published)
10096 param_item->published = true;
10097 devlink_param_notify(devlink, 0, param_item,
10098 DEVLINK_CMD_PARAM_NEW);
10101 EXPORT_SYMBOL_GPL(devlink_params_publish);
10104 * devlink_params_unpublish - unpublish configuration parameters
10106 * @devlink: devlink
10108 * Unpublish previously registered configuration parameters.
10110 void devlink_params_unpublish(struct devlink *devlink)
10112 struct devlink_param_item *param_item;
10114 list_for_each_entry(param_item, &devlink->param_list, list) {
10115 if (!param_item->published)
10117 param_item->published = false;
10118 devlink_param_notify(devlink, 0, param_item,
10119 DEVLINK_CMD_PARAM_DEL);
10122 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
10125 * devlink_param_publish - publish one configuration parameter
10127 * @devlink: devlink
10128 * @param: one configuration parameter
10130 * Publish previously registered configuration parameter.
10132 void devlink_param_publish(struct devlink *devlink,
10133 const struct devlink_param *param)
10135 struct devlink_param_item *param_item;
10137 list_for_each_entry(param_item, &devlink->param_list, list) {
10138 if (param_item->param != param || param_item->published)
10140 param_item->published = true;
10141 devlink_param_notify(devlink, 0, param_item,
10142 DEVLINK_CMD_PARAM_NEW);
10146 EXPORT_SYMBOL_GPL(devlink_param_publish);
10149 * devlink_param_unpublish - unpublish one configuration parameter
10151 * @devlink: devlink
10152 * @param: one configuration parameter
10154 * Unpublish previously registered configuration parameter.
10156 void devlink_param_unpublish(struct devlink *devlink,
10157 const struct devlink_param *param)
10159 struct devlink_param_item *param_item;
10161 list_for_each_entry(param_item, &devlink->param_list, list) {
10162 if (param_item->param != param || !param_item->published)
10164 param_item->published = false;
10165 devlink_param_notify(devlink, 0, param_item,
10166 DEVLINK_CMD_PARAM_DEL);
10170 EXPORT_SYMBOL_GPL(devlink_param_unpublish);
10173 * devlink_port_params_register - register port configuration parameters
10175 * @devlink_port: devlink port
10176 * @params: configuration parameters array
10177 * @params_count: number of parameters provided
10179 * Register the configuration parameters supported by the port.
10181 int devlink_port_params_register(struct devlink_port *devlink_port,
10182 const struct devlink_param *params,
10183 size_t params_count)
10185 return __devlink_params_register(devlink_port->devlink,
10186 devlink_port->index,
10187 &devlink_port->param_list, params,
10189 DEVLINK_CMD_PORT_PARAM_NEW,
10190 DEVLINK_CMD_PORT_PARAM_DEL);
10192 EXPORT_SYMBOL_GPL(devlink_port_params_register);
10195 * devlink_port_params_unregister - unregister port configuration
10198 * @devlink_port: devlink port
10199 * @params: configuration parameters array
10200 * @params_count: number of parameters provided
10202 void devlink_port_params_unregister(struct devlink_port *devlink_port,
10203 const struct devlink_param *params,
10204 size_t params_count)
10206 return __devlink_params_unregister(devlink_port->devlink,
10207 devlink_port->index,
10208 &devlink_port->param_list,
10209 params, params_count,
10210 DEVLINK_CMD_PORT_PARAM_DEL);
10212 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
10215 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
10216 union devlink_param_value *init_val)
10218 struct devlink_param_item *param_item;
10220 param_item = devlink_param_find_by_id(param_list, param_id);
10224 if (!param_item->driverinit_value_valid ||
10225 !devlink_param_cmode_is_supported(param_item->param,
10226 DEVLINK_PARAM_CMODE_DRIVERINIT))
10227 return -EOPNOTSUPP;
10229 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10230 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
10232 *init_val = param_item->driverinit_value;
10238 __devlink_param_driverinit_value_set(struct devlink *devlink,
10239 unsigned int port_index,
10240 struct list_head *param_list, u32 param_id,
10241 union devlink_param_value init_val,
10242 enum devlink_command cmd)
10244 struct devlink_param_item *param_item;
10246 param_item = devlink_param_find_by_id(param_list, param_id);
10250 if (!devlink_param_cmode_is_supported(param_item->param,
10251 DEVLINK_PARAM_CMODE_DRIVERINIT))
10252 return -EOPNOTSUPP;
10254 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
10255 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
10257 param_item->driverinit_value = init_val;
10258 param_item->driverinit_value_valid = true;
10260 devlink_param_notify(devlink, port_index, param_item, cmd);
10265 * devlink_param_driverinit_value_get - get configuration parameter
10266 * value for driver initializing
10268 * @devlink: devlink
10269 * @param_id: parameter ID
10270 * @init_val: value of parameter in driverinit configuration mode
10272 * This function should be used by the driver to get driverinit
10273 * configuration for initialization after reload command.
10275 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
10276 union devlink_param_value *init_val)
10278 if (!devlink_reload_supported(devlink->ops))
10279 return -EOPNOTSUPP;
10281 return __devlink_param_driverinit_value_get(&devlink->param_list,
10282 param_id, init_val);
10284 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
10287 * devlink_param_driverinit_value_set - set value of configuration
10288 * parameter for driverinit
10289 * configuration mode
10291 * @devlink: devlink
10292 * @param_id: parameter ID
10293 * @init_val: value of parameter to set for driverinit configuration mode
10295 * This function should be used by the driver to set driverinit
10296 * configuration mode default value.
10298 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
10299 union devlink_param_value init_val)
10301 return __devlink_param_driverinit_value_set(devlink, 0,
10302 &devlink->param_list,
10303 param_id, init_val,
10304 DEVLINK_CMD_PARAM_NEW);
10306 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
10309 * devlink_port_param_driverinit_value_get - get configuration parameter
10310 * value for driver initializing
10312 * @devlink_port: devlink_port
10313 * @param_id: parameter ID
10314 * @init_val: value of parameter in driverinit configuration mode
10316 * This function should be used by the driver to get driverinit
10317 * configuration for initialization after reload command.
10319 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
10321 union devlink_param_value *init_val)
10323 struct devlink *devlink = devlink_port->devlink;
10325 if (!devlink_reload_supported(devlink->ops))
10326 return -EOPNOTSUPP;
10328 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
10329 param_id, init_val);
10331 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
10334 * devlink_port_param_driverinit_value_set - set value of configuration
10335 * parameter for driverinit
10336 * configuration mode
10338 * @devlink_port: devlink_port
10339 * @param_id: parameter ID
10340 * @init_val: value of parameter to set for driverinit configuration mode
10342 * This function should be used by the driver to set driverinit
10343 * configuration mode default value.
10345 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
10347 union devlink_param_value init_val)
10349 return __devlink_param_driverinit_value_set(devlink_port->devlink,
10350 devlink_port->index,
10351 &devlink_port->param_list,
10352 param_id, init_val,
10353 DEVLINK_CMD_PORT_PARAM_NEW);
10355 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
10358 * devlink_param_value_changed - notify devlink on a parameter's value
10359 * change. Should be called by the driver
10360 * right after the change.
10362 * @devlink: devlink
10363 * @param_id: parameter ID
10365 * This function should be used by the driver to notify devlink on value
10366 * change, excluding driverinit configuration mode.
10367 * For driverinit configuration mode driver should use the function
10369 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
10371 struct devlink_param_item *param_item;
10373 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
10374 WARN_ON(!param_item);
10376 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
10378 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
10381 * devlink_port_param_value_changed - notify devlink on a parameter's value
10382 * change. Should be called by the driver
10383 * right after the change.
10385 * @devlink_port: devlink_port
10386 * @param_id: parameter ID
10388 * This function should be used by the driver to notify devlink on value
10389 * change, excluding driverinit configuration mode.
10390 * For driverinit configuration mode driver should use the function
10391 * devlink_port_param_driverinit_value_set() instead.
10393 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
10396 struct devlink_param_item *param_item;
10398 param_item = devlink_param_find_by_id(&devlink_port->param_list,
10400 WARN_ON(!param_item);
10402 devlink_param_notify(devlink_port->devlink, devlink_port->index,
10403 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
10405 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
10408 * devlink_param_value_str_fill - Safely fill-up the string preventing
10409 * from overflow of the preallocated buffer
10411 * @dst_val: destination devlink_param_value
10412 * @src: source buffer
10414 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
10419 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
10420 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
10422 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
10425 * devlink_region_create - create a new address region
10427 * @devlink: devlink
10428 * @ops: region operations and name
10429 * @region_max_snapshots: Maximum supported number of snapshots for region
10430 * @region_size: size of region
10432 struct devlink_region *
10433 devlink_region_create(struct devlink *devlink,
10434 const struct devlink_region_ops *ops,
10435 u32 region_max_snapshots, u64 region_size)
10437 struct devlink_region *region;
10440 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
10441 return ERR_PTR(-EINVAL);
10443 mutex_lock(&devlink->lock);
10445 if (devlink_region_get_by_name(devlink, ops->name)) {
10450 region = kzalloc(sizeof(*region), GFP_KERNEL);
10456 region->devlink = devlink;
10457 region->max_snapshots = region_max_snapshots;
10459 region->size = region_size;
10460 INIT_LIST_HEAD(®ion->snapshot_list);
10461 list_add_tail(®ion->list, &devlink->region_list);
10462 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
10464 mutex_unlock(&devlink->lock);
10468 mutex_unlock(&devlink->lock);
10469 return ERR_PTR(err);
10471 EXPORT_SYMBOL_GPL(devlink_region_create);
10474 * devlink_port_region_create - create a new address region for a port
10476 * @port: devlink port
10477 * @ops: region operations and name
10478 * @region_max_snapshots: Maximum supported number of snapshots for region
10479 * @region_size: size of region
10481 struct devlink_region *
10482 devlink_port_region_create(struct devlink_port *port,
10483 const struct devlink_port_region_ops *ops,
10484 u32 region_max_snapshots, u64 region_size)
10486 struct devlink *devlink = port->devlink;
10487 struct devlink_region *region;
10490 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
10491 return ERR_PTR(-EINVAL);
10493 mutex_lock(&devlink->lock);
10495 if (devlink_port_region_get_by_name(port, ops->name)) {
10500 region = kzalloc(sizeof(*region), GFP_KERNEL);
10506 region->devlink = devlink;
10507 region->port = port;
10508 region->max_snapshots = region_max_snapshots;
10509 region->port_ops = ops;
10510 region->size = region_size;
10511 INIT_LIST_HEAD(®ion->snapshot_list);
10512 list_add_tail(®ion->list, &port->region_list);
10513 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
10515 mutex_unlock(&devlink->lock);
10519 mutex_unlock(&devlink->lock);
10520 return ERR_PTR(err);
10522 EXPORT_SYMBOL_GPL(devlink_port_region_create);
10525 * devlink_region_destroy - destroy address region
10527 * @region: devlink region to destroy
10529 void devlink_region_destroy(struct devlink_region *region)
10531 struct devlink *devlink = region->devlink;
10532 struct devlink_snapshot *snapshot, *ts;
10534 mutex_lock(&devlink->lock);
10536 /* Free all snapshots of region */
10537 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
10538 devlink_region_snapshot_del(region, snapshot);
10540 list_del(®ion->list);
10542 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
10543 mutex_unlock(&devlink->lock);
10546 EXPORT_SYMBOL_GPL(devlink_region_destroy);
10549 * devlink_region_snapshot_id_get - get snapshot ID
10551 * This callback should be called when adding a new snapshot,
10552 * Driver should use the same id for multiple snapshots taken
10553 * on multiple regions at the same time/by the same trigger.
10555 * The caller of this function must use devlink_region_snapshot_id_put
10556 * when finished creating regions using this id.
10558 * Returns zero on success, or a negative error code on failure.
10560 * @devlink: devlink
10561 * @id: storage to return id
10563 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
10567 mutex_lock(&devlink->lock);
10568 err = __devlink_region_snapshot_id_get(devlink, id);
10569 mutex_unlock(&devlink->lock);
10573 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
10576 * devlink_region_snapshot_id_put - put snapshot ID reference
10578 * This should be called by a driver after finishing creating snapshots
10579 * with an id. Doing so ensures that the ID can later be released in the
10580 * event that all snapshots using it have been destroyed.
10582 * @devlink: devlink
10583 * @id: id to release reference on
10585 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
10587 mutex_lock(&devlink->lock);
10588 __devlink_snapshot_id_decrement(devlink, id);
10589 mutex_unlock(&devlink->lock);
10591 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
10594 * devlink_region_snapshot_create - create a new snapshot
10595 * This will add a new snapshot of a region. The snapshot
10596 * will be stored on the region struct and can be accessed
10597 * from devlink. This is useful for future analyses of snapshots.
10598 * Multiple snapshots can be created on a region.
10599 * The @snapshot_id should be obtained using the getter function.
10601 * @region: devlink region of the snapshot
10602 * @data: snapshot data
10603 * @snapshot_id: snapshot id to be created
10605 int devlink_region_snapshot_create(struct devlink_region *region,
10606 u8 *data, u32 snapshot_id)
10608 struct devlink *devlink = region->devlink;
10611 mutex_lock(&devlink->lock);
10612 err = __devlink_region_snapshot_create(region, data, snapshot_id);
10613 mutex_unlock(&devlink->lock);
10617 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
10619 #define DEVLINK_TRAP(_id, _type) \
10621 .type = DEVLINK_TRAP_TYPE_##_type, \
10622 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
10623 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
10626 static const struct devlink_trap devlink_trap_generic[] = {
10627 DEVLINK_TRAP(SMAC_MC, DROP),
10628 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
10629 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
10630 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
10631 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
10632 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
10633 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
10634 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
10635 DEVLINK_TRAP(TAIL_DROP, DROP),
10636 DEVLINK_TRAP(NON_IP_PACKET, DROP),
10637 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
10638 DEVLINK_TRAP(DIP_LB, DROP),
10639 DEVLINK_TRAP(SIP_MC, DROP),
10640 DEVLINK_TRAP(SIP_LB, DROP),
10641 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
10642 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
10643 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
10644 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
10645 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
10646 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
10647 DEVLINK_TRAP(RPF, EXCEPTION),
10648 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
10649 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
10650 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
10651 DEVLINK_TRAP(NON_ROUTABLE, DROP),
10652 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
10653 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
10654 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
10655 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
10656 DEVLINK_TRAP(STP, CONTROL),
10657 DEVLINK_TRAP(LACP, CONTROL),
10658 DEVLINK_TRAP(LLDP, CONTROL),
10659 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
10660 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
10661 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
10662 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
10663 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
10664 DEVLINK_TRAP(MLD_QUERY, CONTROL),
10665 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
10666 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
10667 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
10668 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
10669 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
10670 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
10671 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
10672 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
10673 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
10674 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
10675 DEVLINK_TRAP(IPV4_BFD, CONTROL),
10676 DEVLINK_TRAP(IPV6_BFD, CONTROL),
10677 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
10678 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
10679 DEVLINK_TRAP(IPV4_BGP, CONTROL),
10680 DEVLINK_TRAP(IPV6_BGP, CONTROL),
10681 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
10682 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
10683 DEVLINK_TRAP(IPV4_PIM, CONTROL),
10684 DEVLINK_TRAP(IPV6_PIM, CONTROL),
10685 DEVLINK_TRAP(UC_LB, CONTROL),
10686 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
10687 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
10688 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
10689 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
10690 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
10691 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
10692 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
10693 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
10694 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
10695 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
10696 DEVLINK_TRAP(PTP_EVENT, CONTROL),
10697 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
10698 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
10699 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
10700 DEVLINK_TRAP(EARLY_DROP, DROP),
10701 DEVLINK_TRAP(VXLAN_PARSING, DROP),
10702 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
10703 DEVLINK_TRAP(VLAN_PARSING, DROP),
10704 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
10705 DEVLINK_TRAP(MPLS_PARSING, DROP),
10706 DEVLINK_TRAP(ARP_PARSING, DROP),
10707 DEVLINK_TRAP(IP_1_PARSING, DROP),
10708 DEVLINK_TRAP(IP_N_PARSING, DROP),
10709 DEVLINK_TRAP(GRE_PARSING, DROP),
10710 DEVLINK_TRAP(UDP_PARSING, DROP),
10711 DEVLINK_TRAP(TCP_PARSING, DROP),
10712 DEVLINK_TRAP(IPSEC_PARSING, DROP),
10713 DEVLINK_TRAP(SCTP_PARSING, DROP),
10714 DEVLINK_TRAP(DCCP_PARSING, DROP),
10715 DEVLINK_TRAP(GTP_PARSING, DROP),
10716 DEVLINK_TRAP(ESP_PARSING, DROP),
10717 DEVLINK_TRAP(BLACKHOLE_NEXTHOP, DROP),
10718 DEVLINK_TRAP(DMAC_FILTER, DROP),
10721 #define DEVLINK_TRAP_GROUP(_id) \
10723 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
10724 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
10727 static const struct devlink_trap_group devlink_trap_group_generic[] = {
10728 DEVLINK_TRAP_GROUP(L2_DROPS),
10729 DEVLINK_TRAP_GROUP(L3_DROPS),
10730 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
10731 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
10732 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
10733 DEVLINK_TRAP_GROUP(ACL_DROPS),
10734 DEVLINK_TRAP_GROUP(STP),
10735 DEVLINK_TRAP_GROUP(LACP),
10736 DEVLINK_TRAP_GROUP(LLDP),
10737 DEVLINK_TRAP_GROUP(MC_SNOOPING),
10738 DEVLINK_TRAP_GROUP(DHCP),
10739 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
10740 DEVLINK_TRAP_GROUP(BFD),
10741 DEVLINK_TRAP_GROUP(OSPF),
10742 DEVLINK_TRAP_GROUP(BGP),
10743 DEVLINK_TRAP_GROUP(VRRP),
10744 DEVLINK_TRAP_GROUP(PIM),
10745 DEVLINK_TRAP_GROUP(UC_LB),
10746 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
10747 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
10748 DEVLINK_TRAP_GROUP(IPV6),
10749 DEVLINK_TRAP_GROUP(PTP_EVENT),
10750 DEVLINK_TRAP_GROUP(PTP_GENERAL),
10751 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
10752 DEVLINK_TRAP_GROUP(ACL_TRAP),
10753 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
10756 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
10758 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
10761 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
10764 if (trap->type != devlink_trap_generic[trap->id].type)
10770 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
10774 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
10777 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
10778 if (!strcmp(trap->name, devlink_trap_generic[i].name))
10785 static int devlink_trap_verify(const struct devlink_trap *trap)
10787 if (!trap || !trap->name)
10791 return devlink_trap_generic_verify(trap);
10793 return devlink_trap_driver_verify(trap);
10797 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
10799 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
10802 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
10809 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
10813 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
10816 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
10817 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
10824 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
10826 if (group->generic)
10827 return devlink_trap_group_generic_verify(group);
10829 return devlink_trap_group_driver_verify(group);
10833 devlink_trap_group_notify(struct devlink *devlink,
10834 const struct devlink_trap_group_item *group_item,
10835 enum devlink_command cmd)
10837 struct sk_buff *msg;
10840 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
10841 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
10843 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10847 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
10854 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10855 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10859 devlink_trap_item_group_link(struct devlink *devlink,
10860 struct devlink_trap_item *trap_item)
10862 u16 group_id = trap_item->trap->init_group_id;
10863 struct devlink_trap_group_item *group_item;
10865 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
10866 if (WARN_ON_ONCE(!group_item))
10869 trap_item->group_item = group_item;
10874 static void devlink_trap_notify(struct devlink *devlink,
10875 const struct devlink_trap_item *trap_item,
10876 enum devlink_command cmd)
10878 struct sk_buff *msg;
10881 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
10882 cmd != DEVLINK_CMD_TRAP_DEL);
10884 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10888 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
10894 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10895 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10899 devlink_trap_register(struct devlink *devlink,
10900 const struct devlink_trap *trap, void *priv)
10902 struct devlink_trap_item *trap_item;
10905 if (devlink_trap_item_lookup(devlink, trap->name))
10908 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
10912 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
10913 if (!trap_item->stats) {
10915 goto err_stats_alloc;
10918 trap_item->trap = trap;
10919 trap_item->action = trap->init_action;
10920 trap_item->priv = priv;
10922 err = devlink_trap_item_group_link(devlink, trap_item);
10924 goto err_group_link;
10926 err = devlink->ops->trap_init(devlink, trap, trap_item);
10928 goto err_trap_init;
10930 list_add_tail(&trap_item->list, &devlink->trap_list);
10931 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
10937 free_percpu(trap_item->stats);
10943 static void devlink_trap_unregister(struct devlink *devlink,
10944 const struct devlink_trap *trap)
10946 struct devlink_trap_item *trap_item;
10948 trap_item = devlink_trap_item_lookup(devlink, trap->name);
10949 if (WARN_ON_ONCE(!trap_item))
10952 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
10953 list_del(&trap_item->list);
10954 if (devlink->ops->trap_fini)
10955 devlink->ops->trap_fini(devlink, trap, trap_item);
10956 free_percpu(trap_item->stats);
10960 static void devlink_trap_disable(struct devlink *devlink,
10961 const struct devlink_trap *trap)
10963 struct devlink_trap_item *trap_item;
10965 trap_item = devlink_trap_item_lookup(devlink, trap->name);
10966 if (WARN_ON_ONCE(!trap_item))
10969 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
10971 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
10975 * devlink_traps_register - Register packet traps with devlink.
10976 * @devlink: devlink.
10977 * @traps: Packet traps.
10978 * @traps_count: Count of provided packet traps.
10979 * @priv: Driver private information.
10981 * Return: Non-zero value on failure.
10983 int devlink_traps_register(struct devlink *devlink,
10984 const struct devlink_trap *traps,
10985 size_t traps_count, void *priv)
10989 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
10992 mutex_lock(&devlink->lock);
10993 for (i = 0; i < traps_count; i++) {
10994 const struct devlink_trap *trap = &traps[i];
10996 err = devlink_trap_verify(trap);
10998 goto err_trap_verify;
11000 err = devlink_trap_register(devlink, trap, priv);
11002 goto err_trap_register;
11004 mutex_unlock(&devlink->lock);
11010 for (i--; i >= 0; i--)
11011 devlink_trap_unregister(devlink, &traps[i]);
11012 mutex_unlock(&devlink->lock);
11015 EXPORT_SYMBOL_GPL(devlink_traps_register);
11018 * devlink_traps_unregister - Unregister packet traps from devlink.
11019 * @devlink: devlink.
11020 * @traps: Packet traps.
11021 * @traps_count: Count of provided packet traps.
11023 void devlink_traps_unregister(struct devlink *devlink,
11024 const struct devlink_trap *traps,
11025 size_t traps_count)
11029 mutex_lock(&devlink->lock);
11030 /* Make sure we do not have any packets in-flight while unregistering
11031 * traps by disabling all of them and waiting for a grace period.
11033 for (i = traps_count - 1; i >= 0; i--)
11034 devlink_trap_disable(devlink, &traps[i]);
11036 for (i = traps_count - 1; i >= 0; i--)
11037 devlink_trap_unregister(devlink, &traps[i]);
11038 mutex_unlock(&devlink->lock);
11040 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
11043 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
11046 struct devlink_stats *stats;
11048 stats = this_cpu_ptr(trap_stats);
11049 u64_stats_update_begin(&stats->syncp);
11050 stats->rx_bytes += skb_len;
11051 stats->rx_packets++;
11052 u64_stats_update_end(&stats->syncp);
11056 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
11057 const struct devlink_trap_item *trap_item,
11058 struct devlink_port *in_devlink_port,
11059 const struct flow_action_cookie *fa_cookie)
11061 metadata->trap_name = trap_item->trap->name;
11062 metadata->trap_group_name = trap_item->group_item->group->name;
11063 metadata->fa_cookie = fa_cookie;
11064 metadata->trap_type = trap_item->trap->type;
11066 spin_lock(&in_devlink_port->type_lock);
11067 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
11068 metadata->input_dev = in_devlink_port->type_dev;
11069 spin_unlock(&in_devlink_port->type_lock);
11073 * devlink_trap_report - Report trapped packet to drop monitor.
11074 * @devlink: devlink.
11075 * @skb: Trapped packet.
11076 * @trap_ctx: Trap context.
11077 * @in_devlink_port: Input devlink port.
11078 * @fa_cookie: Flow action cookie. Could be NULL.
11080 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
11081 void *trap_ctx, struct devlink_port *in_devlink_port,
11082 const struct flow_action_cookie *fa_cookie)
11085 struct devlink_trap_item *trap_item = trap_ctx;
11087 devlink_trap_stats_update(trap_item->stats, skb->len);
11088 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
11090 if (trace_devlink_trap_report_enabled()) {
11091 struct devlink_trap_metadata metadata = {};
11093 devlink_trap_report_metadata_set(&metadata, trap_item,
11094 in_devlink_port, fa_cookie);
11095 trace_devlink_trap_report(devlink, skb, &metadata);
11098 EXPORT_SYMBOL_GPL(devlink_trap_report);
11101 * devlink_trap_ctx_priv - Trap context to driver private information.
11102 * @trap_ctx: Trap context.
11104 * Return: Driver private information passed during registration.
11106 void *devlink_trap_ctx_priv(void *trap_ctx)
11108 struct devlink_trap_item *trap_item = trap_ctx;
11110 return trap_item->priv;
11112 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
11115 devlink_trap_group_item_policer_link(struct devlink *devlink,
11116 struct devlink_trap_group_item *group_item)
11118 u32 policer_id = group_item->group->init_policer_id;
11119 struct devlink_trap_policer_item *policer_item;
11121 if (policer_id == 0)
11124 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
11125 if (WARN_ON_ONCE(!policer_item))
11128 group_item->policer_item = policer_item;
11134 devlink_trap_group_register(struct devlink *devlink,
11135 const struct devlink_trap_group *group)
11137 struct devlink_trap_group_item *group_item;
11140 if (devlink_trap_group_item_lookup(devlink, group->name))
11143 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
11147 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
11148 if (!group_item->stats) {
11150 goto err_stats_alloc;
11153 group_item->group = group;
11155 err = devlink_trap_group_item_policer_link(devlink, group_item);
11157 goto err_policer_link;
11159 if (devlink->ops->trap_group_init) {
11160 err = devlink->ops->trap_group_init(devlink, group);
11162 goto err_group_init;
11165 list_add_tail(&group_item->list, &devlink->trap_group_list);
11166 devlink_trap_group_notify(devlink, group_item,
11167 DEVLINK_CMD_TRAP_GROUP_NEW);
11173 free_percpu(group_item->stats);
11180 devlink_trap_group_unregister(struct devlink *devlink,
11181 const struct devlink_trap_group *group)
11183 struct devlink_trap_group_item *group_item;
11185 group_item = devlink_trap_group_item_lookup(devlink, group->name);
11186 if (WARN_ON_ONCE(!group_item))
11189 devlink_trap_group_notify(devlink, group_item,
11190 DEVLINK_CMD_TRAP_GROUP_DEL);
11191 list_del(&group_item->list);
11192 free_percpu(group_item->stats);
11197 * devlink_trap_groups_register - Register packet trap groups with devlink.
11198 * @devlink: devlink.
11199 * @groups: Packet trap groups.
11200 * @groups_count: Count of provided packet trap groups.
11202 * Return: Non-zero value on failure.
11204 int devlink_trap_groups_register(struct devlink *devlink,
11205 const struct devlink_trap_group *groups,
11206 size_t groups_count)
11210 mutex_lock(&devlink->lock);
11211 for (i = 0; i < groups_count; i++) {
11212 const struct devlink_trap_group *group = &groups[i];
11214 err = devlink_trap_group_verify(group);
11216 goto err_trap_group_verify;
11218 err = devlink_trap_group_register(devlink, group);
11220 goto err_trap_group_register;
11222 mutex_unlock(&devlink->lock);
11226 err_trap_group_register:
11227 err_trap_group_verify:
11228 for (i--; i >= 0; i--)
11229 devlink_trap_group_unregister(devlink, &groups[i]);
11230 mutex_unlock(&devlink->lock);
11233 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
11236 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
11237 * @devlink: devlink.
11238 * @groups: Packet trap groups.
11239 * @groups_count: Count of provided packet trap groups.
11241 void devlink_trap_groups_unregister(struct devlink *devlink,
11242 const struct devlink_trap_group *groups,
11243 size_t groups_count)
11247 mutex_lock(&devlink->lock);
11248 for (i = groups_count - 1; i >= 0; i--)
11249 devlink_trap_group_unregister(devlink, &groups[i]);
11250 mutex_unlock(&devlink->lock);
11252 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
11255 devlink_trap_policer_notify(struct devlink *devlink,
11256 const struct devlink_trap_policer_item *policer_item,
11257 enum devlink_command cmd)
11259 struct sk_buff *msg;
11262 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
11263 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
11265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11269 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
11276 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
11277 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
11281 devlink_trap_policer_register(struct devlink *devlink,
11282 const struct devlink_trap_policer *policer)
11284 struct devlink_trap_policer_item *policer_item;
11287 if (devlink_trap_policer_item_lookup(devlink, policer->id))
11290 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
11294 policer_item->policer = policer;
11295 policer_item->rate = policer->init_rate;
11296 policer_item->burst = policer->init_burst;
11298 if (devlink->ops->trap_policer_init) {
11299 err = devlink->ops->trap_policer_init(devlink, policer);
11301 goto err_policer_init;
11304 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
11305 devlink_trap_policer_notify(devlink, policer_item,
11306 DEVLINK_CMD_TRAP_POLICER_NEW);
11311 kfree(policer_item);
11316 devlink_trap_policer_unregister(struct devlink *devlink,
11317 const struct devlink_trap_policer *policer)
11319 struct devlink_trap_policer_item *policer_item;
11321 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
11322 if (WARN_ON_ONCE(!policer_item))
11325 devlink_trap_policer_notify(devlink, policer_item,
11326 DEVLINK_CMD_TRAP_POLICER_DEL);
11327 list_del(&policer_item->list);
11328 if (devlink->ops->trap_policer_fini)
11329 devlink->ops->trap_policer_fini(devlink, policer);
11330 kfree(policer_item);
11334 * devlink_trap_policers_register - Register packet trap policers with devlink.
11335 * @devlink: devlink.
11336 * @policers: Packet trap policers.
11337 * @policers_count: Count of provided packet trap policers.
11339 * Return: Non-zero value on failure.
11342 devlink_trap_policers_register(struct devlink *devlink,
11343 const struct devlink_trap_policer *policers,
11344 size_t policers_count)
11348 mutex_lock(&devlink->lock);
11349 for (i = 0; i < policers_count; i++) {
11350 const struct devlink_trap_policer *policer = &policers[i];
11352 if (WARN_ON(policer->id == 0 ||
11353 policer->max_rate < policer->min_rate ||
11354 policer->max_burst < policer->min_burst)) {
11356 goto err_trap_policer_verify;
11359 err = devlink_trap_policer_register(devlink, policer);
11361 goto err_trap_policer_register;
11363 mutex_unlock(&devlink->lock);
11367 err_trap_policer_register:
11368 err_trap_policer_verify:
11369 for (i--; i >= 0; i--)
11370 devlink_trap_policer_unregister(devlink, &policers[i]);
11371 mutex_unlock(&devlink->lock);
11374 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
11377 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
11378 * @devlink: devlink.
11379 * @policers: Packet trap policers.
11380 * @policers_count: Count of provided packet trap policers.
11383 devlink_trap_policers_unregister(struct devlink *devlink,
11384 const struct devlink_trap_policer *policers,
11385 size_t policers_count)
11389 mutex_lock(&devlink->lock);
11390 for (i = policers_count - 1; i >= 0; i--)
11391 devlink_trap_policer_unregister(devlink, &policers[i]);
11392 mutex_unlock(&devlink->lock);
11394 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
11396 static void __devlink_compat_running_version(struct devlink *devlink,
11397 char *buf, size_t len)
11399 const struct nlattr *nlattr;
11400 struct devlink_info_req req;
11401 struct sk_buff *msg;
11404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11409 err = devlink->ops->info_get(devlink, &req, NULL);
11413 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
11414 const struct nlattr *kv;
11417 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
11420 nla_for_each_nested(kv, nlattr, rem_kv) {
11421 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
11424 strlcat(buf, nla_data(kv), len);
11425 strlcat(buf, " ", len);
11432 void devlink_compat_running_version(struct net_device *dev,
11433 char *buf, size_t len)
11435 struct devlink *devlink;
11440 devlink = netdev_to_devlink(dev);
11441 if (!devlink || !devlink->ops->info_get)
11444 mutex_lock(&devlink->lock);
11445 __devlink_compat_running_version(devlink, buf, len);
11446 mutex_unlock(&devlink->lock);
11453 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
11455 struct devlink_flash_update_params params = {};
11456 struct devlink *devlink;
11462 devlink = netdev_to_devlink(dev);
11463 if (!devlink || !devlink->ops->flash_update) {
11468 ret = request_firmware(¶ms.fw, file_name, devlink->dev);
11472 mutex_lock(&devlink->lock);
11473 devlink_flash_update_begin_notify(devlink);
11474 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
11475 devlink_flash_update_end_notify(devlink);
11476 mutex_unlock(&devlink->lock);
11478 release_firmware(params.fw);
11487 int devlink_compat_phys_port_name_get(struct net_device *dev,
11488 char *name, size_t len)
11490 struct devlink_port *devlink_port;
11492 /* RTNL mutex is held here which ensures that devlink_port
11493 * instance cannot disappear in the middle. No need to take
11494 * any devlink lock as only permanent values are accessed.
11498 devlink_port = netdev_to_devlink_port(dev);
11500 return -EOPNOTSUPP;
11502 return __devlink_port_phys_port_name_get(devlink_port, name, len);
11505 int devlink_compat_switch_id_get(struct net_device *dev,
11506 struct netdev_phys_item_id *ppid)
11508 struct devlink_port *devlink_port;
11510 /* Caller must hold RTNL mutex or reference to dev, which ensures that
11511 * devlink_port instance cannot disappear in the middle. No need to take
11512 * any devlink lock as only permanent values are accessed.
11514 devlink_port = netdev_to_devlink_port(dev);
11515 if (!devlink_port || !devlink_port->switch_port)
11516 return -EOPNOTSUPP;
11518 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
11523 static void __net_exit devlink_pernet_pre_exit(struct net *net)
11525 struct devlink *devlink;
11526 u32 actions_performed;
11527 unsigned long index;
11530 /* In case network namespace is getting destroyed, reload
11531 * all devlink instances from this namespace into init_net.
11533 mutex_lock(&devlink_mutex);
11534 xa_for_each_marked(&devlinks, index, devlink, DEVLINK_REGISTERED) {
11535 if (!devlink_try_get(devlink))
11538 if (!net_eq(devlink_net(devlink), net))
11541 WARN_ON(!devlink_reload_supported(devlink->ops));
11542 err = devlink_reload(devlink, &init_net,
11543 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
11544 DEVLINK_RELOAD_LIMIT_UNSPEC,
11545 &actions_performed, NULL);
11546 if (err && err != -EOPNOTSUPP)
11547 pr_warn("Failed to reload devlink instance into init_net\n");
11549 devlink_put(devlink);
11551 mutex_unlock(&devlink_mutex);
11554 static struct pernet_operations devlink_pernet_ops __net_initdata = {
11555 .pre_exit = devlink_pernet_pre_exit,
11558 static int __init devlink_init(void)
11562 err = genl_register_family(&devlink_nl_family);
11565 err = register_pernet_subsys(&devlink_pernet_ops);
11572 subsys_initcall(devlink_init);