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 <rdma/ib_verbs.h>
21 #include <net/netlink.h>
22 #include <net/genetlink.h>
23 #include <net/rtnetlink.h>
24 #include <net/net_namespace.h>
26 #include <net/devlink.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/devlink.h>
30 static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
32 .name = "destination mac",
33 .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
38 struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
40 .id = DEVLINK_DPIPE_HEADER_ETHERNET,
41 .fields = devlink_dpipe_fields_ethernet,
42 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
45 EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
47 static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
49 .name = "destination ip",
50 .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
55 struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
57 .id = DEVLINK_DPIPE_HEADER_IPV4,
58 .fields = devlink_dpipe_fields_ipv4,
59 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
62 EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
64 static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
66 .name = "destination ip",
67 .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
72 struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
74 .id = DEVLINK_DPIPE_HEADER_IPV6,
75 .fields = devlink_dpipe_fields_ipv6,
76 .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
79 EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
81 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
82 EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwerr);
84 static LIST_HEAD(devlink_list);
88 * An overall lock guarding every operation coming from userspace.
89 * It also guards devlink devices list and it is taken when
90 * driver registers/unregisters it.
92 static DEFINE_MUTEX(devlink_mutex);
94 static struct net *devlink_net(const struct devlink *devlink)
96 return read_pnet(&devlink->_net);
99 static void devlink_net_set(struct devlink *devlink, struct net *net)
101 write_pnet(&devlink->_net, net);
104 static struct devlink *devlink_get_from_attrs(struct net *net,
105 struct nlattr **attrs)
107 struct devlink *devlink;
111 if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
112 return ERR_PTR(-EINVAL);
114 busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
115 devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
117 lockdep_assert_held(&devlink_mutex);
119 list_for_each_entry(devlink, &devlink_list, list) {
120 if (strcmp(devlink->dev->bus->name, busname) == 0 &&
121 strcmp(dev_name(devlink->dev), devname) == 0 &&
122 net_eq(devlink_net(devlink), net))
126 return ERR_PTR(-ENODEV);
129 static struct devlink *devlink_get_from_info(struct genl_info *info)
131 return devlink_get_from_attrs(genl_info_net(info), info->attrs);
134 static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
137 struct devlink_port *devlink_port;
139 list_for_each_entry(devlink_port, &devlink->port_list, list) {
140 if (devlink_port->index == port_index)
146 static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
148 return devlink_port_get_by_index(devlink, port_index);
151 static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
152 struct nlattr **attrs)
154 if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
155 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
156 struct devlink_port *devlink_port;
158 devlink_port = devlink_port_get_by_index(devlink, port_index);
160 return ERR_PTR(-ENODEV);
163 return ERR_PTR(-EINVAL);
166 static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
167 struct genl_info *info)
169 return devlink_port_get_from_attrs(devlink, info->attrs);
173 struct list_head list;
176 u16 ingress_pools_count;
177 u16 egress_pools_count;
178 u16 ingress_tc_count;
182 static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
184 return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
187 static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
188 unsigned int sb_index)
190 struct devlink_sb *devlink_sb;
192 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
193 if (devlink_sb->index == sb_index)
199 static bool devlink_sb_index_exists(struct devlink *devlink,
200 unsigned int sb_index)
202 return devlink_sb_get_by_index(devlink, sb_index);
205 static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
206 struct nlattr **attrs)
208 if (attrs[DEVLINK_ATTR_SB_INDEX]) {
209 u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
210 struct devlink_sb *devlink_sb;
212 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
214 return ERR_PTR(-ENODEV);
217 return ERR_PTR(-EINVAL);
220 static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
221 struct genl_info *info)
223 return devlink_sb_get_from_attrs(devlink, info->attrs);
226 static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
227 struct nlattr **attrs,
232 if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
235 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
236 if (val >= devlink_sb_pool_count(devlink_sb))
242 static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
243 struct genl_info *info,
246 return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
251 devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
252 enum devlink_sb_pool_type *p_pool_type)
256 if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
259 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
260 if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
261 val != DEVLINK_SB_POOL_TYPE_EGRESS)
268 devlink_sb_pool_type_get_from_info(struct genl_info *info,
269 enum devlink_sb_pool_type *p_pool_type)
271 return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
275 devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
276 enum devlink_sb_threshold_type *p_th_type)
280 if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
283 val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
284 if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
285 val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
292 devlink_sb_th_type_get_from_info(struct genl_info *info,
293 enum devlink_sb_threshold_type *p_th_type)
295 return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
299 devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
300 struct nlattr **attrs,
301 enum devlink_sb_pool_type pool_type,
306 if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
309 val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
310 if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
311 val >= devlink_sb->ingress_tc_count)
313 if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
314 val >= devlink_sb->egress_tc_count)
321 devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
322 struct genl_info *info,
323 enum devlink_sb_pool_type pool_type,
326 return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
327 pool_type, p_tc_index);
330 struct devlink_region {
331 struct devlink *devlink;
332 struct list_head list;
334 struct list_head snapshot_list;
340 struct devlink_snapshot {
341 struct list_head list;
342 struct devlink_region *region;
343 devlink_snapshot_data_dest_t *data_destructor;
349 static struct devlink_region *
350 devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
352 struct devlink_region *region;
354 list_for_each_entry(region, &devlink->region_list, list)
355 if (!strcmp(region->name, region_name))
361 static struct devlink_snapshot *
362 devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
364 struct devlink_snapshot *snapshot;
366 list_for_each_entry(snapshot, ®ion->snapshot_list, list)
367 if (snapshot->id == id)
373 static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
375 snapshot->region->cur_snapshots--;
376 list_del(&snapshot->list);
377 (*snapshot->data_destructor)(snapshot->data);
381 #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
382 #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
383 #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
385 /* The per devlink instance lock is taken by default in the pre-doit
386 * operation, yet several commands do not require this. The global
387 * devlink lock is taken and protects from disruption by user-calls.
389 #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
391 static int devlink_nl_pre_doit(const struct genl_ops *ops,
392 struct sk_buff *skb, struct genl_info *info)
394 struct devlink *devlink;
397 mutex_lock(&devlink_mutex);
398 devlink = devlink_get_from_info(info);
399 if (IS_ERR(devlink)) {
400 mutex_unlock(&devlink_mutex);
401 return PTR_ERR(devlink);
403 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
404 mutex_lock(&devlink->lock);
405 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
406 info->user_ptr[0] = devlink;
407 } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
408 struct devlink_port *devlink_port;
410 devlink_port = devlink_port_get_from_info(devlink, info);
411 if (IS_ERR(devlink_port)) {
412 err = PTR_ERR(devlink_port);
415 info->user_ptr[0] = devlink_port;
417 if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
418 struct devlink_sb *devlink_sb;
420 devlink_sb = devlink_sb_get_from_info(devlink, info);
421 if (IS_ERR(devlink_sb)) {
422 err = PTR_ERR(devlink_sb);
425 info->user_ptr[1] = devlink_sb;
430 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
431 mutex_unlock(&devlink->lock);
432 mutex_unlock(&devlink_mutex);
436 static void devlink_nl_post_doit(const struct genl_ops *ops,
437 struct sk_buff *skb, struct genl_info *info)
439 struct devlink *devlink;
441 devlink = devlink_get_from_info(info);
442 if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
443 mutex_unlock(&devlink->lock);
444 mutex_unlock(&devlink_mutex);
447 static struct genl_family devlink_nl_family;
449 enum devlink_multicast_groups {
450 DEVLINK_MCGRP_CONFIG,
453 static const struct genl_multicast_group devlink_nl_mcgrps[] = {
454 [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
457 static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
459 if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
461 if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
466 static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
467 enum devlink_command cmd, u32 portid,
472 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
476 if (devlink_nl_put_handle(msg, devlink))
477 goto nla_put_failure;
479 genlmsg_end(msg, hdr);
483 genlmsg_cancel(msg, hdr);
487 static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
492 WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
494 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
498 err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
504 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
505 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
508 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
509 struct devlink_port *devlink_port)
511 struct devlink_port_attrs *attrs = &devlink_port->attrs;
515 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
517 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
521 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
523 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
524 attrs->split_subport_number))
529 static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
530 struct devlink_port *devlink_port,
531 enum devlink_command cmd, u32 portid,
536 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
540 if (devlink_nl_put_handle(msg, devlink))
541 goto nla_put_failure;
542 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
543 goto nla_put_failure;
545 spin_lock(&devlink_port->type_lock);
546 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
547 goto nla_put_failure_type_locked;
548 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
549 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
550 devlink_port->desired_type))
551 goto nla_put_failure_type_locked;
552 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
553 struct net_device *netdev = devlink_port->type_dev;
556 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
558 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
560 goto nla_put_failure_type_locked;
562 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
563 struct ib_device *ibdev = devlink_port->type_dev;
566 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
568 goto nla_put_failure_type_locked;
570 spin_unlock(&devlink_port->type_lock);
571 if (devlink_nl_port_attrs_put(msg, devlink_port))
572 goto nla_put_failure;
574 genlmsg_end(msg, hdr);
577 nla_put_failure_type_locked:
578 spin_unlock(&devlink_port->type_lock);
580 genlmsg_cancel(msg, hdr);
584 static void devlink_port_notify(struct devlink_port *devlink_port,
585 enum devlink_command cmd)
587 struct devlink *devlink = devlink_port->devlink;
591 if (!devlink_port->registered)
594 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
596 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
600 err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
606 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
607 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
610 static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
612 struct devlink *devlink = info->user_ptr[0];
616 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
620 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
621 info->snd_portid, info->snd_seq, 0);
627 return genlmsg_reply(msg, info);
630 static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
631 struct netlink_callback *cb)
633 struct devlink *devlink;
634 int start = cb->args[0];
638 mutex_lock(&devlink_mutex);
639 list_for_each_entry(devlink, &devlink_list, list) {
640 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
646 err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
647 NETLINK_CB(cb->skb).portid,
648 cb->nlh->nlmsg_seq, NLM_F_MULTI);
654 mutex_unlock(&devlink_mutex);
660 static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
661 struct genl_info *info)
663 struct devlink_port *devlink_port = info->user_ptr[0];
664 struct devlink *devlink = devlink_port->devlink;
668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
672 err = devlink_nl_port_fill(msg, devlink, devlink_port,
673 DEVLINK_CMD_PORT_NEW,
674 info->snd_portid, info->snd_seq, 0);
680 return genlmsg_reply(msg, info);
683 static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
684 struct netlink_callback *cb)
686 struct devlink *devlink;
687 struct devlink_port *devlink_port;
688 int start = cb->args[0];
692 mutex_lock(&devlink_mutex);
693 list_for_each_entry(devlink, &devlink_list, list) {
694 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
696 mutex_lock(&devlink->lock);
697 list_for_each_entry(devlink_port, &devlink->port_list, list) {
702 err = devlink_nl_port_fill(msg, devlink, devlink_port,
704 NETLINK_CB(cb->skb).portid,
708 mutex_unlock(&devlink->lock);
713 mutex_unlock(&devlink->lock);
716 mutex_unlock(&devlink_mutex);
722 static int devlink_port_type_set(struct devlink *devlink,
723 struct devlink_port *devlink_port,
724 enum devlink_port_type port_type)
729 if (devlink->ops->port_type_set) {
730 if (port_type == DEVLINK_PORT_TYPE_NOTSET)
732 if (port_type == devlink_port->type)
734 err = devlink->ops->port_type_set(devlink_port, port_type);
737 devlink_port->desired_type = port_type;
738 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
744 static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
745 struct genl_info *info)
747 struct devlink_port *devlink_port = info->user_ptr[0];
748 struct devlink *devlink = devlink_port->devlink;
751 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
752 enum devlink_port_type port_type;
754 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
755 err = devlink_port_type_set(devlink, devlink_port, port_type);
762 static int devlink_port_split(struct devlink *devlink, u32 port_index,
763 u32 count, struct netlink_ext_ack *extack)
766 if (devlink->ops->port_split)
767 return devlink->ops->port_split(devlink, port_index, count,
772 static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
773 struct genl_info *info)
775 struct devlink *devlink = info->user_ptr[0];
779 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
780 !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
783 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
784 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
785 return devlink_port_split(devlink, port_index, count, info->extack);
788 static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
789 struct netlink_ext_ack *extack)
792 if (devlink->ops->port_unsplit)
793 return devlink->ops->port_unsplit(devlink, port_index, extack);
797 static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
798 struct genl_info *info)
800 struct devlink *devlink = info->user_ptr[0];
803 if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
806 port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
807 return devlink_port_unsplit(devlink, port_index, info->extack);
810 static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
811 struct devlink_sb *devlink_sb,
812 enum devlink_command cmd, u32 portid,
817 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
821 if (devlink_nl_put_handle(msg, devlink))
822 goto nla_put_failure;
823 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
824 goto nla_put_failure;
825 if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
826 goto nla_put_failure;
827 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
828 devlink_sb->ingress_pools_count))
829 goto nla_put_failure;
830 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
831 devlink_sb->egress_pools_count))
832 goto nla_put_failure;
833 if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
834 devlink_sb->ingress_tc_count))
835 goto nla_put_failure;
836 if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
837 devlink_sb->egress_tc_count))
838 goto nla_put_failure;
840 genlmsg_end(msg, hdr);
844 genlmsg_cancel(msg, hdr);
848 static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
849 struct genl_info *info)
851 struct devlink *devlink = info->user_ptr[0];
852 struct devlink_sb *devlink_sb = info->user_ptr[1];
856 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
860 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
862 info->snd_portid, info->snd_seq, 0);
868 return genlmsg_reply(msg, info);
871 static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
872 struct netlink_callback *cb)
874 struct devlink *devlink;
875 struct devlink_sb *devlink_sb;
876 int start = cb->args[0];
880 mutex_lock(&devlink_mutex);
881 list_for_each_entry(devlink, &devlink_list, list) {
882 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
884 mutex_lock(&devlink->lock);
885 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
890 err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
892 NETLINK_CB(cb->skb).portid,
896 mutex_unlock(&devlink->lock);
901 mutex_unlock(&devlink->lock);
904 mutex_unlock(&devlink_mutex);
910 static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
911 struct devlink_sb *devlink_sb,
912 u16 pool_index, enum devlink_command cmd,
913 u32 portid, u32 seq, int flags)
915 struct devlink_sb_pool_info pool_info;
919 err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
920 pool_index, &pool_info);
924 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
928 if (devlink_nl_put_handle(msg, devlink))
929 goto nla_put_failure;
930 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
931 goto nla_put_failure;
932 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
933 goto nla_put_failure;
934 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
935 goto nla_put_failure;
936 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
937 goto nla_put_failure;
938 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
939 pool_info.threshold_type))
940 goto nla_put_failure;
941 if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_CELL_SIZE,
942 pool_info.cell_size))
943 goto nla_put_failure;
945 genlmsg_end(msg, hdr);
949 genlmsg_cancel(msg, hdr);
953 static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
954 struct genl_info *info)
956 struct devlink *devlink = info->user_ptr[0];
957 struct devlink_sb *devlink_sb = info->user_ptr[1];
962 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
967 if (!devlink->ops->sb_pool_get)
970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
974 err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
975 DEVLINK_CMD_SB_POOL_NEW,
976 info->snd_portid, info->snd_seq, 0);
982 return genlmsg_reply(msg, info);
985 static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
986 struct devlink *devlink,
987 struct devlink_sb *devlink_sb,
990 u16 pool_count = devlink_sb_pool_count(devlink_sb);
994 for (pool_index = 0; pool_index < pool_count; pool_index++) {
995 if (*p_idx < start) {
999 err = devlink_nl_sb_pool_fill(msg, devlink,
1002 DEVLINK_CMD_SB_POOL_NEW,
1003 portid, seq, NLM_F_MULTI);
1011 static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
1012 struct netlink_callback *cb)
1014 struct devlink *devlink;
1015 struct devlink_sb *devlink_sb;
1016 int start = cb->args[0];
1020 mutex_lock(&devlink_mutex);
1021 list_for_each_entry(devlink, &devlink_list, list) {
1022 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1023 !devlink->ops->sb_pool_get)
1025 mutex_lock(&devlink->lock);
1026 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1027 err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
1029 NETLINK_CB(cb->skb).portid,
1030 cb->nlh->nlmsg_seq);
1031 if (err && err != -EOPNOTSUPP) {
1032 mutex_unlock(&devlink->lock);
1036 mutex_unlock(&devlink->lock);
1039 mutex_unlock(&devlink_mutex);
1045 static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
1046 u16 pool_index, u32 size,
1047 enum devlink_sb_threshold_type threshold_type,
1048 struct netlink_ext_ack *extack)
1051 const struct devlink_ops *ops = devlink->ops;
1053 if (ops->sb_pool_set)
1054 return ops->sb_pool_set(devlink, sb_index, pool_index,
1055 size, threshold_type, extack);
1059 static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
1060 struct genl_info *info)
1062 struct devlink *devlink = info->user_ptr[0];
1063 struct devlink_sb *devlink_sb = info->user_ptr[1];
1064 enum devlink_sb_threshold_type threshold_type;
1069 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1074 err = devlink_sb_th_type_get_from_info(info, &threshold_type);
1078 if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
1081 size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
1082 return devlink_sb_pool_set(devlink, devlink_sb->index,
1083 pool_index, size, threshold_type,
1087 static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
1088 struct devlink *devlink,
1089 struct devlink_port *devlink_port,
1090 struct devlink_sb *devlink_sb,
1092 enum devlink_command cmd,
1093 u32 portid, u32 seq, int flags)
1095 const struct devlink_ops *ops = devlink->ops;
1100 err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
1101 pool_index, &threshold);
1105 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1109 if (devlink_nl_put_handle(msg, devlink))
1110 goto nla_put_failure;
1111 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1112 goto nla_put_failure;
1113 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1114 goto nla_put_failure;
1115 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1116 goto nla_put_failure;
1117 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1118 goto nla_put_failure;
1120 if (ops->sb_occ_port_pool_get) {
1124 err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
1125 pool_index, &cur, &max);
1126 if (err && err != -EOPNOTSUPP)
1129 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1130 goto nla_put_failure;
1131 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1132 goto nla_put_failure;
1136 genlmsg_end(msg, hdr);
1140 genlmsg_cancel(msg, hdr);
1144 static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
1145 struct genl_info *info)
1147 struct devlink_port *devlink_port = info->user_ptr[0];
1148 struct devlink *devlink = devlink_port->devlink;
1149 struct devlink_sb *devlink_sb = info->user_ptr[1];
1150 struct sk_buff *msg;
1154 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1159 if (!devlink->ops->sb_port_pool_get)
1162 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1166 err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
1167 devlink_sb, pool_index,
1168 DEVLINK_CMD_SB_PORT_POOL_NEW,
1169 info->snd_portid, info->snd_seq, 0);
1175 return genlmsg_reply(msg, info);
1178 static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
1179 struct devlink *devlink,
1180 struct devlink_sb *devlink_sb,
1181 u32 portid, u32 seq)
1183 struct devlink_port *devlink_port;
1184 u16 pool_count = devlink_sb_pool_count(devlink_sb);
1188 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1189 for (pool_index = 0; pool_index < pool_count; pool_index++) {
1190 if (*p_idx < start) {
1194 err = devlink_nl_sb_port_pool_fill(msg, devlink,
1198 DEVLINK_CMD_SB_PORT_POOL_NEW,
1209 static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
1210 struct netlink_callback *cb)
1212 struct devlink *devlink;
1213 struct devlink_sb *devlink_sb;
1214 int start = cb->args[0];
1218 mutex_lock(&devlink_mutex);
1219 list_for_each_entry(devlink, &devlink_list, list) {
1220 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1221 !devlink->ops->sb_port_pool_get)
1223 mutex_lock(&devlink->lock);
1224 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1225 err = __sb_port_pool_get_dumpit(msg, start, &idx,
1226 devlink, devlink_sb,
1227 NETLINK_CB(cb->skb).portid,
1228 cb->nlh->nlmsg_seq);
1229 if (err && err != -EOPNOTSUPP) {
1230 mutex_unlock(&devlink->lock);
1234 mutex_unlock(&devlink->lock);
1237 mutex_unlock(&devlink_mutex);
1243 static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
1244 unsigned int sb_index, u16 pool_index,
1246 struct netlink_ext_ack *extack)
1249 const struct devlink_ops *ops = devlink_port->devlink->ops;
1251 if (ops->sb_port_pool_set)
1252 return ops->sb_port_pool_set(devlink_port, sb_index,
1253 pool_index, threshold, extack);
1257 static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
1258 struct genl_info *info)
1260 struct devlink_port *devlink_port = info->user_ptr[0];
1261 struct devlink_sb *devlink_sb = info->user_ptr[1];
1266 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1271 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1274 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1275 return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
1276 pool_index, threshold, info->extack);
1280 devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
1281 struct devlink_port *devlink_port,
1282 struct devlink_sb *devlink_sb, u16 tc_index,
1283 enum devlink_sb_pool_type pool_type,
1284 enum devlink_command cmd,
1285 u32 portid, u32 seq, int flags)
1287 const struct devlink_ops *ops = devlink->ops;
1293 err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
1294 tc_index, pool_type,
1295 &pool_index, &threshold);
1299 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1303 if (devlink_nl_put_handle(msg, devlink))
1304 goto nla_put_failure;
1305 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
1306 goto nla_put_failure;
1307 if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
1308 goto nla_put_failure;
1309 if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
1310 goto nla_put_failure;
1311 if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
1312 goto nla_put_failure;
1313 if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
1314 goto nla_put_failure;
1315 if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
1316 goto nla_put_failure;
1318 if (ops->sb_occ_tc_port_bind_get) {
1322 err = ops->sb_occ_tc_port_bind_get(devlink_port,
1324 tc_index, pool_type,
1326 if (err && err != -EOPNOTSUPP)
1329 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
1330 goto nla_put_failure;
1331 if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
1332 goto nla_put_failure;
1336 genlmsg_end(msg, hdr);
1340 genlmsg_cancel(msg, hdr);
1344 static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
1345 struct genl_info *info)
1347 struct devlink_port *devlink_port = info->user_ptr[0];
1348 struct devlink *devlink = devlink_port->devlink;
1349 struct devlink_sb *devlink_sb = info->user_ptr[1];
1350 struct sk_buff *msg;
1351 enum devlink_sb_pool_type pool_type;
1355 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1359 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1360 pool_type, &tc_index);
1364 if (!devlink->ops->sb_tc_pool_bind_get)
1367 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1371 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
1372 devlink_sb, tc_index, pool_type,
1373 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1381 return genlmsg_reply(msg, info);
1384 static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1385 int start, int *p_idx,
1386 struct devlink *devlink,
1387 struct devlink_sb *devlink_sb,
1388 u32 portid, u32 seq)
1390 struct devlink_port *devlink_port;
1394 list_for_each_entry(devlink_port, &devlink->port_list, list) {
1396 tc_index < devlink_sb->ingress_tc_count; tc_index++) {
1397 if (*p_idx < start) {
1401 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1405 DEVLINK_SB_POOL_TYPE_INGRESS,
1406 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1414 tc_index < devlink_sb->egress_tc_count; tc_index++) {
1415 if (*p_idx < start) {
1419 err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
1423 DEVLINK_SB_POOL_TYPE_EGRESS,
1424 DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
1436 devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
1437 struct netlink_callback *cb)
1439 struct devlink *devlink;
1440 struct devlink_sb *devlink_sb;
1441 int start = cb->args[0];
1445 mutex_lock(&devlink_mutex);
1446 list_for_each_entry(devlink, &devlink_list, list) {
1447 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
1448 !devlink->ops->sb_tc_pool_bind_get)
1451 mutex_lock(&devlink->lock);
1452 list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
1453 err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
1456 NETLINK_CB(cb->skb).portid,
1457 cb->nlh->nlmsg_seq);
1458 if (err && err != -EOPNOTSUPP) {
1459 mutex_unlock(&devlink->lock);
1463 mutex_unlock(&devlink->lock);
1466 mutex_unlock(&devlink_mutex);
1472 static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1473 unsigned int sb_index, u16 tc_index,
1474 enum devlink_sb_pool_type pool_type,
1475 u16 pool_index, u32 threshold,
1476 struct netlink_ext_ack *extack)
1479 const struct devlink_ops *ops = devlink_port->devlink->ops;
1481 if (ops->sb_tc_pool_bind_set)
1482 return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
1483 tc_index, pool_type,
1484 pool_index, threshold, extack);
1488 static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
1489 struct genl_info *info)
1491 struct devlink_port *devlink_port = info->user_ptr[0];
1492 struct devlink_sb *devlink_sb = info->user_ptr[1];
1493 enum devlink_sb_pool_type pool_type;
1499 err = devlink_sb_pool_type_get_from_info(info, &pool_type);
1503 err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
1504 pool_type, &tc_index);
1508 err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
1513 if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
1516 threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
1517 return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
1518 tc_index, pool_type,
1519 pool_index, threshold, info->extack);
1522 static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
1523 struct genl_info *info)
1525 struct devlink *devlink = info->user_ptr[0];
1526 struct devlink_sb *devlink_sb = info->user_ptr[1];
1527 const struct devlink_ops *ops = devlink->ops;
1529 if (ops->sb_occ_snapshot)
1530 return ops->sb_occ_snapshot(devlink, devlink_sb->index);
1534 static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
1535 struct genl_info *info)
1537 struct devlink *devlink = info->user_ptr[0];
1538 struct devlink_sb *devlink_sb = info->user_ptr[1];
1539 const struct devlink_ops *ops = devlink->ops;
1541 if (ops->sb_occ_max_clear)
1542 return ops->sb_occ_max_clear(devlink, devlink_sb->index);
1546 static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
1547 enum devlink_command cmd, u32 portid,
1550 const struct devlink_ops *ops = devlink->ops;
1551 u8 inline_mode, encap_mode;
1556 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
1560 err = devlink_nl_put_handle(msg, devlink);
1562 goto nla_put_failure;
1564 if (ops->eswitch_mode_get) {
1565 err = ops->eswitch_mode_get(devlink, &mode);
1567 goto nla_put_failure;
1568 err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
1570 goto nla_put_failure;
1573 if (ops->eswitch_inline_mode_get) {
1574 err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
1576 goto nla_put_failure;
1577 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
1580 goto nla_put_failure;
1583 if (ops->eswitch_encap_mode_get) {
1584 err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
1586 goto nla_put_failure;
1587 err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
1589 goto nla_put_failure;
1592 genlmsg_end(msg, hdr);
1596 genlmsg_cancel(msg, hdr);
1600 static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
1601 struct genl_info *info)
1603 struct devlink *devlink = info->user_ptr[0];
1604 struct sk_buff *msg;
1607 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1611 err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
1612 info->snd_portid, info->snd_seq, 0);
1619 return genlmsg_reply(msg, info);
1622 static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
1623 struct genl_info *info)
1625 struct devlink *devlink = info->user_ptr[0];
1626 const struct devlink_ops *ops = devlink->ops;
1627 u8 inline_mode, encap_mode;
1631 if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
1632 if (!ops->eswitch_mode_set)
1634 mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
1635 err = ops->eswitch_mode_set(devlink, mode, info->extack);
1640 if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
1641 if (!ops->eswitch_inline_mode_set)
1643 inline_mode = nla_get_u8(
1644 info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
1645 err = ops->eswitch_inline_mode_set(devlink, inline_mode,
1651 if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
1652 if (!ops->eswitch_encap_mode_set)
1654 encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
1655 err = ops->eswitch_encap_mode_set(devlink, encap_mode,
1664 int devlink_dpipe_match_put(struct sk_buff *skb,
1665 struct devlink_dpipe_match *match)
1667 struct devlink_dpipe_header *header = match->header;
1668 struct devlink_dpipe_field *field = &header->fields[match->field_id];
1669 struct nlattr *match_attr;
1671 match_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_MATCH);
1675 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
1676 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
1677 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1678 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1679 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1680 goto nla_put_failure;
1682 nla_nest_end(skb, match_attr);
1686 nla_nest_cancel(skb, match_attr);
1689 EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
1691 static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
1692 struct sk_buff *skb)
1694 struct nlattr *matches_attr;
1696 matches_attr = nla_nest_start_noflag(skb,
1697 DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
1701 if (table->table_ops->matches_dump(table->priv, skb))
1702 goto nla_put_failure;
1704 nla_nest_end(skb, matches_attr);
1708 nla_nest_cancel(skb, matches_attr);
1712 int devlink_dpipe_action_put(struct sk_buff *skb,
1713 struct devlink_dpipe_action *action)
1715 struct devlink_dpipe_header *header = action->header;
1716 struct devlink_dpipe_field *field = &header->fields[action->field_id];
1717 struct nlattr *action_attr;
1719 action_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ACTION);
1723 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
1724 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
1725 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
1726 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
1727 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
1728 goto nla_put_failure;
1730 nla_nest_end(skb, action_attr);
1734 nla_nest_cancel(skb, action_attr);
1737 EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
1739 static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
1740 struct sk_buff *skb)
1742 struct nlattr *actions_attr;
1744 actions_attr = nla_nest_start_noflag(skb,
1745 DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
1749 if (table->table_ops->actions_dump(table->priv, skb))
1750 goto nla_put_failure;
1752 nla_nest_end(skb, actions_attr);
1756 nla_nest_cancel(skb, actions_attr);
1760 static int devlink_dpipe_table_put(struct sk_buff *skb,
1761 struct devlink_dpipe_table *table)
1763 struct nlattr *table_attr;
1766 table_size = table->table_ops->size_get(table->priv);
1767 table_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLE);
1771 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
1772 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
1774 goto nla_put_failure;
1775 if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
1776 table->counters_enabled))
1777 goto nla_put_failure;
1779 if (table->resource_valid) {
1780 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
1781 table->resource_id, DEVLINK_ATTR_PAD) ||
1782 nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
1783 table->resource_units, DEVLINK_ATTR_PAD))
1784 goto nla_put_failure;
1786 if (devlink_dpipe_matches_put(table, skb))
1787 goto nla_put_failure;
1789 if (devlink_dpipe_actions_put(table, skb))
1790 goto nla_put_failure;
1792 nla_nest_end(skb, table_attr);
1796 nla_nest_cancel(skb, table_attr);
1800 static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
1801 struct genl_info *info)
1806 err = genlmsg_reply(*pskb, info);
1810 *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1816 static int devlink_dpipe_tables_fill(struct genl_info *info,
1817 enum devlink_command cmd, int flags,
1818 struct list_head *dpipe_tables,
1819 const char *table_name)
1821 struct devlink *devlink = info->user_ptr[0];
1822 struct devlink_dpipe_table *table;
1823 struct nlattr *tables_attr;
1824 struct sk_buff *skb = NULL;
1825 struct nlmsghdr *nlh;
1831 table = list_first_entry(dpipe_tables,
1832 struct devlink_dpipe_table, list);
1834 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1838 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
1839 &devlink_nl_family, NLM_F_MULTI, cmd);
1845 if (devlink_nl_put_handle(skb, devlink))
1846 goto nla_put_failure;
1847 tables_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_TABLES);
1849 goto nla_put_failure;
1853 list_for_each_entry_from(table, dpipe_tables, list) {
1855 err = devlink_dpipe_table_put(skb, table);
1863 if (!strcmp(table->name, table_name)) {
1864 err = devlink_dpipe_table_put(skb, table);
1872 nla_nest_end(skb, tables_attr);
1873 genlmsg_end(skb, hdr);
1878 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
1879 NLMSG_DONE, 0, flags | NLM_F_MULTI);
1881 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
1887 return genlmsg_reply(skb, info);
1896 static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
1897 struct genl_info *info)
1899 struct devlink *devlink = info->user_ptr[0];
1900 const char *table_name = NULL;
1902 if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
1903 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
1905 return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
1906 &devlink->dpipe_table_list,
1910 static int devlink_dpipe_value_put(struct sk_buff *skb,
1911 struct devlink_dpipe_value *value)
1913 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
1914 value->value_size, value->value))
1917 if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
1918 value->value_size, value->mask))
1920 if (value->mapping_valid)
1921 if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
1922 value->mapping_value))
1927 static int devlink_dpipe_action_value_put(struct sk_buff *skb,
1928 struct devlink_dpipe_value *value)
1932 if (devlink_dpipe_action_put(skb, value->action))
1934 if (devlink_dpipe_value_put(skb, value))
1939 static int devlink_dpipe_action_values_put(struct sk_buff *skb,
1940 struct devlink_dpipe_value *values,
1941 unsigned int values_count)
1943 struct nlattr *action_attr;
1947 for (i = 0; i < values_count; i++) {
1948 action_attr = nla_nest_start_noflag(skb,
1949 DEVLINK_ATTR_DPIPE_ACTION_VALUE);
1952 err = devlink_dpipe_action_value_put(skb, &values[i]);
1954 goto err_action_value_put;
1955 nla_nest_end(skb, action_attr);
1959 err_action_value_put:
1960 nla_nest_cancel(skb, action_attr);
1964 static int devlink_dpipe_match_value_put(struct sk_buff *skb,
1965 struct devlink_dpipe_value *value)
1969 if (devlink_dpipe_match_put(skb, value->match))
1971 if (devlink_dpipe_value_put(skb, value))
1976 static int devlink_dpipe_match_values_put(struct sk_buff *skb,
1977 struct devlink_dpipe_value *values,
1978 unsigned int values_count)
1980 struct nlattr *match_attr;
1984 for (i = 0; i < values_count; i++) {
1985 match_attr = nla_nest_start_noflag(skb,
1986 DEVLINK_ATTR_DPIPE_MATCH_VALUE);
1989 err = devlink_dpipe_match_value_put(skb, &values[i]);
1991 goto err_match_value_put;
1992 nla_nest_end(skb, match_attr);
1996 err_match_value_put:
1997 nla_nest_cancel(skb, match_attr);
2001 static int devlink_dpipe_entry_put(struct sk_buff *skb,
2002 struct devlink_dpipe_entry *entry)
2004 struct nlattr *entry_attr, *matches_attr, *actions_attr;
2007 entry_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_ENTRY);
2011 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
2013 goto nla_put_failure;
2014 if (entry->counter_valid)
2015 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
2016 entry->counter, DEVLINK_ATTR_PAD))
2017 goto nla_put_failure;
2019 matches_attr = nla_nest_start_noflag(skb,
2020 DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
2022 goto nla_put_failure;
2024 err = devlink_dpipe_match_values_put(skb, entry->match_values,
2025 entry->match_values_count);
2027 nla_nest_cancel(skb, matches_attr);
2028 goto err_match_values_put;
2030 nla_nest_end(skb, matches_attr);
2032 actions_attr = nla_nest_start_noflag(skb,
2033 DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
2035 goto nla_put_failure;
2037 err = devlink_dpipe_action_values_put(skb, entry->action_values,
2038 entry->action_values_count);
2040 nla_nest_cancel(skb, actions_attr);
2041 goto err_action_values_put;
2043 nla_nest_end(skb, actions_attr);
2045 nla_nest_end(skb, entry_attr);
2050 err_match_values_put:
2051 err_action_values_put:
2052 nla_nest_cancel(skb, entry_attr);
2056 static struct devlink_dpipe_table *
2057 devlink_dpipe_table_find(struct list_head *dpipe_tables,
2058 const char *table_name)
2060 struct devlink_dpipe_table *table;
2062 list_for_each_entry_rcu(table, dpipe_tables, list) {
2063 if (!strcmp(table->name, table_name))
2069 int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
2071 struct devlink *devlink;
2074 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
2079 dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
2080 dump_ctx->info->snd_portid,
2081 dump_ctx->info->snd_seq,
2082 &devlink_nl_family, NLM_F_MULTI,
2085 goto nla_put_failure;
2087 devlink = dump_ctx->info->user_ptr[0];
2088 if (devlink_nl_put_handle(dump_ctx->skb, devlink))
2089 goto nla_put_failure;
2090 dump_ctx->nest = nla_nest_start_noflag(dump_ctx->skb,
2091 DEVLINK_ATTR_DPIPE_ENTRIES);
2092 if (!dump_ctx->nest)
2093 goto nla_put_failure;
2097 nlmsg_free(dump_ctx->skb);
2100 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
2102 int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
2103 struct devlink_dpipe_entry *entry)
2105 return devlink_dpipe_entry_put(dump_ctx->skb, entry);
2107 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
2109 int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
2111 nla_nest_end(dump_ctx->skb, dump_ctx->nest);
2112 genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
2115 EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
2117 void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
2120 unsigned int value_count, value_index;
2121 struct devlink_dpipe_value *value;
2123 value = entry->action_values;
2124 value_count = entry->action_values_count;
2125 for (value_index = 0; value_index < value_count; value_index++) {
2126 kfree(value[value_index].value);
2127 kfree(value[value_index].mask);
2130 value = entry->match_values;
2131 value_count = entry->match_values_count;
2132 for (value_index = 0; value_index < value_count; value_index++) {
2133 kfree(value[value_index].value);
2134 kfree(value[value_index].mask);
2137 EXPORT_SYMBOL(devlink_dpipe_entry_clear);
2139 static int devlink_dpipe_entries_fill(struct genl_info *info,
2140 enum devlink_command cmd, int flags,
2141 struct devlink_dpipe_table *table)
2143 struct devlink_dpipe_dump_ctx dump_ctx;
2144 struct nlmsghdr *nlh;
2147 dump_ctx.skb = NULL;
2149 dump_ctx.info = info;
2151 err = table->table_ops->entries_dump(table->priv,
2152 table->counters_enabled,
2158 nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
2159 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2161 err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
2166 return genlmsg_reply(dump_ctx.skb, info);
2169 static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
2170 struct genl_info *info)
2172 struct devlink *devlink = info->user_ptr[0];
2173 struct devlink_dpipe_table *table;
2174 const char *table_name;
2176 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
2179 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2180 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2185 if (!table->table_ops->entries_dump)
2188 return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
2192 static int devlink_dpipe_fields_put(struct sk_buff *skb,
2193 const struct devlink_dpipe_header *header)
2195 struct devlink_dpipe_field *field;
2196 struct nlattr *field_attr;
2199 for (i = 0; i < header->fields_count; i++) {
2200 field = &header->fields[i];
2201 field_attr = nla_nest_start_noflag(skb,
2202 DEVLINK_ATTR_DPIPE_FIELD);
2205 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
2206 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
2207 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
2208 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
2209 goto nla_put_failure;
2210 nla_nest_end(skb, field_attr);
2215 nla_nest_cancel(skb, field_attr);
2219 static int devlink_dpipe_header_put(struct sk_buff *skb,
2220 struct devlink_dpipe_header *header)
2222 struct nlattr *fields_attr, *header_attr;
2225 header_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADER);
2229 if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
2230 nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
2231 nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
2232 goto nla_put_failure;
2234 fields_attr = nla_nest_start_noflag(skb,
2235 DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
2237 goto nla_put_failure;
2239 err = devlink_dpipe_fields_put(skb, header);
2241 nla_nest_cancel(skb, fields_attr);
2242 goto nla_put_failure;
2244 nla_nest_end(skb, fields_attr);
2245 nla_nest_end(skb, header_attr);
2250 nla_nest_cancel(skb, header_attr);
2254 static int devlink_dpipe_headers_fill(struct genl_info *info,
2255 enum devlink_command cmd, int flags,
2256 struct devlink_dpipe_headers *
2259 struct devlink *devlink = info->user_ptr[0];
2260 struct nlattr *headers_attr;
2261 struct sk_buff *skb = NULL;
2262 struct nlmsghdr *nlh;
2269 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2273 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2274 &devlink_nl_family, NLM_F_MULTI, cmd);
2280 if (devlink_nl_put_handle(skb, devlink))
2281 goto nla_put_failure;
2282 headers_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_DPIPE_HEADERS);
2284 goto nla_put_failure;
2287 for (; i < dpipe_headers->headers_count; i++) {
2288 err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
2296 nla_nest_end(skb, headers_attr);
2297 genlmsg_end(skb, hdr);
2298 if (i != dpipe_headers->headers_count)
2302 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2303 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2305 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2310 return genlmsg_reply(skb, info);
2319 static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
2320 struct genl_info *info)
2322 struct devlink *devlink = info->user_ptr[0];
2324 if (!devlink->dpipe_headers)
2326 return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
2327 0, devlink->dpipe_headers);
2330 static int devlink_dpipe_table_counters_set(struct devlink *devlink,
2331 const char *table_name,
2334 struct devlink_dpipe_table *table;
2336 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
2341 if (table->counter_control_extern)
2344 if (!(table->counters_enabled ^ enable))
2347 table->counters_enabled = enable;
2348 if (table->table_ops->counters_set_update)
2349 table->table_ops->counters_set_update(table->priv, enable);
2353 static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
2354 struct genl_info *info)
2356 struct devlink *devlink = info->user_ptr[0];
2357 const char *table_name;
2358 bool counters_enable;
2360 if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
2361 !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
2364 table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
2365 counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
2367 return devlink_dpipe_table_counters_set(devlink, table_name,
2371 static struct devlink_resource *
2372 devlink_resource_find(struct devlink *devlink,
2373 struct devlink_resource *resource, u64 resource_id)
2375 struct list_head *resource_list;
2378 resource_list = &resource->resource_list;
2380 resource_list = &devlink->resource_list;
2382 list_for_each_entry(resource, resource_list, list) {
2383 struct devlink_resource *child_resource;
2385 if (resource->id == resource_id)
2388 child_resource = devlink_resource_find(devlink, resource,
2391 return child_resource;
2397 devlink_resource_validate_children(struct devlink_resource *resource)
2399 struct devlink_resource *child_resource;
2400 bool size_valid = true;
2403 if (list_empty(&resource->resource_list))
2406 list_for_each_entry(child_resource, &resource->resource_list, list)
2407 parts_size += child_resource->size_new;
2409 if (parts_size > resource->size_new)
2412 resource->size_valid = size_valid;
2416 devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
2417 struct netlink_ext_ack *extack)
2422 if (size > resource->size_params.size_max) {
2423 NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
2427 if (size < resource->size_params.size_min) {
2428 NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
2432 div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
2434 NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
2441 static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
2442 struct genl_info *info)
2444 struct devlink *devlink = info->user_ptr[0];
2445 struct devlink_resource *resource;
2450 if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
2451 !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
2453 resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
2455 resource = devlink_resource_find(devlink, NULL, resource_id);
2459 size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
2460 err = devlink_resource_validate_size(resource, size, info->extack);
2464 resource->size_new = size;
2465 devlink_resource_validate_children(resource);
2466 if (resource->parent)
2467 devlink_resource_validate_children(resource->parent);
2472 devlink_resource_size_params_put(struct devlink_resource *resource,
2473 struct sk_buff *skb)
2475 struct devlink_resource_size_params *size_params;
2477 size_params = &resource->size_params;
2478 if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
2479 size_params->size_granularity, DEVLINK_ATTR_PAD) ||
2480 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
2481 size_params->size_max, DEVLINK_ATTR_PAD) ||
2482 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
2483 size_params->size_min, DEVLINK_ATTR_PAD) ||
2484 nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
2489 static int devlink_resource_occ_put(struct devlink_resource *resource,
2490 struct sk_buff *skb)
2492 if (!resource->occ_get)
2494 return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
2495 resource->occ_get(resource->occ_get_priv),
2499 static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
2500 struct devlink_resource *resource)
2502 struct devlink_resource *child_resource;
2503 struct nlattr *child_resource_attr;
2504 struct nlattr *resource_attr;
2506 resource_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_RESOURCE);
2510 if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
2511 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
2512 DEVLINK_ATTR_PAD) ||
2513 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
2515 goto nla_put_failure;
2516 if (resource->size != resource->size_new)
2517 nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
2518 resource->size_new, DEVLINK_ATTR_PAD);
2519 if (devlink_resource_occ_put(resource, skb))
2520 goto nla_put_failure;
2521 if (devlink_resource_size_params_put(resource, skb))
2522 goto nla_put_failure;
2523 if (list_empty(&resource->resource_list))
2526 if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
2527 resource->size_valid))
2528 goto nla_put_failure;
2530 child_resource_attr = nla_nest_start_noflag(skb,
2531 DEVLINK_ATTR_RESOURCE_LIST);
2532 if (!child_resource_attr)
2533 goto nla_put_failure;
2535 list_for_each_entry(child_resource, &resource->resource_list, list) {
2536 if (devlink_resource_put(devlink, skb, child_resource))
2537 goto resource_put_failure;
2540 nla_nest_end(skb, child_resource_attr);
2542 nla_nest_end(skb, resource_attr);
2545 resource_put_failure:
2546 nla_nest_cancel(skb, child_resource_attr);
2548 nla_nest_cancel(skb, resource_attr);
2552 static int devlink_resource_fill(struct genl_info *info,
2553 enum devlink_command cmd, int flags)
2555 struct devlink *devlink = info->user_ptr[0];
2556 struct devlink_resource *resource;
2557 struct nlattr *resources_attr;
2558 struct sk_buff *skb = NULL;
2559 struct nlmsghdr *nlh;
2565 resource = list_first_entry(&devlink->resource_list,
2566 struct devlink_resource, list);
2568 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2572 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
2573 &devlink_nl_family, NLM_F_MULTI, cmd);
2579 if (devlink_nl_put_handle(skb, devlink))
2580 goto nla_put_failure;
2582 resources_attr = nla_nest_start_noflag(skb,
2583 DEVLINK_ATTR_RESOURCE_LIST);
2584 if (!resources_attr)
2585 goto nla_put_failure;
2589 list_for_each_entry_from(resource, &devlink->resource_list, list) {
2590 err = devlink_resource_put(devlink, skb, resource);
2593 goto err_resource_put;
2599 nla_nest_end(skb, resources_attr);
2600 genlmsg_end(skb, hdr);
2604 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
2605 NLMSG_DONE, 0, flags | NLM_F_MULTI);
2607 err = devlink_dpipe_send_and_alloc_skb(&skb, info);
2612 return genlmsg_reply(skb, info);
2621 static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
2622 struct genl_info *info)
2624 struct devlink *devlink = info->user_ptr[0];
2626 if (list_empty(&devlink->resource_list))
2629 return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
2633 devlink_resources_validate(struct devlink *devlink,
2634 struct devlink_resource *resource,
2635 struct genl_info *info)
2637 struct list_head *resource_list;
2641 resource_list = &resource->resource_list;
2643 resource_list = &devlink->resource_list;
2645 list_for_each_entry(resource, resource_list, list) {
2646 if (!resource->size_valid)
2648 err = devlink_resources_validate(devlink, resource, info);
2655 static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
2657 struct devlink *devlink = info->user_ptr[0];
2660 if (!devlink->ops->reload)
2663 err = devlink_resources_validate(devlink, NULL, info);
2665 NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
2668 return devlink->ops->reload(devlink, info->extack);
2671 static int devlink_nl_cmd_flash_update(struct sk_buff *skb,
2672 struct genl_info *info)
2674 struct devlink *devlink = info->user_ptr[0];
2675 const char *file_name, *component;
2676 struct nlattr *nla_component;
2678 if (!devlink->ops->flash_update)
2681 if (!info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME])
2683 file_name = nla_data(info->attrs[DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME]);
2685 nla_component = info->attrs[DEVLINK_ATTR_FLASH_UPDATE_COMPONENT];
2686 component = nla_component ? nla_data(nla_component) : NULL;
2688 return devlink->ops->flash_update(devlink, file_name, component,
2692 static const struct devlink_param devlink_param_generic[] = {
2694 .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
2695 .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
2696 .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
2699 .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
2700 .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
2701 .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
2704 .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
2705 .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
2706 .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
2709 .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
2710 .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
2711 .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
2714 .id = DEVLINK_PARAM_GENERIC_ID_IGNORE_ARI,
2715 .name = DEVLINK_PARAM_GENERIC_IGNORE_ARI_NAME,
2716 .type = DEVLINK_PARAM_GENERIC_IGNORE_ARI_TYPE,
2719 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MAX,
2720 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_NAME,
2721 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MAX_TYPE,
2724 .id = DEVLINK_PARAM_GENERIC_ID_MSIX_VEC_PER_PF_MIN,
2725 .name = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_NAME,
2726 .type = DEVLINK_PARAM_GENERIC_MSIX_VEC_PER_PF_MIN_TYPE,
2729 .id = DEVLINK_PARAM_GENERIC_ID_FW_LOAD_POLICY,
2730 .name = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_NAME,
2731 .type = DEVLINK_PARAM_GENERIC_FW_LOAD_POLICY_TYPE,
2735 static int devlink_param_generic_verify(const struct devlink_param *param)
2737 /* verify it match generic parameter by id and name */
2738 if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
2740 if (strcmp(param->name, devlink_param_generic[param->id].name))
2743 WARN_ON(param->type != devlink_param_generic[param->id].type);
2748 static int devlink_param_driver_verify(const struct devlink_param *param)
2752 if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
2754 /* verify no such name in generic params */
2755 for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
2756 if (!strcmp(param->name, devlink_param_generic[i].name))
2762 static struct devlink_param_item *
2763 devlink_param_find_by_name(struct list_head *param_list,
2764 const char *param_name)
2766 struct devlink_param_item *param_item;
2768 list_for_each_entry(param_item, param_list, list)
2769 if (!strcmp(param_item->param->name, param_name))
2774 static struct devlink_param_item *
2775 devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
2777 struct devlink_param_item *param_item;
2779 list_for_each_entry(param_item, param_list, list)
2780 if (param_item->param->id == param_id)
2786 devlink_param_cmode_is_supported(const struct devlink_param *param,
2787 enum devlink_param_cmode cmode)
2789 return test_bit(cmode, ¶m->supported_cmodes);
2792 static int devlink_param_get(struct devlink *devlink,
2793 const struct devlink_param *param,
2794 struct devlink_param_gset_ctx *ctx)
2798 return param->get(devlink, param->id, ctx);
2801 static int devlink_param_set(struct devlink *devlink,
2802 const struct devlink_param *param,
2803 struct devlink_param_gset_ctx *ctx)
2807 return param->set(devlink, param->id, ctx);
2811 devlink_param_type_to_nla_type(enum devlink_param_type param_type)
2813 switch (param_type) {
2814 case DEVLINK_PARAM_TYPE_U8:
2816 case DEVLINK_PARAM_TYPE_U16:
2818 case DEVLINK_PARAM_TYPE_U32:
2820 case DEVLINK_PARAM_TYPE_STRING:
2822 case DEVLINK_PARAM_TYPE_BOOL:
2830 devlink_nl_param_value_fill_one(struct sk_buff *msg,
2831 enum devlink_param_type type,
2832 enum devlink_param_cmode cmode,
2833 union devlink_param_value val)
2835 struct nlattr *param_value_attr;
2837 param_value_attr = nla_nest_start_noflag(msg,
2838 DEVLINK_ATTR_PARAM_VALUE);
2839 if (!param_value_attr)
2840 goto nla_put_failure;
2842 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
2843 goto value_nest_cancel;
2846 case DEVLINK_PARAM_TYPE_U8:
2847 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
2848 goto value_nest_cancel;
2850 case DEVLINK_PARAM_TYPE_U16:
2851 if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
2852 goto value_nest_cancel;
2854 case DEVLINK_PARAM_TYPE_U32:
2855 if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
2856 goto value_nest_cancel;
2858 case DEVLINK_PARAM_TYPE_STRING:
2859 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
2861 goto value_nest_cancel;
2863 case DEVLINK_PARAM_TYPE_BOOL:
2865 nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
2866 goto value_nest_cancel;
2870 nla_nest_end(msg, param_value_attr);
2874 nla_nest_cancel(msg, param_value_attr);
2879 static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
2880 unsigned int port_index,
2881 struct devlink_param_item *param_item,
2882 enum devlink_command cmd,
2883 u32 portid, u32 seq, int flags)
2885 union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
2886 bool param_value_set[DEVLINK_PARAM_CMODE_MAX + 1] = {};
2887 const struct devlink_param *param = param_item->param;
2888 struct devlink_param_gset_ctx ctx;
2889 struct nlattr *param_values_list;
2890 struct nlattr *param_attr;
2896 /* Get value from driver part to driverinit configuration mode */
2897 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2898 if (!devlink_param_cmode_is_supported(param, i))
2900 if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
2901 if (!param_item->driverinit_value_valid)
2903 param_value[i] = param_item->driverinit_value;
2905 if (!param_item->published)
2908 err = devlink_param_get(devlink, param, &ctx);
2911 param_value[i] = ctx.val;
2913 param_value_set[i] = true;
2916 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
2920 if (devlink_nl_put_handle(msg, devlink))
2921 goto genlmsg_cancel;
2923 if (cmd == DEVLINK_CMD_PORT_PARAM_GET ||
2924 cmd == DEVLINK_CMD_PORT_PARAM_NEW ||
2925 cmd == DEVLINK_CMD_PORT_PARAM_DEL)
2926 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, port_index))
2927 goto genlmsg_cancel;
2929 param_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PARAM);
2931 goto genlmsg_cancel;
2932 if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
2933 goto param_nest_cancel;
2934 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
2935 goto param_nest_cancel;
2937 nla_type = devlink_param_type_to_nla_type(param->type);
2939 goto param_nest_cancel;
2940 if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
2941 goto param_nest_cancel;
2943 param_values_list = nla_nest_start_noflag(msg,
2944 DEVLINK_ATTR_PARAM_VALUES_LIST);
2945 if (!param_values_list)
2946 goto param_nest_cancel;
2948 for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
2949 if (!param_value_set[i])
2951 err = devlink_nl_param_value_fill_one(msg, param->type,
2954 goto values_list_nest_cancel;
2957 nla_nest_end(msg, param_values_list);
2958 nla_nest_end(msg, param_attr);
2959 genlmsg_end(msg, hdr);
2962 values_list_nest_cancel:
2963 nla_nest_end(msg, param_values_list);
2965 nla_nest_cancel(msg, param_attr);
2967 genlmsg_cancel(msg, hdr);
2971 static void devlink_param_notify(struct devlink *devlink,
2972 unsigned int port_index,
2973 struct devlink_param_item *param_item,
2974 enum devlink_command cmd)
2976 struct sk_buff *msg;
2979 WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL &&
2980 cmd != DEVLINK_CMD_PORT_PARAM_NEW &&
2981 cmd != DEVLINK_CMD_PORT_PARAM_DEL);
2983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2986 err = devlink_nl_param_fill(msg, devlink, port_index, param_item, cmd,
2993 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
2994 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
2997 static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
2998 struct netlink_callback *cb)
3000 struct devlink_param_item *param_item;
3001 struct devlink *devlink;
3002 int start = cb->args[0];
3006 mutex_lock(&devlink_mutex);
3007 list_for_each_entry(devlink, &devlink_list, list) {
3008 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3010 mutex_lock(&devlink->lock);
3011 list_for_each_entry(param_item, &devlink->param_list, list) {
3016 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3017 DEVLINK_CMD_PARAM_GET,
3018 NETLINK_CB(cb->skb).portid,
3022 mutex_unlock(&devlink->lock);
3027 mutex_unlock(&devlink->lock);
3030 mutex_unlock(&devlink_mutex);
3037 devlink_param_type_get_from_info(struct genl_info *info,
3038 enum devlink_param_type *param_type)
3040 if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
3043 switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
3045 *param_type = DEVLINK_PARAM_TYPE_U8;
3048 *param_type = DEVLINK_PARAM_TYPE_U16;
3051 *param_type = DEVLINK_PARAM_TYPE_U32;
3054 *param_type = DEVLINK_PARAM_TYPE_STRING;
3057 *param_type = DEVLINK_PARAM_TYPE_BOOL;
3067 devlink_param_value_get_from_info(const struct devlink_param *param,
3068 struct genl_info *info,
3069 union devlink_param_value *value)
3073 if (param->type != DEVLINK_PARAM_TYPE_BOOL &&
3074 !info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA])
3077 switch (param->type) {
3078 case DEVLINK_PARAM_TYPE_U8:
3079 value->vu8 = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3081 case DEVLINK_PARAM_TYPE_U16:
3082 value->vu16 = nla_get_u16(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3084 case DEVLINK_PARAM_TYPE_U32:
3085 value->vu32 = nla_get_u32(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]);
3087 case DEVLINK_PARAM_TYPE_STRING:
3088 len = strnlen(nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]),
3089 nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3090 if (len == nla_len(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]) ||
3091 len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
3094 nla_data(info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA]));
3096 case DEVLINK_PARAM_TYPE_BOOL:
3097 value->vbool = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA] ?
3104 static struct devlink_param_item *
3105 devlink_param_get_from_info(struct list_head *param_list,
3106 struct genl_info *info)
3110 if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
3113 param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
3114 return devlink_param_find_by_name(param_list, param_name);
3117 static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
3118 struct genl_info *info)
3120 struct devlink *devlink = info->user_ptr[0];
3121 struct devlink_param_item *param_item;
3122 struct sk_buff *msg;
3125 param_item = devlink_param_get_from_info(&devlink->param_list, info);
3129 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3133 err = devlink_nl_param_fill(msg, devlink, 0, param_item,
3134 DEVLINK_CMD_PARAM_GET,
3135 info->snd_portid, info->snd_seq, 0);
3141 return genlmsg_reply(msg, info);
3144 static int __devlink_nl_cmd_param_set_doit(struct devlink *devlink,
3145 unsigned int port_index,
3146 struct list_head *param_list,
3147 struct genl_info *info,
3148 enum devlink_command cmd)
3150 enum devlink_param_type param_type;
3151 struct devlink_param_gset_ctx ctx;
3152 enum devlink_param_cmode cmode;
3153 struct devlink_param_item *param_item;
3154 const struct devlink_param *param;
3155 union devlink_param_value value;
3158 param_item = devlink_param_get_from_info(param_list, info);
3161 param = param_item->param;
3162 err = devlink_param_type_get_from_info(info, ¶m_type);
3165 if (param_type != param->type)
3167 err = devlink_param_value_get_from_info(param, info, &value);
3170 if (param->validate) {
3171 err = param->validate(devlink, param->id, value, info->extack);
3176 if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
3178 cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
3179 if (!devlink_param_cmode_is_supported(param, cmode))
3182 if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
3183 if (param->type == DEVLINK_PARAM_TYPE_STRING)
3184 strcpy(param_item->driverinit_value.vstr, value.vstr);
3186 param_item->driverinit_value = value;
3187 param_item->driverinit_value_valid = true;
3193 err = devlink_param_set(devlink, param, &ctx);
3198 devlink_param_notify(devlink, port_index, param_item, cmd);
3202 static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
3203 struct genl_info *info)
3205 struct devlink *devlink = info->user_ptr[0];
3207 return __devlink_nl_cmd_param_set_doit(devlink, 0, &devlink->param_list,
3208 info, DEVLINK_CMD_PARAM_NEW);
3211 static int devlink_param_register_one(struct devlink *devlink,
3212 unsigned int port_index,
3213 struct list_head *param_list,
3214 const struct devlink_param *param,
3215 enum devlink_command cmd)
3217 struct devlink_param_item *param_item;
3219 if (devlink_param_find_by_name(param_list, param->name))
3222 if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
3223 WARN_ON(param->get || param->set);
3225 WARN_ON(!param->get || !param->set);
3227 param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
3230 param_item->param = param;
3232 list_add_tail(¶m_item->list, param_list);
3233 devlink_param_notify(devlink, port_index, param_item, cmd);
3237 static void devlink_param_unregister_one(struct devlink *devlink,
3238 unsigned int port_index,
3239 struct list_head *param_list,
3240 const struct devlink_param *param,
3241 enum devlink_command cmd)
3243 struct devlink_param_item *param_item;
3245 param_item = devlink_param_find_by_name(param_list, param->name);
3246 WARN_ON(!param_item);
3247 devlink_param_notify(devlink, port_index, param_item, cmd);
3248 list_del(¶m_item->list);
3252 static int devlink_nl_cmd_port_param_get_dumpit(struct sk_buff *msg,
3253 struct netlink_callback *cb)
3255 struct devlink_param_item *param_item;
3256 struct devlink_port *devlink_port;
3257 struct devlink *devlink;
3258 int start = cb->args[0];
3262 mutex_lock(&devlink_mutex);
3263 list_for_each_entry(devlink, &devlink_list, list) {
3264 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3266 mutex_lock(&devlink->lock);
3267 list_for_each_entry(devlink_port, &devlink->port_list, list) {
3268 list_for_each_entry(param_item,
3269 &devlink_port->param_list, list) {
3274 err = devlink_nl_param_fill(msg,
3275 devlink_port->devlink,
3276 devlink_port->index, param_item,
3277 DEVLINK_CMD_PORT_PARAM_GET,
3278 NETLINK_CB(cb->skb).portid,
3282 mutex_unlock(&devlink->lock);
3288 mutex_unlock(&devlink->lock);
3291 mutex_unlock(&devlink_mutex);
3297 static int devlink_nl_cmd_port_param_get_doit(struct sk_buff *skb,
3298 struct genl_info *info)
3300 struct devlink_port *devlink_port = info->user_ptr[0];
3301 struct devlink_param_item *param_item;
3302 struct sk_buff *msg;
3305 param_item = devlink_param_get_from_info(&devlink_port->param_list,
3310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3314 err = devlink_nl_param_fill(msg, devlink_port->devlink,
3315 devlink_port->index, param_item,
3316 DEVLINK_CMD_PORT_PARAM_GET,
3317 info->snd_portid, info->snd_seq, 0);
3323 return genlmsg_reply(msg, info);
3326 static int devlink_nl_cmd_port_param_set_doit(struct sk_buff *skb,
3327 struct genl_info *info)
3329 struct devlink_port *devlink_port = info->user_ptr[0];
3331 return __devlink_nl_cmd_param_set_doit(devlink_port->devlink,
3332 devlink_port->index,
3333 &devlink_port->param_list, info,
3334 DEVLINK_CMD_PORT_PARAM_NEW);
3337 static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
3338 struct devlink *devlink,
3339 struct devlink_snapshot *snapshot)
3341 struct nlattr *snap_attr;
3344 snap_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
3348 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
3350 goto nla_put_failure;
3352 nla_nest_end(msg, snap_attr);
3356 nla_nest_cancel(msg, snap_attr);
3360 static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
3361 struct devlink *devlink,
3362 struct devlink_region *region)
3364 struct devlink_snapshot *snapshot;
3365 struct nlattr *snapshots_attr;
3368 snapshots_attr = nla_nest_start_noflag(msg,
3369 DEVLINK_ATTR_REGION_SNAPSHOTS);
3370 if (!snapshots_attr)
3373 list_for_each_entry(snapshot, ®ion->snapshot_list, list) {
3374 err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
3376 goto nla_put_failure;
3379 nla_nest_end(msg, snapshots_attr);
3383 nla_nest_cancel(msg, snapshots_attr);
3387 static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
3388 enum devlink_command cmd, u32 portid,
3390 struct devlink_region *region)
3395 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3399 err = devlink_nl_put_handle(msg, devlink);
3401 goto nla_put_failure;
3403 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
3405 goto nla_put_failure;
3407 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3411 goto nla_put_failure;
3413 err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
3415 goto nla_put_failure;
3417 genlmsg_end(msg, hdr);
3421 genlmsg_cancel(msg, hdr);
3425 static void devlink_nl_region_notify(struct devlink_region *region,
3426 struct devlink_snapshot *snapshot,
3427 enum devlink_command cmd)
3429 struct devlink *devlink = region->devlink;
3430 struct sk_buff *msg;
3434 WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
3436 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3440 hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
3444 err = devlink_nl_put_handle(msg, devlink);
3446 goto out_cancel_msg;
3448 err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
3451 goto out_cancel_msg;
3454 err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
3457 goto out_cancel_msg;
3459 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
3460 region->size, DEVLINK_ATTR_PAD);
3462 goto out_cancel_msg;
3464 genlmsg_end(msg, hdr);
3466 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
3467 msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
3472 genlmsg_cancel(msg, hdr);
3477 static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
3478 struct genl_info *info)
3480 struct devlink *devlink = info->user_ptr[0];
3481 struct devlink_region *region;
3482 const char *region_name;
3483 struct sk_buff *msg;
3486 if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
3489 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3490 region = devlink_region_get_by_name(devlink, region_name);
3494 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3498 err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
3499 info->snd_portid, info->snd_seq, 0,
3506 return genlmsg_reply(msg, info);
3509 static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
3510 struct netlink_callback *cb)
3512 struct devlink_region *region;
3513 struct devlink *devlink;
3514 int start = cb->args[0];
3518 mutex_lock(&devlink_mutex);
3519 list_for_each_entry(devlink, &devlink_list, list) {
3520 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3523 mutex_lock(&devlink->lock);
3524 list_for_each_entry(region, &devlink->region_list, list) {
3529 err = devlink_nl_region_fill(msg, devlink,
3530 DEVLINK_CMD_REGION_GET,
3531 NETLINK_CB(cb->skb).portid,
3533 NLM_F_MULTI, region);
3535 mutex_unlock(&devlink->lock);
3540 mutex_unlock(&devlink->lock);
3543 mutex_unlock(&devlink_mutex);
3548 static int devlink_nl_cmd_region_del(struct sk_buff *skb,
3549 struct genl_info *info)
3551 struct devlink *devlink = info->user_ptr[0];
3552 struct devlink_snapshot *snapshot;
3553 struct devlink_region *region;
3554 const char *region_name;
3557 if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
3558 !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
3561 region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
3562 snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3564 region = devlink_region_get_by_name(devlink, region_name);
3568 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3572 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
3573 devlink_region_snapshot_del(snapshot);
3577 static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
3578 struct devlink *devlink,
3579 u8 *chunk, u32 chunk_size,
3582 struct nlattr *chunk_attr;
3585 chunk_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_REGION_CHUNK);
3589 err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
3591 goto nla_put_failure;
3593 err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
3596 goto nla_put_failure;
3598 nla_nest_end(msg, chunk_attr);
3602 nla_nest_cancel(msg, chunk_attr);
3606 #define DEVLINK_REGION_READ_CHUNK_SIZE 256
3608 static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
3609 struct devlink *devlink,
3610 struct devlink_region *region,
3611 struct nlattr **attrs,
3617 struct devlink_snapshot *snapshot;
3618 u64 curr_offset = start_offset;
3622 *new_offset = start_offset;
3624 snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
3625 snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
3629 if (end_offset > snapshot->data_len || dump)
3630 end_offset = snapshot->data_len;
3632 while (curr_offset < end_offset) {
3636 if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
3637 data_size = end_offset - curr_offset;
3639 data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
3641 data = &snapshot->data[curr_offset];
3642 err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
3648 curr_offset += data_size;
3650 *new_offset = curr_offset;
3655 static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
3656 struct netlink_callback *cb)
3658 u64 ret_offset, start_offset, end_offset = 0;
3659 struct devlink_region *region;
3660 struct nlattr *chunks_attr;
3661 const char *region_name;
3662 struct devlink *devlink;
3663 struct nlattr **attrs;
3668 start_offset = *((u64 *)&cb->args[0]);
3670 attrs = kmalloc_array(DEVLINK_ATTR_MAX + 1, sizeof(*attrs), GFP_KERNEL);
3674 err = nlmsg_parse_deprecated(cb->nlh,
3675 GENL_HDRLEN + devlink_nl_family.hdrsize,
3676 attrs, DEVLINK_ATTR_MAX,
3677 devlink_nl_family.policy, cb->extack);
3681 mutex_lock(&devlink_mutex);
3682 devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
3683 if (IS_ERR(devlink)) {
3684 err = PTR_ERR(devlink);
3688 mutex_lock(&devlink->lock);
3690 if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
3691 !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]) {
3696 region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
3697 region = devlink_region_get_by_name(devlink, region_name);
3703 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3704 &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
3705 DEVLINK_CMD_REGION_READ);
3711 err = devlink_nl_put_handle(skb, devlink);
3713 goto nla_put_failure;
3715 err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
3717 goto nla_put_failure;
3719 chunks_attr = nla_nest_start_noflag(skb, DEVLINK_ATTR_REGION_CHUNKS);
3722 goto nla_put_failure;
3725 if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
3726 attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
3729 nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3731 end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
3732 end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
3736 err = devlink_nl_region_read_snapshot_fill(skb, devlink,
3742 if (err && err != -EMSGSIZE)
3743 goto nla_put_failure;
3745 /* Check if there was any progress done to prevent infinite loop */
3746 if (ret_offset == start_offset) {
3748 goto nla_put_failure;
3751 *((u64 *)&cb->args[0]) = ret_offset;
3753 nla_nest_end(skb, chunks_attr);
3754 genlmsg_end(skb, hdr);
3755 mutex_unlock(&devlink->lock);
3756 mutex_unlock(&devlink_mutex);
3762 genlmsg_cancel(skb, hdr);
3764 mutex_unlock(&devlink->lock);
3766 mutex_unlock(&devlink_mutex);
3772 struct devlink_info_req {
3773 struct sk_buff *msg;
3776 int devlink_info_driver_name_put(struct devlink_info_req *req, const char *name)
3778 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_DRIVER_NAME, name);
3780 EXPORT_SYMBOL_GPL(devlink_info_driver_name_put);
3782 int devlink_info_serial_number_put(struct devlink_info_req *req, const char *sn)
3784 return nla_put_string(req->msg, DEVLINK_ATTR_INFO_SERIAL_NUMBER, sn);
3786 EXPORT_SYMBOL_GPL(devlink_info_serial_number_put);
3788 static int devlink_info_version_put(struct devlink_info_req *req, int attr,
3789 const char *version_name,
3790 const char *version_value)
3792 struct nlattr *nest;
3795 nest = nla_nest_start_noflag(req->msg, attr);
3799 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_NAME,
3802 goto nla_put_failure;
3804 err = nla_put_string(req->msg, DEVLINK_ATTR_INFO_VERSION_VALUE,
3807 goto nla_put_failure;
3809 nla_nest_end(req->msg, nest);
3814 nla_nest_cancel(req->msg, nest);
3818 int devlink_info_version_fixed_put(struct devlink_info_req *req,
3819 const char *version_name,
3820 const char *version_value)
3822 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_FIXED,
3823 version_name, version_value);
3825 EXPORT_SYMBOL_GPL(devlink_info_version_fixed_put);
3827 int devlink_info_version_stored_put(struct devlink_info_req *req,
3828 const char *version_name,
3829 const char *version_value)
3831 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_STORED,
3832 version_name, version_value);
3834 EXPORT_SYMBOL_GPL(devlink_info_version_stored_put);
3836 int devlink_info_version_running_put(struct devlink_info_req *req,
3837 const char *version_name,
3838 const char *version_value)
3840 return devlink_info_version_put(req, DEVLINK_ATTR_INFO_VERSION_RUNNING,
3841 version_name, version_value);
3843 EXPORT_SYMBOL_GPL(devlink_info_version_running_put);
3846 devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
3847 enum devlink_command cmd, u32 portid,
3848 u32 seq, int flags, struct netlink_ext_ack *extack)
3850 struct devlink_info_req req;
3854 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
3859 if (devlink_nl_put_handle(msg, devlink))
3860 goto err_cancel_msg;
3863 err = devlink->ops->info_get(devlink, &req, extack);
3865 goto err_cancel_msg;
3867 genlmsg_end(msg, hdr);
3871 genlmsg_cancel(msg, hdr);
3875 static int devlink_nl_cmd_info_get_doit(struct sk_buff *skb,
3876 struct genl_info *info)
3878 struct devlink *devlink = info->user_ptr[0];
3879 struct sk_buff *msg;
3882 if (!devlink->ops->info_get)
3885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3889 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3890 info->snd_portid, info->snd_seq, 0,
3897 return genlmsg_reply(msg, info);
3900 static int devlink_nl_cmd_info_get_dumpit(struct sk_buff *msg,
3901 struct netlink_callback *cb)
3903 struct devlink *devlink;
3904 int start = cb->args[0];
3908 mutex_lock(&devlink_mutex);
3909 list_for_each_entry(devlink, &devlink_list, list) {
3910 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
3917 if (!devlink->ops->info_get) {
3922 mutex_lock(&devlink->lock);
3923 err = devlink_nl_info_fill(msg, devlink, DEVLINK_CMD_INFO_GET,
3924 NETLINK_CB(cb->skb).portid,
3925 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3927 mutex_unlock(&devlink->lock);
3932 mutex_unlock(&devlink_mutex);
3938 struct devlink_fmsg_item {
3939 struct list_head list;
3946 struct devlink_fmsg {
3947 struct list_head item_list;
3950 static struct devlink_fmsg *devlink_fmsg_alloc(void)
3952 struct devlink_fmsg *fmsg;
3954 fmsg = kzalloc(sizeof(*fmsg), GFP_KERNEL);
3958 INIT_LIST_HEAD(&fmsg->item_list);
3963 static void devlink_fmsg_free(struct devlink_fmsg *fmsg)
3965 struct devlink_fmsg_item *item, *tmp;
3967 list_for_each_entry_safe(item, tmp, &fmsg->item_list, list) {
3968 list_del(&item->list);
3974 static int devlink_fmsg_nest_common(struct devlink_fmsg *fmsg,
3977 struct devlink_fmsg_item *item;
3979 item = kzalloc(sizeof(*item), GFP_KERNEL);
3983 item->attrtype = attrtype;
3984 list_add_tail(&item->list, &fmsg->item_list);
3989 int devlink_fmsg_obj_nest_start(struct devlink_fmsg *fmsg)
3991 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_OBJ_NEST_START);
3993 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_start);
3995 static int devlink_fmsg_nest_end(struct devlink_fmsg *fmsg)
3997 return devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_NEST_END);
4000 int devlink_fmsg_obj_nest_end(struct devlink_fmsg *fmsg)
4002 return devlink_fmsg_nest_end(fmsg);
4004 EXPORT_SYMBOL_GPL(devlink_fmsg_obj_nest_end);
4006 #define DEVLINK_FMSG_MAX_SIZE (GENLMSG_DEFAULT_SIZE - GENL_HDRLEN - NLA_HDRLEN)
4008 static int devlink_fmsg_put_name(struct devlink_fmsg *fmsg, const char *name)
4010 struct devlink_fmsg_item *item;
4012 if (strlen(name) + 1 > DEVLINK_FMSG_MAX_SIZE)
4015 item = kzalloc(sizeof(*item) + strlen(name) + 1, GFP_KERNEL);
4019 item->nla_type = NLA_NUL_STRING;
4020 item->len = strlen(name) + 1;
4021 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME;
4022 memcpy(&item->value, name, item->len);
4023 list_add_tail(&item->list, &fmsg->item_list);
4028 int devlink_fmsg_pair_nest_start(struct devlink_fmsg *fmsg, const char *name)
4032 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_PAIR_NEST_START);
4036 err = devlink_fmsg_put_name(fmsg, name);
4042 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_start);
4044 int devlink_fmsg_pair_nest_end(struct devlink_fmsg *fmsg)
4046 return devlink_fmsg_nest_end(fmsg);
4048 EXPORT_SYMBOL_GPL(devlink_fmsg_pair_nest_end);
4050 int devlink_fmsg_arr_pair_nest_start(struct devlink_fmsg *fmsg,
4055 err = devlink_fmsg_pair_nest_start(fmsg, name);
4059 err = devlink_fmsg_nest_common(fmsg, DEVLINK_ATTR_FMSG_ARR_NEST_START);
4065 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_start);
4067 int devlink_fmsg_arr_pair_nest_end(struct devlink_fmsg *fmsg)
4071 err = devlink_fmsg_nest_end(fmsg);
4075 err = devlink_fmsg_nest_end(fmsg);
4081 EXPORT_SYMBOL_GPL(devlink_fmsg_arr_pair_nest_end);
4083 static int devlink_fmsg_put_value(struct devlink_fmsg *fmsg,
4084 const void *value, u16 value_len,
4087 struct devlink_fmsg_item *item;
4089 if (value_len > DEVLINK_FMSG_MAX_SIZE)
4092 item = kzalloc(sizeof(*item) + value_len, GFP_KERNEL);
4096 item->nla_type = value_nla_type;
4097 item->len = value_len;
4098 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4099 memcpy(&item->value, value, item->len);
4100 list_add_tail(&item->list, &fmsg->item_list);
4105 int devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value)
4107 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG);
4109 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_put);
4111 int devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value)
4113 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8);
4115 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_put);
4117 int devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value)
4119 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32);
4121 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put);
4123 int devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value)
4125 return devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64);
4127 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_put);
4129 int devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value)
4131 return devlink_fmsg_put_value(fmsg, value, strlen(value) + 1,
4134 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put);
4136 int devlink_fmsg_binary_put(struct devlink_fmsg *fmsg, const void *value,
4139 return devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY);
4141 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put);
4143 int devlink_fmsg_bool_pair_put(struct devlink_fmsg *fmsg, const char *name,
4148 err = devlink_fmsg_pair_nest_start(fmsg, name);
4152 err = devlink_fmsg_bool_put(fmsg, value);
4156 err = devlink_fmsg_pair_nest_end(fmsg);
4162 EXPORT_SYMBOL_GPL(devlink_fmsg_bool_pair_put);
4164 int devlink_fmsg_u8_pair_put(struct devlink_fmsg *fmsg, const char *name,
4169 err = devlink_fmsg_pair_nest_start(fmsg, name);
4173 err = devlink_fmsg_u8_put(fmsg, value);
4177 err = devlink_fmsg_pair_nest_end(fmsg);
4183 EXPORT_SYMBOL_GPL(devlink_fmsg_u8_pair_put);
4185 int devlink_fmsg_u32_pair_put(struct devlink_fmsg *fmsg, const char *name,
4190 err = devlink_fmsg_pair_nest_start(fmsg, name);
4194 err = devlink_fmsg_u32_put(fmsg, value);
4198 err = devlink_fmsg_pair_nest_end(fmsg);
4204 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_pair_put);
4206 int devlink_fmsg_u64_pair_put(struct devlink_fmsg *fmsg, const char *name,
4211 err = devlink_fmsg_pair_nest_start(fmsg, name);
4215 err = devlink_fmsg_u64_put(fmsg, value);
4219 err = devlink_fmsg_pair_nest_end(fmsg);
4225 EXPORT_SYMBOL_GPL(devlink_fmsg_u64_pair_put);
4227 int devlink_fmsg_string_pair_put(struct devlink_fmsg *fmsg, const char *name,
4232 err = devlink_fmsg_pair_nest_start(fmsg, name);
4236 err = devlink_fmsg_string_put(fmsg, value);
4240 err = devlink_fmsg_pair_nest_end(fmsg);
4246 EXPORT_SYMBOL_GPL(devlink_fmsg_string_pair_put);
4248 int devlink_fmsg_binary_pair_put(struct devlink_fmsg *fmsg, const char *name,
4249 const void *value, u16 value_len)
4253 err = devlink_fmsg_pair_nest_start(fmsg, name);
4257 err = devlink_fmsg_binary_put(fmsg, value, value_len);
4261 err = devlink_fmsg_pair_nest_end(fmsg);
4267 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put);
4270 devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4272 switch (msg->nla_type) {
4277 case NLA_NUL_STRING:
4279 return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE,
4287 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb)
4289 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA;
4292 switch (msg->nla_type) {
4294 /* Always provide flag data, regardless of its value */
4295 tmp = *(bool *) msg->value;
4297 return nla_put_u8(skb, attrtype, tmp);
4299 return nla_put_u8(skb, attrtype, *(u8 *) msg->value);
4301 return nla_put_u32(skb, attrtype, *(u32 *) msg->value);
4303 return nla_put_u64_64bit(skb, attrtype, *(u64 *) msg->value,
4305 case NLA_NUL_STRING:
4306 return nla_put_string(skb, attrtype, (char *) &msg->value);
4308 return nla_put(skb, attrtype, msg->len, (void *) &msg->value);
4315 devlink_fmsg_prepare_skb(struct devlink_fmsg *fmsg, struct sk_buff *skb,
4318 struct devlink_fmsg_item *item;
4319 struct nlattr *fmsg_nlattr;
4323 fmsg_nlattr = nla_nest_start_noflag(skb, DEVLINK_ATTR_FMSG);
4327 list_for_each_entry(item, &fmsg->item_list, list) {
4333 switch (item->attrtype) {
4334 case DEVLINK_ATTR_FMSG_OBJ_NEST_START:
4335 case DEVLINK_ATTR_FMSG_PAIR_NEST_START:
4336 case DEVLINK_ATTR_FMSG_ARR_NEST_START:
4337 case DEVLINK_ATTR_FMSG_NEST_END:
4338 err = nla_put_flag(skb, item->attrtype);
4340 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA:
4341 err = devlink_fmsg_item_fill_type(item, skb);
4344 err = devlink_fmsg_item_fill_data(item, skb);
4346 case DEVLINK_ATTR_FMSG_OBJ_NAME:
4347 err = nla_put_string(skb, item->attrtype,
4348 (char *) &item->value);
4360 nla_nest_end(skb, fmsg_nlattr);
4364 static int devlink_fmsg_snd(struct devlink_fmsg *fmsg,
4365 struct genl_info *info,
4366 enum devlink_command cmd, int flags)
4368 struct nlmsghdr *nlh;
4369 struct sk_buff *skb;
4376 int tmp_index = index;
4378 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4382 hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
4383 &devlink_nl_family, flags | NLM_F_MULTI, cmd);
4386 goto nla_put_failure;
4389 err = devlink_fmsg_prepare_skb(fmsg, skb, &index);
4392 else if (err != -EMSGSIZE || tmp_index == index)
4393 goto nla_put_failure;
4395 genlmsg_end(skb, hdr);
4396 err = genlmsg_reply(skb, info);
4401 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
4404 nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
4405 NLMSG_DONE, 0, flags | NLM_F_MULTI);
4408 goto nla_put_failure;
4411 return genlmsg_reply(skb, info);
4418 struct devlink_health_reporter {
4419 struct list_head list;
4421 const struct devlink_health_reporter_ops *ops;
4422 struct devlink *devlink;
4423 struct devlink_fmsg *dump_fmsg;
4424 struct mutex dump_lock; /* lock parallel read/write from dump buffers */
4425 u64 graceful_period;
4431 u64 last_recovery_ts;
4432 refcount_t refcount;
4436 devlink_health_reporter_priv(struct devlink_health_reporter *reporter)
4438 return reporter->priv;
4440 EXPORT_SYMBOL_GPL(devlink_health_reporter_priv);
4442 static struct devlink_health_reporter *
4443 devlink_health_reporter_find_by_name(struct devlink *devlink,
4444 const char *reporter_name)
4446 struct devlink_health_reporter *reporter;
4448 lockdep_assert_held(&devlink->reporters_lock);
4449 list_for_each_entry(reporter, &devlink->reporter_list, list)
4450 if (!strcmp(reporter->ops->name, reporter_name))
4456 * devlink_health_reporter_create - create devlink health reporter
4460 * @graceful_period: to avoid recovery loops, in msecs
4461 * @auto_recover: auto recover when error occurs
4464 struct devlink_health_reporter *
4465 devlink_health_reporter_create(struct devlink *devlink,
4466 const struct devlink_health_reporter_ops *ops,
4467 u64 graceful_period, bool auto_recover,
4470 struct devlink_health_reporter *reporter;
4472 mutex_lock(&devlink->reporters_lock);
4473 if (devlink_health_reporter_find_by_name(devlink, ops->name)) {
4474 reporter = ERR_PTR(-EEXIST);
4478 if (WARN_ON(auto_recover && !ops->recover) ||
4479 WARN_ON(graceful_period && !ops->recover)) {
4480 reporter = ERR_PTR(-EINVAL);
4484 reporter = kzalloc(sizeof(*reporter), GFP_KERNEL);
4486 reporter = ERR_PTR(-ENOMEM);
4490 reporter->priv = priv;
4491 reporter->ops = ops;
4492 reporter->devlink = devlink;
4493 reporter->graceful_period = graceful_period;
4494 reporter->auto_recover = auto_recover;
4495 mutex_init(&reporter->dump_lock);
4496 refcount_set(&reporter->refcount, 1);
4497 list_add_tail(&reporter->list, &devlink->reporter_list);
4499 mutex_unlock(&devlink->reporters_lock);
4502 EXPORT_SYMBOL_GPL(devlink_health_reporter_create);
4505 * devlink_health_reporter_destroy - destroy devlink health reporter
4507 * @reporter: devlink health reporter to destroy
4510 devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
4512 mutex_lock(&reporter->devlink->reporters_lock);
4513 list_del(&reporter->list);
4514 mutex_unlock(&reporter->devlink->reporters_lock);
4515 while (refcount_read(&reporter->refcount) > 1)
4517 mutex_destroy(&reporter->dump_lock);
4518 if (reporter->dump_fmsg)
4519 devlink_fmsg_free(reporter->dump_fmsg);
4522 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
4525 devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
4526 enum devlink_health_reporter_state state)
4528 if (WARN_ON(state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY &&
4529 state != DEVLINK_HEALTH_REPORTER_STATE_ERROR))
4532 if (reporter->health_state == state)
4535 reporter->health_state = state;
4536 trace_devlink_health_reporter_state_update(reporter->devlink,
4537 reporter->ops->name, state);
4539 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
4542 devlink_health_reporter_recover(struct devlink_health_reporter *reporter,
4547 if (!reporter->ops->recover)
4550 err = reporter->ops->recover(reporter, priv_ctx);
4554 reporter->recovery_count++;
4555 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_HEALTHY;
4556 reporter->last_recovery_ts = jiffies;
4562 devlink_health_dump_clear(struct devlink_health_reporter *reporter)
4564 if (!reporter->dump_fmsg)
4566 devlink_fmsg_free(reporter->dump_fmsg);
4567 reporter->dump_fmsg = NULL;
4570 static int devlink_health_do_dump(struct devlink_health_reporter *reporter,
4575 if (!reporter->ops->dump)
4578 if (reporter->dump_fmsg)
4581 reporter->dump_fmsg = devlink_fmsg_alloc();
4582 if (!reporter->dump_fmsg) {
4587 err = devlink_fmsg_obj_nest_start(reporter->dump_fmsg);
4591 err = reporter->ops->dump(reporter, reporter->dump_fmsg,
4596 err = devlink_fmsg_obj_nest_end(reporter->dump_fmsg);
4600 reporter->dump_ts = jiffies;
4605 devlink_health_dump_clear(reporter);
4609 int devlink_health_report(struct devlink_health_reporter *reporter,
4610 const char *msg, void *priv_ctx)
4612 enum devlink_health_reporter_state prev_health_state;
4613 struct devlink *devlink = reporter->devlink;
4615 /* write a log message of the current error */
4617 trace_devlink_health_report(devlink, reporter->ops->name, msg);
4618 reporter->error_count++;
4619 prev_health_state = reporter->health_state;
4620 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4622 /* abort if the previous error wasn't recovered */
4623 if (reporter->auto_recover &&
4624 (prev_health_state != DEVLINK_HEALTH_REPORTER_STATE_HEALTHY ||
4625 jiffies - reporter->last_recovery_ts <
4626 msecs_to_jiffies(reporter->graceful_period))) {
4627 trace_devlink_health_recover_aborted(devlink,
4628 reporter->ops->name,
4629 reporter->health_state,
4631 reporter->last_recovery_ts);
4635 reporter->health_state = DEVLINK_HEALTH_REPORTER_STATE_ERROR;
4637 mutex_lock(&reporter->dump_lock);
4638 /* store current dump of current error, for later analysis */
4639 devlink_health_do_dump(reporter, priv_ctx);
4640 mutex_unlock(&reporter->dump_lock);
4642 if (reporter->auto_recover)
4643 return devlink_health_reporter_recover(reporter, priv_ctx);
4647 EXPORT_SYMBOL_GPL(devlink_health_report);
4649 static struct devlink_health_reporter *
4650 devlink_health_reporter_get_from_info(struct devlink *devlink,
4651 struct genl_info *info)
4653 struct devlink_health_reporter *reporter;
4654 char *reporter_name;
4656 if (!info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
4660 nla_data(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
4661 mutex_lock(&devlink->reporters_lock);
4662 reporter = devlink_health_reporter_find_by_name(devlink, reporter_name);
4664 refcount_inc(&reporter->refcount);
4665 mutex_unlock(&devlink->reporters_lock);
4670 devlink_health_reporter_put(struct devlink_health_reporter *reporter)
4672 refcount_dec(&reporter->refcount);
4676 devlink_nl_health_reporter_fill(struct sk_buff *msg,
4677 struct devlink *devlink,
4678 struct devlink_health_reporter *reporter,
4679 enum devlink_command cmd, u32 portid,
4682 struct nlattr *reporter_attr;
4685 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
4689 if (devlink_nl_put_handle(msg, devlink))
4690 goto genlmsg_cancel;
4692 reporter_attr = nla_nest_start_noflag(msg,
4693 DEVLINK_ATTR_HEALTH_REPORTER);
4695 goto genlmsg_cancel;
4696 if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
4697 reporter->ops->name))
4698 goto reporter_nest_cancel;
4699 if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
4700 reporter->health_state))
4701 goto reporter_nest_cancel;
4702 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
4703 reporter->error_count, DEVLINK_ATTR_PAD))
4704 goto reporter_nest_cancel;
4705 if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
4706 reporter->recovery_count, DEVLINK_ATTR_PAD))
4707 goto reporter_nest_cancel;
4708 if (reporter->ops->recover &&
4709 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
4710 reporter->graceful_period,
4712 goto reporter_nest_cancel;
4713 if (reporter->ops->recover &&
4714 nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
4715 reporter->auto_recover))
4716 goto reporter_nest_cancel;
4717 if (reporter->dump_fmsg &&
4718 nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
4719 jiffies_to_msecs(reporter->dump_ts),
4721 goto reporter_nest_cancel;
4723 nla_nest_end(msg, reporter_attr);
4724 genlmsg_end(msg, hdr);
4727 reporter_nest_cancel:
4728 nla_nest_end(msg, reporter_attr);
4730 genlmsg_cancel(msg, hdr);
4734 static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
4735 struct genl_info *info)
4737 struct devlink *devlink = info->user_ptr[0];
4738 struct devlink_health_reporter *reporter;
4739 struct sk_buff *msg;
4742 reporter = devlink_health_reporter_get_from_info(devlink, info);
4746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4752 err = devlink_nl_health_reporter_fill(msg, devlink, reporter,
4753 DEVLINK_CMD_HEALTH_REPORTER_GET,
4754 info->snd_portid, info->snd_seq,
4761 err = genlmsg_reply(msg, info);
4763 devlink_health_reporter_put(reporter);
4768 devlink_nl_cmd_health_reporter_get_dumpit(struct sk_buff *msg,
4769 struct netlink_callback *cb)
4771 struct devlink_health_reporter *reporter;
4772 struct devlink *devlink;
4773 int start = cb->args[0];
4777 mutex_lock(&devlink_mutex);
4778 list_for_each_entry(devlink, &devlink_list, list) {
4779 if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
4781 mutex_lock(&devlink->reporters_lock);
4782 list_for_each_entry(reporter, &devlink->reporter_list,
4788 err = devlink_nl_health_reporter_fill(msg, devlink,
4790 DEVLINK_CMD_HEALTH_REPORTER_GET,
4791 NETLINK_CB(cb->skb).portid,
4795 mutex_unlock(&devlink->reporters_lock);
4800 mutex_unlock(&devlink->reporters_lock);
4803 mutex_unlock(&devlink_mutex);
4810 devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
4811 struct genl_info *info)
4813 struct devlink *devlink = info->user_ptr[0];
4814 struct devlink_health_reporter *reporter;
4817 reporter = devlink_health_reporter_get_from_info(devlink, info);
4821 if (!reporter->ops->recover &&
4822 (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
4823 info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])) {
4828 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
4829 reporter->graceful_period =
4830 nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
4832 if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
4833 reporter->auto_recover =
4834 nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
4836 devlink_health_reporter_put(reporter);
4839 devlink_health_reporter_put(reporter);
4843 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
4844 struct genl_info *info)
4846 struct devlink *devlink = info->user_ptr[0];
4847 struct devlink_health_reporter *reporter;
4850 reporter = devlink_health_reporter_get_from_info(devlink, info);
4854 err = devlink_health_reporter_recover(reporter, NULL);
4856 devlink_health_reporter_put(reporter);
4860 static int devlink_nl_cmd_health_reporter_diagnose_doit(struct sk_buff *skb,
4861 struct genl_info *info)
4863 struct devlink *devlink = info->user_ptr[0];
4864 struct devlink_health_reporter *reporter;
4865 struct devlink_fmsg *fmsg;
4868 reporter = devlink_health_reporter_get_from_info(devlink, info);
4872 if (!reporter->ops->diagnose) {
4873 devlink_health_reporter_put(reporter);
4877 fmsg = devlink_fmsg_alloc();
4879 devlink_health_reporter_put(reporter);
4883 err = devlink_fmsg_obj_nest_start(fmsg);
4887 err = reporter->ops->diagnose(reporter, fmsg);
4891 err = devlink_fmsg_obj_nest_end(fmsg);
4895 err = devlink_fmsg_snd(fmsg, info,
4896 DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE, 0);
4899 devlink_fmsg_free(fmsg);
4900 devlink_health_reporter_put(reporter);
4904 static int devlink_nl_cmd_health_reporter_dump_get_doit(struct sk_buff *skb,
4905 struct genl_info *info)
4907 struct devlink *devlink = info->user_ptr[0];
4908 struct devlink_health_reporter *reporter;
4911 reporter = devlink_health_reporter_get_from_info(devlink, info);
4915 if (!reporter->ops->dump) {
4916 devlink_health_reporter_put(reporter);
4920 mutex_lock(&reporter->dump_lock);
4921 err = devlink_health_do_dump(reporter, NULL);
4925 err = devlink_fmsg_snd(reporter->dump_fmsg, info,
4926 DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET, 0);
4929 mutex_unlock(&reporter->dump_lock);
4930 devlink_health_reporter_put(reporter);
4935 devlink_nl_cmd_health_reporter_dump_clear_doit(struct sk_buff *skb,
4936 struct genl_info *info)
4938 struct devlink *devlink = info->user_ptr[0];
4939 struct devlink_health_reporter *reporter;
4941 reporter = devlink_health_reporter_get_from_info(devlink, info);
4945 if (!reporter->ops->dump) {
4946 devlink_health_reporter_put(reporter);
4950 mutex_lock(&reporter->dump_lock);
4951 devlink_health_dump_clear(reporter);
4952 mutex_unlock(&reporter->dump_lock);
4953 devlink_health_reporter_put(reporter);
4957 static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
4958 [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
4959 [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
4960 [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
4961 [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
4962 [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
4963 [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
4964 [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
4965 [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
4966 [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
4967 [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
4968 [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
4969 [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
4970 [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
4971 [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
4972 [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
4973 [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
4974 [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
4975 [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
4976 [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
4977 [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
4978 [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
4979 [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
4980 [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
4981 [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
4982 [DEVLINK_ATTR_HEALTH_REPORTER_NAME] = { .type = NLA_NUL_STRING },
4983 [DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] = { .type = NLA_U64 },
4984 [DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER] = { .type = NLA_U8 },
4985 [DEVLINK_ATTR_FLASH_UPDATE_FILE_NAME] = { .type = NLA_NUL_STRING },
4986 [DEVLINK_ATTR_FLASH_UPDATE_COMPONENT] = { .type = NLA_NUL_STRING },
4989 static const struct genl_ops devlink_nl_ops[] = {
4991 .cmd = DEVLINK_CMD_GET,
4992 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4993 .doit = devlink_nl_cmd_get_doit,
4994 .dumpit = devlink_nl_cmd_get_dumpit,
4995 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
4996 /* can be retrieved by unprivileged users */
4999 .cmd = DEVLINK_CMD_PORT_GET,
5000 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5001 .doit = devlink_nl_cmd_port_get_doit,
5002 .dumpit = devlink_nl_cmd_port_get_dumpit,
5003 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5004 /* can be retrieved by unprivileged users */
5007 .cmd = DEVLINK_CMD_PORT_SET,
5008 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5009 .doit = devlink_nl_cmd_port_set_doit,
5010 .flags = GENL_ADMIN_PERM,
5011 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5014 .cmd = DEVLINK_CMD_PORT_SPLIT,
5015 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5016 .doit = devlink_nl_cmd_port_split_doit,
5017 .flags = GENL_ADMIN_PERM,
5018 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5019 DEVLINK_NL_FLAG_NO_LOCK,
5022 .cmd = DEVLINK_CMD_PORT_UNSPLIT,
5023 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5024 .doit = devlink_nl_cmd_port_unsplit_doit,
5025 .flags = GENL_ADMIN_PERM,
5026 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5027 DEVLINK_NL_FLAG_NO_LOCK,
5030 .cmd = DEVLINK_CMD_SB_GET,
5031 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5032 .doit = devlink_nl_cmd_sb_get_doit,
5033 .dumpit = devlink_nl_cmd_sb_get_dumpit,
5034 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5035 DEVLINK_NL_FLAG_NEED_SB,
5036 /* can be retrieved by unprivileged users */
5039 .cmd = DEVLINK_CMD_SB_POOL_GET,
5040 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5041 .doit = devlink_nl_cmd_sb_pool_get_doit,
5042 .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
5043 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5044 DEVLINK_NL_FLAG_NEED_SB,
5045 /* can be retrieved by unprivileged users */
5048 .cmd = DEVLINK_CMD_SB_POOL_SET,
5049 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5050 .doit = devlink_nl_cmd_sb_pool_set_doit,
5051 .flags = GENL_ADMIN_PERM,
5052 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5053 DEVLINK_NL_FLAG_NEED_SB,
5056 .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
5057 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5058 .doit = devlink_nl_cmd_sb_port_pool_get_doit,
5059 .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
5060 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5061 DEVLINK_NL_FLAG_NEED_SB,
5062 /* can be retrieved by unprivileged users */
5065 .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
5066 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5067 .doit = devlink_nl_cmd_sb_port_pool_set_doit,
5068 .flags = GENL_ADMIN_PERM,
5069 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5070 DEVLINK_NL_FLAG_NEED_SB,
5073 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
5074 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5075 .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
5076 .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
5077 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5078 DEVLINK_NL_FLAG_NEED_SB,
5079 /* can be retrieved by unprivileged users */
5082 .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
5083 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5084 .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
5085 .flags = GENL_ADMIN_PERM,
5086 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
5087 DEVLINK_NL_FLAG_NEED_SB,
5090 .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
5091 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5092 .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
5093 .flags = GENL_ADMIN_PERM,
5094 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5095 DEVLINK_NL_FLAG_NEED_SB,
5098 .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
5099 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5100 .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
5101 .flags = GENL_ADMIN_PERM,
5102 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5103 DEVLINK_NL_FLAG_NEED_SB,
5106 .cmd = DEVLINK_CMD_ESWITCH_GET,
5107 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5108 .doit = devlink_nl_cmd_eswitch_get_doit,
5109 .flags = GENL_ADMIN_PERM,
5110 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5113 .cmd = DEVLINK_CMD_ESWITCH_SET,
5114 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5115 .doit = devlink_nl_cmd_eswitch_set_doit,
5116 .flags = GENL_ADMIN_PERM,
5117 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5118 DEVLINK_NL_FLAG_NO_LOCK,
5121 .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
5122 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5123 .doit = devlink_nl_cmd_dpipe_table_get,
5124 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5125 /* can be retrieved by unprivileged users */
5128 .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
5129 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5130 .doit = devlink_nl_cmd_dpipe_entries_get,
5131 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5132 /* can be retrieved by unprivileged users */
5135 .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
5136 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5137 .doit = devlink_nl_cmd_dpipe_headers_get,
5138 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5139 /* can be retrieved by unprivileged users */
5142 .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
5143 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5144 .doit = devlink_nl_cmd_dpipe_table_counters_set,
5145 .flags = GENL_ADMIN_PERM,
5146 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5149 .cmd = DEVLINK_CMD_RESOURCE_SET,
5150 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5151 .doit = devlink_nl_cmd_resource_set,
5152 .flags = GENL_ADMIN_PERM,
5153 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5156 .cmd = DEVLINK_CMD_RESOURCE_DUMP,
5157 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5158 .doit = devlink_nl_cmd_resource_dump,
5159 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5160 /* can be retrieved by unprivileged users */
5163 .cmd = DEVLINK_CMD_RELOAD,
5164 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5165 .doit = devlink_nl_cmd_reload,
5166 .flags = GENL_ADMIN_PERM,
5167 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5168 DEVLINK_NL_FLAG_NO_LOCK,
5171 .cmd = DEVLINK_CMD_PARAM_GET,
5172 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5173 .doit = devlink_nl_cmd_param_get_doit,
5174 .dumpit = devlink_nl_cmd_param_get_dumpit,
5175 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5176 /* can be retrieved by unprivileged users */
5179 .cmd = DEVLINK_CMD_PARAM_SET,
5180 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5181 .doit = devlink_nl_cmd_param_set_doit,
5182 .flags = GENL_ADMIN_PERM,
5183 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5186 .cmd = DEVLINK_CMD_PORT_PARAM_GET,
5187 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5188 .doit = devlink_nl_cmd_port_param_get_doit,
5189 .dumpit = devlink_nl_cmd_port_param_get_dumpit,
5190 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5191 /* can be retrieved by unprivileged users */
5194 .cmd = DEVLINK_CMD_PORT_PARAM_SET,
5195 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5196 .doit = devlink_nl_cmd_port_param_set_doit,
5197 .flags = GENL_ADMIN_PERM,
5198 .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
5201 .cmd = DEVLINK_CMD_REGION_GET,
5202 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5203 .doit = devlink_nl_cmd_region_get_doit,
5204 .dumpit = devlink_nl_cmd_region_get_dumpit,
5205 .flags = GENL_ADMIN_PERM,
5206 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5209 .cmd = DEVLINK_CMD_REGION_DEL,
5210 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5211 .doit = devlink_nl_cmd_region_del,
5212 .flags = GENL_ADMIN_PERM,
5213 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5216 .cmd = DEVLINK_CMD_REGION_READ,
5217 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5218 .dumpit = devlink_nl_cmd_region_read_dumpit,
5219 .flags = GENL_ADMIN_PERM,
5220 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5223 .cmd = DEVLINK_CMD_INFO_GET,
5224 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5225 .doit = devlink_nl_cmd_info_get_doit,
5226 .dumpit = devlink_nl_cmd_info_get_dumpit,
5227 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5228 /* can be retrieved by unprivileged users */
5231 .cmd = DEVLINK_CMD_HEALTH_REPORTER_GET,
5232 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5233 .doit = devlink_nl_cmd_health_reporter_get_doit,
5234 .dumpit = devlink_nl_cmd_health_reporter_get_dumpit,
5235 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5236 DEVLINK_NL_FLAG_NO_LOCK,
5237 /* can be retrieved by unprivileged users */
5240 .cmd = DEVLINK_CMD_HEALTH_REPORTER_SET,
5241 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5242 .doit = devlink_nl_cmd_health_reporter_set_doit,
5243 .flags = GENL_ADMIN_PERM,
5244 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5245 DEVLINK_NL_FLAG_NO_LOCK,
5248 .cmd = DEVLINK_CMD_HEALTH_REPORTER_RECOVER,
5249 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5250 .doit = devlink_nl_cmd_health_reporter_recover_doit,
5251 .flags = GENL_ADMIN_PERM,
5252 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5253 DEVLINK_NL_FLAG_NO_LOCK,
5256 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DIAGNOSE,
5257 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5258 .doit = devlink_nl_cmd_health_reporter_diagnose_doit,
5259 .flags = GENL_ADMIN_PERM,
5260 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5261 DEVLINK_NL_FLAG_NO_LOCK,
5264 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_GET,
5265 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5266 .doit = devlink_nl_cmd_health_reporter_dump_get_doit,
5267 .flags = GENL_ADMIN_PERM,
5268 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5269 DEVLINK_NL_FLAG_NO_LOCK,
5272 .cmd = DEVLINK_CMD_HEALTH_REPORTER_DUMP_CLEAR,
5273 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5274 .doit = devlink_nl_cmd_health_reporter_dump_clear_doit,
5275 .flags = GENL_ADMIN_PERM,
5276 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
5277 DEVLINK_NL_FLAG_NO_LOCK,
5280 .cmd = DEVLINK_CMD_FLASH_UPDATE,
5281 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
5282 .doit = devlink_nl_cmd_flash_update,
5283 .flags = GENL_ADMIN_PERM,
5284 .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
5288 static struct genl_family devlink_nl_family __ro_after_init = {
5289 .name = DEVLINK_GENL_NAME,
5290 .version = DEVLINK_GENL_VERSION,
5291 .maxattr = DEVLINK_ATTR_MAX,
5292 .policy = devlink_nl_policy,
5294 .pre_doit = devlink_nl_pre_doit,
5295 .post_doit = devlink_nl_post_doit,
5296 .module = THIS_MODULE,
5297 .ops = devlink_nl_ops,
5298 .n_ops = ARRAY_SIZE(devlink_nl_ops),
5299 .mcgrps = devlink_nl_mcgrps,
5300 .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
5304 * devlink_alloc - Allocate new devlink instance resources
5307 * @priv_size: size of user private data
5309 * Allocate new devlink instance resources, including devlink index
5312 struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
5314 struct devlink *devlink;
5319 devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
5323 devlink_net_set(devlink, &init_net);
5324 INIT_LIST_HEAD(&devlink->port_list);
5325 INIT_LIST_HEAD(&devlink->sb_list);
5326 INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
5327 INIT_LIST_HEAD(&devlink->resource_list);
5328 INIT_LIST_HEAD(&devlink->param_list);
5329 INIT_LIST_HEAD(&devlink->region_list);
5330 INIT_LIST_HEAD(&devlink->reporter_list);
5331 mutex_init(&devlink->lock);
5332 mutex_init(&devlink->reporters_lock);
5335 EXPORT_SYMBOL_GPL(devlink_alloc);
5338 * devlink_register - Register devlink instance
5341 * @dev: parent device
5343 int devlink_register(struct devlink *devlink, struct device *dev)
5345 mutex_lock(&devlink_mutex);
5347 list_add_tail(&devlink->list, &devlink_list);
5348 devlink_notify(devlink, DEVLINK_CMD_NEW);
5349 mutex_unlock(&devlink_mutex);
5352 EXPORT_SYMBOL_GPL(devlink_register);
5355 * devlink_unregister - Unregister devlink instance
5359 void devlink_unregister(struct devlink *devlink)
5361 mutex_lock(&devlink_mutex);
5362 devlink_notify(devlink, DEVLINK_CMD_DEL);
5363 list_del(&devlink->list);
5364 mutex_unlock(&devlink_mutex);
5366 EXPORT_SYMBOL_GPL(devlink_unregister);
5369 * devlink_free - Free devlink instance resources
5373 void devlink_free(struct devlink *devlink)
5375 mutex_destroy(&devlink->reporters_lock);
5376 mutex_destroy(&devlink->lock);
5377 WARN_ON(!list_empty(&devlink->reporter_list));
5378 WARN_ON(!list_empty(&devlink->region_list));
5379 WARN_ON(!list_empty(&devlink->param_list));
5380 WARN_ON(!list_empty(&devlink->resource_list));
5381 WARN_ON(!list_empty(&devlink->dpipe_table_list));
5382 WARN_ON(!list_empty(&devlink->sb_list));
5383 WARN_ON(!list_empty(&devlink->port_list));
5387 EXPORT_SYMBOL_GPL(devlink_free);
5390 * devlink_port_register - Register devlink port
5393 * @devlink_port: devlink port
5394 * @port_index: driver-specific numerical identifier of the port
5396 * Register devlink port with provided port index. User can use
5397 * any indexing, even hw-related one. devlink_port structure
5398 * is convenient to be embedded inside user driver private structure.
5399 * Note that the caller should take care of zeroing the devlink_port
5402 int devlink_port_register(struct devlink *devlink,
5403 struct devlink_port *devlink_port,
5404 unsigned int port_index)
5406 mutex_lock(&devlink->lock);
5407 if (devlink_port_index_exists(devlink, port_index)) {
5408 mutex_unlock(&devlink->lock);
5411 devlink_port->devlink = devlink;
5412 devlink_port->index = port_index;
5413 devlink_port->registered = true;
5414 spin_lock_init(&devlink_port->type_lock);
5415 list_add_tail(&devlink_port->list, &devlink->port_list);
5416 INIT_LIST_HEAD(&devlink_port->param_list);
5417 mutex_unlock(&devlink->lock);
5418 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5421 EXPORT_SYMBOL_GPL(devlink_port_register);
5424 * devlink_port_unregister - Unregister devlink port
5426 * @devlink_port: devlink port
5428 void devlink_port_unregister(struct devlink_port *devlink_port)
5430 struct devlink *devlink = devlink_port->devlink;
5432 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
5433 mutex_lock(&devlink->lock);
5434 list_del(&devlink_port->list);
5435 mutex_unlock(&devlink->lock);
5437 EXPORT_SYMBOL_GPL(devlink_port_unregister);
5439 static void __devlink_port_type_set(struct devlink_port *devlink_port,
5440 enum devlink_port_type type,
5443 if (WARN_ON(!devlink_port->registered))
5445 spin_lock(&devlink_port->type_lock);
5446 devlink_port->type = type;
5447 devlink_port->type_dev = type_dev;
5448 spin_unlock(&devlink_port->type_lock);
5449 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
5453 * devlink_port_type_eth_set - Set port type to Ethernet
5455 * @devlink_port: devlink port
5456 * @netdev: related netdevice
5458 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
5459 struct net_device *netdev)
5461 const struct net_device_ops *ops = netdev->netdev_ops;
5463 /* If driver registers devlink port, it should set devlink port
5464 * attributes accordingly so the compat functions are called
5465 * and the original ops are not used.
5467 if (ops->ndo_get_phys_port_name) {
5468 /* Some drivers use the same set of ndos for netdevs
5469 * that have devlink_port registered and also for
5470 * those who don't. Make sure that ndo_get_phys_port_name
5471 * returns -EOPNOTSUPP here in case it is defined.
5474 char name[IFNAMSIZ];
5477 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
5478 WARN_ON(err != -EOPNOTSUPP);
5480 if (ops->ndo_get_port_parent_id) {
5481 /* Some drivers use the same set of ndos for netdevs
5482 * that have devlink_port registered and also for
5483 * those who don't. Make sure that ndo_get_port_parent_id
5484 * returns -EOPNOTSUPP here in case it is defined.
5487 struct netdev_phys_item_id ppid;
5490 err = ops->ndo_get_port_parent_id(netdev, &ppid);
5491 WARN_ON(err != -EOPNOTSUPP);
5493 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, netdev);
5495 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
5498 * devlink_port_type_ib_set - Set port type to InfiniBand
5500 * @devlink_port: devlink port
5501 * @ibdev: related IB device
5503 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
5504 struct ib_device *ibdev)
5506 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
5508 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
5511 * devlink_port_type_clear - Clear port type
5513 * @devlink_port: devlink port
5515 void devlink_port_type_clear(struct devlink_port *devlink_port)
5517 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL);
5519 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
5522 * devlink_port_attrs_set - Set port attributes
5524 * @devlink_port: devlink port
5525 * @flavour: flavour of the port
5526 * @port_number: number of the port that is facing user, for example
5527 * the front panel port number
5528 * @split: indicates if this is split port
5529 * @split_subport_number: if the port is split, this is the number
5531 * @switch_id: if the port is part of switch, this is buffer with ID,
5532 * otwerwise this is NULL
5533 * @switch_id_len: length of the switch_id buffer
5535 void devlink_port_attrs_set(struct devlink_port *devlink_port,
5536 enum devlink_port_flavour flavour,
5537 u32 port_number, bool split,
5538 u32 split_subport_number,
5539 const unsigned char *switch_id,
5540 unsigned char switch_id_len)
5542 struct devlink_port_attrs *attrs = &devlink_port->attrs;
5544 if (WARN_ON(devlink_port->registered))
5547 attrs->flavour = flavour;
5548 attrs->port_number = port_number;
5549 attrs->split = split;
5550 attrs->split_subport_number = split_subport_number;
5552 attrs->switch_port = true;
5553 if (WARN_ON(switch_id_len > MAX_PHYS_ITEM_ID_LEN))
5554 switch_id_len = MAX_PHYS_ITEM_ID_LEN;
5555 memcpy(attrs->switch_id.id, switch_id, switch_id_len);
5556 attrs->switch_id.id_len = switch_id_len;
5558 attrs->switch_port = false;
5561 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
5563 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
5564 char *name, size_t len)
5566 struct devlink_port_attrs *attrs = &devlink_port->attrs;
5572 switch (attrs->flavour) {
5573 case DEVLINK_PORT_FLAVOUR_PHYSICAL:
5575 n = snprintf(name, len, "p%u", attrs->port_number);
5577 n = snprintf(name, len, "p%us%u", attrs->port_number,
5578 attrs->split_subport_number);
5580 case DEVLINK_PORT_FLAVOUR_CPU:
5581 case DEVLINK_PORT_FLAVOUR_DSA:
5582 /* As CPU and DSA ports do not have a netdevice associated
5583 * case should not ever happen.
5595 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
5596 u32 size, u16 ingress_pools_count,
5597 u16 egress_pools_count, u16 ingress_tc_count,
5598 u16 egress_tc_count)
5600 struct devlink_sb *devlink_sb;
5603 mutex_lock(&devlink->lock);
5604 if (devlink_sb_index_exists(devlink, sb_index)) {
5609 devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
5614 devlink_sb->index = sb_index;
5615 devlink_sb->size = size;
5616 devlink_sb->ingress_pools_count = ingress_pools_count;
5617 devlink_sb->egress_pools_count = egress_pools_count;
5618 devlink_sb->ingress_tc_count = ingress_tc_count;
5619 devlink_sb->egress_tc_count = egress_tc_count;
5620 list_add_tail(&devlink_sb->list, &devlink->sb_list);
5622 mutex_unlock(&devlink->lock);
5625 EXPORT_SYMBOL_GPL(devlink_sb_register);
5627 void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
5629 struct devlink_sb *devlink_sb;
5631 mutex_lock(&devlink->lock);
5632 devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
5633 WARN_ON(!devlink_sb);
5634 list_del(&devlink_sb->list);
5635 mutex_unlock(&devlink->lock);
5638 EXPORT_SYMBOL_GPL(devlink_sb_unregister);
5641 * devlink_dpipe_headers_register - register dpipe headers
5644 * @dpipe_headers: dpipe header array
5646 * Register the headers supported by hardware.
5648 int devlink_dpipe_headers_register(struct devlink *devlink,
5649 struct devlink_dpipe_headers *dpipe_headers)
5651 mutex_lock(&devlink->lock);
5652 devlink->dpipe_headers = dpipe_headers;
5653 mutex_unlock(&devlink->lock);
5656 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
5659 * devlink_dpipe_headers_unregister - unregister dpipe headers
5663 * Unregister the headers supported by hardware.
5665 void devlink_dpipe_headers_unregister(struct devlink *devlink)
5667 mutex_lock(&devlink->lock);
5668 devlink->dpipe_headers = NULL;
5669 mutex_unlock(&devlink->lock);
5671 EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
5674 * devlink_dpipe_table_counter_enabled - check if counter allocation
5677 * @table_name: tables name
5679 * Used by driver to check if counter allocation is required.
5680 * After counter allocation is turned on the table entries
5681 * are updated to include counter statistics.
5683 * After that point on the driver must respect the counter
5684 * state so that each entry added to the table is added
5687 bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
5688 const char *table_name)
5690 struct devlink_dpipe_table *table;
5694 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5698 enabled = table->counters_enabled;
5702 EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
5705 * devlink_dpipe_table_register - register dpipe table
5708 * @table_name: table name
5709 * @table_ops: table ops
5711 * @counter_control_extern: external control for counters
5713 int devlink_dpipe_table_register(struct devlink *devlink,
5714 const char *table_name,
5715 struct devlink_dpipe_table_ops *table_ops,
5716 void *priv, bool counter_control_extern)
5718 struct devlink_dpipe_table *table;
5720 if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
5723 if (WARN_ON(!table_ops->size_get))
5726 table = kzalloc(sizeof(*table), GFP_KERNEL);
5730 table->name = table_name;
5731 table->table_ops = table_ops;
5733 table->counter_control_extern = counter_control_extern;
5735 mutex_lock(&devlink->lock);
5736 list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
5737 mutex_unlock(&devlink->lock);
5740 EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
5743 * devlink_dpipe_table_unregister - unregister dpipe table
5746 * @table_name: table name
5748 void devlink_dpipe_table_unregister(struct devlink *devlink,
5749 const char *table_name)
5751 struct devlink_dpipe_table *table;
5753 mutex_lock(&devlink->lock);
5754 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5758 list_del_rcu(&table->list);
5759 mutex_unlock(&devlink->lock);
5760 kfree_rcu(table, rcu);
5763 mutex_unlock(&devlink->lock);
5765 EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
5768 * devlink_resource_register - devlink resource register
5771 * @resource_name: resource's name
5772 * @resource_size: resource's size
5773 * @resource_id: resource's id
5774 * @parent_resource_id: resource's parent id
5775 * @size_params: size parameters
5777 int devlink_resource_register(struct devlink *devlink,
5778 const char *resource_name,
5781 u64 parent_resource_id,
5782 const struct devlink_resource_size_params *size_params)
5784 struct devlink_resource *resource;
5785 struct list_head *resource_list;
5789 top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
5791 mutex_lock(&devlink->lock);
5792 resource = devlink_resource_find(devlink, NULL, resource_id);
5798 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
5804 if (top_hierarchy) {
5805 resource_list = &devlink->resource_list;
5807 struct devlink_resource *parent_resource;
5809 parent_resource = devlink_resource_find(devlink, NULL,
5810 parent_resource_id);
5811 if (parent_resource) {
5812 resource_list = &parent_resource->resource_list;
5813 resource->parent = parent_resource;
5821 resource->name = resource_name;
5822 resource->size = resource_size;
5823 resource->size_new = resource_size;
5824 resource->id = resource_id;
5825 resource->size_valid = true;
5826 memcpy(&resource->size_params, size_params,
5827 sizeof(resource->size_params));
5828 INIT_LIST_HEAD(&resource->resource_list);
5829 list_add_tail(&resource->list, resource_list);
5831 mutex_unlock(&devlink->lock);
5834 EXPORT_SYMBOL_GPL(devlink_resource_register);
5837 * devlink_resources_unregister - free all resources
5840 * @resource: resource
5842 void devlink_resources_unregister(struct devlink *devlink,
5843 struct devlink_resource *resource)
5845 struct devlink_resource *tmp, *child_resource;
5846 struct list_head *resource_list;
5849 resource_list = &resource->resource_list;
5851 resource_list = &devlink->resource_list;
5854 mutex_lock(&devlink->lock);
5856 list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
5857 devlink_resources_unregister(devlink, child_resource);
5858 list_del(&child_resource->list);
5859 kfree(child_resource);
5863 mutex_unlock(&devlink->lock);
5865 EXPORT_SYMBOL_GPL(devlink_resources_unregister);
5868 * devlink_resource_size_get - get and update size
5871 * @resource_id: the requested resource id
5872 * @p_resource_size: ptr to update
5874 int devlink_resource_size_get(struct devlink *devlink,
5876 u64 *p_resource_size)
5878 struct devlink_resource *resource;
5881 mutex_lock(&devlink->lock);
5882 resource = devlink_resource_find(devlink, NULL, resource_id);
5887 *p_resource_size = resource->size_new;
5888 resource->size = resource->size_new;
5890 mutex_unlock(&devlink->lock);
5893 EXPORT_SYMBOL_GPL(devlink_resource_size_get);
5896 * devlink_dpipe_table_resource_set - set the resource id
5899 * @table_name: table name
5900 * @resource_id: resource id
5901 * @resource_units: number of resource's units consumed per table's entry
5903 int devlink_dpipe_table_resource_set(struct devlink *devlink,
5904 const char *table_name, u64 resource_id,
5907 struct devlink_dpipe_table *table;
5910 mutex_lock(&devlink->lock);
5911 table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
5917 table->resource_id = resource_id;
5918 table->resource_units = resource_units;
5919 table->resource_valid = true;
5921 mutex_unlock(&devlink->lock);
5924 EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
5927 * devlink_resource_occ_get_register - register occupancy getter
5930 * @resource_id: resource id
5931 * @occ_get: occupancy getter callback
5932 * @occ_get_priv: occupancy getter callback priv
5934 void devlink_resource_occ_get_register(struct devlink *devlink,
5936 devlink_resource_occ_get_t *occ_get,
5939 struct devlink_resource *resource;
5941 mutex_lock(&devlink->lock);
5942 resource = devlink_resource_find(devlink, NULL, resource_id);
5943 if (WARN_ON(!resource))
5945 WARN_ON(resource->occ_get);
5947 resource->occ_get = occ_get;
5948 resource->occ_get_priv = occ_get_priv;
5950 mutex_unlock(&devlink->lock);
5952 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
5955 * devlink_resource_occ_get_unregister - unregister occupancy getter
5958 * @resource_id: resource id
5960 void devlink_resource_occ_get_unregister(struct devlink *devlink,
5963 struct devlink_resource *resource;
5965 mutex_lock(&devlink->lock);
5966 resource = devlink_resource_find(devlink, NULL, resource_id);
5967 if (WARN_ON(!resource))
5969 WARN_ON(!resource->occ_get);
5971 resource->occ_get = NULL;
5972 resource->occ_get_priv = NULL;
5974 mutex_unlock(&devlink->lock);
5976 EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
5978 static int devlink_param_verify(const struct devlink_param *param)
5980 if (!param || !param->name || !param->supported_cmodes)
5983 return devlink_param_generic_verify(param);
5985 return devlink_param_driver_verify(param);
5988 static int __devlink_params_register(struct devlink *devlink,
5989 unsigned int port_index,
5990 struct list_head *param_list,
5991 const struct devlink_param *params,
5992 size_t params_count,
5993 enum devlink_command reg_cmd,
5994 enum devlink_command unreg_cmd)
5996 const struct devlink_param *param = params;
6000 mutex_lock(&devlink->lock);
6001 for (i = 0; i < params_count; i++, param++) {
6002 err = devlink_param_verify(param);
6006 err = devlink_param_register_one(devlink, port_index,
6007 param_list, param, reg_cmd);
6012 mutex_unlock(&devlink->lock);
6018 for (param--; i > 0; i--, param--)
6019 devlink_param_unregister_one(devlink, port_index, param_list,
6022 mutex_unlock(&devlink->lock);
6026 static void __devlink_params_unregister(struct devlink *devlink,
6027 unsigned int port_index,
6028 struct list_head *param_list,
6029 const struct devlink_param *params,
6030 size_t params_count,
6031 enum devlink_command cmd)
6033 const struct devlink_param *param = params;
6036 mutex_lock(&devlink->lock);
6037 for (i = 0; i < params_count; i++, param++)
6038 devlink_param_unregister_one(devlink, 0, param_list, param,
6040 mutex_unlock(&devlink->lock);
6044 * devlink_params_register - register configuration parameters
6047 * @params: configuration parameters array
6048 * @params_count: number of parameters provided
6050 * Register the configuration parameters supported by the driver.
6052 int devlink_params_register(struct devlink *devlink,
6053 const struct devlink_param *params,
6054 size_t params_count)
6056 return __devlink_params_register(devlink, 0, &devlink->param_list,
6057 params, params_count,
6058 DEVLINK_CMD_PARAM_NEW,
6059 DEVLINK_CMD_PARAM_DEL);
6061 EXPORT_SYMBOL_GPL(devlink_params_register);
6064 * devlink_params_unregister - unregister configuration parameters
6066 * @params: configuration parameters to unregister
6067 * @params_count: number of parameters provided
6069 void devlink_params_unregister(struct devlink *devlink,
6070 const struct devlink_param *params,
6071 size_t params_count)
6073 return __devlink_params_unregister(devlink, 0, &devlink->param_list,
6074 params, params_count,
6075 DEVLINK_CMD_PARAM_DEL);
6077 EXPORT_SYMBOL_GPL(devlink_params_unregister);
6080 * devlink_params_publish - publish configuration parameters
6084 * Publish previously registered configuration parameters.
6086 void devlink_params_publish(struct devlink *devlink)
6088 struct devlink_param_item *param_item;
6090 list_for_each_entry(param_item, &devlink->param_list, list) {
6091 if (param_item->published)
6093 param_item->published = true;
6094 devlink_param_notify(devlink, 0, param_item,
6095 DEVLINK_CMD_PARAM_NEW);
6098 EXPORT_SYMBOL_GPL(devlink_params_publish);
6101 * devlink_params_unpublish - unpublish configuration parameters
6105 * Unpublish previously registered configuration parameters.
6107 void devlink_params_unpublish(struct devlink *devlink)
6109 struct devlink_param_item *param_item;
6111 list_for_each_entry(param_item, &devlink->param_list, list) {
6112 if (!param_item->published)
6114 param_item->published = false;
6115 devlink_param_notify(devlink, 0, param_item,
6116 DEVLINK_CMD_PARAM_DEL);
6119 EXPORT_SYMBOL_GPL(devlink_params_unpublish);
6122 * devlink_port_params_register - register port configuration parameters
6124 * @devlink_port: devlink port
6125 * @params: configuration parameters array
6126 * @params_count: number of parameters provided
6128 * Register the configuration parameters supported by the port.
6130 int devlink_port_params_register(struct devlink_port *devlink_port,
6131 const struct devlink_param *params,
6132 size_t params_count)
6134 return __devlink_params_register(devlink_port->devlink,
6135 devlink_port->index,
6136 &devlink_port->param_list, params,
6138 DEVLINK_CMD_PORT_PARAM_NEW,
6139 DEVLINK_CMD_PORT_PARAM_DEL);
6141 EXPORT_SYMBOL_GPL(devlink_port_params_register);
6144 * devlink_port_params_unregister - unregister port configuration
6147 * @devlink_port: devlink port
6148 * @params: configuration parameters array
6149 * @params_count: number of parameters provided
6151 void devlink_port_params_unregister(struct devlink_port *devlink_port,
6152 const struct devlink_param *params,
6153 size_t params_count)
6155 return __devlink_params_unregister(devlink_port->devlink,
6156 devlink_port->index,
6157 &devlink_port->param_list,
6158 params, params_count,
6159 DEVLINK_CMD_PORT_PARAM_DEL);
6161 EXPORT_SYMBOL_GPL(devlink_port_params_unregister);
6164 __devlink_param_driverinit_value_get(struct list_head *param_list, u32 param_id,
6165 union devlink_param_value *init_val)
6167 struct devlink_param_item *param_item;
6169 param_item = devlink_param_find_by_id(param_list, param_id);
6173 if (!param_item->driverinit_value_valid ||
6174 !devlink_param_cmode_is_supported(param_item->param,
6175 DEVLINK_PARAM_CMODE_DRIVERINIT))
6178 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6179 strcpy(init_val->vstr, param_item->driverinit_value.vstr);
6181 *init_val = param_item->driverinit_value;
6187 __devlink_param_driverinit_value_set(struct devlink *devlink,
6188 unsigned int port_index,
6189 struct list_head *param_list, u32 param_id,
6190 union devlink_param_value init_val,
6191 enum devlink_command cmd)
6193 struct devlink_param_item *param_item;
6195 param_item = devlink_param_find_by_id(param_list, param_id);
6199 if (!devlink_param_cmode_is_supported(param_item->param,
6200 DEVLINK_PARAM_CMODE_DRIVERINIT))
6203 if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
6204 strcpy(param_item->driverinit_value.vstr, init_val.vstr);
6206 param_item->driverinit_value = init_val;
6207 param_item->driverinit_value_valid = true;
6209 devlink_param_notify(devlink, port_index, param_item, cmd);
6214 * devlink_param_driverinit_value_get - get configuration parameter
6215 * value for driver initializing
6218 * @param_id: parameter ID
6219 * @init_val: value of parameter in driverinit configuration mode
6221 * This function should be used by the driver to get driverinit
6222 * configuration for initialization after reload command.
6224 int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
6225 union devlink_param_value *init_val)
6227 if (!devlink->ops->reload)
6230 return __devlink_param_driverinit_value_get(&devlink->param_list,
6231 param_id, init_val);
6233 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
6236 * devlink_param_driverinit_value_set - set value of configuration
6237 * parameter for driverinit
6238 * configuration mode
6241 * @param_id: parameter ID
6242 * @init_val: value of parameter to set for driverinit configuration mode
6244 * This function should be used by the driver to set driverinit
6245 * configuration mode default value.
6247 int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
6248 union devlink_param_value init_val)
6250 return __devlink_param_driverinit_value_set(devlink, 0,
6251 &devlink->param_list,
6253 DEVLINK_CMD_PARAM_NEW);
6255 EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
6258 * devlink_port_param_driverinit_value_get - get configuration parameter
6259 * value for driver initializing
6261 * @devlink_port: devlink_port
6262 * @param_id: parameter ID
6263 * @init_val: value of parameter in driverinit configuration mode
6265 * This function should be used by the driver to get driverinit
6266 * configuration for initialization after reload command.
6268 int devlink_port_param_driverinit_value_get(struct devlink_port *devlink_port,
6270 union devlink_param_value *init_val)
6272 struct devlink *devlink = devlink_port->devlink;
6274 if (!devlink->ops->reload)
6277 return __devlink_param_driverinit_value_get(&devlink_port->param_list,
6278 param_id, init_val);
6280 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_get);
6283 * devlink_port_param_driverinit_value_set - set value of configuration
6284 * parameter for driverinit
6285 * configuration mode
6287 * @devlink_port: devlink_port
6288 * @param_id: parameter ID
6289 * @init_val: value of parameter to set for driverinit configuration mode
6291 * This function should be used by the driver to set driverinit
6292 * configuration mode default value.
6294 int devlink_port_param_driverinit_value_set(struct devlink_port *devlink_port,
6296 union devlink_param_value init_val)
6298 return __devlink_param_driverinit_value_set(devlink_port->devlink,
6299 devlink_port->index,
6300 &devlink_port->param_list,
6302 DEVLINK_CMD_PORT_PARAM_NEW);
6304 EXPORT_SYMBOL_GPL(devlink_port_param_driverinit_value_set);
6307 * devlink_param_value_changed - notify devlink on a parameter's value
6308 * change. Should be called by the driver
6309 * right after the change.
6312 * @param_id: parameter ID
6314 * This function should be used by the driver to notify devlink on value
6315 * change, excluding driverinit configuration mode.
6316 * For driverinit configuration mode driver should use the function
6318 void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
6320 struct devlink_param_item *param_item;
6322 param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
6323 WARN_ON(!param_item);
6325 devlink_param_notify(devlink, 0, param_item, DEVLINK_CMD_PARAM_NEW);
6327 EXPORT_SYMBOL_GPL(devlink_param_value_changed);
6330 * devlink_port_param_value_changed - notify devlink on a parameter's value
6331 * change. Should be called by the driver
6332 * right after the change.
6334 * @devlink_port: devlink_port
6335 * @param_id: parameter ID
6337 * This function should be used by the driver to notify devlink on value
6338 * change, excluding driverinit configuration mode.
6339 * For driverinit configuration mode driver should use the function
6340 * devlink_port_param_driverinit_value_set() instead.
6342 void devlink_port_param_value_changed(struct devlink_port *devlink_port,
6345 struct devlink_param_item *param_item;
6347 param_item = devlink_param_find_by_id(&devlink_port->param_list,
6349 WARN_ON(!param_item);
6351 devlink_param_notify(devlink_port->devlink, devlink_port->index,
6352 param_item, DEVLINK_CMD_PORT_PARAM_NEW);
6354 EXPORT_SYMBOL_GPL(devlink_port_param_value_changed);
6357 * devlink_param_value_str_fill - Safely fill-up the string preventing
6358 * from overflow of the preallocated buffer
6360 * @dst_val: destination devlink_param_value
6361 * @src: source buffer
6363 void devlink_param_value_str_fill(union devlink_param_value *dst_val,
6368 len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
6369 WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
6371 EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
6374 * devlink_region_create - create a new address region
6377 * @region_name: region name
6378 * @region_max_snapshots: Maximum supported number of snapshots for region
6379 * @region_size: size of region
6381 struct devlink_region *devlink_region_create(struct devlink *devlink,
6382 const char *region_name,
6383 u32 region_max_snapshots,
6386 struct devlink_region *region;
6389 mutex_lock(&devlink->lock);
6391 if (devlink_region_get_by_name(devlink, region_name)) {
6396 region = kzalloc(sizeof(*region), GFP_KERNEL);
6402 region->devlink = devlink;
6403 region->max_snapshots = region_max_snapshots;
6404 region->name = region_name;
6405 region->size = region_size;
6406 INIT_LIST_HEAD(®ion->snapshot_list);
6407 list_add_tail(®ion->list, &devlink->region_list);
6408 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
6410 mutex_unlock(&devlink->lock);
6414 mutex_unlock(&devlink->lock);
6415 return ERR_PTR(err);
6417 EXPORT_SYMBOL_GPL(devlink_region_create);
6420 * devlink_region_destroy - destroy address region
6422 * @region: devlink region to destroy
6424 void devlink_region_destroy(struct devlink_region *region)
6426 struct devlink *devlink = region->devlink;
6427 struct devlink_snapshot *snapshot, *ts;
6429 mutex_lock(&devlink->lock);
6431 /* Free all snapshots of region */
6432 list_for_each_entry_safe(snapshot, ts, ®ion->snapshot_list, list)
6433 devlink_region_snapshot_del(snapshot);
6435 list_del(®ion->list);
6437 devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
6438 mutex_unlock(&devlink->lock);
6441 EXPORT_SYMBOL_GPL(devlink_region_destroy);
6444 * devlink_region_shapshot_id_get - get snapshot ID
6446 * This callback should be called when adding a new snapshot,
6447 * Driver should use the same id for multiple snapshots taken
6448 * on multiple regions at the same time/by the same trigger.
6452 u32 devlink_region_shapshot_id_get(struct devlink *devlink)
6456 mutex_lock(&devlink->lock);
6457 id = ++devlink->snapshot_id;
6458 mutex_unlock(&devlink->lock);
6462 EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
6465 * devlink_region_snapshot_create - create a new snapshot
6466 * This will add a new snapshot of a region. The snapshot
6467 * will be stored on the region struct and can be accessed
6468 * from devlink. This is useful for future analyses of snapshots.
6469 * Multiple snapshots can be created on a region.
6470 * The @snapshot_id should be obtained using the getter function.
6472 * @region: devlink region of the snapshot
6473 * @data_len: size of snapshot data
6474 * @data: snapshot data
6475 * @snapshot_id: snapshot id to be created
6476 * @data_destructor: pointer to destructor function to free data
6478 int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
6479 u8 *data, u32 snapshot_id,
6480 devlink_snapshot_data_dest_t *data_destructor)
6482 struct devlink *devlink = region->devlink;
6483 struct devlink_snapshot *snapshot;
6486 mutex_lock(&devlink->lock);
6488 /* check if region can hold one more snapshot */
6489 if (region->cur_snapshots == region->max_snapshots) {
6494 if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
6499 snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
6505 snapshot->id = snapshot_id;
6506 snapshot->region = region;
6507 snapshot->data = data;
6508 snapshot->data_len = data_len;
6509 snapshot->data_destructor = data_destructor;
6511 list_add_tail(&snapshot->list, ®ion->snapshot_list);
6513 region->cur_snapshots++;
6515 devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
6516 mutex_unlock(&devlink->lock);
6520 mutex_unlock(&devlink->lock);
6523 EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
6525 static void __devlink_compat_running_version(struct devlink *devlink,
6526 char *buf, size_t len)
6528 const struct nlattr *nlattr;
6529 struct devlink_info_req req;
6530 struct sk_buff *msg;
6533 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6538 err = devlink->ops->info_get(devlink, &req, NULL);
6542 nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) {
6543 const struct nlattr *kv;
6546 if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING)
6549 nla_for_each_nested(kv, nlattr, rem_kv) {
6550 if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE)
6553 strlcat(buf, nla_data(kv), len);
6554 strlcat(buf, " ", len);
6561 void devlink_compat_running_version(struct net_device *dev,
6562 char *buf, size_t len)
6564 struct devlink *devlink;
6569 devlink = netdev_to_devlink(dev);
6570 if (!devlink || !devlink->ops->info_get)
6573 mutex_lock(&devlink->lock);
6574 __devlink_compat_running_version(devlink, buf, len);
6575 mutex_unlock(&devlink->lock);
6582 int devlink_compat_flash_update(struct net_device *dev, const char *file_name)
6584 struct devlink *devlink;
6590 devlink = netdev_to_devlink(dev);
6591 if (!devlink || !devlink->ops->flash_update) {
6596 mutex_lock(&devlink->lock);
6597 ret = devlink->ops->flash_update(devlink, file_name, NULL, NULL);
6598 mutex_unlock(&devlink->lock);
6607 int devlink_compat_phys_port_name_get(struct net_device *dev,
6608 char *name, size_t len)
6610 struct devlink_port *devlink_port;
6612 /* RTNL mutex is held here which ensures that devlink_port
6613 * instance cannot disappear in the middle. No need to take
6614 * any devlink lock as only permanent values are accessed.
6618 devlink_port = netdev_to_devlink_port(dev);
6622 return __devlink_port_phys_port_name_get(devlink_port, name, len);
6625 int devlink_compat_switch_id_get(struct net_device *dev,
6626 struct netdev_phys_item_id *ppid)
6628 struct devlink_port *devlink_port;
6630 /* RTNL mutex is held here which ensures that devlink_port
6631 * instance cannot disappear in the middle. No need to take
6632 * any devlink lock as only permanent values are accessed.
6635 devlink_port = netdev_to_devlink_port(dev);
6636 if (!devlink_port || !devlink_port->attrs.switch_port)
6639 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));
6644 static int __init devlink_init(void)
6646 return genl_register_family(&devlink_nl_family);
6649 subsys_initcall(devlink_init);