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 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4218 region->port->index))
4219 goto nla_put_failure;
4221 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
4223 goto nla_put_failure;
4225 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4229 goto nla_put_failure;
4231 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
4233 goto nla_put_failure;
4235 genlmsg_end(msg, hdr);
4239 genlmsg_cancel(msg, hdr);
4243 static struct sk_buff *
4244 devlink_nl_region_notify_build(struct devlink_region *region,
4245 struct devlink_snapshot *snapshot,
4246 enum devlink_command cmd, u32 portid, u32 seq)
4248 struct devlink *devlink = region->devlink;
4249 struct sk_buff *msg;
4254 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4256 return ERR_PTR(-ENOMEM);
4258 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
4264 err = devlink_nl_put_handle(msg, devlink);
4266 goto out_cancel_msg;
4269 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4270 region->port->index))
4271 goto out_cancel_msg;
4273 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
4276 goto out_cancel_msg;
4279 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
4282 goto out_cancel_msg;
4284 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4285 region->size, DEVLINK_ATTR_PAD);
4287 goto out_cancel_msg;
4289 genlmsg_end(msg, hdr);
4294 genlmsg_cancel(msg, hdr);
4297 return ERR_PTR(err);
4300 static void devlink_nl_region_notify(struct devlink_region *region,
4301 struct devlink_snapshot *snapshot,
4302 enum devlink_command cmd)
4304 struct devlink *devlink = region->devlink;
4305 struct sk_buff *msg;
4307 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
4309 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
4313 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4314 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4318 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4319 * @devlink: devlink instance
4320 * @id: the snapshot id
4322 * Track when a new snapshot begins using an id. Load the count for the
4323 * given id from the snapshot xarray, increment it, and store it back.
4325 * Called when a new snapshot is created with the given id.
4327 * The id *must* have been previously allocated by
4328 * devlink_region_snapshot_id_get().
4330 * Returns 0 on success, or an error on failure.
4332 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
4334 unsigned long count;
4337 lockdep_assert_held(&devlink->lock);
4339 p = xa_load(&devlink->snapshot_ids, id);
4343 if (WARN_ON(!xa_is_value(p)))
4346 count = xa_to_value(p);
4349 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4354 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4355 * @devlink: devlink instance
4356 * @id: the snapshot id
4358 * Track when a snapshot is deleted and stops using an id. Load the count
4359 * for the given id from the snapshot xarray, decrement it, and store it
4362 * If the count reaches zero, erase this id from the xarray, freeing it
4363 * up for future re-use by devlink_region_snapshot_id_get().
4365 * Called when a snapshot using the given id is deleted, and when the
4366 * initial allocator of the id is finished using it.
4368 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
4370 unsigned long count;
4373 lockdep_assert_held(&devlink->lock);
4375 p = xa_load(&devlink->snapshot_ids, id);
4379 if (WARN_ON(!xa_is_value(p)))
4382 count = xa_to_value(p);
4386 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4389 /* If this was the last user, we can erase this id */
4390 xa_erase(&devlink->snapshot_ids, id);
4395 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4396 * @devlink: devlink instance
4397 * @id: the snapshot id
4399 * Mark the given snapshot id as used by inserting a zero value into the
4402 * This must be called while holding the devlink instance lock. Unlike
4403 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4404 * It is expected that the id will immediately be used before
4405 * releasing the devlink instance lock.
4407 * Returns zero on success, or an error code if the snapshot id could not
4410 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
4412 lockdep_assert_held(&devlink->lock);
4414 if (xa_load(&devlink->snapshot_ids, id))
4417 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
4422 * __devlink_region_snapshot_id_get - get snapshot ID
4423 * @devlink: devlink instance
4424 * @id: storage to return snapshot id
4426 * Allocates a new snapshot id. Returns zero on success, or a negative
4427 * error on failure. Must be called while holding the devlink instance
4430 * Snapshot IDs are tracked using an xarray which stores the number of
4431 * users of the snapshot id.
4433 * Note that the caller of this function counts as a 'user', in order to
4434 * avoid race conditions. The caller must release its hold on the
4435 * snapshot by using devlink_region_snapshot_id_put.
4437 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
4439 lockdep_assert_held(&devlink->lock);
4441 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
4442 xa_limit_32b, GFP_KERNEL);
4446 * __devlink_region_snapshot_create - create a new snapshot
4447 * This will add a new snapshot of a region. The snapshot
4448 * will be stored on the region struct and can be accessed
4449 * from devlink. This is useful for future analyses of snapshots.
4450 * Multiple snapshots can be created on a region.
4451 * The @snapshot_id should be obtained using the getter function.
4453 * Must be called only while holding the devlink instance lock.
4455 * @region: devlink region of the snapshot
4456 * @data: snapshot data
4457 * @snapshot_id: snapshot id to be created
4460 __devlink_region_snapshot_create(struct devlink_region *region,
4461 u8 *data, u32 snapshot_id)
4463 struct devlink *devlink = region->devlink;
4464 struct devlink_snapshot *snapshot;
4467 lockdep_assert_held(&devlink->lock);
4469 /* check if region can hold one more snapshot */
4470 if (region->cur_snapshots == region->max_snapshots)
4473 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
4476 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4480 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
4482 goto err_snapshot_id_increment;
4484 snapshot->id = snapshot_id;
4485 snapshot->region = region;
4486 snapshot->data = data;
4488 list_add_tail(&snapshot->list, ®ion->snapshot_list);
4490 region->cur_snapshots++;
4492 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4495 err_snapshot_id_increment:
4500 static void devlink_region_snapshot_del(struct devlink_region *region,
4501 struct devlink_snapshot *snapshot)
4503 struct devlink *devlink = region->devlink;
4505 lockdep_assert_held(&devlink->lock);
4507 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
4508 region->cur_snapshots--;
4509 list_del(&snapshot->list);
4510 region->ops->destructor(snapshot->data);
4511 __devlink_snapshot_id_decrement(devlink, snapshot->id);
4515 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
4516 struct genl_info *info)
4518 struct devlink *devlink = info->user_ptr[0];
4519 struct devlink_port *port = NULL;
4520 struct devlink_region *region;
4521 const char *region_name;
4522 struct sk_buff *msg;
4526 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
4529 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4530 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4532 port = devlink_port_get_by_index(devlink, index);
4537 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4539 region = devlink_port_region_get_by_name(port, region_name);
4541 region = devlink_region_get_by_name(devlink, region_name);
4546 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4550 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4551 info->snd_portid, info->snd_seq, 0,
4558 return genlmsg_reply(msg, info);
4561 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
4562 struct netlink_callback *cb,
4563 struct devlink_port *port,
4567 struct devlink_region *region;
4570 list_for_each_entry(region, &port->region_list, list) {
4575 err = devlink_nl_region_fill(msg, port->devlink,
4576 DEVLINK_CMD_REGION_GET,
4577 NETLINK_CB(cb->skb).portid,
4579 NLM_F_MULTI, region);
4589 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
4590 struct netlink_callback *cb,
4591 struct devlink *devlink,
4595 struct devlink_region *region;
4596 struct devlink_port *port;
4599 mutex_lock(&devlink->lock);
4600 list_for_each_entry(region, &devlink->region_list, list) {
4605 err = devlink_nl_region_fill(msg, devlink,
4606 DEVLINK_CMD_REGION_GET,
4607 NETLINK_CB(cb->skb).portid,
4609 NLM_F_MULTI, region);
4615 list_for_each_entry(port, &devlink->port_list, list) {
4616 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
4623 mutex_unlock(&devlink->lock);
4627 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
4628 struct netlink_callback *cb)
4630 struct devlink *devlink;
4631 int start = cb->args[0];
4635 mutex_lock(&devlink_mutex);
4636 list_for_each_entry(devlink, &devlink_list, list) {
4637 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4639 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
4645 mutex_unlock(&devlink_mutex);
4650 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4651 struct genl_info *info)
4653 struct devlink *devlink = info->user_ptr[0];
4654 struct devlink_snapshot *snapshot;
4655 struct devlink_port *port = NULL;
4656 struct devlink_region *region;
4657 const char *region_name;
4661 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
4662 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
4665 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4666 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4668 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4669 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4671 port = devlink_port_get_by_index(devlink, index);
4677 region = devlink_port_region_get_by_name(port, region_name);
4679 region = devlink_region_get_by_name(devlink, region_name);
4684 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4688 devlink_region_snapshot_del(region, snapshot);
4693 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4695 struct devlink *devlink = info->user_ptr[0];
4696 struct devlink_snapshot *snapshot;
4697 struct devlink_port *port = NULL;
4698 struct nlattr *snapshot_id_attr;
4699 struct devlink_region *region;
4700 const char *region_name;
4706 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4707 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4711 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4713 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4714 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4716 port = devlink_port_get_by_index(devlink, index);
4722 region = devlink_port_region_get_by_name(port, region_name);
4724 region = devlink_region_get_by_name(devlink, region_name);
4727 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4731 if (!region->ops->snapshot) {
4732 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4736 if (region->cur_snapshots == region->max_snapshots) {
4737 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4741 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
4742 if (snapshot_id_attr) {
4743 snapshot_id = nla_get_u32(snapshot_id_attr);
4745 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4746 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4750 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4754 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
4756 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
4762 err = region->port_ops->snapshot(port, region->port_ops,
4763 info->extack, &data);
4765 err = region->ops->snapshot(devlink, region->ops,
4766 info->extack, &data);
4768 goto err_snapshot_capture;
4770 err = __devlink_region_snapshot_create(region, data, snapshot_id);
4772 goto err_snapshot_create;
4774 if (!snapshot_id_attr) {
4775 struct sk_buff *msg;
4777 snapshot = devlink_region_snapshot_get_by_id(region,
4779 if (WARN_ON(!snapshot))
4782 msg = devlink_nl_region_notify_build(region, snapshot,
4783 DEVLINK_CMD_REGION_NEW,
4786 err = PTR_ERR_OR_ZERO(msg);
4790 err = genlmsg_reply(msg, info);
4797 err_snapshot_create:
4798 region->ops->destructor(data);
4799 err_snapshot_capture:
4800 __devlink_snapshot_id_decrement(devlink, snapshot_id);
4804 devlink_region_snapshot_del(region, snapshot);
4808 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
4809 struct devlink *devlink,
4810 u8 *chunk, u32 chunk_size,
4813 struct nlattr *chunk_attr;
4816 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
4820 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
4822 goto nla_put_failure;
4824 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
4827 goto nla_put_failure;
4829 nla_nest_end(msg, chunk_attr);
4833 nla_nest_cancel(msg, chunk_attr);
4837 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4839 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
4840 struct devlink *devlink,
4841 struct devlink_region *region,
4842 struct nlattr **attrs,
4847 struct devlink_snapshot *snapshot;
4848 u64 curr_offset = start_offset;
4852 *new_offset = start_offset;
4854 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4855 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4859 while (curr_offset < end_offset) {
4863 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
4864 data_size = end_offset - curr_offset;
4866 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
4868 data = &snapshot->data[curr_offset];
4869 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
4875 curr_offset += data_size;
4877 *new_offset = curr_offset;
4882 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
4883 struct netlink_callback *cb)
4885 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
4886 u64 ret_offset, start_offset, end_offset = U64_MAX;
4887 struct nlattr **attrs = info->attrs;
4888 struct devlink_port *port = NULL;
4889 struct devlink_region *region;
4890 struct nlattr *chunks_attr;
4891 const char *region_name;
4892 struct devlink *devlink;
4897 start_offset = *((u64 *)&cb->args[0]);
4899 mutex_lock(&devlink_mutex);
4900 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4901 if (IS_ERR(devlink)) {
4902 err = PTR_ERR(devlink);
4906 mutex_lock(&devlink->lock);
4908 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
4909 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4914 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4915 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4917 port = devlink_port_get_by_index(devlink, index);
4922 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
4925 region = devlink_port_region_get_by_name(port, region_name);
4927 region = devlink_region_get_by_name(devlink, region_name);
4934 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4935 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4938 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4940 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4941 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4944 if (end_offset > region->size)
4945 end_offset = region->size;
4947 /* return 0 if there is no further data to read */
4948 if (start_offset == end_offset) {
4953 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4954 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
4955 DEVLINK_CMD_REGION_READ);
4961 err = devlink_nl_put_handle(skb, devlink);
4963 goto nla_put_failure;
4966 if (nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
4967 region->port->index))
4968 goto nla_put_failure;
4970 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4972 goto nla_put_failure;
4974 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
4977 goto nla_put_failure;
4980 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4983 end_offset, &ret_offset);
4985 if (err && err != -EMSGSIZE)
4986 goto nla_put_failure;
4988 /* Check if there was any progress done to prevent infinite loop */
4989 if (ret_offset == start_offset) {
4991 goto nla_put_failure;
4994 *((u64 *)&cb->args[0]) = ret_offset;
4996 nla_nest_end(skb, chunks_attr);
4997 genlmsg_end(skb, hdr);
4998 mutex_unlock(&devlink->lock);
4999 mutex_unlock(&devlink_mutex);
5004 genlmsg_cancel(skb, hdr);
5006 mutex_unlock(&devlink->lock);
5008 mutex_unlock(&devlink_mutex);
5012 struct devlink_info_req {
5013 struct sk_buff *msg;
5016 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
5018 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
5020 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
5022 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
5024 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
5026 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
5028 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
5031 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
5034 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
5036 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
5037 const char *version_name,
5038 const char *version_value)
5040 struct nlattr *nest;
5043 nest = nla_nest_start_noflag(req->msg, attr);
5047 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
5050 goto nla_put_failure;
5052 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
5055 goto nla_put_failure;
5057 nla_nest_end(req->msg, nest);
5062 nla_nest_cancel(req->msg, nest);
5066 int devlink_info_version_fixed_put(struct devlink_info_req *req,
5067 const char *version_name,
5068 const char *version_value)
5070 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
5071 version_name, version_value);
5073 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
5075 int devlink_info_version_stored_put(struct devlink_info_req *req,
5076 const char *version_name,
5077 const char *version_value)
5079 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
5080 version_name, version_value);
5082 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
5084 int devlink_info_version_running_put(struct devlink_info_req *req,
5085 const char *version_name,
5086 const char *version_value)
5088 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
5089 version_name, version_value);
5091 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
5094 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
5095 enum devlink_command cmd, u32 portid,
5096 u32 seq, int flags, struct netlink_ext_ack *extack)
5098 struct devlink_info_req req;
5102 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5107 if (devlink_nl_put_handle(msg, devlink))
5108 goto err_cancel_msg;
5111 err = devlink->ops->info_get(devlink, &req, extack);
5113 goto err_cancel_msg;
5115 genlmsg_end(msg, hdr);
5119 genlmsg_cancel(msg, hdr);
5123 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
5124 struct genl_info *info)
5126 struct devlink *devlink = info->user_ptr[0];
5127 struct sk_buff *msg;
5130 if (!devlink->ops->info_get)
5133 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5137 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5138 info->snd_portid, info->snd_seq, 0,
5145 return genlmsg_reply(msg, info);
5148 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
5149 struct netlink_callback *cb)
5151 struct devlink *devlink;
5152 int start = cb->args[0];
5156 mutex_lock(&devlink_mutex);
5157 list_for_each_entry(devlink, &devlink_list, list) {
5158 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5165 if (!devlink->ops->info_get) {
5170 mutex_lock(&devlink->lock);
5171 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5172 NETLINK_CB(cb->skb).portid,
5173 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5175 mutex_unlock(&devlink->lock);
5176 if (err == -EOPNOTSUPP)
5182 mutex_unlock(&devlink_mutex);
5184 if (err != -EMSGSIZE)
5191 struct devlink_fmsg_item {
5192 struct list_head list;
5199 struct devlink_fmsg {
5200 struct list_head item_list;
5201 bool putting_binary; /* This flag forces enclosing of binary data
5202 * in an array brackets. It forces using
5203 * of designated API:
5204 * devlink_fmsg_binary_pair_nest_start()
5205 * devlink_fmsg_binary_pair_nest_end()
5209 static struct devlink_fmsg *devlink_fmsg_alloc(void)
5211 struct devlink_fmsg *fmsg;
5213 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
5217 INIT_LIST_HEAD(&fmsg->item_list);
5222 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
5224 struct devlink_fmsg_item *item, *tmp;
5226 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
5227 list_del(&item->list);
5233 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
5236 struct devlink_fmsg_item *item;
5238 item = kzalloc(sizeof(*item), GFP_KERNEL);
5242 item->attrtype = attrtype;
5243 list_add_tail(&item->list, &fmsg->item_list);
5248 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
5250 if (fmsg->putting_binary)
5253 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
5255 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
5257 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
5259 if (fmsg->putting_binary)
5262 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
5265 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
5267 if (fmsg->putting_binary)
5270 return devlink_fmsg_nest_end(fmsg);
5272 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
5274 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
5276 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
5278 struct devlink_fmsg_item *item;
5280 if (fmsg->putting_binary)
5283 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
5286 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
5290 item->nla_type = NLA_NUL_STRING;
5291 item->len = strlen(name) + 1;
5292 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
5293 memcpy(&item->value, name, item->len);
5294 list_add_tail(&item->list, &fmsg->item_list);
5299 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
5303 if (fmsg->putting_binary)
5306 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
5310 err = devlink_fmsg_put_name(fmsg, name);
5316 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
5318 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
5320 if (fmsg->putting_binary)
5323 return devlink_fmsg_nest_end(fmsg);
5325 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
5327 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
5332 if (fmsg->putting_binary)
5335 err = devlink_fmsg_pair_nest_start(fmsg, name);
5339 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
5345 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
5347 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
5351 if (fmsg->putting_binary)
5354 err = devlink_fmsg_nest_end(fmsg);
5358 err = devlink_fmsg_nest_end(fmsg);
5364 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
5366 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
5371 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
5375 fmsg->putting_binary = true;
5378 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
5380 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
5382 if (!fmsg->putting_binary)
5385 fmsg->putting_binary = false;
5386 return devlink_fmsg_arr_pair_nest_end(fmsg);
5388 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
5390 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
5391 const void *value, u16 value_len,
5394 struct devlink_fmsg_item *item;
5396 if (value_len > DEVLINK_FMSG_MAX_SIZE)
5399 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
5403 item->nla_type = value_nla_type;
5404 item->len = value_len;
5405 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5406 memcpy(&item->value, value, item->len);
5407 list_add_tail(&item->list, &fmsg->item_list);
5412 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
5414 if (fmsg->putting_binary)
5417 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
5419 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
5421 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
5423 if (fmsg->putting_binary)
5426 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
5428 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
5430 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
5432 if (fmsg->putting_binary)
5435 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
5437 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
5439 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
5441 if (fmsg->putting_binary)
5444 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
5446 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
5448 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
5450 if (fmsg->putting_binary)
5453 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
5456 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
5458 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
5461 if (!fmsg->putting_binary)
5464 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
5466 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
5468 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
5473 err = devlink_fmsg_pair_nest_start(fmsg, name);
5477 err = devlink_fmsg_bool_put(fmsg, value);
5481 err = devlink_fmsg_pair_nest_end(fmsg);
5487 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
5489 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
5494 err = devlink_fmsg_pair_nest_start(fmsg, name);
5498 err = devlink_fmsg_u8_put(fmsg, value);
5502 err = devlink_fmsg_pair_nest_end(fmsg);
5508 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
5510 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
5515 err = devlink_fmsg_pair_nest_start(fmsg, name);
5519 err = devlink_fmsg_u32_put(fmsg, value);
5523 err = devlink_fmsg_pair_nest_end(fmsg);
5529 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
5531 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
5536 err = devlink_fmsg_pair_nest_start(fmsg, name);
5540 err = devlink_fmsg_u64_put(fmsg, value);
5544 err = devlink_fmsg_pair_nest_end(fmsg);
5550 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
5552 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
5557 err = devlink_fmsg_pair_nest_start(fmsg, name);
5561 err = devlink_fmsg_string_put(fmsg, value);
5565 err = devlink_fmsg_pair_nest_end(fmsg);
5571 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
5573 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
5574 const void *value, u32 value_len)
5581 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
5585 for (offset = 0; offset < value_len; offset += data_size) {
5586 data_size = value_len - offset;
5587 if (data_size > DEVLINK_FMSG_MAX_SIZE)
5588 data_size = DEVLINK_FMSG_MAX_SIZE;
5589 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
5592 /* Exit from loop with a break (instead of
5593 * return) to make sure putting_binary is turned off in
5594 * devlink_fmsg_binary_pair_nest_end
5598 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
5604 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
5607 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5609 switch (msg->nla_type) {
5614 case NLA_NUL_STRING:
5616 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
5624 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5626 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5629 switch (msg->nla_type) {
5631 /* Always provide flag data, regardless of its value */
5632 tmp = *(bool *) msg->value;
5634 return nla_put_u8(skb, attrtype, tmp);
5636 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
5638 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
5640 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
5642 case NLA_NUL_STRING:
5643 return nla_put_string(skb, attrtype, (char *) &msg->value);
5645 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
5652 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5655 struct devlink_fmsg_item *item;
5656 struct nlattr *fmsg_nlattr;
5660 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
5664 list_for_each_entry(item, &fmsg->item_list, list) {
5670 switch (item->attrtype) {
5671 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
5672 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
5673 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
5674 case DEVLINK_ATTR_FMSG_NEST_END:
5675 err = nla_put_flag(skb, item->attrtype);
5677 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
5678 err = devlink_fmsg_item_fill_type(item, skb);
5681 err = devlink_fmsg_item_fill_data(item, skb);
5683 case DEVLINK_ATTR_FMSG_OBJ_NAME:
5684 err = nla_put_string(skb, item->attrtype,
5685 (char *) &item->value);
5697 nla_nest_end(skb, fmsg_nlattr);
5701 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
5702 struct genl_info *info,
5703 enum devlink_command cmd, int flags)
5705 struct nlmsghdr *nlh;
5706 struct sk_buff *skb;
5713 int tmp_index = index;
5715 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5719 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
5720 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
5723 goto nla_put_failure;
5726 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5729 else if (err != -EMSGSIZE || tmp_index == index)
5730 goto nla_put_failure;
5732 genlmsg_end(skb, hdr);
5733 err = genlmsg_reply(skb, info);
5738 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5741 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
5742 NLMSG_DONE, 0, flags | NLM_F_MULTI);
5745 goto nla_put_failure;
5748 return genlmsg_reply(skb, info);
5755 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5756 struct netlink_callback *cb,
5757 enum devlink_command cmd)
5759 int index = cb->args[0];
5760 int tmp_index = index;
5764 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5765 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
5768 goto nla_put_failure;
5771 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5772 if ((err && err != -EMSGSIZE) || tmp_index == index)
5773 goto nla_put_failure;
5775 cb->args[0] = index;
5776 genlmsg_end(skb, hdr);
5780 genlmsg_cancel(skb, hdr);
5784 struct devlink_health_reporter {
5785 struct list_head list;
5787 const struct devlink_health_reporter_ops *ops;
5788 struct devlink *devlink;
5789 struct devlink_port *devlink_port;
5790 struct devlink_fmsg *dump_fmsg;
5791 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
5792 u64 graceful_period;
5800 u64 last_recovery_ts;
5801 refcount_t refcount;
5805 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
5807 return reporter->priv;
5809 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
5811 static struct devlink_health_reporter *
5812 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
5813 struct mutex *list_lock,
5814 const char *reporter_name)
5816 struct devlink_health_reporter *reporter;
5818 lockdep_assert_held(list_lock);
5819 list_for_each_entry(reporter, reporter_list, list)
5820 if (!strcmp(reporter->ops->name, reporter_name))
5825 static struct devlink_health_reporter *
5826 devlink_health_reporter_find_by_name(struct devlink *devlink,
5827 const char *reporter_name)
5829 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
5830 &devlink->reporters_lock,
5834 static struct devlink_health_reporter *
5835 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
5836 const char *reporter_name)
5838 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
5839 &devlink_port->reporters_lock,
5843 static struct devlink_health_reporter *
5844 __devlink_health_reporter_create(struct devlink *devlink,
5845 const struct devlink_health_reporter_ops *ops,
5846 u64 graceful_period, void *priv)
5848 struct devlink_health_reporter *reporter;
5850 if (WARN_ON(graceful_period && !ops->recover))
5851 return ERR_PTR(-EINVAL);
5853 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
5855 return ERR_PTR(-ENOMEM);
5857 reporter->priv = priv;
5858 reporter->ops = ops;
5859 reporter->devlink = devlink;
5860 reporter->graceful_period = graceful_period;
5861 reporter->auto_recover = !!ops->recover;
5862 reporter->auto_dump = !!ops->dump;
5863 mutex_init(&reporter->dump_lock);
5864 refcount_set(&reporter->refcount, 1);
5869 * devlink_port_health_reporter_create - create devlink health reporter for
5870 * specified port instance
5872 * @port: devlink_port which should contain the new reporter
5874 * @graceful_period: to avoid recovery loops, in msecs
5877 struct devlink_health_reporter *
5878 devlink_port_health_reporter_create(struct devlink_port *port,
5879 const struct devlink_health_reporter_ops *ops,
5880 u64 graceful_period, void *priv)
5882 struct devlink_health_reporter *reporter;
5884 mutex_lock(&port->reporters_lock);
5885 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
5886 &port->reporters_lock, ops->name)) {
5887 reporter = ERR_PTR(-EEXIST);
5891 reporter = __devlink_health_reporter_create(port->devlink, ops,
5892 graceful_period, priv);
5893 if (IS_ERR(reporter))
5896 reporter->devlink_port = port;
5897 list_add_tail(&reporter->list, &port->reporter_list);
5899 mutex_unlock(&port->reporters_lock);
5902 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
5905 * devlink_health_reporter_create - create devlink health reporter
5909 * @graceful_period: to avoid recovery loops, in msecs
5912 struct devlink_health_reporter *
5913 devlink_health_reporter_create(struct devlink *devlink,
5914 const struct devlink_health_reporter_ops *ops,
5915 u64 graceful_period, void *priv)
5917 struct devlink_health_reporter *reporter;
5919 mutex_lock(&devlink->reporters_lock);
5920 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
5921 reporter = ERR_PTR(-EEXIST);
5925 reporter = __devlink_health_reporter_create(devlink, ops,
5926 graceful_period, priv);
5927 if (IS_ERR(reporter))
5930 list_add_tail(&reporter->list, &devlink->reporter_list);
5932 mutex_unlock(&devlink->reporters_lock);
5935 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
5938 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
5940 mutex_destroy(&reporter->dump_lock);
5941 if (reporter->dump_fmsg)
5942 devlink_fmsg_free(reporter->dump_fmsg);
5947 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5949 if (refcount_dec_and_test(&reporter->refcount))
5950 devlink_health_reporter_free(reporter);
5954 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5956 list_del(&reporter->list);
5957 devlink_health_reporter_put(reporter);
5961 * devlink_health_reporter_destroy - destroy devlink health reporter
5963 * @reporter: devlink health reporter to destroy
5966 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5968 struct mutex *lock = &reporter->devlink->reporters_lock;
5971 __devlink_health_reporter_destroy(reporter);
5974 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
5977 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
5979 * @reporter: devlink health reporter to destroy
5982 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
5984 struct mutex *lock = &reporter->devlink_port->reporters_lock;
5987 __devlink_health_reporter_destroy(reporter);
5990 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
5993 devlink_nl_health_reporter_fill(struct sk_buff *msg,
5994 struct devlink *devlink,
5995 struct devlink_health_reporter *reporter,
5996 enum devlink_command cmd, u32 portid,
5999 struct nlattr *reporter_attr;
6002 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6006 if (devlink_nl_put_handle(msg, devlink))
6007 goto genlmsg_cancel;
6009 if (reporter->devlink_port) {
6010 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
6011 goto genlmsg_cancel;
6013 reporter_attr = nla_nest_start_noflag(msg,
6014 DEVLINK_ATTR_HEALTH_REPORTER);
6016 goto genlmsg_cancel;
6017 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
6018 reporter->ops->name))
6019 goto reporter_nest_cancel;
6020 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
6021 reporter->health_state))
6022 goto reporter_nest_cancel;
6023 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
6024 reporter->error_count, DEVLINK_ATTR_PAD))
6025 goto reporter_nest_cancel;
6026 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
6027 reporter->recovery_count, DEVLINK_ATTR_PAD))
6028 goto reporter_nest_cancel;
6029 if (reporter->ops->recover &&
6030 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
6031 reporter->graceful_period,
6033 goto reporter_nest_cancel;
6034 if (reporter->ops->recover &&
6035 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
6036 reporter->auto_recover))
6037 goto reporter_nest_cancel;
6038 if (reporter->dump_fmsg &&
6039 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
6040 jiffies_to_msecs(reporter->dump_ts),
6042 goto reporter_nest_cancel;
6043 if (reporter->dump_fmsg &&
6044 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
6045 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
6046 goto reporter_nest_cancel;
6047 if (reporter->ops->dump &&
6048 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
6049 reporter->auto_dump))
6050 goto reporter_nest_cancel;
6052 nla_nest_end(msg, reporter_attr);
6053 genlmsg_end(msg, hdr);
6056 reporter_nest_cancel:
6057 nla_nest_end(msg, reporter_attr);
6059 genlmsg_cancel(msg, hdr);
6063 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
6064 enum devlink_command cmd)
6066 struct sk_buff *msg;
6069 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6071 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6075 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
6076 reporter, cmd, 0, 0, 0);
6082 genlmsg_multicast_netns(&devlink_nl_family,
6083 devlink_net(reporter->devlink),
6084 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
6088 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
6090 reporter->recovery_count++;
6091 reporter->last_recovery_ts = jiffies;
6093 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
6096 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
6097 void *priv_ctx, struct netlink_ext_ack *extack)
6101 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
6104 if (!reporter->ops->recover)
6107 err = reporter->ops->recover(reporter, priv_ctx, extack);
6111 devlink_health_reporter_recovery_done(reporter);
6112 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
6113 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6119 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
6121 if (!reporter->dump_fmsg)
6123 devlink_fmsg_free(reporter->dump_fmsg);
6124 reporter->dump_fmsg = NULL;
6127 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
6129 struct netlink_ext_ack *extack)
6133 if (!reporter->ops->dump)
6136 if (reporter->dump_fmsg)
6139 reporter->dump_fmsg = devlink_fmsg_alloc();
6140 if (!reporter->dump_fmsg) {
6145 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
6149 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
6154 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
6158 reporter->dump_ts = jiffies;
6159 reporter->dump_real_ts = ktime_get_real_ns();
6164 devlink_health_dump_clear(reporter);
6168 int devlink_health_report(struct devlink_health_reporter *reporter,
6169 const char *msg, void *priv_ctx)
6171 enum devlink_health_reporter_state prev_health_state;
6172 struct devlink *devlink = reporter->devlink;
6173 unsigned long recover_ts_threshold;
6175 /* write a log message of the current error */
6177 trace_devlink_health_report(devlink, reporter->ops->name, msg);
6178 reporter->error_count++;
6179 prev_health_state = reporter->health_state;
6180 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6181 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6183 /* abort if the previous error wasn't recovered */
6184 recover_ts_threshold = reporter->last_recovery_ts +
6185 msecs_to_jiffies(reporter->graceful_period);
6186 if (reporter->auto_recover &&
6187 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
6188 (reporter->last_recovery_ts && reporter->recovery_count &&
6189 time_is_after_jiffies(recover_ts_threshold)))) {
6190 trace_devlink_health_recover_aborted(devlink,
6191 reporter->ops->name,
6192 reporter->health_state,
6194 reporter->last_recovery_ts);
6198 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6200 if (reporter->auto_dump) {
6201 mutex_lock(&reporter->dump_lock);
6202 /* store current dump of current error, for later analysis */
6203 devlink_health_do_dump(reporter, priv_ctx, NULL);
6204 mutex_unlock(&reporter->dump_lock);
6207 if (reporter->auto_recover)
6208 return devlink_health_reporter_recover(reporter,
6213 EXPORT_SYMBOL_GPL(devlink_health_report);
6215 static struct devlink_health_reporter *
6216 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
6217 struct nlattr **attrs)
6219 struct devlink_health_reporter *reporter;
6220 struct devlink_port *devlink_port;
6221 char *reporter_name;
6223 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
6226 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
6227 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
6228 if (IS_ERR(devlink_port)) {
6229 mutex_lock(&devlink->reporters_lock);
6230 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
6232 refcount_inc(&reporter->refcount);
6233 mutex_unlock(&devlink->reporters_lock);
6235 mutex_lock(&devlink_port->reporters_lock);
6236 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
6238 refcount_inc(&reporter->refcount);
6239 mutex_unlock(&devlink_port->reporters_lock);
6245 static struct devlink_health_reporter *
6246 devlink_health_reporter_get_from_info(struct devlink *devlink,
6247 struct genl_info *info)
6249 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
6252 static struct devlink_health_reporter *
6253 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
6255 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6256 struct devlink_health_reporter *reporter;
6257 struct nlattr **attrs = info->attrs;
6258 struct devlink *devlink;
6260 mutex_lock(&devlink_mutex);
6261 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6262 if (IS_ERR(devlink))
6265 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
6266 mutex_unlock(&devlink_mutex);
6269 mutex_unlock(&devlink_mutex);
6274 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
6275 enum devlink_health_reporter_state state)
6277 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
6278 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
6281 if (reporter->health_state == state)
6284 reporter->health_state = state;
6285 trace_devlink_health_reporter_state_update(reporter->devlink,
6286 reporter->ops->name, state);
6287 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6289 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
6291 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
6292 struct genl_info *info)
6294 struct devlink *devlink = info->user_ptr[0];
6295 struct devlink_health_reporter *reporter;
6296 struct sk_buff *msg;
6299 reporter = devlink_health_reporter_get_from_info(devlink, info);
6303 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6309 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6310 DEVLINK_CMD_HEALTH_REPORTER_GET,
6311 info->snd_portid, info->snd_seq,
6318 err = genlmsg_reply(msg, info);
6320 devlink_health_reporter_put(reporter);
6325 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
6326 struct netlink_callback *cb)
6328 struct devlink_health_reporter *reporter;
6329 struct devlink_port *port;
6330 struct devlink *devlink;
6331 int start = cb->args[0];
6335 mutex_lock(&devlink_mutex);
6336 list_for_each_entry(devlink, &devlink_list, list) {
6337 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6339 mutex_lock(&devlink->reporters_lock);
6340 list_for_each_entry(reporter, &devlink->reporter_list,
6346 err = devlink_nl_health_reporter_fill(msg, devlink,
6348 DEVLINK_CMD_HEALTH_REPORTER_GET,
6349 NETLINK_CB(cb->skb).portid,
6353 mutex_unlock(&devlink->reporters_lock);
6358 mutex_unlock(&devlink->reporters_lock);
6361 list_for_each_entry(devlink, &devlink_list, list) {
6362 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6364 mutex_lock(&devlink->lock);
6365 list_for_each_entry(port, &devlink->port_list, list) {
6366 mutex_lock(&port->reporters_lock);
6367 list_for_each_entry(reporter, &port->reporter_list, list) {
6372 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6373 DEVLINK_CMD_HEALTH_REPORTER_GET,
6374 NETLINK_CB(cb->skb).portid,
6378 mutex_unlock(&port->reporters_lock);
6379 mutex_unlock(&devlink->lock);
6384 mutex_unlock(&port->reporters_lock);
6386 mutex_unlock(&devlink->lock);
6389 mutex_unlock(&devlink_mutex);
6396 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
6397 struct genl_info *info)
6399 struct devlink *devlink = info->user_ptr[0];
6400 struct devlink_health_reporter *reporter;
6403 reporter = devlink_health_reporter_get_from_info(devlink, info);
6407 if (!reporter->ops->recover &&
6408 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
6409 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
6413 if (!reporter->ops->dump &&
6414 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
6419 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
6420 reporter->graceful_period =
6421 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
6423 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
6424 reporter->auto_recover =
6425 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
6427 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
6428 reporter->auto_dump =
6429 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
6431 devlink_health_reporter_put(reporter);
6434 devlink_health_reporter_put(reporter);
6438 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
6439 struct genl_info *info)
6441 struct devlink *devlink = info->user_ptr[0];
6442 struct devlink_health_reporter *reporter;
6445 reporter = devlink_health_reporter_get_from_info(devlink, info);
6449 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
6451 devlink_health_reporter_put(reporter);
6455 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
6456 struct genl_info *info)
6458 struct devlink *devlink = info->user_ptr[0];
6459 struct devlink_health_reporter *reporter;
6460 struct devlink_fmsg *fmsg;
6463 reporter = devlink_health_reporter_get_from_info(devlink, info);
6467 if (!reporter->ops->diagnose) {
6468 devlink_health_reporter_put(reporter);
6472 fmsg = devlink_fmsg_alloc();
6474 devlink_health_reporter_put(reporter);
6478 err = devlink_fmsg_obj_nest_start(fmsg);
6482 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
6486 err = devlink_fmsg_obj_nest_end(fmsg);
6490 err = devlink_fmsg_snd(fmsg, info,
6491 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
6494 devlink_fmsg_free(fmsg);
6495 devlink_health_reporter_put(reporter);
6500 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
6501 struct netlink_callback *cb)
6503 struct devlink_health_reporter *reporter;
6504 u64 start = cb->args[0];
6507 reporter = devlink_health_reporter_get_from_cb(cb);
6511 if (!reporter->ops->dump) {
6515 mutex_lock(&reporter->dump_lock);
6517 err = devlink_health_do_dump(reporter, NULL, cb->extack);
6520 cb->args[1] = reporter->dump_ts;
6522 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
6523 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
6528 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
6529 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
6531 mutex_unlock(&reporter->dump_lock);
6533 devlink_health_reporter_put(reporter);
6538 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
6539 struct genl_info *info)
6541 struct devlink *devlink = info->user_ptr[0];
6542 struct devlink_health_reporter *reporter;
6544 reporter = devlink_health_reporter_get_from_info(devlink, info);
6548 if (!reporter->ops->dump) {
6549 devlink_health_reporter_put(reporter);
6553 mutex_lock(&reporter->dump_lock);
6554 devlink_health_dump_clear(reporter);
6555 mutex_unlock(&reporter->dump_lock);
6556 devlink_health_reporter_put(reporter);
6560 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
6561 struct genl_info *info)
6563 struct devlink *devlink = info->user_ptr[0];
6564 struct devlink_health_reporter *reporter;
6567 reporter = devlink_health_reporter_get_from_info(devlink, info);
6571 if (!reporter->ops->test) {
6572 devlink_health_reporter_put(reporter);
6576 err = reporter->ops->test(reporter, info->extack);
6578 devlink_health_reporter_put(reporter);
6582 struct devlink_stats {
6585 struct u64_stats_sync syncp;
6589 * struct devlink_trap_policer_item - Packet trap policer attributes.
6590 * @policer: Immutable packet trap policer attributes.
6591 * @rate: Rate in packets / sec.
6592 * @burst: Burst size in packets.
6593 * @list: trap_policer_list member.
6595 * Describes packet trap policer attributes. Created by devlink during trap
6596 * policer registration.
6598 struct devlink_trap_policer_item {
6599 const struct devlink_trap_policer *policer;
6602 struct list_head list;
6606 * struct devlink_trap_group_item - Packet trap group attributes.
6607 * @group: Immutable packet trap group attributes.
6608 * @policer_item: Associated policer item. Can be NULL.
6609 * @list: trap_group_list member.
6610 * @stats: Trap group statistics.
6612 * Describes packet trap group attributes. Created by devlink during trap
6613 * group registration.
6615 struct devlink_trap_group_item {
6616 const struct devlink_trap_group *group;
6617 struct devlink_trap_policer_item *policer_item;
6618 struct list_head list;
6619 struct devlink_stats __percpu *stats;
6623 * struct devlink_trap_item - Packet trap attributes.
6624 * @trap: Immutable packet trap attributes.
6625 * @group_item: Associated group item.
6626 * @list: trap_list member.
6627 * @action: Trap action.
6628 * @stats: Trap statistics.
6629 * @priv: Driver private information.
6631 * Describes both mutable and immutable packet trap attributes. Created by
6632 * devlink during trap registration and used for all trap related operations.
6634 struct devlink_trap_item {
6635 const struct devlink_trap *trap;
6636 struct devlink_trap_group_item *group_item;
6637 struct list_head list;
6638 enum devlink_trap_action action;
6639 struct devlink_stats __percpu *stats;
6643 static struct devlink_trap_policer_item *
6644 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
6646 struct devlink_trap_policer_item *policer_item;
6648 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
6649 if (policer_item->policer->id == id)
6650 return policer_item;
6656 static struct devlink_trap_item *
6657 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
6659 struct devlink_trap_item *trap_item;
6661 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6662 if (!strcmp(trap_item->trap->name, name))
6669 static struct devlink_trap_item *
6670 devlink_trap_item_get_from_info(struct devlink *devlink,
6671 struct genl_info *info)
6673 struct nlattr *attr;
6675 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
6677 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
6679 return devlink_trap_item_lookup(devlink, nla_data(attr));
6683 devlink_trap_action_get_from_info(struct genl_info *info,
6684 enum devlink_trap_action *p_trap_action)
6688 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
6690 case DEVLINK_TRAP_ACTION_DROP:
6691 case DEVLINK_TRAP_ACTION_TRAP:
6692 case DEVLINK_TRAP_ACTION_MIRROR:
6693 *p_trap_action = val;
6702 static int devlink_trap_metadata_put(struct sk_buff *msg,
6703 const struct devlink_trap *trap)
6705 struct nlattr *attr;
6707 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
6711 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
6712 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
6713 goto nla_put_failure;
6714 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
6715 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
6716 goto nla_put_failure;
6718 nla_nest_end(msg, attr);
6723 nla_nest_cancel(msg, attr);
6727 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
6728 struct devlink_stats *stats)
6732 memset(stats, 0, sizeof(*stats));
6733 for_each_possible_cpu(i) {
6734 struct devlink_stats *cpu_stats;
6735 u64 rx_packets, rx_bytes;
6738 cpu_stats = per_cpu_ptr(trap_stats, i);
6740 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6741 rx_packets = cpu_stats->rx_packets;
6742 rx_bytes = cpu_stats->rx_bytes;
6743 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6745 stats->rx_packets += rx_packets;
6746 stats->rx_bytes += rx_bytes;
6750 static int devlink_trap_stats_put(struct sk_buff *msg,
6751 struct devlink_stats __percpu *trap_stats)
6753 struct devlink_stats stats;
6754 struct nlattr *attr;
6756 devlink_trap_stats_read(trap_stats, &stats);
6758 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6762 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
6763 stats.rx_packets, DEVLINK_ATTR_PAD))
6764 goto nla_put_failure;
6766 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
6767 stats.rx_bytes, DEVLINK_ATTR_PAD))
6768 goto nla_put_failure;
6770 nla_nest_end(msg, attr);
6775 nla_nest_cancel(msg, attr);
6779 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
6780 const struct devlink_trap_item *trap_item,
6781 enum devlink_command cmd, u32 portid, u32 seq,
6784 struct devlink_trap_group_item *group_item = trap_item->group_item;
6788 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6792 if (devlink_nl_put_handle(msg, devlink))
6793 goto nla_put_failure;
6795 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6796 group_item->group->name))
6797 goto nla_put_failure;
6799 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
6800 goto nla_put_failure;
6802 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
6803 goto nla_put_failure;
6805 if (trap_item->trap->generic &&
6806 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6807 goto nla_put_failure;
6809 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
6810 goto nla_put_failure;
6812 err = devlink_trap_metadata_put(msg, trap_item->trap);
6814 goto nla_put_failure;
6816 err = devlink_trap_stats_put(msg, trap_item->stats);
6818 goto nla_put_failure;
6820 genlmsg_end(msg, hdr);
6825 genlmsg_cancel(msg, hdr);
6829 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
6830 struct genl_info *info)
6832 struct netlink_ext_ack *extack = info->extack;
6833 struct devlink *devlink = info->user_ptr[0];
6834 struct devlink_trap_item *trap_item;
6835 struct sk_buff *msg;
6838 if (list_empty(&devlink->trap_list))
6841 trap_item = devlink_trap_item_get_from_info(devlink, info);
6843 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6851 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6852 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
6857 return genlmsg_reply(msg, info);
6864 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
6865 struct netlink_callback *cb)
6867 struct devlink_trap_item *trap_item;
6868 struct devlink *devlink;
6869 int start = cb->args[0];
6873 mutex_lock(&devlink_mutex);
6874 list_for_each_entry(devlink, &devlink_list, list) {
6875 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6877 mutex_lock(&devlink->lock);
6878 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6883 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6884 DEVLINK_CMD_TRAP_NEW,
6885 NETLINK_CB(cb->skb).portid,
6889 mutex_unlock(&devlink->lock);
6894 mutex_unlock(&devlink->lock);
6897 mutex_unlock(&devlink_mutex);
6903 static int __devlink_trap_action_set(struct devlink *devlink,
6904 struct devlink_trap_item *trap_item,
6905 enum devlink_trap_action trap_action,
6906 struct netlink_ext_ack *extack)
6910 if (trap_item->action != trap_action &&
6911 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
6912 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
6916 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
6917 trap_action, extack);
6921 trap_item->action = trap_action;
6926 static int devlink_trap_action_set(struct devlink *devlink,
6927 struct devlink_trap_item *trap_item,
6928 struct genl_info *info)
6930 enum devlink_trap_action trap_action;
6933 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6936 err = devlink_trap_action_get_from_info(info, &trap_action);
6938 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6942 return __devlink_trap_action_set(devlink, trap_item, trap_action,
6946 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
6947 struct genl_info *info)
6949 struct netlink_ext_ack *extack = info->extack;
6950 struct devlink *devlink = info->user_ptr[0];
6951 struct devlink_trap_item *trap_item;
6954 if (list_empty(&devlink->trap_list))
6957 trap_item = devlink_trap_item_get_from_info(devlink, info);
6959 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6963 err = devlink_trap_action_set(devlink, trap_item, info);
6970 static struct devlink_trap_group_item *
6971 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
6973 struct devlink_trap_group_item *group_item;
6975 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6976 if (!strcmp(group_item->group->name, name))
6983 static struct devlink_trap_group_item *
6984 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
6986 struct devlink_trap_group_item *group_item;
6988 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6989 if (group_item->group->id == id)
6996 static struct devlink_trap_group_item *
6997 devlink_trap_group_item_get_from_info(struct devlink *devlink,
6998 struct genl_info *info)
7002 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
7004 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
7006 return devlink_trap_group_item_lookup(devlink, name);
7010 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
7011 const struct devlink_trap_group_item *group_item,
7012 enum devlink_command cmd, u32 portid, u32 seq,
7018 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7022 if (devlink_nl_put_handle(msg, devlink))
7023 goto nla_put_failure;
7025 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7026 group_item->group->name))
7027 goto nla_put_failure;
7029 if (group_item->group->generic &&
7030 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7031 goto nla_put_failure;
7033 if (group_item->policer_item &&
7034 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7035 group_item->policer_item->policer->id))
7036 goto nla_put_failure;
7038 err = devlink_trap_stats_put(msg, group_item->stats);
7040 goto nla_put_failure;
7042 genlmsg_end(msg, hdr);
7047 genlmsg_cancel(msg, hdr);
7051 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
7052 struct genl_info *info)
7054 struct netlink_ext_ack *extack = info->extack;
7055 struct devlink *devlink = info->user_ptr[0];
7056 struct devlink_trap_group_item *group_item;
7057 struct sk_buff *msg;
7060 if (list_empty(&devlink->trap_group_list))
7063 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7065 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7069 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7073 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
7074 DEVLINK_CMD_TRAP_GROUP_NEW,
7075 info->snd_portid, info->snd_seq, 0);
7077 goto err_trap_group_fill;
7079 return genlmsg_reply(msg, info);
7081 err_trap_group_fill:
7086 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
7087 struct netlink_callback *cb)
7089 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
7090 struct devlink_trap_group_item *group_item;
7091 u32 portid = NETLINK_CB(cb->skb).portid;
7092 struct devlink *devlink;
7093 int start = cb->args[0];
7097 mutex_lock(&devlink_mutex);
7098 list_for_each_entry(devlink, &devlink_list, list) {
7099 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7101 mutex_lock(&devlink->lock);
7102 list_for_each_entry(group_item, &devlink->trap_group_list,
7108 err = devlink_nl_trap_group_fill(msg, devlink,
7114 mutex_unlock(&devlink->lock);
7119 mutex_unlock(&devlink->lock);
7122 mutex_unlock(&devlink_mutex);
7129 __devlink_trap_group_action_set(struct devlink *devlink,
7130 struct devlink_trap_group_item *group_item,
7131 enum devlink_trap_action trap_action,
7132 struct netlink_ext_ack *extack)
7134 const char *group_name = group_item->group->name;
7135 struct devlink_trap_item *trap_item;
7138 if (devlink->ops->trap_group_action_set) {
7139 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
7140 trap_action, extack);
7144 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7145 if (strcmp(trap_item->group_item->group->name, group_name))
7147 if (trap_item->action != trap_action &&
7148 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
7150 trap_item->action = trap_action;
7156 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7157 if (strcmp(trap_item->group_item->group->name, group_name))
7159 err = __devlink_trap_action_set(devlink, trap_item,
7160 trap_action, extack);
7169 devlink_trap_group_action_set(struct devlink *devlink,
7170 struct devlink_trap_group_item *group_item,
7171 struct genl_info *info, bool *p_modified)
7173 enum devlink_trap_action trap_action;
7176 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
7179 err = devlink_trap_action_get_from_info(info, &trap_action);
7181 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
7185 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
7195 static int devlink_trap_group_set(struct devlink *devlink,
7196 struct devlink_trap_group_item *group_item,
7197 struct genl_info *info)
7199 struct devlink_trap_policer_item *policer_item;
7200 struct netlink_ext_ack *extack = info->extack;
7201 const struct devlink_trap_policer *policer;
7202 struct nlattr **attrs = info->attrs;
7205 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7208 if (!devlink->ops->trap_group_set)
7211 policer_item = group_item->policer_item;
7212 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
7215 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7216 policer_item = devlink_trap_policer_item_lookup(devlink,
7218 if (policer_id && !policer_item) {
7219 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7223 policer = policer_item ? policer_item->policer : NULL;
7225 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
7230 group_item->policer_item = policer_item;
7235 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
7236 struct genl_info *info)
7238 struct netlink_ext_ack *extack = info->extack;
7239 struct devlink *devlink = info->user_ptr[0];
7240 struct devlink_trap_group_item *group_item;
7241 bool modified = false;
7244 if (list_empty(&devlink->trap_group_list))
7247 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7249 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7253 err = devlink_trap_group_action_set(devlink, group_item, info,
7258 err = devlink_trap_group_set(devlink, group_item, info);
7260 goto err_trap_group_set;
7266 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
7270 static struct devlink_trap_policer_item *
7271 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
7272 struct genl_info *info)
7276 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7278 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7280 return devlink_trap_policer_item_lookup(devlink, id);
7284 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
7285 const struct devlink_trap_policer *policer)
7287 struct nlattr *attr;
7291 if (!devlink->ops->trap_policer_counter_get)
7294 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
7298 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7302 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
7304 goto nla_put_failure;
7306 nla_nest_end(msg, attr);
7311 nla_nest_cancel(msg, attr);
7316 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
7317 const struct devlink_trap_policer_item *policer_item,
7318 enum devlink_command cmd, u32 portid, u32 seq,
7324 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7328 if (devlink_nl_put_handle(msg, devlink))
7329 goto nla_put_failure;
7331 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7332 policer_item->policer->id))
7333 goto nla_put_failure;
7335 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
7336 policer_item->rate, DEVLINK_ATTR_PAD))
7337 goto nla_put_failure;
7339 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
7340 policer_item->burst, DEVLINK_ATTR_PAD))
7341 goto nla_put_failure;
7343 err = devlink_trap_policer_stats_put(msg, devlink,
7344 policer_item->policer);
7346 goto nla_put_failure;
7348 genlmsg_end(msg, hdr);
7353 genlmsg_cancel(msg, hdr);
7357 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
7358 struct genl_info *info)
7360 struct devlink_trap_policer_item *policer_item;
7361 struct netlink_ext_ack *extack = info->extack;
7362 struct devlink *devlink = info->user_ptr[0];
7363 struct sk_buff *msg;
7366 if (list_empty(&devlink->trap_policer_list))
7369 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7370 if (!policer_item) {
7371 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7379 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
7380 DEVLINK_CMD_TRAP_POLICER_NEW,
7381 info->snd_portid, info->snd_seq, 0);
7383 goto err_trap_policer_fill;
7385 return genlmsg_reply(msg, info);
7387 err_trap_policer_fill:
7392 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
7393 struct netlink_callback *cb)
7395 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
7396 struct devlink_trap_policer_item *policer_item;
7397 u32 portid = NETLINK_CB(cb->skb).portid;
7398 struct devlink *devlink;
7399 int start = cb->args[0];
7403 mutex_lock(&devlink_mutex);
7404 list_for_each_entry(devlink, &devlink_list, list) {
7405 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7407 mutex_lock(&devlink->lock);
7408 list_for_each_entry(policer_item, &devlink->trap_policer_list,
7414 err = devlink_nl_trap_policer_fill(msg, devlink,
7420 mutex_unlock(&devlink->lock);
7425 mutex_unlock(&devlink->lock);
7428 mutex_unlock(&devlink_mutex);
7435 devlink_trap_policer_set(struct devlink *devlink,
7436 struct devlink_trap_policer_item *policer_item,
7437 struct genl_info *info)
7439 struct netlink_ext_ack *extack = info->extack;
7440 struct nlattr **attrs = info->attrs;
7444 rate = policer_item->rate;
7445 burst = policer_item->burst;
7447 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
7448 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
7450 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
7451 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
7453 if (rate < policer_item->policer->min_rate) {
7454 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
7458 if (rate > policer_item->policer->max_rate) {
7459 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
7463 if (burst < policer_item->policer->min_burst) {
7464 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
7468 if (burst > policer_item->policer->max_burst) {
7469 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
7473 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
7474 rate, burst, info->extack);
7478 policer_item->rate = rate;
7479 policer_item->burst = burst;
7484 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
7485 struct genl_info *info)
7487 struct devlink_trap_policer_item *policer_item;
7488 struct netlink_ext_ack *extack = info->extack;
7489 struct devlink *devlink = info->user_ptr[0];
7491 if (list_empty(&devlink->trap_policer_list))
7494 if (!devlink->ops->trap_policer_set)
7497 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7498 if (!policer_item) {
7499 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7503 return devlink_trap_policer_set(devlink, policer_item, info);
7506 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
7507 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
7508 DEVLINK_ATTR_TRAP_POLICER_ID },
7509 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
7510 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
7511 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
7512 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
7513 DEVLINK_PORT_TYPE_IB),
7514 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
7515 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
7516 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
7517 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
7518 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
7519 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
7520 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
7521 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
7522 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
7523 DEVLINK_ESWITCH_MODE_SWITCHDEV),
7524 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
7525 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
7526 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
7527 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
7528 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
7529 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
7530 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
7531 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
7532 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
7533 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
7534 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
7535 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
7536 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
7537 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
7538 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
7539 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
7540 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
7541 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
7542 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
7543 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
7544 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
7545 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
7546 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
7547 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
7548 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
7549 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
7550 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
7551 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
7552 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
7553 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
7554 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
7555 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
7556 DEVLINK_RELOAD_ACTION_MAX),
7557 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
7560 static const struct genl_small_ops devlink_nl_ops[] = {
7562 .cmd = DEVLINK_CMD_GET,
7563 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7564 .doit = devlink_nl_cmd_get_doit,
7565 .dumpit = devlink_nl_cmd_get_dumpit,
7566 /* can be retrieved by unprivileged users */
7569 .cmd = DEVLINK_CMD_PORT_GET,
7570 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7571 .doit = devlink_nl_cmd_port_get_doit,
7572 .dumpit = devlink_nl_cmd_port_get_dumpit,
7573 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7574 /* can be retrieved by unprivileged users */
7577 .cmd = DEVLINK_CMD_PORT_SET,
7578 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7579 .doit = devlink_nl_cmd_port_set_doit,
7580 .flags = GENL_ADMIN_PERM,
7581 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7584 .cmd = DEVLINK_CMD_PORT_SPLIT,
7585 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7586 .doit = devlink_nl_cmd_port_split_doit,
7587 .flags = GENL_ADMIN_PERM,
7588 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7591 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
7592 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7593 .doit = devlink_nl_cmd_port_unsplit_doit,
7594 .flags = GENL_ADMIN_PERM,
7595 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7598 .cmd = DEVLINK_CMD_SB_GET,
7599 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7600 .doit = devlink_nl_cmd_sb_get_doit,
7601 .dumpit = devlink_nl_cmd_sb_get_dumpit,
7602 /* can be retrieved by unprivileged users */
7605 .cmd = DEVLINK_CMD_SB_POOL_GET,
7606 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7607 .doit = devlink_nl_cmd_sb_pool_get_doit,
7608 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
7609 /* can be retrieved by unprivileged users */
7612 .cmd = DEVLINK_CMD_SB_POOL_SET,
7613 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7614 .doit = devlink_nl_cmd_sb_pool_set_doit,
7615 .flags = GENL_ADMIN_PERM,
7618 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
7619 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7620 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
7621 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
7622 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7623 /* can be retrieved by unprivileged users */
7626 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
7627 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7628 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
7629 .flags = GENL_ADMIN_PERM,
7630 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7633 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
7634 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7635 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
7636 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
7637 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7638 /* can be retrieved by unprivileged users */
7641 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
7642 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7643 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
7644 .flags = GENL_ADMIN_PERM,
7645 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7648 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
7649 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7650 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
7651 .flags = GENL_ADMIN_PERM,
7654 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
7655 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7656 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
7657 .flags = GENL_ADMIN_PERM,
7660 .cmd = DEVLINK_CMD_ESWITCH_GET,
7661 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7662 .doit = devlink_nl_cmd_eswitch_get_doit,
7663 .flags = GENL_ADMIN_PERM,
7664 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7667 .cmd = DEVLINK_CMD_ESWITCH_SET,
7668 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7669 .doit = devlink_nl_cmd_eswitch_set_doit,
7670 .flags = GENL_ADMIN_PERM,
7671 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7674 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
7675 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7676 .doit = devlink_nl_cmd_dpipe_table_get,
7677 /* can be retrieved by unprivileged users */
7680 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
7681 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7682 .doit = devlink_nl_cmd_dpipe_entries_get,
7683 /* can be retrieved by unprivileged users */
7686 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
7687 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7688 .doit = devlink_nl_cmd_dpipe_headers_get,
7689 /* can be retrieved by unprivileged users */
7692 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
7693 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7694 .doit = devlink_nl_cmd_dpipe_table_counters_set,
7695 .flags = GENL_ADMIN_PERM,
7698 .cmd = DEVLINK_CMD_RESOURCE_SET,
7699 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7700 .doit = devlink_nl_cmd_resource_set,
7701 .flags = GENL_ADMIN_PERM,
7704 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
7705 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7706 .doit = devlink_nl_cmd_resource_dump,
7707 /* can be retrieved by unprivileged users */
7710 .cmd = DEVLINK_CMD_RELOAD,
7711 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7712 .doit = devlink_nl_cmd_reload,
7713 .flags = GENL_ADMIN_PERM,
7714 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7717 .cmd = DEVLINK_CMD_PARAM_GET,
7718 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7719 .doit = devlink_nl_cmd_param_get_doit,
7720 .dumpit = devlink_nl_cmd_param_get_dumpit,
7721 /* can be retrieved by unprivileged users */
7724 .cmd = DEVLINK_CMD_PARAM_SET,
7725 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7726 .doit = devlink_nl_cmd_param_set_doit,
7727 .flags = GENL_ADMIN_PERM,
7730 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
7731 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7732 .doit = devlink_nl_cmd_port_param_get_doit,
7733 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
7734 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7735 /* can be retrieved by unprivileged users */
7738 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
7739 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7740 .doit = devlink_nl_cmd_port_param_set_doit,
7741 .flags = GENL_ADMIN_PERM,
7742 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7745 .cmd = DEVLINK_CMD_REGION_GET,
7746 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7747 .doit = devlink_nl_cmd_region_get_doit,
7748 .dumpit = devlink_nl_cmd_region_get_dumpit,
7749 .flags = GENL_ADMIN_PERM,
7752 .cmd = DEVLINK_CMD_REGION_NEW,
7753 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7754 .doit = devlink_nl_cmd_region_new,
7755 .flags = GENL_ADMIN_PERM,
7758 .cmd = DEVLINK_CMD_REGION_DEL,
7759 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7760 .doit = devlink_nl_cmd_region_del,
7761 .flags = GENL_ADMIN_PERM,
7764 .cmd = DEVLINK_CMD_REGION_READ,
7765 .validate = GENL_DONT_VALIDATE_STRICT |
7766 GENL_DONT_VALIDATE_DUMP_STRICT,
7767 .dumpit = devlink_nl_cmd_region_read_dumpit,
7768 .flags = GENL_ADMIN_PERM,
7771 .cmd = DEVLINK_CMD_INFO_GET,
7772 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7773 .doit = devlink_nl_cmd_info_get_doit,
7774 .dumpit = devlink_nl_cmd_info_get_dumpit,
7775 /* can be retrieved by unprivileged users */
7778 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
7779 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7780 .doit = devlink_nl_cmd_health_reporter_get_doit,
7781 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
7782 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7783 DEVLINK_NL_FLAG_NO_LOCK,
7784 /* can be retrieved by unprivileged users */
7787 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
7788 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7789 .doit = devlink_nl_cmd_health_reporter_set_doit,
7790 .flags = GENL_ADMIN_PERM,
7791 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7792 DEVLINK_NL_FLAG_NO_LOCK,
7795 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
7796 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7797 .doit = devlink_nl_cmd_health_reporter_recover_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_DIAGNOSE,
7804 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7805 .doit = devlink_nl_cmd_health_reporter_diagnose_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_DUMP_GET,
7812 .validate = GENL_DONT_VALIDATE_STRICT |
7813 GENL_DONT_VALIDATE_DUMP_STRICT,
7814 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
7815 .flags = GENL_ADMIN_PERM,
7816 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7817 DEVLINK_NL_FLAG_NO_LOCK,
7820 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
7821 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7822 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
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_TEST,
7829 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7830 .doit = devlink_nl_cmd_health_reporter_test_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_FLASH_UPDATE,
7837 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7838 .doit = devlink_nl_cmd_flash_update,
7839 .flags = GENL_ADMIN_PERM,
7842 .cmd = DEVLINK_CMD_TRAP_GET,
7843 .doit = devlink_nl_cmd_trap_get_doit,
7844 .dumpit = devlink_nl_cmd_trap_get_dumpit,
7845 /* can be retrieved by unprivileged users */
7848 .cmd = DEVLINK_CMD_TRAP_SET,
7849 .doit = devlink_nl_cmd_trap_set_doit,
7850 .flags = GENL_ADMIN_PERM,
7853 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
7854 .doit = devlink_nl_cmd_trap_group_get_doit,
7855 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
7856 /* can be retrieved by unprivileged users */
7859 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
7860 .doit = devlink_nl_cmd_trap_group_set_doit,
7861 .flags = GENL_ADMIN_PERM,
7864 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
7865 .doit = devlink_nl_cmd_trap_policer_get_doit,
7866 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
7867 /* can be retrieved by unprivileged users */
7870 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
7871 .doit = devlink_nl_cmd_trap_policer_set_doit,
7872 .flags = GENL_ADMIN_PERM,
7876 static struct genl_family devlink_nl_family __ro_after_init = {
7877 .name = DEVLINK_GENL_NAME,
7878 .version = DEVLINK_GENL_VERSION,
7879 .maxattr = DEVLINK_ATTR_MAX,
7880 .policy = devlink_nl_policy,
7882 .pre_doit = devlink_nl_pre_doit,
7883 .post_doit = devlink_nl_post_doit,
7884 .module = THIS_MODULE,
7885 .small_ops = devlink_nl_ops,
7886 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
7887 .mcgrps = devlink_nl_mcgrps,
7888 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
7891 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
7893 const struct devlink_reload_combination *comb;
7896 if (!devlink_reload_supported(ops)) {
7897 if (WARN_ON(ops->reload_actions))
7902 if (WARN_ON(!ops->reload_actions ||
7903 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
7904 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
7907 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
7908 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
7911 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
7912 comb = &devlink_reload_invalid_combinations[i];
7913 if (ops->reload_actions == BIT(comb->action) &&
7914 ops->reload_limits == BIT(comb->limit))
7921 * devlink_alloc - Allocate new devlink instance resources
7924 * @priv_size: size of user private data
7926 * Allocate new devlink instance resources, including devlink index
7929 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
7931 struct devlink *devlink;
7936 if (!devlink_reload_actions_valid(ops))
7939 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
7943 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
7944 __devlink_net_set(devlink, &init_net);
7945 INIT_LIST_HEAD(&devlink->port_list);
7946 INIT_LIST_HEAD(&devlink->sb_list);
7947 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
7948 INIT_LIST_HEAD(&devlink->resource_list);
7949 INIT_LIST_HEAD(&devlink->param_list);
7950 INIT_LIST_HEAD(&devlink->region_list);
7951 INIT_LIST_HEAD(&devlink->reporter_list);
7952 INIT_LIST_HEAD(&devlink->trap_list);
7953 INIT_LIST_HEAD(&devlink->trap_group_list);
7954 INIT_LIST_HEAD(&devlink->trap_policer_list);
7955 mutex_init(&devlink->lock);
7956 mutex_init(&devlink->reporters_lock);
7959 EXPORT_SYMBOL_GPL(devlink_alloc);
7962 * devlink_register - Register devlink instance
7965 * @dev: parent device
7967 int devlink_register(struct devlink *devlink, struct device *dev)
7970 devlink->registered = true;
7971 mutex_lock(&devlink_mutex);
7972 list_add_tail(&devlink->list, &devlink_list);
7973 devlink_notify(devlink, DEVLINK_CMD_NEW);
7974 mutex_unlock(&devlink_mutex);
7977 EXPORT_SYMBOL_GPL(devlink_register);
7980 * devlink_unregister - Unregister devlink instance
7984 void devlink_unregister(struct devlink *devlink)
7986 mutex_lock(&devlink_mutex);
7987 WARN_ON(devlink_reload_supported(devlink->ops) &&
7988 devlink->reload_enabled);
7989 devlink_notify(devlink, DEVLINK_CMD_DEL);
7990 list_del(&devlink->list);
7991 mutex_unlock(&devlink_mutex);
7993 EXPORT_SYMBOL_GPL(devlink_unregister);
7996 * devlink_reload_enable - Enable reload of devlink instance
8000 * Should be called at end of device initialization
8001 * process when reload operation is supported.
8003 void devlink_reload_enable(struct devlink *devlink)
8005 mutex_lock(&devlink_mutex);
8006 devlink->reload_enabled = true;
8007 mutex_unlock(&devlink_mutex);
8009 EXPORT_SYMBOL_GPL(devlink_reload_enable);
8012 * devlink_reload_disable - Disable reload of devlink instance
8016 * Should be called at the beginning of device cleanup
8017 * process when reload operation is supported.
8019 void devlink_reload_disable(struct devlink *devlink)
8021 mutex_lock(&devlink_mutex);
8022 /* Mutex is taken which ensures that no reload operation is in
8023 * progress while setting up forbidded flag.
8025 devlink->reload_enabled = false;
8026 mutex_unlock(&devlink_mutex);
8028 EXPORT_SYMBOL_GPL(devlink_reload_disable);
8031 * devlink_free - Free devlink instance resources
8035 void devlink_free(struct devlink *devlink)
8037 mutex_destroy(&devlink->reporters_lock);
8038 mutex_destroy(&devlink->lock);
8039 WARN_ON(!list_empty(&devlink->trap_policer_list));
8040 WARN_ON(!list_empty(&devlink->trap_group_list));
8041 WARN_ON(!list_empty(&devlink->trap_list));
8042 WARN_ON(!list_empty(&devlink->reporter_list));
8043 WARN_ON(!list_empty(&devlink->region_list));
8044 WARN_ON(!list_empty(&devlink->param_list));
8045 WARN_ON(!list_empty(&devlink->resource_list));
8046 WARN_ON(!list_empty(&devlink->dpipe_table_list));
8047 WARN_ON(!list_empty(&devlink->sb_list));
8048 WARN_ON(!list_empty(&devlink->port_list));
8050 xa_destroy(&devlink->snapshot_ids);
8054 EXPORT_SYMBOL_GPL(devlink_free);
8056 static void devlink_port_type_warn(struct work_struct *work)
8058 WARN(true, "Type was not set for devlink port.");
8061 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
8063 /* Ignore CPU and DSA flavours. */
8064 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
8065 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
8066 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
8069 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
8071 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
8073 if (!devlink_port_type_should_warn(devlink_port))
8075 /* Schedule a work to WARN in case driver does not set port
8076 * type within timeout.
8078 schedule_delayed_work(&devlink_port->type_warn_dw,
8079 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
8082 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
8084 if (!devlink_port_type_should_warn(devlink_port))
8086 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
8090 * devlink_port_register - Register devlink port
8093 * @devlink_port: devlink port
8094 * @port_index: driver-specific numerical identifier of the port
8096 * Register devlink port with provided port index. User can use
8097 * any indexing, even hw-related one. devlink_port structure
8098 * is convenient to be embedded inside user driver private structure.
8099 * Note that the caller should take care of zeroing the devlink_port
8102 int devlink_port_register(struct devlink *devlink,
8103 struct devlink_port *devlink_port,
8104 unsigned int port_index)
8106 mutex_lock(&devlink->lock);
8107 if (devlink_port_index_exists(devlink, port_index)) {
8108 mutex_unlock(&devlink->lock);
8111 devlink_port->devlink = devlink;
8112 devlink_port->index = port_index;
8113 devlink_port->registered = true;
8114 spin_lock_init(&devlink_port->type_lock);
8115 INIT_LIST_HEAD(&devlink_port->reporter_list);
8116 mutex_init(&devlink_port->reporters_lock);
8117 list_add_tail(&devlink_port->list, &devlink->port_list);
8118 INIT_LIST_HEAD(&devlink_port->param_list);
8119 INIT_LIST_HEAD(&devlink_port->region_list);
8120 mutex_unlock(&devlink->lock);
8121 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
8122 devlink_port_type_warn_schedule(devlink_port);
8123 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8126 EXPORT_SYMBOL_GPL(devlink_port_register);
8129 * devlink_port_unregister - Unregister devlink port
8131 * @devlink_port: devlink port
8133 void devlink_port_unregister(struct devlink_port *devlink_port)
8135 struct devlink *devlink = devlink_port->devlink;
8137 devlink_port_type_warn_cancel(devlink_port);
8138 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
8139 mutex_lock(&devlink->lock);
8140 list_del(&devlink_port->list);
8141 mutex_unlock(&devlink->lock);
8142 WARN_ON(!list_empty(&devlink_port->reporter_list));
8143 WARN_ON(!list_empty(&devlink_port->region_list));
8144 mutex_destroy(&devlink_port->reporters_lock);
8146 EXPORT_SYMBOL_GPL(devlink_port_unregister);
8148 static void __devlink_port_type_set(struct devlink_port *devlink_port,
8149 enum devlink_port_type type,
8152 if (WARN_ON(!devlink_port->registered))
8154 devlink_port_type_warn_cancel(devlink_port);
8155 spin_lock_bh(&devlink_port->type_lock);
8156 devlink_port->type = type;
8157 devlink_port->type_dev = type_dev;
8158 spin_unlock_bh(&devlink_port->type_lock);
8159 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8162 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
8163 struct net_device *netdev)
8165 const struct net_device_ops *ops = netdev->netdev_ops;
8167 /* If driver registers devlink port, it should set devlink port
8168 * attributes accordingly so the compat functions are called
8169 * and the original ops are not used.
8171 if (ops->ndo_get_phys_port_name) {
8172 /* Some drivers use the same set of ndos for netdevs
8173 * that have devlink_port registered and also for
8174 * those who don't. Make sure that ndo_get_phys_port_name
8175 * returns -EOPNOTSUPP here in case it is defined.
8178 char name[IFNAMSIZ];
8181 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
8182 WARN_ON(err != -EOPNOTSUPP);
8184 if (ops->ndo_get_port_parent_id) {
8185 /* Some drivers use the same set of ndos for netdevs
8186 * that have devlink_port registered and also for
8187 * those who don't. Make sure that ndo_get_port_parent_id
8188 * returns -EOPNOTSUPP here in case it is defined.
8191 struct netdev_phys_item_id ppid;
8194 err = ops->ndo_get_port_parent_id(netdev, &ppid);
8195 WARN_ON(err != -EOPNOTSUPP);
8200 * devlink_port_type_eth_set - Set port type to Ethernet
8202 * @devlink_port: devlink port
8203 * @netdev: related netdevice
8205 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
8206 struct net_device *netdev)
8209 devlink_port_type_netdev_checks(devlink_port, netdev);
8211 dev_warn(devlink_port->devlink->dev,
8212 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
8213 devlink_port->index);
8215 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
8217 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
8220 * devlink_port_type_ib_set - Set port type to InfiniBand
8222 * @devlink_port: devlink port
8223 * @ibdev: related IB device
8225 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
8226 struct ib_device *ibdev)
8228 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
8230 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
8233 * devlink_port_type_clear - Clear port type
8235 * @devlink_port: devlink port
8237 void devlink_port_type_clear(struct devlink_port *devlink_port)
8239 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
8240 devlink_port_type_warn_schedule(devlink_port);
8242 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
8244 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
8245 enum devlink_port_flavour flavour)
8247 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8249 if (WARN_ON(devlink_port->registered))
8251 devlink_port->attrs_set = true;
8252 attrs->flavour = flavour;
8253 if (attrs->switch_id.id_len) {
8254 devlink_port->switch_port = true;
8255 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
8256 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
8258 devlink_port->switch_port = false;
8264 * devlink_port_attrs_set - Set port attributes
8266 * @devlink_port: devlink port
8267 * @attrs: devlink port attrs
8269 void devlink_port_attrs_set(struct devlink_port *devlink_port,
8270 struct devlink_port_attrs *attrs)
8274 devlink_port->attrs = *attrs;
8275 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
8278 WARN_ON(attrs->splittable && attrs->split);
8280 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
8283 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
8285 * @devlink_port: devlink port
8286 * @controller: associated controller number for the devlink port instance
8287 * @pf: associated PF for the devlink port instance
8288 * @external: indicates if the port is for an external controller
8290 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
8291 u16 pf, bool external)
8293 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8296 ret = __devlink_port_attrs_set(devlink_port,
8297 DEVLINK_PORT_FLAVOUR_PCI_PF);
8300 attrs->pci_pf.controller = controller;
8301 attrs->pci_pf.pf = pf;
8302 attrs->pci_pf.external = external;
8304 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
8307 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
8309 * @devlink_port: devlink port
8310 * @controller: associated controller number for the devlink port instance
8311 * @pf: associated PF for the devlink port instance
8312 * @vf: associated VF of a PF for the devlink port instance
8313 * @external: indicates if the port is for an external controller
8315 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
8316 u16 pf, u16 vf, bool external)
8318 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8321 ret = __devlink_port_attrs_set(devlink_port,
8322 DEVLINK_PORT_FLAVOUR_PCI_VF);
8325 attrs->pci_vf.controller = controller;
8326 attrs->pci_vf.pf = pf;
8327 attrs->pci_vf.vf = vf;
8328 attrs->pci_vf.external = external;
8330 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
8332 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
8333 char *name, size_t len)
8335 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8338 if (!devlink_port->attrs_set)
8341 switch (attrs->flavour) {
8342 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
8343 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
8345 n = snprintf(name, len, "p%u", attrs->phys.port_number);
8347 n = snprintf(name, len, "p%us%u",
8348 attrs->phys.port_number,
8349 attrs->phys.split_subport_number);
8351 case DEVLINK_PORT_FLAVOUR_CPU:
8352 case DEVLINK_PORT_FLAVOUR_DSA:
8353 case DEVLINK_PORT_FLAVOUR_UNUSED:
8354 /* As CPU and DSA ports do not have a netdevice associated
8355 * case should not ever happen.
8359 case DEVLINK_PORT_FLAVOUR_PCI_PF:
8360 if (attrs->pci_pf.external) {
8361 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
8367 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
8369 case DEVLINK_PORT_FLAVOUR_PCI_VF:
8370 if (attrs->pci_vf.external) {
8371 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
8377 n = snprintf(name, len, "pf%uvf%u",
8378 attrs->pci_vf.pf, attrs->pci_vf.vf);
8388 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
8389 u32 size, u16 ingress_pools_count,
8390 u16 egress_pools_count, u16 ingress_tc_count,
8391 u16 egress_tc_count)
8393 struct devlink_sb *devlink_sb;
8396 mutex_lock(&devlink->lock);
8397 if (devlink_sb_index_exists(devlink, sb_index)) {
8402 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
8407 devlink_sb->index = sb_index;
8408 devlink_sb->size = size;
8409 devlink_sb->ingress_pools_count = ingress_pools_count;
8410 devlink_sb->egress_pools_count = egress_pools_count;
8411 devlink_sb->ingress_tc_count = ingress_tc_count;
8412 devlink_sb->egress_tc_count = egress_tc_count;
8413 list_add_tail(&devlink_sb->list, &devlink->sb_list);
8415 mutex_unlock(&devlink->lock);
8418 EXPORT_SYMBOL_GPL(devlink_sb_register);
8420 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
8422 struct devlink_sb *devlink_sb;
8424 mutex_lock(&devlink->lock);
8425 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
8426 WARN_ON(!devlink_sb);
8427 list_del(&devlink_sb->list);
8428 mutex_unlock(&devlink->lock);
8431 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
8434 * devlink_dpipe_headers_register - register dpipe headers
8437 * @dpipe_headers: dpipe header array
8439 * Register the headers supported by hardware.
8441 int devlink_dpipe_headers_register(struct devlink *devlink,
8442 struct devlink_dpipe_headers *dpipe_headers)
8444 mutex_lock(&devlink->lock);
8445 devlink->dpipe_headers = dpipe_headers;
8446 mutex_unlock(&devlink->lock);
8449 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
8452 * devlink_dpipe_headers_unregister - unregister dpipe headers
8456 * Unregister the headers supported by hardware.
8458 void devlink_dpipe_headers_unregister(struct devlink *devlink)
8460 mutex_lock(&devlink->lock);
8461 devlink->dpipe_headers = NULL;
8462 mutex_unlock(&devlink->lock);
8464 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
8467 * devlink_dpipe_table_counter_enabled - check if counter allocation
8470 * @table_name: tables name
8472 * Used by driver to check if counter allocation is required.
8473 * After counter allocation is turned on the table entries
8474 * are updated to include counter statistics.
8476 * After that point on the driver must respect the counter
8477 * state so that each entry added to the table is added
8480 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
8481 const char *table_name)
8483 struct devlink_dpipe_table *table;
8487 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8488 table_name, devlink);
8491 enabled = table->counters_enabled;
8495 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
8498 * devlink_dpipe_table_register - register dpipe table
8501 * @table_name: table name
8502 * @table_ops: table ops
8504 * @counter_control_extern: external control for counters
8506 int devlink_dpipe_table_register(struct devlink *devlink,
8507 const char *table_name,
8508 struct devlink_dpipe_table_ops *table_ops,
8509 void *priv, bool counter_control_extern)
8511 struct devlink_dpipe_table *table;
8514 if (WARN_ON(!table_ops->size_get))
8517 mutex_lock(&devlink->lock);
8519 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
8525 table = kzalloc(sizeof(*table), GFP_KERNEL);
8531 table->name = table_name;
8532 table->table_ops = table_ops;
8534 table->counter_control_extern = counter_control_extern;
8536 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
8538 mutex_unlock(&devlink->lock);
8541 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
8544 * devlink_dpipe_table_unregister - unregister dpipe table
8547 * @table_name: table name
8549 void devlink_dpipe_table_unregister(struct devlink *devlink,
8550 const char *table_name)
8552 struct devlink_dpipe_table *table;
8554 mutex_lock(&devlink->lock);
8555 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8556 table_name, devlink);
8559 list_del_rcu(&table->list);
8560 mutex_unlock(&devlink->lock);
8561 kfree_rcu(table, rcu);
8564 mutex_unlock(&devlink->lock);
8566 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
8569 * devlink_resource_register - devlink resource register
8572 * @resource_name: resource's name
8573 * @resource_size: resource's size
8574 * @resource_id: resource's id
8575 * @parent_resource_id: resource's parent id
8576 * @size_params: size parameters
8578 int devlink_resource_register(struct devlink *devlink,
8579 const char *resource_name,
8582 u64 parent_resource_id,
8583 const struct devlink_resource_size_params *size_params)
8585 struct devlink_resource *resource;
8586 struct list_head *resource_list;
8590 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
8592 mutex_lock(&devlink->lock);
8593 resource = devlink_resource_find(devlink, NULL, resource_id);
8599 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
8605 if (top_hierarchy) {
8606 resource_list = &devlink->resource_list;
8608 struct devlink_resource *parent_resource;
8610 parent_resource = devlink_resource_find(devlink, NULL,
8611 parent_resource_id);
8612 if (parent_resource) {
8613 resource_list = &parent_resource->resource_list;
8614 resource->parent = parent_resource;
8622 resource->name = resource_name;
8623 resource->size = resource_size;
8624 resource->size_new = resource_size;
8625 resource->id = resource_id;
8626 resource->size_valid = true;
8627 memcpy(&resource->size_params, size_params,
8628 sizeof(resource->size_params));
8629 INIT_LIST_HEAD(&resource->resource_list);
8630 list_add_tail(&resource->list, resource_list);
8632 mutex_unlock(&devlink->lock);
8635 EXPORT_SYMBOL_GPL(devlink_resource_register);
8638 * devlink_resources_unregister - free all resources
8641 * @resource: resource
8643 void devlink_resources_unregister(struct devlink *devlink,
8644 struct devlink_resource *resource)
8646 struct devlink_resource *tmp, *child_resource;
8647 struct list_head *resource_list;
8650 resource_list = &resource->resource_list;
8652 resource_list = &devlink->resource_list;
8655 mutex_lock(&devlink->lock);
8657 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
8658 devlink_resources_unregister(devlink, child_resource);
8659 list_del(&child_resource->list);
8660 kfree(child_resource);
8664 mutex_unlock(&devlink->lock);
8666 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
8669 * devlink_resource_size_get - get and update size
8672 * @resource_id: the requested resource id
8673 * @p_resource_size: ptr to update
8675 int devlink_resource_size_get(struct devlink *devlink,
8677 u64 *p_resource_size)
8679 struct devlink_resource *resource;
8682 mutex_lock(&devlink->lock);
8683 resource = devlink_resource_find(devlink, NULL, resource_id);
8688 *p_resource_size = resource->size_new;
8689 resource->size = resource->size_new;
8691 mutex_unlock(&devlink->lock);
8694 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
8697 * devlink_dpipe_table_resource_set - set the resource id
8700 * @table_name: table name
8701 * @resource_id: resource id
8702 * @resource_units: number of resource's units consumed per table's entry
8704 int devlink_dpipe_table_resource_set(struct devlink *devlink,
8705 const char *table_name, u64 resource_id,
8708 struct devlink_dpipe_table *table;
8711 mutex_lock(&devlink->lock);
8712 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8713 table_name, devlink);
8718 table->resource_id = resource_id;
8719 table->resource_units = resource_units;
8720 table->resource_valid = true;
8722 mutex_unlock(&devlink->lock);
8725 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
8728 * devlink_resource_occ_get_register - register occupancy getter
8731 * @resource_id: resource id
8732 * @occ_get: occupancy getter callback
8733 * @occ_get_priv: occupancy getter callback priv
8735 void devlink_resource_occ_get_register(struct devlink *devlink,
8737 devlink_resource_occ_get_t *occ_get,
8740 struct devlink_resource *resource;
8742 mutex_lock(&devlink->lock);
8743 resource = devlink_resource_find(devlink, NULL, resource_id);
8744 if (WARN_ON(!resource))
8746 WARN_ON(resource->occ_get);
8748 resource->occ_get = occ_get;
8749 resource->occ_get_priv = occ_get_priv;
8751 mutex_unlock(&devlink->lock);
8753 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
8756 * devlink_resource_occ_get_unregister - unregister occupancy getter
8759 * @resource_id: resource id
8761 void devlink_resource_occ_get_unregister(struct devlink *devlink,
8764 struct devlink_resource *resource;
8766 mutex_lock(&devlink->lock);
8767 resource = devlink_resource_find(devlink, NULL, resource_id);
8768 if (WARN_ON(!resource))
8770 WARN_ON(!resource->occ_get);
8772 resource->occ_get = NULL;
8773 resource->occ_get_priv = NULL;
8775 mutex_unlock(&devlink->lock);
8777 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
8779 static int devlink_param_verify(const struct devlink_param *param)
8781 if (!param || !param->name || !param->supported_cmodes)
8784 return devlink_param_generic_verify(param);
8786 return devlink_param_driver_verify(param);
8789 static int __devlink_params_register(struct devlink *devlink,
8790 unsigned int port_index,
8791 struct list_head *param_list,
8792 const struct devlink_param *params,
8793 size_t params_count,
8794 enum devlink_command reg_cmd,
8795 enum devlink_command unreg_cmd)
8797 const struct devlink_param *param = params;
8801 mutex_lock(&devlink->lock);
8802 for (i = 0; i < params_count; i++, param++) {
8803 err = devlink_param_verify(param);
8807 err = devlink_param_register_one(devlink, port_index,
8808 param_list, param, reg_cmd);
8813 mutex_unlock(&devlink->lock);
8819 for (param--; i > 0; i--, param--)
8820 devlink_param_unregister_one(devlink, port_index, param_list,
8823 mutex_unlock(&devlink->lock);
8827 static void __devlink_params_unregister(struct devlink *devlink,
8828 unsigned int port_index,
8829 struct list_head *param_list,
8830 const struct devlink_param *params,
8831 size_t params_count,
8832 enum devlink_command cmd)
8834 const struct devlink_param *param = params;
8837 mutex_lock(&devlink->lock);
8838 for (i = 0; i < params_count; i++, param++)
8839 devlink_param_unregister_one(devlink, 0, param_list, param,
8841 mutex_unlock(&devlink->lock);
8845 * devlink_params_register - register configuration parameters
8848 * @params: configuration parameters array
8849 * @params_count: number of parameters provided
8851 * Register the configuration parameters supported by the driver.
8853 int devlink_params_register(struct devlink *devlink,
8854 const struct devlink_param *params,
8855 size_t params_count)
8857 return __devlink_params_register(devlink, 0, &devlink->param_list,
8858 params, params_count,
8859 DEVLINK_CMD_PARAM_NEW,
8860 DEVLINK_CMD_PARAM_DEL);
8862 EXPORT_SYMBOL_GPL(devlink_params_register);
8865 * devlink_params_unregister - unregister configuration parameters
8867 * @params: configuration parameters to unregister
8868 * @params_count: number of parameters provided
8870 void devlink_params_unregister(struct devlink *devlink,
8871 const struct devlink_param *params,
8872 size_t params_count)
8874 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
8875 params, params_count,
8876 DEVLINK_CMD_PARAM_DEL);
8878 EXPORT_SYMBOL_GPL(devlink_params_unregister);
8881 * devlink_params_publish - publish configuration parameters
8885 * Publish previously registered configuration parameters.
8887 void devlink_params_publish(struct devlink *devlink)
8889 struct devlink_param_item *param_item;
8891 list_for_each_entry(param_item, &devlink->param_list, list) {
8892 if (param_item->published)
8894 param_item->published = true;
8895 devlink_param_notify(devlink, 0, param_item,
8896 DEVLINK_CMD_PARAM_NEW);
8899 EXPORT_SYMBOL_GPL(devlink_params_publish);
8902 * devlink_params_unpublish - unpublish configuration parameters
8906 * Unpublish previously registered configuration parameters.
8908 void devlink_params_unpublish(struct devlink *devlink)
8910 struct devlink_param_item *param_item;
8912 list_for_each_entry(param_item, &devlink->param_list, list) {
8913 if (!param_item->published)
8915 param_item->published = false;
8916 devlink_param_notify(devlink, 0, param_item,
8917 DEVLINK_CMD_PARAM_DEL);
8920 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
8923 * devlink_port_params_register - register port configuration parameters
8925 * @devlink_port: devlink port
8926 * @params: configuration parameters array
8927 * @params_count: number of parameters provided
8929 * Register the configuration parameters supported by the port.
8931 int devlink_port_params_register(struct devlink_port *devlink_port,
8932 const struct devlink_param *params,
8933 size_t params_count)
8935 return __devlink_params_register(devlink_port->devlink,
8936 devlink_port->index,
8937 &devlink_port->param_list, params,
8939 DEVLINK_CMD_PORT_PARAM_NEW,
8940 DEVLINK_CMD_PORT_PARAM_DEL);
8942 EXPORT_SYMBOL_GPL(devlink_port_params_register);
8945 * devlink_port_params_unregister - unregister port configuration
8948 * @devlink_port: devlink port
8949 * @params: configuration parameters array
8950 * @params_count: number of parameters provided
8952 void devlink_port_params_unregister(struct devlink_port *devlink_port,
8953 const struct devlink_param *params,
8954 size_t params_count)
8956 return __devlink_params_unregister(devlink_port->devlink,
8957 devlink_port->index,
8958 &devlink_port->param_list,
8959 params, params_count,
8960 DEVLINK_CMD_PORT_PARAM_DEL);
8962 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
8965 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
8966 union devlink_param_value *init_val)
8968 struct devlink_param_item *param_item;
8970 param_item = devlink_param_find_by_id(param_list, param_id);
8974 if (!param_item->driverinit_value_valid ||
8975 !devlink_param_cmode_is_supported(param_item->param,
8976 DEVLINK_PARAM_CMODE_DRIVERINIT))
8979 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8980 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
8982 *init_val = param_item->driverinit_value;
8988 __devlink_param_driverinit_value_set(struct devlink *devlink,
8989 unsigned int port_index,
8990 struct list_head *param_list, u32 param_id,
8991 union devlink_param_value init_val,
8992 enum devlink_command cmd)
8994 struct devlink_param_item *param_item;
8996 param_item = devlink_param_find_by_id(param_list, param_id);
9000 if (!devlink_param_cmode_is_supported(param_item->param,
9001 DEVLINK_PARAM_CMODE_DRIVERINIT))
9004 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
9005 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
9007 param_item->driverinit_value = init_val;
9008 param_item->driverinit_value_valid = true;
9010 devlink_param_notify(devlink, port_index, param_item, cmd);
9015 * devlink_param_driverinit_value_get - get configuration parameter
9016 * value for driver initializing
9019 * @param_id: parameter ID
9020 * @init_val: value of parameter in driverinit configuration mode
9022 * This function should be used by the driver to get driverinit
9023 * configuration for initialization after reload command.
9025 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
9026 union devlink_param_value *init_val)
9028 if (!devlink_reload_supported(devlink->ops))
9031 return __devlink_param_driverinit_value_get(&devlink->param_list,
9032 param_id, init_val);
9034 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
9037 * devlink_param_driverinit_value_set - set value of configuration
9038 * parameter for driverinit
9039 * configuration mode
9042 * @param_id: parameter ID
9043 * @init_val: value of parameter to set for driverinit configuration mode
9045 * This function should be used by the driver to set driverinit
9046 * configuration mode default value.
9048 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
9049 union devlink_param_value init_val)
9051 return __devlink_param_driverinit_value_set(devlink, 0,
9052 &devlink->param_list,
9054 DEVLINK_CMD_PARAM_NEW);
9056 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
9059 * devlink_port_param_driverinit_value_get - get configuration parameter
9060 * value for driver initializing
9062 * @devlink_port: devlink_port
9063 * @param_id: parameter ID
9064 * @init_val: value of parameter in driverinit configuration mode
9066 * This function should be used by the driver to get driverinit
9067 * configuration for initialization after reload command.
9069 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
9071 union devlink_param_value *init_val)
9073 struct devlink *devlink = devlink_port->devlink;
9075 if (!devlink_reload_supported(devlink->ops))
9078 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
9079 param_id, init_val);
9081 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
9084 * devlink_port_param_driverinit_value_set - set value of configuration
9085 * parameter for driverinit
9086 * configuration mode
9088 * @devlink_port: devlink_port
9089 * @param_id: parameter ID
9090 * @init_val: value of parameter to set for driverinit configuration mode
9092 * This function should be used by the driver to set driverinit
9093 * configuration mode default value.
9095 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
9097 union devlink_param_value init_val)
9099 return __devlink_param_driverinit_value_set(devlink_port->devlink,
9100 devlink_port->index,
9101 &devlink_port->param_list,
9103 DEVLINK_CMD_PORT_PARAM_NEW);
9105 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
9108 * devlink_param_value_changed - notify devlink on a parameter's value
9109 * change. Should be called by the driver
9110 * right after the change.
9113 * @param_id: parameter ID
9115 * This function should be used by the driver to notify devlink on value
9116 * change, excluding driverinit configuration mode.
9117 * For driverinit configuration mode driver should use the function
9119 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
9121 struct devlink_param_item *param_item;
9123 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
9124 WARN_ON(!param_item);
9126 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
9128 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
9131 * devlink_port_param_value_changed - notify devlink on a parameter's value
9132 * change. Should be called by the driver
9133 * right after the change.
9135 * @devlink_port: devlink_port
9136 * @param_id: parameter ID
9138 * This function should be used by the driver to notify devlink on value
9139 * change, excluding driverinit configuration mode.
9140 * For driverinit configuration mode driver should use the function
9141 * devlink_port_param_driverinit_value_set() instead.
9143 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
9146 struct devlink_param_item *param_item;
9148 param_item = devlink_param_find_by_id(&devlink_port->param_list,
9150 WARN_ON(!param_item);
9152 devlink_param_notify(devlink_port->devlink, devlink_port->index,
9153 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
9155 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
9158 * devlink_param_value_str_fill - Safely fill-up the string preventing
9159 * from overflow of the preallocated buffer
9161 * @dst_val: destination devlink_param_value
9162 * @src: source buffer
9164 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
9169 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
9170 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
9172 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
9175 * devlink_region_create - create a new address region
9178 * @ops: region operations and name
9179 * @region_max_snapshots: Maximum supported number of snapshots for region
9180 * @region_size: size of region
9182 struct devlink_region *
9183 devlink_region_create(struct devlink *devlink,
9184 const struct devlink_region_ops *ops,
9185 u32 region_max_snapshots, u64 region_size)
9187 struct devlink_region *region;
9190 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9191 return ERR_PTR(-EINVAL);
9193 mutex_lock(&devlink->lock);
9195 if (devlink_region_get_by_name(devlink, ops->name)) {
9200 region = kzalloc(sizeof(*region), GFP_KERNEL);
9206 region->devlink = devlink;
9207 region->max_snapshots = region_max_snapshots;
9209 region->size = region_size;
9210 INIT_LIST_HEAD(®ion->snapshot_list);
9211 list_add_tail(®ion->list, &devlink->region_list);
9212 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9214 mutex_unlock(&devlink->lock);
9218 mutex_unlock(&devlink->lock);
9219 return ERR_PTR(err);
9221 EXPORT_SYMBOL_GPL(devlink_region_create);
9224 * devlink_port_region_create - create a new address region for a port
9226 * @port: devlink port
9227 * @ops: region operations and name
9228 * @region_max_snapshots: Maximum supported number of snapshots for region
9229 * @region_size: size of region
9231 struct devlink_region *
9232 devlink_port_region_create(struct devlink_port *port,
9233 const struct devlink_port_region_ops *ops,
9234 u32 region_max_snapshots, u64 region_size)
9236 struct devlink *devlink = port->devlink;
9237 struct devlink_region *region;
9240 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9241 return ERR_PTR(-EINVAL);
9243 mutex_lock(&devlink->lock);
9245 if (devlink_port_region_get_by_name(port, ops->name)) {
9250 region = kzalloc(sizeof(*region), GFP_KERNEL);
9256 region->devlink = devlink;
9257 region->port = port;
9258 region->max_snapshots = region_max_snapshots;
9259 region->port_ops = ops;
9260 region->size = region_size;
9261 INIT_LIST_HEAD(®ion->snapshot_list);
9262 list_add_tail(®ion->list, &port->region_list);
9263 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9265 mutex_unlock(&devlink->lock);
9269 mutex_unlock(&devlink->lock);
9270 return ERR_PTR(err);
9272 EXPORT_SYMBOL_GPL(devlink_port_region_create);
9275 * devlink_region_destroy - destroy address region
9277 * @region: devlink region to destroy
9279 void devlink_region_destroy(struct devlink_region *region)
9281 struct devlink *devlink = region->devlink;
9282 struct devlink_snapshot *snapshot, *ts;
9284 mutex_lock(&devlink->lock);
9286 /* Free all snapshots of region */
9287 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
9288 devlink_region_snapshot_del(region, snapshot);
9290 list_del(®ion->list);
9292 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9293 mutex_unlock(&devlink->lock);
9296 EXPORT_SYMBOL_GPL(devlink_region_destroy);
9299 * devlink_region_snapshot_id_get - get snapshot ID
9301 * This callback should be called when adding a new snapshot,
9302 * Driver should use the same id for multiple snapshots taken
9303 * on multiple regions at the same time/by the same trigger.
9305 * The caller of this function must use devlink_region_snapshot_id_put
9306 * when finished creating regions using this id.
9308 * Returns zero on success, or a negative error code on failure.
9311 * @id: storage to return id
9313 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
9317 mutex_lock(&devlink->lock);
9318 err = __devlink_region_snapshot_id_get(devlink, id);
9319 mutex_unlock(&devlink->lock);
9323 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
9326 * devlink_region_snapshot_id_put - put snapshot ID reference
9328 * This should be called by a driver after finishing creating snapshots
9329 * with an id. Doing so ensures that the ID can later be released in the
9330 * event that all snapshots using it have been destroyed.
9333 * @id: id to release reference on
9335 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
9337 mutex_lock(&devlink->lock);
9338 __devlink_snapshot_id_decrement(devlink, id);
9339 mutex_unlock(&devlink->lock);
9341 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
9344 * devlink_region_snapshot_create - create a new snapshot
9345 * This will add a new snapshot of a region. The snapshot
9346 * will be stored on the region struct and can be accessed
9347 * from devlink. This is useful for future analyses of snapshots.
9348 * Multiple snapshots can be created on a region.
9349 * The @snapshot_id should be obtained using the getter function.
9351 * @region: devlink region of the snapshot
9352 * @data: snapshot data
9353 * @snapshot_id: snapshot id to be created
9355 int devlink_region_snapshot_create(struct devlink_region *region,
9356 u8 *data, u32 snapshot_id)
9358 struct devlink *devlink = region->devlink;
9361 mutex_lock(&devlink->lock);
9362 err = __devlink_region_snapshot_create(region, data, snapshot_id);
9363 mutex_unlock(&devlink->lock);
9367 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
9369 #define DEVLINK_TRAP(_id, _type) \
9371 .type = DEVLINK_TRAP_TYPE_##_type, \
9372 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
9373 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
9376 static const struct devlink_trap devlink_trap_generic[] = {
9377 DEVLINK_TRAP(SMAC_MC, DROP),
9378 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
9379 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
9380 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
9381 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
9382 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
9383 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
9384 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
9385 DEVLINK_TRAP(TAIL_DROP, DROP),
9386 DEVLINK_TRAP(NON_IP_PACKET, DROP),
9387 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
9388 DEVLINK_TRAP(DIP_LB, DROP),
9389 DEVLINK_TRAP(SIP_MC, DROP),
9390 DEVLINK_TRAP(SIP_LB, DROP),
9391 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
9392 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
9393 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
9394 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
9395 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
9396 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
9397 DEVLINK_TRAP(RPF, EXCEPTION),
9398 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
9399 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
9400 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
9401 DEVLINK_TRAP(NON_ROUTABLE, DROP),
9402 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
9403 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
9404 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
9405 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
9406 DEVLINK_TRAP(STP, CONTROL),
9407 DEVLINK_TRAP(LACP, CONTROL),
9408 DEVLINK_TRAP(LLDP, CONTROL),
9409 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
9410 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
9411 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
9412 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
9413 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
9414 DEVLINK_TRAP(MLD_QUERY, CONTROL),
9415 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
9416 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
9417 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
9418 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
9419 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
9420 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
9421 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
9422 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
9423 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
9424 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
9425 DEVLINK_TRAP(IPV4_BFD, CONTROL),
9426 DEVLINK_TRAP(IPV6_BFD, CONTROL),
9427 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
9428 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
9429 DEVLINK_TRAP(IPV4_BGP, CONTROL),
9430 DEVLINK_TRAP(IPV6_BGP, CONTROL),
9431 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
9432 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
9433 DEVLINK_TRAP(IPV4_PIM, CONTROL),
9434 DEVLINK_TRAP(IPV6_PIM, CONTROL),
9435 DEVLINK_TRAP(UC_LB, CONTROL),
9436 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
9437 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
9438 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
9439 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
9440 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
9441 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
9442 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
9443 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
9444 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
9445 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
9446 DEVLINK_TRAP(PTP_EVENT, CONTROL),
9447 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
9448 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
9449 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
9450 DEVLINK_TRAP(EARLY_DROP, DROP),
9451 DEVLINK_TRAP(VXLAN_PARSING, DROP),
9452 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
9453 DEVLINK_TRAP(VLAN_PARSING, DROP),
9454 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
9455 DEVLINK_TRAP(MPLS_PARSING, DROP),
9456 DEVLINK_TRAP(ARP_PARSING, DROP),
9457 DEVLINK_TRAP(IP_1_PARSING, DROP),
9458 DEVLINK_TRAP(IP_N_PARSING, DROP),
9459 DEVLINK_TRAP(GRE_PARSING, DROP),
9460 DEVLINK_TRAP(UDP_PARSING, DROP),
9461 DEVLINK_TRAP(TCP_PARSING, DROP),
9462 DEVLINK_TRAP(IPSEC_PARSING, DROP),
9463 DEVLINK_TRAP(SCTP_PARSING, DROP),
9464 DEVLINK_TRAP(DCCP_PARSING, DROP),
9465 DEVLINK_TRAP(GTP_PARSING, DROP),
9466 DEVLINK_TRAP(ESP_PARSING, DROP),
9469 #define DEVLINK_TRAP_GROUP(_id) \
9471 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
9472 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
9475 static const struct devlink_trap_group devlink_trap_group_generic[] = {
9476 DEVLINK_TRAP_GROUP(L2_DROPS),
9477 DEVLINK_TRAP_GROUP(L3_DROPS),
9478 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
9479 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
9480 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
9481 DEVLINK_TRAP_GROUP(ACL_DROPS),
9482 DEVLINK_TRAP_GROUP(STP),
9483 DEVLINK_TRAP_GROUP(LACP),
9484 DEVLINK_TRAP_GROUP(LLDP),
9485 DEVLINK_TRAP_GROUP(MC_SNOOPING),
9486 DEVLINK_TRAP_GROUP(DHCP),
9487 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
9488 DEVLINK_TRAP_GROUP(BFD),
9489 DEVLINK_TRAP_GROUP(OSPF),
9490 DEVLINK_TRAP_GROUP(BGP),
9491 DEVLINK_TRAP_GROUP(VRRP),
9492 DEVLINK_TRAP_GROUP(PIM),
9493 DEVLINK_TRAP_GROUP(UC_LB),
9494 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
9495 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
9496 DEVLINK_TRAP_GROUP(IPV6),
9497 DEVLINK_TRAP_GROUP(PTP_EVENT),
9498 DEVLINK_TRAP_GROUP(PTP_GENERAL),
9499 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
9500 DEVLINK_TRAP_GROUP(ACL_TRAP),
9501 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
9504 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
9506 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
9509 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
9512 if (trap->type != devlink_trap_generic[trap->id].type)
9518 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
9522 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
9525 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
9526 if (!strcmp(trap->name, devlink_trap_generic[i].name))
9533 static int devlink_trap_verify(const struct devlink_trap *trap)
9535 if (!trap || !trap->name)
9539 return devlink_trap_generic_verify(trap);
9541 return devlink_trap_driver_verify(trap);
9545 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
9547 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9550 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
9557 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
9561 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9564 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
9565 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
9572 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
9575 return devlink_trap_group_generic_verify(group);
9577 return devlink_trap_group_driver_verify(group);
9581 devlink_trap_group_notify(struct devlink *devlink,
9582 const struct devlink_trap_group_item *group_item,
9583 enum devlink_command cmd)
9585 struct sk_buff *msg;
9588 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
9589 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
9591 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9595 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
9602 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9603 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9607 devlink_trap_item_group_link(struct devlink *devlink,
9608 struct devlink_trap_item *trap_item)
9610 u16 group_id = trap_item->trap->init_group_id;
9611 struct devlink_trap_group_item *group_item;
9613 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
9614 if (WARN_ON_ONCE(!group_item))
9617 trap_item->group_item = group_item;
9622 static void devlink_trap_notify(struct devlink *devlink,
9623 const struct devlink_trap_item *trap_item,
9624 enum devlink_command cmd)
9626 struct sk_buff *msg;
9629 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
9630 cmd != DEVLINK_CMD_TRAP_DEL);
9632 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9636 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
9642 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9643 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9647 devlink_trap_register(struct devlink *devlink,
9648 const struct devlink_trap *trap, void *priv)
9650 struct devlink_trap_item *trap_item;
9653 if (devlink_trap_item_lookup(devlink, trap->name))
9656 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
9660 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9661 if (!trap_item->stats) {
9663 goto err_stats_alloc;
9666 trap_item->trap = trap;
9667 trap_item->action = trap->init_action;
9668 trap_item->priv = priv;
9670 err = devlink_trap_item_group_link(devlink, trap_item);
9672 goto err_group_link;
9674 err = devlink->ops->trap_init(devlink, trap, trap_item);
9678 list_add_tail(&trap_item->list, &devlink->trap_list);
9679 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9685 free_percpu(trap_item->stats);
9691 static void devlink_trap_unregister(struct devlink *devlink,
9692 const struct devlink_trap *trap)
9694 struct devlink_trap_item *trap_item;
9696 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9697 if (WARN_ON_ONCE(!trap_item))
9700 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9701 list_del(&trap_item->list);
9702 if (devlink->ops->trap_fini)
9703 devlink->ops->trap_fini(devlink, trap, trap_item);
9704 free_percpu(trap_item->stats);
9708 static void devlink_trap_disable(struct devlink *devlink,
9709 const struct devlink_trap *trap)
9711 struct devlink_trap_item *trap_item;
9713 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9714 if (WARN_ON_ONCE(!trap_item))
9717 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
9719 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
9723 * devlink_traps_register - Register packet traps with devlink.
9724 * @devlink: devlink.
9725 * @traps: Packet traps.
9726 * @traps_count: Count of provided packet traps.
9727 * @priv: Driver private information.
9729 * Return: Non-zero value on failure.
9731 int devlink_traps_register(struct devlink *devlink,
9732 const struct devlink_trap *traps,
9733 size_t traps_count, void *priv)
9737 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
9740 mutex_lock(&devlink->lock);
9741 for (i = 0; i < traps_count; i++) {
9742 const struct devlink_trap *trap = &traps[i];
9744 err = devlink_trap_verify(trap);
9746 goto err_trap_verify;
9748 err = devlink_trap_register(devlink, trap, priv);
9750 goto err_trap_register;
9752 mutex_unlock(&devlink->lock);
9758 for (i--; i >= 0; i--)
9759 devlink_trap_unregister(devlink, &traps[i]);
9760 mutex_unlock(&devlink->lock);
9763 EXPORT_SYMBOL_GPL(devlink_traps_register);
9766 * devlink_traps_unregister - Unregister packet traps from devlink.
9767 * @devlink: devlink.
9768 * @traps: Packet traps.
9769 * @traps_count: Count of provided packet traps.
9771 void devlink_traps_unregister(struct devlink *devlink,
9772 const struct devlink_trap *traps,
9777 mutex_lock(&devlink->lock);
9778 /* Make sure we do not have any packets in-flight while unregistering
9779 * traps by disabling all of them and waiting for a grace period.
9781 for (i = traps_count - 1; i >= 0; i--)
9782 devlink_trap_disable(devlink, &traps[i]);
9784 for (i = traps_count - 1; i >= 0; i--)
9785 devlink_trap_unregister(devlink, &traps[i]);
9786 mutex_unlock(&devlink->lock);
9788 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
9791 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
9794 struct devlink_stats *stats;
9796 stats = this_cpu_ptr(trap_stats);
9797 u64_stats_update_begin(&stats->syncp);
9798 stats->rx_bytes += skb_len;
9799 stats->rx_packets++;
9800 u64_stats_update_end(&stats->syncp);
9804 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
9805 const struct devlink_trap_item *trap_item,
9806 struct devlink_port *in_devlink_port,
9807 const struct flow_action_cookie *fa_cookie)
9809 metadata->trap_name = trap_item->trap->name;
9810 metadata->trap_group_name = trap_item->group_item->group->name;
9811 metadata->fa_cookie = fa_cookie;
9812 metadata->trap_type = trap_item->trap->type;
9814 spin_lock(&in_devlink_port->type_lock);
9815 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9816 metadata->input_dev = in_devlink_port->type_dev;
9817 spin_unlock(&in_devlink_port->type_lock);
9821 * devlink_trap_report - Report trapped packet to drop monitor.
9822 * @devlink: devlink.
9823 * @skb: Trapped packet.
9824 * @trap_ctx: Trap context.
9825 * @in_devlink_port: Input devlink port.
9826 * @fa_cookie: Flow action cookie. Could be NULL.
9828 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
9829 void *trap_ctx, struct devlink_port *in_devlink_port,
9830 const struct flow_action_cookie *fa_cookie)
9833 struct devlink_trap_item *trap_item = trap_ctx;
9835 devlink_trap_stats_update(trap_item->stats, skb->len);
9836 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
9838 if (trace_devlink_trap_report_enabled()) {
9839 struct devlink_trap_metadata metadata = {};
9841 devlink_trap_report_metadata_set(&metadata, trap_item,
9842 in_devlink_port, fa_cookie);
9843 trace_devlink_trap_report(devlink, skb, &metadata);
9846 EXPORT_SYMBOL_GPL(devlink_trap_report);
9849 * devlink_trap_ctx_priv - Trap context to driver private information.
9850 * @trap_ctx: Trap context.
9852 * Return: Driver private information passed during registration.
9854 void *devlink_trap_ctx_priv(void *trap_ctx)
9856 struct devlink_trap_item *trap_item = trap_ctx;
9858 return trap_item->priv;
9860 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
9863 devlink_trap_group_item_policer_link(struct devlink *devlink,
9864 struct devlink_trap_group_item *group_item)
9866 u32 policer_id = group_item->group->init_policer_id;
9867 struct devlink_trap_policer_item *policer_item;
9869 if (policer_id == 0)
9872 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
9873 if (WARN_ON_ONCE(!policer_item))
9876 group_item->policer_item = policer_item;
9882 devlink_trap_group_register(struct devlink *devlink,
9883 const struct devlink_trap_group *group)
9885 struct devlink_trap_group_item *group_item;
9888 if (devlink_trap_group_item_lookup(devlink, group->name))
9891 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
9895 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9896 if (!group_item->stats) {
9898 goto err_stats_alloc;
9901 group_item->group = group;
9903 err = devlink_trap_group_item_policer_link(devlink, group_item);
9905 goto err_policer_link;
9907 if (devlink->ops->trap_group_init) {
9908 err = devlink->ops->trap_group_init(devlink, group);
9910 goto err_group_init;
9913 list_add_tail(&group_item->list, &devlink->trap_group_list);
9914 devlink_trap_group_notify(devlink, group_item,
9915 DEVLINK_CMD_TRAP_GROUP_NEW);
9921 free_percpu(group_item->stats);
9928 devlink_trap_group_unregister(struct devlink *devlink,
9929 const struct devlink_trap_group *group)
9931 struct devlink_trap_group_item *group_item;
9933 group_item = devlink_trap_group_item_lookup(devlink, group->name);
9934 if (WARN_ON_ONCE(!group_item))
9937 devlink_trap_group_notify(devlink, group_item,
9938 DEVLINK_CMD_TRAP_GROUP_DEL);
9939 list_del(&group_item->list);
9940 free_percpu(group_item->stats);
9945 * devlink_trap_groups_register - Register packet trap groups with devlink.
9946 * @devlink: devlink.
9947 * @groups: Packet trap groups.
9948 * @groups_count: Count of provided packet trap groups.
9950 * Return: Non-zero value on failure.
9952 int devlink_trap_groups_register(struct devlink *devlink,
9953 const struct devlink_trap_group *groups,
9954 size_t groups_count)
9958 mutex_lock(&devlink->lock);
9959 for (i = 0; i < groups_count; i++) {
9960 const struct devlink_trap_group *group = &groups[i];
9962 err = devlink_trap_group_verify(group);
9964 goto err_trap_group_verify;
9966 err = devlink_trap_group_register(devlink, group);
9968 goto err_trap_group_register;
9970 mutex_unlock(&devlink->lock);
9974 err_trap_group_register:
9975 err_trap_group_verify:
9976 for (i--; i >= 0; i--)
9977 devlink_trap_group_unregister(devlink, &groups[i]);
9978 mutex_unlock(&devlink->lock);
9981 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
9984 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
9985 * @devlink: devlink.
9986 * @groups: Packet trap groups.
9987 * @groups_count: Count of provided packet trap groups.
9989 void devlink_trap_groups_unregister(struct devlink *devlink,
9990 const struct devlink_trap_group *groups,
9991 size_t groups_count)
9995 mutex_lock(&devlink->lock);
9996 for (i = groups_count - 1; i >= 0; i--)
9997 devlink_trap_group_unregister(devlink, &groups[i]);
9998 mutex_unlock(&devlink->lock);
10000 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
10003 devlink_trap_policer_notify(struct devlink *devlink,
10004 const struct devlink_trap_policer_item *policer_item,
10005 enum devlink_command cmd)
10007 struct sk_buff *msg;
10010 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
10011 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
10013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10017 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
10024 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10025 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10029 devlink_trap_policer_register(struct devlink *devlink,
10030 const struct devlink_trap_policer *policer)
10032 struct devlink_trap_policer_item *policer_item;
10035 if (devlink_trap_policer_item_lookup(devlink, policer->id))
10038 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
10042 policer_item->policer = policer;
10043 policer_item->rate = policer->init_rate;
10044 policer_item->burst = policer->init_burst;
10046 if (devlink->ops->trap_policer_init) {
10047 err = devlink->ops->trap_policer_init(devlink, policer);
10049 goto err_policer_init;
10052 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
10053 devlink_trap_policer_notify(devlink, policer_item,
10054 DEVLINK_CMD_TRAP_POLICER_NEW);
10059 kfree(policer_item);
10064 devlink_trap_policer_unregister(struct devlink *devlink,
10065 const struct devlink_trap_policer *policer)
10067 struct devlink_trap_policer_item *policer_item;
10069 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
10070 if (WARN_ON_ONCE(!policer_item))
10073 devlink_trap_policer_notify(devlink, policer_item,
10074 DEVLINK_CMD_TRAP_POLICER_DEL);
10075 list_del(&policer_item->list);
10076 if (devlink->ops->trap_policer_fini)
10077 devlink->ops->trap_policer_fini(devlink, policer);
10078 kfree(policer_item);
10082 * devlink_trap_policers_register - Register packet trap policers with devlink.
10083 * @devlink: devlink.
10084 * @policers: Packet trap policers.
10085 * @policers_count: Count of provided packet trap policers.
10087 * Return: Non-zero value on failure.
10090 devlink_trap_policers_register(struct devlink *devlink,
10091 const struct devlink_trap_policer *policers,
10092 size_t policers_count)
10096 mutex_lock(&devlink->lock);
10097 for (i = 0; i < policers_count; i++) {
10098 const struct devlink_trap_policer *policer = &policers[i];
10100 if (WARN_ON(policer->id == 0 ||
10101 policer->max_rate < policer->min_rate ||
10102 policer->max_burst < policer->min_burst)) {
10104 goto err_trap_policer_verify;
10107 err = devlink_trap_policer_register(devlink, policer);
10109 goto err_trap_policer_register;
10111 mutex_unlock(&devlink->lock);
10115 err_trap_policer_register:
10116 err_trap_policer_verify:
10117 for (i--; i >= 0; i--)
10118 devlink_trap_policer_unregister(devlink, &policers[i]);
10119 mutex_unlock(&devlink->lock);
10122 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
10125 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
10126 * @devlink: devlink.
10127 * @policers: Packet trap policers.
10128 * @policers_count: Count of provided packet trap policers.
10131 devlink_trap_policers_unregister(struct devlink *devlink,
10132 const struct devlink_trap_policer *policers,
10133 size_t policers_count)
10137 mutex_lock(&devlink->lock);
10138 for (i = policers_count - 1; i >= 0; i--)
10139 devlink_trap_policer_unregister(devlink, &policers[i]);
10140 mutex_unlock(&devlink->lock);
10142 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
10144 static void __devlink_compat_running_version(struct devlink *devlink,
10145 char *buf, size_t len)
10147 const struct nlattr *nlattr;
10148 struct devlink_info_req req;
10149 struct sk_buff *msg;
10152 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10157 err = devlink->ops->info_get(devlink, &req, NULL);
10161 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
10162 const struct nlattr *kv;
10165 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
10168 nla_for_each_nested(kv, nlattr, rem_kv) {
10169 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
10172 strlcat(buf, nla_data(kv), len);
10173 strlcat(buf, " ", len);
10180 void devlink_compat_running_version(struct net_device *dev,
10181 char *buf, size_t len)
10183 struct devlink *devlink;
10188 devlink = netdev_to_devlink(dev);
10189 if (!devlink || !devlink->ops->info_get)
10192 mutex_lock(&devlink->lock);
10193 __devlink_compat_running_version(devlink, buf, len);
10194 mutex_unlock(&devlink->lock);
10201 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
10203 struct devlink_flash_update_params params = {};
10204 struct devlink *devlink;
10210 devlink = netdev_to_devlink(dev);
10211 if (!devlink || !devlink->ops->flash_update) {
10216 params.file_name = file_name;
10218 mutex_lock(&devlink->lock);
10219 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
10220 mutex_unlock(&devlink->lock);
10229 int devlink_compat_phys_port_name_get(struct net_device *dev,
10230 char *name, size_t len)
10232 struct devlink_port *devlink_port;
10234 /* RTNL mutex is held here which ensures that devlink_port
10235 * instance cannot disappear in the middle. No need to take
10236 * any devlink lock as only permanent values are accessed.
10240 devlink_port = netdev_to_devlink_port(dev);
10242 return -EOPNOTSUPP;
10244 return __devlink_port_phys_port_name_get(devlink_port, name, len);
10247 int devlink_compat_switch_id_get(struct net_device *dev,
10248 struct netdev_phys_item_id *ppid)
10250 struct devlink_port *devlink_port;
10252 /* Caller must hold RTNL mutex or reference to dev, which ensures that
10253 * devlink_port instance cannot disappear in the middle. No need to take
10254 * any devlink lock as only permanent values are accessed.
10256 devlink_port = netdev_to_devlink_port(dev);
10257 if (!devlink_port || !devlink_port->switch_port)
10258 return -EOPNOTSUPP;
10260 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
10265 static void __net_exit devlink_pernet_pre_exit(struct net *net)
10267 struct devlink *devlink;
10268 u32 actions_performed;
10271 /* In case network namespace is getting destroyed, reload
10272 * all devlink instances from this namespace into init_net.
10274 mutex_lock(&devlink_mutex);
10275 list_for_each_entry(devlink, &devlink_list, list) {
10276 if (net_eq(devlink_net(devlink), net)) {
10277 if (WARN_ON(!devlink_reload_supported(devlink->ops)))
10279 err = devlink_reload(devlink, &init_net,
10280 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
10281 DEVLINK_RELOAD_LIMIT_UNSPEC,
10282 &actions_performed, NULL);
10283 if (err && err != -EOPNOTSUPP)
10284 pr_warn("Failed to reload devlink instance into init_net\n");
10287 mutex_unlock(&devlink_mutex);
10290 static struct pernet_operations devlink_pernet_ops __net_initdata = {
10291 .pre_exit = devlink_pernet_pre_exit,
10294 static int __init devlink_init(void)
10298 err = genl_register_family(&devlink_nl_family);
10301 err = register_pernet_subsys(&devlink_pernet_ops);
10308 subsys_initcall(devlink_init);