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)
1451 goto sb_occ_get_failure;
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);
1466 genlmsg_cancel(msg, hdr);
1470 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1471 struct genl_info *info)
1473 struct devlink_port *devlink_port = info->user_ptr[1];
1474 struct devlink *devlink = devlink_port->devlink;
1475 struct devlink_sb *devlink_sb;
1476 struct sk_buff *msg;
1480 devlink_sb = devlink_sb_get_from_info(devlink, info);
1481 if (IS_ERR(devlink_sb))
1482 return PTR_ERR(devlink_sb);
1484 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1489 if (!devlink->ops->sb_port_pool_get)
1492 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1496 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1497 devlink_sb, pool_index,
1498 DEVLINK_CMD_SB_PORT_POOL_NEW,
1499 info->snd_portid, info->snd_seq, 0);
1505 return genlmsg_reply(msg, info);
1508 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1509 struct devlink *devlink,
1510 struct devlink_sb *devlink_sb,
1511 u32 portid, u32 seq)
1513 struct devlink_port *devlink_port;
1514 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1518 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1519 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1520 if (*p_idx < start) {
1524 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1528 DEVLINK_CMD_SB_PORT_POOL_NEW,
1539 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1540 struct netlink_callback *cb)
1542 struct devlink *devlink;
1543 struct devlink_sb *devlink_sb;
1544 int start = cb->args[0];
1548 mutex_lock(&devlink_mutex);
1549 list_for_each_entry(devlink, &devlink_list, list) {
1550 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1551 !devlink->ops->sb_port_pool_get)
1553 mutex_lock(&devlink->lock);
1554 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1555 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1556 devlink, devlink_sb,
1557 NETLINK_CB(cb->skb).portid,
1558 cb->nlh->nlmsg_seq);
1559 if (err == -EOPNOTSUPP) {
1562 mutex_unlock(&devlink->lock);
1566 mutex_unlock(&devlink->lock);
1569 mutex_unlock(&devlink_mutex);
1571 if (err != -EMSGSIZE)
1578 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1579 unsigned int sb_index, u16 pool_index,
1581 struct netlink_ext_ack *extack)
1584 const struct devlink_ops *ops = devlink_port->devlink->ops;
1586 if (ops->sb_port_pool_set)
1587 return ops->sb_port_pool_set(devlink_port, sb_index,
1588 pool_index, threshold, extack);
1592 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1593 struct genl_info *info)
1595 struct devlink_port *devlink_port = info->user_ptr[1];
1596 struct devlink *devlink = info->user_ptr[0];
1597 struct devlink_sb *devlink_sb;
1602 devlink_sb = devlink_sb_get_from_info(devlink, info);
1603 if (IS_ERR(devlink_sb))
1604 return PTR_ERR(devlink_sb);
1606 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1611 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1614 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1615 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1616 pool_index, threshold, info->extack);
1620 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1621 struct devlink_port *devlink_port,
1622 struct devlink_sb *devlink_sb, u16 tc_index,
1623 enum devlink_sb_pool_type pool_type,
1624 enum devlink_command cmd,
1625 u32 portid, u32 seq, int flags)
1627 const struct devlink_ops *ops = devlink->ops;
1633 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1634 tc_index, pool_type,
1635 &pool_index, &threshold);
1639 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1643 if (devlink_nl_put_handle(msg, devlink))
1644 goto nla_put_failure;
1645 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1646 goto nla_put_failure;
1647 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1648 goto nla_put_failure;
1649 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1650 goto nla_put_failure;
1651 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1652 goto nla_put_failure;
1653 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1654 goto nla_put_failure;
1655 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1656 goto nla_put_failure;
1658 if (ops->sb_occ_tc_port_bind_get) {
1662 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1664 tc_index, pool_type,
1666 if (err && err != -EOPNOTSUPP)
1669 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1670 goto nla_put_failure;
1671 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1672 goto nla_put_failure;
1676 genlmsg_end(msg, hdr);
1680 genlmsg_cancel(msg, hdr);
1684 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1685 struct genl_info *info)
1687 struct devlink_port *devlink_port = info->user_ptr[1];
1688 struct devlink *devlink = devlink_port->devlink;
1689 struct devlink_sb *devlink_sb;
1690 struct sk_buff *msg;
1691 enum devlink_sb_pool_type pool_type;
1695 devlink_sb = devlink_sb_get_from_info(devlink, info);
1696 if (IS_ERR(devlink_sb))
1697 return PTR_ERR(devlink_sb);
1699 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1703 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1704 pool_type, &tc_index);
1708 if (!devlink->ops->sb_tc_pool_bind_get)
1711 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1715 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1716 devlink_sb, tc_index, pool_type,
1717 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1725 return genlmsg_reply(msg, info);
1728 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1729 int start, int *p_idx,
1730 struct devlink *devlink,
1731 struct devlink_sb *devlink_sb,
1732 u32 portid, u32 seq)
1734 struct devlink_port *devlink_port;
1738 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1740 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1741 if (*p_idx < start) {
1745 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1749 DEVLINK_SB_POOL_TYPE_INGRESS,
1750 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1758 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1759 if (*p_idx < start) {
1763 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1767 DEVLINK_SB_POOL_TYPE_EGRESS,
1768 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1780 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1781 struct netlink_callback *cb)
1783 struct devlink *devlink;
1784 struct devlink_sb *devlink_sb;
1785 int start = cb->args[0];
1789 mutex_lock(&devlink_mutex);
1790 list_for_each_entry(devlink, &devlink_list, list) {
1791 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1792 !devlink->ops->sb_tc_pool_bind_get)
1795 mutex_lock(&devlink->lock);
1796 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1797 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1800 NETLINK_CB(cb->skb).portid,
1801 cb->nlh->nlmsg_seq);
1802 if (err == -EOPNOTSUPP) {
1805 mutex_unlock(&devlink->lock);
1809 mutex_unlock(&devlink->lock);
1812 mutex_unlock(&devlink_mutex);
1814 if (err != -EMSGSIZE)
1821 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1822 unsigned int sb_index, u16 tc_index,
1823 enum devlink_sb_pool_type pool_type,
1824 u16 pool_index, u32 threshold,
1825 struct netlink_ext_ack *extack)
1828 const struct devlink_ops *ops = devlink_port->devlink->ops;
1830 if (ops->sb_tc_pool_bind_set)
1831 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1832 tc_index, pool_type,
1833 pool_index, threshold, extack);
1837 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1838 struct genl_info *info)
1840 struct devlink_port *devlink_port = info->user_ptr[1];
1841 struct devlink *devlink = info->user_ptr[0];
1842 enum devlink_sb_pool_type pool_type;
1843 struct devlink_sb *devlink_sb;
1849 devlink_sb = devlink_sb_get_from_info(devlink, info);
1850 if (IS_ERR(devlink_sb))
1851 return PTR_ERR(devlink_sb);
1853 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1857 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1858 pool_type, &tc_index);
1862 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1867 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1870 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1871 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1872 tc_index, pool_type,
1873 pool_index, threshold, info->extack);
1876 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1877 struct genl_info *info)
1879 struct devlink *devlink = info->user_ptr[0];
1880 const struct devlink_ops *ops = devlink->ops;
1881 struct devlink_sb *devlink_sb;
1883 devlink_sb = devlink_sb_get_from_info(devlink, info);
1884 if (IS_ERR(devlink_sb))
1885 return PTR_ERR(devlink_sb);
1887 if (ops->sb_occ_snapshot)
1888 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1892 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1893 struct genl_info *info)
1895 struct devlink *devlink = info->user_ptr[0];
1896 const struct devlink_ops *ops = devlink->ops;
1897 struct devlink_sb *devlink_sb;
1899 devlink_sb = devlink_sb_get_from_info(devlink, info);
1900 if (IS_ERR(devlink_sb))
1901 return PTR_ERR(devlink_sb);
1903 if (ops->sb_occ_max_clear)
1904 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1908 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1909 enum devlink_command cmd, u32 portid,
1912 const struct devlink_ops *ops = devlink->ops;
1913 enum devlink_eswitch_encap_mode encap_mode;
1919 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1923 err = devlink_nl_put_handle(msg, devlink);
1925 goto nla_put_failure;
1927 if (ops->eswitch_mode_get) {
1928 err = ops->eswitch_mode_get(devlink, &mode);
1930 goto nla_put_failure;
1931 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1933 goto nla_put_failure;
1936 if (ops->eswitch_inline_mode_get) {
1937 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1939 goto nla_put_failure;
1940 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1943 goto nla_put_failure;
1946 if (ops->eswitch_encap_mode_get) {
1947 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1949 goto nla_put_failure;
1950 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1952 goto nla_put_failure;
1955 genlmsg_end(msg, hdr);
1959 genlmsg_cancel(msg, hdr);
1963 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1964 struct genl_info *info)
1966 struct devlink *devlink = info->user_ptr[0];
1967 struct sk_buff *msg;
1970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1974 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1975 info->snd_portid, info->snd_seq, 0);
1982 return genlmsg_reply(msg, info);
1985 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1986 struct genl_info *info)
1988 struct devlink *devlink = info->user_ptr[0];
1989 const struct devlink_ops *ops = devlink->ops;
1990 enum devlink_eswitch_encap_mode encap_mode;
1995 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1996 if (!ops->eswitch_mode_set)
1998 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1999 err = ops->eswitch_mode_set(devlink, mode, info->extack);
2004 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
2005 if (!ops->eswitch_inline_mode_set)
2007 inline_mode = nla_get_u8(
2008 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
2009 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
2015 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
2016 if (!ops->eswitch_encap_mode_set)
2018 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
2019 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
2028 int devlink_dpipe_match_put(struct sk_buff *skb,
2029 struct devlink_dpipe_match *match)
2031 struct devlink_dpipe_header *header = match->header;
2032 struct devlink_dpipe_field *field = &header->fields[match->field_id];
2033 struct nlattr *match_attr;
2035 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
2039 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
2040 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
2041 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2042 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2043 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2044 goto nla_put_failure;
2046 nla_nest_end(skb, match_attr);
2050 nla_nest_cancel(skb, match_attr);
2053 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
2055 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
2056 struct sk_buff *skb)
2058 struct nlattr *matches_attr;
2060 matches_attr = nla_nest_start_noflag(skb,
2061 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
2065 if (table->table_ops->matches_dump(table->priv, skb))
2066 goto nla_put_failure;
2068 nla_nest_end(skb, matches_attr);
2072 nla_nest_cancel(skb, matches_attr);
2076 int devlink_dpipe_action_put(struct sk_buff *skb,
2077 struct devlink_dpipe_action *action)
2079 struct devlink_dpipe_header *header = action->header;
2080 struct devlink_dpipe_field *field = &header->fields[action->field_id];
2081 struct nlattr *action_attr;
2083 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
2087 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
2088 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
2089 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2090 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2091 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2092 goto nla_put_failure;
2094 nla_nest_end(skb, action_attr);
2098 nla_nest_cancel(skb, action_attr);
2101 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
2103 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
2104 struct sk_buff *skb)
2106 struct nlattr *actions_attr;
2108 actions_attr = nla_nest_start_noflag(skb,
2109 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
2113 if (table->table_ops->actions_dump(table->priv, skb))
2114 goto nla_put_failure;
2116 nla_nest_end(skb, actions_attr);
2120 nla_nest_cancel(skb, actions_attr);
2124 static int devlink_dpipe_table_put(struct sk_buff *skb,
2125 struct devlink_dpipe_table *table)
2127 struct nlattr *table_attr;
2130 table_size = table->table_ops->size_get(table->priv);
2131 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
2135 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
2136 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
2138 goto nla_put_failure;
2139 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
2140 table->counters_enabled))
2141 goto nla_put_failure;
2143 if (table->resource_valid) {
2144 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
2145 table->resource_id, DEVLINK_ATTR_PAD) ||
2146 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
2147 table->resource_units, DEVLINK_ATTR_PAD))
2148 goto nla_put_failure;
2150 if (devlink_dpipe_matches_put(table, skb))
2151 goto nla_put_failure;
2153 if (devlink_dpipe_actions_put(table, skb))
2154 goto nla_put_failure;
2156 nla_nest_end(skb, table_attr);
2160 nla_nest_cancel(skb, table_attr);
2164 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
2165 struct genl_info *info)
2170 err = genlmsg_reply(*pskb, info);
2174 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2180 static int devlink_dpipe_tables_fill(struct genl_info *info,
2181 enum devlink_command cmd, int flags,
2182 struct list_head *dpipe_tables,
2183 const char *table_name)
2185 struct devlink *devlink = info->user_ptr[0];
2186 struct devlink_dpipe_table *table;
2187 struct nlattr *tables_attr;
2188 struct sk_buff *skb = NULL;
2189 struct nlmsghdr *nlh;
2195 table = list_first_entry(dpipe_tables,
2196 struct devlink_dpipe_table, list);
2198 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2202 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2203 &devlink_nl_family, NLM_F_MULTI, cmd);
2209 if (devlink_nl_put_handle(skb, devlink))
2210 goto nla_put_failure;
2211 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
2213 goto nla_put_failure;
2217 list_for_each_entry_from(table, dpipe_tables, list) {
2219 err = devlink_dpipe_table_put(skb, table);
2227 if (!strcmp(table->name, table_name)) {
2228 err = devlink_dpipe_table_put(skb, table);
2236 nla_nest_end(skb, tables_attr);
2237 genlmsg_end(skb, hdr);
2242 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2243 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2245 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2251 return genlmsg_reply(skb, info);
2260 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
2261 struct genl_info *info)
2263 struct devlink *devlink = info->user_ptr[0];
2264 const char *table_name = NULL;
2266 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2267 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2269 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
2270 &devlink->dpipe_table_list,
2274 static int devlink_dpipe_value_put(struct sk_buff *skb,
2275 struct devlink_dpipe_value *value)
2277 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
2278 value->value_size, value->value))
2281 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
2282 value->value_size, value->mask))
2284 if (value->mapping_valid)
2285 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
2286 value->mapping_value))
2291 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
2292 struct devlink_dpipe_value *value)
2296 if (devlink_dpipe_action_put(skb, value->action))
2298 if (devlink_dpipe_value_put(skb, value))
2303 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
2304 struct devlink_dpipe_value *values,
2305 unsigned int values_count)
2307 struct nlattr *action_attr;
2311 for (i = 0; i < values_count; i++) {
2312 action_attr = nla_nest_start_noflag(skb,
2313 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
2316 err = devlink_dpipe_action_value_put(skb, &values[i]);
2318 goto err_action_value_put;
2319 nla_nest_end(skb, action_attr);
2323 err_action_value_put:
2324 nla_nest_cancel(skb, action_attr);
2328 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
2329 struct devlink_dpipe_value *value)
2333 if (devlink_dpipe_match_put(skb, value->match))
2335 if (devlink_dpipe_value_put(skb, value))
2340 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
2341 struct devlink_dpipe_value *values,
2342 unsigned int values_count)
2344 struct nlattr *match_attr;
2348 for (i = 0; i < values_count; i++) {
2349 match_attr = nla_nest_start_noflag(skb,
2350 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
2353 err = devlink_dpipe_match_value_put(skb, &values[i]);
2355 goto err_match_value_put;
2356 nla_nest_end(skb, match_attr);
2360 err_match_value_put:
2361 nla_nest_cancel(skb, match_attr);
2365 static int devlink_dpipe_entry_put(struct sk_buff *skb,
2366 struct devlink_dpipe_entry *entry)
2368 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2371 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2375 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2377 goto nla_put_failure;
2378 if (entry->counter_valid)
2379 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2380 entry->counter, DEVLINK_ATTR_PAD))
2381 goto nla_put_failure;
2383 matches_attr = nla_nest_start_noflag(skb,
2384 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2386 goto nla_put_failure;
2388 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2389 entry->match_values_count);
2391 nla_nest_cancel(skb, matches_attr);
2392 goto err_match_values_put;
2394 nla_nest_end(skb, matches_attr);
2396 actions_attr = nla_nest_start_noflag(skb,
2397 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2399 goto nla_put_failure;
2401 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2402 entry->action_values_count);
2404 nla_nest_cancel(skb, actions_attr);
2405 goto err_action_values_put;
2407 nla_nest_end(skb, actions_attr);
2409 nla_nest_end(skb, entry_attr);
2414 err_match_values_put:
2415 err_action_values_put:
2416 nla_nest_cancel(skb, entry_attr);
2420 static struct devlink_dpipe_table *
2421 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2422 const char *table_name, struct devlink *devlink)
2424 struct devlink_dpipe_table *table;
2425 list_for_each_entry_rcu(table, dpipe_tables, list,
2426 lockdep_is_held(&devlink->lock)) {
2427 if (!strcmp(table->name, table_name))
2433 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2435 struct devlink *devlink;
2438 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2443 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2444 dump_ctx->info->snd_portid,
2445 dump_ctx->info->snd_seq,
2446 &devlink_nl_family, NLM_F_MULTI,
2449 goto nla_put_failure;
2451 devlink = dump_ctx->info->user_ptr[0];
2452 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2453 goto nla_put_failure;
2454 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2455 DEVLINK_ATTR_DPIPE_ENTRIES);
2456 if (!dump_ctx->nest)
2457 goto nla_put_failure;
2461 nlmsg_free(dump_ctx->skb);
2464 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2466 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2467 struct devlink_dpipe_entry *entry)
2469 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2471 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2473 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2475 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2476 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2479 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2481 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2484 unsigned int value_count, value_index;
2485 struct devlink_dpipe_value *value;
2487 value = entry->action_values;
2488 value_count = entry->action_values_count;
2489 for (value_index = 0; value_index < value_count; value_index++) {
2490 kfree(value[value_index].value);
2491 kfree(value[value_index].mask);
2494 value = entry->match_values;
2495 value_count = entry->match_values_count;
2496 for (value_index = 0; value_index < value_count; value_index++) {
2497 kfree(value[value_index].value);
2498 kfree(value[value_index].mask);
2501 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2503 static int devlink_dpipe_entries_fill(struct genl_info *info,
2504 enum devlink_command cmd, int flags,
2505 struct devlink_dpipe_table *table)
2507 struct devlink_dpipe_dump_ctx dump_ctx;
2508 struct nlmsghdr *nlh;
2511 dump_ctx.skb = NULL;
2513 dump_ctx.info = info;
2515 err = table->table_ops->entries_dump(table->priv,
2516 table->counters_enabled,
2522 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2523 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2525 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2530 return genlmsg_reply(dump_ctx.skb, info);
2533 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2534 struct genl_info *info)
2536 struct devlink *devlink = info->user_ptr[0];
2537 struct devlink_dpipe_table *table;
2538 const char *table_name;
2540 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2543 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2544 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2545 table_name, devlink);
2549 if (!table->table_ops->entries_dump)
2552 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2556 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2557 const struct devlink_dpipe_header *header)
2559 struct devlink_dpipe_field *field;
2560 struct nlattr *field_attr;
2563 for (i = 0; i < header->fields_count; i++) {
2564 field = &header->fields[i];
2565 field_attr = nla_nest_start_noflag(skb,
2566 DEVLINK_ATTR_DPIPE_FIELD);
2569 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2570 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2571 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2572 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2573 goto nla_put_failure;
2574 nla_nest_end(skb, field_attr);
2579 nla_nest_cancel(skb, field_attr);
2583 static int devlink_dpipe_header_put(struct sk_buff *skb,
2584 struct devlink_dpipe_header *header)
2586 struct nlattr *fields_attr, *header_attr;
2589 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
2593 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2594 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2595 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2596 goto nla_put_failure;
2598 fields_attr = nla_nest_start_noflag(skb,
2599 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2601 goto nla_put_failure;
2603 err = devlink_dpipe_fields_put(skb, header);
2605 nla_nest_cancel(skb, fields_attr);
2606 goto nla_put_failure;
2608 nla_nest_end(skb, fields_attr);
2609 nla_nest_end(skb, header_attr);
2614 nla_nest_cancel(skb, header_attr);
2618 static int devlink_dpipe_headers_fill(struct genl_info *info,
2619 enum devlink_command cmd, int flags,
2620 struct devlink_dpipe_headers *
2623 struct devlink *devlink = info->user_ptr[0];
2624 struct nlattr *headers_attr;
2625 struct sk_buff *skb = NULL;
2626 struct nlmsghdr *nlh;
2633 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2637 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2638 &devlink_nl_family, NLM_F_MULTI, cmd);
2644 if (devlink_nl_put_handle(skb, devlink))
2645 goto nla_put_failure;
2646 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2648 goto nla_put_failure;
2651 for (; i < dpipe_headers->headers_count; i++) {
2652 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2660 nla_nest_end(skb, headers_attr);
2661 genlmsg_end(skb, hdr);
2662 if (i != dpipe_headers->headers_count)
2666 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2667 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2669 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2674 return genlmsg_reply(skb, info);
2683 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2684 struct genl_info *info)
2686 struct devlink *devlink = info->user_ptr[0];
2688 if (!devlink->dpipe_headers)
2690 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2691 0, devlink->dpipe_headers);
2694 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2695 const char *table_name,
2698 struct devlink_dpipe_table *table;
2700 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2701 table_name, devlink);
2705 if (table->counter_control_extern)
2708 if (!(table->counters_enabled ^ enable))
2711 table->counters_enabled = enable;
2712 if (table->table_ops->counters_set_update)
2713 table->table_ops->counters_set_update(table->priv, enable);
2717 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2718 struct genl_info *info)
2720 struct devlink *devlink = info->user_ptr[0];
2721 const char *table_name;
2722 bool counters_enable;
2724 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2725 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2728 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2729 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2731 return devlink_dpipe_table_counters_set(devlink, table_name,
2735 static struct devlink_resource *
2736 devlink_resource_find(struct devlink *devlink,
2737 struct devlink_resource *resource, u64 resource_id)
2739 struct list_head *resource_list;
2742 resource_list = &resource->resource_list;
2744 resource_list = &devlink->resource_list;
2746 list_for_each_entry(resource, resource_list, list) {
2747 struct devlink_resource *child_resource;
2749 if (resource->id == resource_id)
2752 child_resource = devlink_resource_find(devlink, resource,
2755 return child_resource;
2761 devlink_resource_validate_children(struct devlink_resource *resource)
2763 struct devlink_resource *child_resource;
2764 bool size_valid = true;
2767 if (list_empty(&resource->resource_list))
2770 list_for_each_entry(child_resource, &resource->resource_list, list)
2771 parts_size += child_resource->size_new;
2773 if (parts_size > resource->size_new)
2776 resource->size_valid = size_valid;
2780 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2781 struct netlink_ext_ack *extack)
2786 if (size > resource->size_params.size_max) {
2787 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2791 if (size < resource->size_params.size_min) {
2792 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2796 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2798 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2805 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2806 struct genl_info *info)
2808 struct devlink *devlink = info->user_ptr[0];
2809 struct devlink_resource *resource;
2814 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2815 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2817 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2819 resource = devlink_resource_find(devlink, NULL, resource_id);
2823 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2824 err = devlink_resource_validate_size(resource, size, info->extack);
2828 resource->size_new = size;
2829 devlink_resource_validate_children(resource);
2830 if (resource->parent)
2831 devlink_resource_validate_children(resource->parent);
2836 devlink_resource_size_params_put(struct devlink_resource *resource,
2837 struct sk_buff *skb)
2839 struct devlink_resource_size_params *size_params;
2841 size_params = &resource->size_params;
2842 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2843 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2844 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2845 size_params->size_max, DEVLINK_ATTR_PAD) ||
2846 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2847 size_params->size_min, DEVLINK_ATTR_PAD) ||
2848 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2853 static int devlink_resource_occ_put(struct devlink_resource *resource,
2854 struct sk_buff *skb)
2856 if (!resource->occ_get)
2858 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2859 resource->occ_get(resource->occ_get_priv),
2863 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2864 struct devlink_resource *resource)
2866 struct devlink_resource *child_resource;
2867 struct nlattr *child_resource_attr;
2868 struct nlattr *resource_attr;
2870 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
2874 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2875 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2876 DEVLINK_ATTR_PAD) ||
2877 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2879 goto nla_put_failure;
2880 if (resource->size != resource->size_new)
2881 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2882 resource->size_new, DEVLINK_ATTR_PAD);
2883 if (devlink_resource_occ_put(resource, skb))
2884 goto nla_put_failure;
2885 if (devlink_resource_size_params_put(resource, skb))
2886 goto nla_put_failure;
2887 if (list_empty(&resource->resource_list))
2890 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2891 resource->size_valid))
2892 goto nla_put_failure;
2894 child_resource_attr = nla_nest_start_noflag(skb,
2895 DEVLINK_ATTR_RESOURCE_LIST);
2896 if (!child_resource_attr)
2897 goto nla_put_failure;
2899 list_for_each_entry(child_resource, &resource->resource_list, list) {
2900 if (devlink_resource_put(devlink, skb, child_resource))
2901 goto resource_put_failure;
2904 nla_nest_end(skb, child_resource_attr);
2906 nla_nest_end(skb, resource_attr);
2909 resource_put_failure:
2910 nla_nest_cancel(skb, child_resource_attr);
2912 nla_nest_cancel(skb, resource_attr);
2916 static int devlink_resource_fill(struct genl_info *info,
2917 enum devlink_command cmd, int flags)
2919 struct devlink *devlink = info->user_ptr[0];
2920 struct devlink_resource *resource;
2921 struct nlattr *resources_attr;
2922 struct sk_buff *skb = NULL;
2923 struct nlmsghdr *nlh;
2929 resource = list_first_entry(&devlink->resource_list,
2930 struct devlink_resource, list);
2932 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2936 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2937 &devlink_nl_family, NLM_F_MULTI, cmd);
2943 if (devlink_nl_put_handle(skb, devlink))
2944 goto nla_put_failure;
2946 resources_attr = nla_nest_start_noflag(skb,
2947 DEVLINK_ATTR_RESOURCE_LIST);
2948 if (!resources_attr)
2949 goto nla_put_failure;
2953 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2954 err = devlink_resource_put(devlink, skb, resource);
2957 goto err_resource_put;
2963 nla_nest_end(skb, resources_attr);
2964 genlmsg_end(skb, hdr);
2968 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2969 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2971 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2976 return genlmsg_reply(skb, info);
2985 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2986 struct genl_info *info)
2988 struct devlink *devlink = info->user_ptr[0];
2990 if (list_empty(&devlink->resource_list))
2993 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2997 devlink_resources_validate(struct devlink *devlink,
2998 struct devlink_resource *resource,
2999 struct genl_info *info)
3001 struct list_head *resource_list;
3005 resource_list = &resource->resource_list;
3007 resource_list = &devlink->resource_list;
3009 list_for_each_entry(resource, resource_list, list) {
3010 if (!resource->size_valid)
3012 err = devlink_resources_validate(devlink, resource, info);
3019 static struct net *devlink_netns_get(struct sk_buff *skb,
3020 struct genl_info *info)
3022 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
3023 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
3024 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
3027 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
3028 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
3029 return ERR_PTR(-EINVAL);
3032 if (netns_pid_attr) {
3033 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
3034 } else if (netns_fd_attr) {
3035 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
3036 } else if (netns_id_attr) {
3037 net = get_net_ns_by_id(sock_net(skb->sk),
3038 nla_get_u32(netns_id_attr));
3040 net = ERR_PTR(-EINVAL);
3043 net = ERR_PTR(-EINVAL);
3046 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
3047 return ERR_PTR(-EINVAL);
3049 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
3051 return ERR_PTR(-EPERM);
3056 static void devlink_param_notify(struct devlink *devlink,
3057 unsigned int port_index,
3058 struct devlink_param_item *param_item,
3059 enum devlink_command cmd);
3061 static void devlink_reload_netns_change(struct devlink *devlink,
3062 struct net *dest_net)
3064 struct devlink_param_item *param_item;
3066 /* Userspace needs to be notified about devlink objects
3067 * removed from original and entering new network namespace.
3068 * The rest of the devlink objects are re-created during
3069 * reload process so the notifications are generated separatelly.
3072 list_for_each_entry(param_item, &devlink->param_list, list)
3073 devlink_param_notify(devlink, 0, param_item,
3074 DEVLINK_CMD_PARAM_DEL);
3075 devlink_notify(devlink, DEVLINK_CMD_DEL);
3077 __devlink_net_set(devlink, dest_net);
3079 devlink_notify(devlink, DEVLINK_CMD_NEW);
3080 list_for_each_entry(param_item, &devlink->param_list, list)
3081 devlink_param_notify(devlink, 0, param_item,
3082 DEVLINK_CMD_PARAM_NEW);
3085 static bool devlink_reload_supported(const struct devlink_ops *ops)
3087 return ops->reload_down && ops->reload_up;
3090 static void devlink_reload_failed_set(struct devlink *devlink,
3093 if (devlink->reload_failed == reload_failed)
3095 devlink->reload_failed = reload_failed;
3096 devlink_notify(devlink, DEVLINK_CMD_NEW);
3099 bool devlink_is_reload_failed(const struct devlink *devlink)
3101 return devlink->reload_failed;
3103 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
3106 __devlink_reload_stats_update(struct devlink *devlink, u32 *reload_stats,
3107 enum devlink_reload_limit limit, u32 actions_performed)
3109 unsigned long actions = actions_performed;
3113 for_each_set_bit(action, &actions, __DEVLINK_RELOAD_ACTION_MAX) {
3114 stat_idx = limit * __DEVLINK_RELOAD_ACTION_MAX + action;
3115 reload_stats[stat_idx]++;
3117 devlink_notify(devlink, DEVLINK_CMD_NEW);
3121 devlink_reload_stats_update(struct devlink *devlink, enum devlink_reload_limit limit,
3122 u32 actions_performed)
3124 __devlink_reload_stats_update(devlink, devlink->stats.reload_stats, limit,
3129 * devlink_remote_reload_actions_performed - Update devlink on reload actions
3130 * performed which are not a direct result of devlink reload call.
3132 * This should be called by a driver after performing reload actions in case it was not
3133 * a result of devlink reload call. For example fw_activate was performed as a result
3134 * of devlink reload triggered fw_activate on another host.
3135 * The motivation for this function is to keep data on reload actions performed on this
3136 * function whether it was done due to direct devlink reload call or not.
3139 * @limit: reload limit
3140 * @actions_performed: bitmask of actions performed
3142 void devlink_remote_reload_actions_performed(struct devlink *devlink,
3143 enum devlink_reload_limit limit,
3144 u32 actions_performed)
3146 if (WARN_ON(!actions_performed ||
3147 actions_performed & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
3148 actions_performed >= BIT(__DEVLINK_RELOAD_ACTION_MAX) ||
3149 limit > DEVLINK_RELOAD_LIMIT_MAX))
3152 __devlink_reload_stats_update(devlink, devlink->stats.remote_reload_stats, limit,
3155 EXPORT_SYMBOL_GPL(devlink_remote_reload_actions_performed);
3157 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
3158 enum devlink_reload_action action, enum devlink_reload_limit limit,
3159 u32 *actions_performed, struct netlink_ext_ack *extack)
3161 u32 remote_reload_stats[DEVLINK_RELOAD_STATS_ARRAY_SIZE];
3164 if (!devlink->reload_enabled)
3167 memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
3168 sizeof(remote_reload_stats));
3169 err = devlink->ops->reload_down(devlink, !!dest_net, action, limit, extack);
3173 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
3174 devlink_reload_netns_change(devlink, dest_net);
3176 err = devlink->ops->reload_up(devlink, action, limit, actions_performed, extack);
3177 devlink_reload_failed_set(devlink, !!err);
3181 WARN_ON(!(*actions_performed & BIT(action)));
3182 /* Catch driver on updating the remote action within devlink reload */
3183 WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
3184 sizeof(remote_reload_stats)));
3185 devlink_reload_stats_update(devlink, limit, *actions_performed);
3190 devlink_nl_reload_actions_performed_snd(struct devlink *devlink, u32 actions_performed,
3191 enum devlink_command cmd, struct genl_info *info)
3193 struct sk_buff *msg;
3196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3200 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &devlink_nl_family, 0, cmd);
3204 if (devlink_nl_put_handle(msg, devlink))
3205 goto nla_put_failure;
3207 if (nla_put_bitfield32(msg, DEVLINK_ATTR_RELOAD_ACTIONS_PERFORMED, actions_performed,
3209 goto nla_put_failure;
3210 genlmsg_end(msg, hdr);
3212 return genlmsg_reply(msg, info);
3215 genlmsg_cancel(msg, hdr);
3221 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
3223 struct devlink *devlink = info->user_ptr[0];
3224 enum devlink_reload_action action;
3225 enum devlink_reload_limit limit;
3226 struct net *dest_net = NULL;
3227 u32 actions_performed;
3230 if (!devlink_reload_supported(devlink->ops))
3233 err = devlink_resources_validate(devlink, NULL, info);
3235 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
3239 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
3240 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
3241 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
3242 dest_net = devlink_netns_get(skb, info);
3243 if (IS_ERR(dest_net))
3244 return PTR_ERR(dest_net);
3247 if (info->attrs[DEVLINK_ATTR_RELOAD_ACTION])
3248 action = nla_get_u8(info->attrs[DEVLINK_ATTR_RELOAD_ACTION]);
3250 action = DEVLINK_RELOAD_ACTION_DRIVER_REINIT;
3252 if (!devlink_reload_action_is_supported(devlink, action)) {
3253 NL_SET_ERR_MSG_MOD(info->extack,
3254 "Requested reload action is not supported by the driver");
3258 limit = DEVLINK_RELOAD_LIMIT_UNSPEC;
3259 if (info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]) {
3260 struct nla_bitfield32 limits;
3261 u32 limits_selected;
3263 limits = nla_get_bitfield32(info->attrs[DEVLINK_ATTR_RELOAD_LIMITS]);
3264 limits_selected = limits.value & limits.selector;
3265 if (!limits_selected) {
3266 NL_SET_ERR_MSG_MOD(info->extack, "Invalid limit selected");
3269 for (limit = 0 ; limit <= DEVLINK_RELOAD_LIMIT_MAX ; limit++)
3270 if (limits_selected & BIT(limit))
3272 /* UAPI enables multiselection, but currently it is not used */
3273 if (limits_selected != BIT(limit)) {
3274 NL_SET_ERR_MSG_MOD(info->extack,
3275 "Multiselection of limit is not supported");
3278 if (!devlink_reload_limit_is_supported(devlink, limit)) {
3279 NL_SET_ERR_MSG_MOD(info->extack,
3280 "Requested limit is not supported by the driver");
3283 if (devlink_reload_combination_is_invalid(action, limit)) {
3284 NL_SET_ERR_MSG_MOD(info->extack,
3285 "Requested limit is invalid for this action");
3289 err = devlink_reload(devlink, dest_net, action, limit, &actions_performed, info->extack);
3296 /* For backward compatibility generate reply only if attributes used by user */
3297 if (!info->attrs[DEVLINK_ATTR_RELOAD_ACTION] && !info->attrs[DEVLINK_ATTR_RELOAD_LIMITS])
3300 return devlink_nl_reload_actions_performed_snd(devlink, actions_performed,
3301 DEVLINK_CMD_RELOAD, info);
3304 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
3305 struct devlink *devlink,
3306 enum devlink_command cmd,
3307 struct devlink_flash_notify *params)
3311 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3315 if (devlink_nl_put_handle(msg, devlink))
3316 goto nla_put_failure;
3318 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
3321 if (params->status_msg &&
3322 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
3323 params->status_msg))
3324 goto nla_put_failure;
3325 if (params->component &&
3326 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
3328 goto nla_put_failure;
3329 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
3330 params->done, DEVLINK_ATTR_PAD))
3331 goto nla_put_failure;
3332 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
3333 params->total, DEVLINK_ATTR_PAD))
3334 goto nla_put_failure;
3335 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
3336 params->timeout, DEVLINK_ATTR_PAD))
3337 goto nla_put_failure;
3340 genlmsg_end(msg, hdr);
3344 genlmsg_cancel(msg, hdr);
3348 static void __devlink_flash_update_notify(struct devlink *devlink,
3349 enum devlink_command cmd,
3350 struct devlink_flash_notify *params)
3352 struct sk_buff *msg;
3355 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
3356 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
3357 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
3359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3363 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
3367 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3368 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3375 void devlink_flash_update_begin_notify(struct devlink *devlink)
3377 struct devlink_flash_notify params = { 0 };
3379 __devlink_flash_update_notify(devlink,
3380 DEVLINK_CMD_FLASH_UPDATE,
3383 EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
3385 void devlink_flash_update_end_notify(struct devlink *devlink)
3387 struct devlink_flash_notify params = { 0 };
3389 __devlink_flash_update_notify(devlink,
3390 DEVLINK_CMD_FLASH_UPDATE_END,
3393 EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
3395 void devlink_flash_update_status_notify(struct devlink *devlink,
3396 const char *status_msg,
3397 const char *component,
3399 unsigned long total)
3401 struct devlink_flash_notify params = {
3402 .status_msg = status_msg,
3403 .component = component,
3408 __devlink_flash_update_notify(devlink,
3409 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3412 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
3414 void devlink_flash_update_timeout_notify(struct devlink *devlink,
3415 const char *status_msg,
3416 const char *component,
3417 unsigned long timeout)
3419 struct devlink_flash_notify params = {
3420 .status_msg = status_msg,
3421 .component = component,
3425 __devlink_flash_update_notify(devlink,
3426 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3429 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
3431 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
3432 struct genl_info *info)
3434 struct nlattr *nla_component, *nla_overwrite_mask;
3435 struct devlink_flash_update_params params = {};
3436 struct devlink *devlink = info->user_ptr[0];
3437 u32 supported_params;
3439 if (!devlink->ops->flash_update)
3442 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
3445 supported_params = devlink->ops->supported_flash_update_params;
3447 params.file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
3449 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
3450 if (nla_component) {
3451 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
3452 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
3453 "component update is not supported by this device");
3456 params.component = nla_data(nla_component);
3459 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
3460 if (nla_overwrite_mask) {
3461 struct nla_bitfield32 sections;
3463 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
3464 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
3465 "overwrite settings are not supported by this device");
3468 sections = nla_get_bitfield32(nla_overwrite_mask);
3469 params.overwrite_mask = sections.value & sections.selector;
3472 return devlink->ops->flash_update(devlink, ¶ms, info->extack);
3475 static const struct devlink_param devlink_param_generic[] = {
3477 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
3478 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
3479 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
3482 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3483 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
3484 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
3487 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
3488 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
3489 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
3492 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
3493 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
3494 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
3497 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
3498 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
3499 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
3502 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
3503 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
3504 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
3507 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
3508 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
3509 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
3512 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3513 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3514 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3517 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3518 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3519 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3522 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3523 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3524 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3527 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_REMOTE_DEV_RESET,
3528 .name = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_NAME,
3529 .type = DEVLINK_PARAM_GENERIC_ENABLE_REMOTE_DEV_RESET_TYPE,
3533 static int devlink_param_generic_verify(const struct devlink_param *param)
3535 /* verify it match generic parameter by id and name */
3536 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3538 if (strcmp(param->name, devlink_param_generic[param->id].name))
3541 WARN_ON(param->type != devlink_param_generic[param->id].type);
3546 static int devlink_param_driver_verify(const struct devlink_param *param)
3550 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3552 /* verify no such name in generic params */
3553 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3554 if (!strcmp(param->name, devlink_param_generic[i].name))
3560 static struct devlink_param_item *
3561 devlink_param_find_by_name(struct list_head *param_list,
3562 const char *param_name)
3564 struct devlink_param_item *param_item;
3566 list_for_each_entry(param_item, param_list, list)
3567 if (!strcmp(param_item->param->name, param_name))
3572 static struct devlink_param_item *
3573 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3575 struct devlink_param_item *param_item;
3577 list_for_each_entry(param_item, param_list, list)
3578 if (param_item->param->id == param_id)
3584 devlink_param_cmode_is_supported(const struct devlink_param *param,
3585 enum devlink_param_cmode cmode)
3587 return test_bit(cmode, ¶m->supported_cmodes);
3590 static int devlink_param_get(struct devlink *devlink,
3591 const struct devlink_param *param,
3592 struct devlink_param_gset_ctx *ctx)
3596 return param->get(devlink, param->id, ctx);
3599 static int devlink_param_set(struct devlink *devlink,
3600 const struct devlink_param *param,
3601 struct devlink_param_gset_ctx *ctx)
3605 return param->set(devlink, param->id, ctx);
3609 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3611 switch (param_type) {
3612 case DEVLINK_PARAM_TYPE_U8:
3614 case DEVLINK_PARAM_TYPE_U16:
3616 case DEVLINK_PARAM_TYPE_U32:
3618 case DEVLINK_PARAM_TYPE_STRING:
3620 case DEVLINK_PARAM_TYPE_BOOL:
3628 devlink_nl_param_value_fill_one(struct sk_buff *msg,
3629 enum devlink_param_type type,
3630 enum devlink_param_cmode cmode,
3631 union devlink_param_value val)
3633 struct nlattr *param_value_attr;
3635 param_value_attr = nla_nest_start_noflag(msg,
3636 DEVLINK_ATTR_PARAM_VALUE);
3637 if (!param_value_attr)
3638 goto nla_put_failure;
3640 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3641 goto value_nest_cancel;
3644 case DEVLINK_PARAM_TYPE_U8:
3645 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3646 goto value_nest_cancel;
3648 case DEVLINK_PARAM_TYPE_U16:
3649 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3650 goto value_nest_cancel;
3652 case DEVLINK_PARAM_TYPE_U32:
3653 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3654 goto value_nest_cancel;
3656 case DEVLINK_PARAM_TYPE_STRING:
3657 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3659 goto value_nest_cancel;
3661 case DEVLINK_PARAM_TYPE_BOOL:
3663 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3664 goto value_nest_cancel;
3668 nla_nest_end(msg, param_value_attr);
3672 nla_nest_cancel(msg, param_value_attr);
3677 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
3678 unsigned int port_index,
3679 struct devlink_param_item *param_item,
3680 enum devlink_command cmd,
3681 u32 portid, u32 seq, int flags)
3683 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
3684 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
3685 const struct devlink_param *param = param_item->param;
3686 struct devlink_param_gset_ctx ctx;
3687 struct nlattr *param_values_list;
3688 struct nlattr *param_attr;
3694 /* Get value from driver part to driverinit configuration mode */
3695 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3696 if (!devlink_param_cmode_is_supported(param, i))
3698 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3699 if (!param_item->driverinit_value_valid)
3701 param_value[i] = param_item->driverinit_value;
3703 if (!param_item->published)
3706 err = devlink_param_get(devlink, param, &ctx);
3709 param_value[i] = ctx.val;
3711 param_value_set[i] = true;
3714 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3718 if (devlink_nl_put_handle(msg, devlink))
3719 goto genlmsg_cancel;
3721 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3722 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3723 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
3724 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3725 goto genlmsg_cancel;
3727 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
3729 goto genlmsg_cancel;
3730 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3731 goto param_nest_cancel;
3732 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3733 goto param_nest_cancel;
3735 nla_type = devlink_param_type_to_nla_type(param->type);
3737 goto param_nest_cancel;
3738 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3739 goto param_nest_cancel;
3741 param_values_list = nla_nest_start_noflag(msg,
3742 DEVLINK_ATTR_PARAM_VALUES_LIST);
3743 if (!param_values_list)
3744 goto param_nest_cancel;
3746 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3747 if (!param_value_set[i])
3749 err = devlink_nl_param_value_fill_one(msg, param->type,
3752 goto values_list_nest_cancel;
3755 nla_nest_end(msg, param_values_list);
3756 nla_nest_end(msg, param_attr);
3757 genlmsg_end(msg, hdr);
3760 values_list_nest_cancel:
3761 nla_nest_end(msg, param_values_list);
3763 nla_nest_cancel(msg, param_attr);
3765 genlmsg_cancel(msg, hdr);
3769 static void devlink_param_notify(struct devlink *devlink,
3770 unsigned int port_index,
3771 struct devlink_param_item *param_item,
3772 enum devlink_command cmd)
3774 struct sk_buff *msg;
3777 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3778 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3779 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
3781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3784 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3791 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3792 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3795 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3796 struct netlink_callback *cb)
3798 struct devlink_param_item *param_item;
3799 struct devlink *devlink;
3800 int start = cb->args[0];
3804 mutex_lock(&devlink_mutex);
3805 list_for_each_entry(devlink, &devlink_list, list) {
3806 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3808 mutex_lock(&devlink->lock);
3809 list_for_each_entry(param_item, &devlink->param_list, list) {
3814 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3815 DEVLINK_CMD_PARAM_GET,
3816 NETLINK_CB(cb->skb).portid,
3819 if (err == -EOPNOTSUPP) {
3822 mutex_unlock(&devlink->lock);
3827 mutex_unlock(&devlink->lock);
3830 mutex_unlock(&devlink_mutex);
3832 if (err != -EMSGSIZE)
3840 devlink_param_type_get_from_info(struct genl_info *info,
3841 enum devlink_param_type *param_type)
3843 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3846 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3848 *param_type = DEVLINK_PARAM_TYPE_U8;
3851 *param_type = DEVLINK_PARAM_TYPE_U16;
3854 *param_type = DEVLINK_PARAM_TYPE_U32;
3857 *param_type = DEVLINK_PARAM_TYPE_STRING;
3860 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3870 devlink_param_value_get_from_info(const struct devlink_param *param,
3871 struct genl_info *info,
3872 union devlink_param_value *value)
3874 struct nlattr *param_data;
3877 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3879 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
3882 switch (param->type) {
3883 case DEVLINK_PARAM_TYPE_U8:
3884 if (nla_len(param_data) != sizeof(u8))
3886 value->vu8 = nla_get_u8(param_data);
3888 case DEVLINK_PARAM_TYPE_U16:
3889 if (nla_len(param_data) != sizeof(u16))
3891 value->vu16 = nla_get_u16(param_data);
3893 case DEVLINK_PARAM_TYPE_U32:
3894 if (nla_len(param_data) != sizeof(u32))
3896 value->vu32 = nla_get_u32(param_data);
3898 case DEVLINK_PARAM_TYPE_STRING:
3899 len = strnlen(nla_data(param_data), nla_len(param_data));
3900 if (len == nla_len(param_data) ||
3901 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3903 strcpy(value->vstr, nla_data(param_data));
3905 case DEVLINK_PARAM_TYPE_BOOL:
3906 if (param_data && nla_len(param_data))
3908 value->vbool = nla_get_flag(param_data);
3914 static struct devlink_param_item *
3915 devlink_param_get_from_info(struct list_head *param_list,
3916 struct genl_info *info)
3920 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3923 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3924 return devlink_param_find_by_name(param_list, param_name);
3927 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3928 struct genl_info *info)
3930 struct devlink *devlink = info->user_ptr[0];
3931 struct devlink_param_item *param_item;
3932 struct sk_buff *msg;
3935 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3939 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3943 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3944 DEVLINK_CMD_PARAM_GET,
3945 info->snd_portid, info->snd_seq, 0);
3951 return genlmsg_reply(msg, info);
3954 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3955 unsigned int port_index,
3956 struct list_head *param_list,
3957 struct genl_info *info,
3958 enum devlink_command cmd)
3960 enum devlink_param_type param_type;
3961 struct devlink_param_gset_ctx ctx;
3962 enum devlink_param_cmode cmode;
3963 struct devlink_param_item *param_item;
3964 const struct devlink_param *param;
3965 union devlink_param_value value;
3968 param_item = devlink_param_get_from_info(param_list, info);
3971 param = param_item->param;
3972 err = devlink_param_type_get_from_info(info, ¶m_type);
3975 if (param_type != param->type)
3977 err = devlink_param_value_get_from_info(param, info, &value);
3980 if (param->validate) {
3981 err = param->validate(devlink, param->id, value, info->extack);
3986 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3988 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3989 if (!devlink_param_cmode_is_supported(param, cmode))
3992 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3993 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3994 strcpy(param_item->driverinit_value.vstr, value.vstr);
3996 param_item->driverinit_value = value;
3997 param_item->driverinit_value_valid = true;
4003 err = devlink_param_set(devlink, param, &ctx);
4008 devlink_param_notify(devlink, port_index, param_item, cmd);
4012 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
4013 struct genl_info *info)
4015 struct devlink *devlink = info->user_ptr[0];
4017 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
4018 info, DEVLINK_CMD_PARAM_NEW);
4021 static int devlink_param_register_one(struct devlink *devlink,
4022 unsigned int port_index,
4023 struct list_head *param_list,
4024 const struct devlink_param *param,
4025 enum devlink_command cmd)
4027 struct devlink_param_item *param_item;
4029 if (devlink_param_find_by_name(param_list, param->name))
4032 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
4033 WARN_ON(param->get || param->set);
4035 WARN_ON(!param->get || !param->set);
4037 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
4040 param_item->param = param;
4042 list_add_tail(¶m_item->list, param_list);
4043 devlink_param_notify(devlink, port_index, param_item, cmd);
4047 static void devlink_param_unregister_one(struct devlink *devlink,
4048 unsigned int port_index,
4049 struct list_head *param_list,
4050 const struct devlink_param *param,
4051 enum devlink_command cmd)
4053 struct devlink_param_item *param_item;
4055 param_item = devlink_param_find_by_name(param_list, param->name);
4056 WARN_ON(!param_item);
4057 devlink_param_notify(devlink, port_index, param_item, cmd);
4058 list_del(¶m_item->list);
4062 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
4063 struct netlink_callback *cb)
4065 struct devlink_param_item *param_item;
4066 struct devlink_port *devlink_port;
4067 struct devlink *devlink;
4068 int start = cb->args[0];
4072 mutex_lock(&devlink_mutex);
4073 list_for_each_entry(devlink, &devlink_list, list) {
4074 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4076 mutex_lock(&devlink->lock);
4077 list_for_each_entry(devlink_port, &devlink->port_list, list) {
4078 list_for_each_entry(param_item,
4079 &devlink_port->param_list, list) {
4084 err = devlink_nl_param_fill(msg,
4085 devlink_port->devlink,
4086 devlink_port->index, param_item,
4087 DEVLINK_CMD_PORT_PARAM_GET,
4088 NETLINK_CB(cb->skb).portid,
4091 if (err == -EOPNOTSUPP) {
4094 mutex_unlock(&devlink->lock);
4100 mutex_unlock(&devlink->lock);
4103 mutex_unlock(&devlink_mutex);
4105 if (err != -EMSGSIZE)
4112 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
4113 struct genl_info *info)
4115 struct devlink_port *devlink_port = info->user_ptr[0];
4116 struct devlink_param_item *param_item;
4117 struct sk_buff *msg;
4120 param_item = devlink_param_get_from_info(&devlink_port->param_list,
4125 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4129 err = devlink_nl_param_fill(msg, devlink_port->devlink,
4130 devlink_port->index, param_item,
4131 DEVLINK_CMD_PORT_PARAM_GET,
4132 info->snd_portid, info->snd_seq, 0);
4138 return genlmsg_reply(msg, info);
4141 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
4142 struct genl_info *info)
4144 struct devlink_port *devlink_port = info->user_ptr[0];
4146 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
4147 devlink_port->index,
4148 &devlink_port->param_list, info,
4149 DEVLINK_CMD_PORT_PARAM_NEW);
4152 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
4153 struct devlink *devlink,
4154 struct devlink_snapshot *snapshot)
4156 struct nlattr *snap_attr;
4159 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
4163 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
4165 goto nla_put_failure;
4167 nla_nest_end(msg, snap_attr);
4171 nla_nest_cancel(msg, snap_attr);
4175 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
4176 struct devlink *devlink,
4177 struct devlink_region *region)
4179 struct devlink_snapshot *snapshot;
4180 struct nlattr *snapshots_attr;
4183 snapshots_attr = nla_nest_start_noflag(msg,
4184 DEVLINK_ATTR_REGION_SNAPSHOTS);
4185 if (!snapshots_attr)
4188 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
4189 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
4191 goto nla_put_failure;
4194 nla_nest_end(msg, snapshots_attr);
4198 nla_nest_cancel(msg, snapshots_attr);
4202 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
4203 enum devlink_command cmd, u32 portid,
4205 struct devlink_region *region)
4210 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4214 err = devlink_nl_put_handle(msg, devlink);
4216 goto nla_put_failure;
4219 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4220 region->port->index);
4222 goto nla_put_failure;
4225 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
4227 goto nla_put_failure;
4229 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4233 goto nla_put_failure;
4235 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
4237 goto nla_put_failure;
4239 genlmsg_end(msg, hdr);
4243 genlmsg_cancel(msg, hdr);
4247 static struct sk_buff *
4248 devlink_nl_region_notify_build(struct devlink_region *region,
4249 struct devlink_snapshot *snapshot,
4250 enum devlink_command cmd, u32 portid, u32 seq)
4252 struct devlink *devlink = region->devlink;
4253 struct sk_buff *msg;
4258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4260 return ERR_PTR(-ENOMEM);
4262 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
4268 err = devlink_nl_put_handle(msg, devlink);
4270 goto out_cancel_msg;
4273 err = nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX,
4274 region->port->index);
4276 goto out_cancel_msg;
4279 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
4282 goto out_cancel_msg;
4285 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
4288 goto out_cancel_msg;
4290 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
4291 region->size, DEVLINK_ATTR_PAD);
4293 goto out_cancel_msg;
4295 genlmsg_end(msg, hdr);
4300 genlmsg_cancel(msg, hdr);
4303 return ERR_PTR(err);
4306 static void devlink_nl_region_notify(struct devlink_region *region,
4307 struct devlink_snapshot *snapshot,
4308 enum devlink_command cmd)
4310 struct devlink *devlink = region->devlink;
4311 struct sk_buff *msg;
4313 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
4315 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
4319 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4320 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4324 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4325 * @devlink: devlink instance
4326 * @id: the snapshot id
4328 * Track when a new snapshot begins using an id. Load the count for the
4329 * given id from the snapshot xarray, increment it, and store it back.
4331 * Called when a new snapshot is created with the given id.
4333 * The id *must* have been previously allocated by
4334 * devlink_region_snapshot_id_get().
4336 * Returns 0 on success, or an error on failure.
4338 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
4340 unsigned long count;
4343 lockdep_assert_held(&devlink->lock);
4345 p = xa_load(&devlink->snapshot_ids, id);
4349 if (WARN_ON(!xa_is_value(p)))
4352 count = xa_to_value(p);
4355 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4360 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4361 * @devlink: devlink instance
4362 * @id: the snapshot id
4364 * Track when a snapshot is deleted and stops using an id. Load the count
4365 * for the given id from the snapshot xarray, decrement it, and store it
4368 * If the count reaches zero, erase this id from the xarray, freeing it
4369 * up for future re-use by devlink_region_snapshot_id_get().
4371 * Called when a snapshot using the given id is deleted, and when the
4372 * initial allocator of the id is finished using it.
4374 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
4376 unsigned long count;
4379 lockdep_assert_held(&devlink->lock);
4381 p = xa_load(&devlink->snapshot_ids, id);
4385 if (WARN_ON(!xa_is_value(p)))
4388 count = xa_to_value(p);
4392 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4395 /* If this was the last user, we can erase this id */
4396 xa_erase(&devlink->snapshot_ids, id);
4401 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4402 * @devlink: devlink instance
4403 * @id: the snapshot id
4405 * Mark the given snapshot id as used by inserting a zero value into the
4408 * This must be called while holding the devlink instance lock. Unlike
4409 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4410 * It is expected that the id will immediately be used before
4411 * releasing the devlink instance lock.
4413 * Returns zero on success, or an error code if the snapshot id could not
4416 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
4418 lockdep_assert_held(&devlink->lock);
4420 if (xa_load(&devlink->snapshot_ids, id))
4423 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
4428 * __devlink_region_snapshot_id_get - get snapshot ID
4429 * @devlink: devlink instance
4430 * @id: storage to return snapshot id
4432 * Allocates a new snapshot id. Returns zero on success, or a negative
4433 * error on failure. Must be called while holding the devlink instance
4436 * Snapshot IDs are tracked using an xarray which stores the number of
4437 * users of the snapshot id.
4439 * Note that the caller of this function counts as a 'user', in order to
4440 * avoid race conditions. The caller must release its hold on the
4441 * snapshot by using devlink_region_snapshot_id_put.
4443 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
4445 lockdep_assert_held(&devlink->lock);
4447 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
4448 xa_limit_32b, GFP_KERNEL);
4452 * __devlink_region_snapshot_create - create a new snapshot
4453 * This will add a new snapshot of a region. The snapshot
4454 * will be stored on the region struct and can be accessed
4455 * from devlink. This is useful for future analyses of snapshots.
4456 * Multiple snapshots can be created on a region.
4457 * The @snapshot_id should be obtained using the getter function.
4459 * Must be called only while holding the devlink instance lock.
4461 * @region: devlink region of the snapshot
4462 * @data: snapshot data
4463 * @snapshot_id: snapshot id to be created
4466 __devlink_region_snapshot_create(struct devlink_region *region,
4467 u8 *data, u32 snapshot_id)
4469 struct devlink *devlink = region->devlink;
4470 struct devlink_snapshot *snapshot;
4473 lockdep_assert_held(&devlink->lock);
4475 /* check if region can hold one more snapshot */
4476 if (region->cur_snapshots == region->max_snapshots)
4479 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
4482 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4486 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
4488 goto err_snapshot_id_increment;
4490 snapshot->id = snapshot_id;
4491 snapshot->region = region;
4492 snapshot->data = data;
4494 list_add_tail(&snapshot->list, ®ion->snapshot_list);
4496 region->cur_snapshots++;
4498 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4501 err_snapshot_id_increment:
4506 static void devlink_region_snapshot_del(struct devlink_region *region,
4507 struct devlink_snapshot *snapshot)
4509 struct devlink *devlink = region->devlink;
4511 lockdep_assert_held(&devlink->lock);
4513 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
4514 region->cur_snapshots--;
4515 list_del(&snapshot->list);
4516 region->ops->destructor(snapshot->data);
4517 __devlink_snapshot_id_decrement(devlink, snapshot->id);
4521 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
4522 struct genl_info *info)
4524 struct devlink *devlink = info->user_ptr[0];
4525 struct devlink_port *port = NULL;
4526 struct devlink_region *region;
4527 const char *region_name;
4528 struct sk_buff *msg;
4532 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
4535 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4536 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4538 port = devlink_port_get_by_index(devlink, index);
4543 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4545 region = devlink_port_region_get_by_name(port, region_name);
4547 region = devlink_region_get_by_name(devlink, region_name);
4552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4556 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4557 info->snd_portid, info->snd_seq, 0,
4564 return genlmsg_reply(msg, info);
4567 static int devlink_nl_cmd_region_get_port_dumpit(struct sk_buff *msg,
4568 struct netlink_callback *cb,
4569 struct devlink_port *port,
4573 struct devlink_region *region;
4576 list_for_each_entry(region, &port->region_list, list) {
4581 err = devlink_nl_region_fill(msg, port->devlink,
4582 DEVLINK_CMD_REGION_GET,
4583 NETLINK_CB(cb->skb).portid,
4585 NLM_F_MULTI, region);
4595 static int devlink_nl_cmd_region_get_devlink_dumpit(struct sk_buff *msg,
4596 struct netlink_callback *cb,
4597 struct devlink *devlink,
4601 struct devlink_region *region;
4602 struct devlink_port *port;
4605 mutex_lock(&devlink->lock);
4606 list_for_each_entry(region, &devlink->region_list, list) {
4611 err = devlink_nl_region_fill(msg, devlink,
4612 DEVLINK_CMD_REGION_GET,
4613 NETLINK_CB(cb->skb).portid,
4615 NLM_F_MULTI, region);
4621 list_for_each_entry(port, &devlink->port_list, list) {
4622 err = devlink_nl_cmd_region_get_port_dumpit(msg, cb, port, idx,
4629 mutex_unlock(&devlink->lock);
4633 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
4634 struct netlink_callback *cb)
4636 struct devlink *devlink;
4637 int start = cb->args[0];
4641 mutex_lock(&devlink_mutex);
4642 list_for_each_entry(devlink, &devlink_list, list) {
4643 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4645 err = devlink_nl_cmd_region_get_devlink_dumpit(msg, cb, devlink,
4651 mutex_unlock(&devlink_mutex);
4656 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4657 struct genl_info *info)
4659 struct devlink *devlink = info->user_ptr[0];
4660 struct devlink_snapshot *snapshot;
4661 struct devlink_port *port = NULL;
4662 struct devlink_region *region;
4663 const char *region_name;
4667 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
4668 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
4671 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4672 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4674 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4675 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4677 port = devlink_port_get_by_index(devlink, index);
4683 region = devlink_port_region_get_by_name(port, region_name);
4685 region = devlink_region_get_by_name(devlink, region_name);
4690 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4694 devlink_region_snapshot_del(region, snapshot);
4699 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4701 struct devlink *devlink = info->user_ptr[0];
4702 struct devlink_snapshot *snapshot;
4703 struct devlink_port *port = NULL;
4704 struct nlattr *snapshot_id_attr;
4705 struct devlink_region *region;
4706 const char *region_name;
4712 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4713 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4717 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4719 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4720 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4722 port = devlink_port_get_by_index(devlink, index);
4728 region = devlink_port_region_get_by_name(port, region_name);
4730 region = devlink_region_get_by_name(devlink, region_name);
4733 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4737 if (!region->ops->snapshot) {
4738 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4742 if (region->cur_snapshots == region->max_snapshots) {
4743 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4747 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
4748 if (snapshot_id_attr) {
4749 snapshot_id = nla_get_u32(snapshot_id_attr);
4751 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4752 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4756 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4760 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
4762 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
4768 err = region->port_ops->snapshot(port, region->port_ops,
4769 info->extack, &data);
4771 err = region->ops->snapshot(devlink, region->ops,
4772 info->extack, &data);
4774 goto err_snapshot_capture;
4776 err = __devlink_region_snapshot_create(region, data, snapshot_id);
4778 goto err_snapshot_create;
4780 if (!snapshot_id_attr) {
4781 struct sk_buff *msg;
4783 snapshot = devlink_region_snapshot_get_by_id(region,
4785 if (WARN_ON(!snapshot))
4788 msg = devlink_nl_region_notify_build(region, snapshot,
4789 DEVLINK_CMD_REGION_NEW,
4792 err = PTR_ERR_OR_ZERO(msg);
4796 err = genlmsg_reply(msg, info);
4803 err_snapshot_create:
4804 region->ops->destructor(data);
4805 err_snapshot_capture:
4806 __devlink_snapshot_id_decrement(devlink, snapshot_id);
4810 devlink_region_snapshot_del(region, snapshot);
4814 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
4815 struct devlink *devlink,
4816 u8 *chunk, u32 chunk_size,
4819 struct nlattr *chunk_attr;
4822 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
4826 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
4828 goto nla_put_failure;
4830 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
4833 goto nla_put_failure;
4835 nla_nest_end(msg, chunk_attr);
4839 nla_nest_cancel(msg, chunk_attr);
4843 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4845 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
4846 struct devlink *devlink,
4847 struct devlink_region *region,
4848 struct nlattr **attrs,
4853 struct devlink_snapshot *snapshot;
4854 u64 curr_offset = start_offset;
4858 *new_offset = start_offset;
4860 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4861 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4865 while (curr_offset < end_offset) {
4869 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
4870 data_size = end_offset - curr_offset;
4872 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
4874 data = &snapshot->data[curr_offset];
4875 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
4881 curr_offset += data_size;
4883 *new_offset = curr_offset;
4888 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
4889 struct netlink_callback *cb)
4891 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
4892 u64 ret_offset, start_offset, end_offset = U64_MAX;
4893 struct nlattr **attrs = info->attrs;
4894 struct devlink_port *port = NULL;
4895 struct devlink_region *region;
4896 struct nlattr *chunks_attr;
4897 const char *region_name;
4898 struct devlink *devlink;
4903 start_offset = *((u64 *)&cb->args[0]);
4905 mutex_lock(&devlink_mutex);
4906 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4907 if (IS_ERR(devlink)) {
4908 err = PTR_ERR(devlink);
4912 mutex_lock(&devlink->lock);
4914 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
4915 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4920 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) {
4921 index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
4923 port = devlink_port_get_by_index(devlink, index);
4930 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
4933 region = devlink_port_region_get_by_name(port, region_name);
4935 region = devlink_region_get_by_name(devlink, region_name);
4942 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4943 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4946 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4948 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4949 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4952 if (end_offset > region->size)
4953 end_offset = region->size;
4955 /* return 0 if there is no further data to read */
4956 if (start_offset == end_offset) {
4961 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4962 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
4963 DEVLINK_CMD_REGION_READ);
4969 err = devlink_nl_put_handle(skb, devlink);
4971 goto nla_put_failure;
4974 err = nla_put_u32(skb, DEVLINK_ATTR_PORT_INDEX,
4975 region->port->index);
4977 goto nla_put_failure;
4980 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4982 goto nla_put_failure;
4984 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
4987 goto nla_put_failure;
4990 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4993 end_offset, &ret_offset);
4995 if (err && err != -EMSGSIZE)
4996 goto nla_put_failure;
4998 /* Check if there was any progress done to prevent infinite loop */
4999 if (ret_offset == start_offset) {
5001 goto nla_put_failure;
5004 *((u64 *)&cb->args[0]) = ret_offset;
5006 nla_nest_end(skb, chunks_attr);
5007 genlmsg_end(skb, hdr);
5008 mutex_unlock(&devlink->lock);
5009 mutex_unlock(&devlink_mutex);
5014 genlmsg_cancel(skb, hdr);
5016 mutex_unlock(&devlink->lock);
5018 mutex_unlock(&devlink_mutex);
5022 struct devlink_info_req {
5023 struct sk_buff *msg;
5026 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
5028 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
5030 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
5032 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
5034 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
5036 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
5038 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
5041 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
5044 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
5046 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
5047 const char *version_name,
5048 const char *version_value)
5050 struct nlattr *nest;
5053 nest = nla_nest_start_noflag(req->msg, attr);
5057 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
5060 goto nla_put_failure;
5062 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
5065 goto nla_put_failure;
5067 nla_nest_end(req->msg, nest);
5072 nla_nest_cancel(req->msg, nest);
5076 int devlink_info_version_fixed_put(struct devlink_info_req *req,
5077 const char *version_name,
5078 const char *version_value)
5080 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
5081 version_name, version_value);
5083 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
5085 int devlink_info_version_stored_put(struct devlink_info_req *req,
5086 const char *version_name,
5087 const char *version_value)
5089 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
5090 version_name, version_value);
5092 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
5094 int devlink_info_version_running_put(struct devlink_info_req *req,
5095 const char *version_name,
5096 const char *version_value)
5098 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
5099 version_name, version_value);
5101 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
5104 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
5105 enum devlink_command cmd, u32 portid,
5106 u32 seq, int flags, struct netlink_ext_ack *extack)
5108 struct devlink_info_req req;
5112 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5117 if (devlink_nl_put_handle(msg, devlink))
5118 goto err_cancel_msg;
5121 err = devlink->ops->info_get(devlink, &req, extack);
5123 goto err_cancel_msg;
5125 genlmsg_end(msg, hdr);
5129 genlmsg_cancel(msg, hdr);
5133 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
5134 struct genl_info *info)
5136 struct devlink *devlink = info->user_ptr[0];
5137 struct sk_buff *msg;
5140 if (!devlink->ops->info_get)
5143 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5147 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5148 info->snd_portid, info->snd_seq, 0,
5155 return genlmsg_reply(msg, info);
5158 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
5159 struct netlink_callback *cb)
5161 struct devlink *devlink;
5162 int start = cb->args[0];
5166 mutex_lock(&devlink_mutex);
5167 list_for_each_entry(devlink, &devlink_list, list) {
5168 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5175 if (!devlink->ops->info_get) {
5180 mutex_lock(&devlink->lock);
5181 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
5182 NETLINK_CB(cb->skb).portid,
5183 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5185 mutex_unlock(&devlink->lock);
5186 if (err == -EOPNOTSUPP)
5192 mutex_unlock(&devlink_mutex);
5194 if (err != -EMSGSIZE)
5201 struct devlink_fmsg_item {
5202 struct list_head list;
5209 struct devlink_fmsg {
5210 struct list_head item_list;
5211 bool putting_binary; /* This flag forces enclosing of binary data
5212 * in an array brackets. It forces using
5213 * of designated API:
5214 * devlink_fmsg_binary_pair_nest_start()
5215 * devlink_fmsg_binary_pair_nest_end()
5219 static struct devlink_fmsg *devlink_fmsg_alloc(void)
5221 struct devlink_fmsg *fmsg;
5223 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
5227 INIT_LIST_HEAD(&fmsg->item_list);
5232 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
5234 struct devlink_fmsg_item *item, *tmp;
5236 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
5237 list_del(&item->list);
5243 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
5246 struct devlink_fmsg_item *item;
5248 item = kzalloc(sizeof(*item), GFP_KERNEL);
5252 item->attrtype = attrtype;
5253 list_add_tail(&item->list, &fmsg->item_list);
5258 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
5260 if (fmsg->putting_binary)
5263 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
5265 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
5267 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
5269 if (fmsg->putting_binary)
5272 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
5275 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
5277 if (fmsg->putting_binary)
5280 return devlink_fmsg_nest_end(fmsg);
5282 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
5284 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
5286 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
5288 struct devlink_fmsg_item *item;
5290 if (fmsg->putting_binary)
5293 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
5296 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
5300 item->nla_type = NLA_NUL_STRING;
5301 item->len = strlen(name) + 1;
5302 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
5303 memcpy(&item->value, name, item->len);
5304 list_add_tail(&item->list, &fmsg->item_list);
5309 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
5313 if (fmsg->putting_binary)
5316 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
5320 err = devlink_fmsg_put_name(fmsg, name);
5326 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
5328 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
5330 if (fmsg->putting_binary)
5333 return devlink_fmsg_nest_end(fmsg);
5335 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
5337 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
5342 if (fmsg->putting_binary)
5345 err = devlink_fmsg_pair_nest_start(fmsg, name);
5349 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
5355 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
5357 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
5361 if (fmsg->putting_binary)
5364 err = devlink_fmsg_nest_end(fmsg);
5368 err = devlink_fmsg_nest_end(fmsg);
5374 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
5376 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
5381 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
5385 fmsg->putting_binary = true;
5388 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
5390 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
5392 if (!fmsg->putting_binary)
5395 fmsg->putting_binary = false;
5396 return devlink_fmsg_arr_pair_nest_end(fmsg);
5398 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
5400 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
5401 const void *value, u16 value_len,
5404 struct devlink_fmsg_item *item;
5406 if (value_len > DEVLINK_FMSG_MAX_SIZE)
5409 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
5413 item->nla_type = value_nla_type;
5414 item->len = value_len;
5415 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5416 memcpy(&item->value, value, item->len);
5417 list_add_tail(&item->list, &fmsg->item_list);
5422 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
5424 if (fmsg->putting_binary)
5427 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
5429 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
5431 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
5433 if (fmsg->putting_binary)
5436 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
5438 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
5440 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
5442 if (fmsg->putting_binary)
5445 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
5447 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
5449 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
5451 if (fmsg->putting_binary)
5454 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
5456 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
5458 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
5460 if (fmsg->putting_binary)
5463 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
5466 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
5468 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
5471 if (!fmsg->putting_binary)
5474 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
5476 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
5478 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
5483 err = devlink_fmsg_pair_nest_start(fmsg, name);
5487 err = devlink_fmsg_bool_put(fmsg, value);
5491 err = devlink_fmsg_pair_nest_end(fmsg);
5497 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
5499 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
5504 err = devlink_fmsg_pair_nest_start(fmsg, name);
5508 err = devlink_fmsg_u8_put(fmsg, value);
5512 err = devlink_fmsg_pair_nest_end(fmsg);
5518 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
5520 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
5525 err = devlink_fmsg_pair_nest_start(fmsg, name);
5529 err = devlink_fmsg_u32_put(fmsg, value);
5533 err = devlink_fmsg_pair_nest_end(fmsg);
5539 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
5541 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
5546 err = devlink_fmsg_pair_nest_start(fmsg, name);
5550 err = devlink_fmsg_u64_put(fmsg, value);
5554 err = devlink_fmsg_pair_nest_end(fmsg);
5560 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
5562 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
5567 err = devlink_fmsg_pair_nest_start(fmsg, name);
5571 err = devlink_fmsg_string_put(fmsg, value);
5575 err = devlink_fmsg_pair_nest_end(fmsg);
5581 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
5583 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
5584 const void *value, u32 value_len)
5591 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
5595 for (offset = 0; offset < value_len; offset += data_size) {
5596 data_size = value_len - offset;
5597 if (data_size > DEVLINK_FMSG_MAX_SIZE)
5598 data_size = DEVLINK_FMSG_MAX_SIZE;
5599 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
5602 /* Exit from loop with a break (instead of
5603 * return) to make sure putting_binary is turned off in
5604 * devlink_fmsg_binary_pair_nest_end
5608 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
5614 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
5617 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5619 switch (msg->nla_type) {
5624 case NLA_NUL_STRING:
5626 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
5634 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5636 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5639 switch (msg->nla_type) {
5641 /* Always provide flag data, regardless of its value */
5642 tmp = *(bool *) msg->value;
5644 return nla_put_u8(skb, attrtype, tmp);
5646 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
5648 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
5650 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
5652 case NLA_NUL_STRING:
5653 return nla_put_string(skb, attrtype, (char *) &msg->value);
5655 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
5662 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5665 struct devlink_fmsg_item *item;
5666 struct nlattr *fmsg_nlattr;
5670 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
5674 list_for_each_entry(item, &fmsg->item_list, list) {
5680 switch (item->attrtype) {
5681 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
5682 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
5683 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
5684 case DEVLINK_ATTR_FMSG_NEST_END:
5685 err = nla_put_flag(skb, item->attrtype);
5687 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
5688 err = devlink_fmsg_item_fill_type(item, skb);
5691 err = devlink_fmsg_item_fill_data(item, skb);
5693 case DEVLINK_ATTR_FMSG_OBJ_NAME:
5694 err = nla_put_string(skb, item->attrtype,
5695 (char *) &item->value);
5707 nla_nest_end(skb, fmsg_nlattr);
5711 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
5712 struct genl_info *info,
5713 enum devlink_command cmd, int flags)
5715 struct nlmsghdr *nlh;
5716 struct sk_buff *skb;
5723 int tmp_index = index;
5725 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5729 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
5730 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
5733 goto nla_put_failure;
5736 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5739 else if (err != -EMSGSIZE || tmp_index == index)
5740 goto nla_put_failure;
5742 genlmsg_end(skb, hdr);
5743 err = genlmsg_reply(skb, info);
5748 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5751 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
5752 NLMSG_DONE, 0, flags | NLM_F_MULTI);
5755 goto nla_put_failure;
5758 return genlmsg_reply(skb, info);
5765 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5766 struct netlink_callback *cb,
5767 enum devlink_command cmd)
5769 int index = cb->args[0];
5770 int tmp_index = index;
5774 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5775 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
5778 goto nla_put_failure;
5781 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5782 if ((err && err != -EMSGSIZE) || tmp_index == index)
5783 goto nla_put_failure;
5785 cb->args[0] = index;
5786 genlmsg_end(skb, hdr);
5790 genlmsg_cancel(skb, hdr);
5794 struct devlink_health_reporter {
5795 struct list_head list;
5797 const struct devlink_health_reporter_ops *ops;
5798 struct devlink *devlink;
5799 struct devlink_port *devlink_port;
5800 struct devlink_fmsg *dump_fmsg;
5801 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
5802 u64 graceful_period;
5810 u64 last_recovery_ts;
5811 refcount_t refcount;
5815 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
5817 return reporter->priv;
5819 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
5821 static struct devlink_health_reporter *
5822 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
5823 struct mutex *list_lock,
5824 const char *reporter_name)
5826 struct devlink_health_reporter *reporter;
5828 lockdep_assert_held(list_lock);
5829 list_for_each_entry(reporter, reporter_list, list)
5830 if (!strcmp(reporter->ops->name, reporter_name))
5835 static struct devlink_health_reporter *
5836 devlink_health_reporter_find_by_name(struct devlink *devlink,
5837 const char *reporter_name)
5839 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
5840 &devlink->reporters_lock,
5844 static struct devlink_health_reporter *
5845 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
5846 const char *reporter_name)
5848 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
5849 &devlink_port->reporters_lock,
5853 static struct devlink_health_reporter *
5854 __devlink_health_reporter_create(struct devlink *devlink,
5855 const struct devlink_health_reporter_ops *ops,
5856 u64 graceful_period, void *priv)
5858 struct devlink_health_reporter *reporter;
5860 if (WARN_ON(graceful_period && !ops->recover))
5861 return ERR_PTR(-EINVAL);
5863 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
5865 return ERR_PTR(-ENOMEM);
5867 reporter->priv = priv;
5868 reporter->ops = ops;
5869 reporter->devlink = devlink;
5870 reporter->graceful_period = graceful_period;
5871 reporter->auto_recover = !!ops->recover;
5872 reporter->auto_dump = !!ops->dump;
5873 mutex_init(&reporter->dump_lock);
5874 refcount_set(&reporter->refcount, 1);
5879 * devlink_port_health_reporter_create - create devlink health reporter for
5880 * specified port instance
5882 * @port: devlink_port which should contain the new reporter
5884 * @graceful_period: to avoid recovery loops, in msecs
5887 struct devlink_health_reporter *
5888 devlink_port_health_reporter_create(struct devlink_port *port,
5889 const struct devlink_health_reporter_ops *ops,
5890 u64 graceful_period, void *priv)
5892 struct devlink_health_reporter *reporter;
5894 mutex_lock(&port->reporters_lock);
5895 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
5896 &port->reporters_lock, ops->name)) {
5897 reporter = ERR_PTR(-EEXIST);
5901 reporter = __devlink_health_reporter_create(port->devlink, ops,
5902 graceful_period, priv);
5903 if (IS_ERR(reporter))
5906 reporter->devlink_port = port;
5907 list_add_tail(&reporter->list, &port->reporter_list);
5909 mutex_unlock(&port->reporters_lock);
5912 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
5915 * devlink_health_reporter_create - create devlink health reporter
5919 * @graceful_period: to avoid recovery loops, in msecs
5922 struct devlink_health_reporter *
5923 devlink_health_reporter_create(struct devlink *devlink,
5924 const struct devlink_health_reporter_ops *ops,
5925 u64 graceful_period, void *priv)
5927 struct devlink_health_reporter *reporter;
5929 mutex_lock(&devlink->reporters_lock);
5930 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
5931 reporter = ERR_PTR(-EEXIST);
5935 reporter = __devlink_health_reporter_create(devlink, ops,
5936 graceful_period, priv);
5937 if (IS_ERR(reporter))
5940 list_add_tail(&reporter->list, &devlink->reporter_list);
5942 mutex_unlock(&devlink->reporters_lock);
5945 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
5948 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
5950 mutex_destroy(&reporter->dump_lock);
5951 if (reporter->dump_fmsg)
5952 devlink_fmsg_free(reporter->dump_fmsg);
5957 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5959 if (refcount_dec_and_test(&reporter->refcount))
5960 devlink_health_reporter_free(reporter);
5964 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5966 list_del(&reporter->list);
5967 devlink_health_reporter_put(reporter);
5971 * devlink_health_reporter_destroy - destroy devlink health reporter
5973 * @reporter: devlink health reporter to destroy
5976 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5978 struct mutex *lock = &reporter->devlink->reporters_lock;
5981 __devlink_health_reporter_destroy(reporter);
5984 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
5987 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
5989 * @reporter: devlink health reporter to destroy
5992 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
5994 struct mutex *lock = &reporter->devlink_port->reporters_lock;
5997 __devlink_health_reporter_destroy(reporter);
6000 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
6003 devlink_nl_health_reporter_fill(struct sk_buff *msg,
6004 struct devlink *devlink,
6005 struct devlink_health_reporter *reporter,
6006 enum devlink_command cmd, u32 portid,
6009 struct nlattr *reporter_attr;
6012 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6016 if (devlink_nl_put_handle(msg, devlink))
6017 goto genlmsg_cancel;
6019 if (reporter->devlink_port) {
6020 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
6021 goto genlmsg_cancel;
6023 reporter_attr = nla_nest_start_noflag(msg,
6024 DEVLINK_ATTR_HEALTH_REPORTER);
6026 goto genlmsg_cancel;
6027 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
6028 reporter->ops->name))
6029 goto reporter_nest_cancel;
6030 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
6031 reporter->health_state))
6032 goto reporter_nest_cancel;
6033 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
6034 reporter->error_count, DEVLINK_ATTR_PAD))
6035 goto reporter_nest_cancel;
6036 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
6037 reporter->recovery_count, DEVLINK_ATTR_PAD))
6038 goto reporter_nest_cancel;
6039 if (reporter->ops->recover &&
6040 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
6041 reporter->graceful_period,
6043 goto reporter_nest_cancel;
6044 if (reporter->ops->recover &&
6045 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
6046 reporter->auto_recover))
6047 goto reporter_nest_cancel;
6048 if (reporter->dump_fmsg &&
6049 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
6050 jiffies_to_msecs(reporter->dump_ts),
6052 goto reporter_nest_cancel;
6053 if (reporter->dump_fmsg &&
6054 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
6055 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
6056 goto reporter_nest_cancel;
6057 if (reporter->ops->dump &&
6058 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
6059 reporter->auto_dump))
6060 goto reporter_nest_cancel;
6062 nla_nest_end(msg, reporter_attr);
6063 genlmsg_end(msg, hdr);
6066 reporter_nest_cancel:
6067 nla_nest_end(msg, reporter_attr);
6069 genlmsg_cancel(msg, hdr);
6073 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
6074 enum devlink_command cmd)
6076 struct sk_buff *msg;
6079 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6081 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6085 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
6086 reporter, cmd, 0, 0, 0);
6092 genlmsg_multicast_netns(&devlink_nl_family,
6093 devlink_net(reporter->devlink),
6094 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
6098 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
6100 reporter->recovery_count++;
6101 reporter->last_recovery_ts = jiffies;
6103 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
6106 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
6107 void *priv_ctx, struct netlink_ext_ack *extack)
6111 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
6114 if (!reporter->ops->recover)
6117 err = reporter->ops->recover(reporter, priv_ctx, extack);
6121 devlink_health_reporter_recovery_done(reporter);
6122 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
6123 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6129 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
6131 if (!reporter->dump_fmsg)
6133 devlink_fmsg_free(reporter->dump_fmsg);
6134 reporter->dump_fmsg = NULL;
6137 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
6139 struct netlink_ext_ack *extack)
6143 if (!reporter->ops->dump)
6146 if (reporter->dump_fmsg)
6149 reporter->dump_fmsg = devlink_fmsg_alloc();
6150 if (!reporter->dump_fmsg) {
6155 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
6159 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
6164 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
6168 reporter->dump_ts = jiffies;
6169 reporter->dump_real_ts = ktime_get_real_ns();
6174 devlink_health_dump_clear(reporter);
6178 int devlink_health_report(struct devlink_health_reporter *reporter,
6179 const char *msg, void *priv_ctx)
6181 enum devlink_health_reporter_state prev_health_state;
6182 struct devlink *devlink = reporter->devlink;
6183 unsigned long recover_ts_threshold;
6185 /* write a log message of the current error */
6187 trace_devlink_health_report(devlink, reporter->ops->name, msg);
6188 reporter->error_count++;
6189 prev_health_state = reporter->health_state;
6190 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6191 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6193 /* abort if the previous error wasn't recovered */
6194 recover_ts_threshold = reporter->last_recovery_ts +
6195 msecs_to_jiffies(reporter->graceful_period);
6196 if (reporter->auto_recover &&
6197 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
6198 (reporter->last_recovery_ts && reporter->recovery_count &&
6199 time_is_after_jiffies(recover_ts_threshold)))) {
6200 trace_devlink_health_recover_aborted(devlink,
6201 reporter->ops->name,
6202 reporter->health_state,
6204 reporter->last_recovery_ts);
6208 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
6210 if (reporter->auto_dump) {
6211 mutex_lock(&reporter->dump_lock);
6212 /* store current dump of current error, for later analysis */
6213 devlink_health_do_dump(reporter, priv_ctx, NULL);
6214 mutex_unlock(&reporter->dump_lock);
6217 if (reporter->auto_recover)
6218 return devlink_health_reporter_recover(reporter,
6223 EXPORT_SYMBOL_GPL(devlink_health_report);
6225 static struct devlink_health_reporter *
6226 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
6227 struct nlattr **attrs)
6229 struct devlink_health_reporter *reporter;
6230 struct devlink_port *devlink_port;
6231 char *reporter_name;
6233 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
6236 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
6237 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
6238 if (IS_ERR(devlink_port)) {
6239 mutex_lock(&devlink->reporters_lock);
6240 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
6242 refcount_inc(&reporter->refcount);
6243 mutex_unlock(&devlink->reporters_lock);
6245 mutex_lock(&devlink_port->reporters_lock);
6246 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
6248 refcount_inc(&reporter->refcount);
6249 mutex_unlock(&devlink_port->reporters_lock);
6255 static struct devlink_health_reporter *
6256 devlink_health_reporter_get_from_info(struct devlink *devlink,
6257 struct genl_info *info)
6259 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
6262 static struct devlink_health_reporter *
6263 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
6265 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
6266 struct devlink_health_reporter *reporter;
6267 struct nlattr **attrs = info->attrs;
6268 struct devlink *devlink;
6270 mutex_lock(&devlink_mutex);
6271 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
6272 if (IS_ERR(devlink))
6275 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
6276 mutex_unlock(&devlink_mutex);
6279 mutex_unlock(&devlink_mutex);
6284 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
6285 enum devlink_health_reporter_state state)
6287 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
6288 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
6291 if (reporter->health_state == state)
6294 reporter->health_state = state;
6295 trace_devlink_health_reporter_state_update(reporter->devlink,
6296 reporter->ops->name, state);
6297 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
6299 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
6301 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
6302 struct genl_info *info)
6304 struct devlink *devlink = info->user_ptr[0];
6305 struct devlink_health_reporter *reporter;
6306 struct sk_buff *msg;
6309 reporter = devlink_health_reporter_get_from_info(devlink, info);
6313 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6319 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6320 DEVLINK_CMD_HEALTH_REPORTER_GET,
6321 info->snd_portid, info->snd_seq,
6328 err = genlmsg_reply(msg, info);
6330 devlink_health_reporter_put(reporter);
6335 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
6336 struct netlink_callback *cb)
6338 struct devlink_health_reporter *reporter;
6339 struct devlink_port *port;
6340 struct devlink *devlink;
6341 int start = cb->args[0];
6345 mutex_lock(&devlink_mutex);
6346 list_for_each_entry(devlink, &devlink_list, list) {
6347 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6349 mutex_lock(&devlink->reporters_lock);
6350 list_for_each_entry(reporter, &devlink->reporter_list,
6356 err = devlink_nl_health_reporter_fill(msg, devlink,
6358 DEVLINK_CMD_HEALTH_REPORTER_GET,
6359 NETLINK_CB(cb->skb).portid,
6363 mutex_unlock(&devlink->reporters_lock);
6368 mutex_unlock(&devlink->reporters_lock);
6371 list_for_each_entry(devlink, &devlink_list, list) {
6372 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6374 mutex_lock(&devlink->lock);
6375 list_for_each_entry(port, &devlink->port_list, list) {
6376 mutex_lock(&port->reporters_lock);
6377 list_for_each_entry(reporter, &port->reporter_list, list) {
6382 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
6383 DEVLINK_CMD_HEALTH_REPORTER_GET,
6384 NETLINK_CB(cb->skb).portid,
6388 mutex_unlock(&port->reporters_lock);
6389 mutex_unlock(&devlink->lock);
6394 mutex_unlock(&port->reporters_lock);
6396 mutex_unlock(&devlink->lock);
6399 mutex_unlock(&devlink_mutex);
6406 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
6407 struct genl_info *info)
6409 struct devlink *devlink = info->user_ptr[0];
6410 struct devlink_health_reporter *reporter;
6413 reporter = devlink_health_reporter_get_from_info(devlink, info);
6417 if (!reporter->ops->recover &&
6418 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
6419 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
6423 if (!reporter->ops->dump &&
6424 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
6429 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
6430 reporter->graceful_period =
6431 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
6433 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
6434 reporter->auto_recover =
6435 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
6437 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
6438 reporter->auto_dump =
6439 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
6441 devlink_health_reporter_put(reporter);
6444 devlink_health_reporter_put(reporter);
6448 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
6449 struct genl_info *info)
6451 struct devlink *devlink = info->user_ptr[0];
6452 struct devlink_health_reporter *reporter;
6455 reporter = devlink_health_reporter_get_from_info(devlink, info);
6459 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
6461 devlink_health_reporter_put(reporter);
6465 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
6466 struct genl_info *info)
6468 struct devlink *devlink = info->user_ptr[0];
6469 struct devlink_health_reporter *reporter;
6470 struct devlink_fmsg *fmsg;
6473 reporter = devlink_health_reporter_get_from_info(devlink, info);
6477 if (!reporter->ops->diagnose) {
6478 devlink_health_reporter_put(reporter);
6482 fmsg = devlink_fmsg_alloc();
6484 devlink_health_reporter_put(reporter);
6488 err = devlink_fmsg_obj_nest_start(fmsg);
6492 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
6496 err = devlink_fmsg_obj_nest_end(fmsg);
6500 err = devlink_fmsg_snd(fmsg, info,
6501 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
6504 devlink_fmsg_free(fmsg);
6505 devlink_health_reporter_put(reporter);
6510 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
6511 struct netlink_callback *cb)
6513 struct devlink_health_reporter *reporter;
6514 u64 start = cb->args[0];
6517 reporter = devlink_health_reporter_get_from_cb(cb);
6521 if (!reporter->ops->dump) {
6525 mutex_lock(&reporter->dump_lock);
6527 err = devlink_health_do_dump(reporter, NULL, cb->extack);
6530 cb->args[1] = reporter->dump_ts;
6532 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
6533 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
6538 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
6539 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
6541 mutex_unlock(&reporter->dump_lock);
6543 devlink_health_reporter_put(reporter);
6548 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
6549 struct genl_info *info)
6551 struct devlink *devlink = info->user_ptr[0];
6552 struct devlink_health_reporter *reporter;
6554 reporter = devlink_health_reporter_get_from_info(devlink, info);
6558 if (!reporter->ops->dump) {
6559 devlink_health_reporter_put(reporter);
6563 mutex_lock(&reporter->dump_lock);
6564 devlink_health_dump_clear(reporter);
6565 mutex_unlock(&reporter->dump_lock);
6566 devlink_health_reporter_put(reporter);
6570 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
6571 struct genl_info *info)
6573 struct devlink *devlink = info->user_ptr[0];
6574 struct devlink_health_reporter *reporter;
6577 reporter = devlink_health_reporter_get_from_info(devlink, info);
6581 if (!reporter->ops->test) {
6582 devlink_health_reporter_put(reporter);
6586 err = reporter->ops->test(reporter, info->extack);
6588 devlink_health_reporter_put(reporter);
6592 struct devlink_stats {
6595 struct u64_stats_sync syncp;
6599 * struct devlink_trap_policer_item - Packet trap policer attributes.
6600 * @policer: Immutable packet trap policer attributes.
6601 * @rate: Rate in packets / sec.
6602 * @burst: Burst size in packets.
6603 * @list: trap_policer_list member.
6605 * Describes packet trap policer attributes. Created by devlink during trap
6606 * policer registration.
6608 struct devlink_trap_policer_item {
6609 const struct devlink_trap_policer *policer;
6612 struct list_head list;
6616 * struct devlink_trap_group_item - Packet trap group attributes.
6617 * @group: Immutable packet trap group attributes.
6618 * @policer_item: Associated policer item. Can be NULL.
6619 * @list: trap_group_list member.
6620 * @stats: Trap group statistics.
6622 * Describes packet trap group attributes. Created by devlink during trap
6623 * group registration.
6625 struct devlink_trap_group_item {
6626 const struct devlink_trap_group *group;
6627 struct devlink_trap_policer_item *policer_item;
6628 struct list_head list;
6629 struct devlink_stats __percpu *stats;
6633 * struct devlink_trap_item - Packet trap attributes.
6634 * @trap: Immutable packet trap attributes.
6635 * @group_item: Associated group item.
6636 * @list: trap_list member.
6637 * @action: Trap action.
6638 * @stats: Trap statistics.
6639 * @priv: Driver private information.
6641 * Describes both mutable and immutable packet trap attributes. Created by
6642 * devlink during trap registration and used for all trap related operations.
6644 struct devlink_trap_item {
6645 const struct devlink_trap *trap;
6646 struct devlink_trap_group_item *group_item;
6647 struct list_head list;
6648 enum devlink_trap_action action;
6649 struct devlink_stats __percpu *stats;
6653 static struct devlink_trap_policer_item *
6654 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
6656 struct devlink_trap_policer_item *policer_item;
6658 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
6659 if (policer_item->policer->id == id)
6660 return policer_item;
6666 static struct devlink_trap_item *
6667 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
6669 struct devlink_trap_item *trap_item;
6671 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6672 if (!strcmp(trap_item->trap->name, name))
6679 static struct devlink_trap_item *
6680 devlink_trap_item_get_from_info(struct devlink *devlink,
6681 struct genl_info *info)
6683 struct nlattr *attr;
6685 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
6687 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
6689 return devlink_trap_item_lookup(devlink, nla_data(attr));
6693 devlink_trap_action_get_from_info(struct genl_info *info,
6694 enum devlink_trap_action *p_trap_action)
6698 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
6700 case DEVLINK_TRAP_ACTION_DROP:
6701 case DEVLINK_TRAP_ACTION_TRAP:
6702 case DEVLINK_TRAP_ACTION_MIRROR:
6703 *p_trap_action = val;
6712 static int devlink_trap_metadata_put(struct sk_buff *msg,
6713 const struct devlink_trap *trap)
6715 struct nlattr *attr;
6717 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
6721 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
6722 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
6723 goto nla_put_failure;
6724 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
6725 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
6726 goto nla_put_failure;
6728 nla_nest_end(msg, attr);
6733 nla_nest_cancel(msg, attr);
6737 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
6738 struct devlink_stats *stats)
6742 memset(stats, 0, sizeof(*stats));
6743 for_each_possible_cpu(i) {
6744 struct devlink_stats *cpu_stats;
6745 u64 rx_packets, rx_bytes;
6748 cpu_stats = per_cpu_ptr(trap_stats, i);
6750 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6751 rx_packets = cpu_stats->rx_packets;
6752 rx_bytes = cpu_stats->rx_bytes;
6753 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6755 stats->rx_packets += rx_packets;
6756 stats->rx_bytes += rx_bytes;
6760 static int devlink_trap_stats_put(struct sk_buff *msg,
6761 struct devlink_stats __percpu *trap_stats)
6763 struct devlink_stats stats;
6764 struct nlattr *attr;
6766 devlink_trap_stats_read(trap_stats, &stats);
6768 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6772 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
6773 stats.rx_packets, DEVLINK_ATTR_PAD))
6774 goto nla_put_failure;
6776 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
6777 stats.rx_bytes, DEVLINK_ATTR_PAD))
6778 goto nla_put_failure;
6780 nla_nest_end(msg, attr);
6785 nla_nest_cancel(msg, attr);
6789 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
6790 const struct devlink_trap_item *trap_item,
6791 enum devlink_command cmd, u32 portid, u32 seq,
6794 struct devlink_trap_group_item *group_item = trap_item->group_item;
6798 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6802 if (devlink_nl_put_handle(msg, devlink))
6803 goto nla_put_failure;
6805 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6806 group_item->group->name))
6807 goto nla_put_failure;
6809 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
6810 goto nla_put_failure;
6812 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
6813 goto nla_put_failure;
6815 if (trap_item->trap->generic &&
6816 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6817 goto nla_put_failure;
6819 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
6820 goto nla_put_failure;
6822 err = devlink_trap_metadata_put(msg, trap_item->trap);
6824 goto nla_put_failure;
6826 err = devlink_trap_stats_put(msg, trap_item->stats);
6828 goto nla_put_failure;
6830 genlmsg_end(msg, hdr);
6835 genlmsg_cancel(msg, hdr);
6839 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
6840 struct genl_info *info)
6842 struct netlink_ext_ack *extack = info->extack;
6843 struct devlink *devlink = info->user_ptr[0];
6844 struct devlink_trap_item *trap_item;
6845 struct sk_buff *msg;
6848 if (list_empty(&devlink->trap_list))
6851 trap_item = devlink_trap_item_get_from_info(devlink, info);
6853 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6857 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6861 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6862 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
6867 return genlmsg_reply(msg, info);
6874 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
6875 struct netlink_callback *cb)
6877 struct devlink_trap_item *trap_item;
6878 struct devlink *devlink;
6879 int start = cb->args[0];
6883 mutex_lock(&devlink_mutex);
6884 list_for_each_entry(devlink, &devlink_list, list) {
6885 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6887 mutex_lock(&devlink->lock);
6888 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6893 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6894 DEVLINK_CMD_TRAP_NEW,
6895 NETLINK_CB(cb->skb).portid,
6899 mutex_unlock(&devlink->lock);
6904 mutex_unlock(&devlink->lock);
6907 mutex_unlock(&devlink_mutex);
6913 static int __devlink_trap_action_set(struct devlink *devlink,
6914 struct devlink_trap_item *trap_item,
6915 enum devlink_trap_action trap_action,
6916 struct netlink_ext_ack *extack)
6920 if (trap_item->action != trap_action &&
6921 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
6922 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
6926 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
6927 trap_action, extack);
6931 trap_item->action = trap_action;
6936 static int devlink_trap_action_set(struct devlink *devlink,
6937 struct devlink_trap_item *trap_item,
6938 struct genl_info *info)
6940 enum devlink_trap_action trap_action;
6943 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6946 err = devlink_trap_action_get_from_info(info, &trap_action);
6948 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6952 return __devlink_trap_action_set(devlink, trap_item, trap_action,
6956 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
6957 struct genl_info *info)
6959 struct netlink_ext_ack *extack = info->extack;
6960 struct devlink *devlink = info->user_ptr[0];
6961 struct devlink_trap_item *trap_item;
6964 if (list_empty(&devlink->trap_list))
6967 trap_item = devlink_trap_item_get_from_info(devlink, info);
6969 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6973 err = devlink_trap_action_set(devlink, trap_item, info);
6980 static struct devlink_trap_group_item *
6981 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
6983 struct devlink_trap_group_item *group_item;
6985 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6986 if (!strcmp(group_item->group->name, name))
6993 static struct devlink_trap_group_item *
6994 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
6996 struct devlink_trap_group_item *group_item;
6998 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6999 if (group_item->group->id == id)
7006 static struct devlink_trap_group_item *
7007 devlink_trap_group_item_get_from_info(struct devlink *devlink,
7008 struct genl_info *info)
7012 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
7014 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
7016 return devlink_trap_group_item_lookup(devlink, name);
7020 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
7021 const struct devlink_trap_group_item *group_item,
7022 enum devlink_command cmd, u32 portid, u32 seq,
7028 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7032 if (devlink_nl_put_handle(msg, devlink))
7033 goto nla_put_failure;
7035 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
7036 group_item->group->name))
7037 goto nla_put_failure;
7039 if (group_item->group->generic &&
7040 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
7041 goto nla_put_failure;
7043 if (group_item->policer_item &&
7044 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7045 group_item->policer_item->policer->id))
7046 goto nla_put_failure;
7048 err = devlink_trap_stats_put(msg, group_item->stats);
7050 goto nla_put_failure;
7052 genlmsg_end(msg, hdr);
7057 genlmsg_cancel(msg, hdr);
7061 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
7062 struct genl_info *info)
7064 struct netlink_ext_ack *extack = info->extack;
7065 struct devlink *devlink = info->user_ptr[0];
7066 struct devlink_trap_group_item *group_item;
7067 struct sk_buff *msg;
7070 if (list_empty(&devlink->trap_group_list))
7073 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7075 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7079 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7083 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
7084 DEVLINK_CMD_TRAP_GROUP_NEW,
7085 info->snd_portid, info->snd_seq, 0);
7087 goto err_trap_group_fill;
7089 return genlmsg_reply(msg, info);
7091 err_trap_group_fill:
7096 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
7097 struct netlink_callback *cb)
7099 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
7100 struct devlink_trap_group_item *group_item;
7101 u32 portid = NETLINK_CB(cb->skb).portid;
7102 struct devlink *devlink;
7103 int start = cb->args[0];
7107 mutex_lock(&devlink_mutex);
7108 list_for_each_entry(devlink, &devlink_list, list) {
7109 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7111 mutex_lock(&devlink->lock);
7112 list_for_each_entry(group_item, &devlink->trap_group_list,
7118 err = devlink_nl_trap_group_fill(msg, devlink,
7124 mutex_unlock(&devlink->lock);
7129 mutex_unlock(&devlink->lock);
7132 mutex_unlock(&devlink_mutex);
7139 __devlink_trap_group_action_set(struct devlink *devlink,
7140 struct devlink_trap_group_item *group_item,
7141 enum devlink_trap_action trap_action,
7142 struct netlink_ext_ack *extack)
7144 const char *group_name = group_item->group->name;
7145 struct devlink_trap_item *trap_item;
7148 if (devlink->ops->trap_group_action_set) {
7149 err = devlink->ops->trap_group_action_set(devlink, group_item->group,
7150 trap_action, extack);
7154 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7155 if (strcmp(trap_item->group_item->group->name, group_name))
7157 if (trap_item->action != trap_action &&
7158 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP)
7160 trap_item->action = trap_action;
7166 list_for_each_entry(trap_item, &devlink->trap_list, list) {
7167 if (strcmp(trap_item->group_item->group->name, group_name))
7169 err = __devlink_trap_action_set(devlink, trap_item,
7170 trap_action, extack);
7179 devlink_trap_group_action_set(struct devlink *devlink,
7180 struct devlink_trap_group_item *group_item,
7181 struct genl_info *info, bool *p_modified)
7183 enum devlink_trap_action trap_action;
7186 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
7189 err = devlink_trap_action_get_from_info(info, &trap_action);
7191 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
7195 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
7205 static int devlink_trap_group_set(struct devlink *devlink,
7206 struct devlink_trap_group_item *group_item,
7207 struct genl_info *info)
7209 struct devlink_trap_policer_item *policer_item;
7210 struct netlink_ext_ack *extack = info->extack;
7211 const struct devlink_trap_policer *policer;
7212 struct nlattr **attrs = info->attrs;
7215 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7218 if (!devlink->ops->trap_group_set)
7221 policer_item = group_item->policer_item;
7222 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
7225 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7226 policer_item = devlink_trap_policer_item_lookup(devlink,
7228 if (policer_id && !policer_item) {
7229 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7233 policer = policer_item ? policer_item->policer : NULL;
7235 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
7240 group_item->policer_item = policer_item;
7245 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
7246 struct genl_info *info)
7248 struct netlink_ext_ack *extack = info->extack;
7249 struct devlink *devlink = info->user_ptr[0];
7250 struct devlink_trap_group_item *group_item;
7251 bool modified = false;
7254 if (list_empty(&devlink->trap_group_list))
7257 group_item = devlink_trap_group_item_get_from_info(devlink, info);
7259 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
7263 err = devlink_trap_group_action_set(devlink, group_item, info,
7268 err = devlink_trap_group_set(devlink, group_item, info);
7270 goto err_trap_group_set;
7276 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
7280 static struct devlink_trap_policer_item *
7281 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
7282 struct genl_info *info)
7286 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
7288 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
7290 return devlink_trap_policer_item_lookup(devlink, id);
7294 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
7295 const struct devlink_trap_policer *policer)
7297 struct nlattr *attr;
7301 if (!devlink->ops->trap_policer_counter_get)
7304 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
7308 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
7312 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
7314 goto nla_put_failure;
7316 nla_nest_end(msg, attr);
7321 nla_nest_cancel(msg, attr);
7326 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
7327 const struct devlink_trap_policer_item *policer_item,
7328 enum devlink_command cmd, u32 portid, u32 seq,
7334 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
7338 if (devlink_nl_put_handle(msg, devlink))
7339 goto nla_put_failure;
7341 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
7342 policer_item->policer->id))
7343 goto nla_put_failure;
7345 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
7346 policer_item->rate, DEVLINK_ATTR_PAD))
7347 goto nla_put_failure;
7349 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
7350 policer_item->burst, DEVLINK_ATTR_PAD))
7351 goto nla_put_failure;
7353 err = devlink_trap_policer_stats_put(msg, devlink,
7354 policer_item->policer);
7356 goto nla_put_failure;
7358 genlmsg_end(msg, hdr);
7363 genlmsg_cancel(msg, hdr);
7367 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
7368 struct genl_info *info)
7370 struct devlink_trap_policer_item *policer_item;
7371 struct netlink_ext_ack *extack = info->extack;
7372 struct devlink *devlink = info->user_ptr[0];
7373 struct sk_buff *msg;
7376 if (list_empty(&devlink->trap_policer_list))
7379 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7380 if (!policer_item) {
7381 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7385 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7389 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
7390 DEVLINK_CMD_TRAP_POLICER_NEW,
7391 info->snd_portid, info->snd_seq, 0);
7393 goto err_trap_policer_fill;
7395 return genlmsg_reply(msg, info);
7397 err_trap_policer_fill:
7402 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
7403 struct netlink_callback *cb)
7405 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
7406 struct devlink_trap_policer_item *policer_item;
7407 u32 portid = NETLINK_CB(cb->skb).portid;
7408 struct devlink *devlink;
7409 int start = cb->args[0];
7413 mutex_lock(&devlink_mutex);
7414 list_for_each_entry(devlink, &devlink_list, list) {
7415 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
7417 mutex_lock(&devlink->lock);
7418 list_for_each_entry(policer_item, &devlink->trap_policer_list,
7424 err = devlink_nl_trap_policer_fill(msg, devlink,
7430 mutex_unlock(&devlink->lock);
7435 mutex_unlock(&devlink->lock);
7438 mutex_unlock(&devlink_mutex);
7445 devlink_trap_policer_set(struct devlink *devlink,
7446 struct devlink_trap_policer_item *policer_item,
7447 struct genl_info *info)
7449 struct netlink_ext_ack *extack = info->extack;
7450 struct nlattr **attrs = info->attrs;
7454 rate = policer_item->rate;
7455 burst = policer_item->burst;
7457 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
7458 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
7460 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
7461 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
7463 if (rate < policer_item->policer->min_rate) {
7464 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
7468 if (rate > policer_item->policer->max_rate) {
7469 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
7473 if (burst < policer_item->policer->min_burst) {
7474 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
7478 if (burst > policer_item->policer->max_burst) {
7479 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
7483 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
7484 rate, burst, info->extack);
7488 policer_item->rate = rate;
7489 policer_item->burst = burst;
7494 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
7495 struct genl_info *info)
7497 struct devlink_trap_policer_item *policer_item;
7498 struct netlink_ext_ack *extack = info->extack;
7499 struct devlink *devlink = info->user_ptr[0];
7501 if (list_empty(&devlink->trap_policer_list))
7504 if (!devlink->ops->trap_policer_set)
7507 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7508 if (!policer_item) {
7509 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7513 return devlink_trap_policer_set(devlink, policer_item, info);
7516 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
7517 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
7518 DEVLINK_ATTR_TRAP_POLICER_ID },
7519 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
7520 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
7521 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
7522 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
7523 DEVLINK_PORT_TYPE_IB),
7524 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
7525 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
7526 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
7527 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
7528 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
7529 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
7530 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
7531 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
7532 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
7533 DEVLINK_ESWITCH_MODE_SWITCHDEV),
7534 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
7535 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
7536 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
7537 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
7538 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
7539 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
7540 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
7541 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
7542 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
7543 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
7544 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
7545 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
7546 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
7547 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
7548 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
7549 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
7550 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
7551 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
7552 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
7553 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
7554 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
7555 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
7556 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
7557 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
7558 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
7559 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
7560 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
7561 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
7562 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
7563 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
7564 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
7565 [DEVLINK_ATTR_RELOAD_ACTION] = NLA_POLICY_RANGE(NLA_U8, DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
7566 DEVLINK_RELOAD_ACTION_MAX),
7567 [DEVLINK_ATTR_RELOAD_LIMITS] = NLA_POLICY_BITFIELD32(DEVLINK_RELOAD_LIMITS_VALID_MASK),
7570 static const struct genl_small_ops devlink_nl_ops[] = {
7572 .cmd = DEVLINK_CMD_GET,
7573 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7574 .doit = devlink_nl_cmd_get_doit,
7575 .dumpit = devlink_nl_cmd_get_dumpit,
7576 /* can be retrieved by unprivileged users */
7579 .cmd = DEVLINK_CMD_PORT_GET,
7580 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7581 .doit = devlink_nl_cmd_port_get_doit,
7582 .dumpit = devlink_nl_cmd_port_get_dumpit,
7583 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7584 /* can be retrieved by unprivileged users */
7587 .cmd = DEVLINK_CMD_PORT_SET,
7588 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7589 .doit = devlink_nl_cmd_port_set_doit,
7590 .flags = GENL_ADMIN_PERM,
7591 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7594 .cmd = DEVLINK_CMD_PORT_SPLIT,
7595 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7596 .doit = devlink_nl_cmd_port_split_doit,
7597 .flags = GENL_ADMIN_PERM,
7598 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7601 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
7602 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7603 .doit = devlink_nl_cmd_port_unsplit_doit,
7604 .flags = GENL_ADMIN_PERM,
7605 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7608 .cmd = DEVLINK_CMD_SB_GET,
7609 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7610 .doit = devlink_nl_cmd_sb_get_doit,
7611 .dumpit = devlink_nl_cmd_sb_get_dumpit,
7612 /* can be retrieved by unprivileged users */
7615 .cmd = DEVLINK_CMD_SB_POOL_GET,
7616 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7617 .doit = devlink_nl_cmd_sb_pool_get_doit,
7618 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
7619 /* can be retrieved by unprivileged users */
7622 .cmd = DEVLINK_CMD_SB_POOL_SET,
7623 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7624 .doit = devlink_nl_cmd_sb_pool_set_doit,
7625 .flags = GENL_ADMIN_PERM,
7628 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
7629 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7630 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
7631 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
7632 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7633 /* can be retrieved by unprivileged users */
7636 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
7637 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7638 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
7639 .flags = GENL_ADMIN_PERM,
7640 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7643 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
7644 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7645 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
7646 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
7647 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7648 /* can be retrieved by unprivileged users */
7651 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
7652 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7653 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
7654 .flags = GENL_ADMIN_PERM,
7655 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7658 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
7659 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7660 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
7661 .flags = GENL_ADMIN_PERM,
7664 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
7665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7666 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
7667 .flags = GENL_ADMIN_PERM,
7670 .cmd = DEVLINK_CMD_ESWITCH_GET,
7671 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7672 .doit = devlink_nl_cmd_eswitch_get_doit,
7673 .flags = GENL_ADMIN_PERM,
7674 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7677 .cmd = DEVLINK_CMD_ESWITCH_SET,
7678 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7679 .doit = devlink_nl_cmd_eswitch_set_doit,
7680 .flags = GENL_ADMIN_PERM,
7681 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7684 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
7685 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7686 .doit = devlink_nl_cmd_dpipe_table_get,
7687 /* can be retrieved by unprivileged users */
7690 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
7691 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7692 .doit = devlink_nl_cmd_dpipe_entries_get,
7693 /* can be retrieved by unprivileged users */
7696 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
7697 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7698 .doit = devlink_nl_cmd_dpipe_headers_get,
7699 /* can be retrieved by unprivileged users */
7702 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
7703 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7704 .doit = devlink_nl_cmd_dpipe_table_counters_set,
7705 .flags = GENL_ADMIN_PERM,
7708 .cmd = DEVLINK_CMD_RESOURCE_SET,
7709 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7710 .doit = devlink_nl_cmd_resource_set,
7711 .flags = GENL_ADMIN_PERM,
7714 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
7715 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7716 .doit = devlink_nl_cmd_resource_dump,
7717 /* can be retrieved by unprivileged users */
7720 .cmd = DEVLINK_CMD_RELOAD,
7721 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7722 .doit = devlink_nl_cmd_reload,
7723 .flags = GENL_ADMIN_PERM,
7724 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7727 .cmd = DEVLINK_CMD_PARAM_GET,
7728 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7729 .doit = devlink_nl_cmd_param_get_doit,
7730 .dumpit = devlink_nl_cmd_param_get_dumpit,
7731 /* can be retrieved by unprivileged users */
7734 .cmd = DEVLINK_CMD_PARAM_SET,
7735 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7736 .doit = devlink_nl_cmd_param_set_doit,
7737 .flags = GENL_ADMIN_PERM,
7740 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
7741 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7742 .doit = devlink_nl_cmd_port_param_get_doit,
7743 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
7744 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7745 /* can be retrieved by unprivileged users */
7748 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
7749 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7750 .doit = devlink_nl_cmd_port_param_set_doit,
7751 .flags = GENL_ADMIN_PERM,
7752 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7755 .cmd = DEVLINK_CMD_REGION_GET,
7756 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7757 .doit = devlink_nl_cmd_region_get_doit,
7758 .dumpit = devlink_nl_cmd_region_get_dumpit,
7759 .flags = GENL_ADMIN_PERM,
7762 .cmd = DEVLINK_CMD_REGION_NEW,
7763 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7764 .doit = devlink_nl_cmd_region_new,
7765 .flags = GENL_ADMIN_PERM,
7768 .cmd = DEVLINK_CMD_REGION_DEL,
7769 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7770 .doit = devlink_nl_cmd_region_del,
7771 .flags = GENL_ADMIN_PERM,
7774 .cmd = DEVLINK_CMD_REGION_READ,
7775 .validate = GENL_DONT_VALIDATE_STRICT |
7776 GENL_DONT_VALIDATE_DUMP_STRICT,
7777 .dumpit = devlink_nl_cmd_region_read_dumpit,
7778 .flags = GENL_ADMIN_PERM,
7781 .cmd = DEVLINK_CMD_INFO_GET,
7782 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7783 .doit = devlink_nl_cmd_info_get_doit,
7784 .dumpit = devlink_nl_cmd_info_get_dumpit,
7785 /* can be retrieved by unprivileged users */
7788 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
7789 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7790 .doit = devlink_nl_cmd_health_reporter_get_doit,
7791 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
7792 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7793 DEVLINK_NL_FLAG_NO_LOCK,
7794 /* can be retrieved by unprivileged users */
7797 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
7798 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7799 .doit = devlink_nl_cmd_health_reporter_set_doit,
7800 .flags = GENL_ADMIN_PERM,
7801 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7802 DEVLINK_NL_FLAG_NO_LOCK,
7805 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
7806 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7807 .doit = devlink_nl_cmd_health_reporter_recover_doit,
7808 .flags = GENL_ADMIN_PERM,
7809 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7810 DEVLINK_NL_FLAG_NO_LOCK,
7813 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
7814 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7815 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
7816 .flags = GENL_ADMIN_PERM,
7817 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7818 DEVLINK_NL_FLAG_NO_LOCK,
7821 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
7822 .validate = GENL_DONT_VALIDATE_STRICT |
7823 GENL_DONT_VALIDATE_DUMP_STRICT,
7824 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
7825 .flags = GENL_ADMIN_PERM,
7826 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7827 DEVLINK_NL_FLAG_NO_LOCK,
7830 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
7831 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7832 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
7833 .flags = GENL_ADMIN_PERM,
7834 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7835 DEVLINK_NL_FLAG_NO_LOCK,
7838 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
7839 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7840 .doit = devlink_nl_cmd_health_reporter_test_doit,
7841 .flags = GENL_ADMIN_PERM,
7842 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7843 DEVLINK_NL_FLAG_NO_LOCK,
7846 .cmd = DEVLINK_CMD_FLASH_UPDATE,
7847 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7848 .doit = devlink_nl_cmd_flash_update,
7849 .flags = GENL_ADMIN_PERM,
7852 .cmd = DEVLINK_CMD_TRAP_GET,
7853 .doit = devlink_nl_cmd_trap_get_doit,
7854 .dumpit = devlink_nl_cmd_trap_get_dumpit,
7855 /* can be retrieved by unprivileged users */
7858 .cmd = DEVLINK_CMD_TRAP_SET,
7859 .doit = devlink_nl_cmd_trap_set_doit,
7860 .flags = GENL_ADMIN_PERM,
7863 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
7864 .doit = devlink_nl_cmd_trap_group_get_doit,
7865 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
7866 /* can be retrieved by unprivileged users */
7869 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
7870 .doit = devlink_nl_cmd_trap_group_set_doit,
7871 .flags = GENL_ADMIN_PERM,
7874 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
7875 .doit = devlink_nl_cmd_trap_policer_get_doit,
7876 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
7877 /* can be retrieved by unprivileged users */
7880 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
7881 .doit = devlink_nl_cmd_trap_policer_set_doit,
7882 .flags = GENL_ADMIN_PERM,
7886 static struct genl_family devlink_nl_family __ro_after_init = {
7887 .name = DEVLINK_GENL_NAME,
7888 .version = DEVLINK_GENL_VERSION,
7889 .maxattr = DEVLINK_ATTR_MAX,
7890 .policy = devlink_nl_policy,
7892 .pre_doit = devlink_nl_pre_doit,
7893 .post_doit = devlink_nl_post_doit,
7894 .module = THIS_MODULE,
7895 .small_ops = devlink_nl_ops,
7896 .n_small_ops = ARRAY_SIZE(devlink_nl_ops),
7897 .mcgrps = devlink_nl_mcgrps,
7898 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
7901 static bool devlink_reload_actions_valid(const struct devlink_ops *ops)
7903 const struct devlink_reload_combination *comb;
7906 if (!devlink_reload_supported(ops)) {
7907 if (WARN_ON(ops->reload_actions))
7912 if (WARN_ON(!ops->reload_actions ||
7913 ops->reload_actions & BIT(DEVLINK_RELOAD_ACTION_UNSPEC) ||
7914 ops->reload_actions >= BIT(__DEVLINK_RELOAD_ACTION_MAX)))
7917 if (WARN_ON(ops->reload_limits & BIT(DEVLINK_RELOAD_LIMIT_UNSPEC) ||
7918 ops->reload_limits >= BIT(__DEVLINK_RELOAD_LIMIT_MAX)))
7921 for (i = 0; i < ARRAY_SIZE(devlink_reload_invalid_combinations); i++) {
7922 comb = &devlink_reload_invalid_combinations[i];
7923 if (ops->reload_actions == BIT(comb->action) &&
7924 ops->reload_limits == BIT(comb->limit))
7931 * devlink_alloc - Allocate new devlink instance resources
7934 * @priv_size: size of user private data
7936 * Allocate new devlink instance resources, including devlink index
7939 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
7941 struct devlink *devlink;
7946 if (!devlink_reload_actions_valid(ops))
7949 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
7953 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
7954 __devlink_net_set(devlink, &init_net);
7955 INIT_LIST_HEAD(&devlink->port_list);
7956 INIT_LIST_HEAD(&devlink->sb_list);
7957 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
7958 INIT_LIST_HEAD(&devlink->resource_list);
7959 INIT_LIST_HEAD(&devlink->param_list);
7960 INIT_LIST_HEAD(&devlink->region_list);
7961 INIT_LIST_HEAD(&devlink->reporter_list);
7962 INIT_LIST_HEAD(&devlink->trap_list);
7963 INIT_LIST_HEAD(&devlink->trap_group_list);
7964 INIT_LIST_HEAD(&devlink->trap_policer_list);
7965 mutex_init(&devlink->lock);
7966 mutex_init(&devlink->reporters_lock);
7969 EXPORT_SYMBOL_GPL(devlink_alloc);
7972 * devlink_register - Register devlink instance
7975 * @dev: parent device
7977 int devlink_register(struct devlink *devlink, struct device *dev)
7980 devlink->registered = true;
7981 mutex_lock(&devlink_mutex);
7982 list_add_tail(&devlink->list, &devlink_list);
7983 devlink_notify(devlink, DEVLINK_CMD_NEW);
7984 mutex_unlock(&devlink_mutex);
7987 EXPORT_SYMBOL_GPL(devlink_register);
7990 * devlink_unregister - Unregister devlink instance
7994 void devlink_unregister(struct devlink *devlink)
7996 mutex_lock(&devlink_mutex);
7997 WARN_ON(devlink_reload_supported(devlink->ops) &&
7998 devlink->reload_enabled);
7999 devlink_notify(devlink, DEVLINK_CMD_DEL);
8000 list_del(&devlink->list);
8001 mutex_unlock(&devlink_mutex);
8003 EXPORT_SYMBOL_GPL(devlink_unregister);
8006 * devlink_reload_enable - Enable reload of devlink instance
8010 * Should be called at end of device initialization
8011 * process when reload operation is supported.
8013 void devlink_reload_enable(struct devlink *devlink)
8015 mutex_lock(&devlink_mutex);
8016 devlink->reload_enabled = true;
8017 mutex_unlock(&devlink_mutex);
8019 EXPORT_SYMBOL_GPL(devlink_reload_enable);
8022 * devlink_reload_disable - Disable reload of devlink instance
8026 * Should be called at the beginning of device cleanup
8027 * process when reload operation is supported.
8029 void devlink_reload_disable(struct devlink *devlink)
8031 mutex_lock(&devlink_mutex);
8032 /* Mutex is taken which ensures that no reload operation is in
8033 * progress while setting up forbidded flag.
8035 devlink->reload_enabled = false;
8036 mutex_unlock(&devlink_mutex);
8038 EXPORT_SYMBOL_GPL(devlink_reload_disable);
8041 * devlink_free - Free devlink instance resources
8045 void devlink_free(struct devlink *devlink)
8047 mutex_destroy(&devlink->reporters_lock);
8048 mutex_destroy(&devlink->lock);
8049 WARN_ON(!list_empty(&devlink->trap_policer_list));
8050 WARN_ON(!list_empty(&devlink->trap_group_list));
8051 WARN_ON(!list_empty(&devlink->trap_list));
8052 WARN_ON(!list_empty(&devlink->reporter_list));
8053 WARN_ON(!list_empty(&devlink->region_list));
8054 WARN_ON(!list_empty(&devlink->param_list));
8055 WARN_ON(!list_empty(&devlink->resource_list));
8056 WARN_ON(!list_empty(&devlink->dpipe_table_list));
8057 WARN_ON(!list_empty(&devlink->sb_list));
8058 WARN_ON(!list_empty(&devlink->port_list));
8060 xa_destroy(&devlink->snapshot_ids);
8064 EXPORT_SYMBOL_GPL(devlink_free);
8066 static void devlink_port_type_warn(struct work_struct *work)
8068 WARN(true, "Type was not set for devlink port.");
8071 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
8073 /* Ignore CPU and DSA flavours. */
8074 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
8075 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA &&
8076 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED;
8079 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
8081 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
8083 if (!devlink_port_type_should_warn(devlink_port))
8085 /* Schedule a work to WARN in case driver does not set port
8086 * type within timeout.
8088 schedule_delayed_work(&devlink_port->type_warn_dw,
8089 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
8092 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
8094 if (!devlink_port_type_should_warn(devlink_port))
8096 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
8100 * devlink_port_register - Register devlink port
8103 * @devlink_port: devlink port
8104 * @port_index: driver-specific numerical identifier of the port
8106 * Register devlink port with provided port index. User can use
8107 * any indexing, even hw-related one. devlink_port structure
8108 * is convenient to be embedded inside user driver private structure.
8109 * Note that the caller should take care of zeroing the devlink_port
8112 int devlink_port_register(struct devlink *devlink,
8113 struct devlink_port *devlink_port,
8114 unsigned int port_index)
8116 mutex_lock(&devlink->lock);
8117 if (devlink_port_index_exists(devlink, port_index)) {
8118 mutex_unlock(&devlink->lock);
8121 devlink_port->devlink = devlink;
8122 devlink_port->index = port_index;
8123 devlink_port->registered = true;
8124 spin_lock_init(&devlink_port->type_lock);
8125 INIT_LIST_HEAD(&devlink_port->reporter_list);
8126 mutex_init(&devlink_port->reporters_lock);
8127 list_add_tail(&devlink_port->list, &devlink->port_list);
8128 INIT_LIST_HEAD(&devlink_port->param_list);
8129 INIT_LIST_HEAD(&devlink_port->region_list);
8130 mutex_unlock(&devlink->lock);
8131 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
8132 devlink_port_type_warn_schedule(devlink_port);
8133 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8136 EXPORT_SYMBOL_GPL(devlink_port_register);
8139 * devlink_port_unregister - Unregister devlink port
8141 * @devlink_port: devlink port
8143 void devlink_port_unregister(struct devlink_port *devlink_port)
8145 struct devlink *devlink = devlink_port->devlink;
8147 devlink_port_type_warn_cancel(devlink_port);
8148 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
8149 mutex_lock(&devlink->lock);
8150 list_del(&devlink_port->list);
8151 mutex_unlock(&devlink->lock);
8152 WARN_ON(!list_empty(&devlink_port->reporter_list));
8153 WARN_ON(!list_empty(&devlink_port->region_list));
8154 mutex_destroy(&devlink_port->reporters_lock);
8156 EXPORT_SYMBOL_GPL(devlink_port_unregister);
8158 static void __devlink_port_type_set(struct devlink_port *devlink_port,
8159 enum devlink_port_type type,
8162 if (WARN_ON(!devlink_port->registered))
8164 devlink_port_type_warn_cancel(devlink_port);
8165 spin_lock_bh(&devlink_port->type_lock);
8166 devlink_port->type = type;
8167 devlink_port->type_dev = type_dev;
8168 spin_unlock_bh(&devlink_port->type_lock);
8169 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
8172 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
8173 struct net_device *netdev)
8175 const struct net_device_ops *ops = netdev->netdev_ops;
8177 /* If driver registers devlink port, it should set devlink port
8178 * attributes accordingly so the compat functions are called
8179 * and the original ops are not used.
8181 if (ops->ndo_get_phys_port_name) {
8182 /* Some drivers use the same set of ndos for netdevs
8183 * that have devlink_port registered and also for
8184 * those who don't. Make sure that ndo_get_phys_port_name
8185 * returns -EOPNOTSUPP here in case it is defined.
8188 char name[IFNAMSIZ];
8191 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
8192 WARN_ON(err != -EOPNOTSUPP);
8194 if (ops->ndo_get_port_parent_id) {
8195 /* Some drivers use the same set of ndos for netdevs
8196 * that have devlink_port registered and also for
8197 * those who don't. Make sure that ndo_get_port_parent_id
8198 * returns -EOPNOTSUPP here in case it is defined.
8201 struct netdev_phys_item_id ppid;
8204 err = ops->ndo_get_port_parent_id(netdev, &ppid);
8205 WARN_ON(err != -EOPNOTSUPP);
8210 * devlink_port_type_eth_set - Set port type to Ethernet
8212 * @devlink_port: devlink port
8213 * @netdev: related netdevice
8215 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
8216 struct net_device *netdev)
8219 devlink_port_type_netdev_checks(devlink_port, netdev);
8221 dev_warn(devlink_port->devlink->dev,
8222 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
8223 devlink_port->index);
8225 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
8227 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
8230 * devlink_port_type_ib_set - Set port type to InfiniBand
8232 * @devlink_port: devlink port
8233 * @ibdev: related IB device
8235 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
8236 struct ib_device *ibdev)
8238 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
8240 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
8243 * devlink_port_type_clear - Clear port type
8245 * @devlink_port: devlink port
8247 void devlink_port_type_clear(struct devlink_port *devlink_port)
8249 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
8250 devlink_port_type_warn_schedule(devlink_port);
8252 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
8254 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
8255 enum devlink_port_flavour flavour)
8257 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8259 devlink_port->attrs_set = true;
8260 attrs->flavour = flavour;
8261 if (attrs->switch_id.id_len) {
8262 devlink_port->switch_port = true;
8263 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
8264 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
8266 devlink_port->switch_port = false;
8272 * devlink_port_attrs_set - Set port attributes
8274 * @devlink_port: devlink port
8275 * @attrs: devlink port attrs
8277 void devlink_port_attrs_set(struct devlink_port *devlink_port,
8278 struct devlink_port_attrs *attrs)
8282 if (WARN_ON(devlink_port->registered))
8284 devlink_port->attrs = *attrs;
8285 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
8288 WARN_ON(attrs->splittable && attrs->split);
8290 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
8293 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
8295 * @devlink_port: devlink port
8296 * @controller: associated controller number for the devlink port instance
8297 * @pf: associated PF for the devlink port instance
8298 * @external: indicates if the port is for an external controller
8300 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
8301 u16 pf, bool external)
8303 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8306 if (WARN_ON(devlink_port->registered))
8308 ret = __devlink_port_attrs_set(devlink_port,
8309 DEVLINK_PORT_FLAVOUR_PCI_PF);
8312 attrs->pci_pf.controller = controller;
8313 attrs->pci_pf.pf = pf;
8314 attrs->pci_pf.external = external;
8316 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
8319 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
8321 * @devlink_port: devlink port
8322 * @controller: associated controller number for the devlink port instance
8323 * @pf: associated PF for the devlink port instance
8324 * @vf: associated VF of a PF for the devlink port instance
8325 * @external: indicates if the port is for an external controller
8327 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
8328 u16 pf, u16 vf, bool external)
8330 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8333 if (WARN_ON(devlink_port->registered))
8335 ret = __devlink_port_attrs_set(devlink_port,
8336 DEVLINK_PORT_FLAVOUR_PCI_VF);
8339 attrs->pci_vf.controller = controller;
8340 attrs->pci_vf.pf = pf;
8341 attrs->pci_vf.vf = vf;
8342 attrs->pci_vf.external = external;
8344 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
8346 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
8347 char *name, size_t len)
8349 struct devlink_port_attrs *attrs = &devlink_port->attrs;
8352 if (!devlink_port->attrs_set)
8355 switch (attrs->flavour) {
8356 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
8357 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
8359 n = snprintf(name, len, "p%u", attrs->phys.port_number);
8361 n = snprintf(name, len, "p%us%u",
8362 attrs->phys.port_number,
8363 attrs->phys.split_subport_number);
8365 case DEVLINK_PORT_FLAVOUR_CPU:
8366 case DEVLINK_PORT_FLAVOUR_DSA:
8367 case DEVLINK_PORT_FLAVOUR_UNUSED:
8368 /* As CPU and DSA ports do not have a netdevice associated
8369 * case should not ever happen.
8373 case DEVLINK_PORT_FLAVOUR_PCI_PF:
8374 if (attrs->pci_pf.external) {
8375 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
8381 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
8383 case DEVLINK_PORT_FLAVOUR_PCI_VF:
8384 if (attrs->pci_vf.external) {
8385 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
8391 n = snprintf(name, len, "pf%uvf%u",
8392 attrs->pci_vf.pf, attrs->pci_vf.vf);
8402 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
8403 u32 size, u16 ingress_pools_count,
8404 u16 egress_pools_count, u16 ingress_tc_count,
8405 u16 egress_tc_count)
8407 struct devlink_sb *devlink_sb;
8410 mutex_lock(&devlink->lock);
8411 if (devlink_sb_index_exists(devlink, sb_index)) {
8416 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
8421 devlink_sb->index = sb_index;
8422 devlink_sb->size = size;
8423 devlink_sb->ingress_pools_count = ingress_pools_count;
8424 devlink_sb->egress_pools_count = egress_pools_count;
8425 devlink_sb->ingress_tc_count = ingress_tc_count;
8426 devlink_sb->egress_tc_count = egress_tc_count;
8427 list_add_tail(&devlink_sb->list, &devlink->sb_list);
8429 mutex_unlock(&devlink->lock);
8432 EXPORT_SYMBOL_GPL(devlink_sb_register);
8434 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
8436 struct devlink_sb *devlink_sb;
8438 mutex_lock(&devlink->lock);
8439 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
8440 WARN_ON(!devlink_sb);
8441 list_del(&devlink_sb->list);
8442 mutex_unlock(&devlink->lock);
8445 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
8448 * devlink_dpipe_headers_register - register dpipe headers
8451 * @dpipe_headers: dpipe header array
8453 * Register the headers supported by hardware.
8455 int devlink_dpipe_headers_register(struct devlink *devlink,
8456 struct devlink_dpipe_headers *dpipe_headers)
8458 mutex_lock(&devlink->lock);
8459 devlink->dpipe_headers = dpipe_headers;
8460 mutex_unlock(&devlink->lock);
8463 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
8466 * devlink_dpipe_headers_unregister - unregister dpipe headers
8470 * Unregister the headers supported by hardware.
8472 void devlink_dpipe_headers_unregister(struct devlink *devlink)
8474 mutex_lock(&devlink->lock);
8475 devlink->dpipe_headers = NULL;
8476 mutex_unlock(&devlink->lock);
8478 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
8481 * devlink_dpipe_table_counter_enabled - check if counter allocation
8484 * @table_name: tables name
8486 * Used by driver to check if counter allocation is required.
8487 * After counter allocation is turned on the table entries
8488 * are updated to include counter statistics.
8490 * After that point on the driver must respect the counter
8491 * state so that each entry added to the table is added
8494 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
8495 const char *table_name)
8497 struct devlink_dpipe_table *table;
8501 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8502 table_name, devlink);
8505 enabled = table->counters_enabled;
8509 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
8512 * devlink_dpipe_table_register - register dpipe table
8515 * @table_name: table name
8516 * @table_ops: table ops
8518 * @counter_control_extern: external control for counters
8520 int devlink_dpipe_table_register(struct devlink *devlink,
8521 const char *table_name,
8522 struct devlink_dpipe_table_ops *table_ops,
8523 void *priv, bool counter_control_extern)
8525 struct devlink_dpipe_table *table;
8528 if (WARN_ON(!table_ops->size_get))
8531 mutex_lock(&devlink->lock);
8533 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
8539 table = kzalloc(sizeof(*table), GFP_KERNEL);
8545 table->name = table_name;
8546 table->table_ops = table_ops;
8548 table->counter_control_extern = counter_control_extern;
8550 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
8552 mutex_unlock(&devlink->lock);
8555 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
8558 * devlink_dpipe_table_unregister - unregister dpipe table
8561 * @table_name: table name
8563 void devlink_dpipe_table_unregister(struct devlink *devlink,
8564 const char *table_name)
8566 struct devlink_dpipe_table *table;
8568 mutex_lock(&devlink->lock);
8569 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8570 table_name, devlink);
8573 list_del_rcu(&table->list);
8574 mutex_unlock(&devlink->lock);
8575 kfree_rcu(table, rcu);
8578 mutex_unlock(&devlink->lock);
8580 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
8583 * devlink_resource_register - devlink resource register
8586 * @resource_name: resource's name
8587 * @resource_size: resource's size
8588 * @resource_id: resource's id
8589 * @parent_resource_id: resource's parent id
8590 * @size_params: size parameters
8592 int devlink_resource_register(struct devlink *devlink,
8593 const char *resource_name,
8596 u64 parent_resource_id,
8597 const struct devlink_resource_size_params *size_params)
8599 struct devlink_resource *resource;
8600 struct list_head *resource_list;
8604 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
8606 mutex_lock(&devlink->lock);
8607 resource = devlink_resource_find(devlink, NULL, resource_id);
8613 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
8619 if (top_hierarchy) {
8620 resource_list = &devlink->resource_list;
8622 struct devlink_resource *parent_resource;
8624 parent_resource = devlink_resource_find(devlink, NULL,
8625 parent_resource_id);
8626 if (parent_resource) {
8627 resource_list = &parent_resource->resource_list;
8628 resource->parent = parent_resource;
8636 resource->name = resource_name;
8637 resource->size = resource_size;
8638 resource->size_new = resource_size;
8639 resource->id = resource_id;
8640 resource->size_valid = true;
8641 memcpy(&resource->size_params, size_params,
8642 sizeof(resource->size_params));
8643 INIT_LIST_HEAD(&resource->resource_list);
8644 list_add_tail(&resource->list, resource_list);
8646 mutex_unlock(&devlink->lock);
8649 EXPORT_SYMBOL_GPL(devlink_resource_register);
8652 * devlink_resources_unregister - free all resources
8655 * @resource: resource
8657 void devlink_resources_unregister(struct devlink *devlink,
8658 struct devlink_resource *resource)
8660 struct devlink_resource *tmp, *child_resource;
8661 struct list_head *resource_list;
8664 resource_list = &resource->resource_list;
8666 resource_list = &devlink->resource_list;
8669 mutex_lock(&devlink->lock);
8671 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
8672 devlink_resources_unregister(devlink, child_resource);
8673 list_del(&child_resource->list);
8674 kfree(child_resource);
8678 mutex_unlock(&devlink->lock);
8680 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
8683 * devlink_resource_size_get - get and update size
8686 * @resource_id: the requested resource id
8687 * @p_resource_size: ptr to update
8689 int devlink_resource_size_get(struct devlink *devlink,
8691 u64 *p_resource_size)
8693 struct devlink_resource *resource;
8696 mutex_lock(&devlink->lock);
8697 resource = devlink_resource_find(devlink, NULL, resource_id);
8702 *p_resource_size = resource->size_new;
8703 resource->size = resource->size_new;
8705 mutex_unlock(&devlink->lock);
8708 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
8711 * devlink_dpipe_table_resource_set - set the resource id
8714 * @table_name: table name
8715 * @resource_id: resource id
8716 * @resource_units: number of resource's units consumed per table's entry
8718 int devlink_dpipe_table_resource_set(struct devlink *devlink,
8719 const char *table_name, u64 resource_id,
8722 struct devlink_dpipe_table *table;
8725 mutex_lock(&devlink->lock);
8726 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8727 table_name, devlink);
8732 table->resource_id = resource_id;
8733 table->resource_units = resource_units;
8734 table->resource_valid = true;
8736 mutex_unlock(&devlink->lock);
8739 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
8742 * devlink_resource_occ_get_register - register occupancy getter
8745 * @resource_id: resource id
8746 * @occ_get: occupancy getter callback
8747 * @occ_get_priv: occupancy getter callback priv
8749 void devlink_resource_occ_get_register(struct devlink *devlink,
8751 devlink_resource_occ_get_t *occ_get,
8754 struct devlink_resource *resource;
8756 mutex_lock(&devlink->lock);
8757 resource = devlink_resource_find(devlink, NULL, resource_id);
8758 if (WARN_ON(!resource))
8760 WARN_ON(resource->occ_get);
8762 resource->occ_get = occ_get;
8763 resource->occ_get_priv = occ_get_priv;
8765 mutex_unlock(&devlink->lock);
8767 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
8770 * devlink_resource_occ_get_unregister - unregister occupancy getter
8773 * @resource_id: resource id
8775 void devlink_resource_occ_get_unregister(struct devlink *devlink,
8778 struct devlink_resource *resource;
8780 mutex_lock(&devlink->lock);
8781 resource = devlink_resource_find(devlink, NULL, resource_id);
8782 if (WARN_ON(!resource))
8784 WARN_ON(!resource->occ_get);
8786 resource->occ_get = NULL;
8787 resource->occ_get_priv = NULL;
8789 mutex_unlock(&devlink->lock);
8791 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
8793 static int devlink_param_verify(const struct devlink_param *param)
8795 if (!param || !param->name || !param->supported_cmodes)
8798 return devlink_param_generic_verify(param);
8800 return devlink_param_driver_verify(param);
8803 static int __devlink_params_register(struct devlink *devlink,
8804 unsigned int port_index,
8805 struct list_head *param_list,
8806 const struct devlink_param *params,
8807 size_t params_count,
8808 enum devlink_command reg_cmd,
8809 enum devlink_command unreg_cmd)
8811 const struct devlink_param *param = params;
8815 mutex_lock(&devlink->lock);
8816 for (i = 0; i < params_count; i++, param++) {
8817 err = devlink_param_verify(param);
8821 err = devlink_param_register_one(devlink, port_index,
8822 param_list, param, reg_cmd);
8827 mutex_unlock(&devlink->lock);
8833 for (param--; i > 0; i--, param--)
8834 devlink_param_unregister_one(devlink, port_index, param_list,
8837 mutex_unlock(&devlink->lock);
8841 static void __devlink_params_unregister(struct devlink *devlink,
8842 unsigned int port_index,
8843 struct list_head *param_list,
8844 const struct devlink_param *params,
8845 size_t params_count,
8846 enum devlink_command cmd)
8848 const struct devlink_param *param = params;
8851 mutex_lock(&devlink->lock);
8852 for (i = 0; i < params_count; i++, param++)
8853 devlink_param_unregister_one(devlink, 0, param_list, param,
8855 mutex_unlock(&devlink->lock);
8859 * devlink_params_register - register configuration parameters
8862 * @params: configuration parameters array
8863 * @params_count: number of parameters provided
8865 * Register the configuration parameters supported by the driver.
8867 int devlink_params_register(struct devlink *devlink,
8868 const struct devlink_param *params,
8869 size_t params_count)
8871 return __devlink_params_register(devlink, 0, &devlink->param_list,
8872 params, params_count,
8873 DEVLINK_CMD_PARAM_NEW,
8874 DEVLINK_CMD_PARAM_DEL);
8876 EXPORT_SYMBOL_GPL(devlink_params_register);
8879 * devlink_params_unregister - unregister configuration parameters
8881 * @params: configuration parameters to unregister
8882 * @params_count: number of parameters provided
8884 void devlink_params_unregister(struct devlink *devlink,
8885 const struct devlink_param *params,
8886 size_t params_count)
8888 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
8889 params, params_count,
8890 DEVLINK_CMD_PARAM_DEL);
8892 EXPORT_SYMBOL_GPL(devlink_params_unregister);
8895 * devlink_params_publish - publish configuration parameters
8899 * Publish previously registered configuration parameters.
8901 void devlink_params_publish(struct devlink *devlink)
8903 struct devlink_param_item *param_item;
8905 list_for_each_entry(param_item, &devlink->param_list, list) {
8906 if (param_item->published)
8908 param_item->published = true;
8909 devlink_param_notify(devlink, 0, param_item,
8910 DEVLINK_CMD_PARAM_NEW);
8913 EXPORT_SYMBOL_GPL(devlink_params_publish);
8916 * devlink_params_unpublish - unpublish configuration parameters
8920 * Unpublish previously registered configuration parameters.
8922 void devlink_params_unpublish(struct devlink *devlink)
8924 struct devlink_param_item *param_item;
8926 list_for_each_entry(param_item, &devlink->param_list, list) {
8927 if (!param_item->published)
8929 param_item->published = false;
8930 devlink_param_notify(devlink, 0, param_item,
8931 DEVLINK_CMD_PARAM_DEL);
8934 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
8937 * devlink_port_params_register - register port configuration parameters
8939 * @devlink_port: devlink port
8940 * @params: configuration parameters array
8941 * @params_count: number of parameters provided
8943 * Register the configuration parameters supported by the port.
8945 int devlink_port_params_register(struct devlink_port *devlink_port,
8946 const struct devlink_param *params,
8947 size_t params_count)
8949 return __devlink_params_register(devlink_port->devlink,
8950 devlink_port->index,
8951 &devlink_port->param_list, params,
8953 DEVLINK_CMD_PORT_PARAM_NEW,
8954 DEVLINK_CMD_PORT_PARAM_DEL);
8956 EXPORT_SYMBOL_GPL(devlink_port_params_register);
8959 * devlink_port_params_unregister - unregister port configuration
8962 * @devlink_port: devlink port
8963 * @params: configuration parameters array
8964 * @params_count: number of parameters provided
8966 void devlink_port_params_unregister(struct devlink_port *devlink_port,
8967 const struct devlink_param *params,
8968 size_t params_count)
8970 return __devlink_params_unregister(devlink_port->devlink,
8971 devlink_port->index,
8972 &devlink_port->param_list,
8973 params, params_count,
8974 DEVLINK_CMD_PORT_PARAM_DEL);
8976 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
8979 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
8980 union devlink_param_value *init_val)
8982 struct devlink_param_item *param_item;
8984 param_item = devlink_param_find_by_id(param_list, param_id);
8988 if (!param_item->driverinit_value_valid ||
8989 !devlink_param_cmode_is_supported(param_item->param,
8990 DEVLINK_PARAM_CMODE_DRIVERINIT))
8993 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8994 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
8996 *init_val = param_item->driverinit_value;
9002 __devlink_param_driverinit_value_set(struct devlink *devlink,
9003 unsigned int port_index,
9004 struct list_head *param_list, u32 param_id,
9005 union devlink_param_value init_val,
9006 enum devlink_command cmd)
9008 struct devlink_param_item *param_item;
9010 param_item = devlink_param_find_by_id(param_list, param_id);
9014 if (!devlink_param_cmode_is_supported(param_item->param,
9015 DEVLINK_PARAM_CMODE_DRIVERINIT))
9018 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
9019 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
9021 param_item->driverinit_value = init_val;
9022 param_item->driverinit_value_valid = true;
9024 devlink_param_notify(devlink, port_index, param_item, cmd);
9029 * devlink_param_driverinit_value_get - get configuration parameter
9030 * value for driver initializing
9033 * @param_id: parameter ID
9034 * @init_val: value of parameter in driverinit configuration mode
9036 * This function should be used by the driver to get driverinit
9037 * configuration for initialization after reload command.
9039 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
9040 union devlink_param_value *init_val)
9042 if (!devlink_reload_supported(devlink->ops))
9045 return __devlink_param_driverinit_value_get(&devlink->param_list,
9046 param_id, init_val);
9048 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
9051 * devlink_param_driverinit_value_set - set value of configuration
9052 * parameter for driverinit
9053 * configuration mode
9056 * @param_id: parameter ID
9057 * @init_val: value of parameter to set for driverinit configuration mode
9059 * This function should be used by the driver to set driverinit
9060 * configuration mode default value.
9062 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
9063 union devlink_param_value init_val)
9065 return __devlink_param_driverinit_value_set(devlink, 0,
9066 &devlink->param_list,
9068 DEVLINK_CMD_PARAM_NEW);
9070 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
9073 * devlink_port_param_driverinit_value_get - get configuration parameter
9074 * value for driver initializing
9076 * @devlink_port: devlink_port
9077 * @param_id: parameter ID
9078 * @init_val: value of parameter in driverinit configuration mode
9080 * This function should be used by the driver to get driverinit
9081 * configuration for initialization after reload command.
9083 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
9085 union devlink_param_value *init_val)
9087 struct devlink *devlink = devlink_port->devlink;
9089 if (!devlink_reload_supported(devlink->ops))
9092 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
9093 param_id, init_val);
9095 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
9098 * devlink_port_param_driverinit_value_set - set value of configuration
9099 * parameter for driverinit
9100 * configuration mode
9102 * @devlink_port: devlink_port
9103 * @param_id: parameter ID
9104 * @init_val: value of parameter to set for driverinit configuration mode
9106 * This function should be used by the driver to set driverinit
9107 * configuration mode default value.
9109 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
9111 union devlink_param_value init_val)
9113 return __devlink_param_driverinit_value_set(devlink_port->devlink,
9114 devlink_port->index,
9115 &devlink_port->param_list,
9117 DEVLINK_CMD_PORT_PARAM_NEW);
9119 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
9122 * devlink_param_value_changed - notify devlink on a parameter's value
9123 * change. Should be called by the driver
9124 * right after the change.
9127 * @param_id: parameter ID
9129 * This function should be used by the driver to notify devlink on value
9130 * change, excluding driverinit configuration mode.
9131 * For driverinit configuration mode driver should use the function
9133 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
9135 struct devlink_param_item *param_item;
9137 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
9138 WARN_ON(!param_item);
9140 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
9142 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
9145 * devlink_port_param_value_changed - notify devlink on a parameter's value
9146 * change. Should be called by the driver
9147 * right after the change.
9149 * @devlink_port: devlink_port
9150 * @param_id: parameter ID
9152 * This function should be used by the driver to notify devlink on value
9153 * change, excluding driverinit configuration mode.
9154 * For driverinit configuration mode driver should use the function
9155 * devlink_port_param_driverinit_value_set() instead.
9157 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
9160 struct devlink_param_item *param_item;
9162 param_item = devlink_param_find_by_id(&devlink_port->param_list,
9164 WARN_ON(!param_item);
9166 devlink_param_notify(devlink_port->devlink, devlink_port->index,
9167 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
9169 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
9172 * devlink_param_value_str_fill - Safely fill-up the string preventing
9173 * from overflow of the preallocated buffer
9175 * @dst_val: destination devlink_param_value
9176 * @src: source buffer
9178 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
9183 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
9184 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
9186 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
9189 * devlink_region_create - create a new address region
9192 * @ops: region operations and name
9193 * @region_max_snapshots: Maximum supported number of snapshots for region
9194 * @region_size: size of region
9196 struct devlink_region *
9197 devlink_region_create(struct devlink *devlink,
9198 const struct devlink_region_ops *ops,
9199 u32 region_max_snapshots, u64 region_size)
9201 struct devlink_region *region;
9204 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9205 return ERR_PTR(-EINVAL);
9207 mutex_lock(&devlink->lock);
9209 if (devlink_region_get_by_name(devlink, ops->name)) {
9214 region = kzalloc(sizeof(*region), GFP_KERNEL);
9220 region->devlink = devlink;
9221 region->max_snapshots = region_max_snapshots;
9223 region->size = region_size;
9224 INIT_LIST_HEAD(®ion->snapshot_list);
9225 list_add_tail(®ion->list, &devlink->region_list);
9226 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9228 mutex_unlock(&devlink->lock);
9232 mutex_unlock(&devlink->lock);
9233 return ERR_PTR(err);
9235 EXPORT_SYMBOL_GPL(devlink_region_create);
9238 * devlink_port_region_create - create a new address region for a port
9240 * @port: devlink port
9241 * @ops: region operations and name
9242 * @region_max_snapshots: Maximum supported number of snapshots for region
9243 * @region_size: size of region
9245 struct devlink_region *
9246 devlink_port_region_create(struct devlink_port *port,
9247 const struct devlink_port_region_ops *ops,
9248 u32 region_max_snapshots, u64 region_size)
9250 struct devlink *devlink = port->devlink;
9251 struct devlink_region *region;
9254 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
9255 return ERR_PTR(-EINVAL);
9257 mutex_lock(&devlink->lock);
9259 if (devlink_port_region_get_by_name(port, ops->name)) {
9264 region = kzalloc(sizeof(*region), GFP_KERNEL);
9270 region->devlink = devlink;
9271 region->port = port;
9272 region->max_snapshots = region_max_snapshots;
9273 region->port_ops = ops;
9274 region->size = region_size;
9275 INIT_LIST_HEAD(®ion->snapshot_list);
9276 list_add_tail(®ion->list, &port->region_list);
9277 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
9279 mutex_unlock(&devlink->lock);
9283 mutex_unlock(&devlink->lock);
9284 return ERR_PTR(err);
9286 EXPORT_SYMBOL_GPL(devlink_port_region_create);
9289 * devlink_region_destroy - destroy address region
9291 * @region: devlink region to destroy
9293 void devlink_region_destroy(struct devlink_region *region)
9295 struct devlink *devlink = region->devlink;
9296 struct devlink_snapshot *snapshot, *ts;
9298 mutex_lock(&devlink->lock);
9300 /* Free all snapshots of region */
9301 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
9302 devlink_region_snapshot_del(region, snapshot);
9304 list_del(®ion->list);
9306 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
9307 mutex_unlock(&devlink->lock);
9310 EXPORT_SYMBOL_GPL(devlink_region_destroy);
9313 * devlink_region_snapshot_id_get - get snapshot ID
9315 * This callback should be called when adding a new snapshot,
9316 * Driver should use the same id for multiple snapshots taken
9317 * on multiple regions at the same time/by the same trigger.
9319 * The caller of this function must use devlink_region_snapshot_id_put
9320 * when finished creating regions using this id.
9322 * Returns zero on success, or a negative error code on failure.
9325 * @id: storage to return id
9327 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
9331 mutex_lock(&devlink->lock);
9332 err = __devlink_region_snapshot_id_get(devlink, id);
9333 mutex_unlock(&devlink->lock);
9337 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
9340 * devlink_region_snapshot_id_put - put snapshot ID reference
9342 * This should be called by a driver after finishing creating snapshots
9343 * with an id. Doing so ensures that the ID can later be released in the
9344 * event that all snapshots using it have been destroyed.
9347 * @id: id to release reference on
9349 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
9351 mutex_lock(&devlink->lock);
9352 __devlink_snapshot_id_decrement(devlink, id);
9353 mutex_unlock(&devlink->lock);
9355 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
9358 * devlink_region_snapshot_create - create a new snapshot
9359 * This will add a new snapshot of a region. The snapshot
9360 * will be stored on the region struct and can be accessed
9361 * from devlink. This is useful for future analyses of snapshots.
9362 * Multiple snapshots can be created on a region.
9363 * The @snapshot_id should be obtained using the getter function.
9365 * @region: devlink region of the snapshot
9366 * @data: snapshot data
9367 * @snapshot_id: snapshot id to be created
9369 int devlink_region_snapshot_create(struct devlink_region *region,
9370 u8 *data, u32 snapshot_id)
9372 struct devlink *devlink = region->devlink;
9375 mutex_lock(&devlink->lock);
9376 err = __devlink_region_snapshot_create(region, data, snapshot_id);
9377 mutex_unlock(&devlink->lock);
9381 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
9383 #define DEVLINK_TRAP(_id, _type) \
9385 .type = DEVLINK_TRAP_TYPE_##_type, \
9386 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
9387 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
9390 static const struct devlink_trap devlink_trap_generic[] = {
9391 DEVLINK_TRAP(SMAC_MC, DROP),
9392 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
9393 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
9394 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
9395 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
9396 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
9397 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
9398 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
9399 DEVLINK_TRAP(TAIL_DROP, DROP),
9400 DEVLINK_TRAP(NON_IP_PACKET, DROP),
9401 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
9402 DEVLINK_TRAP(DIP_LB, DROP),
9403 DEVLINK_TRAP(SIP_MC, DROP),
9404 DEVLINK_TRAP(SIP_LB, DROP),
9405 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
9406 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
9407 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
9408 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
9409 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
9410 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
9411 DEVLINK_TRAP(RPF, EXCEPTION),
9412 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
9413 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
9414 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
9415 DEVLINK_TRAP(NON_ROUTABLE, DROP),
9416 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
9417 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
9418 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
9419 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
9420 DEVLINK_TRAP(STP, CONTROL),
9421 DEVLINK_TRAP(LACP, CONTROL),
9422 DEVLINK_TRAP(LLDP, CONTROL),
9423 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
9424 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
9425 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
9426 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
9427 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
9428 DEVLINK_TRAP(MLD_QUERY, CONTROL),
9429 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
9430 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
9431 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
9432 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
9433 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
9434 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
9435 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
9436 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
9437 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
9438 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
9439 DEVLINK_TRAP(IPV4_BFD, CONTROL),
9440 DEVLINK_TRAP(IPV6_BFD, CONTROL),
9441 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
9442 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
9443 DEVLINK_TRAP(IPV4_BGP, CONTROL),
9444 DEVLINK_TRAP(IPV6_BGP, CONTROL),
9445 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
9446 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
9447 DEVLINK_TRAP(IPV4_PIM, CONTROL),
9448 DEVLINK_TRAP(IPV6_PIM, CONTROL),
9449 DEVLINK_TRAP(UC_LB, CONTROL),
9450 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
9451 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
9452 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
9453 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
9454 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
9455 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
9456 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
9457 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
9458 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
9459 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
9460 DEVLINK_TRAP(PTP_EVENT, CONTROL),
9461 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
9462 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
9463 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
9464 DEVLINK_TRAP(EARLY_DROP, DROP),
9465 DEVLINK_TRAP(VXLAN_PARSING, DROP),
9466 DEVLINK_TRAP(LLC_SNAP_PARSING, DROP),
9467 DEVLINK_TRAP(VLAN_PARSING, DROP),
9468 DEVLINK_TRAP(PPPOE_PPP_PARSING, DROP),
9469 DEVLINK_TRAP(MPLS_PARSING, DROP),
9470 DEVLINK_TRAP(ARP_PARSING, DROP),
9471 DEVLINK_TRAP(IP_1_PARSING, DROP),
9472 DEVLINK_TRAP(IP_N_PARSING, DROP),
9473 DEVLINK_TRAP(GRE_PARSING, DROP),
9474 DEVLINK_TRAP(UDP_PARSING, DROP),
9475 DEVLINK_TRAP(TCP_PARSING, DROP),
9476 DEVLINK_TRAP(IPSEC_PARSING, DROP),
9477 DEVLINK_TRAP(SCTP_PARSING, DROP),
9478 DEVLINK_TRAP(DCCP_PARSING, DROP),
9479 DEVLINK_TRAP(GTP_PARSING, DROP),
9480 DEVLINK_TRAP(ESP_PARSING, DROP),
9483 #define DEVLINK_TRAP_GROUP(_id) \
9485 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
9486 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
9489 static const struct devlink_trap_group devlink_trap_group_generic[] = {
9490 DEVLINK_TRAP_GROUP(L2_DROPS),
9491 DEVLINK_TRAP_GROUP(L3_DROPS),
9492 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
9493 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
9494 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
9495 DEVLINK_TRAP_GROUP(ACL_DROPS),
9496 DEVLINK_TRAP_GROUP(STP),
9497 DEVLINK_TRAP_GROUP(LACP),
9498 DEVLINK_TRAP_GROUP(LLDP),
9499 DEVLINK_TRAP_GROUP(MC_SNOOPING),
9500 DEVLINK_TRAP_GROUP(DHCP),
9501 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
9502 DEVLINK_TRAP_GROUP(BFD),
9503 DEVLINK_TRAP_GROUP(OSPF),
9504 DEVLINK_TRAP_GROUP(BGP),
9505 DEVLINK_TRAP_GROUP(VRRP),
9506 DEVLINK_TRAP_GROUP(PIM),
9507 DEVLINK_TRAP_GROUP(UC_LB),
9508 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
9509 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
9510 DEVLINK_TRAP_GROUP(IPV6),
9511 DEVLINK_TRAP_GROUP(PTP_EVENT),
9512 DEVLINK_TRAP_GROUP(PTP_GENERAL),
9513 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
9514 DEVLINK_TRAP_GROUP(ACL_TRAP),
9515 DEVLINK_TRAP_GROUP(PARSER_ERROR_DROPS),
9518 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
9520 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
9523 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
9526 if (trap->type != devlink_trap_generic[trap->id].type)
9532 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
9536 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
9539 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
9540 if (!strcmp(trap->name, devlink_trap_generic[i].name))
9547 static int devlink_trap_verify(const struct devlink_trap *trap)
9549 if (!trap || !trap->name)
9553 return devlink_trap_generic_verify(trap);
9555 return devlink_trap_driver_verify(trap);
9559 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
9561 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9564 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
9571 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
9575 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9578 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
9579 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
9586 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
9589 return devlink_trap_group_generic_verify(group);
9591 return devlink_trap_group_driver_verify(group);
9595 devlink_trap_group_notify(struct devlink *devlink,
9596 const struct devlink_trap_group_item *group_item,
9597 enum devlink_command cmd)
9599 struct sk_buff *msg;
9602 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
9603 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
9605 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9609 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
9616 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9617 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9621 devlink_trap_item_group_link(struct devlink *devlink,
9622 struct devlink_trap_item *trap_item)
9624 u16 group_id = trap_item->trap->init_group_id;
9625 struct devlink_trap_group_item *group_item;
9627 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
9628 if (WARN_ON_ONCE(!group_item))
9631 trap_item->group_item = group_item;
9636 static void devlink_trap_notify(struct devlink *devlink,
9637 const struct devlink_trap_item *trap_item,
9638 enum devlink_command cmd)
9640 struct sk_buff *msg;
9643 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
9644 cmd != DEVLINK_CMD_TRAP_DEL);
9646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9650 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
9656 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9657 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9661 devlink_trap_register(struct devlink *devlink,
9662 const struct devlink_trap *trap, void *priv)
9664 struct devlink_trap_item *trap_item;
9667 if (devlink_trap_item_lookup(devlink, trap->name))
9670 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
9674 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9675 if (!trap_item->stats) {
9677 goto err_stats_alloc;
9680 trap_item->trap = trap;
9681 trap_item->action = trap->init_action;
9682 trap_item->priv = priv;
9684 err = devlink_trap_item_group_link(devlink, trap_item);
9686 goto err_group_link;
9688 err = devlink->ops->trap_init(devlink, trap, trap_item);
9692 list_add_tail(&trap_item->list, &devlink->trap_list);
9693 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9699 free_percpu(trap_item->stats);
9705 static void devlink_trap_unregister(struct devlink *devlink,
9706 const struct devlink_trap *trap)
9708 struct devlink_trap_item *trap_item;
9710 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9711 if (WARN_ON_ONCE(!trap_item))
9714 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9715 list_del(&trap_item->list);
9716 if (devlink->ops->trap_fini)
9717 devlink->ops->trap_fini(devlink, trap, trap_item);
9718 free_percpu(trap_item->stats);
9722 static void devlink_trap_disable(struct devlink *devlink,
9723 const struct devlink_trap *trap)
9725 struct devlink_trap_item *trap_item;
9727 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9728 if (WARN_ON_ONCE(!trap_item))
9731 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
9733 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
9737 * devlink_traps_register - Register packet traps with devlink.
9738 * @devlink: devlink.
9739 * @traps: Packet traps.
9740 * @traps_count: Count of provided packet traps.
9741 * @priv: Driver private information.
9743 * Return: Non-zero value on failure.
9745 int devlink_traps_register(struct devlink *devlink,
9746 const struct devlink_trap *traps,
9747 size_t traps_count, void *priv)
9751 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
9754 mutex_lock(&devlink->lock);
9755 for (i = 0; i < traps_count; i++) {
9756 const struct devlink_trap *trap = &traps[i];
9758 err = devlink_trap_verify(trap);
9760 goto err_trap_verify;
9762 err = devlink_trap_register(devlink, trap, priv);
9764 goto err_trap_register;
9766 mutex_unlock(&devlink->lock);
9772 for (i--; i >= 0; i--)
9773 devlink_trap_unregister(devlink, &traps[i]);
9774 mutex_unlock(&devlink->lock);
9777 EXPORT_SYMBOL_GPL(devlink_traps_register);
9780 * devlink_traps_unregister - Unregister packet traps from devlink.
9781 * @devlink: devlink.
9782 * @traps: Packet traps.
9783 * @traps_count: Count of provided packet traps.
9785 void devlink_traps_unregister(struct devlink *devlink,
9786 const struct devlink_trap *traps,
9791 mutex_lock(&devlink->lock);
9792 /* Make sure we do not have any packets in-flight while unregistering
9793 * traps by disabling all of them and waiting for a grace period.
9795 for (i = traps_count - 1; i >= 0; i--)
9796 devlink_trap_disable(devlink, &traps[i]);
9798 for (i = traps_count - 1; i >= 0; i--)
9799 devlink_trap_unregister(devlink, &traps[i]);
9800 mutex_unlock(&devlink->lock);
9802 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
9805 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
9808 struct devlink_stats *stats;
9810 stats = this_cpu_ptr(trap_stats);
9811 u64_stats_update_begin(&stats->syncp);
9812 stats->rx_bytes += skb_len;
9813 stats->rx_packets++;
9814 u64_stats_update_end(&stats->syncp);
9818 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
9819 const struct devlink_trap_item *trap_item,
9820 struct devlink_port *in_devlink_port,
9821 const struct flow_action_cookie *fa_cookie)
9823 metadata->trap_name = trap_item->trap->name;
9824 metadata->trap_group_name = trap_item->group_item->group->name;
9825 metadata->fa_cookie = fa_cookie;
9826 metadata->trap_type = trap_item->trap->type;
9828 spin_lock(&in_devlink_port->type_lock);
9829 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9830 metadata->input_dev = in_devlink_port->type_dev;
9831 spin_unlock(&in_devlink_port->type_lock);
9835 * devlink_trap_report - Report trapped packet to drop monitor.
9836 * @devlink: devlink.
9837 * @skb: Trapped packet.
9838 * @trap_ctx: Trap context.
9839 * @in_devlink_port: Input devlink port.
9840 * @fa_cookie: Flow action cookie. Could be NULL.
9842 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
9843 void *trap_ctx, struct devlink_port *in_devlink_port,
9844 const struct flow_action_cookie *fa_cookie)
9847 struct devlink_trap_item *trap_item = trap_ctx;
9849 devlink_trap_stats_update(trap_item->stats, skb->len);
9850 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
9852 if (trace_devlink_trap_report_enabled()) {
9853 struct devlink_trap_metadata metadata = {};
9855 devlink_trap_report_metadata_set(&metadata, trap_item,
9856 in_devlink_port, fa_cookie);
9857 trace_devlink_trap_report(devlink, skb, &metadata);
9860 EXPORT_SYMBOL_GPL(devlink_trap_report);
9863 * devlink_trap_ctx_priv - Trap context to driver private information.
9864 * @trap_ctx: Trap context.
9866 * Return: Driver private information passed during registration.
9868 void *devlink_trap_ctx_priv(void *trap_ctx)
9870 struct devlink_trap_item *trap_item = trap_ctx;
9872 return trap_item->priv;
9874 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
9877 devlink_trap_group_item_policer_link(struct devlink *devlink,
9878 struct devlink_trap_group_item *group_item)
9880 u32 policer_id = group_item->group->init_policer_id;
9881 struct devlink_trap_policer_item *policer_item;
9883 if (policer_id == 0)
9886 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
9887 if (WARN_ON_ONCE(!policer_item))
9890 group_item->policer_item = policer_item;
9896 devlink_trap_group_register(struct devlink *devlink,
9897 const struct devlink_trap_group *group)
9899 struct devlink_trap_group_item *group_item;
9902 if (devlink_trap_group_item_lookup(devlink, group->name))
9905 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
9909 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9910 if (!group_item->stats) {
9912 goto err_stats_alloc;
9915 group_item->group = group;
9917 err = devlink_trap_group_item_policer_link(devlink, group_item);
9919 goto err_policer_link;
9921 if (devlink->ops->trap_group_init) {
9922 err = devlink->ops->trap_group_init(devlink, group);
9924 goto err_group_init;
9927 list_add_tail(&group_item->list, &devlink->trap_group_list);
9928 devlink_trap_group_notify(devlink, group_item,
9929 DEVLINK_CMD_TRAP_GROUP_NEW);
9935 free_percpu(group_item->stats);
9942 devlink_trap_group_unregister(struct devlink *devlink,
9943 const struct devlink_trap_group *group)
9945 struct devlink_trap_group_item *group_item;
9947 group_item = devlink_trap_group_item_lookup(devlink, group->name);
9948 if (WARN_ON_ONCE(!group_item))
9951 devlink_trap_group_notify(devlink, group_item,
9952 DEVLINK_CMD_TRAP_GROUP_DEL);
9953 list_del(&group_item->list);
9954 free_percpu(group_item->stats);
9959 * devlink_trap_groups_register - Register packet trap groups with devlink.
9960 * @devlink: devlink.
9961 * @groups: Packet trap groups.
9962 * @groups_count: Count of provided packet trap groups.
9964 * Return: Non-zero value on failure.
9966 int devlink_trap_groups_register(struct devlink *devlink,
9967 const struct devlink_trap_group *groups,
9968 size_t groups_count)
9972 mutex_lock(&devlink->lock);
9973 for (i = 0; i < groups_count; i++) {
9974 const struct devlink_trap_group *group = &groups[i];
9976 err = devlink_trap_group_verify(group);
9978 goto err_trap_group_verify;
9980 err = devlink_trap_group_register(devlink, group);
9982 goto err_trap_group_register;
9984 mutex_unlock(&devlink->lock);
9988 err_trap_group_register:
9989 err_trap_group_verify:
9990 for (i--; i >= 0; i--)
9991 devlink_trap_group_unregister(devlink, &groups[i]);
9992 mutex_unlock(&devlink->lock);
9995 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
9998 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
9999 * @devlink: devlink.
10000 * @groups: Packet trap groups.
10001 * @groups_count: Count of provided packet trap groups.
10003 void devlink_trap_groups_unregister(struct devlink *devlink,
10004 const struct devlink_trap_group *groups,
10005 size_t groups_count)
10009 mutex_lock(&devlink->lock);
10010 for (i = groups_count - 1; i >= 0; i--)
10011 devlink_trap_group_unregister(devlink, &groups[i]);
10012 mutex_unlock(&devlink->lock);
10014 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
10017 devlink_trap_policer_notify(struct devlink *devlink,
10018 const struct devlink_trap_policer_item *policer_item,
10019 enum devlink_command cmd)
10021 struct sk_buff *msg;
10024 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
10025 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
10027 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10031 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
10038 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
10039 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
10043 devlink_trap_policer_register(struct devlink *devlink,
10044 const struct devlink_trap_policer *policer)
10046 struct devlink_trap_policer_item *policer_item;
10049 if (devlink_trap_policer_item_lookup(devlink, policer->id))
10052 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
10056 policer_item->policer = policer;
10057 policer_item->rate = policer->init_rate;
10058 policer_item->burst = policer->init_burst;
10060 if (devlink->ops->trap_policer_init) {
10061 err = devlink->ops->trap_policer_init(devlink, policer);
10063 goto err_policer_init;
10066 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
10067 devlink_trap_policer_notify(devlink, policer_item,
10068 DEVLINK_CMD_TRAP_POLICER_NEW);
10073 kfree(policer_item);
10078 devlink_trap_policer_unregister(struct devlink *devlink,
10079 const struct devlink_trap_policer *policer)
10081 struct devlink_trap_policer_item *policer_item;
10083 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
10084 if (WARN_ON_ONCE(!policer_item))
10087 devlink_trap_policer_notify(devlink, policer_item,
10088 DEVLINK_CMD_TRAP_POLICER_DEL);
10089 list_del(&policer_item->list);
10090 if (devlink->ops->trap_policer_fini)
10091 devlink->ops->trap_policer_fini(devlink, policer);
10092 kfree(policer_item);
10096 * devlink_trap_policers_register - Register packet trap policers with devlink.
10097 * @devlink: devlink.
10098 * @policers: Packet trap policers.
10099 * @policers_count: Count of provided packet trap policers.
10101 * Return: Non-zero value on failure.
10104 devlink_trap_policers_register(struct devlink *devlink,
10105 const struct devlink_trap_policer *policers,
10106 size_t policers_count)
10110 mutex_lock(&devlink->lock);
10111 for (i = 0; i < policers_count; i++) {
10112 const struct devlink_trap_policer *policer = &policers[i];
10114 if (WARN_ON(policer->id == 0 ||
10115 policer->max_rate < policer->min_rate ||
10116 policer->max_burst < policer->min_burst)) {
10118 goto err_trap_policer_verify;
10121 err = devlink_trap_policer_register(devlink, policer);
10123 goto err_trap_policer_register;
10125 mutex_unlock(&devlink->lock);
10129 err_trap_policer_register:
10130 err_trap_policer_verify:
10131 for (i--; i >= 0; i--)
10132 devlink_trap_policer_unregister(devlink, &policers[i]);
10133 mutex_unlock(&devlink->lock);
10136 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
10139 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
10140 * @devlink: devlink.
10141 * @policers: Packet trap policers.
10142 * @policers_count: Count of provided packet trap policers.
10145 devlink_trap_policers_unregister(struct devlink *devlink,
10146 const struct devlink_trap_policer *policers,
10147 size_t policers_count)
10151 mutex_lock(&devlink->lock);
10152 for (i = policers_count - 1; i >= 0; i--)
10153 devlink_trap_policer_unregister(devlink, &policers[i]);
10154 mutex_unlock(&devlink->lock);
10156 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
10158 static void __devlink_compat_running_version(struct devlink *devlink,
10159 char *buf, size_t len)
10161 const struct nlattr *nlattr;
10162 struct devlink_info_req req;
10163 struct sk_buff *msg;
10166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10171 err = devlink->ops->info_get(devlink, &req, NULL);
10175 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
10176 const struct nlattr *kv;
10179 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
10182 nla_for_each_nested(kv, nlattr, rem_kv) {
10183 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
10186 strlcat(buf, nla_data(kv), len);
10187 strlcat(buf, " ", len);
10194 void devlink_compat_running_version(struct net_device *dev,
10195 char *buf, size_t len)
10197 struct devlink *devlink;
10202 devlink = netdev_to_devlink(dev);
10203 if (!devlink || !devlink->ops->info_get)
10206 mutex_lock(&devlink->lock);
10207 __devlink_compat_running_version(devlink, buf, len);
10208 mutex_unlock(&devlink->lock);
10215 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
10217 struct devlink_flash_update_params params = {};
10218 struct devlink *devlink;
10224 devlink = netdev_to_devlink(dev);
10225 if (!devlink || !devlink->ops->flash_update) {
10230 params.file_name = file_name;
10232 mutex_lock(&devlink->lock);
10233 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
10234 mutex_unlock(&devlink->lock);
10243 int devlink_compat_phys_port_name_get(struct net_device *dev,
10244 char *name, size_t len)
10246 struct devlink_port *devlink_port;
10248 /* RTNL mutex is held here which ensures that devlink_port
10249 * instance cannot disappear in the middle. No need to take
10250 * any devlink lock as only permanent values are accessed.
10254 devlink_port = netdev_to_devlink_port(dev);
10256 return -EOPNOTSUPP;
10258 return __devlink_port_phys_port_name_get(devlink_port, name, len);
10261 int devlink_compat_switch_id_get(struct net_device *dev,
10262 struct netdev_phys_item_id *ppid)
10264 struct devlink_port *devlink_port;
10266 /* Caller must hold RTNL mutex or reference to dev, which ensures that
10267 * devlink_port instance cannot disappear in the middle. No need to take
10268 * any devlink lock as only permanent values are accessed.
10270 devlink_port = netdev_to_devlink_port(dev);
10271 if (!devlink_port || !devlink_port->switch_port)
10272 return -EOPNOTSUPP;
10274 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
10279 static void __net_exit devlink_pernet_pre_exit(struct net *net)
10281 struct devlink *devlink;
10282 u32 actions_performed;
10285 /* In case network namespace is getting destroyed, reload
10286 * all devlink instances from this namespace into init_net.
10288 mutex_lock(&devlink_mutex);
10289 list_for_each_entry(devlink, &devlink_list, list) {
10290 if (net_eq(devlink_net(devlink), net)) {
10291 if (WARN_ON(!devlink_reload_supported(devlink->ops)))
10293 err = devlink_reload(devlink, &init_net,
10294 DEVLINK_RELOAD_ACTION_DRIVER_REINIT,
10295 DEVLINK_RELOAD_LIMIT_UNSPEC,
10296 &actions_performed, NULL);
10297 if (err && err != -EOPNOTSUPP)
10298 pr_warn("Failed to reload devlink instance into init_net\n");
10301 mutex_unlock(&devlink_mutex);
10304 static struct pernet_operations devlink_pernet_ops __net_initdata = {
10305 .pre_exit = devlink_pernet_pre_exit,
10308 static int __init devlink_init(void)
10312 err = genl_register_family(&devlink_nl_family);
10315 err = register_pernet_subsys(&devlink_pernet_ops);
10322 subsys_initcall(devlink_init);