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 },
92 static LIST_HEAD(devlink_list);
96 * An overall lock guarding every operation coming from userspace.
97 * It also guards devlink devices list and it is taken when
98 * driver registers/unregisters it.
100 static DEFINE_MUTEX(devlink_mutex);
102 struct net *devlink_net(const struct devlink *devlink)
104 return read_pnet(&devlink->_net);
106 EXPORT_SYMBOL_GPL(devlink_net);
108 static void __devlink_net_set(struct devlink *devlink, struct net *net)
110 write_pnet(&devlink->_net, net);
113 void devlink_net_set(struct devlink *devlink, struct net *net)
115 if (WARN_ON(devlink->registered))
117 __devlink_net_set(devlink, net);
119 EXPORT_SYMBOL_GPL(devlink_net_set);
121 static struct devlink *devlink_get_from_attrs(struct net *net,
122 struct nlattr **attrs)
124 struct devlink *devlink;
128 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
129 return ERR_PTR(-EINVAL);
131 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
132 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
134 lockdep_assert_held(&devlink_mutex);
136 list_for_each_entry(devlink, &devlink_list, list) {
137 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
138 strcmp(dev_name(devlink->dev), devname) == 0 &&
139 net_eq(devlink_net(devlink), net))
143 return ERR_PTR(-ENODEV);
146 static struct devlink *devlink_get_from_info(struct genl_info *info)
148 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
151 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
152 unsigned int port_index)
154 struct devlink_port *devlink_port;
156 list_for_each_entry(devlink_port, &devlink->port_list, list) {
157 if (devlink_port->index == port_index)
163 static bool devlink_port_index_exists(struct devlink *devlink,
164 unsigned int port_index)
166 return devlink_port_get_by_index(devlink, port_index);
169 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
170 struct nlattr **attrs)
172 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
173 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
174 struct devlink_port *devlink_port;
176 devlink_port = devlink_port_get_by_index(devlink, port_index);
178 return ERR_PTR(-ENODEV);
181 return ERR_PTR(-EINVAL);
184 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
185 struct genl_info *info)
187 return devlink_port_get_from_attrs(devlink, info->attrs);
191 struct list_head list;
194 u16 ingress_pools_count;
195 u16 egress_pools_count;
196 u16 ingress_tc_count;
200 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
202 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
205 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
206 unsigned int sb_index)
208 struct devlink_sb *devlink_sb;
210 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
211 if (devlink_sb->index == sb_index)
217 static bool devlink_sb_index_exists(struct devlink *devlink,
218 unsigned int sb_index)
220 return devlink_sb_get_by_index(devlink, sb_index);
223 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
224 struct nlattr **attrs)
226 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
227 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
228 struct devlink_sb *devlink_sb;
230 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
232 return ERR_PTR(-ENODEV);
235 return ERR_PTR(-EINVAL);
238 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
239 struct genl_info *info)
241 return devlink_sb_get_from_attrs(devlink, info->attrs);
244 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
245 struct nlattr **attrs,
250 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
253 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
254 if (val >= devlink_sb_pool_count(devlink_sb))
260 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
261 struct genl_info *info,
264 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
269 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
270 enum devlink_sb_pool_type *p_pool_type)
274 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
277 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
278 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
279 val != DEVLINK_SB_POOL_TYPE_EGRESS)
286 devlink_sb_pool_type_get_from_info(struct genl_info *info,
287 enum devlink_sb_pool_type *p_pool_type)
289 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
293 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
294 enum devlink_sb_threshold_type *p_th_type)
298 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
301 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
302 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
303 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
310 devlink_sb_th_type_get_from_info(struct genl_info *info,
311 enum devlink_sb_threshold_type *p_th_type)
313 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
317 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
318 struct nlattr **attrs,
319 enum devlink_sb_pool_type pool_type,
324 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
327 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
328 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
329 val >= devlink_sb->ingress_tc_count)
331 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
332 val >= devlink_sb->egress_tc_count)
339 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
340 struct genl_info *info,
341 enum devlink_sb_pool_type pool_type,
344 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
345 pool_type, p_tc_index);
348 struct devlink_region {
349 struct devlink *devlink;
350 struct devlink_port *port;
351 struct list_head list;
353 const struct devlink_region_ops *ops;
354 const struct devlink_port_region_ops *port_ops;
356 struct list_head snapshot_list;
362 struct devlink_snapshot {
363 struct list_head list;
364 struct devlink_region *region;
369 static struct devlink_region *
370 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
372 struct devlink_region *region;
374 list_for_each_entry(region, &devlink->region_list, list)
375 if (!strcmp(region->ops->name, region_name))
381 static struct devlink_region *
382 devlink_port_region_get_by_name(struct devlink_port *port,
383 const char *region_name)
385 struct devlink_region *region;
387 list_for_each_entry(region, &port->region_list, list)
388 if (!strcmp(region->ops->name, region_name))
394 static struct devlink_snapshot *
395 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
397 struct devlink_snapshot *snapshot;
399 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
400 if (snapshot->id == id)
406 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
407 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
409 /* The per devlink instance lock is taken by default in the pre-doit
410 * operation, yet several commands do not require this. The global
411 * devlink lock is taken and protects from disruption by user-calls.
413 #define DEVLINK_NL_FLAG_NO_LOCK BIT(2)
415 static int devlink_nl_pre_doit(const struct genl_ops *ops,
416 struct sk_buff *skb, struct genl_info *info)
418 struct devlink_port *devlink_port;
419 struct devlink *devlink;
422 mutex_lock(&devlink_mutex);
423 devlink = devlink_get_from_info(info);
424 if (IS_ERR(devlink)) {
425 mutex_unlock(&devlink_mutex);
426 return PTR_ERR(devlink);
428 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
429 mutex_lock(&devlink->lock);
430 info->user_ptr[0] = devlink;
431 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
432 devlink_port = devlink_port_get_from_info(devlink, info);
433 if (IS_ERR(devlink_port)) {
434 err = PTR_ERR(devlink_port);
437 info->user_ptr[1] = devlink_port;
438 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
439 devlink_port = devlink_port_get_from_info(devlink, info);
440 if (!IS_ERR(devlink_port))
441 info->user_ptr[1] = devlink_port;
446 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
447 mutex_unlock(&devlink->lock);
448 mutex_unlock(&devlink_mutex);
452 static void devlink_nl_post_doit(const struct genl_ops *ops,
453 struct sk_buff *skb, struct genl_info *info)
455 struct devlink *devlink;
457 devlink = info->user_ptr[0];
458 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
459 mutex_unlock(&devlink->lock);
460 mutex_unlock(&devlink_mutex);
463 static struct genl_family devlink_nl_family;
465 enum devlink_multicast_groups {
466 DEVLINK_MCGRP_CONFIG,
469 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
470 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
473 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
475 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
477 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
482 struct devlink_reload_combination {
483 enum devlink_reload_action action;
484 enum devlink_reload_limit limit;
487 static const struct devlink_reload_combination devlink_reload_invalid_combinations[] = {
489 /* can't reinitialize driver with no down time */
490 .action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
491 .limit = DEVLINK_RELOAD_LIMIT_NO_RESET,
496 devlink_reload_combination_is_invalid(enum devlink_reload_action action,
497 enum devlink_reload_limit limit)
501 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++)
502 if (devlink_reload_invalid_combinations[i].action == action &&
503 devlink_reload_invalid_combinations[i].limit == limit)
509 devlink_reload_action_is_supported(struct devlink *devlink, enum devlink_reload_action action)
511 return test_bit(action, &devlink->ops->reload_actions);
515 devlink_reload_limit_is_supported(struct devlink *devlink, enum devlink_reload_limit limit)
517 return test_bit(limit, &devlink->ops->reload_limits);
520 static int devlink_reload_stat_put(struct sk_buff *msg, enum devlink_reload_action action,
521 enum devlink_reload_limit limit, u32 value)
523 struct nlattr *reload_stats_entry;
525 reload_stats_entry = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS_ENTRY);
526 if (!reload_stats_entry)
529 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_ACTION, action) ||
530 nla_put_u8(msg, DEVLINK_ATTR_RELOAD_STATS_LIMIT, limit) ||
531 nla_put_u32(msg, DEVLINK_ATTR_RELOAD_STATS_VALUE, value))
532 goto nla_put_failure;
533 nla_nest_end(msg, reload_stats_entry);
537 nla_nest_cancel(msg, reload_stats_entry);
541 static int devlink_reload_stats_put(struct sk_buff *msg, struct devlink *devlink, bool is_remote)
543 struct nlattr *reload_stats_attr;
548 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_RELOAD_STATS);
550 reload_stats_attr = nla_nest_start(msg, DEVLINK_ATTR_REMOTE_RELOAD_STATS);
552 if (!reload_stats_attr)
555 for (j = 0; j <= DEVLINK_RELOAD_LIMIT_MAX; j++) {
556 /* Remote stats are shown even if not locally supported. Stats
557 * of actions with unspecified limit are shown though drivers
558 * don't need to register unspecified limit.
560 if (!is_remote && j != DEVLINK_RELOAD_LIMIT_UNSPEC &&
561 !devlink_reload_limit_is_supported(devlink, j))
563 for (i = 0; i <= DEVLINK_RELOAD_ACTION_MAX; i++) {
564 if ((!is_remote && !devlink_reload_action_is_supported(devlink, i)) ||
565 i == DEVLINK_RELOAD_ACTION_UNSPEC ||
566 devlink_reload_combination_is_invalid(i, j))
569 stat_idx = j * __DEVLINK_RELOAD_ACTION_MAX + i;
571 value = devlink->stats.reload_stats[stat_idx];
573 value = devlink->stats.remote_reload_stats[stat_idx];
574 if (devlink_reload_stat_put(msg, i, j, value))
575 goto nla_put_failure;
578 nla_nest_end(msg, reload_stats_attr);
582 nla_nest_cancel(msg, reload_stats_attr);
586 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
587 enum devlink_command cmd, u32 portid,
590 struct nlattr *dev_stats;
593 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
597 if (devlink_nl_put_handle(msg, devlink))
598 goto nla_put_failure;
599 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
600 goto nla_put_failure;
602 dev_stats = nla_nest_start(msg, DEVLINK_ATTR_DEV_STATS);
604 goto nla_put_failure;
606 if (devlink_reload_stats_put(msg, devlink, false))
607 goto dev_stats_nest_cancel;
608 if (devlink_reload_stats_put(msg, devlink, true))
609 goto dev_stats_nest_cancel;
611 nla_nest_end(msg, dev_stats);
612 genlmsg_end(msg, hdr);
615 dev_stats_nest_cancel:
616 nla_nest_cancel(msg, dev_stats);
618 genlmsg_cancel(msg, hdr);
622 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
627 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
629 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
633 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
639 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
640 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
643 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
644 struct devlink_port *devlink_port)
646 struct devlink_port_attrs *attrs = &devlink_port->attrs;
648 if (!devlink_port->attrs_set)
651 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
654 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
656 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
658 switch (devlink_port->attrs.flavour) {
659 case DEVLINK_PORT_FLAVOUR_PCI_PF:
660 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
661 attrs->pci_pf.controller) ||
662 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
664 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
667 case DEVLINK_PORT_FLAVOUR_PCI_VF:
668 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
669 attrs->pci_vf.controller) ||
670 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
671 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
673 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
676 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
677 case DEVLINK_PORT_FLAVOUR_CPU:
678 case DEVLINK_PORT_FLAVOUR_DSA:
679 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
680 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
681 attrs->phys.port_number))
685 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
686 attrs->phys.port_number))
688 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
689 attrs->phys.split_subport_number))
699 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
700 struct netlink_ext_ack *extack)
702 struct devlink *devlink = port->devlink;
703 const struct devlink_ops *ops;
704 struct nlattr *function_attr;
705 bool empty_nest = true;
708 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
713 if (ops->port_function_hw_addr_get) {
715 u8 hw_addr[MAX_ADDR_LEN];
717 err = ops->port_function_hw_addr_get(devlink, port, hw_addr, &hw_addr_len, extack);
718 if (err == -EOPNOTSUPP) {
719 /* Port function attributes are optional for a port. If port doesn't
720 * support function attribute, returning -EOPNOTSUPP is not an error.
727 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
734 if (err || empty_nest)
735 nla_nest_cancel(msg, function_attr);
737 nla_nest_end(msg, function_attr);
741 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
742 struct devlink_port *devlink_port,
743 enum devlink_command cmd, u32 portid,
745 struct netlink_ext_ack *extack)
749 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
753 if (devlink_nl_put_handle(msg, devlink))
754 goto nla_put_failure;
755 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
756 goto nla_put_failure;
758 spin_lock_bh(&devlink_port->type_lock);
759 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
760 goto nla_put_failure_type_locked;
761 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
762 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
763 devlink_port->desired_type))
764 goto nla_put_failure_type_locked;
765 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
766 struct net_device *netdev = devlink_port->type_dev;
769 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
771 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
773 goto nla_put_failure_type_locked;
775 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
776 struct ib_device *ibdev = devlink_port->type_dev;
779 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
781 goto nla_put_failure_type_locked;
783 spin_unlock_bh(&devlink_port->type_lock);
784 if (devlink_nl_port_attrs_put(msg, devlink_port))
785 goto nla_put_failure;
786 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
787 goto nla_put_failure;
789 genlmsg_end(msg, hdr);
792 nla_put_failure_type_locked:
793 spin_unlock_bh(&devlink_port->type_lock);
795 genlmsg_cancel(msg, hdr);
799 static void devlink_port_notify(struct devlink_port *devlink_port,
800 enum devlink_command cmd)
802 struct devlink *devlink = devlink_port->devlink;
806 if (!devlink_port->registered)
809 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
811 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
815 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0,
822 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
823 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
826 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
828 struct devlink *devlink = info->user_ptr[0];
832 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
836 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
837 info->snd_portid, info->snd_seq, 0);
843 return genlmsg_reply(msg, info);
846 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
847 struct netlink_callback *cb)
849 struct devlink *devlink;
850 int start = cb->args[0];
854 mutex_lock(&devlink_mutex);
855 list_for_each_entry(devlink, &devlink_list, list) {
856 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
862 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
863 NETLINK_CB(cb->skb).portid,
864 cb->nlh->nlmsg_seq, NLM_F_MULTI);
870 mutex_unlock(&devlink_mutex);
876 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
877 struct genl_info *info)
879 struct devlink_port *devlink_port = info->user_ptr[1];
880 struct devlink *devlink = devlink_port->devlink;
884 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
888 err = devlink_nl_port_fill(msg, devlink, devlink_port,
889 DEVLINK_CMD_PORT_NEW,
890 info->snd_portid, info->snd_seq, 0,
897 return genlmsg_reply(msg, info);
900 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
901 struct netlink_callback *cb)
903 struct devlink *devlink;
904 struct devlink_port *devlink_port;
905 int start = cb->args[0];
909 mutex_lock(&devlink_mutex);
910 list_for_each_entry(devlink, &devlink_list, list) {
911 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
913 mutex_lock(&devlink->lock);
914 list_for_each_entry(devlink_port, &devlink->port_list, list) {
919 err = devlink_nl_port_fill(msg, devlink, devlink_port,
921 NETLINK_CB(cb->skb).portid,
926 mutex_unlock(&devlink->lock);
931 mutex_unlock(&devlink->lock);
934 mutex_unlock(&devlink_mutex);
940 static int devlink_port_type_set(struct devlink *devlink,
941 struct devlink_port *devlink_port,
942 enum devlink_port_type port_type)
947 if (devlink->ops->port_type_set) {
948 if (port_type == devlink_port->type)
950 err = devlink->ops->port_type_set(devlink_port, port_type);
953 devlink_port->desired_type = port_type;
954 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
961 devlink_port_function_hw_addr_set(struct devlink *devlink, struct devlink_port *port,
962 const struct nlattr *attr, struct netlink_ext_ack *extack)
964 const struct devlink_ops *ops;
969 hw_addr = nla_data(attr);
970 hw_addr_len = nla_len(attr);
971 if (hw_addr_len > MAX_ADDR_LEN) {
972 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
975 if (port->type == DEVLINK_PORT_TYPE_ETH) {
976 if (hw_addr_len != ETH_ALEN) {
977 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
980 if (!is_unicast_ether_addr(hw_addr)) {
981 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
987 if (!ops->port_function_hw_addr_set) {
988 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
992 err = ops->port_function_hw_addr_set(devlink, port, hw_addr, hw_addr_len, extack);
996 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
1001 devlink_port_function_set(struct devlink *devlink, struct devlink_port *port,
1002 const struct nlattr *attr, struct netlink_ext_ack *extack)
1004 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
1007 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
1008 devlink_function_nl_policy, extack);
1010 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
1014 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
1016 err = devlink_port_function_hw_addr_set(devlink, port, attr, extack);
1021 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
1022 struct genl_info *info)
1024 struct devlink_port *devlink_port = info->user_ptr[1];
1025 struct devlink *devlink = devlink_port->devlink;
1028 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
1029 enum devlink_port_type port_type;
1031 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
1032 err = devlink_port_type_set(devlink, devlink_port, port_type);
1037 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
1038 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
1039 struct netlink_ext_ack *extack = info->extack;
1041 err = devlink_port_function_set(devlink, devlink_port, attr, extack);
1049 static int devlink_port_split(struct devlink *devlink, u32 port_index,
1050 u32 count, struct netlink_ext_ack *extack)
1053 if (devlink->ops->port_split)
1054 return devlink->ops->port_split(devlink, port_index, count,
1059 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
1060 struct genl_info *info)
1062 struct devlink *devlink = info->user_ptr[0];
1063 struct devlink_port *devlink_port;
1067 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
1068 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
1071 devlink_port = devlink_port_get_from_info(devlink, info);
1072 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1073 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
1075 if (IS_ERR(devlink_port))
1078 if (!devlink_port->attrs.splittable) {
1079 /* Split ports cannot be split. */
1080 if (devlink_port->attrs.split)
1081 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
1083 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
1087 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
1088 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
1092 return devlink_port_split(devlink, port_index, count, info->extack);
1095 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
1096 struct netlink_ext_ack *extack)
1099 if (devlink->ops->port_unsplit)
1100 return devlink->ops->port_unsplit(devlink, port_index, extack);
1104 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
1105 struct genl_info *info)
1107 struct devlink *devlink = info->user_ptr[0];
1110 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
1113 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
1114 return devlink_port_unsplit(devlink, port_index, info->extack);
1117 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
1118 struct devlink_sb *devlink_sb,
1119 enum devlink_command cmd, u32 portid,
1124 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1128 if (devlink_nl_put_handle(msg, devlink))
1129 goto nla_put_failure;
1130 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1131 goto nla_put_failure;
1132 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
1133 goto nla_put_failure;
1134 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
1135 devlink_sb->ingress_pools_count))
1136 goto nla_put_failure;
1137 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
1138 devlink_sb->egress_pools_count))
1139 goto nla_put_failure;
1140 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
1141 devlink_sb->ingress_tc_count))
1142 goto nla_put_failure;
1143 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
1144 devlink_sb->egress_tc_count))
1145 goto nla_put_failure;
1147 genlmsg_end(msg, hdr);
1151 genlmsg_cancel(msg, hdr);
1155 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
1156 struct genl_info *info)
1158 struct devlink *devlink = info->user_ptr[0];
1159 struct devlink_sb *devlink_sb;
1160 struct sk_buff *msg;
1163 devlink_sb = devlink_sb_get_from_info(devlink, info);
1164 if (IS_ERR(devlink_sb))
1165 return PTR_ERR(devlink_sb);
1167 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1171 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1173 info->snd_portid, info->snd_seq, 0);
1179 return genlmsg_reply(msg, info);
1182 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
1183 struct netlink_callback *cb)
1185 struct devlink *devlink;
1186 struct devlink_sb *devlink_sb;
1187 int start = cb->args[0];
1191 mutex_lock(&devlink_mutex);
1192 list_for_each_entry(devlink, &devlink_list, list) {
1193 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1195 mutex_lock(&devlink->lock);
1196 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1201 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1203 NETLINK_CB(cb->skb).portid,
1207 mutex_unlock(&devlink->lock);
1212 mutex_unlock(&devlink->lock);
1215 mutex_unlock(&devlink_mutex);
1221 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
1222 struct devlink_sb *devlink_sb,
1223 u16 pool_index, enum devlink_command cmd,
1224 u32 portid, u32 seq, int flags)
1226 struct devlink_sb_pool_info pool_info;
1230 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
1231 pool_index, &pool_info);
1235 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1239 if (devlink_nl_put_handle(msg, devlink))
1240 goto nla_put_failure;
1241 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1242 goto nla_put_failure;
1243 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1244 goto nla_put_failure;
1245 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
1246 goto nla_put_failure;
1247 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
1248 goto nla_put_failure;
1249 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
1250 pool_info.threshold_type))
1251 goto nla_put_failure;
1252 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
1253 pool_info.cell_size))
1254 goto nla_put_failure;
1256 genlmsg_end(msg, hdr);
1260 genlmsg_cancel(msg, hdr);
1264 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
1265 struct genl_info *info)
1267 struct devlink *devlink = info->user_ptr[0];
1268 struct devlink_sb *devlink_sb;
1269 struct sk_buff *msg;
1273 devlink_sb = devlink_sb_get_from_info(devlink, info);
1274 if (IS_ERR(devlink_sb))
1275 return PTR_ERR(devlink_sb);
1277 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1282 if (!devlink->ops->sb_pool_get)
1285 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1289 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
1290 DEVLINK_CMD_SB_POOL_NEW,
1291 info->snd_portid, info->snd_seq, 0);
1297 return genlmsg_reply(msg, info);
1300 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1301 struct devlink *devlink,
1302 struct devlink_sb *devlink_sb,
1303 u32 portid, u32 seq)
1305 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1309 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1310 if (*p_idx < start) {
1314 err = devlink_nl_sb_pool_fill(msg, devlink,
1317 DEVLINK_CMD_SB_POOL_NEW,
1318 portid, seq, NLM_F_MULTI);
1326 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1327 struct netlink_callback *cb)
1329 struct devlink *devlink;
1330 struct devlink_sb *devlink_sb;
1331 int start = cb->args[0];
1335 mutex_lock(&devlink_mutex);
1336 list_for_each_entry(devlink, &devlink_list, list) {
1337 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1338 !devlink->ops->sb_pool_get)
1340 mutex_lock(&devlink->lock);
1341 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1342 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1344 NETLINK_CB(cb->skb).portid,
1345 cb->nlh->nlmsg_seq);
1346 if (err == -EOPNOTSUPP) {
1349 mutex_unlock(&devlink->lock);
1353 mutex_unlock(&devlink->lock);
1356 mutex_unlock(&devlink_mutex);
1358 if (err != -EMSGSIZE)
1365 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1366 u16 pool_index, u32 size,
1367 enum devlink_sb_threshold_type threshold_type,
1368 struct netlink_ext_ack *extack)
1371 const struct devlink_ops *ops = devlink->ops;
1373 if (ops->sb_pool_set)
1374 return ops->sb_pool_set(devlink, sb_index, pool_index,
1375 size, threshold_type, extack);
1379 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1380 struct genl_info *info)
1382 struct devlink *devlink = info->user_ptr[0];
1383 enum devlink_sb_threshold_type threshold_type;
1384 struct devlink_sb *devlink_sb;
1389 devlink_sb = devlink_sb_get_from_info(devlink, info);
1390 if (IS_ERR(devlink_sb))
1391 return PTR_ERR(devlink_sb);
1393 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1398 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1402 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1405 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1406 return devlink_sb_pool_set(devlink, devlink_sb->index,
1407 pool_index, size, threshold_type,
1411 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1412 struct devlink *devlink,
1413 struct devlink_port *devlink_port,
1414 struct devlink_sb *devlink_sb,
1416 enum devlink_command cmd,
1417 u32 portid, u32 seq, int flags)
1419 const struct devlink_ops *ops = devlink->ops;
1424 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1425 pool_index, &threshold);
1429 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1433 if (devlink_nl_put_handle(msg, devlink))
1434 goto nla_put_failure;
1435 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1436 goto nla_put_failure;
1437 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1438 goto nla_put_failure;
1439 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1440 goto nla_put_failure;
1441 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1442 goto nla_put_failure;
1444 if (ops->sb_occ_port_pool_get) {
1448 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1449 pool_index, &cur, &max);
1450 if (err && err != -EOPNOTSUPP)
1453 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1454 goto nla_put_failure;
1455 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1456 goto nla_put_failure;
1460 genlmsg_end(msg, hdr);
1464 genlmsg_cancel(msg, hdr);
1468 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1469 struct genl_info *info)
1471 struct devlink_port *devlink_port = info->user_ptr[1];
1472 struct devlink *devlink = devlink_port->devlink;
1473 struct devlink_sb *devlink_sb;
1474 struct sk_buff *msg;
1478 devlink_sb = devlink_sb_get_from_info(devlink, info);
1479 if (IS_ERR(devlink_sb))
1480 return PTR_ERR(devlink_sb);
1482 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1487 if (!devlink->ops->sb_port_pool_get)
1490 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1494 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1495 devlink_sb, pool_index,
1496 DEVLINK_CMD_SB_PORT_POOL_NEW,
1497 info->snd_portid, info->snd_seq, 0);
1503 return genlmsg_reply(msg, info);
1506 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1507 struct devlink *devlink,
1508 struct devlink_sb *devlink_sb,
1509 u32 portid, u32 seq)
1511 struct devlink_port *devlink_port;
1512 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1516 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1517 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1518 if (*p_idx < start) {
1522 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1526 DEVLINK_CMD_SB_PORT_POOL_NEW,
1537 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1538 struct netlink_callback *cb)
1540 struct devlink *devlink;
1541 struct devlink_sb *devlink_sb;
1542 int start = cb->args[0];
1546 mutex_lock(&devlink_mutex);
1547 list_for_each_entry(devlink, &devlink_list, list) {
1548 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1549 !devlink->ops->sb_port_pool_get)
1551 mutex_lock(&devlink->lock);
1552 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1553 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1554 devlink, devlink_sb,
1555 NETLINK_CB(cb->skb).portid,
1556 cb->nlh->nlmsg_seq);
1557 if (err == -EOPNOTSUPP) {
1560 mutex_unlock(&devlink->lock);
1564 mutex_unlock(&devlink->lock);
1567 mutex_unlock(&devlink_mutex);
1569 if (err != -EMSGSIZE)
1576 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1577 unsigned int sb_index, u16 pool_index,
1579 struct netlink_ext_ack *extack)
1582 const struct devlink_ops *ops = devlink_port->devlink->ops;
1584 if (ops->sb_port_pool_set)
1585 return ops->sb_port_pool_set(devlink_port, sb_index,
1586 pool_index, threshold, extack);
1590 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1591 struct genl_info *info)
1593 struct devlink_port *devlink_port = info->user_ptr[1];
1594 struct devlink *devlink = info->user_ptr[0];
1595 struct devlink_sb *devlink_sb;
1600 devlink_sb = devlink_sb_get_from_info(devlink, info);
1601 if (IS_ERR(devlink_sb))
1602 return PTR_ERR(devlink_sb);
1604 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1609 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1612 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1613 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1614 pool_index, threshold, info->extack);
1618 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1619 struct devlink_port *devlink_port,
1620 struct devlink_sb *devlink_sb, u16 tc_index,
1621 enum devlink_sb_pool_type pool_type,
1622 enum devlink_command cmd,
1623 u32 portid, u32 seq, int flags)
1625 const struct devlink_ops *ops = devlink->ops;
1631 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1632 tc_index, pool_type,
1633 &pool_index, &threshold);
1637 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1641 if (devlink_nl_put_handle(msg, devlink))
1642 goto nla_put_failure;
1643 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1644 goto nla_put_failure;
1645 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1646 goto nla_put_failure;
1647 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1648 goto nla_put_failure;
1649 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1650 goto nla_put_failure;
1651 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1652 goto nla_put_failure;
1653 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1654 goto nla_put_failure;
1656 if (ops->sb_occ_tc_port_bind_get) {
1660 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1662 tc_index, pool_type,
1664 if (err && err != -EOPNOTSUPP)
1667 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1668 goto nla_put_failure;
1669 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1670 goto nla_put_failure;
1674 genlmsg_end(msg, hdr);
1678 genlmsg_cancel(msg, hdr);
1682 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1683 struct genl_info *info)
1685 struct devlink_port *devlink_port = info->user_ptr[1];
1686 struct devlink *devlink = devlink_port->devlink;
1687 struct devlink_sb *devlink_sb;
1688 struct sk_buff *msg;
1689 enum devlink_sb_pool_type pool_type;
1693 devlink_sb = devlink_sb_get_from_info(devlink, info);
1694 if (IS_ERR(devlink_sb))
1695 return PTR_ERR(devlink_sb);
1697 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1701 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1702 pool_type, &tc_index);
1706 if (!devlink->ops->sb_tc_pool_bind_get)
1709 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1713 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1714 devlink_sb, tc_index, pool_type,
1715 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1723 return genlmsg_reply(msg, info);
1726 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1727 int start, int *p_idx,
1728 struct devlink *devlink,
1729 struct devlink_sb *devlink_sb,
1730 u32 portid, u32 seq)
1732 struct devlink_port *devlink_port;
1736 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1738 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1739 if (*p_idx < start) {
1743 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1747 DEVLINK_SB_POOL_TYPE_INGRESS,
1748 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1756 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1757 if (*p_idx < start) {
1761 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1765 DEVLINK_SB_POOL_TYPE_EGRESS,
1766 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1778 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1779 struct netlink_callback *cb)
1781 struct devlink *devlink;
1782 struct devlink_sb *devlink_sb;
1783 int start = cb->args[0];
1787 mutex_lock(&devlink_mutex);
1788 list_for_each_entry(devlink, &devlink_list, list) {
1789 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1790 !devlink->ops->sb_tc_pool_bind_get)
1793 mutex_lock(&devlink->lock);
1794 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1795 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1798 NETLINK_CB(cb->skb).portid,
1799 cb->nlh->nlmsg_seq);
1800 if (err == -EOPNOTSUPP) {
1803 mutex_unlock(&devlink->lock);
1807 mutex_unlock(&devlink->lock);
1810 mutex_unlock(&devlink_mutex);
1812 if (err != -EMSGSIZE)
1819 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1820 unsigned int sb_index, u16 tc_index,
1821 enum devlink_sb_pool_type pool_type,
1822 u16 pool_index, u32 threshold,
1823 struct netlink_ext_ack *extack)
1826 const struct devlink_ops *ops = devlink_port->devlink->ops;
1828 if (ops->sb_tc_pool_bind_set)
1829 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1830 tc_index, pool_type,
1831 pool_index, threshold, extack);
1835 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1836 struct genl_info *info)
1838 struct devlink_port *devlink_port = info->user_ptr[1];
1839 struct devlink *devlink = info->user_ptr[0];
1840 enum devlink_sb_pool_type pool_type;
1841 struct devlink_sb *devlink_sb;
1847 devlink_sb = devlink_sb_get_from_info(devlink, info);
1848 if (IS_ERR(devlink_sb))
1849 return PTR_ERR(devlink_sb);
1851 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1855 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1856 pool_type, &tc_index);
1860 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1865 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1868 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1869 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1870 tc_index, pool_type,
1871 pool_index, threshold, info->extack);
1874 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1875 struct genl_info *info)
1877 struct devlink *devlink = info->user_ptr[0];
1878 const struct devlink_ops *ops = devlink->ops;
1879 struct devlink_sb *devlink_sb;
1881 devlink_sb = devlink_sb_get_from_info(devlink, info);
1882 if (IS_ERR(devlink_sb))
1883 return PTR_ERR(devlink_sb);
1885 if (ops->sb_occ_snapshot)
1886 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1890 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1891 struct genl_info *info)
1893 struct devlink *devlink = info->user_ptr[0];
1894 const struct devlink_ops *ops = devlink->ops;
1895 struct devlink_sb *devlink_sb;
1897 devlink_sb = devlink_sb_get_from_info(devlink, info);
1898 if (IS_ERR(devlink_sb))
1899 return PTR_ERR(devlink_sb);
1901 if (ops->sb_occ_max_clear)
1902 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1906 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1907 enum devlink_command cmd, u32 portid,
1910 const struct devlink_ops *ops = devlink->ops;
1911 enum devlink_eswitch_encap_mode encap_mode;
1917 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1921 err = devlink_nl_put_handle(msg, devlink);
1923 goto nla_put_failure;
1925 if (ops->eswitch_mode_get) {
1926 err = ops->eswitch_mode_get(devlink, &mode);
1928 goto nla_put_failure;
1929 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1931 goto nla_put_failure;
1934 if (ops->eswitch_inline_mode_get) {
1935 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1937 goto nla_put_failure;
1938 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1941 goto nla_put_failure;
1944 if (ops->eswitch_encap_mode_get) {
1945 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1947 goto nla_put_failure;
1948 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1950 goto nla_put_failure;
1953 genlmsg_end(msg, hdr);
1957 genlmsg_cancel(msg, hdr);
1961 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1962 struct genl_info *info)
1964 struct devlink *devlink = info->user_ptr[0];
1965 struct sk_buff *msg;
1968 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1972 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1973 info->snd_portid, info->snd_seq, 0);
1980 return genlmsg_reply(msg, info);
1983 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1984 struct genl_info *info)
1986 struct devlink *devlink = info->user_ptr[0];
1987 const struct devlink_ops *ops = devlink->ops;
1988 enum devlink_eswitch_encap_mode encap_mode;
1993 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1994 if (!ops->eswitch_mode_set)
1996 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1997 err = ops->eswitch_mode_set(devlink, mode, info->extack);
2002 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2003 if (!ops->eswitch_inline_mode_set)
2005 inline_mode = nla_get_u8(
2006 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
2007 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
2013 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2014 if (!ops->eswitch_encap_mode_set)
2016 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
2017 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
2026 int devlink_dpipe_match_put(struct sk_buff *skb,
2027 struct devlink_dpipe_match *match)
2029 struct devlink_dpipe_header *header = match->header;
2030 struct devlink_dpipe_field *field = &header->fields[match->field_id];
2031 struct nlattr *match_attr;
2033 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
2037 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
2038 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
2039 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2040 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2041 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2042 goto nla_put_failure;
2044 nla_nest_end(skb, match_attr);
2048 nla_nest_cancel(skb, match_attr);
2051 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
2053 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
2054 struct sk_buff *skb)
2056 struct nlattr *matches_attr;
2058 matches_attr = nla_nest_start_noflag(skb,
2059 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
2063 if (table->table_ops->matches_dump(table->priv, skb))
2064 goto nla_put_failure;
2066 nla_nest_end(skb, matches_attr);
2070 nla_nest_cancel(skb, matches_attr);
2074 int devlink_dpipe_action_put(struct sk_buff *skb,
2075 struct devlink_dpipe_action *action)
2077 struct devlink_dpipe_header *header = action->header;
2078 struct devlink_dpipe_field *field = &header->fields[action->field_id];
2079 struct nlattr *action_attr;
2081 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
2085 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
2086 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
2087 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2088 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2089 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2090 goto nla_put_failure;
2092 nla_nest_end(skb, action_attr);
2096 nla_nest_cancel(skb, action_attr);
2099 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
2101 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
2102 struct sk_buff *skb)
2104 struct nlattr *actions_attr;
2106 actions_attr = nla_nest_start_noflag(skb,
2107 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
2111 if (table->table_ops->actions_dump(table->priv, skb))
2112 goto nla_put_failure;
2114 nla_nest_end(skb, actions_attr);
2118 nla_nest_cancel(skb, actions_attr);
2122 static int devlink_dpipe_table_put(struct sk_buff *skb,
2123 struct devlink_dpipe_table *table)
2125 struct nlattr *table_attr;
2128 table_size = table->table_ops->size_get(table->priv);
2129 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
2133 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
2134 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
2136 goto nla_put_failure;
2137 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
2138 table->counters_enabled))
2139 goto nla_put_failure;
2141 if (table->resource_valid) {
2142 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
2143 table->resource_id, DEVLINK_ATTR_PAD) ||
2144 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
2145 table->resource_units, DEVLINK_ATTR_PAD))
2146 goto nla_put_failure;
2148 if (devlink_dpipe_matches_put(table, skb))
2149 goto nla_put_failure;
2151 if (devlink_dpipe_actions_put(table, skb))
2152 goto nla_put_failure;
2154 nla_nest_end(skb, table_attr);
2158 nla_nest_cancel(skb, table_attr);
2162 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
2163 struct genl_info *info)
2168 err = genlmsg_reply(*pskb, info);
2172 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2178 static int devlink_dpipe_tables_fill(struct genl_info *info,
2179 enum devlink_command cmd, int flags,
2180 struct list_head *dpipe_tables,
2181 const char *table_name)
2183 struct devlink *devlink = info->user_ptr[0];
2184 struct devlink_dpipe_table *table;
2185 struct nlattr *tables_attr;
2186 struct sk_buff *skb = NULL;
2187 struct nlmsghdr *nlh;
2193 table = list_first_entry(dpipe_tables,
2194 struct devlink_dpipe_table, list);
2196 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2200 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2201 &devlink_nl_family, NLM_F_MULTI, cmd);
2207 if (devlink_nl_put_handle(skb, devlink))
2208 goto nla_put_failure;
2209 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
2211 goto nla_put_failure;
2215 list_for_each_entry_from(table, dpipe_tables, list) {
2217 err = devlink_dpipe_table_put(skb, table);
2225 if (!strcmp(table->name, table_name)) {
2226 err = devlink_dpipe_table_put(skb, table);
2234 nla_nest_end(skb, tables_attr);
2235 genlmsg_end(skb, hdr);
2240 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2241 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2243 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2249 return genlmsg_reply(skb, info);
2258 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
2259 struct genl_info *info)
2261 struct devlink *devlink = info->user_ptr[0];
2262 const char *table_name = NULL;
2264 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2265 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2267 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
2268 &devlink->dpipe_table_list,
2272 static int devlink_dpipe_value_put(struct sk_buff *skb,
2273 struct devlink_dpipe_value *value)
2275 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
2276 value->value_size, value->value))
2279 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
2280 value->value_size, value->mask))
2282 if (value->mapping_valid)
2283 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
2284 value->mapping_value))
2289 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
2290 struct devlink_dpipe_value *value)
2294 if (devlink_dpipe_action_put(skb, value->action))
2296 if (devlink_dpipe_value_put(skb, value))
2301 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
2302 struct devlink_dpipe_value *values,
2303 unsigned int values_count)
2305 struct nlattr *action_attr;
2309 for (i = 0; i < values_count; i++) {
2310 action_attr = nla_nest_start_noflag(skb,
2311 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
2314 err = devlink_dpipe_action_value_put(skb, &values[i]);
2316 goto err_action_value_put;
2317 nla_nest_end(skb, action_attr);
2321 err_action_value_put:
2322 nla_nest_cancel(skb, action_attr);
2326 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
2327 struct devlink_dpipe_value *value)
2331 if (devlink_dpipe_match_put(skb, value->match))
2333 if (devlink_dpipe_value_put(skb, value))
2338 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
2339 struct devlink_dpipe_value *values,
2340 unsigned int values_count)
2342 struct nlattr *match_attr;
2346 for (i = 0; i < values_count; i++) {
2347 match_attr = nla_nest_start_noflag(skb,
2348 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
2351 err = devlink_dpipe_match_value_put(skb, &values[i]);
2353 goto err_match_value_put;
2354 nla_nest_end(skb, match_attr);
2358 err_match_value_put:
2359 nla_nest_cancel(skb, match_attr);
2363 static int devlink_dpipe_entry_put(struct sk_buff *skb,
2364 struct devlink_dpipe_entry *entry)
2366 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2369 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2373 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2375 goto nla_put_failure;
2376 if (entry->counter_valid)
2377 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2378 entry->counter, DEVLINK_ATTR_PAD))
2379 goto nla_put_failure;
2381 matches_attr = nla_nest_start_noflag(skb,
2382 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2384 goto nla_put_failure;
2386 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2387 entry->match_values_count);
2389 nla_nest_cancel(skb, matches_attr);
2390 goto err_match_values_put;
2392 nla_nest_end(skb, matches_attr);
2394 actions_attr = nla_nest_start_noflag(skb,
2395 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2397 goto nla_put_failure;
2399 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2400 entry->action_values_count);
2402 nla_nest_cancel(skb, actions_attr);
2403 goto err_action_values_put;
2405 nla_nest_end(skb, actions_attr);
2407 nla_nest_end(skb, entry_attr);
2412 err_match_values_put:
2413 err_action_values_put:
2414 nla_nest_cancel(skb, entry_attr);
2418 static struct devlink_dpipe_table *
2419 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2420 const char *table_name, struct devlink *devlink)
2422 struct devlink_dpipe_table *table;
2423 list_for_each_entry_rcu(table, dpipe_tables, list,
2424 lockdep_is_held(&devlink->lock)) {
2425 if (!strcmp(table->name, table_name))
2431 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2433 struct devlink *devlink;
2436 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2441 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2442 dump_ctx->info->snd_portid,
2443 dump_ctx->info->snd_seq,
2444 &devlink_nl_family, NLM_F_MULTI,
2447 goto nla_put_failure;
2449 devlink = dump_ctx->info->user_ptr[0];
2450 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2451 goto nla_put_failure;
2452 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2453 DEVLINK_ATTR_DPIPE_ENTRIES);
2454 if (!dump_ctx->nest)
2455 goto nla_put_failure;
2459 nlmsg_free(dump_ctx->skb);
2462 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2464 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2465 struct devlink_dpipe_entry *entry)
2467 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2469 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2471 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2473 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2474 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2477 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2479 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2482 unsigned int value_count, value_index;
2483 struct devlink_dpipe_value *value;
2485 value = entry->action_values;
2486 value_count = entry->action_values_count;
2487 for (value_index = 0; value_index < value_count; value_index++) {
2488 kfree(value[value_index].value);
2489 kfree(value[value_index].mask);
2492 value = entry->match_values;
2493 value_count = entry->match_values_count;
2494 for (value_index = 0; value_index < value_count; value_index++) {
2495 kfree(value[value_index].value);
2496 kfree(value[value_index].mask);
2499 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2501 static int devlink_dpipe_entries_fill(struct genl_info *info,
2502 enum devlink_command cmd, int flags,
2503 struct devlink_dpipe_table *table)
2505 struct devlink_dpipe_dump_ctx dump_ctx;
2506 struct nlmsghdr *nlh;
2509 dump_ctx.skb = NULL;
2511 dump_ctx.info = info;
2513 err = table->table_ops->entries_dump(table->priv,
2514 table->counters_enabled,
2520 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2521 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2523 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2528 return genlmsg_reply(dump_ctx.skb, info);
2531 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2532 struct genl_info *info)
2534 struct devlink *devlink = info->user_ptr[0];
2535 struct devlink_dpipe_table *table;
2536 const char *table_name;
2538 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2541 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2542 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2543 table_name, devlink);
2547 if (!table->table_ops->entries_dump)
2550 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2554 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2555 const struct devlink_dpipe_header *header)
2557 struct devlink_dpipe_field *field;
2558 struct nlattr *field_attr;
2561 for (i = 0; i < header->fields_count; i++) {
2562 field = &header->fields[i];
2563 field_attr = nla_nest_start_noflag(skb,
2564 DEVLINK_ATTR_DPIPE_FIELD);
2567 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2568 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2569 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2570 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2571 goto nla_put_failure;
2572 nla_nest_end(skb, field_attr);
2577 nla_nest_cancel(skb, field_attr);
2581 static int devlink_dpipe_header_put(struct sk_buff *skb,
2582 struct devlink_dpipe_header *header)
2584 struct nlattr *fields_attr, *header_attr;
2587 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
2591 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2592 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2593 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2594 goto nla_put_failure;
2596 fields_attr = nla_nest_start_noflag(skb,
2597 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2599 goto nla_put_failure;
2601 err = devlink_dpipe_fields_put(skb, header);
2603 nla_nest_cancel(skb, fields_attr);
2604 goto nla_put_failure;
2606 nla_nest_end(skb, fields_attr);
2607 nla_nest_end(skb, header_attr);
2612 nla_nest_cancel(skb, header_attr);
2616 static int devlink_dpipe_headers_fill(struct genl_info *info,
2617 enum devlink_command cmd, int flags,
2618 struct devlink_dpipe_headers *
2621 struct devlink *devlink = info->user_ptr[0];
2622 struct nlattr *headers_attr;
2623 struct sk_buff *skb = NULL;
2624 struct nlmsghdr *nlh;
2631 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2635 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2636 &devlink_nl_family, NLM_F_MULTI, cmd);
2642 if (devlink_nl_put_handle(skb, devlink))
2643 goto nla_put_failure;
2644 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2646 goto nla_put_failure;
2649 for (; i < dpipe_headers->headers_count; i++) {
2650 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2658 nla_nest_end(skb, headers_attr);
2659 genlmsg_end(skb, hdr);
2660 if (i != dpipe_headers->headers_count)
2664 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2665 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2667 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2672 return genlmsg_reply(skb, info);
2681 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2682 struct genl_info *info)
2684 struct devlink *devlink = info->user_ptr[0];
2686 if (!devlink->dpipe_headers)
2688 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2689 0, devlink->dpipe_headers);
2692 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2693 const char *table_name,
2696 struct devlink_dpipe_table *table;
2698 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2699 table_name, devlink);
2703 if (table->counter_control_extern)
2706 if (!(table->counters_enabled ^ enable))
2709 table->counters_enabled = enable;
2710 if (table->table_ops->counters_set_update)
2711 table->table_ops->counters_set_update(table->priv, enable);
2715 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2716 struct genl_info *info)
2718 struct devlink *devlink = info->user_ptr[0];
2719 const char *table_name;
2720 bool counters_enable;
2722 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2723 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2726 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2727 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2729 return devlink_dpipe_table_counters_set(devlink, table_name,
2733 static struct devlink_resource *
2734 devlink_resource_find(struct devlink *devlink,
2735 struct devlink_resource *resource, u64 resource_id)
2737 struct list_head *resource_list;
2740 resource_list = &resource->resource_list;
2742 resource_list = &devlink->resource_list;
2744 list_for_each_entry(resource, resource_list, list) {
2745 struct devlink_resource *child_resource;
2747 if (resource->id == resource_id)
2750 child_resource = devlink_resource_find(devlink, resource,
2753 return child_resource;
2759 devlink_resource_validate_children(struct devlink_resource *resource)
2761 struct devlink_resource *child_resource;
2762 bool size_valid = true;
2765 if (list_empty(&resource->resource_list))
2768 list_for_each_entry(child_resource, &resource->resource_list, list)
2769 parts_size += child_resource->size_new;
2771 if (parts_size > resource->size_new)
2774 resource->size_valid = size_valid;
2778 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2779 struct netlink_ext_ack *extack)
2784 if (size > resource->size_params.size_max) {
2785 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2789 if (size < resource->size_params.size_min) {
2790 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2794 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2796 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2803 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2804 struct genl_info *info)
2806 struct devlink *devlink = info->user_ptr[0];
2807 struct devlink_resource *resource;
2812 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2813 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2815 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2817 resource = devlink_resource_find(devlink, NULL, resource_id);
2821 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2822 err = devlink_resource_validate_size(resource, size, info->extack);
2826 resource->size_new = size;
2827 devlink_resource_validate_children(resource);
2828 if (resource->parent)
2829 devlink_resource_validate_children(resource->parent);
2834 devlink_resource_size_params_put(struct devlink_resource *resource,
2835 struct sk_buff *skb)
2837 struct devlink_resource_size_params *size_params;
2839 size_params = &resource->size_params;
2840 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2841 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2842 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2843 size_params->size_max, DEVLINK_ATTR_PAD) ||
2844 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2845 size_params->size_min, DEVLINK_ATTR_PAD) ||
2846 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2851 static int devlink_resource_occ_put(struct devlink_resource *resource,
2852 struct sk_buff *skb)
2854 if (!resource->occ_get)
2856 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2857 resource->occ_get(resource->occ_get_priv),
2861 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2862 struct devlink_resource *resource)
2864 struct devlink_resource *child_resource;
2865 struct nlattr *child_resource_attr;
2866 struct nlattr *resource_attr;
2868 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
2872 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2873 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2874 DEVLINK_ATTR_PAD) ||
2875 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2877 goto nla_put_failure;
2878 if (resource->size != resource->size_new)
2879 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2880 resource->size_new, DEVLINK_ATTR_PAD);
2881 if (devlink_resource_occ_put(resource, skb))
2882 goto nla_put_failure;
2883 if (devlink_resource_size_params_put(resource, skb))
2884 goto nla_put_failure;
2885 if (list_empty(&resource->resource_list))
2888 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2889 resource->size_valid))
2890 goto nla_put_failure;
2892 child_resource_attr = nla_nest_start_noflag(skb,
2893 DEVLINK_ATTR_RESOURCE_LIST);
2894 if (!child_resource_attr)
2895 goto nla_put_failure;
2897 list_for_each_entry(child_resource, &resource->resource_list, list) {
2898 if (devlink_resource_put(devlink, skb, child_resource))
2899 goto resource_put_failure;
2902 nla_nest_end(skb, child_resource_attr);
2904 nla_nest_end(skb, resource_attr);
2907 resource_put_failure:
2908 nla_nest_cancel(skb, child_resource_attr);
2910 nla_nest_cancel(skb, resource_attr);
2914 static int devlink_resource_fill(struct genl_info *info,
2915 enum devlink_command cmd, int flags)
2917 struct devlink *devlink = info->user_ptr[0];
2918 struct devlink_resource *resource;
2919 struct nlattr *resources_attr;
2920 struct sk_buff *skb = NULL;
2921 struct nlmsghdr *nlh;
2927 resource = list_first_entry(&devlink->resource_list,
2928 struct devlink_resource, list);
2930 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2934 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2935 &devlink_nl_family, NLM_F_MULTI, cmd);
2941 if (devlink_nl_put_handle(skb, devlink))
2942 goto nla_put_failure;
2944 resources_attr = nla_nest_start_noflag(skb,
2945 DEVLINK_ATTR_RESOURCE_LIST);
2946 if (!resources_attr)
2947 goto nla_put_failure;
2951 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2952 err = devlink_resource_put(devlink, skb, resource);
2955 goto err_resource_put;
2961 nla_nest_end(skb, resources_attr);
2962 genlmsg_end(skb, hdr);
2966 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2967 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2969 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2974 return genlmsg_reply(skb, info);
2983 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2984 struct genl_info *info)
2986 struct devlink *devlink = info->user_ptr[0];
2988 if (list_empty(&devlink->resource_list))
2991 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2995 devlink_resources_validate(struct devlink *devlink,
2996 struct devlink_resource *resource,
2997 struct genl_info *info)
2999 struct list_head *resource_list;
3003 resource_list = &resource->resource_list;
3005 resource_list = &devlink->resource_list;
3007 list_for_each_entry(resource, resource_list, list) {
3008 if (!resource->size_valid)
3010 err = devlink_resources_validate(devlink, resource, info);
3017 static struct net *devlink_netns_get(struct sk_buff *skb,
3018 struct genl_info *info)
3020 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
3021 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
3022 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
3025 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
3026 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
3027 return ERR_PTR(-EINVAL);
3030 if (netns_pid_attr) {
3031 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
3032 } else if (netns_fd_attr) {
3033 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
3034 } else if (netns_id_attr) {
3035 net = get_net_ns_by_id(sock_net(skb->sk),
3036 nla_get_u32(netns_id_attr));
3038 net = ERR_PTR(-EINVAL);
3041 net = ERR_PTR(-EINVAL);
3044 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
3045 return ERR_PTR(-EINVAL);
3047 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
3049 return ERR_PTR(-EPERM);
3054 static void devlink_param_notify(struct devlink *devlink,
3055 unsigned int port_index,
3056 struct devlink_param_item *param_item,
3057 enum devlink_command cmd);
3059 static void devlink_reload_netns_change(struct devlink *devlink,
3060 struct net *dest_net)
3062 struct devlink_param_item *param_item;
3064 /* Userspace needs to be notified about devlink objects
3065 * removed from original and entering new network namespace.
3066 * The rest of the devlink objects are re-created during
3067 * reload process so the notifications are generated separatelly.
3070 list_for_each_entry(param_item, &devlink->param_list, list)
3071 devlink_param_notify(devlink, 0, param_item,
3072 DEVLINK_CMD_PARAM_DEL);
3073 devlink_notify(devlink, DEVLINK_CMD_DEL);
3075 __devlink_net_set(devlink, dest_net);
3077 devlink_notify(devlink, DEVLINK_CMD_NEW);
3078 list_for_each_entry(param_item, &devlink->param_list, list)
3079 devlink_param_notify(devlink, 0, param_item,
3080 DEVLINK_CMD_PARAM_NEW);
3083 static bool devlink_reload_supported(const struct devlink_ops *ops)
3085 return ops->reload_down && ops->reload_up;
3088 static void devlink_reload_failed_set(struct devlink *devlink,
3091 if (devlink->reload_failed == reload_failed)
3093 devlink->reload_failed = reload_failed;
3094 devlink_notify(devlink, DEVLINK_CMD_NEW);
3097 bool devlink_is_reload_failed(const struct devlink *devlink)
3099 return devlink->reload_failed;
3101 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
3104 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
3105 enum devlink_reload_limit limit, u32 actions_performed)
3107 unsigned long actions = actions_performed;
3111 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
3112 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
3113 reload_stats[stat_idx]++;
3115 devlink_notify(devlink, DEVLINK_CMD_NEW);
3119 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
3120 u32 actions_performed)
3122 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
3127 * devlink_remote_reload_actions_performed - Update devlink on reload actions
3128 * performed which are not a direct result of devlink reload call.
3130 * This should be called by a driver after performing reload actions in case it was not
3131 * a result of devlink reload call. For example fw_activate was performed as a result
3132 * of devlink reload triggered fw_activate on another host.
3133 * The motivation for this function is to keep data on reload actions performed on this
3134 * function whether it was done due to direct devlink reload call or not.
3137 * @limit: reload limit
3138 * @actions_performed: bitmask of actions performed
3140 void devlink_remote_reload_actions_performed(struct devlink *devlink,
3141 enum devlink_reload_limit limit,
3142 u32 actions_performed)
3144 if (WARN_ON(!actions_performed ||
3145 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
3146 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
3147 limit > DEVLINK_RELOAD_LIMIT_MAX))
3150 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
3153 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
3155 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
3156 enum devlink_reload_action action, enum devlink_reload_limit limit,
3157 u32 *actions_performed, struct netlink_ext_ack *extack)
3159 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
3162 if (!devlink->reload_enabled)
3165 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
3166 sizeof(remote_reload_stats));
3167 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
3171 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
3172 devlink_reload_netns_change(devlink, dest_net);
3174 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
3175 devlink_reload_failed_set(devlink, !!err);
3179 WARN_ON(!(*actions_performed & BIT(action)));
3180 /* Catch driver on updating the remote action within devlink reload */
3181 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
3182 sizeof(remote_reload_stats)));
3183 devlink_reload_stats_update(devlink, limit, *actions_performed);
3188 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
3189 enum devlink_command cmd, struct genl_info *info)
3191 struct sk_buff *msg;
3194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3198 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
3202 if (devlink_nl_put_handle(msg, devlink))
3203 goto nla_put_failure;
3205 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
3207 goto nla_put_failure;
3208 genlmsg_end(msg, hdr);
3210 return genlmsg_reply(msg, info);
3213 genlmsg_cancel(msg, hdr);
3219 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
3221 struct devlink *devlink = info->user_ptr[0];
3222 enum devlink_reload_action action;
3223 enum devlink_reload_limit limit;
3224 struct net *dest_net = NULL;
3225 u32 actions_performed;
3228 if (!devlink_reload_supported(devlink->ops))
3231 err = devlink_resources_validate(devlink, NULL, info);
3233 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
3237 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
3238 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
3239 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
3240 dest_net = devlink_netns_get(skb, info);
3241 if (IS_ERR(dest_net))
3242 return PTR_ERR(dest_net);
3245 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
3246 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
3248 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
3250 if (!devlink_reload_action_is_supported(devlink, action)) {
3251 NL_SET_ERR_MSG_MOD(info->extack,
3252 "Requested reload action is not supported by the driver");
3256 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
3257 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
3258 struct nla_bitfield32 limits;
3259 u32 limits_selected;
3261 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
3262 limits_selected = limits.value & limits.selector;
3263 if (!limits_selected) {
3264 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
3267 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
3268 if (limits_selected & BIT(limit))
3270 /* UAPI enables multiselection, but currently it is not used */
3271 if (limits_selected != BIT(limit)) {
3272 NL_SET_ERR_MSG_MOD(info->extack,
3273 "Multiselection of limit is not supported");
3276 if (!devlink_reload_limit_is_supported(devlink, limit)) {
3277 NL_SET_ERR_MSG_MOD(info->extack,
3278 "Requested limit is not supported by the driver");
3281 if (devlink_reload_combination_is_invalid(action, limit)) {
3282 NL_SET_ERR_MSG_MOD(info->extack,
3283 "Requested limit is invalid for this action");
3287 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
3294 /* For backward compatibility generate reply only if attributes used by user */
3295 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
3298 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
3299 DEVLINK_CMD_RELOAD, info);
3302 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
3303 struct devlink *devlink,
3304 enum devlink_command cmd,
3305 struct devlink_flash_notify *params)
3309 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3313 if (devlink_nl_put_handle(msg, devlink))
3314 goto nla_put_failure;
3316 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
3319 if (params->status_msg &&
3320 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
3321 params->status_msg))
3322 goto nla_put_failure;
3323 if (params->component &&
3324 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
3326 goto nla_put_failure;
3327 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
3328 params->done, DEVLINK_ATTR_PAD))
3329 goto nla_put_failure;
3330 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
3331 params->total, DEVLINK_ATTR_PAD))
3332 goto nla_put_failure;
3333 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
3334 params->timeout, DEVLINK_ATTR_PAD))
3335 goto nla_put_failure;
3338 genlmsg_end(msg, hdr);
3342 genlmsg_cancel(msg, hdr);
3346 static void __devlink_flash_update_notify(struct devlink *devlink,
3347 enum devlink_command cmd,
3348 struct devlink_flash_notify *params)
3350 struct sk_buff *msg;
3353 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
3354 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
3355 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
3357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3361 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
3365 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3366 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3373 void devlink_flash_update_begin_notify(struct devlink *devlink)
3375 struct devlink_flash_notify params = { 0 };
3377 __devlink_flash_update_notify(devlink,
3378 DEVLINK_CMD_FLASH_UPDATE,
3381 EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
3383 void devlink_flash_update_end_notify(struct devlink *devlink)
3385 struct devlink_flash_notify params = { 0 };
3387 __devlink_flash_update_notify(devlink,
3388 DEVLINK_CMD_FLASH_UPDATE_END,
3391 EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
3393 void devlink_flash_update_status_notify(struct devlink *devlink,
3394 const char *status_msg,
3395 const char *component,
3397 unsigned long total)
3399 struct devlink_flash_notify params = {
3400 .status_msg = status_msg,
3401 .component = component,
3406 __devlink_flash_update_notify(devlink,
3407 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3410 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
3412 void devlink_flash_update_timeout_notify(struct devlink *devlink,
3413 const char *status_msg,
3414 const char *component,
3415 unsigned long timeout)
3417 struct devlink_flash_notify params = {
3418 .status_msg = status_msg,
3419 .component = component,
3423 __devlink_flash_update_notify(devlink,
3424 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3427 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
3429 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
3430 struct genl_info *info)
3432 struct nlattr *nla_component, *nla_overwrite_mask;
3433 struct devlink_flash_update_params params = {};
3434 struct devlink *devlink = info->user_ptr[0];
3435 u32 supported_params;
3437 if (!devlink->ops->flash_update)
3440 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
3443 supported_params = devlink->ops->supported_flash_update_params;
3445 params.file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
3447 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
3448 if (nla_component) {
3449 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
3450 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
3451 "component update is not supported by this device");
3454 params.component = nla_data(nla_component);
3457 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
3458 if (nla_overwrite_mask) {
3459 struct nla_bitfield32 sections;
3461 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
3462 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
3463 "overwrite settings are not supported by this device");
3466 sections = nla_get_bitfield32(nla_overwrite_mask);
3467 params.overwrite_mask = sections.value & sections.selector;
3470 return devlink->ops->flash_update(devlink, ¶ms, info->extack);
3473 static const struct devlink_param devlink_param_generic[] = {
3475 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
3476 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
3477 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
3480 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3481 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
3482 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
3485 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
3486 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
3487 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
3490 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
3491 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
3492 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
3495 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
3496 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
3497 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
3500 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
3501 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
3502 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
3505 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
3506 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
3507 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
3510 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3511 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3512 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3515 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3516 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3517 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3520 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3521 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3522 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3525 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
3526 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
3527 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
3531 static int devlink_param_generic_verify(const struct devlink_param *param)
3533 /* verify it match generic parameter by id and name */
3534 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3536 if (strcmp(param->name, devlink_param_generic[param->id].name))
3539 WARN_ON(param->type != devlink_param_generic[param->id].type);
3544 static int devlink_param_driver_verify(const struct devlink_param *param)
3548 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3550 /* verify no such name in generic params */
3551 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3552 if (!strcmp(param->name, devlink_param_generic[i].name))
3558 static struct devlink_param_item *
3559 devlink_param_find_by_name(struct list_head *param_list,
3560 const char *param_name)
3562 struct devlink_param_item *param_item;
3564 list_for_each_entry(param_item, param_list, list)
3565 if (!strcmp(param_item->param->name, param_name))
3570 static struct devlink_param_item *
3571 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3573 struct devlink_param_item *param_item;
3575 list_for_each_entry(param_item, param_list, list)
3576 if (param_item->param->id == param_id)
3582 devlink_param_cmode_is_supported(const struct devlink_param *param,
3583 enum devlink_param_cmode cmode)
3585 return test_bit(cmode, ¶m->supported_cmodes);
3588 static int devlink_param_get(struct devlink *devlink,
3589 const struct devlink_param *param,
3590 struct devlink_param_gset_ctx *ctx)
3594 return param->get(devlink, param->id, ctx);
3597 static int devlink_param_set(struct devlink *devlink,
3598 const struct devlink_param *param,
3599 struct devlink_param_gset_ctx *ctx)
3603 return param->set(devlink, param->id, ctx);
3607 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3609 switch (param_type) {
3610 case DEVLINK_PARAM_TYPE_U8:
3612 case DEVLINK_PARAM_TYPE_U16:
3614 case DEVLINK_PARAM_TYPE_U32:
3616 case DEVLINK_PARAM_TYPE_STRING:
3618 case DEVLINK_PARAM_TYPE_BOOL:
3626 devlink_nl_param_value_fill_one(struct sk_buff *msg,
3627 enum devlink_param_type type,
3628 enum devlink_param_cmode cmode,
3629 union devlink_param_value val)
3631 struct nlattr *param_value_attr;
3633 param_value_attr = nla_nest_start_noflag(msg,
3634 DEVLINK_ATTR_PARAM_VALUE);
3635 if (!param_value_attr)
3636 goto nla_put_failure;
3638 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3639 goto value_nest_cancel;
3642 case DEVLINK_PARAM_TYPE_U8:
3643 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3644 goto value_nest_cancel;
3646 case DEVLINK_PARAM_TYPE_U16:
3647 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3648 goto value_nest_cancel;
3650 case DEVLINK_PARAM_TYPE_U32:
3651 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3652 goto value_nest_cancel;
3654 case DEVLINK_PARAM_TYPE_STRING:
3655 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3657 goto value_nest_cancel;
3659 case DEVLINK_PARAM_TYPE_BOOL:
3661 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3662 goto value_nest_cancel;
3666 nla_nest_end(msg, param_value_attr);
3670 nla_nest_cancel(msg, param_value_attr);
3675 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
3676 unsigned int port_index,
3677 struct devlink_param_item *param_item,
3678 enum devlink_command cmd,
3679 u32 portid, u32 seq, int flags)
3681 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
3682 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
3683 const struct devlink_param *param = param_item->param;
3684 struct devlink_param_gset_ctx ctx;
3685 struct nlattr *param_values_list;
3686 struct nlattr *param_attr;
3692 /* Get value from driver part to driverinit configuration mode */
3693 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3694 if (!devlink_param_cmode_is_supported(param, i))
3696 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3697 if (!param_item->driverinit_value_valid)
3699 param_value[i] = param_item->driverinit_value;
3701 if (!param_item->published)
3704 err = devlink_param_get(devlink, param, &ctx);
3707 param_value[i] = ctx.val;
3709 param_value_set[i] = true;
3712 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3716 if (devlink_nl_put_handle(msg, devlink))
3717 goto genlmsg_cancel;
3719 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3720 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3721 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
3722 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3723 goto genlmsg_cancel;
3725 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
3727 goto genlmsg_cancel;
3728 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3729 goto param_nest_cancel;
3730 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3731 goto param_nest_cancel;
3733 nla_type = devlink_param_type_to_nla_type(param->type);
3735 goto param_nest_cancel;
3736 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3737 goto param_nest_cancel;
3739 param_values_list = nla_nest_start_noflag(msg,
3740 DEVLINK_ATTR_PARAM_VALUES_LIST);
3741 if (!param_values_list)
3742 goto param_nest_cancel;
3744 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3745 if (!param_value_set[i])
3747 err = devlink_nl_param_value_fill_one(msg, param->type,
3750 goto values_list_nest_cancel;
3753 nla_nest_end(msg, param_values_list);
3754 nla_nest_end(msg, param_attr);
3755 genlmsg_end(msg, hdr);
3758 values_list_nest_cancel:
3759 nla_nest_end(msg, param_values_list);
3761 nla_nest_cancel(msg, param_attr);
3763 genlmsg_cancel(msg, hdr);
3767 static void devlink_param_notify(struct devlink *devlink,
3768 unsigned int port_index,
3769 struct devlink_param_item *param_item,
3770 enum devlink_command cmd)
3772 struct sk_buff *msg;
3775 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3776 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3777 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
3779 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3782 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3789 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3790 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3793 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3794 struct netlink_callback *cb)
3796 struct devlink_param_item *param_item;
3797 struct devlink *devlink;
3798 int start = cb->args[0];
3802 mutex_lock(&devlink_mutex);
3803 list_for_each_entry(devlink, &devlink_list, list) {
3804 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3806 mutex_lock(&devlink->lock);
3807 list_for_each_entry(param_item, &devlink->param_list, list) {
3812 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3813 DEVLINK_CMD_PARAM_GET,
3814 NETLINK_CB(cb->skb).portid,
3817 if (err == -EOPNOTSUPP) {
3820 mutex_unlock(&devlink->lock);
3825 mutex_unlock(&devlink->lock);
3828 mutex_unlock(&devlink_mutex);
3830 if (err != -EMSGSIZE)
3838 devlink_param_type_get_from_info(struct genl_info *info,
3839 enum devlink_param_type *param_type)
3841 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3844 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3846 *param_type = DEVLINK_PARAM_TYPE_U8;
3849 *param_type = DEVLINK_PARAM_TYPE_U16;
3852 *param_type = DEVLINK_PARAM_TYPE_U32;
3855 *param_type = DEVLINK_PARAM_TYPE_STRING;
3858 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3868 devlink_param_value_get_from_info(const struct devlink_param *param,
3869 struct genl_info *info,
3870 union devlink_param_value *value)
3872 struct nlattr *param_data;
3875 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3877 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
3880 switch (param->type) {
3881 case DEVLINK_PARAM_TYPE_U8:
3882 if (nla_len(param_data) != sizeof(u8))
3884 value->vu8 = nla_get_u8(param_data);
3886 case DEVLINK_PARAM_TYPE_U16:
3887 if (nla_len(param_data) != sizeof(u16))
3889 value->vu16 = nla_get_u16(param_data);
3891 case DEVLINK_PARAM_TYPE_U32:
3892 if (nla_len(param_data) != sizeof(u32))
3894 value->vu32 = nla_get_u32(param_data);
3896 case DEVLINK_PARAM_TYPE_STRING:
3897 len = strnlen(nla_data(param_data), nla_len(param_data));
3898 if (len == nla_len(param_data) ||
3899 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3901 strcpy(value->vstr, nla_data(param_data));
3903 case DEVLINK_PARAM_TYPE_BOOL:
3904 if (param_data && nla_len(param_data))
3906 value->vbool = nla_get_flag(param_data);
3912 static struct devlink_param_item *
3913 devlink_param_get_from_info(struct list_head *param_list,
3914 struct genl_info *info)
3918 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3921 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3922 return devlink_param_find_by_name(param_list, param_name);
3925 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3926 struct genl_info *info)
3928 struct devlink *devlink = info->user_ptr[0];
3929 struct devlink_param_item *param_item;
3930 struct sk_buff *msg;
3933 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3937 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3941 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3942 DEVLINK_CMD_PARAM_GET,
3943 info->snd_portid, info->snd_seq, 0);
3949 return genlmsg_reply(msg, info);
3952 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3953 unsigned int port_index,
3954 struct list_head *param_list,
3955 struct genl_info *info,
3956 enum devlink_command cmd)
3958 enum devlink_param_type param_type;
3959 struct devlink_param_gset_ctx ctx;
3960 enum devlink_param_cmode cmode;
3961 struct devlink_param_item *param_item;
3962 const struct devlink_param *param;
3963 union devlink_param_value value;
3966 param_item = devlink_param_get_from_info(param_list, info);
3969 param = param_item->param;
3970 err = devlink_param_type_get_from_info(info, ¶m_type);
3973 if (param_type != param->type)
3975 err = devlink_param_value_get_from_info(param, info, &value);
3978 if (param->validate) {
3979 err = param->validate(devlink, param->id, value, info->extack);
3984 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3986 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3987 if (!devlink_param_cmode_is_supported(param, cmode))
3990 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3991 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3992 strcpy(param_item->driverinit_value.vstr, value.vstr);
3994 param_item->driverinit_value = value;
3995 param_item->driverinit_value_valid = true;
4001 err = devlink_param_set(devlink, param, &ctx);
4006 devlink_param_notify(devlink, port_index, param_item, cmd);
4010 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
4011 struct genl_info *info)
4013 struct devlink *devlink = info->user_ptr[0];
4015 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
4016 info, DEVLINK_CMD_PARAM_NEW);
4019 static int devlink_param_register_one(struct devlink *devlink,
4020 unsigned int port_index,
4021 struct list_head *param_list,
4022 const struct devlink_param *param,
4023 enum devlink_command cmd)
4025 struct devlink_param_item *param_item;
4027 if (devlink_param_find_by_name(param_list, param->name))
4030 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
4031 WARN_ON(param->get || param->set);
4033 WARN_ON(!param->get || !param->set);
4035 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
4038 param_item->param = param;
4040 list_add_tail(¶m_item->list, param_list);
4041 devlink_param_notify(devlink, port_index, param_item, cmd);
4045 static void devlink_param_unregister_one(struct devlink *devlink,
4046 unsigned int port_index,
4047 struct list_head *param_list,
4048 const struct devlink_param *param,
4049 enum devlink_command cmd)
4051 struct devlink_param_item *param_item;
4053 param_item = devlink_param_find_by_name(param_list, param->name);
4054 WARN_ON(!param_item);
4055 devlink_param_notify(devlink, port_index, param_item, cmd);
4056 list_del(¶m_item->list);
4060 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
4061 struct netlink_callback *cb)
4063 struct devlink_param_item *param_item;
4064 struct devlink_port *devlink_port;
4065 struct devlink *devlink;
4066 int start = cb->args[0];
4070 mutex_lock(&devlink_mutex);
4071 list_for_each_entry(devlink, &devlink_list, list) {
4072 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4074 mutex_lock(&devlink->lock);
4075 list_for_each_entry(devlink_port, &devlink->port_list, list) {
4076 list_for_each_entry(param_item,
4077 &devlink_port->param_list, list) {
4082 err = devlink_nl_param_fill(msg,
4083 devlink_port->devlink,
4084 devlink_port->index, param_item,
4085 DEVLINK_CMD_PORT_PARAM_GET,
4086 NETLINK_CB(cb->skb).portid,
4089 if (err == -EOPNOTSUPP) {
4092 mutex_unlock(&devlink->lock);
4098 mutex_unlock(&devlink->lock);
4101 mutex_unlock(&devlink_mutex);
4103 if (err != -EMSGSIZE)
4110 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
4111 struct genl_info *info)
4113 struct devlink_port *devlink_port = info->user_ptr[0];
4114 struct devlink_param_item *param_item;
4115 struct sk_buff *msg;
4118 param_item = devlink_param_get_from_info(&devlink_port->param_list,
4123 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4127 err = devlink_nl_param_fill(msg, devlink_port->devlink,
4128 devlink_port->index, param_item,
4129 DEVLINK_CMD_PORT_PARAM_GET,
4130 info->snd_portid, info->snd_seq, 0);
4136 return genlmsg_reply(msg, info);
4139 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
4140 struct genl_info *info)
4142 struct devlink_port *devlink_port = info->user_ptr[0];
4144 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
4145 devlink_port->index,
4146 &devlink_port->param_list, info,
4147 DEVLINK_CMD_PORT_PARAM_NEW);
4150 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
4151 struct devlink *devlink,
4152 struct devlink_snapshot *snapshot)
4154 struct nlattr *snap_attr;
4157 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
4161 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
4163 goto nla_put_failure;
4165 nla_nest_end(msg, snap_attr);
4169 nla_nest_cancel(msg, snap_attr);
4173 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
4174 struct devlink *devlink,
4175 struct devlink_region *region)
4177 struct devlink_snapshot *snapshot;
4178 struct nlattr *snapshots_attr;
4181 snapshots_attr = nla_nest_start_noflag(msg,
4182 DEVLINK_ATTR_REGION_SNAPSHOTS);
4183 if (!snapshots_attr)
4186 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
4187 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
4189 goto nla_put_failure;
4192 nla_nest_end(msg, snapshots_attr);
4196 nla_nest_cancel(msg, snapshots_attr);
4200 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
4201 enum devlink_command cmd, u32 portid,
4203 struct devlink_region *region)
4208 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4212 err = devlink_nl_put_handle(msg, devlink);
4214 goto nla_put_failure;
4217 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4218 region->port->index);
4220 goto nla_put_failure;
4223 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
4225 goto nla_put_failure;
4227 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4231 goto nla_put_failure;
4233 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
4235 goto nla_put_failure;
4237 genlmsg_end(msg, hdr);
4241 genlmsg_cancel(msg, hdr);
4245 static struct sk_buff *
4246 devlink_nl_region_notify_build(struct devlink_region *region,
4247 struct devlink_snapshot *snapshot,
4248 enum devlink_command cmd, u32 portid, u32 seq)
4250 struct devlink *devlink = region->devlink;
4251 struct sk_buff *msg;
4256 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4258 return ERR_PTR(-ENOMEM);
4260 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
4266 err = devlink_nl_put_handle(msg, devlink);
4268 goto out_cancel_msg;
4271 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4272 region->port->index);
4274 goto out_cancel_msg;
4277 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
4280 goto out_cancel_msg;
4283 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
4286 goto out_cancel_msg;
4288 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4289 region->size, DEVLINK_ATTR_PAD);
4291 goto out_cancel_msg;
4293 genlmsg_end(msg, hdr);
4298 genlmsg_cancel(msg, hdr);
4301 return ERR_PTR(err);
4304 static void devlink_nl_region_notify(struct devlink_region *region,
4305 struct devlink_snapshot *snapshot,
4306 enum devlink_command cmd)
4308 struct devlink *devlink = region->devlink;
4309 struct sk_buff *msg;
4311 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
4313 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
4317 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4318 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4322 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4323 * @devlink: devlink instance
4324 * @id: the snapshot id
4326 * Track when a new snapshot begins using an id. Load the count for the
4327 * given id from the snapshot xarray, increment it, and store it back.
4329 * Called when a new snapshot is created with the given id.
4331 * The id *must* have been previously allocated by
4332 * devlink_region_snapshot_id_get().
4334 * Returns 0 on success, or an error on failure.
4336 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
4338 unsigned long count;
4341 lockdep_assert_held(&devlink->lock);
4343 p = xa_load(&devlink->snapshot_ids, id);
4347 if (WARN_ON(!xa_is_value(p)))
4350 count = xa_to_value(p);
4353 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4358 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4359 * @devlink: devlink instance
4360 * @id: the snapshot id
4362 * Track when a snapshot is deleted and stops using an id. Load the count
4363 * for the given id from the snapshot xarray, decrement it, and store it
4366 * If the count reaches zero, erase this id from the xarray, freeing it
4367 * up for future re-use by devlink_region_snapshot_id_get().
4369 * Called when a snapshot using the given id is deleted, and when the
4370 * initial allocator of the id is finished using it.
4372 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
4374 unsigned long count;
4377 lockdep_assert_held(&devlink->lock);
4379 p = xa_load(&devlink->snapshot_ids, id);
4383 if (WARN_ON(!xa_is_value(p)))
4386 count = xa_to_value(p);
4390 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4393 /* If this was the last user, we can erase this id */
4394 xa_erase(&devlink->snapshot_ids, id);
4399 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4400 * @devlink: devlink instance
4401 * @id: the snapshot id
4403 * Mark the given snapshot id as used by inserting a zero value into the
4406 * This must be called while holding the devlink instance lock. Unlike
4407 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4408 * It is expected that the id will immediately be used before
4409 * releasing the devlink instance lock.
4411 * Returns zero on success, or an error code if the snapshot id could not
4414 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
4416 lockdep_assert_held(&devlink->lock);
4418 if (xa_load(&devlink->snapshot_ids, id))
4421 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
4426 * __devlink_region_snapshot_id_get - get snapshot ID
4427 * @devlink: devlink instance
4428 * @id: storage to return snapshot id
4430 * Allocates a new snapshot id. Returns zero on success, or a negative
4431 * error on failure. Must be called while holding the devlink instance
4434 * Snapshot IDs are tracked using an xarray which stores the number of
4435 * users of the snapshot id.
4437 * Note that the caller of this function counts as a 'user', in order to
4438 * avoid race conditions. The caller must release its hold on the
4439 * snapshot by using devlink_region_snapshot_id_put.
4441 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
4443 lockdep_assert_held(&devlink->lock);
4445 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
4446 xa_limit_32b, GFP_KERNEL);
4450 * __devlink_region_snapshot_create - create a new snapshot
4451 * This will add a new snapshot of a region. The snapshot
4452 * will be stored on the region struct and can be accessed
4453 * from devlink. This is useful for future analyses of snapshots.
4454 * Multiple snapshots can be created on a region.
4455 * The @snapshot_id should be obtained using the getter function.
4457 * Must be called only while holding the devlink instance lock.
4459 * @region: devlink region of the snapshot
4460 * @data: snapshot data
4461 * @snapshot_id: snapshot id to be created
4464 __devlink_region_snapshot_create(struct devlink_region *region,
4465 u8 *data, u32 snapshot_id)
4467 struct devlink *devlink = region->devlink;
4468 struct devlink_snapshot *snapshot;
4471 lockdep_assert_held(&devlink->lock);
4473 /* check if region can hold one more snapshot */
4474 if (region->cur_snapshots == region->max_snapshots)
4477 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
4480 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4484 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
4486 goto err_snapshot_id_increment;
4488 snapshot->id = snapshot_id;
4489 snapshot->region = region;
4490 snapshot->data = data;
4492 list_add_tail(&snapshot->list, ®ion->snapshot_list);
4494 region->cur_snapshots++;
4496 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4499 err_snapshot_id_increment:
4504 static void devlink_region_snapshot_del(struct devlink_region *region,
4505 struct devlink_snapshot *snapshot)
4507 struct devlink *devlink = region->devlink;
4509 lockdep_assert_held(&devlink->lock);
4511 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
4512 region->cur_snapshots--;
4513 list_del(&snapshot->list);
4514 region->ops->destructor(snapshot->data);
4515 __devlink_snapshot_id_decrement(devlink, snapshot->id);
4519 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
4520 struct genl_info *info)
4522 struct devlink *devlink = info->user_ptr[0];
4523 struct devlink_port *port = NULL;
4524 struct devlink_region *region;
4525 const char *region_name;
4526 struct sk_buff *msg;
4530 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
4533 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4534 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4536 port = devlink_port_get_by_index(devlink, index);
4541 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4543 region = devlink_port_region_get_by_name(port, region_name);
4545 region = devlink_region_get_by_name(devlink, region_name);
4550 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4554 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4555 info->snd_portid, info->snd_seq, 0,
4562 return genlmsg_reply(msg, info);
4565 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
4566 struct netlink_callback *cb,
4567 struct devlink_port *port,
4571 struct devlink_region *region;
4574 list_for_each_entry(region, &port->region_list, list) {
4579 err = devlink_nl_region_fill(msg, port->devlink,
4580 DEVLINK_CMD_REGION_GET,
4581 NETLINK_CB(cb->skb).portid,
4583 NLM_F_MULTI, region);
4593 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
4594 struct netlink_callback *cb,
4595 struct devlink *devlink,
4599 struct devlink_region *region;
4600 struct devlink_port *port;
4603 mutex_lock(&devlink->lock);
4604 list_for_each_entry(region, &devlink->region_list, list) {
4609 err = devlink_nl_region_fill(msg, devlink,
4610 DEVLINK_CMD_REGION_GET,
4611 NETLINK_CB(cb->skb).portid,
4613 NLM_F_MULTI, region);
4619 list_for_each_entry(port, &devlink->port_list, list) {
4620 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
4627 mutex_unlock(&devlink->lock);
4631 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
4632 struct netlink_callback *cb)
4634 struct devlink *devlink;
4635 int start = cb->args[0];
4639 mutex_lock(&devlink_mutex);
4640 list_for_each_entry(devlink, &devlink_list, list) {
4641 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4643 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
4649 mutex_unlock(&devlink_mutex);
4654 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4655 struct genl_info *info)
4657 struct devlink *devlink = info->user_ptr[0];
4658 struct devlink_snapshot *snapshot;
4659 struct devlink_port *port = NULL;
4660 struct devlink_region *region;
4661 const char *region_name;
4665 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
4666 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
4669 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4670 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4672 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4673 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4675 port = devlink_port_get_by_index(devlink, index);
4681 region = devlink_port_region_get_by_name(port, region_name);
4683 region = devlink_region_get_by_name(devlink, region_name);
4688 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4692 devlink_region_snapshot_del(region, snapshot);
4697 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4699 struct devlink *devlink = info->user_ptr[0];
4700 struct devlink_snapshot *snapshot;
4701 struct devlink_port *port = NULL;
4702 struct nlattr *snapshot_id_attr;
4703 struct devlink_region *region;
4704 const char *region_name;
4710 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4711 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4715 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4717 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4718 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4720 port = devlink_port_get_by_index(devlink, index);
4726 region = devlink_port_region_get_by_name(port, region_name);
4728 region = devlink_region_get_by_name(devlink, region_name);
4731 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4735 if (!region->ops->snapshot) {
4736 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4740 if (region->cur_snapshots == region->max_snapshots) {
4741 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4745 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
4746 if (snapshot_id_attr) {
4747 snapshot_id = nla_get_u32(snapshot_id_attr);
4749 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4750 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4754 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4758 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
4760 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
4766 err = region->port_ops->snapshot(port, region->port_ops,
4767 info->extack, &data);
4769 err = region->ops->snapshot(devlink, region->ops,
4770 info->extack, &data);
4772 goto err_snapshot_capture;
4774 err = __devlink_region_snapshot_create(region, data, snapshot_id);
4776 goto err_snapshot_create;
4778 if (!snapshot_id_attr) {
4779 struct sk_buff *msg;
4781 snapshot = devlink_region_snapshot_get_by_id(region,
4783 if (WARN_ON(!snapshot))
4786 msg = devlink_nl_region_notify_build(region, snapshot,
4787 DEVLINK_CMD_REGION_NEW,
4790 err = PTR_ERR_OR_ZERO(msg);
4794 err = genlmsg_reply(msg, info);
4801 err_snapshot_create:
4802 region->ops->destructor(data);
4803 err_snapshot_capture:
4804 __devlink_snapshot_id_decrement(devlink, snapshot_id);
4808 devlink_region_snapshot_del(region, snapshot);
4812 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
4813 struct devlink *devlink,
4814 u8 *chunk, u32 chunk_size,
4817 struct nlattr *chunk_attr;
4820 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
4824 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
4826 goto nla_put_failure;
4828 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
4831 goto nla_put_failure;
4833 nla_nest_end(msg, chunk_attr);
4837 nla_nest_cancel(msg, chunk_attr);
4841 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4843 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
4844 struct devlink *devlink,
4845 struct devlink_region *region,
4846 struct nlattr **attrs,
4851 struct devlink_snapshot *snapshot;
4852 u64 curr_offset = start_offset;
4856 *new_offset = start_offset;
4858 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4859 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4863 while (curr_offset < end_offset) {
4867 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
4868 data_size = end_offset - curr_offset;
4870 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
4872 data = &snapshot->data[curr_offset];
4873 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
4879 curr_offset += data_size;
4881 *new_offset = curr_offset;
4886 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
4887 struct netlink_callback *cb)
4889 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
4890 u64 ret_offset, start_offset, end_offset = U64_MAX;
4891 struct nlattr **attrs = info->attrs;
4892 struct devlink_port *port = NULL;
4893 struct devlink_region *region;
4894 struct nlattr *chunks_attr;
4895 const char *region_name;
4896 struct devlink *devlink;
4901 start_offset = *((u64 *)&cb->args[0]);
4903 mutex_lock(&devlink_mutex);
4904 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4905 if (IS_ERR(devlink)) {
4906 err = PTR_ERR(devlink);
4910 mutex_lock(&devlink->lock);
4912 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
4913 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4918 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4919 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4921 port = devlink_port_get_by_index(devlink, index);
4928 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
4931 region = devlink_port_region_get_by_name(port, region_name);
4933 region = devlink_region_get_by_name(devlink, region_name);
4940 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4941 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4944 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4946 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4947 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4950 if (end_offset > region->size)
4951 end_offset = region->size;
4953 /* return 0 if there is no further data to read */
4954 if (start_offset == end_offset) {
4959 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4960 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
4961 DEVLINK_CMD_REGION_READ);
4967 err = devlink_nl_put_handle(skb, devlink);
4969 goto nla_put_failure;
4972 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
4973 region->port->index);
4975 goto nla_put_failure;
4978 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4980 goto nla_put_failure;
4982 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
4985 goto nla_put_failure;
4988 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4991 end_offset, &ret_offset);
4993 if (err && err != -EMSGSIZE)
4994 goto nla_put_failure;
4996 /* Check if there was any progress done to prevent infinite loop */
4997 if (ret_offset == start_offset) {
4999 goto nla_put_failure;
5002 *((u64 *)&cb->args[0]) = ret_offset;
5004 nla_nest_end(skb, chunks_attr);
5005 genlmsg_end(skb, hdr);
5006 mutex_unlock(&devlink->lock);
5007 mutex_unlock(&devlink_mutex);
5012 genlmsg_cancel(skb, hdr);
5014 mutex_unlock(&devlink->lock);
5016 mutex_unlock(&devlink_mutex);
5020 struct devlink_info_req {
5021 struct sk_buff *msg;
5024 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
5026 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
5028 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
5030 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
5032 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
5034 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
5036 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
5039 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
5042 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
5044 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
5045 const char *version_name,
5046 const char *version_value)
5048 struct nlattr *nest;
5051 nest = nla_nest_start_noflag(req->msg, attr);
5055 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
5058 goto nla_put_failure;
5060 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
5063 goto nla_put_failure;
5065 nla_nest_end(req->msg, nest);
5070 nla_nest_cancel(req->msg, nest);
5074 int devlink_info_version_fixed_put(struct devlink_info_req *req,
5075 const char *version_name,
5076 const char *version_value)
5078 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
5079 version_name, version_value);
5081 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
5083 int devlink_info_version_stored_put(struct devlink_info_req *req,
5084 const char *version_name,
5085 const char *version_value)
5087 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
5088 version_name, version_value);
5090 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
5092 int devlink_info_version_running_put(struct devlink_info_req *req,
5093 const char *version_name,
5094 const char *version_value)
5096 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
5097 version_name, version_value);
5099 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
5102 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
5103 enum devlink_command cmd, u32 portid,
5104 u32 seq, int flags, struct netlink_ext_ack *extack)
5106 struct devlink_info_req req;
5110 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5115 if (devlink_nl_put_handle(msg, devlink))
5116 goto err_cancel_msg;
5119 err = devlink->ops->info_get(devlink, &req, extack);
5121 goto err_cancel_msg;
5123 genlmsg_end(msg, hdr);
5127 genlmsg_cancel(msg, hdr);
5131 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
5132 struct genl_info *info)
5134 struct devlink *devlink = info->user_ptr[0];
5135 struct sk_buff *msg;
5138 if (!devlink->ops->info_get)
5141 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5145 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5146 info->snd_portid, info->snd_seq, 0,
5153 return genlmsg_reply(msg, info);
5156 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
5157 struct netlink_callback *cb)
5159 struct devlink *devlink;
5160 int start = cb->args[0];
5164 mutex_lock(&devlink_mutex);
5165 list_for_each_entry(devlink, &devlink_list, list) {
5166 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5173 if (!devlink->ops->info_get) {
5178 mutex_lock(&devlink->lock);
5179 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5180 NETLINK_CB(cb->skb).portid,
5181 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5183 mutex_unlock(&devlink->lock);
5184 if (err == -EOPNOTSUPP)
5190 mutex_unlock(&devlink_mutex);
5192 if (err != -EMSGSIZE)
5199 struct devlink_fmsg_item {
5200 struct list_head list;
5207 struct devlink_fmsg {
5208 struct list_head item_list;
5209 bool putting_binary; /* This flag forces enclosing of binary data
5210 * in an array brackets. It forces using
5211 * of designated API:
5212 * devlink_fmsg_binary_pair_nest_start()
5213 * devlink_fmsg_binary_pair_nest_end()
5217 static struct devlink_fmsg *devlink_fmsg_alloc(void)
5219 struct devlink_fmsg *fmsg;
5221 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
5225 INIT_LIST_HEAD(&fmsg->item_list);
5230 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
5232 struct devlink_fmsg_item *item, *tmp;
5234 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
5235 list_del(&item->list);
5241 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
5244 struct devlink_fmsg_item *item;
5246 item = kzalloc(sizeof(*item), GFP_KERNEL);
5250 item->attrtype = attrtype;
5251 list_add_tail(&item->list, &fmsg->item_list);
5256 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
5258 if (fmsg->putting_binary)
5261 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
5263 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
5265 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
5267 if (fmsg->putting_binary)
5270 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
5273 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
5275 if (fmsg->putting_binary)
5278 return devlink_fmsg_nest_end(fmsg);
5280 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
5282 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
5284 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
5286 struct devlink_fmsg_item *item;
5288 if (fmsg->putting_binary)
5291 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
5294 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
5298 item->nla_type = NLA_NUL_STRING;
5299 item->len = strlen(name) + 1;
5300 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
5301 memcpy(&item->value, name, item->len);
5302 list_add_tail(&item->list, &fmsg->item_list);
5307 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
5311 if (fmsg->putting_binary)
5314 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
5318 err = devlink_fmsg_put_name(fmsg, name);
5324 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
5326 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
5328 if (fmsg->putting_binary)
5331 return devlink_fmsg_nest_end(fmsg);
5333 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
5335 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
5340 if (fmsg->putting_binary)
5343 err = devlink_fmsg_pair_nest_start(fmsg, name);
5347 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
5353 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
5355 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
5359 if (fmsg->putting_binary)
5362 err = devlink_fmsg_nest_end(fmsg);
5366 err = devlink_fmsg_nest_end(fmsg);
5372 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
5374 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
5379 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
5383 fmsg->putting_binary = true;
5386 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
5388 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
5390 if (!fmsg->putting_binary)
5393 fmsg->putting_binary = false;
5394 return devlink_fmsg_arr_pair_nest_end(fmsg);
5396 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
5398 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
5399 const void *value, u16 value_len,
5402 struct devlink_fmsg_item *item;
5404 if (value_len > DEVLINK_FMSG_MAX_SIZE)
5407 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
5411 item->nla_type = value_nla_type;
5412 item->len = value_len;
5413 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5414 memcpy(&item->value, value, item->len);
5415 list_add_tail(&item->list, &fmsg->item_list);
5420 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
5422 if (fmsg->putting_binary)
5425 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
5427 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
5429 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
5431 if (fmsg->putting_binary)
5434 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
5436 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
5438 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
5440 if (fmsg->putting_binary)
5443 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
5445 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
5447 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
5449 if (fmsg->putting_binary)
5452 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
5454 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
5456 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
5458 if (fmsg->putting_binary)
5461 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
5464 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
5466 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
5469 if (!fmsg->putting_binary)
5472 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
5474 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
5476 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
5481 err = devlink_fmsg_pair_nest_start(fmsg, name);
5485 err = devlink_fmsg_bool_put(fmsg, value);
5489 err = devlink_fmsg_pair_nest_end(fmsg);
5495 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
5497 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
5502 err = devlink_fmsg_pair_nest_start(fmsg, name);
5506 err = devlink_fmsg_u8_put(fmsg, value);
5510 err = devlink_fmsg_pair_nest_end(fmsg);
5516 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
5518 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
5523 err = devlink_fmsg_pair_nest_start(fmsg, name);
5527 err = devlink_fmsg_u32_put(fmsg, value);
5531 err = devlink_fmsg_pair_nest_end(fmsg);
5537 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
5539 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
5544 err = devlink_fmsg_pair_nest_start(fmsg, name);
5548 err = devlink_fmsg_u64_put(fmsg, value);
5552 err = devlink_fmsg_pair_nest_end(fmsg);
5558 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
5560 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
5565 err = devlink_fmsg_pair_nest_start(fmsg, name);
5569 err = devlink_fmsg_string_put(fmsg, value);
5573 err = devlink_fmsg_pair_nest_end(fmsg);
5579 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
5581 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
5582 const void *value, u32 value_len)
5589 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
5593 for (offset = 0; offset < value_len; offset += data_size) {
5594 data_size = value_len - offset;
5595 if (data_size > DEVLINK_FMSG_MAX_SIZE)
5596 data_size = DEVLINK_FMSG_MAX_SIZE;
5597 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
5600 /* Exit from loop with a break (instead of
5601 * return) to make sure putting_binary is turned off in
5602 * devlink_fmsg_binary_pair_nest_end
5606 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
5612 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
5615 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5617 switch (msg->nla_type) {
5622 case NLA_NUL_STRING:
5624 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
5632 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5634 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5637 switch (msg->nla_type) {
5639 /* Always provide flag data, regardless of its value */
5640 tmp = *(bool *) msg->value;
5642 return nla_put_u8(skb, attrtype, tmp);
5644 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
5646 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
5648 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
5650 case NLA_NUL_STRING:
5651 return nla_put_string(skb, attrtype, (char *) &msg->value);
5653 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
5660 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5663 struct devlink_fmsg_item *item;
5664 struct nlattr *fmsg_nlattr;
5668 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
5672 list_for_each_entry(item, &fmsg->item_list, list) {
5678 switch (item->attrtype) {
5679 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
5680 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
5681 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
5682 case DEVLINK_ATTR_FMSG_NEST_END:
5683 err = nla_put_flag(skb, item->attrtype);
5685 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
5686 err = devlink_fmsg_item_fill_type(item, skb);
5689 err = devlink_fmsg_item_fill_data(item, skb);
5691 case DEVLINK_ATTR_FMSG_OBJ_NAME:
5692 err = nla_put_string(skb, item->attrtype,
5693 (char *) &item->value);
5705 nla_nest_end(skb, fmsg_nlattr);
5709 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
5710 struct genl_info *info,
5711 enum devlink_command cmd, int flags)
5713 struct nlmsghdr *nlh;
5714 struct sk_buff *skb;
5721 int tmp_index = index;
5723 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5727 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
5728 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
5731 goto nla_put_failure;
5734 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5737 else if (err != -EMSGSIZE || tmp_index == index)
5738 goto nla_put_failure;
5740 genlmsg_end(skb, hdr);
5741 err = genlmsg_reply(skb, info);
5746 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5749 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
5750 NLMSG_DONE, 0, flags | NLM_F_MULTI);
5753 goto nla_put_failure;
5756 return genlmsg_reply(skb, info);
5763 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5764 struct netlink_callback *cb,
5765 enum devlink_command cmd)
5767 int index = cb->args[0];
5768 int tmp_index = index;
5772 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5773 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
5776 goto nla_put_failure;
5779 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5780 if ((err && err != -EMSGSIZE) || tmp_index == index)
5781 goto nla_put_failure;
5783 cb->args[0] = index;
5784 genlmsg_end(skb, hdr);
5788 genlmsg_cancel(skb, hdr);
5792 struct devlink_health_reporter {
5793 struct list_head list;
5795 const struct devlink_health_reporter_ops *ops;
5796 struct devlink *devlink;
5797 struct devlink_port *devlink_port;
5798 struct devlink_fmsg *dump_fmsg;
5799 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
5800 u64 graceful_period;
5808 u64 last_recovery_ts;
5809 refcount_t refcount;
5813 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
5815 return reporter->priv;
5817 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
5819 static struct devlink_health_reporter *
5820 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
5821 struct mutex *list_lock,
5822 const char *reporter_name)
5824 struct devlink_health_reporter *reporter;
5826 lockdep_assert_held(list_lock);
5827 list_for_each_entry(reporter, reporter_list, list)
5828 if (!strcmp(reporter->ops->name, reporter_name))
5833 static struct devlink_health_reporter *
5834 devlink_health_reporter_find_by_name(struct devlink *devlink,
5835 const char *reporter_name)
5837 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
5838 &devlink->reporters_lock,
5842 static struct devlink_health_reporter *
5843 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
5844 const char *reporter_name)
5846 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
5847 &devlink_port->reporters_lock,
5851 static struct devlink_health_reporter *
5852 __devlink_health_reporter_create(struct devlink *devlink,
5853 const struct devlink_health_reporter_ops *ops,
5854 u64 graceful_period, void *priv)
5856 struct devlink_health_reporter *reporter;
5858 if (WARN_ON(graceful_period && !ops->recover))
5859 return ERR_PTR(-EINVAL);
5861 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
5863 return ERR_PTR(-ENOMEM);
5865 reporter->priv = priv;
5866 reporter->ops = ops;
5867 reporter->devlink = devlink;
5868 reporter->graceful_period = graceful_period;
5869 reporter->auto_recover = !!ops->recover;
5870 reporter->auto_dump = !!ops->dump;
5871 mutex_init(&reporter->dump_lock);
5872 refcount_set(&reporter->refcount, 1);
5877 * devlink_port_health_reporter_create - create devlink health reporter for
5878 * specified port instance
5880 * @port: devlink_port which should contain the new reporter
5882 * @graceful_period: to avoid recovery loops, in msecs
5885 struct devlink_health_reporter *
5886 devlink_port_health_reporter_create(struct devlink_port *port,
5887 const struct devlink_health_reporter_ops *ops,
5888 u64 graceful_period, void *priv)
5890 struct devlink_health_reporter *reporter;
5892 mutex_lock(&port->reporters_lock);
5893 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
5894 &port->reporters_lock, ops->name)) {
5895 reporter = ERR_PTR(-EEXIST);
5899 reporter = __devlink_health_reporter_create(port->devlink, ops,
5900 graceful_period, priv);
5901 if (IS_ERR(reporter))
5904 reporter->devlink_port = port;
5905 list_add_tail(&reporter->list, &port->reporter_list);
5907 mutex_unlock(&port->reporters_lock);
5910 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
5913 * devlink_health_reporter_create - create devlink health reporter
5917 * @graceful_period: to avoid recovery loops, in msecs
5920 struct devlink_health_reporter *
5921 devlink_health_reporter_create(struct devlink *devlink,
5922 const struct devlink_health_reporter_ops *ops,
5923 u64 graceful_period, void *priv)
5925 struct devlink_health_reporter *reporter;
5927 mutex_lock(&devlink->reporters_lock);
5928 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
5929 reporter = ERR_PTR(-EEXIST);
5933 reporter = __devlink_health_reporter_create(devlink, ops,
5934 graceful_period, priv);
5935 if (IS_ERR(reporter))
5938 list_add_tail(&reporter->list, &devlink->reporter_list);
5940 mutex_unlock(&devlink->reporters_lock);
5943 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
5946 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
5948 mutex_destroy(&reporter->dump_lock);
5949 if (reporter->dump_fmsg)
5950 devlink_fmsg_free(reporter->dump_fmsg);
5955 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5957 if (refcount_dec_and_test(&reporter->refcount))
5958 devlink_health_reporter_free(reporter);
5962 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5964 list_del(&reporter->list);
5965 devlink_health_reporter_put(reporter);
5969 * devlink_health_reporter_destroy - destroy devlink health reporter
5971 * @reporter: devlink health reporter to destroy
5974 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5976 struct mutex *lock = &reporter->devlink->reporters_lock;
5979 __devlink_health_reporter_destroy(reporter);
5982 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
5985 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
5987 * @reporter: devlink health reporter to destroy
5990 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
5992 struct mutex *lock = &reporter->devlink_port->reporters_lock;
5995 __devlink_health_reporter_destroy(reporter);
5998 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
6001 devlink_nl_health_reporter_fill(struct sk_buff *msg,
6002 struct devlink *devlink,
6003 struct devlink_health_reporter *reporter,
6004 enum devlink_command cmd, u32 portid,
6007 struct nlattr *reporter_attr;
6010 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6014 if (devlink_nl_put_handle(msg, devlink))
6015 goto genlmsg_cancel;
6017 if (reporter->devlink_port) {
6018 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
6019 goto genlmsg_cancel;
6021 reporter_attr = nla_nest_start_noflag(msg,
6022 DEVLINK_ATTR_HEALTH_REPORTER);
6024 goto genlmsg_cancel;
6025 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
6026 reporter->ops->name))
6027 goto reporter_nest_cancel;
6028 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
6029 reporter->health_state))
6030 goto reporter_nest_cancel;
6031 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
6032 reporter->error_count, DEVLINK_ATTR_PAD))
6033 goto reporter_nest_cancel;
6034 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
6035 reporter->recovery_count, DEVLINK_ATTR_PAD))
6036 goto reporter_nest_cancel;
6037 if (reporter->ops->recover &&
6038 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
6039 reporter->graceful_period,
6041 goto reporter_nest_cancel;
6042 if (reporter->ops->recover &&
6043 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
6044 reporter->auto_recover))
6045 goto reporter_nest_cancel;
6046 if (reporter->dump_fmsg &&
6047 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
6048 jiffies_to_msecs(reporter->dump_ts),
6050 goto reporter_nest_cancel;
6051 if (reporter->dump_fmsg &&
6052 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
6053 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
6054 goto reporter_nest_cancel;
6055 if (reporter->ops->dump &&
6056 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
6057 reporter->auto_dump))
6058 goto reporter_nest_cancel;
6060 nla_nest_end(msg, reporter_attr);
6061 genlmsg_end(msg, hdr);
6064 reporter_nest_cancel:
6065 nla_nest_end(msg, reporter_attr);
6067 genlmsg_cancel(msg, hdr);
6071 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
6072 enum devlink_command cmd)
6074 struct sk_buff *msg;
6077 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6079 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6083 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
6084 reporter, cmd, 0, 0, 0);
6090 genlmsg_multicast_netns(&devlink_nl_family,
6091 devlink_net(reporter->devlink),
6092 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
6096 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
6098 reporter->recovery_count++;
6099 reporter->last_recovery_ts = jiffies;
6101 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
6104 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
6105 void *priv_ctx, struct netlink_ext_ack *extack)
6109 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
6112 if (!reporter->ops->recover)
6115 err = reporter->ops->recover(reporter, priv_ctx, extack);
6119 devlink_health_reporter_recovery_done(reporter);
6120 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
6121 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6127 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
6129 if (!reporter->dump_fmsg)
6131 devlink_fmsg_free(reporter->dump_fmsg);
6132 reporter->dump_fmsg = NULL;
6135 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
6137 struct netlink_ext_ack *extack)
6141 if (!reporter->ops->dump)
6144 if (reporter->dump_fmsg)
6147 reporter->dump_fmsg = devlink_fmsg_alloc();
6148 if (!reporter->dump_fmsg) {
6153 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
6157 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
6162 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
6166 reporter->dump_ts = jiffies;
6167 reporter->dump_real_ts = ktime_get_real_ns();
6172 devlink_health_dump_clear(reporter);
6176 int devlink_health_report(struct devlink_health_reporter *reporter,
6177 const char *msg, void *priv_ctx)
6179 enum devlink_health_reporter_state prev_health_state;
6180 struct devlink *devlink = reporter->devlink;
6181 unsigned long recover_ts_threshold;
6183 /* write a log message of the current error */
6185 trace_devlink_health_report(devlink, reporter->ops->name, msg);
6186 reporter->error_count++;
6187 prev_health_state = reporter->health_state;
6188 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6189 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6191 /* abort if the previous error wasn't recovered */
6192 recover_ts_threshold = reporter->last_recovery_ts +
6193 msecs_to_jiffies(reporter->graceful_period);
6194 if (reporter->auto_recover &&
6195 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
6196 (reporter->last_recovery_ts && reporter->recovery_count &&
6197 time_is_after_jiffies(recover_ts_threshold)))) {
6198 trace_devlink_health_recover_aborted(devlink,
6199 reporter->ops->name,
6200 reporter->health_state,
6202 reporter->last_recovery_ts);
6206 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6208 if (reporter->auto_dump) {
6209 mutex_lock(&reporter->dump_lock);
6210 /* store current dump of current error, for later analysis */
6211 devlink_health_do_dump(reporter, priv_ctx, NULL);
6212 mutex_unlock(&reporter->dump_lock);
6215 if (reporter->auto_recover)
6216 return devlink_health_reporter_recover(reporter,
6221 EXPORT_SYMBOL_GPL(devlink_health_report);
6223 static struct devlink_health_reporter *
6224 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
6225 struct nlattr **attrs)
6227 struct devlink_health_reporter *reporter;
6228 struct devlink_port *devlink_port;
6229 char *reporter_name;
6231 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
6234 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
6235 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
6236 if (IS_ERR(devlink_port)) {
6237 mutex_lock(&devlink->reporters_lock);
6238 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
6240 refcount_inc(&reporter->refcount);
6241 mutex_unlock(&devlink->reporters_lock);
6243 mutex_lock(&devlink_port->reporters_lock);
6244 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
6246 refcount_inc(&reporter->refcount);
6247 mutex_unlock(&devlink_port->reporters_lock);
6253 static struct devlink_health_reporter *
6254 devlink_health_reporter_get_from_info(struct devlink *devlink,
6255 struct genl_info *info)
6257 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
6260 static struct devlink_health_reporter *
6261 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
6263 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6264 struct devlink_health_reporter *reporter;
6265 struct nlattr **attrs = info->attrs;
6266 struct devlink *devlink;
6268 mutex_lock(&devlink_mutex);
6269 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6270 if (IS_ERR(devlink))
6273 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
6274 mutex_unlock(&devlink_mutex);
6277 mutex_unlock(&devlink_mutex);
6282 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
6283 enum devlink_health_reporter_state state)
6285 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
6286 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
6289 if (reporter->health_state == state)
6292 reporter->health_state = state;
6293 trace_devlink_health_reporter_state_update(reporter->devlink,
6294 reporter->ops->name, state);
6295 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6297 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
6299 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
6300 struct genl_info *info)
6302 struct devlink *devlink = info->user_ptr[0];
6303 struct devlink_health_reporter *reporter;
6304 struct sk_buff *msg;
6307 reporter = devlink_health_reporter_get_from_info(devlink, info);
6311 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6317 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6318 DEVLINK_CMD_HEALTH_REPORTER_GET,
6319 info->snd_portid, info->snd_seq,
6326 err = genlmsg_reply(msg, info);
6328 devlink_health_reporter_put(reporter);
6333 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
6334 struct netlink_callback *cb)
6336 struct devlink_health_reporter *reporter;
6337 struct devlink_port *port;
6338 struct devlink *devlink;
6339 int start = cb->args[0];
6343 mutex_lock(&devlink_mutex);
6344 list_for_each_entry(devlink, &devlink_list, list) {
6345 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6347 mutex_lock(&devlink->reporters_lock);
6348 list_for_each_entry(reporter, &devlink->reporter_list,
6354 err = devlink_nl_health_reporter_fill(msg, devlink,
6356 DEVLINK_CMD_HEALTH_REPORTER_GET,
6357 NETLINK_CB(cb->skb).portid,
6361 mutex_unlock(&devlink->reporters_lock);
6366 mutex_unlock(&devlink->reporters_lock);
6369 list_for_each_entry(devlink, &devlink_list, list) {
6370 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6372 mutex_lock(&devlink->lock);
6373 list_for_each_entry(port, &devlink->port_list, list) {
6374 mutex_lock(&port->reporters_lock);
6375 list_for_each_entry(reporter, &port->reporter_list, list) {
6380 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6381 DEVLINK_CMD_HEALTH_REPORTER_GET,
6382 NETLINK_CB(cb->skb).portid,
6386 mutex_unlock(&port->reporters_lock);
6387 mutex_unlock(&devlink->lock);
6392 mutex_unlock(&port->reporters_lock);
6394 mutex_unlock(&devlink->lock);
6397 mutex_unlock(&devlink_mutex);
6404 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
6405 struct genl_info *info)
6407 struct devlink *devlink = info->user_ptr[0];
6408 struct devlink_health_reporter *reporter;
6411 reporter = devlink_health_reporter_get_from_info(devlink, info);
6415 if (!reporter->ops->recover &&
6416 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
6417 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
6421 if (!reporter->ops->dump &&
6422 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
6427 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
6428 reporter->graceful_period =
6429 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
6431 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
6432 reporter->auto_recover =
6433 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
6435 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
6436 reporter->auto_dump =
6437 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
6439 devlink_health_reporter_put(reporter);
6442 devlink_health_reporter_put(reporter);
6446 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
6447 struct genl_info *info)
6449 struct devlink *devlink = info->user_ptr[0];
6450 struct devlink_health_reporter *reporter;
6453 reporter = devlink_health_reporter_get_from_info(devlink, info);
6457 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
6459 devlink_health_reporter_put(reporter);
6463 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
6464 struct genl_info *info)
6466 struct devlink *devlink = info->user_ptr[0];
6467 struct devlink_health_reporter *reporter;
6468 struct devlink_fmsg *fmsg;
6471 reporter = devlink_health_reporter_get_from_info(devlink, info);
6475 if (!reporter->ops->diagnose) {
6476 devlink_health_reporter_put(reporter);
6480 fmsg = devlink_fmsg_alloc();
6482 devlink_health_reporter_put(reporter);
6486 err = devlink_fmsg_obj_nest_start(fmsg);
6490 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
6494 err = devlink_fmsg_obj_nest_end(fmsg);
6498 err = devlink_fmsg_snd(fmsg, info,
6499 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
6502 devlink_fmsg_free(fmsg);
6503 devlink_health_reporter_put(reporter);
6508 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
6509 struct netlink_callback *cb)
6511 struct devlink_health_reporter *reporter;
6512 u64 start = cb->args[0];
6515 reporter = devlink_health_reporter_get_from_cb(cb);
6519 if (!reporter->ops->dump) {
6523 mutex_lock(&reporter->dump_lock);
6525 err = devlink_health_do_dump(reporter, NULL, cb->extack);
6528 cb->args[1] = reporter->dump_ts;
6530 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
6531 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
6536 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
6537 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
6539 mutex_unlock(&reporter->dump_lock);
6541 devlink_health_reporter_put(reporter);
6546 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
6547 struct genl_info *info)
6549 struct devlink *devlink = info->user_ptr[0];
6550 struct devlink_health_reporter *reporter;
6552 reporter = devlink_health_reporter_get_from_info(devlink, info);
6556 if (!reporter->ops->dump) {
6557 devlink_health_reporter_put(reporter);
6561 mutex_lock(&reporter->dump_lock);
6562 devlink_health_dump_clear(reporter);
6563 mutex_unlock(&reporter->dump_lock);
6564 devlink_health_reporter_put(reporter);
6568 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
6569 struct genl_info *info)
6571 struct devlink *devlink = info->user_ptr[0];
6572 struct devlink_health_reporter *reporter;
6575 reporter = devlink_health_reporter_get_from_info(devlink, info);
6579 if (!reporter->ops->test) {
6580 devlink_health_reporter_put(reporter);
6584 err = reporter->ops->test(reporter, info->extack);
6586 devlink_health_reporter_put(reporter);
6590 struct devlink_stats {
6593 struct u64_stats_sync syncp;
6597 * struct devlink_trap_policer_item - Packet trap policer attributes.
6598 * @policer: Immutable packet trap policer attributes.
6599 * @rate: Rate in packets / sec.
6600 * @burst: Burst size in packets.
6601 * @list: trap_policer_list member.
6603 * Describes packet trap policer attributes. Created by devlink during trap
6604 * policer registration.
6606 struct devlink_trap_policer_item {
6607 const struct devlink_trap_policer *policer;
6610 struct list_head list;
6614 * struct devlink_trap_group_item - Packet trap group attributes.
6615 * @group: Immutable packet trap group attributes.
6616 * @policer_item: Associated policer item. Can be NULL.
6617 * @list: trap_group_list member.
6618 * @stats: Trap group statistics.
6620 * Describes packet trap group attributes. Created by devlink during trap
6621 * group registration.
6623 struct devlink_trap_group_item {
6624 const struct devlink_trap_group *group;
6625 struct devlink_trap_policer_item *policer_item;
6626 struct list_head list;
6627 struct devlink_stats __percpu *stats;
6631 * struct devlink_trap_item - Packet trap attributes.
6632 * @trap: Immutable packet trap attributes.
6633 * @group_item: Associated group item.
6634 * @list: trap_list member.
6635 * @action: Trap action.
6636 * @stats: Trap statistics.
6637 * @priv: Driver private information.
6639 * Describes both mutable and immutable packet trap attributes. Created by
6640 * devlink during trap registration and used for all trap related operations.
6642 struct devlink_trap_item {
6643 const struct devlink_trap *trap;
6644 struct devlink_trap_group_item *group_item;
6645 struct list_head list;
6646 enum devlink_trap_action action;
6647 struct devlink_stats __percpu *stats;
6651 static struct devlink_trap_policer_item *
6652 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
6654 struct devlink_trap_policer_item *policer_item;
6656 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
6657 if (policer_item->policer->id == id)
6658 return policer_item;
6664 static struct devlink_trap_item *
6665 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
6667 struct devlink_trap_item *trap_item;
6669 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6670 if (!strcmp(trap_item->trap->name, name))
6677 static struct devlink_trap_item *
6678 devlink_trap_item_get_from_info(struct devlink *devlink,
6679 struct genl_info *info)
6681 struct nlattr *attr;
6683 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
6685 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
6687 return devlink_trap_item_lookup(devlink, nla_data(attr));
6691 devlink_trap_action_get_from_info(struct genl_info *info,
6692 enum devlink_trap_action *p_trap_action)
6696 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
6698 case DEVLINK_TRAP_ACTION_DROP:
6699 case DEVLINK_TRAP_ACTION_TRAP:
6700 case DEVLINK_TRAP_ACTION_MIRROR:
6701 *p_trap_action = val;
6710 static int devlink_trap_metadata_put(struct sk_buff *msg,
6711 const struct devlink_trap *trap)
6713 struct nlattr *attr;
6715 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
6719 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
6720 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
6721 goto nla_put_failure;
6722 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
6723 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
6724 goto nla_put_failure;
6726 nla_nest_end(msg, attr);
6731 nla_nest_cancel(msg, attr);
6735 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
6736 struct devlink_stats *stats)
6740 memset(stats, 0, sizeof(*stats));
6741 for_each_possible_cpu(i) {
6742 struct devlink_stats *cpu_stats;
6743 u64 rx_packets, rx_bytes;
6746 cpu_stats = per_cpu_ptr(trap_stats, i);
6748 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6749 rx_packets = cpu_stats->rx_packets;
6750 rx_bytes = cpu_stats->rx_bytes;
6751 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6753 stats->rx_packets += rx_packets;
6754 stats->rx_bytes += rx_bytes;
6758 static int devlink_trap_stats_put(struct sk_buff *msg,
6759 struct devlink_stats __percpu *trap_stats)
6761 struct devlink_stats stats;
6762 struct nlattr *attr;
6764 devlink_trap_stats_read(trap_stats, &stats);
6766 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6770 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
6771 stats.rx_packets, DEVLINK_ATTR_PAD))
6772 goto nla_put_failure;
6774 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
6775 stats.rx_bytes, DEVLINK_ATTR_PAD))
6776 goto nla_put_failure;
6778 nla_nest_end(msg, attr);
6783 nla_nest_cancel(msg, attr);
6787 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
6788 const struct devlink_trap_item *trap_item,
6789 enum devlink_command cmd, u32 portid, u32 seq,
6792 struct devlink_trap_group_item *group_item = trap_item->group_item;
6796 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6800 if (devlink_nl_put_handle(msg, devlink))
6801 goto nla_put_failure;
6803 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6804 group_item->group->name))
6805 goto nla_put_failure;
6807 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
6808 goto nla_put_failure;
6810 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
6811 goto nla_put_failure;
6813 if (trap_item->trap->generic &&
6814 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6815 goto nla_put_failure;
6817 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
6818 goto nla_put_failure;
6820 err = devlink_trap_metadata_put(msg, trap_item->trap);
6822 goto nla_put_failure;
6824 err = devlink_trap_stats_put(msg, trap_item->stats);
6826 goto nla_put_failure;
6828 genlmsg_end(msg, hdr);
6833 genlmsg_cancel(msg, hdr);
6837 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
6838 struct genl_info *info)
6840 struct netlink_ext_ack *extack = info->extack;
6841 struct devlink *devlink = info->user_ptr[0];
6842 struct devlink_trap_item *trap_item;
6843 struct sk_buff *msg;
6846 if (list_empty(&devlink->trap_list))
6849 trap_item = devlink_trap_item_get_from_info(devlink, info);
6851 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6855 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6859 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6860 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
6865 return genlmsg_reply(msg, info);
6872 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
6873 struct netlink_callback *cb)
6875 struct devlink_trap_item *trap_item;
6876 struct devlink *devlink;
6877 int start = cb->args[0];
6881 mutex_lock(&devlink_mutex);
6882 list_for_each_entry(devlink, &devlink_list, list) {
6883 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6885 mutex_lock(&devlink->lock);
6886 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6891 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6892 DEVLINK_CMD_TRAP_NEW,
6893 NETLINK_CB(cb->skb).portid,
6897 mutex_unlock(&devlink->lock);
6902 mutex_unlock(&devlink->lock);
6905 mutex_unlock(&devlink_mutex);
6911 static int __devlink_trap_action_set(struct devlink *devlink,
6912 struct devlink_trap_item *trap_item,
6913 enum devlink_trap_action trap_action,
6914 struct netlink_ext_ack *extack)
6918 if (trap_item->action != trap_action &&
6919 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
6920 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
6924 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
6925 trap_action, extack);
6929 trap_item->action = trap_action;
6934 static int devlink_trap_action_set(struct devlink *devlink,
6935 struct devlink_trap_item *trap_item,
6936 struct genl_info *info)
6938 enum devlink_trap_action trap_action;
6941 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6944 err = devlink_trap_action_get_from_info(info, &trap_action);
6946 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6950 return __devlink_trap_action_set(devlink, trap_item, trap_action,
6954 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
6955 struct genl_info *info)
6957 struct netlink_ext_ack *extack = info->extack;
6958 struct devlink *devlink = info->user_ptr[0];
6959 struct devlink_trap_item *trap_item;
6962 if (list_empty(&devlink->trap_list))
6965 trap_item = devlink_trap_item_get_from_info(devlink, info);
6967 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6971 err = devlink_trap_action_set(devlink, trap_item, info);
6978 static struct devlink_trap_group_item *
6979 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
6981 struct devlink_trap_group_item *group_item;
6983 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6984 if (!strcmp(group_item->group->name, name))
6991 static struct devlink_trap_group_item *
6992 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
6994 struct devlink_trap_group_item *group_item;
6996 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6997 if (group_item->group->id == id)
7004 static struct devlink_trap_group_item *
7005 devlink_trap_group_item_get_from_info(struct devlink *devlink,
7006 struct genl_info *info)
7010 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
7012 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
7014 return devlink_trap_group_item_lookup(devlink, name);
7018 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
7019 const struct devlink_trap_group_item *group_item,
7020 enum devlink_command cmd, u32 portid, u32 seq,
7026 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7030 if (devlink_nl_put_handle(msg, devlink))
7031 goto nla_put_failure;
7033 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7034 group_item->group->name))
7035 goto nla_put_failure;
7037 if (group_item->group->generic &&
7038 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7039 goto nla_put_failure;
7041 if (group_item->policer_item &&
7042 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7043 group_item->policer_item->policer->id))
7044 goto nla_put_failure;
7046 err = devlink_trap_stats_put(msg, group_item->stats);
7048 goto nla_put_failure;
7050 genlmsg_end(msg, hdr);
7055 genlmsg_cancel(msg, hdr);
7059 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
7060 struct genl_info *info)
7062 struct netlink_ext_ack *extack = info->extack;
7063 struct devlink *devlink = info->user_ptr[0];
7064 struct devlink_trap_group_item *group_item;
7065 struct sk_buff *msg;
7068 if (list_empty(&devlink->trap_group_list))
7071 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7073 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7077 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7081 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
7082 DEVLINK_CMD_TRAP_GROUP_NEW,
7083 info->snd_portid, info->snd_seq, 0);
7085 goto err_trap_group_fill;
7087 return genlmsg_reply(msg, info);
7089 err_trap_group_fill:
7094 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
7095 struct netlink_callback *cb)
7097 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
7098 struct devlink_trap_group_item *group_item;
7099 u32 portid = NETLINK_CB(cb->skb).portid;
7100 struct devlink *devlink;
7101 int start = cb->args[0];
7105 mutex_lock(&devlink_mutex);
7106 list_for_each_entry(devlink, &devlink_list, list) {
7107 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7109 mutex_lock(&devlink->lock);
7110 list_for_each_entry(group_item, &devlink->trap_group_list,
7116 err = devlink_nl_trap_group_fill(msg, devlink,
7122 mutex_unlock(&devlink->lock);
7127 mutex_unlock(&devlink->lock);
7130 mutex_unlock(&devlink_mutex);
7137 __devlink_trap_group_action_set(struct devlink *devlink,
7138 struct devlink_trap_group_item *group_item,
7139 enum devlink_trap_action trap_action,
7140 struct netlink_ext_ack *extack)
7142 const char *group_name = group_item->group->name;
7143 struct devlink_trap_item *trap_item;
7146 if (devlink->ops->trap_group_action_set) {
7147 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
7148 trap_action, extack);
7152 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7153 if (strcmp(trap_item->group_item->group->name, group_name))
7155 if (trap_item->action != trap_action &&
7156 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
7158 trap_item->action = trap_action;
7164 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7165 if (strcmp(trap_item->group_item->group->name, group_name))
7167 err = __devlink_trap_action_set(devlink, trap_item,
7168 trap_action, extack);
7177 devlink_trap_group_action_set(struct devlink *devlink,
7178 struct devlink_trap_group_item *group_item,
7179 struct genl_info *info, bool *p_modified)
7181 enum devlink_trap_action trap_action;
7184 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
7187 err = devlink_trap_action_get_from_info(info, &trap_action);
7189 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
7193 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
7203 static int devlink_trap_group_set(struct devlink *devlink,
7204 struct devlink_trap_group_item *group_item,
7205 struct genl_info *info)
7207 struct devlink_trap_policer_item *policer_item;
7208 struct netlink_ext_ack *extack = info->extack;
7209 const struct devlink_trap_policer *policer;
7210 struct nlattr **attrs = info->attrs;
7213 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7216 if (!devlink->ops->trap_group_set)
7219 policer_item = group_item->policer_item;
7220 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
7223 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7224 policer_item = devlink_trap_policer_item_lookup(devlink,
7226 if (policer_id && !policer_item) {
7227 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7231 policer = policer_item ? policer_item->policer : NULL;
7233 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
7238 group_item->policer_item = policer_item;
7243 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
7244 struct genl_info *info)
7246 struct netlink_ext_ack *extack = info->extack;
7247 struct devlink *devlink = info->user_ptr[0];
7248 struct devlink_trap_group_item *group_item;
7249 bool modified = false;
7252 if (list_empty(&devlink->trap_group_list))
7255 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7257 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7261 err = devlink_trap_group_action_set(devlink, group_item, info,
7266 err = devlink_trap_group_set(devlink, group_item, info);
7268 goto err_trap_group_set;
7274 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
7278 static struct devlink_trap_policer_item *
7279 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
7280 struct genl_info *info)
7284 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7286 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7288 return devlink_trap_policer_item_lookup(devlink, id);
7292 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
7293 const struct devlink_trap_policer *policer)
7295 struct nlattr *attr;
7299 if (!devlink->ops->trap_policer_counter_get)
7302 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
7306 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7310 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
7312 goto nla_put_failure;
7314 nla_nest_end(msg, attr);
7319 nla_nest_cancel(msg, attr);
7324 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
7325 const struct devlink_trap_policer_item *policer_item,
7326 enum devlink_command cmd, u32 portid, u32 seq,
7332 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7336 if (devlink_nl_put_handle(msg, devlink))
7337 goto nla_put_failure;
7339 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7340 policer_item->policer->id))
7341 goto nla_put_failure;
7343 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
7344 policer_item->rate, DEVLINK_ATTR_PAD))
7345 goto nla_put_failure;
7347 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
7348 policer_item->burst, DEVLINK_ATTR_PAD))
7349 goto nla_put_failure;
7351 err = devlink_trap_policer_stats_put(msg, devlink,
7352 policer_item->policer);
7354 goto nla_put_failure;
7356 genlmsg_end(msg, hdr);
7361 genlmsg_cancel(msg, hdr);
7365 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
7366 struct genl_info *info)
7368 struct devlink_trap_policer_item *policer_item;
7369 struct netlink_ext_ack *extack = info->extack;
7370 struct devlink *devlink = info->user_ptr[0];
7371 struct sk_buff *msg;
7374 if (list_empty(&devlink->trap_policer_list))
7377 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7378 if (!policer_item) {
7379 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7383 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7387 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
7388 DEVLINK_CMD_TRAP_POLICER_NEW,
7389 info->snd_portid, info->snd_seq, 0);
7391 goto err_trap_policer_fill;
7393 return genlmsg_reply(msg, info);
7395 err_trap_policer_fill:
7400 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
7401 struct netlink_callback *cb)
7403 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
7404 struct devlink_trap_policer_item *policer_item;
7405 u32 portid = NETLINK_CB(cb->skb).portid;
7406 struct devlink *devlink;
7407 int start = cb->args[0];
7411 mutex_lock(&devlink_mutex);
7412 list_for_each_entry(devlink, &devlink_list, list) {
7413 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7415 mutex_lock(&devlink->lock);
7416 list_for_each_entry(policer_item, &devlink->trap_policer_list,
7422 err = devlink_nl_trap_policer_fill(msg, devlink,
7428 mutex_unlock(&devlink->lock);
7433 mutex_unlock(&devlink->lock);
7436 mutex_unlock(&devlink_mutex);
7443 devlink_trap_policer_set(struct devlink *devlink,
7444 struct devlink_trap_policer_item *policer_item,
7445 struct genl_info *info)
7447 struct netlink_ext_ack *extack = info->extack;
7448 struct nlattr **attrs = info->attrs;
7452 rate = policer_item->rate;
7453 burst = policer_item->burst;
7455 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
7456 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
7458 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
7459 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
7461 if (rate < policer_item->policer->min_rate) {
7462 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
7466 if (rate > policer_item->policer->max_rate) {
7467 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
7471 if (burst < policer_item->policer->min_burst) {
7472 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
7476 if (burst > policer_item->policer->max_burst) {
7477 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
7481 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
7482 rate, burst, info->extack);
7486 policer_item->rate = rate;
7487 policer_item->burst = burst;
7492 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
7493 struct genl_info *info)
7495 struct devlink_trap_policer_item *policer_item;
7496 struct netlink_ext_ack *extack = info->extack;
7497 struct devlink *devlink = info->user_ptr[0];
7499 if (list_empty(&devlink->trap_policer_list))
7502 if (!devlink->ops->trap_policer_set)
7505 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7506 if (!policer_item) {
7507 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7511 return devlink_trap_policer_set(devlink, policer_item, info);
7514 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
7515 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
7516 DEVLINK_ATTR_TRAP_POLICER_ID },
7517 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
7518 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
7519 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
7520 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
7521 DEVLINK_PORT_TYPE_IB),
7522 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
7523 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
7524 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
7525 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
7526 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
7527 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
7528 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
7529 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
7530 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
7531 DEVLINK_ESWITCH_MODE_SWITCHDEV),
7532 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
7533 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
7534 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
7535 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
7536 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
7537 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
7538 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
7539 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
7540 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
7541 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
7542 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
7543 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
7544 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
7545 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
7546 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
7547 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
7548 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
7549 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
7550 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
7551 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
7552 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
7553 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
7554 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
7555 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
7556 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
7557 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
7558 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
7559 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
7560 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
7561 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
7562 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
7563 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
7564 DEVLINK_RELOAD_ACTION_MAX),
7565 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
7568 static const struct genl_small_ops devlink_nl_ops[] = {
7570 .cmd = DEVLINK_CMD_GET,
7571 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7572 .doit = devlink_nl_cmd_get_doit,
7573 .dumpit = devlink_nl_cmd_get_dumpit,
7574 /* can be retrieved by unprivileged users */
7577 .cmd = DEVLINK_CMD_PORT_GET,
7578 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7579 .doit = devlink_nl_cmd_port_get_doit,
7580 .dumpit = devlink_nl_cmd_port_get_dumpit,
7581 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7582 /* can be retrieved by unprivileged users */
7585 .cmd = DEVLINK_CMD_PORT_SET,
7586 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7587 .doit = devlink_nl_cmd_port_set_doit,
7588 .flags = GENL_ADMIN_PERM,
7589 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7592 .cmd = DEVLINK_CMD_PORT_SPLIT,
7593 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7594 .doit = devlink_nl_cmd_port_split_doit,
7595 .flags = GENL_ADMIN_PERM,
7596 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7599 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
7600 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7601 .doit = devlink_nl_cmd_port_unsplit_doit,
7602 .flags = GENL_ADMIN_PERM,
7603 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7606 .cmd = DEVLINK_CMD_SB_GET,
7607 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7608 .doit = devlink_nl_cmd_sb_get_doit,
7609 .dumpit = devlink_nl_cmd_sb_get_dumpit,
7610 /* can be retrieved by unprivileged users */
7613 .cmd = DEVLINK_CMD_SB_POOL_GET,
7614 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7615 .doit = devlink_nl_cmd_sb_pool_get_doit,
7616 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
7617 /* can be retrieved by unprivileged users */
7620 .cmd = DEVLINK_CMD_SB_POOL_SET,
7621 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7622 .doit = devlink_nl_cmd_sb_pool_set_doit,
7623 .flags = GENL_ADMIN_PERM,
7626 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
7627 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7628 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
7629 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
7630 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7631 /* can be retrieved by unprivileged users */
7634 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
7635 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7636 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
7637 .flags = GENL_ADMIN_PERM,
7638 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7641 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
7642 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7643 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
7644 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
7645 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7646 /* can be retrieved by unprivileged users */
7649 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
7650 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7651 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
7652 .flags = GENL_ADMIN_PERM,
7653 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7656 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
7657 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7658 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
7659 .flags = GENL_ADMIN_PERM,
7662 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
7663 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7664 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
7665 .flags = GENL_ADMIN_PERM,
7668 .cmd = DEVLINK_CMD_ESWITCH_GET,
7669 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7670 .doit = devlink_nl_cmd_eswitch_get_doit,
7671 .flags = GENL_ADMIN_PERM,
7672 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7675 .cmd = DEVLINK_CMD_ESWITCH_SET,
7676 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7677 .doit = devlink_nl_cmd_eswitch_set_doit,
7678 .flags = GENL_ADMIN_PERM,
7679 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7682 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
7683 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7684 .doit = devlink_nl_cmd_dpipe_table_get,
7685 /* can be retrieved by unprivileged users */
7688 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
7689 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7690 .doit = devlink_nl_cmd_dpipe_entries_get,
7691 /* can be retrieved by unprivileged users */
7694 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
7695 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7696 .doit = devlink_nl_cmd_dpipe_headers_get,
7697 /* can be retrieved by unprivileged users */
7700 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
7701 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7702 .doit = devlink_nl_cmd_dpipe_table_counters_set,
7703 .flags = GENL_ADMIN_PERM,
7706 .cmd = DEVLINK_CMD_RESOURCE_SET,
7707 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7708 .doit = devlink_nl_cmd_resource_set,
7709 .flags = GENL_ADMIN_PERM,
7712 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
7713 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7714 .doit = devlink_nl_cmd_resource_dump,
7715 /* can be retrieved by unprivileged users */
7718 .cmd = DEVLINK_CMD_RELOAD,
7719 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7720 .doit = devlink_nl_cmd_reload,
7721 .flags = GENL_ADMIN_PERM,
7722 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7725 .cmd = DEVLINK_CMD_PARAM_GET,
7726 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7727 .doit = devlink_nl_cmd_param_get_doit,
7728 .dumpit = devlink_nl_cmd_param_get_dumpit,
7729 /* can be retrieved by unprivileged users */
7732 .cmd = DEVLINK_CMD_PARAM_SET,
7733 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7734 .doit = devlink_nl_cmd_param_set_doit,
7735 .flags = GENL_ADMIN_PERM,
7738 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
7739 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7740 .doit = devlink_nl_cmd_port_param_get_doit,
7741 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
7742 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7743 /* can be retrieved by unprivileged users */
7746 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
7747 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7748 .doit = devlink_nl_cmd_port_param_set_doit,
7749 .flags = GENL_ADMIN_PERM,
7750 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7753 .cmd = DEVLINK_CMD_REGION_GET,
7754 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7755 .doit = devlink_nl_cmd_region_get_doit,
7756 .dumpit = devlink_nl_cmd_region_get_dumpit,
7757 .flags = GENL_ADMIN_PERM,
7760 .cmd = DEVLINK_CMD_REGION_NEW,
7761 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7762 .doit = devlink_nl_cmd_region_new,
7763 .flags = GENL_ADMIN_PERM,
7766 .cmd = DEVLINK_CMD_REGION_DEL,
7767 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7768 .doit = devlink_nl_cmd_region_del,
7769 .flags = GENL_ADMIN_PERM,
7772 .cmd = DEVLINK_CMD_REGION_READ,
7773 .validate = GENL_DONT_VALIDATE_STRICT |
7774 GENL_DONT_VALIDATE_DUMP_STRICT,
7775 .dumpit = devlink_nl_cmd_region_read_dumpit,
7776 .flags = GENL_ADMIN_PERM,
7779 .cmd = DEVLINK_CMD_INFO_GET,
7780 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7781 .doit = devlink_nl_cmd_info_get_doit,
7782 .dumpit = devlink_nl_cmd_info_get_dumpit,
7783 /* can be retrieved by unprivileged users */
7786 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
7787 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7788 .doit = devlink_nl_cmd_health_reporter_get_doit,
7789 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
7790 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7791 DEVLINK_NL_FLAG_NO_LOCK,
7792 /* can be retrieved by unprivileged users */
7795 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
7796 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7797 .doit = devlink_nl_cmd_health_reporter_set_doit,
7798 .flags = GENL_ADMIN_PERM,
7799 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7800 DEVLINK_NL_FLAG_NO_LOCK,
7803 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
7804 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7805 .doit = devlink_nl_cmd_health_reporter_recover_doit,
7806 .flags = GENL_ADMIN_PERM,
7807 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7808 DEVLINK_NL_FLAG_NO_LOCK,
7811 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
7812 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7813 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
7814 .flags = GENL_ADMIN_PERM,
7815 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7816 DEVLINK_NL_FLAG_NO_LOCK,
7819 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
7820 .validate = GENL_DONT_VALIDATE_STRICT |
7821 GENL_DONT_VALIDATE_DUMP_STRICT,
7822 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
7823 .flags = GENL_ADMIN_PERM,
7824 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7825 DEVLINK_NL_FLAG_NO_LOCK,
7828 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
7829 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7830 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
7831 .flags = GENL_ADMIN_PERM,
7832 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7833 DEVLINK_NL_FLAG_NO_LOCK,
7836 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
7837 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7838 .doit = devlink_nl_cmd_health_reporter_test_doit,
7839 .flags = GENL_ADMIN_PERM,
7840 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7841 DEVLINK_NL_FLAG_NO_LOCK,
7844 .cmd = DEVLINK_CMD_FLASH_UPDATE,
7845 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7846 .doit = devlink_nl_cmd_flash_update,
7847 .flags = GENL_ADMIN_PERM,
7850 .cmd = DEVLINK_CMD_TRAP_GET,
7851 .doit = devlink_nl_cmd_trap_get_doit,
7852 .dumpit = devlink_nl_cmd_trap_get_dumpit,
7853 /* can be retrieved by unprivileged users */
7856 .cmd = DEVLINK_CMD_TRAP_SET,
7857 .doit = devlink_nl_cmd_trap_set_doit,
7858 .flags = GENL_ADMIN_PERM,
7861 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
7862 .doit = devlink_nl_cmd_trap_group_get_doit,
7863 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
7864 /* can be retrieved by unprivileged users */
7867 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
7868 .doit = devlink_nl_cmd_trap_group_set_doit,
7869 .flags = GENL_ADMIN_PERM,
7872 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
7873 .doit = devlink_nl_cmd_trap_policer_get_doit,
7874 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
7875 /* can be retrieved by unprivileged users */
7878 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
7879 .doit = devlink_nl_cmd_trap_policer_set_doit,
7880 .flags = GENL_ADMIN_PERM,
7884 static struct genl_family devlink_nl_family __ro_after_init = {
7885 .name = DEVLINK_GENL_NAME,
7886 .version = DEVLINK_GENL_VERSION,
7887 .maxattr = DEVLINK_ATTR_MAX,
7888 .policy = devlink_nl_policy,
7890 .pre_doit = devlink_nl_pre_doit,
7891 .post_doit = devlink_nl_post_doit,
7892 .module = THIS_MODULE,
7893 .small_ops = devlink_nl_ops,
7894 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
7895 .mcgrps = devlink_nl_mcgrps,
7896 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
7899 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
7901 const struct devlink_reload_combination *comb;
7904 if (!devlink_reload_supported(ops)) {
7905 if (WARN_ON(ops->reload_actions))
7910 if (WARN_ON(!ops->reload_actions ||
7911 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
7912 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
7915 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
7916 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
7919 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
7920 comb = &devlink_reload_invalid_combinations[i];
7921 if (ops->reload_actions == BIT(comb->action) &&
7922 ops->reload_limits == BIT(comb->limit))
7929 * devlink_alloc - Allocate new devlink instance resources
7932 * @priv_size: size of user private data
7934 * Allocate new devlink instance resources, including devlink index
7937 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
7939 struct devlink *devlink;
7944 if (!devlink_reload_actions_valid(ops))
7947 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
7951 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
7952 __devlink_net_set(devlink, &init_net);
7953 INIT_LIST_HEAD(&devlink->port_list);
7954 INIT_LIST_HEAD(&devlink->sb_list);
7955 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
7956 INIT_LIST_HEAD(&devlink->resource_list);
7957 INIT_LIST_HEAD(&devlink->param_list);
7958 INIT_LIST_HEAD(&devlink->region_list);
7959 INIT_LIST_HEAD(&devlink->reporter_list);
7960 INIT_LIST_HEAD(&devlink->trap_list);
7961 INIT_LIST_HEAD(&devlink->trap_group_list);
7962 INIT_LIST_HEAD(&devlink->trap_policer_list);
7963 mutex_init(&devlink->lock);
7964 mutex_init(&devlink->reporters_lock);
7967 EXPORT_SYMBOL_GPL(devlink_alloc);
7970 * devlink_register - Register devlink instance
7973 * @dev: parent device
7975 int devlink_register(struct devlink *devlink, struct device *dev)
7978 devlink->registered = true;
7979 mutex_lock(&devlink_mutex);
7980 list_add_tail(&devlink->list, &devlink_list);
7981 devlink_notify(devlink, DEVLINK_CMD_NEW);
7982 mutex_unlock(&devlink_mutex);
7985 EXPORT_SYMBOL_GPL(devlink_register);
7988 * devlink_unregister - Unregister devlink instance
7992 void devlink_unregister(struct devlink *devlink)
7994 mutex_lock(&devlink_mutex);
7995 WARN_ON(devlink_reload_supported(devlink->ops) &&
7996 devlink->reload_enabled);
7997 devlink_notify(devlink, DEVLINK_CMD_DEL);
7998 list_del(&devlink->list);
7999 mutex_unlock(&devlink_mutex);
8001 EXPORT_SYMBOL_GPL(devlink_unregister);
8004 * devlink_reload_enable - Enable reload of devlink instance
8008 * Should be called at end of device initialization
8009 * process when reload operation is supported.
8011 void devlink_reload_enable(struct devlink *devlink)
8013 mutex_lock(&devlink_mutex);
8014 devlink->reload_enabled = true;
8015 mutex_unlock(&devlink_mutex);
8017 EXPORT_SYMBOL_GPL(devlink_reload_enable);
8020 * devlink_reload_disable - Disable reload of devlink instance
8024 * Should be called at the beginning of device cleanup
8025 * process when reload operation is supported.
8027 void devlink_reload_disable(struct devlink *devlink)
8029 mutex_lock(&devlink_mutex);
8030 /* Mutex is taken which ensures that no reload operation is in
8031 * progress while setting up forbidded flag.
8033 devlink->reload_enabled = false;
8034 mutex_unlock(&devlink_mutex);
8036 EXPORT_SYMBOL_GPL(devlink_reload_disable);
8039 * devlink_free - Free devlink instance resources
8043 void devlink_free(struct devlink *devlink)
8045 mutex_destroy(&devlink->reporters_lock);
8046 mutex_destroy(&devlink->lock);
8047 WARN_ON(!list_empty(&devlink->trap_policer_list));
8048 WARN_ON(!list_empty(&devlink->trap_group_list));
8049 WARN_ON(!list_empty(&devlink->trap_list));
8050 WARN_ON(!list_empty(&devlink->reporter_list));
8051 WARN_ON(!list_empty(&devlink->region_list));
8052 WARN_ON(!list_empty(&devlink->param_list));
8053 WARN_ON(!list_empty(&devlink->resource_list));
8054 WARN_ON(!list_empty(&devlink->dpipe_table_list));
8055 WARN_ON(!list_empty(&devlink->sb_list));
8056 WARN_ON(!list_empty(&devlink->port_list));
8058 xa_destroy(&devlink->snapshot_ids);
8062 EXPORT_SYMBOL_GPL(devlink_free);
8064 static void devlink_port_type_warn(struct work_struct *work)
8066 WARN(true, "Type was not set for devlink port.");
8069 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
8071 /* Ignore CPU and DSA flavours. */
8072 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
8073 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
8074 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
8077 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
8079 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
8081 if (!devlink_port_type_should_warn(devlink_port))
8083 /* Schedule a work to WARN in case driver does not set port
8084 * type within timeout.
8086 schedule_delayed_work(&devlink_port->type_warn_dw,
8087 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
8090 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
8092 if (!devlink_port_type_should_warn(devlink_port))
8094 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
8098 * devlink_port_register - Register devlink port
8101 * @devlink_port: devlink port
8102 * @port_index: driver-specific numerical identifier of the port
8104 * Register devlink port with provided port index. User can use
8105 * any indexing, even hw-related one. devlink_port structure
8106 * is convenient to be embedded inside user driver private structure.
8107 * Note that the caller should take care of zeroing the devlink_port
8110 int devlink_port_register(struct devlink *devlink,
8111 struct devlink_port *devlink_port,
8112 unsigned int port_index)
8114 mutex_lock(&devlink->lock);
8115 if (devlink_port_index_exists(devlink, port_index)) {
8116 mutex_unlock(&devlink->lock);
8119 devlink_port->devlink = devlink;
8120 devlink_port->index = port_index;
8121 devlink_port->registered = true;
8122 spin_lock_init(&devlink_port->type_lock);
8123 INIT_LIST_HEAD(&devlink_port->reporter_list);
8124 mutex_init(&devlink_port->reporters_lock);
8125 list_add_tail(&devlink_port->list, &devlink->port_list);
8126 INIT_LIST_HEAD(&devlink_port->param_list);
8127 INIT_LIST_HEAD(&devlink_port->region_list);
8128 mutex_unlock(&devlink->lock);
8129 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
8130 devlink_port_type_warn_schedule(devlink_port);
8131 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8134 EXPORT_SYMBOL_GPL(devlink_port_register);
8137 * devlink_port_unregister - Unregister devlink port
8139 * @devlink_port: devlink port
8141 void devlink_port_unregister(struct devlink_port *devlink_port)
8143 struct devlink *devlink = devlink_port->devlink;
8145 devlink_port_type_warn_cancel(devlink_port);
8146 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
8147 mutex_lock(&devlink->lock);
8148 list_del(&devlink_port->list);
8149 mutex_unlock(&devlink->lock);
8150 WARN_ON(!list_empty(&devlink_port->reporter_list));
8151 WARN_ON(!list_empty(&devlink_port->region_list));
8152 mutex_destroy(&devlink_port->reporters_lock);
8154 EXPORT_SYMBOL_GPL(devlink_port_unregister);
8156 static void __devlink_port_type_set(struct devlink_port *devlink_port,
8157 enum devlink_port_type type,
8160 if (WARN_ON(!devlink_port->registered))
8162 devlink_port_type_warn_cancel(devlink_port);
8163 spin_lock_bh(&devlink_port->type_lock);
8164 devlink_port->type = type;
8165 devlink_port->type_dev = type_dev;
8166 spin_unlock_bh(&devlink_port->type_lock);
8167 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8170 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
8171 struct net_device *netdev)
8173 const struct net_device_ops *ops = netdev->netdev_ops;
8175 /* If driver registers devlink port, it should set devlink port
8176 * attributes accordingly so the compat functions are called
8177 * and the original ops are not used.
8179 if (ops->ndo_get_phys_port_name) {
8180 /* Some drivers use the same set of ndos for netdevs
8181 * that have devlink_port registered and also for
8182 * those who don't. Make sure that ndo_get_phys_port_name
8183 * returns -EOPNOTSUPP here in case it is defined.
8186 char name[IFNAMSIZ];
8189 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
8190 WARN_ON(err != -EOPNOTSUPP);
8192 if (ops->ndo_get_port_parent_id) {
8193 /* Some drivers use the same set of ndos for netdevs
8194 * that have devlink_port registered and also for
8195 * those who don't. Make sure that ndo_get_port_parent_id
8196 * returns -EOPNOTSUPP here in case it is defined.
8199 struct netdev_phys_item_id ppid;
8202 err = ops->ndo_get_port_parent_id(netdev, &ppid);
8203 WARN_ON(err != -EOPNOTSUPP);
8208 * devlink_port_type_eth_set - Set port type to Ethernet
8210 * @devlink_port: devlink port
8211 * @netdev: related netdevice
8213 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
8214 struct net_device *netdev)
8217 devlink_port_type_netdev_checks(devlink_port, netdev);
8219 dev_warn(devlink_port->devlink->dev,
8220 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
8221 devlink_port->index);
8223 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
8225 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
8228 * devlink_port_type_ib_set - Set port type to InfiniBand
8230 * @devlink_port: devlink port
8231 * @ibdev: related IB device
8233 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
8234 struct ib_device *ibdev)
8236 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
8238 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
8241 * devlink_port_type_clear - Clear port type
8243 * @devlink_port: devlink port
8245 void devlink_port_type_clear(struct devlink_port *devlink_port)
8247 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
8248 devlink_port_type_warn_schedule(devlink_port);
8250 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
8252 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
8253 enum devlink_port_flavour flavour)
8255 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8257 if (WARN_ON(devlink_port->registered))
8259 devlink_port->attrs_set = true;
8260 attrs->flavour = flavour;
8261 if (attrs->switch_id.id_len) {
8262 devlink_port->switch_port = true;
8263 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
8264 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
8266 devlink_port->switch_port = false;
8272 * devlink_port_attrs_set - Set port attributes
8274 * @devlink_port: devlink port
8275 * @attrs: devlink port attrs
8277 void devlink_port_attrs_set(struct devlink_port *devlink_port,
8278 struct devlink_port_attrs *attrs)
8282 devlink_port->attrs = *attrs;
8283 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
8286 WARN_ON(attrs->splittable && attrs->split);
8288 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
8291 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
8293 * @devlink_port: devlink port
8294 * @controller: associated controller number for the devlink port instance
8295 * @pf: associated PF for the devlink port instance
8296 * @external: indicates if the port is for an external controller
8298 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
8299 u16 pf, bool external)
8301 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8304 ret = __devlink_port_attrs_set(devlink_port,
8305 DEVLINK_PORT_FLAVOUR_PCI_PF);
8308 attrs->pci_pf.controller = controller;
8309 attrs->pci_pf.pf = pf;
8310 attrs->pci_pf.external = external;
8312 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
8315 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
8317 * @devlink_port: devlink port
8318 * @controller: associated controller number for the devlink port instance
8319 * @pf: associated PF for the devlink port instance
8320 * @vf: associated VF of a PF for the devlink port instance
8321 * @external: indicates if the port is for an external controller
8323 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
8324 u16 pf, u16 vf, bool external)
8326 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8329 ret = __devlink_port_attrs_set(devlink_port,
8330 DEVLINK_PORT_FLAVOUR_PCI_VF);
8333 attrs->pci_vf.controller = controller;
8334 attrs->pci_vf.pf = pf;
8335 attrs->pci_vf.vf = vf;
8336 attrs->pci_vf.external = external;
8338 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
8340 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
8341 char *name, size_t len)
8343 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8346 if (!devlink_port->attrs_set)
8349 switch (attrs->flavour) {
8350 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
8351 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
8353 n = snprintf(name, len, "p%u", attrs->phys.port_number);
8355 n = snprintf(name, len, "p%us%u",
8356 attrs->phys.port_number,
8357 attrs->phys.split_subport_number);
8359 case DEVLINK_PORT_FLAVOUR_CPU:
8360 case DEVLINK_PORT_FLAVOUR_DSA:
8361 case DEVLINK_PORT_FLAVOUR_UNUSED:
8362 /* As CPU and DSA ports do not have a netdevice associated
8363 * case should not ever happen.
8367 case DEVLINK_PORT_FLAVOUR_PCI_PF:
8368 if (attrs->pci_pf.external) {
8369 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
8375 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
8377 case DEVLINK_PORT_FLAVOUR_PCI_VF:
8378 if (attrs->pci_vf.external) {
8379 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
8385 n = snprintf(name, len, "pf%uvf%u",
8386 attrs->pci_vf.pf, attrs->pci_vf.vf);
8396 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
8397 u32 size, u16 ingress_pools_count,
8398 u16 egress_pools_count, u16 ingress_tc_count,
8399 u16 egress_tc_count)
8401 struct devlink_sb *devlink_sb;
8404 mutex_lock(&devlink->lock);
8405 if (devlink_sb_index_exists(devlink, sb_index)) {
8410 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
8415 devlink_sb->index = sb_index;
8416 devlink_sb->size = size;
8417 devlink_sb->ingress_pools_count = ingress_pools_count;
8418 devlink_sb->egress_pools_count = egress_pools_count;
8419 devlink_sb->ingress_tc_count = ingress_tc_count;
8420 devlink_sb->egress_tc_count = egress_tc_count;
8421 list_add_tail(&devlink_sb->list, &devlink->sb_list);
8423 mutex_unlock(&devlink->lock);
8426 EXPORT_SYMBOL_GPL(devlink_sb_register);
8428 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
8430 struct devlink_sb *devlink_sb;
8432 mutex_lock(&devlink->lock);
8433 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
8434 WARN_ON(!devlink_sb);
8435 list_del(&devlink_sb->list);
8436 mutex_unlock(&devlink->lock);
8439 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
8442 * devlink_dpipe_headers_register - register dpipe headers
8445 * @dpipe_headers: dpipe header array
8447 * Register the headers supported by hardware.
8449 int devlink_dpipe_headers_register(struct devlink *devlink,
8450 struct devlink_dpipe_headers *dpipe_headers)
8452 mutex_lock(&devlink->lock);
8453 devlink->dpipe_headers = dpipe_headers;
8454 mutex_unlock(&devlink->lock);
8457 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
8460 * devlink_dpipe_headers_unregister - unregister dpipe headers
8464 * Unregister the headers supported by hardware.
8466 void devlink_dpipe_headers_unregister(struct devlink *devlink)
8468 mutex_lock(&devlink->lock);
8469 devlink->dpipe_headers = NULL;
8470 mutex_unlock(&devlink->lock);
8472 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
8475 * devlink_dpipe_table_counter_enabled - check if counter allocation
8478 * @table_name: tables name
8480 * Used by driver to check if counter allocation is required.
8481 * After counter allocation is turned on the table entries
8482 * are updated to include counter statistics.
8484 * After that point on the driver must respect the counter
8485 * state so that each entry added to the table is added
8488 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
8489 const char *table_name)
8491 struct devlink_dpipe_table *table;
8495 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8496 table_name, devlink);
8499 enabled = table->counters_enabled;
8503 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
8506 * devlink_dpipe_table_register - register dpipe table
8509 * @table_name: table name
8510 * @table_ops: table ops
8512 * @counter_control_extern: external control for counters
8514 int devlink_dpipe_table_register(struct devlink *devlink,
8515 const char *table_name,
8516 struct devlink_dpipe_table_ops *table_ops,
8517 void *priv, bool counter_control_extern)
8519 struct devlink_dpipe_table *table;
8522 if (WARN_ON(!table_ops->size_get))
8525 mutex_lock(&devlink->lock);
8527 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
8533 table = kzalloc(sizeof(*table), GFP_KERNEL);
8539 table->name = table_name;
8540 table->table_ops = table_ops;
8542 table->counter_control_extern = counter_control_extern;
8544 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
8546 mutex_unlock(&devlink->lock);
8549 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
8552 * devlink_dpipe_table_unregister - unregister dpipe table
8555 * @table_name: table name
8557 void devlink_dpipe_table_unregister(struct devlink *devlink,
8558 const char *table_name)
8560 struct devlink_dpipe_table *table;
8562 mutex_lock(&devlink->lock);
8563 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8564 table_name, devlink);
8567 list_del_rcu(&table->list);
8568 mutex_unlock(&devlink->lock);
8569 kfree_rcu(table, rcu);
8572 mutex_unlock(&devlink->lock);
8574 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
8577 * devlink_resource_register - devlink resource register
8580 * @resource_name: resource's name
8581 * @resource_size: resource's size
8582 * @resource_id: resource's id
8583 * @parent_resource_id: resource's parent id
8584 * @size_params: size parameters
8586 int devlink_resource_register(struct devlink *devlink,
8587 const char *resource_name,
8590 u64 parent_resource_id,
8591 const struct devlink_resource_size_params *size_params)
8593 struct devlink_resource *resource;
8594 struct list_head *resource_list;
8598 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
8600 mutex_lock(&devlink->lock);
8601 resource = devlink_resource_find(devlink, NULL, resource_id);
8607 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
8613 if (top_hierarchy) {
8614 resource_list = &devlink->resource_list;
8616 struct devlink_resource *parent_resource;
8618 parent_resource = devlink_resource_find(devlink, NULL,
8619 parent_resource_id);
8620 if (parent_resource) {
8621 resource_list = &parent_resource->resource_list;
8622 resource->parent = parent_resource;
8630 resource->name = resource_name;
8631 resource->size = resource_size;
8632 resource->size_new = resource_size;
8633 resource->id = resource_id;
8634 resource->size_valid = true;
8635 memcpy(&resource->size_params, size_params,
8636 sizeof(resource->size_params));
8637 INIT_LIST_HEAD(&resource->resource_list);
8638 list_add_tail(&resource->list, resource_list);
8640 mutex_unlock(&devlink->lock);
8643 EXPORT_SYMBOL_GPL(devlink_resource_register);
8646 * devlink_resources_unregister - free all resources
8649 * @resource: resource
8651 void devlink_resources_unregister(struct devlink *devlink,
8652 struct devlink_resource *resource)
8654 struct devlink_resource *tmp, *child_resource;
8655 struct list_head *resource_list;
8658 resource_list = &resource->resource_list;
8660 resource_list = &devlink->resource_list;
8663 mutex_lock(&devlink->lock);
8665 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
8666 devlink_resources_unregister(devlink, child_resource);
8667 list_del(&child_resource->list);
8668 kfree(child_resource);
8672 mutex_unlock(&devlink->lock);
8674 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
8677 * devlink_resource_size_get - get and update size
8680 * @resource_id: the requested resource id
8681 * @p_resource_size: ptr to update
8683 int devlink_resource_size_get(struct devlink *devlink,
8685 u64 *p_resource_size)
8687 struct devlink_resource *resource;
8690 mutex_lock(&devlink->lock);
8691 resource = devlink_resource_find(devlink, NULL, resource_id);
8696 *p_resource_size = resource->size_new;
8697 resource->size = resource->size_new;
8699 mutex_unlock(&devlink->lock);
8702 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
8705 * devlink_dpipe_table_resource_set - set the resource id
8708 * @table_name: table name
8709 * @resource_id: resource id
8710 * @resource_units: number of resource's units consumed per table's entry
8712 int devlink_dpipe_table_resource_set(struct devlink *devlink,
8713 const char *table_name, u64 resource_id,
8716 struct devlink_dpipe_table *table;
8719 mutex_lock(&devlink->lock);
8720 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8721 table_name, devlink);
8726 table->resource_id = resource_id;
8727 table->resource_units = resource_units;
8728 table->resource_valid = true;
8730 mutex_unlock(&devlink->lock);
8733 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
8736 * devlink_resource_occ_get_register - register occupancy getter
8739 * @resource_id: resource id
8740 * @occ_get: occupancy getter callback
8741 * @occ_get_priv: occupancy getter callback priv
8743 void devlink_resource_occ_get_register(struct devlink *devlink,
8745 devlink_resource_occ_get_t *occ_get,
8748 struct devlink_resource *resource;
8750 mutex_lock(&devlink->lock);
8751 resource = devlink_resource_find(devlink, NULL, resource_id);
8752 if (WARN_ON(!resource))
8754 WARN_ON(resource->occ_get);
8756 resource->occ_get = occ_get;
8757 resource->occ_get_priv = occ_get_priv;
8759 mutex_unlock(&devlink->lock);
8761 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
8764 * devlink_resource_occ_get_unregister - unregister occupancy getter
8767 * @resource_id: resource id
8769 void devlink_resource_occ_get_unregister(struct devlink *devlink,
8772 struct devlink_resource *resource;
8774 mutex_lock(&devlink->lock);
8775 resource = devlink_resource_find(devlink, NULL, resource_id);
8776 if (WARN_ON(!resource))
8778 WARN_ON(!resource->occ_get);
8780 resource->occ_get = NULL;
8781 resource->occ_get_priv = NULL;
8783 mutex_unlock(&devlink->lock);
8785 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
8787 static int devlink_param_verify(const struct devlink_param *param)
8789 if (!param || !param->name || !param->supported_cmodes)
8792 return devlink_param_generic_verify(param);
8794 return devlink_param_driver_verify(param);
8797 static int __devlink_params_register(struct devlink *devlink,
8798 unsigned int port_index,
8799 struct list_head *param_list,
8800 const struct devlink_param *params,
8801 size_t params_count,
8802 enum devlink_command reg_cmd,
8803 enum devlink_command unreg_cmd)
8805 const struct devlink_param *param = params;
8809 mutex_lock(&devlink->lock);
8810 for (i = 0; i < params_count; i++, param++) {
8811 err = devlink_param_verify(param);
8815 err = devlink_param_register_one(devlink, port_index,
8816 param_list, param, reg_cmd);
8821 mutex_unlock(&devlink->lock);
8827 for (param--; i > 0; i--, param--)
8828 devlink_param_unregister_one(devlink, port_index, param_list,
8831 mutex_unlock(&devlink->lock);
8835 static void __devlink_params_unregister(struct devlink *devlink,
8836 unsigned int port_index,
8837 struct list_head *param_list,
8838 const struct devlink_param *params,
8839 size_t params_count,
8840 enum devlink_command cmd)
8842 const struct devlink_param *param = params;
8845 mutex_lock(&devlink->lock);
8846 for (i = 0; i < params_count; i++, param++)
8847 devlink_param_unregister_one(devlink, 0, param_list, param,
8849 mutex_unlock(&devlink->lock);
8853 * devlink_params_register - register configuration parameters
8856 * @params: configuration parameters array
8857 * @params_count: number of parameters provided
8859 * Register the configuration parameters supported by the driver.
8861 int devlink_params_register(struct devlink *devlink,
8862 const struct devlink_param *params,
8863 size_t params_count)
8865 return __devlink_params_register(devlink, 0, &devlink->param_list,
8866 params, params_count,
8867 DEVLINK_CMD_PARAM_NEW,
8868 DEVLINK_CMD_PARAM_DEL);
8870 EXPORT_SYMBOL_GPL(devlink_params_register);
8873 * devlink_params_unregister - unregister configuration parameters
8875 * @params: configuration parameters to unregister
8876 * @params_count: number of parameters provided
8878 void devlink_params_unregister(struct devlink *devlink,
8879 const struct devlink_param *params,
8880 size_t params_count)
8882 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
8883 params, params_count,
8884 DEVLINK_CMD_PARAM_DEL);
8886 EXPORT_SYMBOL_GPL(devlink_params_unregister);
8889 * devlink_params_publish - publish configuration parameters
8893 * Publish previously registered configuration parameters.
8895 void devlink_params_publish(struct devlink *devlink)
8897 struct devlink_param_item *param_item;
8899 list_for_each_entry(param_item, &devlink->param_list, list) {
8900 if (param_item->published)
8902 param_item->published = true;
8903 devlink_param_notify(devlink, 0, param_item,
8904 DEVLINK_CMD_PARAM_NEW);
8907 EXPORT_SYMBOL_GPL(devlink_params_publish);
8910 * devlink_params_unpublish - unpublish configuration parameters
8914 * Unpublish previously registered configuration parameters.
8916 void devlink_params_unpublish(struct devlink *devlink)
8918 struct devlink_param_item *param_item;
8920 list_for_each_entry(param_item, &devlink->param_list, list) {
8921 if (!param_item->published)
8923 param_item->published = false;
8924 devlink_param_notify(devlink, 0, param_item,
8925 DEVLINK_CMD_PARAM_DEL);
8928 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
8931 * devlink_port_params_register - register port configuration parameters
8933 * @devlink_port: devlink port
8934 * @params: configuration parameters array
8935 * @params_count: number of parameters provided
8937 * Register the configuration parameters supported by the port.
8939 int devlink_port_params_register(struct devlink_port *devlink_port,
8940 const struct devlink_param *params,
8941 size_t params_count)
8943 return __devlink_params_register(devlink_port->devlink,
8944 devlink_port->index,
8945 &devlink_port->param_list, params,
8947 DEVLINK_CMD_PORT_PARAM_NEW,
8948 DEVLINK_CMD_PORT_PARAM_DEL);
8950 EXPORT_SYMBOL_GPL(devlink_port_params_register);
8953 * devlink_port_params_unregister - unregister port configuration
8956 * @devlink_port: devlink port
8957 * @params: configuration parameters array
8958 * @params_count: number of parameters provided
8960 void devlink_port_params_unregister(struct devlink_port *devlink_port,
8961 const struct devlink_param *params,
8962 size_t params_count)
8964 return __devlink_params_unregister(devlink_port->devlink,
8965 devlink_port->index,
8966 &devlink_port->param_list,
8967 params, params_count,
8968 DEVLINK_CMD_PORT_PARAM_DEL);
8970 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
8973 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
8974 union devlink_param_value *init_val)
8976 struct devlink_param_item *param_item;
8978 param_item = devlink_param_find_by_id(param_list, param_id);
8982 if (!param_item->driverinit_value_valid ||
8983 !devlink_param_cmode_is_supported(param_item->param,
8984 DEVLINK_PARAM_CMODE_DRIVERINIT))
8987 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8988 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
8990 *init_val = param_item->driverinit_value;
8996 __devlink_param_driverinit_value_set(struct devlink *devlink,
8997 unsigned int port_index,
8998 struct list_head *param_list, u32 param_id,
8999 union devlink_param_value init_val,
9000 enum devlink_command cmd)
9002 struct devlink_param_item *param_item;
9004 param_item = devlink_param_find_by_id(param_list, param_id);
9008 if (!devlink_param_cmode_is_supported(param_item->param,
9009 DEVLINK_PARAM_CMODE_DRIVERINIT))
9012 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
9013 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
9015 param_item->driverinit_value = init_val;
9016 param_item->driverinit_value_valid = true;
9018 devlink_param_notify(devlink, port_index, param_item, cmd);
9023 * devlink_param_driverinit_value_get - get configuration parameter
9024 * value for driver initializing
9027 * @param_id: parameter ID
9028 * @init_val: value of parameter in driverinit configuration mode
9030 * This function should be used by the driver to get driverinit
9031 * configuration for initialization after reload command.
9033 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
9034 union devlink_param_value *init_val)
9036 if (!devlink_reload_supported(devlink->ops))
9039 return __devlink_param_driverinit_value_get(&devlink->param_list,
9040 param_id, init_val);
9042 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
9045 * devlink_param_driverinit_value_set - set value of configuration
9046 * parameter for driverinit
9047 * configuration mode
9050 * @param_id: parameter ID
9051 * @init_val: value of parameter to set for driverinit configuration mode
9053 * This function should be used by the driver to set driverinit
9054 * configuration mode default value.
9056 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
9057 union devlink_param_value init_val)
9059 return __devlink_param_driverinit_value_set(devlink, 0,
9060 &devlink->param_list,
9062 DEVLINK_CMD_PARAM_NEW);
9064 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
9067 * devlink_port_param_driverinit_value_get - get configuration parameter
9068 * value for driver initializing
9070 * @devlink_port: devlink_port
9071 * @param_id: parameter ID
9072 * @init_val: value of parameter in driverinit configuration mode
9074 * This function should be used by the driver to get driverinit
9075 * configuration for initialization after reload command.
9077 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
9079 union devlink_param_value *init_val)
9081 struct devlink *devlink = devlink_port->devlink;
9083 if (!devlink_reload_supported(devlink->ops))
9086 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
9087 param_id, init_val);
9089 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
9092 * devlink_port_param_driverinit_value_set - set value of configuration
9093 * parameter for driverinit
9094 * configuration mode
9096 * @devlink_port: devlink_port
9097 * @param_id: parameter ID
9098 * @init_val: value of parameter to set for driverinit configuration mode
9100 * This function should be used by the driver to set driverinit
9101 * configuration mode default value.
9103 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
9105 union devlink_param_value init_val)
9107 return __devlink_param_driverinit_value_set(devlink_port->devlink,
9108 devlink_port->index,
9109 &devlink_port->param_list,
9111 DEVLINK_CMD_PORT_PARAM_NEW);
9113 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
9116 * devlink_param_value_changed - notify devlink on a parameter's value
9117 * change. Should be called by the driver
9118 * right after the change.
9121 * @param_id: parameter ID
9123 * This function should be used by the driver to notify devlink on value
9124 * change, excluding driverinit configuration mode.
9125 * For driverinit configuration mode driver should use the function
9127 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
9129 struct devlink_param_item *param_item;
9131 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
9132 WARN_ON(!param_item);
9134 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
9136 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
9139 * devlink_port_param_value_changed - notify devlink on a parameter's value
9140 * change. Should be called by the driver
9141 * right after the change.
9143 * @devlink_port: devlink_port
9144 * @param_id: parameter ID
9146 * This function should be used by the driver to notify devlink on value
9147 * change, excluding driverinit configuration mode.
9148 * For driverinit configuration mode driver should use the function
9149 * devlink_port_param_driverinit_value_set() instead.
9151 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
9154 struct devlink_param_item *param_item;
9156 param_item = devlink_param_find_by_id(&devlink_port->param_list,
9158 WARN_ON(!param_item);
9160 devlink_param_notify(devlink_port->devlink, devlink_port->index,
9161 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
9163 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
9166 * devlink_param_value_str_fill - Safely fill-up the string preventing
9167 * from overflow of the preallocated buffer
9169 * @dst_val: destination devlink_param_value
9170 * @src: source buffer
9172 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
9177 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
9178 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
9180 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
9183 * devlink_region_create - create a new address region
9186 * @ops: region operations and name
9187 * @region_max_snapshots: Maximum supported number of snapshots for region
9188 * @region_size: size of region
9190 struct devlink_region *
9191 devlink_region_create(struct devlink *devlink,
9192 const struct devlink_region_ops *ops,
9193 u32 region_max_snapshots, u64 region_size)
9195 struct devlink_region *region;
9198 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9199 return ERR_PTR(-EINVAL);
9201 mutex_lock(&devlink->lock);
9203 if (devlink_region_get_by_name(devlink, ops->name)) {
9208 region = kzalloc(sizeof(*region), GFP_KERNEL);
9214 region->devlink = devlink;
9215 region->max_snapshots = region_max_snapshots;
9217 region->size = region_size;
9218 INIT_LIST_HEAD(®ion->snapshot_list);
9219 list_add_tail(®ion->list, &devlink->region_list);
9220 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9222 mutex_unlock(&devlink->lock);
9226 mutex_unlock(&devlink->lock);
9227 return ERR_PTR(err);
9229 EXPORT_SYMBOL_GPL(devlink_region_create);
9232 * devlink_port_region_create - create a new address region for a port
9234 * @port: devlink port
9235 * @ops: region operations and name
9236 * @region_max_snapshots: Maximum supported number of snapshots for region
9237 * @region_size: size of region
9239 struct devlink_region *
9240 devlink_port_region_create(struct devlink_port *port,
9241 const struct devlink_port_region_ops *ops,
9242 u32 region_max_snapshots, u64 region_size)
9244 struct devlink *devlink = port->devlink;
9245 struct devlink_region *region;
9248 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9249 return ERR_PTR(-EINVAL);
9251 mutex_lock(&devlink->lock);
9253 if (devlink_port_region_get_by_name(port, ops->name)) {
9258 region = kzalloc(sizeof(*region), GFP_KERNEL);
9264 region->devlink = devlink;
9265 region->port = port;
9266 region->max_snapshots = region_max_snapshots;
9267 region->port_ops = ops;
9268 region->size = region_size;
9269 INIT_LIST_HEAD(®ion->snapshot_list);
9270 list_add_tail(®ion->list, &port->region_list);
9271 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9273 mutex_unlock(&devlink->lock);
9277 mutex_unlock(&devlink->lock);
9278 return ERR_PTR(err);
9280 EXPORT_SYMBOL_GPL(devlink_port_region_create);
9283 * devlink_region_destroy - destroy address region
9285 * @region: devlink region to destroy
9287 void devlink_region_destroy(struct devlink_region *region)
9289 struct devlink *devlink = region->devlink;
9290 struct devlink_snapshot *snapshot, *ts;
9292 mutex_lock(&devlink->lock);
9294 /* Free all snapshots of region */
9295 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
9296 devlink_region_snapshot_del(region, snapshot);
9298 list_del(®ion->list);
9300 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9301 mutex_unlock(&devlink->lock);
9304 EXPORT_SYMBOL_GPL(devlink_region_destroy);
9307 * devlink_region_snapshot_id_get - get snapshot ID
9309 * This callback should be called when adding a new snapshot,
9310 * Driver should use the same id for multiple snapshots taken
9311 * on multiple regions at the same time/by the same trigger.
9313 * The caller of this function must use devlink_region_snapshot_id_put
9314 * when finished creating regions using this id.
9316 * Returns zero on success, or a negative error code on failure.
9319 * @id: storage to return id
9321 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
9325 mutex_lock(&devlink->lock);
9326 err = __devlink_region_snapshot_id_get(devlink, id);
9327 mutex_unlock(&devlink->lock);
9331 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
9334 * devlink_region_snapshot_id_put - put snapshot ID reference
9336 * This should be called by a driver after finishing creating snapshots
9337 * with an id. Doing so ensures that the ID can later be released in the
9338 * event that all snapshots using it have been destroyed.
9341 * @id: id to release reference on
9343 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
9345 mutex_lock(&devlink->lock);
9346 __devlink_snapshot_id_decrement(devlink, id);
9347 mutex_unlock(&devlink->lock);
9349 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
9352 * devlink_region_snapshot_create - create a new snapshot
9353 * This will add a new snapshot of a region. The snapshot
9354 * will be stored on the region struct and can be accessed
9355 * from devlink. This is useful for future analyses of snapshots.
9356 * Multiple snapshots can be created on a region.
9357 * The @snapshot_id should be obtained using the getter function.
9359 * @region: devlink region of the snapshot
9360 * @data: snapshot data
9361 * @snapshot_id: snapshot id to be created
9363 int devlink_region_snapshot_create(struct devlink_region *region,
9364 u8 *data, u32 snapshot_id)
9366 struct devlink *devlink = region->devlink;
9369 mutex_lock(&devlink->lock);
9370 err = __devlink_region_snapshot_create(region, data, snapshot_id);
9371 mutex_unlock(&devlink->lock);
9375 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
9377 #define DEVLINK_TRAP(_id, _type) \
9379 .type = DEVLINK_TRAP_TYPE_##_type, \
9380 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
9381 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
9384 static const struct devlink_trap devlink_trap_generic[] = {
9385 DEVLINK_TRAP(SMAC_MC, DROP),
9386 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
9387 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
9388 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
9389 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
9390 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
9391 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
9392 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
9393 DEVLINK_TRAP(TAIL_DROP, DROP),
9394 DEVLINK_TRAP(NON_IP_PACKET, DROP),
9395 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
9396 DEVLINK_TRAP(DIP_LB, DROP),
9397 DEVLINK_TRAP(SIP_MC, DROP),
9398 DEVLINK_TRAP(SIP_LB, DROP),
9399 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
9400 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
9401 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
9402 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
9403 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
9404 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
9405 DEVLINK_TRAP(RPF, EXCEPTION),
9406 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
9407 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
9408 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
9409 DEVLINK_TRAP(NON_ROUTABLE, DROP),
9410 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
9411 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
9412 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
9413 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
9414 DEVLINK_TRAP(STP, CONTROL),
9415 DEVLINK_TRAP(LACP, CONTROL),
9416 DEVLINK_TRAP(LLDP, CONTROL),
9417 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
9418 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
9419 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
9420 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
9421 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
9422 DEVLINK_TRAP(MLD_QUERY, CONTROL),
9423 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
9424 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
9425 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
9426 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
9427 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
9428 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
9429 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
9430 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
9431 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
9432 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
9433 DEVLINK_TRAP(IPV4_BFD, CONTROL),
9434 DEVLINK_TRAP(IPV6_BFD, CONTROL),
9435 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
9436 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
9437 DEVLINK_TRAP(IPV4_BGP, CONTROL),
9438 DEVLINK_TRAP(IPV6_BGP, CONTROL),
9439 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
9440 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
9441 DEVLINK_TRAP(IPV4_PIM, CONTROL),
9442 DEVLINK_TRAP(IPV6_PIM, CONTROL),
9443 DEVLINK_TRAP(UC_LB, CONTROL),
9444 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
9445 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
9446 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
9447 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
9448 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
9449 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
9450 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
9451 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
9452 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
9453 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
9454 DEVLINK_TRAP(PTP_EVENT, CONTROL),
9455 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
9456 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
9457 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
9458 DEVLINK_TRAP(EARLY_DROP, DROP),
9459 DEVLINK_TRAP(VXLAN_PARSING, DROP),
9460 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
9461 DEVLINK_TRAP(VLAN_PARSING, DROP),
9462 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
9463 DEVLINK_TRAP(MPLS_PARSING, DROP),
9464 DEVLINK_TRAP(ARP_PARSING, DROP),
9465 DEVLINK_TRAP(IP_1_PARSING, DROP),
9466 DEVLINK_TRAP(IP_N_PARSING, DROP),
9467 DEVLINK_TRAP(GRE_PARSING, DROP),
9468 DEVLINK_TRAP(UDP_PARSING, DROP),
9469 DEVLINK_TRAP(TCP_PARSING, DROP),
9470 DEVLINK_TRAP(IPSEC_PARSING, DROP),
9471 DEVLINK_TRAP(SCTP_PARSING, DROP),
9472 DEVLINK_TRAP(DCCP_PARSING, DROP),
9473 DEVLINK_TRAP(GTP_PARSING, DROP),
9474 DEVLINK_TRAP(ESP_PARSING, DROP),
9477 #define DEVLINK_TRAP_GROUP(_id) \
9479 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
9480 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
9483 static const struct devlink_trap_group devlink_trap_group_generic[] = {
9484 DEVLINK_TRAP_GROUP(L2_DROPS),
9485 DEVLINK_TRAP_GROUP(L3_DROPS),
9486 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
9487 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
9488 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
9489 DEVLINK_TRAP_GROUP(ACL_DROPS),
9490 DEVLINK_TRAP_GROUP(STP),
9491 DEVLINK_TRAP_GROUP(LACP),
9492 DEVLINK_TRAP_GROUP(LLDP),
9493 DEVLINK_TRAP_GROUP(MC_SNOOPING),
9494 DEVLINK_TRAP_GROUP(DHCP),
9495 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
9496 DEVLINK_TRAP_GROUP(BFD),
9497 DEVLINK_TRAP_GROUP(OSPF),
9498 DEVLINK_TRAP_GROUP(BGP),
9499 DEVLINK_TRAP_GROUP(VRRP),
9500 DEVLINK_TRAP_GROUP(PIM),
9501 DEVLINK_TRAP_GROUP(UC_LB),
9502 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
9503 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
9504 DEVLINK_TRAP_GROUP(IPV6),
9505 DEVLINK_TRAP_GROUP(PTP_EVENT),
9506 DEVLINK_TRAP_GROUP(PTP_GENERAL),
9507 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
9508 DEVLINK_TRAP_GROUP(ACL_TRAP),
9509 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
9512 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
9514 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
9517 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
9520 if (trap->type != devlink_trap_generic[trap->id].type)
9526 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
9530 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
9533 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
9534 if (!strcmp(trap->name, devlink_trap_generic[i].name))
9541 static int devlink_trap_verify(const struct devlink_trap *trap)
9543 if (!trap || !trap->name)
9547 return devlink_trap_generic_verify(trap);
9549 return devlink_trap_driver_verify(trap);
9553 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
9555 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9558 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
9565 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
9569 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9572 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
9573 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
9580 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
9583 return devlink_trap_group_generic_verify(group);
9585 return devlink_trap_group_driver_verify(group);
9589 devlink_trap_group_notify(struct devlink *devlink,
9590 const struct devlink_trap_group_item *group_item,
9591 enum devlink_command cmd)
9593 struct sk_buff *msg;
9596 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
9597 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
9599 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9603 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
9610 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9611 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9615 devlink_trap_item_group_link(struct devlink *devlink,
9616 struct devlink_trap_item *trap_item)
9618 u16 group_id = trap_item->trap->init_group_id;
9619 struct devlink_trap_group_item *group_item;
9621 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
9622 if (WARN_ON_ONCE(!group_item))
9625 trap_item->group_item = group_item;
9630 static void devlink_trap_notify(struct devlink *devlink,
9631 const struct devlink_trap_item *trap_item,
9632 enum devlink_command cmd)
9634 struct sk_buff *msg;
9637 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
9638 cmd != DEVLINK_CMD_TRAP_DEL);
9640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9644 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
9650 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9651 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9655 devlink_trap_register(struct devlink *devlink,
9656 const struct devlink_trap *trap, void *priv)
9658 struct devlink_trap_item *trap_item;
9661 if (devlink_trap_item_lookup(devlink, trap->name))
9664 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
9668 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9669 if (!trap_item->stats) {
9671 goto err_stats_alloc;
9674 trap_item->trap = trap;
9675 trap_item->action = trap->init_action;
9676 trap_item->priv = priv;
9678 err = devlink_trap_item_group_link(devlink, trap_item);
9680 goto err_group_link;
9682 err = devlink->ops->trap_init(devlink, trap, trap_item);
9686 list_add_tail(&trap_item->list, &devlink->trap_list);
9687 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9693 free_percpu(trap_item->stats);
9699 static void devlink_trap_unregister(struct devlink *devlink,
9700 const struct devlink_trap *trap)
9702 struct devlink_trap_item *trap_item;
9704 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9705 if (WARN_ON_ONCE(!trap_item))
9708 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9709 list_del(&trap_item->list);
9710 if (devlink->ops->trap_fini)
9711 devlink->ops->trap_fini(devlink, trap, trap_item);
9712 free_percpu(trap_item->stats);
9716 static void devlink_trap_disable(struct devlink *devlink,
9717 const struct devlink_trap *trap)
9719 struct devlink_trap_item *trap_item;
9721 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9722 if (WARN_ON_ONCE(!trap_item))
9725 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
9727 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
9731 * devlink_traps_register - Register packet traps with devlink.
9732 * @devlink: devlink.
9733 * @traps: Packet traps.
9734 * @traps_count: Count of provided packet traps.
9735 * @priv: Driver private information.
9737 * Return: Non-zero value on failure.
9739 int devlink_traps_register(struct devlink *devlink,
9740 const struct devlink_trap *traps,
9741 size_t traps_count, void *priv)
9745 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
9748 mutex_lock(&devlink->lock);
9749 for (i = 0; i < traps_count; i++) {
9750 const struct devlink_trap *trap = &traps[i];
9752 err = devlink_trap_verify(trap);
9754 goto err_trap_verify;
9756 err = devlink_trap_register(devlink, trap, priv);
9758 goto err_trap_register;
9760 mutex_unlock(&devlink->lock);
9766 for (i--; i >= 0; i--)
9767 devlink_trap_unregister(devlink, &traps[i]);
9768 mutex_unlock(&devlink->lock);
9771 EXPORT_SYMBOL_GPL(devlink_traps_register);
9774 * devlink_traps_unregister - Unregister packet traps from devlink.
9775 * @devlink: devlink.
9776 * @traps: Packet traps.
9777 * @traps_count: Count of provided packet traps.
9779 void devlink_traps_unregister(struct devlink *devlink,
9780 const struct devlink_trap *traps,
9785 mutex_lock(&devlink->lock);
9786 /* Make sure we do not have any packets in-flight while unregistering
9787 * traps by disabling all of them and waiting for a grace period.
9789 for (i = traps_count - 1; i >= 0; i--)
9790 devlink_trap_disable(devlink, &traps[i]);
9792 for (i = traps_count - 1; i >= 0; i--)
9793 devlink_trap_unregister(devlink, &traps[i]);
9794 mutex_unlock(&devlink->lock);
9796 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
9799 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
9802 struct devlink_stats *stats;
9804 stats = this_cpu_ptr(trap_stats);
9805 u64_stats_update_begin(&stats->syncp);
9806 stats->rx_bytes += skb_len;
9807 stats->rx_packets++;
9808 u64_stats_update_end(&stats->syncp);
9812 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
9813 const struct devlink_trap_item *trap_item,
9814 struct devlink_port *in_devlink_port,
9815 const struct flow_action_cookie *fa_cookie)
9817 metadata->trap_name = trap_item->trap->name;
9818 metadata->trap_group_name = trap_item->group_item->group->name;
9819 metadata->fa_cookie = fa_cookie;
9820 metadata->trap_type = trap_item->trap->type;
9822 spin_lock(&in_devlink_port->type_lock);
9823 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9824 metadata->input_dev = in_devlink_port->type_dev;
9825 spin_unlock(&in_devlink_port->type_lock);
9829 * devlink_trap_report - Report trapped packet to drop monitor.
9830 * @devlink: devlink.
9831 * @skb: Trapped packet.
9832 * @trap_ctx: Trap context.
9833 * @in_devlink_port: Input devlink port.
9834 * @fa_cookie: Flow action cookie. Could be NULL.
9836 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
9837 void *trap_ctx, struct devlink_port *in_devlink_port,
9838 const struct flow_action_cookie *fa_cookie)
9841 struct devlink_trap_item *trap_item = trap_ctx;
9843 devlink_trap_stats_update(trap_item->stats, skb->len);
9844 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
9846 if (trace_devlink_trap_report_enabled()) {
9847 struct devlink_trap_metadata metadata = {};
9849 devlink_trap_report_metadata_set(&metadata, trap_item,
9850 in_devlink_port, fa_cookie);
9851 trace_devlink_trap_report(devlink, skb, &metadata);
9854 EXPORT_SYMBOL_GPL(devlink_trap_report);
9857 * devlink_trap_ctx_priv - Trap context to driver private information.
9858 * @trap_ctx: Trap context.
9860 * Return: Driver private information passed during registration.
9862 void *devlink_trap_ctx_priv(void *trap_ctx)
9864 struct devlink_trap_item *trap_item = trap_ctx;
9866 return trap_item->priv;
9868 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
9871 devlink_trap_group_item_policer_link(struct devlink *devlink,
9872 struct devlink_trap_group_item *group_item)
9874 u32 policer_id = group_item->group->init_policer_id;
9875 struct devlink_trap_policer_item *policer_item;
9877 if (policer_id == 0)
9880 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
9881 if (WARN_ON_ONCE(!policer_item))
9884 group_item->policer_item = policer_item;
9890 devlink_trap_group_register(struct devlink *devlink,
9891 const struct devlink_trap_group *group)
9893 struct devlink_trap_group_item *group_item;
9896 if (devlink_trap_group_item_lookup(devlink, group->name))
9899 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
9903 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9904 if (!group_item->stats) {
9906 goto err_stats_alloc;
9909 group_item->group = group;
9911 err = devlink_trap_group_item_policer_link(devlink, group_item);
9913 goto err_policer_link;
9915 if (devlink->ops->trap_group_init) {
9916 err = devlink->ops->trap_group_init(devlink, group);
9918 goto err_group_init;
9921 list_add_tail(&group_item->list, &devlink->trap_group_list);
9922 devlink_trap_group_notify(devlink, group_item,
9923 DEVLINK_CMD_TRAP_GROUP_NEW);
9929 free_percpu(group_item->stats);
9936 devlink_trap_group_unregister(struct devlink *devlink,
9937 const struct devlink_trap_group *group)
9939 struct devlink_trap_group_item *group_item;
9941 group_item = devlink_trap_group_item_lookup(devlink, group->name);
9942 if (WARN_ON_ONCE(!group_item))
9945 devlink_trap_group_notify(devlink, group_item,
9946 DEVLINK_CMD_TRAP_GROUP_DEL);
9947 list_del(&group_item->list);
9948 free_percpu(group_item->stats);
9953 * devlink_trap_groups_register - Register packet trap groups with devlink.
9954 * @devlink: devlink.
9955 * @groups: Packet trap groups.
9956 * @groups_count: Count of provided packet trap groups.
9958 * Return: Non-zero value on failure.
9960 int devlink_trap_groups_register(struct devlink *devlink,
9961 const struct devlink_trap_group *groups,
9962 size_t groups_count)
9966 mutex_lock(&devlink->lock);
9967 for (i = 0; i < groups_count; i++) {
9968 const struct devlink_trap_group *group = &groups[i];
9970 err = devlink_trap_group_verify(group);
9972 goto err_trap_group_verify;
9974 err = devlink_trap_group_register(devlink, group);
9976 goto err_trap_group_register;
9978 mutex_unlock(&devlink->lock);
9982 err_trap_group_register:
9983 err_trap_group_verify:
9984 for (i--; i >= 0; i--)
9985 devlink_trap_group_unregister(devlink, &groups[i]);
9986 mutex_unlock(&devlink->lock);
9989 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
9992 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
9993 * @devlink: devlink.
9994 * @groups: Packet trap groups.
9995 * @groups_count: Count of provided packet trap groups.
9997 void devlink_trap_groups_unregister(struct devlink *devlink,
9998 const struct devlink_trap_group *groups,
9999 size_t groups_count)
10003 mutex_lock(&devlink->lock);
10004 for (i = groups_count - 1; i >= 0; i--)
10005 devlink_trap_group_unregister(devlink, &groups[i]);
10006 mutex_unlock(&devlink->lock);
10008 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
10011 devlink_trap_policer_notify(struct devlink *devlink,
10012 const struct devlink_trap_policer_item *policer_item,
10013 enum devlink_command cmd)
10015 struct sk_buff *msg;
10018 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
10019 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
10021 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10025 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
10032 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10033 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10037 devlink_trap_policer_register(struct devlink *devlink,
10038 const struct devlink_trap_policer *policer)
10040 struct devlink_trap_policer_item *policer_item;
10043 if (devlink_trap_policer_item_lookup(devlink, policer->id))
10046 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
10050 policer_item->policer = policer;
10051 policer_item->rate = policer->init_rate;
10052 policer_item->burst = policer->init_burst;
10054 if (devlink->ops->trap_policer_init) {
10055 err = devlink->ops->trap_policer_init(devlink, policer);
10057 goto err_policer_init;
10060 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
10061 devlink_trap_policer_notify(devlink, policer_item,
10062 DEVLINK_CMD_TRAP_POLICER_NEW);
10067 kfree(policer_item);
10072 devlink_trap_policer_unregister(struct devlink *devlink,
10073 const struct devlink_trap_policer *policer)
10075 struct devlink_trap_policer_item *policer_item;
10077 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
10078 if (WARN_ON_ONCE(!policer_item))
10081 devlink_trap_policer_notify(devlink, policer_item,
10082 DEVLINK_CMD_TRAP_POLICER_DEL);
10083 list_del(&policer_item->list);
10084 if (devlink->ops->trap_policer_fini)
10085 devlink->ops->trap_policer_fini(devlink, policer);
10086 kfree(policer_item);
10090 * devlink_trap_policers_register - Register packet trap policers with devlink.
10091 * @devlink: devlink.
10092 * @policers: Packet trap policers.
10093 * @policers_count: Count of provided packet trap policers.
10095 * Return: Non-zero value on failure.
10098 devlink_trap_policers_register(struct devlink *devlink,
10099 const struct devlink_trap_policer *policers,
10100 size_t policers_count)
10104 mutex_lock(&devlink->lock);
10105 for (i = 0; i < policers_count; i++) {
10106 const struct devlink_trap_policer *policer = &policers[i];
10108 if (WARN_ON(policer->id == 0 ||
10109 policer->max_rate < policer->min_rate ||
10110 policer->max_burst < policer->min_burst)) {
10112 goto err_trap_policer_verify;
10115 err = devlink_trap_policer_register(devlink, policer);
10117 goto err_trap_policer_register;
10119 mutex_unlock(&devlink->lock);
10123 err_trap_policer_register:
10124 err_trap_policer_verify:
10125 for (i--; i >= 0; i--)
10126 devlink_trap_policer_unregister(devlink, &policers[i]);
10127 mutex_unlock(&devlink->lock);
10130 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
10133 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
10134 * @devlink: devlink.
10135 * @policers: Packet trap policers.
10136 * @policers_count: Count of provided packet trap policers.
10139 devlink_trap_policers_unregister(struct devlink *devlink,
10140 const struct devlink_trap_policer *policers,
10141 size_t policers_count)
10145 mutex_lock(&devlink->lock);
10146 for (i = policers_count - 1; i >= 0; i--)
10147 devlink_trap_policer_unregister(devlink, &policers[i]);
10148 mutex_unlock(&devlink->lock);
10150 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
10152 static void __devlink_compat_running_version(struct devlink *devlink,
10153 char *buf, size_t len)
10155 const struct nlattr *nlattr;
10156 struct devlink_info_req req;
10157 struct sk_buff *msg;
10160 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10165 err = devlink->ops->info_get(devlink, &req, NULL);
10169 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
10170 const struct nlattr *kv;
10173 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
10176 nla_for_each_nested(kv, nlattr, rem_kv) {
10177 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
10180 strlcat(buf, nla_data(kv), len);
10181 strlcat(buf, " ", len);
10188 void devlink_compat_running_version(struct net_device *dev,
10189 char *buf, size_t len)
10191 struct devlink *devlink;
10196 devlink = netdev_to_devlink(dev);
10197 if (!devlink || !devlink->ops->info_get)
10200 mutex_lock(&devlink->lock);
10201 __devlink_compat_running_version(devlink, buf, len);
10202 mutex_unlock(&devlink->lock);
10209 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
10211 struct devlink_flash_update_params params = {};
10212 struct devlink *devlink;
10218 devlink = netdev_to_devlink(dev);
10219 if (!devlink || !devlink->ops->flash_update) {
10224 params.file_name = file_name;
10226 mutex_lock(&devlink->lock);
10227 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
10228 mutex_unlock(&devlink->lock);
10237 int devlink_compat_phys_port_name_get(struct net_device *dev,
10238 char *name, size_t len)
10240 struct devlink_port *devlink_port;
10242 /* RTNL mutex is held here which ensures that devlink_port
10243 * instance cannot disappear in the middle. No need to take
10244 * any devlink lock as only permanent values are accessed.
10248 devlink_port = netdev_to_devlink_port(dev);
10250 return -EOPNOTSUPP;
10252 return __devlink_port_phys_port_name_get(devlink_port, name, len);
10255 int devlink_compat_switch_id_get(struct net_device *dev,
10256 struct netdev_phys_item_id *ppid)
10258 struct devlink_port *devlink_port;
10260 /* Caller must hold RTNL mutex or reference to dev, which ensures that
10261 * devlink_port instance cannot disappear in the middle. No need to take
10262 * any devlink lock as only permanent values are accessed.
10264 devlink_port = netdev_to_devlink_port(dev);
10265 if (!devlink_port || !devlink_port->switch_port)
10266 return -EOPNOTSUPP;
10268 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
10273 static void __net_exit devlink_pernet_pre_exit(struct net *net)
10275 struct devlink *devlink;
10276 u32 actions_performed;
10279 /* In case network namespace is getting destroyed, reload
10280 * all devlink instances from this namespace into init_net.
10282 mutex_lock(&devlink_mutex);
10283 list_for_each_entry(devlink, &devlink_list, list) {
10284 if (net_eq(devlink_net(devlink), net)) {
10285 if (WARN_ON(!devlink_reload_supported(devlink->ops)))
10287 err = devlink_reload(devlink, &init_net,
10288 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
10289 DEVLINK_RELOAD_LIMIT_UNSPEC,
10290 &actions_performed, NULL);
10291 if (err && err != -EOPNOTSUPP)
10292 pr_warn("Failed to reload devlink instance into init_net\n");
10295 mutex_unlock(&devlink_mutex);
10298 static struct pernet_operations devlink_pernet_ops __net_initdata = {
10299 .pre_exit = devlink_pernet_pre_exit,
10302 static int __init devlink_init(void)
10306 err = genl_register_family(&devlink_nl_family);
10309 err = register_pernet_subsys(&devlink_pernet_ops);
10316 subsys_initcall(devlink_init);