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 list_head list;
351 const struct devlink_region_ops *ops;
352 struct list_head snapshot_list;
358 struct devlink_snapshot {
359 struct list_head list;
360 struct devlink_region *region;
365 static struct devlink_region *
366 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
368 struct devlink_region *region;
370 list_for_each_entry(region, &devlink->region_list, list)
371 if (!strcmp(region->ops->name, region_name))
377 static struct devlink_snapshot *
378 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
380 struct devlink_snapshot *snapshot;
382 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
383 if (snapshot->id == id)
389 #define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
390 #define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
392 /* The per devlink instance lock is taken by default in the pre-doit
393 * operation, yet several commands do not require this. The global
394 * devlink lock is taken and protects from disruption by user-calls.
396 #define DEVLINK_NL_FLAG_NO_LOCK BIT(2)
398 static int devlink_nl_pre_doit(const struct genl_ops *ops,
399 struct sk_buff *skb, struct genl_info *info)
401 struct devlink_port *devlink_port;
402 struct devlink *devlink;
405 mutex_lock(&devlink_mutex);
406 devlink = devlink_get_from_info(info);
407 if (IS_ERR(devlink)) {
408 mutex_unlock(&devlink_mutex);
409 return PTR_ERR(devlink);
411 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
412 mutex_lock(&devlink->lock);
413 info->user_ptr[0] = devlink;
414 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
415 devlink_port = devlink_port_get_from_info(devlink, info);
416 if (IS_ERR(devlink_port)) {
417 err = PTR_ERR(devlink_port);
420 info->user_ptr[1] = devlink_port;
421 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
422 devlink_port = devlink_port_get_from_info(devlink, info);
423 if (!IS_ERR(devlink_port))
424 info->user_ptr[1] = devlink_port;
429 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
430 mutex_unlock(&devlink->lock);
431 mutex_unlock(&devlink_mutex);
435 static void devlink_nl_post_doit(const struct genl_ops *ops,
436 struct sk_buff *skb, struct genl_info *info)
438 struct devlink *devlink;
440 devlink = info->user_ptr[0];
441 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
442 mutex_unlock(&devlink->lock);
443 mutex_unlock(&devlink_mutex);
446 static struct genl_family devlink_nl_family;
448 enum devlink_multicast_groups {
449 DEVLINK_MCGRP_CONFIG,
452 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
453 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
456 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
458 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
460 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
465 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
466 enum devlink_command cmd, u32 portid,
471 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
475 if (devlink_nl_put_handle(msg, devlink))
476 goto nla_put_failure;
477 if (nla_put_u8(msg, DEVLINK_ATTR_RELOAD_FAILED, devlink->reload_failed))
478 goto nla_put_failure;
480 genlmsg_end(msg, hdr);
484 genlmsg_cancel(msg, hdr);
488 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
493 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
495 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
499 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
505 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
506 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
509 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
510 struct devlink_port *devlink_port)
512 struct devlink_port_attrs *attrs = &devlink_port->attrs;
514 if (!devlink_port->attrs_set)
517 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
520 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
522 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
524 switch (devlink_port->attrs.flavour) {
525 case DEVLINK_PORT_FLAVOUR_PCI_PF:
526 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
527 attrs->pci_pf.controller) ||
528 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf))
530 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
533 case DEVLINK_PORT_FLAVOUR_PCI_VF:
534 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER,
535 attrs->pci_vf.controller) ||
536 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) ||
537 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf))
539 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
542 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
543 case DEVLINK_PORT_FLAVOUR_CPU:
544 case DEVLINK_PORT_FLAVOUR_DSA:
545 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
546 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER,
547 attrs->phys.port_number))
551 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
552 attrs->phys.port_number))
554 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
555 attrs->phys.split_subport_number))
565 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
566 struct netlink_ext_ack *extack)
568 struct devlink *devlink = port->devlink;
569 const struct devlink_ops *ops;
570 struct nlattr *function_attr;
571 bool empty_nest = true;
574 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
579 if (ops->port_function_hw_addr_get) {
581 u8 hw_addr[MAX_ADDR_LEN];
583 err = ops->port_function_hw_addr_get(devlink, port, hw_addr, &hw_addr_len, extack);
584 if (err == -EOPNOTSUPP) {
585 /* Port function attributes are optional for a port. If port doesn't
586 * support function attribute, returning -EOPNOTSUPP is not an error.
593 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
600 if (err || empty_nest)
601 nla_nest_cancel(msg, function_attr);
603 nla_nest_end(msg, function_attr);
607 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
608 struct devlink_port *devlink_port,
609 enum devlink_command cmd, u32 portid,
611 struct netlink_ext_ack *extack)
615 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
619 if (devlink_nl_put_handle(msg, devlink))
620 goto nla_put_failure;
621 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
622 goto nla_put_failure;
624 spin_lock_bh(&devlink_port->type_lock);
625 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
626 goto nla_put_failure_type_locked;
627 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
628 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
629 devlink_port->desired_type))
630 goto nla_put_failure_type_locked;
631 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
632 struct net_device *netdev = devlink_port->type_dev;
635 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
637 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
639 goto nla_put_failure_type_locked;
641 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
642 struct ib_device *ibdev = devlink_port->type_dev;
645 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
647 goto nla_put_failure_type_locked;
649 spin_unlock_bh(&devlink_port->type_lock);
650 if (devlink_nl_port_attrs_put(msg, devlink_port))
651 goto nla_put_failure;
652 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack))
653 goto nla_put_failure;
655 genlmsg_end(msg, hdr);
658 nla_put_failure_type_locked:
659 spin_unlock_bh(&devlink_port->type_lock);
661 genlmsg_cancel(msg, hdr);
665 static void devlink_port_notify(struct devlink_port *devlink_port,
666 enum devlink_command cmd)
668 struct devlink *devlink = devlink_port->devlink;
672 if (!devlink_port->registered)
675 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
677 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
681 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0,
688 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
689 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
692 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
694 struct devlink *devlink = info->user_ptr[0];
698 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
702 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
703 info->snd_portid, info->snd_seq, 0);
709 return genlmsg_reply(msg, info);
712 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
713 struct netlink_callback *cb)
715 struct devlink *devlink;
716 int start = cb->args[0];
720 mutex_lock(&devlink_mutex);
721 list_for_each_entry(devlink, &devlink_list, list) {
722 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
728 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
729 NETLINK_CB(cb->skb).portid,
730 cb->nlh->nlmsg_seq, NLM_F_MULTI);
736 mutex_unlock(&devlink_mutex);
742 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
743 struct genl_info *info)
745 struct devlink_port *devlink_port = info->user_ptr[1];
746 struct devlink *devlink = devlink_port->devlink;
750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
754 err = devlink_nl_port_fill(msg, devlink, devlink_port,
755 DEVLINK_CMD_PORT_NEW,
756 info->snd_portid, info->snd_seq, 0,
763 return genlmsg_reply(msg, info);
766 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
767 struct netlink_callback *cb)
769 struct devlink *devlink;
770 struct devlink_port *devlink_port;
771 int start = cb->args[0];
775 mutex_lock(&devlink_mutex);
776 list_for_each_entry(devlink, &devlink_list, list) {
777 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
779 mutex_lock(&devlink->lock);
780 list_for_each_entry(devlink_port, &devlink->port_list, list) {
785 err = devlink_nl_port_fill(msg, devlink, devlink_port,
787 NETLINK_CB(cb->skb).portid,
792 mutex_unlock(&devlink->lock);
797 mutex_unlock(&devlink->lock);
800 mutex_unlock(&devlink_mutex);
806 static int devlink_port_type_set(struct devlink *devlink,
807 struct devlink_port *devlink_port,
808 enum devlink_port_type port_type)
813 if (devlink->ops->port_type_set) {
814 if (port_type == devlink_port->type)
816 err = devlink->ops->port_type_set(devlink_port, port_type);
819 devlink_port->desired_type = port_type;
820 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
827 devlink_port_function_hw_addr_set(struct devlink *devlink, struct devlink_port *port,
828 const struct nlattr *attr, struct netlink_ext_ack *extack)
830 const struct devlink_ops *ops;
835 hw_addr = nla_data(attr);
836 hw_addr_len = nla_len(attr);
837 if (hw_addr_len > MAX_ADDR_LEN) {
838 NL_SET_ERR_MSG_MOD(extack, "Port function hardware address too long");
841 if (port->type == DEVLINK_PORT_TYPE_ETH) {
842 if (hw_addr_len != ETH_ALEN) {
843 NL_SET_ERR_MSG_MOD(extack, "Address must be 6 bytes for Ethernet device");
846 if (!is_unicast_ether_addr(hw_addr)) {
847 NL_SET_ERR_MSG_MOD(extack, "Non-unicast hardware address unsupported");
853 if (!ops->port_function_hw_addr_set) {
854 NL_SET_ERR_MSG_MOD(extack, "Port doesn't support function attributes");
858 err = ops->port_function_hw_addr_set(devlink, port, hw_addr, hw_addr_len, extack);
862 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
867 devlink_port_function_set(struct devlink *devlink, struct devlink_port *port,
868 const struct nlattr *attr, struct netlink_ext_ack *extack)
870 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
873 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
874 devlink_function_nl_policy, extack);
876 NL_SET_ERR_MSG_MOD(extack, "Fail to parse port function attributes");
880 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
882 err = devlink_port_function_hw_addr_set(devlink, port, attr, extack);
887 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
888 struct genl_info *info)
890 struct devlink_port *devlink_port = info->user_ptr[1];
891 struct devlink *devlink = devlink_port->devlink;
894 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
895 enum devlink_port_type port_type;
897 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
898 err = devlink_port_type_set(devlink, devlink_port, port_type);
903 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) {
904 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION];
905 struct netlink_ext_ack *extack = info->extack;
907 err = devlink_port_function_set(devlink, devlink_port, attr, extack);
915 static int devlink_port_split(struct devlink *devlink, u32 port_index,
916 u32 count, struct netlink_ext_ack *extack)
919 if (devlink->ops->port_split)
920 return devlink->ops->port_split(devlink, port_index, count,
925 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
926 struct genl_info *info)
928 struct devlink *devlink = info->user_ptr[0];
929 struct devlink_port *devlink_port;
933 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
934 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
937 devlink_port = devlink_port_get_from_info(devlink, info);
938 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
939 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
941 if (IS_ERR(devlink_port))
944 if (!devlink_port->attrs.splittable) {
945 /* Split ports cannot be split. */
946 if (devlink_port->attrs.split)
947 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split further");
949 NL_SET_ERR_MSG_MOD(info->extack, "Port cannot be split");
953 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
954 NL_SET_ERR_MSG_MOD(info->extack, "Invalid split count");
958 return devlink_port_split(devlink, port_index, count, info->extack);
961 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
962 struct netlink_ext_ack *extack)
965 if (devlink->ops->port_unsplit)
966 return devlink->ops->port_unsplit(devlink, port_index, extack);
970 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
971 struct genl_info *info)
973 struct devlink *devlink = info->user_ptr[0];
976 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
979 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
980 return devlink_port_unsplit(devlink, port_index, info->extack);
983 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
984 struct devlink_sb *devlink_sb,
985 enum devlink_command cmd, u32 portid,
990 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
994 if (devlink_nl_put_handle(msg, devlink))
995 goto nla_put_failure;
996 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
997 goto nla_put_failure;
998 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
999 goto nla_put_failure;
1000 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
1001 devlink_sb->ingress_pools_count))
1002 goto nla_put_failure;
1003 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
1004 devlink_sb->egress_pools_count))
1005 goto nla_put_failure;
1006 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
1007 devlink_sb->ingress_tc_count))
1008 goto nla_put_failure;
1009 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
1010 devlink_sb->egress_tc_count))
1011 goto nla_put_failure;
1013 genlmsg_end(msg, hdr);
1017 genlmsg_cancel(msg, hdr);
1021 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
1022 struct genl_info *info)
1024 struct devlink *devlink = info->user_ptr[0];
1025 struct devlink_sb *devlink_sb;
1026 struct sk_buff *msg;
1029 devlink_sb = devlink_sb_get_from_info(devlink, info);
1030 if (IS_ERR(devlink_sb))
1031 return PTR_ERR(devlink_sb);
1033 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1037 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1039 info->snd_portid, info->snd_seq, 0);
1045 return genlmsg_reply(msg, info);
1048 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
1049 struct netlink_callback *cb)
1051 struct devlink *devlink;
1052 struct devlink_sb *devlink_sb;
1053 int start = cb->args[0];
1057 mutex_lock(&devlink_mutex);
1058 list_for_each_entry(devlink, &devlink_list, list) {
1059 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
1061 mutex_lock(&devlink->lock);
1062 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1067 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
1069 NETLINK_CB(cb->skb).portid,
1073 mutex_unlock(&devlink->lock);
1078 mutex_unlock(&devlink->lock);
1081 mutex_unlock(&devlink_mutex);
1087 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
1088 struct devlink_sb *devlink_sb,
1089 u16 pool_index, enum devlink_command cmd,
1090 u32 portid, u32 seq, int flags)
1092 struct devlink_sb_pool_info pool_info;
1096 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
1097 pool_index, &pool_info);
1101 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1105 if (devlink_nl_put_handle(msg, devlink))
1106 goto nla_put_failure;
1107 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1108 goto nla_put_failure;
1109 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1110 goto nla_put_failure;
1111 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
1112 goto nla_put_failure;
1113 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
1114 goto nla_put_failure;
1115 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
1116 pool_info.threshold_type))
1117 goto nla_put_failure;
1118 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
1119 pool_info.cell_size))
1120 goto nla_put_failure;
1122 genlmsg_end(msg, hdr);
1126 genlmsg_cancel(msg, hdr);
1130 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
1131 struct genl_info *info)
1133 struct devlink *devlink = info->user_ptr[0];
1134 struct devlink_sb *devlink_sb;
1135 struct sk_buff *msg;
1139 devlink_sb = devlink_sb_get_from_info(devlink, info);
1140 if (IS_ERR(devlink_sb))
1141 return PTR_ERR(devlink_sb);
1143 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1148 if (!devlink->ops->sb_pool_get)
1151 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1155 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
1156 DEVLINK_CMD_SB_POOL_NEW,
1157 info->snd_portid, info->snd_seq, 0);
1163 return genlmsg_reply(msg, info);
1166 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1167 struct devlink *devlink,
1168 struct devlink_sb *devlink_sb,
1169 u32 portid, u32 seq)
1171 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1175 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1176 if (*p_idx < start) {
1180 err = devlink_nl_sb_pool_fill(msg, devlink,
1183 DEVLINK_CMD_SB_POOL_NEW,
1184 portid, seq, NLM_F_MULTI);
1192 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1193 struct netlink_callback *cb)
1195 struct devlink *devlink;
1196 struct devlink_sb *devlink_sb;
1197 int start = cb->args[0];
1201 mutex_lock(&devlink_mutex);
1202 list_for_each_entry(devlink, &devlink_list, list) {
1203 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1204 !devlink->ops->sb_pool_get)
1206 mutex_lock(&devlink->lock);
1207 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1208 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1210 NETLINK_CB(cb->skb).portid,
1211 cb->nlh->nlmsg_seq);
1212 if (err == -EOPNOTSUPP) {
1215 mutex_unlock(&devlink->lock);
1219 mutex_unlock(&devlink->lock);
1222 mutex_unlock(&devlink_mutex);
1224 if (err != -EMSGSIZE)
1231 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1232 u16 pool_index, u32 size,
1233 enum devlink_sb_threshold_type threshold_type,
1234 struct netlink_ext_ack *extack)
1237 const struct devlink_ops *ops = devlink->ops;
1239 if (ops->sb_pool_set)
1240 return ops->sb_pool_set(devlink, sb_index, pool_index,
1241 size, threshold_type, extack);
1245 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1246 struct genl_info *info)
1248 struct devlink *devlink = info->user_ptr[0];
1249 enum devlink_sb_threshold_type threshold_type;
1250 struct devlink_sb *devlink_sb;
1255 devlink_sb = devlink_sb_get_from_info(devlink, info);
1256 if (IS_ERR(devlink_sb))
1257 return PTR_ERR(devlink_sb);
1259 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1264 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1268 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1271 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1272 return devlink_sb_pool_set(devlink, devlink_sb->index,
1273 pool_index, size, threshold_type,
1277 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1278 struct devlink *devlink,
1279 struct devlink_port *devlink_port,
1280 struct devlink_sb *devlink_sb,
1282 enum devlink_command cmd,
1283 u32 portid, u32 seq, int flags)
1285 const struct devlink_ops *ops = devlink->ops;
1290 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1291 pool_index, &threshold);
1295 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1299 if (devlink_nl_put_handle(msg, devlink))
1300 goto nla_put_failure;
1301 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1302 goto nla_put_failure;
1303 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1304 goto nla_put_failure;
1305 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1306 goto nla_put_failure;
1307 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1308 goto nla_put_failure;
1310 if (ops->sb_occ_port_pool_get) {
1314 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1315 pool_index, &cur, &max);
1316 if (err && err != -EOPNOTSUPP)
1319 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1320 goto nla_put_failure;
1321 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1322 goto nla_put_failure;
1326 genlmsg_end(msg, hdr);
1330 genlmsg_cancel(msg, hdr);
1334 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1335 struct genl_info *info)
1337 struct devlink_port *devlink_port = info->user_ptr[1];
1338 struct devlink *devlink = devlink_port->devlink;
1339 struct devlink_sb *devlink_sb;
1340 struct sk_buff *msg;
1344 devlink_sb = devlink_sb_get_from_info(devlink, info);
1345 if (IS_ERR(devlink_sb))
1346 return PTR_ERR(devlink_sb);
1348 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1353 if (!devlink->ops->sb_port_pool_get)
1356 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1360 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1361 devlink_sb, pool_index,
1362 DEVLINK_CMD_SB_PORT_POOL_NEW,
1363 info->snd_portid, info->snd_seq, 0);
1369 return genlmsg_reply(msg, info);
1372 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1373 struct devlink *devlink,
1374 struct devlink_sb *devlink_sb,
1375 u32 portid, u32 seq)
1377 struct devlink_port *devlink_port;
1378 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1382 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1383 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1384 if (*p_idx < start) {
1388 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1392 DEVLINK_CMD_SB_PORT_POOL_NEW,
1403 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1404 struct netlink_callback *cb)
1406 struct devlink *devlink;
1407 struct devlink_sb *devlink_sb;
1408 int start = cb->args[0];
1412 mutex_lock(&devlink_mutex);
1413 list_for_each_entry(devlink, &devlink_list, list) {
1414 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1415 !devlink->ops->sb_port_pool_get)
1417 mutex_lock(&devlink->lock);
1418 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1419 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1420 devlink, devlink_sb,
1421 NETLINK_CB(cb->skb).portid,
1422 cb->nlh->nlmsg_seq);
1423 if (err == -EOPNOTSUPP) {
1426 mutex_unlock(&devlink->lock);
1430 mutex_unlock(&devlink->lock);
1433 mutex_unlock(&devlink_mutex);
1435 if (err != -EMSGSIZE)
1442 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1443 unsigned int sb_index, u16 pool_index,
1445 struct netlink_ext_ack *extack)
1448 const struct devlink_ops *ops = devlink_port->devlink->ops;
1450 if (ops->sb_port_pool_set)
1451 return ops->sb_port_pool_set(devlink_port, sb_index,
1452 pool_index, threshold, extack);
1456 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1457 struct genl_info *info)
1459 struct devlink_port *devlink_port = info->user_ptr[1];
1460 struct devlink *devlink = info->user_ptr[0];
1461 struct devlink_sb *devlink_sb;
1466 devlink_sb = devlink_sb_get_from_info(devlink, info);
1467 if (IS_ERR(devlink_sb))
1468 return PTR_ERR(devlink_sb);
1470 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1475 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1478 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1479 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1480 pool_index, threshold, info->extack);
1484 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1485 struct devlink_port *devlink_port,
1486 struct devlink_sb *devlink_sb, u16 tc_index,
1487 enum devlink_sb_pool_type pool_type,
1488 enum devlink_command cmd,
1489 u32 portid, u32 seq, int flags)
1491 const struct devlink_ops *ops = devlink->ops;
1497 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1498 tc_index, pool_type,
1499 &pool_index, &threshold);
1503 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1507 if (devlink_nl_put_handle(msg, devlink))
1508 goto nla_put_failure;
1509 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1510 goto nla_put_failure;
1511 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1512 goto nla_put_failure;
1513 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1514 goto nla_put_failure;
1515 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1516 goto nla_put_failure;
1517 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1518 goto nla_put_failure;
1519 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1520 goto nla_put_failure;
1522 if (ops->sb_occ_tc_port_bind_get) {
1526 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1528 tc_index, pool_type,
1530 if (err && err != -EOPNOTSUPP)
1533 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1534 goto nla_put_failure;
1535 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1536 goto nla_put_failure;
1540 genlmsg_end(msg, hdr);
1544 genlmsg_cancel(msg, hdr);
1548 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1549 struct genl_info *info)
1551 struct devlink_port *devlink_port = info->user_ptr[1];
1552 struct devlink *devlink = devlink_port->devlink;
1553 struct devlink_sb *devlink_sb;
1554 struct sk_buff *msg;
1555 enum devlink_sb_pool_type pool_type;
1559 devlink_sb = devlink_sb_get_from_info(devlink, info);
1560 if (IS_ERR(devlink_sb))
1561 return PTR_ERR(devlink_sb);
1563 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1567 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1568 pool_type, &tc_index);
1572 if (!devlink->ops->sb_tc_pool_bind_get)
1575 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1579 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1580 devlink_sb, tc_index, pool_type,
1581 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1589 return genlmsg_reply(msg, info);
1592 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1593 int start, int *p_idx,
1594 struct devlink *devlink,
1595 struct devlink_sb *devlink_sb,
1596 u32 portid, u32 seq)
1598 struct devlink_port *devlink_port;
1602 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1604 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1605 if (*p_idx < start) {
1609 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1613 DEVLINK_SB_POOL_TYPE_INGRESS,
1614 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1622 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1623 if (*p_idx < start) {
1627 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1631 DEVLINK_SB_POOL_TYPE_EGRESS,
1632 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1644 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1645 struct netlink_callback *cb)
1647 struct devlink *devlink;
1648 struct devlink_sb *devlink_sb;
1649 int start = cb->args[0];
1653 mutex_lock(&devlink_mutex);
1654 list_for_each_entry(devlink, &devlink_list, list) {
1655 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1656 !devlink->ops->sb_tc_pool_bind_get)
1659 mutex_lock(&devlink->lock);
1660 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1661 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1664 NETLINK_CB(cb->skb).portid,
1665 cb->nlh->nlmsg_seq);
1666 if (err == -EOPNOTSUPP) {
1669 mutex_unlock(&devlink->lock);
1673 mutex_unlock(&devlink->lock);
1676 mutex_unlock(&devlink_mutex);
1678 if (err != -EMSGSIZE)
1685 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1686 unsigned int sb_index, u16 tc_index,
1687 enum devlink_sb_pool_type pool_type,
1688 u16 pool_index, u32 threshold,
1689 struct netlink_ext_ack *extack)
1692 const struct devlink_ops *ops = devlink_port->devlink->ops;
1694 if (ops->sb_tc_pool_bind_set)
1695 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1696 tc_index, pool_type,
1697 pool_index, threshold, extack);
1701 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1702 struct genl_info *info)
1704 struct devlink_port *devlink_port = info->user_ptr[1];
1705 struct devlink *devlink = info->user_ptr[0];
1706 enum devlink_sb_pool_type pool_type;
1707 struct devlink_sb *devlink_sb;
1713 devlink_sb = devlink_sb_get_from_info(devlink, info);
1714 if (IS_ERR(devlink_sb))
1715 return PTR_ERR(devlink_sb);
1717 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1721 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1722 pool_type, &tc_index);
1726 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1731 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1734 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1735 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1736 tc_index, pool_type,
1737 pool_index, threshold, info->extack);
1740 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1741 struct genl_info *info)
1743 struct devlink *devlink = info->user_ptr[0];
1744 const struct devlink_ops *ops = devlink->ops;
1745 struct devlink_sb *devlink_sb;
1747 devlink_sb = devlink_sb_get_from_info(devlink, info);
1748 if (IS_ERR(devlink_sb))
1749 return PTR_ERR(devlink_sb);
1751 if (ops->sb_occ_snapshot)
1752 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1756 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1757 struct genl_info *info)
1759 struct devlink *devlink = info->user_ptr[0];
1760 const struct devlink_ops *ops = devlink->ops;
1761 struct devlink_sb *devlink_sb;
1763 devlink_sb = devlink_sb_get_from_info(devlink, info);
1764 if (IS_ERR(devlink_sb))
1765 return PTR_ERR(devlink_sb);
1767 if (ops->sb_occ_max_clear)
1768 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1772 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1773 enum devlink_command cmd, u32 portid,
1776 const struct devlink_ops *ops = devlink->ops;
1777 enum devlink_eswitch_encap_mode encap_mode;
1783 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1787 err = devlink_nl_put_handle(msg, devlink);
1789 goto nla_put_failure;
1791 if (ops->eswitch_mode_get) {
1792 err = ops->eswitch_mode_get(devlink, &mode);
1794 goto nla_put_failure;
1795 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1797 goto nla_put_failure;
1800 if (ops->eswitch_inline_mode_get) {
1801 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1803 goto nla_put_failure;
1804 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1807 goto nla_put_failure;
1810 if (ops->eswitch_encap_mode_get) {
1811 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1813 goto nla_put_failure;
1814 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1816 goto nla_put_failure;
1819 genlmsg_end(msg, hdr);
1823 genlmsg_cancel(msg, hdr);
1827 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1828 struct genl_info *info)
1830 struct devlink *devlink = info->user_ptr[0];
1831 struct sk_buff *msg;
1834 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1838 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1839 info->snd_portid, info->snd_seq, 0);
1846 return genlmsg_reply(msg, info);
1849 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1850 struct genl_info *info)
1852 struct devlink *devlink = info->user_ptr[0];
1853 const struct devlink_ops *ops = devlink->ops;
1854 enum devlink_eswitch_encap_mode encap_mode;
1859 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1860 if (!ops->eswitch_mode_set)
1862 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1863 err = ops->eswitch_mode_set(devlink, mode, info->extack);
1868 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1869 if (!ops->eswitch_inline_mode_set)
1871 inline_mode = nla_get_u8(
1872 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1873 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1879 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1880 if (!ops->eswitch_encap_mode_set)
1882 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1883 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1892 int devlink_dpipe_match_put(struct sk_buff *skb,
1893 struct devlink_dpipe_match *match)
1895 struct devlink_dpipe_header *header = match->header;
1896 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1897 struct nlattr *match_attr;
1899 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
1903 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1904 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1905 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1906 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1907 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1908 goto nla_put_failure;
1910 nla_nest_end(skb, match_attr);
1914 nla_nest_cancel(skb, match_attr);
1917 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1919 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1920 struct sk_buff *skb)
1922 struct nlattr *matches_attr;
1924 matches_attr = nla_nest_start_noflag(skb,
1925 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1929 if (table->table_ops->matches_dump(table->priv, skb))
1930 goto nla_put_failure;
1932 nla_nest_end(skb, matches_attr);
1936 nla_nest_cancel(skb, matches_attr);
1940 int devlink_dpipe_action_put(struct sk_buff *skb,
1941 struct devlink_dpipe_action *action)
1943 struct devlink_dpipe_header *header = action->header;
1944 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1945 struct nlattr *action_attr;
1947 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
1951 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1952 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1953 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1954 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1955 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1956 goto nla_put_failure;
1958 nla_nest_end(skb, action_attr);
1962 nla_nest_cancel(skb, action_attr);
1965 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1967 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1968 struct sk_buff *skb)
1970 struct nlattr *actions_attr;
1972 actions_attr = nla_nest_start_noflag(skb,
1973 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1977 if (table->table_ops->actions_dump(table->priv, skb))
1978 goto nla_put_failure;
1980 nla_nest_end(skb, actions_attr);
1984 nla_nest_cancel(skb, actions_attr);
1988 static int devlink_dpipe_table_put(struct sk_buff *skb,
1989 struct devlink_dpipe_table *table)
1991 struct nlattr *table_attr;
1994 table_size = table->table_ops->size_get(table->priv);
1995 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
1999 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
2000 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
2002 goto nla_put_failure;
2003 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
2004 table->counters_enabled))
2005 goto nla_put_failure;
2007 if (table->resource_valid) {
2008 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
2009 table->resource_id, DEVLINK_ATTR_PAD) ||
2010 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
2011 table->resource_units, DEVLINK_ATTR_PAD))
2012 goto nla_put_failure;
2014 if (devlink_dpipe_matches_put(table, skb))
2015 goto nla_put_failure;
2017 if (devlink_dpipe_actions_put(table, skb))
2018 goto nla_put_failure;
2020 nla_nest_end(skb, table_attr);
2024 nla_nest_cancel(skb, table_attr);
2028 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
2029 struct genl_info *info)
2034 err = genlmsg_reply(*pskb, info);
2038 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2044 static int devlink_dpipe_tables_fill(struct genl_info *info,
2045 enum devlink_command cmd, int flags,
2046 struct list_head *dpipe_tables,
2047 const char *table_name)
2049 struct devlink *devlink = info->user_ptr[0];
2050 struct devlink_dpipe_table *table;
2051 struct nlattr *tables_attr;
2052 struct sk_buff *skb = NULL;
2053 struct nlmsghdr *nlh;
2059 table = list_first_entry(dpipe_tables,
2060 struct devlink_dpipe_table, list);
2062 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2066 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2067 &devlink_nl_family, NLM_F_MULTI, cmd);
2073 if (devlink_nl_put_handle(skb, devlink))
2074 goto nla_put_failure;
2075 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
2077 goto nla_put_failure;
2081 list_for_each_entry_from(table, dpipe_tables, list) {
2083 err = devlink_dpipe_table_put(skb, table);
2091 if (!strcmp(table->name, table_name)) {
2092 err = devlink_dpipe_table_put(skb, table);
2100 nla_nest_end(skb, tables_attr);
2101 genlmsg_end(skb, hdr);
2106 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2107 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2109 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2115 return genlmsg_reply(skb, info);
2124 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
2125 struct genl_info *info)
2127 struct devlink *devlink = info->user_ptr[0];
2128 const char *table_name = NULL;
2130 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2131 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2133 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
2134 &devlink->dpipe_table_list,
2138 static int devlink_dpipe_value_put(struct sk_buff *skb,
2139 struct devlink_dpipe_value *value)
2141 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
2142 value->value_size, value->value))
2145 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
2146 value->value_size, value->mask))
2148 if (value->mapping_valid)
2149 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
2150 value->mapping_value))
2155 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
2156 struct devlink_dpipe_value *value)
2160 if (devlink_dpipe_action_put(skb, value->action))
2162 if (devlink_dpipe_value_put(skb, value))
2167 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
2168 struct devlink_dpipe_value *values,
2169 unsigned int values_count)
2171 struct nlattr *action_attr;
2175 for (i = 0; i < values_count; i++) {
2176 action_attr = nla_nest_start_noflag(skb,
2177 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
2180 err = devlink_dpipe_action_value_put(skb, &values[i]);
2182 goto err_action_value_put;
2183 nla_nest_end(skb, action_attr);
2187 err_action_value_put:
2188 nla_nest_cancel(skb, action_attr);
2192 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
2193 struct devlink_dpipe_value *value)
2197 if (devlink_dpipe_match_put(skb, value->match))
2199 if (devlink_dpipe_value_put(skb, value))
2204 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
2205 struct devlink_dpipe_value *values,
2206 unsigned int values_count)
2208 struct nlattr *match_attr;
2212 for (i = 0; i < values_count; i++) {
2213 match_attr = nla_nest_start_noflag(skb,
2214 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
2217 err = devlink_dpipe_match_value_put(skb, &values[i]);
2219 goto err_match_value_put;
2220 nla_nest_end(skb, match_attr);
2224 err_match_value_put:
2225 nla_nest_cancel(skb, match_attr);
2229 static int devlink_dpipe_entry_put(struct sk_buff *skb,
2230 struct devlink_dpipe_entry *entry)
2232 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2235 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2239 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2241 goto nla_put_failure;
2242 if (entry->counter_valid)
2243 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2244 entry->counter, DEVLINK_ATTR_PAD))
2245 goto nla_put_failure;
2247 matches_attr = nla_nest_start_noflag(skb,
2248 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2250 goto nla_put_failure;
2252 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2253 entry->match_values_count);
2255 nla_nest_cancel(skb, matches_attr);
2256 goto err_match_values_put;
2258 nla_nest_end(skb, matches_attr);
2260 actions_attr = nla_nest_start_noflag(skb,
2261 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2263 goto nla_put_failure;
2265 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2266 entry->action_values_count);
2268 nla_nest_cancel(skb, actions_attr);
2269 goto err_action_values_put;
2271 nla_nest_end(skb, actions_attr);
2273 nla_nest_end(skb, entry_attr);
2278 err_match_values_put:
2279 err_action_values_put:
2280 nla_nest_cancel(skb, entry_attr);
2284 static struct devlink_dpipe_table *
2285 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2286 const char *table_name, struct devlink *devlink)
2288 struct devlink_dpipe_table *table;
2289 list_for_each_entry_rcu(table, dpipe_tables, list,
2290 lockdep_is_held(&devlink->lock)) {
2291 if (!strcmp(table->name, table_name))
2297 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2299 struct devlink *devlink;
2302 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2307 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2308 dump_ctx->info->snd_portid,
2309 dump_ctx->info->snd_seq,
2310 &devlink_nl_family, NLM_F_MULTI,
2313 goto nla_put_failure;
2315 devlink = dump_ctx->info->user_ptr[0];
2316 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2317 goto nla_put_failure;
2318 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2319 DEVLINK_ATTR_DPIPE_ENTRIES);
2320 if (!dump_ctx->nest)
2321 goto nla_put_failure;
2325 nlmsg_free(dump_ctx->skb);
2328 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2330 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2331 struct devlink_dpipe_entry *entry)
2333 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2335 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2337 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2339 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2340 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2343 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2345 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2348 unsigned int value_count, value_index;
2349 struct devlink_dpipe_value *value;
2351 value = entry->action_values;
2352 value_count = entry->action_values_count;
2353 for (value_index = 0; value_index < value_count; value_index++) {
2354 kfree(value[value_index].value);
2355 kfree(value[value_index].mask);
2358 value = entry->match_values;
2359 value_count = entry->match_values_count;
2360 for (value_index = 0; value_index < value_count; value_index++) {
2361 kfree(value[value_index].value);
2362 kfree(value[value_index].mask);
2365 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2367 static int devlink_dpipe_entries_fill(struct genl_info *info,
2368 enum devlink_command cmd, int flags,
2369 struct devlink_dpipe_table *table)
2371 struct devlink_dpipe_dump_ctx dump_ctx;
2372 struct nlmsghdr *nlh;
2375 dump_ctx.skb = NULL;
2377 dump_ctx.info = info;
2379 err = table->table_ops->entries_dump(table->priv,
2380 table->counters_enabled,
2386 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2387 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2389 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2394 return genlmsg_reply(dump_ctx.skb, info);
2397 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2398 struct genl_info *info)
2400 struct devlink *devlink = info->user_ptr[0];
2401 struct devlink_dpipe_table *table;
2402 const char *table_name;
2404 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2407 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2408 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2409 table_name, devlink);
2413 if (!table->table_ops->entries_dump)
2416 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2420 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2421 const struct devlink_dpipe_header *header)
2423 struct devlink_dpipe_field *field;
2424 struct nlattr *field_attr;
2427 for (i = 0; i < header->fields_count; i++) {
2428 field = &header->fields[i];
2429 field_attr = nla_nest_start_noflag(skb,
2430 DEVLINK_ATTR_DPIPE_FIELD);
2433 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2434 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2435 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2436 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2437 goto nla_put_failure;
2438 nla_nest_end(skb, field_attr);
2443 nla_nest_cancel(skb, field_attr);
2447 static int devlink_dpipe_header_put(struct sk_buff *skb,
2448 struct devlink_dpipe_header *header)
2450 struct nlattr *fields_attr, *header_attr;
2453 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
2457 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2458 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2459 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2460 goto nla_put_failure;
2462 fields_attr = nla_nest_start_noflag(skb,
2463 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2465 goto nla_put_failure;
2467 err = devlink_dpipe_fields_put(skb, header);
2469 nla_nest_cancel(skb, fields_attr);
2470 goto nla_put_failure;
2472 nla_nest_end(skb, fields_attr);
2473 nla_nest_end(skb, header_attr);
2478 nla_nest_cancel(skb, header_attr);
2482 static int devlink_dpipe_headers_fill(struct genl_info *info,
2483 enum devlink_command cmd, int flags,
2484 struct devlink_dpipe_headers *
2487 struct devlink *devlink = info->user_ptr[0];
2488 struct nlattr *headers_attr;
2489 struct sk_buff *skb = NULL;
2490 struct nlmsghdr *nlh;
2497 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2501 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2502 &devlink_nl_family, NLM_F_MULTI, cmd);
2508 if (devlink_nl_put_handle(skb, devlink))
2509 goto nla_put_failure;
2510 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2512 goto nla_put_failure;
2515 for (; i < dpipe_headers->headers_count; i++) {
2516 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2524 nla_nest_end(skb, headers_attr);
2525 genlmsg_end(skb, hdr);
2526 if (i != dpipe_headers->headers_count)
2530 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2531 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2533 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2538 return genlmsg_reply(skb, info);
2547 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2548 struct genl_info *info)
2550 struct devlink *devlink = info->user_ptr[0];
2552 if (!devlink->dpipe_headers)
2554 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2555 0, devlink->dpipe_headers);
2558 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2559 const char *table_name,
2562 struct devlink_dpipe_table *table;
2564 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2565 table_name, devlink);
2569 if (table->counter_control_extern)
2572 if (!(table->counters_enabled ^ enable))
2575 table->counters_enabled = enable;
2576 if (table->table_ops->counters_set_update)
2577 table->table_ops->counters_set_update(table->priv, enable);
2581 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2582 struct genl_info *info)
2584 struct devlink *devlink = info->user_ptr[0];
2585 const char *table_name;
2586 bool counters_enable;
2588 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2589 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2592 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2593 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2595 return devlink_dpipe_table_counters_set(devlink, table_name,
2599 static struct devlink_resource *
2600 devlink_resource_find(struct devlink *devlink,
2601 struct devlink_resource *resource, u64 resource_id)
2603 struct list_head *resource_list;
2606 resource_list = &resource->resource_list;
2608 resource_list = &devlink->resource_list;
2610 list_for_each_entry(resource, resource_list, list) {
2611 struct devlink_resource *child_resource;
2613 if (resource->id == resource_id)
2616 child_resource = devlink_resource_find(devlink, resource,
2619 return child_resource;
2625 devlink_resource_validate_children(struct devlink_resource *resource)
2627 struct devlink_resource *child_resource;
2628 bool size_valid = true;
2631 if (list_empty(&resource->resource_list))
2634 list_for_each_entry(child_resource, &resource->resource_list, list)
2635 parts_size += child_resource->size_new;
2637 if (parts_size > resource->size_new)
2640 resource->size_valid = size_valid;
2644 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2645 struct netlink_ext_ack *extack)
2650 if (size > resource->size_params.size_max) {
2651 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2655 if (size < resource->size_params.size_min) {
2656 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2660 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2662 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2669 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2670 struct genl_info *info)
2672 struct devlink *devlink = info->user_ptr[0];
2673 struct devlink_resource *resource;
2678 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2679 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2681 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2683 resource = devlink_resource_find(devlink, NULL, resource_id);
2687 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2688 err = devlink_resource_validate_size(resource, size, info->extack);
2692 resource->size_new = size;
2693 devlink_resource_validate_children(resource);
2694 if (resource->parent)
2695 devlink_resource_validate_children(resource->parent);
2700 devlink_resource_size_params_put(struct devlink_resource *resource,
2701 struct sk_buff *skb)
2703 struct devlink_resource_size_params *size_params;
2705 size_params = &resource->size_params;
2706 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2707 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2708 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2709 size_params->size_max, DEVLINK_ATTR_PAD) ||
2710 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2711 size_params->size_min, DEVLINK_ATTR_PAD) ||
2712 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2717 static int devlink_resource_occ_put(struct devlink_resource *resource,
2718 struct sk_buff *skb)
2720 if (!resource->occ_get)
2722 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2723 resource->occ_get(resource->occ_get_priv),
2727 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2728 struct devlink_resource *resource)
2730 struct devlink_resource *child_resource;
2731 struct nlattr *child_resource_attr;
2732 struct nlattr *resource_attr;
2734 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
2738 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2739 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2740 DEVLINK_ATTR_PAD) ||
2741 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2743 goto nla_put_failure;
2744 if (resource->size != resource->size_new)
2745 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2746 resource->size_new, DEVLINK_ATTR_PAD);
2747 if (devlink_resource_occ_put(resource, skb))
2748 goto nla_put_failure;
2749 if (devlink_resource_size_params_put(resource, skb))
2750 goto nla_put_failure;
2751 if (list_empty(&resource->resource_list))
2754 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2755 resource->size_valid))
2756 goto nla_put_failure;
2758 child_resource_attr = nla_nest_start_noflag(skb,
2759 DEVLINK_ATTR_RESOURCE_LIST);
2760 if (!child_resource_attr)
2761 goto nla_put_failure;
2763 list_for_each_entry(child_resource, &resource->resource_list, list) {
2764 if (devlink_resource_put(devlink, skb, child_resource))
2765 goto resource_put_failure;
2768 nla_nest_end(skb, child_resource_attr);
2770 nla_nest_end(skb, resource_attr);
2773 resource_put_failure:
2774 nla_nest_cancel(skb, child_resource_attr);
2776 nla_nest_cancel(skb, resource_attr);
2780 static int devlink_resource_fill(struct genl_info *info,
2781 enum devlink_command cmd, int flags)
2783 struct devlink *devlink = info->user_ptr[0];
2784 struct devlink_resource *resource;
2785 struct nlattr *resources_attr;
2786 struct sk_buff *skb = NULL;
2787 struct nlmsghdr *nlh;
2793 resource = list_first_entry(&devlink->resource_list,
2794 struct devlink_resource, list);
2796 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2800 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2801 &devlink_nl_family, NLM_F_MULTI, cmd);
2807 if (devlink_nl_put_handle(skb, devlink))
2808 goto nla_put_failure;
2810 resources_attr = nla_nest_start_noflag(skb,
2811 DEVLINK_ATTR_RESOURCE_LIST);
2812 if (!resources_attr)
2813 goto nla_put_failure;
2817 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2818 err = devlink_resource_put(devlink, skb, resource);
2821 goto err_resource_put;
2827 nla_nest_end(skb, resources_attr);
2828 genlmsg_end(skb, hdr);
2832 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2833 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2835 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2840 return genlmsg_reply(skb, info);
2849 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2850 struct genl_info *info)
2852 struct devlink *devlink = info->user_ptr[0];
2854 if (list_empty(&devlink->resource_list))
2857 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2861 devlink_resources_validate(struct devlink *devlink,
2862 struct devlink_resource *resource,
2863 struct genl_info *info)
2865 struct list_head *resource_list;
2869 resource_list = &resource->resource_list;
2871 resource_list = &devlink->resource_list;
2873 list_for_each_entry(resource, resource_list, list) {
2874 if (!resource->size_valid)
2876 err = devlink_resources_validate(devlink, resource, info);
2883 static struct net *devlink_netns_get(struct sk_buff *skb,
2884 struct genl_info *info)
2886 struct nlattr *netns_pid_attr = info->attrs[DEVLINK_ATTR_NETNS_PID];
2887 struct nlattr *netns_fd_attr = info->attrs[DEVLINK_ATTR_NETNS_FD];
2888 struct nlattr *netns_id_attr = info->attrs[DEVLINK_ATTR_NETNS_ID];
2891 if (!!netns_pid_attr + !!netns_fd_attr + !!netns_id_attr > 1) {
2892 NL_SET_ERR_MSG_MOD(info->extack, "multiple netns identifying attributes specified");
2893 return ERR_PTR(-EINVAL);
2896 if (netns_pid_attr) {
2897 net = get_net_ns_by_pid(nla_get_u32(netns_pid_attr));
2898 } else if (netns_fd_attr) {
2899 net = get_net_ns_by_fd(nla_get_u32(netns_fd_attr));
2900 } else if (netns_id_attr) {
2901 net = get_net_ns_by_id(sock_net(skb->sk),
2902 nla_get_u32(netns_id_attr));
2904 net = ERR_PTR(-EINVAL);
2907 net = ERR_PTR(-EINVAL);
2910 NL_SET_ERR_MSG_MOD(info->extack, "Unknown network namespace");
2911 return ERR_PTR(-EINVAL);
2913 if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
2915 return ERR_PTR(-EPERM);
2920 static void devlink_param_notify(struct devlink *devlink,
2921 unsigned int port_index,
2922 struct devlink_param_item *param_item,
2923 enum devlink_command cmd);
2925 static void devlink_reload_netns_change(struct devlink *devlink,
2926 struct net *dest_net)
2928 struct devlink_param_item *param_item;
2930 /* Userspace needs to be notified about devlink objects
2931 * removed from original and entering new network namespace.
2932 * The rest of the devlink objects are re-created during
2933 * reload process so the notifications are generated separatelly.
2936 list_for_each_entry(param_item, &devlink->param_list, list)
2937 devlink_param_notify(devlink, 0, param_item,
2938 DEVLINK_CMD_PARAM_DEL);
2939 devlink_notify(devlink, DEVLINK_CMD_DEL);
2941 __devlink_net_set(devlink, dest_net);
2943 devlink_notify(devlink, DEVLINK_CMD_NEW);
2944 list_for_each_entry(param_item, &devlink->param_list, list)
2945 devlink_param_notify(devlink, 0, param_item,
2946 DEVLINK_CMD_PARAM_NEW);
2949 static bool devlink_reload_supported(const struct devlink *devlink)
2951 return devlink->ops->reload_down && devlink->ops->reload_up;
2954 static void devlink_reload_failed_set(struct devlink *devlink,
2957 if (devlink->reload_failed == reload_failed)
2959 devlink->reload_failed = reload_failed;
2960 devlink_notify(devlink, DEVLINK_CMD_NEW);
2963 bool devlink_is_reload_failed(const struct devlink *devlink)
2965 return devlink->reload_failed;
2967 EXPORT_SYMBOL_GPL(devlink_is_reload_failed);
2969 static int devlink_reload(struct devlink *devlink, struct net *dest_net,
2970 struct netlink_ext_ack *extack)
2974 if (!devlink->reload_enabled)
2977 err = devlink->ops->reload_down(devlink, !!dest_net, extack);
2981 if (dest_net && !net_eq(dest_net, devlink_net(devlink)))
2982 devlink_reload_netns_change(devlink, dest_net);
2984 err = devlink->ops->reload_up(devlink, extack);
2985 devlink_reload_failed_set(devlink, !!err);
2989 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2991 struct devlink *devlink = info->user_ptr[0];
2992 struct net *dest_net = NULL;
2995 if (!devlink_reload_supported(devlink))
2998 err = devlink_resources_validate(devlink, NULL, info);
3000 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
3004 if (info->attrs[DEVLINK_ATTR_NETNS_PID] ||
3005 info->attrs[DEVLINK_ATTR_NETNS_FD] ||
3006 info->attrs[DEVLINK_ATTR_NETNS_ID]) {
3007 dest_net = devlink_netns_get(skb, info);
3008 if (IS_ERR(dest_net))
3009 return PTR_ERR(dest_net);
3012 err = devlink_reload(devlink, dest_net, info->extack);
3020 static int devlink_nl_flash_update_fill(struct sk_buff *msg,
3021 struct devlink *devlink,
3022 enum devlink_command cmd,
3023 struct devlink_flash_notify *params)
3027 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3031 if (devlink_nl_put_handle(msg, devlink))
3032 goto nla_put_failure;
3034 if (cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS)
3037 if (params->status_msg &&
3038 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_MSG,
3039 params->status_msg))
3040 goto nla_put_failure;
3041 if (params->component &&
3042 nla_put_string(msg, DEVLINK_ATTR_FLASH_UPDATE_COMPONENT,
3044 goto nla_put_failure;
3045 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_DONE,
3046 params->done, DEVLINK_ATTR_PAD))
3047 goto nla_put_failure;
3048 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TOTAL,
3049 params->total, DEVLINK_ATTR_PAD))
3050 goto nla_put_failure;
3051 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_FLASH_UPDATE_STATUS_TIMEOUT,
3052 params->timeout, DEVLINK_ATTR_PAD))
3053 goto nla_put_failure;
3056 genlmsg_end(msg, hdr);
3060 genlmsg_cancel(msg, hdr);
3064 static void __devlink_flash_update_notify(struct devlink *devlink,
3065 enum devlink_command cmd,
3066 struct devlink_flash_notify *params)
3068 struct sk_buff *msg;
3071 WARN_ON(cmd != DEVLINK_CMD_FLASH_UPDATE &&
3072 cmd != DEVLINK_CMD_FLASH_UPDATE_END &&
3073 cmd != DEVLINK_CMD_FLASH_UPDATE_STATUS);
3075 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3079 err = devlink_nl_flash_update_fill(msg, devlink, cmd, params);
3083 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3084 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3091 void devlink_flash_update_begin_notify(struct devlink *devlink)
3093 struct devlink_flash_notify params = { 0 };
3095 __devlink_flash_update_notify(devlink,
3096 DEVLINK_CMD_FLASH_UPDATE,
3099 EXPORT_SYMBOL_GPL(devlink_flash_update_begin_notify);
3101 void devlink_flash_update_end_notify(struct devlink *devlink)
3103 struct devlink_flash_notify params = { 0 };
3105 __devlink_flash_update_notify(devlink,
3106 DEVLINK_CMD_FLASH_UPDATE_END,
3109 EXPORT_SYMBOL_GPL(devlink_flash_update_end_notify);
3111 void devlink_flash_update_status_notify(struct devlink *devlink,
3112 const char *status_msg,
3113 const char *component,
3115 unsigned long total)
3117 struct devlink_flash_notify params = {
3118 .status_msg = status_msg,
3119 .component = component,
3124 __devlink_flash_update_notify(devlink,
3125 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3128 EXPORT_SYMBOL_GPL(devlink_flash_update_status_notify);
3130 void devlink_flash_update_timeout_notify(struct devlink *devlink,
3131 const char *status_msg,
3132 const char *component,
3133 unsigned long timeout)
3135 struct devlink_flash_notify params = {
3136 .status_msg = status_msg,
3137 .component = component,
3141 __devlink_flash_update_notify(devlink,
3142 DEVLINK_CMD_FLASH_UPDATE_STATUS,
3145 EXPORT_SYMBOL_GPL(devlink_flash_update_timeout_notify);
3147 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
3148 struct genl_info *info)
3150 struct nlattr *nla_component, *nla_overwrite_mask;
3151 struct devlink_flash_update_params params = {};
3152 struct devlink *devlink = info->user_ptr[0];
3153 u32 supported_params;
3155 if (!devlink->ops->flash_update)
3158 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
3161 supported_params = devlink->ops->supported_flash_update_params;
3163 params.file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
3165 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
3166 if (nla_component) {
3167 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_COMPONENT)) {
3168 NL_SET_ERR_MSG_ATTR(info->extack, nla_component,
3169 "component update is not supported by this device");
3172 params.component = nla_data(nla_component);
3175 nla_overwrite_mask = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK];
3176 if (nla_overwrite_mask) {
3177 struct nla_bitfield32 sections;
3179 if (!(supported_params & DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK)) {
3180 NL_SET_ERR_MSG_ATTR(info->extack, nla_overwrite_mask,
3181 "overwrite settings are not supported by this device");
3184 sections = nla_get_bitfield32(nla_overwrite_mask);
3185 params.overwrite_mask = sections.value & sections.selector;
3188 return devlink->ops->flash_update(devlink, ¶ms, info->extack);
3191 static const struct devlink_param devlink_param_generic[] = {
3193 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
3194 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
3195 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
3198 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
3199 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
3200 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
3203 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
3204 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
3205 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
3208 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
3209 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
3210 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
3213 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
3214 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
3215 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
3218 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
3219 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
3220 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
3223 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
3224 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
3225 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
3228 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
3229 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
3230 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
3233 .id = DEVLINK_PARAM_GENERIC_ID_RESET_DEV_ON_DRV_PROBE,
3234 .name = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_NAME,
3235 .type = DEVLINK_PARAM_GENERIC_RESET_DEV_ON_DRV_PROBE_TYPE,
3238 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
3239 .name = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_NAME,
3240 .type = DEVLINK_PARAM_GENERIC_ENABLE_ROCE_TYPE,
3244 static int devlink_param_generic_verify(const struct devlink_param *param)
3246 /* verify it match generic parameter by id and name */
3247 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
3249 if (strcmp(param->name, devlink_param_generic[param->id].name))
3252 WARN_ON(param->type != devlink_param_generic[param->id].type);
3257 static int devlink_param_driver_verify(const struct devlink_param *param)
3261 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
3263 /* verify no such name in generic params */
3264 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
3265 if (!strcmp(param->name, devlink_param_generic[i].name))
3271 static struct devlink_param_item *
3272 devlink_param_find_by_name(struct list_head *param_list,
3273 const char *param_name)
3275 struct devlink_param_item *param_item;
3277 list_for_each_entry(param_item, param_list, list)
3278 if (!strcmp(param_item->param->name, param_name))
3283 static struct devlink_param_item *
3284 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
3286 struct devlink_param_item *param_item;
3288 list_for_each_entry(param_item, param_list, list)
3289 if (param_item->param->id == param_id)
3295 devlink_param_cmode_is_supported(const struct devlink_param *param,
3296 enum devlink_param_cmode cmode)
3298 return test_bit(cmode, ¶m->supported_cmodes);
3301 static int devlink_param_get(struct devlink *devlink,
3302 const struct devlink_param *param,
3303 struct devlink_param_gset_ctx *ctx)
3307 return param->get(devlink, param->id, ctx);
3310 static int devlink_param_set(struct devlink *devlink,
3311 const struct devlink_param *param,
3312 struct devlink_param_gset_ctx *ctx)
3316 return param->set(devlink, param->id, ctx);
3320 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
3322 switch (param_type) {
3323 case DEVLINK_PARAM_TYPE_U8:
3325 case DEVLINK_PARAM_TYPE_U16:
3327 case DEVLINK_PARAM_TYPE_U32:
3329 case DEVLINK_PARAM_TYPE_STRING:
3331 case DEVLINK_PARAM_TYPE_BOOL:
3339 devlink_nl_param_value_fill_one(struct sk_buff *msg,
3340 enum devlink_param_type type,
3341 enum devlink_param_cmode cmode,
3342 union devlink_param_value val)
3344 struct nlattr *param_value_attr;
3346 param_value_attr = nla_nest_start_noflag(msg,
3347 DEVLINK_ATTR_PARAM_VALUE);
3348 if (!param_value_attr)
3349 goto nla_put_failure;
3351 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
3352 goto value_nest_cancel;
3355 case DEVLINK_PARAM_TYPE_U8:
3356 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
3357 goto value_nest_cancel;
3359 case DEVLINK_PARAM_TYPE_U16:
3360 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
3361 goto value_nest_cancel;
3363 case DEVLINK_PARAM_TYPE_U32:
3364 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
3365 goto value_nest_cancel;
3367 case DEVLINK_PARAM_TYPE_STRING:
3368 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
3370 goto value_nest_cancel;
3372 case DEVLINK_PARAM_TYPE_BOOL:
3374 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
3375 goto value_nest_cancel;
3379 nla_nest_end(msg, param_value_attr);
3383 nla_nest_cancel(msg, param_value_attr);
3388 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
3389 unsigned int port_index,
3390 struct devlink_param_item *param_item,
3391 enum devlink_command cmd,
3392 u32 portid, u32 seq, int flags)
3394 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
3395 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
3396 const struct devlink_param *param = param_item->param;
3397 struct devlink_param_gset_ctx ctx;
3398 struct nlattr *param_values_list;
3399 struct nlattr *param_attr;
3405 /* Get value from driver part to driverinit configuration mode */
3406 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3407 if (!devlink_param_cmode_is_supported(param, i))
3409 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3410 if (!param_item->driverinit_value_valid)
3412 param_value[i] = param_item->driverinit_value;
3414 if (!param_item->published)
3417 err = devlink_param_get(devlink, param, &ctx);
3420 param_value[i] = ctx.val;
3422 param_value_set[i] = true;
3425 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3429 if (devlink_nl_put_handle(msg, devlink))
3430 goto genlmsg_cancel;
3432 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
3433 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
3434 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
3435 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
3436 goto genlmsg_cancel;
3438 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
3440 goto genlmsg_cancel;
3441 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
3442 goto param_nest_cancel;
3443 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
3444 goto param_nest_cancel;
3446 nla_type = devlink_param_type_to_nla_type(param->type);
3448 goto param_nest_cancel;
3449 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
3450 goto param_nest_cancel;
3452 param_values_list = nla_nest_start_noflag(msg,
3453 DEVLINK_ATTR_PARAM_VALUES_LIST);
3454 if (!param_values_list)
3455 goto param_nest_cancel;
3457 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
3458 if (!param_value_set[i])
3460 err = devlink_nl_param_value_fill_one(msg, param->type,
3463 goto values_list_nest_cancel;
3466 nla_nest_end(msg, param_values_list);
3467 nla_nest_end(msg, param_attr);
3468 genlmsg_end(msg, hdr);
3471 values_list_nest_cancel:
3472 nla_nest_end(msg, param_values_list);
3474 nla_nest_cancel(msg, param_attr);
3476 genlmsg_cancel(msg, hdr);
3480 static void devlink_param_notify(struct devlink *devlink,
3481 unsigned int port_index,
3482 struct devlink_param_item *param_item,
3483 enum devlink_command cmd)
3485 struct sk_buff *msg;
3488 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
3489 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
3490 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
3492 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3495 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
3502 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3503 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3506 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
3507 struct netlink_callback *cb)
3509 struct devlink_param_item *param_item;
3510 struct devlink *devlink;
3511 int start = cb->args[0];
3515 mutex_lock(&devlink_mutex);
3516 list_for_each_entry(devlink, &devlink_list, list) {
3517 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3519 mutex_lock(&devlink->lock);
3520 list_for_each_entry(param_item, &devlink->param_list, list) {
3525 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3526 DEVLINK_CMD_PARAM_GET,
3527 NETLINK_CB(cb->skb).portid,
3530 if (err == -EOPNOTSUPP) {
3533 mutex_unlock(&devlink->lock);
3538 mutex_unlock(&devlink->lock);
3541 mutex_unlock(&devlink_mutex);
3543 if (err != -EMSGSIZE)
3551 devlink_param_type_get_from_info(struct genl_info *info,
3552 enum devlink_param_type *param_type)
3554 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3557 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3559 *param_type = DEVLINK_PARAM_TYPE_U8;
3562 *param_type = DEVLINK_PARAM_TYPE_U16;
3565 *param_type = DEVLINK_PARAM_TYPE_U32;
3568 *param_type = DEVLINK_PARAM_TYPE_STRING;
3571 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3581 devlink_param_value_get_from_info(const struct devlink_param *param,
3582 struct genl_info *info,
3583 union devlink_param_value *value)
3585 struct nlattr *param_data;
3588 param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
3590 if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
3593 switch (param->type) {
3594 case DEVLINK_PARAM_TYPE_U8:
3595 if (nla_len(param_data) != sizeof(u8))
3597 value->vu8 = nla_get_u8(param_data);
3599 case DEVLINK_PARAM_TYPE_U16:
3600 if (nla_len(param_data) != sizeof(u16))
3602 value->vu16 = nla_get_u16(param_data);
3604 case DEVLINK_PARAM_TYPE_U32:
3605 if (nla_len(param_data) != sizeof(u32))
3607 value->vu32 = nla_get_u32(param_data);
3609 case DEVLINK_PARAM_TYPE_STRING:
3610 len = strnlen(nla_data(param_data), nla_len(param_data));
3611 if (len == nla_len(param_data) ||
3612 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3614 strcpy(value->vstr, nla_data(param_data));
3616 case DEVLINK_PARAM_TYPE_BOOL:
3617 if (param_data && nla_len(param_data))
3619 value->vbool = nla_get_flag(param_data);
3625 static struct devlink_param_item *
3626 devlink_param_get_from_info(struct list_head *param_list,
3627 struct genl_info *info)
3631 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3634 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3635 return devlink_param_find_by_name(param_list, param_name);
3638 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3639 struct genl_info *info)
3641 struct devlink *devlink = info->user_ptr[0];
3642 struct devlink_param_item *param_item;
3643 struct sk_buff *msg;
3646 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3650 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3654 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3655 DEVLINK_CMD_PARAM_GET,
3656 info->snd_portid, info->snd_seq, 0);
3662 return genlmsg_reply(msg, info);
3665 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3666 unsigned int port_index,
3667 struct list_head *param_list,
3668 struct genl_info *info,
3669 enum devlink_command cmd)
3671 enum devlink_param_type param_type;
3672 struct devlink_param_gset_ctx ctx;
3673 enum devlink_param_cmode cmode;
3674 struct devlink_param_item *param_item;
3675 const struct devlink_param *param;
3676 union devlink_param_value value;
3679 param_item = devlink_param_get_from_info(param_list, info);
3682 param = param_item->param;
3683 err = devlink_param_type_get_from_info(info, ¶m_type);
3686 if (param_type != param->type)
3688 err = devlink_param_value_get_from_info(param, info, &value);
3691 if (param->validate) {
3692 err = param->validate(devlink, param->id, value, info->extack);
3697 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3699 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3700 if (!devlink_param_cmode_is_supported(param, cmode))
3703 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3704 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3705 strcpy(param_item->driverinit_value.vstr, value.vstr);
3707 param_item->driverinit_value = value;
3708 param_item->driverinit_value_valid = true;
3714 err = devlink_param_set(devlink, param, &ctx);
3719 devlink_param_notify(devlink, port_index, param_item, cmd);
3723 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3724 struct genl_info *info)
3726 struct devlink *devlink = info->user_ptr[0];
3728 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
3729 info, DEVLINK_CMD_PARAM_NEW);
3732 static int devlink_param_register_one(struct devlink *devlink,
3733 unsigned int port_index,
3734 struct list_head *param_list,
3735 const struct devlink_param *param,
3736 enum devlink_command cmd)
3738 struct devlink_param_item *param_item;
3740 if (devlink_param_find_by_name(param_list, param->name))
3743 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3744 WARN_ON(param->get || param->set);
3746 WARN_ON(!param->get || !param->set);
3748 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3751 param_item->param = param;
3753 list_add_tail(¶m_item->list, param_list);
3754 devlink_param_notify(devlink, port_index, param_item, cmd);
3758 static void devlink_param_unregister_one(struct devlink *devlink,
3759 unsigned int port_index,
3760 struct list_head *param_list,
3761 const struct devlink_param *param,
3762 enum devlink_command cmd)
3764 struct devlink_param_item *param_item;
3766 param_item = devlink_param_find_by_name(param_list, param->name);
3767 WARN_ON(!param_item);
3768 devlink_param_notify(devlink, port_index, param_item, cmd);
3769 list_del(¶m_item->list);
3773 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3774 struct netlink_callback *cb)
3776 struct devlink_param_item *param_item;
3777 struct devlink_port *devlink_port;
3778 struct devlink *devlink;
3779 int start = cb->args[0];
3783 mutex_lock(&devlink_mutex);
3784 list_for_each_entry(devlink, &devlink_list, list) {
3785 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3787 mutex_lock(&devlink->lock);
3788 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3789 list_for_each_entry(param_item,
3790 &devlink_port->param_list, list) {
3795 err = devlink_nl_param_fill(msg,
3796 devlink_port->devlink,
3797 devlink_port->index, param_item,
3798 DEVLINK_CMD_PORT_PARAM_GET,
3799 NETLINK_CB(cb->skb).portid,
3802 if (err == -EOPNOTSUPP) {
3805 mutex_unlock(&devlink->lock);
3811 mutex_unlock(&devlink->lock);
3814 mutex_unlock(&devlink_mutex);
3816 if (err != -EMSGSIZE)
3823 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3824 struct genl_info *info)
3826 struct devlink_port *devlink_port = info->user_ptr[0];
3827 struct devlink_param_item *param_item;
3828 struct sk_buff *msg;
3831 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3836 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3840 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3841 devlink_port->index, param_item,
3842 DEVLINK_CMD_PORT_PARAM_GET,
3843 info->snd_portid, info->snd_seq, 0);
3849 return genlmsg_reply(msg, info);
3852 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3853 struct genl_info *info)
3855 struct devlink_port *devlink_port = info->user_ptr[0];
3857 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
3858 devlink_port->index,
3859 &devlink_port->param_list, info,
3860 DEVLINK_CMD_PORT_PARAM_NEW);
3863 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3864 struct devlink *devlink,
3865 struct devlink_snapshot *snapshot)
3867 struct nlattr *snap_attr;
3870 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3874 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3876 goto nla_put_failure;
3878 nla_nest_end(msg, snap_attr);
3882 nla_nest_cancel(msg, snap_attr);
3886 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3887 struct devlink *devlink,
3888 struct devlink_region *region)
3890 struct devlink_snapshot *snapshot;
3891 struct nlattr *snapshots_attr;
3894 snapshots_attr = nla_nest_start_noflag(msg,
3895 DEVLINK_ATTR_REGION_SNAPSHOTS);
3896 if (!snapshots_attr)
3899 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
3900 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3902 goto nla_put_failure;
3905 nla_nest_end(msg, snapshots_attr);
3909 nla_nest_cancel(msg, snapshots_attr);
3913 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3914 enum devlink_command cmd, u32 portid,
3916 struct devlink_region *region)
3921 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3925 err = devlink_nl_put_handle(msg, devlink);
3927 goto nla_put_failure;
3929 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->ops->name);
3931 goto nla_put_failure;
3933 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3937 goto nla_put_failure;
3939 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3941 goto nla_put_failure;
3943 genlmsg_end(msg, hdr);
3947 genlmsg_cancel(msg, hdr);
3951 static struct sk_buff *
3952 devlink_nl_region_notify_build(struct devlink_region *region,
3953 struct devlink_snapshot *snapshot,
3954 enum devlink_command cmd, u32 portid, u32 seq)
3956 struct devlink *devlink = region->devlink;
3957 struct sk_buff *msg;
3962 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3964 return ERR_PTR(-ENOMEM);
3966 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, 0, cmd);
3972 err = devlink_nl_put_handle(msg, devlink);
3974 goto out_cancel_msg;
3976 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3979 goto out_cancel_msg;
3982 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3985 goto out_cancel_msg;
3987 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3988 region->size, DEVLINK_ATTR_PAD);
3990 goto out_cancel_msg;
3992 genlmsg_end(msg, hdr);
3997 genlmsg_cancel(msg, hdr);
4000 return ERR_PTR(err);
4003 static void devlink_nl_region_notify(struct devlink_region *region,
4004 struct devlink_snapshot *snapshot,
4005 enum devlink_command cmd)
4007 struct devlink *devlink = region->devlink;
4008 struct sk_buff *msg;
4010 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
4012 msg = devlink_nl_region_notify_build(region, snapshot, cmd, 0, 0);
4016 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
4017 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
4021 * __devlink_snapshot_id_increment - Increment number of snapshots using an id
4022 * @devlink: devlink instance
4023 * @id: the snapshot id
4025 * Track when a new snapshot begins using an id. Load the count for the
4026 * given id from the snapshot xarray, increment it, and store it back.
4028 * Called when a new snapshot is created with the given id.
4030 * The id *must* have been previously allocated by
4031 * devlink_region_snapshot_id_get().
4033 * Returns 0 on success, or an error on failure.
4035 static int __devlink_snapshot_id_increment(struct devlink *devlink, u32 id)
4037 unsigned long count;
4040 lockdep_assert_held(&devlink->lock);
4042 p = xa_load(&devlink->snapshot_ids, id);
4046 if (WARN_ON(!xa_is_value(p)))
4049 count = xa_to_value(p);
4052 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4057 * __devlink_snapshot_id_decrement - Decrease number of snapshots using an id
4058 * @devlink: devlink instance
4059 * @id: the snapshot id
4061 * Track when a snapshot is deleted and stops using an id. Load the count
4062 * for the given id from the snapshot xarray, decrement it, and store it
4065 * If the count reaches zero, erase this id from the xarray, freeing it
4066 * up for future re-use by devlink_region_snapshot_id_get().
4068 * Called when a snapshot using the given id is deleted, and when the
4069 * initial allocator of the id is finished using it.
4071 static void __devlink_snapshot_id_decrement(struct devlink *devlink, u32 id)
4073 unsigned long count;
4076 lockdep_assert_held(&devlink->lock);
4078 p = xa_load(&devlink->snapshot_ids, id);
4082 if (WARN_ON(!xa_is_value(p)))
4085 count = xa_to_value(p);
4089 xa_store(&devlink->snapshot_ids, id, xa_mk_value(count),
4092 /* If this was the last user, we can erase this id */
4093 xa_erase(&devlink->snapshot_ids, id);
4098 * __devlink_snapshot_id_insert - Insert a specific snapshot ID
4099 * @devlink: devlink instance
4100 * @id: the snapshot id
4102 * Mark the given snapshot id as used by inserting a zero value into the
4105 * This must be called while holding the devlink instance lock. Unlike
4106 * devlink_snapshot_id_get, the initial reference count is zero, not one.
4107 * It is expected that the id will immediately be used before
4108 * releasing the devlink instance lock.
4110 * Returns zero on success, or an error code if the snapshot id could not
4113 static int __devlink_snapshot_id_insert(struct devlink *devlink, u32 id)
4115 lockdep_assert_held(&devlink->lock);
4117 if (xa_load(&devlink->snapshot_ids, id))
4120 return xa_err(xa_store(&devlink->snapshot_ids, id, xa_mk_value(0),
4125 * __devlink_region_snapshot_id_get - get snapshot ID
4126 * @devlink: devlink instance
4127 * @id: storage to return snapshot id
4129 * Allocates a new snapshot id. Returns zero on success, or a negative
4130 * error on failure. Must be called while holding the devlink instance
4133 * Snapshot IDs are tracked using an xarray which stores the number of
4134 * users of the snapshot id.
4136 * Note that the caller of this function counts as a 'user', in order to
4137 * avoid race conditions. The caller must release its hold on the
4138 * snapshot by using devlink_region_snapshot_id_put.
4140 static int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
4142 lockdep_assert_held(&devlink->lock);
4144 return xa_alloc(&devlink->snapshot_ids, id, xa_mk_value(1),
4145 xa_limit_32b, GFP_KERNEL);
4149 * __devlink_region_snapshot_create - create a new snapshot
4150 * This will add a new snapshot of a region. The snapshot
4151 * will be stored on the region struct and can be accessed
4152 * from devlink. This is useful for future analyses of snapshots.
4153 * Multiple snapshots can be created on a region.
4154 * The @snapshot_id should be obtained using the getter function.
4156 * Must be called only while holding the devlink instance lock.
4158 * @region: devlink region of the snapshot
4159 * @data: snapshot data
4160 * @snapshot_id: snapshot id to be created
4163 __devlink_region_snapshot_create(struct devlink_region *region,
4164 u8 *data, u32 snapshot_id)
4166 struct devlink *devlink = region->devlink;
4167 struct devlink_snapshot *snapshot;
4170 lockdep_assert_held(&devlink->lock);
4172 /* check if region can hold one more snapshot */
4173 if (region->cur_snapshots == region->max_snapshots)
4176 if (devlink_region_snapshot_get_by_id(region, snapshot_id))
4179 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
4183 err = __devlink_snapshot_id_increment(devlink, snapshot_id);
4185 goto err_snapshot_id_increment;
4187 snapshot->id = snapshot_id;
4188 snapshot->region = region;
4189 snapshot->data = data;
4191 list_add_tail(&snapshot->list, ®ion->snapshot_list);
4193 region->cur_snapshots++;
4195 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
4198 err_snapshot_id_increment:
4203 static void devlink_region_snapshot_del(struct devlink_region *region,
4204 struct devlink_snapshot *snapshot)
4206 struct devlink *devlink = region->devlink;
4208 lockdep_assert_held(&devlink->lock);
4210 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
4211 region->cur_snapshots--;
4212 list_del(&snapshot->list);
4213 region->ops->destructor(snapshot->data);
4214 __devlink_snapshot_id_decrement(devlink, snapshot->id);
4218 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
4219 struct genl_info *info)
4221 struct devlink *devlink = info->user_ptr[0];
4222 struct devlink_region *region;
4223 const char *region_name;
4224 struct sk_buff *msg;
4227 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
4230 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4231 region = devlink_region_get_by_name(devlink, region_name);
4235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4239 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
4240 info->snd_portid, info->snd_seq, 0,
4247 return genlmsg_reply(msg, info);
4250 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
4251 struct netlink_callback *cb)
4253 struct devlink_region *region;
4254 struct devlink *devlink;
4255 int start = cb->args[0];
4259 mutex_lock(&devlink_mutex);
4260 list_for_each_entry(devlink, &devlink_list, list) {
4261 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4264 mutex_lock(&devlink->lock);
4265 list_for_each_entry(region, &devlink->region_list, list) {
4270 err = devlink_nl_region_fill(msg, devlink,
4271 DEVLINK_CMD_REGION_GET,
4272 NETLINK_CB(cb->skb).portid,
4274 NLM_F_MULTI, region);
4276 mutex_unlock(&devlink->lock);
4281 mutex_unlock(&devlink->lock);
4284 mutex_unlock(&devlink_mutex);
4289 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
4290 struct genl_info *info)
4292 struct devlink *devlink = info->user_ptr[0];
4293 struct devlink_snapshot *snapshot;
4294 struct devlink_region *region;
4295 const char *region_name;
4298 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
4299 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
4302 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4303 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4305 region = devlink_region_get_by_name(devlink, region_name);
4309 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4313 devlink_region_snapshot_del(region, snapshot);
4318 devlink_nl_cmd_region_new(struct sk_buff *skb, struct genl_info *info)
4320 struct devlink *devlink = info->user_ptr[0];
4321 struct devlink_snapshot *snapshot;
4322 struct nlattr *snapshot_id_attr;
4323 struct devlink_region *region;
4324 const char *region_name;
4329 if (!info->attrs[DEVLINK_ATTR_REGION_NAME]) {
4330 NL_SET_ERR_MSG_MOD(info->extack, "No region name provided");
4334 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
4335 region = devlink_region_get_by_name(devlink, region_name);
4337 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not exist");
4341 if (!region->ops->snapshot) {
4342 NL_SET_ERR_MSG_MOD(info->extack, "The requested region does not support taking an immediate snapshot");
4346 if (region->cur_snapshots == region->max_snapshots) {
4347 NL_SET_ERR_MSG_MOD(info->extack, "The region has reached the maximum number of stored snapshots");
4351 snapshot_id_attr = info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID];
4352 if (snapshot_id_attr) {
4353 snapshot_id = nla_get_u32(snapshot_id_attr);
4355 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
4356 NL_SET_ERR_MSG_MOD(info->extack, "The requested snapshot id is already in use");
4360 err = __devlink_snapshot_id_insert(devlink, snapshot_id);
4364 err = __devlink_region_snapshot_id_get(devlink, &snapshot_id);
4366 NL_SET_ERR_MSG_MOD(info->extack, "Failed to allocate a new snapshot id");
4371 err = region->ops->snapshot(devlink, region->ops, info->extack, &data);
4373 goto err_snapshot_capture;
4375 err = __devlink_region_snapshot_create(region, data, snapshot_id);
4377 goto err_snapshot_create;
4379 if (!snapshot_id_attr) {
4380 struct sk_buff *msg;
4382 snapshot = devlink_region_snapshot_get_by_id(region,
4384 if (WARN_ON(!snapshot))
4387 msg = devlink_nl_region_notify_build(region, snapshot,
4388 DEVLINK_CMD_REGION_NEW,
4391 err = PTR_ERR_OR_ZERO(msg);
4395 err = genlmsg_reply(msg, info);
4402 err_snapshot_create:
4403 region->ops->destructor(data);
4404 err_snapshot_capture:
4405 __devlink_snapshot_id_decrement(devlink, snapshot_id);
4409 devlink_region_snapshot_del(region, snapshot);
4413 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
4414 struct devlink *devlink,
4415 u8 *chunk, u32 chunk_size,
4418 struct nlattr *chunk_attr;
4421 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
4425 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
4427 goto nla_put_failure;
4429 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
4432 goto nla_put_failure;
4434 nla_nest_end(msg, chunk_attr);
4438 nla_nest_cancel(msg, chunk_attr);
4442 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
4444 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
4445 struct devlink *devlink,
4446 struct devlink_region *region,
4447 struct nlattr **attrs,
4452 struct devlink_snapshot *snapshot;
4453 u64 curr_offset = start_offset;
4457 *new_offset = start_offset;
4459 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
4460 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
4464 while (curr_offset < end_offset) {
4468 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
4469 data_size = end_offset - curr_offset;
4471 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
4473 data = &snapshot->data[curr_offset];
4474 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
4480 curr_offset += data_size;
4482 *new_offset = curr_offset;
4487 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
4488 struct netlink_callback *cb)
4490 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
4491 u64 ret_offset, start_offset, end_offset = U64_MAX;
4492 struct nlattr **attrs = info->attrs;
4493 struct devlink_region *region;
4494 struct nlattr *chunks_attr;
4495 const char *region_name;
4496 struct devlink *devlink;
4500 start_offset = *((u64 *)&cb->args[0]);
4502 mutex_lock(&devlink_mutex);
4503 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
4504 if (IS_ERR(devlink)) {
4505 err = PTR_ERR(devlink);
4509 mutex_lock(&devlink->lock);
4511 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
4512 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
4517 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
4518 region = devlink_region_get_by_name(devlink, region_name);
4524 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
4525 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
4528 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4530 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
4531 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
4534 if (end_offset > region->size)
4535 end_offset = region->size;
4537 /* return 0 if there is no further data to read */
4538 if (start_offset == end_offset) {
4543 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
4544 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
4545 DEVLINK_CMD_REGION_READ);
4551 err = devlink_nl_put_handle(skb, devlink);
4553 goto nla_put_failure;
4555 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
4557 goto nla_put_failure;
4559 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
4562 goto nla_put_failure;
4565 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
4568 end_offset, &ret_offset);
4570 if (err && err != -EMSGSIZE)
4571 goto nla_put_failure;
4573 /* Check if there was any progress done to prevent infinite loop */
4574 if (ret_offset == start_offset) {
4576 goto nla_put_failure;
4579 *((u64 *)&cb->args[0]) = ret_offset;
4581 nla_nest_end(skb, chunks_attr);
4582 genlmsg_end(skb, hdr);
4583 mutex_unlock(&devlink->lock);
4584 mutex_unlock(&devlink_mutex);
4589 genlmsg_cancel(skb, hdr);
4591 mutex_unlock(&devlink->lock);
4593 mutex_unlock(&devlink_mutex);
4597 struct devlink_info_req {
4598 struct sk_buff *msg;
4601 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
4603 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
4605 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
4607 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
4609 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
4611 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
4613 int devlink_info_board_serial_number_put(struct devlink_info_req *req,
4616 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_BOARD_SERIAL_NUMBER,
4619 EXPORT_SYMBOL_GPL(devlink_info_board_serial_number_put);
4621 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
4622 const char *version_name,
4623 const char *version_value)
4625 struct nlattr *nest;
4628 nest = nla_nest_start_noflag(req->msg, attr);
4632 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
4635 goto nla_put_failure;
4637 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
4640 goto nla_put_failure;
4642 nla_nest_end(req->msg, nest);
4647 nla_nest_cancel(req->msg, nest);
4651 int devlink_info_version_fixed_put(struct devlink_info_req *req,
4652 const char *version_name,
4653 const char *version_value)
4655 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
4656 version_name, version_value);
4658 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
4660 int devlink_info_version_stored_put(struct devlink_info_req *req,
4661 const char *version_name,
4662 const char *version_value)
4664 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
4665 version_name, version_value);
4667 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
4669 int devlink_info_version_running_put(struct devlink_info_req *req,
4670 const char *version_name,
4671 const char *version_value)
4673 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
4674 version_name, version_value);
4676 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
4679 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
4680 enum devlink_command cmd, u32 portid,
4681 u32 seq, int flags, struct netlink_ext_ack *extack)
4683 struct devlink_info_req req;
4687 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4692 if (devlink_nl_put_handle(msg, devlink))
4693 goto err_cancel_msg;
4696 err = devlink->ops->info_get(devlink, &req, extack);
4698 goto err_cancel_msg;
4700 genlmsg_end(msg, hdr);
4704 genlmsg_cancel(msg, hdr);
4708 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
4709 struct genl_info *info)
4711 struct devlink *devlink = info->user_ptr[0];
4712 struct sk_buff *msg;
4715 if (!devlink->ops->info_get)
4718 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4722 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4723 info->snd_portid, info->snd_seq, 0,
4730 return genlmsg_reply(msg, info);
4733 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
4734 struct netlink_callback *cb)
4736 struct devlink *devlink;
4737 int start = cb->args[0];
4741 mutex_lock(&devlink_mutex);
4742 list_for_each_entry(devlink, &devlink_list, list) {
4743 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4750 if (!devlink->ops->info_get) {
4755 mutex_lock(&devlink->lock);
4756 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
4757 NETLINK_CB(cb->skb).portid,
4758 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4760 mutex_unlock(&devlink->lock);
4761 if (err == -EOPNOTSUPP)
4767 mutex_unlock(&devlink_mutex);
4769 if (err != -EMSGSIZE)
4776 struct devlink_fmsg_item {
4777 struct list_head list;
4784 struct devlink_fmsg {
4785 struct list_head item_list;
4786 bool putting_binary; /* This flag forces enclosing of binary data
4787 * in an array brackets. It forces using
4788 * of designated API:
4789 * devlink_fmsg_binary_pair_nest_start()
4790 * devlink_fmsg_binary_pair_nest_end()
4794 static struct devlink_fmsg *devlink_fmsg_alloc(void)
4796 struct devlink_fmsg *fmsg;
4798 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
4802 INIT_LIST_HEAD(&fmsg->item_list);
4807 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
4809 struct devlink_fmsg_item *item, *tmp;
4811 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
4812 list_del(&item->list);
4818 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
4821 struct devlink_fmsg_item *item;
4823 item = kzalloc(sizeof(*item), GFP_KERNEL);
4827 item->attrtype = attrtype;
4828 list_add_tail(&item->list, &fmsg->item_list);
4833 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
4835 if (fmsg->putting_binary)
4838 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
4840 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
4842 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
4844 if (fmsg->putting_binary)
4847 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4850 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4852 if (fmsg->putting_binary)
4855 return devlink_fmsg_nest_end(fmsg);
4857 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4859 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4861 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4863 struct devlink_fmsg_item *item;
4865 if (fmsg->putting_binary)
4868 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4871 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4875 item->nla_type = NLA_NUL_STRING;
4876 item->len = strlen(name) + 1;
4877 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4878 memcpy(&item->value, name, item->len);
4879 list_add_tail(&item->list, &fmsg->item_list);
4884 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4888 if (fmsg->putting_binary)
4891 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4895 err = devlink_fmsg_put_name(fmsg, name);
4901 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4903 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4905 if (fmsg->putting_binary)
4908 return devlink_fmsg_nest_end(fmsg);
4910 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4912 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4917 if (fmsg->putting_binary)
4920 err = devlink_fmsg_pair_nest_start(fmsg, name);
4924 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4930 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4932 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4936 if (fmsg->putting_binary)
4939 err = devlink_fmsg_nest_end(fmsg);
4943 err = devlink_fmsg_nest_end(fmsg);
4949 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4951 int devlink_fmsg_binary_pair_nest_start(struct devlink_fmsg *fmsg,
4956 err = devlink_fmsg_arr_pair_nest_start(fmsg, name);
4960 fmsg->putting_binary = true;
4963 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_start);
4965 int devlink_fmsg_binary_pair_nest_end(struct devlink_fmsg *fmsg)
4967 if (!fmsg->putting_binary)
4970 fmsg->putting_binary = false;
4971 return devlink_fmsg_arr_pair_nest_end(fmsg);
4973 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_nest_end);
4975 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4976 const void *value, u16 value_len,
4979 struct devlink_fmsg_item *item;
4981 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4984 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4988 item->nla_type = value_nla_type;
4989 item->len = value_len;
4990 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4991 memcpy(&item->value, value, item->len);
4992 list_add_tail(&item->list, &fmsg->item_list);
4997 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4999 if (fmsg->putting_binary)
5002 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
5004 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
5006 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
5008 if (fmsg->putting_binary)
5011 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
5013 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
5015 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
5017 if (fmsg->putting_binary)
5020 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
5022 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
5024 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
5026 if (fmsg->putting_binary)
5029 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
5031 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
5033 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
5035 if (fmsg->putting_binary)
5038 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
5041 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
5043 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
5046 if (!fmsg->putting_binary)
5049 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
5051 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
5053 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
5058 err = devlink_fmsg_pair_nest_start(fmsg, name);
5062 err = devlink_fmsg_bool_put(fmsg, value);
5066 err = devlink_fmsg_pair_nest_end(fmsg);
5072 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
5074 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
5079 err = devlink_fmsg_pair_nest_start(fmsg, name);
5083 err = devlink_fmsg_u8_put(fmsg, value);
5087 err = devlink_fmsg_pair_nest_end(fmsg);
5093 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
5095 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
5100 err = devlink_fmsg_pair_nest_start(fmsg, name);
5104 err = devlink_fmsg_u32_put(fmsg, value);
5108 err = devlink_fmsg_pair_nest_end(fmsg);
5114 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
5116 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
5121 err = devlink_fmsg_pair_nest_start(fmsg, name);
5125 err = devlink_fmsg_u64_put(fmsg, value);
5129 err = devlink_fmsg_pair_nest_end(fmsg);
5135 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
5137 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
5142 err = devlink_fmsg_pair_nest_start(fmsg, name);
5146 err = devlink_fmsg_string_put(fmsg, value);
5150 err = devlink_fmsg_pair_nest_end(fmsg);
5156 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
5158 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
5159 const void *value, u32 value_len)
5166 err = devlink_fmsg_binary_pair_nest_start(fmsg, name);
5170 for (offset = 0; offset < value_len; offset += data_size) {
5171 data_size = value_len - offset;
5172 if (data_size > DEVLINK_FMSG_MAX_SIZE)
5173 data_size = DEVLINK_FMSG_MAX_SIZE;
5174 err = devlink_fmsg_binary_put(fmsg, value + offset, data_size);
5177 /* Exit from loop with a break (instead of
5178 * return) to make sure putting_binary is turned off in
5179 * devlink_fmsg_binary_pair_nest_end
5183 end_err = devlink_fmsg_binary_pair_nest_end(fmsg);
5189 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
5192 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5194 switch (msg->nla_type) {
5199 case NLA_NUL_STRING:
5201 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
5209 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
5211 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
5214 switch (msg->nla_type) {
5216 /* Always provide flag data, regardless of its value */
5217 tmp = *(bool *) msg->value;
5219 return nla_put_u8(skb, attrtype, tmp);
5221 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
5223 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
5225 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
5227 case NLA_NUL_STRING:
5228 return nla_put_string(skb, attrtype, (char *) &msg->value);
5230 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
5237 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5240 struct devlink_fmsg_item *item;
5241 struct nlattr *fmsg_nlattr;
5245 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
5249 list_for_each_entry(item, &fmsg->item_list, list) {
5255 switch (item->attrtype) {
5256 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
5257 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
5258 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
5259 case DEVLINK_ATTR_FMSG_NEST_END:
5260 err = nla_put_flag(skb, item->attrtype);
5262 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
5263 err = devlink_fmsg_item_fill_type(item, skb);
5266 err = devlink_fmsg_item_fill_data(item, skb);
5268 case DEVLINK_ATTR_FMSG_OBJ_NAME:
5269 err = nla_put_string(skb, item->attrtype,
5270 (char *) &item->value);
5282 nla_nest_end(skb, fmsg_nlattr);
5286 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
5287 struct genl_info *info,
5288 enum devlink_command cmd, int flags)
5290 struct nlmsghdr *nlh;
5291 struct sk_buff *skb;
5298 int tmp_index = index;
5300 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5304 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
5305 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
5308 goto nla_put_failure;
5311 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5314 else if (err != -EMSGSIZE || tmp_index == index)
5315 goto nla_put_failure;
5317 genlmsg_end(skb, hdr);
5318 err = genlmsg_reply(skb, info);
5323 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
5326 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
5327 NLMSG_DONE, 0, flags | NLM_F_MULTI);
5330 goto nla_put_failure;
5333 return genlmsg_reply(skb, info);
5340 static int devlink_fmsg_dumpit(struct devlink_fmsg *fmsg, struct sk_buff *skb,
5341 struct netlink_callback *cb,
5342 enum devlink_command cmd)
5344 int index = cb->args[0];
5345 int tmp_index = index;
5349 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
5350 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI, cmd);
5353 goto nla_put_failure;
5356 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
5357 if ((err && err != -EMSGSIZE) || tmp_index == index)
5358 goto nla_put_failure;
5360 cb->args[0] = index;
5361 genlmsg_end(skb, hdr);
5365 genlmsg_cancel(skb, hdr);
5369 struct devlink_health_reporter {
5370 struct list_head list;
5372 const struct devlink_health_reporter_ops *ops;
5373 struct devlink *devlink;
5374 struct devlink_port *devlink_port;
5375 struct devlink_fmsg *dump_fmsg;
5376 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
5377 u64 graceful_period;
5385 u64 last_recovery_ts;
5386 refcount_t refcount;
5390 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
5392 return reporter->priv;
5394 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
5396 static struct devlink_health_reporter *
5397 __devlink_health_reporter_find_by_name(struct list_head *reporter_list,
5398 struct mutex *list_lock,
5399 const char *reporter_name)
5401 struct devlink_health_reporter *reporter;
5403 lockdep_assert_held(list_lock);
5404 list_for_each_entry(reporter, reporter_list, list)
5405 if (!strcmp(reporter->ops->name, reporter_name))
5410 static struct devlink_health_reporter *
5411 devlink_health_reporter_find_by_name(struct devlink *devlink,
5412 const char *reporter_name)
5414 return __devlink_health_reporter_find_by_name(&devlink->reporter_list,
5415 &devlink->reporters_lock,
5419 static struct devlink_health_reporter *
5420 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
5421 const char *reporter_name)
5423 return __devlink_health_reporter_find_by_name(&devlink_port->reporter_list,
5424 &devlink_port->reporters_lock,
5428 static struct devlink_health_reporter *
5429 __devlink_health_reporter_create(struct devlink *devlink,
5430 const struct devlink_health_reporter_ops *ops,
5431 u64 graceful_period, void *priv)
5433 struct devlink_health_reporter *reporter;
5435 if (WARN_ON(graceful_period && !ops->recover))
5436 return ERR_PTR(-EINVAL);
5438 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
5440 return ERR_PTR(-ENOMEM);
5442 reporter->priv = priv;
5443 reporter->ops = ops;
5444 reporter->devlink = devlink;
5445 reporter->graceful_period = graceful_period;
5446 reporter->auto_recover = !!ops->recover;
5447 reporter->auto_dump = !!ops->dump;
5448 mutex_init(&reporter->dump_lock);
5449 refcount_set(&reporter->refcount, 1);
5454 * devlink_port_health_reporter_create - create devlink health reporter for
5455 * specified port instance
5457 * @port: devlink_port which should contain the new reporter
5459 * @graceful_period: to avoid recovery loops, in msecs
5462 struct devlink_health_reporter *
5463 devlink_port_health_reporter_create(struct devlink_port *port,
5464 const struct devlink_health_reporter_ops *ops,
5465 u64 graceful_period, void *priv)
5467 struct devlink_health_reporter *reporter;
5469 mutex_lock(&port->reporters_lock);
5470 if (__devlink_health_reporter_find_by_name(&port->reporter_list,
5471 &port->reporters_lock, ops->name)) {
5472 reporter = ERR_PTR(-EEXIST);
5476 reporter = __devlink_health_reporter_create(port->devlink, ops,
5477 graceful_period, priv);
5478 if (IS_ERR(reporter))
5481 reporter->devlink_port = port;
5482 list_add_tail(&reporter->list, &port->reporter_list);
5484 mutex_unlock(&port->reporters_lock);
5487 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_create);
5490 * devlink_health_reporter_create - create devlink health reporter
5494 * @graceful_period: to avoid recovery loops, in msecs
5497 struct devlink_health_reporter *
5498 devlink_health_reporter_create(struct devlink *devlink,
5499 const struct devlink_health_reporter_ops *ops,
5500 u64 graceful_period, void *priv)
5502 struct devlink_health_reporter *reporter;
5504 mutex_lock(&devlink->reporters_lock);
5505 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
5506 reporter = ERR_PTR(-EEXIST);
5510 reporter = __devlink_health_reporter_create(devlink, ops,
5511 graceful_period, priv);
5512 if (IS_ERR(reporter))
5515 list_add_tail(&reporter->list, &devlink->reporter_list);
5517 mutex_unlock(&devlink->reporters_lock);
5520 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
5523 devlink_health_reporter_free(struct devlink_health_reporter *reporter)
5525 mutex_destroy(&reporter->dump_lock);
5526 if (reporter->dump_fmsg)
5527 devlink_fmsg_free(reporter->dump_fmsg);
5532 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
5534 if (refcount_dec_and_test(&reporter->refcount))
5535 devlink_health_reporter_free(reporter);
5539 __devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5541 list_del(&reporter->list);
5542 devlink_health_reporter_put(reporter);
5546 * devlink_health_reporter_destroy - destroy devlink health reporter
5548 * @reporter: devlink health reporter to destroy
5551 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
5553 struct mutex *lock = &reporter->devlink->reporters_lock;
5556 __devlink_health_reporter_destroy(reporter);
5559 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
5562 * devlink_port_health_reporter_destroy - destroy devlink port health reporter
5564 * @reporter: devlink health reporter to destroy
5567 devlink_port_health_reporter_destroy(struct devlink_health_reporter *reporter)
5569 struct mutex *lock = &reporter->devlink_port->reporters_lock;
5572 __devlink_health_reporter_destroy(reporter);
5575 EXPORT_SYMBOL_GPL(devlink_port_health_reporter_destroy);
5578 devlink_nl_health_reporter_fill(struct sk_buff *msg,
5579 struct devlink *devlink,
5580 struct devlink_health_reporter *reporter,
5581 enum devlink_command cmd, u32 portid,
5584 struct nlattr *reporter_attr;
5587 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
5591 if (devlink_nl_put_handle(msg, devlink))
5592 goto genlmsg_cancel;
5594 if (reporter->devlink_port) {
5595 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
5596 goto genlmsg_cancel;
5598 reporter_attr = nla_nest_start_noflag(msg,
5599 DEVLINK_ATTR_HEALTH_REPORTER);
5601 goto genlmsg_cancel;
5602 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
5603 reporter->ops->name))
5604 goto reporter_nest_cancel;
5605 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
5606 reporter->health_state))
5607 goto reporter_nest_cancel;
5608 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
5609 reporter->error_count, DEVLINK_ATTR_PAD))
5610 goto reporter_nest_cancel;
5611 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
5612 reporter->recovery_count, DEVLINK_ATTR_PAD))
5613 goto reporter_nest_cancel;
5614 if (reporter->ops->recover &&
5615 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
5616 reporter->graceful_period,
5618 goto reporter_nest_cancel;
5619 if (reporter->ops->recover &&
5620 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
5621 reporter->auto_recover))
5622 goto reporter_nest_cancel;
5623 if (reporter->dump_fmsg &&
5624 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
5625 jiffies_to_msecs(reporter->dump_ts),
5627 goto reporter_nest_cancel;
5628 if (reporter->dump_fmsg &&
5629 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
5630 reporter->dump_real_ts, DEVLINK_ATTR_PAD))
5631 goto reporter_nest_cancel;
5632 if (reporter->ops->dump &&
5633 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
5634 reporter->auto_dump))
5635 goto reporter_nest_cancel;
5637 nla_nest_end(msg, reporter_attr);
5638 genlmsg_end(msg, hdr);
5641 reporter_nest_cancel:
5642 nla_nest_end(msg, reporter_attr);
5644 genlmsg_cancel(msg, hdr);
5648 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
5649 enum devlink_command cmd)
5651 struct sk_buff *msg;
5654 WARN_ON(cmd != DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5660 err = devlink_nl_health_reporter_fill(msg, reporter->devlink,
5661 reporter, cmd, 0, 0, 0);
5667 genlmsg_multicast_netns(&devlink_nl_family,
5668 devlink_net(reporter->devlink),
5669 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
5673 devlink_health_reporter_recovery_done(struct devlink_health_reporter *reporter)
5675 reporter->recovery_count++;
5676 reporter->last_recovery_ts = jiffies;
5678 EXPORT_SYMBOL_GPL(devlink_health_reporter_recovery_done);
5681 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
5682 void *priv_ctx, struct netlink_ext_ack *extack)
5686 if (reporter->health_state == DEVLINK_HEALTH_REPORTER_STATE_HEALTHY)
5689 if (!reporter->ops->recover)
5692 err = reporter->ops->recover(reporter, priv_ctx, extack);
5696 devlink_health_reporter_recovery_done(reporter);
5697 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
5698 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5704 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
5706 if (!reporter->dump_fmsg)
5708 devlink_fmsg_free(reporter->dump_fmsg);
5709 reporter->dump_fmsg = NULL;
5712 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
5714 struct netlink_ext_ack *extack)
5718 if (!reporter->ops->dump)
5721 if (reporter->dump_fmsg)
5724 reporter->dump_fmsg = devlink_fmsg_alloc();
5725 if (!reporter->dump_fmsg) {
5730 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
5734 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
5739 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
5743 reporter->dump_ts = jiffies;
5744 reporter->dump_real_ts = ktime_get_real_ns();
5749 devlink_health_dump_clear(reporter);
5753 int devlink_health_report(struct devlink_health_reporter *reporter,
5754 const char *msg, void *priv_ctx)
5756 enum devlink_health_reporter_state prev_health_state;
5757 struct devlink *devlink = reporter->devlink;
5758 unsigned long recover_ts_threshold;
5760 /* write a log message of the current error */
5762 trace_devlink_health_report(devlink, reporter->ops->name, msg);
5763 reporter->error_count++;
5764 prev_health_state = reporter->health_state;
5765 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
5766 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5768 /* abort if the previous error wasn't recovered */
5769 recover_ts_threshold = reporter->last_recovery_ts +
5770 msecs_to_jiffies(reporter->graceful_period);
5771 if (reporter->auto_recover &&
5772 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
5773 (reporter->last_recovery_ts && reporter->recovery_count &&
5774 time_is_after_jiffies(recover_ts_threshold)))) {
5775 trace_devlink_health_recover_aborted(devlink,
5776 reporter->ops->name,
5777 reporter->health_state,
5779 reporter->last_recovery_ts);
5783 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
5785 if (reporter->auto_dump) {
5786 mutex_lock(&reporter->dump_lock);
5787 /* store current dump of current error, for later analysis */
5788 devlink_health_do_dump(reporter, priv_ctx, NULL);
5789 mutex_unlock(&reporter->dump_lock);
5792 if (reporter->auto_recover)
5793 return devlink_health_reporter_recover(reporter,
5798 EXPORT_SYMBOL_GPL(devlink_health_report);
5800 static struct devlink_health_reporter *
5801 devlink_health_reporter_get_from_attrs(struct devlink *devlink,
5802 struct nlattr **attrs)
5804 struct devlink_health_reporter *reporter;
5805 struct devlink_port *devlink_port;
5806 char *reporter_name;
5808 if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
5811 reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
5812 devlink_port = devlink_port_get_from_attrs(devlink, attrs);
5813 if (IS_ERR(devlink_port)) {
5814 mutex_lock(&devlink->reporters_lock);
5815 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
5817 refcount_inc(&reporter->refcount);
5818 mutex_unlock(&devlink->reporters_lock);
5820 mutex_lock(&devlink_port->reporters_lock);
5821 reporter = devlink_port_health_reporter_find_by_name(devlink_port, reporter_name);
5823 refcount_inc(&reporter->refcount);
5824 mutex_unlock(&devlink_port->reporters_lock);
5830 static struct devlink_health_reporter *
5831 devlink_health_reporter_get_from_info(struct devlink *devlink,
5832 struct genl_info *info)
5834 return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
5837 static struct devlink_health_reporter *
5838 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
5840 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
5841 struct devlink_health_reporter *reporter;
5842 struct nlattr **attrs = info->attrs;
5843 struct devlink *devlink;
5845 mutex_lock(&devlink_mutex);
5846 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
5847 if (IS_ERR(devlink))
5850 reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
5851 mutex_unlock(&devlink_mutex);
5854 mutex_unlock(&devlink_mutex);
5859 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
5860 enum devlink_health_reporter_state state)
5862 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
5863 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
5866 if (reporter->health_state == state)
5869 reporter->health_state = state;
5870 trace_devlink_health_reporter_state_update(reporter->devlink,
5871 reporter->ops->name, state);
5872 devlink_recover_notify(reporter, DEVLINK_CMD_HEALTH_REPORTER_RECOVER);
5874 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
5876 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
5877 struct genl_info *info)
5879 struct devlink *devlink = info->user_ptr[0];
5880 struct devlink_health_reporter *reporter;
5881 struct sk_buff *msg;
5884 reporter = devlink_health_reporter_get_from_info(devlink, info);
5888 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5894 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
5895 DEVLINK_CMD_HEALTH_REPORTER_GET,
5896 info->snd_portid, info->snd_seq,
5903 err = genlmsg_reply(msg, info);
5905 devlink_health_reporter_put(reporter);
5910 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
5911 struct netlink_callback *cb)
5913 struct devlink_health_reporter *reporter;
5914 struct devlink_port *port;
5915 struct devlink *devlink;
5916 int start = cb->args[0];
5920 mutex_lock(&devlink_mutex);
5921 list_for_each_entry(devlink, &devlink_list, list) {
5922 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5924 mutex_lock(&devlink->reporters_lock);
5925 list_for_each_entry(reporter, &devlink->reporter_list,
5931 err = devlink_nl_health_reporter_fill(msg, devlink,
5933 DEVLINK_CMD_HEALTH_REPORTER_GET,
5934 NETLINK_CB(cb->skb).portid,
5938 mutex_unlock(&devlink->reporters_lock);
5943 mutex_unlock(&devlink->reporters_lock);
5946 list_for_each_entry(devlink, &devlink_list, list) {
5947 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
5949 mutex_lock(&devlink->lock);
5950 list_for_each_entry(port, &devlink->port_list, list) {
5951 mutex_lock(&port->reporters_lock);
5952 list_for_each_entry(reporter, &port->reporter_list, list) {
5957 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
5958 DEVLINK_CMD_HEALTH_REPORTER_GET,
5959 NETLINK_CB(cb->skb).portid,
5963 mutex_unlock(&port->reporters_lock);
5964 mutex_unlock(&devlink->lock);
5969 mutex_unlock(&port->reporters_lock);
5971 mutex_unlock(&devlink->lock);
5974 mutex_unlock(&devlink_mutex);
5981 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
5982 struct genl_info *info)
5984 struct devlink *devlink = info->user_ptr[0];
5985 struct devlink_health_reporter *reporter;
5988 reporter = devlink_health_reporter_get_from_info(devlink, info);
5992 if (!reporter->ops->recover &&
5993 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
5994 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
5998 if (!reporter->ops->dump &&
5999 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]) {
6004 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
6005 reporter->graceful_period =
6006 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
6008 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
6009 reporter->auto_recover =
6010 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
6012 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
6013 reporter->auto_dump =
6014 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
6016 devlink_health_reporter_put(reporter);
6019 devlink_health_reporter_put(reporter);
6023 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
6024 struct genl_info *info)
6026 struct devlink *devlink = info->user_ptr[0];
6027 struct devlink_health_reporter *reporter;
6030 reporter = devlink_health_reporter_get_from_info(devlink, info);
6034 err = devlink_health_reporter_recover(reporter, NULL, info->extack);
6036 devlink_health_reporter_put(reporter);
6040 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
6041 struct genl_info *info)
6043 struct devlink *devlink = info->user_ptr[0];
6044 struct devlink_health_reporter *reporter;
6045 struct devlink_fmsg *fmsg;
6048 reporter = devlink_health_reporter_get_from_info(devlink, info);
6052 if (!reporter->ops->diagnose) {
6053 devlink_health_reporter_put(reporter);
6057 fmsg = devlink_fmsg_alloc();
6059 devlink_health_reporter_put(reporter);
6063 err = devlink_fmsg_obj_nest_start(fmsg);
6067 err = reporter->ops->diagnose(reporter, fmsg, info->extack);
6071 err = devlink_fmsg_obj_nest_end(fmsg);
6075 err = devlink_fmsg_snd(fmsg, info,
6076 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
6079 devlink_fmsg_free(fmsg);
6080 devlink_health_reporter_put(reporter);
6085 devlink_nl_cmd_health_reporter_dump_get_dumpit(struct sk_buff *skb,
6086 struct netlink_callback *cb)
6088 struct devlink_health_reporter *reporter;
6089 u64 start = cb->args[0];
6092 reporter = devlink_health_reporter_get_from_cb(cb);
6096 if (!reporter->ops->dump) {
6100 mutex_lock(&reporter->dump_lock);
6102 err = devlink_health_do_dump(reporter, NULL, cb->extack);
6105 cb->args[1] = reporter->dump_ts;
6107 if (!reporter->dump_fmsg || cb->args[1] != reporter->dump_ts) {
6108 NL_SET_ERR_MSG_MOD(cb->extack, "Dump trampled, please retry");
6113 err = devlink_fmsg_dumpit(reporter->dump_fmsg, skb, cb,
6114 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET);
6116 mutex_unlock(&reporter->dump_lock);
6118 devlink_health_reporter_put(reporter);
6123 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
6124 struct genl_info *info)
6126 struct devlink *devlink = info->user_ptr[0];
6127 struct devlink_health_reporter *reporter;
6129 reporter = devlink_health_reporter_get_from_info(devlink, info);
6133 if (!reporter->ops->dump) {
6134 devlink_health_reporter_put(reporter);
6138 mutex_lock(&reporter->dump_lock);
6139 devlink_health_dump_clear(reporter);
6140 mutex_unlock(&reporter->dump_lock);
6141 devlink_health_reporter_put(reporter);
6145 static int devlink_nl_cmd_health_reporter_test_doit(struct sk_buff *skb,
6146 struct genl_info *info)
6148 struct devlink *devlink = info->user_ptr[0];
6149 struct devlink_health_reporter *reporter;
6152 reporter = devlink_health_reporter_get_from_info(devlink, info);
6156 if (!reporter->ops->test) {
6157 devlink_health_reporter_put(reporter);
6161 err = reporter->ops->test(reporter, info->extack);
6163 devlink_health_reporter_put(reporter);
6167 struct devlink_stats {
6170 struct u64_stats_sync syncp;
6174 * struct devlink_trap_policer_item - Packet trap policer attributes.
6175 * @policer: Immutable packet trap policer attributes.
6176 * @rate: Rate in packets / sec.
6177 * @burst: Burst size in packets.
6178 * @list: trap_policer_list member.
6180 * Describes packet trap policer attributes. Created by devlink during trap
6181 * policer registration.
6183 struct devlink_trap_policer_item {
6184 const struct devlink_trap_policer *policer;
6187 struct list_head list;
6191 * struct devlink_trap_group_item - Packet trap group attributes.
6192 * @group: Immutable packet trap group attributes.
6193 * @policer_item: Associated policer item. Can be NULL.
6194 * @list: trap_group_list member.
6195 * @stats: Trap group statistics.
6197 * Describes packet trap group attributes. Created by devlink during trap
6198 * group registration.
6200 struct devlink_trap_group_item {
6201 const struct devlink_trap_group *group;
6202 struct devlink_trap_policer_item *policer_item;
6203 struct list_head list;
6204 struct devlink_stats __percpu *stats;
6208 * struct devlink_trap_item - Packet trap attributes.
6209 * @trap: Immutable packet trap attributes.
6210 * @group_item: Associated group item.
6211 * @list: trap_list member.
6212 * @action: Trap action.
6213 * @stats: Trap statistics.
6214 * @priv: Driver private information.
6216 * Describes both mutable and immutable packet trap attributes. Created by
6217 * devlink during trap registration and used for all trap related operations.
6219 struct devlink_trap_item {
6220 const struct devlink_trap *trap;
6221 struct devlink_trap_group_item *group_item;
6222 struct list_head list;
6223 enum devlink_trap_action action;
6224 struct devlink_stats __percpu *stats;
6228 static struct devlink_trap_policer_item *
6229 devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
6231 struct devlink_trap_policer_item *policer_item;
6233 list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
6234 if (policer_item->policer->id == id)
6235 return policer_item;
6241 static struct devlink_trap_item *
6242 devlink_trap_item_lookup(struct devlink *devlink, const char *name)
6244 struct devlink_trap_item *trap_item;
6246 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6247 if (!strcmp(trap_item->trap->name, name))
6254 static struct devlink_trap_item *
6255 devlink_trap_item_get_from_info(struct devlink *devlink,
6256 struct genl_info *info)
6258 struct nlattr *attr;
6260 if (!info->attrs[DEVLINK_ATTR_TRAP_NAME])
6262 attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];
6264 return devlink_trap_item_lookup(devlink, nla_data(attr));
6268 devlink_trap_action_get_from_info(struct genl_info *info,
6269 enum devlink_trap_action *p_trap_action)
6273 val = nla_get_u8(info->attrs[DEVLINK_ATTR_TRAP_ACTION]);
6275 case DEVLINK_TRAP_ACTION_DROP:
6276 case DEVLINK_TRAP_ACTION_TRAP:
6277 case DEVLINK_TRAP_ACTION_MIRROR:
6278 *p_trap_action = val;
6287 static int devlink_trap_metadata_put(struct sk_buff *msg,
6288 const struct devlink_trap *trap)
6290 struct nlattr *attr;
6292 attr = nla_nest_start(msg, DEVLINK_ATTR_TRAP_METADATA);
6296 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_IN_PORT) &&
6297 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_IN_PORT))
6298 goto nla_put_failure;
6299 if ((trap->metadata_cap & DEVLINK_TRAP_METADATA_TYPE_F_FA_COOKIE) &&
6300 nla_put_flag(msg, DEVLINK_ATTR_TRAP_METADATA_TYPE_FA_COOKIE))
6301 goto nla_put_failure;
6303 nla_nest_end(msg, attr);
6308 nla_nest_cancel(msg, attr);
6312 static void devlink_trap_stats_read(struct devlink_stats __percpu *trap_stats,
6313 struct devlink_stats *stats)
6317 memset(stats, 0, sizeof(*stats));
6318 for_each_possible_cpu(i) {
6319 struct devlink_stats *cpu_stats;
6320 u64 rx_packets, rx_bytes;
6323 cpu_stats = per_cpu_ptr(trap_stats, i);
6325 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
6326 rx_packets = cpu_stats->rx_packets;
6327 rx_bytes = cpu_stats->rx_bytes;
6328 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
6330 stats->rx_packets += rx_packets;
6331 stats->rx_bytes += rx_bytes;
6335 static int devlink_trap_stats_put(struct sk_buff *msg,
6336 struct devlink_stats __percpu *trap_stats)
6338 struct devlink_stats stats;
6339 struct nlattr *attr;
6341 devlink_trap_stats_read(trap_stats, &stats);
6343 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6347 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_PACKETS,
6348 stats.rx_packets, DEVLINK_ATTR_PAD))
6349 goto nla_put_failure;
6351 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_BYTES,
6352 stats.rx_bytes, DEVLINK_ATTR_PAD))
6353 goto nla_put_failure;
6355 nla_nest_end(msg, attr);
6360 nla_nest_cancel(msg, attr);
6364 static int devlink_nl_trap_fill(struct sk_buff *msg, struct devlink *devlink,
6365 const struct devlink_trap_item *trap_item,
6366 enum devlink_command cmd, u32 portid, u32 seq,
6369 struct devlink_trap_group_item *group_item = trap_item->group_item;
6373 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6377 if (devlink_nl_put_handle(msg, devlink))
6378 goto nla_put_failure;
6380 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6381 group_item->group->name))
6382 goto nla_put_failure;
6384 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_NAME, trap_item->trap->name))
6385 goto nla_put_failure;
6387 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_TYPE, trap_item->trap->type))
6388 goto nla_put_failure;
6390 if (trap_item->trap->generic &&
6391 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6392 goto nla_put_failure;
6394 if (nla_put_u8(msg, DEVLINK_ATTR_TRAP_ACTION, trap_item->action))
6395 goto nla_put_failure;
6397 err = devlink_trap_metadata_put(msg, trap_item->trap);
6399 goto nla_put_failure;
6401 err = devlink_trap_stats_put(msg, trap_item->stats);
6403 goto nla_put_failure;
6405 genlmsg_end(msg, hdr);
6410 genlmsg_cancel(msg, hdr);
6414 static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
6415 struct genl_info *info)
6417 struct netlink_ext_ack *extack = info->extack;
6418 struct devlink *devlink = info->user_ptr[0];
6419 struct devlink_trap_item *trap_item;
6420 struct sk_buff *msg;
6423 if (list_empty(&devlink->trap_list))
6426 trap_item = devlink_trap_item_get_from_info(devlink, info);
6428 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6432 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6436 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6437 DEVLINK_CMD_TRAP_NEW, info->snd_portid,
6442 return genlmsg_reply(msg, info);
6449 static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
6450 struct netlink_callback *cb)
6452 struct devlink_trap_item *trap_item;
6453 struct devlink *devlink;
6454 int start = cb->args[0];
6458 mutex_lock(&devlink_mutex);
6459 list_for_each_entry(devlink, &devlink_list, list) {
6460 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6462 mutex_lock(&devlink->lock);
6463 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6468 err = devlink_nl_trap_fill(msg, devlink, trap_item,
6469 DEVLINK_CMD_TRAP_NEW,
6470 NETLINK_CB(cb->skb).portid,
6474 mutex_unlock(&devlink->lock);
6479 mutex_unlock(&devlink->lock);
6482 mutex_unlock(&devlink_mutex);
6488 static int __devlink_trap_action_set(struct devlink *devlink,
6489 struct devlink_trap_item *trap_item,
6490 enum devlink_trap_action trap_action,
6491 struct netlink_ext_ack *extack)
6495 if (trap_item->action != trap_action &&
6496 trap_item->trap->type != DEVLINK_TRAP_TYPE_DROP) {
6497 NL_SET_ERR_MSG_MOD(extack, "Cannot change action of non-drop traps. Skipping");
6501 err = devlink->ops->trap_action_set(devlink, trap_item->trap,
6502 trap_action, extack);
6506 trap_item->action = trap_action;
6511 static int devlink_trap_action_set(struct devlink *devlink,
6512 struct devlink_trap_item *trap_item,
6513 struct genl_info *info)
6515 enum devlink_trap_action trap_action;
6518 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6521 err = devlink_trap_action_get_from_info(info, &trap_action);
6523 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6527 return __devlink_trap_action_set(devlink, trap_item, trap_action,
6531 static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
6532 struct genl_info *info)
6534 struct netlink_ext_ack *extack = info->extack;
6535 struct devlink *devlink = info->user_ptr[0];
6536 struct devlink_trap_item *trap_item;
6539 if (list_empty(&devlink->trap_list))
6542 trap_item = devlink_trap_item_get_from_info(devlink, info);
6544 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
6548 err = devlink_trap_action_set(devlink, trap_item, info);
6555 static struct devlink_trap_group_item *
6556 devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
6558 struct devlink_trap_group_item *group_item;
6560 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6561 if (!strcmp(group_item->group->name, name))
6568 static struct devlink_trap_group_item *
6569 devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
6571 struct devlink_trap_group_item *group_item;
6573 list_for_each_entry(group_item, &devlink->trap_group_list, list) {
6574 if (group_item->group->id == id)
6581 static struct devlink_trap_group_item *
6582 devlink_trap_group_item_get_from_info(struct devlink *devlink,
6583 struct genl_info *info)
6587 if (!info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME])
6589 name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);
6591 return devlink_trap_group_item_lookup(devlink, name);
6595 devlink_nl_trap_group_fill(struct sk_buff *msg, struct devlink *devlink,
6596 const struct devlink_trap_group_item *group_item,
6597 enum devlink_command cmd, u32 portid, u32 seq,
6603 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6607 if (devlink_nl_put_handle(msg, devlink))
6608 goto nla_put_failure;
6610 if (nla_put_string(msg, DEVLINK_ATTR_TRAP_GROUP_NAME,
6611 group_item->group->name))
6612 goto nla_put_failure;
6614 if (group_item->group->generic &&
6615 nla_put_flag(msg, DEVLINK_ATTR_TRAP_GENERIC))
6616 goto nla_put_failure;
6618 if (group_item->policer_item &&
6619 nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
6620 group_item->policer_item->policer->id))
6621 goto nla_put_failure;
6623 err = devlink_trap_stats_put(msg, group_item->stats);
6625 goto nla_put_failure;
6627 genlmsg_end(msg, hdr);
6632 genlmsg_cancel(msg, hdr);
6636 static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
6637 struct genl_info *info)
6639 struct netlink_ext_ack *extack = info->extack;
6640 struct devlink *devlink = info->user_ptr[0];
6641 struct devlink_trap_group_item *group_item;
6642 struct sk_buff *msg;
6645 if (list_empty(&devlink->trap_group_list))
6648 group_item = devlink_trap_group_item_get_from_info(devlink, info);
6650 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
6654 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6658 err = devlink_nl_trap_group_fill(msg, devlink, group_item,
6659 DEVLINK_CMD_TRAP_GROUP_NEW,
6660 info->snd_portid, info->snd_seq, 0);
6662 goto err_trap_group_fill;
6664 return genlmsg_reply(msg, info);
6666 err_trap_group_fill:
6671 static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
6672 struct netlink_callback *cb)
6674 enum devlink_command cmd = DEVLINK_CMD_TRAP_GROUP_NEW;
6675 struct devlink_trap_group_item *group_item;
6676 u32 portid = NETLINK_CB(cb->skb).portid;
6677 struct devlink *devlink;
6678 int start = cb->args[0];
6682 mutex_lock(&devlink_mutex);
6683 list_for_each_entry(devlink, &devlink_list, list) {
6684 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6686 mutex_lock(&devlink->lock);
6687 list_for_each_entry(group_item, &devlink->trap_group_list,
6693 err = devlink_nl_trap_group_fill(msg, devlink,
6699 mutex_unlock(&devlink->lock);
6704 mutex_unlock(&devlink->lock);
6707 mutex_unlock(&devlink_mutex);
6714 __devlink_trap_group_action_set(struct devlink *devlink,
6715 struct devlink_trap_group_item *group_item,
6716 enum devlink_trap_action trap_action,
6717 struct netlink_ext_ack *extack)
6719 const char *group_name = group_item->group->name;
6720 struct devlink_trap_item *trap_item;
6723 list_for_each_entry(trap_item, &devlink->trap_list, list) {
6724 if (strcmp(trap_item->group_item->group->name, group_name))
6726 err = __devlink_trap_action_set(devlink, trap_item,
6727 trap_action, extack);
6736 devlink_trap_group_action_set(struct devlink *devlink,
6737 struct devlink_trap_group_item *group_item,
6738 struct genl_info *info, bool *p_modified)
6740 enum devlink_trap_action trap_action;
6743 if (!info->attrs[DEVLINK_ATTR_TRAP_ACTION])
6746 err = devlink_trap_action_get_from_info(info, &trap_action);
6748 NL_SET_ERR_MSG_MOD(info->extack, "Invalid trap action");
6752 err = __devlink_trap_group_action_set(devlink, group_item, trap_action,
6762 static int devlink_trap_group_set(struct devlink *devlink,
6763 struct devlink_trap_group_item *group_item,
6764 struct genl_info *info)
6766 struct devlink_trap_policer_item *policer_item;
6767 struct netlink_ext_ack *extack = info->extack;
6768 const struct devlink_trap_policer *policer;
6769 struct nlattr **attrs = info->attrs;
6772 if (!attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
6775 if (!devlink->ops->trap_group_set)
6778 policer_item = group_item->policer_item;
6779 if (attrs[DEVLINK_ATTR_TRAP_POLICER_ID]) {
6782 policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
6783 policer_item = devlink_trap_policer_item_lookup(devlink,
6785 if (policer_id && !policer_item) {
6786 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
6790 policer = policer_item ? policer_item->policer : NULL;
6792 err = devlink->ops->trap_group_set(devlink, group_item->group, policer,
6797 group_item->policer_item = policer_item;
6802 static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
6803 struct genl_info *info)
6805 struct netlink_ext_ack *extack = info->extack;
6806 struct devlink *devlink = info->user_ptr[0];
6807 struct devlink_trap_group_item *group_item;
6808 bool modified = false;
6811 if (list_empty(&devlink->trap_group_list))
6814 group_item = devlink_trap_group_item_get_from_info(devlink, info);
6816 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
6820 err = devlink_trap_group_action_set(devlink, group_item, info,
6825 err = devlink_trap_group_set(devlink, group_item, info);
6827 goto err_trap_group_set;
6833 NL_SET_ERR_MSG_MOD(extack, "Trap group set failed, but some changes were committed already");
6837 static struct devlink_trap_policer_item *
6838 devlink_trap_policer_item_get_from_info(struct devlink *devlink,
6839 struct genl_info *info)
6843 if (!info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID])
6845 id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
6847 return devlink_trap_policer_item_lookup(devlink, id);
6851 devlink_trap_policer_stats_put(struct sk_buff *msg, struct devlink *devlink,
6852 const struct devlink_trap_policer *policer)
6854 struct nlattr *attr;
6858 if (!devlink->ops->trap_policer_counter_get)
6861 err = devlink->ops->trap_policer_counter_get(devlink, policer, &drops);
6865 attr = nla_nest_start(msg, DEVLINK_ATTR_STATS);
6869 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_STATS_RX_DROPPED, drops,
6871 goto nla_put_failure;
6873 nla_nest_end(msg, attr);
6878 nla_nest_cancel(msg, attr);
6883 devlink_nl_trap_policer_fill(struct sk_buff *msg, struct devlink *devlink,
6884 const struct devlink_trap_policer_item *policer_item,
6885 enum devlink_command cmd, u32 portid, u32 seq,
6891 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
6895 if (devlink_nl_put_handle(msg, devlink))
6896 goto nla_put_failure;
6898 if (nla_put_u32(msg, DEVLINK_ATTR_TRAP_POLICER_ID,
6899 policer_item->policer->id))
6900 goto nla_put_failure;
6902 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_RATE,
6903 policer_item->rate, DEVLINK_ATTR_PAD))
6904 goto nla_put_failure;
6906 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_TRAP_POLICER_BURST,
6907 policer_item->burst, DEVLINK_ATTR_PAD))
6908 goto nla_put_failure;
6910 err = devlink_trap_policer_stats_put(msg, devlink,
6911 policer_item->policer);
6913 goto nla_put_failure;
6915 genlmsg_end(msg, hdr);
6920 genlmsg_cancel(msg, hdr);
6924 static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
6925 struct genl_info *info)
6927 struct devlink_trap_policer_item *policer_item;
6928 struct netlink_ext_ack *extack = info->extack;
6929 struct devlink *devlink = info->user_ptr[0];
6930 struct sk_buff *msg;
6933 if (list_empty(&devlink->trap_policer_list))
6936 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
6937 if (!policer_item) {
6938 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
6942 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6946 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item,
6947 DEVLINK_CMD_TRAP_POLICER_NEW,
6948 info->snd_portid, info->snd_seq, 0);
6950 goto err_trap_policer_fill;
6952 return genlmsg_reply(msg, info);
6954 err_trap_policer_fill:
6959 static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
6960 struct netlink_callback *cb)
6962 enum devlink_command cmd = DEVLINK_CMD_TRAP_POLICER_NEW;
6963 struct devlink_trap_policer_item *policer_item;
6964 u32 portid = NETLINK_CB(cb->skb).portid;
6965 struct devlink *devlink;
6966 int start = cb->args[0];
6970 mutex_lock(&devlink_mutex);
6971 list_for_each_entry(devlink, &devlink_list, list) {
6972 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
6974 mutex_lock(&devlink->lock);
6975 list_for_each_entry(policer_item, &devlink->trap_policer_list,
6981 err = devlink_nl_trap_policer_fill(msg, devlink,
6987 mutex_unlock(&devlink->lock);
6992 mutex_unlock(&devlink->lock);
6995 mutex_unlock(&devlink_mutex);
7002 devlink_trap_policer_set(struct devlink *devlink,
7003 struct devlink_trap_policer_item *policer_item,
7004 struct genl_info *info)
7006 struct netlink_ext_ack *extack = info->extack;
7007 struct nlattr **attrs = info->attrs;
7011 rate = policer_item->rate;
7012 burst = policer_item->burst;
7014 if (attrs[DEVLINK_ATTR_TRAP_POLICER_RATE])
7015 rate = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_RATE]);
7017 if (attrs[DEVLINK_ATTR_TRAP_POLICER_BURST])
7018 burst = nla_get_u64(attrs[DEVLINK_ATTR_TRAP_POLICER_BURST]);
7020 if (rate < policer_item->policer->min_rate) {
7021 NL_SET_ERR_MSG_MOD(extack, "Policer rate lower than limit");
7025 if (rate > policer_item->policer->max_rate) {
7026 NL_SET_ERR_MSG_MOD(extack, "Policer rate higher than limit");
7030 if (burst < policer_item->policer->min_burst) {
7031 NL_SET_ERR_MSG_MOD(extack, "Policer burst size lower than limit");
7035 if (burst > policer_item->policer->max_burst) {
7036 NL_SET_ERR_MSG_MOD(extack, "Policer burst size higher than limit");
7040 err = devlink->ops->trap_policer_set(devlink, policer_item->policer,
7041 rate, burst, info->extack);
7045 policer_item->rate = rate;
7046 policer_item->burst = burst;
7051 static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
7052 struct genl_info *info)
7054 struct devlink_trap_policer_item *policer_item;
7055 struct netlink_ext_ack *extack = info->extack;
7056 struct devlink *devlink = info->user_ptr[0];
7058 if (list_empty(&devlink->trap_policer_list))
7061 if (!devlink->ops->trap_policer_set)
7064 policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
7065 if (!policer_item) {
7066 NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
7070 return devlink_trap_policer_set(devlink, policer_item, info);
7073 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
7074 [DEVLINK_ATTR_UNSPEC] = { .strict_start_type =
7075 DEVLINK_ATTR_TRAP_POLICER_ID },
7076 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
7077 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
7078 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
7079 [DEVLINK_ATTR_PORT_TYPE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_PORT_TYPE_AUTO,
7080 DEVLINK_PORT_TYPE_IB),
7081 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
7082 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
7083 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
7084 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
7085 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
7086 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
7087 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
7088 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
7089 [DEVLINK_ATTR_ESWITCH_MODE] = NLA_POLICY_RANGE(NLA_U16, DEVLINK_ESWITCH_MODE_LEGACY,
7090 DEVLINK_ESWITCH_MODE_SWITCHDEV),
7091 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
7092 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
7093 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
7094 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
7095 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
7096 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
7097 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
7098 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
7099 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
7100 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
7101 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
7102 [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
7103 [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
7104 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
7105 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
7106 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
7107 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
7108 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
7109 [DEVLINK_ATTR_FLASH_UPDATE_OVERWRITE_MASK] =
7110 NLA_POLICY_BITFIELD32(DEVLINK_SUPPORTED_FLASH_OVERWRITE_SECTIONS),
7111 [DEVLINK_ATTR_TRAP_NAME] = { .type = NLA_NUL_STRING },
7112 [DEVLINK_ATTR_TRAP_ACTION] = { .type = NLA_U8 },
7113 [DEVLINK_ATTR_TRAP_GROUP_NAME] = { .type = NLA_NUL_STRING },
7114 [DEVLINK_ATTR_NETNS_PID] = { .type = NLA_U32 },
7115 [DEVLINK_ATTR_NETNS_FD] = { .type = NLA_U32 },
7116 [DEVLINK_ATTR_NETNS_ID] = { .type = NLA_U32 },
7117 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP] = { .type = NLA_U8 },
7118 [DEVLINK_ATTR_TRAP_POLICER_ID] = { .type = NLA_U32 },
7119 [DEVLINK_ATTR_TRAP_POLICER_RATE] = { .type = NLA_U64 },
7120 [DEVLINK_ATTR_TRAP_POLICER_BURST] = { .type = NLA_U64 },
7121 [DEVLINK_ATTR_PORT_FUNCTION] = { .type = NLA_NESTED },
7124 static const struct genl_ops devlink_nl_ops[] = {
7126 .cmd = DEVLINK_CMD_GET,
7127 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7128 .doit = devlink_nl_cmd_get_doit,
7129 .dumpit = devlink_nl_cmd_get_dumpit,
7130 /* can be retrieved by unprivileged users */
7133 .cmd = DEVLINK_CMD_PORT_GET,
7134 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7135 .doit = devlink_nl_cmd_port_get_doit,
7136 .dumpit = devlink_nl_cmd_port_get_dumpit,
7137 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7138 /* can be retrieved by unprivileged users */
7141 .cmd = DEVLINK_CMD_PORT_SET,
7142 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7143 .doit = devlink_nl_cmd_port_set_doit,
7144 .flags = GENL_ADMIN_PERM,
7145 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7148 .cmd = DEVLINK_CMD_PORT_SPLIT,
7149 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7150 .doit = devlink_nl_cmd_port_split_doit,
7151 .flags = GENL_ADMIN_PERM,
7152 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7155 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
7156 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7157 .doit = devlink_nl_cmd_port_unsplit_doit,
7158 .flags = GENL_ADMIN_PERM,
7159 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7162 .cmd = DEVLINK_CMD_SB_GET,
7163 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7164 .doit = devlink_nl_cmd_sb_get_doit,
7165 .dumpit = devlink_nl_cmd_sb_get_dumpit,
7166 /* can be retrieved by unprivileged users */
7169 .cmd = DEVLINK_CMD_SB_POOL_GET,
7170 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7171 .doit = devlink_nl_cmd_sb_pool_get_doit,
7172 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
7173 /* can be retrieved by unprivileged users */
7176 .cmd = DEVLINK_CMD_SB_POOL_SET,
7177 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7178 .doit = devlink_nl_cmd_sb_pool_set_doit,
7179 .flags = GENL_ADMIN_PERM,
7182 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
7183 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7184 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
7185 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
7186 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7187 /* can be retrieved by unprivileged users */
7190 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
7191 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7192 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
7193 .flags = GENL_ADMIN_PERM,
7194 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7197 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
7198 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7199 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
7200 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
7201 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7202 /* can be retrieved by unprivileged users */
7205 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
7206 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7207 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
7208 .flags = GENL_ADMIN_PERM,
7209 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7212 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
7213 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7214 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
7215 .flags = GENL_ADMIN_PERM,
7218 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
7219 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7220 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
7221 .flags = GENL_ADMIN_PERM,
7224 .cmd = DEVLINK_CMD_ESWITCH_GET,
7225 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7226 .doit = devlink_nl_cmd_eswitch_get_doit,
7227 .flags = GENL_ADMIN_PERM,
7228 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7231 .cmd = DEVLINK_CMD_ESWITCH_SET,
7232 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7233 .doit = devlink_nl_cmd_eswitch_set_doit,
7234 .flags = GENL_ADMIN_PERM,
7235 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7238 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
7239 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7240 .doit = devlink_nl_cmd_dpipe_table_get,
7241 /* can be retrieved by unprivileged users */
7244 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
7245 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7246 .doit = devlink_nl_cmd_dpipe_entries_get,
7247 /* can be retrieved by unprivileged users */
7250 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
7251 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7252 .doit = devlink_nl_cmd_dpipe_headers_get,
7253 /* can be retrieved by unprivileged users */
7256 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
7257 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7258 .doit = devlink_nl_cmd_dpipe_table_counters_set,
7259 .flags = GENL_ADMIN_PERM,
7262 .cmd = DEVLINK_CMD_RESOURCE_SET,
7263 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7264 .doit = devlink_nl_cmd_resource_set,
7265 .flags = GENL_ADMIN_PERM,
7268 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
7269 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7270 .doit = devlink_nl_cmd_resource_dump,
7271 /* can be retrieved by unprivileged users */
7274 .cmd = DEVLINK_CMD_RELOAD,
7275 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7276 .doit = devlink_nl_cmd_reload,
7277 .flags = GENL_ADMIN_PERM,
7278 .internal_flags = DEVLINK_NL_FLAG_NO_LOCK,
7281 .cmd = DEVLINK_CMD_PARAM_GET,
7282 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7283 .doit = devlink_nl_cmd_param_get_doit,
7284 .dumpit = devlink_nl_cmd_param_get_dumpit,
7285 /* can be retrieved by unprivileged users */
7288 .cmd = DEVLINK_CMD_PARAM_SET,
7289 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7290 .doit = devlink_nl_cmd_param_set_doit,
7291 .flags = GENL_ADMIN_PERM,
7294 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
7295 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7296 .doit = devlink_nl_cmd_port_param_get_doit,
7297 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
7298 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7299 /* can be retrieved by unprivileged users */
7302 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
7303 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7304 .doit = devlink_nl_cmd_port_param_set_doit,
7305 .flags = GENL_ADMIN_PERM,
7306 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
7309 .cmd = DEVLINK_CMD_REGION_GET,
7310 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7311 .doit = devlink_nl_cmd_region_get_doit,
7312 .dumpit = devlink_nl_cmd_region_get_dumpit,
7313 .flags = GENL_ADMIN_PERM,
7316 .cmd = DEVLINK_CMD_REGION_NEW,
7317 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7318 .doit = devlink_nl_cmd_region_new,
7319 .flags = GENL_ADMIN_PERM,
7322 .cmd = DEVLINK_CMD_REGION_DEL,
7323 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7324 .doit = devlink_nl_cmd_region_del,
7325 .flags = GENL_ADMIN_PERM,
7328 .cmd = DEVLINK_CMD_REGION_READ,
7329 .validate = GENL_DONT_VALIDATE_STRICT |
7330 GENL_DONT_VALIDATE_DUMP_STRICT,
7331 .dumpit = devlink_nl_cmd_region_read_dumpit,
7332 .flags = GENL_ADMIN_PERM,
7335 .cmd = DEVLINK_CMD_INFO_GET,
7336 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7337 .doit = devlink_nl_cmd_info_get_doit,
7338 .dumpit = devlink_nl_cmd_info_get_dumpit,
7339 /* can be retrieved by unprivileged users */
7342 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
7343 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7344 .doit = devlink_nl_cmd_health_reporter_get_doit,
7345 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
7346 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7347 DEVLINK_NL_FLAG_NO_LOCK,
7348 /* can be retrieved by unprivileged users */
7351 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
7352 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7353 .doit = devlink_nl_cmd_health_reporter_set_doit,
7354 .flags = GENL_ADMIN_PERM,
7355 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7356 DEVLINK_NL_FLAG_NO_LOCK,
7359 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
7360 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7361 .doit = devlink_nl_cmd_health_reporter_recover_doit,
7362 .flags = GENL_ADMIN_PERM,
7363 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7364 DEVLINK_NL_FLAG_NO_LOCK,
7367 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
7368 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7369 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
7370 .flags = GENL_ADMIN_PERM,
7371 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7372 DEVLINK_NL_FLAG_NO_LOCK,
7375 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
7376 .validate = GENL_DONT_VALIDATE_STRICT |
7377 GENL_DONT_VALIDATE_DUMP_STRICT,
7378 .dumpit = devlink_nl_cmd_health_reporter_dump_get_dumpit,
7379 .flags = GENL_ADMIN_PERM,
7380 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7381 DEVLINK_NL_FLAG_NO_LOCK,
7384 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
7385 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7386 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
7387 .flags = GENL_ADMIN_PERM,
7388 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7389 DEVLINK_NL_FLAG_NO_LOCK,
7392 .cmd = DEVLINK_CMD_HEALTH_REPORTER_TEST,
7393 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7394 .doit = devlink_nl_cmd_health_reporter_test_doit,
7395 .flags = GENL_ADMIN_PERM,
7396 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT |
7397 DEVLINK_NL_FLAG_NO_LOCK,
7400 .cmd = DEVLINK_CMD_FLASH_UPDATE,
7401 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
7402 .doit = devlink_nl_cmd_flash_update,
7403 .flags = GENL_ADMIN_PERM,
7406 .cmd = DEVLINK_CMD_TRAP_GET,
7407 .doit = devlink_nl_cmd_trap_get_doit,
7408 .dumpit = devlink_nl_cmd_trap_get_dumpit,
7409 /* can be retrieved by unprivileged users */
7412 .cmd = DEVLINK_CMD_TRAP_SET,
7413 .doit = devlink_nl_cmd_trap_set_doit,
7414 .flags = GENL_ADMIN_PERM,
7417 .cmd = DEVLINK_CMD_TRAP_GROUP_GET,
7418 .doit = devlink_nl_cmd_trap_group_get_doit,
7419 .dumpit = devlink_nl_cmd_trap_group_get_dumpit,
7420 /* can be retrieved by unprivileged users */
7423 .cmd = DEVLINK_CMD_TRAP_GROUP_SET,
7424 .doit = devlink_nl_cmd_trap_group_set_doit,
7425 .flags = GENL_ADMIN_PERM,
7428 .cmd = DEVLINK_CMD_TRAP_POLICER_GET,
7429 .doit = devlink_nl_cmd_trap_policer_get_doit,
7430 .dumpit = devlink_nl_cmd_trap_policer_get_dumpit,
7431 /* can be retrieved by unprivileged users */
7434 .cmd = DEVLINK_CMD_TRAP_POLICER_SET,
7435 .doit = devlink_nl_cmd_trap_policer_set_doit,
7436 .flags = GENL_ADMIN_PERM,
7440 static struct genl_family devlink_nl_family __ro_after_init = {
7441 .name = DEVLINK_GENL_NAME,
7442 .version = DEVLINK_GENL_VERSION,
7443 .maxattr = DEVLINK_ATTR_MAX,
7444 .policy = devlink_nl_policy,
7446 .pre_doit = devlink_nl_pre_doit,
7447 .post_doit = devlink_nl_post_doit,
7448 .module = THIS_MODULE,
7449 .ops = devlink_nl_ops,
7450 .n_ops = ARRAY_SIZE(devlink_nl_ops),
7451 .mcgrps = devlink_nl_mcgrps,
7452 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
7456 * devlink_alloc - Allocate new devlink instance resources
7459 * @priv_size: size of user private data
7461 * Allocate new devlink instance resources, including devlink index
7464 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
7466 struct devlink *devlink;
7471 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
7475 xa_init_flags(&devlink->snapshot_ids, XA_FLAGS_ALLOC);
7476 __devlink_net_set(devlink, &init_net);
7477 INIT_LIST_HEAD(&devlink->port_list);
7478 INIT_LIST_HEAD(&devlink->sb_list);
7479 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
7480 INIT_LIST_HEAD(&devlink->resource_list);
7481 INIT_LIST_HEAD(&devlink->param_list);
7482 INIT_LIST_HEAD(&devlink->region_list);
7483 INIT_LIST_HEAD(&devlink->reporter_list);
7484 INIT_LIST_HEAD(&devlink->trap_list);
7485 INIT_LIST_HEAD(&devlink->trap_group_list);
7486 INIT_LIST_HEAD(&devlink->trap_policer_list);
7487 mutex_init(&devlink->lock);
7488 mutex_init(&devlink->reporters_lock);
7491 EXPORT_SYMBOL_GPL(devlink_alloc);
7494 * devlink_register - Register devlink instance
7497 * @dev: parent device
7499 int devlink_register(struct devlink *devlink, struct device *dev)
7502 devlink->registered = true;
7503 mutex_lock(&devlink_mutex);
7504 list_add_tail(&devlink->list, &devlink_list);
7505 devlink_notify(devlink, DEVLINK_CMD_NEW);
7506 mutex_unlock(&devlink_mutex);
7509 EXPORT_SYMBOL_GPL(devlink_register);
7512 * devlink_unregister - Unregister devlink instance
7516 void devlink_unregister(struct devlink *devlink)
7518 mutex_lock(&devlink_mutex);
7519 WARN_ON(devlink_reload_supported(devlink) &&
7520 devlink->reload_enabled);
7521 devlink_notify(devlink, DEVLINK_CMD_DEL);
7522 list_del(&devlink->list);
7523 mutex_unlock(&devlink_mutex);
7525 EXPORT_SYMBOL_GPL(devlink_unregister);
7528 * devlink_reload_enable - Enable reload of devlink instance
7532 * Should be called at end of device initialization
7533 * process when reload operation is supported.
7535 void devlink_reload_enable(struct devlink *devlink)
7537 mutex_lock(&devlink_mutex);
7538 devlink->reload_enabled = true;
7539 mutex_unlock(&devlink_mutex);
7541 EXPORT_SYMBOL_GPL(devlink_reload_enable);
7544 * devlink_reload_disable - Disable reload of devlink instance
7548 * Should be called at the beginning of device cleanup
7549 * process when reload operation is supported.
7551 void devlink_reload_disable(struct devlink *devlink)
7553 mutex_lock(&devlink_mutex);
7554 /* Mutex is taken which ensures that no reload operation is in
7555 * progress while setting up forbidded flag.
7557 devlink->reload_enabled = false;
7558 mutex_unlock(&devlink_mutex);
7560 EXPORT_SYMBOL_GPL(devlink_reload_disable);
7563 * devlink_free - Free devlink instance resources
7567 void devlink_free(struct devlink *devlink)
7569 mutex_destroy(&devlink->reporters_lock);
7570 mutex_destroy(&devlink->lock);
7571 WARN_ON(!list_empty(&devlink->trap_policer_list));
7572 WARN_ON(!list_empty(&devlink->trap_group_list));
7573 WARN_ON(!list_empty(&devlink->trap_list));
7574 WARN_ON(!list_empty(&devlink->reporter_list));
7575 WARN_ON(!list_empty(&devlink->region_list));
7576 WARN_ON(!list_empty(&devlink->param_list));
7577 WARN_ON(!list_empty(&devlink->resource_list));
7578 WARN_ON(!list_empty(&devlink->dpipe_table_list));
7579 WARN_ON(!list_empty(&devlink->sb_list));
7580 WARN_ON(!list_empty(&devlink->port_list));
7582 xa_destroy(&devlink->snapshot_ids);
7586 EXPORT_SYMBOL_GPL(devlink_free);
7588 static void devlink_port_type_warn(struct work_struct *work)
7590 WARN(true, "Type was not set for devlink port.");
7593 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
7595 /* Ignore CPU and DSA flavours. */
7596 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU &&
7597 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA;
7600 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
7602 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
7604 if (!devlink_port_type_should_warn(devlink_port))
7606 /* Schedule a work to WARN in case driver does not set port
7607 * type within timeout.
7609 schedule_delayed_work(&devlink_port->type_warn_dw,
7610 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
7613 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
7615 if (!devlink_port_type_should_warn(devlink_port))
7617 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
7621 * devlink_port_register - Register devlink port
7624 * @devlink_port: devlink port
7625 * @port_index: driver-specific numerical identifier of the port
7627 * Register devlink port with provided port index. User can use
7628 * any indexing, even hw-related one. devlink_port structure
7629 * is convenient to be embedded inside user driver private structure.
7630 * Note that the caller should take care of zeroing the devlink_port
7633 int devlink_port_register(struct devlink *devlink,
7634 struct devlink_port *devlink_port,
7635 unsigned int port_index)
7637 mutex_lock(&devlink->lock);
7638 if (devlink_port_index_exists(devlink, port_index)) {
7639 mutex_unlock(&devlink->lock);
7642 devlink_port->devlink = devlink;
7643 devlink_port->index = port_index;
7644 devlink_port->registered = true;
7645 spin_lock_init(&devlink_port->type_lock);
7646 INIT_LIST_HEAD(&devlink_port->reporter_list);
7647 mutex_init(&devlink_port->reporters_lock);
7648 list_add_tail(&devlink_port->list, &devlink->port_list);
7649 INIT_LIST_HEAD(&devlink_port->param_list);
7650 mutex_unlock(&devlink->lock);
7651 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn);
7652 devlink_port_type_warn_schedule(devlink_port);
7653 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
7656 EXPORT_SYMBOL_GPL(devlink_port_register);
7659 * devlink_port_unregister - Unregister devlink port
7661 * @devlink_port: devlink port
7663 void devlink_port_unregister(struct devlink_port *devlink_port)
7665 struct devlink *devlink = devlink_port->devlink;
7667 devlink_port_type_warn_cancel(devlink_port);
7668 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
7669 mutex_lock(&devlink->lock);
7670 list_del(&devlink_port->list);
7671 mutex_unlock(&devlink->lock);
7672 WARN_ON(!list_empty(&devlink_port->reporter_list));
7673 mutex_destroy(&devlink_port->reporters_lock);
7675 EXPORT_SYMBOL_GPL(devlink_port_unregister);
7677 static void __devlink_port_type_set(struct devlink_port *devlink_port,
7678 enum devlink_port_type type,
7681 if (WARN_ON(!devlink_port->registered))
7683 devlink_port_type_warn_cancel(devlink_port);
7684 spin_lock_bh(&devlink_port->type_lock);
7685 devlink_port->type = type;
7686 devlink_port->type_dev = type_dev;
7687 spin_unlock_bh(&devlink_port->type_lock);
7688 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
7691 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
7692 struct net_device *netdev)
7694 const struct net_device_ops *ops = netdev->netdev_ops;
7696 /* If driver registers devlink port, it should set devlink port
7697 * attributes accordingly so the compat functions are called
7698 * and the original ops are not used.
7700 if (ops->ndo_get_phys_port_name) {
7701 /* Some drivers use the same set of ndos for netdevs
7702 * that have devlink_port registered and also for
7703 * those who don't. Make sure that ndo_get_phys_port_name
7704 * returns -EOPNOTSUPP here in case it is defined.
7707 char name[IFNAMSIZ];
7710 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
7711 WARN_ON(err != -EOPNOTSUPP);
7713 if (ops->ndo_get_port_parent_id) {
7714 /* Some drivers use the same set of ndos for netdevs
7715 * that have devlink_port registered and also for
7716 * those who don't. Make sure that ndo_get_port_parent_id
7717 * returns -EOPNOTSUPP here in case it is defined.
7720 struct netdev_phys_item_id ppid;
7723 err = ops->ndo_get_port_parent_id(netdev, &ppid);
7724 WARN_ON(err != -EOPNOTSUPP);
7729 * devlink_port_type_eth_set - Set port type to Ethernet
7731 * @devlink_port: devlink port
7732 * @netdev: related netdevice
7734 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
7735 struct net_device *netdev)
7738 devlink_port_type_netdev_checks(devlink_port, netdev);
7740 dev_warn(devlink_port->devlink->dev,
7741 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n",
7742 devlink_port->index);
7744 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
7746 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
7749 * devlink_port_type_ib_set - Set port type to InfiniBand
7751 * @devlink_port: devlink port
7752 * @ibdev: related IB device
7754 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
7755 struct ib_device *ibdev)
7757 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
7759 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
7762 * devlink_port_type_clear - Clear port type
7764 * @devlink_port: devlink port
7766 void devlink_port_type_clear(struct devlink_port *devlink_port)
7768 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
7769 devlink_port_type_warn_schedule(devlink_port);
7771 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
7773 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
7774 enum devlink_port_flavour flavour)
7776 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7778 if (WARN_ON(devlink_port->registered))
7780 devlink_port->attrs_set = true;
7781 attrs->flavour = flavour;
7782 if (attrs->switch_id.id_len) {
7783 devlink_port->switch_port = true;
7784 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN))
7785 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN;
7787 devlink_port->switch_port = false;
7793 * devlink_port_attrs_set - Set port attributes
7795 * @devlink_port: devlink port
7796 * @attrs: devlink port attrs
7798 void devlink_port_attrs_set(struct devlink_port *devlink_port,
7799 struct devlink_port_attrs *attrs)
7803 devlink_port->attrs = *attrs;
7804 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
7807 WARN_ON(attrs->splittable && attrs->split);
7809 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
7812 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
7814 * @devlink_port: devlink port
7815 * @controller: associated controller number for the devlink port instance
7816 * @pf: associated PF for the devlink port instance
7817 * @external: indicates if the port is for an external controller
7819 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
7820 u16 pf, bool external)
7822 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7825 ret = __devlink_port_attrs_set(devlink_port,
7826 DEVLINK_PORT_FLAVOUR_PCI_PF);
7829 attrs->pci_pf.controller = controller;
7830 attrs->pci_pf.pf = pf;
7831 attrs->pci_pf.external = external;
7833 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
7836 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
7838 * @devlink_port: devlink port
7839 * @controller: associated controller number for the devlink port instance
7840 * @pf: associated PF for the devlink port instance
7841 * @vf: associated VF of a PF for the devlink port instance
7842 * @external: indicates if the port is for an external controller
7844 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
7845 u16 pf, u16 vf, bool external)
7847 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7850 ret = __devlink_port_attrs_set(devlink_port,
7851 DEVLINK_PORT_FLAVOUR_PCI_VF);
7854 attrs->pci_vf.controller = controller;
7855 attrs->pci_vf.pf = pf;
7856 attrs->pci_vf.vf = vf;
7857 attrs->pci_vf.external = external;
7859 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
7861 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
7862 char *name, size_t len)
7864 struct devlink_port_attrs *attrs = &devlink_port->attrs;
7867 if (!devlink_port->attrs_set)
7870 switch (attrs->flavour) {
7871 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
7872 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
7874 n = snprintf(name, len, "p%u", attrs->phys.port_number);
7876 n = snprintf(name, len, "p%us%u",
7877 attrs->phys.port_number,
7878 attrs->phys.split_subport_number);
7880 case DEVLINK_PORT_FLAVOUR_CPU:
7881 case DEVLINK_PORT_FLAVOUR_DSA:
7882 /* As CPU and DSA ports do not have a netdevice associated
7883 * case should not ever happen.
7887 case DEVLINK_PORT_FLAVOUR_PCI_PF:
7888 if (attrs->pci_pf.external) {
7889 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
7895 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
7897 case DEVLINK_PORT_FLAVOUR_PCI_VF:
7898 if (attrs->pci_vf.external) {
7899 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
7905 n = snprintf(name, len, "pf%uvf%u",
7906 attrs->pci_vf.pf, attrs->pci_vf.vf);
7916 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
7917 u32 size, u16 ingress_pools_count,
7918 u16 egress_pools_count, u16 ingress_tc_count,
7919 u16 egress_tc_count)
7921 struct devlink_sb *devlink_sb;
7924 mutex_lock(&devlink->lock);
7925 if (devlink_sb_index_exists(devlink, sb_index)) {
7930 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
7935 devlink_sb->index = sb_index;
7936 devlink_sb->size = size;
7937 devlink_sb->ingress_pools_count = ingress_pools_count;
7938 devlink_sb->egress_pools_count = egress_pools_count;
7939 devlink_sb->ingress_tc_count = ingress_tc_count;
7940 devlink_sb->egress_tc_count = egress_tc_count;
7941 list_add_tail(&devlink_sb->list, &devlink->sb_list);
7943 mutex_unlock(&devlink->lock);
7946 EXPORT_SYMBOL_GPL(devlink_sb_register);
7948 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
7950 struct devlink_sb *devlink_sb;
7952 mutex_lock(&devlink->lock);
7953 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
7954 WARN_ON(!devlink_sb);
7955 list_del(&devlink_sb->list);
7956 mutex_unlock(&devlink->lock);
7959 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
7962 * devlink_dpipe_headers_register - register dpipe headers
7965 * @dpipe_headers: dpipe header array
7967 * Register the headers supported by hardware.
7969 int devlink_dpipe_headers_register(struct devlink *devlink,
7970 struct devlink_dpipe_headers *dpipe_headers)
7972 mutex_lock(&devlink->lock);
7973 devlink->dpipe_headers = dpipe_headers;
7974 mutex_unlock(&devlink->lock);
7977 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
7980 * devlink_dpipe_headers_unregister - unregister dpipe headers
7984 * Unregister the headers supported by hardware.
7986 void devlink_dpipe_headers_unregister(struct devlink *devlink)
7988 mutex_lock(&devlink->lock);
7989 devlink->dpipe_headers = NULL;
7990 mutex_unlock(&devlink->lock);
7992 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
7995 * devlink_dpipe_table_counter_enabled - check if counter allocation
7998 * @table_name: tables name
8000 * Used by driver to check if counter allocation is required.
8001 * After counter allocation is turned on the table entries
8002 * are updated to include counter statistics.
8004 * After that point on the driver must respect the counter
8005 * state so that each entry added to the table is added
8008 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
8009 const char *table_name)
8011 struct devlink_dpipe_table *table;
8015 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8016 table_name, devlink);
8019 enabled = table->counters_enabled;
8023 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
8026 * devlink_dpipe_table_register - register dpipe table
8029 * @table_name: table name
8030 * @table_ops: table ops
8032 * @counter_control_extern: external control for counters
8034 int devlink_dpipe_table_register(struct devlink *devlink,
8035 const char *table_name,
8036 struct devlink_dpipe_table_ops *table_ops,
8037 void *priv, bool counter_control_extern)
8039 struct devlink_dpipe_table *table;
8042 if (WARN_ON(!table_ops->size_get))
8045 mutex_lock(&devlink->lock);
8047 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name,
8053 table = kzalloc(sizeof(*table), GFP_KERNEL);
8059 table->name = table_name;
8060 table->table_ops = table_ops;
8062 table->counter_control_extern = counter_control_extern;
8064 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
8066 mutex_unlock(&devlink->lock);
8069 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
8072 * devlink_dpipe_table_unregister - unregister dpipe table
8075 * @table_name: table name
8077 void devlink_dpipe_table_unregister(struct devlink *devlink,
8078 const char *table_name)
8080 struct devlink_dpipe_table *table;
8082 mutex_lock(&devlink->lock);
8083 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8084 table_name, devlink);
8087 list_del_rcu(&table->list);
8088 mutex_unlock(&devlink->lock);
8089 kfree_rcu(table, rcu);
8092 mutex_unlock(&devlink->lock);
8094 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
8097 * devlink_resource_register - devlink resource register
8100 * @resource_name: resource's name
8101 * @resource_size: resource's size
8102 * @resource_id: resource's id
8103 * @parent_resource_id: resource's parent id
8104 * @size_params: size parameters
8106 int devlink_resource_register(struct devlink *devlink,
8107 const char *resource_name,
8110 u64 parent_resource_id,
8111 const struct devlink_resource_size_params *size_params)
8113 struct devlink_resource *resource;
8114 struct list_head *resource_list;
8118 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
8120 mutex_lock(&devlink->lock);
8121 resource = devlink_resource_find(devlink, NULL, resource_id);
8127 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
8133 if (top_hierarchy) {
8134 resource_list = &devlink->resource_list;
8136 struct devlink_resource *parent_resource;
8138 parent_resource = devlink_resource_find(devlink, NULL,
8139 parent_resource_id);
8140 if (parent_resource) {
8141 resource_list = &parent_resource->resource_list;
8142 resource->parent = parent_resource;
8150 resource->name = resource_name;
8151 resource->size = resource_size;
8152 resource->size_new = resource_size;
8153 resource->id = resource_id;
8154 resource->size_valid = true;
8155 memcpy(&resource->size_params, size_params,
8156 sizeof(resource->size_params));
8157 INIT_LIST_HEAD(&resource->resource_list);
8158 list_add_tail(&resource->list, resource_list);
8160 mutex_unlock(&devlink->lock);
8163 EXPORT_SYMBOL_GPL(devlink_resource_register);
8166 * devlink_resources_unregister - free all resources
8169 * @resource: resource
8171 void devlink_resources_unregister(struct devlink *devlink,
8172 struct devlink_resource *resource)
8174 struct devlink_resource *tmp, *child_resource;
8175 struct list_head *resource_list;
8178 resource_list = &resource->resource_list;
8180 resource_list = &devlink->resource_list;
8183 mutex_lock(&devlink->lock);
8185 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
8186 devlink_resources_unregister(devlink, child_resource);
8187 list_del(&child_resource->list);
8188 kfree(child_resource);
8192 mutex_unlock(&devlink->lock);
8194 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
8197 * devlink_resource_size_get - get and update size
8200 * @resource_id: the requested resource id
8201 * @p_resource_size: ptr to update
8203 int devlink_resource_size_get(struct devlink *devlink,
8205 u64 *p_resource_size)
8207 struct devlink_resource *resource;
8210 mutex_lock(&devlink->lock);
8211 resource = devlink_resource_find(devlink, NULL, resource_id);
8216 *p_resource_size = resource->size_new;
8217 resource->size = resource->size_new;
8219 mutex_unlock(&devlink->lock);
8222 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
8225 * devlink_dpipe_table_resource_set - set the resource id
8228 * @table_name: table name
8229 * @resource_id: resource id
8230 * @resource_units: number of resource's units consumed per table's entry
8232 int devlink_dpipe_table_resource_set(struct devlink *devlink,
8233 const char *table_name, u64 resource_id,
8236 struct devlink_dpipe_table *table;
8239 mutex_lock(&devlink->lock);
8240 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
8241 table_name, devlink);
8246 table->resource_id = resource_id;
8247 table->resource_units = resource_units;
8248 table->resource_valid = true;
8250 mutex_unlock(&devlink->lock);
8253 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
8256 * devlink_resource_occ_get_register - register occupancy getter
8259 * @resource_id: resource id
8260 * @occ_get: occupancy getter callback
8261 * @occ_get_priv: occupancy getter callback priv
8263 void devlink_resource_occ_get_register(struct devlink *devlink,
8265 devlink_resource_occ_get_t *occ_get,
8268 struct devlink_resource *resource;
8270 mutex_lock(&devlink->lock);
8271 resource = devlink_resource_find(devlink, NULL, resource_id);
8272 if (WARN_ON(!resource))
8274 WARN_ON(resource->occ_get);
8276 resource->occ_get = occ_get;
8277 resource->occ_get_priv = occ_get_priv;
8279 mutex_unlock(&devlink->lock);
8281 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
8284 * devlink_resource_occ_get_unregister - unregister occupancy getter
8287 * @resource_id: resource id
8289 void devlink_resource_occ_get_unregister(struct devlink *devlink,
8292 struct devlink_resource *resource;
8294 mutex_lock(&devlink->lock);
8295 resource = devlink_resource_find(devlink, NULL, resource_id);
8296 if (WARN_ON(!resource))
8298 WARN_ON(!resource->occ_get);
8300 resource->occ_get = NULL;
8301 resource->occ_get_priv = NULL;
8303 mutex_unlock(&devlink->lock);
8305 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
8307 static int devlink_param_verify(const struct devlink_param *param)
8309 if (!param || !param->name || !param->supported_cmodes)
8312 return devlink_param_generic_verify(param);
8314 return devlink_param_driver_verify(param);
8317 static int __devlink_params_register(struct devlink *devlink,
8318 unsigned int port_index,
8319 struct list_head *param_list,
8320 const struct devlink_param *params,
8321 size_t params_count,
8322 enum devlink_command reg_cmd,
8323 enum devlink_command unreg_cmd)
8325 const struct devlink_param *param = params;
8329 mutex_lock(&devlink->lock);
8330 for (i = 0; i < params_count; i++, param++) {
8331 err = devlink_param_verify(param);
8335 err = devlink_param_register_one(devlink, port_index,
8336 param_list, param, reg_cmd);
8341 mutex_unlock(&devlink->lock);
8347 for (param--; i > 0; i--, param--)
8348 devlink_param_unregister_one(devlink, port_index, param_list,
8351 mutex_unlock(&devlink->lock);
8355 static void __devlink_params_unregister(struct devlink *devlink,
8356 unsigned int port_index,
8357 struct list_head *param_list,
8358 const struct devlink_param *params,
8359 size_t params_count,
8360 enum devlink_command cmd)
8362 const struct devlink_param *param = params;
8365 mutex_lock(&devlink->lock);
8366 for (i = 0; i < params_count; i++, param++)
8367 devlink_param_unregister_one(devlink, 0, param_list, param,
8369 mutex_unlock(&devlink->lock);
8373 * devlink_params_register - register configuration parameters
8376 * @params: configuration parameters array
8377 * @params_count: number of parameters provided
8379 * Register the configuration parameters supported by the driver.
8381 int devlink_params_register(struct devlink *devlink,
8382 const struct devlink_param *params,
8383 size_t params_count)
8385 return __devlink_params_register(devlink, 0, &devlink->param_list,
8386 params, params_count,
8387 DEVLINK_CMD_PARAM_NEW,
8388 DEVLINK_CMD_PARAM_DEL);
8390 EXPORT_SYMBOL_GPL(devlink_params_register);
8393 * devlink_params_unregister - unregister configuration parameters
8395 * @params: configuration parameters to unregister
8396 * @params_count: number of parameters provided
8398 void devlink_params_unregister(struct devlink *devlink,
8399 const struct devlink_param *params,
8400 size_t params_count)
8402 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
8403 params, params_count,
8404 DEVLINK_CMD_PARAM_DEL);
8406 EXPORT_SYMBOL_GPL(devlink_params_unregister);
8409 * devlink_params_publish - publish configuration parameters
8413 * Publish previously registered configuration parameters.
8415 void devlink_params_publish(struct devlink *devlink)
8417 struct devlink_param_item *param_item;
8419 list_for_each_entry(param_item, &devlink->param_list, list) {
8420 if (param_item->published)
8422 param_item->published = true;
8423 devlink_param_notify(devlink, 0, param_item,
8424 DEVLINK_CMD_PARAM_NEW);
8427 EXPORT_SYMBOL_GPL(devlink_params_publish);
8430 * devlink_params_unpublish - unpublish configuration parameters
8434 * Unpublish previously registered configuration parameters.
8436 void devlink_params_unpublish(struct devlink *devlink)
8438 struct devlink_param_item *param_item;
8440 list_for_each_entry(param_item, &devlink->param_list, list) {
8441 if (!param_item->published)
8443 param_item->published = false;
8444 devlink_param_notify(devlink, 0, param_item,
8445 DEVLINK_CMD_PARAM_DEL);
8448 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
8451 * devlink_port_params_register - register port configuration parameters
8453 * @devlink_port: devlink port
8454 * @params: configuration parameters array
8455 * @params_count: number of parameters provided
8457 * Register the configuration parameters supported by the port.
8459 int devlink_port_params_register(struct devlink_port *devlink_port,
8460 const struct devlink_param *params,
8461 size_t params_count)
8463 return __devlink_params_register(devlink_port->devlink,
8464 devlink_port->index,
8465 &devlink_port->param_list, params,
8467 DEVLINK_CMD_PORT_PARAM_NEW,
8468 DEVLINK_CMD_PORT_PARAM_DEL);
8470 EXPORT_SYMBOL_GPL(devlink_port_params_register);
8473 * devlink_port_params_unregister - unregister port configuration
8476 * @devlink_port: devlink port
8477 * @params: configuration parameters array
8478 * @params_count: number of parameters provided
8480 void devlink_port_params_unregister(struct devlink_port *devlink_port,
8481 const struct devlink_param *params,
8482 size_t params_count)
8484 return __devlink_params_unregister(devlink_port->devlink,
8485 devlink_port->index,
8486 &devlink_port->param_list,
8487 params, params_count,
8488 DEVLINK_CMD_PORT_PARAM_DEL);
8490 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
8493 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
8494 union devlink_param_value *init_val)
8496 struct devlink_param_item *param_item;
8498 param_item = devlink_param_find_by_id(param_list, param_id);
8502 if (!param_item->driverinit_value_valid ||
8503 !devlink_param_cmode_is_supported(param_item->param,
8504 DEVLINK_PARAM_CMODE_DRIVERINIT))
8507 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8508 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
8510 *init_val = param_item->driverinit_value;
8516 __devlink_param_driverinit_value_set(struct devlink *devlink,
8517 unsigned int port_index,
8518 struct list_head *param_list, u32 param_id,
8519 union devlink_param_value init_val,
8520 enum devlink_command cmd)
8522 struct devlink_param_item *param_item;
8524 param_item = devlink_param_find_by_id(param_list, param_id);
8528 if (!devlink_param_cmode_is_supported(param_item->param,
8529 DEVLINK_PARAM_CMODE_DRIVERINIT))
8532 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
8533 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
8535 param_item->driverinit_value = init_val;
8536 param_item->driverinit_value_valid = true;
8538 devlink_param_notify(devlink, port_index, param_item, cmd);
8543 * devlink_param_driverinit_value_get - get configuration parameter
8544 * value for driver initializing
8547 * @param_id: parameter ID
8548 * @init_val: value of parameter in driverinit configuration mode
8550 * This function should be used by the driver to get driverinit
8551 * configuration for initialization after reload command.
8553 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
8554 union devlink_param_value *init_val)
8556 if (!devlink_reload_supported(devlink))
8559 return __devlink_param_driverinit_value_get(&devlink->param_list,
8560 param_id, init_val);
8562 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
8565 * devlink_param_driverinit_value_set - set value of configuration
8566 * parameter for driverinit
8567 * configuration mode
8570 * @param_id: parameter ID
8571 * @init_val: value of parameter to set for driverinit configuration mode
8573 * This function should be used by the driver to set driverinit
8574 * configuration mode default value.
8576 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
8577 union devlink_param_value init_val)
8579 return __devlink_param_driverinit_value_set(devlink, 0,
8580 &devlink->param_list,
8582 DEVLINK_CMD_PARAM_NEW);
8584 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
8587 * devlink_port_param_driverinit_value_get - get configuration parameter
8588 * value for driver initializing
8590 * @devlink_port: devlink_port
8591 * @param_id: parameter ID
8592 * @init_val: value of parameter in driverinit configuration mode
8594 * This function should be used by the driver to get driverinit
8595 * configuration for initialization after reload command.
8597 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
8599 union devlink_param_value *init_val)
8601 struct devlink *devlink = devlink_port->devlink;
8603 if (!devlink_reload_supported(devlink))
8606 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
8607 param_id, init_val);
8609 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
8612 * devlink_port_param_driverinit_value_set - set value of configuration
8613 * parameter for driverinit
8614 * configuration mode
8616 * @devlink_port: devlink_port
8617 * @param_id: parameter ID
8618 * @init_val: value of parameter to set for driverinit configuration mode
8620 * This function should be used by the driver to set driverinit
8621 * configuration mode default value.
8623 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
8625 union devlink_param_value init_val)
8627 return __devlink_param_driverinit_value_set(devlink_port->devlink,
8628 devlink_port->index,
8629 &devlink_port->param_list,
8631 DEVLINK_CMD_PORT_PARAM_NEW);
8633 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
8636 * devlink_param_value_changed - notify devlink on a parameter's value
8637 * change. Should be called by the driver
8638 * right after the change.
8641 * @param_id: parameter ID
8643 * This function should be used by the driver to notify devlink on value
8644 * change, excluding driverinit configuration mode.
8645 * For driverinit configuration mode driver should use the function
8647 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
8649 struct devlink_param_item *param_item;
8651 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
8652 WARN_ON(!param_item);
8654 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
8656 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
8659 * devlink_port_param_value_changed - notify devlink on a parameter's value
8660 * change. Should be called by the driver
8661 * right after the change.
8663 * @devlink_port: devlink_port
8664 * @param_id: parameter ID
8666 * This function should be used by the driver to notify devlink on value
8667 * change, excluding driverinit configuration mode.
8668 * For driverinit configuration mode driver should use the function
8669 * devlink_port_param_driverinit_value_set() instead.
8671 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
8674 struct devlink_param_item *param_item;
8676 param_item = devlink_param_find_by_id(&devlink_port->param_list,
8678 WARN_ON(!param_item);
8680 devlink_param_notify(devlink_port->devlink, devlink_port->index,
8681 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
8683 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
8686 * devlink_param_value_str_fill - Safely fill-up the string preventing
8687 * from overflow of the preallocated buffer
8689 * @dst_val: destination devlink_param_value
8690 * @src: source buffer
8692 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
8697 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
8698 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
8700 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
8703 * devlink_region_create - create a new address region
8706 * @ops: region operations and name
8707 * @region_max_snapshots: Maximum supported number of snapshots for region
8708 * @region_size: size of region
8710 struct devlink_region *
8711 devlink_region_create(struct devlink *devlink,
8712 const struct devlink_region_ops *ops,
8713 u32 region_max_snapshots, u64 region_size)
8715 struct devlink_region *region;
8718 if (WARN_ON(!ops) || WARN_ON(!ops->destructor))
8719 return ERR_PTR(-EINVAL);
8721 mutex_lock(&devlink->lock);
8723 if (devlink_region_get_by_name(devlink, ops->name)) {
8728 region = kzalloc(sizeof(*region), GFP_KERNEL);
8734 region->devlink = devlink;
8735 region->max_snapshots = region_max_snapshots;
8737 region->size = region_size;
8738 INIT_LIST_HEAD(®ion->snapshot_list);
8739 list_add_tail(®ion->list, &devlink->region_list);
8740 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
8742 mutex_unlock(&devlink->lock);
8746 mutex_unlock(&devlink->lock);
8747 return ERR_PTR(err);
8749 EXPORT_SYMBOL_GPL(devlink_region_create);
8752 * devlink_region_destroy - destroy address region
8754 * @region: devlink region to destroy
8756 void devlink_region_destroy(struct devlink_region *region)
8758 struct devlink *devlink = region->devlink;
8759 struct devlink_snapshot *snapshot, *ts;
8761 mutex_lock(&devlink->lock);
8763 /* Free all snapshots of region */
8764 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
8765 devlink_region_snapshot_del(region, snapshot);
8767 list_del(®ion->list);
8769 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
8770 mutex_unlock(&devlink->lock);
8773 EXPORT_SYMBOL_GPL(devlink_region_destroy);
8776 * devlink_region_snapshot_id_get - get snapshot ID
8778 * This callback should be called when adding a new snapshot,
8779 * Driver should use the same id for multiple snapshots taken
8780 * on multiple regions at the same time/by the same trigger.
8782 * The caller of this function must use devlink_region_snapshot_id_put
8783 * when finished creating regions using this id.
8785 * Returns zero on success, or a negative error code on failure.
8788 * @id: storage to return id
8790 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id)
8794 mutex_lock(&devlink->lock);
8795 err = __devlink_region_snapshot_id_get(devlink, id);
8796 mutex_unlock(&devlink->lock);
8800 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_get);
8803 * devlink_region_snapshot_id_put - put snapshot ID reference
8805 * This should be called by a driver after finishing creating snapshots
8806 * with an id. Doing so ensures that the ID can later be released in the
8807 * event that all snapshots using it have been destroyed.
8810 * @id: id to release reference on
8812 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id)
8814 mutex_lock(&devlink->lock);
8815 __devlink_snapshot_id_decrement(devlink, id);
8816 mutex_unlock(&devlink->lock);
8818 EXPORT_SYMBOL_GPL(devlink_region_snapshot_id_put);
8821 * devlink_region_snapshot_create - create a new snapshot
8822 * This will add a new snapshot of a region. The snapshot
8823 * will be stored on the region struct and can be accessed
8824 * from devlink. This is useful for future analyses of snapshots.
8825 * Multiple snapshots can be created on a region.
8826 * The @snapshot_id should be obtained using the getter function.
8828 * @region: devlink region of the snapshot
8829 * @data: snapshot data
8830 * @snapshot_id: snapshot id to be created
8832 int devlink_region_snapshot_create(struct devlink_region *region,
8833 u8 *data, u32 snapshot_id)
8835 struct devlink *devlink = region->devlink;
8838 mutex_lock(&devlink->lock);
8839 err = __devlink_region_snapshot_create(region, data, snapshot_id);
8840 mutex_unlock(&devlink->lock);
8844 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
8846 #define DEVLINK_TRAP(_id, _type) \
8848 .type = DEVLINK_TRAP_TYPE_##_type, \
8849 .id = DEVLINK_TRAP_GENERIC_ID_##_id, \
8850 .name = DEVLINK_TRAP_GENERIC_NAME_##_id, \
8853 static const struct devlink_trap devlink_trap_generic[] = {
8854 DEVLINK_TRAP(SMAC_MC, DROP),
8855 DEVLINK_TRAP(VLAN_TAG_MISMATCH, DROP),
8856 DEVLINK_TRAP(INGRESS_VLAN_FILTER, DROP),
8857 DEVLINK_TRAP(INGRESS_STP_FILTER, DROP),
8858 DEVLINK_TRAP(EMPTY_TX_LIST, DROP),
8859 DEVLINK_TRAP(PORT_LOOPBACK_FILTER, DROP),
8860 DEVLINK_TRAP(BLACKHOLE_ROUTE, DROP),
8861 DEVLINK_TRAP(TTL_ERROR, EXCEPTION),
8862 DEVLINK_TRAP(TAIL_DROP, DROP),
8863 DEVLINK_TRAP(NON_IP_PACKET, DROP),
8864 DEVLINK_TRAP(UC_DIP_MC_DMAC, DROP),
8865 DEVLINK_TRAP(DIP_LB, DROP),
8866 DEVLINK_TRAP(SIP_MC, DROP),
8867 DEVLINK_TRAP(SIP_LB, DROP),
8868 DEVLINK_TRAP(CORRUPTED_IP_HDR, DROP),
8869 DEVLINK_TRAP(IPV4_SIP_BC, DROP),
8870 DEVLINK_TRAP(IPV6_MC_DIP_RESERVED_SCOPE, DROP),
8871 DEVLINK_TRAP(IPV6_MC_DIP_INTERFACE_LOCAL_SCOPE, DROP),
8872 DEVLINK_TRAP(MTU_ERROR, EXCEPTION),
8873 DEVLINK_TRAP(UNRESOLVED_NEIGH, EXCEPTION),
8874 DEVLINK_TRAP(RPF, EXCEPTION),
8875 DEVLINK_TRAP(REJECT_ROUTE, EXCEPTION),
8876 DEVLINK_TRAP(IPV4_LPM_UNICAST_MISS, EXCEPTION),
8877 DEVLINK_TRAP(IPV6_LPM_UNICAST_MISS, EXCEPTION),
8878 DEVLINK_TRAP(NON_ROUTABLE, DROP),
8879 DEVLINK_TRAP(DECAP_ERROR, EXCEPTION),
8880 DEVLINK_TRAP(OVERLAY_SMAC_MC, DROP),
8881 DEVLINK_TRAP(INGRESS_FLOW_ACTION_DROP, DROP),
8882 DEVLINK_TRAP(EGRESS_FLOW_ACTION_DROP, DROP),
8883 DEVLINK_TRAP(STP, CONTROL),
8884 DEVLINK_TRAP(LACP, CONTROL),
8885 DEVLINK_TRAP(LLDP, CONTROL),
8886 DEVLINK_TRAP(IGMP_QUERY, CONTROL),
8887 DEVLINK_TRAP(IGMP_V1_REPORT, CONTROL),
8888 DEVLINK_TRAP(IGMP_V2_REPORT, CONTROL),
8889 DEVLINK_TRAP(IGMP_V3_REPORT, CONTROL),
8890 DEVLINK_TRAP(IGMP_V2_LEAVE, CONTROL),
8891 DEVLINK_TRAP(MLD_QUERY, CONTROL),
8892 DEVLINK_TRAP(MLD_V1_REPORT, CONTROL),
8893 DEVLINK_TRAP(MLD_V2_REPORT, CONTROL),
8894 DEVLINK_TRAP(MLD_V1_DONE, CONTROL),
8895 DEVLINK_TRAP(IPV4_DHCP, CONTROL),
8896 DEVLINK_TRAP(IPV6_DHCP, CONTROL),
8897 DEVLINK_TRAP(ARP_REQUEST, CONTROL),
8898 DEVLINK_TRAP(ARP_RESPONSE, CONTROL),
8899 DEVLINK_TRAP(ARP_OVERLAY, CONTROL),
8900 DEVLINK_TRAP(IPV6_NEIGH_SOLICIT, CONTROL),
8901 DEVLINK_TRAP(IPV6_NEIGH_ADVERT, CONTROL),
8902 DEVLINK_TRAP(IPV4_BFD, CONTROL),
8903 DEVLINK_TRAP(IPV6_BFD, CONTROL),
8904 DEVLINK_TRAP(IPV4_OSPF, CONTROL),
8905 DEVLINK_TRAP(IPV6_OSPF, CONTROL),
8906 DEVLINK_TRAP(IPV4_BGP, CONTROL),
8907 DEVLINK_TRAP(IPV6_BGP, CONTROL),
8908 DEVLINK_TRAP(IPV4_VRRP, CONTROL),
8909 DEVLINK_TRAP(IPV6_VRRP, CONTROL),
8910 DEVLINK_TRAP(IPV4_PIM, CONTROL),
8911 DEVLINK_TRAP(IPV6_PIM, CONTROL),
8912 DEVLINK_TRAP(UC_LB, CONTROL),
8913 DEVLINK_TRAP(LOCAL_ROUTE, CONTROL),
8914 DEVLINK_TRAP(EXTERNAL_ROUTE, CONTROL),
8915 DEVLINK_TRAP(IPV6_UC_DIP_LINK_LOCAL_SCOPE, CONTROL),
8916 DEVLINK_TRAP(IPV6_DIP_ALL_NODES, CONTROL),
8917 DEVLINK_TRAP(IPV6_DIP_ALL_ROUTERS, CONTROL),
8918 DEVLINK_TRAP(IPV6_ROUTER_SOLICIT, CONTROL),
8919 DEVLINK_TRAP(IPV6_ROUTER_ADVERT, CONTROL),
8920 DEVLINK_TRAP(IPV6_REDIRECT, CONTROL),
8921 DEVLINK_TRAP(IPV4_ROUTER_ALERT, CONTROL),
8922 DEVLINK_TRAP(IPV6_ROUTER_ALERT, CONTROL),
8923 DEVLINK_TRAP(PTP_EVENT, CONTROL),
8924 DEVLINK_TRAP(PTP_GENERAL, CONTROL),
8925 DEVLINK_TRAP(FLOW_ACTION_SAMPLE, CONTROL),
8926 DEVLINK_TRAP(FLOW_ACTION_TRAP, CONTROL),
8927 DEVLINK_TRAP(EARLY_DROP, DROP),
8930 #define DEVLINK_TRAP_GROUP(_id) \
8932 .id = DEVLINK_TRAP_GROUP_GENERIC_ID_##_id, \
8933 .name = DEVLINK_TRAP_GROUP_GENERIC_NAME_##_id, \
8936 static const struct devlink_trap_group devlink_trap_group_generic[] = {
8937 DEVLINK_TRAP_GROUP(L2_DROPS),
8938 DEVLINK_TRAP_GROUP(L3_DROPS),
8939 DEVLINK_TRAP_GROUP(L3_EXCEPTIONS),
8940 DEVLINK_TRAP_GROUP(BUFFER_DROPS),
8941 DEVLINK_TRAP_GROUP(TUNNEL_DROPS),
8942 DEVLINK_TRAP_GROUP(ACL_DROPS),
8943 DEVLINK_TRAP_GROUP(STP),
8944 DEVLINK_TRAP_GROUP(LACP),
8945 DEVLINK_TRAP_GROUP(LLDP),
8946 DEVLINK_TRAP_GROUP(MC_SNOOPING),
8947 DEVLINK_TRAP_GROUP(DHCP),
8948 DEVLINK_TRAP_GROUP(NEIGH_DISCOVERY),
8949 DEVLINK_TRAP_GROUP(BFD),
8950 DEVLINK_TRAP_GROUP(OSPF),
8951 DEVLINK_TRAP_GROUP(BGP),
8952 DEVLINK_TRAP_GROUP(VRRP),
8953 DEVLINK_TRAP_GROUP(PIM),
8954 DEVLINK_TRAP_GROUP(UC_LB),
8955 DEVLINK_TRAP_GROUP(LOCAL_DELIVERY),
8956 DEVLINK_TRAP_GROUP(EXTERNAL_DELIVERY),
8957 DEVLINK_TRAP_GROUP(IPV6),
8958 DEVLINK_TRAP_GROUP(PTP_EVENT),
8959 DEVLINK_TRAP_GROUP(PTP_GENERAL),
8960 DEVLINK_TRAP_GROUP(ACL_SAMPLE),
8961 DEVLINK_TRAP_GROUP(ACL_TRAP),
8964 static int devlink_trap_generic_verify(const struct devlink_trap *trap)
8966 if (trap->id > DEVLINK_TRAP_GENERIC_ID_MAX)
8969 if (strcmp(trap->name, devlink_trap_generic[trap->id].name))
8972 if (trap->type != devlink_trap_generic[trap->id].type)
8978 static int devlink_trap_driver_verify(const struct devlink_trap *trap)
8982 if (trap->id <= DEVLINK_TRAP_GENERIC_ID_MAX)
8985 for (i = 0; i < ARRAY_SIZE(devlink_trap_generic); i++) {
8986 if (!strcmp(trap->name, devlink_trap_generic[i].name))
8993 static int devlink_trap_verify(const struct devlink_trap *trap)
8995 if (!trap || !trap->name)
8999 return devlink_trap_generic_verify(trap);
9001 return devlink_trap_driver_verify(trap);
9005 devlink_trap_group_generic_verify(const struct devlink_trap_group *group)
9007 if (group->id > DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9010 if (strcmp(group->name, devlink_trap_group_generic[group->id].name))
9017 devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
9021 if (group->id <= DEVLINK_TRAP_GROUP_GENERIC_ID_MAX)
9024 for (i = 0; i < ARRAY_SIZE(devlink_trap_group_generic); i++) {
9025 if (!strcmp(group->name, devlink_trap_group_generic[i].name))
9032 static int devlink_trap_group_verify(const struct devlink_trap_group *group)
9035 return devlink_trap_group_generic_verify(group);
9037 return devlink_trap_group_driver_verify(group);
9041 devlink_trap_group_notify(struct devlink *devlink,
9042 const struct devlink_trap_group_item *group_item,
9043 enum devlink_command cmd)
9045 struct sk_buff *msg;
9048 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_GROUP_NEW &&
9049 cmd != DEVLINK_CMD_TRAP_GROUP_DEL);
9051 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9055 err = devlink_nl_trap_group_fill(msg, devlink, group_item, cmd, 0, 0,
9062 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9063 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9067 devlink_trap_item_group_link(struct devlink *devlink,
9068 struct devlink_trap_item *trap_item)
9070 u16 group_id = trap_item->trap->init_group_id;
9071 struct devlink_trap_group_item *group_item;
9073 group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
9074 if (WARN_ON_ONCE(!group_item))
9077 trap_item->group_item = group_item;
9082 static void devlink_trap_notify(struct devlink *devlink,
9083 const struct devlink_trap_item *trap_item,
9084 enum devlink_command cmd)
9086 struct sk_buff *msg;
9089 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_NEW &&
9090 cmd != DEVLINK_CMD_TRAP_DEL);
9092 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9096 err = devlink_nl_trap_fill(msg, devlink, trap_item, cmd, 0, 0, 0);
9102 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9103 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9107 devlink_trap_register(struct devlink *devlink,
9108 const struct devlink_trap *trap, void *priv)
9110 struct devlink_trap_item *trap_item;
9113 if (devlink_trap_item_lookup(devlink, trap->name))
9116 trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
9120 trap_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9121 if (!trap_item->stats) {
9123 goto err_stats_alloc;
9126 trap_item->trap = trap;
9127 trap_item->action = trap->init_action;
9128 trap_item->priv = priv;
9130 err = devlink_trap_item_group_link(devlink, trap_item);
9132 goto err_group_link;
9134 err = devlink->ops->trap_init(devlink, trap, trap_item);
9138 list_add_tail(&trap_item->list, &devlink->trap_list);
9139 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);
9145 free_percpu(trap_item->stats);
9151 static void devlink_trap_unregister(struct devlink *devlink,
9152 const struct devlink_trap *trap)
9154 struct devlink_trap_item *trap_item;
9156 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9157 if (WARN_ON_ONCE(!trap_item))
9160 devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_DEL);
9161 list_del(&trap_item->list);
9162 if (devlink->ops->trap_fini)
9163 devlink->ops->trap_fini(devlink, trap, trap_item);
9164 free_percpu(trap_item->stats);
9168 static void devlink_trap_disable(struct devlink *devlink,
9169 const struct devlink_trap *trap)
9171 struct devlink_trap_item *trap_item;
9173 trap_item = devlink_trap_item_lookup(devlink, trap->name);
9174 if (WARN_ON_ONCE(!trap_item))
9177 devlink->ops->trap_action_set(devlink, trap, DEVLINK_TRAP_ACTION_DROP,
9179 trap_item->action = DEVLINK_TRAP_ACTION_DROP;
9183 * devlink_traps_register - Register packet traps with devlink.
9184 * @devlink: devlink.
9185 * @traps: Packet traps.
9186 * @traps_count: Count of provided packet traps.
9187 * @priv: Driver private information.
9189 * Return: Non-zero value on failure.
9191 int devlink_traps_register(struct devlink *devlink,
9192 const struct devlink_trap *traps,
9193 size_t traps_count, void *priv)
9197 if (!devlink->ops->trap_init || !devlink->ops->trap_action_set)
9200 mutex_lock(&devlink->lock);
9201 for (i = 0; i < traps_count; i++) {
9202 const struct devlink_trap *trap = &traps[i];
9204 err = devlink_trap_verify(trap);
9206 goto err_trap_verify;
9208 err = devlink_trap_register(devlink, trap, priv);
9210 goto err_trap_register;
9212 mutex_unlock(&devlink->lock);
9218 for (i--; i >= 0; i--)
9219 devlink_trap_unregister(devlink, &traps[i]);
9220 mutex_unlock(&devlink->lock);
9223 EXPORT_SYMBOL_GPL(devlink_traps_register);
9226 * devlink_traps_unregister - Unregister packet traps from devlink.
9227 * @devlink: devlink.
9228 * @traps: Packet traps.
9229 * @traps_count: Count of provided packet traps.
9231 void devlink_traps_unregister(struct devlink *devlink,
9232 const struct devlink_trap *traps,
9237 mutex_lock(&devlink->lock);
9238 /* Make sure we do not have any packets in-flight while unregistering
9239 * traps by disabling all of them and waiting for a grace period.
9241 for (i = traps_count - 1; i >= 0; i--)
9242 devlink_trap_disable(devlink, &traps[i]);
9244 for (i = traps_count - 1; i >= 0; i--)
9245 devlink_trap_unregister(devlink, &traps[i]);
9246 mutex_unlock(&devlink->lock);
9248 EXPORT_SYMBOL_GPL(devlink_traps_unregister);
9251 devlink_trap_stats_update(struct devlink_stats __percpu *trap_stats,
9254 struct devlink_stats *stats;
9256 stats = this_cpu_ptr(trap_stats);
9257 u64_stats_update_begin(&stats->syncp);
9258 stats->rx_bytes += skb_len;
9259 stats->rx_packets++;
9260 u64_stats_update_end(&stats->syncp);
9264 devlink_trap_report_metadata_set(struct devlink_trap_metadata *metadata,
9265 const struct devlink_trap_item *trap_item,
9266 struct devlink_port *in_devlink_port,
9267 const struct flow_action_cookie *fa_cookie)
9269 metadata->trap_name = trap_item->trap->name;
9270 metadata->trap_group_name = trap_item->group_item->group->name;
9271 metadata->fa_cookie = fa_cookie;
9273 spin_lock(&in_devlink_port->type_lock);
9274 if (in_devlink_port->type == DEVLINK_PORT_TYPE_ETH)
9275 metadata->input_dev = in_devlink_port->type_dev;
9276 spin_unlock(&in_devlink_port->type_lock);
9280 * devlink_trap_report - Report trapped packet to drop monitor.
9281 * @devlink: devlink.
9282 * @skb: Trapped packet.
9283 * @trap_ctx: Trap context.
9284 * @in_devlink_port: Input devlink port.
9285 * @fa_cookie: Flow action cookie. Could be NULL.
9287 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
9288 void *trap_ctx, struct devlink_port *in_devlink_port,
9289 const struct flow_action_cookie *fa_cookie)
9292 struct devlink_trap_item *trap_item = trap_ctx;
9294 devlink_trap_stats_update(trap_item->stats, skb->len);
9295 devlink_trap_stats_update(trap_item->group_item->stats, skb->len);
9297 /* Control packets were not dropped by the device or encountered an
9298 * exception during forwarding and therefore should not be reported to
9299 * the kernel's drop monitor.
9301 if (trap_item->trap->type == DEVLINK_TRAP_TYPE_CONTROL)
9304 if (trace_devlink_trap_report_enabled()) {
9305 struct devlink_trap_metadata metadata = {};
9307 devlink_trap_report_metadata_set(&metadata, trap_item,
9308 in_devlink_port, fa_cookie);
9309 trace_devlink_trap_report(devlink, skb, &metadata);
9312 EXPORT_SYMBOL_GPL(devlink_trap_report);
9315 * devlink_trap_ctx_priv - Trap context to driver private information.
9316 * @trap_ctx: Trap context.
9318 * Return: Driver private information passed during registration.
9320 void *devlink_trap_ctx_priv(void *trap_ctx)
9322 struct devlink_trap_item *trap_item = trap_ctx;
9324 return trap_item->priv;
9326 EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);
9329 devlink_trap_group_item_policer_link(struct devlink *devlink,
9330 struct devlink_trap_group_item *group_item)
9332 u32 policer_id = group_item->group->init_policer_id;
9333 struct devlink_trap_policer_item *policer_item;
9335 if (policer_id == 0)
9338 policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
9339 if (WARN_ON_ONCE(!policer_item))
9342 group_item->policer_item = policer_item;
9348 devlink_trap_group_register(struct devlink *devlink,
9349 const struct devlink_trap_group *group)
9351 struct devlink_trap_group_item *group_item;
9354 if (devlink_trap_group_item_lookup(devlink, group->name))
9357 group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
9361 group_item->stats = netdev_alloc_pcpu_stats(struct devlink_stats);
9362 if (!group_item->stats) {
9364 goto err_stats_alloc;
9367 group_item->group = group;
9369 err = devlink_trap_group_item_policer_link(devlink, group_item);
9371 goto err_policer_link;
9373 if (devlink->ops->trap_group_init) {
9374 err = devlink->ops->trap_group_init(devlink, group);
9376 goto err_group_init;
9379 list_add_tail(&group_item->list, &devlink->trap_group_list);
9380 devlink_trap_group_notify(devlink, group_item,
9381 DEVLINK_CMD_TRAP_GROUP_NEW);
9387 free_percpu(group_item->stats);
9394 devlink_trap_group_unregister(struct devlink *devlink,
9395 const struct devlink_trap_group *group)
9397 struct devlink_trap_group_item *group_item;
9399 group_item = devlink_trap_group_item_lookup(devlink, group->name);
9400 if (WARN_ON_ONCE(!group_item))
9403 devlink_trap_group_notify(devlink, group_item,
9404 DEVLINK_CMD_TRAP_GROUP_DEL);
9405 list_del(&group_item->list);
9406 free_percpu(group_item->stats);
9411 * devlink_trap_groups_register - Register packet trap groups with devlink.
9412 * @devlink: devlink.
9413 * @groups: Packet trap groups.
9414 * @groups_count: Count of provided packet trap groups.
9416 * Return: Non-zero value on failure.
9418 int devlink_trap_groups_register(struct devlink *devlink,
9419 const struct devlink_trap_group *groups,
9420 size_t groups_count)
9424 mutex_lock(&devlink->lock);
9425 for (i = 0; i < groups_count; i++) {
9426 const struct devlink_trap_group *group = &groups[i];
9428 err = devlink_trap_group_verify(group);
9430 goto err_trap_group_verify;
9432 err = devlink_trap_group_register(devlink, group);
9434 goto err_trap_group_register;
9436 mutex_unlock(&devlink->lock);
9440 err_trap_group_register:
9441 err_trap_group_verify:
9442 for (i--; i >= 0; i--)
9443 devlink_trap_group_unregister(devlink, &groups[i]);
9444 mutex_unlock(&devlink->lock);
9447 EXPORT_SYMBOL_GPL(devlink_trap_groups_register);
9450 * devlink_trap_groups_unregister - Unregister packet trap groups from devlink.
9451 * @devlink: devlink.
9452 * @groups: Packet trap groups.
9453 * @groups_count: Count of provided packet trap groups.
9455 void devlink_trap_groups_unregister(struct devlink *devlink,
9456 const struct devlink_trap_group *groups,
9457 size_t groups_count)
9461 mutex_lock(&devlink->lock);
9462 for (i = groups_count - 1; i >= 0; i--)
9463 devlink_trap_group_unregister(devlink, &groups[i]);
9464 mutex_unlock(&devlink->lock);
9466 EXPORT_SYMBOL_GPL(devlink_trap_groups_unregister);
9469 devlink_trap_policer_notify(struct devlink *devlink,
9470 const struct devlink_trap_policer_item *policer_item,
9471 enum devlink_command cmd)
9473 struct sk_buff *msg;
9476 WARN_ON_ONCE(cmd != DEVLINK_CMD_TRAP_POLICER_NEW &&
9477 cmd != DEVLINK_CMD_TRAP_POLICER_DEL);
9479 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9483 err = devlink_nl_trap_policer_fill(msg, devlink, policer_item, cmd, 0,
9490 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
9491 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
9495 devlink_trap_policer_register(struct devlink *devlink,
9496 const struct devlink_trap_policer *policer)
9498 struct devlink_trap_policer_item *policer_item;
9501 if (devlink_trap_policer_item_lookup(devlink, policer->id))
9504 policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
9508 policer_item->policer = policer;
9509 policer_item->rate = policer->init_rate;
9510 policer_item->burst = policer->init_burst;
9512 if (devlink->ops->trap_policer_init) {
9513 err = devlink->ops->trap_policer_init(devlink, policer);
9515 goto err_policer_init;
9518 list_add_tail(&policer_item->list, &devlink->trap_policer_list);
9519 devlink_trap_policer_notify(devlink, policer_item,
9520 DEVLINK_CMD_TRAP_POLICER_NEW);
9525 kfree(policer_item);
9530 devlink_trap_policer_unregister(struct devlink *devlink,
9531 const struct devlink_trap_policer *policer)
9533 struct devlink_trap_policer_item *policer_item;
9535 policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
9536 if (WARN_ON_ONCE(!policer_item))
9539 devlink_trap_policer_notify(devlink, policer_item,
9540 DEVLINK_CMD_TRAP_POLICER_DEL);
9541 list_del(&policer_item->list);
9542 if (devlink->ops->trap_policer_fini)
9543 devlink->ops->trap_policer_fini(devlink, policer);
9544 kfree(policer_item);
9548 * devlink_trap_policers_register - Register packet trap policers with devlink.
9549 * @devlink: devlink.
9550 * @policers: Packet trap policers.
9551 * @policers_count: Count of provided packet trap policers.
9553 * Return: Non-zero value on failure.
9556 devlink_trap_policers_register(struct devlink *devlink,
9557 const struct devlink_trap_policer *policers,
9558 size_t policers_count)
9562 mutex_lock(&devlink->lock);
9563 for (i = 0; i < policers_count; i++) {
9564 const struct devlink_trap_policer *policer = &policers[i];
9566 if (WARN_ON(policer->id == 0 ||
9567 policer->max_rate < policer->min_rate ||
9568 policer->max_burst < policer->min_burst)) {
9570 goto err_trap_policer_verify;
9573 err = devlink_trap_policer_register(devlink, policer);
9575 goto err_trap_policer_register;
9577 mutex_unlock(&devlink->lock);
9581 err_trap_policer_register:
9582 err_trap_policer_verify:
9583 for (i--; i >= 0; i--)
9584 devlink_trap_policer_unregister(devlink, &policers[i]);
9585 mutex_unlock(&devlink->lock);
9588 EXPORT_SYMBOL_GPL(devlink_trap_policers_register);
9591 * devlink_trap_policers_unregister - Unregister packet trap policers from devlink.
9592 * @devlink: devlink.
9593 * @policers: Packet trap policers.
9594 * @policers_count: Count of provided packet trap policers.
9597 devlink_trap_policers_unregister(struct devlink *devlink,
9598 const struct devlink_trap_policer *policers,
9599 size_t policers_count)
9603 mutex_lock(&devlink->lock);
9604 for (i = policers_count - 1; i >= 0; i--)
9605 devlink_trap_policer_unregister(devlink, &policers[i]);
9606 mutex_unlock(&devlink->lock);
9608 EXPORT_SYMBOL_GPL(devlink_trap_policers_unregister);
9610 static void __devlink_compat_running_version(struct devlink *devlink,
9611 char *buf, size_t len)
9613 const struct nlattr *nlattr;
9614 struct devlink_info_req req;
9615 struct sk_buff *msg;
9618 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9623 err = devlink->ops->info_get(devlink, &req, NULL);
9627 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
9628 const struct nlattr *kv;
9631 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
9634 nla_for_each_nested(kv, nlattr, rem_kv) {
9635 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
9638 strlcat(buf, nla_data(kv), len);
9639 strlcat(buf, " ", len);
9646 void devlink_compat_running_version(struct net_device *dev,
9647 char *buf, size_t len)
9649 struct devlink *devlink;
9654 devlink = netdev_to_devlink(dev);
9655 if (!devlink || !devlink->ops->info_get)
9658 mutex_lock(&devlink->lock);
9659 __devlink_compat_running_version(devlink, buf, len);
9660 mutex_unlock(&devlink->lock);
9667 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
9669 struct devlink_flash_update_params params = {};
9670 struct devlink *devlink;
9676 devlink = netdev_to_devlink(dev);
9677 if (!devlink || !devlink->ops->flash_update) {
9682 params.file_name = file_name;
9684 mutex_lock(&devlink->lock);
9685 ret = devlink->ops->flash_update(devlink, ¶ms, NULL);
9686 mutex_unlock(&devlink->lock);
9695 int devlink_compat_phys_port_name_get(struct net_device *dev,
9696 char *name, size_t len)
9698 struct devlink_port *devlink_port;
9700 /* RTNL mutex is held here which ensures that devlink_port
9701 * instance cannot disappear in the middle. No need to take
9702 * any devlink lock as only permanent values are accessed.
9706 devlink_port = netdev_to_devlink_port(dev);
9710 return __devlink_port_phys_port_name_get(devlink_port, name, len);
9713 int devlink_compat_switch_id_get(struct net_device *dev,
9714 struct netdev_phys_item_id *ppid)
9716 struct devlink_port *devlink_port;
9718 /* Caller must hold RTNL mutex or reference to dev, which ensures that
9719 * devlink_port instance cannot disappear in the middle. No need to take
9720 * any devlink lock as only permanent values are accessed.
9722 devlink_port = netdev_to_devlink_port(dev);
9723 if (!devlink_port || !devlink_port->switch_port)
9726 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
9731 static void __net_exit devlink_pernet_pre_exit(struct net *net)
9733 struct devlink *devlink;
9736 /* In case network namespace is getting destroyed, reload
9737 * all devlink instances from this namespace into init_net.
9739 mutex_lock(&devlink_mutex);
9740 list_for_each_entry(devlink, &devlink_list, list) {
9741 if (net_eq(devlink_net(devlink), net)) {
9742 if (WARN_ON(!devlink_reload_supported(devlink)))
9744 err = devlink_reload(devlink, &init_net, NULL);
9745 if (err && err != -EOPNOTSUPP)
9746 pr_warn("Failed to reload devlink instance into init_net\n");
9749 mutex_unlock(&devlink_mutex);
9752 static struct pernet_operations devlink_pernet_ops __net_initdata = {
9753 .pre_exit = devlink_pernet_pre_exit,
9756 static int __init devlink_init(void)
9760 err = genl_register_family(&devlink_nl_family);
9763 err = register_pernet_subsys(&devlink_pernet_ops);
9770 subsys_initcall(devlink_init);